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