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