*** empty log message ***
[emacs.git] / src / w32term.c
blob08b076df037d01bf28f834f297a88a1c15d73b3c
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
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"
30 #include "w32heap.h"
31 #include "w32term.h"
32 #include "w32bdf.h"
33 #include <shellapi.h>
35 #include "systty.h"
36 #include "systime.h"
37 #include "atimer.h"
38 #include "keymap.h"
40 #include <ctype.h>
41 #include <errno.h>
42 #include <setjmp.h>
43 #include <sys/stat.h>
45 #include "keyboard.h"
46 #include "frame.h"
47 #include "dispextern.h"
48 #include "fontset.h"
49 #include "termhooks.h"
50 #include "termopts.h"
51 #include "termchar.h"
52 #include "gnu.h"
53 #include "disptab.h"
54 #include "buffer.h"
55 #include "window.h"
56 #include "intervals.h"
57 #include "composite.h"
58 #include "coding.h"
60 #define abs(x) ((x) < 0 ? -(x) : (x))
62 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
65 /* Fringe bitmaps. */
67 enum fringe_bitmap_type
69 NO_FRINGE_BITMAP,
70 LEFT_TRUNCATION_BITMAP,
71 RIGHT_TRUNCATION_BITMAP,
72 OVERLAY_ARROW_BITMAP,
73 CONTINUED_LINE_BITMAP,
74 CONTINUATION_LINE_BITMAP,
75 ZV_LINE_BITMAP
78 /* Bitmaps are all unsigned short, as Windows requires bitmap data to
79 be Word aligned. For some reason they are horizontally reflected
80 compared to how they appear on X, so changes in xterm.c should be
81 reflected here. */
83 /* Bitmap drawn to indicate lines not displaying text if
84 `indicate-empty-lines' is non-nil. */
86 #define zv_width 8
87 #define zv_height 72
88 #define zv_period 3
89 static unsigned short zv_bits[] = {
90 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
91 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
92 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
93 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
94 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
95 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
96 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
97 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
98 static HBITMAP zv_bmp;
100 /* An arrow like this: `<-'. */
102 #define left_width 8
103 #define left_height 8
104 static unsigned short left_bits[] = {
105 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
106 static HBITMAP left_bmp;
108 /* Right truncation arrow bitmap `->'. */
110 #define right_width 8
111 #define right_height 8
112 static unsigned short right_bits[] = {
113 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
114 static HBITMAP right_bmp;
116 /* Marker for continued lines. */
118 #define continued_width 8
119 #define continued_height 8
120 static unsigned short continued_bits[] = {
121 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
122 static HBITMAP continued_bmp;
124 /* Marker for continuation lines. */
126 #define continuation_width 8
127 #define continuation_height 8
128 static unsigned short continuation_bits[] = {
129 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
130 static HBITMAP continuation_bmp;
132 /* Overlay arrow bitmap. */
134 #if 0
135 /* A bomb. */
136 #define ov_width 8
137 #define ov_height 8
138 static unsigned short ov_bits[] = {
139 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
140 #else
141 /* A triangular arrow. */
142 #define ov_width 8
143 #define ov_height 8
144 static unsigned short ov_bits[] = {
145 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
146 #endif
147 static HBITMAP ov_bmp;
149 extern Lisp_Object Qhelp_echo;
152 /* Non-nil means Emacs uses toolkit scroll bars. */
154 Lisp_Object Vx_toolkit_scroll_bars;
156 /* If a string, w32_read_socket generates an event to display that string.
157 (The display is done in read_char.) */
159 static Lisp_Object help_echo;
160 static Lisp_Object help_echo_window;
161 static Lisp_Object help_echo_object;
162 static int help_echo_pos;
164 /* Temporary variables for w32_read_socket. */
166 static Lisp_Object previous_help_echo;
167 static int last_mousemove_x = 0;
168 static int last_mousemove_y = 0;
170 /* Non-zero means that a HELP_EVENT has been generated since Emacs
171 start. */
173 static int any_help_event_p;
175 /* Non-zero means autoselect window with the mouse cursor. */
177 int mouse_autoselect_window;
179 /* Non-zero means draw block and hollow cursor as wide as the glyph
180 under it. For example, if a block cursor is over a tab, it will be
181 drawn as wide as that tab on the display. */
183 int x_stretch_cursor_p;
185 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
187 int x_use_underline_position_properties;
189 extern unsigned int msh_mousewheel;
191 extern void free_frame_menubar ();
193 extern int w32_codepage_for_font (char *fontname);
195 extern glyph_metric *w32_BDF_TextMetric(bdffont *fontp,
196 unsigned char *text, int dim);
197 extern Lisp_Object Vwindow_system;
199 #define x_any_window_to_frame x_window_to_frame
200 #define x_top_window_to_frame x_window_to_frame
203 /* This is display since w32 does not support multiple ones. */
204 struct w32_display_info one_w32_display_info;
205 struct w32_display_info *x_display_list;
207 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
208 one for each element of w32_display_list and in the same order.
209 NAME is the name of the frame.
210 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
211 Lisp_Object w32_display_name_list;
213 /* Frame being updated by update_frame. This is declared in term.c.
214 This is set by update_begin and looked at by all the
215 w32 functions. It is zero while not inside an update.
216 In that case, the w32 functions assume that `SELECTED_FRAME ()'
217 is the frame to apply to. */
218 extern struct frame *updating_frame;
220 /* This is a frame waiting to be autoraised, within w32_read_socket. */
221 struct frame *pending_autoraise_frame;
223 /* Nominal cursor position -- where to draw output.
224 HPOS and VPOS are window relative glyph matrix coordinates.
225 X and Y are window relative pixel coordinates. */
227 struct cursor_pos output_cursor;
229 /* The handle of the frame that currently owns the system caret. */
230 HWND w32_system_caret_hwnd;
231 int w32_system_caret_height;
232 int w32_system_caret_x;
233 int w32_system_caret_y;
234 int w32_use_visible_system_caret;
236 /* Flag to enable Unicode output in case users wish to use programs
237 like Twinbridge on '95 rather than installed system level support
238 for Far East languages. */
239 int w32_enable_unicode_output;
241 DWORD dwWindowsThreadId = 0;
242 HANDLE hWindowsThread = NULL;
243 DWORD dwMainThreadId = 0;
244 HANDLE hMainThread = NULL;
246 #ifndef SIF_ALL
247 /* These definitions are new with Windows 95. */
248 #define SIF_RANGE 0x0001
249 #define SIF_PAGE 0x0002
250 #define SIF_POS 0x0004
251 #define SIF_DISABLENOSCROLL 0x0008
252 #define SIF_TRACKPOS 0x0010
253 #define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
255 typedef struct tagSCROLLINFO
257 UINT cbSize;
258 UINT fMask;
259 int nMin;
260 int nMax;
261 UINT nPage;
262 int nPos;
263 int nTrackPos;
264 } SCROLLINFO, FAR *LPSCROLLINFO;
265 typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
266 #endif /* SIF_ALL */
268 /* Dynamic linking to new proportional scroll bar functions. */
269 int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
270 BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
272 int vertical_scroll_bar_min_handle;
273 int vertical_scroll_bar_top_border;
274 int vertical_scroll_bar_bottom_border;
276 int last_scroll_bar_drag_pos;
278 /* Mouse movement. */
280 /* Where the mouse was last time we reported a mouse event. */
282 FRAME_PTR last_mouse_frame;
283 static RECT last_mouse_glyph;
284 static Lisp_Object last_mouse_press_frame;
286 Lisp_Object Vw32_num_mouse_buttons;
288 Lisp_Object Vw32_swap_mouse_buttons;
290 /* Control whether x_raise_frame also sets input focus. */
291 Lisp_Object Vw32_grab_focus_on_raise;
293 /* Control whether Caps Lock affects non-ascii characters. */
294 Lisp_Object Vw32_capslock_is_shiftlock;
296 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
297 Lisp_Object Vw32_recognize_altgr;
299 /* The scroll bar in which the last motion event occurred.
301 If the last motion event occurred in a scroll bar, we set this
302 so w32_mouse_position can know whether to report a scroll bar motion or
303 an ordinary motion.
305 If the last motion event didn't occur in a scroll bar, we set this
306 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
307 static Lisp_Object last_mouse_scroll_bar;
308 static int last_mouse_scroll_bar_pos;
310 /* This is a hack. We would really prefer that w32_mouse_position would
311 return the time associated with the position it returns, but there
312 doesn't seem to be any way to wrest the time-stamp from the server
313 along with the position query. So, we just keep track of the time
314 of the last movement we received, and return that in hopes that
315 it's somewhat accurate. */
317 static Time last_mouse_movement_time;
319 /* Incremented by w32_read_socket whenever it really tries to read
320 events. */
322 #ifdef __STDC__
323 static int volatile input_signal_count;
324 #else
325 static int input_signal_count;
326 #endif
328 extern Lisp_Object Vcommand_line_args, Vsystem_name;
330 extern Lisp_Object Qface, Qmouse_face;
332 #ifndef USE_CRT_DLL
333 extern int errno;
334 #endif
336 /* A mask of extra modifier bits to put into every keyboard char. */
338 extern EMACS_INT extra_keyboard_modifiers;
340 /* Enumeration for overriding/changing the face to use for drawing
341 glyphs in x_draw_glyphs. */
343 enum draw_glyphs_face
345 DRAW_NORMAL_TEXT,
346 DRAW_INVERSE_VIDEO,
347 DRAW_CURSOR,
348 DRAW_MOUSE_FACE,
349 DRAW_IMAGE_RAISED,
350 DRAW_IMAGE_SUNKEN
353 static void x_update_window_end P_ ((struct window *, int, int));
354 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
355 void w32_delete_display P_ ((struct w32_display_info *));
356 static int fast_find_position P_ ((struct window *, int, int *, int *,
357 int *, int *, Lisp_Object));
358 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
359 int *, int *, int *, int *, int));
360 static void set_output_cursor P_ ((struct cursor_pos *));
361 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
362 int *, int *, int *, int));
363 static void note_mode_line_highlight P_ ((struct window *, int, int));
364 static void note_mouse_highlight P_ ((struct frame *, int, int));
365 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
366 static void w32_handle_tool_bar_click P_ ((struct frame *,
367 struct input_event *));
368 static void show_mouse_face P_ ((struct w32_display_info *,
369 enum draw_glyphs_face));
370 static int cursor_in_mouse_face_p P_ ((struct window *));
371 static int clear_mouse_face P_ ((struct w32_display_info *));
373 void x_lower_frame P_ ((struct frame *));
374 void x_scroll_bar_clear P_ ((struct frame *));
375 void x_wm_set_size_hint P_ ((struct frame *, long, int));
376 void x_raise_frame P_ ((struct frame *));
377 void x_set_window_size P_ ((struct frame *, int, int, int));
378 void x_wm_set_window_state P_ ((struct frame *, int));
379 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
380 void w32_initialize P_ ((void));
381 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
382 int x_compute_min_glyph_bounds P_ ((struct frame *));
383 static void x_draw_phys_cursor_glyph P_ ((struct window *,
384 struct glyph_row *,
385 enum draw_glyphs_face));
386 static void x_update_end P_ ((struct frame *));
387 static void w32_frame_up_to_date P_ ((struct frame *));
388 static void w32_set_terminal_modes P_ ((void));
389 static void w32_reset_terminal_modes P_ ((void));
390 static void w32_cursor_to P_ ((int, int, int, int));
391 static void x_write_glyphs P_ ((struct glyph *, int));
392 static void x_clear_end_of_line P_ ((int));
393 static void x_clear_frame P_ ((void));
394 static void x_clear_cursor P_ ((struct window *));
395 static void frame_highlight P_ ((struct frame *));
396 static void frame_unhighlight P_ ((struct frame *));
397 static void x_new_focus_frame P_ ((struct w32_display_info *,
398 struct frame *));
399 static void w32_frame_rehighlight P_ ((struct frame *));
400 static void x_frame_rehighlight P_ ((struct w32_display_info *));
401 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
402 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
403 enum text_cursor_kinds));
404 static void expose_frame P_ ((struct frame *, int, int, int, int));
405 static int expose_window_tree P_ ((struct window *, RECT *));
406 static void expose_overlaps P_ ((struct window *, struct glyph_row *,
407 struct glyph_row *));
408 static int expose_window P_ ((struct window *, RECT *));
409 static void expose_area P_ ((struct window *, struct glyph_row *,
410 RECT *, enum glyph_row_area));
411 static int expose_line P_ ((struct window *, struct glyph_row *,
412 RECT *));
413 void x_update_cursor P_ ((struct frame *, int));
414 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
415 static void x_update_window_cursor P_ ((struct window *, int));
416 static void x_erase_phys_cursor P_ ((struct window *));
417 void x_display_cursor P_ ((struct window *w, int, int, int, int, int));
418 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
419 static void w32_draw_fringe_bitmap P_ ((struct window *, HDC hdc,
420 struct glyph_row *,
421 enum fringe_bitmap_type, int left_p));
422 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *,
423 HDC, int));
424 static int x_phys_cursor_in_rect_p P_ ((struct window *, RECT *));
425 static void x_draw_row_fringe_bitmaps P_ ((struct window *,
426 struct glyph_row *));
427 static void notice_overwritten_cursor P_ ((struct window *,
428 enum glyph_row_area,
429 int, int, int, int));
431 static Lisp_Object Qvendor_specific_keysyms;
434 /***********************************************************************
435 Debugging
436 ***********************************************************************/
438 #if 0
440 /* This is a function useful for recording debugging information about
441 the sequence of occurrences in this file. */
443 struct record
445 char *locus;
446 int type;
449 struct record event_record[100];
451 int event_record_index;
453 record_event (locus, type)
454 char *locus;
455 int type;
457 if (event_record_index == sizeof (event_record) / sizeof (struct record))
458 event_record_index = 0;
460 event_record[event_record_index].locus = locus;
461 event_record[event_record_index].type = type;
462 event_record_index++;
465 #endif /* 0 */
468 void XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
469 XGCValues *xgcv)
471 if (mask & GCForeground)
472 gc->foreground = xgcv->foreground;
473 if (mask & GCBackground)
474 gc->background = xgcv->background;
475 if (mask & GCFont)
476 gc->font = xgcv->font;
479 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
480 XGCValues *xgcv)
482 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
483 bzero (gc, sizeof (XGCValues));
485 XChangeGC (ignore, gc, mask, xgcv);
487 return gc;
490 void XGetGCValues (void* ignore, XGCValues *gc,
491 unsigned long mask, XGCValues *xgcv)
493 XChangeGC (ignore, xgcv, mask, gc);
496 static void
497 w32_set_clip_rectangle (HDC hdc, RECT *rect)
499 if (rect)
501 HRGN clip_region = CreateRectRgnIndirect (rect);
502 SelectClipRgn (hdc, clip_region);
503 DeleteObject (clip_region);
505 else
506 SelectClipRgn (hdc, NULL);
510 /* Draw a hollow rectangle at the specified position. */
511 void
512 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
513 int width, int height)
515 HBRUSH hb, oldhb;
516 HPEN hp, oldhp;
518 hb = CreateSolidBrush (gc->background);
519 hp = CreatePen (PS_SOLID, 0, gc->foreground);
520 oldhb = SelectObject (hdc, hb);
521 oldhp = SelectObject (hdc, hp);
523 Rectangle (hdc, x, y, x + width, y + height);
525 SelectObject (hdc, oldhb);
526 SelectObject (hdc, oldhp);
527 DeleteObject (hb);
528 DeleteObject (hp);
531 /* Draw a filled rectangle at the specified position. */
532 void
533 w32_fill_rect (f, hdc, pix, lprect)
534 FRAME_PTR f;
535 HDC hdc;
536 COLORREF pix;
537 RECT * lprect;
539 HBRUSH hb;
541 hb = CreateSolidBrush (pix);
542 FillRect (hdc, lprect, hb);
543 DeleteObject (hb);
546 void
547 w32_clear_window (f)
548 FRAME_PTR f;
550 RECT rect;
551 HDC hdc = get_frame_dc (f);
553 /* Under certain conditions, this can be called at startup with
554 a console frame pointer before the GUI frame is created. An HDC
555 of 0 indicates this. */
556 if (hdc)
558 GetClientRect (FRAME_W32_WINDOW (f), &rect);
559 w32_clear_rect (f, hdc, &rect);
562 release_frame_dc (f, hdc);
566 /***********************************************************************
567 Starting and ending an update
568 ***********************************************************************/
570 /* Start an update of frame F. This function is installed as a hook
571 for update_begin, i.e. it is called when update_begin is called.
572 This function is called prior to calls to x_update_window_begin for
573 each window being updated. */
575 static void
576 x_update_begin (f)
577 struct frame *f;
579 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
581 if (! FRAME_W32_P (f))
582 return;
584 /* Regenerate display palette before drawing if list of requested
585 colors has changed. */
586 if (display_info->regen_palette)
588 w32_regenerate_palette (f);
589 display_info->regen_palette = FALSE;
594 /* Start update of window W. Set the global variable updated_window
595 to the window being updated and set output_cursor to the cursor
596 position of W. */
598 static void
599 x_update_window_begin (w)
600 struct window *w;
602 struct frame *f = XFRAME (WINDOW_FRAME (w));
603 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
605 /* Hide the system caret during an update. */
606 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
608 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
611 updated_window = w;
612 set_output_cursor (&w->cursor);
614 BLOCK_INPUT;
616 if (f == display_info->mouse_face_mouse_frame)
618 /* Don't do highlighting for mouse motion during the update. */
619 display_info->mouse_face_defer = 1;
621 /* If F needs to be redrawn, simply forget about any prior mouse
622 highlighting. */
623 if (FRAME_GARBAGED_P (f))
624 display_info->mouse_face_window = Qnil;
626 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
627 their mouse_face_p flag set, which means that they are always
628 unequal to rows in a desired matrix which never have that
629 flag set. So, rows containing mouse-face glyphs are never
630 scrolled, and we don't have to switch the mouse highlight off
631 here to prevent it from being scrolled. */
633 /* Can we tell that this update does not affect the window
634 where the mouse highlight is? If so, no need to turn off.
635 Likewise, don't do anything if the frame is garbaged;
636 in that case, the frame's current matrix that we would use
637 is all wrong, and we will redisplay that line anyway. */
638 if (!NILP (display_info->mouse_face_window)
639 && w == XWINDOW (display_info->mouse_face_window))
641 int i;
643 for (i = 0; i < w->desired_matrix->nrows; ++i)
644 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
645 break;
647 if (i < w->desired_matrix->nrows)
648 clear_mouse_face (display_info);
650 #endif /* 0 */
653 UNBLOCK_INPUT;
657 /* Draw a vertical window border to the right of window W if W doesn't
658 have vertical scroll bars. */
660 static void
661 x_draw_vertical_border (w)
662 struct window *w;
664 struct frame *f = XFRAME (WINDOW_FRAME (w));
666 /* Redraw borders between horizontally adjacent windows. Don't
667 do it for frames with vertical scroll bars because either the
668 right scroll bar of a window, or the left scroll bar of its
669 neighbor will suffice as a border. */
670 if (!WINDOW_RIGHTMOST_P (w)
671 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
673 RECT r;
674 HDC hdc;
676 window_box_edges (w, -1, (int *) &r.left, (int *) &r.top,
677 (int *) &r.right, (int *) &r.bottom);
678 r.left = r.right + FRAME_X_RIGHT_FRINGE_WIDTH (f);
679 r.right = r.left + 1;
680 r.bottom -= 1;
682 hdc = get_frame_dc (f);
683 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
684 release_frame_dc (f, hdc);
689 /* End update of window W (which is equal to updated_window).
691 Draw vertical borders between horizontally adjacent windows, and
692 display W's cursor if CURSOR_ON_P is non-zero.
694 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
695 glyphs in mouse-face were overwritten. In that case we have to
696 make sure that the mouse-highlight is properly redrawn.
698 W may be a menu bar pseudo-window in case we don't have X toolkit
699 support. Such windows don't have a cursor, so don't display it
700 here. */
702 static void
703 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
704 struct window *w;
705 int cursor_on_p, mouse_face_overwritten_p;
707 struct w32_display_info *dpyinfo
708 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
710 if (!w->pseudo_window_p)
712 BLOCK_INPUT;
714 if (cursor_on_p)
715 x_display_and_set_cursor (w, 1, output_cursor.hpos,
716 output_cursor.vpos,
717 output_cursor.x, output_cursor.y);
719 x_draw_vertical_border (w);
720 UNBLOCK_INPUT;
723 /* If a row with mouse-face was overwritten, arrange for
724 XTframe_up_to_date to redisplay the mouse highlight. */
725 if (mouse_face_overwritten_p)
727 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
728 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
729 dpyinfo->mouse_face_window = Qnil;
732 /* Unhide the caret. This won't actually show the cursor, unless it
733 was visible before the corresponding call to HideCaret in
734 x_update_window_begin. */
735 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
737 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
740 updated_window = NULL;
744 /* End update of frame F. This function is installed as a hook in
745 update_end. */
747 static void
748 x_update_end (f)
749 struct frame *f;
751 if (! FRAME_W32_P (f))
752 return;
754 /* Mouse highlight may be displayed again. */
755 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
759 /* This function is called from various places in xdisp.c whenever a
760 complete update has been performed. The global variable
761 updated_window is not available here. */
763 static void
764 w32_frame_up_to_date (f)
765 struct frame *f;
767 if (FRAME_W32_P (f))
769 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
770 if (dpyinfo->mouse_face_deferred_gc
771 || f == dpyinfo->mouse_face_mouse_frame)
773 BLOCK_INPUT;
774 if (dpyinfo->mouse_face_mouse_frame)
775 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
776 dpyinfo->mouse_face_mouse_x,
777 dpyinfo->mouse_face_mouse_y);
778 dpyinfo->mouse_face_deferred_gc = 0;
779 UNBLOCK_INPUT;
785 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
786 arrow bitmaps, or clear the fringes if no bitmaps are required
787 before DESIRED_ROW is made current. The window being updated is
788 found in updated_window. This function is called from
789 update_window_line only if it is known that there are differences
790 between bitmaps to be drawn between current row and DESIRED_ROW. */
792 static void
793 x_after_update_window_line (desired_row)
794 struct glyph_row *desired_row;
796 struct window *w = updated_window;
797 struct frame *f;
798 int width, height;
800 xassert (w);
802 if (!desired_row->mode_line_p && !w->pseudo_window_p)
804 BLOCK_INPUT;
805 x_draw_row_fringe_bitmaps (w, desired_row);
806 UNBLOCK_INPUT;
809 /* When a window has disappeared, make sure that no rest of
810 full-width rows stays visible in the internal border. Could
811 check here if updated_window is the leftmost/rightmost window,
812 but I guess it's not worth doing since vertically split windows
813 are almost never used, internal border is rarely set, and the
814 overhead is very small. */
815 if (windows_or_buffers_changed
816 && desired_row->full_width_p
817 && (f = XFRAME (w->frame),
818 width = FRAME_INTERNAL_BORDER_WIDTH (f),
819 width != 0)
820 && (height = desired_row->visible_height,
821 height > 0))
823 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
824 /* Internal border is drawn below the tool bar. */
825 if (WINDOWP (f->tool_bar_window)
826 && w == XWINDOW (f->tool_bar_window))
827 y -= width;
829 BLOCK_INPUT;
831 HDC hdc = get_frame_dc (f);
832 w32_clear_area (f, hdc, 0, y, width, height);
833 w32_clear_area (f, hdc, f->output_data.w32->pixel_width - width,
834 y, width, height);
835 release_frame_dc (f, hdc);
837 UNBLOCK_INPUT;
842 /* Draw the bitmap WHICH in one of the left or right fringes of
843 window W. ROW is the glyph row for which to display the bitmap; it
844 determines the vertical position at which the bitmap has to be
845 drawn. */
847 static void
848 w32_draw_fringe_bitmap (w, hdc, row, which, left_p)
849 struct window *w;
850 HDC hdc;
851 struct glyph_row *row;
852 enum fringe_bitmap_type which;
853 int left_p;
855 struct frame *f = XFRAME (WINDOW_FRAME (w));
856 HDC compat_hdc;
857 int x, y, wd, h, dy;
858 int b1, b2;
859 HBITMAP pixmap;
860 HANDLE horig_obj;
861 struct face *face;
863 /* Must clip because of partially visible lines. */
864 w32_clip_to_row (w, row, hdc, 1);
866 /* Convert row to frame coordinates. */
867 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
869 switch (which)
871 case NO_FRINGE_BITMAP:
872 wd = 0;
873 h = 0;
874 break;
876 case LEFT_TRUNCATION_BITMAP:
877 wd = left_width;
878 h = left_height;
879 pixmap = left_bmp;
880 break;
882 case OVERLAY_ARROW_BITMAP:
883 wd = ov_width;
884 h = ov_height;
885 pixmap = ov_bmp;
886 break;
888 case RIGHT_TRUNCATION_BITMAP:
889 wd = right_width;
890 h = right_height;
891 pixmap = right_bmp;
892 break;
894 case CONTINUED_LINE_BITMAP:
895 wd = continued_width;
896 h = continued_height;
897 pixmap = continued_bmp;
898 break;
900 case CONTINUATION_LINE_BITMAP:
901 wd = continuation_width;
902 h = continuation_height;
903 pixmap = continuation_bmp;
904 break;
906 case ZV_LINE_BITMAP:
907 wd = zv_width;
908 h = zv_height - (y % zv_period);
909 pixmap = zv_bmp;
910 break;
912 default:
913 abort ();
916 /* Clip bitmap if too high. */
917 if (h > row->height)
918 h = row->height;
920 /* Set dy to the offset in the row to start drawing the bitmap. */
921 dy = (row->height - h) / 2;
923 /* Draw the bitmap. */
924 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
925 PREPARE_FACE_FOR_DISPLAY (f, face);
927 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
928 the fringe. */
929 b1 = -1;
930 if (left_p)
932 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
933 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
934 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
935 - wd
936 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
937 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
939 /* If W has a vertical border to its left, don't draw over it. */
940 int border = ((XFASTINT (w->left) > 0
941 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
942 ? 1 : 0);
943 b1 = (window_box_left (w, -1)
944 - FRAME_X_LEFT_FRINGE_WIDTH (f)
945 + border);
946 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
949 else
951 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
952 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
953 x = (window_box_right (w, -1)
954 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
955 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
956 the fringe. */
957 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
959 b1 = window_box_right (w, -1);
960 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
964 if (b1 >= 0)
966 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
968 w32_fill_area (f, hdc, face->background,
970 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
971 row->y)),
973 row->visible_height);
976 if (which == NO_FRINGE_BITMAP)
977 return;
979 compat_hdc = CreateCompatibleDC (hdc);
980 SaveDC (hdc);
982 horig_obj = SelectObject (compat_hdc, pixmap);
983 SetTextColor (hdc, face->background);
984 SetBkColor (hdc, face->foreground);
986 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0,
987 (which == ZV_LINE_BITMAP ? (row->y % zv_period) : 0),
988 SRCCOPY);
990 SelectObject (compat_hdc, horig_obj);
991 DeleteDC (compat_hdc);
992 RestoreDC (hdc, -1);
996 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
997 function with input blocked. */
999 static void
1000 x_draw_row_fringe_bitmaps (w, row)
1001 struct window *w;
1002 struct glyph_row *row;
1004 struct frame *f = XFRAME (w->frame);
1005 enum fringe_bitmap_type bitmap;
1006 HDC hdc;
1008 xassert (interrupt_input_blocked);
1010 /* If row is completely invisible, because of vscrolling, we
1011 don't have to draw anything. */
1012 if (row->visible_height <= 0)
1013 return;
1015 hdc = get_frame_dc (f);
1017 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1019 /* Decide which bitmap to draw in the left fringe. */
1020 if (row->overlay_arrow_p)
1021 bitmap = OVERLAY_ARROW_BITMAP;
1022 else if (row->truncated_on_left_p)
1023 bitmap = LEFT_TRUNCATION_BITMAP;
1024 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1025 bitmap = CONTINUATION_LINE_BITMAP;
1026 else if (row->indicate_empty_line_p)
1027 bitmap = ZV_LINE_BITMAP;
1028 else
1029 bitmap = NO_FRINGE_BITMAP;
1031 w32_draw_fringe_bitmap (w, hdc, row, bitmap, 1);
1034 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1036 /* Decide which bitmap to draw in the right fringe. */
1037 if (row->truncated_on_right_p)
1038 bitmap = RIGHT_TRUNCATION_BITMAP;
1039 else if (row->continued_p)
1040 bitmap = CONTINUED_LINE_BITMAP;
1041 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1042 bitmap = ZV_LINE_BITMAP;
1043 else
1044 bitmap = NO_FRINGE_BITMAP;
1046 w32_draw_fringe_bitmap (w, hdc, row, bitmap, 0);
1049 release_frame_dc (f, hdc);
1053 /* This is called when starting Emacs and when restarting after
1054 suspend. When starting Emacs, no window is mapped. And nothing
1055 must be done to Emacs's own window if it is suspended (though that
1056 rarely happens). */
1058 static void
1059 w32_set_terminal_modes (void)
1063 /* This is called when exiting or suspending Emacs. Exiting will make
1064 the W32 windows go away, and suspending requires no action. */
1066 static void
1067 w32_reset_terminal_modes (void)
1073 /***********************************************************************
1074 Output Cursor
1075 ***********************************************************************/
1077 /* Set the global variable output_cursor to CURSOR. All cursor
1078 positions are relative to updated_window. */
1080 static void
1081 set_output_cursor (cursor)
1082 struct cursor_pos *cursor;
1084 output_cursor.hpos = cursor->hpos;
1085 output_cursor.vpos = cursor->vpos;
1086 output_cursor.x = cursor->x;
1087 output_cursor.y = cursor->y;
1091 /* Set a nominal cursor position.
1093 HPOS and VPOS are column/row positions in a window glyph matrix. X
1094 and Y are window text area relative pixel positions.
1096 If this is done during an update, updated_window will contain the
1097 window that is being updated and the position is the future output
1098 cursor position for that window. If updated_window is null, use
1099 selected_window and display the cursor at the given position. */
1101 static void
1102 w32_cursor_to (vpos, hpos, y, x)
1103 int vpos, hpos, y, x;
1105 struct window *w;
1107 /* If updated_window is not set, work on selected_window. */
1108 if (updated_window)
1109 w = updated_window;
1110 else
1111 w = XWINDOW (selected_window);
1113 /* Set the output cursor. */
1114 output_cursor.hpos = hpos;
1115 output_cursor.vpos = vpos;
1116 output_cursor.x = x;
1117 output_cursor.y = y;
1119 /* If not called as part of an update, really display the cursor.
1120 This will also set the cursor position of W. */
1121 if (updated_window == NULL)
1123 BLOCK_INPUT;
1124 x_display_cursor (w, 1, hpos, vpos, x, y);
1125 UNBLOCK_INPUT;
1131 /***********************************************************************
1132 Display Iterator
1133 ***********************************************************************/
1135 /* Function prototypes of this page. */
1137 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1138 struct glyph *,
1139 wchar_t *,
1140 int *));
1141 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1142 int, wchar_t *, int));
1143 static XCharStruct *w32_per_char_metric P_ ((XFontStruct *,
1144 wchar_t *,
1145 enum w32_char_font_type));
1146 static enum w32_char_font_type
1147 w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
1148 static void x_append_glyph P_ ((struct it *));
1149 static void x_append_composite_glyph P_ ((struct it *));
1150 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1151 int, int, double));
1152 static void x_produce_glyphs P_ ((struct it *));
1153 static void x_produce_image_glyph P_ ((struct it *it));
1156 /* Dealing with bits of wchar_t as if they were an XChar2B. */
1157 #define BUILD_WCHAR_T(byte1, byte2) \
1158 ((wchar_t)((((byte1) & 0x00ff) << 8) | ((byte2) & 0x00ff)))
1161 #define BYTE1(ch) \
1162 (((ch) & 0xff00) >> 8)
1164 #define BYTE2(ch) \
1165 ((ch) & 0x00ff)
1168 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
1169 If CHAR2B is not contained in FONT, the font's default character
1170 metric is returned. */
1172 static int
1173 w32_bdf_per_char_metric (font, char2b, dim, pcm)
1174 XFontStruct *font;
1175 wchar_t *char2b;
1176 int dim;
1177 XCharStruct * pcm;
1179 glyph_metric * bdf_metric;
1180 char buf[2];
1182 if (dim == 1)
1183 buf[0] = (char)(*char2b);
1184 else
1186 buf[0] = BYTE1 (*char2b);
1187 buf[1] = BYTE2 (*char2b);
1190 bdf_metric = w32_BDF_TextMetric (font->bdf, buf, dim);
1192 if (bdf_metric)
1194 pcm->width = bdf_metric->dwidth;
1195 pcm->lbearing = bdf_metric->bbox;
1196 pcm->rbearing = bdf_metric->dwidth
1197 - (bdf_metric->bbox + bdf_metric->bbw);
1198 pcm->ascent = bdf_metric->bboy + bdf_metric->bbh;
1199 pcm->descent = -bdf_metric->bboy;
1201 return 1;
1203 return 0;
1207 static int
1208 w32_native_per_char_metric (font, char2b, font_type, pcm)
1209 XFontStruct *font;
1210 wchar_t *char2b;
1211 enum w32_char_font_type font_type;
1212 XCharStruct * pcm;
1214 HDC hdc = GetDC (NULL);
1215 HFONT old_font;
1216 BOOL retval = FALSE;
1218 xassert (font && char2b);
1219 xassert (font->hfont);
1220 xassert (font_type == UNICODE_FONT || font_type == ANSI_FONT);
1222 old_font = SelectObject (hdc, font->hfont);
1224 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
1226 ABC char_widths;
1228 if (font_type == UNICODE_FONT)
1229 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
1230 else
1231 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
1233 if (retval)
1235 #if 0
1236 /* Disabled until we can find a way to get the right results
1237 on all versions of Windows. */
1239 /* Don't trust the ABC widths. For synthesized fonts they are
1240 wrong, and so is the result of GetCharWidth()! */
1241 int real_width;
1242 GetCharWidth (hdc, *char2b, *char2b, &real_width);
1243 #endif
1244 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
1245 #if 0
1246 /* As far as I can tell, this is the best way to determine what
1247 ExtTextOut will do with the broken font. */
1248 if (pcm->width != real_width)
1249 pcm->width = (pcm->width + real_width) / 2;
1250 #endif
1251 pcm->lbearing = char_widths.abcA;
1252 pcm->rbearing = char_widths.abcA + char_widths.abcB;
1253 pcm->ascent = FONT_BASE (font);
1254 pcm->descent = FONT_DESCENT (font);
1258 if (!retval)
1260 /* Either font is not a True-type font, or GetCharABCWidthsW
1261 failed (it is not supported on Windows 9x for instance), so we
1262 can't determine the full info we would like. All is not lost
1263 though - we can call GetTextExtentPoint32 to get rbearing and
1264 deduce width based on the font's per-string overhang. lbearing
1265 is assumed to be zero. */
1267 /* TODO: Some Thai characters (and other composites if Windows
1268 supports them) do have lbearing, and report their total width
1269 as zero. Need some way of handling them when
1270 GetCharABCWidthsW fails. */
1271 SIZE sz;
1273 if (font_type == UNICODE_FONT)
1274 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
1275 else
1276 retval = GetTextExtentPoint32A (hdc, (char*)char2b, 1, &sz);
1278 if (retval)
1280 pcm->width = sz.cx - font->tm.tmOverhang;
1281 pcm->rbearing = sz.cx;
1282 pcm->lbearing = 0;
1283 pcm->ascent = FONT_BASE (font);
1284 pcm->descent = FONT_DESCENT (font);
1289 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1291 retval = FALSE;
1294 SelectObject (hdc, old_font);
1295 ReleaseDC (NULL, hdc);
1297 return retval;
1301 static XCharStruct *
1302 w32_per_char_metric (font, char2b, font_type)
1303 XFontStruct *font;
1304 wchar_t *char2b;
1305 enum w32_char_font_type font_type;
1307 /* The result metric information. */
1308 XCharStruct *pcm;
1309 BOOL retval;
1311 xassert (font && char2b);
1312 xassert (font_type != UNKNOWN_FONT);
1314 /* Handle the common cases quickly. */
1315 if (!font->bdf && font->per_char == NULL)
1316 /* TODO: determine whether char2b exists in font? */
1317 return &font->max_bounds;
1318 else if (!font->bdf && *char2b < 128)
1319 return &font->per_char[*char2b];
1321 pcm = &font->scratch;
1323 if (font_type == BDF_1D_FONT)
1324 retval = w32_bdf_per_char_metric (font, char2b, 1, pcm);
1325 else if (font_type == BDF_2D_FONT)
1326 retval = w32_bdf_per_char_metric (font, char2b, 2, pcm);
1327 else
1328 retval = w32_native_per_char_metric (font, char2b, font_type, pcm);
1330 if (retval)
1331 return pcm;
1333 return NULL;
1336 void
1337 w32_cache_char_metrics (font)
1338 XFontStruct *font;
1340 wchar_t char2b = L'x';
1342 /* Cache char metrics for the common cases. */
1343 if (font->bdf)
1345 /* TODO: determine whether font is fixed-pitch. */
1346 if (!w32_bdf_per_char_metric (font, &char2b, 1, &font->max_bounds))
1348 /* Use the font width and height as max bounds, as not all BDF
1349 fonts contain the letter 'x'. */
1350 font->max_bounds.width = FONT_MAX_WIDTH (font);
1351 font->max_bounds.lbearing = -font->bdf->llx;
1352 font->max_bounds.rbearing = FONT_MAX_WIDTH (font) - font->bdf->urx;
1353 font->max_bounds.ascent = FONT_BASE (font);
1354 font->max_bounds.descent = FONT_DESCENT (font);
1357 else
1359 if (((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH) != 0)
1360 /* Some fonts (eg DBCS fonts) are marked as fixed width even
1361 though they contain characters of different widths. */
1362 || (font->tm.tmMaxCharWidth != font->tm.tmAveCharWidth))
1364 /* Font is not fixed pitch, so cache per_char info for the
1365 ASCII characters. It would be much more work, and probably
1366 not worth it, to cache other chars, since we may change
1367 between using Unicode and ANSI text drawing functions at
1368 run-time. */
1369 int i;
1371 font->per_char = xmalloc (128 * sizeof(XCharStruct));
1372 for (i = 0; i < 128; i++)
1374 char2b = i;
1375 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1376 &font->per_char[i]);
1379 else
1380 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1381 &font->max_bounds);
1386 /* Determine if a font is double byte. */
1387 int w32_font_is_double_byte (XFontStruct *font)
1389 return font->double_byte_p;
1393 static BOOL
1394 w32_use_unicode_for_codepage (codepage)
1395 int codepage;
1397 /* If the current codepage is supported, use Unicode for output. */
1398 return (w32_enable_unicode_output
1399 && codepage != CP_8BIT
1400 && (codepage == CP_UNICODE || IsValidCodePage (codepage)));
1403 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1404 the two-byte form of C. Encoding is returned in *CHAR2B. */
1406 static INLINE enum w32_char_font_type
1407 w32_encode_char (c, char2b, font_info, two_byte_p)
1408 int c;
1409 wchar_t *char2b;
1410 struct font_info *font_info;
1411 int * two_byte_p;
1413 int charset = CHAR_CHARSET (c);
1414 int codepage;
1415 int unicode_p = 0;
1417 XFontStruct *font = font_info->font;
1419 xassert (two_byte_p);
1421 *two_byte_p = w32_font_is_double_byte (font);
1423 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1424 This may be either a program in a special encoder language or a
1425 fixed encoding. */
1426 if (font_info->font_encoder)
1428 /* It's a program. */
1429 struct ccl_program *ccl = font_info->font_encoder;
1431 if (CHARSET_DIMENSION (charset) == 1)
1433 ccl->reg[0] = charset;
1434 ccl->reg[1] = BYTE2 (*char2b);
1436 else
1438 ccl->reg[0] = charset;
1439 ccl->reg[1] = BYTE1 (*char2b);
1440 ccl->reg[2] = BYTE2 (*char2b);
1443 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1445 /* We assume that MSBs are appropriately set/reset by CCL
1446 program. */
1447 if (!*two_byte_p) /* 1-byte font */
1448 *char2b = BUILD_WCHAR_T (0, ccl->reg[1]);
1449 else
1450 *char2b = BUILD_WCHAR_T (ccl->reg[1], ccl->reg[2]);
1452 else if (font_info->encoding[charset])
1454 /* Fixed encoding scheme. See fontset.h for the meaning of the
1455 encoding numbers. */
1456 int enc = font_info->encoding[charset];
1458 if ((enc == 1 || enc == 2)
1459 && CHARSET_DIMENSION (charset) == 2)
1460 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b) | 0x80, BYTE2 (*char2b));
1462 if (enc == 1 || enc == 3
1463 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1464 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b), BYTE2 (*char2b) | 0x80);
1465 else if (enc == 4)
1467 int sjis1, sjis2;
1469 ENCODE_SJIS (BYTE1 (*char2b), BYTE2 (*char2b),
1470 sjis1, sjis2);
1471 *char2b = BUILD_WCHAR_T (sjis1, sjis2);
1474 codepage = font_info->codepage;
1476 /* If charset is not ASCII or Latin-1, may need to move it into
1477 Unicode space. */
1478 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
1479 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1
1480 && charset != CHARSET_8_BIT_CONTROL && charset != CHARSET_8_BIT_GRAPHIC)
1482 char temp[3];
1483 temp[0] = BYTE1 (*char2b);
1484 temp[1] = BYTE2 (*char2b);
1485 temp[2] = '\0';
1486 if (codepage != CP_UNICODE)
1488 if (temp[0])
1489 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1490 else
1491 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1493 unicode_p = 1;
1494 *two_byte_p = 1;
1496 if (!font)
1497 return UNKNOWN_FONT;
1498 else if (font->bdf && CHARSET_DIMENSION (charset) == 1)
1499 return BDF_1D_FONT;
1500 else if (font->bdf)
1501 return BDF_2D_FONT;
1502 else if (unicode_p)
1503 return UNICODE_FONT;
1504 else
1505 return ANSI_FONT;
1509 /* Get face and two-byte form of character C in face FACE_ID on frame
1510 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1511 means we want to display multibyte text. Value is a pointer to a
1512 realized face that is ready for display. */
1514 static INLINE struct face *
1515 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1516 struct frame *f;
1517 int c, face_id;
1518 wchar_t *char2b;
1519 int multibyte_p;
1521 struct face *face = FACE_FROM_ID (f, face_id);
1523 if (!multibyte_p)
1525 /* Unibyte case. We don't have to encode, but we have to make
1526 sure to use a face suitable for unibyte. */
1527 *char2b = BUILD_WCHAR_T (0, c);
1528 face_id = FACE_FOR_CHAR (f, face, c);
1529 face = FACE_FROM_ID (f, face_id);
1531 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1533 /* Case of ASCII in a face known to fit ASCII. */
1534 *char2b = BUILD_WCHAR_T (0, c);
1536 else
1538 int c1, c2, charset;
1540 /* Split characters into bytes. If c2 is -1 afterwards, C is
1541 really a one-byte character so that byte1 is zero. */
1542 SPLIT_CHAR (c, charset, c1, c2);
1543 if (c2 > 0)
1544 *char2b = BUILD_WCHAR_T (c1, c2);
1545 else
1546 *char2b = BUILD_WCHAR_T (0, c1);
1548 /* Maybe encode the character in *CHAR2B. */
1549 if (face->font != NULL)
1551 struct font_info *font_info
1552 = FONT_INFO_FROM_ID (f, face->font_info_id);
1553 if (font_info)
1554 w32_encode_char (c, char2b, font_info, &multibyte_p);
1558 /* Make sure X resources of the face are allocated. */
1559 xassert (face != NULL);
1560 PREPARE_FACE_FOR_DISPLAY (f, face);
1562 return face;
1566 /* Get face and two-byte form of character glyph GLYPH on frame F.
1567 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1568 a pointer to a realized face that is ready for display. */
1570 static INLINE struct face *
1571 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1572 struct frame *f;
1573 struct glyph *glyph;
1574 wchar_t *char2b;
1575 int *two_byte_p;
1577 struct face *face;
1578 int dummy = 0;
1580 xassert (glyph->type == CHAR_GLYPH);
1581 face = FACE_FROM_ID (f, glyph->face_id);
1583 if (two_byte_p)
1584 *two_byte_p = 0;
1585 else
1586 two_byte_p = &dummy;
1588 if (!glyph->multibyte_p)
1590 /* Unibyte case. We don't have to encode, but we have to make
1591 sure to use a face suitable for unibyte. */
1592 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1594 else if (glyph->u.ch < 128
1595 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1597 /* Case of ASCII in a face known to fit ASCII. */
1598 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1600 else
1602 int c1, c2, charset;
1604 /* Split characters into bytes. If c2 is -1 afterwards, C is
1605 really a one-byte character so that byte1 is zero. */
1606 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1607 if (c2 > 0)
1608 *char2b = BUILD_WCHAR_T (c1, c2);
1609 else
1610 *char2b = BUILD_WCHAR_T (0, c1);
1612 /* Maybe encode the character in *CHAR2B. */
1613 if (charset != CHARSET_ASCII)
1615 struct font_info *font_info
1616 = FONT_INFO_FROM_ID (f, face->font_info_id);
1617 if (font_info)
1619 glyph->w32_font_type
1620 = w32_encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
1625 /* Make sure X resources of the face are allocated. */
1626 xassert (face != NULL);
1627 PREPARE_FACE_FOR_DISPLAY (f, face);
1628 return face;
1632 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1633 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1635 static INLINE void
1636 x_append_glyph (it)
1637 struct it *it;
1639 struct glyph *glyph;
1640 enum glyph_row_area area = it->area;
1642 xassert (it->glyph_row);
1643 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1645 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1646 if (glyph < it->glyph_row->glyphs[area + 1])
1648 glyph->charpos = CHARPOS (it->position);
1649 glyph->object = it->object;
1650 glyph->pixel_width = it->pixel_width;
1651 glyph->voffset = it->voffset;
1652 glyph->type = CHAR_GLYPH;
1653 glyph->multibyte_p = it->multibyte_p;
1654 glyph->left_box_line_p = it->start_of_box_run_p;
1655 glyph->right_box_line_p = it->end_of_box_run_p;
1656 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1657 || it->phys_descent > it->descent);
1658 glyph->padding_p = 0;
1659 glyph->glyph_not_available_p = it->glyph_not_available_p;
1660 glyph->face_id = it->face_id;
1661 glyph->u.ch = it->char_to_display;
1662 glyph->w32_font_type = UNKNOWN_FONT;
1663 ++it->glyph_row->used[area];
1667 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1668 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1670 static INLINE void
1671 x_append_composite_glyph (it)
1672 struct it *it;
1674 struct glyph *glyph;
1675 enum glyph_row_area area = it->area;
1677 xassert (it->glyph_row);
1679 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1680 if (glyph < it->glyph_row->glyphs[area + 1])
1682 glyph->charpos = CHARPOS (it->position);
1683 glyph->object = it->object;
1684 glyph->pixel_width = it->pixel_width;
1685 glyph->voffset = it->voffset;
1686 glyph->type = COMPOSITE_GLYPH;
1687 glyph->multibyte_p = it->multibyte_p;
1688 glyph->left_box_line_p = it->start_of_box_run_p;
1689 glyph->right_box_line_p = it->end_of_box_run_p;
1690 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1691 || it->phys_descent > it->descent);
1692 glyph->padding_p = 0;
1693 glyph->glyph_not_available_p = 0;
1694 glyph->face_id = it->face_id;
1695 glyph->u.cmp_id = it->cmp_id;
1696 glyph->w32_font_type = UNKNOWN_FONT;
1697 ++it->glyph_row->used[area];
1702 /* Change IT->ascent and IT->height according to the setting of
1703 IT->voffset. */
1705 static INLINE void
1706 take_vertical_position_into_account (it)
1707 struct it *it;
1709 if (it->voffset)
1711 if (it->voffset < 0)
1712 /* Increase the ascent so that we can display the text higher
1713 in the line. */
1714 it->ascent += abs (it->voffset);
1715 else
1716 /* Increase the descent so that we can display the text lower
1717 in the line. */
1718 it->descent += it->voffset;
1723 /* Produce glyphs/get display metrics for the image IT is loaded with.
1724 See the description of struct display_iterator in dispextern.h for
1725 an overview of struct display_iterator. */
1727 static void
1728 x_produce_image_glyph (it)
1729 struct it *it;
1731 struct image *img;
1732 struct face *face;
1734 xassert (it->what == IT_IMAGE);
1736 face = FACE_FROM_ID (it->f, it->face_id);
1737 img = IMAGE_FROM_ID (it->f, it->image_id);
1738 xassert (img);
1740 /* Make sure X resources of the face and image are loaded. */
1741 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1742 prepare_image_for_display (it->f, img);
1744 it->ascent = it->phys_ascent = image_ascent (img, face);
1745 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1746 it->pixel_width = img->width + 2 * img->hmargin;
1748 it->nglyphs = 1;
1750 if (face->box != FACE_NO_BOX)
1752 if (face->box_line_width > 0)
1754 it->ascent += face->box_line_width;
1755 it->descent += face->box_line_width;
1758 if (it->start_of_box_run_p)
1759 it->pixel_width += abs (face->box_line_width);
1760 if (it->end_of_box_run_p)
1761 it->pixel_width += abs (face->box_line_width);
1764 take_vertical_position_into_account (it);
1766 if (it->glyph_row)
1768 struct glyph *glyph;
1769 enum glyph_row_area area = it->area;
1771 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1772 if (glyph < it->glyph_row->glyphs[area + 1])
1774 glyph->charpos = CHARPOS (it->position);
1775 glyph->object = it->object;
1776 glyph->pixel_width = it->pixel_width;
1777 glyph->voffset = it->voffset;
1778 glyph->type = IMAGE_GLYPH;
1779 glyph->multibyte_p = it->multibyte_p;
1780 glyph->left_box_line_p = it->start_of_box_run_p;
1781 glyph->right_box_line_p = it->end_of_box_run_p;
1782 glyph->overlaps_vertically_p = 0;
1783 glyph->padding_p = 0;
1784 glyph->glyph_not_available_p = 0;
1785 glyph->face_id = it->face_id;
1786 glyph->u.img_id = img->id;
1787 glyph->w32_font_type = UNKNOWN_FONT;
1788 ++it->glyph_row->used[area];
1794 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1795 of the glyph, WIDTH and HEIGHT are the width and height of the
1796 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1797 ascent of the glyph (0 <= ASCENT <= 1). */
1799 static void
1800 x_append_stretch_glyph (it, object, width, height, ascent)
1801 struct it *it;
1802 Lisp_Object object;
1803 int width, height;
1804 double ascent;
1806 struct glyph *glyph;
1807 enum glyph_row_area area = it->area;
1809 xassert (ascent >= 0 && ascent <= 1);
1811 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1812 if (glyph < it->glyph_row->glyphs[area + 1])
1814 glyph->charpos = CHARPOS (it->position);
1815 glyph->object = object;
1816 glyph->pixel_width = width;
1817 glyph->voffset = it->voffset;
1818 glyph->type = STRETCH_GLYPH;
1819 glyph->multibyte_p = it->multibyte_p;
1820 glyph->left_box_line_p = it->start_of_box_run_p;
1821 glyph->right_box_line_p = it->end_of_box_run_p;
1822 glyph->overlaps_vertically_p = 0;
1823 glyph->padding_p = 0;
1824 glyph->glyph_not_available_p = 0;
1825 glyph->face_id = it->face_id;
1826 glyph->u.stretch.ascent = height * ascent;
1827 glyph->u.stretch.height = height;
1828 glyph->w32_font_type = UNKNOWN_FONT;
1829 ++it->glyph_row->used[area];
1834 /* Produce a stretch glyph for iterator IT. IT->object is the value
1835 of the glyph property displayed. The value must be a list
1836 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1837 being recognized:
1839 1. `:width WIDTH' specifies that the space should be WIDTH *
1840 canonical char width wide. WIDTH may be an integer or floating
1841 point number.
1843 2. `:relative-width FACTOR' specifies that the width of the stretch
1844 should be computed from the width of the first character having the
1845 `glyph' property, and should be FACTOR times that width.
1847 3. `:align-to HPOS' specifies that the space should be wide enough
1848 to reach HPOS, a value in canonical character units.
1850 Exactly one of the above pairs must be present.
1852 4. `:height HEIGHT' specifies that the height of the stretch produced
1853 should be HEIGHT, measured in canonical character units.
1855 5. `:relative-height FACTOR' specifies that the height of the
1856 stretch should be FACTOR times the height of the characters having
1857 the glyph property.
1859 Either none or exactly one of 4 or 5 must be present.
1861 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1862 of the stretch should be used for the ascent of the stretch.
1863 ASCENT must be in the range 0 <= ASCENT <= 100. */
1865 #define NUMVAL(X) \
1866 ((INTEGERP (X) || FLOATP (X)) \
1867 ? XFLOATINT (X) \
1868 : - 1)
1871 static void
1872 x_produce_stretch_glyph (it)
1873 struct it *it;
1875 /* (space :width WIDTH :height HEIGHT. */
1876 #if GLYPH_DEBUG
1877 extern Lisp_Object Qspace;
1878 #endif
1879 extern Lisp_Object QCwidth, QCheight, QCascent;
1880 extern Lisp_Object QCrelative_width, QCrelative_height;
1881 extern Lisp_Object QCalign_to;
1882 Lisp_Object prop, plist;
1883 double width = 0, height = 0, ascent = 0;
1884 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1885 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1887 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1889 /* List should start with `space'. */
1890 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1891 plist = XCDR (it->object);
1893 /* Compute the width of the stretch. */
1894 if (prop = Fplist_get (plist, QCwidth),
1895 NUMVAL (prop) > 0)
1896 /* Absolute width `:width WIDTH' specified and valid. */
1897 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1898 else if (prop = Fplist_get (plist, QCrelative_width),
1899 NUMVAL (prop) > 0)
1901 /* Relative width `:relative-width FACTOR' specified and valid.
1902 Compute the width of the characters having the `glyph'
1903 property. */
1904 struct it it2;
1905 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1907 it2 = *it;
1908 if (it->multibyte_p)
1910 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1911 - IT_BYTEPOS (*it));
1912 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1914 else
1915 it2.c = *p, it2.len = 1;
1917 it2.glyph_row = NULL;
1918 it2.what = IT_CHARACTER;
1919 x_produce_glyphs (&it2);
1920 width = NUMVAL (prop) * it2.pixel_width;
1922 else if (prop = Fplist_get (plist, QCalign_to),
1923 NUMVAL (prop) > 0)
1924 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1925 else
1926 /* Nothing specified -> width defaults to canonical char width. */
1927 width = CANON_X_UNIT (it->f);
1929 /* Compute height. */
1930 if (prop = Fplist_get (plist, QCheight),
1931 NUMVAL (prop) > 0)
1932 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1933 else if (prop = Fplist_get (plist, QCrelative_height),
1934 NUMVAL (prop) > 0)
1935 height = FONT_HEIGHT (font) * NUMVAL (prop);
1936 else
1937 height = FONT_HEIGHT (font);
1939 /* Compute percentage of height used for ascent. If
1940 `:ascent ASCENT' is present and valid, use that. Otherwise,
1941 derive the ascent from the font in use. */
1942 if (prop = Fplist_get (plist, QCascent),
1943 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1944 ascent = NUMVAL (prop) / 100.0;
1945 else
1946 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
1948 if (width <= 0)
1949 width = 1;
1950 if (height <= 0)
1951 height = 1;
1953 if (it->glyph_row)
1955 Lisp_Object object = it->stack[it->sp - 1].string;
1956 if (!STRINGP (object))
1957 object = it->w->buffer;
1958 x_append_stretch_glyph (it, object, width, height, ascent);
1961 it->pixel_width = width;
1962 it->ascent = it->phys_ascent = height * ascent;
1963 it->descent = it->phys_descent = height - it->ascent;
1964 it->nglyphs = 1;
1966 if (face->box != FACE_NO_BOX)
1968 if (face->box_line_width > 0)
1970 it->ascent += face->box_line_width;
1971 it->descent += face->box_line_width;
1974 if (it->start_of_box_run_p)
1975 it->pixel_width += abs (face->box_line_width);
1976 if (it->end_of_box_run_p)
1977 it->pixel_width += abs (face->box_line_width);
1980 take_vertical_position_into_account (it);
1983 /* Return proper value to be used as baseline offset of font that has
1984 ASCENT and DESCENT to draw characters by the font at the vertical
1985 center of the line of frame F.
1987 Here, out task is to find the value of BOFF in the following figure;
1989 -------------------------+-----------+-
1990 -+-+---------+-+ | |
1991 | | | | | |
1992 | | | | F_ASCENT F_HEIGHT
1993 | | | ASCENT | |
1994 HEIGHT | | | | |
1995 | | |-|-+------+-----------|------- baseline
1996 | | | | BOFF | |
1997 | |---------|-+-+ | |
1998 | | | DESCENT | |
1999 -+-+---------+-+ F_DESCENT |
2000 -------------------------+-----------+-
2002 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
2003 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
2004 DESCENT = FONT->descent
2005 HEIGHT = FONT_HEIGHT (FONT)
2006 F_DESCENT = (F->output_data.x->font->descent
2007 - F->output_data.x->baseline_offset)
2008 F_HEIGHT = FRAME_LINE_HEIGHT (F)
2011 #define VCENTER_BASELINE_OFFSET(FONT, F) \
2012 (FONT_DESCENT (FONT) \
2013 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
2014 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
2015 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
2017 /* Produce glyphs/get display metrics for the display element IT is
2018 loaded with. See the description of struct display_iterator in
2019 dispextern.h for an overview of struct display_iterator. */
2021 static void
2022 x_produce_glyphs (it)
2023 struct it *it;
2025 it->glyph_not_available_p = 0;
2027 if (it->what == IT_CHARACTER)
2029 wchar_t char2b;
2030 XFontStruct *font;
2031 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2032 XCharStruct *pcm;
2033 int font_not_found_p;
2034 struct font_info *font_info;
2035 int boff; /* baseline offset */
2036 /* We may change it->multibyte_p upon unibyte<->multibyte
2037 conversion. So, save the current value now and restore it
2038 later.
2040 Note: It seems that we don't have to record multibyte_p in
2041 struct glyph because the character code itself tells if or
2042 not the character is multibyte. Thus, in the future, we must
2043 consider eliminating the field `multibyte_p' in the struct
2044 glyph.
2046 int saved_multibyte_p = it->multibyte_p;
2048 /* Maybe translate single-byte characters to multibyte, or the
2049 other way. */
2050 it->char_to_display = it->c;
2051 if (!ASCII_BYTE_P (it->c))
2053 if (unibyte_display_via_language_environment
2054 && SINGLE_BYTE_CHAR_P (it->c)
2055 && (it->c >= 0240
2056 || !NILP (Vnonascii_translation_table)))
2058 it->char_to_display = unibyte_char_to_multibyte (it->c);
2059 it->multibyte_p = 1;
2060 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2061 face = FACE_FROM_ID (it->f, it->face_id);
2063 else if (!SINGLE_BYTE_CHAR_P (it->c)
2064 && !it->multibyte_p)
2066 it->multibyte_p = 1;
2067 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2068 face = FACE_FROM_ID (it->f, it->face_id);
2072 /* Get font to use. Encode IT->char_to_display. */
2073 x_get_char_face_and_encoding (it->f, it->char_to_display,
2074 it->face_id, &char2b,
2075 it->multibyte_p);
2076 font = face->font;
2078 /* When no suitable font found, use the default font. */
2079 font_not_found_p = font == NULL;
2080 if (font_not_found_p)
2082 font = FRAME_FONT (it->f);
2083 boff = it->f->output_data.w32->baseline_offset;
2084 font_info = NULL;
2086 else
2088 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2089 boff = font_info->baseline_offset;
2090 if (font_info->vertical_centering)
2091 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2094 if (it->char_to_display >= ' '
2095 && (!it->multibyte_p || it->char_to_display < 128))
2097 /* Either unibyte or ASCII. */
2098 int stretched_p;
2100 it->nglyphs = 1;
2102 pcm = w32_per_char_metric (font, &char2b,
2103 font->bdf ? BDF_1D_FONT : ANSI_FONT);
2104 it->ascent = FONT_BASE (font) + boff;
2105 it->descent = FONT_DESCENT (font) - boff;
2107 if (pcm)
2109 it->phys_ascent = pcm->ascent + boff;
2110 it->phys_descent = pcm->descent - boff;
2111 it->pixel_width = pcm->width;
2113 else
2115 it->glyph_not_available_p = 1;
2116 it->phys_ascent = FONT_BASE (font) + boff;
2117 it->phys_descent = FONT_DESCENT (font) - boff;
2118 it->pixel_width = FONT_WIDTH (font);
2121 /* If this is a space inside a region of text with
2122 `space-width' property, change its width. */
2123 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
2124 if (stretched_p)
2125 it->pixel_width *= XFLOATINT (it->space_width);
2127 /* If face has a box, add the box thickness to the character
2128 height. If character has a box line to the left and/or
2129 right, add the box line width to the character's width. */
2130 if (face->box != FACE_NO_BOX)
2132 int thick = face->box_line_width;
2134 if (thick > 0)
2136 it->ascent += thick;
2137 it->descent += thick;
2139 else
2140 thick = -thick;
2142 if (it->start_of_box_run_p)
2143 it->pixel_width += thick;
2144 if (it->end_of_box_run_p)
2145 it->pixel_width += thick;
2148 /* If face has an overline, add the height of the overline
2149 (1 pixel) and a 1 pixel margin to the character height. */
2150 if (face->overline_p)
2151 it->ascent += 2;
2153 take_vertical_position_into_account (it);
2155 /* If we have to actually produce glyphs, do it. */
2156 if (it->glyph_row)
2158 if (stretched_p)
2160 /* Translate a space with a `space-width' property
2161 into a stretch glyph. */
2162 double ascent = (double) FONT_BASE (font)
2163 / FONT_HEIGHT (font);
2164 x_append_stretch_glyph (it, it->object, it->pixel_width,
2165 it->ascent + it->descent, ascent);
2167 else
2168 x_append_glyph (it);
2170 /* If characters with lbearing or rbearing are displayed
2171 in this line, record that fact in a flag of the
2172 glyph row. This is used to optimize X output code. */
2173 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
2174 it->glyph_row->contains_overlapping_glyphs_p = 1;
2177 else if (it->char_to_display == '\n')
2179 /* A newline has no width but we need the height of the line. */
2180 it->pixel_width = 0;
2181 it->nglyphs = 0;
2182 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2183 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2185 if (face->box != FACE_NO_BOX
2186 && face->box_line_width > 0)
2188 it->ascent += face->box_line_width;
2189 it->descent += face->box_line_width;
2192 else if (it->char_to_display == '\t')
2194 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
2195 int x = it->current_x + it->continuation_lines_width;
2196 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2198 /* If the distance from the current position to the next tab
2199 stop is less than a canonical character width, use the
2200 tab stop after that. */
2201 if (next_tab_x - x < CANON_X_UNIT (it->f))
2202 next_tab_x += tab_width;
2204 it->pixel_width = next_tab_x - x;
2205 it->nglyphs = 1;
2206 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2207 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2209 if (it->glyph_row)
2211 double ascent = (double) it->ascent / (it->ascent + it->descent);
2212 x_append_stretch_glyph (it, it->object, it->pixel_width,
2213 it->ascent + it->descent, ascent);
2216 else
2218 /* A multi-byte character.
2219 If we found a font, this font should give us the right
2220 metrics. If we didn't find a font, use the frame's
2221 default font and calculate the width of the character
2222 from the charset width; this is what old redisplay code
2223 did. */
2224 enum w32_char_font_type type;
2226 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2227 type = BDF_1D_FONT;
2228 else if (font->bdf)
2229 type = BDF_2D_FONT;
2230 else
2231 type = UNICODE_FONT;
2233 pcm = w32_per_char_metric (font, &char2b, type);
2235 if (font_not_found_p || !pcm)
2237 int charset = CHAR_CHARSET (it->char_to_display);
2239 it->glyph_not_available_p = 1;
2240 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2241 * CHARSET_WIDTH (charset));
2242 it->phys_ascent = FONT_BASE (font) + boff;
2243 it->phys_descent = FONT_DESCENT (font) - boff;
2245 else
2247 it->pixel_width = pcm->width;
2248 it->phys_ascent = pcm->ascent + boff;
2249 it->phys_descent = pcm->descent - boff;
2250 if (it->glyph_row
2251 && (pcm->lbearing < 0
2252 || pcm->rbearing > pcm->width))
2253 it->glyph_row->contains_overlapping_glyphs_p = 1;
2255 it->nglyphs = 1;
2256 it->ascent = FONT_BASE (font) + boff;
2257 it->descent = FONT_DESCENT (font) - boff;
2258 if (face->box != FACE_NO_BOX)
2260 int thick = face->box_line_width;
2262 if (thick > 0)
2264 it->ascent += thick;
2265 it->descent += thick;
2267 else
2268 thick = - thick;
2270 if (it->start_of_box_run_p)
2271 it->pixel_width += thick;
2272 if (it->end_of_box_run_p)
2273 it->pixel_width += thick;
2276 /* If face has an overline, add the height of the overline
2277 (1 pixel) and a 1 pixel margin to the character height. */
2278 if (face->overline_p)
2279 it->ascent += 2;
2281 take_vertical_position_into_account (it);
2283 if (it->glyph_row)
2284 x_append_glyph (it);
2286 it->multibyte_p = saved_multibyte_p;
2288 else if (it->what == IT_COMPOSITION)
2290 /* Note: A composition is represented as one glyph in the
2291 glyph matrix. There are no padding glyphs. */
2292 wchar_t char2b;
2293 XFontStruct *font;
2294 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2295 XCharStruct *pcm;
2296 int font_not_found_p;
2297 struct font_info *font_info;
2298 int boff; /* baseline offset */
2299 struct composition *cmp = composition_table[it->cmp_id];
2301 /* Maybe translate single-byte characters to multibyte. */
2302 it->char_to_display = it->c;
2303 if (unibyte_display_via_language_environment
2304 && SINGLE_BYTE_CHAR_P (it->c)
2305 && (it->c >= 0240
2306 || (it->c >= 0200
2307 && !NILP (Vnonascii_translation_table))))
2309 it->char_to_display = unibyte_char_to_multibyte (it->c);
2312 /* Get face and font to use. Encode IT->char_to_display. */
2313 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2314 face = FACE_FROM_ID (it->f, it->face_id);
2315 x_get_char_face_and_encoding (it->f, it->char_to_display,
2316 it->face_id, &char2b, it->multibyte_p);
2317 font = face->font;
2319 /* When no suitable font found, use the default font. */
2320 font_not_found_p = font == NULL;
2321 if (font_not_found_p)
2323 font = FRAME_FONT (it->f);
2324 boff = it->f->output_data.w32->baseline_offset;
2325 font_info = NULL;
2327 else
2329 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2330 boff = font_info->baseline_offset;
2331 if (font_info->vertical_centering)
2332 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2335 /* There are no padding glyphs, so there is only one glyph to
2336 produce for the composition. Important is that pixel_width,
2337 ascent and descent are the values of what is drawn by
2338 draw_glyphs (i.e. the values of the overall glyphs composed). */
2339 it->nglyphs = 1;
2341 /* If we have not yet calculated pixel size data of glyphs of
2342 the composition for the current face font, calculate them
2343 now. Theoretically, we have to check all fonts for the
2344 glyphs, but that requires much time and memory space. So,
2345 here we check only the font of the first glyph. This leads
2346 to incorrect display very rarely, and C-l (recenter) can
2347 correct the display anyway. */
2348 if (cmp->font != (void *) font)
2350 /* Ascent and descent of the font of the first character of
2351 this composition (adjusted by baseline offset). Ascent
2352 and descent of overall glyphs should not be less than
2353 them respectively. */
2354 int font_ascent = FONT_BASE (font) + boff;
2355 int font_descent = FONT_DESCENT (font) - boff;
2356 /* Bounding box of the overall glyphs. */
2357 int leftmost, rightmost, lowest, highest;
2358 int i, width, ascent, descent;
2359 enum w32_char_font_type font_type;
2361 cmp->font = (void *) font;
2363 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2364 font_type = BDF_1D_FONT;
2365 else if (font->bdf)
2366 font_type = BDF_2D_FONT;
2367 else
2368 font_type = UNICODE_FONT;
2370 /* Initialize the bounding box. */
2371 if (font_info
2372 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
2374 width = pcm->width;
2375 ascent = pcm->ascent;
2376 descent = pcm->descent;
2378 else
2380 width = FONT_WIDTH (font);
2381 ascent = FONT_BASE (font);
2382 descent = FONT_DESCENT (font);
2385 rightmost = width;
2386 lowest = - descent + boff;
2387 highest = ascent + boff;
2388 leftmost = 0;
2390 if (font_info
2391 && font_info->default_ascent
2392 && CHAR_TABLE_P (Vuse_default_ascent)
2393 && !NILP (Faref (Vuse_default_ascent,
2394 make_number (it->char_to_display))))
2395 highest = font_info->default_ascent + boff;
2397 /* Draw the first glyph at the normal position. It may be
2398 shifted to right later if some other glyphs are drawn at
2399 the left. */
2400 cmp->offsets[0] = 0;
2401 cmp->offsets[1] = boff;
2403 /* Set cmp->offsets for the remaining glyphs. */
2404 for (i = 1; i < cmp->glyph_len; i++)
2406 int left, right, btm, top;
2407 int ch = COMPOSITION_GLYPH (cmp, i);
2408 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2410 face = FACE_FROM_ID (it->f, face_id);
2411 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2412 it->multibyte_p);
2413 font = face->font;
2414 if (font == NULL)
2416 font = FRAME_FONT (it->f);
2417 boff = it->f->output_data.w32->baseline_offset;
2418 font_info = NULL;
2420 else
2422 font_info
2423 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2424 boff = font_info->baseline_offset;
2425 if (font_info->vertical_centering)
2426 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2429 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (ch)) == 1)
2430 font_type = BDF_1D_FONT;
2431 else if (font->bdf)
2432 font_type = BDF_2D_FONT;
2433 else
2434 font_type = UNICODE_FONT;
2436 if (font_info
2437 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
2439 width = pcm->width;
2440 ascent = pcm->ascent;
2441 descent = pcm->descent;
2443 else
2445 width = FONT_WIDTH (font);
2446 ascent = 1;
2447 descent = 0;
2450 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2452 /* Relative composition with or without
2453 alternate chars. */
2454 left = (leftmost + rightmost - width) / 2;
2455 btm = - descent + boff;
2456 if (font_info && font_info->relative_compose
2457 && (! CHAR_TABLE_P (Vignore_relative_composition)
2458 || NILP (Faref (Vignore_relative_composition,
2459 make_number (ch)))))
2462 if (- descent >= font_info->relative_compose)
2463 /* One extra pixel between two glyphs. */
2464 btm = highest + 1;
2465 else if (ascent <= 0)
2466 /* One extra pixel between two glyphs. */
2467 btm = lowest - 1 - ascent - descent;
2470 else
2472 /* A composition rule is specified by an integer
2473 value that encodes global and new reference
2474 points (GREF and NREF). GREF and NREF are
2475 specified by numbers as below:
2477 0---1---2 -- ascent
2481 9--10--11 -- center
2483 ---3---4---5--- baseline
2485 6---7---8 -- descent
2487 int rule = COMPOSITION_RULE (cmp, i);
2488 int gref, nref, grefx, grefy, nrefx, nrefy;
2490 COMPOSITION_DECODE_RULE (rule, gref, nref);
2491 grefx = gref % 3, nrefx = nref % 3;
2492 grefy = gref / 3, nrefy = nref / 3;
2494 left = (leftmost
2495 + grefx * (rightmost - leftmost) / 2
2496 - nrefx * width / 2);
2497 btm = ((grefy == 0 ? highest
2498 : grefy == 1 ? 0
2499 : grefy == 2 ? lowest
2500 : (highest + lowest) / 2)
2501 - (nrefy == 0 ? ascent + descent
2502 : nrefy == 1 ? descent - boff
2503 : nrefy == 2 ? 0
2504 : (ascent + descent) / 2));
2507 cmp->offsets[i * 2] = left;
2508 cmp->offsets[i * 2 + 1] = btm + descent;
2510 /* Update the bounding box of the overall glyphs. */
2511 right = left + width;
2512 top = btm + descent + ascent;
2513 if (left < leftmost)
2514 leftmost = left;
2515 if (right > rightmost)
2516 rightmost = right;
2517 if (top > highest)
2518 highest = top;
2519 if (btm < lowest)
2520 lowest = btm;
2523 /* If there are glyphs whose x-offsets are negative,
2524 shift all glyphs to the right and make all x-offsets
2525 non-negative. */
2526 if (leftmost < 0)
2528 for (i = 0; i < cmp->glyph_len; i++)
2529 cmp->offsets[i * 2] -= leftmost;
2530 rightmost -= leftmost;
2533 cmp->pixel_width = rightmost;
2534 cmp->ascent = highest;
2535 cmp->descent = - lowest;
2536 if (cmp->ascent < font_ascent)
2537 cmp->ascent = font_ascent;
2538 if (cmp->descent < font_descent)
2539 cmp->descent = font_descent;
2542 it->pixel_width = cmp->pixel_width;
2543 it->ascent = it->phys_ascent = cmp->ascent;
2544 it->descent = it->phys_descent = cmp->descent;
2546 if (face->box != FACE_NO_BOX)
2548 int thick = face->box_line_width;
2550 if (thick > 0)
2552 it->ascent += thick;
2553 it->descent += thick;
2555 else
2556 thick = - thick;
2558 if (it->start_of_box_run_p)
2559 it->pixel_width += thick;
2560 if (it->end_of_box_run_p)
2561 it->pixel_width += thick;
2564 /* If face has an overline, add the height of the overline
2565 (1 pixel) and a 1 pixel margin to the character height. */
2566 if (face->overline_p)
2567 it->ascent += 2;
2569 take_vertical_position_into_account (it);
2571 if (it->glyph_row)
2572 x_append_composite_glyph (it);
2574 else if (it->what == IT_IMAGE)
2575 x_produce_image_glyph (it);
2576 else if (it->what == IT_STRETCH)
2577 x_produce_stretch_glyph (it);
2579 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2580 because this isn't true for images with `:ascent 100'. */
2581 xassert (it->ascent >= 0 && it->descent >= 0);
2582 if (it->area == TEXT_AREA)
2583 it->current_x += it->pixel_width;
2585 it->descent += it->extra_line_spacing;
2587 it->max_ascent = max (it->max_ascent, it->ascent);
2588 it->max_descent = max (it->max_descent, it->descent);
2589 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2590 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2594 /* Estimate the pixel height of the mode or top line on frame F.
2595 FACE_ID specifies what line's height to estimate. */
2598 x_estimate_mode_line_height (f, face_id)
2599 struct frame *f;
2600 enum face_id face_id;
2602 int height = FONT_HEIGHT (FRAME_FONT (f));
2604 /* This function is called so early when Emacs starts that the face
2605 cache and mode line face are not yet initialized. */
2606 if (FRAME_FACE_CACHE (f))
2608 struct face *face = FACE_FROM_ID (f, face_id);
2609 if (face)
2611 if (face->font)
2612 height = FONT_HEIGHT (face->font);
2613 if (face->box_line_width > 0)
2614 height += 2 * face->box_line_width;
2618 return height;
2622 /***********************************************************************
2623 Glyph display
2624 ***********************************************************************/
2626 /* A sequence of glyphs to be drawn in the same face.
2628 This data structure is not really completely X specific, so it
2629 could possibly, at least partially, be useful for other systems. It
2630 is currently not part of the external redisplay interface because
2631 it's not clear what other systems will need. */
2633 struct glyph_string
2635 /* X-origin of the string. */
2636 int x;
2638 /* Y-origin and y-position of the base line of this string. */
2639 int y, ybase;
2641 /* The width of the string, not including a face extension. */
2642 int width;
2644 /* The width of the string, including a face extension. */
2645 int background_width;
2647 /* The height of this string. This is the height of the line this
2648 string is drawn in, and can be different from the height of the
2649 font the string is drawn in. */
2650 int height;
2652 /* Number of pixels this string overwrites in front of its x-origin.
2653 This number is zero if the string has an lbearing >= 0; it is
2654 -lbearing, if the string has an lbearing < 0. */
2655 int left_overhang;
2657 /* Number of pixels this string overwrites past its right-most
2658 nominal x-position, i.e. x + width. Zero if the string's
2659 rbearing is <= its nominal width, rbearing - width otherwise. */
2660 int right_overhang;
2662 /* The frame on which the glyph string is drawn. */
2663 struct frame *f;
2665 /* The window on which the glyph string is drawn. */
2666 struct window *w;
2668 /* X display and window for convenience. */
2669 Window window;
2671 /* The glyph row for which this string was built. It determines the
2672 y-origin and height of the string. */
2673 struct glyph_row *row;
2675 /* The area within row. */
2676 enum glyph_row_area area;
2678 /* Characters to be drawn, and number of characters. */
2679 wchar_t *char2b;
2680 int nchars;
2682 /* A face-override for drawing cursors, mouse face and similar. */
2683 enum draw_glyphs_face hl;
2685 /* Face in which this string is to be drawn. */
2686 struct face *face;
2688 /* Font in which this string is to be drawn. */
2689 XFontStruct *font;
2691 /* Font info for this string. */
2692 struct font_info *font_info;
2694 /* Non-null means this string describes (part of) a composition.
2695 All characters from char2b are drawn composed. */
2696 struct composition *cmp;
2698 /* Index of this glyph string's first character in the glyph
2699 definition of CMP. If this is zero, this glyph string describes
2700 the first character of a composition. */
2701 int gidx;
2703 /* 1 means this glyph strings face has to be drawn to the right end
2704 of the window's drawing area. */
2705 unsigned extends_to_end_of_line_p : 1;
2707 /* 1 means the background of this string has been drawn. */
2708 unsigned background_filled_p : 1;
2710 /* 1 means glyph string must be drawn with 16-bit functions. */
2711 unsigned two_byte_p : 1;
2713 /* 1 means that the original font determined for drawing this glyph
2714 string could not be loaded. The member `font' has been set to
2715 the frame's default font in this case. */
2716 unsigned font_not_found_p : 1;
2718 /* 1 means that the face in which this glyph string is drawn has a
2719 stipple pattern. */
2720 unsigned stippled_p : 1;
2722 /* 1 means only the foreground of this glyph string must be drawn,
2723 and we should use the physical height of the line this glyph
2724 string appears in as clip rect. */
2725 unsigned for_overlaps_p : 1;
2727 /* The GC to use for drawing this glyph string. */
2728 XGCValues *gc;
2730 HDC hdc;
2732 /* A pointer to the first glyph in the string. This glyph
2733 corresponds to char2b[0]. Needed to draw rectangles if
2734 font_not_found_p is 1. */
2735 struct glyph *first_glyph;
2737 /* Image, if any. */
2738 struct image *img;
2740 struct glyph_string *next, *prev;
2744 /* Encapsulate the different ways of displaying text under W32. */
2746 static void
2747 w32_text_out (s, x, y,chars,nchars)
2748 struct glyph_string * s;
2749 int x, y;
2750 wchar_t * chars;
2751 int nchars;
2753 int charset_dim = w32_font_is_double_byte (s->gc->font) ? 2 : 1;
2754 if (s->gc->font->bdf)
2755 w32_BDF_TextOut (s->gc->font->bdf, s->hdc,
2756 x, y, (char *) chars, charset_dim,
2757 nchars * charset_dim, 0);
2758 else if (s->first_glyph->w32_font_type == UNICODE_FONT)
2759 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
2760 else
2761 ExtTextOutA (s->hdc, x, y, 0, NULL, (char *) chars,
2762 nchars * charset_dim, NULL);
2765 #if GLYPH_DEBUG
2767 static void
2768 x_dump_glyph_string (s)
2769 struct glyph_string *s;
2771 fprintf (stderr, "glyph string\n");
2772 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2773 s->x, s->y, s->width, s->height);
2774 fprintf (stderr, " ybase = %d\n", s->ybase);
2775 fprintf (stderr, " hl = %d\n", s->hl);
2776 fprintf (stderr, " left overhang = %d, right = %d\n",
2777 s->left_overhang, s->right_overhang);
2778 fprintf (stderr, " nchars = %d\n", s->nchars);
2779 fprintf (stderr, " extends to end of line = %d\n",
2780 s->extends_to_end_of_line_p);
2781 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2782 fprintf (stderr, " bg width = %d\n", s->background_width);
2785 #endif /* GLYPH_DEBUG */
2789 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2790 struct glyph_string **,
2791 struct glyph_string *,
2792 struct glyph_string *));
2793 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2794 struct glyph_string **,
2795 struct glyph_string *,
2796 struct glyph_string *));
2797 static void x_append_glyph_string P_ ((struct glyph_string **,
2798 struct glyph_string **,
2799 struct glyph_string *));
2800 static int x_left_overwritten P_ ((struct glyph_string *));
2801 static int x_left_overwriting P_ ((struct glyph_string *));
2802 static int x_right_overwritten P_ ((struct glyph_string *));
2803 static int x_right_overwriting P_ ((struct glyph_string *));
2804 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2805 int));
2806 static void w32_init_glyph_string P_ ((struct glyph_string *, HDC hdc,
2807 wchar_t *, struct window *,
2808 struct glyph_row *,
2809 enum glyph_row_area, int,
2810 enum draw_glyphs_face));
2811 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2812 enum glyph_row_area, int, int,
2813 enum draw_glyphs_face, int));
2814 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2815 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2816 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2817 int));
2818 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2819 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2820 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2821 static void x_draw_glyph_string P_ ((struct glyph_string *));
2822 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2823 static void x_set_cursor_gc P_ ((struct glyph_string *));
2824 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2825 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2826 static void w32_get_glyph_overhangs P_ ((HDC hdc, struct glyph *,
2827 struct frame *,
2828 int *, int *));
2829 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2830 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
2831 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
2832 double, int, COLORREF));
2833 static void x_setup_relief_colors P_ ((struct glyph_string *));
2834 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2835 static void x_draw_image_relief P_ ((struct glyph_string *));
2836 static void x_draw_image_foreground P_ ((struct glyph_string *));
2837 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
2838 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2839 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2840 int, int, int));
2841 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2842 int, int, int, int, RECT *));
2843 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2844 int, int, int, RECT *));
2845 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2846 enum glyph_row_area));
2847 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2848 struct glyph_row *,
2849 enum glyph_row_area, int, int));
2851 #if GLYPH_DEBUG
2852 static void x_check_font P_ ((struct frame *, XFontStruct *));
2853 #endif
2856 /* Append the list of glyph strings with head H and tail T to the list
2857 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2859 static INLINE void
2860 x_append_glyph_string_lists (head, tail, h, t)
2861 struct glyph_string **head, **tail;
2862 struct glyph_string *h, *t;
2864 if (h)
2866 if (*head)
2867 (*tail)->next = h;
2868 else
2869 *head = h;
2870 h->prev = *tail;
2871 *tail = t;
2876 /* Prepend the list of glyph strings with head H and tail T to the
2877 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2878 result. */
2880 static INLINE void
2881 x_prepend_glyph_string_lists (head, tail, h, t)
2882 struct glyph_string **head, **tail;
2883 struct glyph_string *h, *t;
2885 if (h)
2887 if (*head)
2888 (*head)->prev = t;
2889 else
2890 *tail = t;
2891 t->next = *head;
2892 *head = h;
2897 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2898 Set *HEAD and *TAIL to the resulting list. */
2900 static INLINE void
2901 x_append_glyph_string (head, tail, s)
2902 struct glyph_string **head, **tail;
2903 struct glyph_string *s;
2905 s->next = s->prev = NULL;
2906 x_append_glyph_string_lists (head, tail, s, s);
2910 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2911 face. */
2913 static void
2914 x_set_cursor_gc (s)
2915 struct glyph_string *s;
2917 if (s->font == FRAME_FONT (s->f)
2918 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2919 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2920 && !s->cmp)
2921 s->gc = s->f->output_data.w32->cursor_gc;
2922 else
2924 /* Cursor on non-default face: must merge. */
2925 XGCValues xgcv;
2926 unsigned long mask;
2928 xgcv.background = s->f->output_data.w32->cursor_pixel;
2929 xgcv.foreground = s->face->background;
2931 /* If the glyph would be invisible, try a different foreground. */
2932 if (xgcv.foreground == xgcv.background)
2933 xgcv.foreground = s->face->foreground;
2934 if (xgcv.foreground == xgcv.background)
2935 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
2936 if (xgcv.foreground == xgcv.background)
2937 xgcv.foreground = s->face->foreground;
2939 /* Make sure the cursor is distinct from text in this face. */
2940 if (xgcv.background == s->face->background
2941 && xgcv.foreground == s->face->foreground)
2943 xgcv.background = s->face->foreground;
2944 xgcv.foreground = s->face->background;
2947 IF_DEBUG (x_check_font (s->f, s->font));
2948 xgcv.font = s->font;
2949 mask = GCForeground | GCBackground | GCFont;
2951 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2952 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2953 mask, &xgcv);
2954 else
2955 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2956 = XCreateGC (NULL, s->window, mask, &xgcv);
2958 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2963 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2965 static void
2966 x_set_mouse_face_gc (s)
2967 struct glyph_string *s;
2969 int face_id;
2970 struct face *face;
2972 /* What face has to be used last for the mouse face? */
2973 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
2974 face = FACE_FROM_ID (s->f, face_id);
2975 if (face == NULL)
2976 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2978 if (s->first_glyph->type == CHAR_GLYPH)
2979 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2980 else
2981 face_id = FACE_FOR_CHAR (s->f, face, 0);
2982 s->face = FACE_FROM_ID (s->f, face_id);
2983 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2985 /* If font in this face is same as S->font, use it. */
2986 if (s->font == s->face->font)
2987 s->gc = s->face->gc;
2988 else
2990 /* Otherwise construct scratch_cursor_gc with values from FACE
2991 but font FONT. */
2992 XGCValues xgcv;
2993 unsigned long mask;
2995 xgcv.background = s->face->background;
2996 xgcv.foreground = s->face->foreground;
2997 IF_DEBUG (x_check_font (s->f, s->font));
2998 xgcv.font = s->font;
2999 mask = GCForeground | GCBackground | GCFont;
3001 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
3002 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
3003 mask, &xgcv);
3004 else
3005 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
3006 = XCreateGC (NULL, s->window, mask, &xgcv);
3008 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
3011 xassert (s->gc != 0);
3015 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
3016 Faces to use in the mode line have already been computed when the
3017 matrix was built, so there isn't much to do, here. */
3019 static INLINE void
3020 x_set_mode_line_face_gc (s)
3021 struct glyph_string *s;
3023 s->gc = s->face->gc;
3027 /* Set S->gc of glyph string S for drawing that glyph string. Set
3028 S->stippled_p to a non-zero value if the face of S has a stipple
3029 pattern. */
3031 static INLINE void
3032 x_set_glyph_string_gc (s)
3033 struct glyph_string *s;
3035 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
3037 if (s->hl == DRAW_NORMAL_TEXT)
3039 s->gc = s->face->gc;
3040 s->stippled_p = s->face->stipple != 0;
3042 else if (s->hl == DRAW_INVERSE_VIDEO)
3044 x_set_mode_line_face_gc (s);
3045 s->stippled_p = s->face->stipple != 0;
3047 else if (s->hl == DRAW_CURSOR)
3049 x_set_cursor_gc (s);
3050 s->stippled_p = 0;
3052 else if (s->hl == DRAW_MOUSE_FACE)
3054 x_set_mouse_face_gc (s);
3055 s->stippled_p = s->face->stipple != 0;
3057 else if (s->hl == DRAW_IMAGE_RAISED
3058 || s->hl == DRAW_IMAGE_SUNKEN)
3060 s->gc = s->face->gc;
3061 s->stippled_p = s->face->stipple != 0;
3063 else
3065 s->gc = s->face->gc;
3066 s->stippled_p = s->face->stipple != 0;
3069 /* GC must have been set. */
3070 xassert (s->gc != 0);
3074 /* Return in *R the clipping rectangle for glyph string S. */
3076 static void
3077 w32_get_glyph_string_clip_rect (s, r)
3078 struct glyph_string *s;
3079 RECT *r;
3081 int r_height, r_width;
3083 if (s->row->full_width_p)
3085 /* Draw full-width. X coordinates are relative to S->w->left. */
3086 int canon_x = CANON_X_UNIT (s->f);
3088 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
3089 r_width = XFASTINT (s->w->width) * canon_x;
3091 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
3093 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
3094 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
3095 r->left -= width;
3098 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
3100 /* Unless displaying a mode or menu bar line, which are always
3101 fully visible, clip to the visible part of the row. */
3102 if (s->w->pseudo_window_p)
3103 r_height = s->row->visible_height;
3104 else
3105 r_height = s->height;
3107 else
3109 /* This is a text line that may be partially visible. */
3110 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
3111 r_width = window_box_width (s->w, s->area);
3112 r_height = s->row->visible_height;
3115 /* If S draws overlapping rows, it's sufficient to use the top and
3116 bottom of the window for clipping because this glyph string
3117 intentionally draws over other lines. */
3118 if (s->for_overlaps_p)
3120 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3121 r_height = window_text_bottom_y (s->w) - r->top;
3123 else
3125 /* Don't use S->y for clipping because it doesn't take partially
3126 visible lines into account. For example, it can be negative for
3127 partially visible lines at the top of a window. */
3128 if (!s->row->full_width_p
3129 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
3130 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3131 else
3132 r->top = max (0, s->row->y);
3134 /* If drawing a tool-bar window, draw it over the internal border
3135 at the top of the window. */
3136 if (s->w == XWINDOW (s->f->tool_bar_window))
3137 r->top -= s->f->output_data.w32->internal_border_width;
3140 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
3142 /* If drawing the cursor, don't let glyph draw outside its
3143 advertised boundaries. Cleartype does this under some circumstances. */
3144 if (s->hl == DRAW_CURSOR)
3146 if (s->x > r->left)
3148 r_width -= s->x - r->left;
3149 r->left = s->x;
3151 r_width = min (r_width, s->first_glyph->pixel_width);
3154 r->bottom = r->top + r_height;
3155 r->right = r->left + r_width;
3159 /* Set clipping for output of glyph string S. S may be part of a mode
3160 line or menu if we don't have X toolkit support. */
3162 static INLINE void
3163 x_set_glyph_string_clipping (s)
3164 struct glyph_string *s;
3166 RECT r;
3167 w32_get_glyph_string_clip_rect (s, &r);
3168 w32_set_clip_rectangle (s->hdc, &r);
3172 /* Compute left and right overhang of glyph string S. If S is a glyph
3173 string for a composition, assume overhangs don't exist. */
3175 static INLINE void
3176 x_compute_glyph_string_overhangs (s)
3177 struct glyph_string *s;
3179 /* TODO: Windows does not appear to have a method for
3180 getting this info without getting the ABC widths for each
3181 individual character and working it out manually. */
3185 /* Compute overhangs and x-positions for glyph string S and its
3186 predecessors, or successors. X is the starting x-position for S.
3187 BACKWARD_P non-zero means process predecessors. */
3189 static void
3190 x_compute_overhangs_and_x (s, x, backward_p)
3191 struct glyph_string *s;
3192 int x;
3193 int backward_p;
3195 if (backward_p)
3197 while (s)
3199 x_compute_glyph_string_overhangs (s);
3200 x -= s->width;
3201 s->x = x;
3202 s = s->prev;
3205 else
3207 while (s)
3209 x_compute_glyph_string_overhangs (s);
3210 s->x = x;
3211 x += s->width;
3212 s = s->next;
3218 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
3219 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
3220 assumed to be zero. */
3222 static void
3223 w32_get_glyph_overhangs (hdc, glyph, f, left, right)
3224 HDC hdc;
3225 struct glyph *glyph;
3226 struct frame *f;
3227 int *left, *right;
3229 *left = *right = 0;
3231 if (glyph->type == CHAR_GLYPH)
3233 XFontStruct *font;
3234 struct face *face;
3235 wchar_t char2b;
3236 XCharStruct *pcm;
3238 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
3239 font = face->font;
3241 if (font
3242 && (pcm = w32_per_char_metric (font, &char2b,
3243 glyph->w32_font_type)))
3245 if (pcm->rbearing > pcm->width)
3246 *right = pcm->rbearing - pcm->width;
3247 if (pcm->lbearing < 0)
3248 *left = -pcm->lbearing;
3254 static void
3255 x_get_glyph_overhangs (glyph, f, left, right)
3256 struct glyph *glyph;
3257 struct frame *f;
3258 int *left, *right;
3260 HDC hdc = get_frame_dc (f);
3261 /* Convert to unicode! */
3262 w32_get_glyph_overhangs (hdc, glyph, f, left, right);
3263 release_frame_dc (f, hdc);
3267 /* Return the index of the first glyph preceding glyph string S that
3268 is overwritten by S because of S's left overhang. Value is -1
3269 if no glyphs are overwritten. */
3271 static int
3272 x_left_overwritten (s)
3273 struct glyph_string *s;
3275 int k;
3277 if (s->left_overhang)
3279 int x = 0, i;
3280 struct glyph *glyphs = s->row->glyphs[s->area];
3281 int first = s->first_glyph - glyphs;
3283 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3284 x -= glyphs[i].pixel_width;
3286 k = i + 1;
3288 else
3289 k = -1;
3291 return k;
3295 /* Return the index of the first glyph preceding glyph string S that
3296 is overwriting S because of its right overhang. Value is -1 if no
3297 glyph in front of S overwrites S. */
3299 static int
3300 x_left_overwriting (s)
3301 struct glyph_string *s;
3303 int i, k, x;
3304 struct glyph *glyphs = s->row->glyphs[s->area];
3305 int first = s->first_glyph - glyphs;
3307 k = -1;
3308 x = 0;
3309 for (i = first - 1; i >= 0; --i)
3311 int left, right;
3312 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
3313 if (x + right > 0)
3314 k = i;
3315 x -= glyphs[i].pixel_width;
3318 return k;
3322 /* Return the index of the last glyph following glyph string S that is
3323 not overwritten by S because of S's right overhang. Value is -1 if
3324 no such glyph is found. */
3326 static int
3327 x_right_overwritten (s)
3328 struct glyph_string *s;
3330 int k = -1;
3332 if (s->right_overhang)
3334 int x = 0, i;
3335 struct glyph *glyphs = s->row->glyphs[s->area];
3336 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3337 int end = s->row->used[s->area];
3339 for (i = first; i < end && s->right_overhang > x; ++i)
3340 x += glyphs[i].pixel_width;
3342 k = i;
3345 return k;
3349 /* Return the index of the last glyph following glyph string S that
3350 overwrites S because of its left overhang. Value is negative
3351 if no such glyph is found. */
3353 static int
3354 x_right_overwriting (s)
3355 struct glyph_string *s;
3357 int i, k, x;
3358 int end = s->row->used[s->area];
3359 struct glyph *glyphs = s->row->glyphs[s->area];
3360 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3362 k = -1;
3363 x = 0;
3364 for (i = first; i < end; ++i)
3366 int left, right;
3367 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
3368 if (x - left < 0)
3369 k = i;
3370 x += glyphs[i].pixel_width;
3373 return k;
3377 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3379 static INLINE void
3380 x_clear_glyph_string_rect (s, x, y, w, h)
3381 struct glyph_string *s;
3382 int x, y, w, h;
3384 int real_x = x;
3385 int real_y = y;
3386 int real_w = w;
3387 int real_h = h;
3388 #if 0
3389 /* Take clipping into account. */
3390 if (s->gc->clip_mask == Rect)
3392 real_x = max (real_x, s->gc->clip_rectangle.left);
3393 real_y = max (real_y, s->gc->clip_rectangle.top);
3394 real_w = min (real_w, s->gc->clip_rectangle.right
3395 - s->gc->clip_rectangle.left);
3396 real_h = min (real_h, s->gc->clip_rectangle.bottom
3397 - s->gc->clip_rectangle.top);
3399 #endif
3400 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
3401 real_w, real_h);
3405 /* Draw the background of glyph_string S. If S->background_filled_p
3406 is non-zero don't draw it. FORCE_P non-zero means draw the
3407 background even if it wouldn't be drawn normally. This is used
3408 when a string preceding S draws into the background of S, or S
3409 contains the first component of a composition. */
3411 static void
3412 x_draw_glyph_string_background (s, force_p)
3413 struct glyph_string *s;
3414 int force_p;
3416 /* Nothing to do if background has already been drawn or if it
3417 shouldn't be drawn in the first place. */
3418 if (!s->background_filled_p)
3420 int box_line_width = max (s->face->box_line_width, 0);
3422 #if 0 /* TODO: stipple */
3423 if (s->stippled_p)
3425 /* Fill background with a stipple pattern. */
3426 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3427 XFillRectangle (s->display, s->window, s->gc, s->x,
3428 s->y + box_line_width,
3429 s->background_width,
3430 s->height - 2 * box_line_width);
3431 XSetFillStyle (s->display, s->gc, FillSolid);
3432 s->background_filled_p = 1;
3434 else
3435 #endif
3436 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3437 || s->font_not_found_p
3438 || s->extends_to_end_of_line_p
3439 || s->font->bdf
3440 || force_p)
3442 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3443 s->background_width,
3444 s->height - 2 * box_line_width);
3445 s->background_filled_p = 1;
3451 /* Draw the foreground of glyph string S. */
3453 static void
3454 x_draw_glyph_string_foreground (s)
3455 struct glyph_string *s;
3457 int i, x;
3458 HFONT old_font;
3460 /* If first glyph of S has a left box line, start drawing the text
3461 of S to the right of that box line. */
3462 if (s->face->box != FACE_NO_BOX
3463 && s->first_glyph->left_box_line_p)
3464 x = s->x + abs (s->face->box_line_width);
3465 else
3466 x = s->x;
3468 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
3469 SetBkMode (s->hdc, TRANSPARENT);
3470 else
3471 SetBkMode (s->hdc, OPAQUE);
3473 SetTextColor (s->hdc, s->gc->foreground);
3474 SetBkColor (s->hdc, s->gc->background);
3475 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3477 if (s->font && s->font->hfont)
3478 old_font = SelectObject (s->hdc, s->font->hfont);
3480 /* Draw characters of S as rectangles if S's font could not be
3481 loaded. */
3482 if (s->font_not_found_p)
3484 for (i = 0; i < s->nchars; ++i)
3486 struct glyph *g = s->first_glyph + i;
3488 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
3489 s->height - 1);
3490 x += g->pixel_width;
3493 else
3495 char *char1b = (char *) s->char2b;
3496 int boff = s->font_info->baseline_offset;
3498 if (s->font_info->vertical_centering)
3499 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3501 /* If we can use 8-bit functions, condense S->char2b. */
3502 if (!s->two_byte_p)
3503 for (i = 0; i < s->nchars; ++i)
3504 char1b[i] = BYTE2 (s->char2b[i]);
3506 /* Draw text with TextOut and friends. */
3507 w32_text_out (s, x, s->ybase - boff, s->char2b, s->nchars);
3509 if (s->font && s->font->hfont)
3510 SelectObject (s->hdc, old_font);
3513 /* Draw the foreground of composite glyph string S. */
3515 static void
3516 x_draw_composite_glyph_string_foreground (s)
3517 struct glyph_string *s;
3519 int i, x;
3520 HFONT old_font;
3522 /* If first glyph of S has a left box line, start drawing the text
3523 of S to the right of that box line. */
3524 if (s->face->box != FACE_NO_BOX
3525 && s->first_glyph->left_box_line_p)
3526 x = s->x + abs (s->face->box_line_width);
3527 else
3528 x = s->x;
3530 /* S is a glyph string for a composition. S->gidx is the index of
3531 the first character drawn for glyphs of this composition.
3532 S->gidx == 0 means we are drawing the very first character of
3533 this composition. */
3535 SetTextColor (s->hdc, s->gc->foreground);
3536 SetBkColor (s->hdc, s->gc->background);
3537 SetBkMode (s->hdc, TRANSPARENT);
3538 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3540 if (s->font && s->font->hfont)
3541 old_font = SelectObject (s->hdc, s->font->hfont);
3543 /* Draw a rectangle for the composition if the font for the very
3544 first character of the composition could not be loaded. */
3545 if (s->font_not_found_p)
3547 if (s->gidx == 0)
3548 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
3549 s->height - 1);
3551 else
3553 for (i = 0; i < s->nchars; i++, ++s->gidx)
3554 w32_text_out (s, x + s->cmp->offsets[s->gidx * 2],
3555 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3556 s->char2b + i, 1);
3558 if (s->font && s->font->hfont)
3559 SelectObject (s->hdc, old_font);
3563 /* Brightness beyond which a color won't have its highlight brightness
3564 boosted.
3566 Nominally, highlight colors for `3d' faces are calculated by
3567 brightening an object's color by a constant scale factor, but this
3568 doesn't yield good results for dark colors, so for colors who's
3569 brightness is less than this value (on a scale of 0-255) have to
3570 use an additional additive factor.
3572 The value here is set so that the default menu-bar/mode-line color
3573 (grey75) will not have its highlights changed at all. */
3574 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
3577 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
3578 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3579 If this produces the same color as COLOR, try a color where all RGB
3580 values have DELTA added. Return the allocated color in *COLOR.
3581 DISPLAY is the X display, CMAP is the colormap to operate on.
3582 Value is non-zero if successful. */
3584 static int
3585 w32_alloc_lighter_color (f, color, factor, delta)
3586 struct frame *f;
3587 COLORREF *color;
3588 double factor;
3589 int delta;
3591 COLORREF new;
3592 long bright;
3594 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
3595 delta /= 256;
3597 /* Change RGB values by specified FACTOR. Avoid overflow! */
3598 xassert (factor >= 0);
3599 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
3600 min (0xff, factor * GetGValue (*color)),
3601 min (0xff, factor * GetBValue (*color)));
3603 /* Calculate brightness of COLOR. */
3604 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
3605 + GetBValue (*color)) / 6;
3607 /* We only boost colors that are darker than
3608 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3609 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3610 /* Make an additive adjustment to NEW, because it's dark enough so
3611 that scaling by FACTOR alone isn't enough. */
3613 /* How far below the limit this color is (0 - 1, 1 being darker). */
3614 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3615 /* The additive adjustment. */
3616 int min_delta = delta * dimness * factor / 2;
3618 if (factor < 1)
3619 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
3620 max (0, min (0xff, min_delta - GetGValue (*color))),
3621 max (0, min (0xff, min_delta - GetBValue (*color))));
3622 else
3623 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
3624 max (0, min (0xff, min_delta + GetGValue (*color))),
3625 max (0, min (0xff, min_delta + GetBValue (*color))));
3628 if (new == *color)
3629 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
3630 max (0, min (0xff, delta + GetGValue (*color))),
3631 max (0, min (0xff, delta + GetBValue (*color))));
3633 /* TODO: Map to palette and retry with delta if same? */
3634 /* TODO: Free colors (if using palette)? */
3636 if (new == *color)
3637 return 0;
3639 *color = new;
3641 return 1;
3645 /* Set up the foreground color for drawing relief lines of glyph
3646 string S. RELIEF is a pointer to a struct relief containing the GC
3647 with which lines will be drawn. Use a color that is FACTOR or
3648 DELTA lighter or darker than the relief's background which is found
3649 in S->f->output_data.x->relief_background. If such a color cannot
3650 be allocated, use DEFAULT_PIXEL, instead. */
3652 static void
3653 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
3654 struct frame *f;
3655 struct relief *relief;
3656 double factor;
3657 int delta;
3658 COLORREF default_pixel;
3660 XGCValues xgcv;
3661 struct w32_output *di = f->output_data.w32;
3662 unsigned long mask = GCForeground;
3663 COLORREF pixel;
3664 COLORREF background = di->relief_background;
3665 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3667 /* TODO: Free colors (if using palette)? */
3669 /* Allocate new color. */
3670 xgcv.foreground = default_pixel;
3671 pixel = background;
3672 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
3674 relief->allocated_p = 1;
3675 xgcv.foreground = relief->pixel = pixel;
3678 if (relief->gc == 0)
3680 #if 0 /* TODO: stipple */
3681 xgcv.stipple = dpyinfo->gray;
3682 mask |= GCStipple;
3683 #endif
3684 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
3686 else
3687 XChangeGC (NULL, relief->gc, mask, &xgcv);
3691 /* Set up colors for the relief lines around glyph string S. */
3693 static void
3694 x_setup_relief_colors (s)
3695 struct glyph_string *s;
3697 struct w32_output *di = s->f->output_data.w32;
3698 COLORREF color;
3700 if (s->face->use_box_color_for_shadows_p)
3701 color = s->face->box_color;
3702 else if (s->first_glyph->type == IMAGE_GLYPH
3703 && s->img->pixmap
3704 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3705 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3706 else
3707 color = s->gc->background;
3709 if (di->white_relief.gc == 0
3710 || color != di->relief_background)
3712 di->relief_background = color;
3713 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3714 WHITE_PIX_DEFAULT (s->f));
3715 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3716 BLACK_PIX_DEFAULT (s->f));
3721 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3722 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3723 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3724 relief. LEFT_P non-zero means draw a relief on the left side of
3725 the rectangle. RIGHT_P non-zero means draw a relief on the right
3726 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3727 when drawing. */
3729 static void
3730 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3731 raised_p, left_p, right_p, clip_rect)
3732 struct frame *f;
3733 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3734 RECT *clip_rect;
3736 int i;
3737 XGCValues gc;
3738 HDC hdc = get_frame_dc (f);
3740 if (raised_p)
3741 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3742 else
3743 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
3745 w32_set_clip_rectangle (hdc, clip_rect);
3747 /* Top. */
3748 for (i = 0; i < width; ++i)
3749 w32_fill_area (f, hdc, gc.foreground,
3750 left_x + i * left_p, top_y + i,
3751 right_x - left_x - i * (left_p + right_p ) + 1, 1);
3753 /* Left. */
3754 if (left_p)
3755 for (i = 0; i < width; ++i)
3756 w32_fill_area (f, hdc, gc.foreground,
3757 left_x + i, top_y + i, 1,
3758 bottom_y - top_y - 2 * i + 1);
3760 if (raised_p)
3761 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
3762 else
3763 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3765 /* Bottom. */
3766 for (i = 0; i < width; ++i)
3767 w32_fill_area (f, hdc, gc.foreground,
3768 left_x + i * left_p, bottom_y - i,
3769 right_x - left_x - i * (left_p + right_p) + 1, 1);
3771 /* Right. */
3772 if (right_p)
3773 for (i = 0; i < width; ++i)
3774 w32_fill_area (f, hdc, gc.foreground,
3775 right_x - i, top_y + i + 1, 1,
3776 bottom_y - top_y - 2 * i - 1);
3778 w32_set_clip_rectangle (hdc, NULL);
3780 release_frame_dc (f, hdc);
3784 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3785 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3786 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3787 left side of the rectangle. RIGHT_P non-zero means draw a line
3788 on the right side of the rectangle. CLIP_RECT is the clipping
3789 rectangle to use when drawing. */
3791 static void
3792 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3793 left_p, right_p, clip_rect)
3794 struct glyph_string *s;
3795 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3796 RECT *clip_rect;
3798 w32_set_clip_rectangle (s->hdc, clip_rect);
3800 /* Top. */
3801 w32_fill_area (s->f, s->hdc, s->face->box_color,
3802 left_x, top_y, right_x - left_x + 1, width);
3804 /* Left. */
3805 if (left_p)
3807 w32_fill_area (s->f, s->hdc, s->face->box_color,
3808 left_x, top_y, width, bottom_y - top_y + 1);
3811 /* Bottom. */
3812 w32_fill_area (s->f, s->hdc, s->face->box_color,
3813 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3815 /* Right. */
3816 if (right_p)
3818 w32_fill_area (s->f, s->hdc, s->face->box_color,
3819 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3822 w32_set_clip_rectangle (s->hdc, NULL);
3826 /* Draw a box around glyph string S. */
3828 static void
3829 x_draw_glyph_string_box (s)
3830 struct glyph_string *s;
3832 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3833 int left_p, right_p;
3834 struct glyph *last_glyph;
3835 RECT clip_rect;
3837 last_x = window_box_right (s->w, s->area);
3838 if (s->row->full_width_p
3839 && !s->w->pseudo_window_p)
3841 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3842 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3843 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3846 /* The glyph that may have a right box line. */
3847 last_glyph = (s->cmp || s->img
3848 ? s->first_glyph
3849 : s->first_glyph + s->nchars - 1);
3851 width = abs (s->face->box_line_width);
3852 raised_p = s->face->box == FACE_RAISED_BOX;
3853 left_x = s->x;
3854 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
3855 ? last_x - 1
3856 : min (last_x, s->x + s->background_width) - 1));
3857 top_y = s->y;
3858 bottom_y = top_y + s->height - 1;
3860 left_p = (s->first_glyph->left_box_line_p
3861 || (s->hl == DRAW_MOUSE_FACE
3862 && (s->prev == NULL
3863 || s->prev->hl != s->hl)));
3864 right_p = (last_glyph->right_box_line_p
3865 || (s->hl == DRAW_MOUSE_FACE
3866 && (s->next == NULL
3867 || s->next->hl != s->hl)));
3869 w32_get_glyph_string_clip_rect (s, &clip_rect);
3871 if (s->face->box == FACE_SIMPLE_BOX)
3872 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3873 left_p, right_p, &clip_rect);
3874 else
3876 x_setup_relief_colors (s);
3877 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3878 width, raised_p, left_p, right_p, &clip_rect);
3883 /* Draw foreground of image glyph string S. */
3885 static void
3886 x_draw_image_foreground (s)
3887 struct glyph_string *s;
3889 int x;
3890 int y = s->ybase - image_ascent (s->img, s->face);
3892 /* If first glyph of S has a left box line, start drawing it to the
3893 right of that line. */
3894 if (s->face->box != FACE_NO_BOX
3895 && s->first_glyph->left_box_line_p)
3896 x = s->x + abs (s->face->box_line_width);
3897 else
3898 x = s->x;
3900 /* If there is a margin around the image, adjust x- and y-position
3901 by that margin. */
3902 x += s->img->hmargin;
3903 y += s->img->vmargin;
3905 SaveDC (s->hdc);
3907 if (s->img->pixmap)
3909 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3910 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3911 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3912 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3913 SetBkColor (compat_hdc, RGB (255, 255, 255));
3914 SetTextColor (s->hdc, RGB (0, 0, 0));
3915 x_set_glyph_string_clipping (s);
3917 if (s->img->mask)
3919 HDC mask_dc = CreateCompatibleDC (s->hdc);
3920 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
3922 SetTextColor (s->hdc, RGB (255, 255, 255));
3923 SetBkColor (s->hdc, RGB (0, 0, 0));
3925 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3926 compat_hdc, 0, 0, SRCINVERT);
3927 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3928 mask_dc, 0, 0, SRCAND);
3929 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3930 compat_hdc, 0, 0, SRCINVERT);
3932 SelectObject (mask_dc, mask_orig_obj);
3933 DeleteDC (mask_dc);
3935 else
3937 SetTextColor (s->hdc, s->gc->foreground);
3938 SetBkColor (s->hdc, s->gc->background);
3940 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3941 compat_hdc, 0, 0, SRCCOPY);
3943 /* When the image has a mask, we can expect that at
3944 least part of a mouse highlight or a block cursor will
3945 be visible. If the image doesn't have a mask, make
3946 a block cursor visible by drawing a rectangle around
3947 the image. I believe it's looking better if we do
3948 nothing here for mouse-face. */
3949 if (s->hl == DRAW_CURSOR)
3951 int r = s->img->relief;
3952 if (r < 0) r = -r;
3953 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
3954 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
3958 w32_set_clip_rectangle (s->hdc, NULL);
3959 SelectObject (s->hdc, orig_brush);
3960 DeleteObject (fg_brush);
3961 SelectObject (compat_hdc, orig_obj);
3962 DeleteDC (compat_hdc);
3964 else
3965 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width -1,
3966 s->img->height - 1);
3968 RestoreDC (s->hdc ,-1);
3973 /* Draw a relief around the image glyph string S. */
3975 static void
3976 x_draw_image_relief (s)
3977 struct glyph_string *s;
3979 int x0, y0, x1, y1, thick, raised_p;
3980 RECT r;
3981 int x;
3982 int y = s->ybase - image_ascent (s->img, s->face);
3984 /* If first glyph of S has a left box line, start drawing it to the
3985 right of that line. */
3986 if (s->face->box != FACE_NO_BOX
3987 && s->first_glyph->left_box_line_p)
3988 x = s->x + abs (s->face->box_line_width);
3989 else
3990 x = s->x;
3992 /* If there is a margin around the image, adjust x- and y-position
3993 by that margin. */
3994 x += s->img->hmargin;
3995 y += s->img->vmargin;
3997 if (s->hl == DRAW_IMAGE_SUNKEN
3998 || s->hl == DRAW_IMAGE_RAISED)
4000 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4001 raised_p = s->hl == DRAW_IMAGE_RAISED;
4003 else
4005 thick = abs (s->img->relief);
4006 raised_p = s->img->relief > 0;
4009 x0 = x - thick;
4010 y0 = y - thick;
4011 x1 = x + s->img->width + thick - 1;
4012 y1 = y + s->img->height + thick - 1;
4014 x_setup_relief_colors (s);
4015 w32_get_glyph_string_clip_rect (s, &r);
4016 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4020 /* Draw the foreground of image glyph string S to PIXMAP. */
4022 static void
4023 w32_draw_image_foreground_1 (s, pixmap)
4024 struct glyph_string *s;
4025 HBITMAP pixmap;
4027 HDC hdc = CreateCompatibleDC (s->hdc);
4028 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
4029 int x;
4030 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4032 /* If first glyph of S has a left box line, start drawing it to the
4033 right of that line. */
4034 if (s->face->box != FACE_NO_BOX
4035 && s->first_glyph->left_box_line_p)
4036 x = abs (s->face->box_line_width);
4037 else
4038 x = 0;
4040 /* If there is a margin around the image, adjust x- and y-position
4041 by that margin. */
4042 x += s->img->hmargin;
4043 y += s->img->vmargin;
4045 if (s->img->pixmap)
4047 HDC compat_hdc = CreateCompatibleDC (hdc);
4048 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4049 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
4050 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
4052 if (s->img->mask)
4054 HDC mask_dc = CreateCompatibleDC (hdc);
4055 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
4057 SetTextColor (hdc, RGB (0, 0, 0));
4058 SetBkColor (hdc, RGB (255, 255, 255));
4059 BitBlt (hdc, x, y, s->img->width, s->img->height,
4060 compat_hdc, 0, 0, SRCINVERT);
4061 BitBlt (hdc, x, y, s->img->width, s->img->height,
4062 mask_dc, 0, 0, SRCAND);
4063 BitBlt (hdc, x, y, s->img->width, s->img->height,
4064 compat_hdc, 0, 0, SRCINVERT);
4066 SelectObject (mask_dc, mask_orig_obj);
4067 DeleteDC (mask_dc);
4069 else
4071 SetTextColor (hdc, s->gc->foreground);
4072 SetBkColor (hdc, s->gc->background);
4074 BitBlt (hdc, x, y, s->img->width, s->img->height,
4075 compat_hdc, 0, 0, SRCCOPY);
4077 /* When the image has a mask, we can expect that at
4078 least part of a mouse highlight or a block cursor will
4079 be visible. If the image doesn't have a mask, make
4080 a block cursor visible by drawing a rectangle around
4081 the image. I believe it's looking better if we do
4082 nothing here for mouse-face. */
4083 if (s->hl == DRAW_CURSOR)
4085 int r = s->img->relief;
4086 if (r < 0) r = -r;
4087 w32_draw_rectangle (hdc, s->gc, x - r, y - r ,
4088 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4092 SelectObject (hdc, orig_brush);
4093 DeleteObject (fg_brush);
4094 SelectObject (compat_hdc, orig_obj);
4095 DeleteDC (compat_hdc);
4097 else
4098 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4099 s->img->height - 1);
4101 SelectObject (hdc, orig_hdc_obj);
4102 DeleteDC (hdc);
4106 /* Draw part of the background of glyph string S. X, Y, W, and H
4107 give the rectangle to draw. */
4109 static void
4110 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4111 struct glyph_string *s;
4112 int x, y, w, h;
4114 #if 0 /* TODO: stipple */
4115 if (s->stippled_p)
4117 /* Fill background with a stipple pattern. */
4118 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4119 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4120 XSetFillStyle (s->display, s->gc, FillSolid);
4122 else
4123 #endif
4124 x_clear_glyph_string_rect (s, x, y, w, h);
4128 /* Draw image glyph string S.
4130 s->y
4131 s->x +-------------------------
4132 | s->face->box
4134 | +-------------------------
4135 | | s->img->vmargin
4137 | | +-------------------
4138 | | | the image
4142 static void
4143 x_draw_image_glyph_string (s)
4144 struct glyph_string *s;
4146 int x, y;
4147 int box_line_hwidth = abs (s->face->box_line_width);
4148 int box_line_vwidth = max (s->face->box_line_width, 0);
4149 int height;
4150 HBITMAP pixmap = 0;
4152 height = s->height - 2 * box_line_vwidth;
4154 /* Fill background with face under the image. Do it only if row is
4155 taller than image or if image has a clip mask to reduce
4156 flickering. */
4157 s->stippled_p = s->face->stipple != 0;
4158 if (height > s->img->height
4159 || s->img->hmargin
4160 || s->img->vmargin
4161 || s->img->mask
4162 || s->img->pixmap == 0
4163 || s->width != s->background_width)
4165 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4166 x = s->x + box_line_hwidth;
4167 else
4168 x = s->x;
4170 y = s->y + box_line_vwidth;
4171 #if 0 /* TODO: figure out if we need to do this on Windows. */
4172 if (s->img->mask)
4174 /* Create a pixmap as large as the glyph string. Fill it
4175 with the background color. Copy the image to it, using
4176 its mask. Copy the temporary pixmap to the display. */
4177 Screen *screen = FRAME_X_SCREEN (s->f);
4178 int depth = DefaultDepthOfScreen (screen);
4180 /* Create a pixmap as large as the glyph string. */
4181 pixmap = XCreatePixmap (s->display, s->window,
4182 s->background_width,
4183 s->height, depth);
4185 /* Don't clip in the following because we're working on the
4186 pixmap. */
4187 XSetClipMask (s->display, s->gc, None);
4189 /* Fill the pixmap with the background color/stipple. */
4190 if (s->stippled_p)
4192 /* Fill background with a stipple pattern. */
4193 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4194 XFillRectangle (s->display, pixmap, s->gc,
4195 0, 0, s->background_width, s->height);
4196 XSetFillStyle (s->display, s->gc, FillSolid);
4198 else
4200 XGCValues xgcv;
4201 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4202 &xgcv);
4203 XSetForeground (s->display, s->gc, xgcv.background);
4204 XFillRectangle (s->display, pixmap, s->gc,
4205 0, 0, s->background_width, s->height);
4206 XSetForeground (s->display, s->gc, xgcv.foreground);
4209 else
4210 #endif
4211 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4213 s->background_filled_p = 1;
4216 /* Draw the foreground. */
4217 if (pixmap != 0)
4219 w32_draw_image_foreground_1 (s, pixmap);
4220 x_set_glyph_string_clipping (s);
4222 HDC compat_hdc = CreateCompatibleDC (s->hdc);
4223 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4224 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
4225 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
4227 SetTextColor (s->hdc, s->gc->foreground);
4228 SetBkColor (s->hdc, s->gc->background);
4229 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4230 compat_hdc, 0, 0, SRCCOPY);
4232 SelectObject (s->hdc, orig_brush);
4233 DeleteObject (fg_brush);
4234 SelectObject (compat_hdc, orig_obj);
4235 DeleteDC (compat_hdc);
4237 DeleteObject (pixmap);
4238 pixmap = 0;
4240 else
4241 x_draw_image_foreground (s);
4243 /* If we must draw a relief around the image, do it. */
4244 if (s->img->relief
4245 || s->hl == DRAW_IMAGE_RAISED
4246 || s->hl == DRAW_IMAGE_SUNKEN)
4247 x_draw_image_relief (s);
4251 /* Draw stretch glyph string S. */
4253 static void
4254 x_draw_stretch_glyph_string (s)
4255 struct glyph_string *s;
4257 xassert (s->first_glyph->type == STRETCH_GLYPH);
4258 s->stippled_p = s->face->stipple != 0;
4260 if (s->hl == DRAW_CURSOR
4261 && !x_stretch_cursor_p)
4263 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4264 as wide as the stretch glyph. */
4265 int width = min (CANON_X_UNIT (s->f), s->background_width);
4267 /* Draw cursor. */
4268 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4270 /* Clear rest using the GC of the original non-cursor face. */
4271 if (width < s->background_width)
4273 XGCValues *gc = s->face->gc;
4274 int x = s->x + width, y = s->y;
4275 int w = s->background_width - width, h = s->height;
4276 RECT r;
4277 HDC hdc = s->hdc;
4279 if (s->row->mouse_face_p
4280 && cursor_in_mouse_face_p (s->w))
4282 x_set_mouse_face_gc (s);
4283 gc = s->gc;
4285 else
4286 gc = s->face->gc;
4288 w32_get_glyph_string_clip_rect (s, &r);
4289 w32_set_clip_rectangle (hdc, &r);
4291 #if 0 /* TODO: stipple */
4292 if (s->face->stipple)
4294 /* Fill background with a stipple pattern. */
4295 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4296 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4297 XSetFillStyle (s->display, gc, FillSolid);
4299 else
4300 #endif
4302 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
4306 else if (!s->background_filled_p)
4307 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4308 s->height);
4310 s->background_filled_p = 1;
4314 /* Draw glyph string S. */
4316 static void
4317 x_draw_glyph_string (s)
4318 struct glyph_string *s;
4320 int relief_drawn_p = 0;
4322 /* If S draws into the background of its successor, draw the
4323 background of the successor first so that S can draw into it.
4324 This makes S->next use XDrawString instead of XDrawImageString. */
4325 if (s->next && s->right_overhang && !s->for_overlaps_p)
4327 xassert (s->next->img == NULL);
4328 x_set_glyph_string_gc (s->next);
4329 x_set_glyph_string_clipping (s->next);
4330 x_draw_glyph_string_background (s->next, 1);
4333 /* Set up S->gc, set clipping and draw S. */
4334 x_set_glyph_string_gc (s);
4336 /* Draw relief (if any) in advance for char/composition so that the
4337 glyph string can be drawn over it. */
4338 if (!s->for_overlaps_p
4339 && s->face->box != FACE_NO_BOX
4340 && (s->first_glyph->type == CHAR_GLYPH
4341 || s->first_glyph->type == COMPOSITE_GLYPH))
4344 x_set_glyph_string_clipping (s);
4345 x_draw_glyph_string_background (s, 1);
4346 x_draw_glyph_string_box (s);
4347 x_set_glyph_string_clipping (s);
4348 relief_drawn_p = 1;
4350 else
4351 x_set_glyph_string_clipping (s);
4353 switch (s->first_glyph->type)
4355 case IMAGE_GLYPH:
4356 x_draw_image_glyph_string (s);
4357 break;
4359 case STRETCH_GLYPH:
4360 x_draw_stretch_glyph_string (s);
4361 break;
4363 case CHAR_GLYPH:
4364 if (s->for_overlaps_p)
4365 s->background_filled_p = 1;
4366 else
4367 x_draw_glyph_string_background (s, 0);
4368 x_draw_glyph_string_foreground (s);
4369 break;
4371 case COMPOSITE_GLYPH:
4372 if (s->for_overlaps_p || s->gidx > 0)
4373 s->background_filled_p = 1;
4374 else
4375 x_draw_glyph_string_background (s, 1);
4376 x_draw_composite_glyph_string_foreground (s);
4377 break;
4379 default:
4380 abort ();
4383 if (!s->for_overlaps_p)
4385 /* Draw underline. */
4386 if (s->face->underline_p
4387 && (s->font->bdf || !s->font->tm.tmUnderlined))
4389 unsigned long h = 1;
4390 unsigned long dy = s->height - h;
4392 /* TODO: Use font information for positioning and thickness
4393 of underline. See OUTLINETEXTMETRIC, and xterm.c. */
4394 if (s->face->underline_defaulted_p)
4396 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4397 s->y + dy, s->width, 1);
4399 else
4401 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4402 s->y + dy, s->width, 1);
4406 /* Draw overline. */
4407 if (s->face->overline_p)
4409 unsigned long dy = 0, h = 1;
4411 if (s->face->overline_color_defaulted_p)
4413 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4414 s->y + dy, s->width, h);
4416 else
4418 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4419 s->y + dy, s->width, h);
4423 /* Draw strike-through. */
4424 if (s->face->strike_through_p
4425 && (s->font->bdf || !s->font->tm.tmStruckOut))
4427 unsigned long h = 1;
4428 unsigned long dy = (s->height - h) / 2;
4430 if (s->face->strike_through_color_defaulted_p)
4432 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
4433 s->width, h);
4435 else
4437 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4438 s->y + dy, s->width, h);
4442 /* Draw relief. */
4443 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4444 x_draw_glyph_string_box (s);
4447 /* Reset clipping. */
4448 w32_set_clip_rectangle (s->hdc, NULL);
4452 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4453 struct face **, int));
4456 /* Fill glyph string S with composition components specified by S->cmp.
4458 FACES is an array of faces for all components of this composition.
4459 S->gidx is the index of the first component for S.
4460 OVERLAPS_P non-zero means S should draw the foreground only, and
4461 use its physical height for clipping.
4463 Value is the index of a component not in S. */
4465 static int
4466 x_fill_composite_glyph_string (s, faces, overlaps_p)
4467 struct glyph_string *s;
4468 struct face **faces;
4469 int overlaps_p;
4471 int i;
4473 xassert (s);
4475 s->for_overlaps_p = overlaps_p;
4477 s->face = faces[s->gidx];
4478 s->font = s->face->font;
4479 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4481 /* For all glyphs of this composition, starting at the offset
4482 S->gidx, until we reach the end of the definition or encounter a
4483 glyph that requires the different face, add it to S. */
4484 ++s->nchars;
4485 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4486 ++s->nchars;
4488 /* All glyph strings for the same composition has the same width,
4489 i.e. the width set for the first component of the composition. */
4491 s->width = s->first_glyph->pixel_width;
4493 /* If the specified font could not be loaded, use the frame's
4494 default font, but record the fact that we couldn't load it in
4495 the glyph string so that we can draw rectangles for the
4496 characters of the glyph string. */
4497 if (s->font == NULL)
4499 s->font_not_found_p = 1;
4500 s->font = FRAME_FONT (s->f);
4503 /* Adjust base line for subscript/superscript text. */
4504 s->ybase += s->first_glyph->voffset;
4506 xassert (s->face && s->face->gc);
4508 /* This glyph string must always be drawn with 16-bit functions. */
4509 s->two_byte_p = 1;
4511 return s->gidx + s->nchars;
4515 /* Fill glyph string S from a sequence of character glyphs.
4517 FACE_ID is the face id of the string. START is the index of the
4518 first glyph to consider, END is the index of the last + 1.
4519 OVERLAPS_P non-zero means S should draw the foreground only, and
4520 use its physical height for clipping.
4522 Value is the index of the first glyph not in S. */
4524 static int
4525 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4526 struct glyph_string *s;
4527 int face_id;
4528 int start, end, overlaps_p;
4530 struct glyph *glyph, *last;
4531 int voffset;
4532 int glyph_not_available_p;
4534 xassert (s->f == XFRAME (s->w->frame));
4535 xassert (s->nchars == 0);
4536 xassert (start >= 0 && end > start);
4538 s->for_overlaps_p = overlaps_p;
4539 glyph = s->row->glyphs[s->area] + start;
4540 last = s->row->glyphs[s->area] + end;
4541 voffset = glyph->voffset;
4543 glyph_not_available_p = glyph->glyph_not_available_p;
4545 while (glyph < last
4546 && glyph->type == CHAR_GLYPH
4547 && glyph->voffset == voffset
4548 /* Same face id implies same font, nowadays. */
4549 && glyph->face_id == face_id
4550 && glyph->glyph_not_available_p == glyph_not_available_p)
4552 int two_byte_p;
4554 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4555 s->char2b + s->nchars,
4556 &two_byte_p);
4557 s->two_byte_p = two_byte_p;
4558 ++s->nchars;
4559 xassert (s->nchars <= end - start);
4560 s->width += glyph->pixel_width;
4561 ++glyph;
4564 s->font = s->face->font;
4565 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4567 /* If the specified font could not be loaded, use the frame's font,
4568 but record the fact that we couldn't load it in
4569 S->font_not_found_p so that we can draw rectangles for the
4570 characters of the glyph string. */
4571 if (s->font == NULL || glyph_not_available_p)
4573 s->font_not_found_p = 1;
4574 s->font = FRAME_FONT (s->f);
4577 /* Adjust base line for subscript/superscript text. */
4578 s->ybase += voffset;
4580 xassert (s->face && s->face->gc);
4581 return glyph - s->row->glyphs[s->area];
4585 /* Fill glyph string S from image glyph S->first_glyph. */
4587 static void
4588 x_fill_image_glyph_string (s)
4589 struct glyph_string *s;
4591 xassert (s->first_glyph->type == IMAGE_GLYPH);
4592 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4593 xassert (s->img);
4594 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4595 s->font = s->face->font;
4596 s->width = s->first_glyph->pixel_width;
4598 /* Adjust base line for subscript/superscript text. */
4599 s->ybase += s->first_glyph->voffset;
4603 /* Fill glyph string S from a sequence of stretch glyphs.
4605 ROW is the glyph row in which the glyphs are found, AREA is the
4606 area within the row. START is the index of the first glyph to
4607 consider, END is the index of the last + 1.
4609 Value is the index of the first glyph not in S. */
4611 static int
4612 x_fill_stretch_glyph_string (s, row, area, start, end)
4613 struct glyph_string *s;
4614 struct glyph_row *row;
4615 enum glyph_row_area area;
4616 int start, end;
4618 struct glyph *glyph, *last;
4619 int voffset, face_id;
4621 xassert (s->first_glyph->type == STRETCH_GLYPH);
4623 glyph = s->row->glyphs[s->area] + start;
4624 last = s->row->glyphs[s->area] + end;
4625 face_id = glyph->face_id;
4626 s->face = FACE_FROM_ID (s->f, face_id);
4627 s->font = s->face->font;
4628 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4629 s->width = glyph->pixel_width;
4630 voffset = glyph->voffset;
4632 for (++glyph;
4633 (glyph < last
4634 && glyph->type == STRETCH_GLYPH
4635 && glyph->voffset == voffset
4636 && glyph->face_id == face_id);
4637 ++glyph)
4638 s->width += glyph->pixel_width;
4640 /* Adjust base line for subscript/superscript text. */
4641 s->ybase += voffset;
4643 xassert (s->face);
4644 return glyph - s->row->glyphs[s->area];
4648 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4649 of XChar2b structures for S; it can't be allocated in
4650 x_init_glyph_string because it must be allocated via `alloca'. W
4651 is the window on which S is drawn. ROW and AREA are the glyph row
4652 and area within the row from which S is constructed. START is the
4653 index of the first glyph structure covered by S. HL is a
4654 face-override for drawing S. */
4656 static void
4657 w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4658 struct glyph_string *s;
4659 HDC hdc;
4660 wchar_t *char2b;
4661 struct window *w;
4662 struct glyph_row *row;
4663 enum glyph_row_area area;
4664 int start;
4665 enum draw_glyphs_face hl;
4667 bzero (s, sizeof *s);
4668 s->w = w;
4669 s->f = XFRAME (w->frame);
4670 s->hdc = hdc;
4671 s->window = FRAME_W32_WINDOW (s->f);
4672 s->char2b = char2b;
4673 s->hl = hl;
4674 s->row = row;
4675 s->area = area;
4676 s->first_glyph = row->glyphs[area] + start;
4677 s->height = row->height;
4678 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4680 /* Display the internal border below the tool-bar window. */
4681 if (s->w == XWINDOW (s->f->tool_bar_window))
4682 s->y -= s->f->output_data.w32->internal_border_width;
4684 s->ybase = s->y + row->ascent;
4688 /* Set background width of glyph string S. START is the index of the
4689 first glyph following S. LAST_X is the right-most x-position + 1
4690 in the drawing area. */
4692 static INLINE void
4693 x_set_glyph_string_background_width (s, start, last_x)
4694 struct glyph_string *s;
4695 int start;
4696 int last_x;
4698 /* If the face of this glyph string has to be drawn to the end of
4699 the drawing area, set S->extends_to_end_of_line_p. */
4700 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4702 if (start == s->row->used[s->area]
4703 && s->area == TEXT_AREA
4704 && ((s->hl == DRAW_NORMAL_TEXT
4705 && (s->row->fill_line_p
4706 || s->face->background != default_face->background
4707 || s->face->stipple != default_face->stipple
4708 || s->row->mouse_face_p))
4709 || s->hl == DRAW_MOUSE_FACE
4710 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4711 && s->row->fill_line_p)))
4712 s->extends_to_end_of_line_p = 1;
4714 /* If S extends its face to the end of the line, set its
4715 background_width to the distance to the right edge of the drawing
4716 area. */
4717 if (s->extends_to_end_of_line_p)
4718 s->background_width = last_x - s->x + 1;
4719 else
4720 s->background_width = s->width;
4724 /* Add a glyph string for a stretch glyph to the list of strings
4725 between HEAD and TAIL. START is the index of the stretch glyph in
4726 row area AREA of glyph row ROW. END is the index of the last glyph
4727 in that glyph row area. X is the current output position assigned
4728 to the new glyph string constructed. HL overrides that face of the
4729 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4730 is the right-most x-position of the drawing area. */
4732 #define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4733 do \
4735 s = (struct glyph_string *) alloca (sizeof *s); \
4736 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4737 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4738 x_append_glyph_string (&HEAD, &TAIL, s); \
4739 s->x = (X); \
4741 while (0)
4744 /* Add a glyph string for an image glyph to the list of strings
4745 between HEAD and TAIL. START is the index of the image glyph in
4746 row area AREA of glyph row ROW. END is the index of the last glyph
4747 in that glyph row area. X is the current output position assigned
4748 to the new glyph string constructed. HL overrides that face of the
4749 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4750 is the right-most x-position of the drawing area. */
4752 #define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4753 do \
4755 s = (struct glyph_string *) alloca (sizeof *s); \
4756 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4757 x_fill_image_glyph_string (s); \
4758 x_append_glyph_string (&HEAD, &TAIL, s); \
4759 ++START; \
4760 s->x = (X); \
4762 while (0)
4765 /* Add a glyph string for a sequence of character glyphs to the list
4766 of strings between HEAD and TAIL. START is the index of the first
4767 glyph in row area AREA of glyph row ROW that is part of the new
4768 glyph string. END is the index of the last glyph in that glyph row
4769 area. X is the current output position assigned to the new glyph
4770 string constructed. HL overrides that face of the glyph; e.g. it
4771 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4772 right-most x-position of the drawing area. */
4774 #define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4775 do \
4777 int c, face_id; \
4778 wchar_t *char2b; \
4780 c = (ROW)->glyphs[AREA][START].u.ch; \
4781 face_id = (ROW)->glyphs[AREA][START].face_id; \
4783 s = (struct glyph_string *) alloca (sizeof *s); \
4784 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4785 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4786 x_append_glyph_string (&HEAD, &TAIL, s); \
4787 s->x = (X); \
4788 START = x_fill_glyph_string (s, face_id, START, END, \
4789 OVERLAPS_P); \
4791 while (0)
4794 /* Add a glyph string for a composite sequence to the list of strings
4795 between HEAD and TAIL. START is the index of the first glyph in
4796 row area AREA of glyph row ROW that is part of the new glyph
4797 string. END is the index of the last glyph in that glyph row area.
4798 X is the current output position assigned to the new glyph string
4799 constructed. HL overrides that face of the glyph; e.g. it is
4800 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4801 x-position of the drawing area. */
4803 #define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4804 do { \
4805 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4806 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4807 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4808 struct composition *cmp = composition_table[cmp_id]; \
4809 int glyph_len = cmp->glyph_len; \
4810 wchar_t *char2b; \
4811 struct face **faces; \
4812 struct glyph_string *first_s = NULL; \
4813 int n; \
4815 base_face = base_face->ascii_face; \
4816 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4817 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4818 /* At first, fill in `char2b' and `faces'. */ \
4819 for (n = 0; n < glyph_len; n++) \
4821 int c = COMPOSITION_GLYPH (cmp, n); \
4822 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4823 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4824 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4825 this_face_id, char2b + n, 1); \
4828 /* Make glyph_strings for each glyph sequence that is drawable by \
4829 the same face, and append them to HEAD/TAIL. */ \
4830 for (n = 0; n < cmp->glyph_len;) \
4832 s = (struct glyph_string *) alloca (sizeof *s); \
4833 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4834 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4835 s->cmp = cmp; \
4836 s->gidx = n; \
4837 s->x = (X); \
4839 if (n == 0) \
4840 first_s = s; \
4842 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4845 ++START; \
4846 s = first_s; \
4847 } while (0)
4850 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4851 of AREA of glyph row ROW on window W between indices START and END.
4852 HL overrides the face for drawing glyph strings, e.g. it is
4853 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4854 x-positions of the drawing area.
4856 This is an ugly monster macro construct because we must use alloca
4857 to allocate glyph strings (because x_draw_glyphs can be called
4858 asynchronously). */
4860 #define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4861 do \
4863 HEAD = TAIL = NULL; \
4864 while (START < END) \
4866 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4867 switch (first_glyph->type) \
4869 case CHAR_GLYPH: \
4870 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4871 HEAD, TAIL, HL, X, LAST_X, \
4872 OVERLAPS_P); \
4873 break; \
4875 case COMPOSITE_GLYPH: \
4876 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4877 END, HEAD, TAIL, HL, X, \
4878 LAST_X, OVERLAPS_P); \
4879 break; \
4881 case STRETCH_GLYPH: \
4882 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4883 HEAD, TAIL, HL, X, LAST_X); \
4884 break; \
4886 case IMAGE_GLYPH: \
4887 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4888 HEAD, TAIL, HL, X, LAST_X); \
4889 break; \
4891 default: \
4892 abort (); \
4895 x_set_glyph_string_background_width (s, START, LAST_X); \
4896 (X) += s->width; \
4899 while (0)
4902 /* Draw glyphs between START and END in AREA of ROW on window W,
4903 starting at x-position X. X is relative to AREA in W. HL is a
4904 face-override with the following meaning:
4906 DRAW_NORMAL_TEXT draw normally
4907 DRAW_CURSOR draw in cursor face
4908 DRAW_MOUSE_FACE draw in mouse face.
4909 DRAW_INVERSE_VIDEO draw in mode line face
4910 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4911 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4913 If OVERLAPS_P is non-zero, draw only the foreground of characters
4914 and clip to the physical height of ROW.
4916 Value is the x-position reached, relative to AREA of W. */
4918 static int
4919 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
4920 struct window *w;
4921 int x;
4922 struct glyph_row *row;
4923 enum glyph_row_area area;
4924 int start, end;
4925 enum draw_glyphs_face hl;
4926 int overlaps_p;
4928 struct glyph_string *head, *tail;
4929 struct glyph_string *s;
4930 int last_x, area_width;
4931 int x_reached;
4932 int i, j;
4933 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4935 /* Let's rather be paranoid than getting a SEGV. */
4936 end = min (end, row->used[area]);
4937 start = max (0, start);
4938 start = min (end, start);
4940 /* Translate X to frame coordinates. Set last_x to the right
4941 end of the drawing area. */
4942 if (row->full_width_p)
4944 /* X is relative to the left edge of W, without scroll bars
4945 or fringes. */
4946 struct frame *f = XFRAME (WINDOW_FRAME (w));
4947 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4949 x += window_left_x;
4950 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4951 last_x = window_left_x + area_width;
4953 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4955 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4956 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4957 last_x += width;
4958 else
4959 x -= width;
4962 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4963 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4965 else
4967 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4968 area_width = window_box_width (w, area);
4969 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4972 /* Build a doubly-linked list of glyph_string structures between
4973 head and tail from what we have to draw. Note that the macro
4974 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4975 the reason we use a separate variable `i'. */
4976 i = start;
4977 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4978 overlaps_p);
4979 if (tail)
4980 x_reached = tail->x + tail->background_width;
4981 else
4982 x_reached = x;
4984 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4985 the row, redraw some glyphs in front or following the glyph
4986 strings built above. */
4987 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4989 int dummy_x = 0;
4990 struct glyph_string *h, *t;
4992 /* Compute overhangs for all glyph strings. */
4993 for (s = head; s; s = s->next)
4994 x_compute_glyph_string_overhangs (s);
4996 /* Prepend glyph strings for glyphs in front of the first glyph
4997 string that are overwritten because of the first glyph
4998 string's left overhang. The background of all strings
4999 prepended must be drawn because the first glyph string
5000 draws over it. */
5001 i = x_left_overwritten (head);
5002 if (i >= 0)
5004 j = i;
5005 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
5006 DRAW_NORMAL_TEXT, dummy_x, last_x,
5007 overlaps_p);
5008 start = i;
5009 x_compute_overhangs_and_x (t, head->x, 1);
5010 x_prepend_glyph_string_lists (&head, &tail, h, t);
5013 /* Prepend glyph strings for glyphs in front of the first glyph
5014 string that overwrite that glyph string because of their
5015 right overhang. For these strings, only the foreground must
5016 be drawn, because it draws over the glyph string at `head'.
5017 The background must not be drawn because this would overwrite
5018 right overhangs of preceding glyphs for which no glyph
5019 strings exist. */
5020 i = x_left_overwriting (head);
5021 if (i >= 0)
5023 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
5024 DRAW_NORMAL_TEXT, dummy_x, last_x,
5025 overlaps_p);
5026 for (s = h; s; s = s->next)
5027 s->background_filled_p = 1;
5028 x_compute_overhangs_and_x (t, head->x, 1);
5029 x_prepend_glyph_string_lists (&head, &tail, h, t);
5032 /* Append glyphs strings for glyphs following the last glyph
5033 string tail that are overwritten by tail. The background of
5034 these strings has to be drawn because tail's foreground draws
5035 over it. */
5036 i = x_right_overwritten (tail);
5037 if (i >= 0)
5039 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5040 DRAW_NORMAL_TEXT, x, last_x,
5041 overlaps_p);
5042 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5043 x_append_glyph_string_lists (&head, &tail, h, t);
5046 /* Append glyph strings for glyphs following the last glyph
5047 string tail that overwrite tail. The foreground of such
5048 glyphs has to be drawn because it writes into the background
5049 of tail. The background must not be drawn because it could
5050 paint over the foreground of following glyphs. */
5051 i = x_right_overwriting (tail);
5052 if (i >= 0)
5054 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5055 DRAW_NORMAL_TEXT, x, last_x,
5056 overlaps_p);
5057 for (s = h; s; s = s->next)
5058 s->background_filled_p = 1;
5059 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5060 x_append_glyph_string_lists (&head, &tail, h, t);
5064 /* Draw all strings. */
5065 for (s = head; s; s = s->next)
5066 x_draw_glyph_string (s);
5068 if (area == TEXT_AREA
5069 && !row->full_width_p
5070 /* When drawing overlapping rows, only the glyph strings'
5071 foreground is drawn, which doesn't erase a cursor
5072 completely. */
5073 && !overlaps_p)
5075 int x0 = head ? head->x : x;
5076 int x1 = tail ? tail->x + tail->background_width : x;
5078 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5079 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5081 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
5083 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5084 x0 -= left_area_width;
5085 x1 -= left_area_width;
5088 notice_overwritten_cursor (w, area, x0, x1,
5089 row->y, MATRIX_ROW_BOTTOM_Y (row));
5092 /* Value is the x-position up to which drawn, relative to AREA of W.
5093 This doesn't include parts drawn because of overhangs. */
5094 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5095 if (!row->full_width_p)
5097 if (area > LEFT_MARGIN_AREA)
5098 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5099 if (area > TEXT_AREA)
5100 x_reached -= window_box_width (w, TEXT_AREA);
5103 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
5105 return x_reached;
5109 /* Fix the display of area AREA of overlapping row ROW in window W. */
5111 static void
5112 x_fix_overlapping_area (w, row, area)
5113 struct window *w;
5114 struct glyph_row *row;
5115 enum glyph_row_area area;
5117 int i, x;
5119 BLOCK_INPUT;
5121 if (area == LEFT_MARGIN_AREA)
5122 x = 0;
5123 else if (area == TEXT_AREA)
5124 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5125 else
5126 x = (window_box_width (w, LEFT_MARGIN_AREA)
5127 + window_box_width (w, TEXT_AREA));
5129 for (i = 0; i < row->used[area];)
5131 if (row->glyphs[area][i].overlaps_vertically_p)
5133 int start = i, start_x = x;
5137 x += row->glyphs[area][i].pixel_width;
5138 ++i;
5140 while (i < row->used[area]
5141 && row->glyphs[area][i].overlaps_vertically_p);
5143 x_draw_glyphs (w, start_x, row, area, start, i,
5144 DRAW_NORMAL_TEXT, 1);
5146 else
5148 x += row->glyphs[area][i].pixel_width;
5149 ++i;
5153 UNBLOCK_INPUT;
5157 /* Output LEN glyphs starting at START at the nominal cursor position.
5158 Advance the nominal cursor over the text. The global variable
5159 updated_window contains the window being updated, updated_row is
5160 the glyph row being updated, and updated_area is the area of that
5161 row being updated. */
5163 static void
5164 x_write_glyphs (start, len)
5165 struct glyph *start;
5166 int len;
5168 int x, hpos;
5170 xassert (updated_window && updated_row);
5171 BLOCK_INPUT;
5173 /* Write glyphs. */
5175 hpos = start - updated_row->glyphs[updated_area];
5176 x = x_draw_glyphs (updated_window, output_cursor.x,
5177 updated_row, updated_area,
5178 hpos, hpos + len,
5179 DRAW_NORMAL_TEXT, 0);
5181 UNBLOCK_INPUT;
5183 /* Advance the output cursor. */
5184 output_cursor.hpos += len;
5185 output_cursor.x = x;
5189 /* Insert LEN glyphs from START at the nominal cursor position. */
5191 static void
5192 x_insert_glyphs (start, len)
5193 struct glyph *start;
5194 register int len;
5196 struct frame *f;
5197 struct window *w;
5198 int line_height, shift_by_width, shifted_region_width;
5199 struct glyph_row *row;
5200 struct glyph *glyph;
5201 int frame_x, frame_y, hpos;
5202 HDC hdc;
5204 xassert (updated_window && updated_row);
5205 BLOCK_INPUT;
5206 w = updated_window;
5207 f = XFRAME (WINDOW_FRAME (w));
5208 hdc = get_frame_dc (f);
5210 /* Get the height of the line we are in. */
5211 row = updated_row;
5212 line_height = row->height;
5214 /* Get the width of the glyphs to insert. */
5215 shift_by_width = 0;
5216 for (glyph = start; glyph < start + len; ++glyph)
5217 shift_by_width += glyph->pixel_width;
5219 /* Get the width of the region to shift right. */
5220 shifted_region_width = (window_box_width (w, updated_area)
5221 - output_cursor.x
5222 - shift_by_width);
5224 /* Shift right. */
5225 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5226 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5227 BitBlt (hdc, frame_x + shift_by_width, frame_y,
5228 shifted_region_width, line_height,
5229 hdc, frame_x, frame_y, SRCCOPY);
5231 /* Write the glyphs. */
5232 hpos = start - row->glyphs[updated_area];
5233 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5234 DRAW_NORMAL_TEXT, 0);
5236 /* Advance the output cursor. */
5237 output_cursor.hpos += len;
5238 output_cursor.x += shift_by_width;
5239 release_frame_dc (f, hdc);
5241 UNBLOCK_INPUT;
5245 /* Delete N glyphs at the nominal cursor position. Not implemented
5246 for X frames. */
5248 static void
5249 x_delete_glyphs (n)
5250 register int n;
5252 struct frame *f;
5254 if (updating_frame)
5255 f = updating_frame;
5256 else
5257 f = SELECTED_FRAME ();
5259 if (! FRAME_W32_P (f))
5260 return;
5262 abort ();
5266 /* Erase the current text line from the nominal cursor position
5267 (inclusive) to pixel column TO_X (exclusive). The idea is that
5268 everything from TO_X onward is already erased.
5270 TO_X is a pixel position relative to updated_area of
5271 updated_window. TO_X == -1 means clear to the end of this area. */
5273 static void
5274 x_clear_end_of_line (to_x)
5275 int to_x;
5277 struct frame *f;
5278 struct window *w = updated_window;
5279 int max_x, min_y, max_y;
5280 int from_x, from_y, to_y;
5282 xassert (updated_window && updated_row);
5283 f = XFRAME (w->frame);
5285 if (updated_row->full_width_p)
5287 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5288 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5289 && !w->pseudo_window_p)
5290 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5292 else
5293 max_x = window_box_width (w, updated_area);
5294 max_y = window_text_bottom_y (w);
5296 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5297 of window. For TO_X > 0, truncate to end of drawing area. */
5298 if (to_x == 0)
5299 return;
5300 else if (to_x < 0)
5301 to_x = max_x;
5302 else
5303 to_x = min (to_x, max_x);
5305 to_y = min (max_y, output_cursor.y + updated_row->height);
5307 /* Notice if the cursor will be cleared by this operation. */
5308 if (!updated_row->full_width_p)
5309 notice_overwritten_cursor (w, updated_area,
5310 output_cursor.x, -1,
5311 updated_row->y,
5312 MATRIX_ROW_BOTTOM_Y (updated_row));
5314 from_x = output_cursor.x;
5316 /* Translate to frame coordinates. */
5317 if (updated_row->full_width_p)
5319 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5320 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5322 else
5324 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5325 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5328 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5329 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5330 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5332 /* Prevent inadvertently clearing to end of the X window. */
5333 if (to_x > from_x && to_y > from_y)
5335 HDC hdc;
5336 BLOCK_INPUT;
5337 hdc = get_frame_dc (f);
5339 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
5340 release_frame_dc (f, hdc);
5341 UNBLOCK_INPUT;
5346 /* Clear entire frame. If updating_frame is non-null, clear that
5347 frame. Otherwise clear the selected frame. */
5349 static void
5350 x_clear_frame ()
5352 struct frame *f;
5354 if (updating_frame)
5355 f = updating_frame;
5356 else
5357 f = SELECTED_FRAME ();
5359 if (! FRAME_W32_P (f))
5360 return;
5362 /* Clearing the frame will erase any cursor, so mark them all as no
5363 longer visible. */
5364 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5365 output_cursor.hpos = output_cursor.vpos = 0;
5366 output_cursor.x = -1;
5368 /* We don't set the output cursor here because there will always
5369 follow an explicit cursor_to. */
5370 BLOCK_INPUT;
5372 w32_clear_window (f);
5374 /* We have to clear the scroll bars, too. If we have changed
5375 colors or something like that, then they should be notified. */
5376 x_scroll_bar_clear (f);
5378 UNBLOCK_INPUT;
5382 /* Make audible bell. */
5384 static void
5385 w32_ring_bell (void)
5387 struct frame *f;
5389 f = SELECTED_FRAME ();
5391 BLOCK_INPUT;
5393 if (FRAME_W32_P (f) && visible_bell)
5395 int i;
5396 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
5398 for (i = 0; i < 5; i++)
5400 FlashWindow (hwnd, TRUE);
5401 Sleep (10);
5403 FlashWindow (hwnd, FALSE);
5405 else
5406 w32_sys_ring_bell ();
5408 UNBLOCK_INPUT;
5412 /* Specify how many text lines, from the top of the window,
5413 should be affected by insert-lines and delete-lines operations.
5414 This, and those operations, are used only within an update
5415 that is bounded by calls to x_update_begin and x_update_end. */
5417 static void
5418 w32_set_terminal_window (n)
5419 register int n;
5421 /* This function intentionally left blank. */
5426 /***********************************************************************
5427 Line Dance
5428 ***********************************************************************/
5430 /* Perform an insert-lines or delete-lines operation, inserting N
5431 lines or deleting -N lines at vertical position VPOS. */
5433 static void
5434 x_ins_del_lines (vpos, n)
5435 int vpos, n;
5437 struct frame *f;
5439 if (updating_frame)
5440 f = updating_frame;
5441 else
5442 f = SELECTED_FRAME ();
5444 if (! FRAME_W32_P (f))
5445 return;
5447 abort ();
5451 /* Scroll part of the display as described by RUN. */
5453 static void
5454 x_scroll_run (w, run)
5455 struct window *w;
5456 struct run *run;
5458 struct frame *f = XFRAME (w->frame);
5459 int x, y, width, height, from_y, to_y, bottom_y;
5460 HWND hwnd = FRAME_W32_WINDOW (f);
5461 HRGN expect_dirty;
5463 /* Get frame-relative bounding box of the text display area of W,
5464 without mode lines. Include in this box the left and right
5465 fringes of W. */
5466 window_box (w, -1, &x, &y, &width, &height);
5467 width += FRAME_X_FRINGE_WIDTH (f);
5468 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5470 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5471 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5472 bottom_y = y + height;
5474 if (to_y < from_y)
5476 /* Scrolling up. Make sure we don't copy part of the mode
5477 line at the bottom. */
5478 if (from_y + run->height > bottom_y)
5479 height = bottom_y - from_y;
5480 else
5481 height = run->height;
5482 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
5484 else
5486 /* Scolling down. Make sure we don't copy over the mode line.
5487 at the bottom. */
5488 if (to_y + run->height > bottom_y)
5489 height = bottom_y - to_y;
5490 else
5491 height = run->height;
5492 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
5495 BLOCK_INPUT;
5497 /* Cursor off. Will be switched on again in x_update_window_end. */
5498 updated_window = w;
5499 x_clear_cursor (w);
5502 RECT from;
5503 RECT to;
5504 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
5505 HRGN combined = CreateRectRgn (0, 0, 0, 0);
5507 from.left = to.left = x;
5508 from.right = to.right = x + width;
5509 from.top = from_y;
5510 from.bottom = from_y + height;
5511 to.top = y;
5512 to.bottom = bottom_y;
5514 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
5515 NULL, SW_INVALIDATE);
5517 /* Combine this with what we expect to be dirty. This covers the
5518 case where not all of the region we expect is actually dirty. */
5519 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
5521 /* If the dirty region is not what we expected, redraw the entire frame. */
5522 if (!EqualRgn (combined, expect_dirty))
5523 SET_FRAME_GARBAGED (f);
5526 UNBLOCK_INPUT;
5531 /***********************************************************************
5532 Exposure Events
5533 ***********************************************************************/
5535 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5536 corner of the exposed rectangle. W and H are width and height of
5537 the exposed area. All are pixel values. W or H zero means redraw
5538 the entire frame. */
5540 static void
5541 expose_frame (f, x, y, w, h)
5542 struct frame *f;
5543 int x, y, w, h;
5545 RECT r;
5546 int mouse_face_overwritten_p = 0;
5548 TRACE ((stderr, "expose_frame "));
5550 /* No need to redraw if frame will be redrawn soon. */
5551 if (FRAME_GARBAGED_P (f))
5553 TRACE ((stderr, " garbaged\n"));
5554 return;
5557 /* If basic faces haven't been realized yet, there is no point in
5558 trying to redraw anything. This can happen when we get an expose
5559 event while Emacs is starting, e.g. by moving another window. */
5560 if (FRAME_FACE_CACHE (f) == NULL
5561 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5563 TRACE ((stderr, " no faces\n"));
5564 return;
5567 if (w == 0 || h == 0)
5569 r.left = r.top = 0;
5570 r.right = CANON_X_UNIT (f) * f->width;
5571 r.bottom = CANON_Y_UNIT (f) * f->height;
5573 else
5575 r.left = x;
5576 r.top = y;
5577 r.right = x + w;
5578 r.bottom = y + h;
5581 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
5582 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5584 if (WINDOWP (f->tool_bar_window))
5585 mouse_face_overwritten_p
5586 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5588 /* Some window managers support a focus-follows-mouse style with
5589 delayed raising of frames. Imagine a partially obscured frame,
5590 and moving the mouse into partially obscured mouse-face on that
5591 frame. The visible part of the mouse-face will be highlighted,
5592 then the WM raises the obscured frame. With at least one WM, KDE
5593 2.1, Emacs is not getting any event for the raising of the frame
5594 (even tried with SubstructureRedirectMask), only Expose events.
5595 These expose events will draw text normally, i.e. not
5596 highlighted. Which means we must redo the highlight here.
5597 Subsume it under ``we love X''. --gerd 2001-08-15 */
5598 /* Included in Windows version because Windows most likely does not
5599 do the right thing if any third party tool offers
5600 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
5601 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5603 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5604 if (f == dpyinfo->mouse_face_mouse_frame)
5606 int x = dpyinfo->mouse_face_mouse_x;
5607 int y = dpyinfo->mouse_face_mouse_y;
5608 clear_mouse_face (dpyinfo);
5609 note_mouse_highlight (f, x, y);
5615 /* Redraw (parts) of all windows in the window tree rooted at W that
5616 intersect R. R contains frame pixel coordinates. */
5618 static int
5619 expose_window_tree (w, r)
5620 struct window *w;
5621 RECT *r;
5623 struct frame *f = XFRAME (w->frame);
5624 int mouse_face_overwritten_p = 0;
5626 while (w && !FRAME_GARBAGED_P (f))
5628 if (!NILP (w->hchild))
5629 mouse_face_overwritten_p
5630 |= expose_window_tree (XWINDOW (w->hchild), r);
5631 else if (!NILP (w->vchild))
5632 mouse_face_overwritten_p
5633 |= expose_window_tree (XWINDOW (w->vchild), r);
5634 else
5635 mouse_face_overwritten_p |= expose_window (w, r);
5637 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5640 return mouse_face_overwritten_p;
5644 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5645 which intersects rectangle R. R is in window-relative coordinates. */
5647 static void
5648 expose_area (w, row, r, area)
5649 struct window *w;
5650 struct glyph_row *row;
5651 RECT *r;
5652 enum glyph_row_area area;
5654 struct glyph *first = row->glyphs[area];
5655 struct glyph *end = row->glyphs[area] + row->used[area];
5656 struct glyph *last;
5657 int first_x, start_x, x;
5659 if (area == TEXT_AREA && row->fill_line_p)
5660 /* If row extends face to end of line write the whole line. */
5661 x_draw_glyphs (w, 0, row, area,
5662 0, row->used[area],
5663 DRAW_NORMAL_TEXT, 0);
5664 else
5666 /* Set START_X to the window-relative start position for drawing glyphs of
5667 AREA. The first glyph of the text area can be partially visible.
5668 The first glyphs of other areas cannot. */
5669 if (area == LEFT_MARGIN_AREA)
5670 start_x = 0;
5671 else if (area == TEXT_AREA)
5672 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5673 else
5674 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5675 + window_box_width (w, TEXT_AREA));
5676 x = start_x;
5678 /* Find the first glyph that must be redrawn. */
5679 while (first < end
5680 && x + first->pixel_width < r->left)
5682 x += first->pixel_width;
5683 ++first;
5686 /* Find the last one. */
5687 last = first;
5688 first_x = x;
5689 while (last < end
5690 && x < r->right)
5692 x += last->pixel_width;
5693 ++last;
5696 /* Repaint. */
5697 if (last > first)
5698 x_draw_glyphs (w, first_x - start_x, row, area,
5699 first - row->glyphs[area],
5700 last - row->glyphs[area],
5701 DRAW_NORMAL_TEXT, 0);
5706 /* Redraw the parts of the glyph row ROW on window W intersecting
5707 rectangle R. R is in window-relative coordinates. Value is
5708 non-zero if mouse face was overwritten. */
5710 static int
5711 expose_line (w, row, r)
5712 struct window *w;
5713 struct glyph_row *row;
5714 RECT *r;
5716 xassert (row->enabled_p);
5718 if (row->mode_line_p || w->pseudo_window_p)
5719 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5720 DRAW_NORMAL_TEXT, 0);
5721 else
5723 if (row->used[LEFT_MARGIN_AREA])
5724 expose_area (w, row, r, LEFT_MARGIN_AREA);
5725 if (row->used[TEXT_AREA])
5726 expose_area (w, row, r, TEXT_AREA);
5727 if (row->used[RIGHT_MARGIN_AREA])
5728 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5729 x_draw_row_fringe_bitmaps (w, row);
5732 return row->mouse_face_p;
5736 /* Return non-zero if W's cursor intersects rectangle R. */
5738 static int
5739 x_phys_cursor_in_rect_p (w, r)
5740 struct window *w;
5741 RECT *r;
5743 RECT cr, result;
5744 struct glyph *cursor_glyph;
5746 cursor_glyph = get_phys_cursor_glyph (w);
5747 if (cursor_glyph)
5749 cr.left = w->phys_cursor.x;
5750 cr.top = w->phys_cursor.y;
5751 cr.right = cr.left + cursor_glyph->pixel_width;
5752 cr.bottom = cr.top + w->phys_cursor_height;
5753 return IntersectRect (&result, &cr, r);
5755 else
5756 return 0;
5760 /* Redraw those parts of glyphs rows during expose event handling that
5761 overlap other rows. Redrawing of an exposed line writes over parts
5762 of lines overlapping that exposed line; this function fixes that.
5764 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
5765 row in W's current matrix that is exposed and overlaps other rows.
5766 LAST_OVERLAPPING_ROW is the last such row. */
5768 static void
5769 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
5770 struct window *w;
5771 struct glyph_row *first_overlapping_row;
5772 struct glyph_row *last_overlapping_row;
5774 struct glyph_row *row;
5776 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
5777 if (row->overlapping_p)
5779 xassert (row->enabled_p && !row->mode_line_p);
5781 if (row->used[LEFT_MARGIN_AREA])
5782 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
5784 if (row->used[TEXT_AREA])
5785 x_fix_overlapping_area (w, row, TEXT_AREA);
5787 if (row->used[RIGHT_MARGIN_AREA])
5788 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
5793 /* Redraw the part of window W intersection rectagle FR. Pixel
5794 coordinates in FR are frame relative. Call this function with
5795 input blocked. Value is non-zero if the exposure overwrites
5796 mouse-face. */
5798 static int
5799 expose_window (w, fr)
5800 struct window *w;
5801 RECT *fr;
5803 struct frame *f = XFRAME (w->frame);
5804 RECT wr, r;
5805 int mouse_face_overwritten_p = 0;
5807 /* If window is not yet fully initialized, do nothing. This can
5808 happen when toolkit scroll bars are used and a window is split.
5809 Reconfiguring the scroll bar will generate an expose for a newly
5810 created window. */
5811 if (w->current_matrix == NULL)
5812 return 0;
5814 /* When we're currently updating the window, display and current
5815 matrix usually don't agree. Arrange for a thorough display
5816 later. */
5817 if (w == updated_window)
5819 SET_FRAME_GARBAGED (f);
5820 return 0;
5823 /* Frame-relative pixel rectangle of W. */
5824 wr.left = XFASTINT (w->left) * CANON_X_UNIT (f);
5825 wr.top = XFASTINT (w->top) * CANON_Y_UNIT (f);
5826 wr.right = wr.left + XFASTINT (w->width) * CANON_X_UNIT (f);
5827 wr.bottom = wr.top + XFASTINT (w->height) * CANON_Y_UNIT (f);
5829 if (IntersectRect(&r, fr, &wr))
5831 int yb = window_text_bottom_y (w);
5832 struct glyph_row *row;
5833 int cursor_cleared_p;
5834 struct glyph_row *first_overlapping_row, *last_overlapping_row;
5836 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5837 r.left, r.top, r.right, r.bottom));
5839 /* Convert to window coordinates. */
5840 r.left = FRAME_TO_WINDOW_PIXEL_X (w, r.left);
5841 r.right = FRAME_TO_WINDOW_PIXEL_X (w, r.right);
5842 r.top = FRAME_TO_WINDOW_PIXEL_Y (w, r.top);
5843 r.bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r.bottom);
5845 /* Turn off the cursor. */
5846 if (!w->pseudo_window_p
5847 && x_phys_cursor_in_rect_p (w, &r))
5849 x_clear_cursor (w);
5850 cursor_cleared_p = 1;
5852 else
5853 cursor_cleared_p = 0;
5855 /* Update lines intersecting rectangle R. */
5856 first_overlapping_row = last_overlapping_row = NULL;
5857 for (row = w->current_matrix->rows;
5858 row->enabled_p;
5859 ++row)
5861 int y0 = row->y;
5862 int y1 = MATRIX_ROW_BOTTOM_Y (row);
5864 if ((y0 >= r.top && y0 < r.bottom)
5865 || (y1 > r.top && y1 < r.bottom)
5866 || (r.top >= y0 && r.top < y1)
5867 || (r.bottom > y0 && r.bottom < y1))
5869 if (row->overlapping_p)
5871 if (first_overlapping_row == NULL)
5872 first_overlapping_row = row;
5873 last_overlapping_row = row;
5876 if (expose_line (w, row, &r))
5877 mouse_face_overwritten_p = 1;
5880 if (y1 >= yb)
5881 break;
5884 /* Display the mode line if there is one. */
5885 if (WINDOW_WANTS_MODELINE_P (w)
5886 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5887 row->enabled_p)
5888 && row->y < r.bottom)
5890 if (expose_line (w, row, &r))
5891 mouse_face_overwritten_p = 1;
5894 if (!w->pseudo_window_p)
5896 /* Fix the display of overlapping rows. */
5897 if (first_overlapping_row)
5898 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
5900 /* Draw border between windows. */
5901 x_draw_vertical_border (w);
5903 /* Turn the cursor on again. */
5904 if (cursor_cleared_p)
5905 x_update_window_cursor (w, 1);
5909 return mouse_face_overwritten_p;
5913 static void
5914 frame_highlight (f)
5915 struct frame *f;
5917 x_update_cursor (f, 1);
5920 static void
5921 frame_unhighlight (f)
5922 struct frame *f;
5924 x_update_cursor (f, 1);
5927 /* The focus has changed. Update the frames as necessary to reflect
5928 the new situation. Note that we can't change the selected frame
5929 here, because the Lisp code we are interrupting might become confused.
5930 Each event gets marked with the frame in which it occurred, so the
5931 Lisp code can tell when the switch took place by examining the events. */
5933 static void
5934 x_new_focus_frame (dpyinfo, frame)
5935 struct w32_display_info *dpyinfo;
5936 struct frame *frame;
5938 struct frame *old_focus = dpyinfo->w32_focus_frame;
5940 if (frame != dpyinfo->w32_focus_frame)
5942 /* Set this before calling other routines, so that they see
5943 the correct value of w32_focus_frame. */
5944 dpyinfo->w32_focus_frame = frame;
5946 if (old_focus && old_focus->auto_lower)
5947 x_lower_frame (old_focus);
5949 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5950 pending_autoraise_frame = dpyinfo->w32_focus_frame;
5951 else
5952 pending_autoraise_frame = 0;
5955 x_frame_rehighlight (dpyinfo);
5958 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5960 void
5961 x_mouse_leave (dpyinfo)
5962 struct w32_display_info *dpyinfo;
5964 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
5967 /* The focus has changed, or we have redirected a frame's focus to
5968 another frame (this happens when a frame uses a surrogate
5969 mini-buffer frame). Shift the highlight as appropriate.
5971 The FRAME argument doesn't necessarily have anything to do with which
5972 frame is being highlighted or un-highlighted; we only use it to find
5973 the appropriate X display info. */
5975 static void
5976 w32_frame_rehighlight (frame)
5977 struct frame *frame;
5979 if (! FRAME_W32_P (frame))
5980 return;
5981 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
5984 static void
5985 x_frame_rehighlight (dpyinfo)
5986 struct w32_display_info *dpyinfo;
5988 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5990 if (dpyinfo->w32_focus_frame)
5992 dpyinfo->x_highlight_frame
5993 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5994 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5995 : dpyinfo->w32_focus_frame);
5996 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5998 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
5999 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
6002 else
6003 dpyinfo->x_highlight_frame = 0;
6005 if (dpyinfo->x_highlight_frame != old_highlight)
6007 if (old_highlight)
6008 frame_unhighlight (old_highlight);
6009 if (dpyinfo->x_highlight_frame)
6010 frame_highlight (dpyinfo->x_highlight_frame);
6014 /* Keyboard processing - modifier keys, etc. */
6016 /* Convert a keysym to its name. */
6018 char *
6019 x_get_keysym_name (keysym)
6020 int keysym;
6022 /* Make static so we can always return it */
6023 static char value[100];
6025 BLOCK_INPUT;
6026 GetKeyNameText (keysym, value, 100);
6027 UNBLOCK_INPUT;
6029 return value;
6034 /* Mouse clicks and mouse movement. Rah. */
6036 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6037 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6038 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6039 not force the value into range. */
6041 void
6042 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6043 FRAME_PTR f;
6044 register int pix_x, pix_y;
6045 register int *x, *y;
6046 RECT *bounds;
6047 int noclip;
6049 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
6050 if (NILP (Vwindow_system))
6052 *x = pix_x;
6053 *y = pix_y;
6054 return;
6057 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6058 even for negative values. */
6059 if (pix_x < 0)
6060 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
6061 if (pix_y < 0)
6062 pix_y -= (f)->output_data.w32->line_height - 1;
6064 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6065 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6067 if (bounds)
6069 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
6070 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
6071 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
6072 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
6075 if (!noclip)
6077 if (pix_x < 0)
6078 pix_x = 0;
6079 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6080 pix_x = FRAME_WINDOW_WIDTH (f);
6082 if (pix_y < 0)
6083 pix_y = 0;
6084 else if (pix_y > f->height)
6085 pix_y = f->height;
6088 *x = pix_x;
6089 *y = pix_y;
6093 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6094 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6095 can't tell the positions because W's display is not up to date,
6096 return 0. */
6099 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6100 struct window *w;
6101 int hpos, vpos;
6102 int *frame_x, *frame_y;
6104 int success_p;
6106 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6107 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6109 if (display_completed)
6111 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6112 struct glyph *glyph = row->glyphs[TEXT_AREA];
6113 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6115 *frame_y = row->y;
6116 *frame_x = row->x;
6117 while (glyph < end)
6119 *frame_x += glyph->pixel_width;
6120 ++glyph;
6123 success_p = 1;
6125 else
6127 *frame_y = *frame_x = 0;
6128 success_p = 0;
6131 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6132 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6133 return success_p;
6136 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
6137 the state in PUP. XBUTTON provides extra information for extended mouse
6138 button messages. Returns FALSE if unable to parse the message. */
6139 BOOL
6140 parse_button (message, xbutton, pbutton, pup)
6141 int message;
6142 int xbutton;
6143 int * pbutton;
6144 int * pup;
6146 int button = 0;
6147 int up = 0;
6149 switch (message)
6151 case WM_LBUTTONDOWN:
6152 button = 0;
6153 up = 0;
6154 break;
6155 case WM_LBUTTONUP:
6156 button = 0;
6157 up = 1;
6158 break;
6159 case WM_MBUTTONDOWN:
6160 if (NILP (Vw32_swap_mouse_buttons))
6161 button = 1;
6162 else
6163 button = 2;
6164 up = 0;
6165 break;
6166 case WM_MBUTTONUP:
6167 if (NILP (Vw32_swap_mouse_buttons))
6168 button = 1;
6169 else
6170 button = 2;
6171 up = 1;
6172 break;
6173 case WM_RBUTTONDOWN:
6174 if (NILP (Vw32_swap_mouse_buttons))
6175 button = 2;
6176 else
6177 button = 1;
6178 up = 0;
6179 break;
6180 case WM_RBUTTONUP:
6181 if (NILP (Vw32_swap_mouse_buttons))
6182 button = 2;
6183 else
6184 button = 1;
6185 up = 1;
6186 break;
6187 case WM_XBUTTONDOWN:
6188 button = xbutton + 2;
6189 up = 0;
6190 break;
6191 case WM_XBUTTONUP:
6192 button = xbutton + 2;
6193 up = 1;
6194 break;
6195 default:
6196 return (FALSE);
6199 if (pup) *pup = up;
6200 if (pbutton) *pbutton = button;
6202 return (TRUE);
6206 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6208 If the event is a button press, then note that we have grabbed
6209 the mouse. */
6211 static Lisp_Object
6212 construct_mouse_click (result, msg, f)
6213 struct input_event *result;
6214 W32Msg *msg;
6215 struct frame *f;
6217 int button;
6218 int up;
6220 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
6221 &button, &up);
6223 /* Make the event type NO_EVENT; we'll change that when we decide
6224 otherwise. */
6225 result->kind = MOUSE_CLICK_EVENT;
6226 result->code = button;
6227 result->timestamp = msg->msg.time;
6228 result->modifiers = (msg->dwModifiers
6229 | (up
6230 ? up_modifier
6231 : down_modifier));
6233 XSETINT (result->x, LOWORD (msg->msg.lParam));
6234 XSETINT (result->y, HIWORD (msg->msg.lParam));
6235 XSETFRAME (result->frame_or_window, f);
6236 result->arg = Qnil;
6237 return Qnil;
6240 static Lisp_Object
6241 construct_mouse_wheel (result, msg, f)
6242 struct input_event *result;
6243 W32Msg *msg;
6244 struct frame *f;
6246 POINT p;
6247 result->kind = MOUSE_WHEEL_EVENT;
6248 result->code = (short) HIWORD (msg->msg.wParam);
6249 result->timestamp = msg->msg.time;
6250 result->modifiers = msg->dwModifiers;
6251 p.x = LOWORD (msg->msg.lParam);
6252 p.y = HIWORD (msg->msg.lParam);
6253 ScreenToClient (msg->msg.hwnd, &p);
6254 XSETINT (result->x, p.x);
6255 XSETINT (result->y, p.y);
6256 XSETFRAME (result->frame_or_window, f);
6257 result->arg = Qnil;
6258 return Qnil;
6261 static Lisp_Object
6262 construct_drag_n_drop (result, msg, f)
6263 struct input_event *result;
6264 W32Msg *msg;
6265 struct frame *f;
6267 Lisp_Object files;
6268 Lisp_Object frame;
6269 HDROP hdrop;
6270 POINT p;
6271 WORD num_files;
6272 char *name;
6273 int i, len;
6275 result->kind = DRAG_N_DROP_EVENT;
6276 result->code = 0;
6277 result->timestamp = msg->msg.time;
6278 result->modifiers = msg->dwModifiers;
6280 hdrop = (HDROP) msg->msg.wParam;
6281 DragQueryPoint (hdrop, &p);
6283 #if 0
6284 p.x = LOWORD (msg->msg.lParam);
6285 p.y = HIWORD (msg->msg.lParam);
6286 ScreenToClient (msg->msg.hwnd, &p);
6287 #endif
6289 XSETINT (result->x, p.x);
6290 XSETINT (result->y, p.y);
6292 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
6293 files = Qnil;
6295 for (i = 0; i < num_files; i++)
6297 len = DragQueryFile (hdrop, i, NULL, 0);
6298 if (len <= 0)
6299 continue;
6300 name = alloca (len + 1);
6301 DragQueryFile (hdrop, i, name, len + 1);
6302 files = Fcons (DECODE_FILE (build_string (name)), files);
6305 DragFinish (hdrop);
6307 XSETFRAME (frame, f);
6308 result->frame_or_window = Fcons (frame, files);
6309 result->arg = Qnil;
6310 return Qnil;
6314 /* Function to report a mouse movement to the mainstream Emacs code.
6315 The input handler calls this.
6317 We have received a mouse movement event, which is given in *event.
6318 If the mouse is over a different glyph than it was last time, tell
6319 the mainstream emacs code by setting mouse_moved. If not, ask for
6320 another motion event, so we can check again the next time it moves. */
6322 static MSG last_mouse_motion_event;
6323 static Lisp_Object last_mouse_motion_frame;
6325 static void remember_mouse_glyph P_ ((struct frame *, int, int));
6327 static void
6328 note_mouse_movement (frame, msg)
6329 FRAME_PTR frame;
6330 MSG *msg;
6332 int mouse_x = LOWORD (msg->lParam);
6333 int mouse_y = HIWORD (msg->lParam);
6335 last_mouse_movement_time = msg->time;
6336 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
6337 XSETFRAME (last_mouse_motion_frame, frame);
6339 #if 0 /* Calling Lisp asynchronously is not safe. */
6340 if (mouse_autoselect_window)
6342 int area;
6343 Lisp_Object window;
6344 static Lisp_Object last_window;
6346 window = window_from_coordinates (frame, mouse_x, mouse_y, &area, 0);
6348 /* Window will be selected only when it is not selected now and
6349 last mouse movement event was not in it. Minibuffer window
6350 will be selected iff it is active. */
6351 if (!EQ (window, last_window)
6352 && !EQ (window, selected_window)
6353 && (!MINI_WINDOW_P (XWINDOW (window))
6354 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6355 Fselect_window (window);
6357 last_window=window;
6359 #endif
6361 if (msg->hwnd != FRAME_W32_WINDOW (frame))
6363 frame->mouse_moved = 1;
6364 last_mouse_scroll_bar = Qnil;
6365 note_mouse_highlight (frame, -1, -1);
6368 /* Has the mouse moved off the glyph it was on at the last sighting? */
6369 else if (mouse_x < last_mouse_glyph.left
6370 || mouse_x > last_mouse_glyph.right
6371 || mouse_y < last_mouse_glyph.top
6372 || mouse_y > last_mouse_glyph.bottom)
6374 frame->mouse_moved = 1;
6375 last_mouse_scroll_bar = Qnil;
6376 note_mouse_highlight (frame, mouse_x, mouse_y);
6377 /* Remember the mouse position here, as w32_mouse_position only
6378 gets called when mouse tracking is enabled but we also need
6379 to keep track of the mouse for help_echo and highlighting at
6380 other times. */
6381 remember_mouse_glyph (frame, mouse_x, mouse_y);
6386 /************************************************************************
6387 Mouse Face
6388 ************************************************************************/
6390 /* Find the glyph under window-relative coordinates X/Y in window W.
6391 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6392 strings. Return in *HPOS and *VPOS the row and column number of
6393 the glyph found. Return in *AREA the glyph area containing X.
6394 Value is a pointer to the glyph found or null if X/Y is not on
6395 text, or we can't tell because W's current matrix is not up to
6396 date. */
6398 static struct glyph *
6399 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6400 struct window *w;
6401 int x, y;
6402 int *hpos, *vpos, *area;
6403 int buffer_only_p;
6405 struct glyph *glyph, *end;
6406 struct glyph_row *row = NULL;
6407 int x0, i, left_area_width;
6409 /* Find row containing Y. Give up if some row is not enabled. */
6410 for (i = 0; i < w->current_matrix->nrows; ++i)
6412 row = MATRIX_ROW (w->current_matrix, i);
6413 if (!row->enabled_p)
6414 return NULL;
6415 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6416 break;
6419 *vpos = i;
6420 *hpos = 0;
6422 /* Give up if Y is not in the window. */
6423 if (i == w->current_matrix->nrows)
6424 return NULL;
6426 /* Get the glyph area containing X. */
6427 if (w->pseudo_window_p)
6429 *area = TEXT_AREA;
6430 x0 = 0;
6432 else
6434 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6435 if (x < left_area_width)
6437 *area = LEFT_MARGIN_AREA;
6438 x0 = 0;
6440 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6442 *area = TEXT_AREA;
6443 x0 = row->x + left_area_width;
6445 else
6447 *area = RIGHT_MARGIN_AREA;
6448 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6452 /* Find glyph containing X. */
6453 glyph = row->glyphs[*area];
6454 end = glyph + row->used[*area];
6455 while (glyph < end)
6457 if (x < x0 + glyph->pixel_width)
6459 if (w->pseudo_window_p)
6460 break;
6461 else if (!buffer_only_p || BUFFERP (glyph->object))
6462 break;
6465 x0 += glyph->pixel_width;
6466 ++glyph;
6469 if (glyph == end)
6470 return NULL;
6472 *hpos = glyph - row->glyphs[*area];
6473 return glyph;
6477 /* Convert frame-relative x/y to coordinates relative to window W.
6478 Takes pseudo-windows into account. */
6480 static void
6481 frame_to_window_pixel_xy (w, x, y)
6482 struct window *w;
6483 int *x, *y;
6485 if (w->pseudo_window_p)
6487 /* A pseudo-window is always full-width, and starts at the
6488 left edge of the frame, plus a frame border. */
6489 struct frame *f = XFRAME (w->frame);
6490 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6491 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6493 else
6495 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6496 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6501 /* Take proper action when mouse has moved to the mode or header line of
6502 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6503 mode line. X is relative to the start of the text display area of
6504 W, so the width of fringes and scroll bars must be subtracted
6505 to get a position relative to the start of the mode line. */
6507 static void
6508 note_mode_line_highlight (w, x, mode_line_p)
6509 struct window *w;
6510 int x, mode_line_p;
6512 struct frame *f = XFRAME (w->frame);
6513 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6514 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6515 struct glyph_row *row;
6517 if (mode_line_p)
6518 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6519 else
6520 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6522 if (row->enabled_p)
6524 struct glyph *glyph, *end;
6525 Lisp_Object help, map;
6526 int x0;
6528 /* Find the glyph under X. */
6529 glyph = row->glyphs[TEXT_AREA];
6530 end = glyph + row->used[TEXT_AREA];
6531 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6532 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6534 while (glyph < end
6535 && x >= x0 + glyph->pixel_width)
6537 x0 += glyph->pixel_width;
6538 ++glyph;
6541 if (glyph < end
6542 && STRINGP (glyph->object)
6543 && STRING_INTERVALS (glyph->object)
6544 && glyph->charpos >= 0
6545 && glyph->charpos < SCHARS (glyph->object))
6547 /* If we're on a string with `help-echo' text property,
6548 arrange for the help to be displayed. This is done by
6549 setting the global variable help_echo to the help string. */
6550 help = Fget_text_property (make_number (glyph->charpos),
6551 Qhelp_echo, glyph->object);
6552 if (!NILP (help))
6554 help_echo = help;
6555 XSETWINDOW (help_echo_window, w);
6556 help_echo_object = glyph->object;
6557 help_echo_pos = glyph->charpos;
6560 /* Change the mouse pointer according to what is under X/Y. */
6561 map = Fget_text_property (make_number (glyph->charpos),
6562 Qlocal_map, glyph->object);
6563 if (KEYMAPP (map))
6564 cursor = f->output_data.w32->nontext_cursor;
6565 else
6567 map = Fget_text_property (make_number (glyph->charpos),
6568 Qkeymap, glyph->object);
6569 if (KEYMAPP (map))
6570 cursor = f->output_data.w32->nontext_cursor;
6574 #if 0 /* TODO: mouse cursor */
6575 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6576 #endif
6580 /* Take proper action when the mouse has moved to position X, Y on
6581 frame F as regards highlighting characters that have mouse-face
6582 properties. Also de-highlighting chars where the mouse was before.
6583 X and Y can be negative or out of range. */
6585 static void
6586 note_mouse_highlight (f, x, y)
6587 struct frame *f;
6588 int x, y;
6590 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6591 int portion;
6592 Lisp_Object window;
6593 struct window *w;
6594 struct buffer *b;
6596 /* When a menu is active, don't highlight because this looks odd. */
6597 if (popup_activated ())
6598 return;
6600 if (NILP (Vmouse_highlight)
6601 || !f->glyphs_initialized_p)
6602 return;
6604 dpyinfo->mouse_face_mouse_x = x;
6605 dpyinfo->mouse_face_mouse_y = y;
6606 dpyinfo->mouse_face_mouse_frame = f;
6608 if (dpyinfo->mouse_face_defer)
6609 return;
6611 if (gc_in_progress)
6613 dpyinfo->mouse_face_deferred_gc = 1;
6614 return;
6617 /* Which window is that in? */
6618 window = window_from_coordinates (f, x, y, &portion, 1);
6620 /* If we were displaying active text in another window, clear that. */
6621 if (! EQ (window, dpyinfo->mouse_face_window))
6622 clear_mouse_face (dpyinfo);
6624 /* Not on a window -> return. */
6625 if (!WINDOWP (window))
6626 return;
6628 /* Reset help_echo. It will get recomputed below. */
6629 help_echo = Qnil;
6631 /* Convert to window-relative pixel coordinates. */
6632 w = XWINDOW (window);
6633 frame_to_window_pixel_xy (w, &x, &y);
6635 /* Handle tool-bar window differently since it doesn't display a
6636 buffer. */
6637 if (EQ (window, f->tool_bar_window))
6639 note_tool_bar_highlight (f, x, y);
6640 return;
6643 /* Mouse is on the mode or header line? */
6644 if (portion == 1 || portion == 3)
6646 note_mode_line_highlight (w, x, portion == 1);
6647 return;
6649 #if 0 /* TODO: mouse cursor */
6650 if (portion == 2)
6651 cursor = f->output_data.x->horizontal_drag_cursor;
6652 else
6653 cursor = f->output_data.x->text_cursor;
6654 #endif
6655 /* Are we in a window whose display is up to date?
6656 And verify the buffer's text has not changed. */
6657 b = XBUFFER (w->buffer);
6658 if (/* Within text portion of the window. */
6659 portion == 0
6660 && EQ (w->window_end_valid, w->buffer)
6661 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6662 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6664 int hpos, vpos, pos, i, area;
6665 struct glyph *glyph;
6666 Lisp_Object object;
6667 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6668 Lisp_Object *overlay_vec = NULL;
6669 int len, noverlays;
6670 struct buffer *obuf;
6671 int obegv, ozv, same_region;
6673 /* Find the glyph under X/Y. */
6674 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6676 /* Clear mouse face if X/Y not over text. */
6677 if (glyph == NULL
6678 || area != TEXT_AREA
6679 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6681 clear_mouse_face (dpyinfo);
6682 /* TODO: mouse cursor */
6683 goto set_cursor;
6686 pos = glyph->charpos;
6687 object = glyph->object;
6688 if (!STRINGP (object) && !BUFFERP (object))
6689 goto set_cursor;
6691 /* If we get an out-of-range value, return now; avoid an error. */
6692 if (BUFFERP (object) && pos > BUF_Z (b))
6693 goto set_cursor;
6695 /* Make the window's buffer temporarily current for
6696 overlays_at and compute_char_face. */
6697 obuf = current_buffer;
6698 current_buffer = b;
6699 obegv = BEGV;
6700 ozv = ZV;
6701 BEGV = BEG;
6702 ZV = Z;
6704 /* Is this char mouse-active or does it have help-echo? */
6705 position = make_number (pos);
6707 if (BUFFERP (object))
6709 /* Put all the overlays we want in a vector in overlay_vec.
6710 Store the length in len. If there are more than 10, make
6711 enough space for all, and try again. */
6712 len = 10;
6713 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6714 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6715 if (noverlays > len)
6717 len = noverlays;
6718 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6719 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6722 /* Sort overlays into increasing priority order. */
6723 noverlays = sort_overlays (overlay_vec, noverlays, w);
6725 else
6726 noverlays = 0;
6728 same_region = (EQ (window, dpyinfo->mouse_face_window)
6729 && vpos >= dpyinfo->mouse_face_beg_row
6730 && vpos <= dpyinfo->mouse_face_end_row
6731 && (vpos > dpyinfo->mouse_face_beg_row
6732 || hpos >= dpyinfo->mouse_face_beg_col)
6733 && (vpos < dpyinfo->mouse_face_end_row
6734 || hpos < dpyinfo->mouse_face_end_col
6735 || dpyinfo->mouse_face_past_end));
6737 /* TODO: if (same_region)
6738 mouse cursor */
6740 /* Check mouse-face highlighting. */
6741 if (! same_region
6742 /* If there exists an overlay with mouse-face overlapping
6743 the one we are currently highlighting, we have to
6744 check if we enter the overlapping overlay, and then
6745 highlight that. */
6746 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6747 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6749 /* Find the highest priority overlay that has a mouse-face
6750 property. */
6751 overlay = Qnil;
6752 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6754 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6755 if (!NILP (mouse_face))
6756 overlay = overlay_vec[i];
6759 /* If we're actually highlighting the same overlay as
6760 before, there's no need to do that again. */
6761 if (!NILP (overlay)
6762 && EQ (overlay, dpyinfo->mouse_face_overlay))
6763 goto check_help_echo;
6765 dpyinfo->mouse_face_overlay = overlay;
6767 /* Clear the display of the old active region, if any. */
6768 clear_mouse_face (dpyinfo);
6769 /* TODO: mouse cursor changes. */
6771 /* If no overlay applies, get a text property. */
6772 if (NILP (overlay))
6773 mouse_face = Fget_text_property (position, Qmouse_face, object);
6775 /* Handle the overlay case. */
6776 if (!NILP (overlay))
6778 /* Find the range of text around this char that
6779 should be active. */
6780 Lisp_Object before, after;
6781 int ignore;
6783 before = Foverlay_start (overlay);
6784 after = Foverlay_end (overlay);
6785 /* Record this as the current active region. */
6786 fast_find_position (w, XFASTINT (before),
6787 &dpyinfo->mouse_face_beg_col,
6788 &dpyinfo->mouse_face_beg_row,
6789 &dpyinfo->mouse_face_beg_x,
6790 &dpyinfo->mouse_face_beg_y, Qnil);
6792 dpyinfo->mouse_face_past_end
6793 = !fast_find_position (w, XFASTINT (after),
6794 &dpyinfo->mouse_face_end_col,
6795 &dpyinfo->mouse_face_end_row,
6796 &dpyinfo->mouse_face_end_x,
6797 &dpyinfo->mouse_face_end_y, Qnil);
6798 dpyinfo->mouse_face_window = window;
6800 dpyinfo->mouse_face_face_id
6801 = face_at_buffer_position (w, pos, 0, 0,
6802 &ignore, pos + 1,
6803 !dpyinfo->mouse_face_hidden);
6805 /* Display it as active. */
6806 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6807 /* TODO: mouse cursor changes. */
6809 /* Handle the text property case. */
6810 else if (! NILP (mouse_face) && BUFFERP (object))
6812 /* Find the range of text around this char that
6813 should be active. */
6814 Lisp_Object before, after, beginning, end;
6815 int ignore;
6817 beginning = Fmarker_position (w->start);
6818 end = make_number (BUF_Z (XBUFFER (object))
6819 - XFASTINT (w->window_end_pos));
6820 before
6821 = Fprevious_single_property_change (make_number (pos + 1),
6822 Qmouse_face,
6823 object, beginning);
6824 after
6825 = Fnext_single_property_change (position, Qmouse_face,
6826 object, end);
6828 /* Record this as the current active region. */
6829 fast_find_position (w, XFASTINT (before),
6830 &dpyinfo->mouse_face_beg_col,
6831 &dpyinfo->mouse_face_beg_row,
6832 &dpyinfo->mouse_face_beg_x,
6833 &dpyinfo->mouse_face_beg_y, Qnil);
6834 dpyinfo->mouse_face_past_end
6835 = !fast_find_position (w, XFASTINT (after),
6836 &dpyinfo->mouse_face_end_col,
6837 &dpyinfo->mouse_face_end_row,
6838 &dpyinfo->mouse_face_end_x,
6839 &dpyinfo->mouse_face_end_y, Qnil);
6840 dpyinfo->mouse_face_window = window;
6842 if (BUFFERP (object))
6843 dpyinfo->mouse_face_face_id
6844 = face_at_buffer_position (w, pos, 0, 0,
6845 &ignore, pos + 1,
6846 !dpyinfo->mouse_face_hidden);
6848 /* Display it as active. */
6849 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6850 /* TODO: mouse cursor changes. */
6852 else if (!NILP (mouse_face) && STRINGP (object))
6854 Lisp_Object b, e;
6855 int ignore;
6857 b = Fprevious_single_property_change (make_number (pos + 1),
6858 Qmouse_face,
6859 object, Qnil);
6860 e = Fnext_single_property_change (position, Qmouse_face,
6861 object, Qnil);
6862 if (NILP (b))
6863 b = make_number (0);
6864 if (NILP (e))
6865 e = make_number (SCHARS (object) - 1);
6866 fast_find_string_pos (w, XINT (b), object,
6867 &dpyinfo->mouse_face_beg_col,
6868 &dpyinfo->mouse_face_beg_row,
6869 &dpyinfo->mouse_face_beg_x,
6870 &dpyinfo->mouse_face_beg_y, 0);
6871 fast_find_string_pos (w, XINT (e), object,
6872 &dpyinfo->mouse_face_end_col,
6873 &dpyinfo->mouse_face_end_row,
6874 &dpyinfo->mouse_face_end_x,
6875 &dpyinfo->mouse_face_end_y, 1);
6876 dpyinfo->mouse_face_past_end = 0;
6877 dpyinfo->mouse_face_window = window;
6878 dpyinfo->mouse_face_face_id
6879 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
6880 glyph->face_id, 1);
6881 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6882 /* TODO: mouse cursor changes. */
6884 else if (STRINGP (object) && NILP (mouse_face))
6886 /* A string which doesn't have mouse-face, but
6887 the text ``under'' it might have. */
6888 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
6889 int start = MATRIX_ROW_START_CHARPOS (r);
6891 pos = string_buffer_position (w, object, start);
6892 if (pos > 0)
6893 mouse_face = get_char_property_and_overlay (make_number (pos),
6894 Qmouse_face,
6895 w->buffer,
6896 &overlay);
6897 if (!NILP (mouse_face) && !NILP (overlay))
6899 Lisp_Object before = Foverlay_start (overlay);
6900 Lisp_Object after = Foverlay_end (overlay);
6901 int ignore;
6903 /* Note that we might not be able to find position
6904 BEFORE in the glyph matrix if the overlay is
6905 entirely covered by a `display' property. In
6906 this case, we overshoot. So let's stop in
6907 the glyph matrix before glyphs for OBJECT. */
6908 fast_find_position (w, XFASTINT (before),
6909 &dpyinfo->mouse_face_beg_col,
6910 &dpyinfo->mouse_face_beg_row,
6911 &dpyinfo->mouse_face_beg_x,
6912 &dpyinfo->mouse_face_beg_y,
6913 object);
6915 dpyinfo->mouse_face_past_end
6916 = !fast_find_position (w, XFASTINT (after),
6917 &dpyinfo->mouse_face_end_col,
6918 &dpyinfo->mouse_face_end_row,
6919 &dpyinfo->mouse_face_end_x,
6920 &dpyinfo->mouse_face_end_y,
6921 Qnil);
6922 dpyinfo->mouse_face_window = window;
6923 dpyinfo->mouse_face_face_id
6924 = face_at_buffer_position (w, pos, 0, 0,
6925 &ignore, pos + 1,
6926 !dpyinfo->mouse_face_hidden);
6928 /* Display it as active. */
6929 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6930 /* TODO: mouse cursor changes. */
6935 check_help_echo:
6937 /* Look for a `help-echo' property. */
6939 Lisp_Object help, overlay;
6941 /* Check overlays first. */
6942 help = overlay = Qnil;
6943 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6945 overlay = overlay_vec[i];
6946 help = Foverlay_get (overlay, Qhelp_echo);
6949 if (!NILP (help))
6951 help_echo = help;
6952 help_echo_window = window;
6953 help_echo_object = overlay;
6954 help_echo_pos = pos;
6956 else
6958 Lisp_Object object = glyph->object;
6959 int charpos = glyph->charpos;
6961 /* Try text properties. */
6962 if (STRINGP (object)
6963 && charpos >= 0
6964 && charpos < SCHARS (object))
6966 help = Fget_text_property (make_number (charpos),
6967 Qhelp_echo, object);
6968 if (NILP (help))
6970 /* If the string itself doesn't specify a help-echo,
6971 see if the buffer text ``under'' it does. */
6972 struct glyph_row *r
6973 = MATRIX_ROW (w->current_matrix, vpos);
6974 int start = MATRIX_ROW_START_CHARPOS (r);
6975 int pos = string_buffer_position (w, object, start);
6976 if (pos > 0)
6978 help = Fget_char_property (make_number (pos),
6979 Qhelp_echo, w->buffer);
6980 if (!NILP (help))
6982 charpos = pos;
6983 object = w->buffer;
6988 else if (BUFFERP (object)
6989 && charpos >= BEGV
6990 && charpos < ZV)
6991 help = Fget_text_property (make_number (charpos), Qhelp_echo,
6992 object);
6994 if (!NILP (help))
6996 help_echo = help;
6997 help_echo_window = window;
6998 help_echo_object = object;
6999 help_echo_pos = charpos;
7004 BEGV = obegv;
7005 ZV = ozv;
7006 current_buffer = obuf;
7009 set_cursor:
7010 /* TODO: mouse cursor changes. */
7014 static void
7015 redo_mouse_highlight ()
7017 if (!NILP (last_mouse_motion_frame)
7018 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7019 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7020 LOWORD (last_mouse_motion_event.lParam),
7021 HIWORD (last_mouse_motion_event.lParam));
7026 /***********************************************************************
7027 Tool-bars
7028 ***********************************************************************/
7030 static int x_tool_bar_item P_ ((struct frame *, int, int,
7031 struct glyph **, int *, int *, int *));
7033 /* Tool-bar item index of the item on which a mouse button was pressed
7034 or -1. */
7036 static int last_tool_bar_item;
7039 /* Get information about the tool-bar item at position X/Y on frame F.
7040 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7041 the current matrix of the tool-bar window of F, or NULL if not
7042 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7043 item in F->tool_bar_items. Value is
7045 -1 if X/Y is not on a tool-bar item
7046 0 if X/Y is on the same item that was highlighted before.
7047 1 otherwise. */
7049 static int
7050 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7051 struct frame *f;
7052 int x, y;
7053 struct glyph **glyph;
7054 int *hpos, *vpos, *prop_idx;
7056 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7057 struct window *w = XWINDOW (f->tool_bar_window);
7058 int area;
7060 /* Find the glyph under X/Y. */
7061 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7062 if (*glyph == NULL)
7063 return -1;
7065 /* Get the start of this tool-bar item's properties in
7066 f->tool_bar_items. */
7067 if (!tool_bar_item_info (f, *glyph, prop_idx))
7068 return -1;
7070 /* Is mouse on the highlighted item? */
7071 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7072 && *vpos >= dpyinfo->mouse_face_beg_row
7073 && *vpos <= dpyinfo->mouse_face_end_row
7074 && (*vpos > dpyinfo->mouse_face_beg_row
7075 || *hpos >= dpyinfo->mouse_face_beg_col)
7076 && (*vpos < dpyinfo->mouse_face_end_row
7077 || *hpos < dpyinfo->mouse_face_end_col
7078 || dpyinfo->mouse_face_past_end))
7079 return 0;
7081 return 1;
7085 /* Handle mouse button event on the tool-bar of frame F, at
7086 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7087 or ButtonRelase. */
7089 static void
7090 w32_handle_tool_bar_click (f, button_event)
7091 struct frame *f;
7092 struct input_event *button_event;
7094 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7095 struct window *w = XWINDOW (f->tool_bar_window);
7096 int hpos, vpos, prop_idx;
7097 struct glyph *glyph;
7098 Lisp_Object enabled_p;
7099 int x = XFASTINT (button_event->x);
7100 int y = XFASTINT (button_event->y);
7102 /* If not on the highlighted tool-bar item, return. */
7103 frame_to_window_pixel_xy (w, &x, &y);
7104 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7105 return;
7107 /* If item is disabled, do nothing. */
7108 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7109 if (NILP (enabled_p))
7110 return;
7112 if (button_event->modifiers & down_modifier)
7114 /* Show item in pressed state. */
7115 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7116 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7117 last_tool_bar_item = prop_idx;
7119 else
7121 Lisp_Object key, frame;
7122 struct input_event event;
7124 /* Show item in released state. */
7125 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7126 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7128 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7130 XSETFRAME (frame, f);
7131 event.kind = TOOL_BAR_EVENT;
7132 event.frame_or_window = frame;
7133 event.arg = frame;
7134 kbd_buffer_store_event (&event);
7136 event.kind = TOOL_BAR_EVENT;
7137 event.frame_or_window = frame;
7138 event.arg = key;
7139 /* The keyboard buffer doesn't like the up modifier being set. */
7140 event.modifiers = button_event->modifiers & ~up_modifier;
7141 kbd_buffer_store_event (&event);
7142 last_tool_bar_item = -1;
7147 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7148 tool-bar window-relative coordinates X/Y. Called from
7149 note_mouse_highlight. */
7151 static void
7152 note_tool_bar_highlight (f, x, y)
7153 struct frame *f;
7154 int x, y;
7156 Lisp_Object window = f->tool_bar_window;
7157 struct window *w = XWINDOW (window);
7158 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7159 int hpos, vpos;
7160 struct glyph *glyph;
7161 struct glyph_row *row;
7162 int i;
7163 Lisp_Object enabled_p;
7164 int prop_idx;
7165 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7166 int mouse_down_p, rc;
7168 /* Function note_mouse_highlight is called with negative x(y
7169 values when mouse moves outside of the frame. */
7170 if (x <= 0 || y <= 0)
7172 clear_mouse_face (dpyinfo);
7173 return;
7176 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7177 if (rc < 0)
7179 /* Not on tool-bar item. */
7180 clear_mouse_face (dpyinfo);
7181 return;
7183 else if (rc == 0)
7184 /* On same tool-bar item as before. */
7185 goto set_help_echo;
7187 clear_mouse_face (dpyinfo);
7189 /* Mouse is down, but on different tool-bar item? */
7190 mouse_down_p = (dpyinfo->grabbed
7191 && f == last_mouse_frame
7192 && FRAME_LIVE_P (f));
7193 if (mouse_down_p
7194 && last_tool_bar_item != prop_idx)
7195 return;
7197 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7198 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7200 /* If tool-bar item is not enabled, don't highlight it. */
7201 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7202 if (!NILP (enabled_p))
7204 /* Compute the x-position of the glyph. In front and past the
7205 image is a space. We include this is the highlighted area. */
7206 row = MATRIX_ROW (w->current_matrix, vpos);
7207 for (i = x = 0; i < hpos; ++i)
7208 x += row->glyphs[TEXT_AREA][i].pixel_width;
7210 /* Record this as the current active region. */
7211 dpyinfo->mouse_face_beg_col = hpos;
7212 dpyinfo->mouse_face_beg_row = vpos;
7213 dpyinfo->mouse_face_beg_x = x;
7214 dpyinfo->mouse_face_beg_y = row->y;
7215 dpyinfo->mouse_face_past_end = 0;
7217 dpyinfo->mouse_face_end_col = hpos + 1;
7218 dpyinfo->mouse_face_end_row = vpos;
7219 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7220 dpyinfo->mouse_face_end_y = row->y;
7221 dpyinfo->mouse_face_window = window;
7222 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7224 /* Display it as active. */
7225 show_mouse_face (dpyinfo, draw);
7226 dpyinfo->mouse_face_image_state = draw;
7229 set_help_echo:
7231 /* Set help_echo to a help string.to display for this tool-bar item.
7232 w32_read_socket does the rest. */
7233 help_echo_object = help_echo_window = Qnil;
7234 help_echo_pos = -1;
7235 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7236 if (NILP (help_echo))
7237 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7242 /* Find the glyph matrix position of buffer position CHARPOS in window
7243 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7244 current glyphs must be up to date. If CHARPOS is above window
7245 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7246 of last line in W. In the row containing CHARPOS, stop before glyphs
7247 having STOP as object. */
7249 #if 0 /* This is a version of fast_find_position that's more correct
7250 in the presence of hscrolling, for example. I didn't install
7251 it right away because the problem fixed is minor, it failed
7252 in 20.x as well, and I think it's too risky to install
7253 so near the release of 21.1. 2001-09-25 gerd. */
7255 static int
7256 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7257 struct window *w;
7258 int charpos;
7259 int *hpos, *vpos, *x, *y;
7260 Lisp_Object stop;
7262 struct glyph_row *row, *first;
7263 struct glyph *glyph, *end;
7264 int i, past_end = 0;
7266 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7267 row = row_containing_pos (w, charpos, first, NULL, 0);
7268 if (row == NULL)
7270 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7272 *x = *y = *hpos = *vpos = 0;
7273 return 0;
7275 else
7277 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7278 past_end = 1;
7282 *x = row->x;
7283 *y = row->y;
7284 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7286 glyph = row->glyphs[TEXT_AREA];
7287 end = glyph + row->used[TEXT_AREA];
7289 /* Skip over glyphs not having an object at the start of the row.
7290 These are special glyphs like truncation marks on terminal
7291 frames. */
7292 if (row->displays_text_p)
7293 while (glyph < end
7294 && INTEGERP (glyph->object)
7295 && !EQ (stop, glyph->object)
7296 && glyph->charpos < 0)
7298 *x += glyph->pixel_width;
7299 ++glyph;
7302 while (glyph < end
7303 && !INTEGERP (glyph->object)
7304 && !EQ (stop, glyph->object)
7305 && (!BUFFERP (glyph->object)
7306 || glyph->charpos < charpos))
7308 *x += glyph->pixel_width;
7309 ++glyph;
7312 *hpos = glyph - row->glyphs[TEXT_AREA];
7313 return past_end;
7316 #else /* not 0 */
7318 static int
7319 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7320 struct window *w;
7321 int pos;
7322 int *hpos, *vpos, *x, *y;
7323 Lisp_Object stop;
7325 int i;
7326 int lastcol;
7327 int maybe_next_line_p = 0;
7328 int line_start_position;
7329 int yb = window_text_bottom_y (w);
7330 struct glyph_row *row, *best_row;
7331 int row_vpos, best_row_vpos;
7332 int current_x;
7334 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7335 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7337 while (row->y < yb)
7339 if (row->used[TEXT_AREA])
7340 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7341 else
7342 line_start_position = 0;
7344 if (line_start_position > pos)
7345 break;
7346 /* If the position sought is the end of the buffer,
7347 don't include the blank lines at the bottom of the window. */
7348 else if (line_start_position == pos
7349 && pos == BUF_ZV (XBUFFER (w->buffer)))
7351 maybe_next_line_p = 1;
7352 break;
7354 else if (line_start_position > 0)
7356 best_row = row;
7357 best_row_vpos = row_vpos;
7360 if (row->y + row->height >= yb)
7361 break;
7363 ++row;
7364 ++row_vpos;
7367 /* Find the right column within BEST_ROW. */
7368 lastcol = 0;
7369 current_x = best_row->x;
7370 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7372 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7373 int charpos = glyph->charpos;
7375 if (BUFFERP (glyph->object))
7377 if (charpos == pos)
7379 *hpos = i;
7380 *vpos = best_row_vpos;
7381 *x = current_x;
7382 *y = best_row->y;
7383 return 1;
7385 else if (charpos > pos)
7386 break;
7388 else if (EQ (glyph->object, stop))
7389 break;
7391 if (charpos > 0)
7392 lastcol = i;
7393 current_x += glyph->pixel_width;
7396 /* If we're looking for the end of the buffer,
7397 and we didn't find it in the line we scanned,
7398 use the start of the following line. */
7399 if (maybe_next_line_p)
7401 ++best_row;
7402 ++best_row_vpos;
7403 lastcol = 0;
7404 current_x = best_row->x;
7407 *vpos = best_row_vpos;
7408 *hpos = lastcol + 1;
7409 *x = current_x;
7410 *y = best_row->y;
7411 return 0;
7414 #endif /* not 0 */
7417 /* Find the position of the glyph for position POS in OBJECT in
7418 window W's current matrix, and return in *X/*Y the pixel
7419 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7421 RIGHT_P non-zero means return the position of the right edge of the
7422 glyph, RIGHT_P zero means return the left edge position.
7424 If no glyph for POS exists in the matrix, return the position of
7425 the glyph with the next smaller position that is in the matrix, if
7426 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7427 exists in the matrix, return the position of the glyph with the
7428 next larger position in OBJECT.
7430 Value is non-zero if a glyph was found. */
7432 static int
7433 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7434 struct window *w;
7435 int pos;
7436 Lisp_Object object;
7437 int *hpos, *vpos, *x, *y;
7438 int right_p;
7440 int yb = window_text_bottom_y (w);
7441 struct glyph_row *r;
7442 struct glyph *best_glyph = NULL;
7443 struct glyph_row *best_row = NULL;
7444 int best_x = 0;
7446 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7447 r->enabled_p && r->y < yb;
7448 ++r)
7450 struct glyph *g = r->glyphs[TEXT_AREA];
7451 struct glyph *e = g + r->used[TEXT_AREA];
7452 int gx;
7454 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7455 if (EQ (g->object, object))
7457 if (g->charpos == pos)
7459 best_glyph = g;
7460 best_x = gx;
7461 best_row = r;
7462 goto found;
7464 else if (best_glyph == NULL
7465 || ((abs (g->charpos - pos)
7466 < abs (best_glyph->charpos - pos))
7467 && (right_p
7468 ? g->charpos < pos
7469 : g->charpos > pos)))
7471 best_glyph = g;
7472 best_x = gx;
7473 best_row = r;
7478 found:
7480 if (best_glyph)
7482 *x = best_x;
7483 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7485 if (right_p)
7487 *x += best_glyph->pixel_width;
7488 ++*hpos;
7491 *y = best_row->y;
7492 *vpos = best_row - w->current_matrix->rows;
7495 return best_glyph != NULL;
7499 /* Display the active region described by mouse_face_*
7500 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7502 static void
7503 show_mouse_face (dpyinfo, draw)
7504 struct w32_display_info *dpyinfo;
7505 enum draw_glyphs_face draw;
7507 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7508 struct frame *f = XFRAME (WINDOW_FRAME (w));
7510 if (/* If window is in the process of being destroyed, don't bother
7511 to do anything. */
7512 w->current_matrix != NULL
7513 /* Don't update mouse highlight if hidden */
7514 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7515 /* Recognize when we are called to operate on rows that don't exist
7516 anymore. This can happen when a window is split. */
7517 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7519 int phys_cursor_on_p = w->phys_cursor_on_p;
7520 struct glyph_row *row, *first, *last;
7522 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7523 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7525 for (row = first; row <= last && row->enabled_p; ++row)
7527 int start_hpos, end_hpos, start_x;
7529 /* For all but the first row, the highlight starts at column 0. */
7530 if (row == first)
7532 start_hpos = dpyinfo->mouse_face_beg_col;
7533 start_x = dpyinfo->mouse_face_beg_x;
7535 else
7537 start_hpos = 0;
7538 start_x = 0;
7541 if (row == last)
7542 end_hpos = dpyinfo->mouse_face_end_col;
7543 else
7544 end_hpos = row->used[TEXT_AREA];
7546 if (end_hpos > start_hpos)
7548 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7549 start_hpos, end_hpos, draw, 0);
7551 row->mouse_face_p
7552 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7556 /* When we've written over the cursor, arrange for it to
7557 be displayed again. */
7558 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7559 x_display_cursor (w, 1,
7560 w->phys_cursor.hpos, w->phys_cursor.vpos,
7561 w->phys_cursor.x, w->phys_cursor.y);
7564 #if 0 /* TODO: mouse cursor */
7565 /* Change the mouse cursor. */
7566 if (draw == DRAW_NORMAL_TEXT)
7567 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7568 f->output_data.x->text_cursor);
7569 else if (draw == DRAW_MOUSE_FACE)
7570 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7571 f->output_data.x->cross_cursor);
7572 else
7573 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7574 f->output_data.x->nontext_cursor);
7575 #endif
7578 /* Clear out the mouse-highlighted active region.
7579 Redraw it un-highlighted first. */
7581 static int
7582 clear_mouse_face (dpyinfo)
7583 struct w32_display_info *dpyinfo;
7585 int cleared = 0;
7587 if (! NILP (dpyinfo->mouse_face_window))
7589 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7590 cleared = 1;
7593 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7594 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7595 dpyinfo->mouse_face_window = Qnil;
7596 dpyinfo->mouse_face_overlay = Qnil;
7597 return cleared;
7601 /* Clear any mouse-face on window W. This function is part of the
7602 redisplay interface, and is called from try_window_id and similar
7603 functions to ensure the mouse-highlight is off. */
7605 static void
7606 x_clear_mouse_face (w)
7607 struct window *w;
7609 struct w32_display_info *dpyinfo
7610 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
7611 Lisp_Object window;
7613 BLOCK_INPUT;
7614 XSETWINDOW (window, w);
7615 if (EQ (window, dpyinfo->mouse_face_window))
7616 clear_mouse_face (dpyinfo);
7617 UNBLOCK_INPUT;
7621 /* Just discard the mouse face information for frame F, if any.
7622 This is used when the size of F is changed. */
7624 void
7625 cancel_mouse_face (f)
7626 FRAME_PTR f;
7628 Lisp_Object window;
7629 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7631 window = dpyinfo->mouse_face_window;
7632 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7634 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7635 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7636 dpyinfo->mouse_face_window = Qnil;
7640 static struct scroll_bar *x_window_to_scroll_bar ();
7641 static void x_scroll_bar_report_motion ();
7642 static void x_check_fullscreen P_ ((struct frame *));
7643 static void x_check_fullscreen_move P_ ((struct frame *));
7644 static int glyph_rect P_ ((struct frame *f, int, int, RECT *));
7647 /* Try to determine frame pixel position and size of the glyph under
7648 frame pixel coordinates X/Y on frame F . Return the position and
7649 size in *RECT. Value is non-zero if we could compute these
7650 values. */
7652 static int
7653 glyph_rect (f, x, y, rect)
7654 struct frame *f;
7655 int x, y;
7656 RECT *rect;
7658 Lisp_Object window;
7659 int part;
7661 window = window_from_coordinates (f, x, y, &part, 0);
7662 if (!NILP (window))
7664 struct window *w = XWINDOW (window);
7665 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7666 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7668 frame_to_window_pixel_xy (w, &x, &y);
7670 for (; r < end && r->enabled_p; ++r)
7671 if (r->y <= y && r->y + r->height > y)
7673 /* Found the row at y. */
7674 struct glyph *g = r->glyphs[TEXT_AREA];
7675 struct glyph *end = g + r->used[TEXT_AREA];
7676 int gx;
7678 rect->top = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7679 rect->bottom = rect->top + r->height;
7681 if (x < r->x)
7683 /* x is to the left of the first glyph in the row. */
7684 rect->left = XINT (w->left);
7685 rect->right = WINDOW_TO_FRAME_PIXEL_X (w, r->x);
7686 return 1;
7689 for (gx = r->x; g < end; gx += g->pixel_width, ++g)
7690 if (gx <= x && gx + g->pixel_width > x)
7692 /* x is on a glyph. */
7693 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7694 rect->right = rect->left + g->pixel_width;
7695 return 1;
7698 /* x is to the right of the last glyph in the row. */
7699 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7700 rect->right = XINT (w->left) + XINT (w->width);
7701 return 1;
7705 /* The y is not on any row. */
7706 return 0;
7709 /* Record the position of the mouse in last_mouse_glyph. */
7710 static void
7711 remember_mouse_glyph (f1, gx, gy)
7712 struct frame * f1;
7713 int gx, gy;
7715 if (!glyph_rect (f1, gx, gy, &last_mouse_glyph))
7717 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7718 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7720 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7721 round down even for negative values. */
7722 if (gx < 0)
7723 gx -= width - 1;
7724 if (gy < 0)
7725 gy -= height - 1;
7726 #if 0
7727 /* This was the original code from XTmouse_position, but it seems
7728 to give the position of the glyph diagonally next to the one
7729 the mouse is over. */
7730 gx = (gx + width - 1) / width * width;
7731 gy = (gy + height - 1) / height * height;
7732 #else
7733 gx = gx / width * width;
7734 gy = gy / height * height;
7735 #endif
7737 last_mouse_glyph.left = gx;
7738 last_mouse_glyph.top = gy;
7739 last_mouse_glyph.right = gx + width;
7740 last_mouse_glyph.bottom = gy + height;
7744 /* Return the current position of the mouse.
7745 *fp should be a frame which indicates which display to ask about.
7747 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7748 and *part to the frame, window, and scroll bar part that the mouse
7749 is over. Set *x and *y to the portion and whole of the mouse's
7750 position on the scroll bar.
7752 If the mouse movement started elsewhere, set *fp to the frame the
7753 mouse is on, *bar_window to nil, and *x and *y to the character cell
7754 the mouse is over.
7756 Set *time to the server time-stamp for the time at which the mouse
7757 was at this position.
7759 Don't store anything if we don't have a valid set of values to report.
7761 This clears the mouse_moved flag, so we can wait for the next mouse
7762 movement. */
7764 static void
7765 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
7766 FRAME_PTR *fp;
7767 int insist;
7768 Lisp_Object *bar_window;
7769 enum scroll_bar_part *part;
7770 Lisp_Object *x, *y;
7771 unsigned long *time;
7773 FRAME_PTR f1;
7775 BLOCK_INPUT;
7777 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7778 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7779 else
7781 POINT pt;
7783 Lisp_Object frame, tail;
7785 /* Clear the mouse-moved flag for every frame on this display. */
7786 FOR_EACH_FRAME (tail, frame)
7787 XFRAME (frame)->mouse_moved = 0;
7789 last_mouse_scroll_bar = Qnil;
7791 GetCursorPos (&pt);
7793 /* Now we have a position on the root; find the innermost window
7794 containing the pointer. */
7796 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7797 && FRAME_LIVE_P (last_mouse_frame))
7799 /* If mouse was grabbed on a frame, give coords for that frame
7800 even if the mouse is now outside it. */
7801 f1 = last_mouse_frame;
7803 else
7805 /* Is window under mouse one of our frames? */
7806 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
7807 WindowFromPoint (pt));
7810 /* If not, is it one of our scroll bars? */
7811 if (! f1)
7813 struct scroll_bar *bar
7814 = x_window_to_scroll_bar (WindowFromPoint (pt));
7816 if (bar)
7818 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7822 if (f1 == 0 && insist > 0)
7823 f1 = SELECTED_FRAME ();
7825 if (f1)
7827 /* Ok, we found a frame. Store all the values.
7828 last_mouse_glyph is a rectangle used to reduce the
7829 generation of mouse events. To not miss any motion
7830 events, we must divide the frame into rectangles of the
7831 size of the smallest character that could be displayed
7832 on it, i.e. into the same rectangles that matrices on
7833 the frame are divided into. */
7835 #if OLD_REDISPLAY_CODE
7836 int ignore1, ignore2;
7838 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7840 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
7841 &last_mouse_glyph,
7842 FRAME_W32_DISPLAY_INFO (f1)->grabbed
7843 || insist);
7844 #else
7845 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7846 remember_mouse_glyph (f1, pt.x, pt.y);
7847 #endif
7849 *bar_window = Qnil;
7850 *part = 0;
7851 *fp = f1;
7852 XSETINT (*x, pt.x);
7853 XSETINT (*y, pt.y);
7854 *time = last_mouse_movement_time;
7859 UNBLOCK_INPUT;
7863 /* Scroll bar support. */
7865 /* Given a window ID, find the struct scroll_bar which manages it.
7866 This can be called in GC, so we have to make sure to strip off mark
7867 bits. */
7869 static struct scroll_bar *
7870 x_window_to_scroll_bar (window_id)
7871 Window window_id;
7873 Lisp_Object tail;
7875 for (tail = Vframe_list;
7876 XGCTYPE (tail) == Lisp_Cons;
7877 tail = XCDR (tail))
7879 Lisp_Object frame, bar, condemned;
7881 frame = XCAR (tail);
7882 /* All elements of Vframe_list should be frames. */
7883 if (! GC_FRAMEP (frame))
7884 abort ();
7886 /* Scan this frame's scroll bar list for a scroll bar with the
7887 right window ID. */
7888 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7889 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7890 /* This trick allows us to search both the ordinary and
7891 condemned scroll bar lists with one loop. */
7892 ! GC_NILP (bar) || (bar = condemned,
7893 condemned = Qnil,
7894 ! GC_NILP (bar));
7895 bar = XSCROLL_BAR (bar)->next)
7896 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
7897 return XSCROLL_BAR (bar);
7900 return 0;
7905 /* Set the thumb size and position of scroll bar BAR. We are currently
7906 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7908 static void
7909 w32_set_scroll_bar_thumb (bar, portion, position, whole)
7910 struct scroll_bar *bar;
7911 int portion, position, whole;
7913 Window w = SCROLL_BAR_W32_WINDOW (bar);
7914 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7915 int sb_page, sb_pos;
7916 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
7918 if (whole)
7920 /* Position scroll bar at rock bottom if the bottom of the
7921 buffer is visible. This avoids shinking the thumb away
7922 to nothing if it is held at the bottom of the buffer. */
7923 if (position + portion >= whole)
7925 sb_page = range * (whole - position) / whole
7926 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7927 sb_pos = range;
7930 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7931 sb_pos = position * range / whole;
7933 else
7935 sb_page = range;
7936 sb_pos = 0;
7939 BLOCK_INPUT;
7941 if (pfnSetScrollInfo)
7943 SCROLLINFO si;
7945 si.cbSize = sizeof (si);
7946 /* Only update page size if currently dragging, to reduce
7947 flicker effects. */
7948 if (draggingp)
7949 si.fMask = SIF_PAGE;
7950 else
7951 si.fMask = SIF_PAGE | SIF_POS;
7952 si.nPage = sb_page;
7953 si.nPos = sb_pos;
7955 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
7957 else
7958 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
7960 UNBLOCK_INPUT;
7964 /************************************************************************
7965 Scroll bars, general
7966 ************************************************************************/
7968 HWND
7969 my_create_scrollbar (f, bar)
7970 struct frame * f;
7971 struct scroll_bar * bar;
7973 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
7974 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
7975 (LPARAM) bar);
7978 /*#define ATTACH_THREADS*/
7980 BOOL
7981 my_show_window (FRAME_PTR f, HWND hwnd, int how)
7983 #ifndef ATTACH_THREADS
7984 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
7985 (WPARAM) hwnd, (LPARAM) how);
7986 #else
7987 return ShowWindow (hwnd, how);
7988 #endif
7991 void
7992 my_set_window_pos (HWND hwnd, HWND hwndAfter,
7993 int x, int y, int cx, int cy, UINT flags)
7995 #ifndef ATTACH_THREADS
7996 WINDOWPOS pos;
7997 pos.hwndInsertAfter = hwndAfter;
7998 pos.x = x;
7999 pos.y = y;
8000 pos.cx = cx;
8001 pos.cy = cy;
8002 pos.flags = flags;
8003 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
8004 #else
8005 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
8006 #endif
8009 void
8010 my_set_focus (f, hwnd)
8011 struct frame * f;
8012 HWND hwnd;
8014 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
8015 (WPARAM) hwnd, 0);
8018 void
8019 my_set_foreground_window (hwnd)
8020 HWND hwnd;
8022 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
8025 void
8026 my_destroy_window (f, hwnd)
8027 struct frame * f;
8028 HWND hwnd;
8030 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
8031 (WPARAM) hwnd, 0);
8034 /* Create a scroll bar and return the scroll bar vector for it. W is
8035 the Emacs window on which to create the scroll bar. TOP, LEFT,
8036 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8037 scroll bar. */
8039 static struct scroll_bar *
8040 x_scroll_bar_create (w, top, left, width, height)
8041 struct window *w;
8042 int top, left, width, height;
8044 struct frame *f = XFRAME (WINDOW_FRAME (w));
8045 HWND hwnd;
8046 struct scroll_bar *bar
8047 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8049 BLOCK_INPUT;
8051 XSETWINDOW (bar->window, w);
8052 XSETINT (bar->top, top);
8053 XSETINT (bar->left, left);
8054 XSETINT (bar->width, width);
8055 XSETINT (bar->height, height);
8056 XSETINT (bar->start, 0);
8057 XSETINT (bar->end, 0);
8058 bar->dragging = Qnil;
8060 /* Requires geometry to be set before call to create the real window */
8062 hwnd = my_create_scrollbar (f, bar);
8064 if (pfnSetScrollInfo)
8066 SCROLLINFO si;
8068 si.cbSize = sizeof (si);
8069 si.fMask = SIF_ALL;
8070 si.nMin = 0;
8071 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
8072 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
8073 si.nPage = si.nMax;
8074 si.nPos = 0;
8076 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
8078 else
8080 SetScrollRange (hwnd, SB_CTL, 0,
8081 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
8082 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
8085 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
8087 /* Add bar to its frame's list of scroll bars. */
8088 bar->next = FRAME_SCROLL_BARS (f);
8089 bar->prev = Qnil;
8090 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8091 if (! NILP (bar->next))
8092 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8094 UNBLOCK_INPUT;
8096 return bar;
8100 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8101 nil. */
8103 static void
8104 x_scroll_bar_remove (bar)
8105 struct scroll_bar *bar;
8107 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8109 BLOCK_INPUT;
8111 /* Destroy the window. */
8112 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
8114 /* Disassociate this scroll bar from its window. */
8115 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8117 UNBLOCK_INPUT;
8120 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8121 that we are displaying PORTION characters out of a total of WHOLE
8122 characters, starting at POSITION. If WINDOW has no scroll bar,
8123 create one. */
8124 static void
8125 w32_set_vertical_scroll_bar (w, portion, whole, position)
8126 struct window *w;
8127 int portion, whole, position;
8129 struct frame *f = XFRAME (w->frame);
8130 struct scroll_bar *bar;
8131 int top, height, left, sb_left, width, sb_width;
8132 int window_x, window_y, window_width, window_height;
8134 /* Get window dimensions. */
8135 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8136 top = window_y;
8137 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8138 height = window_height;
8140 /* Compute the left edge of the scroll bar area. */
8141 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8142 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8143 else
8144 left = XFASTINT (w->left);
8145 left *= CANON_X_UNIT (f);
8146 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8148 /* Compute the width of the scroll bar which might be less than
8149 the width of the area reserved for the scroll bar. */
8150 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8151 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8152 else
8153 sb_width = width;
8155 /* Compute the left edge of the scroll bar. */
8156 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8157 sb_left = left + width - sb_width - (width - sb_width) / 2;
8158 else
8159 sb_left = left + (width - sb_width) / 2;
8161 /* Does the scroll bar exist yet? */
8162 if (NILP (w->vertical_scroll_bar))
8164 HDC hdc;
8165 BLOCK_INPUT;
8166 if (width > 0 && height > 0)
8168 hdc = get_frame_dc (f);
8169 w32_clear_area (f, hdc, left, top, width, height);
8170 release_frame_dc (f, hdc);
8172 UNBLOCK_INPUT;
8174 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8176 else
8178 /* It may just need to be moved and resized. */
8179 HWND hwnd;
8181 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8182 hwnd = SCROLL_BAR_W32_WINDOW (bar);
8184 /* If already correctly positioned, do nothing. */
8185 if ( XINT (bar->left) == sb_left
8186 && XINT (bar->top) == top
8187 && XINT (bar->width) == sb_width
8188 && XINT (bar->height) == height )
8190 /* Redraw after clear_frame. */
8191 if (!my_show_window (f, hwnd, SW_NORMAL))
8192 InvalidateRect (hwnd, NULL, FALSE);
8194 else
8196 HDC hdc;
8197 BLOCK_INPUT;
8198 if (width && height)
8200 hdc = get_frame_dc (f);
8201 /* Since Windows scroll bars are smaller than the space reserved
8202 for them on the frame, we have to clear "under" them. */
8203 w32_clear_area (f, hdc,
8204 left,
8205 top,
8206 width,
8207 height);
8208 release_frame_dc (f, hdc);
8210 /* Make sure scroll bar is "visible" before moving, to ensure the
8211 area of the parent window now exposed will be refreshed. */
8212 my_show_window (f, hwnd, SW_HIDE);
8213 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8214 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8215 max (height, 1), TRUE);
8216 if (pfnSetScrollInfo)
8218 SCROLLINFO si;
8220 si.cbSize = sizeof (si);
8221 si.fMask = SIF_RANGE;
8222 si.nMin = 0;
8223 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
8224 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
8226 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
8228 else
8229 SetScrollRange (hwnd, SB_CTL, 0,
8230 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
8231 my_show_window (f, hwnd, SW_NORMAL);
8232 /* InvalidateRect (w, NULL, FALSE); */
8234 /* Remember new settings. */
8235 XSETINT (bar->left, sb_left);
8236 XSETINT (bar->top, top);
8237 XSETINT (bar->width, sb_width);
8238 XSETINT (bar->height, height);
8240 UNBLOCK_INPUT;
8243 w32_set_scroll_bar_thumb (bar, portion, position, whole);
8245 XSETVECTOR (w->vertical_scroll_bar, bar);
8249 /* The following three hooks are used when we're doing a thorough
8250 redisplay of the frame. We don't explicitly know which scroll bars
8251 are going to be deleted, because keeping track of when windows go
8252 away is a real pain - "Can you say set-window-configuration, boys
8253 and girls?" Instead, we just assert at the beginning of redisplay
8254 that *all* scroll bars are to be removed, and then save a scroll bar
8255 from the fiery pit when we actually redisplay its window. */
8257 /* Arrange for all scroll bars on FRAME to be removed at the next call
8258 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8259 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8261 static void
8262 w32_condemn_scroll_bars (frame)
8263 FRAME_PTR frame;
8265 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8266 while (! NILP (FRAME_SCROLL_BARS (frame)))
8268 Lisp_Object bar;
8269 bar = FRAME_SCROLL_BARS (frame);
8270 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8271 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8272 XSCROLL_BAR (bar)->prev = Qnil;
8273 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8274 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8275 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8280 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8281 Note that WINDOW isn't necessarily condemned at all. */
8283 static void
8284 w32_redeem_scroll_bar (window)
8285 struct window *window;
8287 struct scroll_bar *bar;
8288 struct frame *f;
8290 /* We can't redeem this window's scroll bar if it doesn't have one. */
8291 if (NILP (window->vertical_scroll_bar))
8292 abort ();
8294 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8296 /* Unlink it from the condemned list. */
8297 f = XFRAME (WINDOW_FRAME (window));
8298 if (NILP (bar->prev))
8300 /* If the prev pointer is nil, it must be the first in one of
8301 the lists. */
8302 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8303 /* It's not condemned. Everything's fine. */
8304 return;
8305 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8306 window->vertical_scroll_bar))
8307 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8308 else
8309 /* If its prev pointer is nil, it must be at the front of
8310 one or the other! */
8311 abort ();
8313 else
8314 XSCROLL_BAR (bar->prev)->next = bar->next;
8316 if (! NILP (bar->next))
8317 XSCROLL_BAR (bar->next)->prev = bar->prev;
8319 bar->next = FRAME_SCROLL_BARS (f);
8320 bar->prev = Qnil;
8321 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8322 if (! NILP (bar->next))
8323 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8326 /* Remove all scroll bars on FRAME that haven't been saved since the
8327 last call to `*condemn_scroll_bars_hook'. */
8329 static void
8330 w32_judge_scroll_bars (f)
8331 FRAME_PTR f;
8333 Lisp_Object bar, next;
8335 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8337 /* Clear out the condemned list now so we won't try to process any
8338 more events on the hapless scroll bars. */
8339 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8341 for (; ! NILP (bar); bar = next)
8343 struct scroll_bar *b = XSCROLL_BAR (bar);
8345 x_scroll_bar_remove (b);
8347 next = b->next;
8348 b->next = b->prev = Qnil;
8351 /* Now there should be no references to the condemned scroll bars,
8352 and they should get garbage-collected. */
8355 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8356 is set to something other than NO_EVENT, it is enqueued.
8358 This may be called from a signal handler, so we have to ignore GC
8359 mark bits. */
8361 static int
8362 w32_scroll_bar_handle_click (bar, msg, emacs_event)
8363 struct scroll_bar *bar;
8364 W32Msg *msg;
8365 struct input_event *emacs_event;
8367 if (! GC_WINDOWP (bar->window))
8368 abort ();
8370 emacs_event->kind = W32_SCROLL_BAR_CLICK_EVENT;
8371 emacs_event->code = 0;
8372 /* not really meaningful to distinguish up/down */
8373 emacs_event->modifiers = msg->dwModifiers;
8374 emacs_event->frame_or_window = bar->window;
8375 emacs_event->arg = Qnil;
8376 emacs_event->timestamp = msg->msg.time;
8379 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8380 int y;
8381 int dragging = !NILP (bar->dragging);
8383 if (pfnGetScrollInfo)
8385 SCROLLINFO si;
8387 si.cbSize = sizeof (si);
8388 si.fMask = SIF_POS;
8390 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
8391 y = si.nPos;
8393 else
8394 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
8396 bar->dragging = Qnil;
8399 last_mouse_scroll_bar_pos = msg->msg.wParam;
8401 switch (LOWORD (msg->msg.wParam))
8403 case SB_LINEDOWN:
8404 emacs_event->part = scroll_bar_down_arrow;
8405 break;
8406 case SB_LINEUP:
8407 emacs_event->part = scroll_bar_up_arrow;
8408 break;
8409 case SB_PAGEUP:
8410 emacs_event->part = scroll_bar_above_handle;
8411 break;
8412 case SB_PAGEDOWN:
8413 emacs_event->part = scroll_bar_below_handle;
8414 break;
8415 case SB_TOP:
8416 emacs_event->part = scroll_bar_handle;
8417 y = 0;
8418 break;
8419 case SB_BOTTOM:
8420 emacs_event->part = scroll_bar_handle;
8421 y = top_range;
8422 break;
8423 case SB_THUMBTRACK:
8424 case SB_THUMBPOSITION:
8425 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
8426 y = HIWORD (msg->msg.wParam);
8427 bar->dragging = Qt;
8428 emacs_event->part = scroll_bar_handle;
8430 /* "Silently" update current position. */
8431 if (pfnSetScrollInfo)
8433 SCROLLINFO si;
8435 si.cbSize = sizeof (si);
8436 si.fMask = SIF_POS;
8437 si.nPos = y;
8438 /* Remember apparent position (we actually lag behind the real
8439 position, so don't set that directly. */
8440 last_scroll_bar_drag_pos = y;
8442 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
8444 else
8445 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
8446 break;
8447 case SB_ENDSCROLL:
8448 /* If this is the end of a drag sequence, then reset the scroll
8449 handle size to normal and do a final redraw. Otherwise do
8450 nothing. */
8451 if (dragging)
8453 if (pfnSetScrollInfo)
8455 SCROLLINFO si;
8456 int start = XINT (bar->start);
8457 int end = XINT (bar->end);
8459 si.cbSize = sizeof (si);
8460 si.fMask = SIF_PAGE | SIF_POS;
8461 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
8462 si.nPos = last_scroll_bar_drag_pos;
8463 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
8465 else
8466 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
8468 /* fall through */
8469 default:
8470 emacs_event->kind = NO_EVENT;
8471 return FALSE;
8474 XSETINT (emacs_event->x, y);
8475 XSETINT (emacs_event->y, top_range);
8477 return TRUE;
8481 /* Return information to the user about the current position of the mouse
8482 on the scroll bar. */
8484 static void
8485 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8486 FRAME_PTR *fp;
8487 Lisp_Object *bar_window;
8488 enum scroll_bar_part *part;
8489 Lisp_Object *x, *y;
8490 unsigned long *time;
8492 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8493 Window w = SCROLL_BAR_W32_WINDOW (bar);
8494 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8495 int pos;
8496 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8498 BLOCK_INPUT;
8500 *fp = f;
8501 *bar_window = bar->window;
8503 if (pfnGetScrollInfo)
8505 SCROLLINFO si;
8507 si.cbSize = sizeof (si);
8508 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
8510 pfnGetScrollInfo (w, SB_CTL, &si);
8511 pos = si.nPos;
8512 top_range = si.nMax - si.nPage + 1;
8514 else
8515 pos = GetScrollPos (w, SB_CTL);
8517 switch (LOWORD (last_mouse_scroll_bar_pos))
8519 case SB_THUMBPOSITION:
8520 case SB_THUMBTRACK:
8521 *part = scroll_bar_handle;
8522 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
8523 pos = HIWORD (last_mouse_scroll_bar_pos);
8524 break;
8525 case SB_LINEDOWN:
8526 *part = scroll_bar_handle;
8527 pos++;
8528 break;
8529 default:
8530 *part = scroll_bar_handle;
8531 break;
8534 XSETINT (*x, pos);
8535 XSETINT (*y, top_range);
8537 f->mouse_moved = 0;
8538 last_mouse_scroll_bar = Qnil;
8540 *time = last_mouse_movement_time;
8542 UNBLOCK_INPUT;
8546 /* The screen has been cleared so we may have changed foreground or
8547 background colors, and the scroll bars may need to be redrawn.
8548 Clear out the scroll bars, and ask for expose events, so we can
8549 redraw them. */
8551 void
8552 x_scroll_bar_clear (f)
8553 FRAME_PTR f;
8555 Lisp_Object bar;
8557 /* We can have scroll bars even if this is 0,
8558 if we just turned off scroll bar mode.
8559 But in that case we should not clear them. */
8560 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8561 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8562 bar = XSCROLL_BAR (bar)->next)
8564 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
8565 HDC hdc = GetDC (window);
8566 RECT rect;
8568 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
8569 arranges to refresh the scroll bar if hidden. */
8570 my_show_window (f, window, SW_HIDE);
8572 GetClientRect (window, &rect);
8573 select_palette (f, hdc);
8574 w32_clear_rect (f, hdc, &rect);
8575 deselect_palette (f, hdc);
8577 ReleaseDC (window, hdc);
8582 /* The main W32 event-reading loop - w32_read_socket. */
8584 /* Record the last 100 characters stored
8585 to help debug the loss-of-chars-during-GC problem. */
8587 static int temp_index;
8588 static short temp_buffer[100];
8591 /* Read events coming from the W32 shell.
8592 This routine is called by the SIGIO handler.
8593 We return as soon as there are no more events to be read.
8595 Events representing keys are stored in buffer BUFP,
8596 which can hold up to NUMCHARS characters.
8597 We return the number of characters stored into the buffer,
8598 thus pretending to be `read'.
8600 EXPECTED is nonzero if the caller knows input is available.
8602 Some of these messages are reposted back to the message queue since the
8603 system calls the windows proc directly in a context where we cannot return
8604 the data nor can we guarantee the state we are in. So if we dispatch them
8605 we will get into an infinite loop. To prevent this from ever happening we
8606 will set a variable to indicate we are in the read_socket call and indicate
8607 which message we are processing since the windows proc gets called
8608 recursively with different messages by the system.
8612 w32_read_socket (sd, bufp, numchars, expected)
8613 register int sd;
8614 /* register */ struct input_event *bufp;
8615 /* register */ int numchars;
8616 int expected;
8618 int count = 0;
8619 int check_visibility = 0;
8620 W32Msg msg;
8621 struct frame *f;
8622 struct w32_display_info *dpyinfo = &one_w32_display_info;
8624 if (interrupt_input_blocked)
8626 interrupt_input_pending = 1;
8627 return -1;
8630 interrupt_input_pending = 0;
8631 BLOCK_INPUT;
8633 /* So people can tell when we have read the available input. */
8634 input_signal_count++;
8636 if (numchars <= 0)
8637 abort (); /* Don't think this happens. */
8639 /* TODO: tool-bars, ghostscript integration, mouse
8640 cursors. */
8641 while (get_next_msg (&msg, FALSE))
8643 switch (msg.msg.message)
8645 case WM_PAINT:
8646 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8648 if (f)
8650 if (msg.rect.right == msg.rect.left ||
8651 msg.rect.bottom == msg.rect.top)
8653 /* We may get paint messages even though the client
8654 area is clipped - these are not expose events. */
8655 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
8656 SDATA (f->name)));
8658 else if (f->async_visible != 1)
8660 /* Definitely not obscured, so mark as visible. */
8661 f->async_visible = 1;
8662 f->async_iconified = 0;
8663 SET_FRAME_GARBAGED (f);
8664 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
8665 SDATA (f->name)));
8667 /* WM_PAINT serves as MapNotify as well, so report
8668 visibility changes properly. */
8669 if (f->iconified)
8671 bufp->kind = DEICONIFY_EVENT;
8672 XSETFRAME (bufp->frame_or_window, f);
8673 bufp->arg = Qnil;
8674 bufp++;
8675 count++;
8676 numchars--;
8678 else if (! NILP (Vframe_list)
8679 && ! NILP (XCDR (Vframe_list)))
8680 /* Force a redisplay sooner or later to update the
8681 frame titles in case this is the second frame. */
8682 record_asynch_buffer_change ();
8684 else
8686 HDC hdc = get_frame_dc (f);
8688 /* Erase background again for safety. */
8689 w32_clear_rect (f, hdc, &msg.rect);
8690 release_frame_dc (f, hdc);
8691 expose_frame (f,
8692 msg.rect.left,
8693 msg.rect.top,
8694 msg.rect.right - msg.rect.left,
8695 msg.rect.bottom - msg.rect.top);
8698 break;
8700 case WM_INPUTLANGCHANGE:
8701 /* Generate a language change event. */
8702 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8704 if (f)
8706 if (numchars == 0)
8707 abort ();
8709 bufp->kind = LANGUAGE_CHANGE_EVENT;
8710 XSETFRAME (bufp->frame_or_window, f);
8711 bufp->arg = Qnil;
8712 bufp->code = msg.msg.wParam;
8713 bufp->modifiers = msg.msg.lParam & 0xffff;
8714 bufp++;
8715 count++;
8716 numchars--;
8718 break;
8720 case WM_KEYDOWN:
8721 case WM_SYSKEYDOWN:
8722 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8724 if (f && !f->iconified)
8726 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
8728 dpyinfo->mouse_face_hidden = 1;
8729 clear_mouse_face (dpyinfo);
8732 if (temp_index == sizeof temp_buffer / sizeof (short))
8733 temp_index = 0;
8734 temp_buffer[temp_index++] = msg.msg.wParam;
8735 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
8736 bufp->code = msg.msg.wParam;
8737 bufp->modifiers = msg.dwModifiers;
8738 XSETFRAME (bufp->frame_or_window, f);
8739 bufp->arg = Qnil;
8740 bufp->timestamp = msg.msg.time;
8741 bufp++;
8742 numchars--;
8743 count++;
8745 break;
8747 case WM_SYSCHAR:
8748 case WM_CHAR:
8749 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8751 if (f && !f->iconified)
8753 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
8755 dpyinfo->mouse_face_hidden = 1;
8756 clear_mouse_face (dpyinfo);
8759 if (temp_index == sizeof temp_buffer / sizeof (short))
8760 temp_index = 0;
8761 temp_buffer[temp_index++] = msg.msg.wParam;
8762 bufp->kind = ASCII_KEYSTROKE_EVENT;
8763 bufp->code = msg.msg.wParam;
8764 bufp->modifiers = msg.dwModifiers;
8765 XSETFRAME (bufp->frame_or_window, f);
8766 bufp->arg = Qnil;
8767 bufp->timestamp = msg.msg.time;
8768 bufp++;
8769 numchars--;
8770 count++;
8772 break;
8774 case WM_MOUSEMOVE:
8775 /* Ignore non-movement. */
8777 int x = LOWORD (msg.msg.lParam);
8778 int y = HIWORD (msg.msg.lParam);
8779 if (x == last_mousemove_x && y == last_mousemove_y)
8780 break;
8781 last_mousemove_x = x;
8782 last_mousemove_y = y;
8785 previous_help_echo = help_echo;
8787 if (dpyinfo->grabbed && last_mouse_frame
8788 && FRAME_LIVE_P (last_mouse_frame))
8789 f = last_mouse_frame;
8790 else
8791 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8793 if (dpyinfo->mouse_face_hidden)
8795 dpyinfo->mouse_face_hidden = 0;
8796 clear_mouse_face (dpyinfo);
8799 if (f)
8800 note_mouse_movement (f, &msg.msg);
8801 else
8803 /* If we move outside the frame, then we're
8804 certainly no longer on any text in the frame. */
8805 clear_mouse_face (dpyinfo);
8808 /* If the contents of the global variable help_echo
8809 has changed, generate a HELP_EVENT. */
8810 if (help_echo != previous_help_echo ||
8811 (!NILP (help_echo) && !STRINGP (help_echo) && f->mouse_moved))
8813 Lisp_Object frame;
8814 int n;
8816 if (help_echo == Qnil)
8818 help_echo_object = help_echo_window = Qnil;
8819 help_echo_pos = -1;
8822 if (f)
8823 XSETFRAME (frame, f);
8824 else
8825 frame = Qnil;
8827 any_help_event_p = 1;
8828 n = gen_help_event (bufp, numchars, help_echo, frame,
8829 help_echo_window, help_echo_object,
8830 help_echo_pos);
8831 bufp += n, count += n, numchars -= n;
8833 break;
8835 case WM_LBUTTONDOWN:
8836 case WM_LBUTTONUP:
8837 case WM_MBUTTONDOWN:
8838 case WM_MBUTTONUP:
8839 case WM_RBUTTONDOWN:
8840 case WM_RBUTTONUP:
8841 case WM_XBUTTONDOWN:
8842 case WM_XBUTTONUP:
8844 /* If we decide we want to generate an event to be seen
8845 by the rest of Emacs, we put it here. */
8846 struct input_event emacs_event;
8847 int tool_bar_p = 0;
8848 int button;
8849 int up;
8851 emacs_event.kind = NO_EVENT;
8853 if (dpyinfo->grabbed && last_mouse_frame
8854 && FRAME_LIVE_P (last_mouse_frame))
8855 f = last_mouse_frame;
8856 else
8857 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8859 if (f)
8861 construct_mouse_click (&emacs_event, &msg, f);
8863 /* Is this in the tool-bar? */
8864 if (WINDOWP (f->tool_bar_window)
8865 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
8867 Lisp_Object window;
8868 int p, x, y;
8870 x = XFASTINT (emacs_event.x);
8871 y = XFASTINT (emacs_event.y);
8873 /* Set x and y. */
8874 window = window_from_coordinates (f, x, y, &p, 1);
8876 if (EQ (window, f->tool_bar_window))
8878 w32_handle_tool_bar_click (f, &emacs_event);
8879 tool_bar_p = 1;
8883 if (!tool_bar_p)
8884 if (!dpyinfo->w32_focus_frame
8885 || f == dpyinfo->w32_focus_frame
8886 && (numchars >= 1))
8888 construct_mouse_click (bufp, &msg, f);
8889 bufp++;
8890 count++;
8891 numchars--;
8895 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
8896 &button, &up);
8898 if (up)
8900 dpyinfo->grabbed &= ~ (1 << button);
8902 else
8904 dpyinfo->grabbed |= (1 << button);
8905 last_mouse_frame = f;
8906 /* Ignore any mouse motion that happened
8907 before this event; any subsequent mouse-movement
8908 Emacs events should reflect only motion after
8909 the ButtonPress. */
8910 if (f != 0)
8911 f->mouse_moved = 0;
8913 if (!tool_bar_p)
8914 last_tool_bar_item = -1;
8916 break;
8919 case WM_MOUSEWHEEL:
8920 if (dpyinfo->grabbed && last_mouse_frame
8921 && FRAME_LIVE_P (last_mouse_frame))
8922 f = last_mouse_frame;
8923 else
8924 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8926 if (f)
8928 if ((!dpyinfo->w32_focus_frame
8929 || f == dpyinfo->w32_focus_frame)
8930 && (numchars >= 1))
8932 construct_mouse_wheel (bufp, &msg, f);
8933 bufp++;
8934 count++;
8935 numchars--;
8938 break;
8940 case WM_DROPFILES:
8941 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8943 if (f)
8945 construct_drag_n_drop (bufp, &msg, f);
8946 bufp++;
8947 count++;
8948 numchars--;
8950 break;
8952 case WM_VSCROLL:
8954 struct scroll_bar *bar =
8955 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
8957 if (bar && numchars >= 1)
8959 if (w32_scroll_bar_handle_click (bar, &msg, bufp))
8961 bufp++;
8962 count++;
8963 numchars--;
8966 break;
8969 case WM_WINDOWPOSCHANGED:
8970 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8971 if (f)
8973 x_check_fullscreen_move(f);
8974 if (f->output_data.w32->want_fullscreen & FULLSCREEN_WAIT)
8975 f->output_data.w32->want_fullscreen &=
8976 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
8978 check_visibility = 1;
8979 break;
8981 case WM_ACTIVATE:
8982 case WM_ACTIVATEAPP:
8983 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8984 if (f)
8985 x_check_fullscreen (f);
8986 check_visibility = 1;
8987 break;
8989 case WM_MOVE:
8990 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8992 if (f && !f->async_iconified)
8994 int x, y;
8996 x_real_positions (f, &x, &y);
8997 f->output_data.w32->left_pos = x;
8998 f->output_data.w32->top_pos = y;
9001 check_visibility = 1;
9002 break;
9004 case WM_SHOWWINDOW:
9005 /* wParam non-zero means Window is about to be shown, 0 means
9006 about to be hidden. */
9007 /* Redo the mouse-highlight after the tooltip has gone. */
9008 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
9010 tip_window = NULL;
9011 redo_mouse_highlight ();
9014 /* If window has been obscured or exposed by another window
9015 being maximised or minimised/restored, then recheck
9016 visibility of all frames. Direct changes to our own
9017 windows get handled by WM_SIZE. */
9018 #if 0
9019 if (msg.msg.lParam != 0)
9020 check_visibility = 1;
9021 else
9023 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9024 f->async_visible = msg.msg.wParam;
9026 #endif
9028 check_visibility = 1;
9029 break;
9031 case WM_SIZE:
9032 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9034 /* Inform lisp of whether frame has been iconified etc. */
9035 if (f)
9037 switch (msg.msg.wParam)
9039 case SIZE_MINIMIZED:
9040 f->async_visible = 0;
9041 f->async_iconified = 1;
9043 bufp->kind = ICONIFY_EVENT;
9044 XSETFRAME (bufp->frame_or_window, f);
9045 bufp->arg = Qnil;
9046 bufp++;
9047 count++;
9048 numchars--;
9049 break;
9051 case SIZE_MAXIMIZED:
9052 case SIZE_RESTORED:
9053 f->async_visible = 1;
9054 f->async_iconified = 0;
9056 /* wait_reading_process_input will notice this and update
9057 the frame's display structures. */
9058 SET_FRAME_GARBAGED (f);
9060 if (f->iconified)
9062 int x, y;
9064 /* Reset top and left positions of the Window
9065 here since Windows sends a WM_MOVE message
9066 BEFORE telling us the Window is minimized
9067 when the Window is iconified, with 3000,3000
9068 as the co-ords. */
9069 x_real_positions (f, &x, &y);
9070 f->output_data.w32->left_pos = x;
9071 f->output_data.w32->top_pos = y;
9073 bufp->kind = DEICONIFY_EVENT;
9074 XSETFRAME (bufp->frame_or_window, f);
9075 bufp->arg = Qnil;
9076 bufp++;
9077 count++;
9078 numchars--;
9080 else if (! NILP (Vframe_list)
9081 && ! NILP (XCDR (Vframe_list)))
9082 /* Force a redisplay sooner or later
9083 to update the frame titles
9084 in case this is the second frame. */
9085 record_asynch_buffer_change ();
9086 break;
9090 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
9092 RECT rect;
9093 int rows;
9094 int columns;
9095 int width;
9096 int height;
9098 GetClientRect (msg.msg.hwnd, &rect);
9100 height = rect.bottom - rect.top;
9101 width = rect.right - rect.left;
9103 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
9104 columns = PIXEL_TO_CHAR_WIDTH (f, width);
9106 /* TODO: Clip size to the screen dimensions. */
9108 /* Even if the number of character rows and columns has
9109 not changed, the font size may have changed, so we need
9110 to check the pixel dimensions as well. */
9112 if (columns != f->width
9113 || rows != f->height
9114 || width != f->output_data.w32->pixel_width
9115 || height != f->output_data.w32->pixel_height)
9117 change_frame_size (f, rows, columns, 0, 1, 0);
9118 SET_FRAME_GARBAGED (f);
9119 cancel_mouse_face (f);
9120 f->output_data.w32->pixel_width = width;
9121 f->output_data.w32->pixel_height = height;
9122 f->output_data.w32->win_gravity = NorthWestGravity;
9126 check_visibility = 1;
9127 break;
9129 case WM_MOUSELEAVE:
9130 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
9131 if (f)
9133 if (f == dpyinfo->mouse_face_mouse_frame)
9135 /* If we move outside the frame, then we're
9136 certainly no longer on any text in the frame. */
9137 clear_mouse_face (dpyinfo);
9138 dpyinfo->mouse_face_mouse_frame = 0;
9141 /* Generate a nil HELP_EVENT to cancel a help-echo.
9142 Do it only if there's something to cancel.
9143 Otherwise, the startup message is cleared when
9144 the mouse leaves the frame. */
9145 if (any_help_event_p)
9147 Lisp_Object frame;
9148 int n;
9150 XSETFRAME (frame, f);
9151 help_echo = Qnil;
9152 n = gen_help_event (bufp, numchars,
9153 Qnil, frame, Qnil, Qnil, 0);
9154 bufp += n, count += n, numchars -= n;
9157 break;
9159 case WM_SETFOCUS:
9160 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
9162 dpyinfo->w32_focus_event_frame = f;
9164 if (f)
9165 x_new_focus_frame (dpyinfo, f);
9168 dpyinfo->grabbed = 0;
9169 check_visibility = 1;
9170 break;
9172 case WM_KILLFOCUS:
9173 /* TODO: some of this belongs in MOUSE_LEAVE */
9174 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
9176 if (f)
9178 if (f == dpyinfo->w32_focus_event_frame)
9179 dpyinfo->w32_focus_event_frame = 0;
9181 if (f == dpyinfo->w32_focus_frame)
9182 x_new_focus_frame (dpyinfo, 0);
9184 if (f == dpyinfo->mouse_face_mouse_frame)
9186 /* If we move outside the frame, then we're
9187 certainly no longer on any text in the frame. */
9188 clear_mouse_face (dpyinfo);
9189 dpyinfo->mouse_face_mouse_frame = 0;
9192 /* Generate a nil HELP_EVENT to cancel a help-echo.
9193 Do it only if there's something to cancel.
9194 Otherwise, the startup message is cleared when
9195 the mouse leaves the frame. */
9196 if (any_help_event_p)
9198 Lisp_Object frame;
9199 int n;
9201 XSETFRAME (frame, f);
9202 help_echo = Qnil;
9203 n = gen_help_event (bufp, numchars,
9204 Qnil, frame, Qnil, Qnil, 0);
9205 bufp += n, count += n, numchars -=n;
9209 dpyinfo->grabbed = 0;
9210 check_visibility = 1;
9211 break;
9213 case WM_CLOSE:
9214 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9216 if (f)
9218 if (numchars == 0)
9219 abort ();
9221 bufp->kind = DELETE_WINDOW_EVENT;
9222 XSETFRAME (bufp->frame_or_window, f);
9223 bufp->arg = Qnil;
9224 bufp++;
9225 count++;
9226 numchars--;
9228 break;
9230 case WM_INITMENU:
9231 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9233 if (f)
9235 if (numchars == 0)
9236 abort ();
9238 bufp->kind = MENU_BAR_ACTIVATE_EVENT;
9239 XSETFRAME (bufp->frame_or_window, f);
9240 bufp->arg = Qnil;
9241 bufp++;
9242 count++;
9243 numchars--;
9245 break;
9247 case WM_COMMAND:
9248 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9250 if (f)
9252 extern void menubar_selection_callback
9253 (FRAME_PTR f, void * client_data);
9254 menubar_selection_callback (f, (void *)msg.msg.wParam);
9257 check_visibility = 1;
9258 break;
9260 case WM_DISPLAYCHANGE:
9261 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9263 if (f)
9265 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
9266 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
9267 dpyinfo->n_cbits = msg.msg.wParam;
9268 DebPrint (("display change: %d %d\n", dpyinfo->width,
9269 dpyinfo->height));
9272 check_visibility = 1;
9273 break;
9275 default:
9276 /* Check for messages registered at runtime. */
9277 if (msg.msg.message == msh_mousewheel)
9279 if (dpyinfo->grabbed && last_mouse_frame
9280 && FRAME_LIVE_P (last_mouse_frame))
9281 f = last_mouse_frame;
9282 else
9283 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9285 if (f)
9287 if ((!dpyinfo->w32_focus_frame
9288 || f == dpyinfo->w32_focus_frame)
9289 && (numchars >= 1))
9291 construct_mouse_wheel (bufp, &msg, f);
9292 bufp++;
9293 count++;
9294 numchars--;
9298 break;
9302 /* If the focus was just given to an autoraising frame,
9303 raise it now. */
9304 /* ??? This ought to be able to handle more than one such frame. */
9305 if (pending_autoraise_frame)
9307 x_raise_frame (pending_autoraise_frame);
9308 pending_autoraise_frame = 0;
9311 /* Check which frames are still visisble, if we have enqueued any user
9312 events or been notified of events that may affect visibility. We
9313 do this here because there doesn't seem to be any direct
9314 notification from Windows that the visibility of a window has
9315 changed (at least, not in all cases). */
9316 if (count > 0 || check_visibility)
9318 Lisp_Object tail, frame;
9320 FOR_EACH_FRAME (tail, frame)
9322 FRAME_PTR f = XFRAME (frame);
9323 /* The tooltip has been drawn already. Avoid the
9324 SET_FRAME_GARBAGED below. */
9325 if (EQ (frame, tip_frame))
9326 continue;
9328 /* Check "visible" frames and mark each as obscured or not.
9329 Note that async_visible is nonzero for unobscured and
9330 obscured frames, but zero for hidden and iconified frames. */
9331 if (FRAME_W32_P (f) && f->async_visible)
9333 RECT clipbox;
9334 HDC hdc;
9336 enter_crit ();
9337 /* Query clipping rectangle for the entire window area
9338 (GetWindowDC), not just the client portion (GetDC).
9339 Otherwise, the scrollbars and menubar aren't counted as
9340 part of the visible area of the frame, and we may think
9341 the frame is obscured when really a scrollbar is still
9342 visible and gets WM_PAINT messages above. */
9343 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
9344 GetClipBox (hdc, &clipbox);
9345 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
9346 leave_crit ();
9348 if (clipbox.right == clipbox.left
9349 || clipbox.bottom == clipbox.top)
9351 /* Frame has become completely obscured so mark as
9352 such (we do this by setting async_visible to 2 so
9353 that FRAME_VISIBLE_P is still true, but redisplay
9354 will skip it). */
9355 f->async_visible = 2;
9357 if (!FRAME_OBSCURED_P (f))
9359 DebPrint (("frame %p (%s) obscured\n", f,
9360 SDATA (f->name)));
9363 else
9365 /* Frame is not obscured, so mark it as such. */
9366 f->async_visible = 1;
9368 if (FRAME_OBSCURED_P (f))
9370 SET_FRAME_GARBAGED (f);
9371 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
9372 SDATA (f->name)));
9374 /* Force a redisplay sooner or later. */
9375 record_asynch_buffer_change ();
9382 UNBLOCK_INPUT;
9383 return count;
9389 /***********************************************************************
9390 Text Cursor
9391 ***********************************************************************/
9393 /* Notice if the text cursor of window W has been overwritten by a
9394 drawing operation that outputs glyphs starting at START_X and
9395 ending at END_X in the line given by output_cursor.vpos.
9396 Coordinates are area-relative. END_X < 0 means all the rest
9397 of the line after START_X has been written. */
9399 static void
9400 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
9401 struct window *w;
9402 enum glyph_row_area area;
9403 int x0, x1, y0, y1;
9405 if (area == TEXT_AREA
9406 && w->phys_cursor_on_p
9407 && y0 <= w->phys_cursor.y
9408 && y1 >= w->phys_cursor.y + w->phys_cursor_height
9409 && x0 <= w->phys_cursor.x
9410 && (x1 < 0 || x1 > w->phys_cursor.x))
9411 w->phys_cursor_on_p = 0;
9415 /* Set clipping for output in glyph row ROW. W is the window in which
9416 we operate. GC is the graphics context to set clipping in.
9417 WHOLE_LINE_P non-zero means include the areas used for truncation
9418 mark display and alike in the clipping rectangle.
9420 ROW may be a text row or, e.g., a mode line. Text rows must be
9421 clipped to the interior of the window dedicated to text display,
9422 mode lines must be clipped to the whole window. */
9424 static void
9425 w32_clip_to_row (w, row, hdc, whole_line_p)
9426 struct window *w;
9427 struct glyph_row *row;
9428 HDC hdc;
9429 int whole_line_p;
9431 struct frame *f = XFRAME (WINDOW_FRAME (w));
9432 RECT clip_rect;
9433 int window_x, window_y, window_width, window_height;
9435 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9437 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9438 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9439 clip_rect.top = max (clip_rect.top, window_y);
9440 clip_rect.right = clip_rect.left + window_width;
9441 clip_rect.bottom = clip_rect.top + row->visible_height;
9443 /* If clipping to the whole line, including trunc marks, extend
9444 the rectangle to the left and increase its width. */
9445 if (whole_line_p)
9447 clip_rect.left -= FRAME_X_LEFT_FRINGE_WIDTH (f);
9448 clip_rect.right += FRAME_X_FRINGE_WIDTH (f);
9451 w32_set_clip_rectangle (hdc, &clip_rect);
9455 /* Draw a hollow box cursor on window W in glyph row ROW. */
9457 static void
9458 x_draw_hollow_cursor (w, row)
9459 struct window *w;
9460 struct glyph_row *row;
9462 struct frame *f = XFRAME (WINDOW_FRAME (w));
9463 HDC hdc;
9464 RECT rect;
9465 int wd;
9466 struct glyph *cursor_glyph;
9467 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
9469 /* Compute frame-relative coordinates from window-relative
9470 coordinates. */
9471 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9472 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9473 + row->ascent - w->phys_cursor_ascent);
9474 rect.bottom = rect.top + row->height;
9476 /* Get the glyph the cursor is on. If we can't tell because
9477 the current matrix is invalid or such, give up. */
9478 cursor_glyph = get_phys_cursor_glyph (w);
9479 if (cursor_glyph == NULL)
9480 return;
9482 /* Compute the width of the rectangle to draw. If on a stretch
9483 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9484 rectangle as wide as the glyph, but use a canonical character
9485 width instead. */
9486 wd = cursor_glyph->pixel_width;
9487 if (cursor_glyph->type == STRETCH_GLYPH
9488 && !x_stretch_cursor_p)
9489 wd = min (CANON_X_UNIT (f), wd);
9491 rect.right = rect.left + wd;
9492 hdc = get_frame_dc (f);
9493 FrameRect (hdc, &rect, hb);
9494 DeleteObject (hb);
9496 release_frame_dc (f, hdc);
9500 /* Draw a bar cursor on window W in glyph row ROW.
9502 Implementation note: One would like to draw a bar cursor with an
9503 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9504 Unfortunately, I didn't find a font yet that has this property set.
9505 --gerd. */
9507 static void
9508 x_draw_bar_cursor (w, row, width, kind)
9509 struct window *w;
9510 struct glyph_row *row;
9511 int width;
9512 enum text_cursor_kinds kind;
9514 struct frame *f = XFRAME (w->frame);
9515 struct glyph *cursor_glyph;
9516 int x;
9517 HDC hdc;
9519 /* If cursor is out of bounds, don't draw garbage. This can happen
9520 in mini-buffer windows when switching between echo area glyphs
9521 and mini-buffer. */
9522 cursor_glyph = get_phys_cursor_glyph (w);
9523 if (cursor_glyph == NULL)
9524 return;
9526 /* If on an image, draw like a normal cursor. That's usually better
9527 visible than drawing a bar, esp. if the image is large so that
9528 the bar might not be in the window. */
9529 if (cursor_glyph->type == IMAGE_GLYPH)
9531 struct glyph_row *row;
9532 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9533 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
9535 else
9537 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
9538 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9540 if (width < 0)
9541 width = FRAME_CURSOR_WIDTH (f);
9542 width = min (cursor_glyph->pixel_width, width);
9544 /* If the glyph's background equals the color we normally draw
9545 the bar cursor in, the bar cursor in its normal color is
9546 invisible. Use the glyph's foreground color instead in this
9547 case, on the assumption that the glyph's colors are chosen so
9548 that the glyph is legible. */
9549 if (face->background == cursor_color)
9550 cursor_color = face->foreground;
9552 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9553 hdc = get_frame_dc (f);
9554 w32_clip_to_row (w, row, hdc, 0);
9556 if (kind == BAR_CURSOR)
9558 w32_fill_area (f, hdc, cursor_color, x,
9559 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9560 width, row->height);
9562 else
9564 w32_fill_area (f, hdc, cursor_color, x,
9565 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9566 row->height - width),
9567 cursor_glyph->pixel_width, width);
9569 release_frame_dc (f, hdc);
9574 /* Clear the cursor of window W to background color, and mark the
9575 cursor as not shown. This is used when the text where the cursor
9576 is is about to be rewritten. */
9578 static void
9579 x_clear_cursor (w)
9580 struct window *w;
9582 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9583 x_update_window_cursor (w, 0);
9587 /* Draw the cursor glyph of window W in glyph row ROW. See the
9588 comment of x_draw_glyphs for the meaning of HL. */
9590 static void
9591 x_draw_phys_cursor_glyph (w, row, hl)
9592 struct window *w;
9593 struct glyph_row *row;
9594 enum draw_glyphs_face hl;
9596 /* If cursor hpos is out of bounds, don't draw garbage. This can
9597 happen in mini-buffer windows when switching between echo area
9598 glyphs and mini-buffer. */
9599 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
9601 int on_p = w->phys_cursor_on_p;
9602 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9603 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
9604 hl, 0);
9605 w->phys_cursor_on_p = on_p;
9607 /* When we erase the cursor, and ROW is overlapped by other
9608 rows, make sure that these overlapping parts of other rows
9609 are redrawn. */
9610 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
9612 if (row > w->current_matrix->rows
9613 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9614 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9616 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9617 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9618 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9624 /* Erase the image of a cursor of window W from the screen. */
9626 static void
9627 x_erase_phys_cursor (w)
9628 struct window *w;
9630 struct frame *f = XFRAME (w->frame);
9631 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9632 int hpos = w->phys_cursor.hpos;
9633 int vpos = w->phys_cursor.vpos;
9634 int mouse_face_here_p = 0;
9635 struct glyph_matrix *active_glyphs = w->current_matrix;
9636 struct glyph_row *cursor_row;
9637 struct glyph *cursor_glyph;
9638 enum draw_glyphs_face hl;
9640 /* No cursor displayed or row invalidated => nothing to do on the
9641 screen. */
9642 if (w->phys_cursor_type == NO_CURSOR)
9643 goto mark_cursor_off;
9645 /* VPOS >= active_glyphs->nrows means that window has been resized.
9646 Don't bother to erase the cursor. */
9647 if (vpos >= active_glyphs->nrows)
9648 goto mark_cursor_off;
9650 /* If row containing cursor is marked invalid, there is nothing we
9651 can do. */
9652 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9653 if (!cursor_row->enabled_p)
9654 goto mark_cursor_off;
9656 /* If row is completely invisible, don't attempt to delete a cursor which
9657 isn't there. This may happen if cursor is at top of window, and
9658 we switch to a buffer with a header line in that window. */
9659 if (cursor_row->visible_height <= 0)
9660 goto mark_cursor_off;
9662 /* This can happen when the new row is shorter than the old one.
9663 In this case, either x_draw_glyphs or clear_end_of_line
9664 should have cleared the cursor. Note that we wouldn't be
9665 able to erase the cursor in this case because we don't have a
9666 cursor glyph at hand. */
9667 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9668 goto mark_cursor_off;
9670 /* If the cursor is in the mouse face area, redisplay that when
9671 we clear the cursor. */
9672 if (! NILP (dpyinfo->mouse_face_window)
9673 && w == XWINDOW (dpyinfo->mouse_face_window)
9674 && (vpos > dpyinfo->mouse_face_beg_row
9675 || (vpos == dpyinfo->mouse_face_beg_row
9676 && hpos >= dpyinfo->mouse_face_beg_col))
9677 && (vpos < dpyinfo->mouse_face_end_row
9678 || (vpos == dpyinfo->mouse_face_end_row
9679 && hpos < dpyinfo->mouse_face_end_col))
9680 /* Don't redraw the cursor's spot in mouse face if it is at the
9681 end of a line (on a newline). The cursor appears there, but
9682 mouse highlighting does not. */
9683 && cursor_row->used[TEXT_AREA] > hpos)
9684 mouse_face_here_p = 1;
9686 /* Maybe clear the display under the cursor. */
9687 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9689 int x;
9690 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9691 HDC hdc;
9693 cursor_glyph = get_phys_cursor_glyph (w);
9694 if (cursor_glyph == NULL)
9695 goto mark_cursor_off;
9697 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9699 hdc = get_frame_dc (f);
9700 w32_clear_area (f, hdc, x,
9701 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9702 cursor_row->y)),
9703 cursor_glyph->pixel_width,
9704 cursor_row->visible_height);
9705 release_frame_dc (f, hdc);
9708 /* Erase the cursor by redrawing the character underneath it. */
9709 if (mouse_face_here_p)
9710 hl = DRAW_MOUSE_FACE;
9711 else
9712 hl = DRAW_NORMAL_TEXT;
9713 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9715 mark_cursor_off:
9716 w->phys_cursor_on_p = 0;
9717 w->phys_cursor_type = NO_CURSOR;
9721 /* Non-zero if physical cursor of window W is within mouse face. */
9723 static int
9724 cursor_in_mouse_face_p (w)
9725 struct window *w;
9727 struct w32_display_info *dpyinfo
9728 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
9729 int in_mouse_face = 0;
9731 if (WINDOWP (dpyinfo->mouse_face_window)
9732 && XWINDOW (dpyinfo->mouse_face_window) == w)
9734 int hpos = w->phys_cursor.hpos;
9735 int vpos = w->phys_cursor.vpos;
9737 if (vpos >= dpyinfo->mouse_face_beg_row
9738 && vpos <= dpyinfo->mouse_face_end_row
9739 && (vpos > dpyinfo->mouse_face_beg_row
9740 || hpos >= dpyinfo->mouse_face_beg_col)
9741 && (vpos < dpyinfo->mouse_face_end_row
9742 || hpos < dpyinfo->mouse_face_end_col
9743 || dpyinfo->mouse_face_past_end))
9744 in_mouse_face = 1;
9747 return in_mouse_face;
9751 /* Display or clear cursor of window W. If ON is zero, clear the
9752 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9753 where to put the cursor is specified by HPOS, VPOS, X and Y. */
9755 void
9756 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9757 struct window *w;
9758 int on, hpos, vpos, x, y;
9760 struct frame *f = XFRAME (w->frame);
9761 int new_cursor_type;
9762 int new_cursor_width;
9763 int active_cursor;
9764 struct glyph_matrix *current_glyphs;
9765 struct glyph_row *glyph_row;
9766 struct glyph *glyph;
9768 /* This is pointless on invisible frames, and dangerous on garbaged
9769 windows and frames; in the latter case, the frame or window may
9770 be in the midst of changing its size, and x and y may be off the
9771 window. */
9772 if (! FRAME_VISIBLE_P (f)
9773 || FRAME_GARBAGED_P (f)
9774 || vpos >= w->current_matrix->nrows
9775 || hpos >= w->current_matrix->matrix_w)
9776 return;
9778 /* If cursor is off and we want it off, return quickly. */
9779 if (!on && !w->phys_cursor_on_p)
9780 return;
9782 current_glyphs = w->current_matrix;
9783 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9784 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9786 /* If cursor row is not enabled, we don't really know where to
9787 display the cursor. */
9788 if (!glyph_row->enabled_p)
9790 w->phys_cursor_on_p = 0;
9791 return;
9794 xassert (interrupt_input_blocked);
9796 /* Set new_cursor_type to the cursor we want to be displayed. */
9797 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
9799 /* If cursor is currently being shown and we don't want it to be or
9800 it is in the wrong place, or the cursor type is not what we want,
9801 erase it. */
9802 if (w->phys_cursor_on_p
9803 && (!on
9804 || w->phys_cursor.x != x
9805 || w->phys_cursor.y != y
9806 || new_cursor_type != w->phys_cursor_type
9807 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
9808 && new_cursor_width != w->phys_cursor_width)))
9809 x_erase_phys_cursor (w);
9811 /* Don't check phys_cursor_on_p here because that flag is only set
9812 to zero in some cases where we know that the cursor has been
9813 completely erased, to avoid the extra work of erasing the cursor
9814 twice. In other words, phys_cursor_on_p can be 1 and the cursor
9815 still not be visible, or it has only been partly erased. */
9816 if (on)
9818 w->phys_cursor_ascent = glyph_row->ascent;
9819 w->phys_cursor_height = glyph_row->height;
9821 /* Set phys_cursor_.* before x_draw_.* is called because some
9822 of them may need the information. */
9823 w->phys_cursor.x = x;
9824 w->phys_cursor.y = glyph_row->y;
9825 w->phys_cursor.hpos = hpos;
9826 w->phys_cursor.vpos = vpos;
9828 /* If the user wants to use the system caret, make sure our own
9829 cursor remains invisible. */
9830 if (w32_use_visible_system_caret)
9832 if (w->phys_cursor_type != NO_CURSOR)
9833 x_erase_phys_cursor (w);
9835 new_cursor_type = w->phys_cursor_type = NO_CURSOR;
9836 w->phys_cursor_width = -1;
9838 else
9840 w->phys_cursor_type = new_cursor_type;
9841 w->phys_cursor_width = new_cursor_width;
9844 w->phys_cursor_on_p = 1;
9846 /* If this is the active cursor, we need to track it with the
9847 system caret, so third party software like screen magnifiers
9848 and speech synthesizers can follow the cursor. */
9849 if (active_cursor)
9851 HWND hwnd = FRAME_W32_WINDOW (f);
9853 w32_system_caret_x
9854 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9855 w32_system_caret_y
9856 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9857 + glyph_row->ascent - w->phys_cursor_ascent);
9859 /* If the size of the active cursor changed, destroy the old
9860 system caret. */
9861 if (w32_system_caret_hwnd
9862 && (w32_system_caret_height != w->phys_cursor_height))
9863 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
9865 w32_system_caret_height = w->phys_cursor_height;
9867 /* Move the system caret. */
9868 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
9871 switch (new_cursor_type)
9873 case HOLLOW_BOX_CURSOR:
9874 x_draw_hollow_cursor (w, glyph_row);
9875 break;
9877 case FILLED_BOX_CURSOR:
9878 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9879 break;
9881 case BAR_CURSOR:
9882 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
9883 break;
9885 case HBAR_CURSOR:
9886 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
9887 break;
9889 case NO_CURSOR:
9890 break;
9892 default:
9893 abort ();
9899 /* Display the cursor on window W, or clear it. X and Y are window
9900 relative pixel coordinates. HPOS and VPOS are glyph matrix
9901 positions. If W is not the selected window, display a hollow
9902 cursor. ON non-zero means display the cursor at X, Y which
9903 correspond to HPOS, VPOS, otherwise it is cleared. */
9905 void
9906 x_display_cursor (w, on, hpos, vpos, x, y)
9907 struct window *w;
9908 int on, hpos, vpos, x, y;
9910 BLOCK_INPUT;
9911 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9912 UNBLOCK_INPUT;
9916 /* Display the cursor on window W, or clear it, according to ON_P.
9917 Don't change the cursor's position. */
9919 void
9920 x_update_cursor (f, on_p)
9921 struct frame *f;
9922 int on_p;
9924 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9928 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
9929 in the window tree rooted at W. */
9931 static void
9932 x_update_cursor_in_window_tree (w, on_p)
9933 struct window *w;
9934 int on_p;
9936 while (w)
9938 if (!NILP (w->hchild))
9939 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9940 else if (!NILP (w->vchild))
9941 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9942 else
9943 x_update_window_cursor (w, on_p);
9945 w = NILP (w->next) ? 0 : XWINDOW (w->next);
9950 /* Switch the display of W's cursor on or off, according to the value
9951 of ON. */
9953 static void
9954 x_update_window_cursor (w, on)
9955 struct window *w;
9956 int on;
9958 /* Don't update cursor in windows whose frame is in the process
9959 of being deleted. */
9960 if (w->current_matrix)
9962 BLOCK_INPUT;
9963 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9964 w->phys_cursor.vpos, w->phys_cursor.x,
9965 w->phys_cursor.y);
9966 UNBLOCK_INPUT;
9973 /* Icons. */
9976 x_bitmap_icon (f, icon)
9977 struct frame *f;
9978 Lisp_Object icon;
9980 HANDLE hicon;
9982 if (FRAME_W32_WINDOW (f) == 0)
9983 return 1;
9985 if (NILP (icon))
9986 hicon = LoadIcon (hinst, EMACS_CLASS);
9987 else if (STRINGP (icon))
9988 hicon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
9989 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9990 else if (SYMBOLP (icon))
9992 LPCTSTR name;
9994 if (EQ (icon, intern ("application")))
9995 name = (LPCTSTR) IDI_APPLICATION;
9996 else if (EQ (icon, intern ("hand")))
9997 name = (LPCTSTR) IDI_HAND;
9998 else if (EQ (icon, intern ("question")))
9999 name = (LPCTSTR) IDI_QUESTION;
10000 else if (EQ (icon, intern ("exclamation")))
10001 name = (LPCTSTR) IDI_EXCLAMATION;
10002 else if (EQ (icon, intern ("asterisk")))
10003 name = (LPCTSTR) IDI_ASTERISK;
10004 else if (EQ (icon, intern ("winlogo")))
10005 name = (LPCTSTR) IDI_WINLOGO;
10006 else
10007 return 1;
10009 hicon = LoadIcon (NULL, name);
10011 else
10012 return 1;
10014 if (hicon == NULL)
10015 return 1;
10017 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
10018 (LPARAM) hicon);
10020 return 0;
10024 /************************************************************************
10025 Handling X errors
10026 ************************************************************************/
10028 /* Display Error Handling functions not used on W32. Listing them here
10029 helps diff stay in step when comparing w32term.c with xterm.c.
10031 x_error_catcher (display, error)
10032 x_catch_errors (dpy)
10033 x_catch_errors_unwind (old_val)
10034 x_check_errors (dpy, format)
10035 x_had_errors_p (dpy)
10036 x_clear_errors (dpy)
10037 x_uncatch_errors (dpy, count)
10038 x_trace_wire ()
10039 x_connection_signal (signalnum)
10040 x_connection_closed (dpy, error_message)
10041 x_error_quitter (display, error)
10042 x_error_handler (display, error)
10043 x_io_error_quitter (display)
10048 /* Changing the font of the frame. */
10050 /* Give frame F the font named FONTNAME as its default font, and
10051 return the full name of that font. FONTNAME may be a wildcard
10052 pattern; in that case, we choose some font that fits the pattern.
10053 The return value shows which font we chose. */
10055 Lisp_Object
10056 x_new_font (f, fontname)
10057 struct frame *f;
10058 register char *fontname;
10060 struct font_info *fontp
10061 = FS_LOAD_FONT (f, 0, fontname, -1);
10063 if (!fontp)
10064 return Qnil;
10066 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
10067 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
10068 FRAME_FONTSET (f) = -1;
10070 /* Compute the scroll bar width in character columns. */
10071 if (f->scroll_bar_pixel_width > 0)
10073 int wid = FONT_WIDTH (FRAME_FONT (f));
10074 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10076 else
10078 int wid = FONT_WIDTH (FRAME_FONT (f));
10079 f->scroll_bar_cols = (14 + wid - 1) / wid;
10082 /* Now make the frame display the given font. */
10083 if (FRAME_W32_WINDOW (f) != 0)
10085 frame_update_line_height (f);
10086 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10087 x_set_window_size (f, 0, f->width, f->height);
10089 else
10090 /* If we are setting a new frame's font for the first time,
10091 there are no faces yet, so this font's height is the line height. */
10092 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
10094 return build_string (fontp->full_name);
10097 /* Give frame F the fontset named FONTSETNAME as its default font, and
10098 return the full name of that fontset. FONTSETNAME may be a wildcard
10099 pattern; in that case, we choose some fontset that fits the pattern.
10100 The return value shows which fontset we chose. */
10102 Lisp_Object
10103 x_new_fontset (f, fontsetname)
10104 struct frame *f;
10105 char *fontsetname;
10107 int fontset = fs_query_fontset (build_string (fontsetname), 0);
10108 Lisp_Object result;
10110 if (fontset < 0)
10111 return Qnil;
10113 if (FRAME_FONTSET (f) == fontset)
10114 /* This fontset is already set in frame F. There's nothing more
10115 to do. */
10116 return fontset_name (fontset);
10118 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
10120 if (!STRINGP (result))
10121 /* Can't load ASCII font. */
10122 return Qnil;
10124 /* Since x_new_font doesn't update any fontset information, do it now. */
10125 FRAME_FONTSET(f) = fontset;
10127 return build_string (fontsetname);
10130 /* Compute actual fringe widths */
10132 void
10133 x_compute_fringe_widths (f, redraw)
10134 struct frame *f;
10135 int redraw;
10137 int o_left = f->output_data.w32->left_fringe_width;
10138 int o_right = f->output_data.w32->right_fringe_width;
10139 int o_cols = f->output_data.w32->fringe_cols;
10141 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
10142 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
10143 int left_fringe_width, right_fringe_width;
10145 if (!NILP (left_fringe))
10146 left_fringe = Fcdr (left_fringe);
10147 if (!NILP (right_fringe))
10148 right_fringe = Fcdr (right_fringe);
10150 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
10151 XINT (left_fringe));
10152 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
10153 XINT (right_fringe));
10155 if (left_fringe_width || right_fringe_width)
10157 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
10158 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
10159 int conf_wid = left_wid + right_wid;
10160 int font_wid = FONT_WIDTH (f->output_data.w32->font);
10161 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
10162 int real_wid = cols * font_wid;
10163 if (left_wid && right_wid)
10165 if (left_fringe_width < 0)
10167 /* Left fringe width is fixed, adjust right fringe if necessary */
10168 f->output_data.w32->left_fringe_width = left_wid;
10169 f->output_data.w32->right_fringe_width = real_wid - left_wid;
10171 else if (right_fringe_width < 0)
10173 /* Right fringe width is fixed, adjust left fringe if necessary */
10174 f->output_data.w32->left_fringe_width = real_wid - right_wid;
10175 f->output_data.w32->right_fringe_width = right_wid;
10177 else
10179 /* Adjust both fringes with an equal amount.
10180 Note that we are doing integer arithmetic here, so don't
10181 lose a pixel if the total width is an odd number. */
10182 int fill = real_wid - conf_wid;
10183 f->output_data.w32->left_fringe_width = left_wid + fill/2;
10184 f->output_data.w32->right_fringe_width = right_wid + fill - fill/2;
10187 else if (left_fringe_width)
10189 f->output_data.w32->left_fringe_width = real_wid;
10190 f->output_data.w32->right_fringe_width = 0;
10192 else
10194 f->output_data.w32->left_fringe_width = 0;
10195 f->output_data.w32->right_fringe_width = real_wid;
10197 f->output_data.w32->fringe_cols = cols;
10198 f->output_data.w32->fringes_extra = real_wid;
10200 else
10202 f->output_data.w32->left_fringe_width = 0;
10203 f->output_data.w32->right_fringe_width = 0;
10204 f->output_data.w32->fringe_cols = 0;
10205 f->output_data.w32->fringes_extra = 0;
10208 if (redraw && FRAME_VISIBLE_P (f))
10209 if (o_left != f->output_data.w32->left_fringe_width ||
10210 o_right != f->output_data.w32->right_fringe_width ||
10211 o_cols != f->output_data.w32->fringe_cols)
10212 redraw_frame (f);
10215 /***********************************************************************
10216 TODO: W32 Input Methods
10217 ***********************************************************************/
10218 /* Listing missing functions from xterm.c helps diff stay in step.
10220 xim_destroy_callback (xim, client_data, call_data)
10221 xim_open_dpy (dpyinfo, resource_name)
10222 struct xim_inst_t
10223 xim_instantiate_callback (display, client_data, call_data)
10224 xim_initialize (dpyinfo, resource_name)
10225 xim_close_dpy (dpyinfo)
10230 /* Calculate the absolute position in frame F
10231 from its current recorded position values and gravity. */
10233 void
10234 x_calc_absolute_position (f)
10235 struct frame *f;
10237 POINT pt;
10238 int flags = f->output_data.w32->size_hint_flags;
10240 pt.x = pt.y = 0;
10242 /* Find the position of the outside upper-left corner of
10243 the inner window, with respect to the outer window.
10244 But do this only if we will need the results. */
10245 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
10247 BLOCK_INPUT;
10248 MapWindowPoints (FRAME_W32_WINDOW (f),
10249 f->output_data.w32->parent_desc,
10250 &pt, 1);
10251 UNBLOCK_INPUT;
10255 RECT rt;
10256 rt.left = rt.right = rt.top = rt.bottom = 0;
10258 BLOCK_INPUT;
10259 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
10260 FRAME_EXTERNAL_MENU_BAR (f));
10261 UNBLOCK_INPUT;
10263 pt.x += (rt.right - rt.left);
10264 pt.y += (rt.bottom - rt.top);
10267 /* Treat negative positions as relative to the leftmost bottommost
10268 position that fits on the screen. */
10269 if (flags & XNegative)
10270 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
10271 - 2 * f->output_data.w32->border_width - pt.x
10272 - PIXEL_WIDTH (f)
10273 + f->output_data.w32->left_pos);
10275 if (flags & YNegative)
10276 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
10277 - 2 * f->output_data.w32->border_width - pt.y
10278 - PIXEL_HEIGHT (f)
10279 + f->output_data.w32->top_pos);
10280 /* The left_pos and top_pos
10281 are now relative to the top and left screen edges,
10282 so the flags should correspond. */
10283 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
10286 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10287 to really change the position, and 0 when calling from
10288 x_make_frame_visible (in that case, XOFF and YOFF are the current
10289 position values). It is -1 when calling from x_set_frame_parameters,
10290 which means, do adjust for borders but don't change the gravity. */
10292 void
10293 x_set_offset (f, xoff, yoff, change_gravity)
10294 struct frame *f;
10295 register int xoff, yoff;
10296 int change_gravity;
10298 int modified_top, modified_left;
10300 if (change_gravity > 0)
10302 f->output_data.w32->top_pos = yoff;
10303 f->output_data.w32->left_pos = xoff;
10304 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
10305 if (xoff < 0)
10306 f->output_data.w32->size_hint_flags |= XNegative;
10307 if (yoff < 0)
10308 f->output_data.w32->size_hint_flags |= YNegative;
10309 f->output_data.w32->win_gravity = NorthWestGravity;
10311 x_calc_absolute_position (f);
10313 BLOCK_INPUT;
10314 x_wm_set_size_hint (f, (long) 0, 0);
10316 modified_left = f->output_data.w32->left_pos;
10317 modified_top = f->output_data.w32->top_pos;
10319 my_set_window_pos (FRAME_W32_WINDOW (f),
10320 NULL,
10321 modified_left, modified_top,
10322 0, 0,
10323 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
10324 UNBLOCK_INPUT;
10328 /* Check if we need to resize the frame due to a fullscreen request.
10329 If so needed, resize the frame. */
10330 static void
10331 x_check_fullscreen (f)
10332 struct frame *f;
10334 if (f->output_data.w32->want_fullscreen & FULLSCREEN_BOTH)
10336 int width, height, ign;
10338 x_real_positions (f, &f->output_data.w32->left_pos,
10339 &f->output_data.w32->top_pos);
10341 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
10343 /* We do not need to move the window, it shall be taken care of
10344 when setting WM manager hints.
10345 If the frame is visible already, the position is checked by
10346 x_check_fullscreen_move. */
10347 if (f->width != width || f->height != height)
10349 change_frame_size (f, height, width, 0, 1, 0);
10350 SET_FRAME_GARBAGED (f);
10351 cancel_mouse_face (f);
10353 /* Wait for the change of frame size to occur */
10354 f->output_data.w32->want_fullscreen |= FULLSCREEN_WAIT;
10359 /* If frame parameters are set after the frame is mapped, we need to move
10360 the window. This is done in xfns.c.
10361 Some window managers moves the window to the right position, some
10362 moves the outer window manager window to the specified position.
10363 Here we check that we are in the right spot. If not, make a second
10364 move, assuming we are dealing with the second kind of window manager. */
10365 static void
10366 x_check_fullscreen_move (f)
10367 struct frame *f;
10369 if (f->output_data.w32->want_fullscreen & FULLSCREEN_MOVE_WAIT)
10371 int expect_top = f->output_data.w32->top_pos;
10372 int expect_left = f->output_data.w32->left_pos;
10374 if (f->output_data.w32->want_fullscreen & FULLSCREEN_HEIGHT)
10375 expect_top = 0;
10376 if (f->output_data.w32->want_fullscreen & FULLSCREEN_WIDTH)
10377 expect_left = 0;
10379 if (expect_top != f->output_data.w32->top_pos
10380 || expect_left != f->output_data.w32->left_pos)
10381 x_set_offset (f, expect_left, expect_top, 1);
10383 /* Just do this once */
10384 f->output_data.w32->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
10389 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
10390 wanted positions of the WM window (not emacs window).
10391 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
10392 window (FRAME_X_WINDOW).
10394 void
10395 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
10396 struct frame *f;
10397 int *width;
10398 int *height;
10399 int *top_pos;
10400 int *left_pos;
10402 int newwidth = f->width, newheight = f->height;
10404 *top_pos = f->output_data.w32->top_pos;
10405 *left_pos = f->output_data.w32->left_pos;
10407 if (f->output_data.w32->want_fullscreen & FULLSCREEN_HEIGHT)
10409 int ph;
10411 ph = FRAME_X_DISPLAY_INFO (f)->height;
10412 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
10413 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
10414 - f->output_data.w32->y_pixels_diff;
10415 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
10416 *top_pos = 0;
10419 if (f->output_data.w32->want_fullscreen & FULLSCREEN_WIDTH)
10421 int pw;
10423 pw = FRAME_X_DISPLAY_INFO (f)->width;
10424 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
10425 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
10426 - f->output_data.w32->x_pixels_diff;
10427 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
10428 *left_pos = 0;
10431 *width = newwidth;
10432 *height = newheight;
10436 /* Call this to change the size of frame F's x-window.
10437 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
10438 for this size change and subsequent size changes.
10439 Otherwise we leave the window gravity unchanged. */
10441 void
10442 x_set_window_size (f, change_gravity, cols, rows)
10443 struct frame *f;
10444 int change_gravity;
10445 int cols, rows;
10447 int pixelwidth, pixelheight;
10449 BLOCK_INPUT;
10451 check_frame_size (f, &rows, &cols);
10452 f->output_data.w32->vertical_scroll_bar_extra
10453 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10455 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
10457 x_compute_fringe_widths (f, 0);
10459 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10460 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
10462 f->output_data.w32->win_gravity = NorthWestGravity;
10463 x_wm_set_size_hint (f, (long) 0, 0);
10466 RECT rect;
10468 rect.left = rect.top = 0;
10469 rect.right = pixelwidth;
10470 rect.bottom = pixelheight;
10472 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
10473 FRAME_EXTERNAL_MENU_BAR (f));
10475 my_set_window_pos (FRAME_W32_WINDOW (f),
10476 NULL,
10477 0, 0,
10478 rect.right - rect.left,
10479 rect.bottom - rect.top,
10480 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
10483 /* Now, strictly speaking, we can't be sure that this is accurate,
10484 but the window manager will get around to dealing with the size
10485 change request eventually, and we'll hear how it went when the
10486 ConfigureNotify event gets here.
10488 We could just not bother storing any of this information here,
10489 and let the ConfigureNotify event set everything up, but that
10490 might be kind of confusing to the Lisp code, since size changes
10491 wouldn't be reported in the frame parameters until some random
10492 point in the future when the ConfigureNotify event arrives.
10494 We pass 1 for DELAY since we can't run Lisp code inside of
10495 a BLOCK_INPUT. */
10496 change_frame_size (f, rows, cols, 0, 1, 0);
10497 PIXEL_WIDTH (f) = pixelwidth;
10498 PIXEL_HEIGHT (f) = pixelheight;
10500 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10501 receive in the ConfigureNotify event; if we get what we asked
10502 for, then the event won't cause the screen to become garbaged, so
10503 we have to make sure to do it here. */
10504 SET_FRAME_GARBAGED (f);
10506 /* If cursor was outside the new size, mark it as off. */
10507 mark_window_cursors_off (XWINDOW (f->root_window));
10509 /* Clear out any recollection of where the mouse highlighting was,
10510 since it might be in a place that's outside the new frame size.
10511 Actually checking whether it is outside is a pain in the neck,
10512 so don't try--just let the highlighting be done afresh with new size. */
10513 cancel_mouse_face (f);
10515 UNBLOCK_INPUT;
10518 /* Mouse warping. */
10520 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
10522 void
10523 x_set_mouse_position (f, x, y)
10524 struct frame *f;
10525 int x, y;
10527 int pix_x, pix_y;
10529 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
10530 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
10532 if (pix_x < 0) pix_x = 0;
10533 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
10535 if (pix_y < 0) pix_y = 0;
10536 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
10538 x_set_mouse_pixel_position (f, pix_x, pix_y);
10541 void
10542 x_set_mouse_pixel_position (f, pix_x, pix_y)
10543 struct frame *f;
10544 int pix_x, pix_y;
10546 RECT rect;
10547 POINT pt;
10549 BLOCK_INPUT;
10551 GetClientRect (FRAME_W32_WINDOW (f), &rect);
10552 pt.x = rect.left + pix_x;
10553 pt.y = rect.top + pix_y;
10554 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
10556 SetCursorPos (pt.x, pt.y);
10558 UNBLOCK_INPUT;
10562 /* focus shifting, raising and lowering. */
10564 void
10565 x_focus_on_frame (f)
10566 struct frame *f;
10568 struct w32_display_info *dpyinfo = &one_w32_display_info;
10570 /* Give input focus to frame. */
10571 BLOCK_INPUT;
10572 #if 0
10573 /* Try not to change its Z-order if possible. */
10574 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
10575 my_set_focus (f, FRAME_W32_WINDOW (f));
10576 else
10577 #endif
10578 my_set_foreground_window (FRAME_W32_WINDOW (f));
10579 UNBLOCK_INPUT;
10582 void
10583 x_unfocus_frame (f)
10584 struct frame *f;
10588 /* Raise frame F. */
10589 void
10590 x_raise_frame (f)
10591 struct frame *f;
10593 BLOCK_INPUT;
10595 /* Strictly speaking, raise-frame should only change the frame's Z
10596 order, leaving input focus unchanged. This is reasonable behaviour
10597 on X where the usual policy is point-to-focus. However, this
10598 behaviour would be very odd on Windows where the usual policy is
10599 click-to-focus.
10601 On X, if the mouse happens to be over the raised frame, it gets
10602 input focus anyway (so the window with focus will never be
10603 completely obscured) - if not, then just moving the mouse over it
10604 is sufficient to give it focus. On Windows, the user must actually
10605 click on the frame (preferrably the title bar so as not to move
10606 point), which is more awkward. Also, no other Windows program
10607 raises a window to the top but leaves another window (possibly now
10608 completely obscured) with input focus.
10610 Because there is a system setting on Windows that allows the user
10611 to choose the point to focus policy, we make the strict semantics
10612 optional, but by default we grab focus when raising. */
10614 if (NILP (Vw32_grab_focus_on_raise))
10616 /* The obvious call to my_set_window_pos doesn't work if Emacs is
10617 not already the foreground application: the frame is raised
10618 above all other frames belonging to us, but not above the
10619 current top window. To achieve that, we have to resort to this
10620 more cumbersome method. */
10622 HDWP handle = BeginDeferWindowPos (2);
10623 if (handle)
10625 DeferWindowPos (handle,
10626 FRAME_W32_WINDOW (f),
10627 HWND_TOP,
10628 0, 0, 0, 0,
10629 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
10631 DeferWindowPos (handle,
10632 GetForegroundWindow (),
10633 FRAME_W32_WINDOW (f),
10634 0, 0, 0, 0,
10635 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
10637 EndDeferWindowPos (handle);
10640 else
10642 my_set_foreground_window (FRAME_W32_WINDOW (f));
10645 UNBLOCK_INPUT;
10648 /* Lower frame F. */
10649 void
10650 x_lower_frame (f)
10651 struct frame *f;
10653 BLOCK_INPUT;
10654 my_set_window_pos (FRAME_W32_WINDOW (f),
10655 HWND_BOTTOM,
10656 0, 0, 0, 0,
10657 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
10658 UNBLOCK_INPUT;
10661 static void
10662 w32_frame_raise_lower (f, raise_flag)
10663 FRAME_PTR f;
10664 int raise_flag;
10666 if (! FRAME_W32_P (f))
10667 return;
10669 if (raise_flag)
10670 x_raise_frame (f);
10671 else
10672 x_lower_frame (f);
10675 /* Change of visibility. */
10677 /* This tries to wait until the frame is really visible.
10678 However, if the window manager asks the user where to position
10679 the frame, this will return before the user finishes doing that.
10680 The frame will not actually be visible at that time,
10681 but it will become visible later when the window manager
10682 finishes with it. */
10684 void
10685 x_make_frame_visible (f)
10686 struct frame *f;
10688 Lisp_Object type;
10690 BLOCK_INPUT;
10692 type = x_icon_type (f);
10693 if (!NILP (type))
10694 x_bitmap_icon (f, type);
10696 if (! FRAME_VISIBLE_P (f))
10698 /* We test FRAME_GARBAGED_P here to make sure we don't
10699 call x_set_offset a second time
10700 if we get to x_make_frame_visible a second time
10701 before the window gets really visible. */
10702 if (! FRAME_ICONIFIED_P (f)
10703 && ! f->output_data.w32->asked_for_visible)
10704 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
10706 f->output_data.w32->asked_for_visible = 1;
10708 /* my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW); */
10709 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
10712 /* Synchronize to ensure Emacs knows the frame is visible
10713 before we do anything else. We do this loop with input not blocked
10714 so that incoming events are handled. */
10716 Lisp_Object frame;
10717 int count;
10719 /* This must come after we set COUNT. */
10720 UNBLOCK_INPUT;
10722 XSETFRAME (frame, f);
10724 /* Wait until the frame is visible. Process X events until a
10725 MapNotify event has been seen, or until we think we won't get a
10726 MapNotify at all.. */
10727 for (count = input_signal_count + 10;
10728 input_signal_count < count && !FRAME_VISIBLE_P (f);)
10730 /* Force processing of queued events. */
10731 /* TODO: x_sync equivalent? */
10733 /* Machines that do polling rather than SIGIO have been observed
10734 to go into a busy-wait here. So we'll fake an alarm signal
10735 to let the handler know that there's something to be read.
10736 We used to raise a real alarm, but it seems that the handler
10737 isn't always enabled here. This is probably a bug. */
10738 if (input_polling_used ())
10740 /* It could be confusing if a real alarm arrives while processing
10741 the fake one. Turn it off and let the handler reset it. */
10742 int old_poll_suppress_count = poll_suppress_count;
10743 poll_suppress_count = 1;
10744 poll_for_input_1 ();
10745 poll_suppress_count = old_poll_suppress_count;
10748 FRAME_SAMPLE_VISIBILITY (f);
10752 /* Change from mapped state to withdrawn state. */
10754 /* Make the frame visible (mapped and not iconified). */
10756 x_make_frame_invisible (f)
10757 struct frame *f;
10759 /* Don't keep the highlight on an invisible frame. */
10760 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
10761 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
10763 BLOCK_INPUT;
10765 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
10767 /* We can't distinguish this from iconification
10768 just by the event that we get from the server.
10769 So we can't win using the usual strategy of letting
10770 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10771 and synchronize with the server to make sure we agree. */
10772 f->visible = 0;
10773 FRAME_ICONIFIED_P (f) = 0;
10774 f->async_visible = 0;
10775 f->async_iconified = 0;
10777 UNBLOCK_INPUT;
10780 /* Change window state from mapped to iconified. */
10782 void
10783 x_iconify_frame (f)
10784 struct frame *f;
10786 Lisp_Object type;
10788 /* Don't keep the highlight on an invisible frame. */
10789 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
10790 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
10792 if (f->async_iconified)
10793 return;
10795 BLOCK_INPUT;
10797 type = x_icon_type (f);
10798 if (!NILP (type))
10799 x_bitmap_icon (f, type);
10801 /* Simulate the user minimizing the frame. */
10802 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
10804 UNBLOCK_INPUT;
10808 /* Free X resources of frame F. */
10810 void
10811 x_free_frame_resources (f)
10812 struct frame *f;
10814 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10816 BLOCK_INPUT;
10818 if (FRAME_W32_WINDOW (f))
10819 my_destroy_window (f, FRAME_W32_WINDOW (f));
10821 free_frame_menubar (f);
10823 unload_color (f, f->output_data.x->foreground_pixel);
10824 unload_color (f, f->output_data.x->background_pixel);
10825 unload_color (f, f->output_data.w32->cursor_pixel);
10826 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
10827 unload_color (f, f->output_data.w32->border_pixel);
10828 unload_color (f, f->output_data.w32->mouse_pixel);
10829 if (f->output_data.w32->white_relief.allocated_p)
10830 unload_color (f, f->output_data.w32->white_relief.pixel);
10831 if (f->output_data.w32->black_relief.allocated_p)
10832 unload_color (f, f->output_data.w32->black_relief.pixel);
10834 if (FRAME_FACE_CACHE (f))
10835 free_frame_faces (f);
10837 xfree (f->output_data.w32);
10838 f->output_data.w32 = NULL;
10840 if (f == dpyinfo->w32_focus_frame)
10841 dpyinfo->w32_focus_frame = 0;
10842 if (f == dpyinfo->w32_focus_event_frame)
10843 dpyinfo->w32_focus_event_frame = 0;
10844 if (f == dpyinfo->x_highlight_frame)
10845 dpyinfo->x_highlight_frame = 0;
10847 if (f == dpyinfo->mouse_face_mouse_frame)
10849 dpyinfo->mouse_face_beg_row
10850 = dpyinfo->mouse_face_beg_col = -1;
10851 dpyinfo->mouse_face_end_row
10852 = dpyinfo->mouse_face_end_col = -1;
10853 dpyinfo->mouse_face_window = Qnil;
10854 dpyinfo->mouse_face_deferred_gc = 0;
10855 dpyinfo->mouse_face_mouse_frame = 0;
10858 UNBLOCK_INPUT;
10862 /* Destroy the window of frame F. */
10864 x_destroy_window (f)
10865 struct frame *f;
10867 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10869 x_free_frame_resources (f);
10871 dpyinfo->reference_count--;
10875 /* Setting window manager hints. */
10877 /* Set the normal size hints for the window manager, for frame F.
10878 FLAGS is the flags word to use--or 0 meaning preserve the flags
10879 that the window now has.
10880 If USER_POSITION is nonzero, we set the USPosition
10881 flag (this is useful when FLAGS is 0). */
10882 void
10883 x_wm_set_size_hint (f, flags, user_position)
10884 struct frame *f;
10885 long flags;
10886 int user_position;
10888 Window window = FRAME_W32_WINDOW (f);
10890 enter_crit ();
10892 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
10893 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
10894 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
10895 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
10897 leave_crit ();
10900 /* Window manager things */
10901 x_wm_set_icon_position (f, icon_x, icon_y)
10902 struct frame *f;
10903 int icon_x, icon_y;
10905 #if 0
10906 Window window = FRAME_W32_WINDOW (f);
10908 f->display.x->wm_hints.flags |= IconPositionHint;
10909 f->display.x->wm_hints.icon_x = icon_x;
10910 f->display.x->wm_hints.icon_y = icon_y;
10912 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
10913 #endif
10917 /***********************************************************************
10918 Fonts
10919 ***********************************************************************/
10921 /* The following functions are listed here to help diff stay in step
10922 with xterm.c. See w32fns.c for definitions.
10924 x_get_font_info (f, font_idx)
10925 x_list_fonts (f, pattern, size, maxnames)
10929 #if GLYPH_DEBUG
10931 /* Check that FONT is valid on frame F. It is if it can be found in F's
10932 font table. */
10934 static void
10935 x_check_font (f, font)
10936 struct frame *f;
10937 XFontStruct *font;
10939 int i;
10940 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10942 xassert (font != NULL);
10944 for (i = 0; i < dpyinfo->n_fonts; i++)
10945 if (dpyinfo->font_table[i].name
10946 && font == dpyinfo->font_table[i].font)
10947 break;
10949 xassert (i < dpyinfo->n_fonts);
10952 #endif /* GLYPH_DEBUG != 0 */
10954 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10955 Note: There are (broken) X fonts out there with invalid XFontStruct
10956 min_bounds contents. For example, handa@etl.go.jp reports that
10957 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10958 have font->min_bounds.width == 0. */
10960 static INLINE void
10961 x_font_min_bounds (font, w, h)
10962 XFontStruct *font;
10963 int *w, *h;
10966 * TODO: Windows does not appear to offer min bound, only
10967 * average and maximum width, and maximum height.
10969 *h = FONT_HEIGHT (font);
10970 *w = FONT_WIDTH (font);
10974 /* Compute the smallest character width and smallest font height over
10975 all fonts available on frame F. Set the members smallest_char_width
10976 and smallest_font_height in F's x_display_info structure to
10977 the values computed. Value is non-zero if smallest_font_height or
10978 smallest_char_width become smaller than they were before. */
10981 x_compute_min_glyph_bounds (f)
10982 struct frame *f;
10984 int i;
10985 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10986 XFontStruct *font;
10987 int old_width = dpyinfo->smallest_char_width;
10988 int old_height = dpyinfo->smallest_font_height;
10990 dpyinfo->smallest_font_height = 100000;
10991 dpyinfo->smallest_char_width = 100000;
10993 for (i = 0; i < dpyinfo->n_fonts; ++i)
10994 if (dpyinfo->font_table[i].name)
10996 struct font_info *fontp = dpyinfo->font_table + i;
10997 int w, h;
10999 font = (XFontStruct *) fontp->font;
11000 xassert (font != (XFontStruct *) ~0);
11001 x_font_min_bounds (font, &w, &h);
11003 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
11004 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
11007 xassert (dpyinfo->smallest_char_width > 0
11008 && dpyinfo->smallest_font_height > 0);
11010 return (dpyinfo->n_fonts == 1
11011 || dpyinfo->smallest_char_width < old_width
11012 || dpyinfo->smallest_font_height < old_height);
11015 /* The following functions are listed here to help diff stay in step
11016 with xterm.c. See w32fns.c for definitions.
11018 x_load_font (f, fontname, size)
11019 x_query_font (f, fontname)
11020 x_find_ccl_program (fontp)
11024 /***********************************************************************
11025 Initialization
11026 ***********************************************************************/
11028 static int w32_initialized = 0;
11030 void
11031 w32_initialize_display_info (display_name)
11032 Lisp_Object display_name;
11034 struct w32_display_info *dpyinfo = &one_w32_display_info;
11036 bzero (dpyinfo, sizeof (*dpyinfo));
11038 /* Put it on w32_display_name_list. */
11039 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
11040 w32_display_name_list);
11041 dpyinfo->name_list_element = XCAR (w32_display_name_list);
11043 dpyinfo->w32_id_name
11044 = (char *) xmalloc (SCHARS (Vinvocation_name)
11045 + SCHARS (Vsystem_name)
11046 + 2);
11047 sprintf (dpyinfo->w32_id_name, "%s@%s",
11048 SDATA (Vinvocation_name), SDATA (Vsystem_name));
11050 /* Default Console mode values - overridden when running in GUI mode
11051 with values obtained from system metrics. */
11052 dpyinfo->resx = 1;
11053 dpyinfo->resy = 1;
11054 dpyinfo->height_in = 1;
11055 dpyinfo->width_in = 1;
11056 dpyinfo->n_planes = 1;
11057 dpyinfo->n_cbits = 4;
11058 dpyinfo->n_fonts = 0;
11059 dpyinfo->smallest_font_height = 1;
11060 dpyinfo->smallest_char_width = 1;
11062 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
11063 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
11064 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
11065 dpyinfo->mouse_face_window = Qnil;
11066 dpyinfo->mouse_face_overlay = Qnil;
11067 dpyinfo->mouse_face_hidden = 0;
11068 /* TODO: dpyinfo->gray */
11072 struct w32_display_info *
11073 w32_term_init (display_name, xrm_option, resource_name)
11074 Lisp_Object display_name;
11075 char *xrm_option;
11076 char *resource_name;
11078 struct w32_display_info *dpyinfo;
11079 HDC hdc;
11081 BLOCK_INPUT;
11083 if (!w32_initialized)
11085 w32_initialize ();
11086 w32_initialized = 1;
11090 int argc = 0;
11091 char *argv[3];
11093 argv[0] = "";
11094 argc = 1;
11095 if (xrm_option)
11097 argv[argc++] = "-xrm";
11098 argv[argc++] = xrm_option;
11102 w32_initialize_display_info (display_name);
11104 dpyinfo = &one_w32_display_info;
11106 /* Put this display on the chain. */
11107 dpyinfo->next = x_display_list;
11108 x_display_list = dpyinfo;
11110 hdc = GetDC (GetDesktopWindow ());
11112 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
11113 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
11114 dpyinfo->root_window = GetDesktopWindow ();
11115 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
11116 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
11117 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
11118 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
11119 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
11120 dpyinfo->image_cache = make_image_cache ();
11121 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
11122 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
11123 ReleaseDC (GetDesktopWindow (), hdc);
11125 /* initialise palette with white and black */
11127 XColor color;
11128 w32_defined_color (0, "white", &color, 1);
11129 w32_defined_color (0, "black", &color, 1);
11132 /* Create Row Bitmaps and store them for later use. */
11133 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
11134 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
11135 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
11136 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
11137 1, continued_bits);
11138 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
11139 1, 1, continuation_bits);
11140 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
11142 #ifndef F_SETOWN_BUG
11143 #ifdef F_SETOWN
11144 #ifdef F_SETOWN_SOCK_NEG
11145 /* stdin is a socket here */
11146 fcntl (connection, F_SETOWN, -getpid ());
11147 #else /* ! defined (F_SETOWN_SOCK_NEG) */
11148 fcntl (connection, F_SETOWN, getpid ());
11149 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
11150 #endif /* ! defined (F_SETOWN) */
11151 #endif /* F_SETOWN_BUG */
11153 #ifdef SIGIO
11154 if (interrupt_input)
11155 init_sigio (connection);
11156 #endif /* ! defined (SIGIO) */
11158 UNBLOCK_INPUT;
11160 return dpyinfo;
11163 /* Get rid of display DPYINFO, assuming all frames are already gone. */
11165 void
11166 x_delete_display (dpyinfo)
11167 struct w32_display_info *dpyinfo;
11169 /* Discard this display from w32_display_name_list and w32_display_list.
11170 We can't use Fdelq because that can quit. */
11171 if (! NILP (w32_display_name_list)
11172 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
11173 w32_display_name_list = XCDR (w32_display_name_list);
11174 else
11176 Lisp_Object tail;
11178 tail = w32_display_name_list;
11179 while (CONSP (tail) && CONSP (XCDR (tail)))
11181 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11183 XSETCDR (tail, XCDR (XCDR (tail)));
11184 break;
11186 tail = XCDR (tail);
11190 /* free palette table */
11192 struct w32_palette_entry * plist;
11194 plist = dpyinfo->color_list;
11195 while (plist)
11197 struct w32_palette_entry * pentry = plist;
11198 plist = plist->next;
11199 xfree (pentry);
11201 dpyinfo->color_list = NULL;
11202 if (dpyinfo->palette)
11203 DeleteObject(dpyinfo->palette);
11205 xfree (dpyinfo->font_table);
11206 xfree (dpyinfo->w32_id_name);
11208 /* Destroy row bitmaps. */
11209 DeleteObject (left_bmp);
11210 DeleteObject (ov_bmp);
11211 DeleteObject (right_bmp);
11212 DeleteObject (continued_bmp);
11213 DeleteObject (continuation_bmp);
11214 DeleteObject (zv_bmp);
11217 /* Set up use of W32. */
11219 DWORD w32_msg_worker ();
11221 void
11222 x_flush (struct frame * f)
11223 { /* Nothing to do */ }
11225 static struct redisplay_interface w32_redisplay_interface =
11227 x_produce_glyphs,
11228 x_write_glyphs,
11229 x_insert_glyphs,
11230 x_clear_end_of_line,
11231 x_scroll_run,
11232 x_after_update_window_line,
11233 x_update_window_begin,
11234 x_update_window_end,
11235 w32_cursor_to,
11236 x_flush,
11237 x_clear_mouse_face,
11238 x_get_glyph_overhangs,
11239 x_fix_overlapping_area
11242 void
11243 w32_initialize ()
11245 rif = &w32_redisplay_interface;
11247 /* MSVC does not type K&R functions with no arguments correctly, and
11248 so we must explicitly cast them. */
11249 clear_frame_hook = (void (*)(void)) x_clear_frame;
11250 ring_bell_hook = (void (*)(void)) w32_ring_bell;
11251 update_begin_hook = x_update_begin;
11252 update_end_hook = x_update_end;
11254 read_socket_hook = w32_read_socket;
11256 frame_up_to_date_hook = w32_frame_up_to_date;
11258 mouse_position_hook = w32_mouse_position;
11259 frame_rehighlight_hook = w32_frame_rehighlight;
11260 frame_raise_lower_hook = w32_frame_raise_lower;
11261 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
11262 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
11263 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
11264 judge_scroll_bars_hook = w32_judge_scroll_bars;
11265 estimate_mode_line_height_hook = x_estimate_mode_line_height;
11267 scroll_region_ok = 1; /* we'll scroll partial frames */
11268 char_ins_del_ok = 1;
11269 line_ins_del_ok = 1; /* we'll just blt 'em */
11270 fast_clear_end_of_line = 1; /* X does this well */
11271 memory_below_frame = 0; /* we don't remember what scrolls
11272 off the bottom */
11273 baud_rate = 19200;
11275 w32_system_caret_hwnd = NULL;
11276 w32_system_caret_height = 0;
11277 w32_system_caret_x = 0;
11278 w32_system_caret_y = 0;
11280 last_tool_bar_item = -1;
11281 any_help_event_p = 0;
11283 /* Initialize input mode: interrupt_input off, no flow control, allow
11284 8 bit character input, standard quit char. */
11285 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
11287 /* Create the window thread - it will terminate itself or when the app terminates */
11289 init_crit ();
11291 dwMainThreadId = GetCurrentThreadId ();
11292 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
11293 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
11295 /* Wait for thread to start */
11298 MSG msg;
11300 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
11302 hWindowsThread = CreateThread (NULL, 0,
11303 (LPTHREAD_START_ROUTINE) w32_msg_worker,
11304 0, 0, &dwWindowsThreadId);
11306 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
11309 /* It is desirable that mainThread should have the same notion of
11310 focus window and active window as windowsThread. Unfortunately, the
11311 following call to AttachThreadInput, which should do precisely what
11312 we need, causes major problems when Emacs is linked as a console
11313 program. Unfortunately, we have good reasons for doing that, so
11314 instead we need to send messages to windowsThread to make some API
11315 calls for us (ones that affect, or depend on, the active/focus
11316 window state. */
11317 #ifdef ATTACH_THREADS
11318 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
11319 #endif
11321 /* Dynamically link to optional system components. */
11323 HANDLE user_lib = LoadLibrary ("user32.dll");
11325 #define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
11327 /* New proportional scroll bar functions. */
11328 LOAD_PROC (SetScrollInfo);
11329 LOAD_PROC (GetScrollInfo);
11331 #undef LOAD_PROC
11333 FreeLibrary (user_lib);
11335 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
11336 otherwise use the fixed height. */
11337 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
11338 GetSystemMetrics (SM_CYVTHUMB);
11340 /* For either kind of scroll bar, take account of the arrows; these
11341 effectively form the border of the main scroll bar range. */
11342 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
11343 = GetSystemMetrics (SM_CYVSCROLL);
11347 void
11348 syms_of_w32term ()
11350 staticpro (&w32_display_name_list);
11351 w32_display_name_list = Qnil;
11353 staticpro (&last_mouse_scroll_bar);
11354 last_mouse_scroll_bar = Qnil;
11356 staticpro (&Qvendor_specific_keysyms);
11357 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11359 DEFVAR_INT ("w32-num-mouse-buttons",
11360 &Vw32_num_mouse_buttons,
11361 doc: /* Number of physical mouse buttons. */);
11362 Vw32_num_mouse_buttons = Qnil;
11364 DEFVAR_LISP ("w32-swap-mouse-buttons",
11365 &Vw32_swap_mouse_buttons,
11366 doc: /* Swap the mapping of middle and right mouse buttons.
11367 When nil, middle button is mouse-2 and right button is mouse-3. */);
11368 Vw32_swap_mouse_buttons = Qnil;
11370 DEFVAR_LISP ("w32-grab-focus-on-raise",
11371 &Vw32_grab_focus_on_raise,
11372 doc: /* Raised frame grabs input focus.
11373 When t, `raise-frame' grabs input focus as well. This fits well
11374 with the normal Windows click-to-focus policy, but might not be
11375 desirable when using a point-to-focus policy. */);
11376 Vw32_grab_focus_on_raise = Qt;
11378 DEFVAR_LISP ("w32-capslock-is-shiftlock",
11379 &Vw32_capslock_is_shiftlock,
11380 doc: /* Apply CapsLock state to non character input keys.
11381 When nil, CapsLock only affects normal character input keys. */);
11382 Vw32_capslock_is_shiftlock = Qnil;
11384 DEFVAR_LISP ("w32-recognize-altgr",
11385 &Vw32_recognize_altgr,
11386 doc: /* Recognize right-alt and left-ctrl as AltGr.
11387 When nil, the right-alt and left-ctrl key combination is
11388 interpreted normally. */);
11389 Vw32_recognize_altgr = Qt;
11391 DEFVAR_BOOL ("w32-enable-unicode-output",
11392 &w32_enable_unicode_output,
11393 doc: /* Enable the use of Unicode for text output if non-nil.
11394 Unicode output may prevent some third party applications for displaying
11395 Far-East Languages on Windows 95/98 from working properly.
11396 NT uses Unicode internally anyway, so this flag will probably have no
11397 affect on NT machines. */);
11398 w32_enable_unicode_output = 1;
11400 help_echo = Qnil;
11401 staticpro (&help_echo);
11402 help_echo_object = Qnil;
11403 staticpro (&help_echo_object);
11404 help_echo_window = Qnil;
11405 staticpro (&help_echo_window);
11406 previous_help_echo = Qnil;
11407 staticpro (&previous_help_echo);
11408 help_echo_pos = -1;
11410 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
11411 doc: /* *Non-nil means autoselect window with mouse pointer. */);
11412 mouse_autoselect_window = 0;
11414 DEFVAR_BOOL ("w32-use-visible-system-caret",
11415 &w32_use_visible_system_caret,
11416 doc: /* Flag to make the system caret visible.
11417 When this is non-nil, Emacs will indicate the position of point by
11418 using the system caret instead of drawing its own cursor. Some screen
11419 reader software does not track the system cursor properly when it is
11420 invisible, and gets confused by Emacs drawing its own cursor, so this
11421 variable is initialized to t when Emacs detects that screen reader
11422 software is running as it starts up.
11424 When this variable is set, other variables affecting the appearance of
11425 the cursor have no effect. */);
11427 /* Initialize w32_use_visible_system_caret based on whether a screen
11428 reader is in use. */
11429 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
11430 &w32_use_visible_system_caret, 0))
11431 w32_use_visible_system_caret = 0;
11433 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
11434 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
11435 For example, if a block cursor is over a tab, it will be drawn as
11436 wide as that tab on the display. */);
11437 x_stretch_cursor_p = 0;
11439 DEFVAR_BOOL ("x-use-underline-position-properties",
11440 &x_use_underline_position_properties,
11441 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11442 nil means ignore them. If you encounter fonts with bogus
11443 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11444 to 4.1, set this to nil. */);
11445 x_use_underline_position_properties = 1;
11447 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11448 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
11449 Vx_toolkit_scroll_bars = Qt;
11451 staticpro (&last_mouse_motion_frame);
11452 last_mouse_motion_frame = Qnil;