Include keymap.h.
[bpt/emacs.git] / src / w32term.c
CommitLineData
e9e23e23 1/* Implementation of GUI terminal on the Microsoft W32 API.
c2cc16fa 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
9ef2e2cf 3 Free Software Foundation, Inc.
ee78dc32
GV
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ef2e2cf
JR
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1fb87c77 20Boston, MA 02111-1307, USA. */
ee78dc32 21
ee78dc32 22#include <config.h>
68c45bf0 23#include <signal.h>
ee78dc32 24#include <stdio.h>
791f420f 25#include <stdlib.h>
ee78dc32 26#include "lisp.h"
2091aeb2 27#include "charset.h"
ee78dc32
GV
28#include "blockinput.h"
29
e12ca9c2 30#include "w32heap.h"
689004fa 31#include "w32term.h"
791f420f 32#include "w32bdf.h"
12857dfd 33#include <shellapi.h>
ee78dc32
GV
34
35#include "systty.h"
36#include "systime.h"
f7737f5d 37#include "atimer.h"
8feddab4 38#include "keymap.h"
ee78dc32
GV
39
40#include <ctype.h>
41#include <errno.h>
42#include <setjmp.h>
43#include <sys/stat.h>
44
38006079 45#include "keyboard.h"
ee78dc32
GV
46#include "frame.h"
47#include "dispextern.h"
93ff4395 48#include "fontset.h"
ee78dc32
GV
49#include "termhooks.h"
50#include "termopts.h"
51#include "termchar.h"
52#include "gnu.h"
53#include "disptab.h"
54#include "buffer.h"
55#include "window.h"
ee78dc32 56#include "intervals.h"
791f420f 57#include "composite.h"
ef0e360f 58#include "coding.h"
ee78dc32 59
791f420f
JR
60#define abs(x) ((x) < 0 ? -(x) : (x))
61
62#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
63
64\f
65/* Bitmaps for truncated lines. */
66
67enum bitmap_type
68{
69 NO_BITMAP,
70 LEFT_TRUNCATION_BITMAP,
71 RIGHT_TRUNCATION_BITMAP,
72 OVERLAY_ARROW_BITMAP,
73 CONTINUED_LINE_BITMAP,
74 CONTINUATION_LINE_BITMAP,
75 ZV_LINE_BITMAP
76};
77
78/* Bitmaps are all unsigned short, as Windows requires bitmap data to
79 be Word aligned. For some reason they are horizontally reflected
80 compared to how they appear on X, so changes in xterm.c should be
81 reflected here. */
82
83/* Bitmap drawn to indicate lines not displaying text if
84 `indicate-empty-lines' is non-nil. */
85
86#define zv_width 8
87#define zv_height 8
88static unsigned short zv_bits[] = {
89 0x00, 0x00, 0x78, 0x78, 0x78, 0x78, 0x00, 0x00};
f7737f5d 90static HBITMAP zv_bmp;
791f420f
JR
91
92/* An arrow like this: `<-'. */
93
94#define left_width 8
95#define left_height 8
96static unsigned short left_bits[] = {
97 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
f7737f5d 98static HBITMAP left_bmp;
791f420f
JR
99
100/* Right truncation arrow bitmap `->'. */
101
102#define right_width 8
103#define right_height 8
104static unsigned short right_bits[] = {
105 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
f7737f5d 106static HBITMAP right_bmp;
791f420f
JR
107
108/* Marker for continued lines. */
109
110#define continued_width 8
111#define continued_height 8
112static unsigned short continued_bits[] = {
113 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
f7737f5d 114static HBITMAP continued_bmp;
791f420f
JR
115
116/* Marker for continuation lines. */
117
118#define continuation_width 8
119#define continuation_height 8
120static unsigned short continuation_bits[] = {
121 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
f7737f5d 122static HBITMAP continuation_bmp;
791f420f
JR
123
124/* Overlay arrow bitmap. */
125
126#if 0
127/* A bomb. */
128#define ov_width 8
129#define ov_height 8
130static unsigned short ov_bits[] = {
131 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
132#else
133/* A triangular arrow. */
134#define ov_width 8
135#define ov_height 8
136static unsigned short ov_bits[] = {
137 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
791f420f 138#endif
f7737f5d 139static HBITMAP ov_bmp;
791f420f
JR
140
141extern Lisp_Object Qhelp_echo;
142
143\f
5bf04520 144/* Non-nil means Emacs uses toolkit scroll bars. */
791f420f 145
5bf04520 146Lisp_Object Vx_toolkit_scroll_bars;
791f420f
JR
147
148/* If a string, w32_read_socket generates an event to display that string.
149 (The display is done in read_char.) */
150
151static Lisp_Object help_echo;
158cba56 152static Lisp_Object help_echo_window;
ec48c3a7
JR
153static Lisp_Object help_echo_object;
154static int help_echo_pos;
791f420f
JR
155
156/* Temporary variable for w32_read_socket. */
157
158static Lisp_Object previous_help_echo;
159
160/* Non-zero means that a HELP_EVENT has been generated since Emacs
161 start. */
162
163static int any_help_event_p;
164
165/* Non-zero means draw block and hollow cursor as wide as the glyph
166 under it. For example, if a block cursor is over a tab, it will be
167 drawn as wide as that tab on the display. */
168
169int x_stretch_cursor_p;
170
e7efd97e
GV
171extern unsigned int msh_mousewheel;
172
ee78dc32
GV
173extern void free_frame_menubar ();
174
93ff4395
JR
175extern void w32_menu_display_help (HMENU menu, UINT menu_item, UINT flags);
176
9ef2e2cf
JR
177extern int w32_codepage_for_font (char *fontname);
178
82f9d565
AI
179extern glyph_metric *w32_BDF_TextMetric(bdffont *fontp,
180 unsigned char *text, int dim);
52cf03a1
GV
181extern Lisp_Object Vwindow_system;
182
ee78dc32
GV
183#define x_any_window_to_frame x_window_to_frame
184#define x_top_window_to_frame x_window_to_frame
185
186\f
fbd6baed
GV
187/* This is display since w32 does not support multiple ones. */
188struct w32_display_info one_w32_display_info;
8c3b00cb 189struct w32_display_info *x_display_list;
ee78dc32
GV
190
191/* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
fbd6baed 192 one for each element of w32_display_list and in the same order.
ee78dc32
GV
193 NAME is the name of the frame.
194 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
fbd6baed 195Lisp_Object w32_display_name_list;
ee78dc32
GV
196
197/* Frame being updated by update_frame. This is declared in term.c.
198 This is set by update_begin and looked at by all the
fbd6baed 199 w32 functions. It is zero while not inside an update.
791f420f 200 In that case, the w32 functions assume that `SELECTED_FRAME ()'
ee78dc32
GV
201 is the frame to apply to. */
202extern struct frame *updating_frame;
203
204/* This is a frame waiting to be autoraised, within w32_read_socket. */
205struct frame *pending_autoraise_frame;
206
ee78dc32 207/* Nominal cursor position -- where to draw output.
791f420f
JR
208 HPOS and VPOS are window relative glyph matrix coordinates.
209 X and Y are window relative pixel coordinates. */
ee78dc32 210
791f420f 211struct cursor_pos output_cursor;
ee78dc32 212
484fa2c1 213/* Flag to enable Unicode output in case users wish to use programs
cabb23bc
GV
214 like Twinbridge on '95 rather than installed system level support
215 for Far East languages. */
484fa2c1 216int w32_enable_unicode_output;
cabb23bc 217
e9e23e23
GV
218DWORD dwWindowsThreadId = 0;
219HANDLE hWindowsThread = NULL;
ee78dc32
GV
220DWORD dwMainThreadId = 0;
221HANDLE hMainThread = NULL;
222
689004fa
GV
223#ifndef SIF_ALL
224/* These definitions are new with Windows 95. */
225#define SIF_RANGE 0x0001
226#define SIF_PAGE 0x0002
227#define SIF_POS 0x0004
228#define SIF_DISABLENOSCROLL 0x0008
229#define SIF_TRACKPOS 0x0010
230#define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
231
232typedef struct tagSCROLLINFO
233{
234 UINT cbSize;
235 UINT fMask;
236 int nMin;
237 int nMax;
238 UINT nPage;
239 int nPos;
240 int nTrackPos;
241} SCROLLINFO, FAR *LPSCROLLINFO;
242typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
243#endif /* SIF_ALL */
244
245/* Dynamic linking to new proportional scroll bar functions. */
246int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
247BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
248
249int vertical_scroll_bar_min_handle;
250int vertical_scroll_bar_top_border;
251int vertical_scroll_bar_bottom_border;
252
253int last_scroll_bar_drag_pos;
254
ee78dc32
GV
255/* Mouse movement. */
256
257/* Where the mouse was last time we reported a mouse event. */
9ef2e2cf 258
791f420f 259FRAME_PTR last_mouse_frame;
ee78dc32 260static RECT last_mouse_glyph;
791f420f 261static Lisp_Object last_mouse_press_frame;
ee78dc32 262
fbd6baed 263Lisp_Object Vw32_num_mouse_buttons;
52cf03a1 264
fbd6baed 265Lisp_Object Vw32_swap_mouse_buttons;
52cf03a1 266
689004fa
GV
267/* Control whether x_raise_frame also sets input focus. */
268Lisp_Object Vw32_grab_focus_on_raise;
269
270/* Control whether Caps Lock affects non-ascii characters. */
271Lisp_Object Vw32_capslock_is_shiftlock;
272
ef0e360f
GV
273/* Control whether right-alt and left-ctrl should be recognized as AltGr. */
274Lisp_Object Vw32_recognize_altgr;
275
ee78dc32
GV
276/* The scroll bar in which the last motion event occurred.
277
278 If the last motion event occurred in a scroll bar, we set this
fbd6baed 279 so w32_mouse_position can know whether to report a scroll bar motion or
ee78dc32
GV
280 an ordinary motion.
281
282 If the last motion event didn't occur in a scroll bar, we set this
fbd6baed 283 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
9ef2e2cf
JR
284static Lisp_Object last_mouse_scroll_bar;
285static int last_mouse_scroll_bar_pos;
ee78dc32 286
fbd6baed 287/* This is a hack. We would really prefer that w32_mouse_position would
ee78dc32 288 return the time associated with the position it returns, but there
9ef2e2cf 289 doesn't seem to be any way to wrest the time-stamp from the server
ee78dc32
GV
290 along with the position query. So, we just keep track of the time
291 of the last movement we received, and return that in hopes that
292 it's somewhat accurate. */
ee78dc32 293
9ef2e2cf
JR
294static Time last_mouse_movement_time;
295
296/* Incremented by w32_read_socket whenever it really tries to read
297 events. */
a1b9438c 298
ee78dc32
GV
299#ifdef __STDC__
300static int volatile input_signal_count;
301#else
302static int input_signal_count;
303#endif
304
305extern Lisp_Object Vcommand_line_args, Vsystem_name;
306
307extern Lisp_Object Qface, Qmouse_face;
308
38006079 309#ifndef USE_CRT_DLL
ee78dc32 310extern int errno;
38006079 311#endif
ee78dc32
GV
312
313/* A mask of extra modifier bits to put into every keyboard char. */
9ef2e2cf 314
ee78dc32
GV
315extern int extra_keyboard_modifiers;
316
791f420f
JR
317/* Enumeration for overriding/changing the face to use for drawing
318 glyphs in x_draw_glyphs. */
319
320enum draw_glyphs_face
321{
322 DRAW_NORMAL_TEXT,
323 DRAW_INVERSE_VIDEO,
324 DRAW_CURSOR,
325 DRAW_MOUSE_FACE,
326 DRAW_IMAGE_RAISED,
327 DRAW_IMAGE_SUNKEN
328};
329
38006079 330static void x_update_window_end P_ ((struct window *, int, int));
791f420f
JR
331static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
332void w32_delete_display P_ ((struct w32_display_info *));
333static int fast_find_position P_ ((struct window *, int, int *, int *,
334 int *, int *));
335static void set_output_cursor P_ ((struct cursor_pos *));
336static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
337 int *, int *, int *));
338static void note_mode_line_highlight P_ ((struct window *, int, int));
791f420f
JR
339static void note_mouse_highlight P_ ((struct frame *, int, int));
340static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
341static void w32_handle_tool_bar_click P_ ((struct frame *,
342 struct input_event *));
343static void show_mouse_face P_ ((struct w32_display_info *,
344 enum draw_glyphs_face));
345void clear_mouse_face P_ ((struct w32_display_info *));
346
347void x_lower_frame P_ ((struct frame *));
348void x_scroll_bar_clear P_ ((struct frame *));
349void x_wm_set_size_hint P_ ((struct frame *, long, int));
350void x_raise_frame P_ ((struct frame *));
351void x_set_window_size P_ ((struct frame *, int, int, int));
352void x_wm_set_window_state P_ ((struct frame *, int));
353void x_wm_set_icon_pixmap P_ ((struct frame *, int));
354void w32_initialize P_ ((void));
355static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
356int x_compute_min_glyph_bounds P_ ((struct frame *));
357static void x_draw_phys_cursor_glyph P_ ((struct window *,
358 struct glyph_row *,
359 enum draw_glyphs_face));
360static void x_update_end P_ ((struct frame *));
361static void w32_frame_up_to_date P_ ((struct frame *));
362static void w32_reassert_line_highlight P_ ((int, int));
363static void x_change_line_highlight P_ ((int, int, int, int));
364static void w32_set_terminal_modes P_ ((void));
365static void w32_reset_terminal_modes P_ ((void));
366static void w32_cursor_to P_ ((int, int, int, int));
367static void x_write_glyphs P_ ((struct glyph *, int));
368static void x_clear_end_of_line P_ ((int));
369static void x_clear_frame P_ ((void));
370static void x_clear_cursor P_ ((struct window *));
371static void frame_highlight P_ ((struct frame *));
372static void frame_unhighlight P_ ((struct frame *));
373static void w32_new_focus_frame P_ ((struct w32_display_info *,
374 struct frame *));
375static void w32_frame_rehighlight P_ ((struct frame *));
376static void x_frame_rehighlight P_ ((struct w32_display_info *));
377static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
9ef2e2cf 378static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
791f420f
JR
379static void expose_frame P_ ((struct frame *, int, int, int, int));
380static void expose_window_tree P_ ((struct window *, RECT *));
381static void expose_window P_ ((struct window *, RECT *));
382static void expose_area P_ ((struct window *, struct glyph_row *,
383 RECT *, enum glyph_row_area));
384static void expose_line P_ ((struct window *, struct glyph_row *,
385 RECT *));
386void x_update_cursor P_ ((struct frame *, int));
387static void x_update_cursor_in_window_tree P_ ((struct window *, int));
388static void x_update_window_cursor P_ ((struct window *, int));
389static void x_erase_phys_cursor P_ ((struct window *));
390void x_display_cursor P_ ((struct window *w, int, int, int, int, int));
391void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
392static void w32_draw_bitmap P_ ((struct window *, HDC hdc, struct glyph_row *,
393 enum bitmap_type));
c2cc16fa
JR
394static void w32_clip_to_row P_ ((struct window *, struct glyph_row *,
395 HDC, int));
791f420f
JR
396static int x_phys_cursor_in_rect_p P_ ((struct window *, RECT *));
397static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
398static void note_overwritten_text_cursor P_ ((struct window *, int, int));
791f420f 399
ee78dc32
GV
400static Lisp_Object Qvendor_specific_keysyms;
401
ee78dc32 402\f
9ef2e2cf
JR
403/***********************************************************************
404 Debugging
405 ***********************************************************************/
406
ee78dc32 407#if 0
9ef2e2cf
JR
408
409/* This is a function useful for recording debugging information about
410 the sequence of occurrences in this file. */
ee78dc32
GV
411
412struct record
413{
414 char *locus;
415 int type;
416};
417
418struct record event_record[100];
419
420int event_record_index;
421
422record_event (locus, type)
423 char *locus;
424 int type;
425{
426 if (event_record_index == sizeof (event_record) / sizeof (struct record))
427 event_record_index = 0;
428
429 event_record[event_record_index].locus = locus;
430 event_record[event_record_index].type = type;
431 event_record_index++;
432}
433
434#endif /* 0 */
435\f
791f420f
JR
436
437void XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
438 XGCValues *xgcv)
439{
440 if (mask & GCForeground)
441 gc->foreground = xgcv->foreground;
442 if (mask & GCBackground)
443 gc->background = xgcv->background;
444 if (mask & GCFont)
445 gc->font = xgcv->font;
446}
447
448XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
449 XGCValues *xgcv)
450{
451 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
452 bzero (gc, sizeof (XGCValues));
453
454 XChangeGC (ignore, gc, mask, xgcv);
455
456 return gc;
457}
458
459void XGetGCValues (void* ignore, XGCValues *gc,
460 unsigned long mask, XGCValues *xgcv)
461{
462 XChangeGC (ignore, xgcv, mask, gc);
463}
464
791f420f
JR
465static void
466w32_set_clip_rectangle (HDC hdc, RECT *rect)
467{
468 if (rect)
469 {
470 HRGN clip_region = CreateRectRgnIndirect (rect);
471 SelectClipRgn (hdc, clip_region);
472 DeleteObject (clip_region);
473 }
474 else
475 SelectClipRgn (hdc, NULL);
476}
477
791f420f
JR
478
479/* Draw a hollow rectangle at the specified position. */
480void
481w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
482 int width, int height)
483{
484 HBRUSH hb, oldhb;
485 HPEN hp, oldhp;
486
487 hb = CreateSolidBrush (gc->background);
488 hp = CreatePen (PS_SOLID, 0, gc->foreground);
489 oldhb = SelectObject (hdc, hb);
490 oldhp = SelectObject (hdc, hp);
491
492 Rectangle (hdc, x, y, x + width, y + height);
493
494 SelectObject (hdc, oldhb);
495 SelectObject (hdc, oldhp);
496 DeleteObject (hb);
497 DeleteObject (hp);
498}
499
500/* Draw a filled rectangle at the specified position. */
ee78dc32 501void
00fe468b 502w32_fill_rect (f, hdc, pix, lprect)
ee78dc32 503 FRAME_PTR f;
00fe468b 504 HDC hdc;
ee78dc32
GV
505 COLORREF pix;
506 RECT * lprect;
507{
ee78dc32 508 HBRUSH hb;
791f420f 509
ee78dc32 510 hb = CreateSolidBrush (pix);
ee78dc32 511 FillRect (hdc, lprect, hb);
ee78dc32 512 DeleteObject (hb);
ee78dc32
GV
513}
514
515void
fbd6baed 516w32_clear_window (f)
ee78dc32
GV
517 FRAME_PTR f;
518{
519 RECT rect;
00fe468b 520 HDC hdc = get_frame_dc (f);
52cf03a1 521
e4a52412
JR
522 /* Under certain conditions, this can be called at startup with
523 a console frame pointer before the GUI frame is created. An HDC
524 of 0 indicates this. */
525 if (hdc)
526 {
527 GetClientRect (FRAME_W32_WINDOW (f), &rect);
528 w32_clear_rect (f, hdc, &rect);
529 }
530
00fe468b 531 release_frame_dc (f, hdc);
ee78dc32
GV
532}
533
534\f
791f420f
JR
535/***********************************************************************
536 Starting and ending an update
537 ***********************************************************************/
538
539/* Start an update of frame F. This function is installed as a hook
540 for update_begin, i.e. it is called when update_begin is called.
541 This function is called prior to calls to x_update_window_begin for
99012074 542 each window being updated. */
ee78dc32 543
96214669 544static void
791f420f 545x_update_begin (f)
ee78dc32
GV
546 struct frame *f;
547{
99012074
AI
548 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
549
7e6ac5b9
AI
550 if (! FRAME_W32_P (f))
551 return;
552
99012074
AI
553 /* Regenerate display palette before drawing if list of requested
554 colors has changed. */
555 if (display_info->regen_palette)
556 {
557 w32_regenerate_palette (f);
558 display_info->regen_palette = FALSE;
559 }
791f420f 560}
ee78dc32 561
791f420f
JR
562
563/* Start update of window W. Set the global variable updated_window
564 to the window being updated and set output_cursor to the cursor
565 position of W. */
566
567static void
568x_update_window_begin (w)
569 struct window *w;
570{
571 struct frame *f = XFRAME (WINDOW_FRAME (w));
572 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
573
574 updated_window = w;
575 set_output_cursor (&w->cursor);
ee78dc32
GV
576
577 BLOCK_INPUT;
578
791f420f 579 if (f == display_info->mouse_face_mouse_frame)
ee78dc32
GV
580 {
581 /* Don't do highlighting for mouse motion during the update. */
791f420f 582 display_info->mouse_face_defer = 1;
ee78dc32 583
9ef2e2cf
JR
584 /* If F needs to be redrawn, simply forget about any prior mouse
585 highlighting. */
ee78dc32 586 if (FRAME_GARBAGED_P (f))
791f420f
JR
587 display_info->mouse_face_window = Qnil;
588
ec48c3a7
JR
589#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
590 their mouse_face_p flag set, which means that they are always
591 unequal to rows in a desired matrix which never have that
592 flag set. So, rows containing mouse-face glyphs are never
593 scrolled, and we don't have to switch the mouse highlight off
594 here to prevent it from being scrolled. */
595
791f420f
JR
596 /* Can we tell that this update does not affect the window
597 where the mouse highlight is? If so, no need to turn off.
598 Likewise, don't do anything if the frame is garbaged;
599 in that case, the frame's current matrix that we would use
600 is all wrong, and we will redisplay that line anyway. */
601 if (!NILP (display_info->mouse_face_window)
602 && w == XWINDOW (display_info->mouse_face_window))
ee78dc32 603 {
791f420f 604 int i;
ee78dc32 605
791f420f
JR
606 for (i = 0; i < w->desired_matrix->nrows; ++i)
607 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
ee78dc32
GV
608 break;
609
791f420f
JR
610 if (i < w->desired_matrix->nrows)
611 clear_mouse_face (display_info);
ee78dc32 612 }
ec48c3a7 613#endif /* 0 */
ee78dc32
GV
614 }
615
616 UNBLOCK_INPUT;
617}
618
791f420f
JR
619
620/* Draw a vertical window border to the right of window W if W doesn't
621 have vertical scroll bars. */
622
96214669 623static void
791f420f
JR
624x_draw_vertical_border (w)
625 struct window *w;
ee78dc32 626{
791f420f
JR
627 struct frame *f = XFRAME (WINDOW_FRAME (w));
628
629 /* Redraw borders between horizontally adjacent windows. Don't
630 do it for frames with vertical scroll bars because either the
631 right scroll bar of a window, or the left scroll bar of its
632 neighbor will suffice as a border. */
633 if (!WINDOW_RIGHTMOST_P (w)
634 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
635 {
636 RECT r;
00fe468b 637 HDC hdc;
ee78dc32 638
791f420f
JR
639 window_box_edges (w, -1, &r.left, &r.top, &r.right, &r.bottom);
640 r.left = r.right + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
641 r.right = r.left + 1;
642 r.bottom -= 1;
ee78dc32 643
00fe468b 644 hdc = get_frame_dc (f);
e4a52412 645 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
00fe468b 646 release_frame_dc (f, hdc);
791f420f
JR
647 }
648}
ee78dc32 649
791f420f 650
ec48c3a7
JR
651/* End update of window W (which is equal to updated_window).
652
653 Draw vertical borders between horizontally adjacent windows, and
654 display W's cursor if CURSOR_ON_P is non-zero.
655
656 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
657 glyphs in mouse-face were overwritten. In that case we have to
658 make sure that the mouse-highlight is properly redrawn.
659
660 W may be a menu bar pseudo-window in case we don't have X toolkit
661 support. Such windows don't have a cursor, so don't display it
662 here. */
791f420f
JR
663
664static void
ec48c3a7 665x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
791f420f 666 struct window *w;
ec48c3a7 667 int cursor_on_p, mouse_face_overwritten_p;
791f420f
JR
668{
669 if (!w->pseudo_window_p)
670 {
ec48c3a7
JR
671 struct w32_display_info *dpyinfo
672 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
673
791f420f 674 BLOCK_INPUT;
ec48c3a7
JR
675
676 /* If a row with mouse-face was overwritten, arrange for
677 XTframe_up_to_date to redisplay the mouse highlight. */
678 if (mouse_face_overwritten_p)
679 {
680 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
681 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
682 dpyinfo->mouse_face_window = Qnil;
683 }
684
791f420f
JR
685 if (cursor_on_p)
686 x_display_and_set_cursor (w, 1, output_cursor.hpos,
687 output_cursor.vpos,
688 output_cursor.x, output_cursor.y);
c2cc16fa 689
791f420f
JR
690 x_draw_vertical_border (w);
691 UNBLOCK_INPUT;
692 }
693
694 updated_window = NULL;
695}
696
9ef2e2cf
JR
697
698/* End update of frame F. This function is installed as a hook in
699 update_end. */
700
791f420f
JR
701static void
702x_update_end (f)
703 struct frame *f;
704{
7e6ac5b9
AI
705 if (! FRAME_W32_P (f))
706 return;
707
791f420f
JR
708 /* Mouse highlight may be displayed again. */
709 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
ee78dc32
GV
710}
711
9ef2e2cf 712
791f420f
JR
713/* This function is called from various places in xdisp.c whenever a
714 complete update has been performed. The global variable
715 updated_window is not available here. */
ee78dc32 716
96214669 717static void
fbd6baed 718w32_frame_up_to_date (f)
791f420f 719 struct frame *f;
ee78dc32 720{
791f420f 721 if (FRAME_W32_P (f))
ee78dc32 722 {
791f420f
JR
723 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
724 if (dpyinfo->mouse_face_deferred_gc
725 || f == dpyinfo->mouse_face_mouse_frame)
726 {
727 BLOCK_INPUT;
728 if (dpyinfo->mouse_face_mouse_frame)
729 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
730 dpyinfo->mouse_face_mouse_x,
731 dpyinfo->mouse_face_mouse_y);
732 dpyinfo->mouse_face_deferred_gc = 0;
733 UNBLOCK_INPUT;
734 }
ee78dc32
GV
735 }
736}
791f420f
JR
737
738
739/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
740 arrow bitmaps, or clear the areas where they would be displayed
741 before DESIRED_ROW is made current. The window being updated is
742 found in updated_window. This function It is called from
743 update_window_line only if it is known that there are differences
744 between bitmaps to be drawn between current row and DESIRED_ROW. */
745
746static void
747x_after_update_window_line (desired_row)
748 struct glyph_row *desired_row;
749{
750 struct window *w = updated_window;
751
752 xassert (w);
753
754 if (!desired_row->mode_line_p && !w->pseudo_window_p)
755 {
49be9f70
JR
756 struct frame *f;
757 int width;
758
791f420f
JR
759 BLOCK_INPUT;
760 x_draw_row_bitmaps (w, desired_row);
761
762 /* When a window has disappeared, make sure that no rest of
763 full-width rows stays visible in the internal border. */
49be9f70
JR
764 if (windows_or_buffers_changed
765 && (f = XFRAME (w->frame),
766 width = FRAME_INTERNAL_BORDER_WIDTH (f),
767 width != 0))
791f420f 768 {
791f420f
JR
769 int height = desired_row->visible_height;
770 int x = (window_box_right (w, -1)
771 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
772 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
00fe468b 773 HDC hdc = get_frame_dc (f);
791f420f 774
00fe468b
JR
775 w32_clear_area (f, hdc, x, y, width, height);
776 release_frame_dc (f, hdc);
791f420f
JR
777 }
778
779 UNBLOCK_INPUT;
780 }
781}
782
783
784/* Draw the bitmap WHICH in one of the areas to the left or right of
785 window W. ROW is the glyph row for which to display the bitmap; it
786 determines the vertical position at which the bitmap has to be
787 drawn. */
788
789static void
00fe468b 790w32_draw_bitmap (w, hdc, row, which)
791f420f 791 struct window *w;
00fe468b 792 HDC hdc;
791f420f
JR
793 struct glyph_row *row;
794 enum bitmap_type which;
795{
796 struct frame *f = XFRAME (WINDOW_FRAME (w));
797 Window window = FRAME_W32_WINDOW (f);
791f420f
JR
798 HDC compat_hdc;
799 int x, y, wd, h, dy;
791f420f 800 HBITMAP pixmap;
791f420f
JR
801 HANDLE horig_obj;
802 struct face *face;
803
804 /* Must clip because of partially visible lines. */
805 w32_clip_to_row (w, row, hdc, 1);
806
807 switch (which)
808 {
809 case LEFT_TRUNCATION_BITMAP:
810 wd = left_width;
811 h = left_height;
f7737f5d 812 pixmap = left_bmp;
791f420f
JR
813 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
814 - wd
815 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
816 break;
817
818 case OVERLAY_ARROW_BITMAP:
819 wd = ov_width;
820 h = ov_height;
f7737f5d 821 pixmap = ov_bmp;
791f420f
JR
822 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
823 - wd
824 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
825 break;
826
827 case RIGHT_TRUNCATION_BITMAP:
828 wd = right_width;
829 h = right_height;
f7737f5d 830 pixmap = right_bmp;
791f420f
JR
831 x = window_box_right (w, -1);
832 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
833 break;
834
835 case CONTINUED_LINE_BITMAP:
836 wd = continued_width;
837 h = continued_height;
f7737f5d 838 pixmap = continued_bmp;
791f420f
JR
839 x = window_box_right (w, -1);
840 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
841 break;
842
843 case CONTINUATION_LINE_BITMAP:
844 wd = continuation_width;
845 h = continuation_height;
f7737f5d 846 pixmap = continuation_bmp;
791f420f
JR
847 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
848 - wd
849 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
850 break;
851
852 case ZV_LINE_BITMAP:
853 wd = zv_width;
854 h = zv_height;
f7737f5d 855 pixmap = zv_bmp;
791f420f
JR
856 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
857 - wd
858 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
859 break;
860
861 default:
862 abort ();
863 }
864
865 /* Convert to frame coordinates. Set dy to the offset in the row to
866 start drawing the bitmap. */
867 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
868 dy = (row->height - h) / 2;
869
f7737f5d 870 /* Draw the bitmap. */
791f420f 871 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
791f420f
JR
872
873 compat_hdc = CreateCompatibleDC (hdc);
874 SaveDC (hdc);
9436cdf9 875
791f420f 876 horig_obj = SelectObject (compat_hdc, pixmap);
9436cdf9
JR
877 SetTextColor (hdc, face->background);
878 SetBkColor (hdc, face->foreground);
879
791f420f 880 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, SRCCOPY);
9436cdf9 881
791f420f 882 SelectObject (compat_hdc, horig_obj);
791f420f
JR
883 DeleteDC (compat_hdc);
884 RestoreDC (hdc, -1);
791f420f
JR
885}
886
887
888/* Draw flags bitmaps for glyph row ROW on window W. Call this
889 function with input blocked. */
890
891static void
892x_draw_row_bitmaps (w, row)
893 struct window *w;
894 struct glyph_row *row;
895{
896 struct frame *f = XFRAME (w->frame);
897 enum bitmap_type bitmap;
898 struct face *face;
899 int header_line_height = -1;
fccb8288 900 HDC hdc;
791f420f
JR
901
902 xassert (interrupt_input_blocked);
903
904 /* If row is completely invisible, because of vscrolling, we
905 don't have to draw anything. */
906 if (row->visible_height <= 0)
907 return;
908
909 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
910 PREPARE_FACE_FOR_DISPLAY (f, face);
911
912 /* Decide which bitmap to draw at the left side. */
913 if (row->overlay_arrow_p)
914 bitmap = OVERLAY_ARROW_BITMAP;
915 else if (row->truncated_on_left_p)
916 bitmap = LEFT_TRUNCATION_BITMAP;
917 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
918 bitmap = CONTINUATION_LINE_BITMAP;
919 else if (row->indicate_empty_line_p)
920 bitmap = ZV_LINE_BITMAP;
921 else
922 bitmap = NO_BITMAP;
923
fccb8288
EZ
924 hdc = get_frame_dc (f);
925
791f420f
JR
926 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
927 the flags area. */
928 if (bitmap == NO_BITMAP
929 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
930 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
931 {
932 /* If W has a vertical border to its left, don't draw over it. */
933 int border = ((XFASTINT (w->left) > 0
934 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
935 ? 1 : 0);
936 int left = window_box_left (w, -1);
937
938 if (header_line_height < 0)
939 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
940
941 w32_fill_area (f, hdc, face->background,
942 left - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) + border,
943 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
944 row->y)),
945 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
946 row->visible_height);
947 }
948
949 /* Draw the left bitmap. */
950 if (bitmap != NO_BITMAP)
951 w32_draw_bitmap (w, hdc, row, bitmap);
952
953 /* Decide which bitmap to draw at the right side. */
954 if (row->truncated_on_right_p)
955 bitmap = RIGHT_TRUNCATION_BITMAP;
956 else if (row->continued_p)
957 bitmap = CONTINUED_LINE_BITMAP;
958 else
959 bitmap = NO_BITMAP;
960
961 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
962 the flags area. */
963 if (bitmap == NO_BITMAP
964 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
965 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
966 {
967 int right = window_box_right (w, -1);
968
969 if (header_line_height < 0)
970 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
971
972 w32_fill_area (f, hdc, face->background,
973 right,
974 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
975 row->y)),
976 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
977 row->visible_height);
978 }
979
980 /* Draw the right bitmap. */
981 if (bitmap != NO_BITMAP)
982 w32_draw_bitmap (w, hdc, row, bitmap);
983
984 release_frame_dc (f, hdc);
985}
986
ee78dc32 987\f
791f420f
JR
988/***********************************************************************
989 Line Highlighting
990 ***********************************************************************/
991
992/* External interface to control of standout mode. Not used for W32
993 frames. Aborts when called. */
ee78dc32 994
96214669 995static void
fbd6baed 996w32_reassert_line_highlight (new, vpos)
ee78dc32
GV
997 int new, vpos;
998{
7e6ac5b9
AI
999 struct frame *f;
1000
1001 if (updating_frame)
1002 f = updating_frame;
1003 else
1004 f = SELECTED_FRAME ();
1005
1006 if (! FRAME_W32_P (f))
1007 return;
1008
791f420f 1009 abort ();
ee78dc32
GV
1010}
1011
c2cc16fa 1012
791f420f
JR
1013/* Call this when about to modify line at position VPOS and change
1014 whether it is highlighted. Not used for W32 frames. Aborts when
1015 called. */
ee78dc32 1016
96214669 1017static void
791f420f
JR
1018x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1019 int new_highlight, vpos, y, first_unused_hpos;
ee78dc32 1020{
7e6ac5b9
AI
1021 struct frame *f;
1022
1023 if (updating_frame)
1024 f = updating_frame;
1025 else
1026 f = SELECTED_FRAME ();
1027
1028 if (! FRAME_W32_P (f))
1029 return;
1030
791f420f 1031 abort ();
ee78dc32
GV
1032}
1033
c2cc16fa 1034
9ef2e2cf
JR
1035/* This is called when starting Emacs and when restarting after
1036 suspend. When starting Emacs, no window is mapped. And nothing
1037 must be done to Emacs's own window if it is suspended (though that
1038 rarely happens). */
ee78dc32 1039
96214669
GV
1040static void
1041w32_set_terminal_modes (void)
ee78dc32
GV
1042{
1043}
1044
9ef2e2cf
JR
1045/* This is called when exiting or suspending Emacs. Exiting will make
1046 the W32 windows go away, and suspending requires no action. */
ee78dc32 1047
96214669
GV
1048static void
1049w32_reset_terminal_modes (void)
ee78dc32
GV
1050{
1051}
791f420f 1052
9ef2e2cf 1053
ee78dc32 1054\f
791f420f
JR
1055/***********************************************************************
1056 Output Cursor
1057 ***********************************************************************/
1058
1059/* Set the global variable output_cursor to CURSOR. All cursor
1060 positions are relative to updated_window. */
9ef2e2cf 1061
791f420f
JR
1062static void
1063set_output_cursor (cursor)
1064 struct cursor_pos *cursor;
1065{
1066 output_cursor.hpos = cursor->hpos;
1067 output_cursor.vpos = cursor->vpos;
1068 output_cursor.x = cursor->x;
1069 output_cursor.y = cursor->y;
1070}
1071
1072
1073/* Set a nominal cursor position.
1074
1075 HPOS and VPOS are column/row positions in a window glyph matrix. X
1076 and Y are window text area relative pixel positions.
1077
1078 If this is done during an update, updated_window will contain the
1079 window that is being updated and the position is the future output
1080 cursor position for that window. If updated_window is null, use
1081 selected_window and display the cursor at the given position. */
ee78dc32 1082
96214669 1083static void
791f420f
JR
1084w32_cursor_to (vpos, hpos, y, x)
1085 int vpos, hpos, y, x;
ee78dc32 1086{
791f420f 1087 struct window *w;
ee78dc32 1088
791f420f
JR
1089 /* If updated_window is not set, work on selected_window. */
1090 if (updated_window)
1091 w = updated_window;
1092 else
1093 w = XWINDOW (selected_window);
1094
1095 /* Set the output cursor. */
1096 output_cursor.hpos = hpos;
1097 output_cursor.vpos = vpos;
1098 output_cursor.x = x;
1099 output_cursor.y = y;
ee78dc32 1100
791f420f
JR
1101 /* If not called as part of an update, really display the cursor.
1102 This will also set the cursor position of W. */
1103 if (updated_window == NULL)
ee78dc32
GV
1104 {
1105 BLOCK_INPUT;
791f420f 1106 x_display_cursor (w, 1, hpos, vpos, x, y);
ee78dc32
GV
1107 UNBLOCK_INPUT;
1108 }
1109}
791f420f 1110
9ef2e2cf 1111
ee78dc32 1112\f
791f420f
JR
1113/***********************************************************************
1114 Display Iterator
1115 ***********************************************************************/
1116
1117/* Function prototypes of this page. */
1118
1119static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1120 struct glyph *,
93ff4395
JR
1121 wchar_t *,
1122 int *));
791f420f
JR
1123static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1124 int, wchar_t *, int));
82f9d565 1125static XCharStruct *w32_per_char_metric P_ ((XFontStruct *,
9ef2e2cf
JR
1126 wchar_t *,
1127 enum w32_char_font_type));
1128static enum w32_char_font_type
1129 w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
791f420f
JR
1130static void x_append_glyph P_ ((struct it *));
1131static void x_append_composite_glyph P_ ((struct it *));
1132static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1133 int, int, double));
1134static void x_produce_glyphs P_ ((struct it *));
1135static void x_produce_image_glyph P_ ((struct it *it));
a1b9438c 1136
a1b9438c 1137
791f420f
JR
1138/* Dealing with bits of wchar_t as if they were an XChar2B. */
1139#define BUILD_WCHAR_T(byte1, byte2) \
1140 ((wchar_t)((((byte1) & 0x00ff) << 8) | ((byte2) & 0x00ff)))
a1b9438c 1141
a1b9438c 1142
791f420f
JR
1143#define BYTE1(ch) \
1144 (((ch) & 0xff00) >> 8)
a1b9438c 1145
791f420f
JR
1146#define BYTE2(ch) \
1147 ((ch) & 0x00ff)
cabb23bc 1148
791f420f
JR
1149
1150/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1151 If CHAR2B is not contained in FONT, the font's default character
9ef2e2cf 1152 metric is returned. */
791f420f 1153
82f9d565
AI
1154static int
1155w32_bdf_per_char_metric (font, char2b, dim, pcm)
9127e20e
JR
1156 XFontStruct *font;
1157 wchar_t *char2b;
1158 int dim;
82f9d565 1159 XCharStruct * pcm;
9127e20e
JR
1160{
1161 glyph_metric * bdf_metric;
1162 char buf[2];
9127e20e
JR
1163
1164 if (dim == 1)
4b99045f 1165 buf[0] = (char)(*char2b);
9127e20e
JR
1166 else
1167 {
1168 buf[0] = BYTE1 (*char2b);
1169 buf[1] = BYTE2 (*char2b);
1170 }
1171
1172 bdf_metric = w32_BDF_TextMetric (font->bdf, buf, dim);
1173
1174 if (bdf_metric)
1175 {
1176 pcm->width = bdf_metric->dwidth;
1177 pcm->lbearing = bdf_metric->bbox;
1178 pcm->rbearing = bdf_metric->dwidth
1179 - (bdf_metric->bbox + bdf_metric->bbw);
1180 pcm->ascent = bdf_metric->bboy + bdf_metric->bbh;
00c96995 1181 pcm->descent = -bdf_metric->bboy;
82f9d565
AI
1182
1183 return 1;
9127e20e 1184 }
82f9d565 1185 return 0;
9127e20e
JR
1186}
1187
1188
82f9d565
AI
1189static int
1190w32_native_per_char_metric (font, char2b, font_type, pcm)
791f420f
JR
1191 XFontStruct *font;
1192 wchar_t *char2b;
9ef2e2cf 1193 enum w32_char_font_type font_type;
82f9d565 1194 XCharStruct * pcm;
cabb23bc 1195{
82f9d565
AI
1196 HDC hdc = GetDC (NULL);
1197 HFONT old_font;
1198 BOOL retval = FALSE;
cabb23bc 1199
791f420f 1200 xassert (font && char2b);
82f9d565
AI
1201 xassert (font->hfont);
1202 xassert (font_type == UNICODE_FONT || font_type == ANSI_FONT);
cabb23bc 1203
82f9d565 1204 old_font = SelectObject (hdc, font->hfont);
791f420f 1205
dfaaaafb 1206 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
791f420f 1207 {
dfaaaafb
AI
1208 ABC char_widths;
1209
1210 if (font_type == UNICODE_FONT)
1211 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
82f9d565 1212 else
dfaaaafb
AI
1213 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
1214
1215 if (retval)
1216 {
1217 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
1218 pcm->lbearing = char_widths.abcA;
1219 pcm->rbearing = pcm->width - char_widths.abcC;
82f9d565
AI
1220 pcm->ascent = FONT_BASE (font);
1221 pcm->descent = FONT_DESCENT (font);
dfaaaafb 1222 }
791f420f 1223 }
82f9d565
AI
1224
1225 if (!retval)
791f420f 1226 {
82f9d565
AI
1227 /* Either font is not a True-type font, or GetCharABCWidthsW
1228 failed (it is not supported on Windows 9x for instance), so we
1229 can't determine the full info we would like. All is not lost
1230 though - we can call GetTextExtentPoint32 to get rbearing and
1231 deduce width based on the font's per-string overhang. lbearing
1232 is assumed to be zero. */
01b220b6
JR
1233
1234 /* TODO: Some Thai characters (and other composites if Windows
1235 supports them) do have lbearing, and report their total width
1236 as zero. Need some way of handling them when
1237 GetCharABCWidthsW fails. */
82f9d565
AI
1238 SIZE sz;
1239
1240 if (font_type == UNICODE_FONT)
1241 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
1242 else
1243 retval = GetTextExtentPoint32A (hdc, (char*)char2b, 1, &sz);
791f420f
JR
1244
1245 if (retval)
dfaaaafb 1246 {
82f9d565
AI
1247 pcm->width = sz.cx - font->tm.tmOverhang;
1248 pcm->rbearing = sz.cx;
dfaaaafb 1249 pcm->lbearing = 0;
82f9d565
AI
1250 pcm->ascent = FONT_BASE (font);
1251 pcm->descent = FONT_DESCENT (font);
dfaaaafb 1252 }
791f420f
JR
1253 }
1254
791f420f 1255
93ff4395
JR
1256 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1257 {
82f9d565 1258 retval = FALSE;
93ff4395
JR
1259 }
1260
82f9d565
AI
1261 SelectObject (hdc, old_font);
1262 ReleaseDC (NULL, hdc);
1263
1264 return retval;
1265}
1266
1267
1268static XCharStruct *
1269w32_per_char_metric (font, char2b, font_type)
1270 XFontStruct *font;
1271 wchar_t *char2b;
1272 enum w32_char_font_type font_type;
1273{
1274 /* The result metric information. */
1275 XCharStruct *pcm;
1276 BOOL retval;
1277
1278 xassert (font && char2b);
1279 xassert (font_type != UNKNOWN_FONT);
1280
1281 /* Handle the common cases quickly. */
4b99045f 1282 if (!font->bdf && font->per_char == NULL)
82f9d565
AI
1283 /* TODO: determine whether char2b exists in font? */
1284 return &font->max_bounds;
4b99045f 1285 else if (!font->bdf && *char2b < 128)
82f9d565
AI
1286 return &font->per_char[*char2b];
1287
1288 pcm = &font->scratch;
1289
1290 if (font_type == BDF_1D_FONT)
1291 retval = w32_bdf_per_char_metric (font, char2b, 1, pcm);
1292 else if (font_type == BDF_2D_FONT)
1293 retval = w32_bdf_per_char_metric (font, char2b, 2, pcm);
1294 else
1295 retval = w32_native_per_char_metric (font, char2b, font_type, pcm);
1296
1297 if (retval)
1298 return pcm;
1299
1300 return NULL;
1301}
1302
1303void
1304w32_cache_char_metrics (font)
1305 XFontStruct *font;
1306{
1307 wchar_t char2b = L'x';
1308
1309 /* Cache char metrics for the common cases. */
1310 if (font->bdf)
1311 {
1312 /* TODO: determine whether font is fixed-pitch. */
00c96995
JR
1313 if (!w32_bdf_per_char_metric (font, &char2b, 1, &font->max_bounds))
1314 {
1315 /* Use the font width and height as max bounds, as not all BDF
1316 fonts contain the letter 'x'. */
1317 font->max_bounds.width = FONT_MAX_WIDTH (font);
1318 font->max_bounds.lbearing = -font->bdf->llx;
1319 font->max_bounds.rbearing = FONT_MAX_WIDTH (font) - font->bdf->urx;
1320 font->max_bounds.ascent = FONT_BASE (font);
1321 font->max_bounds.descent = FONT_DESCENT (font);
1322 }
82f9d565
AI
1323 }
1324 else
1325 {
f1eed8ff
JR
1326 if (((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH) != 0)
1327 /* Some fonts (eg DBCS fonts) are marked as fixed width even
1328 though they contain characters of different widths. */
1329 || (font->tm.tmMaxCharWidth != font->tm.tmAveCharWidth))
82f9d565
AI
1330 {
1331 /* Font is not fixed pitch, so cache per_char info for the
1332 ASCII characters. It would be much more work, and probably
1333 not worth it, to cache other chars, since we may change
1334 between using Unicode and ANSI text drawing functions at
1335 run-time. */
1336 int i;
1337
1338 font->per_char = xmalloc (128 * sizeof(XCharStruct));
1339 for (i = 0; i < 128; i++)
1340 {
1341 char2b = i;
1342 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1343 &font->per_char[i]);
1344 }
1345 }
1346 else
1347 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1348 &font->max_bounds);
1349 }
791f420f
JR
1350}
1351
1352
9ef2e2cf
JR
1353/* Determine if a font is double byte. */
1354int w32_font_is_double_byte (XFontStruct *font)
1355{
1356 return font->double_byte_p;
1357}
1358
1359
d6ff54d5
JR
1360static BOOL
1361w32_use_unicode_for_codepage (codepage)
1362 int codepage;
1363{
1364 /* If the current codepage is supported, use Unicode for output. */
1365 return (w32_enable_unicode_output
1366 && codepage != CP_8BIT
1367 && (codepage == CP_UNICODE || IsValidCodePage (codepage)));
1368}
1369
791f420f
JR
1370/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1371 the two-byte form of C. Encoding is returned in *CHAR2B. */
1372
9ef2e2cf
JR
1373static INLINE enum w32_char_font_type
1374w32_encode_char (c, char2b, font_info, two_byte_p)
791f420f
JR
1375 int c;
1376 wchar_t *char2b;
1377 struct font_info *font_info;
9ef2e2cf 1378 int * two_byte_p;
791f420f
JR
1379{
1380 int charset = CHAR_CHARSET (c);
1381 int codepage;
9ef2e2cf
JR
1382 int unicode_p = 0;
1383
791f420f
JR
1384 XFontStruct *font = font_info->font;
1385
9127e20e 1386 xassert (two_byte_p);
9ef2e2cf
JR
1387
1388 *two_byte_p = w32_font_is_double_byte (font);
1389
791f420f
JR
1390 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1391 This may be either a program in a special encoder language or a
1392 fixed encoding. */
1393 if (font_info->font_encoder)
1394 {
1395 /* It's a program. */
1396 struct ccl_program *ccl = font_info->font_encoder;
1397
1398 if (CHARSET_DIMENSION (charset) == 1)
1399 {
1400 ccl->reg[0] = charset;
1401 ccl->reg[1] = BYTE2 (*char2b);
1402 }
1403 else
1404 {
1405 ccl->reg[0] = charset;
1406 ccl->reg[1] = BYTE1 (*char2b);
1407 ccl->reg[2] = BYTE2 (*char2b);
1408 }
1409
1410 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1411
1412 /* We assume that MSBs are appropriately set/reset by CCL
1413 program. */
9ef2e2cf 1414 if (!*two_byte_p) /* 1-byte font */
791f420f 1415 *char2b = BUILD_WCHAR_T (0, ccl->reg[1]);
791f420f
JR
1416 else
1417 *char2b = BUILD_WCHAR_T (ccl->reg[1], ccl->reg[2]);
791f420f
JR
1418 }
1419 else if (font_info->encoding[charset])
1420 {
1421 /* Fixed encoding scheme. See fontset.h for the meaning of the
1422 encoding numbers. */
1423 int enc = font_info->encoding[charset];
1424
1425 if ((enc == 1 || enc == 2)
1426 && CHARSET_DIMENSION (charset) == 2)
1427 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b) | 0x80, BYTE2 (*char2b));
1428
1429 if (enc == 1 || enc == 3
1430 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1431 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b), BYTE2 (*char2b) | 0x80);
1432 else if (enc == 4)
1433 {
1434 int sjis1, sjis2;
1435
1436 ENCODE_SJIS (BYTE1 (*char2b), BYTE2 (*char2b),
1437 sjis1, sjis2);
1438 *char2b = BUILD_WCHAR_T (sjis1, sjis2);
1439 }
1440 }
1441 codepage = w32_codepage_for_font (font_info->name);
1442
1443 /* If charset is not ASCII or Latin-1, may need to move it into
1444 Unicode space. */
1445 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
49be9f70
JR
1446 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1
1447 && charset != CHARSET_8_BIT_CONTROL && charset != CHARSET_8_BIT_GRAPHIC)
791f420f
JR
1448 {
1449 char temp[3];
1450 temp[0] = BYTE1 (*char2b);
1451 temp[1] = BYTE2 (*char2b);
1452 temp[2] = '\0';
02f593f3
JR
1453 if (codepage != CP_UNICODE)
1454 {
1455 if (temp[0])
1456 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1457 else
1458 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1459 }
9ef2e2cf
JR
1460 unicode_p = 1;
1461 *two_byte_p = 1;
1462 }
1463 if (!font)
1464 return UNKNOWN_FONT;
9127e20e
JR
1465 else if (font->bdf && CHARSET_DIMENSION (charset) == 1)
1466 return BDF_1D_FONT;
9ef2e2cf 1467 else if (font->bdf)
9127e20e 1468 return BDF_2D_FONT;
9ef2e2cf
JR
1469 else if (unicode_p)
1470 return UNICODE_FONT;
1471 else
1472 return ANSI_FONT;
791f420f
JR
1473}
1474
1475
1476/* Get face and two-byte form of character C in face FACE_ID on frame
1477 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1478 means we want to display multibyte text. Value is a pointer to a
1479 realized face that is ready for display. */
1480
1481static INLINE struct face *
1482x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1483 struct frame *f;
1484 int c, face_id;
1485 wchar_t *char2b;
1486 int multibyte_p;
1487{
1488 struct face *face = FACE_FROM_ID (f, face_id);
1489
1490 if (!multibyte_p)
1491 {
1492 /* Unibyte case. We don't have to encode, but we have to make
1493 sure to use a face suitable for unibyte. */
1494 *char2b = BUILD_WCHAR_T (0, c);
93ff4395
JR
1495 face_id = FACE_FOR_CHAR (f, face, c);
1496 face = FACE_FROM_ID (f, face_id);
791f420f
JR
1497 }
1498 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1499 {
1500 /* Case of ASCII in a face known to fit ASCII. */
1501 *char2b = BUILD_WCHAR_T (0, c);
1502 }
1503 else
1504 {
1505 int c1, c2, charset;
1506
1507 /* Split characters into bytes. If c2 is -1 afterwards, C is
1508 really a one-byte character so that byte1 is zero. */
1509 SPLIT_CHAR (c, charset, c1, c2);
1510 if (c2 > 0)
1511 *char2b = BUILD_WCHAR_T (c1, c2);
1512 else
1513 *char2b = BUILD_WCHAR_T (0, c1);
791f420f
JR
1514
1515 /* Maybe encode the character in *CHAR2B. */
93ff4395 1516 if (face->font != NULL)
791f420f
JR
1517 {
1518 struct font_info *font_info
1519 = FONT_INFO_FROM_ID (f, face->font_info_id);
1520 if (font_info)
9ef2e2cf 1521 w32_encode_char (c, char2b, font_info, &multibyte_p);
791f420f
JR
1522 }
1523 }
1524
1525 /* Make sure X resources of the face are allocated. */
1526 xassert (face != NULL);
1527 PREPARE_FACE_FOR_DISPLAY (f, face);
1528
1529 return face;
1530}
1531
1532
1533/* Get face and two-byte form of character glyph GLYPH on frame F.
1534 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1535 a pointer to a realized face that is ready for display. */
1536
1537static INLINE struct face *
93ff4395 1538x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
791f420f
JR
1539 struct frame *f;
1540 struct glyph *glyph;
1541 wchar_t *char2b;
93ff4395 1542 int *two_byte_p;
791f420f
JR
1543{
1544 struct face *face;
9ef2e2cf 1545 int dummy = 0;
791f420f
JR
1546
1547 xassert (glyph->type == CHAR_GLYPH);
1548 face = FACE_FROM_ID (f, glyph->face_id);
1549
93ff4395
JR
1550 if (two_byte_p)
1551 *two_byte_p = 0;
9ef2e2cf
JR
1552 else
1553 two_byte_p = &dummy;
93ff4395 1554
791f420f
JR
1555 if (!glyph->multibyte_p)
1556 {
1557 /* Unibyte case. We don't have to encode, but we have to make
1558 sure to use a face suitable for unibyte. */
1559 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1560 }
1561 else if (glyph->u.ch < 128
1562 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1563 {
1564 /* Case of ASCII in a face known to fit ASCII. */
1565 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1566 }
1567 else
1568 {
1569 int c1, c2, charset;
1570
1571 /* Split characters into bytes. If c2 is -1 afterwards, C is
1572 really a one-byte character so that byte1 is zero. */
1573 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1574 if (c2 > 0)
1575 *char2b = BUILD_WCHAR_T (c1, c2);
1576 else
1577 *char2b = BUILD_WCHAR_T (0, c1);
1578
1579 /* Maybe encode the character in *CHAR2B. */
1580 if (charset != CHARSET_ASCII)
1581 {
1582 struct font_info *font_info
1583 = FONT_INFO_FROM_ID (f, face->font_info_id);
1584 if (font_info)
1585 {
9ef2e2cf
JR
1586 glyph->w32_font_type
1587 = w32_encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
791f420f
JR
1588 }
1589 }
1590 }
1591
1592 /* Make sure X resources of the face are allocated. */
1593 xassert (face != NULL);
1594 PREPARE_FACE_FOR_DISPLAY (f, face);
1595 return face;
1596}
1597
1598
1599/* Store one glyph for IT->char_to_display in IT->glyph_row.
1600 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1601
1602static INLINE void
1603x_append_glyph (it)
1604 struct it *it;
1605{
1606 struct glyph *glyph;
1607 enum glyph_row_area area = it->area;
1608
1609 xassert (it->glyph_row);
1610 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1611
1612 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1613 if (glyph < it->glyph_row->glyphs[area + 1])
1614 {
791f420f
JR
1615 glyph->charpos = CHARPOS (it->position);
1616 glyph->object = it->object;
ec48c3a7 1617 glyph->pixel_width = it->pixel_width;
791f420f 1618 glyph->voffset = it->voffset;
ec48c3a7 1619 glyph->type = CHAR_GLYPH;
791f420f 1620 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1621 glyph->left_box_line_p = it->start_of_box_run_p;
1622 glyph->right_box_line_p = it->end_of_box_run_p;
791f420f
JR
1623 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1624 || it->phys_descent > it->descent);
ec48c3a7 1625 glyph->padding_p = 0;
93ff4395 1626 glyph->glyph_not_available_p = it->glyph_not_available_p;
ec48c3a7
JR
1627 glyph->face_id = it->face_id;
1628 glyph->u.ch = it->char_to_display;
1629 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1630 ++it->glyph_row->used[area];
1631 }
1632}
1633
1634/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1635 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1636
1637static INLINE void
1638x_append_composite_glyph (it)
1639 struct it *it;
1640{
1641 struct glyph *glyph;
1642 enum glyph_row_area area = it->area;
1643
1644 xassert (it->glyph_row);
1645
1646 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1647 if (glyph < it->glyph_row->glyphs[area + 1])
1648 {
791f420f
JR
1649 glyph->charpos = CHARPOS (it->position);
1650 glyph->object = it->object;
ec48c3a7 1651 glyph->pixel_width = it->pixel_width;
791f420f 1652 glyph->voffset = it->voffset;
ec48c3a7 1653 glyph->type = COMPOSITE_GLYPH;
791f420f 1654 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1655 glyph->left_box_line_p = it->start_of_box_run_p;
1656 glyph->right_box_line_p = it->end_of_box_run_p;
791f420f
JR
1657 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1658 || it->phys_descent > it->descent);
ec48c3a7
JR
1659 glyph->padding_p = 0;
1660 glyph->glyph_not_available_p = 0;
1661 glyph->face_id = it->face_id;
1662 glyph->u.cmp_id = it->cmp_id;
1663 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1664 ++it->glyph_row->used[area];
1665 }
1666}
1667
1668
1669/* Change IT->ascent and IT->height according to the setting of
1670 IT->voffset. */
1671
1672static INLINE void
1673take_vertical_position_into_account (it)
1674 struct it *it;
1675{
1676 if (it->voffset)
1677 {
1678 if (it->voffset < 0)
1679 /* Increase the ascent so that we can display the text higher
1680 in the line. */
1681 it->ascent += abs (it->voffset);
1682 else
1683 /* Increase the descent so that we can display the text lower
1684 in the line. */
1685 it->descent += it->voffset;
1686 }
1687}
1688
1689
1690/* Produce glyphs/get display metrics for the image IT is loaded with.
1691 See the description of struct display_iterator in dispextern.h for
1692 an overview of struct display_iterator. */
1693
1694static void
1695x_produce_image_glyph (it)
1696 struct it *it;
1697{
1698 struct image *img;
1699 struct face *face;
1700
1701 xassert (it->what == IT_IMAGE);
1702
1703 face = FACE_FROM_ID (it->f, it->face_id);
1704 img = IMAGE_FROM_ID (it->f, it->image_id);
1705 xassert (img);
1706
1707 /* Make sure X resources of the face and image are loaded. */
1708 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1709 prepare_image_for_display (it->f, img);
1710
9ef2e2cf 1711 it->ascent = it->phys_ascent = image_ascent (img, face);
d6ff54d5
JR
1712 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1713 it->pixel_width = img->width + 2 * img->hmargin;
791f420f
JR
1714
1715 it->nglyphs = 1;
1716
1717 if (face->box != FACE_NO_BOX)
1718 {
60222d69
AI
1719 if (face->box_line_width > 0)
1720 {
1721 it->ascent += face->box_line_width;
1722 it->descent += face->box_line_width;
1723 }
791f420f
JR
1724
1725 if (it->start_of_box_run_p)
60222d69 1726 it->pixel_width += abs (face->box_line_width);
791f420f 1727 if (it->end_of_box_run_p)
60222d69 1728 it->pixel_width += abs (face->box_line_width);
791f420f
JR
1729 }
1730
1731 take_vertical_position_into_account (it);
1732
1733 if (it->glyph_row)
1734 {
1735 struct glyph *glyph;
1736 enum glyph_row_area area = it->area;
1737
1738 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1739 if (glyph < it->glyph_row->glyphs[area + 1])
1740 {
791f420f
JR
1741 glyph->charpos = CHARPOS (it->position);
1742 glyph->object = it->object;
ec48c3a7 1743 glyph->pixel_width = it->pixel_width;
791f420f 1744 glyph->voffset = it->voffset;
ec48c3a7 1745 glyph->type = IMAGE_GLYPH;
791f420f 1746 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1747 glyph->left_box_line_p = it->start_of_box_run_p;
1748 glyph->right_box_line_p = it->end_of_box_run_p;
1749 glyph->overlaps_vertically_p = 0;
1750 glyph->padding_p = 0;
1751 glyph->glyph_not_available_p = 0;
1752 glyph->face_id = it->face_id;
1753 glyph->u.img_id = img->id;
1754 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1755 ++it->glyph_row->used[area];
1756 }
1757 }
1758}
1759
1760
1761/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1762 of the glyph, WIDTH and HEIGHT are the width and height of the
1763 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1764 ascent of the glyph (0 <= ASCENT <= 1). */
1765
1766static void
1767x_append_stretch_glyph (it, object, width, height, ascent)
1768 struct it *it;
1769 Lisp_Object object;
1770 int width, height;
1771 double ascent;
1772{
1773 struct glyph *glyph;
1774 enum glyph_row_area area = it->area;
1775
1776 xassert (ascent >= 0 && ascent <= 1);
1777
1778 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1779 if (glyph < it->glyph_row->glyphs[area + 1])
1780 {
791f420f
JR
1781 glyph->charpos = CHARPOS (it->position);
1782 glyph->object = object;
ec48c3a7 1783 glyph->pixel_width = width;
791f420f 1784 glyph->voffset = it->voffset;
ec48c3a7 1785 glyph->type = STRETCH_GLYPH;
791f420f 1786 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1787 glyph->left_box_line_p = it->start_of_box_run_p;
1788 glyph->right_box_line_p = it->end_of_box_run_p;
1789 glyph->overlaps_vertically_p = 0;
1790 glyph->padding_p = 0;
1791 glyph->glyph_not_available_p = 0;
1792 glyph->face_id = it->face_id;
1793 glyph->u.stretch.ascent = height * ascent;
1794 glyph->u.stretch.height = height;
1795 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1796 ++it->glyph_row->used[area];
1797 }
1798}
1799
1800
1801/* Produce a stretch glyph for iterator IT. IT->object is the value
1802 of the glyph property displayed. The value must be a list
1803 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1804 being recognized:
1805
1806 1. `:width WIDTH' specifies that the space should be WIDTH *
1807 canonical char width wide. WIDTH may be an integer or floating
1808 point number.
1809
1810 2. `:relative-width FACTOR' specifies that the width of the stretch
1811 should be computed from the width of the first character having the
1812 `glyph' property, and should be FACTOR times that width.
1813
1814 3. `:align-to HPOS' specifies that the space should be wide enough
1815 to reach HPOS, a value in canonical character units.
1816
1817 Exactly one of the above pairs must be present.
1818
1819 4. `:height HEIGHT' specifies that the height of the stretch produced
1820 should be HEIGHT, measured in canonical character units.
1821
1822 5. `:relative-height FACTOR' specifies that the height of the the
1823 stretch should be FACTOR times the height of the characters having
1824 the glyph property.
1825
1826 Either none or exactly one of 4 or 5 must be present.
1827
1828 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1829 of the stretch should be used for the ascent of the stretch.
1830 ASCENT must be in the range 0 <= ASCENT <= 100. */
1831
1832#define NUMVAL(X) \
1833 ((INTEGERP (X) || FLOATP (X)) \
1834 ? XFLOATINT (X) \
1835 : - 1)
1836
1837
1838static void
1839x_produce_stretch_glyph (it)
1840 struct it *it;
1841{
1842 /* (space :width WIDTH :height HEIGHT. */
c2cc16fa
JR
1843#if GLYPH_DEBUG
1844 extern Lisp_Object Qspace;
1845#endif
1846 extern Lisp_Object QCwidth, QCheight, QCascent;
791f420f
JR
1847 extern Lisp_Object QCrelative_width, QCrelative_height;
1848 extern Lisp_Object QCalign_to;
1849 Lisp_Object prop, plist;
1850 double width = 0, height = 0, ascent = 0;
1851 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1852 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1853
1854 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1855
1856 /* List should start with `space'. */
1857 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1858 plist = XCDR (it->object);
1859
1860 /* Compute the width of the stretch. */
1861 if (prop = Fplist_get (plist, QCwidth),
1862 NUMVAL (prop) > 0)
1863 /* Absolute width `:width WIDTH' specified and valid. */
1864 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1865 else if (prop = Fplist_get (plist, QCrelative_width),
1866 NUMVAL (prop) > 0)
1867 {
1868 /* Relative width `:relative-width FACTOR' specified and valid.
1869 Compute the width of the characters having the `glyph'
1870 property. */
1871 struct it it2;
1872 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1873
1874 it2 = *it;
1875 if (it->multibyte_p)
1876 {
1877 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1878 - IT_BYTEPOS (*it));
1879 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1880 }
1881 else
1882 it2.c = *p, it2.len = 1;
1883
1884 it2.glyph_row = NULL;
1885 it2.what = IT_CHARACTER;
1886 x_produce_glyphs (&it2);
1887 width = NUMVAL (prop) * it2.pixel_width;
1888 }
1889 else if (prop = Fplist_get (plist, QCalign_to),
1890 NUMVAL (prop) > 0)
1891 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1892 else
1893 /* Nothing specified -> width defaults to canonical char width. */
1894 width = CANON_X_UNIT (it->f);
1895
1896 /* Compute height. */
1897 if (prop = Fplist_get (plist, QCheight),
1898 NUMVAL (prop) > 0)
1899 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1900 else if (prop = Fplist_get (plist, QCrelative_height),
1901 NUMVAL (prop) > 0)
1902 height = FONT_HEIGHT (font) * NUMVAL (prop);
1903 else
1904 height = FONT_HEIGHT (font);
1905
1906 /* Compute percentage of height used for ascent. If
1907 `:ascent ASCENT' is present and valid, use that. Otherwise,
1908 derive the ascent from the font in use. */
1909 if (prop = Fplist_get (plist, QCascent),
1910 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1911 ascent = NUMVAL (prop) / 100.0;
1912 else
1913 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
1914
1915 if (width <= 0)
1916 width = 1;
1917 if (height <= 0)
1918 height = 1;
1919
1920 if (it->glyph_row)
1921 {
1922 Lisp_Object object = it->stack[it->sp - 1].string;
1923 if (!STRINGP (object))
1924 object = it->w->buffer;
1925 x_append_stretch_glyph (it, object, width, height, ascent);
1926 }
1927
1928 it->pixel_width = width;
1929 it->ascent = it->phys_ascent = height * ascent;
1930 it->descent = it->phys_descent = height - it->ascent;
1931 it->nglyphs = 1;
1932
1933 if (face->box != FACE_NO_BOX)
1934 {
60222d69
AI
1935 if (face->box_line_width > 0)
1936 {
1937 it->ascent += face->box_line_width;
1938 it->descent += face->box_line_width;
1939 }
791f420f
JR
1940
1941 if (it->start_of_box_run_p)
60222d69 1942 it->pixel_width += abs (face->box_line_width);
791f420f 1943 if (it->end_of_box_run_p)
60222d69 1944 it->pixel_width += abs (face->box_line_width);
791f420f
JR
1945 }
1946
1947 take_vertical_position_into_account (it);
1948}
1949
1950/* Return proper value to be used as baseline offset of font that has
1951 ASCENT and DESCENT to draw characters by the font at the vertical
1952 center of the line of frame F.
1953
1954 Here, out task is to find the value of BOFF in the following figure;
1955
1956 -------------------------+-----------+-
1957 -+-+---------+-+ | |
1958 | | | | | |
1959 | | | | F_ASCENT F_HEIGHT
1960 | | | ASCENT | |
1961 HEIGHT | | | | |
1962 | | |-|-+------+-----------|------- baseline
1963 | | | | BOFF | |
1964 | |---------|-+-+ | |
1965 | | | DESCENT | |
1966 -+-+---------+-+ F_DESCENT |
1967 -------------------------+-----------+-
1968
1969 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1970 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1971 DESCENT = FONT->descent
1972 HEIGHT = FONT_HEIGHT (FONT)
1973 F_DESCENT = (F->output_data.x->font->descent
1974 - F->output_data.x->baseline_offset)
1975 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1976*/
1977
1978#define VCENTER_BASELINE_OFFSET(FONT, F) \
458f45fa
KH
1979 (FONT_DESCENT (FONT) \
1980 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1981 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1982 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
791f420f
JR
1983
1984/* Produce glyphs/get display metrics for the display element IT is
1985 loaded with. See the description of struct display_iterator in
1986 dispextern.h for an overview of struct display_iterator. */
1987
1988static void
1989x_produce_glyphs (it)
1990 struct it *it;
1991{
93ff4395
JR
1992 it->glyph_not_available_p = 0;
1993
791f420f
JR
1994 if (it->what == IT_CHARACTER)
1995 {
1996 wchar_t char2b;
1997 XFontStruct *font;
93ff4395 1998 struct face *face = FACE_FROM_ID (it->f, it->face_id);
791f420f 1999 XCharStruct *pcm;
93ff4395 2000 int font_not_found_p;
791f420f
JR
2001 struct font_info *font_info;
2002 int boff; /* baseline offset */
2d0c0bd7
JR
2003 /* We may change it->multibyte_p upon unibyte<->multibyte
2004 conversion. So, save the current value now and restore it
2005 later.
2006
2007 Note: It seems that we don't have to record multibyte_p in
2008 struct glyph because the character code itself tells if or
2009 not the character is multibyte. Thus, in the future, we must
2010 consider eliminating the field `multibyte_p' in the struct
2011 glyph.
2012 */
2013 int saved_multibyte_p = it->multibyte_p;
791f420f 2014
93ff4395
JR
2015 /* Maybe translate single-byte characters to multibyte, or the
2016 other way. */
791f420f 2017 it->char_to_display = it->c;
93ff4395
JR
2018 if (!ASCII_BYTE_P (it->c))
2019 {
2020 if (unibyte_display_via_language_environment
2021 && SINGLE_BYTE_CHAR_P (it->c)
2022 && (it->c >= 0240
2023 || !NILP (Vnonascii_translation_table)))
2024 {
2025 it->char_to_display = unibyte_char_to_multibyte (it->c);
2d0c0bd7 2026 it->multibyte_p = 1;
93ff4395
JR
2027 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2028 face = FACE_FROM_ID (it->f, it->face_id);
2029 }
2030 else if (!SINGLE_BYTE_CHAR_P (it->c)
2031 && !it->multibyte_p)
2032 {
2033 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
2d0c0bd7 2034 it->multibyte_p = 0;
93ff4395
JR
2035 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2036 face = FACE_FROM_ID (it->f, it->face_id);
2037 }
2038 }
791f420f 2039
93ff4395
JR
2040 /* Get font to use. Encode IT->char_to_display. */
2041 x_get_char_face_and_encoding (it->f, it->char_to_display,
2042 it->face_id, &char2b,
2043 it->multibyte_p);
791f420f
JR
2044 font = face->font;
2045
2046 /* When no suitable font found, use the default font. */
2047 font_not_found_p = font == NULL;
2048 if (font_not_found_p)
2049 {
2050 font = FRAME_FONT (it->f);
2051 boff = it->f->output_data.w32->baseline_offset;
2052 font_info = NULL;
2053 }
2054 else
2055 {
2056 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2057 boff = font_info->baseline_offset;
2058 if (font_info->vertical_centering)
2059 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2060 }
2061
791f420f
JR
2062 if (it->char_to_display >= ' '
2063 && (!it->multibyte_p || it->char_to_display < 128))
2064 {
2065 /* Either unibyte or ASCII. */
2066 int stretched_p;
2067
2068 it->nglyphs = 1;
2069
82f9d565 2070 pcm = w32_per_char_metric (font, &char2b,
9127e20e 2071 font->bdf ? BDF_1D_FONT : ANSI_FONT);
791f420f
JR
2072 it->ascent = FONT_BASE (font) + boff;
2073 it->descent = FONT_DESCENT (font) - boff;
791f420f 2074
9ef2e2cf
JR
2075 if (pcm)
2076 {
2077 it->phys_ascent = pcm->ascent + boff;
2078 it->phys_descent = pcm->descent - boff;
2079 it->pixel_width = pcm->width;
2080 }
2081 else
2082 {
2083 it->glyph_not_available_p = 1;
9127e20e
JR
2084 it->phys_ascent = FONT_BASE (font) + boff;
2085 it->phys_descent = FONT_DESCENT (font) - boff;
2086 it->pixel_width = FONT_WIDTH (font);
9ef2e2cf
JR
2087 }
2088
791f420f
JR
2089 /* If this is a space inside a region of text with
2090 `space-width' property, change its width. */
2091 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
2092 if (stretched_p)
2093 it->pixel_width *= XFLOATINT (it->space_width);
2094
2095 /* If face has a box, add the box thickness to the character
2096 height. If character has a box line to the left and/or
2097 right, add the box line width to the character's width. */
2098 if (face->box != FACE_NO_BOX)
2099 {
2100 int thick = face->box_line_width;
2101
60222d69
AI
2102 if (thick > 0)
2103 {
2104 it->ascent += thick;
2105 it->descent += thick;
2106 }
2107 else
2108 thick = -thick;
791f420f
JR
2109
2110 if (it->start_of_box_run_p)
2111 it->pixel_width += thick;
2112 if (it->end_of_box_run_p)
2113 it->pixel_width += thick;
2114 }
2115
2116 /* If face has an overline, add the height of the overline
2117 (1 pixel) and a 1 pixel margin to the character height. */
2118 if (face->overline_p)
2119 it->ascent += 2;
2120
2121 take_vertical_position_into_account (it);
2122
2123 /* If we have to actually produce glyphs, do it. */
2124 if (it->glyph_row)
2125 {
2126 if (stretched_p)
2127 {
2128 /* Translate a space with a `space-width' property
2129 into a stretch glyph. */
2130 double ascent = (double) FONT_BASE (font)
2131 / FONT_HEIGHT (font);
2132 x_append_stretch_glyph (it, it->object, it->pixel_width,
2133 it->ascent + it->descent, ascent);
2134 }
2135 else
2136 x_append_glyph (it);
2137
2138 /* If characters with lbearing or rbearing are displayed
2139 in this line, record that fact in a flag of the
2140 glyph row. This is used to optimize X output code. */
9ef2e2cf 2141 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
791f420f
JR
2142 it->glyph_row->contains_overlapping_glyphs_p = 1;
2143 }
2144 }
2145 else if (it->char_to_display == '\n')
2146 {
2147 /* A newline has no width but we need the height of the line. */
2148 it->pixel_width = 0;
2149 it->nglyphs = 0;
2150 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2151 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2152
60222d69
AI
2153 if (face->box != FACE_NO_BOX
2154 && face->box_line_width > 0)
791f420f 2155 {
60222d69
AI
2156 it->ascent += face->box_line_width;
2157 it->descent += face->box_line_width;
791f420f
JR
2158 }
2159 }
2160 else if (it->char_to_display == '\t')
2161 {
2162 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
9ef2e2cf 2163 int x = it->current_x + it->continuation_lines_width;
791f420f
JR
2164 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2165
9127e20e
JR
2166 /* If the distance from the current position to the next tab
2167 stop is less than a canonical character width, use the
2168 tab stop after that. */
2169 if (next_tab_x - x < CANON_X_UNIT (it->f))
2170 next_tab_x += tab_width;
2171
791f420f
JR
2172 it->pixel_width = next_tab_x - x;
2173 it->nglyphs = 1;
2174 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2175 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2176
2177 if (it->glyph_row)
2178 {
2179 double ascent = (double) it->ascent / (it->ascent + it->descent);
2180 x_append_stretch_glyph (it, it->object, it->pixel_width,
2181 it->ascent + it->descent, ascent);
2182 }
2183 }
2184 else
2185 {
00fe468b
JR
2186 /* A multi-byte character.
2187 If we found a font, this font should give us the right
791f420f
JR
2188 metrics. If we didn't find a font, use the frame's
2189 default font and calculate the width of the character
2190 from the charset width; this is what old redisplay code
2191 did. */
9127e20e
JR
2192 enum w32_char_font_type type;
2193
2194 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2195 type = BDF_1D_FONT;
2196 else if (font->bdf)
2197 type = BDF_2D_FONT;
2198 else
2199 type = UNICODE_FONT;
2200
82f9d565 2201 pcm = w32_per_char_metric (font, &char2b, type);
00fe468b 2202
93ff4395
JR
2203 if (font_not_found_p || !pcm)
2204 {
2205 int charset = CHAR_CHARSET (it->char_to_display);
00fe468b 2206
93ff4395
JR
2207 it->glyph_not_available_p = 1;
2208 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2209 * CHARSET_WIDTH (charset));
2210 it->phys_ascent = FONT_BASE (font) + boff;
2211 it->phys_descent = FONT_DESCENT (font) - boff;
2212 }
2213 else
2214 {
9ef2e2cf 2215 it->pixel_width = pcm->width;
93ff4395
JR
2216 it->phys_ascent = pcm->ascent + boff;
2217 it->phys_descent = pcm->descent - boff;
2218 if (it->glyph_row
2219 && (pcm->lbearing < 0
2220 || pcm->rbearing > pcm->width))
2221 it->glyph_row->contains_overlapping_glyphs_p = 1;
00fe468b 2222 }
791f420f
JR
2223 it->nglyphs = 1;
2224 it->ascent = FONT_BASE (font) + boff;
2225 it->descent = FONT_DESCENT (font) - boff;
791f420f
JR
2226 if (face->box != FACE_NO_BOX)
2227 {
2228 int thick = face->box_line_width;
60222d69
AI
2229
2230 if (thick > 0)
2231 {
2232 it->ascent += thick;
2233 it->descent += thick;
2234 }
2235 else
2236 thick = - thick;
791f420f
JR
2237
2238 if (it->start_of_box_run_p)
2239 it->pixel_width += thick;
2240 if (it->end_of_box_run_p)
2241 it->pixel_width += thick;
2242 }
2243
2244 /* If face has an overline, add the height of the overline
2245 (1 pixel) and a 1 pixel margin to the character height. */
2246 if (face->overline_p)
2247 it->ascent += 2;
2248
2249 take_vertical_position_into_account (it);
2250
2251 if (it->glyph_row)
2252 x_append_glyph (it);
2253 }
2d0c0bd7 2254 it->multibyte_p = saved_multibyte_p;
791f420f
JR
2255 }
2256 else if (it->what == IT_COMPOSITION)
2257 {
01b220b6
JR
2258 /* Note: A composition is represented as one glyph in the
2259 glyph matrix. There are no padding glyphs. */
2260 wchar_t char2b;
2261 XFontStruct *font;
2262 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2263 XCharStruct *pcm;
2264 int font_not_found_p;
2265 struct font_info *font_info;
2266 int boff; /* baseline offset */
2267 struct composition *cmp = composition_table[it->cmp_id];
2268
2269 /* Maybe translate single-byte characters to multibyte. */
2270 it->char_to_display = it->c;
2271 if (unibyte_display_via_language_environment
2272 && SINGLE_BYTE_CHAR_P (it->c)
2273 && (it->c >= 0240
2274 || (it->c >= 0200
2275 && !NILP (Vnonascii_translation_table))))
2276 {
2277 it->char_to_display = unibyte_char_to_multibyte (it->c);
2278 }
2279
2280 /* Get face and font to use. Encode IT->char_to_display. */
2281 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2282 face = FACE_FROM_ID (it->f, it->face_id);
2283 x_get_char_face_and_encoding (it->f, it->char_to_display,
2284 it->face_id, &char2b, it->multibyte_p);
2285 font = face->font;
2286
2287 /* When no suitable font found, use the default font. */
2288 font_not_found_p = font == NULL;
2289 if (font_not_found_p)
2290 {
2291 font = FRAME_FONT (it->f);
2292 boff = it->f->output_data.w32->baseline_offset;
2293 font_info = NULL;
2294 }
2295 else
2296 {
2297 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2298 boff = font_info->baseline_offset;
2299 if (font_info->vertical_centering)
2300 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2301 }
2302
2303 /* There are no padding glyphs, so there is only one glyph to
2304 produce for the composition. Important is that pixel_width,
2305 ascent and descent are the values of what is drawn by
2306 draw_glyphs (i.e. the values of the overall glyphs composed). */
2307 it->nglyphs = 1;
2308
2309 /* If we have not yet calculated pixel size data of glyphs of
2310 the composition for the current face font, calculate them
2311 now. Theoretically, we have to check all fonts for the
2312 glyphs, but that requires much time and memory space. So,
2313 here we check only the font of the first glyph. This leads
2314 to incorrect display very rarely, and C-l (recenter) can
2315 correct the display anyway. */
2316 if (cmp->font != (void *) font)
2317 {
2318 /* Ascent and descent of the font of the first character of
2319 this composition (adjusted by baseline offset). Ascent
2320 and descent of overall glyphs should not be less than
2321 them respectively. */
2322 int font_ascent = FONT_BASE (font) + boff;
2323 int font_descent = FONT_DESCENT (font) - boff;
2324 /* Bounding box of the overall glyphs. */
2325 int leftmost, rightmost, lowest, highest;
2326 int i, width, ascent, descent;
1521ce96 2327 enum w32_char_font_type font_type;
01b220b6
JR
2328
2329 cmp->font = (void *) font;
2330
2331 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2332 font_type = BDF_1D_FONT;
2333 else if (font->bdf)
2334 font_type = BDF_2D_FONT;
2335 else
2336 font_type = UNICODE_FONT;
2337
2338 /* Initialize the bounding box. */
f201d732
JR
2339 if (font_info
2340 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
01b220b6
JR
2341 {
2342 width = pcm->width;
2343 ascent = pcm->ascent;
2344 descent = pcm->descent;
2345 }
2346 else
2347 {
2348 width = FONT_WIDTH (font);
2349 ascent = FONT_BASE (font);
2350 descent = FONT_DESCENT (font);
2351 }
2352
2353 rightmost = width;
2354 lowest = - descent + boff;
2355 highest = ascent + boff;
2356 leftmost = 0;
2357
2358 if (font_info
2359 && font_info->default_ascent
2360 && CHAR_TABLE_P (Vuse_default_ascent)
2361 && !NILP (Faref (Vuse_default_ascent,
2362 make_number (it->char_to_display))))
2363 highest = font_info->default_ascent + boff;
2364
2365 /* Draw the first glyph at the normal position. It may be
2366 shifted to right later if some other glyphs are drawn at
2367 the left. */
2368 cmp->offsets[0] = 0;
2369 cmp->offsets[1] = boff;
2370
2371 /* Set cmp->offsets for the remaining glyphs. */
2372 for (i = 1; i < cmp->glyph_len; i++)
2373 {
2374 int left, right, btm, top;
2375 int ch = COMPOSITION_GLYPH (cmp, i);
2376 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2377
2378 face = FACE_FROM_ID (it->f, face_id);
2379 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2380 it->multibyte_p);
2381 font = face->font;
2382 if (font == NULL)
2383 {
2384 font = FRAME_FONT (it->f);
2385 boff = it->f->output_data.w32->baseline_offset;
2386 font_info = NULL;
2387 }
2388 else
2389 {
2390 font_info
2391 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2392 boff = font_info->baseline_offset;
2393 if (font_info->vertical_centering)
2394 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2395 }
2396
2397 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (ch)) == 1)
2398 font_type = BDF_1D_FONT;
2399 else if (font->bdf)
2400 font_type = BDF_2D_FONT;
2401 else
2402 font_type = UNICODE_FONT;
2403
f201d732
JR
2404 if (font_info
2405 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
01b220b6
JR
2406 {
2407 width = pcm->width;
2408 ascent = pcm->ascent;
2409 descent = pcm->descent;
2410 }
2411 else
2412 {
2413 width = FONT_WIDTH (font);
f201d732
JR
2414 ascent = 1;
2415 descent = 0;
01b220b6
JR
2416 }
2417
2418 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2419 {
2420 /* Relative composition with or without
2421 alternate chars. */
2422 left = (leftmost + rightmost - width) / 2;
2423 btm = - descent + boff;
2424 if (font_info && font_info->relative_compose
2425 && (! CHAR_TABLE_P (Vignore_relative_composition)
2426 || NILP (Faref (Vignore_relative_composition,
2427 make_number (ch)))))
2428 {
2429
2430 if (- descent >= font_info->relative_compose)
2431 /* One extra pixel between two glyphs. */
2432 btm = highest + 1;
2433 else if (ascent <= 0)
2434 /* One extra pixel between two glyphs. */
2435 btm = lowest - 1 - ascent - descent;
2436 }
2437 }
2438 else
2439 {
2440 /* A composition rule is specified by an integer
2441 value that encodes global and new reference
2442 points (GREF and NREF). GREF and NREF are
2443 specified by numbers as below:
2444
2445 0---1---2 -- ascent
2446 | |
2447 | |
2448 | |
2449 9--10--11 -- center
2450 | |
2451 ---3---4---5--- baseline
2452 | |
2453 6---7---8 -- descent
2454 */
2455 int rule = COMPOSITION_RULE (cmp, i);
2456 int gref, nref, grefx, grefy, nrefx, nrefy;
2457
2458 COMPOSITION_DECODE_RULE (rule, gref, nref);
2459 grefx = gref % 3, nrefx = nref % 3;
2460 grefy = gref / 3, nrefy = nref / 3;
2461
2462 left = (leftmost
2463 + grefx * (rightmost - leftmost) / 2
2464 - nrefx * width / 2);
2465 btm = ((grefy == 0 ? highest
2466 : grefy == 1 ? 0
2467 : grefy == 2 ? lowest
2468 : (highest + lowest) / 2)
2469 - (nrefy == 0 ? ascent + descent
2470 : nrefy == 1 ? descent - boff
2471 : nrefy == 2 ? 0
2472 : (ascent + descent) / 2));
2473 }
2474
2475 cmp->offsets[i * 2] = left;
2476 cmp->offsets[i * 2 + 1] = btm + descent;
2477
2478 /* Update the bounding box of the overall glyphs. */
2479 right = left + width;
2480 top = btm + descent + ascent;
2481 if (left < leftmost)
2482 leftmost = left;
2483 if (right > rightmost)
2484 rightmost = right;
2485 if (top > highest)
2486 highest = top;
2487 if (btm < lowest)
2488 lowest = btm;
2489 }
2490
2491 /* If there are glyphs whose x-offsets are negative,
2492 shift all glyphs to the right and make all x-offsets
2493 non-negative. */
2494 if (leftmost < 0)
2495 {
2496 for (i = 0; i < cmp->glyph_len; i++)
2497 cmp->offsets[i * 2] -= leftmost;
2498 rightmost -= leftmost;
2499 }
2500
2501 cmp->pixel_width = rightmost;
2502 cmp->ascent = highest;
2503 cmp->descent = - lowest;
2504 if (cmp->ascent < font_ascent)
2505 cmp->ascent = font_ascent;
2506 if (cmp->descent < font_descent)
2507 cmp->descent = font_descent;
2508 }
2509
2510 it->pixel_width = cmp->pixel_width;
2511 it->ascent = it->phys_ascent = cmp->ascent;
2512 it->descent = it->phys_descent = cmp->descent;
2513
2514 if (face->box != FACE_NO_BOX)
2515 {
2516 int thick = face->box_line_width;
60222d69
AI
2517
2518 if (thick > 0)
2519 {
2520 it->ascent += thick;
2521 it->descent += thick;
2522 }
2523 else
2524 thick = - thick;
01b220b6
JR
2525
2526 if (it->start_of_box_run_p)
2527 it->pixel_width += thick;
2528 if (it->end_of_box_run_p)
2529 it->pixel_width += thick;
2530 }
2531
2532 /* If face has an overline, add the height of the overline
2533 (1 pixel) and a 1 pixel margin to the character height. */
2534 if (face->overline_p)
2535 it->ascent += 2;
2536
2537 take_vertical_position_into_account (it);
2538
2539 if (it->glyph_row)
2540 x_append_composite_glyph (it);
791f420f
JR
2541 }
2542 else if (it->what == IT_IMAGE)
2543 x_produce_image_glyph (it);
2544 else if (it->what == IT_STRETCH)
2545 x_produce_stretch_glyph (it);
2546
c2cc16fa
JR
2547 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2548 because this isn't true for images with `:ascent 100'. */
2549 xassert (it->ascent >= 0 && it->descent >= 0);
791f420f
JR
2550 if (it->area == TEXT_AREA)
2551 it->current_x += it->pixel_width;
2552
9ef2e2cf
JR
2553 it->descent += it->extra_line_spacing;
2554
791f420f
JR
2555 it->max_ascent = max (it->max_ascent, it->ascent);
2556 it->max_descent = max (it->max_descent, it->descent);
2557 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2558 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2559}
2560
2561
2562/* Estimate the pixel height of the mode or top line on frame F.
2563 FACE_ID specifies what line's height to estimate. */
2564
2565int
2566x_estimate_mode_line_height (f, face_id)
2567 struct frame *f;
2568 enum face_id face_id;
2569{
250cfece 2570 int height = FONT_HEIGHT (FRAME_FONT (f));
791f420f
JR
2571
2572 /* This function is called so early when Emacs starts that the face
2573 cache and mode line face are not yet initialized. */
2574 if (FRAME_FACE_CACHE (f))
2575 {
2576 struct face *face = FACE_FROM_ID (f, face_id);
9ef2e2cf 2577 if (face)
250cfece
JR
2578 {
2579 if (face->font)
2580 height = FONT_HEIGHT (face->font);
60222d69
AI
2581 if (face->box_line_width > 0)
2582 height += 2 * face->box_line_width;
250cfece 2583 }
791f420f
JR
2584 }
2585
2586 return height;
2587}
cabb23bc 2588
cabb23bc 2589\f
791f420f
JR
2590/***********************************************************************
2591 Glyph display
2592 ***********************************************************************/
2593
2594/* A sequence of glyphs to be drawn in the same face.
2595
2596 This data structure is not really completely X specific, so it
2597 could possibly, at least partially, be useful for other systems. It
2598 is currently not part of the external redisplay interface because
2599 it's not clear what other systems will need. */
2600
2601struct glyph_string
2602{
2603 /* X-origin of the string. */
2604 int x;
2605
2606 /* Y-origin and y-position of the base line of this string. */
2607 int y, ybase;
2608
2609 /* The width of the string, not including a face extension. */
2610 int width;
2611
2612 /* The width of the string, including a face extension. */
2613 int background_width;
2614
2615 /* The height of this string. This is the height of the line this
2616 string is drawn in, and can be different from the height of the
2617 font the string is drawn in. */
2618 int height;
2619
2620 /* Number of pixels this string overwrites in front of its x-origin.
2621 This number is zero if the string has an lbearing >= 0; it is
2622 -lbearing, if the string has an lbearing < 0. */
2623 int left_overhang;
2624
2625 /* Number of pixels this string overwrites past its right-most
2626 nominal x-position, i.e. x + width. Zero if the string's
2627 rbearing is <= its nominal width, rbearing - width otherwise. */
2628 int right_overhang;
2629
2630 /* The frame on which the glyph string is drawn. */
2631 struct frame *f;
2632
2633 /* The window on which the glyph string is drawn. */
2634 struct window *w;
2635
2636 /* X display and window for convenience. */
2637 Window window;
2638
2639 /* The glyph row for which this string was built. It determines the
2640 y-origin and height of the string. */
2641 struct glyph_row *row;
2642
2643 /* The area within row. */
2644 enum glyph_row_area area;
2645
2646 /* Characters to be drawn, and number of characters. */
2647 wchar_t *char2b;
2648 int nchars;
2649
791f420f
JR
2650 /* A face-override for drawing cursors, mouse face and similar. */
2651 enum draw_glyphs_face hl;
2652
2653 /* Face in which this string is to be drawn. */
2654 struct face *face;
2655
2656 /* Font in which this string is to be drawn. */
2657 XFontStruct *font;
2658
2659 /* Font info for this string. */
2660 struct font_info *font_info;
2661
2662 /* Non-null means this string describes (part of) a composition.
2663 All characters from char2b are drawn composed. */
2664 struct composition *cmp;
2665
2666 /* Index of this glyph string's first character in the glyph
2667 definition of CMP. If this is zero, this glyph string describes
2668 the first character of a composition. */
2669 int gidx;
2670
2671 /* 1 means this glyph strings face has to be drawn to the right end
2672 of the window's drawing area. */
2673 unsigned extends_to_end_of_line_p : 1;
2674
2675 /* 1 means the background of this string has been drawn. */
2676 unsigned background_filled_p : 1;
2677
2678 /* 1 means glyph string must be drawn with 16-bit functions. */
2679 unsigned two_byte_p : 1;
2680
2681 /* 1 means that the original font determined for drawing this glyph
2682 string could not be loaded. The member `font' has been set to
2683 the frame's default font in this case. */
2684 unsigned font_not_found_p : 1;
2685
2686 /* 1 means that the face in which this glyph string is drawn has a
2687 stipple pattern. */
2688 unsigned stippled_p : 1;
2689
2690 /* 1 means only the foreground of this glyph string must be drawn,
2691 and we should use the physical height of the line this glyph
2692 string appears in as clip rect. */
2693 unsigned for_overlaps_p : 1;
2694
2695 /* The GC to use for drawing this glyph string. */
2696 XGCValues *gc;
2697
2698 HDC hdc;
2699
2700 /* A pointer to the first glyph in the string. This glyph
2701 corresponds to char2b[0]. Needed to draw rectangles if
2702 font_not_found_p is 1. */
2703 struct glyph *first_glyph;
2704
2705 /* Image, if any. */
2706 struct image *img;
2707
2708 struct glyph_string *next, *prev;
2709};
2710
2711
9ef2e2cf 2712/* Encapsulate the different ways of displaying text under W32. */
791f420f 2713
9127e20e 2714void W32_TEXTOUT (s, x, y,chars,nchars)
791f420f
JR
2715 struct glyph_string * s;
2716 int x, y;
2717 wchar_t * chars;
2718 int nchars;
2719{
9ef2e2cf 2720 int charset_dim = w32_font_is_double_byte (s->gc->font) ? 2 : 1;
791f420f
JR
2721 if (s->gc->font->bdf)
2722 w32_BDF_TextOut (s->gc->font->bdf, s->hdc,
00c96995
JR
2723 x, y, (char *) chars, charset_dim,
2724 nchars * charset_dim, 0);
9ef2e2cf 2725 else if (s->first_glyph->w32_font_type == UNICODE_FONT)
791f420f
JR
2726 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
2727 else
2728 ExtTextOut (s->hdc, x, y, 0, NULL, (char *) chars,
9ef2e2cf 2729 nchars * charset_dim, NULL);
791f420f
JR
2730}
2731
2732#if 0
2733
2734static void
2735x_dump_glyph_string (s)
2736 struct glyph_string *s;
2737{
2738 fprintf (stderr, "glyph string\n");
2739 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2740 s->x, s->y, s->width, s->height);
2741 fprintf (stderr, " ybase = %d\n", s->ybase);
2742 fprintf (stderr, " hl = %d\n", s->hl);
2743 fprintf (stderr, " left overhang = %d, right = %d\n",
2744 s->left_overhang, s->right_overhang);
2745 fprintf (stderr, " nchars = %d\n", s->nchars);
2746 fprintf (stderr, " extends to end of line = %d\n",
2747 s->extends_to_end_of_line_p);
2748 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2749 fprintf (stderr, " bg width = %d\n", s->background_width);
2750}
2751
2752#endif /* GLYPH_DEBUG */
2753
2754
2755
2756static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2757 struct glyph_string **,
2758 struct glyph_string *,
2759 struct glyph_string *));
2760static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2761 struct glyph_string **,
2762 struct glyph_string *,
2763 struct glyph_string *));
2764static void x_append_glyph_string P_ ((struct glyph_string **,
2765 struct glyph_string **,
2766 struct glyph_string *));
2767static int x_left_overwritten P_ ((struct glyph_string *));
2768static int x_left_overwriting P_ ((struct glyph_string *));
2769static int x_right_overwritten P_ ((struct glyph_string *));
2770static int x_right_overwriting P_ ((struct glyph_string *));
c2cc16fa
JR
2771static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2772 int));
791f420f 2773static void w32_init_glyph_string P_ ((struct glyph_string *, HDC hdc,
c2cc16fa
JR
2774 wchar_t *, struct window *,
2775 struct glyph_row *,
2776 enum glyph_row_area, int,
2777 enum draw_glyphs_face));
791f420f
JR
2778static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2779 enum glyph_row_area, int, int,
2780 enum draw_glyphs_face, int *, int *, int));
2781static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2782static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2783static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2784 int));
2785static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2786static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
791f420f
JR
2787static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2788static void x_draw_glyph_string P_ ((struct glyph_string *));
2789static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2790static void x_set_cursor_gc P_ ((struct glyph_string *));
2791static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2792static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2793static void w32_get_glyph_overhangs P_ ((HDC hdc, struct glyph *,
2794 struct frame *,
9ef2e2cf 2795 int *, int *));
791f420f
JR
2796static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2797static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
2798static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
2799 double, int, COLORREF));
2800static void x_setup_relief_colors P_ ((struct glyph_string *));
2801static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2802static void x_draw_image_relief P_ ((struct glyph_string *));
2803static void x_draw_image_foreground P_ ((struct glyph_string *));
2804static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
2805static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2806static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2807 int, int, int));
2808static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2809 int, int, int, int, RECT *));
2810static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2811 int, int, int, RECT *));
2812static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2813 enum glyph_row_area));
c2cc16fa
JR
2814static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2815 struct glyph_row *,
2816 enum glyph_row_area, int, int));
2817
2818#if GLYPH_DEBUG
2819static void x_check_font P_ ((struct frame *, XFontStruct *));
2820#endif
791f420f
JR
2821
2822
2823/* Append the list of glyph strings with head H and tail T to the list
2824 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2825
2826static INLINE void
2827x_append_glyph_string_lists (head, tail, h, t)
2828 struct glyph_string **head, **tail;
2829 struct glyph_string *h, *t;
2830{
2831 if (h)
2832 {
2833 if (*head)
2834 (*tail)->next = h;
2835 else
2836 *head = h;
2837 h->prev = *tail;
2838 *tail = t;
2839 }
2840}
2841
2842
2843/* Prepend the list of glyph strings with head H and tail T to the
2844 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2845 result. */
2846
2847static INLINE void
2848x_prepend_glyph_string_lists (head, tail, h, t)
2849 struct glyph_string **head, **tail;
2850 struct glyph_string *h, *t;
2851{
2852 if (h)
2853 {
2854 if (*head)
2855 (*head)->prev = t;
2856 else
2857 *tail = t;
2858 t->next = *head;
2859 *head = h;
2860 }
2861}
2862
2863
2864/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2865 Set *HEAD and *TAIL to the resulting list. */
2866
2867static INLINE void
2868x_append_glyph_string (head, tail, s)
2869 struct glyph_string **head, **tail;
2870 struct glyph_string *s;
2871{
2872 s->next = s->prev = NULL;
2873 x_append_glyph_string_lists (head, tail, s, s);
2874}
2875
2876
2877/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2878 face. */
2879
2880static void
2881x_set_cursor_gc (s)
2882 struct glyph_string *s;
2883{
2884 if (s->font == FRAME_FONT (s->f)
2885 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2886 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2887 && !s->cmp)
2888 s->gc = s->f->output_data.w32->cursor_gc;
2889 else
2890 {
2891 /* Cursor on non-default face: must merge. */
2892 XGCValues xgcv;
2893 unsigned long mask;
2894
2895 xgcv.background = s->f->output_data.w32->cursor_pixel;
2896 xgcv.foreground = s->face->background;
2897
2898 /* If the glyph would be invisible, try a different foreground. */
2899 if (xgcv.foreground == xgcv.background)
2900 xgcv.foreground = s->face->foreground;
2901 if (xgcv.foreground == xgcv.background)
2902 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
2903 if (xgcv.foreground == xgcv.background)
2904 xgcv.foreground = s->face->foreground;
2905
2906 /* Make sure the cursor is distinct from text in this face. */
2907 if (xgcv.background == s->face->background
2908 && xgcv.foreground == s->face->foreground)
2909 {
2910 xgcv.background = s->face->foreground;
2911 xgcv.foreground = s->face->background;
2912 }
2913
2914 IF_DEBUG (x_check_font (s->f, s->font));
2915 xgcv.font = s->font;
2916 mask = GCForeground | GCBackground | GCFont;
2917
2918 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2919 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2920 mask, &xgcv);
2921 else
2922 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2923 = XCreateGC (NULL, s->window, mask, &xgcv);
2924
2925 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2926 }
2927}
2928
2929
2930/* Set up S->gc of glyph string S for drawing text in mouse face. */
2931
2932static void
2933x_set_mouse_face_gc (s)
2934 struct glyph_string *s;
2935{
2936 int face_id;
93ff4395 2937 struct face *face;
791f420f 2938
c2cc16fa 2939 /* What face has to be used last for the mouse face? */
791f420f 2940 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
93ff4395 2941 face = FACE_FROM_ID (s->f, face_id);
c2cc16fa
JR
2942 if (face == NULL)
2943 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2944
2945 if (s->first_glyph->type == CHAR_GLYPH)
2946 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2947 else
2948 face_id = FACE_FOR_CHAR (s->f, face, 0);
791f420f
JR
2949 s->face = FACE_FROM_ID (s->f, face_id);
2950 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2951
2952 /* If font in this face is same as S->font, use it. */
2953 if (s->font == s->face->font)
2954 s->gc = s->face->gc;
2955 else
2956 {
2957 /* Otherwise construct scratch_cursor_gc with values from FACE
2958 but font FONT. */
2959 XGCValues xgcv;
2960 unsigned long mask;
2961
2962 xgcv.background = s->face->background;
2963 xgcv.foreground = s->face->foreground;
2964 IF_DEBUG (x_check_font (s->f, s->font));
2965 xgcv.font = s->font;
2966 mask = GCForeground | GCBackground | GCFont;
2967
2968 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2969 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2970 mask, &xgcv);
2971 else
2972 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2973 = XCreateGC (NULL, s->window, mask, &xgcv);
2974
2975 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2976 }
2977
2978 xassert (s->gc != 0);
2979}
2980
2981
2982/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2983 Faces to use in the mode line have already been computed when the
2984 matrix was built, so there isn't much to do, here. */
2985
2986static INLINE void
2987x_set_mode_line_face_gc (s)
2988 struct glyph_string *s;
2989{
2990 s->gc = s->face->gc;
791f420f
JR
2991}
2992
2993
2994/* Set S->gc of glyph string S for drawing that glyph string. Set
2995 S->stippled_p to a non-zero value if the face of S has a stipple
2996 pattern. */
2997
2998static INLINE void
2999x_set_glyph_string_gc (s)
3000 struct glyph_string *s;
3001{
ec48c3a7
JR
3002 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
3003
791f420f
JR
3004 if (s->hl == DRAW_NORMAL_TEXT)
3005 {
3006 s->gc = s->face->gc;
3007 s->stippled_p = s->face->stipple != 0;
3008 }
3009 else if (s->hl == DRAW_INVERSE_VIDEO)
3010 {
3011 x_set_mode_line_face_gc (s);
3012 s->stippled_p = s->face->stipple != 0;
3013 }
3014 else if (s->hl == DRAW_CURSOR)
3015 {
3016 x_set_cursor_gc (s);
3017 s->stippled_p = 0;
3018 }
3019 else if (s->hl == DRAW_MOUSE_FACE)
3020 {
3021 x_set_mouse_face_gc (s);
3022 s->stippled_p = s->face->stipple != 0;
3023 }
3024 else if (s->hl == DRAW_IMAGE_RAISED
3025 || s->hl == DRAW_IMAGE_SUNKEN)
3026 {
3027 s->gc = s->face->gc;
3028 s->stippled_p = s->face->stipple != 0;
3029 }
3030 else
3031 {
3032 s->gc = s->face->gc;
3033 s->stippled_p = s->face->stipple != 0;
3034 }
3035
3036 /* GC must have been set. */
3037 xassert (s->gc != 0);
3038}
3039
3040
3041/* Return in *R the clipping rectangle for glyph string S. */
3042
3043static void
3044w32_get_glyph_string_clip_rect (s, r)
3045 struct glyph_string *s;
3046 RECT *r;
3047{
3048 int r_height, r_width;
3049
3050 if (s->row->full_width_p)
3051 {
3052 /* Draw full-width. X coordinates are relative to S->w->left. */
3053 int canon_x = CANON_X_UNIT (s->f);
3054
3055 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
3056 r_width = XFASTINT (s->w->width) * canon_x;
3057
3058 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
3059 {
3060 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
3061 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
3062 r->left -= width;
3063 }
3064
3065 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
3066
3067 /* Unless displaying a mode or menu bar line, which are always
3068 fully visible, clip to the visible part of the row. */
3069 if (s->w->pseudo_window_p)
3070 r_height = s->row->visible_height;
3071 else
3072 r_height = s->height;
3073 }
3074 else
3075 {
3076 /* This is a text line that may be partially visible. */
3077 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
3078 r_width = window_box_width (s->w, s->area);
3079 r_height = s->row->visible_height;
3080 }
3081
3082 /* Don't use S->y for clipping because it doesn't take partially
3083 visible lines into account. For example, it can be negative for
3084 partially visible lines at the top of a window. */
3085 if (!s->row->full_width_p
3086 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
3087 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3088 else
3089 r->top = max (0, s->row->y);
3090
3091 /* If drawing a tool-bar window, draw it over the internal border
3092 at the top of the window. */
3093 if (s->w == XWINDOW (s->f->tool_bar_window))
3094 r->top -= s->f->output_data.w32->internal_border_width;
3095
3096 /* If S draws overlapping rows, it's sufficient to use the top and
3097 bottom of the window for clipping because this glyph string
3098 intentionally draws over other lines. */
3099 if (s->for_overlaps_p)
3100 {
3101 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3102 r_height = window_text_bottom_y (s->w) - r->top;
3103 }
3104
3105 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
3106
3107 r->bottom = r->top + r_height;
3108 r->right = r->left + r_width;
3109}
3110
3111
3112/* Set clipping for output of glyph string S. S may be part of a mode
3113 line or menu if we don't have X toolkit support. */
3114
3115static INLINE void
3116x_set_glyph_string_clipping (s)
3117 struct glyph_string *s;
3118{
3119 RECT r;
3120 w32_get_glyph_string_clip_rect (s, &r);
3121 w32_set_clip_rectangle (s->hdc, &r);
3122}
3123
3124
3125/* Compute left and right overhang of glyph string S. If S is a glyph
3126 string for a composition, assume overhangs don't exist. */
3127
3128static INLINE void
3129x_compute_glyph_string_overhangs (s)
3130 struct glyph_string *s;
3131{
01b220b6 3132 /* TODO: Windows does not appear to have a method for
158cba56
JR
3133 getting this info without getting the ABC widths for each
3134 individual character and working it out manually. */
791f420f
JR
3135}
3136
3137
3138/* Compute overhangs and x-positions for glyph string S and its
3139 predecessors, or successors. X is the starting x-position for S.
3140 BACKWARD_P non-zero means process predecessors. */
3141
3142static void
3143x_compute_overhangs_and_x (s, x, backward_p)
3144 struct glyph_string *s;
3145 int x;
3146 int backward_p;
3147{
3148 if (backward_p)
3149 {
3150 while (s)
3151 {
3152 x_compute_glyph_string_overhangs (s);
3153 x -= s->width;
3154 s->x = x;
3155 s = s->prev;
3156 }
3157 }
3158 else
3159 {
3160 while (s)
3161 {
3162 x_compute_glyph_string_overhangs (s);
3163 s->x = x;
3164 x += s->width;
3165 s = s->next;
3166 }
3167 }
3168}
3169
3170
3171/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
3172 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
3173 assumed to be zero. */
3174
3175static void
9ef2e2cf 3176w32_get_glyph_overhangs (hdc, glyph, f, left, right)
00fe468b 3177 HDC hdc;
791f420f
JR
3178 struct glyph *glyph;
3179 struct frame *f;
9ef2e2cf 3180 int *left, *right;
791f420f 3181{
791f420f
JR
3182 *left = *right = 0;
3183
3184 if (glyph->type == CHAR_GLYPH)
3185 {
3186 XFontStruct *font;
3187 struct face *face;
791f420f 3188 wchar_t char2b;
93ff4395
JR
3189 XCharStruct *pcm;
3190
3191 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
791f420f 3192 font = face->font;
9ef2e2cf 3193
93ff4395 3194 if (font
82f9d565 3195 && (pcm = w32_per_char_metric (font, &char2b,
9ef2e2cf 3196 glyph->w32_font_type)))
791f420f 3197 {
791f420f
JR
3198 if (pcm->rbearing > pcm->width)
3199 *right = pcm->rbearing - pcm->width;
3200 if (pcm->lbearing < 0)
3201 *left = -pcm->lbearing;
3202 }
3203 }
791f420f
JR
3204}
3205
3206
3207static void
3208x_get_glyph_overhangs (glyph, f, left, right)
3209 struct glyph *glyph;
3210 struct frame *f;
3211 int *left, *right;
3212{
3213 HDC hdc = get_frame_dc (f);
9ef2e2cf
JR
3214 /* Convert to unicode! */
3215 w32_get_glyph_overhangs (hdc, glyph, f, left, right);
791f420f
JR
3216 release_frame_dc (f, hdc);
3217}
3218
3219
3220/* Return the index of the first glyph preceding glyph string S that
3221 is overwritten by S because of S's left overhang. Value is -1
3222 if no glyphs are overwritten. */
3223
3224static int
3225x_left_overwritten (s)
3226 struct glyph_string *s;
3227{
3228 int k;
3229
3230 if (s->left_overhang)
3231 {
3232 int x = 0, i;
3233 struct glyph *glyphs = s->row->glyphs[s->area];
3234 int first = s->first_glyph - glyphs;
3235
3236 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3237 x -= glyphs[i].pixel_width;
3238
3239 k = i + 1;
3240 }
3241 else
3242 k = -1;
3243
3244 return k;
3245}
3246
3247
3248/* Return the index of the first glyph preceding glyph string S that
3249 is overwriting S because of its right overhang. Value is -1 if no
3250 glyph in front of S overwrites S. */
3251
3252static int
3253x_left_overwriting (s)
3254 struct glyph_string *s;
3255{
3256 int i, k, x;
3257 struct glyph *glyphs = s->row->glyphs[s->area];
3258 int first = s->first_glyph - glyphs;
3259
3260 k = -1;
3261 x = 0;
3262 for (i = first - 1; i >= 0; --i)
3263 {
3264 int left, right;
9ef2e2cf 3265 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
791f420f
JR
3266 if (x + right > 0)
3267 k = i;
3268 x -= glyphs[i].pixel_width;
3269 }
3270
3271 return k;
3272}
3273
3274
3275/* Return the index of the last glyph following glyph string S that is
3276 not overwritten by S because of S's right overhang. Value is -1 if
3277 no such glyph is found. */
3278
3279static int
3280x_right_overwritten (s)
3281 struct glyph_string *s;
3282{
3283 int k = -1;
3284
3285 if (s->right_overhang)
3286 {
3287 int x = 0, i;
3288 struct glyph *glyphs = s->row->glyphs[s->area];
3289 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3290 int end = s->row->used[s->area];
3291
3292 for (i = first; i < end && s->right_overhang > x; ++i)
3293 x += glyphs[i].pixel_width;
3294
3295 k = i;
3296 }
3297
3298 return k;
3299}
3300
3301
3302/* Return the index of the last glyph following glyph string S that
3303 overwrites S because of its left overhang. Value is negative
3304 if no such glyph is found. */
3305
3306static int
3307x_right_overwriting (s)
3308 struct glyph_string *s;
3309{
3310 int i, k, x;
3311 int end = s->row->used[s->area];
3312 struct glyph *glyphs = s->row->glyphs[s->area];
3313 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3314
3315 k = -1;
3316 x = 0;
3317 for (i = first; i < end; ++i)
3318 {
3319 int left, right;
9ef2e2cf 3320 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
791f420f
JR
3321 if (x - left < 0)
3322 k = i;
3323 x += glyphs[i].pixel_width;
3324 }
3325
3326 return k;
3327}
3328
3329
3330/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3331
3332static INLINE void
3333x_clear_glyph_string_rect (s, x, y, w, h)
3334 struct glyph_string *s;
3335 int x, y, w, h;
3336{
3337 int real_x = x;
3338 int real_y = y;
3339 int real_w = w;
3340 int real_h = h;
3341#if 0
3342 /* Take clipping into account. */
3343 if (s->gc->clip_mask == Rect)
3344 {
3345 real_x = max (real_x, s->gc->clip_rectangle.left);
3346 real_y = max (real_y, s->gc->clip_rectangle.top);
3347 real_w = min (real_w, s->gc->clip_rectangle.right
3348 - s->gc->clip_rectangle.left);
3349 real_h = min (real_h, s->gc->clip_rectangle.bottom
3350 - s->gc->clip_rectangle.top);
3351 }
3352#endif
3353 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
3354 real_w, real_h);
3355}
3356
3357
3358/* Draw the background of glyph_string S. If S->background_filled_p
3359 is non-zero don't draw it. FORCE_P non-zero means draw the
3360 background even if it wouldn't be drawn normally. This is used
3361 when a string preceding S draws into the background of S, or S
3362 contains the first component of a composition. */
3363
3364static void
3365x_draw_glyph_string_background (s, force_p)
3366 struct glyph_string *s;
3367 int force_p;
3368{
3369 /* Nothing to do if background has already been drawn or if it
3370 shouldn't be drawn in the first place. */
3371 if (!s->background_filled_p)
3372 {
60222d69
AI
3373 int box_line_width = max (s->face->box_line_width, 0);
3374
01b220b6 3375#if 0 /* TODO: stipple */
791f420f
JR
3376 if (s->stippled_p)
3377 {
3378 /* Fill background with a stipple pattern. */
3379 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3380 XFillRectangle (s->display, s->window, s->gc, s->x,
60222d69 3381 s->y + box_line_width,
791f420f 3382 s->background_width,
60222d69 3383 s->height - 2 * box_line_width);
791f420f
JR
3384 XSetFillStyle (s->display, s->gc, FillSolid);
3385 s->background_filled_p = 1;
3386 }
3387 else
3388#endif
60222d69 3389 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
791f420f
JR
3390 || s->font_not_found_p
3391 || s->extends_to_end_of_line_p
9127e20e 3392 || s->font->bdf
791f420f
JR
3393 || force_p)
3394 {
60222d69 3395 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
791f420f 3396 s->background_width,
60222d69 3397 s->height - 2 * box_line_width);
791f420f
JR
3398 s->background_filled_p = 1;
3399 }
3400 }
3401}
3402
3403
3404/* Draw the foreground of glyph string S. */
3405
3406static void
3407x_draw_glyph_string_foreground (s)
3408 struct glyph_string *s;
3409{
3410 int i, x;
01b220b6 3411 HFONT old_font;
791f420f
JR
3412
3413 /* If first glyph of S has a left box line, start drawing the text
3414 of S to the right of that box line. */
3415 if (s->face->box != FACE_NO_BOX
3416 && s->first_glyph->left_box_line_p)
60222d69 3417 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3418 else
3419 x = s->x;
3420
3421 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
3422 SetBkMode (s->hdc, TRANSPARENT);
3423 else
3424 SetBkMode (s->hdc, OPAQUE);
3425
3426 SetTextColor (s->hdc, s->gc->foreground);
3427 SetBkColor (s->hdc, s->gc->background);
3428 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3429
3430 if (s->font && s->font->hfont)
01b220b6 3431 old_font = SelectObject (s->hdc, s->font->hfont);
791f420f
JR
3432
3433 /* Draw characters of S as rectangles if S's font could not be
3434 loaded. */
3435 if (s->font_not_found_p)
3436 {
3437 for (i = 0; i < s->nchars; ++i)
3438 {
3439 struct glyph *g = s->first_glyph + i;
3440
3441 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
3442 s->height - 1);
3443 x += g->pixel_width;
3444 }
3445 }
3446 else
3447 {
3448 char *char1b = (char *) s->char2b;
3449 int boff = s->font_info->baseline_offset;
3450
3451 if (s->font_info->vertical_centering)
3452 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3453
3454 /* If we can use 8-bit functions, condense S->char2b. */
3455 if (!s->two_byte_p)
3456 for (i = 0; i < s->nchars; ++i)
3457 char1b[i] = BYTE2 (s->char2b[i]);
3458
3459 /* Draw text with TextOut and friends. */
3460 W32_TEXTOUT (s, x, s->ybase - boff, s->char2b, s->nchars);
3461 }
01b220b6
JR
3462 if (s->font && s->font->hfont)
3463 SelectObject (s->hdc, old_font);
791f420f
JR
3464}
3465
3466/* Draw the foreground of composite glyph string S. */
3467
3468static void
3469x_draw_composite_glyph_string_foreground (s)
3470 struct glyph_string *s;
3471{
3472 int i, x;
01b220b6 3473 HFONT old_font;
791f420f
JR
3474
3475 /* If first glyph of S has a left box line, start drawing the text
3476 of S to the right of that box line. */
3477 if (s->face->box != FACE_NO_BOX
3478 && s->first_glyph->left_box_line_p)
60222d69 3479 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3480 else
3481 x = s->x;
3482
3483 /* S is a glyph string for a composition. S->gidx is the index of
3484 the first character drawn for glyphs of this composition.
3485 S->gidx == 0 means we are drawing the very first character of
3486 this composition. */
3487
3488 SetTextColor (s->hdc, s->gc->foreground);
3489 SetBkColor (s->hdc, s->gc->background);
3490 SetBkMode (s->hdc, TRANSPARENT);
3491 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3492
01b220b6
JR
3493 if (s->font && s->font->hfont)
3494 old_font = SelectObject (s->hdc, s->font->hfont);
3495
791f420f
JR
3496 /* Draw a rectangle for the composition if the font for the very
3497 first character of the composition could not be loaded. */
3498 if (s->font_not_found_p)
3499 {
3500 if (s->gidx == 0)
3501 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
3502 s->height - 1);
3503 }
3504 else
3505 {
3506 for (i = 0; i < s->nchars; i++, ++s->gidx)
01b220b6
JR
3507 W32_TEXTOUT (s, x + s->cmp->offsets[s->gidx * 2],
3508 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3509 s->char2b + i, 1);
791f420f 3510 }
01b220b6
JR
3511 if (s->font && s->font->hfont)
3512 SelectObject (s->hdc, old_font);
791f420f
JR
3513}
3514
e5fa381b
JR
3515
3516/* Brightness beyond which a color won't have its highlight brightness
3517 boosted.
3518
3519 Nominally, highlight colors for `3d' faces are calculated by
3520 brightening an object's color by a constant scale factor, but this
3521 doesn't yield good results for dark colors, so for colors who's
3522 brightness is less than this value (on a scale of 0-255) have to
3523 use an additional additive factor.
3524
3525 The value here is set so that the default menu-bar/mode-line color
3526 (grey75) will not have its highlights changed at all. */
3527#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
3528
3529
791f420f
JR
3530/* Allocate a color which is lighter or darker than *COLOR by FACTOR
3531 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3532 If this produces the same color as COLOR, try a color where all RGB
3533 values have DELTA added. Return the allocated color in *COLOR.
3534 DISPLAY is the X display, CMAP is the colormap to operate on.
3535 Value is non-zero if successful. */
3536
3537static int
3538w32_alloc_lighter_color (f, color, factor, delta)
3539 struct frame *f;
3540 COLORREF *color;
3541 double factor;
3542 int delta;
3543{
3544 COLORREF new;
e5fa381b
JR
3545 long bright;
3546
3547 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
3548 delta /= 256;
791f420f
JR
3549
3550 /* Change RGB values by specified FACTOR. Avoid overflow! */
3551 xassert (factor >= 0);
3552 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
3553 min (0xff, factor * GetGValue (*color)),
3554 min (0xff, factor * GetBValue (*color)));
e5fa381b
JR
3555
3556 /* Calculate brightness of COLOR. */
3557 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
3558 + GetBValue (*color)) / 6;
3559
3560 /* We only boost colors that are darker than
3561 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3562 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3563 /* Make an additive adjustment to NEW, because it's dark enough so
3564 that scaling by FACTOR alone isn't enough. */
3565 {
3566 /* How far below the limit this color is (0 - 1, 1 being darker). */
3567 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3568 /* The additive adjustment. */
3569 int min_delta = delta * dimness * factor / 2;
3570
3571 if (factor < 1)
3572 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
3573 max (0, min (0xff, min_delta - GetGValue (*color))),
3574 max (0, min (0xff, min_delta - GetBValue (*color))));
3575 else
3576 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
3577 max (0, min (0xff, min_delta + GetGValue (*color))),
3578 max (0, min (0xff, min_delta + GetBValue (*color))));
3579 }
3580
791f420f
JR
3581 if (new == *color)
3582 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
3583 max (0, min (0xff, delta + GetGValue (*color))),
3584 max (0, min (0xff, delta + GetBValue (*color))));
3585
01b220b6
JR
3586 /* TODO: Map to palette and retry with delta if same? */
3587 /* TODO: Free colors (if using palette)? */
93ff4395 3588
791f420f
JR
3589 if (new == *color)
3590 return 0;
3591
3592 *color = new;
3593
3594 return 1;
3595}
3596
3597
3598/* Set up the foreground color for drawing relief lines of glyph
3599 string S. RELIEF is a pointer to a struct relief containing the GC
3600 with which lines will be drawn. Use a color that is FACTOR or
3601 DELTA lighter or darker than the relief's background which is found
3602 in S->f->output_data.x->relief_background. If such a color cannot
3603 be allocated, use DEFAULT_PIXEL, instead. */
3604
3605static void
3606w32_setup_relief_color (f, relief, factor, delta, default_pixel)
3607 struct frame *f;
3608 struct relief *relief;
3609 double factor;
3610 int delta;
3611 COLORREF default_pixel;
3612{
3613 XGCValues xgcv;
3614 struct w32_output *di = f->output_data.w32;
3615 unsigned long mask = GCForeground;
3616 COLORREF pixel;
3617 COLORREF background = di->relief_background;
3618 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3619
01b220b6 3620 /* TODO: Free colors (if using palette)? */
93ff4395 3621
791f420f
JR
3622 /* Allocate new color. */
3623 xgcv.foreground = default_pixel;
3624 pixel = background;
3625 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
3626 {
3627 relief->allocated_p = 1;
3628 xgcv.foreground = relief->pixel = pixel;
3629 }
3630
3631 if (relief->gc == 0)
3632 {
01b220b6 3633#if 0 /* TODO: stipple */
791f420f
JR
3634 xgcv.stipple = dpyinfo->gray;
3635 mask |= GCStipple;
3636#endif
3637 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
3638 }
3639 else
3640 XChangeGC (NULL, relief->gc, mask, &xgcv);
3641}
3642
3643
3644/* Set up colors for the relief lines around glyph string S. */
3645
3646static void
3647x_setup_relief_colors (s)
3648 struct glyph_string *s;
3649{
3650 struct w32_output *di = s->f->output_data.w32;
3651 COLORREF color;
3652
3653 if (s->face->use_box_color_for_shadows_p)
3654 color = s->face->box_color;
3655 else
3656 color = s->gc->background;
3657
3658 if (di->white_relief.gc == 0
3659 || color != di->relief_background)
3660 {
3661 di->relief_background = color;
3662 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3663 WHITE_PIX_DEFAULT (s->f));
3664 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3665 BLACK_PIX_DEFAULT (s->f));
3666 }
3667}
3668
3669
3670/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3671 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3672 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3673 relief. LEFT_P non-zero means draw a relief on the left side of
3674 the rectangle. RIGHT_P non-zero means draw a relief on the right
3675 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3676 when drawing. */
3677
3678static void
3679w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3680 raised_p, left_p, right_p, clip_rect)
3681 struct frame *f;
3682 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3683 RECT *clip_rect;
3684{
3685 int i;
3686 XGCValues gc;
3687 HDC hdc = get_frame_dc (f);
3688
3689 if (raised_p)
e5fa381b 3690 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
791f420f 3691 else
e5fa381b 3692 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
791f420f
JR
3693
3694 w32_set_clip_rectangle (hdc, clip_rect);
3695
3696 /* Top. */
3697 for (i = 0; i < width; ++i)
3698 {
3699 w32_fill_area (f, hdc, gc.foreground,
3700 left_x + i * left_p, top_y + i,
3701 (right_x + 1 - i * right_p) - (left_x + i * left_p), 1);
3702 }
3703
3704 /* Left. */
3705 if (left_p)
3706 for (i = 0; i < width; ++i)
3707 {
3708 w32_fill_area (f, hdc, gc.foreground,
3709 left_x + i, top_y + i, 1,
3710 (bottom_y - i) - (top_y + i));
3711 }
3712
3713 w32_set_clip_rectangle (hdc, NULL);
3714
3715 if (raised_p)
e5fa381b 3716 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
791f420f 3717 else
e5fa381b
JR
3718 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3719
791f420f
JR
3720
3721 w32_set_clip_rectangle (hdc, clip_rect);
3722
3723 /* Bottom. */
3724 for (i = 0; i < width; ++i)
3725 {
3726 w32_fill_area (f, hdc, gc.foreground,
3727 left_x + i * left_p, bottom_y - i,
3728 (right_x + 1 - i * right_p) - left_x + i * left_p, 1);
3729 }
3730
3731 /* Right. */
3732 if (right_p)
3733 for (i = 0; i < width; ++i)
3734 {
3735 w32_fill_area (f, hdc, gc.foreground,
3736 right_x - i, top_y + i + 1, 1,
3737 (bottom_y - i) - (top_y + i + 1));
3738 }
3739
3740 w32_set_clip_rectangle (hdc, NULL);
3741
3742 release_frame_dc (f, hdc);
3743}
3744
3745
3746/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3747 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3748 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3749 left side of the rectangle. RIGHT_P non-zero means draw a line
3750 on the right side of the rectangle. CLIP_RECT is the clipping
3751 rectangle to use when drawing. */
3752
3753static void
3754w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3755 left_p, right_p, clip_rect)
3756 struct glyph_string *s;
3757 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3758 RECT *clip_rect;
3759{
00fe468b 3760 w32_set_clip_rectangle (s->hdc, clip_rect);
791f420f
JR
3761
3762 /* Top. */
00fe468b 3763 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3764 left_x, top_y, right_x - left_x + 1, width);
791f420f
JR
3765
3766 /* Left. */
3767 if (left_p)
3768 {
00fe468b 3769 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3770 left_x, top_y, width, bottom_y - top_y + 1);
791f420f
JR
3771 }
3772
3773 /* Bottom. */
00fe468b 3774 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3775 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
791f420f
JR
3776
3777 /* Right. */
3778 if (right_p)
3779 {
00fe468b 3780 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3781 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
791f420f
JR
3782 }
3783
00fe468b 3784 w32_set_clip_rectangle (s->hdc, NULL);
791f420f
JR
3785}
3786
3787
3788/* Draw a box around glyph string S. */
3789
3790static void
3791x_draw_glyph_string_box (s)
3792 struct glyph_string *s;
3793{
3794 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3795 int left_p, right_p;
3796 struct glyph *last_glyph;
3797 RECT clip_rect;
3798
3799 last_x = window_box_right (s->w, s->area);
3800 if (s->row->full_width_p
3801 && !s->w->pseudo_window_p)
3802 {
3803 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3804 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3805 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3806 }
3807
3808 /* The glyph that may have a right box line. */
3809 last_glyph = (s->cmp || s->img
3810 ? s->first_glyph
3811 : s->first_glyph + s->nchars - 1);
3812
60222d69 3813 width = abs (s->face->box_line_width);
791f420f
JR
3814 raised_p = s->face->box == FACE_RAISED_BOX;
3815 left_x = s->x;
3816 right_x = ((s->row->full_width_p
3817 ? last_x - 1
3818 : min (last_x, s->x + s->background_width) - 1));
3819 top_y = s->y;
3820 bottom_y = top_y + s->height - 1;
3821
3822 left_p = (s->first_glyph->left_box_line_p
3823 || (s->hl == DRAW_MOUSE_FACE
3824 && (s->prev == NULL
3825 || s->prev->hl != s->hl)));
3826 right_p = (last_glyph->right_box_line_p
3827 || (s->hl == DRAW_MOUSE_FACE
3828 && (s->next == NULL
3829 || s->next->hl != s->hl)));
3830
3831 w32_get_glyph_string_clip_rect (s, &clip_rect);
3832
3833 if (s->face->box == FACE_SIMPLE_BOX)
3834 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3835 left_p, right_p, &clip_rect);
3836 else
3837 {
3838 x_setup_relief_colors (s);
3839 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3840 width, raised_p, left_p, right_p, &clip_rect);
3841 }
3842}
3843
3844
3845/* Draw foreground of image glyph string S. */
3846
3847static void
3848x_draw_image_foreground (s)
3849 struct glyph_string *s;
3850{
3851 int x;
9ef2e2cf 3852 int y = s->ybase - image_ascent (s->img, s->face);
791f420f
JR
3853
3854 /* If first glyph of S has a left box line, start drawing it to the
3855 right of that line. */
3856 if (s->face->box != FACE_NO_BOX
3857 && s->first_glyph->left_box_line_p)
60222d69 3858 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3859 else
3860 x = s->x;
3861
3862 /* If there is a margin around the image, adjust x- and y-position
3863 by that margin. */
d6ff54d5
JR
3864 x += s->img->hmargin;
3865 y += s->img->vmargin;
791f420f
JR
3866
3867 SaveDC (s->hdc);
3868
3869 if (s->img->pixmap)
3870 {
01b220b6 3871#if 0 /* TODO: image mask */
791f420f
JR
3872 if (s->img->mask)
3873 {
3874 /* We can't set both a clip mask and use XSetClipRectangles
3875 because the latter also sets a clip mask. We also can't
3876 trust on the shape extension to be available
3877 (XShapeCombineRegion). So, compute the rectangle to draw
3878 manually. */
3879 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3880 | GCFunction);
3881 XGCValues xgcv;
3882 XRectangle clip_rect, image_rect, r;
3883
3884 xgcv.clip_mask = s->img->mask;
3885 xgcv.clip_x_origin = x;
3886 xgcv.clip_y_origin = y;
3887 xgcv.function = GXcopy;
3888 XChangeGC (s->display, s->gc, mask, &xgcv);
3889
3890 w32_get_glyph_string_clip_rect (s, &clip_rect);
3891 image_rect.x = x;
3892 image_rect.y = y;
3893 image_rect.width = s->img->width;
3894 image_rect.height = s->img->height;
158cba56 3895 if (IntersectRect (&r, &clip_rect, &image_rect))
791f420f
JR
3896 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3897 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3898 }
3899 else
3900#endif
3901 {
3902 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3903 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3904 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3905 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
158cba56 3906 x_set_glyph_string_clipping (s);
791f420f
JR
3907
3908 SetTextColor (s->hdc, s->gc->foreground);
3909 SetBkColor (s->hdc, s->gc->background);
3910#if 0 /* From w32bdf.c (which is from Meadow). */
3911 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3912 compat_hdc, 0, 0, SRCCOPY);
791f420f
JR
3913 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3914 compat_hdc, 0, 0, 0xB8074A);
9436cdf9
JR
3915#else
3916 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3917 compat_hdc, 0, 0, 0xE20746);
791f420f
JR
3918#endif
3919 SelectObject (s->hdc, orig_brush);
3920 DeleteObject (fg_brush);
3921 SelectObject (compat_hdc, orig_obj);
3922 DeleteDC (compat_hdc);
3923
3924 /* When the image has a mask, we can expect that at
3925 least part of a mouse highlight or a block cursor will
3926 be visible. If the image doesn't have a mask, make
3927 a block cursor visible by drawing a rectangle around
3928 the image. I believe it's looking better if we do
3929 nothing here for mouse-face. */
3930 if (s->hl == DRAW_CURSOR)
3931 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width - 1,
3932 s->img->height - 1);
9127e20e 3933 w32_set_clip_rectangle (s->hdc, NULL);
791f420f
JR
3934 }
3935 }
3936 else
3937 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width -1,
3938 s->img->height - 1);
ee78dc32 3939
791f420f
JR
3940 RestoreDC (s->hdc ,-1);
3941}
ee78dc32 3942
791f420f
JR
3943
3944
3945/* Draw a relief around the image glyph string S. */
3946
3947static void
3948x_draw_image_relief (s)
3949 struct glyph_string *s;
3950{
3951 int x0, y0, x1, y1, thick, raised_p;
3952 RECT r;
3953 int x;
9ef2e2cf
JR
3954 int y = s->ybase - image_ascent (s->img, s->face);
3955
791f420f
JR
3956 /* If first glyph of S has a left box line, start drawing it to the
3957 right of that line. */
3958 if (s->face->box != FACE_NO_BOX
3959 && s->first_glyph->left_box_line_p)
60222d69 3960 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3961 else
3962 x = s->x;
3963
3964 /* If there is a margin around the image, adjust x- and y-position
3965 by that margin. */
d6ff54d5
JR
3966 x += s->img->hmargin;
3967 y += s->img->vmargin;
791f420f
JR
3968
3969 if (s->hl == DRAW_IMAGE_SUNKEN
3970 || s->hl == DRAW_IMAGE_RAISED)
3971 {
3972 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3973 raised_p = s->hl == DRAW_IMAGE_RAISED;
3974 }
3975 else
ee78dc32 3976 {
791f420f
JR
3977 thick = abs (s->img->relief);
3978 raised_p = s->img->relief > 0;
3979 }
3980
3981 x0 = x - thick;
3982 y0 = y - thick;
3983 x1 = x + s->img->width + thick - 1;
3984 y1 = y + s->img->height + thick - 1;
3985
3986 x_setup_relief_colors (s);
3987 w32_get_glyph_string_clip_rect (s, &r);
3988 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3989}
ee78dc32 3990
791f420f
JR
3991
3992/* Draw the foreground of image glyph string S to PIXMAP. */
3993
3994static void
3995w32_draw_image_foreground_1 (s, pixmap)
3996 struct glyph_string *s;
3997 HBITMAP pixmap;
3998{
3999 HDC hdc = CreateCompatibleDC (s->hdc);
4000 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
4001 int x;
9ef2e2cf 4002 int y = s->ybase - s->y - image_ascent (s->img, s->face);
791f420f
JR
4003
4004 /* If first glyph of S has a left box line, start drawing it to the
4005 right of that line. */
4006 if (s->face->box != FACE_NO_BOX
4007 && s->first_glyph->left_box_line_p)
60222d69 4008 x = abs (s->face->box_line_width);
791f420f
JR
4009 else
4010 x = 0;
4011
4012 /* If there is a margin around the image, adjust x- and y-position
4013 by that margin. */
d6ff54d5
JR
4014 x += s->img->hmargin;
4015 y += s->img->vmargin;
791f420f
JR
4016
4017 if (s->img->pixmap)
4018 {
01b220b6 4019#if 0 /* TODO: image mask */
791f420f
JR
4020 if (s->img->mask)
4021 {
4022 /* We can't set both a clip mask and use XSetClipRectangles
4023 because the latter also sets a clip mask. We also can't
4024 trust on the shape extension to be available
4025 (XShapeCombineRegion). So, compute the rectangle to draw
4026 manually. */
4027 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4028 | GCFunction);
4029 XGCValues xgcv;
4030
4031 xgcv.clip_mask = s->img->mask;
4032 xgcv.clip_x_origin = x;
4033 xgcv.clip_y_origin = y;
4034 xgcv.function = GXcopy;
4035 XChangeGC (s->display, s->gc, mask, &xgcv);
4036
4037 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4038 0, 0, s->img->width, s->img->height, x, y);
4039 XSetClipMask (s->display, s->gc, None);
4040 }
4041 else
4042#endif
ef0e360f 4043 {
791f420f
JR
4044 HDC compat_hdc = CreateCompatibleDC (hdc);
4045 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4046 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
4047 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
4048
4049 SetTextColor (hdc, s->gc->foreground);
4050 SetBkColor (hdc, s->gc->background);
4051#if 0 /* From w32bdf.c (which is from Meadow). */
4052 BitBlt (hdc, x, y, s->img->width, s->img->height,
4053 compat_hdc, 0, 0, SRCCOPY);
791f420f
JR
4054 BitBlt (hdc, x, y, s->img->width, s->img->height,
4055 compat_hdc, 0, 0, 0xB8074A);
9436cdf9
JR
4056#else
4057 BitBlt (hdc, x, y, s->img->width, s->img->height,
4058 compat_hdc, 0, 0, 0xE20746);
791f420f
JR
4059#endif
4060 SelectObject (hdc, orig_brush);
4061 DeleteObject (fg_brush);
4062 SelectObject (compat_hdc, orig_obj);
4063 DeleteDC (compat_hdc);
4064
4065 /* When the image has a mask, we can expect that at
4066 least part of a mouse highlight or a block cursor will
4067 be visible. If the image doesn't have a mask, make
4068 a block cursor visible by drawing a rectangle around
4069 the image. I believe it's looking better if we do
4070 nothing here for mouse-face. */
4071 if (s->hl == DRAW_CURSOR)
4072 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4073 s->img->height - 1);
ef0e360f 4074 }
791f420f
JR
4075 }
4076 else
4077 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4078 s->img->height - 1);
4079
4080 SelectObject (hdc, orig_hdc_obj);
4081 DeleteDC (hdc);
4082}
4083
4084
4085/* Draw part of the background of glyph string S. X, Y, W, and H
4086 give the rectangle to draw. */
4087
4088static void
4089x_draw_glyph_string_bg_rect (s, x, y, w, h)
4090 struct glyph_string *s;
4091 int x, y, w, h;
4092{
01b220b6 4093#if 0 /* TODO: stipple */
791f420f
JR
4094 if (s->stippled_p)
4095 {
4096 /* Fill background with a stipple pattern. */
4097 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4098 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4099 XSetFillStyle (s->display, s->gc, FillSolid);
4100 }
4101 else
4102#endif
4103 x_clear_glyph_string_rect (s, x, y, w, h);
4104}
4105
4106
4107/* Draw image glyph string S.
4108
4109 s->y
4110 s->x +-------------------------
4111 | s->face->box
4112 |
4113 | +-------------------------
d6ff54d5 4114 | | s->img->vmargin
791f420f
JR
4115 | |
4116 | | +-------------------
4117 | | | the image
cabb23bc 4118
791f420f
JR
4119 */
4120
4121static void
4122x_draw_image_glyph_string (s)
4123 struct glyph_string *s;
4124{
4125 int x, y;
60222d69
AI
4126 int box_line_hwidth = abs (s->face->box_line_width);
4127 int box_line_vwidth = max (s->face->box_line_width, 0);
791f420f
JR
4128 int height;
4129 HBITMAP pixmap = 0;
4130
60222d69 4131 height = s->height - 2 * box_line_vwidth;
791f420f
JR
4132
4133 /* Fill background with face under the image. Do it only if row is
4134 taller than image or if image has a clip mask to reduce
4135 flickering. */
4136 s->stippled_p = s->face->stipple != 0;
4137 if (height > s->img->height
d6ff54d5 4138 || s->img->hmargin
c2cc16fa 4139 || s->img->vmargin
01b220b6 4140#if 0 /* TODO: image mask */
791f420f
JR
4141 || s->img->mask
4142#endif
4143 || s->img->pixmap == 0
4144 || s->width != s->background_width)
4145 {
60222d69
AI
4146 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4147 x = s->x + box_line_hwidth;
791f420f
JR
4148 else
4149 x = s->x;
4150
60222d69 4151 y = s->y + box_line_vwidth;
01b220b6 4152#if 0 /* TODO: image mask */
791f420f 4153 if (s->img->mask)
ee78dc32 4154 {
c2cc16fa
JR
4155 /* Create a pixmap as large as the glyph string. Fill it
4156 with the background color. Copy the image to it, using
4157 its mask. Copy the temporary pixmap to the display. */
791f420f
JR
4158 Screen *screen = FRAME_X_SCREEN (s->f);
4159 int depth = DefaultDepthOfScreen (screen);
4160
4161 /* Create a pixmap as large as the glyph string. */
4162 pixmap = XCreatePixmap (s->display, s->window,
4163 s->background_width,
4164 s->height, depth);
4165
4166 /* Don't clip in the following because we're working on the
4167 pixmap. */
4168 XSetClipMask (s->display, s->gc, None);
ee78dc32 4169
791f420f
JR
4170 /* Fill the pixmap with the background color/stipple. */
4171 if (s->stippled_p)
4172 {
4173 /* Fill background with a stipple pattern. */
4174 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4175 XFillRectangle (s->display, pixmap, s->gc,
4176 0, 0, s->background_width, s->height);
4177 XSetFillStyle (s->display, s->gc, FillSolid);
4178 }
ef0e360f 4179 else
791f420f
JR
4180 {
4181 XGCValues xgcv;
4182 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4183 &xgcv);
4184 XSetForeground (s->display, s->gc, xgcv.background);
4185 XFillRectangle (s->display, pixmap, s->gc,
4186 0, 0, s->background_width, s->height);
4187 XSetForeground (s->display, s->gc, xgcv.foreground);
4188 }
ee78dc32 4189 }
791f420f
JR
4190 else
4191#endif
791f420f
JR
4192 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4193
4194 s->background_filled_p = 1;
4195 }
ee78dc32 4196
791f420f
JR
4197 /* Draw the foreground. */
4198 if (pixmap != 0)
4199 {
4200 w32_draw_image_foreground_1 (s, pixmap);
4201 x_set_glyph_string_clipping (s);
ee78dc32 4202 {
791f420f
JR
4203 HDC compat_hdc = CreateCompatibleDC (s->hdc);
4204 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4205 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
4206 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
4207
4208 SetTextColor (s->hdc, s->gc->foreground);
4209 SetBkColor (s->hdc, s->gc->background);
4210#if 0 /* From w32bdf.c (which is from Meadow). */
4211 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4212 compat_hdc, 0, 0, SRCCOPY);
791f420f
JR
4213 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4214 compat_hdc, 0, 0, 0xB8074A);
9436cdf9
JR
4215#else
4216 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4217 compat_hdc, 0, 0, 0xE20746);
791f420f
JR
4218#endif
4219 SelectObject (s->hdc, orig_brush);
4220 DeleteObject (fg_brush);
4221 SelectObject (compat_hdc, orig_obj);
4222 DeleteDC (compat_hdc);
4223 }
4224 DeleteObject (pixmap);
4225 pixmap = 0;
4226 }
4227 else
4228 x_draw_image_foreground (s);
ee78dc32 4229
791f420f
JR
4230 /* If we must draw a relief around the image, do it. */
4231 if (s->img->relief
4232 || s->hl == DRAW_IMAGE_RAISED
4233 || s->hl == DRAW_IMAGE_SUNKEN)
4234 x_draw_image_relief (s);
4235}
ee78dc32 4236
cabb23bc 4237
791f420f 4238/* Draw stretch glyph string S. */
cabb23bc 4239
791f420f
JR
4240static void
4241x_draw_stretch_glyph_string (s)
4242 struct glyph_string *s;
4243{
4244 xassert (s->first_glyph->type == STRETCH_GLYPH);
4245 s->stippled_p = s->face->stipple != 0;
65c4903c 4246
791f420f
JR
4247 if (s->hl == DRAW_CURSOR
4248 && !x_stretch_cursor_p)
4249 {
4250 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4251 as wide as the stretch glyph. */
4252 int width = min (CANON_X_UNIT (s->f), s->background_width);
cabb23bc 4253
791f420f
JR
4254 /* Draw cursor. */
4255 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
cabb23bc 4256
791f420f
JR
4257 /* Clear rest using the GC of the original non-cursor face. */
4258 if (width < s->background_width)
4259 {
4260 XGCValues *gc = s->face->gc;
4261 int x = s->x + width, y = s->y;
4262 int w = s->background_width - width, h = s->height;
4263 RECT r;
4264 HDC hdc = s->hdc;
4265 w32_get_glyph_string_clip_rect (s, &r);
4266 w32_set_clip_rectangle (hdc, &r);
4267
01b220b6 4268#if 0 /* TODO: stipple */
791f420f
JR
4269 if (s->face->stipple)
4270 {
4271 /* Fill background with a stipple pattern. */
4272 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4273 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4274 XSetFillStyle (s->display, gc, FillSolid);
4275 }
4276 else
4277#endif
4278 {
4279 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
4280 }
4281 }
4282 }
4283 else
4284 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4285 s->height);
4286
4287 s->background_filled_p = 1;
4288}
cabb23bc 4289
cabb23bc 4290
791f420f
JR
4291/* Draw glyph string S. */
4292
4293static void
4294x_draw_glyph_string (s)
4295 struct glyph_string *s;
4296{
858a55c1
AI
4297 int relief_drawn_p = 0;
4298
791f420f
JR
4299 /* If S draws into the background of its successor, draw the
4300 background of the successor first so that S can draw into it.
4301 This makes S->next use XDrawString instead of XDrawImageString. */
4302 if (s->next && s->right_overhang && !s->for_overlaps_p)
4303 {
4304 xassert (s->next->img == NULL);
4305 x_set_glyph_string_gc (s->next);
4306 x_set_glyph_string_clipping (s->next);
4307 x_draw_glyph_string_background (s->next, 1);
4308 }
4309
4310 /* Set up S->gc, set clipping and draw S. */
4311 x_set_glyph_string_gc (s);
4312 x_set_glyph_string_clipping (s);
4313
858a55c1
AI
4314 /* Draw relief (if any) in advance for char/composition so that the
4315 glyph string can be drawn over it. */
4316 if (!s->for_overlaps_p
4317 && s->face->box != FACE_NO_BOX
4318 && (s->first_glyph->type == CHAR_GLYPH
4319 || s->first_glyph->type == COMPOSITE_GLYPH))
4320
4321 {
4322 x_draw_glyph_string_background (s, 1);
4323 x_draw_glyph_string_box (s);
4324 relief_drawn_p = 1;
4325 }
4326
791f420f
JR
4327 switch (s->first_glyph->type)
4328 {
4329 case IMAGE_GLYPH:
4330 x_draw_image_glyph_string (s);
4331 break;
4332
4333 case STRETCH_GLYPH:
4334 x_draw_stretch_glyph_string (s);
4335 break;
4336
4337 case CHAR_GLYPH:
4338 if (s->for_overlaps_p)
4339 s->background_filled_p = 1;
4340 else
4341 x_draw_glyph_string_background (s, 0);
4342 x_draw_glyph_string_foreground (s);
4343 break;
4344
4345 case COMPOSITE_GLYPH:
4346 if (s->for_overlaps_p || s->gidx > 0)
4347 s->background_filled_p = 1;
4348 else
4349 x_draw_glyph_string_background (s, 1);
4350 x_draw_composite_glyph_string_foreground (s);
4351 break;
4352
4353 default:
4354 abort ();
4355 }
4356
4357 if (!s->for_overlaps_p)
4358 {
4359 /* Draw underline. */
9ef2e2cf
JR
4360 if (s->face->underline_p
4361 && (s->font->bdf || !s->font->tm.tmUnderlined))
791f420f
JR
4362 {
4363 unsigned long h = 1;
4364 unsigned long dy = s->height - h;
4365
4366 if (s->face->underline_defaulted_p)
4367 {
4368 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4369 s->y + dy, s->width, 1);
4370 }
4371 else
4372 {
4373 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4374 s->y + dy, s->width, 1);
4375 }
4376 }
4377
4378 /* Draw overline. */
4379 if (s->face->overline_p)
4380 {
4381 unsigned long dy = 0, h = 1;
4382
4383 if (s->face->overline_color_defaulted_p)
4384 {
4385 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4386 s->y + dy, s->width, h);
4387 }
4388 else
4389 {
4390 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4391 s->y + dy, s->width, h);
4392 }
4393 }
4394
4395 /* Draw strike-through. */
9ef2e2cf
JR
4396 if (s->face->strike_through_p
4397 && (s->font->bdf || !s->font->tm.tmStruckOut))
791f420f
JR
4398 {
4399 unsigned long h = 1;
4400 unsigned long dy = (s->height - h) / 2;
4401
4402 if (s->face->strike_through_color_defaulted_p)
4403 {
4404 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
4405 s->width, h);
4406 }
4407 else
4408 {
4409 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4410 s->y + dy, s->width, h);
4411 }
4412 }
4413
4414 /* Draw relief. */
858a55c1 4415 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
791f420f
JR
4416 x_draw_glyph_string_box (s);
4417 }
4418
4419 /* Reset clipping. */
4420 w32_set_clip_rectangle (s->hdc, NULL);
4421}
4422
4423
4424static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4425 struct face **, int));
4426
4427
c2cc16fa
JR
4428/* Fill glyph string S with composition components specified by S->cmp.
4429
791f420f
JR
4430 FACES is an array of faces for all components of this composition.
4431 S->gidx is the index of the first component for S.
4432 OVERLAPS_P non-zero means S should draw the foreground only, and
c2cc16fa 4433 use its physical height for clipping.
791f420f
JR
4434
4435 Value is the index of a component not in S. */
4436
4437static int
4438x_fill_composite_glyph_string (s, faces, overlaps_p)
4439 struct glyph_string *s;
4440 struct face **faces;
4441 int overlaps_p;
4442{
4443 int i;
4444
4445 xassert (s);
4446
4447 s->for_overlaps_p = overlaps_p;
4448
4449 s->face = faces[s->gidx];
4450 s->font = s->face->font;
4451 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4452
4453 /* For all glyphs of this composition, starting at the offset
4454 S->gidx, until we reach the end of the definition or encounter a
4455 glyph that requires the different face, add it to S. */
4456 ++s->nchars;
4457 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4458 ++s->nchars;
4459
4460 /* All glyph strings for the same composition has the same width,
4461 i.e. the width set for the first component of the composition. */
4462
4463 s->width = s->first_glyph->pixel_width;
4464
4465 /* If the specified font could not be loaded, use the frame's
4466 default font, but record the fact that we couldn't load it in
4467 the glyph string so that we can draw rectangles for the
4468 characters of the glyph string. */
4469 if (s->font == NULL)
4470 {
4471 s->font_not_found_p = 1;
4472 s->font = FRAME_FONT (s->f);
4473 }
4474
4475 /* Adjust base line for subscript/superscript text. */
4476 s->ybase += s->first_glyph->voffset;
4477
4478 xassert (s->face && s->face->gc);
4479
4480 /* This glyph string must always be drawn with 16-bit functions. */
4481 s->two_byte_p = 1;
4482
4483 return s->gidx + s->nchars;
4484}
4485
4486
c2cc16fa
JR
4487/* Fill glyph string S from a sequence of character glyphs.
4488
791f420f
JR
4489 FACE_ID is the face id of the string. START is the index of the
4490 first glyph to consider, END is the index of the last + 1.
4491 OVERLAPS_P non-zero means S should draw the foreground only, and
c2cc16fa 4492 use its physical height for clipping.
791f420f
JR
4493
4494 Value is the index of the first glyph not in S. */
4495
4496static int
4497x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4498 struct glyph_string *s;
4499 int face_id;
4500 int start, end, overlaps_p;
4501{
4502 struct glyph *glyph, *last;
4503 int voffset;
93ff4395 4504 int glyph_not_available_p;
791f420f
JR
4505
4506 xassert (s->f == XFRAME (s->w->frame));
4507 xassert (s->nchars == 0);
4508 xassert (start >= 0 && end > start);
4509
4510 s->for_overlaps_p = overlaps_p;
791f420f
JR
4511 glyph = s->row->glyphs[s->area] + start;
4512 last = s->row->glyphs[s->area] + end;
4513 voffset = glyph->voffset;
93ff4395
JR
4514
4515 glyph_not_available_p = glyph->glyph_not_available_p;
4516
791f420f
JR
4517 while (glyph < last
4518 && glyph->type == CHAR_GLYPH
4519 && glyph->voffset == voffset
93ff4395
JR
4520 /* Same face id implies same font, nowadays. */
4521 && glyph->face_id == face_id
4522 && glyph->glyph_not_available_p == glyph_not_available_p)
791f420f 4523 {
93ff4395
JR
4524 int two_byte_p;
4525
791f420f 4526 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
93ff4395
JR
4527 s->char2b + s->nchars,
4528 &two_byte_p);
4529 s->two_byte_p = two_byte_p;
791f420f
JR
4530 ++s->nchars;
4531 xassert (s->nchars <= end - start);
4532 s->width += glyph->pixel_width;
4533 ++glyph;
4534 }
4535
4536 s->font = s->face->font;
4537 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4538
4539 /* If the specified font could not be loaded, use the frame's font,
4540 but record the fact that we couldn't load it in
4541 S->font_not_found_p so that we can draw rectangles for the
4542 characters of the glyph string. */
93ff4395 4543 if (s->font == NULL || glyph_not_available_p)
791f420f
JR
4544 {
4545 s->font_not_found_p = 1;
4546 s->font = FRAME_FONT (s->f);
4547 }
4548
4549 /* Adjust base line for subscript/superscript text. */
4550 s->ybase += voffset;
4551
4552 xassert (s->face && s->face->gc);
4553 return glyph - s->row->glyphs[s->area];
4554}
4555
4556
4557/* Fill glyph string S from image glyph S->first_glyph. */
4558
4559static void
4560x_fill_image_glyph_string (s)
4561 struct glyph_string *s;
4562{
4563 xassert (s->first_glyph->type == IMAGE_GLYPH);
4564 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4565 xassert (s->img);
4566 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4567 s->font = s->face->font;
4568 s->width = s->first_glyph->pixel_width;
4569
4570 /* Adjust base line for subscript/superscript text. */
4571 s->ybase += s->first_glyph->voffset;
4572}
4573
4574
ec48c3a7 4575/* Fill glyph string S from a sequence of stretch glyphs.
791f420f 4576
ec48c3a7
JR
4577 ROW is the glyph row in which the glyphs are found, AREA is the
4578 area within the row. START is the index of the first glyph to
4579 consider, END is the index of the last + 1.
4580
4581 Value is the index of the first glyph not in S. */
4582
4583static int
4584x_fill_stretch_glyph_string (s, row, area, start, end)
791f420f 4585 struct glyph_string *s;
ec48c3a7
JR
4586 struct glyph_row *row;
4587 enum glyph_row_area area;
4588 int start, end;
791f420f 4589{
ec48c3a7
JR
4590 struct glyph *glyph, *last;
4591 int voffset, face_id;
4592
791f420f 4593 xassert (s->first_glyph->type == STRETCH_GLYPH);
ec48c3a7
JR
4594
4595 glyph = s->row->glyphs[s->area] + start;
4596 last = s->row->glyphs[s->area] + end;
4597 face_id = glyph->face_id;
4598 s->face = FACE_FROM_ID (s->f, face_id);
791f420f 4599 s->font = s->face->font;
ec48c3a7
JR
4600 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4601 s->width = glyph->pixel_width;
4602 voffset = glyph->voffset;
4603
4604 for (++glyph;
4605 (glyph < last
4606 && glyph->type == STRETCH_GLYPH
4607 && glyph->voffset == voffset
4608 && glyph->face_id == face_id);
4609 ++glyph)
4610 s->width += glyph->pixel_width;
791f420f
JR
4611
4612 /* Adjust base line for subscript/superscript text. */
ec48c3a7
JR
4613 s->ybase += voffset;
4614
4615 xassert (s->face && s->face->gc);
4616 return glyph - s->row->glyphs[s->area];
791f420f
JR
4617}
4618
4619
4620/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4621 of XChar2b structures for S; it can't be allocated in
4622 x_init_glyph_string because it must be allocated via `alloca'. W
4623 is the window on which S is drawn. ROW and AREA are the glyph row
4624 and area within the row from which S is constructed. START is the
4625 index of the first glyph structure covered by S. HL is a
4626 face-override for drawing S. */
4627
4628static void
4629w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4630 struct glyph_string *s;
4631 HDC hdc;
4632 wchar_t *char2b;
4633 struct window *w;
4634 struct glyph_row *row;
4635 enum glyph_row_area area;
4636 int start;
4637 enum draw_glyphs_face hl;
4638{
4639 bzero (s, sizeof *s);
4640 s->w = w;
4641 s->f = XFRAME (w->frame);
4642 s->hdc = hdc;
4643 s->window = FRAME_W32_WINDOW (s->f);
4644 s->char2b = char2b;
4645 s->hl = hl;
4646 s->row = row;
4647 s->area = area;
4648 s->first_glyph = row->glyphs[area] + start;
4649 s->height = row->height;
4650 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4651
4652 /* Display the internal border below the tool-bar window. */
4653 if (s->w == XWINDOW (s->f->tool_bar_window))
4654 s->y -= s->f->output_data.w32->internal_border_width;
4655
4656 s->ybase = s->y + row->ascent;
4657}
4658
4659
4660/* Set background width of glyph string S. START is the index of the
4661 first glyph following S. LAST_X is the right-most x-position + 1
4662 in the drawing area. */
4663
4664static INLINE void
4665x_set_glyph_string_background_width (s, start, last_x)
4666 struct glyph_string *s;
4667 int start;
4668 int last_x;
4669{
4670 /* If the face of this glyph string has to be drawn to the end of
4671 the drawing area, set S->extends_to_end_of_line_p. */
4672 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4673
4674 if (start == s->row->used[s->area]
5b844253 4675 && s->area == TEXT_AREA
f4fccc1e
EZ
4676 && ((s->hl == DRAW_NORMAL_TEXT
4677 && (s->row->fill_line_p
4678 || s->face->background != default_face->background
4679 || s->face->stipple != default_face->stipple
4680 || s->row->mouse_face_p))
4681 || s->hl == DRAW_MOUSE_FACE))
791f420f
JR
4682 s->extends_to_end_of_line_p = 1;
4683
4684 /* If S extends its face to the end of the line, set its
4685 background_width to the distance to the right edge of the drawing
4686 area. */
4687 if (s->extends_to_end_of_line_p)
4688 s->background_width = last_x - s->x + 1;
4689 else
4690 s->background_width = s->width;
4691}
4692
4693
4694/* Add a glyph string for a stretch glyph to the list of strings
4695 between HEAD and TAIL. START is the index of the stretch glyph in
4696 row area AREA of glyph row ROW. END is the index of the last glyph
4697 in that glyph row area. X is the current output position assigned
4698 to the new glyph string constructed. HL overrides that face of the
4699 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4700 is the right-most x-position of the drawing area. */
4701
4702#define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4703 do \
4704 { \
4705 s = (struct glyph_string *) alloca (sizeof *s); \
4706 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
ec48c3a7 4707 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
791f420f 4708 x_append_glyph_string (&HEAD, &TAIL, s); \
791f420f
JR
4709 s->x = (X); \
4710 } \
4711 while (0)
4712
4713
4714/* Add a glyph string for an image glyph to the list of strings
4715 between HEAD and TAIL. START is the index of the image glyph in
4716 row area AREA of glyph row ROW. END is the index of the last glyph
4717 in that glyph row area. X is the current output position assigned
4718 to the new glyph string constructed. HL overrides that face of the
4719 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4720 is the right-most x-position of the drawing area. */
4721
4722#define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4723 do \
4724 { \
4725 s = (struct glyph_string *) alloca (sizeof *s); \
4726 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4727 x_fill_image_glyph_string (s); \
4728 x_append_glyph_string (&HEAD, &TAIL, s); \
4729 ++START; \
4730 s->x = (X); \
4731 } \
4732 while (0)
4733
4734
4735/* Add a glyph string for a sequence of character glyphs to the list
4736 of strings between HEAD and TAIL. START is the index of the first
4737 glyph in row area AREA of glyph row ROW that is part of the new
4738 glyph string. END is the index of the last glyph in that glyph row
4739 area. X is the current output position assigned to the new glyph
4740 string constructed. HL overrides that face of the glyph; e.g. it
4741 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4742 right-most x-position of the drawing area. */
4743
4744#define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4745 do \
4746 { \
ec48c3a7 4747 int c, face_id; \
791f420f
JR
4748 wchar_t *char2b; \
4749 \
4750 c = (ROW)->glyphs[AREA][START].u.ch; \
791f420f 4751 face_id = (ROW)->glyphs[AREA][START].face_id; \
9ef2e2cf 4752 \
791f420f
JR
4753 s = (struct glyph_string *) alloca (sizeof *s); \
4754 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4755 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4756 x_append_glyph_string (&HEAD, &TAIL, s); \
791f420f
JR
4757 s->x = (X); \
4758 START = x_fill_glyph_string (s, face_id, START, END, \
4759 OVERLAPS_P); \
4760 } \
4761 while (0)
4762
4763
4764/* Add a glyph string for a composite sequence to the list of strings
4765 between HEAD and TAIL. START is the index of the first glyph in
4766 row area AREA of glyph row ROW that is part of the new glyph
4767 string. END is the index of the last glyph in that glyph row area.
4768 X is the current output position assigned to the new glyph string
4769 constructed. HL overrides that face of the glyph; e.g. it is
4770 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4771 x-position of the drawing area. */
4772
4773#define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4774 do { \
4775 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4776 int face_id = (ROW)->glyphs[AREA][START].face_id; \
93ff4395 4777 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
791f420f
JR
4778 struct composition *cmp = composition_table[cmp_id]; \
4779 int glyph_len = cmp->glyph_len; \
4780 wchar_t *char2b; \
4781 struct face **faces; \
4782 struct glyph_string *first_s = NULL; \
4783 int n; \
4784 \
93ff4395 4785 base_face = base_face->ascii_face; \
791f420f
JR
4786 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4787 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4788 /* At first, fill in `char2b' and `faces'. */ \
4789 for (n = 0; n < glyph_len; n++) \
4790 { \
4791 int c = COMPOSITION_GLYPH (cmp, n); \
93ff4395
JR
4792 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4793 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4794 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4795 this_face_id, char2b + n, 1); \
791f420f
JR
4796 } \
4797 \
4798 /* Make glyph_strings for each glyph sequence that is drawable by \
4799 the same face, and append them to HEAD/TAIL. */ \
4800 for (n = 0; n < cmp->glyph_len;) \
4801 { \
4802 s = (struct glyph_string *) alloca (sizeof *s); \
4803 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4804 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4805 s->cmp = cmp; \
4806 s->gidx = n; \
791f420f
JR
4807 s->x = (X); \
4808 \
4809 if (n == 0) \
4810 first_s = s; \
4811 \
4812 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4813 } \
4814 \
4815 ++START; \
4816 s = first_s; \
4817 } while (0)
4818
9ef2e2cf 4819
791f420f
JR
4820/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4821 of AREA of glyph row ROW on window W between indices START and END.
4822 HL overrides the face for drawing glyph strings, e.g. it is
4823 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4824 x-positions of the drawing area.
4825
4826 This is an ugly monster macro construct because we must use alloca
4827 to allocate glyph strings (because x_draw_glyphs can be called
4828 asynchronously). */
4829
4830#define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4831 do \
4832 { \
4833 HEAD = TAIL = NULL; \
4834 while (START < END) \
4835 { \
4836 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4837 switch (first_glyph->type) \
4838 { \
4839 case CHAR_GLYPH: \
4840 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4841 HEAD, TAIL, HL, X, LAST_X, \
4842 OVERLAPS_P); \
4843 break; \
4844 \
4845 case COMPOSITE_GLYPH: \
4846 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4847 END, HEAD, TAIL, HL, X, \
4848 LAST_X, OVERLAPS_P); \
4849 break; \
4850 \
4851 case STRETCH_GLYPH: \
4852 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4853 HEAD, TAIL, HL, X, LAST_X); \
4854 break; \
4855 \
4856 case IMAGE_GLYPH: \
4857 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4858 HEAD, TAIL, HL, X, LAST_X); \
4859 break; \
4860 \
4861 default: \
4862 abort (); \
4863 } \
4864 \
4865 x_set_glyph_string_background_width (s, START, LAST_X); \
4866 (X) += s->width; \
4867 } \
4868 } \
4869 while (0)
4870
4871
4872/* Draw glyphs between START and END in AREA of ROW on window W,
4873 starting at x-position X. X is relative to AREA in W. HL is a
4874 face-override with the following meaning:
4875
4876 DRAW_NORMAL_TEXT draw normally
4877 DRAW_CURSOR draw in cursor face
4878 DRAW_MOUSE_FACE draw in mouse face.
4879 DRAW_INVERSE_VIDEO draw in mode line face
4880 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4881 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4882
4883 If REAL_START is non-null, return in *REAL_START the real starting
4884 position for display. This can be different from START in case
4885 overlapping glyphs must be displayed. If REAL_END is non-null,
4886 return in *REAL_END the real end position for display. This can be
4887 different from END in case overlapping glyphs must be displayed.
4888
4889 If OVERLAPS_P is non-zero, draw only the foreground of characters
4890 and clip to the physical height of ROW.
4891
4892 Value is the x-position reached, relative to AREA of W. */
4893
4894static int
4895x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4896 overlaps_p)
4897 struct window *w;
4898 int x;
4899 struct glyph_row *row;
4900 enum glyph_row_area area;
4901 int start, end;
4902 enum draw_glyphs_face hl;
4903 int *real_start, *real_end;
4904 int overlaps_p;
4905{
4906 struct glyph_string *head, *tail;
4907 struct glyph_string *s;
4908 int last_x, area_width;
4909 int x_reached;
4910 int i, j;
4911 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4912
4913 /* Let's rather be paranoid than getting a SEGV. */
791f420f 4914 end = min (end, row->used[area]);
9127e20e
JR
4915 start = max (0, start);
4916 start = min (end, start);
791f420f
JR
4917 if (real_start)
4918 *real_start = start;
4919 if (real_end)
4920 *real_end = end;
4921
4922 /* Translate X to frame coordinates. Set last_x to the right
4923 end of the drawing area. */
4924 if (row->full_width_p)
4925 {
791f420f
JR
4926 /* X is relative to the left edge of W, without scroll bars
4927 or flag areas. */
ec48c3a7 4928 struct frame *f = XFRAME (WINDOW_FRAME (w));
791f420f
JR
4929 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4930 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
ee78dc32 4931
791f420f
JR
4932 x += window_left_x;
4933 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4934 last_x = window_left_x + area_width;
ee78dc32 4935
791f420f
JR
4936 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4937 {
4938 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4939 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4940 last_x += width;
4941 else
4942 x -= width;
4943 }
ee78dc32 4944
791f420f
JR
4945 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4946 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4947 }
4948 else
4949 {
4950 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4951 area_width = window_box_width (w, area);
4952 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4953 }
e12ca9c2 4954
791f420f
JR
4955 /* Build a doubly-linked list of glyph_string structures between
4956 head and tail from what we have to draw. Note that the macro
4957 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4958 the reason we use a separate variable `i'. */
4959 i = start;
4960 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4961 overlaps_p);
791f420f
JR
4962 if (tail)
4963 x_reached = tail->x + tail->background_width;
4964 else
4965 x_reached = x;
e12ca9c2 4966
791f420f
JR
4967 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4968 the row, redraw some glyphs in front or following the glyph
4969 strings built above. */
9127e20e 4970 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
791f420f
JR
4971 {
4972 int dummy_x = 0;
4973 struct glyph_string *h, *t;
4974
4975 /* Compute overhangs for all glyph strings. */
4976 for (s = head; s; s = s->next)
4977 x_compute_glyph_string_overhangs (s);
4978
4979 /* Prepend glyph strings for glyphs in front of the first glyph
4980 string that are overwritten because of the first glyph
4981 string's left overhang. The background of all strings
4982 prepended must be drawn because the first glyph string
4983 draws over it. */
4984 i = x_left_overwritten (head);
4985 if (i >= 0)
4986 {
4987 j = i;
4988 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
4989 DRAW_NORMAL_TEXT, dummy_x, last_x,
4990 overlaps_p);
4991 start = i;
4992 if (real_start)
4993 *real_start = start;
4994 x_compute_overhangs_and_x (t, head->x, 1);
4995 x_prepend_glyph_string_lists (&head, &tail, h, t);
4996 }
ee78dc32 4997
791f420f
JR
4998 /* Prepend glyph strings for glyphs in front of the first glyph
4999 string that overwrite that glyph string because of their
5000 right overhang. For these strings, only the foreground must
5001 be drawn, because it draws over the glyph string at `head'.
5002 The background must not be drawn because this would overwrite
5003 right overhangs of preceding glyphs for which no glyph
5004 strings exist. */
5005 i = x_left_overwriting (head);
5006 if (i >= 0)
5007 {
5008 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
5009 DRAW_NORMAL_TEXT, dummy_x, last_x,
5010 overlaps_p);
791f420f
JR
5011 for (s = h; s; s = s->next)
5012 s->background_filled_p = 1;
5013 if (real_start)
5014 *real_start = i;
5015 x_compute_overhangs_and_x (t, head->x, 1);
5016 x_prepend_glyph_string_lists (&head, &tail, h, t);
5017 }
cabb23bc 5018
791f420f
JR
5019 /* Append glyphs strings for glyphs following the last glyph
5020 string tail that are overwritten by tail. The background of
5021 these strings has to be drawn because tail's foreground draws
5022 over it. */
5023 i = x_right_overwritten (tail);
5024 if (i >= 0)
5025 {
5026 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5027 DRAW_NORMAL_TEXT, x, last_x,
5028 overlaps_p);
791f420f
JR
5029 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5030 x_append_glyph_string_lists (&head, &tail, h, t);
5031 if (real_end)
5032 *real_end = i;
5033 }
cabb23bc 5034
791f420f
JR
5035 /* Append glyph strings for glyphs following the last glyph
5036 string tail that overwrite tail. The foreground of such
5037 glyphs has to be drawn because it writes into the background
5038 of tail. The background must not be drawn because it could
5039 paint over the foreground of following glyphs. */
5040 i = x_right_overwriting (tail);
5041 if (i >= 0)
5042 {
5043 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5044 DRAW_NORMAL_TEXT, x, last_x,
5045 overlaps_p);
791f420f
JR
5046 for (s = h; s; s = s->next)
5047 s->background_filled_p = 1;
5048 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5049 x_append_glyph_string_lists (&head, &tail, h, t);
5050 if (real_end)
5051 *real_end = i;
5052 }
5053 }
cabb23bc 5054
791f420f
JR
5055 /* Draw all strings. */
5056 for (s = head; s; s = s->next)
5057 x_draw_glyph_string (s);
cabb23bc 5058
791f420f
JR
5059 /* Value is the x-position up to which drawn, relative to AREA of W.
5060 This doesn't include parts drawn because of overhangs. */
5061 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5062 if (!row->full_width_p)
5063 {
5064 if (area > LEFT_MARGIN_AREA)
5065 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5066 if (area > TEXT_AREA)
5067 x_reached -= window_box_width (w, TEXT_AREA);
5068 }
cabb23bc 5069
791f420f 5070 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
65c4903c 5071
791f420f
JR
5072 return x_reached;
5073}
65c4903c 5074
cabb23bc 5075
791f420f
JR
5076/* Fix the display of area AREA of overlapping row ROW in window W. */
5077
5078static void
5079x_fix_overlapping_area (w, row, area)
5080 struct window *w;
5081 struct glyph_row *row;
5082 enum glyph_row_area area;
5083{
5084 int i, x;
5085
5086 BLOCK_INPUT;
5087
5088 if (area == LEFT_MARGIN_AREA)
5089 x = 0;
5090 else if (area == TEXT_AREA)
5091 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5092 else
5093 x = (window_box_width (w, LEFT_MARGIN_AREA)
5094 + window_box_width (w, TEXT_AREA));
b1ae662f 5095
791f420f
JR
5096 for (i = 0; i < row->used[area];)
5097 {
5098 if (row->glyphs[area][i].overlaps_vertically_p)
ee78dc32 5099 {
791f420f 5100 int start = i, start_x = x;
ee78dc32 5101
791f420f
JR
5102 do
5103 {
5104 x += row->glyphs[area][i].pixel_width;
5105 ++i;
5106 }
5107 while (i < row->used[area]
5108 && row->glyphs[area][i].overlaps_vertically_p);
cabb23bc 5109
791f420f
JR
5110 x_draw_glyphs (w, start_x, row, area, start, i,
5111 (row->inverse_p
5112 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5113 NULL, NULL, 1);
5114 }
5115 else
5116 {
5117 x += row->glyphs[area][i].pixel_width;
5118 ++i;
5119 }
5120 }
5121
5122 UNBLOCK_INPUT;
ee78dc32
GV
5123}
5124
ee78dc32 5125
791f420f
JR
5126/* Output LEN glyphs starting at START at the nominal cursor position.
5127 Advance the nominal cursor over the text. The global variable
5128 updated_window contains the window being updated, updated_row is
5129 the glyph row being updated, and updated_area is the area of that
5130 row being updated. */
ee78dc32 5131
96214669 5132static void
791f420f
JR
5133x_write_glyphs (start, len)
5134 struct glyph *start;
ee78dc32
GV
5135 int len;
5136{
791f420f 5137 int x, hpos, real_start, real_end;
ee78dc32 5138
791f420f 5139 xassert (updated_window && updated_row);
ee78dc32 5140 BLOCK_INPUT;
791f420f
JR
5141
5142 /* Write glyphs. */
ee78dc32 5143
791f420f
JR
5144 hpos = start - updated_row->glyphs[updated_area];
5145 x = x_draw_glyphs (updated_window, output_cursor.x,
5146 updated_row, updated_area,
5147 hpos, hpos + len,
5148 (updated_row->inverse_p
5149 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5150 &real_start, &real_end, 0);
5151
5152 /* If we drew over the cursor, note that it is not visible any more. */
5153 note_overwritten_text_cursor (updated_window, real_start,
5154 real_end - real_start);
5155
5156 UNBLOCK_INPUT;
5157
5158 /* Advance the output cursor. */
5159 output_cursor.hpos += len;
5160 output_cursor.x = x;
5161}
ee78dc32 5162
ee78dc32 5163
791f420f 5164/* Insert LEN glyphs from START at the nominal cursor position. */
ee78dc32 5165
791f420f
JR
5166static void
5167x_insert_glyphs (start, len)
5168 struct glyph *start;
5169 register int len;
5170{
5171 struct frame *f;
5172 struct window *w;
5173 int line_height, shift_by_width, shifted_region_width;
5174 struct glyph_row *row;
5175 struct glyph *glyph;
5176 int frame_x, frame_y, hpos, real_start, real_end;
5177 HDC hdc;
5178
5179 xassert (updated_window && updated_row);
5180 BLOCK_INPUT;
5181 w = updated_window;
5182 f = XFRAME (WINDOW_FRAME (w));
5183 hdc = get_frame_dc (f);
5184
5185 /* Get the height of the line we are in. */
5186 row = updated_row;
5187 line_height = row->height;
5188
5189 /* Get the width of the glyphs to insert. */
5190 shift_by_width = 0;
5191 for (glyph = start; glyph < start + len; ++glyph)
5192 shift_by_width += glyph->pixel_width;
5193
5194 /* Get the width of the region to shift right. */
5195 shifted_region_width = (window_box_width (w, updated_area)
5196 - output_cursor.x
5197 - shift_by_width);
5198
5199 /* Shift right. */
5b844253 5200 frame_x = window_box_left (w, updated_area) + output_cursor.x;
791f420f
JR
5201 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5202 BitBlt (hdc, frame_x + shift_by_width, frame_y,
5203 shifted_region_width, line_height,
5204 hdc, frame_x, frame_y, SRCCOPY);
5205
5206 /* Write the glyphs. */
5207 hpos = start - row->glyphs[updated_area];
5208 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5209 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5210 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5211
5212 /* Advance the output cursor. */
5213 output_cursor.hpos += len;
5214 output_cursor.x += shift_by_width;
5215 release_frame_dc (f, hdc);
ee78dc32
GV
5216
5217 UNBLOCK_INPUT;
5218}
791f420f
JR
5219
5220
5221/* Delete N glyphs at the nominal cursor position. Not implemented
5222 for X frames. */
ee78dc32 5223
96214669 5224static void
791f420f
JR
5225x_delete_glyphs (n)
5226 register int n;
ee78dc32 5227{
7e6ac5b9
AI
5228 struct frame *f;
5229
5230 if (updating_frame)
5231 f = updating_frame;
5232 else
5233 f = SELECTED_FRAME ();
5234
5235 if (! FRAME_W32_P (f))
5236 return;
5237
791f420f
JR
5238 abort ();
5239}
ee78dc32 5240
ee78dc32 5241
791f420f
JR
5242/* Erase the current text line from the nominal cursor position
5243 (inclusive) to pixel column TO_X (exclusive). The idea is that
5244 everything from TO_X onward is already erased.
ee78dc32 5245
791f420f
JR
5246 TO_X is a pixel position relative to updated_area of
5247 updated_window. TO_X == -1 means clear to the end of this area. */
11606873 5248
791f420f
JR
5249static void
5250x_clear_end_of_line (to_x)
5251 int to_x;
5252{
5253 struct frame *f;
5254 struct window *w = updated_window;
5255 int max_x, min_y, max_y;
5256 int from_x, from_y, to_y;
5257
5258 xassert (updated_window && updated_row);
5259 f = XFRAME (w->frame);
5260
5261 if (updated_row->full_width_p)
5262 {
5263 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5264 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5265 && !w->pseudo_window_p)
5266 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5267 }
5268 else
5269 max_x = window_box_width (w, updated_area);
5270 max_y = window_text_bottom_y (w);
ee78dc32 5271
791f420f
JR
5272 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5273 of window. For TO_X > 0, truncate to end of drawing area. */
5274 if (to_x == 0)
5275 return;
5276 else if (to_x < 0)
5277 to_x = max_x;
5278 else
5279 to_x = min (to_x, max_x);
ee78dc32 5280
791f420f
JR
5281 to_y = min (max_y, output_cursor.y + updated_row->height);
5282
ee78dc32 5283 /* Notice if the cursor will be cleared by this operation. */
791f420f
JR
5284 if (!updated_row->full_width_p)
5285 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
ee78dc32 5286
791f420f
JR
5287 from_x = output_cursor.x;
5288
5289 /* Translate to frame coordinates. */
5290 if (updated_row->full_width_p)
5291 {
5292 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5293 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5294 }
5295 else
5296 {
5297 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5298 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5299 }
5300
5301 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5302 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5303 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5304
5305 /* Prevent inadvertently clearing to end of the X window. */
5306 if (to_x > from_x && to_y > from_y)
5307 {
00fe468b 5308 HDC hdc;
791f420f 5309 BLOCK_INPUT;
00fe468b
JR
5310 hdc = get_frame_dc (f);
5311
5312 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
5313 release_frame_dc (f, hdc);
791f420f
JR
5314 UNBLOCK_INPUT;
5315 }
ee78dc32
GV
5316}
5317
791f420f
JR
5318
5319/* Clear entire frame. If updating_frame is non-null, clear that
5320 frame. Otherwise clear the selected frame. */
5321
96214669 5322static void
791f420f 5323x_clear_frame ()
ee78dc32 5324{
791f420f 5325 struct frame *f;
ee78dc32 5326
791f420f
JR
5327 if (updating_frame)
5328 f = updating_frame;
5329 else
5330 f = SELECTED_FRAME ();
ee78dc32 5331
7e6ac5b9
AI
5332 if (! FRAME_W32_P (f))
5333 return;
5334
791f420f
JR
5335 /* Clearing the frame will erase any cursor, so mark them all as no
5336 longer visible. */
5337 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5338 output_cursor.hpos = output_cursor.vpos = 0;
5339 output_cursor.x = -1;
ee78dc32 5340
791f420f
JR
5341 /* We don't set the output cursor here because there will always
5342 follow an explicit cursor_to. */
ee78dc32
GV
5343 BLOCK_INPUT;
5344
fbd6baed 5345 w32_clear_window (f);
ee78dc32
GV
5346
5347 /* We have to clear the scroll bars, too. If we have changed
5348 colors or something like that, then they should be notified. */
5349 x_scroll_bar_clear (f);
5350
5351 UNBLOCK_INPUT;
5352}
791f420f 5353
ee78dc32
GV
5354\f
5355/* Make audible bell. */
5356
96214669
GV
5357static void
5358w32_ring_bell (void)
ee78dc32 5359{
7e6ac5b9
AI
5360 struct frame *f;
5361
5362 f = SELECTED_FRAME ();
5363
ee78dc32
GV
5364 BLOCK_INPUT;
5365
0f32f023 5366 if (FRAME_W32_P (f) && visible_bell)
8331e676
GV
5367 {
5368 int i;
791f420f 5369 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
8331e676
GV
5370
5371 for (i = 0; i < 5; i++)
5372 {
5373 FlashWindow (hwnd, TRUE);
5374 Sleep (10);
5375 }
5376 FlashWindow (hwnd, FALSE);
5377 }
ee78dc32 5378 else
fbd6baed 5379 w32_sys_ring_bell ();
ee78dc32
GV
5380
5381 UNBLOCK_INPUT;
ee78dc32 5382}
791f420f 5383
ee78dc32 5384\f
791f420f
JR
5385/* Specify how many text lines, from the top of the window,
5386 should be affected by insert-lines and delete-lines operations.
5387 This, and those operations, are used only within an update
5388 that is bounded by calls to x_update_begin and x_update_end. */
ee78dc32 5389
96214669 5390static void
791f420f
JR
5391w32_set_terminal_window (n)
5392 register int n;
ee78dc32 5393{
791f420f 5394 /* This function intentionally left blank. */
ee78dc32 5395}
791f420f
JR
5396\f
5397
5398\f
5399/***********************************************************************
5400 Line Dance
5401 ***********************************************************************/
5402
5403/* Perform an insert-lines or delete-lines operation, inserting N
5404 lines or deleting -N lines at vertical position VPOS. */
ee78dc32 5405
96214669 5406static void
791f420f
JR
5407x_ins_del_lines (vpos, n)
5408 int vpos, n;
ee78dc32 5409{
7e6ac5b9
AI
5410 struct frame *f;
5411
5412 if (updating_frame)
5413 f = updating_frame;
5414 else
5415 f = SELECTED_FRAME ();
5416
5417 if (! FRAME_W32_P (f))
5418 return;
5419
ee78dc32
GV
5420 abort ();
5421}
791f420f
JR
5422
5423
5424/* Scroll part of the display as described by RUN. */
5425
5426static void
5427x_scroll_run (w, run)
5428 struct window *w;
5429 struct run *run;
5430{
5431 struct frame *f = XFRAME (w->frame);
5432 int x, y, width, height, from_y, to_y, bottom_y;
5433 HDC hdc = get_frame_dc (f);
5434
5435 /* Get frame-relative bounding box of the text display area of W,
5436 without mode lines. Include in this box the flags areas to the
5437 left and right of W. */
5438 window_box (w, -1, &x, &y, &width, &height);
5439 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5440 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5441
5442 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5443 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5444 bottom_y = y + height;
5445
5446 if (to_y < from_y)
5447 {
5448 /* Scrolling up. Make sure we don't copy part of the mode
5449 line at the bottom. */
5450 if (from_y + run->height > bottom_y)
5451 height = bottom_y - from_y;
5452 else
5453 height = run->height;
5454 }
5455 else
5456 {
5457 /* Scolling down. Make sure we don't copy over the mode line.
5458 at the bottom. */
5459 if (to_y + run->height > bottom_y)
5460 height = bottom_y - to_y;
5461 else
5462 height = run->height;
5463 }
5464
5465 BLOCK_INPUT;
5466
5467 /* Cursor off. Will be switched on again in x_update_window_end. */
5468 updated_window = w;
5469 x_clear_cursor (w);
5470
5471 BitBlt (hdc, x, to_y, width, height, hdc, x, from_y, SRCCOPY);
5472
5473 UNBLOCK_INPUT;
5474 release_frame_dc (f, hdc);
5475}
5476
9ef2e2cf 5477
ee78dc32 5478\f
791f420f
JR
5479/***********************************************************************
5480 Exposure Events
5481 ***********************************************************************/
5482
5483/* Redisplay an exposed area of frame F. X and Y are the upper-left
5484 corner of the exposed rectangle. W and H are width and height of
5485 the exposed area. All are pixel values. W or H zero means redraw
5486 the entire frame. */
ee78dc32 5487
96214669 5488static void
791f420f
JR
5489expose_frame (f, x, y, w, h)
5490 struct frame *f;
5491 int x, y, w, h;
ee78dc32 5492{
791f420f
JR
5493 RECT r;
5494
5495 TRACE ((stderr, "expose_frame "));
5496
5497 /* No need to redraw if frame will be redrawn soon. */
5498 if (FRAME_GARBAGED_P (f))
5499 {
5500 TRACE ((stderr, " garbaged\n"));
5501 return;
5502 }
5503
5504 /* If basic faces haven't been realized yet, there is no point in
5505 trying to redraw anything. This can happen when we get an expose
5506 event while Emacs is starting, e.g. by moving another window. */
5507 if (FRAME_FACE_CACHE (f) == NULL
5508 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5509 {
5510 TRACE ((stderr, " no faces\n"));
5511 return;
5512 }
5513
5514 if (w == 0 || h == 0)
5515 {
5516 r.left = r.top = 0;
5517 r.right = CANON_X_UNIT (f) * f->width;
5518 r.bottom = CANON_Y_UNIT (f) * f->height;
5519 }
5520 else
5521 {
5522 r.left = x;
5523 r.top = y;
5524 r.right = x + w;
5525 r.bottom = y + h;
5526 }
5527
5528 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
5529 expose_window_tree (XWINDOW (f->root_window), &r);
5530
5531 if (WINDOWP (f->tool_bar_window))
5532 {
5533 struct window *w = XWINDOW (f->tool_bar_window);
5534 RECT window_rect;
5535 RECT intersection_rect;
5536 int window_x, window_y, window_width, window_height;
5537
5538 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5539 window_rect.left = window_x;
5540 window_rect.top = window_y;
5541 window_rect.right = window_x + window_width;
5542 window_rect.bottom = window_y + window_height;
5543
158cba56 5544 if (IntersectRect (&intersection_rect, &r, &window_rect))
791f420f
JR
5545 expose_window (w, &intersection_rect);
5546 }
5547}
5548
5549
5550/* Redraw (parts) of all windows in the window tree rooted at W that
5551 intersect R. R contains frame pixel coordinates. */
5552
5553static void
5554expose_window_tree (w, r)
5555 struct window *w;
5556 RECT *r;
5557{
5558 while (w)
5559 {
5560 if (!NILP (w->hchild))
5561 expose_window_tree (XWINDOW (w->hchild), r);
5562 else if (!NILP (w->vchild))
5563 expose_window_tree (XWINDOW (w->vchild), r);
5564 else
5565 {
5566 RECT window_rect;
5567 RECT intersection_rect;
5568 struct frame *f = XFRAME (w->frame);
5569 int window_x, window_y, window_width, window_height;
5570
5571 /* Frame-relative pixel rectangle of W. */
5572 window_box (w, -1, &window_x, &window_y, &window_width,
5573 &window_height);
5574 window_rect.left
5575 = (window_x
5576 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5577 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5578 window_rect.top = window_y;
5579 window_rect.right = window_rect.left
5580 + (window_width
5581 + FRAME_X_FLAGS_AREA_WIDTH (f)
5582 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5583 window_rect.bottom = window_rect.top
5584 + window_height + CURRENT_MODE_LINE_HEIGHT (w);
5585
158cba56 5586 if (IntersectRect (&intersection_rect, r, &window_rect))
791f420f
JR
5587 expose_window (w, &intersection_rect);
5588 }
5589
5590 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5591 }
5592}
5593
5594
5595/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5596 which intersects rectangle R. R is in window-relative coordinates. */
5597
5598static void
5599expose_area (w, row, r, area)
5600 struct window *w;
5601 struct glyph_row *row;
5602 RECT *r;
5603 enum glyph_row_area area;
5604{
791f420f
JR
5605 struct glyph *first = row->glyphs[area];
5606 struct glyph *end = row->glyphs[area] + row->used[area];
5607 struct glyph *last;
2df85294 5608 int first_x, start_x, x;
791f420f 5609
791f420f
JR
5610 if (area == TEXT_AREA && row->fill_line_p)
5611 /* If row extends face to end of line write the whole line. */
c2cc16fa 5612 x_draw_glyphs (w, 0, row, area,
791f420f
JR
5613 0, row->used[area],
5614 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5615 NULL, NULL, 0);
5616 else
5617 {
2df85294
JR
5618 /* Set START_X to the window-relative start position for drawing glyphs of
5619 AREA. The first glyph of the text area can be partially visible.
5620 The first glyphs of other areas cannot. */
5621 if (area == LEFT_MARGIN_AREA)
5622 start_x = 0;
5623 else if (area == TEXT_AREA)
5624 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5625 else
5626 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5627 + window_box_width (w, TEXT_AREA));
5628 x = start_x;
5629
791f420f
JR
5630 /* Find the first glyph that must be redrawn. */
5631 while (first < end
5632 && x + first->pixel_width < r->left)
5633 {
5634 x += first->pixel_width;
5635 ++first;
5636 }
5637
5638 /* Find the last one. */
5639 last = first;
5640 first_x = x;
5641 while (last < end
5642 && x < r->right)
5643 {
5644 x += last->pixel_width;
5645 ++last;
5646 }
2df85294 5647
791f420f
JR
5648 /* Repaint. */
5649 if (last > first)
f201d732 5650 x_draw_glyphs (w, first_x - start_x, row, area,
791f420f
JR
5651 first - row->glyphs[area],
5652 last - row->glyphs[area],
5653 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5654 NULL, NULL, 0);
5655 }
5656}
5657
5658
5659/* Redraw the parts of the glyph row ROW on window W intersecting
5660 rectangle R. R is in window-relative coordinates. */
ee78dc32 5661
791f420f
JR
5662static void
5663expose_line (w, row, r)
5664 struct window *w;
5665 struct glyph_row *row;
5666 RECT *r;
5667{
5668 xassert (row->enabled_p);
5669
5670 if (row->mode_line_p || w->pseudo_window_p)
5671 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5672 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5673 NULL, NULL, 0);
ee78dc32 5674 else
791f420f
JR
5675 {
5676 if (row->used[LEFT_MARGIN_AREA])
5677 expose_area (w, row, r, LEFT_MARGIN_AREA);
5678 if (row->used[TEXT_AREA])
5679 expose_area (w, row, r, TEXT_AREA);
5680 if (row->used[RIGHT_MARGIN_AREA])
5681 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5682 x_draw_row_bitmaps (w, row);
5683 }
ee78dc32 5684}
ee78dc32 5685
ee78dc32 5686
791f420f 5687/* Return non-zero if W's cursor intersects rectangle R. */
96214669 5688
791f420f
JR
5689static int
5690x_phys_cursor_in_rect_p (w, r)
5691 struct window *w;
5692 RECT *r;
ee78dc32 5693{
791f420f
JR
5694 RECT cr, result;
5695 struct glyph *cursor_glyph;
ee78dc32 5696
791f420f
JR
5697 cursor_glyph = get_phys_cursor_glyph (w);
5698 if (cursor_glyph)
ee78dc32 5699 {
791f420f
JR
5700 cr.left = w->phys_cursor.x;
5701 cr.top = w->phys_cursor.y;
5702 cr.right = cr.left + cursor_glyph->pixel_width;
5703 cr.bottom = cr.top + w->phys_cursor_height;
158cba56 5704 return IntersectRect (&result, &cr, r);
ee78dc32
GV
5705 }
5706 else
791f420f 5707 return 0;
ee78dc32
GV
5708}
5709
791f420f
JR
5710
5711/* Redraw a rectangle of window W. R is a rectangle in window
5712 relative coordinates. Call this function with input blocked. */
5713
ee78dc32 5714static void
791f420f
JR
5715expose_window (w, r)
5716 struct window *w;
5717 RECT *r;
ee78dc32 5718{
791f420f
JR
5719 struct glyph_row *row;
5720 int y;
5721 int yb = window_text_bottom_y (w);
5722 int cursor_cleared_p;
5723
5724 /* If window is not yet fully initialized, do nothing. This can
5725 happen when toolkit scroll bars are used and a window is split.
5726 Reconfiguring the scroll bar will generate an expose for a newly
5727 created window. */
9436cdf9 5728 if (w->current_matrix == NULL || w == updated_window)
ee78dc32
GV
5729 return;
5730
791f420f
JR
5731 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5732 r->left, r->top, r->right, r->bottom));
ee78dc32 5733
791f420f
JR
5734 /* Convert to window coordinates. */
5735 r->left = FRAME_TO_WINDOW_PIXEL_X (w, r->left);
5736 r->top = FRAME_TO_WINDOW_PIXEL_Y (w, r->top);
5737 r->right = FRAME_TO_WINDOW_PIXEL_X (w, r->right);
5738 r->bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r->bottom);
ee78dc32 5739
791f420f
JR
5740 /* Turn off the cursor. */
5741 if (!w->pseudo_window_p
5742 && x_phys_cursor_in_rect_p (w, r))
5743 {
5744 x_clear_cursor (w);
5745 cursor_cleared_p = 1;
5746 }
5747 else
5748 cursor_cleared_p = 0;
5749
5750 /* Find the first row intersecting the rectangle R. */
5751 row = w->current_matrix->rows;
5752 y = 0;
5753 while (row->enabled_p
5754 && y < yb
5755 && y + row->height < r->top)
5756 {
5757 y += row->height;
5758 ++row;
5759 }
5760
5761 /* Display the text in the rectangle, one text line at a time. */
5762 while (row->enabled_p
5763 && y < yb
5764 && y < r->bottom)
5765 {
5766 expose_line (w, row, r);
5767 y += row->height;
5768 ++row;
5769 }
ee78dc32 5770
791f420f
JR
5771 /* Display the mode line if there is one. */
5772 if (WINDOW_WANTS_MODELINE_P (w)
5773 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5774 row->enabled_p)
5775 && row->y < r->bottom)
5776 expose_line (w, row, r);
689004fa 5777
791f420f
JR
5778 if (!w->pseudo_window_p)
5779 {
5780 /* Draw border between windows. */
5781 x_draw_vertical_border (w);
5782
5783 /* Turn the cursor on again. */
5784 if (cursor_cleared_p)
5785 x_update_window_cursor (w, 1);
5786 }
ee78dc32 5787}
ee78dc32 5788
ee78dc32
GV
5789\f
5790static void
5791frame_highlight (f)
5792 struct frame *f;
5793{
791f420f 5794 x_update_cursor (f, 1);
ee78dc32
GV
5795}
5796
5797static void
5798frame_unhighlight (f)
5799 struct frame *f;
5800{
791f420f 5801 x_update_cursor (f, 1);
ee78dc32
GV
5802}
5803
ee78dc32
GV
5804/* The focus has changed. Update the frames as necessary to reflect
5805 the new situation. Note that we can't change the selected frame
5806 here, because the Lisp code we are interrupting might become confused.
5807 Each event gets marked with the frame in which it occurred, so the
5808 Lisp code can tell when the switch took place by examining the events. */
5809
9ef2e2cf 5810static void
ee78dc32 5811x_new_focus_frame (dpyinfo, frame)
fbd6baed 5812 struct w32_display_info *dpyinfo;
ee78dc32
GV
5813 struct frame *frame;
5814{
fbd6baed 5815 struct frame *old_focus = dpyinfo->w32_focus_frame;
ee78dc32 5816
fbd6baed 5817 if (frame != dpyinfo->w32_focus_frame)
ee78dc32
GV
5818 {
5819 /* Set this before calling other routines, so that they see
fbd6baed
GV
5820 the correct value of w32_focus_frame. */
5821 dpyinfo->w32_focus_frame = frame;
ee78dc32
GV
5822
5823 if (old_focus && old_focus->auto_lower)
5824 x_lower_frame (old_focus);
5825
fbd6baed
GV
5826 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5827 pending_autoraise_frame = dpyinfo->w32_focus_frame;
ee78dc32
GV
5828 else
5829 pending_autoraise_frame = 0;
5830 }
5831
5832 x_frame_rehighlight (dpyinfo);
5833}
5834
5835/* Handle an event saying the mouse has moved out of an Emacs frame. */
5836
5837void
5838x_mouse_leave (dpyinfo)
fbd6baed 5839 struct w32_display_info *dpyinfo;
ee78dc32 5840{
fbd6baed 5841 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
ee78dc32
GV
5842}
5843
5844/* The focus has changed, or we have redirected a frame's focus to
5845 another frame (this happens when a frame uses a surrogate
9ef2e2cf 5846 mini-buffer frame). Shift the highlight as appropriate.
ee78dc32
GV
5847
5848 The FRAME argument doesn't necessarily have anything to do with which
9ef2e2cf
JR
5849 frame is being highlighted or un-highlighted; we only use it to find
5850 the appropriate X display info. */
5851
ee78dc32 5852static void
fbd6baed 5853w32_frame_rehighlight (frame)
ee78dc32
GV
5854 struct frame *frame;
5855{
7e6ac5b9
AI
5856 if (! FRAME_W32_P (frame))
5857 return;
fbd6baed 5858 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
ee78dc32
GV
5859}
5860
5861static void
5862x_frame_rehighlight (dpyinfo)
fbd6baed 5863 struct w32_display_info *dpyinfo;
ee78dc32 5864{
fbd6baed 5865 struct frame *old_highlight = dpyinfo->w32_highlight_frame;
ee78dc32 5866
fbd6baed 5867 if (dpyinfo->w32_focus_frame)
ee78dc32 5868 {
fbd6baed
GV
5869 dpyinfo->w32_highlight_frame
5870 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5871 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5872 : dpyinfo->w32_focus_frame);
5873 if (! FRAME_LIVE_P (dpyinfo->w32_highlight_frame))
ee78dc32 5874 {
fbd6baed
GV
5875 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
5876 dpyinfo->w32_highlight_frame = dpyinfo->w32_focus_frame;
ee78dc32
GV
5877 }
5878 }
5879 else
fbd6baed 5880 dpyinfo->w32_highlight_frame = 0;
ee78dc32 5881
fbd6baed 5882 if (dpyinfo->w32_highlight_frame != old_highlight)
ee78dc32
GV
5883 {
5884 if (old_highlight)
5885 frame_unhighlight (old_highlight);
fbd6baed
GV
5886 if (dpyinfo->w32_highlight_frame)
5887 frame_highlight (dpyinfo->w32_highlight_frame);
ee78dc32
GV
5888 }
5889}
5890\f
5891/* Keyboard processing - modifier keys, etc. */
5892
5893/* Convert a keysym to its name. */
5894
5895char *
5896x_get_keysym_name (keysym)
5897 int keysym;
5898{
5899 /* Make static so we can always return it */
5900 static char value[100];
5901
5902 BLOCK_INPUT;
9127e20e 5903 GetKeyNameText (keysym, value, 100);
ee78dc32
GV
5904 UNBLOCK_INPUT;
5905
5906 return value;
5907}
9ef2e2cf
JR
5908
5909
ee78dc32
GV
5910\f
5911/* Mouse clicks and mouse movement. Rah. */
5912
9ef2e2cf
JR
5913/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5914 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5915 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5916 not force the value into range. */
ee78dc32
GV
5917
5918void
5919pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5920 FRAME_PTR f;
5921 register int pix_x, pix_y;
5922 register int *x, *y;
5923 RECT *bounds;
5924 int noclip;
5925{
52cf03a1
GV
5926 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
5927 if (NILP (Vwindow_system))
5928 {
5929 *x = pix_x;
5930 *y = pix_y;
5931 return;
5932 }
5933
ee78dc32
GV
5934 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5935 even for negative values. */
5936 if (pix_x < 0)
9127e20e 5937 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
ee78dc32 5938 if (pix_y < 0)
fbd6baed 5939 pix_y -= (f)->output_data.w32->line_height - 1;
ee78dc32
GV
5940
5941 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5942 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5943
5944 if (bounds)
5945 {
5946 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
5947 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
9127e20e 5948 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
fbd6baed 5949 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
ee78dc32
GV
5950 }
5951
5952 if (!noclip)
5953 {
5954 if (pix_x < 0)
5955 pix_x = 0;
ec48c3a7
JR
5956 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5957 pix_x = FRAME_WINDOW_WIDTH (f);
ee78dc32
GV
5958
5959 if (pix_y < 0)
5960 pix_y = 0;
5961 else if (pix_y > f->height)
5962 pix_y = f->height;
5963 }
5964
5965 *x = pix_x;
5966 *y = pix_y;
5967}
5968
791f420f
JR
5969
5970/* Given HPOS/VPOS in the current matrix of W, return corresponding
5971 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5972 can't tell the positions because W's display is not up to date,
5973 return 0. */
5974
5975int
5976glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5977 struct window *w;
5978 int hpos, vpos;
5979 int *frame_x, *frame_y;
ee78dc32 5980{
791f420f
JR
5981 int success_p;
5982
5983 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5984 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5985
5986 if (display_completed)
52cf03a1 5987 {
791f420f
JR
5988 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5989 struct glyph *glyph = row->glyphs[TEXT_AREA];
5990 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5991
5992 *frame_y = row->y;
5993 *frame_x = row->x;
5994 while (glyph < end)
5995 {
5996 *frame_x += glyph->pixel_width;
5997 ++glyph;
5998 }
5999
6000 success_p = 1;
6001 }
6002 else
6003 {
6004 *frame_y = *frame_x = 0;
6005 success_p = 0;
52cf03a1
GV
6006 }
6007
791f420f
JR
6008 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6009 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6010 return success_p;
ee78dc32
GV
6011}
6012
6013BOOL
6014parse_button (message, pbutton, pup)
6015 int message;
6016 int * pbutton;
6017 int * pup;
6018{
6019 int button = 0;
6020 int up = 0;
6021
6022 switch (message)
6023 {
6024 case WM_LBUTTONDOWN:
6025 button = 0;
6026 up = 0;
6027 break;
6028 case WM_LBUTTONUP:
6029 button = 0;
6030 up = 1;
6031 break;
6032 case WM_MBUTTONDOWN:
fbd6baed 6033 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6034 button = 1;
6035 else
6036 button = 2;
ee78dc32
GV
6037 up = 0;
6038 break;
6039 case WM_MBUTTONUP:
fbd6baed 6040 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6041 button = 1;
6042 else
6043 button = 2;
ee78dc32
GV
6044 up = 1;
6045 break;
6046 case WM_RBUTTONDOWN:
fbd6baed 6047 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6048 button = 2;
6049 else
6050 button = 1;
ee78dc32
GV
6051 up = 0;
6052 break;
6053 case WM_RBUTTONUP:
fbd6baed 6054 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6055 button = 2;
6056 else
6057 button = 1;
ee78dc32
GV
6058 up = 1;
6059 break;
6060 default:
6061 return (FALSE);
6062 }
6063
6064 if (pup) *pup = up;
6065 if (pbutton) *pbutton = button;
6066
6067 return (TRUE);
6068}
6069
6070
6071/* Prepare a mouse-event in *RESULT for placement in the input queue.
6072
6073 If the event is a button press, then note that we have grabbed
6074 the mouse. */
6075
ec48c3a7 6076static Lisp_Object
ee78dc32
GV
6077construct_mouse_click (result, msg, f)
6078 struct input_event *result;
fbd6baed 6079 W32Msg *msg;
ee78dc32
GV
6080 struct frame *f;
6081{
6082 int button;
6083 int up;
6084
6085 parse_button (msg->msg.message, &button, &up);
6086
6087 /* Make the event type no_event; we'll change that when we decide
6088 otherwise. */
6089 result->kind = mouse_click;
6090 result->code = button;
6091 result->timestamp = msg->msg.time;
6092 result->modifiers = (msg->dwModifiers
6093 | (up
6094 ? up_modifier
6095 : down_modifier));
6096
ec48c3a7
JR
6097 XSETINT (result->x, LOWORD (msg->msg.lParam));
6098 XSETINT (result->y, HIWORD (msg->msg.lParam));
6099 XSETFRAME (result->frame_or_window, f);
6100 result->arg = Qnil;
6101 return Qnil;
ee78dc32
GV
6102}
6103
ec48c3a7 6104static Lisp_Object
689004fa
GV
6105construct_mouse_wheel (result, msg, f)
6106 struct input_event *result;
6107 W32Msg *msg;
6108 struct frame *f;
6109{
6110 POINT p;
6111 result->kind = mouse_wheel;
6112 result->code = (short) HIWORD (msg->msg.wParam);
6113 result->timestamp = msg->msg.time;
6114 result->modifiers = msg->dwModifiers;
6115 p.x = LOWORD (msg->msg.lParam);
6116 p.y = HIWORD (msg->msg.lParam);
9127e20e 6117 ScreenToClient (msg->msg.hwnd, &p);
689004fa
GV
6118 XSETINT (result->x, p.x);
6119 XSETINT (result->y, p.y);
6120 XSETFRAME (result->frame_or_window, f);
7e889510 6121 result->arg = Qnil;
ec48c3a7 6122 return Qnil;
689004fa
GV
6123}
6124
ec48c3a7 6125static Lisp_Object
12857dfd
RS
6126construct_drag_n_drop (result, msg, f)
6127 struct input_event *result;
6128 W32Msg *msg;
6129 struct frame *f;
6130{
6131 Lisp_Object files;
6132 Lisp_Object frame;
6133 HDROP hdrop;
6134 POINT p;
6135 WORD num_files;
6136 char *name;
6137 int i, len;
6138
6139 result->kind = drag_n_drop;
6140 result->code = 0;
6141 result->timestamp = msg->msg.time;
6142 result->modifiers = msg->dwModifiers;
6143
fb9cc9cc
AI
6144 hdrop = (HDROP) msg->msg.wParam;
6145 DragQueryPoint (hdrop, &p);
6146
e12ca9c2 6147#if 0
12857dfd
RS
6148 p.x = LOWORD (msg->msg.lParam);
6149 p.y = HIWORD (msg->msg.lParam);
6150 ScreenToClient (msg->msg.hwnd, &p);
e12ca9c2
AI
6151#endif
6152
12857dfd
RS
6153 XSETINT (result->x, p.x);
6154 XSETINT (result->y, p.y);
6155
12857dfd
RS
6156 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
6157 files = Qnil;
6158
6159 for (i = 0; i < num_files; i++)
6160 {
6161 len = DragQueryFile (hdrop, i, NULL, 0);
6162 if (len <= 0)
6163 continue;
6164 name = alloca (len + 1);
6165 DragQueryFile (hdrop, i, name, len + 1);
6166 files = Fcons (build_string (name), files);
6167 }
6168
6169 DragFinish (hdrop);
6170
6171 XSETFRAME (frame, f);
6172 result->frame_or_window = Fcons (frame, files);
7e889510 6173 result->arg = Qnil;
ec48c3a7 6174 return Qnil;
12857dfd
RS
6175}
6176
ee78dc32
GV
6177\f
6178/* Function to report a mouse movement to the mainstream Emacs code.
6179 The input handler calls this.
6180
6181 We have received a mouse movement event, which is given in *event.
6182 If the mouse is over a different glyph than it was last time, tell
6183 the mainstream emacs code by setting mouse_moved. If not, ask for
6184 another motion event, so we can check again the next time it moves. */
6185
791f420f
JR
6186static MSG last_mouse_motion_event;
6187static Lisp_Object last_mouse_motion_frame;
6188
ee78dc32
GV
6189static void
6190note_mouse_movement (frame, msg)
6191 FRAME_PTR frame;
6192 MSG *msg;
6193{
6194 last_mouse_movement_time = msg->time;
791f420f
JR
6195 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
6196 XSETFRAME (last_mouse_motion_frame, frame);
ee78dc32 6197
fbd6baed 6198 if (msg->hwnd != FRAME_W32_WINDOW (frame))
ee78dc32
GV
6199 {
6200 frame->mouse_moved = 1;
6201 last_mouse_scroll_bar = Qnil;
ee78dc32
GV
6202 note_mouse_highlight (frame, -1, -1);
6203 }
6204
6205 /* Has the mouse moved off the glyph it was on at the last sighting? */
6206 else if (LOWORD (msg->lParam) < last_mouse_glyph.left
6207 || LOWORD (msg->lParam) > last_mouse_glyph.right
9bf7b6aa 6208 || HIWORD (msg->lParam) < last_mouse_glyph.top
ee78dc32
GV
6209 || HIWORD (msg->lParam) > last_mouse_glyph.bottom)
6210 {
6211 frame->mouse_moved = 1;
6212 last_mouse_scroll_bar = Qnil;
ee78dc32
GV
6213 note_mouse_highlight (frame, LOWORD (msg->lParam), HIWORD (msg->lParam));
6214 }
6215}
6216
6217/* This is used for debugging, to turn off note_mouse_highlight. */
9ef2e2cf 6218
791f420f
JR
6219int disable_mouse_highlight;
6220
6221
6222\f
6223/************************************************************************
6224 Mouse Face
6225 ************************************************************************/
6226
6227/* Find the glyph under window-relative coordinates X/Y in window W.
6228 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6229 strings. Return in *HPOS and *VPOS the row and column number of
6230 the glyph found. Return in *AREA the glyph area containing X.
6231 Value is a pointer to the glyph found or null if X/Y is not on
6232 text, or we can't tell because W's current matrix is not up to
6233 date. */
6234
6235static struct glyph *
6236x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6237 struct window *w;
6238 int x, y;
6239 int *hpos, *vpos, *area;
6240{
6241 struct glyph *glyph, *end;
ec48c3a7 6242 struct glyph_row *row = NULL;
791f420f
JR
6243 int x0, i, left_area_width;
6244
6245 /* Find row containing Y. Give up if some row is not enabled. */
6246 for (i = 0; i < w->current_matrix->nrows; ++i)
6247 {
6248 row = MATRIX_ROW (w->current_matrix, i);
6249 if (!row->enabled_p)
6250 return NULL;
6251 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6252 break;
6253 }
6254
6255 *vpos = i;
6256 *hpos = 0;
6257
6258 /* Give up if Y is not in the window. */
6259 if (i == w->current_matrix->nrows)
6260 return NULL;
6261
6262 /* Get the glyph area containing X. */
6263 if (w->pseudo_window_p)
6264 {
6265 *area = TEXT_AREA;
6266 x0 = 0;
6267 }
6268 else
6269 {
6270 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6271 if (x < left_area_width)
6272 {
6273 *area = LEFT_MARGIN_AREA;
6274 x0 = 0;
6275 }
6276 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6277 {
6278 *area = TEXT_AREA;
6279 x0 = row->x + left_area_width;
6280 }
6281 else
6282 {
6283 *area = RIGHT_MARGIN_AREA;
6284 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6285 }
6286 }
6287
6288 /* Find glyph containing X. */
6289 glyph = row->glyphs[*area];
6290 end = glyph + row->used[*area];
6291 while (glyph < end)
6292 {
6293 if (x < x0 + glyph->pixel_width)
6294 {
6295 if (w->pseudo_window_p)
6296 break;
6297 else if (BUFFERP (glyph->object))
6298 break;
6299 }
6300
6301 x0 += glyph->pixel_width;
6302 ++glyph;
6303 }
6304
6305 if (glyph == end)
6306 return NULL;
6307
6308 *hpos = glyph - row->glyphs[*area];
6309 return glyph;
6310}
6311
6312
6313/* Convert frame-relative x/y to coordinates relative to window W.
6314 Takes pseudo-windows into account. */
6315
6316static void
6317frame_to_window_pixel_xy (w, x, y)
6318 struct window *w;
6319 int *x, *y;
6320{
6321 if (w->pseudo_window_p)
6322 {
6323 /* A pseudo-window is always full-width, and starts at the
6324 left edge of the frame, plus a frame border. */
6325 struct frame *f = XFRAME (w->frame);
6326 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6327 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6328 }
6329 else
6330 {
6331 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6332 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6333 }
6334}
6335
6336
c2cc16fa 6337/* Take proper action when mouse has moved to the mode or header line of
791f420f
JR
6338 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6339 mode line. X is relative to the start of the text display area of
6340 W, so the width of bitmap areas and scroll bars must be subtracted
6341 to get a position relative to the start of the mode line. */
6342
6343static void
6344note_mode_line_highlight (w, x, mode_line_p)
6345 struct window *w;
6346 int x, mode_line_p;
6347{
6348 struct frame *f = XFRAME (w->frame);
6349 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6350 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6351 struct glyph_row *row;
6352
6353 if (mode_line_p)
6354 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6355 else
6356 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6357
6358 if (row->enabled_p)
6359 {
6360 struct glyph *glyph, *end;
6361 Lisp_Object help, map;
6362 int x0;
6363
6364 /* Find the glyph under X. */
6365 glyph = row->glyphs[TEXT_AREA];
6366 end = glyph + row->used[TEXT_AREA];
6367 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6368 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
c2cc16fa 6369
791f420f
JR
6370 while (glyph < end
6371 && x >= x0 + glyph->pixel_width)
6372 {
6373 x0 += glyph->pixel_width;
6374 ++glyph;
6375 }
6376
6377 if (glyph < end
6378 && STRINGP (glyph->object)
6379 && XSTRING (glyph->object)->intervals
6380 && glyph->charpos >= 0
6381 && glyph->charpos < XSTRING (glyph->object)->size)
6382 {
6383 /* If we're on a string with `help-echo' text property,
6384 arrange for the help to be displayed. This is done by
6385 setting the global variable help_echo to the help string. */
6386 help = Fget_text_property (make_number (glyph->charpos),
6387 Qhelp_echo, glyph->object);
b7e80413 6388 if (!NILP (help))
ec48c3a7
JR
6389 {
6390 help_echo = help;
158cba56 6391 XSETWINDOW (help_echo_window, w);
ec48c3a7
JR
6392 help_echo_object = glyph->object;
6393 help_echo_pos = glyph->charpos;
6394 }
791f420f
JR
6395
6396 /* Change the mouse pointer according to what is under X/Y. */
6397 map = Fget_text_property (make_number (glyph->charpos),
6398 Qlocal_map, glyph->object);
02067692 6399 if (KEYMAPP (map))
791f420f 6400 cursor = f->output_data.w32->nontext_cursor;
c2cc16fa
JR
6401 else
6402 {
6403 map = Fget_text_property (make_number (glyph->charpos),
6404 Qkeymap, glyph->object);
6405 if (KEYMAPP (map))
6406 cursor = f->output_data.w32->nontext_cursor;
6407 }
791f420f
JR
6408 }
6409 }
9ef2e2cf 6410
01b220b6 6411#if 0 /* TODO: mouse cursor */
791f420f
JR
6412 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6413#endif
6414}
ee78dc32 6415
9ef2e2cf
JR
6416
6417/* Take proper action when the mouse has moved to position X, Y on
6418 frame F as regards highlighting characters that have mouse-face
6419 properties. Also de-highlighting chars where the mouse was before.
ee78dc32
GV
6420 X and Y can be negative or out of range. */
6421
6422static void
6423note_mouse_highlight (f, x, y)
9ef2e2cf 6424 struct frame *f;
ee78dc32
GV
6425 int x, y;
6426{
791f420f
JR
6427 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6428 int portion;
ee78dc32
GV
6429 Lisp_Object window;
6430 struct window *w;
6431
791f420f
JR
6432 /* When a menu is active, don't highlight because this looks odd. */
6433 if (popup_activated ())
6434 return;
6435
9ef2e2cf
JR
6436 if (disable_mouse_highlight
6437 || !f->glyphs_initialized_p)
ee78dc32
GV
6438 return;
6439
791f420f
JR
6440 dpyinfo->mouse_face_mouse_x = x;
6441 dpyinfo->mouse_face_mouse_y = y;
6442 dpyinfo->mouse_face_mouse_frame = f;
ee78dc32 6443
791f420f 6444 if (dpyinfo->mouse_face_defer)
ee78dc32
GV
6445 return;
6446
6447 if (gc_in_progress)
6448 {
791f420f 6449 dpyinfo->mouse_face_deferred_gc = 1;
ee78dc32
GV
6450 return;
6451 }
6452
ee78dc32 6453 /* Which window is that in? */
791f420f 6454 window = window_from_coordinates (f, x, y, &portion, 1);
ee78dc32
GV
6455
6456 /* If we were displaying active text in another window, clear that. */
791f420f
JR
6457 if (! EQ (window, dpyinfo->mouse_face_window))
6458 clear_mouse_face (dpyinfo);
6459
6460 /* Not on a window -> return. */
6461 if (!WINDOWP (window))
6462 return;
6463
6464 /* Convert to window-relative pixel coordinates. */
6465 w = XWINDOW (window);
6466 frame_to_window_pixel_xy (w, &x, &y);
6467
6468 /* Handle tool-bar window differently since it doesn't display a
6469 buffer. */
6470 if (EQ (window, f->tool_bar_window))
6471 {
6472 note_tool_bar_highlight (f, x, y);
6473 return;
6474 }
6475
6476 if (portion == 1 || portion == 3)
6477 {
6478 /* Mouse is on the mode or top line. */
6479 note_mode_line_highlight (w, x, portion == 1);
6480 return;
6481 }
01b220b6 6482#if 0 /* TODO: mouse cursor */
250cfece
JR
6483 else if (portion == 2)
6484 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6485 f->output_data.x->horizontal_drag_cursor);
791f420f
JR
6486 else
6487 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6488 f->output_data.x->text_cursor);
6489#endif
ee78dc32
GV
6490
6491 /* Are we in a window whose display is up to date?
6492 And verify the buffer's text has not changed. */
9ef2e2cf 6493 if (/* Within text portion of the window. */
791f420f 6494 portion == 0
ee78dc32 6495 && EQ (w->window_end_valid, w->buffer)
9ef2e2cf
JR
6496 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6497 && (XFASTINT (w->last_overlay_modified)
6498 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
ee78dc32 6499 {
791f420f
JR
6500 int hpos, vpos, pos, i, area;
6501 struct glyph *glyph;
6502
6503 /* Find the glyph under X/Y. */
6504 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6505
6506 /* Clear mouse face if X/Y not over text. */
6507 if (glyph == NULL
6508 || area != TEXT_AREA
6509 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6510 {
6511 clear_mouse_face (dpyinfo);
6512 return;
6513 }
6514
6515 pos = glyph->charpos;
6516 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6517
6518 /* Check for mouse-face and help-echo. */
6519 {
6520 Lisp_Object mouse_face, overlay, position;
6521 Lisp_Object *overlay_vec;
6522 int len, noverlays;
6523 struct buffer *obuf;
6524 int obegv, ozv;
6525
6526 /* If we get an out-of-range value, return now; avoid an error. */
6527 if (pos > BUF_Z (XBUFFER (w->buffer)))
6528 return;
6529
6530 /* Make the window's buffer temporarily current for
6531 overlays_at and compute_char_face. */
6532 obuf = current_buffer;
6533 current_buffer = XBUFFER (w->buffer);
6534 obegv = BEGV;
6535 ozv = ZV;
6536 BEGV = BEG;
6537 ZV = Z;
6538
6539 /* Is this char mouse-active or does it have help-echo? */
6540 XSETINT (position, pos);
6541
6542 /* Put all the overlays we want in a vector in overlay_vec.
6543 Store the length in len. If there are more than 10, make
6544 enough space for all, and try again. */
6545 len = 10;
6546 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
46636545 6547 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
791f420f
JR
6548 if (noverlays > len)
6549 {
6550 len = noverlays;
6551 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
46636545 6552 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
791f420f
JR
6553 }
6554
158cba56 6555 /* Sort overlays into increasing priority order. */
791f420f
JR
6556 noverlays = sort_overlays (overlay_vec, noverlays, w);
6557
6558 /* Check mouse-face highlighting. */
6559 if (! (EQ (window, dpyinfo->mouse_face_window)
6560 && vpos >= dpyinfo->mouse_face_beg_row
6561 && vpos <= dpyinfo->mouse_face_end_row
6562 && (vpos > dpyinfo->mouse_face_beg_row
6563 || hpos >= dpyinfo->mouse_face_beg_col)
6564 && (vpos < dpyinfo->mouse_face_end_row
6565 || hpos < dpyinfo->mouse_face_end_col
6566 || dpyinfo->mouse_face_past_end)))
6567 {
6568 /* Clear the display of the old active region, if any. */
6569 clear_mouse_face (dpyinfo);
6570
6571 /* Find the highest priority overlay that has a mouse-face prop. */
6572 overlay = Qnil;
158cba56 6573 for (i = noverlays - 1; i >= 0; --i)
791f420f
JR
6574 {
6575 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6576 if (!NILP (mouse_face))
6577 {
6578 overlay = overlay_vec[i];
6579 break;
6580 }
6581 }
6582
6583 /* If no overlay applies, get a text property. */
6584 if (NILP (overlay))
6585 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6586
6587 /* Handle the overlay case. */
6588 if (! NILP (overlay))
6589 {
6590 /* Find the range of text around this char that
6591 should be active. */
6592 Lisp_Object before, after;
6593 int ignore;
6594
6595 before = Foverlay_start (overlay);
6596 after = Foverlay_end (overlay);
6597 /* Record this as the current active region. */
6598 fast_find_position (w, XFASTINT (before),
6599 &dpyinfo->mouse_face_beg_col,
6600 &dpyinfo->mouse_face_beg_row,
6601 &dpyinfo->mouse_face_beg_x,
6602 &dpyinfo->mouse_face_beg_y);
6603 dpyinfo->mouse_face_past_end
6604 = !fast_find_position (w, XFASTINT (after),
6605 &dpyinfo->mouse_face_end_col,
6606 &dpyinfo->mouse_face_end_row,
6607 &dpyinfo->mouse_face_end_x,
6608 &dpyinfo->mouse_face_end_y);
6609 dpyinfo->mouse_face_window = window;
6610 dpyinfo->mouse_face_face_id
6611 = face_at_buffer_position (w, pos, 0, 0,
6612 &ignore, pos + 1, 1);
6613
6614 /* Display it as active. */
6615 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6616 }
6617 /* Handle the text property case. */
6618 else if (! NILP (mouse_face))
6619 {
6620 /* Find the range of text around this char that
6621 should be active. */
6622 Lisp_Object before, after, beginning, end;
6623 int ignore;
6624
6625 beginning = Fmarker_position (w->start);
6626 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6627 - XFASTINT (w->window_end_pos)));
6628 before
6629 = Fprevious_single_property_change (make_number (pos + 1),
6630 Qmouse_face,
6631 w->buffer, beginning);
6632 after
6633 = Fnext_single_property_change (position, Qmouse_face,
6634 w->buffer, end);
6635 /* Record this as the current active region. */
6636 fast_find_position (w, XFASTINT (before),
6637 &dpyinfo->mouse_face_beg_col,
6638 &dpyinfo->mouse_face_beg_row,
6639 &dpyinfo->mouse_face_beg_x,
6640 &dpyinfo->mouse_face_beg_y);
6641 dpyinfo->mouse_face_past_end
6642 = !fast_find_position (w, XFASTINT (after),
6643 &dpyinfo->mouse_face_end_col,
6644 &dpyinfo->mouse_face_end_row,
6645 &dpyinfo->mouse_face_end_x,
6646 &dpyinfo->mouse_face_end_y);
6647 dpyinfo->mouse_face_window = window;
6648 dpyinfo->mouse_face_face_id
6649 = face_at_buffer_position (w, pos, 0, 0,
6650 &ignore, pos + 1, 1);
6651
6652 /* Display it as active. */
6653 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6654 }
6655 }
6656
6657 /* Look for a `help-echo' property. */
6658 {
158cba56 6659 Lisp_Object help, overlay;
791f420f
JR
6660
6661 /* Check overlays first. */
c2cc16fa 6662 help = overlay = Qnil;
158cba56
JR
6663 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6664 {
6665 overlay = overlay_vec[i];
6666 help = Foverlay_get (overlay, Qhelp_echo);
6667 }
ec48c3a7
JR
6668
6669 if (!NILP (help))
6670 {
6671 help_echo = help;
158cba56
JR
6672 help_echo_window = window;
6673 help_echo_object = overlay;
ec48c3a7
JR
6674 help_echo_pos = pos;
6675 }
6676 else
6677 {
6678 /* Try text properties. */
6679 if ((STRINGP (glyph->object)
791f420f
JR
6680 && glyph->charpos >= 0
6681 && glyph->charpos < XSTRING (glyph->object)->size)
6682 || (BUFFERP (glyph->object)
6683 && glyph->charpos >= BEGV
ec48c3a7
JR
6684 && glyph->charpos < ZV))
6685 help = Fget_text_property (make_number (glyph->charpos),
6686 Qhelp_echo, glyph->object);
791f420f 6687
ec48c3a7
JR
6688 if (!NILP (help))
6689 {
6690 help_echo = help;
158cba56 6691 help_echo_window = window;
ec48c3a7
JR
6692 help_echo_object = glyph->object;
6693 help_echo_pos = glyph->charpos;
6694 }
6695 }
791f420f
JR
6696 }
6697
6698 BEGV = obegv;
6699 ZV = ozv;
6700 current_buffer = obuf;
6701 }
6702 }
6703}
6704
791f420f
JR
6705static void
6706redo_mouse_highlight ()
6707{
6708 if (!NILP (last_mouse_motion_frame)
6709 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6710 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6711 LOWORD (last_mouse_motion_event.lParam),
6712 HIWORD (last_mouse_motion_event.lParam));
6713}
6714
6715
6716\f
6717/***********************************************************************
6718 Tool-bars
6719 ***********************************************************************/
6720
6721static int x_tool_bar_item P_ ((struct frame *, int, int,
6722 struct glyph **, int *, int *, int *));
6723
6724/* Tool-bar item index of the item on which a mouse button was pressed
6725 or -1. */
6726
6727static int last_tool_bar_item;
6728
6729
6730/* Get information about the tool-bar item at position X/Y on frame F.
6731 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6732 the current matrix of the tool-bar window of F, or NULL if not
6733 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
d417b5e0 6734 item in F->tool_bar_items. Value is
791f420f
JR
6735
6736 -1 if X/Y is not on a tool-bar item
6737 0 if X/Y is on the same item that was highlighted before.
6738 1 otherwise. */
6739
6740static int
6741x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6742 struct frame *f;
6743 int x, y;
6744 struct glyph **glyph;
6745 int *hpos, *vpos, *prop_idx;
6746{
6747 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6748 struct window *w = XWINDOW (f->tool_bar_window);
6749 int area;
6750
6751 /* Find the glyph under X/Y. */
6752 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6753 if (*glyph == NULL)
6754 return -1;
6755
6756 /* Get the start of this tool-bar item's properties in
d417b5e0 6757 f->tool_bar_items. */
791f420f
JR
6758 if (!tool_bar_item_info (f, *glyph, prop_idx))
6759 return -1;
6760
6761 /* Is mouse on the highlighted item? */
6762 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6763 && *vpos >= dpyinfo->mouse_face_beg_row
6764 && *vpos <= dpyinfo->mouse_face_end_row
6765 && (*vpos > dpyinfo->mouse_face_beg_row
6766 || *hpos >= dpyinfo->mouse_face_beg_col)
6767 && (*vpos < dpyinfo->mouse_face_end_row
6768 || *hpos < dpyinfo->mouse_face_end_col
6769 || dpyinfo->mouse_face_past_end))
6770 return 0;
6771
6772 return 1;
6773}
6774
6775
9ef2e2cf 6776/* Handle mouse button event on the tool-bar of frame F, at
791f420f
JR
6777 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6778 or ButtonRelase. */
6779
6780static void
6781w32_handle_tool_bar_click (f, button_event)
6782 struct frame *f;
6783 struct input_event *button_event;
6784{
6785 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6786 struct window *w = XWINDOW (f->tool_bar_window);
6787 int hpos, vpos, prop_idx;
6788 struct glyph *glyph;
6789 Lisp_Object enabled_p;
6790 int x = XFASTINT (button_event->x);
6791 int y = XFASTINT (button_event->y);
6792
6793 /* If not on the highlighted tool-bar item, return. */
6794 frame_to_window_pixel_xy (w, &x, &y);
6795 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6796 return;
6797
6798 /* If item is disabled, do nothing. */
d417b5e0 6799 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
791f420f
JR
6800 if (NILP (enabled_p))
6801 return;
6802
6803 if (button_event->kind == mouse_click)
6804 {
6805 /* Show item in pressed state. */
6806 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6807 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6808 last_tool_bar_item = prop_idx;
6809 }
6810 else
6811 {
6812 Lisp_Object key, frame;
6813 struct input_event event;
6814
6815 /* Show item in released state. */
6816 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6817 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6818
d417b5e0 6819 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
791f420f
JR
6820
6821 XSETFRAME (frame, f);
6822 event.kind = TOOL_BAR_EVENT;
c8b33aa6
GM
6823 event.frame_or_window = frame;
6824 event.arg = frame;
791f420f
JR
6825 kbd_buffer_store_event (&event);
6826
6827 event.kind = TOOL_BAR_EVENT;
c8b33aa6
GM
6828 event.frame_or_window = frame;
6829 event.arg = key;
791f420f
JR
6830 event.modifiers = button_event->modifiers;
6831 kbd_buffer_store_event (&event);
6832 last_tool_bar_item = -1;
6833 }
6834}
6835
6836
9ef2e2cf 6837/* Possibly highlight a tool-bar item on frame F when mouse moves to
791f420f
JR
6838 tool-bar window-relative coordinates X/Y. Called from
6839 note_mouse_highlight. */
6840
6841static void
6842note_tool_bar_highlight (f, x, y)
6843 struct frame *f;
6844 int x, y;
6845{
6846 Lisp_Object window = f->tool_bar_window;
6847 struct window *w = XWINDOW (window);
6848 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6849 int hpos, vpos;
6850 struct glyph *glyph;
6851 struct glyph_row *row;
6852 int i;
6853 Lisp_Object enabled_p;
6854 int prop_idx;
6855 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6856 int mouse_down_p, rc;
6857
6858 /* Function note_mouse_highlight is called with negative x(y
6859 values when mouse moves outside of the frame. */
6860 if (x <= 0 || y <= 0)
6861 {
6862 clear_mouse_face (dpyinfo);
6863 return;
6864 }
6865
6866 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6867 if (rc < 0)
6868 {
6869 /* Not on tool-bar item. */
6870 clear_mouse_face (dpyinfo);
6871 return;
6872 }
6873 else if (rc == 0)
6874 /* On same tool-bar item as before. */
6875 goto set_help_echo;
ee78dc32 6876
791f420f
JR
6877 clear_mouse_face (dpyinfo);
6878
6879 /* Mouse is down, but on different tool-bar item? */
6880 mouse_down_p = (dpyinfo->grabbed
6881 && f == last_mouse_frame
6882 && FRAME_LIVE_P (f));
6883 if (mouse_down_p
6884 && last_tool_bar_item != prop_idx)
6885 return;
ee78dc32 6886
791f420f
JR
6887 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6888 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6889
6890 /* If tool-bar item is not enabled, don't highlight it. */
d417b5e0 6891 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
791f420f
JR
6892 if (!NILP (enabled_p))
6893 {
6894 /* Compute the x-position of the glyph. In front and past the
6895 image is a space. We include this is the highlighted area. */
6896 row = MATRIX_ROW (w->current_matrix, vpos);
6897 for (i = x = 0; i < hpos; ++i)
6898 x += row->glyphs[TEXT_AREA][i].pixel_width;
6899
6900 /* Record this as the current active region. */
6901 dpyinfo->mouse_face_beg_col = hpos;
6902 dpyinfo->mouse_face_beg_row = vpos;
6903 dpyinfo->mouse_face_beg_x = x;
6904 dpyinfo->mouse_face_beg_y = row->y;
6905 dpyinfo->mouse_face_past_end = 0;
6906
6907 dpyinfo->mouse_face_end_col = hpos + 1;
6908 dpyinfo->mouse_face_end_row = vpos;
6909 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6910 dpyinfo->mouse_face_end_y = row->y;
6911 dpyinfo->mouse_face_window = window;
6912 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6913
6914 /* Display it as active. */
6915 show_mouse_face (dpyinfo, draw);
6916 dpyinfo->mouse_face_image_state = draw;
ee78dc32 6917 }
791f420f
JR
6918
6919 set_help_echo:
6920
6921 /* Set help_echo to a help string.to display for this tool-bar item.
6922 w32_read_socket does the rest. */
158cba56 6923 help_echo_object = help_echo_window = Qnil;
ec48c3a7 6924 help_echo_pos = -1;
d417b5e0 6925 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 6926 if (NILP (help_echo))
d417b5e0 6927 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
ee78dc32 6928}
ee78dc32 6929
9ef2e2cf 6930
791f420f
JR
6931\f
6932/* Find the glyph matrix position of buffer position POS in window W.
6933 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6934 current glyphs must be up to date. If POS is above window start
6935 return (0, 0, 0, 0). If POS is after end of W, return end of
6936 last line in W. */
ee78dc32
GV
6937
6938static int
791f420f
JR
6939fast_find_position (w, pos, hpos, vpos, x, y)
6940 struct window *w;
ee78dc32 6941 int pos;
791f420f 6942 int *hpos, *vpos, *x, *y;
ee78dc32 6943{
ee78dc32 6944 int i;
ee78dc32 6945 int lastcol;
791f420f
JR
6946 int maybe_next_line_p = 0;
6947 int line_start_position;
6948 int yb = window_text_bottom_y (w);
6949 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6950 struct glyph_row *best_row = row;
6951 int row_vpos = 0, best_row_vpos = 0;
6952 int current_x;
6953
6954 while (row->y < yb)
ee78dc32 6955 {
791f420f
JR
6956 if (row->used[TEXT_AREA])
6957 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6958 else
6959 line_start_position = 0;
6960
6961 if (line_start_position > pos)
ee78dc32
GV
6962 break;
6963 /* If the position sought is the end of the buffer,
6964 don't include the blank lines at the bottom of the window. */
791f420f
JR
6965 else if (line_start_position == pos
6966 && pos == BUF_ZV (XBUFFER (w->buffer)))
ee78dc32 6967 {
791f420f 6968 maybe_next_line_p = 1;
ee78dc32
GV
6969 break;
6970 }
791f420f
JR
6971 else if (line_start_position > 0)
6972 {
6973 best_row = row;
6974 best_row_vpos = row_vpos;
6975 }
6976
9ef2e2cf
JR
6977 if (row->y + row->height >= yb)
6978 break;
6979
791f420f
JR
6980 ++row;
6981 ++row_vpos;
ee78dc32
GV
6982 }
6983
791f420f
JR
6984 /* Find the right column within BEST_ROW. */
6985 lastcol = 0;
6986 current_x = best_row->x;
6987 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
ee78dc32 6988 {
791f420f
JR
6989 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6990 int charpos;
6991
6992 charpos = glyph->charpos;
6993 if (charpos == pos)
ee78dc32 6994 {
791f420f
JR
6995 *hpos = i;
6996 *vpos = best_row_vpos;
6997 *x = current_x;
6998 *y = best_row->y;
ee78dc32
GV
6999 return 1;
7000 }
791f420f 7001 else if (charpos > pos)
ee78dc32 7002 break;
791f420f
JR
7003 else if (charpos > 0)
7004 lastcol = i;
7005
7006 current_x += glyph->pixel_width;
ee78dc32
GV
7007 }
7008
7009 /* If we're looking for the end of the buffer,
7010 and we didn't find it in the line we scanned,
7011 use the start of the following line. */
791f420f 7012 if (maybe_next_line_p)
ee78dc32 7013 {
791f420f
JR
7014 ++best_row;
7015 ++best_row_vpos;
7016 lastcol = 0;
7017 current_x = best_row->x;
ee78dc32
GV
7018 }
7019
791f420f
JR
7020 *vpos = best_row_vpos;
7021 *hpos = lastcol + 1;
7022 *x = current_x;
7023 *y = best_row->y;
ee78dc32
GV
7024 return 0;
7025}
7026
791f420f 7027
ee78dc32
GV
7028/* Display the active region described by mouse_face_*
7029 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7030
7031static void
791f420f 7032show_mouse_face (dpyinfo, draw)
fbd6baed 7033 struct w32_display_info *dpyinfo;
791f420f 7034 enum draw_glyphs_face draw;
ee78dc32
GV
7035{
7036 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
791f420f 7037 struct frame *f = XFRAME (WINDOW_FRAME (w));
ee78dc32 7038 int i;
791f420f
JR
7039 int cursor_off_p = 0;
7040 struct cursor_pos saved_cursor;
7041
7042 saved_cursor = output_cursor;
7043
7044 /* If window is in the process of being destroyed, don't bother
7045 to do anything. */
7046 if (w->current_matrix == NULL)
7047 goto set_x_cursor;
7048
7049 /* Recognize when we are called to operate on rows that don't exist
7050 anymore. This can happen when a window is split. */
7051 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7052 goto set_x_cursor;
7053
7054 set_output_cursor (&w->phys_cursor);
7055
7056 /* Note that mouse_face_beg_row etc. are window relative. */
7057 for (i = dpyinfo->mouse_face_beg_row;
7058 i <= dpyinfo->mouse_face_end_row;
7059 i++)
7060 {
7061 int start_hpos, end_hpos, start_x;
7062 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7063
7064 /* Don't do anything if row doesn't have valid contents. */
7065 if (!row->enabled_p)
7066 continue;
7067
7068 /* For all but the first row, the highlight starts at column 0. */
7069 if (i == dpyinfo->mouse_face_beg_row)
7070 {
7071 start_hpos = dpyinfo->mouse_face_beg_col;
7072 start_x = dpyinfo->mouse_face_beg_x;
7073 }
7074 else
7075 {
7076 start_hpos = 0;
7077 start_x = 0;
7078 }
7079
7080 if (i == dpyinfo->mouse_face_end_row)
7081 end_hpos = dpyinfo->mouse_face_end_col;
7082 else
7083 end_hpos = row->used[TEXT_AREA];
7084
7085 /* If the cursor's in the text we are about to rewrite, turn the
7086 cursor off. */
7087 if (!w->pseudo_window_p
7088 && i == output_cursor.vpos
7089 && output_cursor.hpos >= start_hpos - 1
7090 && output_cursor.hpos <= end_hpos)
ee78dc32 7091 {
791f420f
JR
7092 x_update_window_cursor (w, 0);
7093 cursor_off_p = 1;
ee78dc32
GV
7094 }
7095
791f420f 7096 if (end_hpos > start_hpos)
ec48c3a7 7097 {
ec48c3a7
JR
7098 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7099 start_hpos, end_hpos, draw, NULL, NULL, 0);
f4fccc1e 7100 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
ec48c3a7 7101 }
ee78dc32
GV
7102 }
7103
7104 /* If we turned the cursor off, turn it back on. */
791f420f
JR
7105 if (cursor_off_p)
7106 x_display_cursor (w, 1,
7107 output_cursor.hpos, output_cursor.vpos,
7108 output_cursor.x, output_cursor.y);
7109
7110 output_cursor = saved_cursor;
7111
7112 set_x_cursor:
01b220b6 7113#if 0 /* TODO: mouse cursor */
791f420f
JR
7114 /* Change the mouse cursor. */
7115 if (draw == DRAW_NORMAL_TEXT)
7116 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7117 f->output_data.x->text_cursor);
7118 else if (draw == DRAW_MOUSE_FACE)
7119 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7120 f->output_data.x->cross_cursor);
ee78dc32 7121 else
791f420f
JR
7122 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7123 f->output_data.x->nontext_cursor);
7124#endif
7125 ;
ee78dc32
GV
7126}
7127
7128/* Clear out the mouse-highlighted active region.
9ef2e2cf 7129 Redraw it un-highlighted first. */
ee78dc32 7130
791f420f 7131void
ee78dc32 7132clear_mouse_face (dpyinfo)
fbd6baed 7133 struct w32_display_info *dpyinfo;
ee78dc32 7134{
c2cc16fa
JR
7135#if 0 /* This prevents redrawing tool bar items when changing from one
7136 to another while a tooltip is open, so don't do it. */
25badd7a 7137 if (!NILP (tip_frame))
791f420f 7138 return;
c2cc16fa 7139#endif
791f420f 7140
ee78dc32 7141 if (! NILP (dpyinfo->mouse_face_window))
791f420f 7142 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
ee78dc32
GV
7143
7144 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7145 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7146 dpyinfo->mouse_face_window = Qnil;
7147}
31d4844a 7148
ec48c3a7
JR
7149
7150/* Clear any mouse-face on window W. This function is part of the
7151 redisplay interface, and is called from try_window_id and similar
7152 functions to ensure the mouse-highlight is off. */
7153
7154static void
7155x_clear_mouse_face (w)
7156 struct window *w;
7157{
7158 struct w32_display_info *dpyinfo
7159 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
7160 Lisp_Object window;
7161
9127e20e 7162 BLOCK_INPUT;
ec48c3a7
JR
7163 XSETWINDOW (window, w);
7164 if (EQ (window, dpyinfo->mouse_face_window))
7165 clear_mouse_face (dpyinfo);
9127e20e 7166 UNBLOCK_INPUT;
ec48c3a7
JR
7167}
7168
7169
31d4844a
KH
7170/* Just discard the mouse face information for frame F, if any.
7171 This is used when the size of F is changed. */
7172
7173void
7174cancel_mouse_face (f)
7175 FRAME_PTR f;
7176{
7177 Lisp_Object window;
7178 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7179
7180 window = dpyinfo->mouse_face_window;
7181 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7182 {
7183 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7184 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7185 dpyinfo->mouse_face_window = Qnil;
7186 }
7187}
ee78dc32 7188\f
9ef2e2cf 7189static struct scroll_bar *x_window_to_scroll_bar ();
ee78dc32
GV
7190static void x_scroll_bar_report_motion ();
7191
7192/* Return the current position of the mouse.
7193 *fp should be a frame which indicates which display to ask about.
7194
7195 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7196 and *part to the frame, window, and scroll bar part that the mouse
7197 is over. Set *x and *y to the portion and whole of the mouse's
7198 position on the scroll bar.
7199
7200 If the mouse movement started elsewhere, set *fp to the frame the
7201 mouse is on, *bar_window to nil, and *x and *y to the character cell
7202 the mouse is over.
7203
9ef2e2cf 7204 Set *time to the server time-stamp for the time at which the mouse
ee78dc32
GV
7205 was at this position.
7206
7207 Don't store anything if we don't have a valid set of values to report.
7208
7209 This clears the mouse_moved flag, so we can wait for the next mouse
9ef2e2cf 7210 movement. */
ee78dc32
GV
7211
7212static void
fbd6baed 7213w32_mouse_position (fp, insist, bar_window, part, x, y, time)
ee78dc32
GV
7214 FRAME_PTR *fp;
7215 int insist;
7216 Lisp_Object *bar_window;
7217 enum scroll_bar_part *part;
7218 Lisp_Object *x, *y;
7219 unsigned long *time;
7220{
7221 FRAME_PTR f1;
7222
7223 BLOCK_INPUT;
7224
95fa970d 7225 if (! NILP (last_mouse_scroll_bar) && insist == 0)
ee78dc32
GV
7226 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7227 else
7228 {
7229 POINT pt;
7230
7231 Lisp_Object frame, tail;
7232
7233 /* Clear the mouse-moved flag for every frame on this display. */
7234 FOR_EACH_FRAME (tail, frame)
7235 XFRAME (frame)->mouse_moved = 0;
7236
7237 last_mouse_scroll_bar = Qnil;
7238
7239 GetCursorPos (&pt);
7240
7241 /* Now we have a position on the root; find the innermost window
7242 containing the pointer. */
7243 {
fbd6baed 7244 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
ee78dc32
GV
7245 && FRAME_LIVE_P (last_mouse_frame))
7246 {
08712a41
AI
7247 /* If mouse was grabbed on a frame, give coords for that frame
7248 even if the mouse is now outside it. */
ee78dc32
GV
7249 f1 = last_mouse_frame;
7250 }
7251 else
7252 {
08712a41 7253 /* Is window under mouse one of our frames? */
9127e20e
JR
7254 f1 = x_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
7255 WindowFromPoint (pt));
ee78dc32
GV
7256 }
7257
7258 /* If not, is it one of our scroll bars? */
7259 if (! f1)
7260 {
9127e20e
JR
7261 struct scroll_bar *bar
7262 = x_window_to_scroll_bar (WindowFromPoint (pt));
ee78dc32
GV
7263
7264 if (bar)
7265 {
7266 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7267 }
7268 }
7269
95fa970d 7270 if (f1 == 0 && insist > 0)
791f420f 7271 f1 = SELECTED_FRAME ();
ee78dc32
GV
7272
7273 if (f1)
7274 {
791f420f
JR
7275 /* Ok, we found a frame. Store all the values.
7276 last_mouse_glyph is a rectangle used to reduce the
7277 generation of mouse events. To not miss any motion
7278 events, we must divide the frame into rectangles of the
7279 size of the smallest character that could be displayed
7280 on it, i.e. into the same rectangles that matrices on
7281 the frame are divided into. */
7282
7283#if OLD_REDISPLAY_CODE
ee78dc32
GV
7284 int ignore1, ignore2;
7285
fbd6baed 7286 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
ee78dc32 7287
ee78dc32
GV
7288 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
7289 &last_mouse_glyph,
fbd6baed 7290 FRAME_W32_DISPLAY_INFO (f1)->grabbed
ee78dc32 7291 || insist);
791f420f
JR
7292#else
7293 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7294 {
7295 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7296 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7297 int x = pt.x;
7298 int y = pt.y;
7299
7300 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7301 round down even for negative values. */
7302 if (x < 0)
7303 x -= width - 1;
7304 if (y < 0)
7305 y -= height - 1;
7306
7307 last_mouse_glyph.left = (x + width - 1) / width * width;
7308 last_mouse_glyph.top = (y + height - 1) / height * height;
7309 last_mouse_glyph.right = last_mouse_glyph.left + width;
7310 last_mouse_glyph.bottom = last_mouse_glyph.top + height;
7311 }
7312#endif
ee78dc32
GV
7313
7314 *bar_window = Qnil;
7315 *part = 0;
7316 *fp = f1;
7317 XSETINT (*x, pt.x);
7318 XSETINT (*y, pt.y);
7319 *time = last_mouse_movement_time;
7320 }
7321 }
7322 }
7323
7324 UNBLOCK_INPUT;
7325}
791f420f 7326
ee78dc32
GV
7327\f
7328/* Scroll bar support. */
7329
ec48c3a7 7330/* Given a window ID, find the struct scroll_bar which manages it.
ee78dc32
GV
7331 This can be called in GC, so we have to make sure to strip off mark
7332 bits. */
9ef2e2cf
JR
7333
7334static struct scroll_bar *
ee78dc32
GV
7335x_window_to_scroll_bar (window_id)
7336 Window window_id;
7337{
791f420f 7338 Lisp_Object tail;
ee78dc32
GV
7339
7340 for (tail = Vframe_list;
7341 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7342 tail = XCDR (tail))
ee78dc32
GV
7343 {
7344 Lisp_Object frame, bar, condemned;
7345
8e713be6 7346 frame = XCAR (tail);
ee78dc32
GV
7347 /* All elements of Vframe_list should be frames. */
7348 if (! GC_FRAMEP (frame))
7349 abort ();
7350
7351 /* Scan this frame's scroll bar list for a scroll bar with the
7352 right window ID. */
7353 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7354 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7355 /* This trick allows us to search both the ordinary and
7356 condemned scroll bar lists with one loop. */
7357 ! GC_NILP (bar) || (bar = condemned,
7358 condemned = Qnil,
7359 ! GC_NILP (bar));
7360 bar = XSCROLL_BAR (bar)->next)
fbd6baed 7361 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
ee78dc32
GV
7362 return XSCROLL_BAR (bar);
7363 }
7364
7365 return 0;
7366}
7367
791f420f
JR
7368
7369\f
7370/* Set the thumb size and position of scroll bar BAR. We are currently
7371 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7372
7373static void
7374w32_set_scroll_bar_thumb (bar, portion, position, whole)
7375 struct scroll_bar *bar;
7376 int portion, position, whole;
7377{
7378 Window w = SCROLL_BAR_W32_WINDOW (bar);
d8e675f5 7379 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
791f420f
JR
7380 int sb_page, sb_pos;
7381 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
7382
7383 if (whole)
7384 {
7385 /* Position scroll bar at rock bottom if the bottom of the
7386 buffer is visible. This avoids shinking the thumb away
7387 to nothing if it is held at the bottom of the buffer. */
7388 if (position + portion >= whole)
7389 {
7390 sb_page = range * (whole - position) / whole
7391 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7392 sb_pos = range;
7393 }
7394
7395 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7396 sb_pos = position * range / whole;
7397 }
7398 else
7399 {
7400 sb_page = range;
7401 sb_pos = 0;
7402 }
7403
7404 BLOCK_INPUT;
7405
7406 if (pfnSetScrollInfo)
7407 {
7408 SCROLLINFO si;
7409
7410 si.cbSize = sizeof (si);
7411 /* Only update page size if currently dragging, to reduce
7412 flicker effects. */
7413 if (draggingp)
7414 si.fMask = SIF_PAGE;
7415 else
7416 si.fMask = SIF_PAGE | SIF_POS;
7417 si.nPage = sb_page;
7418 si.nPos = sb_pos;
7419
7420 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
7421 }
7422 else
7423 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
7424
7425 UNBLOCK_INPUT;
7426}
7427
7428\f
7429/************************************************************************
7430 Scroll bars, general
7431 ************************************************************************/
7432
ee78dc32
GV
7433HWND
7434my_create_scrollbar (f, bar)
7435 struct frame * f;
7436 struct scroll_bar * bar;
7437{
689004fa
GV
7438 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
7439 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
7440 (LPARAM) bar);
ee78dc32
GV
7441}
7442
52cf03a1
GV
7443//#define ATTACH_THREADS
7444
689004fa
GV
7445BOOL
7446my_show_window (FRAME_PTR f, HWND hwnd, int how)
52cf03a1
GV
7447{
7448#ifndef ATTACH_THREADS
689004fa
GV
7449 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
7450 (WPARAM) hwnd, (LPARAM) how);
52cf03a1 7451#else
689004fa 7452 return ShowWindow (hwnd, how);
52cf03a1
GV
7453#endif
7454}
7455
7456void
7457my_set_window_pos (HWND hwnd, HWND hwndAfter,
689004fa 7458 int x, int y, int cx, int cy, UINT flags)
52cf03a1
GV
7459{
7460#ifndef ATTACH_THREADS
689004fa
GV
7461 WINDOWPOS pos;
7462 pos.hwndInsertAfter = hwndAfter;
52cf03a1
GV
7463 pos.x = x;
7464 pos.y = y;
7465 pos.cx = cx;
7466 pos.cy = cy;
7467 pos.flags = flags;
7468 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
7469#else
7470 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
7471#endif
7472}
7473
ec48c3a7 7474void
689004fa
GV
7475my_set_focus (f, hwnd)
7476 struct frame * f;
7477 HWND hwnd;
7478{
7479 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
7480 (WPARAM) hwnd, 0);
7481}
7482
ec48c3a7 7483void
ef0e360f
GV
7484my_set_foreground_window (hwnd)
7485 HWND hwnd;
7486{
7487 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
7488}
7489
ee78dc32
GV
7490void
7491my_destroy_window (f, hwnd)
7492 struct frame * f;
7493 HWND hwnd;
7494{
fbd6baed 7495 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
ee78dc32
GV
7496 (WPARAM) hwnd, 0);
7497}
7498
791f420f
JR
7499/* Create a scroll bar and return the scroll bar vector for it. W is
7500 the Emacs window on which to create the scroll bar. TOP, LEFT,
7501 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7502 scroll bar. */
7503
ee78dc32 7504static struct scroll_bar *
791f420f
JR
7505x_scroll_bar_create (w, top, left, width, height)
7506 struct window *w;
ee78dc32
GV
7507 int top, left, width, height;
7508{
791f420f
JR
7509 struct frame *f = XFRAME (WINDOW_FRAME (w));
7510 HWND hwnd;
ee78dc32
GV
7511 struct scroll_bar *bar
7512 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
ee78dc32
GV
7513
7514 BLOCK_INPUT;
7515
791f420f 7516 XSETWINDOW (bar->window, w);
ee78dc32
GV
7517 XSETINT (bar->top, top);
7518 XSETINT (bar->left, left);
7519 XSETINT (bar->width, width);
7520 XSETINT (bar->height, height);
7521 XSETINT (bar->start, 0);
7522 XSETINT (bar->end, 0);
7523 bar->dragging = Qnil;
7524
7525 /* Requires geometry to be set before call to create the real window */
7526
7527 hwnd = my_create_scrollbar (f, bar);
7528
689004fa
GV
7529 if (pfnSetScrollInfo)
7530 {
7531 SCROLLINFO si;
7532
7533 si.cbSize = sizeof (si);
7534 si.fMask = SIF_ALL;
7535 si.nMin = 0;
791f420f
JR
7536 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7537 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
689004fa
GV
7538 si.nPage = si.nMax;
7539 si.nPos = 0;
7540
7541 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7542 }
7543 else
7544 {
791f420f
JR
7545 SetScrollRange (hwnd, SB_CTL, 0,
7546 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
689004fa
GV
7547 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
7548 }
ee78dc32 7549
fbd6baed 7550 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
ee78dc32
GV
7551
7552 /* Add bar to its frame's list of scroll bars. */
7553 bar->next = FRAME_SCROLL_BARS (f);
7554 bar->prev = Qnil;
7555 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7556 if (! NILP (bar->next))
7557 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7558
7559 UNBLOCK_INPUT;
7560
7561 return bar;
7562}
7563
ee78dc32 7564
791f420f
JR
7565/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7566 nil. */
ee78dc32 7567
ee78dc32
GV
7568static void
7569x_scroll_bar_remove (bar)
7570 struct scroll_bar *bar;
7571{
7572 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7573
7574 BLOCK_INPUT;
7575
7576 /* Destroy the window. */
fbd6baed 7577 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
ee78dc32
GV
7578
7579 /* Disassociate this scroll bar from its window. */
7580 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
7581
7582 UNBLOCK_INPUT;
7583}
7584
7585/* Set the handle of the vertical scroll bar for WINDOW to indicate
7586 that we are displaying PORTION characters out of a total of WHOLE
7587 characters, starting at POSITION. If WINDOW has no scroll bar,
7588 create one. */
7589static void
791f420f
JR
7590w32_set_vertical_scroll_bar (w, portion, whole, position)
7591 struct window *w;
ee78dc32
GV
7592 int portion, whole, position;
7593{
791f420f 7594 struct frame *f = XFRAME (w->frame);
ee78dc32 7595 struct scroll_bar *bar;
9ef2e2cf 7596 int top, height, left, sb_left, width, sb_width;
791f420f
JR
7597 int window_x, window_y, window_width, window_height;
7598
7599 /* Get window dimensions. */
7600 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7601 top = window_y;
7602 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7603 height = window_height;
7604
7605 /* Compute the left edge of the scroll bar area. */
7606 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7607 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7608 else
7609 left = XFASTINT (w->left);
7610 left *= CANON_X_UNIT (f);
7611 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7612
7613 /* Compute the width of the scroll bar which might be less than
7614 the width of the area reserved for the scroll bar. */
7615 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7616 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
7617 else
7618 sb_width = width;
ee78dc32 7619
791f420f
JR
7620 /* Compute the left edge of the scroll bar. */
7621 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7622 sb_left = left + width - sb_width - (width - sb_width) / 2;
ee78dc32 7623 else
791f420f
JR
7624 sb_left = left + (width - sb_width) / 2;
7625
7626 /* Does the scroll bar exist yet? */
7627 if (NILP (w->vertical_scroll_bar))
ee78dc32 7628 {
00fe468b 7629 HDC hdc;
791f420f 7630 BLOCK_INPUT;
00fe468b
JR
7631 hdc = get_frame_dc (f);
7632 w32_clear_area (f, hdc, left, top, width, height);
7633 release_frame_dc (f, hdc);
791f420f 7634 UNBLOCK_INPUT;
00fe468b 7635
791f420f 7636 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
ee78dc32 7637 }
791f420f
JR
7638 else
7639 {
7640 /* It may just need to be moved and resized. */
7641 HWND hwnd;
7642
7643 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7644 hwnd = SCROLL_BAR_W32_WINDOW (bar);
7645
7646 /* If already correctly positioned, do nothing. */
7647 if ( XINT (bar->left) == sb_left
7648 && XINT (bar->top) == top
7649 && XINT (bar->width) == sb_width
7650 && XINT (bar->height) == height )
7651 {
7652 /* Redraw after clear_frame. */
7653 if (!my_show_window (f, hwnd, SW_NORMAL))
7654 InvalidateRect (hwnd, NULL, FALSE);
7655 }
7656 else
7657 {
00fe468b 7658 HDC hdc;
791f420f
JR
7659 BLOCK_INPUT;
7660
00fe468b 7661 hdc = get_frame_dc (f);
791f420f
JR
7662 /* Since Windows scroll bars are smaller than the space reserved
7663 for them on the frame, we have to clear "under" them. */
00fe468b 7664 w32_clear_area (f, hdc,
791f420f
JR
7665 left,
7666 top,
7667 width,
7668 height);
00fe468b 7669 release_frame_dc (f, hdc);
791f420f
JR
7670
7671 /* Make sure scroll bar is "visible" before moving, to ensure the
7672 area of the parent window now exposed will be refreshed. */
7673 my_show_window (f, hwnd, SW_HIDE);
7674 MoveWindow (hwnd, sb_left, top,
7675 sb_width, height, TRUE);
7676 if (pfnSetScrollInfo)
7677 {
7678 SCROLLINFO si;
ee78dc32 7679
791f420f
JR
7680 si.cbSize = sizeof (si);
7681 si.fMask = SIF_RANGE;
7682 si.nMin = 0;
7683 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7684 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
ee78dc32 7685
00fe468b 7686 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
791f420f
JR
7687 }
7688 else
00fe468b 7689 SetScrollRange (hwnd, SB_CTL, 0,
791f420f
JR
7690 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7691 my_show_window (f, hwnd, SW_NORMAL);
7692 // InvalidateRect (w, NULL, FALSE);
7693
7694 /* Remember new settings. */
7695 XSETINT (bar->left, sb_left);
7696 XSETINT (bar->top, top);
7697 XSETINT (bar->width, sb_width);
7698 XSETINT (bar->height, height);
7699
7700 UNBLOCK_INPUT;
7701 }
7702 }
7703 w32_set_scroll_bar_thumb (bar, portion, position, whole);
ee78dc32 7704
791f420f 7705 XSETVECTOR (w->vertical_scroll_bar, bar);
ee78dc32
GV
7706}
7707
7708
7709/* The following three hooks are used when we're doing a thorough
7710 redisplay of the frame. We don't explicitly know which scroll bars
7711 are going to be deleted, because keeping track of when windows go
7712 away is a real pain - "Can you say set-window-configuration, boys
7713 and girls?" Instead, we just assert at the beginning of redisplay
7714 that *all* scroll bars are to be removed, and then save a scroll bar
7715 from the fiery pit when we actually redisplay its window. */
7716
7717/* Arrange for all scroll bars on FRAME to be removed at the next call
7718 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9ef2e2cf
JR
7719 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7720
ee78dc32 7721static void
fbd6baed 7722w32_condemn_scroll_bars (frame)
ee78dc32
GV
7723 FRAME_PTR frame;
7724{
ef0e360f
GV
7725 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7726 while (! NILP (FRAME_SCROLL_BARS (frame)))
7727 {
7728 Lisp_Object bar;
7729 bar = FRAME_SCROLL_BARS (frame);
7730 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7731 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7732 XSCROLL_BAR (bar)->prev = Qnil;
7733 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7734 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7735 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7736 }
ee78dc32
GV
7737}
7738
c2cc16fa 7739
9ef2e2cf 7740/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
ee78dc32 7741 Note that WINDOW isn't necessarily condemned at all. */
c2cc16fa 7742
ee78dc32 7743static void
fbd6baed 7744w32_redeem_scroll_bar (window)
ee78dc32
GV
7745 struct window *window;
7746{
7747 struct scroll_bar *bar;
c2cc16fa 7748 struct frame *f;
ee78dc32
GV
7749
7750 /* We can't redeem this window's scroll bar if it doesn't have one. */
7751 if (NILP (window->vertical_scroll_bar))
7752 abort ();
7753
7754 bar = XSCROLL_BAR (window->vertical_scroll_bar);
7755
ef0e360f 7756 /* Unlink it from the condemned list. */
c2cc16fa
JR
7757 f = XFRAME (WINDOW_FRAME (window));
7758 if (NILP (bar->prev))
7759 {
7760 /* If the prev pointer is nil, it must be the first in one of
7761 the lists. */
7762 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
7763 /* It's not condemned. Everything's fine. */
7764 return;
7765 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7766 window->vertical_scroll_bar))
7767 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
7768 else
7769 /* If its prev pointer is nil, it must be at the front of
7770 one or the other! */
7771 abort ();
7772 }
7773 else
7774 XSCROLL_BAR (bar->prev)->next = bar->next;
ef0e360f 7775
c2cc16fa
JR
7776 if (! NILP (bar->next))
7777 XSCROLL_BAR (bar->next)->prev = bar->prev;
ef0e360f 7778
c2cc16fa
JR
7779 bar->next = FRAME_SCROLL_BARS (f);
7780 bar->prev = Qnil;
7781 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7782 if (! NILP (bar->next))
7783 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
ee78dc32
GV
7784}
7785
7786/* Remove all scroll bars on FRAME that haven't been saved since the
7787 last call to `*condemn_scroll_bars_hook'. */
9ef2e2cf 7788
ee78dc32 7789static void
fbd6baed 7790w32_judge_scroll_bars (f)
ee78dc32
GV
7791 FRAME_PTR f;
7792{
7793 Lisp_Object bar, next;
7794
7795 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7796
7797 /* Clear out the condemned list now so we won't try to process any
7798 more events on the hapless scroll bars. */
7799 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
7800
7801 for (; ! NILP (bar); bar = next)
7802 {
7803 struct scroll_bar *b = XSCROLL_BAR (bar);
7804
7805 x_scroll_bar_remove (b);
7806
7807 next = b->next;
7808 b->next = b->prev = Qnil;
7809 }
7810
7811 /* Now there should be no references to the condemned scroll bars,
7812 and they should get garbage-collected. */
7813}
7814
7815/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7816 is set to something other than no_event, it is enqueued.
7817
7818 This may be called from a signal handler, so we have to ignore GC
7819 mark bits. */
2c28562d 7820
52cf03a1 7821static int
c2cc16fa 7822w32_scroll_bar_handle_click (bar, msg, emacs_event)
ee78dc32 7823 struct scroll_bar *bar;
fbd6baed 7824 W32Msg *msg;
ee78dc32
GV
7825 struct input_event *emacs_event;
7826{
7827 if (! GC_WINDOWP (bar->window))
7828 abort ();
7829
fbd6baed 7830 emacs_event->kind = w32_scroll_bar_click;
ee78dc32 7831 emacs_event->code = 0;
52cf03a1
GV
7832 /* not really meaningful to distinguish up/down */
7833 emacs_event->modifiers = msg->dwModifiers;
ee78dc32 7834 emacs_event->frame_or_window = bar->window;
7e889510 7835 emacs_event->arg = Qnil;
ee78dc32
GV
7836 emacs_event->timestamp = msg->msg.time;
7837
7838 {
791f420f 7839 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
689004fa
GV
7840 int y;
7841 int dragging = !NILP (bar->dragging);
7842
7843 if (pfnGetScrollInfo)
7844 {
7845 SCROLLINFO si;
7846
7847 si.cbSize = sizeof (si);
7848 si.fMask = SIF_POS;
7849
7850 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
7851 y = si.nPos;
7852 }
7853 else
7854 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
7855
7856 bar->dragging = Qnil;
ee78dc32 7857
9ef2e2cf
JR
7858
7859 last_mouse_scroll_bar_pos = msg->msg.wParam;
7860
ee78dc32 7861 switch (LOWORD (msg->msg.wParam))
2c28562d 7862 {
2c28562d 7863 case SB_LINEDOWN:
52cf03a1 7864 emacs_event->part = scroll_bar_down_arrow;
ee78dc32 7865 break;
2c28562d 7866 case SB_LINEUP:
52cf03a1 7867 emacs_event->part = scroll_bar_up_arrow;
ee78dc32 7868 break;
2c28562d 7869 case SB_PAGEUP:
ee78dc32
GV
7870 emacs_event->part = scroll_bar_above_handle;
7871 break;
2c28562d 7872 case SB_PAGEDOWN:
ee78dc32
GV
7873 emacs_event->part = scroll_bar_below_handle;
7874 break;
2c28562d 7875 case SB_TOP:
ee78dc32
GV
7876 emacs_event->part = scroll_bar_handle;
7877 y = 0;
7878 break;
2c28562d 7879 case SB_BOTTOM:
ee78dc32
GV
7880 emacs_event->part = scroll_bar_handle;
7881 y = top_range;
7882 break;
689004fa 7883 case SB_THUMBTRACK:
2c28562d 7884 case SB_THUMBPOSITION:
791f420f
JR
7885 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7886 y = HIWORD (msg->msg.wParam);
689004fa 7887 bar->dragging = Qt;
ee78dc32 7888 emacs_event->part = scroll_bar_handle;
689004fa
GV
7889
7890 /* "Silently" update current position. */
7891 if (pfnSetScrollInfo)
7892 {
7893 SCROLLINFO si;
7894
7895 si.cbSize = sizeof (si);
7896 si.fMask = SIF_POS;
689004fa
GV
7897 si.nPos = y;
7898 /* Remember apparent position (we actually lag behind the real
7899 position, so don't set that directly. */
7900 last_scroll_bar_drag_pos = y;
7901
7902 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
7903 }
7904 else
7905 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
ee78dc32 7906 break;
2c28562d 7907 case SB_ENDSCROLL:
689004fa
GV
7908 /* If this is the end of a drag sequence, then reset the scroll
7909 handle size to normal and do a final redraw. Otherwise do
7910 nothing. */
7911 if (dragging)
7912 {
7913 if (pfnSetScrollInfo)
7914 {
7915 SCROLLINFO si;
7916 int start = XINT (bar->start);
7917 int end = XINT (bar->end);
7918
7919 si.cbSize = sizeof (si);
7920 si.fMask = SIF_PAGE | SIF_POS;
791f420f 7921 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
689004fa 7922 si.nPos = last_scroll_bar_drag_pos;
689004fa
GV
7923 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
7924 }
7925 else
7926 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
7927 }
7928 /* fall through */
2c28562d 7929 default:
689004fa 7930 emacs_event->kind = no_event;
52cf03a1 7931 return FALSE;
2c28562d 7932 }
52cf03a1 7933
ee78dc32
GV
7934 XSETINT (emacs_event->x, y);
7935 XSETINT (emacs_event->y, top_range);
52cf03a1
GV
7936
7937 return TRUE;
ee78dc32
GV
7938 }
7939}
7940
7941/* Return information to the user about the current position of the mouse
7942 on the scroll bar. */
9ef2e2cf 7943
ee78dc32
GV
7944static void
7945x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
7946 FRAME_PTR *fp;
7947 Lisp_Object *bar_window;
7948 enum scroll_bar_part *part;
7949 Lisp_Object *x, *y;
7950 unsigned long *time;
7951{
7952 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
fbd6baed 7953 Window w = SCROLL_BAR_W32_WINDOW (bar);
ee78dc32
GV
7954 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7955 int pos;
791f420f 7956 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ee78dc32
GV
7957
7958 BLOCK_INPUT;
7959
7960 *fp = f;
7961 *bar_window = bar->window;
7962
689004fa
GV
7963 if (pfnGetScrollInfo)
7964 {
7965 SCROLLINFO si;
7966
7967 si.cbSize = sizeof (si);
7968 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
7969
7970 pfnGetScrollInfo (w, SB_CTL, &si);
7971 pos = si.nPos;
7972 top_range = si.nMax - si.nPage + 1;
7973 }
7974 else
7975 pos = GetScrollPos (w, SB_CTL);
ee78dc32
GV
7976
7977 switch (LOWORD (last_mouse_scroll_bar_pos))
7978 {
7979 case SB_THUMBPOSITION:
7980 case SB_THUMBTRACK:
7981 *part = scroll_bar_handle;
791f420f 7982 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
ee78dc32
GV
7983 pos = HIWORD (last_mouse_scroll_bar_pos);
7984 break;
7985 case SB_LINEDOWN:
7986 *part = scroll_bar_handle;
7987 pos++;
7988 break;
7989 default:
7990 *part = scroll_bar_handle;
7991 break;
7992 }
7993
9127e20e
JR
7994 XSETINT (*x, pos);
7995 XSETINT (*y, top_range);
ee78dc32
GV
7996
7997 f->mouse_moved = 0;
7998 last_mouse_scroll_bar = Qnil;
7999
8000 *time = last_mouse_movement_time;
8001
8002 UNBLOCK_INPUT;
8003}
8004
9ef2e2cf 8005
ee78dc32
GV
8006/* The screen has been cleared so we may have changed foreground or
8007 background colors, and the scroll bars may need to be redrawn.
8008 Clear out the scroll bars, and ask for expose events, so we can
8009 redraw them. */
9ef2e2cf 8010
791f420f 8011void
ee78dc32 8012x_scroll_bar_clear (f)
9ef2e2cf 8013 FRAME_PTR f;
ee78dc32 8014{
ee78dc32
GV
8015 Lisp_Object bar;
8016
9ef2e2cf
JR
8017 /* We can have scroll bars even if this is 0,
8018 if we just turned off scroll bar mode.
8019 But in that case we should not clear them. */
8020 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8021 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8022 bar = XSCROLL_BAR (bar)->next)
8023 {
8024 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
8025 HDC hdc = GetDC (window);
8026 RECT rect;
52cf03a1 8027
9ef2e2cf
JR
8028 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
8029 arranges to refresh the scroll bar if hidden. */
8030 my_show_window (f, window, SW_HIDE);
689004fa 8031
9ef2e2cf
JR
8032 GetClientRect (window, &rect);
8033 select_palette (f, hdc);
8034 w32_clear_rect (f, hdc, &rect);
8035 deselect_palette (f, hdc);
689004fa 8036
9ef2e2cf
JR
8037 ReleaseDC (window, hdc);
8038 }
52cf03a1
GV
8039}
8040
ee78dc32 8041\f
fbd6baed 8042/* The main W32 event-reading loop - w32_read_socket. */
ee78dc32 8043
9ef2e2cf 8044/* Time stamp of enter window event. This is only used by w32_read_socket,
ee78dc32
GV
8045 but we have to put it out here, since static variables within functions
8046 sometimes don't work. */
9ef2e2cf 8047
ee78dc32
GV
8048static Time enter_timestamp;
8049
8050/* Record the last 100 characters stored
8051 to help debug the loss-of-chars-during-GC problem. */
9ef2e2cf
JR
8052
8053static int temp_index;
8054static short temp_buffer[100];
ee78dc32 8055
afd153f0 8056
fbd6baed 8057/* Read events coming from the W32 shell.
ee78dc32
GV
8058 This routine is called by the SIGIO handler.
8059 We return as soon as there are no more events to be read.
8060
8061 Events representing keys are stored in buffer BUFP,
8062 which can hold up to NUMCHARS characters.
8063 We return the number of characters stored into the buffer,
8064 thus pretending to be `read'.
8065
ee78dc32
GV
8066 EXPECTED is nonzero if the caller knows input is available.
8067
8068 Some of these messages are reposted back to the message queue since the
e9e23e23
GV
8069 system calls the windows proc directly in a context where we cannot return
8070 the data nor can we guarantee the state we are in. So if we dispatch them
ee78dc32
GV
8071 we will get into an infinite loop. To prevent this from ever happening we
8072 will set a variable to indicate we are in the read_socket call and indicate
e9e23e23
GV
8073 which message we are processing since the windows proc gets called
8074 recursively with different messages by the system.
ee78dc32
GV
8075*/
8076
8077int
3aad7613 8078w32_read_socket (sd, bufp, numchars, expected)
ee78dc32 8079 register int sd;
9ef2e2cf
JR
8080 /* register */ struct input_event *bufp;
8081 /* register */ int numchars;
ee78dc32
GV
8082 int expected;
8083{
8084 int count = 0;
689004fa 8085 int check_visibility = 0;
fbd6baed 8086 W32Msg msg;
ee78dc32 8087 struct frame *f;
fbd6baed 8088 struct w32_display_info *dpyinfo = &one_w32_display_info;
ee78dc32
GV
8089
8090 if (interrupt_input_blocked)
8091 {
8092 interrupt_input_pending = 1;
8093 return -1;
8094 }
8095
8096 interrupt_input_pending = 0;
8097 BLOCK_INPUT;
8098
8099 /* So people can tell when we have read the available input. */
8100 input_signal_count++;
8101
8102 if (numchars <= 0)
8103 abort (); /* Don't think this happens. */
8104
01b220b6 8105 /* TODO: tooltips, tool-bars, ghostscript integration, mouse
791f420f 8106 cursors. */
52cf03a1 8107 while (get_next_msg (&msg, FALSE))
ee78dc32
GV
8108 {
8109 switch (msg.msg.message)
8110 {
ee78dc32 8111 case WM_PAINT:
ee78dc32
GV
8112 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8113
8114 if (f)
8115 {
014b6ea1
GV
8116 if (msg.rect.right == msg.rect.left ||
8117 msg.rect.bottom == msg.rect.top)
ee78dc32 8118 {
014b6ea1
GV
8119 /* We may get paint messages even though the client
8120 area is clipped - these are not expose events. */
8f860180 8121 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
014b6ea1
GV
8122 XSTRING (f->name)->data));
8123 }
8124 else if (f->async_visible != 1)
8125 {
8126 /* Definitely not obscured, so mark as visible. */
ee78dc32
GV
8127 f->async_visible = 1;
8128 f->async_iconified = 0;
8129 SET_FRAME_GARBAGED (f);
8f860180 8130 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
014b6ea1 8131 XSTRING (f->name)->data));
689004fa
GV
8132
8133 /* WM_PAINT serves as MapNotify as well, so report
8134 visibility changes properly. */
8135 if (f->iconified)
8136 {
8137 bufp->kind = deiconify_event;
8138 XSETFRAME (bufp->frame_or_window, f);
7e889510 8139 bufp->arg = Qnil;
689004fa
GV
8140 bufp++;
8141 count++;
8142 numchars--;
8143 }
9127e20e 8144 else if (! NILP (Vframe_list)
8e713be6 8145 && ! NILP (XCDR (Vframe_list)))
689004fa
GV
8146 /* Force a redisplay sooner or later to update the
8147 frame titles in case this is the second frame. */
8148 record_asynch_buffer_change ();
ee78dc32
GV
8149 }
8150 else
8151 {
00fe468b
JR
8152 HDC hdc = get_frame_dc (f);
8153
97e6de38 8154 /* Erase background again for safety. */
00fe468b
JR
8155 w32_clear_rect (f, hdc, &msg.rect);
8156 release_frame_dc (f, hdc);
791f420f
JR
8157 expose_frame (f,
8158 msg.rect.left,
8159 msg.rect.top,
8160 msg.rect.right - msg.rect.left,
8161 msg.rect.bottom - msg.rect.top);
ee78dc32
GV
8162 }
8163 }
52cf03a1 8164 break;
689004fa 8165
f98169a0
GV
8166 case WM_INPUTLANGCHANGE:
8167 /* Generate a language change event. */
8168 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8169
8170 if (f)
8171 {
8172 if (numchars == 0)
8173 abort ();
8174
8175 bufp->kind = language_change_event;
8176 XSETFRAME (bufp->frame_or_window, f);
7e889510 8177 bufp->arg = Qnil;
f98169a0
GV
8178 bufp->code = msg.msg.wParam;
8179 bufp->modifiers = msg.msg.lParam & 0xffff;
8180 bufp++;
8181 count++;
8182 numchars--;
8183 }
8184 break;
8185
ee78dc32
GV
8186 case WM_KEYDOWN:
8187 case WM_SYSKEYDOWN:
8188 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8189
8190 if (f && !f->iconified)
8191 {
8192 if (temp_index == sizeof temp_buffer / sizeof (short))
8193 temp_index = 0;
8194 temp_buffer[temp_index++] = msg.msg.wParam;
8195 bufp->kind = non_ascii_keystroke;
8196 bufp->code = msg.msg.wParam;
f98169a0 8197 bufp->modifiers = msg.dwModifiers;
ee78dc32 8198 XSETFRAME (bufp->frame_or_window, f);
7e889510 8199 bufp->arg = Qnil;
ee78dc32
GV
8200 bufp->timestamp = msg.msg.time;
8201 bufp++;
8202 numchars--;
8203 count++;
8204 }
8205 break;
689004fa 8206
ee78dc32
GV
8207 case WM_SYSCHAR:
8208 case WM_CHAR:
8209 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8210
8211 if (f && !f->iconified)
8212 {
f98169a0
GV
8213 if (temp_index == sizeof temp_buffer / sizeof (short))
8214 temp_index = 0;
8215 temp_buffer[temp_index++] = msg.msg.wParam;
8216 bufp->kind = ascii_keystroke;
8217 bufp->code = msg.msg.wParam;
8218 bufp->modifiers = msg.dwModifiers;
8219 XSETFRAME (bufp->frame_or_window, f);
7e889510 8220 bufp->arg = Qnil;
f98169a0
GV
8221 bufp->timestamp = msg.msg.time;
8222 bufp++;
8223 numchars--;
8224 count++;
ee78dc32
GV
8225 }
8226 break;
689004fa 8227
ee78dc32 8228 case WM_MOUSEMOVE:
791f420f 8229 previous_help_echo = help_echo;
158cba56
JR
8230 help_echo = help_echo_object = help_echo_window = Qnil;
8231 help_echo_pos = -1;
791f420f 8232
ee78dc32
GV
8233 if (dpyinfo->grabbed && last_mouse_frame
8234 && FRAME_LIVE_P (last_mouse_frame))
8235 f = last_mouse_frame;
8236 else
8237 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8238
8239 if (f)
8240 note_mouse_movement (f, &msg.msg);
8241 else
791f420f
JR
8242 {
8243 /* If we move outside the frame, then we're
8244 certainly no longer on any text in the frame. */
5b844253 8245 clear_mouse_face (dpyinfo);
791f420f
JR
8246 }
8247
8248 /* If the contents of the global variable help_echo
8249 has changed, generate a HELP_EVENT. */
b7e80413
SM
8250 if (!NILP (help_echo)
8251 || !NILP (previous_help_echo))
791f420f
JR
8252 {
8253 Lisp_Object frame;
ec48c3a7 8254 int n;
791f420f
JR
8255
8256 if (f)
8257 XSETFRAME (frame, f);
8258 else
8259 frame = Qnil;
8260
8261 any_help_event_p = 1;
b4165314
GM
8262 n = gen_help_event (bufp, numchars, help_echo, frame,
8263 help_echo_window, help_echo_object,
8264 help_echo_pos);
ec48c3a7 8265 bufp += n, count += n, numchars -= n;
791f420f
JR
8266 }
8267 break;
689004fa 8268
ee78dc32
GV
8269 case WM_LBUTTONDOWN:
8270 case WM_LBUTTONUP:
8271 case WM_MBUTTONDOWN:
8272 case WM_MBUTTONUP:
8273 case WM_RBUTTONDOWN:
8274 case WM_RBUTTONUP:
8275 {
791f420f
JR
8276 /* If we decide we want to generate an event to be seen
8277 by the rest of Emacs, we put it here. */
8278 struct input_event emacs_event;
8279 int tool_bar_p = 0;
ee78dc32
GV
8280 int button;
8281 int up;
791f420f
JR
8282
8283 emacs_event.kind = no_event;
ee78dc32
GV
8284
8285 if (dpyinfo->grabbed && last_mouse_frame
8286 && FRAME_LIVE_P (last_mouse_frame))
8287 f = last_mouse_frame;
8288 else
8289 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8290
8291 if (f)
8292 {
791f420f
JR
8293 construct_mouse_click (&emacs_event, &msg, f);
8294
8295 /* Is this in the tool-bar? */
8296 if (WINDOWP (f->tool_bar_window)
8297 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
8298 {
8299 Lisp_Object window;
d6ff54d5 8300 int p;
791f420f
JR
8301
8302 /* Set x and y. */
8303 window = window_from_coordinates (f,
8304 emacs_event.x,
8305 emacs_event.y,
8306 &p, 1);
8307 if (EQ (window, f->tool_bar_window))
8308 {
8309 w32_handle_tool_bar_click (f, &emacs_event);
8310 tool_bar_p = 1;
8311 }
8312 }
8313
8314 if (!tool_bar_p)
8315 if (!dpyinfo->w32_focus_frame
8316 || f == dpyinfo->w32_focus_frame
8317 && (numchars >= 1))
8318 {
8319 construct_mouse_click (bufp, &msg, f);
8320 bufp++;
8321 count++;
8322 numchars--;
8323 }
ee78dc32
GV
8324 }
8325
8326 parse_button (msg.msg.message, &button, &up);
8327
8328 if (up)
8329 {
8330 dpyinfo->grabbed &= ~ (1 << button);
8331 }
8332 else
8333 {
8334 dpyinfo->grabbed |= (1 << button);
8335 last_mouse_frame = f;
791f420f
JR
8336 /* Ignore any mouse motion that happened
8337 before this event; any subsequent mouse-movement
8338 Emacs events should reflect only motion after
8339 the ButtonPress. */
8340 if (f != 0)
8341 f->mouse_moved = 0;
8342
8343 if (!tool_bar_p)
8344 last_tool_bar_item = -1;
ee78dc32 8345 }
689004fa 8346 break;
ee78dc32
GV
8347 }
8348
689004fa
GV
8349 case WM_MOUSEWHEEL:
8350 if (dpyinfo->grabbed && last_mouse_frame
8351 && FRAME_LIVE_P (last_mouse_frame))
8352 f = last_mouse_frame;
8353 else
8354 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8355
8356 if (f)
8357 {
8358 if ((!dpyinfo->w32_focus_frame
8359 || f == dpyinfo->w32_focus_frame)
8360 && (numchars >= 1))
8361 {
8362 construct_mouse_wheel (bufp, &msg, f);
8363 bufp++;
8364 count++;
8365 numchars--;
8366 }
8367 }
ee78dc32 8368 break;
689004fa 8369
93ff4395
JR
8370 case WM_MENUSELECT:
8371 {
8372 HMENU menu = (HMENU) msg.msg.lParam;
8373 UINT menu_item = (UINT) LOWORD (msg.msg.wParam);
8374 UINT flags = (UINT) HIWORD (msg.msg.wParam);
9ef2e2cf 8375
93ff4395
JR
8376 w32_menu_display_help (menu, menu_item, flags);
8377 }
8378 break;
8379
12857dfd
RS
8380 case WM_DROPFILES:
8381 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8382
8383 if (f)
8384 {
8385 construct_drag_n_drop (bufp, &msg, f);
8386 bufp++;
8387 count++;
8388 numchars--;
8389 }
8390 break;
8391
ee78dc32
GV
8392 case WM_VSCROLL:
8393 {
689004fa
GV
8394 struct scroll_bar *bar =
8395 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
ee78dc32
GV
8396
8397 if (bar && numchars >= 1)
8398 {
c2cc16fa 8399 if (w32_scroll_bar_handle_click (bar, &msg, bufp))
52cf03a1
GV
8400 {
8401 bufp++;
8402 count++;
8403 numchars--;
8404 }
ee78dc32 8405 }
689004fa 8406 break;
ee78dc32
GV
8407 }
8408
689004fa
GV
8409 case WM_WINDOWPOSCHANGED:
8410 case WM_ACTIVATE:
8411 case WM_ACTIVATEAPP:
8412 check_visibility = 1;
ee78dc32 8413 break;
689004fa 8414
ee78dc32
GV
8415 case WM_MOVE:
8416 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8417
8418 if (f && !f->async_iconified)
8419 {
689004fa
GV
8420 int x, y;
8421
8422 x_real_positions (f, &x, &y);
8423 f->output_data.w32->left_pos = x;
8424 f->output_data.w32->top_pos = y;
ee78dc32 8425 }
689004fa
GV
8426
8427 check_visibility = 1;
8428 break;
8429
8430 case WM_SHOWWINDOW:
8431 /* If window has been obscured or exposed by another window
8432 being maximised or minimised/restored, then recheck
8433 visibility of all frames. Direct changes to our own
8434 windows get handled by WM_SIZE. */
8435#if 0
8436 if (msg.msg.lParam != 0)
8437 check_visibility = 1;
8438 else
8439 {
8440 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8441 f->async_visible = msg.msg.wParam;
8442 }
8443#endif
8444
8445 check_visibility = 1;
ee78dc32 8446 break;
689004fa 8447
ee78dc32
GV
8448 case WM_SIZE:
8449 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8450
689004fa
GV
8451 /* Inform lisp of whether frame has been iconified etc. */
8452 if (f)
ee78dc32 8453 {
689004fa 8454 switch (msg.msg.wParam)
ee78dc32 8455 {
689004fa
GV
8456 case SIZE_MINIMIZED:
8457 f->async_visible = 0;
ee78dc32
GV
8458 f->async_iconified = 1;
8459
8460 bufp->kind = iconify_event;
8461 XSETFRAME (bufp->frame_or_window, f);
7e889510 8462 bufp->arg = Qnil;
ee78dc32
GV
8463 bufp++;
8464 count++;
8465 numchars--;
689004fa
GV
8466 break;
8467
8468 case SIZE_MAXIMIZED:
8469 case SIZE_RESTORED:
ee78dc32
GV
8470 f->async_visible = 1;
8471 f->async_iconified = 0;
8472
8473 /* wait_reading_process_input will notice this and update
8474 the frame's display structures. */
8475 SET_FRAME_GARBAGED (f);
8476
8477 if (f->iconified)
8478 {
31d4844a
KH
8479 int x, y;
8480
8481 /* Reset top and left positions of the Window
8482 here since Windows sends a WM_MOVE message
8483 BEFORE telling us the Window is minimized
8484 when the Window is iconified, with 3000,3000
8485 as the co-ords. */
8486 x_real_positions (f, &x, &y);
8487 f->output_data.w32->left_pos = x;
8488 f->output_data.w32->top_pos = y;
8489
ee78dc32
GV
8490 bufp->kind = deiconify_event;
8491 XSETFRAME (bufp->frame_or_window, f);
7e889510 8492 bufp->arg = Qnil;
ee78dc32
GV
8493 bufp++;
8494 count++;
8495 numchars--;
8496 }
9ef2e2cf
JR
8497 else if (! NILP (Vframe_list)
8498 && ! NILP (XCDR (Vframe_list)))
ee78dc32
GV
8499 /* Force a redisplay sooner or later
8500 to update the frame titles
8501 in case this is the second frame. */
8502 record_asynch_buffer_change ();
689004fa 8503 break;
ee78dc32 8504 }
ee78dc32 8505 }
689004fa 8506
ef0e360f
GV
8507 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
8508 {
8509 RECT rect;
8510 int rows;
8511 int columns;
8512 int width;
8513 int height;
8514
9127e20e 8515 GetClientRect (msg.msg.hwnd, &rect);
ef0e360f
GV
8516
8517 height = rect.bottom - rect.top;
8518 width = rect.right - rect.left;
8519
8520 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
8521 columns = PIXEL_TO_CHAR_WIDTH (f, width);
8522
8523 /* TODO: Clip size to the screen dimensions. */
8524
8525 /* Even if the number of character rows and columns has
8526 not changed, the font size may have changed, so we need
8527 to check the pixel dimensions as well. */
8528
8529 if (columns != f->width
8530 || rows != f->height
8531 || width != f->output_data.w32->pixel_width
8532 || height != f->output_data.w32->pixel_height)
8533 {
791f420f 8534 change_frame_size (f, rows, columns, 0, 1, 0);
ef0e360f 8535 SET_FRAME_GARBAGED (f);
31d4844a 8536 cancel_mouse_face (f);
ef0e360f
GV
8537 f->output_data.w32->pixel_width = width;
8538 f->output_data.w32->pixel_height = height;
8539 f->output_data.w32->win_gravity = NorthWestGravity;
8540 }
8541 }
8542
689004fa
GV
8543 check_visibility = 1;
8544 break;
8545
8546 case WM_SETFOCUS:
791f420f
JR
8547 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
8548
8549 dpyinfo->w32_focus_event_frame = f;
8550
8551 if (f)
8552 x_new_focus_frame (dpyinfo, f);
8553
8554
8555 dpyinfo->grabbed = 0;
8556 check_visibility = 1;
8557 break;
8558
689004fa 8559 case WM_KILLFOCUS:
01b220b6 8560 /* TODO: some of this belongs in MOUSE_LEAVE */
791f420f 8561 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
08712a41 8562
791f420f
JR
8563 if (f)
8564 {
791f420f
JR
8565 if (f == dpyinfo->w32_focus_event_frame)
8566 dpyinfo->w32_focus_event_frame = 0;
8567
8568 if (f == dpyinfo->w32_focus_frame)
8569 x_new_focus_frame (dpyinfo, 0);
8570
8571 if (f == dpyinfo->mouse_face_mouse_frame)
8572 {
8573 /* If we move outside the frame, then we're
8574 certainly no longer on any text in the frame. */
8575 clear_mouse_face (dpyinfo);
8576 dpyinfo->mouse_face_mouse_frame = 0;
8577 }
9ef2e2cf 8578
791f420f
JR
8579 /* Generate a nil HELP_EVENT to cancel a help-echo.
8580 Do it only if there's something to cancel.
8581 Otherwise, the startup message is cleared when
8582 the mouse leaves the frame. */
8583 if (any_help_event_p)
8584 {
c2cc16fa 8585 Lisp_Object frame;
ec48c3a7
JR
8586 int n;
8587
791f420f 8588 XSETFRAME (frame, f);
c2cc16fa
JR
8589 help_echo = Qnil;
8590 n = gen_help_event (bufp, numchars,
8591 Qnil, frame, Qnil, Qnil, 0);
ec48c3a7 8592 bufp += n, count += n, numchars -=n;
791f420f
JR
8593 }
8594 }
689004fa 8595
08712a41 8596 dpyinfo->grabbed = 0;
689004fa 8597 check_visibility = 1;
ee78dc32 8598 break;
689004fa 8599
ee78dc32
GV
8600 case WM_CLOSE:
8601 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8602
8603 if (f)
8604 {
8605 if (numchars == 0)
8606 abort ();
8607
8608 bufp->kind = delete_window_event;
8609 XSETFRAME (bufp->frame_or_window, f);
7e889510 8610 bufp->arg = Qnil;
ee78dc32
GV
8611 bufp++;
8612 count++;
8613 numchars--;
8614 }
689004fa
GV
8615 break;
8616
8617 case WM_INITMENU:
8618 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8619
8620 if (f)
8621 {
8622 if (numchars == 0)
8623 abort ();
ee78dc32 8624
689004fa
GV
8625 bufp->kind = menu_bar_activate_event;
8626 XSETFRAME (bufp->frame_or_window, f);
7e889510 8627 bufp->arg = Qnil;
689004fa
GV
8628 bufp++;
8629 count++;
8630 numchars--;
8631 }
ee78dc32 8632 break;
689004fa 8633
ee78dc32
GV
8634 case WM_COMMAND:
8635 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
014b6ea1 8636
ee78dc32
GV
8637 if (f)
8638 {
f98169a0
GV
8639 extern void menubar_selection_callback
8640 (FRAME_PTR f, void * client_data);
014b6ea1 8641 menubar_selection_callback (f, (void *)msg.msg.wParam);
ee78dc32 8642 }
689004fa
GV
8643
8644 check_visibility = 1;
8645 break;
8646
8647 case WM_DISPLAYCHANGE:
8648 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8649
8650 if (f)
8651 {
8652 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
8653 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
8654 dpyinfo->n_cbits = msg.msg.wParam;
8655 DebPrint (("display change: %d %d\n", dpyinfo->width,
8656 dpyinfo->height));
8657 }
8658
8659 check_visibility = 1;
ee78dc32 8660 break;
e7efd97e
GV
8661
8662 default:
8663 /* Check for messages registered at runtime. */
8664 if (msg.msg.message == msh_mousewheel)
8665 {
8666 if (dpyinfo->grabbed && last_mouse_frame
8667 && FRAME_LIVE_P (last_mouse_frame))
8668 f = last_mouse_frame;
8669 else
8670 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8671
8672 if (f)
8673 {
8674 if ((!dpyinfo->w32_focus_frame
8675 || f == dpyinfo->w32_focus_frame)
8676 && (numchars >= 1))
8677 {
8678 construct_mouse_wheel (bufp, &msg, f);
8679 bufp++;
8680 count++;
8681 numchars--;
8682 }
8683 }
8684 }
8685 break;
ee78dc32
GV
8686 }
8687 }
8688
8689 /* If the focus was just given to an autoraising frame,
8690 raise it now. */
8691 /* ??? This ought to be able to handle more than one such frame. */
8692 if (pending_autoraise_frame)
8693 {
8694 x_raise_frame (pending_autoraise_frame);
8695 pending_autoraise_frame = 0;
8696 }
8697
689004fa
GV
8698 /* Check which frames are still visisble, if we have enqueued any user
8699 events or been notified of events that may affect visibility. We
8700 do this here because there doesn't seem to be any direct
8701 notification from Windows that the visibility of a window has
8702 changed (at least, not in all cases). */
8703 if (count > 0 || check_visibility)
8704 {
8705 Lisp_Object tail, frame;
8706
8707 FOR_EACH_FRAME (tail, frame)
8708 {
8709 FRAME_PTR f = XFRAME (frame);
8710 /* Check "visible" frames and mark each as obscured or not.
8711 Note that async_visible is nonzero for unobscured and
8712 obscured frames, but zero for hidden and iconified frames. */
8713 if (FRAME_W32_P (f) && f->async_visible)
8714 {
8715 RECT clipbox;
8716 HDC hdc = get_frame_dc (f);
8717 GetClipBox (hdc, &clipbox);
8718 release_frame_dc (f, hdc);
8719
8720 if (clipbox.right == clipbox.left
8721 || clipbox.bottom == clipbox.top)
8722 {
8723 /* Frame has become completely obscured so mark as
8724 such (we do this by setting async_visible to 2 so
8725 that FRAME_VISIBLE_P is still true, but redisplay
8726 will skip it). */
8727 f->async_visible = 2;
8728
8729 if (!FRAME_OBSCURED_P (f))
8730 {
8f860180 8731 DebPrint (("frame %p (%s) obscured\n", f,
689004fa
GV
8732 XSTRING (f->name)->data));
8733 }
8734 }
8735 else
8736 {
8737 /* Frame is not obscured, so mark it as such. */
8738 f->async_visible = 1;
8739
8740 if (FRAME_OBSCURED_P (f))
8741 {
8742 SET_FRAME_GARBAGED (f);
8f860180 8743 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
689004fa
GV
8744 XSTRING (f->name)->data));
8745
8746 /* Force a redisplay sooner or later. */
8747 record_asynch_buffer_change ();
8748 }
8749 }
8750 }
8751 }
8752 }
8753
ee78dc32
GV
8754 UNBLOCK_INPUT;
8755 return count;
8756}
791f420f 8757
9ef2e2cf
JR
8758
8759
ee78dc32 8760\f
791f420f
JR
8761/***********************************************************************
8762 Text Cursor
8763 ***********************************************************************/
8764
8765/* Note if the text cursor of window W has been overwritten by a
8766 drawing operation that outputs N glyphs starting at HPOS in the
8767 line given by output_cursor.vpos. N < 0 means all the rest of the
8768 line after HPOS has been written. */
8769
8770static void
8771note_overwritten_text_cursor (w, hpos, n)
8772 struct window *w;
8773 int hpos, n;
8774{
8775 if (updated_area == TEXT_AREA
8776 && output_cursor.vpos == w->phys_cursor.vpos
8777 && hpos <= w->phys_cursor.hpos
8778 && (n < 0
8779 || hpos + n > w->phys_cursor.hpos))
8780 w->phys_cursor_on_p = 0;
8781}
8782
ee78dc32 8783
791f420f
JR
8784/* Set clipping for output in glyph row ROW. W is the window in which
8785 we operate. GC is the graphics context to set clipping in.
8786 WHOLE_LINE_P non-zero means include the areas used for truncation
8787 mark display and alike in the clipping rectangle.
ee78dc32 8788
791f420f
JR
8789 ROW may be a text row or, e.g., a mode line. Text rows must be
8790 clipped to the interior of the window dedicated to text display,
8791 mode lines must be clipped to the whole window. */
ee78dc32
GV
8792
8793static void
791f420f
JR
8794w32_clip_to_row (w, row, hdc, whole_line_p)
8795 struct window *w;
8796 struct glyph_row *row;
8797 HDC hdc;
8798 int whole_line_p;
ee78dc32 8799{
791f420f
JR
8800 struct frame *f = XFRAME (WINDOW_FRAME (w));
8801 RECT clip_rect;
8802 int window_x, window_y, window_width, window_height;
52cf03a1 8803
791f420f 8804 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
52cf03a1 8805
791f420f
JR
8806 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
8807 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8808 clip_rect.top = max (clip_rect.top, window_y);
8809 clip_rect.right = clip_rect.left + window_width;
8810 clip_rect.bottom = clip_rect.top + row->visible_height;
8811
8812 /* If clipping to the whole line, including trunc marks, extend
9ef2e2cf 8813 the rectangle to the left and increase its width. */
791f420f
JR
8814 if (whole_line_p)
8815 {
8816 clip_rect.left -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
8817 clip_rect.right += FRAME_X_FLAGS_AREA_WIDTH (f);
8818 }
8819
8820 w32_set_clip_rectangle (hdc, &clip_rect);
ee78dc32
GV
8821}
8822
791f420f
JR
8823
8824/* Draw a hollow box cursor on window W in glyph row ROW. */
ee78dc32
GV
8825
8826static void
791f420f
JR
8827x_draw_hollow_cursor (w, row)
8828 struct window *w;
8829 struct glyph_row *row;
ee78dc32 8830{
791f420f 8831 struct frame *f = XFRAME (WINDOW_FRAME (w));
988646fc 8832 HDC hdc;
791f420f
JR
8833 RECT rect;
8834 int wd;
8835 struct glyph *cursor_glyph;
8836 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
8837
8838 /* Compute frame-relative coordinates from window-relative
8839 coordinates. */
8840 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8841 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
8842 + row->ascent - w->phys_cursor_ascent);
8843 rect.bottom = rect.top + row->height - 1;
8844
8845 /* Get the glyph the cursor is on. If we can't tell because
8846 the current matrix is invalid or such, give up. */
8847 cursor_glyph = get_phys_cursor_glyph (w);
8848 if (cursor_glyph == NULL)
ee78dc32
GV
8849 return;
8850
791f420f
JR
8851 /* Compute the width of the rectangle to draw. If on a stretch
8852 glyph, and `x-stretch-block-cursor' is nil, don't draw a
8853 rectangle as wide as the glyph, but use a canonical character
8854 width instead. */
8855 wd = cursor_glyph->pixel_width - 1;
8856 if (cursor_glyph->type == STRETCH_GLYPH
8857 && !x_stretch_cursor_p)
8858 wd = min (CANON_X_UNIT (f), wd);
8859
8860 rect.right = rect.left + wd;
988646fc 8861 hdc = get_frame_dc (f);
791f420f
JR
8862 FrameRect (hdc, &rect, hb);
8863 DeleteObject (hb);
8864
8865 release_frame_dc (f, hdc);
ee78dc32
GV
8866}
8867
791f420f
JR
8868
8869/* Draw a bar cursor on window W in glyph row ROW.
8870
8871 Implementation note: One would like to draw a bar cursor with an
8872 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8873 Unfortunately, I didn't find a font yet that has this property set.
8874 --gerd. */
ee78dc32
GV
8875
8876static void
9ef2e2cf 8877x_draw_bar_cursor (w, row, width)
791f420f
JR
8878 struct window *w;
8879 struct glyph_row *row;
9ef2e2cf 8880 int width;
ee78dc32 8881{
c2cc16fa
JR
8882 struct frame *f = XFRAME (w->frame);
8883 struct glyph *cursor_glyph;
8884 int x;
8885 HDC hdc;
791f420f 8886
c2cc16fa
JR
8887 /* If cursor is out of bounds, don't draw garbage. This can happen
8888 in mini-buffer windows when switching between echo area glyphs
8889 and mini-buffer. */
8890 cursor_glyph = get_phys_cursor_glyph (w);
8891 if (cursor_glyph == NULL)
8892 return;
9ef2e2cf 8893
c2cc16fa
JR
8894 /* If on an image, draw like a normal cursor. That's usually better
8895 visible than drawing a bar, esp. if the image is large so that
8896 the bar might not be in the window. */
8897 if (cursor_glyph->type == IMAGE_GLYPH)
8898 {
8899 struct glyph_row *row;
8900 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8901 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
8902 }
8903 else
8904 {
8905 if (width < 0)
8906 width = f->output_data.w32->cursor_width;
158cba56 8907
c2cc16fa
JR
8908 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8909 hdc = get_frame_dc (f);
8910 w32_clip_to_row (w, row, hdc, 0);
8911 w32_fill_area (f, hdc, f->output_data.w32->cursor_pixel,
8912 x,
8913 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8914 min (cursor_glyph->pixel_width, width),
8915 row->height);
8916 release_frame_dc (f, hdc);
791f420f 8917 }
ee78dc32
GV
8918}
8919
791f420f
JR
8920
8921/* Clear the cursor of window W to background color, and mark the
8922 cursor as not shown. This is used when the text where the cursor
8923 is is about to be rewritten. */
8924
ee78dc32 8925static void
791f420f
JR
8926x_clear_cursor (w)
8927 struct window *w;
ee78dc32 8928{
791f420f
JR
8929 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
8930 x_update_window_cursor (w, 0);
8931}
ee78dc32 8932
ee78dc32 8933
791f420f
JR
8934/* Draw the cursor glyph of window W in glyph row ROW. See the
8935 comment of x_draw_glyphs for the meaning of HL. */
ee78dc32 8936
791f420f
JR
8937static void
8938x_draw_phys_cursor_glyph (w, row, hl)
8939 struct window *w;
8940 struct glyph_row *row;
8941 enum draw_glyphs_face hl;
8942{
8943 /* If cursor hpos is out of bounds, don't draw garbage. This can
8944 happen in mini-buffer windows when switching between echo area
8945 glyphs and mini-buffer. */
8946 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
ee78dc32 8947 {
791f420f
JR
8948 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
8949 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
8950 hl, 0, 0, 0);
8951
8952 /* When we erase the cursor, and ROW is overlapped by other
8953 rows, make sure that these overlapping parts of other rows
8954 are redrawn. */
8955 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
8956 {
8957 if (row > w->current_matrix->rows
8958 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
8959 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
8960
8961 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
8962 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
8963 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
8964 }
ee78dc32 8965 }
791f420f 8966}
ee78dc32 8967
791f420f
JR
8968
8969/* Erase the image of a cursor of window W from the screen. */
8970
8971static void
8972x_erase_phys_cursor (w)
8973 struct window *w;
8974{
8975 struct frame *f = XFRAME (w->frame);
8976 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8977 int hpos = w->phys_cursor.hpos;
8978 int vpos = w->phys_cursor.vpos;
8979 int mouse_face_here_p = 0;
8980 struct glyph_matrix *active_glyphs = w->current_matrix;
8981 struct glyph_row *cursor_row;
8982 struct glyph *cursor_glyph;
8983 enum draw_glyphs_face hl;
8984
8985 /* No cursor displayed or row invalidated => nothing to do on the
8986 screen. */
8987 if (w->phys_cursor_type == NO_CURSOR)
8988 goto mark_cursor_off;
8989
8990 /* VPOS >= active_glyphs->nrows means that window has been resized.
8991 Don't bother to erase the cursor. */
8992 if (vpos >= active_glyphs->nrows)
8993 goto mark_cursor_off;
8994
8995 /* If row containing cursor is marked invalid, there is nothing we
8996 can do. */
8997 cursor_row = MATRIX_ROW (active_glyphs, vpos);
8998 if (!cursor_row->enabled_p)
8999 goto mark_cursor_off;
9000
9001 /* This can happen when the new row is shorter than the old one.
9002 In this case, either x_draw_glyphs or clear_end_of_line
9003 should have cleared the cursor. Note that we wouldn't be
9004 able to erase the cursor in this case because we don't have a
9005 cursor glyph at hand. */
9006 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9007 goto mark_cursor_off;
9008
9009 /* If the cursor is in the mouse face area, redisplay that when
9010 we clear the cursor. */
c2cc16fa
JR
9011 if (! NILP (dpyinfo->mouse_face_window)
9012 && w == XWINDOW (dpyinfo->mouse_face_window)
791f420f
JR
9013 && (vpos > dpyinfo->mouse_face_beg_row
9014 || (vpos == dpyinfo->mouse_face_beg_row
9015 && hpos >= dpyinfo->mouse_face_beg_col))
9016 && (vpos < dpyinfo->mouse_face_end_row
9017 || (vpos == dpyinfo->mouse_face_end_row
9018 && hpos < dpyinfo->mouse_face_end_col))
9019 /* Don't redraw the cursor's spot in mouse face if it is at the
9020 end of a line (on a newline). The cursor appears there, but
9021 mouse highlighting does not. */
9022 && cursor_row->used[TEXT_AREA] > hpos)
9023 mouse_face_here_p = 1;
9024
9025 /* Maybe clear the display under the cursor. */
9026 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
ee78dc32 9027 {
791f420f
JR
9028 int x;
9029 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
00fe468b 9030 HDC hdc;
ee78dc32 9031
791f420f
JR
9032 cursor_glyph = get_phys_cursor_glyph (w);
9033 if (cursor_glyph == NULL)
9034 goto mark_cursor_off;
ee78dc32 9035
791f420f 9036 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
00fe468b
JR
9037
9038 hdc = get_frame_dc (f);
9039 w32_clear_area (f, hdc, x,
791f420f
JR
9040 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9041 cursor_row->y)),
9042 cursor_glyph->pixel_width,
9043 cursor_row->visible_height);
00fe468b 9044 release_frame_dc (f, hdc);
ee78dc32 9045 }
791f420f
JR
9046
9047 /* Erase the cursor by redrawing the character underneath it. */
9048 if (mouse_face_here_p)
9049 hl = DRAW_MOUSE_FACE;
9050 else if (cursor_row->inverse_p)
9051 hl = DRAW_INVERSE_VIDEO;
9052 else
9053 hl = DRAW_NORMAL_TEXT;
9054 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9055
9056 mark_cursor_off:
9057 w->phys_cursor_on_p = 0;
9058 w->phys_cursor_type = NO_CURSOR;
ee78dc32
GV
9059}
9060
9061
791f420f
JR
9062/* Display or clear cursor of window W. If ON is zero, clear the
9063 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9064 where to put the cursor is specified by HPOS, VPOS, X and Y. */
ee78dc32 9065
791f420f
JR
9066void
9067x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9068 struct window *w;
9069 int on, hpos, vpos, x, y;
ee78dc32 9070{
791f420f
JR
9071 struct frame *f = XFRAME (w->frame);
9072 int new_cursor_type;
9ef2e2cf 9073 int new_cursor_width;
791f420f
JR
9074 struct glyph_matrix *current_glyphs;
9075 struct glyph_row *glyph_row;
9076 struct glyph *glyph;
ee78dc32
GV
9077
9078 /* This is pointless on invisible frames, and dangerous on garbaged
791f420f
JR
9079 windows and frames; in the latter case, the frame or window may
9080 be in the midst of changing its size, and x and y may be off the
9081 window. */
9082 if (! FRAME_VISIBLE_P (f)
9083 || FRAME_GARBAGED_P (f)
9084 || vpos >= w->current_matrix->nrows
9085 || hpos >= w->current_matrix->matrix_w)
ee78dc32
GV
9086 return;
9087
9088 /* If cursor is off and we want it off, return quickly. */
791f420f 9089 if (!on && !w->phys_cursor_on_p)
ee78dc32
GV
9090 return;
9091
791f420f
JR
9092 current_glyphs = w->current_matrix;
9093 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9094 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9095
9096 /* If cursor row is not enabled, we don't really know where to
9097 display the cursor. */
9098 if (!glyph_row->enabled_p)
9099 {
9100 w->phys_cursor_on_p = 0;
9101 return;
9102 }
9103
9104 xassert (interrupt_input_blocked);
9105
9106 /* Set new_cursor_type to the cursor we want to be displayed. In a
9107 mini-buffer window, we want the cursor only to appear if we are
9108 reading input from this window. For the selected window, we want
9109 the cursor type given by the frame parameter. If explicitly
9110 marked off, draw no cursor. In all other cases, we want a hollow
9111 box cursor. */
9ef2e2cf 9112 new_cursor_width = -1;
791f420f
JR
9113 if (cursor_in_echo_area
9114 && FRAME_HAS_MINIBUF_P (f)
9115 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
9116 {
9117 if (w == XWINDOW (echo_area_window))
9118 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9119 else
9120 new_cursor_type = HOLLOW_BOX_CURSOR;
9121 }
9122 else
9123 {
49be9f70
JR
9124 if (f != FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame
9125 || w != XWINDOW (f->selected_window))
791f420f 9126 {
a2bc11d4
JR
9127 extern int cursor_in_non_selected_windows;
9128
9127e20e
JR
9129 if (MINI_WINDOW_P (w)
9130 || !cursor_in_non_selected_windows
9131 || NILP (XBUFFER (w->buffer)->cursor_type))
791f420f
JR
9132 new_cursor_type = NO_CURSOR;
9133 else
9134 new_cursor_type = HOLLOW_BOX_CURSOR;
9135 }
9136 else if (w->cursor_off_p)
9137 new_cursor_type = NO_CURSOR;
9138 else
9ef2e2cf
JR
9139 {
9140 struct buffer *b = XBUFFER (w->buffer);
9141
9142 if (EQ (b->cursor_type, Qt))
9143 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9144 else
9145 new_cursor_type = x_specified_cursor_type (b->cursor_type,
9146 &new_cursor_width);
9147 }
791f420f
JR
9148 }
9149
9150 /* If cursor is currently being shown and we don't want it to be or
9151 it is in the wrong place, or the cursor type is not what we want,
ee78dc32 9152 erase it. */
791f420f 9153 if (w->phys_cursor_on_p
ee78dc32 9154 && (!on
791f420f
JR
9155 || w->phys_cursor.x != x
9156 || w->phys_cursor.y != y
9157 || new_cursor_type != w->phys_cursor_type))
9158 x_erase_phys_cursor (w);
9159
9160 /* If the cursor is now invisible and we want it to be visible,
9161 display it. */
9162 if (on && !w->phys_cursor_on_p)
9163 {
9164 w->phys_cursor_ascent = glyph_row->ascent;
9165 w->phys_cursor_height = glyph_row->height;
9166
9167 /* Set phys_cursor_.* before x_draw_.* is called because some
9168 of them may need the information. */
9169 w->phys_cursor.x = x;
9170 w->phys_cursor.y = glyph_row->y;
9171 w->phys_cursor.hpos = hpos;
9172 w->phys_cursor.vpos = vpos;
9173 w->phys_cursor_type = new_cursor_type;
9174 w->phys_cursor_on_p = 1;
9175
9176 switch (new_cursor_type)
ee78dc32 9177 {
791f420f
JR
9178 case HOLLOW_BOX_CURSOR:
9179 x_draw_hollow_cursor (w, glyph_row);
9180 break;
9181
9182 case FILLED_BOX_CURSOR:
9183 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9184 break;
9185
9186 case BAR_CURSOR:
9ef2e2cf 9187 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
791f420f 9188 break;
ee78dc32 9189
791f420f
JR
9190 case NO_CURSOR:
9191 break;
9192
9193 default:
9194 abort ();
9195 }
ee78dc32
GV
9196 }
9197}
9198
689004fa 9199
791f420f
JR
9200/* Display the cursor on window W, or clear it. X and Y are window
9201 relative pixel coordinates. HPOS and VPOS are glyph matrix
9202 positions. If W is not the selected window, display a hollow
9203 cursor. ON non-zero means display the cursor at X, Y which
9204 correspond to HPOS, VPOS, otherwise it is cleared. */
9205
9206void
9207x_display_cursor (w, on, hpos, vpos, x, y)
9208 struct window *w;
9209 int on, hpos, vpos, x, y;
ee78dc32
GV
9210{
9211 BLOCK_INPUT;
791f420f
JR
9212 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9213 UNBLOCK_INPUT;
9214}
9215
9216
9217/* Display the cursor on window W, or clear it, according to ON_P.
9218 Don't change the cursor's position. */
9219
9220void
9221x_update_cursor (f, on_p)
9222 struct frame *f;
9223 int on_p;
9224{
9225 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9226}
9227
9228
9229/* Call x_update_window_cursor with parameter ON_P on all leaf windows
9230 in the window tree rooted at W. */
ee78dc32 9231
791f420f
JR
9232static void
9233x_update_cursor_in_window_tree (w, on_p)
9234 struct window *w;
9235 int on_p;
9236{
9237 while (w)
689004fa 9238 {
791f420f
JR
9239 if (!NILP (w->hchild))
9240 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9241 else if (!NILP (w->vchild))
9242 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9243 else
9244 x_update_window_cursor (w, on_p);
9245
9246 w = NILP (w->next) ? 0 : XWINDOW (w->next);
689004fa 9247 }
791f420f 9248}
689004fa 9249
ee78dc32 9250
791f420f
JR
9251/* Switch the display of W's cursor on or off, according to the value
9252 of ON. */
9253
9254static void
9255x_update_window_cursor (w, on)
9256 struct window *w;
9257 int on;
9258{
9ef2e2cf
JR
9259 /* Don't update cursor in windows whose frame is in the process
9260 of being deleted. */
9261 if (w->current_matrix)
9262 {
9263 BLOCK_INPUT;
9264 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9265 w->phys_cursor.vpos, w->phys_cursor.x,
9266 w->phys_cursor.y);
9267 UNBLOCK_INPUT;
9268 }
ee78dc32 9269}
791f420f
JR
9270
9271
9ef2e2cf 9272
ee78dc32 9273\f
7f5d1df8
GV
9274/* Icons. */
9275
9276int
9277x_bitmap_icon (f, icon)
9278 struct frame *f;
9279 Lisp_Object icon;
9280{
7f5d1df8
GV
9281 HANDLE hicon;
9282
9283 if (FRAME_W32_WINDOW (f) == 0)
9284 return 1;
9285
9286 if (NILP (icon))
9287 hicon = LoadIcon (hinst, EMACS_CLASS);
9288 else if (STRINGP (icon))
9289 hicon = LoadImage (NULL, (LPCTSTR) XSTRING (icon)->data, IMAGE_ICON, 0, 0,
9290 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9291 else if (SYMBOLP (icon))
9292 {
9293 LPCTSTR name;
9294
9295 if (EQ (icon, intern ("application")))
9296 name = (LPCTSTR) IDI_APPLICATION;
9297 else if (EQ (icon, intern ("hand")))
9298 name = (LPCTSTR) IDI_HAND;
9299 else if (EQ (icon, intern ("question")))
9300 name = (LPCTSTR) IDI_QUESTION;
9301 else if (EQ (icon, intern ("exclamation")))
9302 name = (LPCTSTR) IDI_EXCLAMATION;
9303 else if (EQ (icon, intern ("asterisk")))
9304 name = (LPCTSTR) IDI_ASTERISK;
9305 else if (EQ (icon, intern ("winlogo")))
9306 name = (LPCTSTR) IDI_WINLOGO;
9307 else
9308 return 1;
9309
9310 hicon = LoadIcon (NULL, name);
9311 }
9312 else
9313 return 1;
9314
9315 if (hicon == NULL)
9316 return 1;
9317
791f420f
JR
9318 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
9319 (LPARAM) hicon);
7f5d1df8
GV
9320
9321 return 0;
9322}
9323
9324\f
c2cc16fa
JR
9325/************************************************************************
9326 Handling X errors
9327 ************************************************************************/
9328
9329/* Display Error Handling functions not used on W32. Listing them here
9330 helps diff stay in step when comparing w32term.c with xterm.c.
9331
9332x_error_catcher (display, error)
9333x_catch_errors (dpy)
9334x_catch_errors_unwind (old_val)
9335x_check_errors (dpy, format)
9336x_had_errors_p (dpy)
9337x_clear_errors (dpy)
9338x_uncatch_errors (dpy, count)
9339x_trace_wire ()
9340x_connection_signal (signalnum)
9341x_connection_closed (dpy, error_message)
9342x_error_quitter (display, error)
9343x_error_handler (display, error)
9344x_io_error_quitter (display)
9345
9346 */
9347
9348\f
ee78dc32
GV
9349/* Changing the font of the frame. */
9350
9351/* Give frame F the font named FONTNAME as its default font, and
9352 return the full name of that font. FONTNAME may be a wildcard
9353 pattern; in that case, we choose some font that fits the pattern.
9354 The return value shows which font we chose. */
9355
9356Lisp_Object
9357x_new_font (f, fontname)
9358 struct frame *f;
9359 register char *fontname;
9360{
cabb23bc 9361 struct font_info *fontp
93ff4395 9362 = FS_LOAD_FONT (f, 0, fontname, -1);
ee78dc32 9363
cabb23bc 9364 if (!fontp)
791f420f 9365 return Qnil;
ee78dc32 9366
cabb23bc 9367 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
791f420f 9368 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
cabb23bc 9369 FRAME_FONTSET (f) = -1;
ee78dc32
GV
9370
9371 /* Compute the scroll bar width in character columns. */
9372 if (f->scroll_bar_pixel_width > 0)
9373 {
ec48c3a7 9374 int wid = FONT_WIDTH (FRAME_FONT (f));
ee78dc32
GV
9375 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
9376 }
9377 else
ec48c3a7
JR
9378 {
9379 int wid = FONT_WIDTH (FRAME_FONT (f));
9380 f->scroll_bar_cols = (14 + wid - 1) / wid;
9381 }
ee78dc32
GV
9382
9383 /* Now make the frame display the given font. */
fbd6baed 9384 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
9385 {
9386 frame_update_line_height (f);
49be9f70
JR
9387 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9388 x_set_window_size (f, 0, f->width, f->height);
ee78dc32
GV
9389 }
9390 else
9391 /* If we are setting a new frame's font for the first time,
9392 there are no faces yet, so this font's height is the line height. */
ec48c3a7 9393 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
ee78dc32 9394
791f420f 9395 return build_string (fontp->full_name);
ee78dc32
GV
9396}
9397\f
cabb23bc
GV
9398/* Give frame F the fontset named FONTSETNAME as its default font, and
9399 return the full name of that fontset. FONTSETNAME may be a wildcard
9400 pattern; in that case, we choose some fontset that fits the pattern.
9401 The return value shows which fontset we chose. */
9402
9403Lisp_Object
9404x_new_fontset (f, fontsetname)
9405 struct frame *f;
9406 char *fontsetname;
9407{
93ff4395 9408 int fontset = fs_query_fontset (build_string (fontsetname), 0);
cabb23bc
GV
9409 Lisp_Object result;
9410
9411 if (fontset < 0)
9412 return Qnil;
9413
9414 if (FRAME_FONTSET (f) == fontset)
9415 /* This fontset is already set in frame F. There's nothing more
9416 to do. */
93ff4395 9417 return fontset_name (fontset);
cabb23bc 9418
93ff4395 9419 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
cabb23bc
GV
9420
9421 if (!STRINGP (result))
9422 /* Can't load ASCII font. */
9423 return Qnil;
9424
9425 /* Since x_new_font doesn't update any fontset information, do it now. */
9426 FRAME_FONTSET(f) = fontset;
cabb23bc
GV
9427
9428 return build_string (fontsetname);
9429}
791f420f 9430
c2cc16fa
JR
9431\f
9432/***********************************************************************
9433 TODO: W32 Input Methods
9434 ***********************************************************************/
9435/* Listing missing functions from xterm.c helps diff stay in step.
791f420f 9436
c2cc16fa
JR
9437xim_destroy_callback (xim, client_data, call_data)
9438xim_open_dpy (dpyinfo, resource_name)
9439struct xim_inst_t
9440xim_instantiate_callback (display, client_data, call_data)
9441xim_initialize (dpyinfo, resource_name)
9442xim_close_dpy (dpyinfo)
791f420f 9443
c2cc16fa 9444 */
791f420f 9445
cabb23bc 9446\f
689004fa
GV
9447/* Calculate the absolute position in frame F
9448 from its current recorded position values and gravity. */
9449
9ef2e2cf 9450void
ee78dc32
GV
9451x_calc_absolute_position (f)
9452 struct frame *f;
9453{
ee78dc32 9454 POINT pt;
fbd6baed 9455 int flags = f->output_data.w32->size_hint_flags;
ee78dc32
GV
9456
9457 pt.x = pt.y = 0;
9458
9459 /* Find the position of the outside upper-left corner of
c2cc16fa
JR
9460 the inner window, with respect to the outer window.
9461 But do this only if we will need the results. */
fbd6baed 9462 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
ee78dc32
GV
9463 {
9464 BLOCK_INPUT;
fbd6baed
GV
9465 MapWindowPoints (FRAME_W32_WINDOW (f),
9466 f->output_data.w32->parent_desc,
ee78dc32
GV
9467 &pt, 1);
9468 UNBLOCK_INPUT;
9469 }
9470
9471 {
9472 RECT rt;
9473 rt.left = rt.right = rt.top = rt.bottom = 0;
9474
9475 BLOCK_INPUT;
fbd6baed 9476 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
97c23857 9477 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32
GV
9478 UNBLOCK_INPUT;
9479
9480 pt.x += (rt.right - rt.left);
9481 pt.y += (rt.bottom - rt.top);
9482 }
9483
9484 /* Treat negative positions as relative to the leftmost bottommost
9485 position that fits on the screen. */
9486 if (flags & XNegative)
fbd6baed
GV
9487 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
9488 - 2 * f->output_data.w32->border_width - pt.x
ee78dc32 9489 - PIXEL_WIDTH (f)
fbd6baed 9490 + f->output_data.w32->left_pos);
158cba56 9491
ee78dc32 9492 if (flags & YNegative)
fbd6baed
GV
9493 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
9494 - 2 * f->output_data.w32->border_width - pt.y
ee78dc32 9495 - PIXEL_HEIGHT (f)
fbd6baed 9496 + f->output_data.w32->top_pos);
ee78dc32
GV
9497 /* The left_pos and top_pos
9498 are now relative to the top and left screen edges,
9499 so the flags should correspond. */
fbd6baed 9500 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32
GV
9501}
9502
9503/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9504 to really change the position, and 0 when calling from
9505 x_make_frame_visible (in that case, XOFF and YOFF are the current
9506 position values). It is -1 when calling from x_set_frame_parameters,
9507 which means, do adjust for borders but don't change the gravity. */
9508
9ef2e2cf 9509void
ee78dc32
GV
9510x_set_offset (f, xoff, yoff, change_gravity)
9511 struct frame *f;
9512 register int xoff, yoff;
9513 int change_gravity;
9514{
9515 int modified_top, modified_left;
9516
9517 if (change_gravity > 0)
9518 {
fbd6baed
GV
9519 f->output_data.w32->top_pos = yoff;
9520 f->output_data.w32->left_pos = xoff;
9521 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 9522 if (xoff < 0)
fbd6baed 9523 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 9524 if (yoff < 0)
fbd6baed
GV
9525 f->output_data.w32->size_hint_flags |= YNegative;
9526 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
9527 }
9528 x_calc_absolute_position (f);
9529
9530 BLOCK_INPUT;
9531 x_wm_set_size_hint (f, (long) 0, 0);
9532
fbd6baed
GV
9533 modified_left = f->output_data.w32->left_pos;
9534 modified_top = f->output_data.w32->top_pos;
ee78dc32 9535
fbd6baed 9536 my_set_window_pos (FRAME_W32_WINDOW (f),
52cf03a1
GV
9537 NULL,
9538 modified_left, modified_top,
cabb23bc 9539 0, 0,
689004fa 9540 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
ee78dc32
GV
9541 UNBLOCK_INPUT;
9542}
9543
9544/* Call this to change the size of frame F's x-window.
9545 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9546 for this size change and subsequent size changes.
9547 Otherwise we leave the window gravity unchanged. */
c2cc16fa 9548
791f420f 9549void
ee78dc32
GV
9550x_set_window_size (f, change_gravity, cols, rows)
9551 struct frame *f;
9552 int change_gravity;
9553 int cols, rows;
9554{
9555 int pixelwidth, pixelheight;
9556
9557 BLOCK_INPUT;
9558
9559 check_frame_size (f, &rows, &cols);
fbd6baed 9560 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
9561 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9562 ? 0
fbd6baed 9563 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
791f420f
JR
9564 f->output_data.w32->flags_areas_extra
9565 = FRAME_FLAGS_AREA_WIDTH (f);
ee78dc32
GV
9566 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
9567 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
9568
fbd6baed 9569 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
9570 x_wm_set_size_hint (f, (long) 0, 0);
9571
9572 {
9573 RECT rect;
9574
9575 rect.left = rect.top = 0;
9576 rect.right = pixelwidth;
9577 rect.bottom = pixelheight;
9578
fbd6baed 9579 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
97c23857 9580 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32 9581
fbd6baed 9582 my_set_window_pos (FRAME_W32_WINDOW (f),
52cf03a1
GV
9583 NULL,
9584 0, 0,
689004fa
GV
9585 rect.right - rect.left,
9586 rect.bottom - rect.top,
9587 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
ee78dc32
GV
9588 }
9589
9590 /* Now, strictly speaking, we can't be sure that this is accurate,
9591 but the window manager will get around to dealing with the size
9592 change request eventually, and we'll hear how it went when the
9593 ConfigureNotify event gets here.
9594
9595 We could just not bother storing any of this information here,
9596 and let the ConfigureNotify event set everything up, but that
ec48c3a7 9597 might be kind of confusing to the Lisp code, since size changes
ee78dc32 9598 wouldn't be reported in the frame parameters until some random
791f420f
JR
9599 point in the future when the ConfigureNotify event arrives.
9600
9601 We pass 1 for DELAY since we can't run Lisp code inside of
9602 a BLOCK_INPUT. */
9603 change_frame_size (f, rows, cols, 0, 1, 0);
ee78dc32
GV
9604 PIXEL_WIDTH (f) = pixelwidth;
9605 PIXEL_HEIGHT (f) = pixelheight;
9606
689004fa
GV
9607 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9608 receive in the ConfigureNotify event; if we get what we asked
9609 for, then the event won't cause the screen to become garbaged, so
9610 we have to make sure to do it here. */
9611 SET_FRAME_GARBAGED (f);
9612
ee78dc32 9613 /* If cursor was outside the new size, mark it as off. */
791f420f 9614 mark_window_cursors_off (XWINDOW (f->root_window));
ee78dc32 9615
689004fa
GV
9616 /* Clear out any recollection of where the mouse highlighting was,
9617 since it might be in a place that's outside the new frame size.
9618 Actually checking whether it is outside is a pain in the neck,
9619 so don't try--just let the highlighting be done afresh with new size. */
31d4844a
KH
9620 cancel_mouse_face (f);
9621
ee78dc32
GV
9622 UNBLOCK_INPUT;
9623}
9624\f
9625/* Mouse warping. */
9626
ec48c3a7
JR
9627void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
9628
9629void
9630x_set_mouse_position (f, x, y)
9631 struct frame *f;
9632 int x, y;
9633{
9634 int pix_x, pix_y;
9635
9636 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
9637 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
9638
9639 if (pix_x < 0) pix_x = 0;
9640 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
9641
9642 if (pix_y < 0) pix_y = 0;
9643 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
9644
9645 x_set_mouse_pixel_position (f, pix_x, pix_y);
9646}
9647
1bcd16f2
MB
9648void
9649x_set_mouse_pixel_position (f, pix_x, pix_y)
9650 struct frame *f;
9651 int pix_x, pix_y;
9652{
689004fa
GV
9653 RECT rect;
9654 POINT pt;
9655
1bcd16f2
MB
9656 BLOCK_INPUT;
9657
689004fa
GV
9658 GetClientRect (FRAME_W32_WINDOW (f), &rect);
9659 pt.x = rect.left + pix_x;
9660 pt.y = rect.top + pix_y;
9661 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
1bcd16f2 9662
689004fa 9663 SetCursorPos (pt.x, pt.y);
1bcd16f2
MB
9664
9665 UNBLOCK_INPUT;
9666}
9667
ee78dc32
GV
9668\f
9669/* focus shifting, raising and lowering. */
9670
ec48c3a7 9671void
ee78dc32
GV
9672x_focus_on_frame (f)
9673 struct frame *f;
9674{
689004fa
GV
9675 struct w32_display_info *dpyinfo = &one_w32_display_info;
9676
9677 /* Give input focus to frame. */
9678 BLOCK_INPUT;
9679#if 0
9680 /* Try not to change its Z-order if possible. */
9681 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
9682 my_set_focus (f, FRAME_W32_WINDOW (f));
9683 else
9684#endif
ef0e360f 9685 my_set_foreground_window (FRAME_W32_WINDOW (f));
689004fa 9686 UNBLOCK_INPUT;
ee78dc32
GV
9687}
9688
ec48c3a7 9689void
ee78dc32
GV
9690x_unfocus_frame (f)
9691 struct frame *f;
9692{
9693}
9694
9695/* Raise frame F. */
791f420f 9696void
ee78dc32
GV
9697x_raise_frame (f)
9698 struct frame *f;
9699{
689004fa
GV
9700 BLOCK_INPUT;
9701
9702 /* Strictly speaking, raise-frame should only change the frame's Z
9703 order, leaving input focus unchanged. This is reasonable behaviour
9704 on X where the usual policy is point-to-focus. However, this
9705 behaviour would be very odd on Windows where the usual policy is
9706 click-to-focus.
9707
9708 On X, if the mouse happens to be over the raised frame, it gets
9709 input focus anyway (so the window with focus will never be
9710 completely obscured) - if not, then just moving the mouse over it
9711 is sufficient to give it focus. On Windows, the user must actually
9712 click on the frame (preferrably the title bar so as not to move
9713 point), which is more awkward. Also, no other Windows program
9714 raises a window to the top but leaves another window (possibly now
9715 completely obscured) with input focus.
9716
9717 Because there is a system setting on Windows that allows the user
9718 to choose the point to focus policy, we make the strict semantics
9719 optional, but by default we grab focus when raising. */
9720
9721 if (NILP (Vw32_grab_focus_on_raise))
ee78dc32 9722 {
689004fa
GV
9723 /* The obvious call to my_set_window_pos doesn't work if Emacs is
9724 not already the foreground application: the frame is raised
9725 above all other frames belonging to us, but not above the
9726 current top window. To achieve that, we have to resort to this
9727 more cumbersome method. */
9728
9729 HDWP handle = BeginDeferWindowPos (2);
9730 if (handle)
9731 {
9732 DeferWindowPos (handle,
9733 FRAME_W32_WINDOW (f),
9734 HWND_TOP,
9735 0, 0, 0, 0,
9736 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9737
9738 DeferWindowPos (handle,
9739 GetForegroundWindow (),
9740 FRAME_W32_WINDOW (f),
9741 0, 0, 0, 0,
9742 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9743
9744 EndDeferWindowPos (handle);
9745 }
ee78dc32 9746 }
689004fa
GV
9747 else
9748 {
ef0e360f 9749 my_set_foreground_window (FRAME_W32_WINDOW (f));
689004fa
GV
9750 }
9751
9752 UNBLOCK_INPUT;
ee78dc32
GV
9753}
9754
9755/* Lower frame F. */
791f420f 9756void
ee78dc32
GV
9757x_lower_frame (f)
9758 struct frame *f;
9759{
689004fa
GV
9760 BLOCK_INPUT;
9761 my_set_window_pos (FRAME_W32_WINDOW (f),
9762 HWND_BOTTOM,
9763 0, 0, 0, 0,
9764 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9765 UNBLOCK_INPUT;
ee78dc32
GV
9766}
9767
9768static void
ec48c3a7 9769w32_frame_raise_lower (f, raise_flag)
ee78dc32 9770 FRAME_PTR f;
ec48c3a7 9771 int raise_flag;
ee78dc32 9772{
7e6ac5b9
AI
9773 if (! FRAME_W32_P (f))
9774 return;
9775
ec48c3a7 9776 if (raise_flag)
ee78dc32
GV
9777 x_raise_frame (f);
9778 else
9779 x_lower_frame (f);
9780}
9781\f
9782/* Change of visibility. */
9783
9784/* This tries to wait until the frame is really visible.
9785 However, if the window manager asks the user where to position
9786 the frame, this will return before the user finishes doing that.
9787 The frame will not actually be visible at that time,
9788 but it will become visible later when the window manager
9789 finishes with it. */
9790
ec48c3a7 9791void
ee78dc32
GV
9792x_make_frame_visible (f)
9793 struct frame *f;
9794{
7f5d1df8
GV
9795 Lisp_Object type;
9796
ee78dc32
GV
9797 BLOCK_INPUT;
9798
7f5d1df8
GV
9799 type = x_icon_type (f);
9800 if (!NILP (type))
9801 x_bitmap_icon (f, type);
9802
ee78dc32
GV
9803 if (! FRAME_VISIBLE_P (f))
9804 {
9805 /* We test FRAME_GARBAGED_P here to make sure we don't
9806 call x_set_offset a second time
9807 if we get to x_make_frame_visible a second time
9808 before the window gets really visible. */
9809 if (! FRAME_ICONIFIED_P (f)
fbd6baed 9810 && ! f->output_data.w32->asked_for_visible)
fbd6baed 9811 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
ee78dc32 9812
fbd6baed 9813 f->output_data.w32->asked_for_visible = 1;
52cf03a1 9814
689004fa
GV
9815// my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW);
9816 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
ee78dc32
GV
9817 }
9818
9819 /* Synchronize to ensure Emacs knows the frame is visible
9820 before we do anything else. We do this loop with input not blocked
9821 so that incoming events are handled. */
9822 {
9823 Lisp_Object frame;
791f420f 9824 int count;
ee78dc32
GV
9825
9826 /* This must come after we set COUNT. */
9827 UNBLOCK_INPUT;
9828
9829 XSETFRAME (frame, f);
9830
791f420f
JR
9831 /* Wait until the frame is visible. Process X events until a
9832 MapNotify event has been seen, or until we think we won't get a
9833 MapNotify at all.. */
9834 for (count = input_signal_count + 10;
9835 input_signal_count < count && !FRAME_VISIBLE_P (f);)
ee78dc32 9836 {
791f420f 9837 /* Force processing of queued events. */
01b220b6 9838 /* TODO: x_sync equivalent? */
791f420f 9839
ee78dc32
GV
9840 /* Machines that do polling rather than SIGIO have been observed
9841 to go into a busy-wait here. So we'll fake an alarm signal
9842 to let the handler know that there's something to be read.
9843 We used to raise a real alarm, but it seems that the handler
9844 isn't always enabled here. This is probably a bug. */
9845 if (input_polling_used ())
9846 {
9847 /* It could be confusing if a real alarm arrives while processing
9848 the fake one. Turn it off and let the handler reset it. */
a9b4e0ec
AI
9849 int old_poll_suppress_count = poll_suppress_count;
9850 poll_suppress_count = 1;
9851 poll_for_input_1 ();
9852 poll_suppress_count = old_poll_suppress_count;
ee78dc32 9853 }
ee78dc32
GV
9854 }
9855 FRAME_SAMPLE_VISIBILITY (f);
9856 }
9857}
9858
9859/* Change from mapped state to withdrawn state. */
9860
9861/* Make the frame visible (mapped and not iconified). */
9862
9863x_make_frame_invisible (f)
9864 struct frame *f;
9865{
ee78dc32 9866 /* Don't keep the highlight on an invisible frame. */
fbd6baed
GV
9867 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9868 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
ee78dc32
GV
9869
9870 BLOCK_INPUT;
9871
689004fa 9872 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
ee78dc32
GV
9873
9874 /* We can't distinguish this from iconification
9875 just by the event that we get from the server.
9876 So we can't win using the usual strategy of letting
9877 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9878 and synchronize with the server to make sure we agree. */
9879 f->visible = 0;
9880 FRAME_ICONIFIED_P (f) = 0;
9881 f->async_visible = 0;
9882 f->async_iconified = 0;
9883
9884 UNBLOCK_INPUT;
9885}
9886
9887/* Change window state from mapped to iconified. */
9888
52cf03a1
GV
9889void
9890x_iconify_frame (f)
ee78dc32
GV
9891 struct frame *f;
9892{
7f5d1df8 9893 Lisp_Object type;
ee78dc32
GV
9894
9895 /* Don't keep the highlight on an invisible frame. */
fbd6baed
GV
9896 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9897 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
ee78dc32
GV
9898
9899 if (f->async_iconified)
9900 return;
9901
9902 BLOCK_INPUT;
9903
7f5d1df8
GV
9904 type = x_icon_type (f);
9905 if (!NILP (type))
9906 x_bitmap_icon (f, type);
9907
689004fa 9908 /* Simulate the user minimizing the frame. */
4934bcdd 9909 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
ee78dc32
GV
9910
9911 UNBLOCK_INPUT;
9912}
c2cc16fa 9913
ee78dc32 9914\f
25badd7a 9915/* Free X resources of frame F. */
ee78dc32 9916
25badd7a
AI
9917void
9918x_free_frame_resources (f)
ee78dc32
GV
9919 struct frame *f;
9920{
fbd6baed 9921 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
9922
9923 BLOCK_INPUT;
9924
25badd7a
AI
9925 if (FRAME_W32_WINDOW (f))
9926 my_destroy_window (f, FRAME_W32_WINDOW (f));
9927
ee78dc32 9928 free_frame_menubar (f);
ee78dc32 9929
25badd7a
AI
9930 unload_color (f, f->output_data.x->foreground_pixel);
9931 unload_color (f, f->output_data.x->background_pixel);
9932 unload_color (f, f->output_data.w32->cursor_pixel);
9933 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
9934 unload_color (f, f->output_data.w32->border_pixel);
9935 unload_color (f, f->output_data.w32->mouse_pixel);
c2cc16fa
JR
9936 if (f->output_data.w32->white_relief.allocated_p)
9937 unload_color (f, f->output_data.w32->white_relief.pixel);
9938 if (f->output_data.w32->black_relief.allocated_p)
9939 unload_color (f, f->output_data.w32->black_relief.pixel);
9940
25badd7a
AI
9941 if (FRAME_FACE_CACHE (f))
9942 free_frame_faces (f);
9943
fbd6baed 9944 xfree (f->output_data.w32);
25badd7a
AI
9945 f->output_data.w32 = NULL;
9946
fbd6baed
GV
9947 if (f == dpyinfo->w32_focus_frame)
9948 dpyinfo->w32_focus_frame = 0;
9949 if (f == dpyinfo->w32_focus_event_frame)
9950 dpyinfo->w32_focus_event_frame = 0;
9951 if (f == dpyinfo->w32_highlight_frame)
9952 dpyinfo->w32_highlight_frame = 0;
ee78dc32 9953
ee78dc32
GV
9954 if (f == dpyinfo->mouse_face_mouse_frame)
9955 {
9956 dpyinfo->mouse_face_beg_row
9957 = dpyinfo->mouse_face_beg_col = -1;
9958 dpyinfo->mouse_face_end_row
9959 = dpyinfo->mouse_face_end_col = -1;
9960 dpyinfo->mouse_face_window = Qnil;
25badd7a
AI
9961 dpyinfo->mouse_face_deferred_gc = 0;
9962 dpyinfo->mouse_face_mouse_frame = 0;
ee78dc32
GV
9963 }
9964
9965 UNBLOCK_INPUT;
9966}
25badd7a
AI
9967
9968
9969/* Destroy the window of frame F. */
9970
9971x_destroy_window (f)
9972 struct frame *f;
9973{
9974 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9975
9976 x_free_frame_resources (f);
9977
9978 dpyinfo->reference_count--;
9979}
c2cc16fa 9980
ee78dc32
GV
9981\f
9982/* Setting window manager hints. */
9983
9984/* Set the normal size hints for the window manager, for frame F.
9985 FLAGS is the flags word to use--or 0 meaning preserve the flags
9986 that the window now has.
9987 If USER_POSITION is nonzero, we set the USPosition
9988 flag (this is useful when FLAGS is 0). */
791f420f 9989void
ee78dc32
GV
9990x_wm_set_size_hint (f, flags, user_position)
9991 struct frame *f;
9992 long flags;
9993 int user_position;
9994{
fbd6baed 9995 Window window = FRAME_W32_WINDOW (f);
ee78dc32 9996
97c23857 9997 enter_crit ();
ee78dc32 9998
689004fa
GV
9999 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
10000 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
10001 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
10002 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
ee78dc32 10003
97c23857 10004 leave_crit ();
ee78dc32
GV
10005}
10006
10007/* Window manager things */
10008x_wm_set_icon_position (f, icon_x, icon_y)
10009 struct frame *f;
10010 int icon_x, icon_y;
10011{
10012#if 0
fbd6baed 10013 Window window = FRAME_W32_WINDOW (f);
ee78dc32
GV
10014
10015 f->display.x->wm_hints.flags |= IconPositionHint;
10016 f->display.x->wm_hints.icon_x = icon_x;
10017 f->display.x->wm_hints.icon_y = icon_y;
10018
10019 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
10020#endif
10021}
10022
c2cc16fa
JR
10023\f
10024/***********************************************************************
10025 Fonts
10026 ***********************************************************************/
10027
10028/* The following functions are listed here to help diff stay in step
10029 with xterm.c. See w32fns.c for definitions.
10030
10031x_get_font_info (f, font_idx)
10032x_list_fonts (f, pattern, size, maxnames)
10033
10034 */
10035
10036#if GLYPH_DEBUG
10037
10038/* Check that FONT is valid on frame F. It is if it can be found in F's
10039 font table. */
10040
10041static void
10042x_check_font (f, font)
10043 struct frame *f;
10044 XFontStruct *font;
10045{
10046 int i;
10047 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10048
10049 xassert (font != NULL);
9ef2e2cf 10050
c2cc16fa
JR
10051 for (i = 0; i < dpyinfo->n_fonts; i++)
10052 if (dpyinfo->font_table[i].name
10053 && font == dpyinfo->font_table[i].font)
10054 break;
10055
10056 xassert (i < dpyinfo->n_fonts);
10057}
10058
10059#endif /* GLYPH_DEBUG != 0 */
10060
10061/* Set *W to the minimum width, *H to the minimum font height of FONT.
10062 Note: There are (broken) X fonts out there with invalid XFontStruct
10063 min_bounds contents. For example, handa@etl.go.jp reports that
10064 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10065 have font->min_bounds.width == 0. */
10066
10067static INLINE void
10068x_font_min_bounds (font, w, h)
10069 XFontStruct *font;
10070 int *w, *h;
10071{
10072 /*
10073 * TODO: Windows does not appear to offer min bound, only
10074 * average and maximum width, and maximum height.
10075 */
10076 *h = FONT_HEIGHT (font);
10077 *w = FONT_WIDTH (font);
10078}
10079
10080
10081/* Compute the smallest character width and smallest font height over
10082 all fonts available on frame F. Set the members smallest_char_width
10083 and smallest_font_height in F's x_display_info structure to
10084 the values computed. Value is non-zero if smallest_font_height or
10085 smallest_char_width become smaller than they were before. */
10086
10087int
10088x_compute_min_glyph_bounds (f)
10089 struct frame *f;
10090{
10091 int i;
10092 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10093 XFontStruct *font;
10094 int old_width = dpyinfo->smallest_char_width;
10095 int old_height = dpyinfo->smallest_font_height;
10096
10097 dpyinfo->smallest_font_height = 100000;
10098 dpyinfo->smallest_char_width = 100000;
10099
10100 for (i = 0; i < dpyinfo->n_fonts; ++i)
10101 if (dpyinfo->font_table[i].name)
10102 {
10103 struct font_info *fontp = dpyinfo->font_table + i;
10104 int w, h;
10105
10106 font = (XFontStruct *) fontp->font;
10107 xassert (font != (XFontStruct *) ~0);
10108 x_font_min_bounds (font, &w, &h);
10109
10110 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10111 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10112 }
10113
10114 xassert (dpyinfo->smallest_char_width > 0
10115 && dpyinfo->smallest_font_height > 0);
10116
10117 return (dpyinfo->n_fonts == 1
10118 || dpyinfo->smallest_char_width < old_width
10119 || dpyinfo->smallest_font_height < old_height);
10120}
10121
10122/* The following functions are listed here to help diff stay in step
10123 with xterm.c. See w32fns.c for definitions.
10124
10125x_load_font (f, fontname, size)
10126x_query_font (f, fontname)
10127x_find_ccl_program (fontp)
10128
10129*/
ee78dc32 10130\f
791f420f
JR
10131/***********************************************************************
10132 Initialization
10133 ***********************************************************************/
ee78dc32 10134
fbd6baed 10135static int w32_initialized = 0;
ee78dc32 10136
f7737f5d
JR
10137void
10138w32_initialize_display_info (display_name)
10139 Lisp_Object display_name;
10140{
10141 struct w32_display_info *dpyinfo = &one_w32_display_info;
10142
10143 bzero (dpyinfo, sizeof (*dpyinfo));
10144
10145 /* Put it on w32_display_name_list. */
10146 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
10147 w32_display_name_list);
10148 dpyinfo->name_list_element = XCAR (w32_display_name_list);
10149
10150 dpyinfo->w32_id_name
10151 = (char *) xmalloc (XSTRING (Vinvocation_name)->size
10152 + XSTRING (Vsystem_name)->size
10153 + 2);
10154 sprintf (dpyinfo->w32_id_name, "%s@%s",
10155 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
10156
10157 /* Default Console mode values - overridden when running in GUI mode
10158 with values obtained from system metrics. */
10159 dpyinfo->resx = 1;
10160 dpyinfo->resy = 1;
10161 dpyinfo->height_in = 1;
10162 dpyinfo->width_in = 1;
10163 dpyinfo->n_planes = 1;
10164 dpyinfo->n_cbits = 4;
10165 dpyinfo->n_fonts = 0;
10166 dpyinfo->smallest_font_height = 1;
10167 dpyinfo->smallest_char_width = 1;
10168
10169 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10170 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10171 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10172 dpyinfo->mouse_face_window = Qnil;
10173
01b220b6 10174 /* TODO: dpyinfo->gray */
f7737f5d
JR
10175
10176}
10177
fbd6baed
GV
10178struct w32_display_info *
10179w32_term_init (display_name, xrm_option, resource_name)
ee78dc32
GV
10180 Lisp_Object display_name;
10181 char *xrm_option;
10182 char *resource_name;
10183{
fbd6baed 10184 struct w32_display_info *dpyinfo;
ee78dc32 10185 HDC hdc;
97c23857 10186
ee78dc32
GV
10187 BLOCK_INPUT;
10188
fbd6baed 10189 if (!w32_initialized)
ee78dc32 10190 {
fbd6baed
GV
10191 w32_initialize ();
10192 w32_initialized = 1;
ee78dc32
GV
10193 }
10194
10195 {
10196 int argc = 0;
10197 char *argv[3];
10198
10199 argv[0] = "";
10200 argc = 1;
10201 if (xrm_option)
10202 {
10203 argv[argc++] = "-xrm";
10204 argv[argc++] = xrm_option;
10205 }
10206 }
10207
f7737f5d 10208 w32_initialize_display_info (display_name);
ee78dc32 10209
f7737f5d 10210 dpyinfo = &one_w32_display_info;
8c3b00cb
AI
10211
10212 /* Put this display on the chain. */
10213 dpyinfo->next = x_display_list;
10214 x_display_list = dpyinfo;
ee78dc32 10215
52cf03a1 10216 hdc = GetDC (GetDesktopWindow ());
791f420f 10217
ee78dc32
GV
10218 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
10219 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
10220 dpyinfo->root_window = GetDesktopWindow ();
10221 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
10222 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
55689ab1
JR
10223 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
10224 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
52cf03a1 10225 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
791f420f 10226 dpyinfo->image_cache = make_image_cache ();
55689ab1
JR
10227 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
10228 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
ee78dc32 10229 ReleaseDC (GetDesktopWindow (), hdc);
52cf03a1
GV
10230
10231 /* initialise palette with white and black */
10232 {
10233 COLORREF color;
55689ab1
JR
10234 w32_defined_color (0, "white", &color, 1);
10235 w32_defined_color (0, "black", &color, 1);
52cf03a1
GV
10236 }
10237
f7737f5d
JR
10238 /* Create Row Bitmaps and store them for later use. */
10239 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
10240 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
10241 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
10242 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
10243 1, continued_bits);
10244 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
10245 1, 1, continuation_bits);
10246 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
10247
ee78dc32
GV
10248#ifndef F_SETOWN_BUG
10249#ifdef F_SETOWN
10250#ifdef F_SETOWN_SOCK_NEG
10251 /* stdin is a socket here */
10252 fcntl (connection, F_SETOWN, -getpid ());
10253#else /* ! defined (F_SETOWN_SOCK_NEG) */
10254 fcntl (connection, F_SETOWN, getpid ());
10255#endif /* ! defined (F_SETOWN_SOCK_NEG) */
10256#endif /* ! defined (F_SETOWN) */
10257#endif /* F_SETOWN_BUG */
10258
10259#ifdef SIGIO
10260 if (interrupt_input)
10261 init_sigio (connection);
10262#endif /* ! defined (SIGIO) */
10263
10264 UNBLOCK_INPUT;
10265
10266 return dpyinfo;
10267}
10268\f
10269/* Get rid of display DPYINFO, assuming all frames are already gone. */
10270
10271void
10272x_delete_display (dpyinfo)
fbd6baed 10273 struct w32_display_info *dpyinfo;
ee78dc32 10274{
fbd6baed 10275 /* Discard this display from w32_display_name_list and w32_display_list.
ee78dc32 10276 We can't use Fdelq because that can quit. */
fbd6baed 10277 if (! NILP (w32_display_name_list)
8e713be6
KR
10278 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
10279 w32_display_name_list = XCDR (w32_display_name_list);
ee78dc32
GV
10280 else
10281 {
10282 Lisp_Object tail;
10283
fbd6baed 10284 tail = w32_display_name_list;
8e713be6 10285 while (CONSP (tail) && CONSP (XCDR (tail)))
ee78dc32 10286 {
f7737f5d 10287 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
ee78dc32 10288 {
8e713be6 10289 XCDR (tail) = XCDR (XCDR (tail));
ee78dc32
GV
10290 break;
10291 }
8e713be6 10292 tail = XCDR (tail);
ee78dc32
GV
10293 }
10294 }
10295
52cf03a1
GV
10296 /* free palette table */
10297 {
fbd6baed 10298 struct w32_palette_entry * plist;
52cf03a1
GV
10299
10300 plist = dpyinfo->color_list;
10301 while (plist)
10302 {
fbd6baed 10303 struct w32_palette_entry * pentry = plist;
52cf03a1 10304 plist = plist->next;
9127e20e 10305 xfree (pentry);
52cf03a1
GV
10306 }
10307 dpyinfo->color_list = NULL;
10308 if (dpyinfo->palette)
10309 DeleteObject(dpyinfo->palette);
10310 }
ee78dc32 10311 xfree (dpyinfo->font_table);
fbd6baed 10312 xfree (dpyinfo->w32_id_name);
f7737f5d
JR
10313
10314 /* Destroy row bitmaps. */
10315 DeleteObject (left_bmp);
10316 DeleteObject (ov_bmp);
10317 DeleteObject (right_bmp);
10318 DeleteObject (continued_bmp);
10319 DeleteObject (continuation_bmp);
10320 DeleteObject (zv_bmp);
ee78dc32
GV
10321}
10322\f
fbd6baed 10323/* Set up use of W32. */
ee78dc32 10324
689004fa 10325DWORD w32_msg_worker ();
ee78dc32 10326
791f420f
JR
10327void
10328x_flush (struct frame * f)
10329{ /* Nothing to do */ }
10330
10331static struct redisplay_interface w32_redisplay_interface =
10332{
10333 x_produce_glyphs,
10334 x_write_glyphs,
10335 x_insert_glyphs,
10336 x_clear_end_of_line,
10337 x_scroll_run,
10338 x_after_update_window_line,
10339 x_update_window_begin,
10340 x_update_window_end,
10341 w32_cursor_to,
10342 x_flush,
ec48c3a7 10343 x_clear_mouse_face,
791f420f
JR
10344 x_get_glyph_overhangs,
10345 x_fix_overlapping_area
10346};
10347
10348void
fbd6baed
GV
10349w32_initialize ()
10350{
791f420f
JR
10351 rif = &w32_redisplay_interface;
10352
96214669
GV
10353 /* MSVC does not type K&R functions with no arguments correctly, and
10354 so we must explicitly cast them. */
791f420f 10355 clear_frame_hook = (void (*)(void)) x_clear_frame;
96214669 10356 ring_bell_hook = (void (*)(void)) w32_ring_bell;
791f420f
JR
10357 update_begin_hook = x_update_begin;
10358 update_end_hook = x_update_end;
0f32f023 10359
ee78dc32 10360 read_socket_hook = w32_read_socket;
0f32f023 10361
fbd6baed 10362 frame_up_to_date_hook = w32_frame_up_to_date;
0f32f023 10363
fbd6baed
GV
10364 mouse_position_hook = w32_mouse_position;
10365 frame_rehighlight_hook = w32_frame_rehighlight;
10366 frame_raise_lower_hook = w32_frame_raise_lower;
10367 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
10368 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
10369 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
10370 judge_scroll_bars_hook = w32_judge_scroll_bars;
791f420f 10371 estimate_mode_line_height_hook = x_estimate_mode_line_height;
ee78dc32
GV
10372
10373 scroll_region_ok = 1; /* we'll scroll partial frames */
49be9f70 10374 char_ins_del_ok = 1;
ee78dc32
GV
10375 line_ins_del_ok = 1; /* we'll just blt 'em */
10376 fast_clear_end_of_line = 1; /* X does this well */
10377 memory_below_frame = 0; /* we don't remember what scrolls
10378 off the bottom */
10379 baud_rate = 19200;
10380
791f420f
JR
10381 last_tool_bar_item = -1;
10382 any_help_event_p = 0;
10383
689004fa
GV
10384 /* Initialize input mode: interrupt_input off, no flow control, allow
10385 8 bit character input, standard quit char. */
10386 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
ee78dc32
GV
10387
10388 /* Create the window thread - it will terminate itself or when the app terminates */
10389
10390 init_crit ();
10391
10392 dwMainThreadId = GetCurrentThreadId ();
10393 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
10394 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
10395
10396 /* Wait for thread to start */
10397
10398 {
10399 MSG msg;
10400
10401 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
10402
e9e23e23 10403 hWindowsThread = CreateThread (NULL, 0,
689004fa 10404 (LPTHREAD_START_ROUTINE) w32_msg_worker,
e9e23e23 10405 0, 0, &dwWindowsThreadId);
ee78dc32
GV
10406
10407 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
10408 }
10409
52cf03a1 10410 /* It is desirable that mainThread should have the same notion of
e9e23e23 10411 focus window and active window as windowsThread. Unfortunately, the
52cf03a1
GV
10412 following call to AttachThreadInput, which should do precisely what
10413 we need, causes major problems when Emacs is linked as a console
10414 program. Unfortunately, we have good reasons for doing that, so
e9e23e23 10415 instead we need to send messages to windowsThread to make some API
52cf03a1
GV
10416 calls for us (ones that affect, or depend on, the active/focus
10417 window state. */
10418#ifdef ATTACH_THREADS
e9e23e23 10419 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
52cf03a1 10420#endif
689004fa
GV
10421
10422 /* Dynamically link to optional system components. */
10423 {
10424 HANDLE user_lib = LoadLibrary ("user32.dll");
10425
10426#define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
10427
10428 /* New proportional scroll bar functions. */
9127e20e
JR
10429 LOAD_PROC (SetScrollInfo);
10430 LOAD_PROC (GetScrollInfo);
689004fa
GV
10431
10432#undef LOAD_PROC
10433
10434 FreeLibrary (user_lib);
10435
10436 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
10437 otherwise use the fixed height. */
10438 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
10439 GetSystemMetrics (SM_CYVTHUMB);
10440
10441 /* For either kind of scroll bar, take account of the arrows; these
10442 effectively form the border of the main scroll bar range. */
10443 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
10444 = GetSystemMetrics (SM_CYVSCROLL);
10445 }
ee78dc32
GV
10446}
10447
10448void
fbd6baed 10449syms_of_w32term ()
ee78dc32 10450{
fbd6baed
GV
10451 staticpro (&w32_display_name_list);
10452 w32_display_name_list = Qnil;
ee78dc32
GV
10453
10454 staticpro (&last_mouse_scroll_bar);
10455 last_mouse_scroll_bar = Qnil;
10456
10457 staticpro (&Qvendor_specific_keysyms);
10458 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
52cf03a1 10459
fbd6baed
GV
10460 DEFVAR_INT ("w32-num-mouse-buttons",
10461 &Vw32_num_mouse_buttons,
52cf03a1 10462 "Number of physical mouse buttons.");
fbd6baed 10463 Vw32_num_mouse_buttons = Qnil;
52cf03a1 10464
fbd6baed
GV
10465 DEFVAR_LISP ("w32-swap-mouse-buttons",
10466 &Vw32_swap_mouse_buttons,
52cf03a1
GV
10467 "Swap the mapping of middle and right mouse buttons.\n\
10468When nil, middle button is mouse-2 and right button is mouse-3.");
fbd6baed 10469 Vw32_swap_mouse_buttons = Qnil;
689004fa
GV
10470
10471 DEFVAR_LISP ("w32-grab-focus-on-raise",
10472 &Vw32_grab_focus_on_raise,
10473 "Raised frame grabs input focus.\n\
10474When t, `raise-frame' grabs input focus as well. This fits well\n\
10475with the normal Windows click-to-focus policy, but might not be\n\
10476desirable when using a point-to-focus policy.");
10477 Vw32_grab_focus_on_raise = Qt;
10478
10479 DEFVAR_LISP ("w32-capslock-is-shiftlock",
10480 &Vw32_capslock_is_shiftlock,
10481 "Apply CapsLock state to non character input keys.\n\
10482When nil, CapsLock only affects normal character input keys.");
10483 Vw32_capslock_is_shiftlock = Qnil;
ef0e360f
GV
10484
10485 DEFVAR_LISP ("w32-recognize-altgr",
10486 &Vw32_recognize_altgr,
10487 "Recognize right-alt and left-ctrl as AltGr.\n\
10488When nil, the right-alt and left-ctrl key combination is\n\
10489interpreted normally.");
10490 Vw32_recognize_altgr = Qt;
bc6af935 10491
484fa2c1
GV
10492 DEFVAR_BOOL ("w32-enable-unicode-output",
10493 &w32_enable_unicode_output,
10494 "Enable the use of Unicode for text output if non-nil.\n\
cabb23bc
GV
10495Unicode output may prevent some third party applications for displaying\n\
10496Far-East Languages on Windows 95/98 from working properly.\n\
10497NT uses Unicode internally anyway, so this flag will probably have no\n\
10498affect on NT machines.");
484fa2c1 10499 w32_enable_unicode_output = 1;
a1b9438c 10500
791f420f 10501 help_echo = Qnil;
ec48c3a7
JR
10502 staticpro (&help_echo);
10503 help_echo_object = Qnil;
10504 staticpro (&help_echo_object);
158cba56
JR
10505 help_echo_window = Qnil;
10506 staticpro (&help_echo_window);
791f420f 10507 previous_help_echo = Qnil;
ec48c3a7 10508 staticpro (&previous_help_echo);
158cba56 10509 help_echo_pos = -1;
791f420f
JR
10510
10511 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
10512 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
10513For example, if a block cursor is over a tab, it will be drawn as\n\
10514wide as that tab on the display.");
10515 x_stretch_cursor_p = 0;
10516
5bf04520 10517 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
791f420f 10518 "If not nil, Emacs uses toolkit scroll bars.");
5bf04520 10519 Vx_toolkit_scroll_bars = Qt;
791f420f
JR
10520
10521 staticpro (&last_mouse_motion_frame);
10522 last_mouse_motion_frame = Qnil;
ee78dc32 10523}