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