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