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