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