*** empty log message ***
[emacs.git] / src / w32term.c
blob372c702ecf312335c3005fca2281c5a2a078c18b
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"
39 #include <ctype.h>
40 #include <errno.h>
41 #include <setjmp.h>
42 #include <sys/stat.h>
44 #include "keyboard.h"
45 #include "frame.h"
46 #include "dispextern.h"
47 #include "fontset.h"
48 #include "termhooks.h"
49 #include "termopts.h"
50 #include "termchar.h"
51 #include "gnu.h"
52 #include "disptab.h"
53 #include "buffer.h"
54 #include "window.h"
55 #include "intervals.h"
56 #include "composite.h"
57 #include "coding.h"
59 #ifndef min
60 #define min(a,b) ((a) < (b) ? (a) : (b))
61 #endif
62 #ifndef max
63 #define max(a,b) ((a) > (b) ? (a) : (b))
64 #endif
66 #define abs(x) ((x) < 0 ? -(x) : (x))
68 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
71 /* Bitmaps for truncated lines. */
73 enum bitmap_type
75 NO_BITMAP,
76 LEFT_TRUNCATION_BITMAP,
77 RIGHT_TRUNCATION_BITMAP,
78 OVERLAY_ARROW_BITMAP,
79 CONTINUED_LINE_BITMAP,
80 CONTINUATION_LINE_BITMAP,
81 ZV_LINE_BITMAP
84 /* Bitmaps are all unsigned short, as Windows requires bitmap data to
85 be Word aligned. For some reason they are horizontally reflected
86 compared to how they appear on X, so changes in xterm.c should be
87 reflected here. */
89 /* Bitmap drawn to indicate lines not displaying text if
90 `indicate-empty-lines' is non-nil. */
92 #define zv_width 8
93 #define zv_height 8
94 static unsigned short zv_bits[] = {
95 0x00, 0x00, 0x78, 0x78, 0x78, 0x78, 0x00, 0x00};
96 static HBITMAP zv_bmp;
98 /* An arrow like this: `<-'. */
100 #define left_width 8
101 #define left_height 8
102 static unsigned short left_bits[] = {
103 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
104 static HBITMAP left_bmp;
106 /* Right truncation arrow bitmap `->'. */
108 #define right_width 8
109 #define right_height 8
110 static unsigned short right_bits[] = {
111 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
112 static HBITMAP right_bmp;
114 /* Marker for continued lines. */
116 #define continued_width 8
117 #define continued_height 8
118 static unsigned short continued_bits[] = {
119 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
120 static HBITMAP continued_bmp;
122 /* Marker for continuation lines. */
124 #define continuation_width 8
125 #define continuation_height 8
126 static unsigned short continuation_bits[] = {
127 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
128 static HBITMAP continuation_bmp;
130 /* Overlay arrow bitmap. */
132 #if 0
133 /* A bomb. */
134 #define ov_width 8
135 #define ov_height 8
136 static unsigned short ov_bits[] = {
137 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
138 #else
139 /* A triangular arrow. */
140 #define ov_width 8
141 #define ov_height 8
142 static unsigned short ov_bits[] = {
143 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
144 #endif
145 static HBITMAP ov_bmp;
147 extern Lisp_Object Qhelp_echo;
150 /* Non-nil means Emacs uses toolkit scroll bars. */
152 Lisp_Object Vx_toolkit_scroll_bars;
154 /* If a string, w32_read_socket generates an event to display that string.
155 (The display is done in read_char.) */
157 static Lisp_Object help_echo;
158 static Lisp_Object help_echo_window;
159 static Lisp_Object help_echo_object;
160 static int help_echo_pos;
162 /* Temporary variable for w32_read_socket. */
164 static Lisp_Object previous_help_echo;
166 /* Non-zero means that a HELP_EVENT has been generated since Emacs
167 start. */
169 static int any_help_event_p;
171 /* Non-zero means draw block and hollow cursor as wide as the glyph
172 under it. For example, if a block cursor is over a tab, it will be
173 drawn as wide as that tab on the display. */
175 int x_stretch_cursor_p;
177 extern unsigned int msh_mousewheel;
179 extern void free_frame_menubar ();
181 extern void w32_menu_display_help (HMENU menu, UINT menu_item, UINT flags);
183 extern int w32_codepage_for_font (char *fontname);
185 extern glyph_metric *w32_BDF_TextMetric(bdffont *fontp,
186 unsigned char *text, int dim);
187 extern Lisp_Object Vwindow_system;
189 #define x_any_window_to_frame x_window_to_frame
190 #define x_top_window_to_frame x_window_to_frame
193 /* This is display since w32 does not support multiple ones. */
194 struct w32_display_info one_w32_display_info;
195 struct w32_display_info *x_display_list;
197 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
198 one for each element of w32_display_list and in the same order.
199 NAME is the name of the frame.
200 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
201 Lisp_Object w32_display_name_list;
203 /* Frame being updated by update_frame. This is declared in term.c.
204 This is set by update_begin and looked at by all the
205 w32 functions. It is zero while not inside an update.
206 In that case, the w32 functions assume that `SELECTED_FRAME ()'
207 is the frame to apply to. */
208 extern struct frame *updating_frame;
210 /* This is a frame waiting to be autoraised, within w32_read_socket. */
211 struct frame *pending_autoraise_frame;
213 /* Nominal cursor position -- where to draw output.
214 HPOS and VPOS are window relative glyph matrix coordinates.
215 X and Y are window relative pixel coordinates. */
217 struct cursor_pos output_cursor;
219 /* Flag to enable Unicode output in case users wish to use programs
220 like Twinbridge on '95 rather than installed system level support
221 for Far East languages. */
222 int w32_enable_unicode_output;
224 DWORD dwWindowsThreadId = 0;
225 HANDLE hWindowsThread = NULL;
226 DWORD dwMainThreadId = 0;
227 HANDLE hMainThread = NULL;
229 #ifndef SIF_ALL
230 /* These definitions are new with Windows 95. */
231 #define SIF_RANGE 0x0001
232 #define SIF_PAGE 0x0002
233 #define SIF_POS 0x0004
234 #define SIF_DISABLENOSCROLL 0x0008
235 #define SIF_TRACKPOS 0x0010
236 #define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
238 typedef struct tagSCROLLINFO
240 UINT cbSize;
241 UINT fMask;
242 int nMin;
243 int nMax;
244 UINT nPage;
245 int nPos;
246 int nTrackPos;
247 } SCROLLINFO, FAR *LPSCROLLINFO;
248 typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
249 #endif /* SIF_ALL */
251 /* Dynamic linking to new proportional scroll bar functions. */
252 int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
253 BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
255 int vertical_scroll_bar_min_handle;
256 int vertical_scroll_bar_top_border;
257 int vertical_scroll_bar_bottom_border;
259 int last_scroll_bar_drag_pos;
261 /* Mouse movement. */
263 /* Where the mouse was last time we reported a mouse event. */
265 FRAME_PTR last_mouse_frame;
266 static RECT last_mouse_glyph;
267 static Lisp_Object last_mouse_press_frame;
269 Lisp_Object Vw32_num_mouse_buttons;
271 Lisp_Object Vw32_swap_mouse_buttons;
273 /* Control whether x_raise_frame also sets input focus. */
274 Lisp_Object Vw32_grab_focus_on_raise;
276 /* Control whether Caps Lock affects non-ascii characters. */
277 Lisp_Object Vw32_capslock_is_shiftlock;
279 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
280 Lisp_Object Vw32_recognize_altgr;
282 /* The scroll bar in which the last motion event occurred.
284 If the last motion event occurred in a scroll bar, we set this
285 so w32_mouse_position can know whether to report a scroll bar motion or
286 an ordinary motion.
288 If the last motion event didn't occur in a scroll bar, we set this
289 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
290 static Lisp_Object last_mouse_scroll_bar;
291 static int last_mouse_scroll_bar_pos;
293 /* This is a hack. We would really prefer that w32_mouse_position would
294 return the time associated with the position it returns, but there
295 doesn't seem to be any way to wrest the time-stamp from the server
296 along with the position query. So, we just keep track of the time
297 of the last movement we received, and return that in hopes that
298 it's somewhat accurate. */
300 static Time last_mouse_movement_time;
302 /* Incremented by w32_read_socket whenever it really tries to read
303 events. */
305 #ifdef __STDC__
306 static int volatile input_signal_count;
307 #else
308 static int input_signal_count;
309 #endif
311 extern Lisp_Object Vcommand_line_args, Vsystem_name;
313 extern Lisp_Object Qface, Qmouse_face;
315 #ifndef USE_CRT_DLL
316 extern int errno;
317 #endif
319 /* A mask of extra modifier bits to put into every keyboard char. */
321 extern int extra_keyboard_modifiers;
323 /* Enumeration for overriding/changing the face to use for drawing
324 glyphs in x_draw_glyphs. */
326 enum draw_glyphs_face
328 DRAW_NORMAL_TEXT,
329 DRAW_INVERSE_VIDEO,
330 DRAW_CURSOR,
331 DRAW_MOUSE_FACE,
332 DRAW_IMAGE_RAISED,
333 DRAW_IMAGE_SUNKEN
336 static void x_update_window_end P_ ((struct window *, int, int));
337 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
338 void w32_delete_display P_ ((struct w32_display_info *));
339 static int fast_find_position P_ ((struct window *, int, int *, int *,
340 int *, int *));
341 static void set_output_cursor P_ ((struct cursor_pos *));
342 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
343 int *, int *, int *));
344 static void note_mode_line_highlight P_ ((struct window *, int, int));
345 static void note_mouse_highlight P_ ((struct frame *, int, int));
346 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
347 static void w32_handle_tool_bar_click P_ ((struct frame *,
348 struct input_event *));
349 static void show_mouse_face P_ ((struct w32_display_info *,
350 enum draw_glyphs_face));
351 void clear_mouse_face P_ ((struct w32_display_info *));
353 void x_lower_frame P_ ((struct frame *));
354 void x_scroll_bar_clear P_ ((struct frame *));
355 void x_wm_set_size_hint P_ ((struct frame *, long, int));
356 void x_raise_frame P_ ((struct frame *));
357 void x_set_window_size P_ ((struct frame *, int, int, int));
358 void x_wm_set_window_state P_ ((struct frame *, int));
359 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
360 void w32_initialize P_ ((void));
361 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
362 int x_compute_min_glyph_bounds P_ ((struct frame *));
363 static void x_draw_phys_cursor_glyph P_ ((struct window *,
364 struct glyph_row *,
365 enum draw_glyphs_face));
366 static void x_update_end P_ ((struct frame *));
367 static void w32_frame_up_to_date P_ ((struct frame *));
368 static void w32_reassert_line_highlight P_ ((int, int));
369 static void x_change_line_highlight P_ ((int, int, int, int));
370 static void w32_set_terminal_modes P_ ((void));
371 static void w32_reset_terminal_modes P_ ((void));
372 static void w32_cursor_to P_ ((int, int, int, int));
373 static void x_write_glyphs P_ ((struct glyph *, int));
374 static void x_clear_end_of_line P_ ((int));
375 static void x_clear_frame P_ ((void));
376 static void x_clear_cursor P_ ((struct window *));
377 static void frame_highlight P_ ((struct frame *));
378 static void frame_unhighlight P_ ((struct frame *));
379 static void w32_new_focus_frame P_ ((struct w32_display_info *,
380 struct frame *));
381 static void w32_frame_rehighlight P_ ((struct frame *));
382 static void x_frame_rehighlight P_ ((struct w32_display_info *));
383 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
384 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
385 static void expose_frame P_ ((struct frame *, int, int, int, int));
386 static void expose_window_tree P_ ((struct window *, RECT *));
387 static void expose_window P_ ((struct window *, RECT *));
388 static void expose_area P_ ((struct window *, struct glyph_row *,
389 RECT *, enum glyph_row_area));
390 static void expose_line P_ ((struct window *, struct glyph_row *,
391 RECT *));
392 void x_update_cursor P_ ((struct frame *, int));
393 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
394 static void x_update_window_cursor P_ ((struct window *, int));
395 static void x_erase_phys_cursor P_ ((struct window *));
396 void x_display_cursor P_ ((struct window *w, int, int, int, int, int));
397 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
398 static void w32_draw_bitmap P_ ((struct window *, HDC hdc, struct glyph_row *,
399 enum bitmap_type));
400 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *,
401 HDC, int));
402 static int x_phys_cursor_in_rect_p P_ ((struct window *, RECT *));
403 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
404 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
406 static Lisp_Object Qvendor_specific_keysyms;
409 /***********************************************************************
410 Debugging
411 ***********************************************************************/
413 #if 0
415 /* This is a function useful for recording debugging information about
416 the sequence of occurrences in this file. */
418 struct record
420 char *locus;
421 int type;
424 struct record event_record[100];
426 int event_record_index;
428 record_event (locus, type)
429 char *locus;
430 int type;
432 if (event_record_index == sizeof (event_record) / sizeof (struct record))
433 event_record_index = 0;
435 event_record[event_record_index].locus = locus;
436 event_record[event_record_index].type = type;
437 event_record_index++;
440 #endif /* 0 */
443 void XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
444 XGCValues *xgcv)
446 if (mask & GCForeground)
447 gc->foreground = xgcv->foreground;
448 if (mask & GCBackground)
449 gc->background = xgcv->background;
450 if (mask & GCFont)
451 gc->font = xgcv->font;
454 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
455 XGCValues *xgcv)
457 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
458 bzero (gc, sizeof (XGCValues));
460 XChangeGC (ignore, gc, mask, xgcv);
462 return gc;
465 void XGetGCValues (void* ignore, XGCValues *gc,
466 unsigned long mask, XGCValues *xgcv)
468 XChangeGC (ignore, xgcv, mask, gc);
471 static void
472 w32_set_clip_rectangle (HDC hdc, RECT *rect)
474 if (rect)
476 HRGN clip_region = CreateRectRgnIndirect (rect);
477 SelectClipRgn (hdc, clip_region);
478 DeleteObject (clip_region);
480 else
481 SelectClipRgn (hdc, NULL);
485 /* Draw a hollow rectangle at the specified position. */
486 void
487 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
488 int width, int height)
490 HBRUSH hb, oldhb;
491 HPEN hp, oldhp;
493 hb = CreateSolidBrush (gc->background);
494 hp = CreatePen (PS_SOLID, 0, gc->foreground);
495 oldhb = SelectObject (hdc, hb);
496 oldhp = SelectObject (hdc, hp);
498 Rectangle (hdc, x, y, x + width, y + height);
500 SelectObject (hdc, oldhb);
501 SelectObject (hdc, oldhp);
502 DeleteObject (hb);
503 DeleteObject (hp);
506 /* Draw a filled rectangle at the specified position. */
507 void
508 w32_fill_rect (f, hdc, pix, lprect)
509 FRAME_PTR f;
510 HDC hdc;
511 COLORREF pix;
512 RECT * lprect;
514 HBRUSH hb;
516 hb = CreateSolidBrush (pix);
517 FillRect (hdc, lprect, hb);
518 DeleteObject (hb);
521 void
522 w32_clear_window (f)
523 FRAME_PTR f;
525 RECT rect;
526 HDC hdc = get_frame_dc (f);
528 /* Under certain conditions, this can be called at startup with
529 a console frame pointer before the GUI frame is created. An HDC
530 of 0 indicates this. */
531 if (hdc)
533 GetClientRect (FRAME_W32_WINDOW (f), &rect);
534 w32_clear_rect (f, hdc, &rect);
537 release_frame_dc (f, hdc);
541 /***********************************************************************
542 Starting and ending an update
543 ***********************************************************************/
545 /* Start an update of frame F. This function is installed as a hook
546 for update_begin, i.e. it is called when update_begin is called.
547 This function is called prior to calls to x_update_window_begin for
548 each window being updated. */
550 static void
551 x_update_begin (f)
552 struct frame *f;
554 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
556 if (! FRAME_W32_P (f))
557 return;
559 /* Regenerate display palette before drawing if list of requested
560 colors has changed. */
561 if (display_info->regen_palette)
563 w32_regenerate_palette (f);
564 display_info->regen_palette = FALSE;
569 /* Start update of window W. Set the global variable updated_window
570 to the window being updated and set output_cursor to the cursor
571 position of W. */
573 static void
574 x_update_window_begin (w)
575 struct window *w;
577 struct frame *f = XFRAME (WINDOW_FRAME (w));
578 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
580 updated_window = w;
581 set_output_cursor (&w->cursor);
583 BLOCK_INPUT;
585 if (f == display_info->mouse_face_mouse_frame)
587 /* Don't do highlighting for mouse motion during the update. */
588 display_info->mouse_face_defer = 1;
590 /* If F needs to be redrawn, simply forget about any prior mouse
591 highlighting. */
592 if (FRAME_GARBAGED_P (f))
593 display_info->mouse_face_window = Qnil;
595 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
596 their mouse_face_p flag set, which means that they are always
597 unequal to rows in a desired matrix which never have that
598 flag set. So, rows containing mouse-face glyphs are never
599 scrolled, and we don't have to switch the mouse highlight off
600 here to prevent it from being scrolled. */
602 /* Can we tell that this update does not affect the window
603 where the mouse highlight is? If so, no need to turn off.
604 Likewise, don't do anything if the frame is garbaged;
605 in that case, the frame's current matrix that we would use
606 is all wrong, and we will redisplay that line anyway. */
607 if (!NILP (display_info->mouse_face_window)
608 && w == XWINDOW (display_info->mouse_face_window))
610 int i;
612 for (i = 0; i < w->desired_matrix->nrows; ++i)
613 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
614 break;
616 if (i < w->desired_matrix->nrows)
617 clear_mouse_face (display_info);
619 #endif /* 0 */
622 UNBLOCK_INPUT;
626 /* Draw a vertical window border to the right of window W if W doesn't
627 have vertical scroll bars. */
629 static void
630 x_draw_vertical_border (w)
631 struct window *w;
633 struct frame *f = XFRAME (WINDOW_FRAME (w));
635 /* Redraw borders between horizontally adjacent windows. Don't
636 do it for frames with vertical scroll bars because either the
637 right scroll bar of a window, or the left scroll bar of its
638 neighbor will suffice as a border. */
639 if (!WINDOW_RIGHTMOST_P (w)
640 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
642 RECT r;
643 HDC hdc;
645 window_box_edges (w, -1, &r.left, &r.top, &r.right, &r.bottom);
646 r.left = r.right + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
647 r.right = r.left + 1;
648 r.bottom -= 1;
650 hdc = get_frame_dc (f);
651 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
652 release_frame_dc (f, hdc);
657 /* End update of window W (which is equal to updated_window).
659 Draw vertical borders between horizontally adjacent windows, and
660 display W's cursor if CURSOR_ON_P is non-zero.
662 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
663 glyphs in mouse-face were overwritten. In that case we have to
664 make sure that the mouse-highlight is properly redrawn.
666 W may be a menu bar pseudo-window in case we don't have X toolkit
667 support. Such windows don't have a cursor, so don't display it
668 here. */
670 static void
671 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
672 struct window *w;
673 int cursor_on_p, mouse_face_overwritten_p;
675 if (!w->pseudo_window_p)
677 struct w32_display_info *dpyinfo
678 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
680 BLOCK_INPUT;
682 /* If a row with mouse-face was overwritten, arrange for
683 XTframe_up_to_date to redisplay the mouse highlight. */
684 if (mouse_face_overwritten_p)
686 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
687 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
688 dpyinfo->mouse_face_window = Qnil;
691 if (cursor_on_p)
692 x_display_and_set_cursor (w, 1, output_cursor.hpos,
693 output_cursor.vpos,
694 output_cursor.x, output_cursor.y);
696 x_draw_vertical_border (w);
697 UNBLOCK_INPUT;
700 updated_window = NULL;
704 /* End update of frame F. This function is installed as a hook in
705 update_end. */
707 static void
708 x_update_end (f)
709 struct frame *f;
711 if (! FRAME_W32_P (f))
712 return;
714 /* Mouse highlight may be displayed again. */
715 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
719 /* This function is called from various places in xdisp.c whenever a
720 complete update has been performed. The global variable
721 updated_window is not available here. */
723 static void
724 w32_frame_up_to_date (f)
725 struct frame *f;
727 if (FRAME_W32_P (f))
729 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
730 if (dpyinfo->mouse_face_deferred_gc
731 || f == dpyinfo->mouse_face_mouse_frame)
733 BLOCK_INPUT;
734 if (dpyinfo->mouse_face_mouse_frame)
735 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
736 dpyinfo->mouse_face_mouse_x,
737 dpyinfo->mouse_face_mouse_y);
738 dpyinfo->mouse_face_deferred_gc = 0;
739 UNBLOCK_INPUT;
745 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
746 arrow bitmaps, or clear the areas where they would be displayed
747 before DESIRED_ROW is made current. The window being updated is
748 found in updated_window. This function It is called from
749 update_window_line only if it is known that there are differences
750 between bitmaps to be drawn between current row and DESIRED_ROW. */
752 static void
753 x_after_update_window_line (desired_row)
754 struct glyph_row *desired_row;
756 struct window *w = updated_window;
758 xassert (w);
760 if (!desired_row->mode_line_p && !w->pseudo_window_p)
762 struct frame *f;
763 int width;
765 BLOCK_INPUT;
766 x_draw_row_bitmaps (w, desired_row);
768 /* When a window has disappeared, make sure that no rest of
769 full-width rows stays visible in the internal border. */
770 if (windows_or_buffers_changed
771 && (f = XFRAME (w->frame),
772 width = FRAME_INTERNAL_BORDER_WIDTH (f),
773 width != 0))
775 int height = desired_row->visible_height;
776 int x = (window_box_right (w, -1)
777 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
778 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
779 HDC hdc = get_frame_dc (f);
781 w32_clear_area (f, hdc, x, y, width, height);
782 release_frame_dc (f, hdc);
785 UNBLOCK_INPUT;
790 /* Draw the bitmap WHICH in one of the areas to the left or right of
791 window W. ROW is the glyph row for which to display the bitmap; it
792 determines the vertical position at which the bitmap has to be
793 drawn. */
795 static void
796 w32_draw_bitmap (w, hdc, row, which)
797 struct window *w;
798 HDC hdc;
799 struct glyph_row *row;
800 enum bitmap_type which;
802 struct frame *f = XFRAME (WINDOW_FRAME (w));
803 Window window = FRAME_W32_WINDOW (f);
804 HDC compat_hdc;
805 int x, y, wd, h, dy;
806 HBITMAP pixmap;
807 HANDLE horig_obj;
808 struct face *face;
810 /* Must clip because of partially visible lines. */
811 w32_clip_to_row (w, row, hdc, 1);
813 switch (which)
815 case LEFT_TRUNCATION_BITMAP:
816 wd = left_width;
817 h = left_height;
818 pixmap = left_bmp;
819 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
820 - wd
821 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
822 break;
824 case OVERLAY_ARROW_BITMAP:
825 wd = ov_width;
826 h = ov_height;
827 pixmap = ov_bmp;
828 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
829 - wd
830 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
831 break;
833 case RIGHT_TRUNCATION_BITMAP:
834 wd = right_width;
835 h = right_height;
836 pixmap = right_bmp;
837 x = window_box_right (w, -1);
838 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
839 break;
841 case CONTINUED_LINE_BITMAP:
842 wd = continued_width;
843 h = continued_height;
844 pixmap = continued_bmp;
845 x = window_box_right (w, -1);
846 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
847 break;
849 case CONTINUATION_LINE_BITMAP:
850 wd = continuation_width;
851 h = continuation_height;
852 pixmap = continuation_bmp;
853 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
854 - wd
855 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
856 break;
858 case ZV_LINE_BITMAP:
859 wd = zv_width;
860 h = zv_height;
861 pixmap = zv_bmp;
862 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
863 - wd
864 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
865 break;
867 default:
868 abort ();
871 /* Convert to frame coordinates. Set dy to the offset in the row to
872 start drawing the bitmap. */
873 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
874 dy = (row->height - h) / 2;
876 /* Draw the bitmap. */
877 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
879 compat_hdc = CreateCompatibleDC (hdc);
880 SaveDC (hdc);
882 horig_obj = SelectObject (compat_hdc, pixmap);
883 SetTextColor (hdc, face->background);
884 SetBkColor (hdc, face->foreground);
886 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, SRCCOPY);
888 SelectObject (compat_hdc, horig_obj);
889 DeleteDC (compat_hdc);
890 RestoreDC (hdc, -1);
894 /* Draw flags bitmaps for glyph row ROW on window W. Call this
895 function with input blocked. */
897 static void
898 x_draw_row_bitmaps (w, row)
899 struct window *w;
900 struct glyph_row *row;
902 struct frame *f = XFRAME (w->frame);
903 enum bitmap_type bitmap;
904 struct face *face;
905 int header_line_height = -1;
906 HDC hdc;
908 xassert (interrupt_input_blocked);
910 /* If row is completely invisible, because of vscrolling, we
911 don't have to draw anything. */
912 if (row->visible_height <= 0)
913 return;
915 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
916 PREPARE_FACE_FOR_DISPLAY (f, face);
918 /* Decide which bitmap to draw at the left side. */
919 if (row->overlay_arrow_p)
920 bitmap = OVERLAY_ARROW_BITMAP;
921 else if (row->truncated_on_left_p)
922 bitmap = LEFT_TRUNCATION_BITMAP;
923 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
924 bitmap = CONTINUATION_LINE_BITMAP;
925 else if (row->indicate_empty_line_p)
926 bitmap = ZV_LINE_BITMAP;
927 else
928 bitmap = NO_BITMAP;
930 hdc = get_frame_dc (f);
932 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
933 the flags area. */
934 if (bitmap == NO_BITMAP
935 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
936 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
938 /* If W has a vertical border to its left, don't draw over it. */
939 int border = ((XFASTINT (w->left) > 0
940 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
941 ? 1 : 0);
942 int left = window_box_left (w, -1);
944 if (header_line_height < 0)
945 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
947 w32_fill_area (f, hdc, face->background,
948 left - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) + border,
949 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
950 row->y)),
951 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
952 row->visible_height);
955 /* Draw the left bitmap. */
956 if (bitmap != NO_BITMAP)
957 w32_draw_bitmap (w, hdc, row, bitmap);
959 /* Decide which bitmap to draw at the right side. */
960 if (row->truncated_on_right_p)
961 bitmap = RIGHT_TRUNCATION_BITMAP;
962 else if (row->continued_p)
963 bitmap = CONTINUED_LINE_BITMAP;
964 else
965 bitmap = NO_BITMAP;
967 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
968 the flags area. */
969 if (bitmap == NO_BITMAP
970 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
971 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
973 int right = window_box_right (w, -1);
975 if (header_line_height < 0)
976 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
978 w32_fill_area (f, hdc, face->background,
979 right,
980 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
981 row->y)),
982 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
983 row->visible_height);
986 /* Draw the right bitmap. */
987 if (bitmap != NO_BITMAP)
988 w32_draw_bitmap (w, hdc, row, bitmap);
990 release_frame_dc (f, hdc);
994 /***********************************************************************
995 Line Highlighting
996 ***********************************************************************/
998 /* External interface to control of standout mode. Not used for W32
999 frames. Aborts when called. */
1001 static void
1002 w32_reassert_line_highlight (new, vpos)
1003 int new, vpos;
1005 struct frame *f;
1007 if (updating_frame)
1008 f = updating_frame;
1009 else
1010 f = SELECTED_FRAME ();
1012 if (! FRAME_W32_P (f))
1013 return;
1015 abort ();
1019 /* Call this when about to modify line at position VPOS and change
1020 whether it is highlighted. Not used for W32 frames. Aborts when
1021 called. */
1023 static void
1024 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1025 int new_highlight, vpos, y, first_unused_hpos;
1027 struct frame *f;
1029 if (updating_frame)
1030 f = updating_frame;
1031 else
1032 f = SELECTED_FRAME ();
1034 if (! FRAME_W32_P (f))
1035 return;
1037 abort ();
1041 /* This is called when starting Emacs and when restarting after
1042 suspend. When starting Emacs, no window is mapped. And nothing
1043 must be done to Emacs's own window if it is suspended (though that
1044 rarely happens). */
1046 static void
1047 w32_set_terminal_modes (void)
1051 /* This is called when exiting or suspending Emacs. Exiting will make
1052 the W32 windows go away, and suspending requires no action. */
1054 static void
1055 w32_reset_terminal_modes (void)
1061 /***********************************************************************
1062 Output Cursor
1063 ***********************************************************************/
1065 /* Set the global variable output_cursor to CURSOR. All cursor
1066 positions are relative to updated_window. */
1068 static void
1069 set_output_cursor (cursor)
1070 struct cursor_pos *cursor;
1072 output_cursor.hpos = cursor->hpos;
1073 output_cursor.vpos = cursor->vpos;
1074 output_cursor.x = cursor->x;
1075 output_cursor.y = cursor->y;
1079 /* Set a nominal cursor position.
1081 HPOS and VPOS are column/row positions in a window glyph matrix. X
1082 and Y are window text area relative pixel positions.
1084 If this is done during an update, updated_window will contain the
1085 window that is being updated and the position is the future output
1086 cursor position for that window. If updated_window is null, use
1087 selected_window and display the cursor at the given position. */
1089 static void
1090 w32_cursor_to (vpos, hpos, y, x)
1091 int vpos, hpos, y, x;
1093 struct window *w;
1095 /* If updated_window is not set, work on selected_window. */
1096 if (updated_window)
1097 w = updated_window;
1098 else
1099 w = XWINDOW (selected_window);
1101 /* Set the output cursor. */
1102 output_cursor.hpos = hpos;
1103 output_cursor.vpos = vpos;
1104 output_cursor.x = x;
1105 output_cursor.y = y;
1107 /* If not called as part of an update, really display the cursor.
1108 This will also set the cursor position of W. */
1109 if (updated_window == NULL)
1111 BLOCK_INPUT;
1112 x_display_cursor (w, 1, hpos, vpos, x, y);
1113 UNBLOCK_INPUT;
1119 /***********************************************************************
1120 Display Iterator
1121 ***********************************************************************/
1123 /* Function prototypes of this page. */
1125 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1126 struct glyph *,
1127 wchar_t *,
1128 int *));
1129 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1130 int, wchar_t *, int));
1131 static XCharStruct *w32_per_char_metric P_ ((XFontStruct *,
1132 wchar_t *,
1133 enum w32_char_font_type));
1134 static enum w32_char_font_type
1135 w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
1136 static void x_append_glyph P_ ((struct it *));
1137 static void x_append_composite_glyph P_ ((struct it *));
1138 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1139 int, int, double));
1140 static void x_produce_glyphs P_ ((struct it *));
1141 static void x_produce_image_glyph P_ ((struct it *it));
1144 /* Dealing with bits of wchar_t as if they were an XChar2B. */
1145 #define BUILD_WCHAR_T(byte1, byte2) \
1146 ((wchar_t)((((byte1) & 0x00ff) << 8) | ((byte2) & 0x00ff)))
1149 #define BYTE1(ch) \
1150 (((ch) & 0xff00) >> 8)
1152 #define BYTE2(ch) \
1153 ((ch) & 0x00ff)
1156 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
1157 If CHAR2B is not contained in FONT, the font's default character
1158 metric is returned. */
1160 static int
1161 w32_bdf_per_char_metric (font, char2b, dim, pcm)
1162 XFontStruct *font;
1163 wchar_t *char2b;
1164 int dim;
1165 XCharStruct * pcm;
1167 glyph_metric * bdf_metric;
1168 char buf[2];
1170 if (dim == 1)
1171 buf[0] = (char)(*char2b);
1172 else
1174 buf[0] = BYTE1 (*char2b);
1175 buf[1] = BYTE2 (*char2b);
1178 bdf_metric = w32_BDF_TextMetric (font->bdf, buf, dim);
1180 if (bdf_metric)
1182 pcm->width = bdf_metric->dwidth;
1183 pcm->lbearing = bdf_metric->bbox;
1184 pcm->rbearing = bdf_metric->dwidth
1185 - (bdf_metric->bbox + bdf_metric->bbw);
1186 pcm->ascent = bdf_metric->bboy + bdf_metric->bbh;
1187 pcm->descent = -bdf_metric->bboy;
1189 return 1;
1191 return 0;
1195 static int
1196 w32_native_per_char_metric (font, char2b, font_type, pcm)
1197 XFontStruct *font;
1198 wchar_t *char2b;
1199 enum w32_char_font_type font_type;
1200 XCharStruct * pcm;
1202 HDC hdc = GetDC (NULL);
1203 HFONT old_font;
1204 BOOL retval = FALSE;
1206 xassert (font && char2b);
1207 xassert (font->hfont);
1208 xassert (font_type == UNICODE_FONT || font_type == ANSI_FONT);
1210 old_font = SelectObject (hdc, font->hfont);
1212 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
1214 ABC char_widths;
1216 if (font_type == UNICODE_FONT)
1217 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
1218 else
1219 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
1221 if (retval)
1223 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
1224 pcm->lbearing = char_widths.abcA;
1225 pcm->rbearing = pcm->width - char_widths.abcC;
1226 pcm->ascent = FONT_BASE (font);
1227 pcm->descent = FONT_DESCENT (font);
1231 if (!retval)
1233 /* Either font is not a True-type font, or GetCharABCWidthsW
1234 failed (it is not supported on Windows 9x for instance), so we
1235 can't determine the full info we would like. All is not lost
1236 though - we can call GetTextExtentPoint32 to get rbearing and
1237 deduce width based on the font's per-string overhang. lbearing
1238 is assumed to be zero. */
1240 /* TODO: Some Thai characters (and other composites if Windows
1241 supports them) do have lbearing, and report their total width
1242 as zero. Need some way of handling them when
1243 GetCharABCWidthsW fails. */
1244 SIZE sz;
1246 if (font_type == UNICODE_FONT)
1247 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
1248 else
1249 retval = GetTextExtentPoint32A (hdc, (char*)char2b, 1, &sz);
1251 if (retval)
1253 pcm->width = sz.cx - font->tm.tmOverhang;
1254 pcm->rbearing = sz.cx;
1255 pcm->lbearing = 0;
1256 pcm->ascent = FONT_BASE (font);
1257 pcm->descent = FONT_DESCENT (font);
1262 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1264 retval = FALSE;
1267 SelectObject (hdc, old_font);
1268 ReleaseDC (NULL, hdc);
1270 return retval;
1274 static XCharStruct *
1275 w32_per_char_metric (font, char2b, font_type)
1276 XFontStruct *font;
1277 wchar_t *char2b;
1278 enum w32_char_font_type font_type;
1280 /* The result metric information. */
1281 XCharStruct *pcm;
1282 BOOL retval;
1284 xassert (font && char2b);
1285 xassert (font_type != UNKNOWN_FONT);
1287 /* Handle the common cases quickly. */
1288 if (!font->bdf && font->per_char == NULL)
1289 /* TODO: determine whether char2b exists in font? */
1290 return &font->max_bounds;
1291 else if (!font->bdf && *char2b < 128)
1292 return &font->per_char[*char2b];
1294 pcm = &font->scratch;
1296 if (font_type == BDF_1D_FONT)
1297 retval = w32_bdf_per_char_metric (font, char2b, 1, pcm);
1298 else if (font_type == BDF_2D_FONT)
1299 retval = w32_bdf_per_char_metric (font, char2b, 2, pcm);
1300 else
1301 retval = w32_native_per_char_metric (font, char2b, font_type, pcm);
1303 if (retval)
1304 return pcm;
1306 return NULL;
1309 void
1310 w32_cache_char_metrics (font)
1311 XFontStruct *font;
1313 wchar_t char2b = L'x';
1315 /* Cache char metrics for the common cases. */
1316 if (font->bdf)
1318 /* TODO: determine whether font is fixed-pitch. */
1319 if (!w32_bdf_per_char_metric (font, &char2b, 1, &font->max_bounds))
1321 /* Use the font width and height as max bounds, as not all BDF
1322 fonts contain the letter 'x'. */
1323 font->max_bounds.width = FONT_MAX_WIDTH (font);
1324 font->max_bounds.lbearing = -font->bdf->llx;
1325 font->max_bounds.rbearing = FONT_MAX_WIDTH (font) - font->bdf->urx;
1326 font->max_bounds.ascent = FONT_BASE (font);
1327 font->max_bounds.descent = FONT_DESCENT (font);
1330 else
1332 if (((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH) != 0)
1333 /* Some fonts (eg DBCS fonts) are marked as fixed width even
1334 though they contain characters of different widths. */
1335 || (font->tm.tmMaxCharWidth != font->tm.tmAveCharWidth))
1337 /* Font is not fixed pitch, so cache per_char info for the
1338 ASCII characters. It would be much more work, and probably
1339 not worth it, to cache other chars, since we may change
1340 between using Unicode and ANSI text drawing functions at
1341 run-time. */
1342 int i;
1344 font->per_char = xmalloc (128 * sizeof(XCharStruct));
1345 for (i = 0; i < 128; i++)
1347 char2b = i;
1348 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1349 &font->per_char[i]);
1352 else
1353 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1354 &font->max_bounds);
1359 /* Determine if a font is double byte. */
1360 int w32_font_is_double_byte (XFontStruct *font)
1362 return font->double_byte_p;
1366 static BOOL
1367 w32_use_unicode_for_codepage (codepage)
1368 int codepage;
1370 /* If the current codepage is supported, use Unicode for output. */
1371 return (w32_enable_unicode_output
1372 && codepage != CP_8BIT
1373 && (codepage == CP_UNICODE || IsValidCodePage (codepage)));
1376 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1377 the two-byte form of C. Encoding is returned in *CHAR2B. */
1379 static INLINE enum w32_char_font_type
1380 w32_encode_char (c, char2b, font_info, two_byte_p)
1381 int c;
1382 wchar_t *char2b;
1383 struct font_info *font_info;
1384 int * two_byte_p;
1386 int charset = CHAR_CHARSET (c);
1387 int codepage;
1388 int unicode_p = 0;
1390 XFontStruct *font = font_info->font;
1392 xassert (two_byte_p);
1394 *two_byte_p = w32_font_is_double_byte (font);
1396 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1397 This may be either a program in a special encoder language or a
1398 fixed encoding. */
1399 if (font_info->font_encoder)
1401 /* It's a program. */
1402 struct ccl_program *ccl = font_info->font_encoder;
1404 if (CHARSET_DIMENSION (charset) == 1)
1406 ccl->reg[0] = charset;
1407 ccl->reg[1] = BYTE2 (*char2b);
1409 else
1411 ccl->reg[0] = charset;
1412 ccl->reg[1] = BYTE1 (*char2b);
1413 ccl->reg[2] = BYTE2 (*char2b);
1416 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1418 /* We assume that MSBs are appropriately set/reset by CCL
1419 program. */
1420 if (!*two_byte_p) /* 1-byte font */
1421 *char2b = BUILD_WCHAR_T (0, ccl->reg[1]);
1422 else
1423 *char2b = BUILD_WCHAR_T (ccl->reg[1], ccl->reg[2]);
1425 else if (font_info->encoding[charset])
1427 /* Fixed encoding scheme. See fontset.h for the meaning of the
1428 encoding numbers. */
1429 int enc = font_info->encoding[charset];
1431 if ((enc == 1 || enc == 2)
1432 && CHARSET_DIMENSION (charset) == 2)
1433 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b) | 0x80, BYTE2 (*char2b));
1435 if (enc == 1 || enc == 3
1436 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1437 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b), BYTE2 (*char2b) | 0x80);
1438 else if (enc == 4)
1440 int sjis1, sjis2;
1442 ENCODE_SJIS (BYTE1 (*char2b), BYTE2 (*char2b),
1443 sjis1, sjis2);
1444 *char2b = BUILD_WCHAR_T (sjis1, sjis2);
1447 codepage = w32_codepage_for_font (font_info->name);
1449 /* If charset is not ASCII or Latin-1, may need to move it into
1450 Unicode space. */
1451 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
1452 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1
1453 && charset != CHARSET_8_BIT_CONTROL && charset != CHARSET_8_BIT_GRAPHIC)
1455 char temp[3];
1456 temp[0] = BYTE1 (*char2b);
1457 temp[1] = BYTE2 (*char2b);
1458 temp[2] = '\0';
1459 if (codepage != CP_UNICODE)
1461 if (temp[0])
1462 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1463 else
1464 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1466 unicode_p = 1;
1467 *two_byte_p = 1;
1469 if (!font)
1470 return UNKNOWN_FONT;
1471 else if (font->bdf && CHARSET_DIMENSION (charset) == 1)
1472 return BDF_1D_FONT;
1473 else if (font->bdf)
1474 return BDF_2D_FONT;
1475 else if (unicode_p)
1476 return UNICODE_FONT;
1477 else
1478 return ANSI_FONT;
1482 /* Get face and two-byte form of character C in face FACE_ID on frame
1483 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1484 means we want to display multibyte text. Value is a pointer to a
1485 realized face that is ready for display. */
1487 static INLINE struct face *
1488 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1489 struct frame *f;
1490 int c, face_id;
1491 wchar_t *char2b;
1492 int multibyte_p;
1494 struct face *face = FACE_FROM_ID (f, face_id);
1496 if (!multibyte_p)
1498 /* Unibyte case. We don't have to encode, but we have to make
1499 sure to use a face suitable for unibyte. */
1500 *char2b = BUILD_WCHAR_T (0, c);
1501 face_id = FACE_FOR_CHAR (f, face, c);
1502 face = FACE_FROM_ID (f, face_id);
1504 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1506 /* Case of ASCII in a face known to fit ASCII. */
1507 *char2b = BUILD_WCHAR_T (0, c);
1509 else
1511 int c1, c2, charset;
1513 /* Split characters into bytes. If c2 is -1 afterwards, C is
1514 really a one-byte character so that byte1 is zero. */
1515 SPLIT_CHAR (c, charset, c1, c2);
1516 if (c2 > 0)
1517 *char2b = BUILD_WCHAR_T (c1, c2);
1518 else
1519 *char2b = BUILD_WCHAR_T (0, c1);
1521 /* Maybe encode the character in *CHAR2B. */
1522 if (face->font != NULL)
1524 struct font_info *font_info
1525 = FONT_INFO_FROM_ID (f, face->font_info_id);
1526 if (font_info)
1527 w32_encode_char (c, char2b, font_info, &multibyte_p);
1531 /* Make sure X resources of the face are allocated. */
1532 xassert (face != NULL);
1533 PREPARE_FACE_FOR_DISPLAY (f, face);
1535 return face;
1539 /* Get face and two-byte form of character glyph GLYPH on frame F.
1540 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1541 a pointer to a realized face that is ready for display. */
1543 static INLINE struct face *
1544 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1545 struct frame *f;
1546 struct glyph *glyph;
1547 wchar_t *char2b;
1548 int *two_byte_p;
1550 struct face *face;
1551 int dummy = 0;
1553 xassert (glyph->type == CHAR_GLYPH);
1554 face = FACE_FROM_ID (f, glyph->face_id);
1556 if (two_byte_p)
1557 *two_byte_p = 0;
1558 else
1559 two_byte_p = &dummy;
1561 if (!glyph->multibyte_p)
1563 /* Unibyte case. We don't have to encode, but we have to make
1564 sure to use a face suitable for unibyte. */
1565 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1567 else if (glyph->u.ch < 128
1568 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1570 /* Case of ASCII in a face known to fit ASCII. */
1571 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1573 else
1575 int c1, c2, charset;
1577 /* Split characters into bytes. If c2 is -1 afterwards, C is
1578 really a one-byte character so that byte1 is zero. */
1579 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1580 if (c2 > 0)
1581 *char2b = BUILD_WCHAR_T (c1, c2);
1582 else
1583 *char2b = BUILD_WCHAR_T (0, c1);
1585 /* Maybe encode the character in *CHAR2B. */
1586 if (charset != CHARSET_ASCII)
1588 struct font_info *font_info
1589 = FONT_INFO_FROM_ID (f, face->font_info_id);
1590 if (font_info)
1592 glyph->w32_font_type
1593 = w32_encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
1598 /* Make sure X resources of the face are allocated. */
1599 xassert (face != NULL);
1600 PREPARE_FACE_FOR_DISPLAY (f, face);
1601 return face;
1605 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1606 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1608 static INLINE void
1609 x_append_glyph (it)
1610 struct it *it;
1612 struct glyph *glyph;
1613 enum glyph_row_area area = it->area;
1615 xassert (it->glyph_row);
1616 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1618 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1619 if (glyph < it->glyph_row->glyphs[area + 1])
1621 glyph->charpos = CHARPOS (it->position);
1622 glyph->object = it->object;
1623 glyph->pixel_width = it->pixel_width;
1624 glyph->voffset = it->voffset;
1625 glyph->type = CHAR_GLYPH;
1626 glyph->multibyte_p = it->multibyte_p;
1627 glyph->left_box_line_p = it->start_of_box_run_p;
1628 glyph->right_box_line_p = it->end_of_box_run_p;
1629 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1630 || it->phys_descent > it->descent);
1631 glyph->padding_p = 0;
1632 glyph->glyph_not_available_p = it->glyph_not_available_p;
1633 glyph->face_id = it->face_id;
1634 glyph->u.ch = it->char_to_display;
1635 glyph->w32_font_type = UNKNOWN_FONT;
1636 ++it->glyph_row->used[area];
1640 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1641 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1643 static INLINE void
1644 x_append_composite_glyph (it)
1645 struct it *it;
1647 struct glyph *glyph;
1648 enum glyph_row_area area = it->area;
1650 xassert (it->glyph_row);
1652 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1653 if (glyph < it->glyph_row->glyphs[area + 1])
1655 glyph->charpos = CHARPOS (it->position);
1656 glyph->object = it->object;
1657 glyph->pixel_width = it->pixel_width;
1658 glyph->voffset = it->voffset;
1659 glyph->type = COMPOSITE_GLYPH;
1660 glyph->multibyte_p = it->multibyte_p;
1661 glyph->left_box_line_p = it->start_of_box_run_p;
1662 glyph->right_box_line_p = it->end_of_box_run_p;
1663 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1664 || it->phys_descent > it->descent);
1665 glyph->padding_p = 0;
1666 glyph->glyph_not_available_p = 0;
1667 glyph->face_id = it->face_id;
1668 glyph->u.cmp_id = it->cmp_id;
1669 glyph->w32_font_type = UNKNOWN_FONT;
1670 ++it->glyph_row->used[area];
1675 /* Change IT->ascent and IT->height according to the setting of
1676 IT->voffset. */
1678 static INLINE void
1679 take_vertical_position_into_account (it)
1680 struct it *it;
1682 if (it->voffset)
1684 if (it->voffset < 0)
1685 /* Increase the ascent so that we can display the text higher
1686 in the line. */
1687 it->ascent += abs (it->voffset);
1688 else
1689 /* Increase the descent so that we can display the text lower
1690 in the line. */
1691 it->descent += it->voffset;
1696 /* Produce glyphs/get display metrics for the image IT is loaded with.
1697 See the description of struct display_iterator in dispextern.h for
1698 an overview of struct display_iterator. */
1700 static void
1701 x_produce_image_glyph (it)
1702 struct it *it;
1704 struct image *img;
1705 struct face *face;
1707 xassert (it->what == IT_IMAGE);
1709 face = FACE_FROM_ID (it->f, it->face_id);
1710 img = IMAGE_FROM_ID (it->f, it->image_id);
1711 xassert (img);
1713 /* Make sure X resources of the face and image are loaded. */
1714 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1715 prepare_image_for_display (it->f, img);
1717 it->ascent = it->phys_ascent = image_ascent (img, face);
1718 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1719 it->pixel_width = img->width + 2 * img->hmargin;
1721 it->nglyphs = 1;
1723 if (face->box != FACE_NO_BOX)
1725 if (face->box_line_width > 0)
1727 it->ascent += face->box_line_width;
1728 it->descent += face->box_line_width;
1731 if (it->start_of_box_run_p)
1732 it->pixel_width += abs (face->box_line_width);
1733 if (it->end_of_box_run_p)
1734 it->pixel_width += abs (face->box_line_width);
1737 take_vertical_position_into_account (it);
1739 if (it->glyph_row)
1741 struct glyph *glyph;
1742 enum glyph_row_area area = it->area;
1744 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1745 if (glyph < it->glyph_row->glyphs[area + 1])
1747 glyph->charpos = CHARPOS (it->position);
1748 glyph->object = it->object;
1749 glyph->pixel_width = it->pixel_width;
1750 glyph->voffset = it->voffset;
1751 glyph->type = IMAGE_GLYPH;
1752 glyph->multibyte_p = it->multibyte_p;
1753 glyph->left_box_line_p = it->start_of_box_run_p;
1754 glyph->right_box_line_p = it->end_of_box_run_p;
1755 glyph->overlaps_vertically_p = 0;
1756 glyph->padding_p = 0;
1757 glyph->glyph_not_available_p = 0;
1758 glyph->face_id = it->face_id;
1759 glyph->u.img_id = img->id;
1760 glyph->w32_font_type = UNKNOWN_FONT;
1761 ++it->glyph_row->used[area];
1767 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1768 of the glyph, WIDTH and HEIGHT are the width and height of the
1769 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1770 ascent of the glyph (0 <= ASCENT <= 1). */
1772 static void
1773 x_append_stretch_glyph (it, object, width, height, ascent)
1774 struct it *it;
1775 Lisp_Object object;
1776 int width, height;
1777 double ascent;
1779 struct glyph *glyph;
1780 enum glyph_row_area area = it->area;
1782 xassert (ascent >= 0 && ascent <= 1);
1784 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1785 if (glyph < it->glyph_row->glyphs[area + 1])
1787 glyph->charpos = CHARPOS (it->position);
1788 glyph->object = object;
1789 glyph->pixel_width = width;
1790 glyph->voffset = it->voffset;
1791 glyph->type = STRETCH_GLYPH;
1792 glyph->multibyte_p = it->multibyte_p;
1793 glyph->left_box_line_p = it->start_of_box_run_p;
1794 glyph->right_box_line_p = it->end_of_box_run_p;
1795 glyph->overlaps_vertically_p = 0;
1796 glyph->padding_p = 0;
1797 glyph->glyph_not_available_p = 0;
1798 glyph->face_id = it->face_id;
1799 glyph->u.stretch.ascent = height * ascent;
1800 glyph->u.stretch.height = height;
1801 glyph->w32_font_type = UNKNOWN_FONT;
1802 ++it->glyph_row->used[area];
1807 /* Produce a stretch glyph for iterator IT. IT->object is the value
1808 of the glyph property displayed. The value must be a list
1809 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1810 being recognized:
1812 1. `:width WIDTH' specifies that the space should be WIDTH *
1813 canonical char width wide. WIDTH may be an integer or floating
1814 point number.
1816 2. `:relative-width FACTOR' specifies that the width of the stretch
1817 should be computed from the width of the first character having the
1818 `glyph' property, and should be FACTOR times that width.
1820 3. `:align-to HPOS' specifies that the space should be wide enough
1821 to reach HPOS, a value in canonical character units.
1823 Exactly one of the above pairs must be present.
1825 4. `:height HEIGHT' specifies that the height of the stretch produced
1826 should be HEIGHT, measured in canonical character units.
1828 5. `:relative-height FACTOR' specifies that the height of the the
1829 stretch should be FACTOR times the height of the characters having
1830 the glyph property.
1832 Either none or exactly one of 4 or 5 must be present.
1834 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1835 of the stretch should be used for the ascent of the stretch.
1836 ASCENT must be in the range 0 <= ASCENT <= 100. */
1838 #define NUMVAL(X) \
1839 ((INTEGERP (X) || FLOATP (X)) \
1840 ? XFLOATINT (X) \
1841 : - 1)
1844 static void
1845 x_produce_stretch_glyph (it)
1846 struct it *it;
1848 /* (space :width WIDTH :height HEIGHT. */
1849 #if GLYPH_DEBUG
1850 extern Lisp_Object Qspace;
1851 #endif
1852 extern Lisp_Object QCwidth, QCheight, QCascent;
1853 extern Lisp_Object QCrelative_width, QCrelative_height;
1854 extern Lisp_Object QCalign_to;
1855 Lisp_Object prop, plist;
1856 double width = 0, height = 0, ascent = 0;
1857 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1858 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1860 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1862 /* List should start with `space'. */
1863 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1864 plist = XCDR (it->object);
1866 /* Compute the width of the stretch. */
1867 if (prop = Fplist_get (plist, QCwidth),
1868 NUMVAL (prop) > 0)
1869 /* Absolute width `:width WIDTH' specified and valid. */
1870 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1871 else if (prop = Fplist_get (plist, QCrelative_width),
1872 NUMVAL (prop) > 0)
1874 /* Relative width `:relative-width FACTOR' specified and valid.
1875 Compute the width of the characters having the `glyph'
1876 property. */
1877 struct it it2;
1878 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1880 it2 = *it;
1881 if (it->multibyte_p)
1883 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1884 - IT_BYTEPOS (*it));
1885 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1887 else
1888 it2.c = *p, it2.len = 1;
1890 it2.glyph_row = NULL;
1891 it2.what = IT_CHARACTER;
1892 x_produce_glyphs (&it2);
1893 width = NUMVAL (prop) * it2.pixel_width;
1895 else if (prop = Fplist_get (plist, QCalign_to),
1896 NUMVAL (prop) > 0)
1897 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1898 else
1899 /* Nothing specified -> width defaults to canonical char width. */
1900 width = CANON_X_UNIT (it->f);
1902 /* Compute height. */
1903 if (prop = Fplist_get (plist, QCheight),
1904 NUMVAL (prop) > 0)
1905 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1906 else if (prop = Fplist_get (plist, QCrelative_height),
1907 NUMVAL (prop) > 0)
1908 height = FONT_HEIGHT (font) * NUMVAL (prop);
1909 else
1910 height = FONT_HEIGHT (font);
1912 /* Compute percentage of height used for ascent. If
1913 `:ascent ASCENT' is present and valid, use that. Otherwise,
1914 derive the ascent from the font in use. */
1915 if (prop = Fplist_get (plist, QCascent),
1916 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1917 ascent = NUMVAL (prop) / 100.0;
1918 else
1919 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
1921 if (width <= 0)
1922 width = 1;
1923 if (height <= 0)
1924 height = 1;
1926 if (it->glyph_row)
1928 Lisp_Object object = it->stack[it->sp - 1].string;
1929 if (!STRINGP (object))
1930 object = it->w->buffer;
1931 x_append_stretch_glyph (it, object, width, height, ascent);
1934 it->pixel_width = width;
1935 it->ascent = it->phys_ascent = height * ascent;
1936 it->descent = it->phys_descent = height - it->ascent;
1937 it->nglyphs = 1;
1939 if (face->box != FACE_NO_BOX)
1941 if (face->box_line_width > 0)
1943 it->ascent += face->box_line_width;
1944 it->descent += face->box_line_width;
1947 if (it->start_of_box_run_p)
1948 it->pixel_width += abs (face->box_line_width);
1949 if (it->end_of_box_run_p)
1950 it->pixel_width += abs (face->box_line_width);
1953 take_vertical_position_into_account (it);
1956 /* Return proper value to be used as baseline offset of font that has
1957 ASCENT and DESCENT to draw characters by the font at the vertical
1958 center of the line of frame F.
1960 Here, out task is to find the value of BOFF in the following figure;
1962 -------------------------+-----------+-
1963 -+-+---------+-+ | |
1964 | | | | | |
1965 | | | | F_ASCENT F_HEIGHT
1966 | | | ASCENT | |
1967 HEIGHT | | | | |
1968 | | |-|-+------+-----------|------- baseline
1969 | | | | BOFF | |
1970 | |---------|-+-+ | |
1971 | | | DESCENT | |
1972 -+-+---------+-+ F_DESCENT |
1973 -------------------------+-----------+-
1975 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1976 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1977 DESCENT = FONT->descent
1978 HEIGHT = FONT_HEIGHT (FONT)
1979 F_DESCENT = (F->output_data.x->font->descent
1980 - F->output_data.x->baseline_offset)
1981 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1984 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1985 (FONT_DESCENT (FONT) \
1986 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1987 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1988 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
1990 /* Produce glyphs/get display metrics for the display element IT is
1991 loaded with. See the description of struct display_iterator in
1992 dispextern.h for an overview of struct display_iterator. */
1994 static void
1995 x_produce_glyphs (it)
1996 struct it *it;
1998 it->glyph_not_available_p = 0;
2000 if (it->what == IT_CHARACTER)
2002 wchar_t char2b;
2003 XFontStruct *font;
2004 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2005 XCharStruct *pcm;
2006 int font_not_found_p;
2007 struct font_info *font_info;
2008 int boff; /* baseline offset */
2009 /* We may change it->multibyte_p upon unibyte<->multibyte
2010 conversion. So, save the current value now and restore it
2011 later.
2013 Note: It seems that we don't have to record multibyte_p in
2014 struct glyph because the character code itself tells if or
2015 not the character is multibyte. Thus, in the future, we must
2016 consider eliminating the field `multibyte_p' in the struct
2017 glyph.
2019 int saved_multibyte_p = it->multibyte_p;
2021 /* Maybe translate single-byte characters to multibyte, or the
2022 other way. */
2023 it->char_to_display = it->c;
2024 if (!ASCII_BYTE_P (it->c))
2026 if (unibyte_display_via_language_environment
2027 && SINGLE_BYTE_CHAR_P (it->c)
2028 && (it->c >= 0240
2029 || !NILP (Vnonascii_translation_table)))
2031 it->char_to_display = unibyte_char_to_multibyte (it->c);
2032 it->multibyte_p = 1;
2033 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2034 face = FACE_FROM_ID (it->f, it->face_id);
2036 else if (!SINGLE_BYTE_CHAR_P (it->c)
2037 && !it->multibyte_p)
2039 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
2040 it->multibyte_p = 0;
2041 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2042 face = FACE_FROM_ID (it->f, it->face_id);
2046 /* Get font to use. Encode IT->char_to_display. */
2047 x_get_char_face_and_encoding (it->f, it->char_to_display,
2048 it->face_id, &char2b,
2049 it->multibyte_p);
2050 font = face->font;
2052 /* When no suitable font found, use the default font. */
2053 font_not_found_p = font == NULL;
2054 if (font_not_found_p)
2056 font = FRAME_FONT (it->f);
2057 boff = it->f->output_data.w32->baseline_offset;
2058 font_info = NULL;
2060 else
2062 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2063 boff = font_info->baseline_offset;
2064 if (font_info->vertical_centering)
2065 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2068 if (it->char_to_display >= ' '
2069 && (!it->multibyte_p || it->char_to_display < 128))
2071 /* Either unibyte or ASCII. */
2072 int stretched_p;
2074 it->nglyphs = 1;
2076 pcm = w32_per_char_metric (font, &char2b,
2077 font->bdf ? BDF_1D_FONT : ANSI_FONT);
2078 it->ascent = FONT_BASE (font) + boff;
2079 it->descent = FONT_DESCENT (font) - boff;
2081 if (pcm)
2083 it->phys_ascent = pcm->ascent + boff;
2084 it->phys_descent = pcm->descent - boff;
2085 it->pixel_width = pcm->width;
2087 else
2089 it->glyph_not_available_p = 1;
2090 it->phys_ascent = FONT_BASE (font) + boff;
2091 it->phys_descent = FONT_DESCENT (font) - boff;
2092 it->pixel_width = FONT_WIDTH (font);
2095 /* If this is a space inside a region of text with
2096 `space-width' property, change its width. */
2097 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
2098 if (stretched_p)
2099 it->pixel_width *= XFLOATINT (it->space_width);
2101 /* If face has a box, add the box thickness to the character
2102 height. If character has a box line to the left and/or
2103 right, add the box line width to the character's width. */
2104 if (face->box != FACE_NO_BOX)
2106 int thick = face->box_line_width;
2108 if (thick > 0)
2110 it->ascent += thick;
2111 it->descent += thick;
2113 else
2114 thick = -thick;
2116 if (it->start_of_box_run_p)
2117 it->pixel_width += thick;
2118 if (it->end_of_box_run_p)
2119 it->pixel_width += thick;
2122 /* If face has an overline, add the height of the overline
2123 (1 pixel) and a 1 pixel margin to the character height. */
2124 if (face->overline_p)
2125 it->ascent += 2;
2127 take_vertical_position_into_account (it);
2129 /* If we have to actually produce glyphs, do it. */
2130 if (it->glyph_row)
2132 if (stretched_p)
2134 /* Translate a space with a `space-width' property
2135 into a stretch glyph. */
2136 double ascent = (double) FONT_BASE (font)
2137 / FONT_HEIGHT (font);
2138 x_append_stretch_glyph (it, it->object, it->pixel_width,
2139 it->ascent + it->descent, ascent);
2141 else
2142 x_append_glyph (it);
2144 /* If characters with lbearing or rbearing are displayed
2145 in this line, record that fact in a flag of the
2146 glyph row. This is used to optimize X output code. */
2147 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
2148 it->glyph_row->contains_overlapping_glyphs_p = 1;
2151 else if (it->char_to_display == '\n')
2153 /* A newline has no width but we need the height of the line. */
2154 it->pixel_width = 0;
2155 it->nglyphs = 0;
2156 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2157 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2159 if (face->box != FACE_NO_BOX
2160 && face->box_line_width > 0)
2162 it->ascent += face->box_line_width;
2163 it->descent += face->box_line_width;
2166 else if (it->char_to_display == '\t')
2168 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
2169 int x = it->current_x + it->continuation_lines_width;
2170 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2172 /* If the distance from the current position to the next tab
2173 stop is less than a canonical character width, use the
2174 tab stop after that. */
2175 if (next_tab_x - x < CANON_X_UNIT (it->f))
2176 next_tab_x += tab_width;
2178 it->pixel_width = next_tab_x - x;
2179 it->nglyphs = 1;
2180 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2181 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2183 if (it->glyph_row)
2185 double ascent = (double) it->ascent / (it->ascent + it->descent);
2186 x_append_stretch_glyph (it, it->object, it->pixel_width,
2187 it->ascent + it->descent, ascent);
2190 else
2192 /* A multi-byte character.
2193 If we found a font, this font should give us the right
2194 metrics. If we didn't find a font, use the frame's
2195 default font and calculate the width of the character
2196 from the charset width; this is what old redisplay code
2197 did. */
2198 enum w32_char_font_type type;
2200 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2201 type = BDF_1D_FONT;
2202 else if (font->bdf)
2203 type = BDF_2D_FONT;
2204 else
2205 type = UNICODE_FONT;
2207 pcm = w32_per_char_metric (font, &char2b, type);
2209 if (font_not_found_p || !pcm)
2211 int charset = CHAR_CHARSET (it->char_to_display);
2213 it->glyph_not_available_p = 1;
2214 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2215 * CHARSET_WIDTH (charset));
2216 it->phys_ascent = FONT_BASE (font) + boff;
2217 it->phys_descent = FONT_DESCENT (font) - boff;
2219 else
2221 it->pixel_width = pcm->width;
2222 it->phys_ascent = pcm->ascent + boff;
2223 it->phys_descent = pcm->descent - boff;
2224 if (it->glyph_row
2225 && (pcm->lbearing < 0
2226 || pcm->rbearing > pcm->width))
2227 it->glyph_row->contains_overlapping_glyphs_p = 1;
2229 it->nglyphs = 1;
2230 it->ascent = FONT_BASE (font) + boff;
2231 it->descent = FONT_DESCENT (font) - boff;
2232 if (face->box != FACE_NO_BOX)
2234 int thick = face->box_line_width;
2236 if (thick > 0)
2238 it->ascent += thick;
2239 it->descent += thick;
2241 else
2242 thick = - thick;
2244 if (it->start_of_box_run_p)
2245 it->pixel_width += thick;
2246 if (it->end_of_box_run_p)
2247 it->pixel_width += thick;
2250 /* If face has an overline, add the height of the overline
2251 (1 pixel) and a 1 pixel margin to the character height. */
2252 if (face->overline_p)
2253 it->ascent += 2;
2255 take_vertical_position_into_account (it);
2257 if (it->glyph_row)
2258 x_append_glyph (it);
2260 it->multibyte_p = saved_multibyte_p;
2262 else if (it->what == IT_COMPOSITION)
2264 /* Note: A composition is represented as one glyph in the
2265 glyph matrix. There are no padding glyphs. */
2266 wchar_t char2b;
2267 XFontStruct *font;
2268 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2269 XCharStruct *pcm;
2270 int font_not_found_p;
2271 struct font_info *font_info;
2272 int boff; /* baseline offset */
2273 struct composition *cmp = composition_table[it->cmp_id];
2275 /* Maybe translate single-byte characters to multibyte. */
2276 it->char_to_display = it->c;
2277 if (unibyte_display_via_language_environment
2278 && SINGLE_BYTE_CHAR_P (it->c)
2279 && (it->c >= 0240
2280 || (it->c >= 0200
2281 && !NILP (Vnonascii_translation_table))))
2283 it->char_to_display = unibyte_char_to_multibyte (it->c);
2286 /* Get face and font to use. Encode IT->char_to_display. */
2287 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2288 face = FACE_FROM_ID (it->f, it->face_id);
2289 x_get_char_face_and_encoding (it->f, it->char_to_display,
2290 it->face_id, &char2b, it->multibyte_p);
2291 font = face->font;
2293 /* When no suitable font found, use the default font. */
2294 font_not_found_p = font == NULL;
2295 if (font_not_found_p)
2297 font = FRAME_FONT (it->f);
2298 boff = it->f->output_data.w32->baseline_offset;
2299 font_info = NULL;
2301 else
2303 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2304 boff = font_info->baseline_offset;
2305 if (font_info->vertical_centering)
2306 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2309 /* There are no padding glyphs, so there is only one glyph to
2310 produce for the composition. Important is that pixel_width,
2311 ascent and descent are the values of what is drawn by
2312 draw_glyphs (i.e. the values of the overall glyphs composed). */
2313 it->nglyphs = 1;
2315 /* If we have not yet calculated pixel size data of glyphs of
2316 the composition for the current face font, calculate them
2317 now. Theoretically, we have to check all fonts for the
2318 glyphs, but that requires much time and memory space. So,
2319 here we check only the font of the first glyph. This leads
2320 to incorrect display very rarely, and C-l (recenter) can
2321 correct the display anyway. */
2322 if (cmp->font != (void *) font)
2324 /* Ascent and descent of the font of the first character of
2325 this composition (adjusted by baseline offset). Ascent
2326 and descent of overall glyphs should not be less than
2327 them respectively. */
2328 int font_ascent = FONT_BASE (font) + boff;
2329 int font_descent = FONT_DESCENT (font) - boff;
2330 /* Bounding box of the overall glyphs. */
2331 int leftmost, rightmost, lowest, highest;
2332 int i, width, ascent, descent;
2333 enum w32_char_font_type font_type;
2335 cmp->font = (void *) font;
2337 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2338 font_type = BDF_1D_FONT;
2339 else if (font->bdf)
2340 font_type = BDF_2D_FONT;
2341 else
2342 font_type = UNICODE_FONT;
2344 /* Initialize the bounding box. */
2345 if (font_info
2346 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
2348 width = pcm->width;
2349 ascent = pcm->ascent;
2350 descent = pcm->descent;
2352 else
2354 width = FONT_WIDTH (font);
2355 ascent = FONT_BASE (font);
2356 descent = FONT_DESCENT (font);
2359 rightmost = width;
2360 lowest = - descent + boff;
2361 highest = ascent + boff;
2362 leftmost = 0;
2364 if (font_info
2365 && font_info->default_ascent
2366 && CHAR_TABLE_P (Vuse_default_ascent)
2367 && !NILP (Faref (Vuse_default_ascent,
2368 make_number (it->char_to_display))))
2369 highest = font_info->default_ascent + boff;
2371 /* Draw the first glyph at the normal position. It may be
2372 shifted to right later if some other glyphs are drawn at
2373 the left. */
2374 cmp->offsets[0] = 0;
2375 cmp->offsets[1] = boff;
2377 /* Set cmp->offsets for the remaining glyphs. */
2378 for (i = 1; i < cmp->glyph_len; i++)
2380 int left, right, btm, top;
2381 int ch = COMPOSITION_GLYPH (cmp, i);
2382 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2384 face = FACE_FROM_ID (it->f, face_id);
2385 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2386 it->multibyte_p);
2387 font = face->font;
2388 if (font == NULL)
2390 font = FRAME_FONT (it->f);
2391 boff = it->f->output_data.w32->baseline_offset;
2392 font_info = NULL;
2394 else
2396 font_info
2397 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2398 boff = font_info->baseline_offset;
2399 if (font_info->vertical_centering)
2400 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2403 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (ch)) == 1)
2404 font_type = BDF_1D_FONT;
2405 else if (font->bdf)
2406 font_type = BDF_2D_FONT;
2407 else
2408 font_type = UNICODE_FONT;
2410 if (font_info
2411 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
2413 width = pcm->width;
2414 ascent = pcm->ascent;
2415 descent = pcm->descent;
2417 else
2419 width = FONT_WIDTH (font);
2420 ascent = 1;
2421 descent = 0;
2424 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2426 /* Relative composition with or without
2427 alternate chars. */
2428 left = (leftmost + rightmost - width) / 2;
2429 btm = - descent + boff;
2430 if (font_info && font_info->relative_compose
2431 && (! CHAR_TABLE_P (Vignore_relative_composition)
2432 || NILP (Faref (Vignore_relative_composition,
2433 make_number (ch)))))
2436 if (- descent >= font_info->relative_compose)
2437 /* One extra pixel between two glyphs. */
2438 btm = highest + 1;
2439 else if (ascent <= 0)
2440 /* One extra pixel between two glyphs. */
2441 btm = lowest - 1 - ascent - descent;
2444 else
2446 /* A composition rule is specified by an integer
2447 value that encodes global and new reference
2448 points (GREF and NREF). GREF and NREF are
2449 specified by numbers as below:
2451 0---1---2 -- ascent
2455 9--10--11 -- center
2457 ---3---4---5--- baseline
2459 6---7---8 -- descent
2461 int rule = COMPOSITION_RULE (cmp, i);
2462 int gref, nref, grefx, grefy, nrefx, nrefy;
2464 COMPOSITION_DECODE_RULE (rule, gref, nref);
2465 grefx = gref % 3, nrefx = nref % 3;
2466 grefy = gref / 3, nrefy = nref / 3;
2468 left = (leftmost
2469 + grefx * (rightmost - leftmost) / 2
2470 - nrefx * width / 2);
2471 btm = ((grefy == 0 ? highest
2472 : grefy == 1 ? 0
2473 : grefy == 2 ? lowest
2474 : (highest + lowest) / 2)
2475 - (nrefy == 0 ? ascent + descent
2476 : nrefy == 1 ? descent - boff
2477 : nrefy == 2 ? 0
2478 : (ascent + descent) / 2));
2481 cmp->offsets[i * 2] = left;
2482 cmp->offsets[i * 2 + 1] = btm + descent;
2484 /* Update the bounding box of the overall glyphs. */
2485 right = left + width;
2486 top = btm + descent + ascent;
2487 if (left < leftmost)
2488 leftmost = left;
2489 if (right > rightmost)
2490 rightmost = right;
2491 if (top > highest)
2492 highest = top;
2493 if (btm < lowest)
2494 lowest = btm;
2497 /* If there are glyphs whose x-offsets are negative,
2498 shift all glyphs to the right and make all x-offsets
2499 non-negative. */
2500 if (leftmost < 0)
2502 for (i = 0; i < cmp->glyph_len; i++)
2503 cmp->offsets[i * 2] -= leftmost;
2504 rightmost -= leftmost;
2507 cmp->pixel_width = rightmost;
2508 cmp->ascent = highest;
2509 cmp->descent = - lowest;
2510 if (cmp->ascent < font_ascent)
2511 cmp->ascent = font_ascent;
2512 if (cmp->descent < font_descent)
2513 cmp->descent = font_descent;
2516 it->pixel_width = cmp->pixel_width;
2517 it->ascent = it->phys_ascent = cmp->ascent;
2518 it->descent = it->phys_descent = cmp->descent;
2520 if (face->box != FACE_NO_BOX)
2522 int thick = face->box_line_width;
2524 if (thick > 0)
2526 it->ascent += thick;
2527 it->descent += thick;
2529 else
2530 thick = - thick;
2532 if (it->start_of_box_run_p)
2533 it->pixel_width += thick;
2534 if (it->end_of_box_run_p)
2535 it->pixel_width += thick;
2538 /* If face has an overline, add the height of the overline
2539 (1 pixel) and a 1 pixel margin to the character height. */
2540 if (face->overline_p)
2541 it->ascent += 2;
2543 take_vertical_position_into_account (it);
2545 if (it->glyph_row)
2546 x_append_composite_glyph (it);
2548 else if (it->what == IT_IMAGE)
2549 x_produce_image_glyph (it);
2550 else if (it->what == IT_STRETCH)
2551 x_produce_stretch_glyph (it);
2553 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2554 because this isn't true for images with `:ascent 100'. */
2555 xassert (it->ascent >= 0 && it->descent >= 0);
2556 if (it->area == TEXT_AREA)
2557 it->current_x += it->pixel_width;
2559 it->descent += it->extra_line_spacing;
2561 it->max_ascent = max (it->max_ascent, it->ascent);
2562 it->max_descent = max (it->max_descent, it->descent);
2563 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2564 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2568 /* Estimate the pixel height of the mode or top line on frame F.
2569 FACE_ID specifies what line's height to estimate. */
2572 x_estimate_mode_line_height (f, face_id)
2573 struct frame *f;
2574 enum face_id face_id;
2576 int height = FONT_HEIGHT (FRAME_FONT (f));
2578 /* This function is called so early when Emacs starts that the face
2579 cache and mode line face are not yet initialized. */
2580 if (FRAME_FACE_CACHE (f))
2582 struct face *face = FACE_FROM_ID (f, face_id);
2583 if (face)
2585 if (face->font)
2586 height = FONT_HEIGHT (face->font);
2587 if (face->box_line_width > 0)
2588 height += 2 * face->box_line_width;
2592 return height;
2596 /***********************************************************************
2597 Glyph display
2598 ***********************************************************************/
2600 /* A sequence of glyphs to be drawn in the same face.
2602 This data structure is not really completely X specific, so it
2603 could possibly, at least partially, be useful for other systems. It
2604 is currently not part of the external redisplay interface because
2605 it's not clear what other systems will need. */
2607 struct glyph_string
2609 /* X-origin of the string. */
2610 int x;
2612 /* Y-origin and y-position of the base line of this string. */
2613 int y, ybase;
2615 /* The width of the string, not including a face extension. */
2616 int width;
2618 /* The width of the string, including a face extension. */
2619 int background_width;
2621 /* The height of this string. This is the height of the line this
2622 string is drawn in, and can be different from the height of the
2623 font the string is drawn in. */
2624 int height;
2626 /* Number of pixels this string overwrites in front of its x-origin.
2627 This number is zero if the string has an lbearing >= 0; it is
2628 -lbearing, if the string has an lbearing < 0. */
2629 int left_overhang;
2631 /* Number of pixels this string overwrites past its right-most
2632 nominal x-position, i.e. x + width. Zero if the string's
2633 rbearing is <= its nominal width, rbearing - width otherwise. */
2634 int right_overhang;
2636 /* The frame on which the glyph string is drawn. */
2637 struct frame *f;
2639 /* The window on which the glyph string is drawn. */
2640 struct window *w;
2642 /* X display and window for convenience. */
2643 Window window;
2645 /* The glyph row for which this string was built. It determines the
2646 y-origin and height of the string. */
2647 struct glyph_row *row;
2649 /* The area within row. */
2650 enum glyph_row_area area;
2652 /* Characters to be drawn, and number of characters. */
2653 wchar_t *char2b;
2654 int nchars;
2656 /* A face-override for drawing cursors, mouse face and similar. */
2657 enum draw_glyphs_face hl;
2659 /* Face in which this string is to be drawn. */
2660 struct face *face;
2662 /* Font in which this string is to be drawn. */
2663 XFontStruct *font;
2665 /* Font info for this string. */
2666 struct font_info *font_info;
2668 /* Non-null means this string describes (part of) a composition.
2669 All characters from char2b are drawn composed. */
2670 struct composition *cmp;
2672 /* Index of this glyph string's first character in the glyph
2673 definition of CMP. If this is zero, this glyph string describes
2674 the first character of a composition. */
2675 int gidx;
2677 /* 1 means this glyph strings face has to be drawn to the right end
2678 of the window's drawing area. */
2679 unsigned extends_to_end_of_line_p : 1;
2681 /* 1 means the background of this string has been drawn. */
2682 unsigned background_filled_p : 1;
2684 /* 1 means glyph string must be drawn with 16-bit functions. */
2685 unsigned two_byte_p : 1;
2687 /* 1 means that the original font determined for drawing this glyph
2688 string could not be loaded. The member `font' has been set to
2689 the frame's default font in this case. */
2690 unsigned font_not_found_p : 1;
2692 /* 1 means that the face in which this glyph string is drawn has a
2693 stipple pattern. */
2694 unsigned stippled_p : 1;
2696 /* 1 means only the foreground of this glyph string must be drawn,
2697 and we should use the physical height of the line this glyph
2698 string appears in as clip rect. */
2699 unsigned for_overlaps_p : 1;
2701 /* The GC to use for drawing this glyph string. */
2702 XGCValues *gc;
2704 HDC hdc;
2706 /* A pointer to the first glyph in the string. This glyph
2707 corresponds to char2b[0]. Needed to draw rectangles if
2708 font_not_found_p is 1. */
2709 struct glyph *first_glyph;
2711 /* Image, if any. */
2712 struct image *img;
2714 struct glyph_string *next, *prev;
2718 /* Encapsulate the different ways of displaying text under W32. */
2720 void W32_TEXTOUT (s, x, y,chars,nchars)
2721 struct glyph_string * s;
2722 int x, y;
2723 wchar_t * chars;
2724 int nchars;
2726 int charset_dim = w32_font_is_double_byte (s->gc->font) ? 2 : 1;
2727 if (s->gc->font->bdf)
2728 w32_BDF_TextOut (s->gc->font->bdf, s->hdc,
2729 x, y, (char *) chars, charset_dim,
2730 nchars * charset_dim, 0);
2731 else if (s->first_glyph->w32_font_type == UNICODE_FONT)
2732 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
2733 else
2734 ExtTextOut (s->hdc, x, y, 0, NULL, (char *) chars,
2735 nchars * charset_dim, NULL);
2738 #if 0
2740 static void
2741 x_dump_glyph_string (s)
2742 struct glyph_string *s;
2744 fprintf (stderr, "glyph string\n");
2745 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2746 s->x, s->y, s->width, s->height);
2747 fprintf (stderr, " ybase = %d\n", s->ybase);
2748 fprintf (stderr, " hl = %d\n", s->hl);
2749 fprintf (stderr, " left overhang = %d, right = %d\n",
2750 s->left_overhang, s->right_overhang);
2751 fprintf (stderr, " nchars = %d\n", s->nchars);
2752 fprintf (stderr, " extends to end of line = %d\n",
2753 s->extends_to_end_of_line_p);
2754 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2755 fprintf (stderr, " bg width = %d\n", s->background_width);
2758 #endif /* GLYPH_DEBUG */
2762 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2763 struct glyph_string **,
2764 struct glyph_string *,
2765 struct glyph_string *));
2766 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2767 struct glyph_string **,
2768 struct glyph_string *,
2769 struct glyph_string *));
2770 static void x_append_glyph_string P_ ((struct glyph_string **,
2771 struct glyph_string **,
2772 struct glyph_string *));
2773 static int x_left_overwritten P_ ((struct glyph_string *));
2774 static int x_left_overwriting P_ ((struct glyph_string *));
2775 static int x_right_overwritten P_ ((struct glyph_string *));
2776 static int x_right_overwriting P_ ((struct glyph_string *));
2777 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2778 int));
2779 static void w32_init_glyph_string P_ ((struct glyph_string *, HDC hdc,
2780 wchar_t *, struct window *,
2781 struct glyph_row *,
2782 enum glyph_row_area, int,
2783 enum draw_glyphs_face));
2784 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2785 enum glyph_row_area, int, int,
2786 enum draw_glyphs_face, int *, int *, int));
2787 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2788 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2789 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2790 int));
2791 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2792 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2793 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2794 static void x_draw_glyph_string P_ ((struct glyph_string *));
2795 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2796 static void x_set_cursor_gc P_ ((struct glyph_string *));
2797 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2798 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2799 static void w32_get_glyph_overhangs P_ ((HDC hdc, struct glyph *,
2800 struct frame *,
2801 int *, int *));
2802 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2803 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
2804 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
2805 double, int, COLORREF));
2806 static void x_setup_relief_colors P_ ((struct glyph_string *));
2807 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2808 static void x_draw_image_relief P_ ((struct glyph_string *));
2809 static void x_draw_image_foreground P_ ((struct glyph_string *));
2810 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
2811 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2812 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2813 int, int, int));
2814 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2815 int, int, int, int, RECT *));
2816 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2817 int, int, int, RECT *));
2818 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2819 enum glyph_row_area));
2820 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2821 struct glyph_row *,
2822 enum glyph_row_area, int, int));
2824 #if GLYPH_DEBUG
2825 static void x_check_font P_ ((struct frame *, XFontStruct *));
2826 #endif
2829 /* Append the list of glyph strings with head H and tail T to the list
2830 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2832 static INLINE void
2833 x_append_glyph_string_lists (head, tail, h, t)
2834 struct glyph_string **head, **tail;
2835 struct glyph_string *h, *t;
2837 if (h)
2839 if (*head)
2840 (*tail)->next = h;
2841 else
2842 *head = h;
2843 h->prev = *tail;
2844 *tail = t;
2849 /* Prepend the list of glyph strings with head H and tail T to the
2850 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2851 result. */
2853 static INLINE void
2854 x_prepend_glyph_string_lists (head, tail, h, t)
2855 struct glyph_string **head, **tail;
2856 struct glyph_string *h, *t;
2858 if (h)
2860 if (*head)
2861 (*head)->prev = t;
2862 else
2863 *tail = t;
2864 t->next = *head;
2865 *head = h;
2870 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2871 Set *HEAD and *TAIL to the resulting list. */
2873 static INLINE void
2874 x_append_glyph_string (head, tail, s)
2875 struct glyph_string **head, **tail;
2876 struct glyph_string *s;
2878 s->next = s->prev = NULL;
2879 x_append_glyph_string_lists (head, tail, s, s);
2883 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2884 face. */
2886 static void
2887 x_set_cursor_gc (s)
2888 struct glyph_string *s;
2890 if (s->font == FRAME_FONT (s->f)
2891 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2892 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2893 && !s->cmp)
2894 s->gc = s->f->output_data.w32->cursor_gc;
2895 else
2897 /* Cursor on non-default face: must merge. */
2898 XGCValues xgcv;
2899 unsigned long mask;
2901 xgcv.background = s->f->output_data.w32->cursor_pixel;
2902 xgcv.foreground = s->face->background;
2904 /* If the glyph would be invisible, try a different foreground. */
2905 if (xgcv.foreground == xgcv.background)
2906 xgcv.foreground = s->face->foreground;
2907 if (xgcv.foreground == xgcv.background)
2908 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
2909 if (xgcv.foreground == xgcv.background)
2910 xgcv.foreground = s->face->foreground;
2912 /* Make sure the cursor is distinct from text in this face. */
2913 if (xgcv.background == s->face->background
2914 && xgcv.foreground == s->face->foreground)
2916 xgcv.background = s->face->foreground;
2917 xgcv.foreground = s->face->background;
2920 IF_DEBUG (x_check_font (s->f, s->font));
2921 xgcv.font = s->font;
2922 mask = GCForeground | GCBackground | GCFont;
2924 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2925 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2926 mask, &xgcv);
2927 else
2928 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2929 = XCreateGC (NULL, s->window, mask, &xgcv);
2931 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2936 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2938 static void
2939 x_set_mouse_face_gc (s)
2940 struct glyph_string *s;
2942 int face_id;
2943 struct face *face;
2945 /* What face has to be used last for the mouse face? */
2946 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
2947 face = FACE_FROM_ID (s->f, face_id);
2948 if (face == NULL)
2949 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2951 if (s->first_glyph->type == CHAR_GLYPH)
2952 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2953 else
2954 face_id = FACE_FOR_CHAR (s->f, face, 0);
2955 s->face = FACE_FROM_ID (s->f, face_id);
2956 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2958 /* If font in this face is same as S->font, use it. */
2959 if (s->font == s->face->font)
2960 s->gc = s->face->gc;
2961 else
2963 /* Otherwise construct scratch_cursor_gc with values from FACE
2964 but font FONT. */
2965 XGCValues xgcv;
2966 unsigned long mask;
2968 xgcv.background = s->face->background;
2969 xgcv.foreground = s->face->foreground;
2970 IF_DEBUG (x_check_font (s->f, s->font));
2971 xgcv.font = s->font;
2972 mask = GCForeground | GCBackground | GCFont;
2974 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2975 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2976 mask, &xgcv);
2977 else
2978 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2979 = XCreateGC (NULL, s->window, mask, &xgcv);
2981 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2984 xassert (s->gc != 0);
2988 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2989 Faces to use in the mode line have already been computed when the
2990 matrix was built, so there isn't much to do, here. */
2992 static INLINE void
2993 x_set_mode_line_face_gc (s)
2994 struct glyph_string *s;
2996 s->gc = s->face->gc;
3000 /* Set S->gc of glyph string S for drawing that glyph string. Set
3001 S->stippled_p to a non-zero value if the face of S has a stipple
3002 pattern. */
3004 static INLINE void
3005 x_set_glyph_string_gc (s)
3006 struct glyph_string *s;
3008 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
3010 if (s->hl == DRAW_NORMAL_TEXT)
3012 s->gc = s->face->gc;
3013 s->stippled_p = s->face->stipple != 0;
3015 else if (s->hl == DRAW_INVERSE_VIDEO)
3017 x_set_mode_line_face_gc (s);
3018 s->stippled_p = s->face->stipple != 0;
3020 else if (s->hl == DRAW_CURSOR)
3022 x_set_cursor_gc (s);
3023 s->stippled_p = 0;
3025 else if (s->hl == DRAW_MOUSE_FACE)
3027 x_set_mouse_face_gc (s);
3028 s->stippled_p = s->face->stipple != 0;
3030 else if (s->hl == DRAW_IMAGE_RAISED
3031 || s->hl == DRAW_IMAGE_SUNKEN)
3033 s->gc = s->face->gc;
3034 s->stippled_p = s->face->stipple != 0;
3036 else
3038 s->gc = s->face->gc;
3039 s->stippled_p = s->face->stipple != 0;
3042 /* GC must have been set. */
3043 xassert (s->gc != 0);
3047 /* Return in *R the clipping rectangle for glyph string S. */
3049 static void
3050 w32_get_glyph_string_clip_rect (s, r)
3051 struct glyph_string *s;
3052 RECT *r;
3054 int r_height, r_width;
3056 if (s->row->full_width_p)
3058 /* Draw full-width. X coordinates are relative to S->w->left. */
3059 int canon_x = CANON_X_UNIT (s->f);
3061 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
3062 r_width = XFASTINT (s->w->width) * canon_x;
3064 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
3066 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
3067 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
3068 r->left -= width;
3071 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
3073 /* Unless displaying a mode or menu bar line, which are always
3074 fully visible, clip to the visible part of the row. */
3075 if (s->w->pseudo_window_p)
3076 r_height = s->row->visible_height;
3077 else
3078 r_height = s->height;
3080 else
3082 /* This is a text line that may be partially visible. */
3083 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
3084 r_width = window_box_width (s->w, s->area);
3085 r_height = s->row->visible_height;
3088 /* Don't use S->y for clipping because it doesn't take partially
3089 visible lines into account. For example, it can be negative for
3090 partially visible lines at the top of a window. */
3091 if (!s->row->full_width_p
3092 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
3093 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3094 else
3095 r->top = max (0, s->row->y);
3097 /* If drawing a tool-bar window, draw it over the internal border
3098 at the top of the window. */
3099 if (s->w == XWINDOW (s->f->tool_bar_window))
3100 r->top -= s->f->output_data.w32->internal_border_width;
3102 /* If S draws overlapping rows, it's sufficient to use the top and
3103 bottom of the window for clipping because this glyph string
3104 intentionally draws over other lines. */
3105 if (s->for_overlaps_p)
3107 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3108 r_height = window_text_bottom_y (s->w) - r->top;
3111 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
3113 r->bottom = r->top + r_height;
3114 r->right = r->left + r_width;
3118 /* Set clipping for output of glyph string S. S may be part of a mode
3119 line or menu if we don't have X toolkit support. */
3121 static INLINE void
3122 x_set_glyph_string_clipping (s)
3123 struct glyph_string *s;
3125 RECT r;
3126 w32_get_glyph_string_clip_rect (s, &r);
3127 w32_set_clip_rectangle (s->hdc, &r);
3131 /* Compute left and right overhang of glyph string S. If S is a glyph
3132 string for a composition, assume overhangs don't exist. */
3134 static INLINE void
3135 x_compute_glyph_string_overhangs (s)
3136 struct glyph_string *s;
3138 /* TODO: Windows does not appear to have a method for
3139 getting this info without getting the ABC widths for each
3140 individual character and working it out manually. */
3144 /* Compute overhangs and x-positions for glyph string S and its
3145 predecessors, or successors. X is the starting x-position for S.
3146 BACKWARD_P non-zero means process predecessors. */
3148 static void
3149 x_compute_overhangs_and_x (s, x, backward_p)
3150 struct glyph_string *s;
3151 int x;
3152 int backward_p;
3154 if (backward_p)
3156 while (s)
3158 x_compute_glyph_string_overhangs (s);
3159 x -= s->width;
3160 s->x = x;
3161 s = s->prev;
3164 else
3166 while (s)
3168 x_compute_glyph_string_overhangs (s);
3169 s->x = x;
3170 x += s->width;
3171 s = s->next;
3177 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
3178 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
3179 assumed to be zero. */
3181 static void
3182 w32_get_glyph_overhangs (hdc, glyph, f, left, right)
3183 HDC hdc;
3184 struct glyph *glyph;
3185 struct frame *f;
3186 int *left, *right;
3188 *left = *right = 0;
3190 if (glyph->type == CHAR_GLYPH)
3192 XFontStruct *font;
3193 struct face *face;
3194 wchar_t char2b;
3195 XCharStruct *pcm;
3197 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
3198 font = face->font;
3200 if (font
3201 && (pcm = w32_per_char_metric (font, &char2b,
3202 glyph->w32_font_type)))
3204 if (pcm->rbearing > pcm->width)
3205 *right = pcm->rbearing - pcm->width;
3206 if (pcm->lbearing < 0)
3207 *left = -pcm->lbearing;
3213 static void
3214 x_get_glyph_overhangs (glyph, f, left, right)
3215 struct glyph *glyph;
3216 struct frame *f;
3217 int *left, *right;
3219 HDC hdc = get_frame_dc (f);
3220 /* Convert to unicode! */
3221 w32_get_glyph_overhangs (hdc, glyph, f, left, right);
3222 release_frame_dc (f, hdc);
3226 /* Return the index of the first glyph preceding glyph string S that
3227 is overwritten by S because of S's left overhang. Value is -1
3228 if no glyphs are overwritten. */
3230 static int
3231 x_left_overwritten (s)
3232 struct glyph_string *s;
3234 int k;
3236 if (s->left_overhang)
3238 int x = 0, i;
3239 struct glyph *glyphs = s->row->glyphs[s->area];
3240 int first = s->first_glyph - glyphs;
3242 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3243 x -= glyphs[i].pixel_width;
3245 k = i + 1;
3247 else
3248 k = -1;
3250 return k;
3254 /* Return the index of the first glyph preceding glyph string S that
3255 is overwriting S because of its right overhang. Value is -1 if no
3256 glyph in front of S overwrites S. */
3258 static int
3259 x_left_overwriting (s)
3260 struct glyph_string *s;
3262 int i, k, x;
3263 struct glyph *glyphs = s->row->glyphs[s->area];
3264 int first = s->first_glyph - glyphs;
3266 k = -1;
3267 x = 0;
3268 for (i = first - 1; i >= 0; --i)
3270 int left, right;
3271 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
3272 if (x + right > 0)
3273 k = i;
3274 x -= glyphs[i].pixel_width;
3277 return k;
3281 /* Return the index of the last glyph following glyph string S that is
3282 not overwritten by S because of S's right overhang. Value is -1 if
3283 no such glyph is found. */
3285 static int
3286 x_right_overwritten (s)
3287 struct glyph_string *s;
3289 int k = -1;
3291 if (s->right_overhang)
3293 int x = 0, i;
3294 struct glyph *glyphs = s->row->glyphs[s->area];
3295 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3296 int end = s->row->used[s->area];
3298 for (i = first; i < end && s->right_overhang > x; ++i)
3299 x += glyphs[i].pixel_width;
3301 k = i;
3304 return k;
3308 /* Return the index of the last glyph following glyph string S that
3309 overwrites S because of its left overhang. Value is negative
3310 if no such glyph is found. */
3312 static int
3313 x_right_overwriting (s)
3314 struct glyph_string *s;
3316 int i, k, x;
3317 int end = s->row->used[s->area];
3318 struct glyph *glyphs = s->row->glyphs[s->area];
3319 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3321 k = -1;
3322 x = 0;
3323 for (i = first; i < end; ++i)
3325 int left, right;
3326 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
3327 if (x - left < 0)
3328 k = i;
3329 x += glyphs[i].pixel_width;
3332 return k;
3336 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3338 static INLINE void
3339 x_clear_glyph_string_rect (s, x, y, w, h)
3340 struct glyph_string *s;
3341 int x, y, w, h;
3343 int real_x = x;
3344 int real_y = y;
3345 int real_w = w;
3346 int real_h = h;
3347 #if 0
3348 /* Take clipping into account. */
3349 if (s->gc->clip_mask == Rect)
3351 real_x = max (real_x, s->gc->clip_rectangle.left);
3352 real_y = max (real_y, s->gc->clip_rectangle.top);
3353 real_w = min (real_w, s->gc->clip_rectangle.right
3354 - s->gc->clip_rectangle.left);
3355 real_h = min (real_h, s->gc->clip_rectangle.bottom
3356 - s->gc->clip_rectangle.top);
3358 #endif
3359 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
3360 real_w, real_h);
3364 /* Draw the background of glyph_string S. If S->background_filled_p
3365 is non-zero don't draw it. FORCE_P non-zero means draw the
3366 background even if it wouldn't be drawn normally. This is used
3367 when a string preceding S draws into the background of S, or S
3368 contains the first component of a composition. */
3370 static void
3371 x_draw_glyph_string_background (s, force_p)
3372 struct glyph_string *s;
3373 int force_p;
3375 /* Nothing to do if background has already been drawn or if it
3376 shouldn't be drawn in the first place. */
3377 if (!s->background_filled_p)
3379 int box_line_width = max (s->face->box_line_width, 0);
3381 #if 0 /* TODO: stipple */
3382 if (s->stippled_p)
3384 /* Fill background with a stipple pattern. */
3385 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3386 XFillRectangle (s->display, s->window, s->gc, s->x,
3387 s->y + box_line_width,
3388 s->background_width,
3389 s->height - 2 * box_line_width);
3390 XSetFillStyle (s->display, s->gc, FillSolid);
3391 s->background_filled_p = 1;
3393 else
3394 #endif
3395 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3396 || s->font_not_found_p
3397 || s->extends_to_end_of_line_p
3398 || s->font->bdf
3399 || force_p)
3401 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3402 s->background_width,
3403 s->height - 2 * box_line_width);
3404 s->background_filled_p = 1;
3410 /* Draw the foreground of glyph string S. */
3412 static void
3413 x_draw_glyph_string_foreground (s)
3414 struct glyph_string *s;
3416 int i, x;
3417 HFONT old_font;
3419 /* If first glyph of S has a left box line, start drawing the text
3420 of S to the right of that box line. */
3421 if (s->face->box != FACE_NO_BOX
3422 && s->first_glyph->left_box_line_p)
3423 x = s->x + abs (s->face->box_line_width);
3424 else
3425 x = s->x;
3427 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
3428 SetBkMode (s->hdc, TRANSPARENT);
3429 else
3430 SetBkMode (s->hdc, OPAQUE);
3432 SetTextColor (s->hdc, s->gc->foreground);
3433 SetBkColor (s->hdc, s->gc->background);
3434 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3436 if (s->font && s->font->hfont)
3437 old_font = SelectObject (s->hdc, s->font->hfont);
3439 /* Draw characters of S as rectangles if S's font could not be
3440 loaded. */
3441 if (s->font_not_found_p)
3443 for (i = 0; i < s->nchars; ++i)
3445 struct glyph *g = s->first_glyph + i;
3447 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
3448 s->height - 1);
3449 x += g->pixel_width;
3452 else
3454 char *char1b = (char *) s->char2b;
3455 int boff = s->font_info->baseline_offset;
3457 if (s->font_info->vertical_centering)
3458 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3460 /* If we can use 8-bit functions, condense S->char2b. */
3461 if (!s->two_byte_p)
3462 for (i = 0; i < s->nchars; ++i)
3463 char1b[i] = BYTE2 (s->char2b[i]);
3465 /* Draw text with TextOut and friends. */
3466 W32_TEXTOUT (s, x, s->ybase - boff, s->char2b, s->nchars);
3468 if (s->font && s->font->hfont)
3469 SelectObject (s->hdc, old_font);
3472 /* Draw the foreground of composite glyph string S. */
3474 static void
3475 x_draw_composite_glyph_string_foreground (s)
3476 struct glyph_string *s;
3478 int i, x;
3479 HFONT old_font;
3481 /* If first glyph of S has a left box line, start drawing the text
3482 of S to the right of that box line. */
3483 if (s->face->box != FACE_NO_BOX
3484 && s->first_glyph->left_box_line_p)
3485 x = s->x + abs (s->face->box_line_width);
3486 else
3487 x = s->x;
3489 /* S is a glyph string for a composition. S->gidx is the index of
3490 the first character drawn for glyphs of this composition.
3491 S->gidx == 0 means we are drawing the very first character of
3492 this composition. */
3494 SetTextColor (s->hdc, s->gc->foreground);
3495 SetBkColor (s->hdc, s->gc->background);
3496 SetBkMode (s->hdc, TRANSPARENT);
3497 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3499 if (s->font && s->font->hfont)
3500 old_font = SelectObject (s->hdc, s->font->hfont);
3502 /* Draw a rectangle for the composition if the font for the very
3503 first character of the composition could not be loaded. */
3504 if (s->font_not_found_p)
3506 if (s->gidx == 0)
3507 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
3508 s->height - 1);
3510 else
3512 for (i = 0; i < s->nchars; i++, ++s->gidx)
3513 W32_TEXTOUT (s, x + s->cmp->offsets[s->gidx * 2],
3514 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3515 s->char2b + i, 1);
3517 if (s->font && s->font->hfont)
3518 SelectObject (s->hdc, old_font);
3522 /* Brightness beyond which a color won't have its highlight brightness
3523 boosted.
3525 Nominally, highlight colors for `3d' faces are calculated by
3526 brightening an object's color by a constant scale factor, but this
3527 doesn't yield good results for dark colors, so for colors who's
3528 brightness is less than this value (on a scale of 0-255) have to
3529 use an additional additive factor.
3531 The value here is set so that the default menu-bar/mode-line color
3532 (grey75) will not have its highlights changed at all. */
3533 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
3536 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
3537 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3538 If this produces the same color as COLOR, try a color where all RGB
3539 values have DELTA added. Return the allocated color in *COLOR.
3540 DISPLAY is the X display, CMAP is the colormap to operate on.
3541 Value is non-zero if successful. */
3543 static int
3544 w32_alloc_lighter_color (f, color, factor, delta)
3545 struct frame *f;
3546 COLORREF *color;
3547 double factor;
3548 int delta;
3550 COLORREF new;
3551 long bright;
3553 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
3554 delta /= 256;
3556 /* Change RGB values by specified FACTOR. Avoid overflow! */
3557 xassert (factor >= 0);
3558 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
3559 min (0xff, factor * GetGValue (*color)),
3560 min (0xff, factor * GetBValue (*color)));
3562 /* Calculate brightness of COLOR. */
3563 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
3564 + GetBValue (*color)) / 6;
3566 /* We only boost colors that are darker than
3567 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3568 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3569 /* Make an additive adjustment to NEW, because it's dark enough so
3570 that scaling by FACTOR alone isn't enough. */
3572 /* How far below the limit this color is (0 - 1, 1 being darker). */
3573 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3574 /* The additive adjustment. */
3575 int min_delta = delta * dimness * factor / 2;
3577 if (factor < 1)
3578 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
3579 max (0, min (0xff, min_delta - GetGValue (*color))),
3580 max (0, min (0xff, min_delta - GetBValue (*color))));
3581 else
3582 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
3583 max (0, min (0xff, min_delta + GetGValue (*color))),
3584 max (0, min (0xff, min_delta + GetBValue (*color))));
3587 if (new == *color)
3588 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
3589 max (0, min (0xff, delta + GetGValue (*color))),
3590 max (0, min (0xff, delta + GetBValue (*color))));
3592 /* TODO: Map to palette and retry with delta if same? */
3593 /* TODO: Free colors (if using palette)? */
3595 if (new == *color)
3596 return 0;
3598 *color = new;
3600 return 1;
3604 /* Set up the foreground color for drawing relief lines of glyph
3605 string S. RELIEF is a pointer to a struct relief containing the GC
3606 with which lines will be drawn. Use a color that is FACTOR or
3607 DELTA lighter or darker than the relief's background which is found
3608 in S->f->output_data.x->relief_background. If such a color cannot
3609 be allocated, use DEFAULT_PIXEL, instead. */
3611 static void
3612 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
3613 struct frame *f;
3614 struct relief *relief;
3615 double factor;
3616 int delta;
3617 COLORREF default_pixel;
3619 XGCValues xgcv;
3620 struct w32_output *di = f->output_data.w32;
3621 unsigned long mask = GCForeground;
3622 COLORREF pixel;
3623 COLORREF background = di->relief_background;
3624 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3626 /* TODO: Free colors (if using palette)? */
3628 /* Allocate new color. */
3629 xgcv.foreground = default_pixel;
3630 pixel = background;
3631 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
3633 relief->allocated_p = 1;
3634 xgcv.foreground = relief->pixel = pixel;
3637 if (relief->gc == 0)
3639 #if 0 /* TODO: stipple */
3640 xgcv.stipple = dpyinfo->gray;
3641 mask |= GCStipple;
3642 #endif
3643 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
3645 else
3646 XChangeGC (NULL, relief->gc, mask, &xgcv);
3650 /* Set up colors for the relief lines around glyph string S. */
3652 static void
3653 x_setup_relief_colors (s)
3654 struct glyph_string *s;
3656 struct w32_output *di = s->f->output_data.w32;
3657 COLORREF color;
3659 if (s->face->use_box_color_for_shadows_p)
3660 color = s->face->box_color;
3661 else
3662 color = s->gc->background;
3664 if (di->white_relief.gc == 0
3665 || color != di->relief_background)
3667 di->relief_background = color;
3668 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3669 WHITE_PIX_DEFAULT (s->f));
3670 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3671 BLACK_PIX_DEFAULT (s->f));
3676 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3677 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3678 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3679 relief. LEFT_P non-zero means draw a relief on the left side of
3680 the rectangle. RIGHT_P non-zero means draw a relief on the right
3681 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3682 when drawing. */
3684 static void
3685 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3686 raised_p, left_p, right_p, clip_rect)
3687 struct frame *f;
3688 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3689 RECT *clip_rect;
3691 int i;
3692 XGCValues gc;
3693 HDC hdc = get_frame_dc (f);
3695 if (raised_p)
3696 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3697 else
3698 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
3700 w32_set_clip_rectangle (hdc, clip_rect);
3702 /* Top. */
3703 for (i = 0; i < width; ++i)
3705 w32_fill_area (f, hdc, gc.foreground,
3706 left_x + i * left_p, top_y + i,
3707 (right_x + 1 - i * right_p) - (left_x + i * left_p), 1);
3710 /* Left. */
3711 if (left_p)
3712 for (i = 0; i < width; ++i)
3714 w32_fill_area (f, hdc, gc.foreground,
3715 left_x + i, top_y + i, 1,
3716 (bottom_y - i) - (top_y + i));
3719 w32_set_clip_rectangle (hdc, NULL);
3721 if (raised_p)
3722 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
3723 else
3724 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3727 w32_set_clip_rectangle (hdc, clip_rect);
3729 /* Bottom. */
3730 for (i = 0; i < width; ++i)
3732 w32_fill_area (f, hdc, gc.foreground,
3733 left_x + i * left_p, bottom_y - i,
3734 (right_x + 1 - i * right_p) - left_x + i * left_p, 1);
3737 /* Right. */
3738 if (right_p)
3739 for (i = 0; i < width; ++i)
3741 w32_fill_area (f, hdc, gc.foreground,
3742 right_x - i, top_y + i + 1, 1,
3743 (bottom_y - i) - (top_y + i + 1));
3746 w32_set_clip_rectangle (hdc, NULL);
3748 release_frame_dc (f, hdc);
3752 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3753 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3754 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3755 left side of the rectangle. RIGHT_P non-zero means draw a line
3756 on the right side of the rectangle. CLIP_RECT is the clipping
3757 rectangle to use when drawing. */
3759 static void
3760 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3761 left_p, right_p, clip_rect)
3762 struct glyph_string *s;
3763 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3764 RECT *clip_rect;
3766 w32_set_clip_rectangle (s->hdc, clip_rect);
3768 /* Top. */
3769 w32_fill_area (s->f, s->hdc, s->face->box_color,
3770 left_x, top_y, right_x - left_x + 1, width);
3772 /* Left. */
3773 if (left_p)
3775 w32_fill_area (s->f, s->hdc, s->face->box_color,
3776 left_x, top_y, width, bottom_y - top_y + 1);
3779 /* Bottom. */
3780 w32_fill_area (s->f, s->hdc, s->face->box_color,
3781 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3783 /* Right. */
3784 if (right_p)
3786 w32_fill_area (s->f, s->hdc, s->face->box_color,
3787 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3790 w32_set_clip_rectangle (s->hdc, NULL);
3794 /* Draw a box around glyph string S. */
3796 static void
3797 x_draw_glyph_string_box (s)
3798 struct glyph_string *s;
3800 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3801 int left_p, right_p;
3802 struct glyph *last_glyph;
3803 RECT clip_rect;
3805 last_x = window_box_right (s->w, s->area);
3806 if (s->row->full_width_p
3807 && !s->w->pseudo_window_p)
3809 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3810 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3811 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3814 /* The glyph that may have a right box line. */
3815 last_glyph = (s->cmp || s->img
3816 ? s->first_glyph
3817 : s->first_glyph + s->nchars - 1);
3819 width = abs (s->face->box_line_width);
3820 raised_p = s->face->box == FACE_RAISED_BOX;
3821 left_x = s->x;
3822 right_x = ((s->row->full_width_p
3823 ? last_x - 1
3824 : min (last_x, s->x + s->background_width) - 1));
3825 top_y = s->y;
3826 bottom_y = top_y + s->height - 1;
3828 left_p = (s->first_glyph->left_box_line_p
3829 || (s->hl == DRAW_MOUSE_FACE
3830 && (s->prev == NULL
3831 || s->prev->hl != s->hl)));
3832 right_p = (last_glyph->right_box_line_p
3833 || (s->hl == DRAW_MOUSE_FACE
3834 && (s->next == NULL
3835 || s->next->hl != s->hl)));
3837 w32_get_glyph_string_clip_rect (s, &clip_rect);
3839 if (s->face->box == FACE_SIMPLE_BOX)
3840 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3841 left_p, right_p, &clip_rect);
3842 else
3844 x_setup_relief_colors (s);
3845 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3846 width, raised_p, left_p, right_p, &clip_rect);
3851 /* Draw foreground of image glyph string S. */
3853 static void
3854 x_draw_image_foreground (s)
3855 struct glyph_string *s;
3857 int x;
3858 int y = s->ybase - image_ascent (s->img, s->face);
3860 /* If first glyph of S has a left box line, start drawing it to the
3861 right of that line. */
3862 if (s->face->box != FACE_NO_BOX
3863 && s->first_glyph->left_box_line_p)
3864 x = s->x + abs (s->face->box_line_width);
3865 else
3866 x = s->x;
3868 /* If there is a margin around the image, adjust x- and y-position
3869 by that margin. */
3870 x += s->img->hmargin;
3871 y += s->img->vmargin;
3873 SaveDC (s->hdc);
3875 if (s->img->pixmap)
3877 #if 0 /* TODO: image mask */
3878 if (s->img->mask)
3880 /* We can't set both a clip mask and use XSetClipRectangles
3881 because the latter also sets a clip mask. We also can't
3882 trust on the shape extension to be available
3883 (XShapeCombineRegion). So, compute the rectangle to draw
3884 manually. */
3885 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3886 | GCFunction);
3887 XGCValues xgcv;
3888 XRectangle clip_rect, image_rect, r;
3890 xgcv.clip_mask = s->img->mask;
3891 xgcv.clip_x_origin = x;
3892 xgcv.clip_y_origin = y;
3893 xgcv.function = GXcopy;
3894 XChangeGC (s->display, s->gc, mask, &xgcv);
3896 w32_get_glyph_string_clip_rect (s, &clip_rect);
3897 image_rect.x = x;
3898 image_rect.y = y;
3899 image_rect.width = s->img->width;
3900 image_rect.height = s->img->height;
3901 if (IntersectRect (&r, &clip_rect, &image_rect))
3902 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3903 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3905 else
3906 #endif
3908 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3909 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3910 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3911 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3912 x_set_glyph_string_clipping (s);
3914 SetTextColor (s->hdc, s->gc->foreground);
3915 SetBkColor (s->hdc, s->gc->background);
3916 #if 0 /* From w32bdf.c (which is from Meadow). */
3917 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3918 compat_hdc, 0, 0, SRCCOPY);
3919 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3920 compat_hdc, 0, 0, 0xB8074A);
3921 #else
3922 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3923 compat_hdc, 0, 0, 0xE20746);
3924 #endif
3925 SelectObject (s->hdc, orig_brush);
3926 DeleteObject (fg_brush);
3927 SelectObject (compat_hdc, orig_obj);
3928 DeleteDC (compat_hdc);
3930 /* When the image has a mask, we can expect that at
3931 least part of a mouse highlight or a block cursor will
3932 be visible. If the image doesn't have a mask, make
3933 a block cursor visible by drawing a rectangle around
3934 the image. I believe it's looking better if we do
3935 nothing here for mouse-face. */
3936 if (s->hl == DRAW_CURSOR)
3937 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width - 1,
3938 s->img->height - 1);
3939 w32_set_clip_rectangle (s->hdc, NULL);
3942 else
3943 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width -1,
3944 s->img->height - 1);
3946 RestoreDC (s->hdc ,-1);
3951 /* Draw a relief around the image glyph string S. */
3953 static void
3954 x_draw_image_relief (s)
3955 struct glyph_string *s;
3957 int x0, y0, x1, y1, thick, raised_p;
3958 RECT r;
3959 int x;
3960 int y = s->ybase - image_ascent (s->img, s->face);
3962 /* If first glyph of S has a left box line, start drawing it to the
3963 right of that line. */
3964 if (s->face->box != FACE_NO_BOX
3965 && s->first_glyph->left_box_line_p)
3966 x = s->x + abs (s->face->box_line_width);
3967 else
3968 x = s->x;
3970 /* If there is a margin around the image, adjust x- and y-position
3971 by that margin. */
3972 x += s->img->hmargin;
3973 y += s->img->vmargin;
3975 if (s->hl == DRAW_IMAGE_SUNKEN
3976 || s->hl == DRAW_IMAGE_RAISED)
3978 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3979 raised_p = s->hl == DRAW_IMAGE_RAISED;
3981 else
3983 thick = abs (s->img->relief);
3984 raised_p = s->img->relief > 0;
3987 x0 = x - thick;
3988 y0 = y - thick;
3989 x1 = x + s->img->width + thick - 1;
3990 y1 = y + s->img->height + thick - 1;
3992 x_setup_relief_colors (s);
3993 w32_get_glyph_string_clip_rect (s, &r);
3994 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3998 /* Draw the foreground of image glyph string S to PIXMAP. */
4000 static void
4001 w32_draw_image_foreground_1 (s, pixmap)
4002 struct glyph_string *s;
4003 HBITMAP pixmap;
4005 HDC hdc = CreateCompatibleDC (s->hdc);
4006 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
4007 int x;
4008 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4010 /* If first glyph of S has a left box line, start drawing it to the
4011 right of that line. */
4012 if (s->face->box != FACE_NO_BOX
4013 && s->first_glyph->left_box_line_p)
4014 x = abs (s->face->box_line_width);
4015 else
4016 x = 0;
4018 /* If there is a margin around the image, adjust x- and y-position
4019 by that margin. */
4020 x += s->img->hmargin;
4021 y += s->img->vmargin;
4023 if (s->img->pixmap)
4025 #if 0 /* TODO: image mask */
4026 if (s->img->mask)
4028 /* We can't set both a clip mask and use XSetClipRectangles
4029 because the latter also sets a clip mask. We also can't
4030 trust on the shape extension to be available
4031 (XShapeCombineRegion). So, compute the rectangle to draw
4032 manually. */
4033 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4034 | GCFunction);
4035 XGCValues xgcv;
4037 xgcv.clip_mask = s->img->mask;
4038 xgcv.clip_x_origin = x;
4039 xgcv.clip_y_origin = y;
4040 xgcv.function = GXcopy;
4041 XChangeGC (s->display, s->gc, mask, &xgcv);
4043 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4044 0, 0, s->img->width, s->img->height, x, y);
4045 XSetClipMask (s->display, s->gc, None);
4047 else
4048 #endif
4050 HDC compat_hdc = CreateCompatibleDC (hdc);
4051 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4052 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
4053 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
4055 SetTextColor (hdc, s->gc->foreground);
4056 SetBkColor (hdc, s->gc->background);
4057 #if 0 /* From w32bdf.c (which is from Meadow). */
4058 BitBlt (hdc, x, y, s->img->width, s->img->height,
4059 compat_hdc, 0, 0, SRCCOPY);
4060 BitBlt (hdc, x, y, s->img->width, s->img->height,
4061 compat_hdc, 0, 0, 0xB8074A);
4062 #else
4063 BitBlt (hdc, x, y, s->img->width, s->img->height,
4064 compat_hdc, 0, 0, 0xE20746);
4065 #endif
4066 SelectObject (hdc, orig_brush);
4067 DeleteObject (fg_brush);
4068 SelectObject (compat_hdc, orig_obj);
4069 DeleteDC (compat_hdc);
4071 /* When the image has a mask, we can expect that at
4072 least part of a mouse highlight or a block cursor will
4073 be visible. If the image doesn't have a mask, make
4074 a block cursor visible by drawing a rectangle around
4075 the image. I believe it's looking better if we do
4076 nothing here for mouse-face. */
4077 if (s->hl == DRAW_CURSOR)
4078 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4079 s->img->height - 1);
4082 else
4083 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4084 s->img->height - 1);
4086 SelectObject (hdc, orig_hdc_obj);
4087 DeleteDC (hdc);
4091 /* Draw part of the background of glyph string S. X, Y, W, and H
4092 give the rectangle to draw. */
4094 static void
4095 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4096 struct glyph_string *s;
4097 int x, y, w, h;
4099 #if 0 /* TODO: stipple */
4100 if (s->stippled_p)
4102 /* Fill background with a stipple pattern. */
4103 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4104 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4105 XSetFillStyle (s->display, s->gc, FillSolid);
4107 else
4108 #endif
4109 x_clear_glyph_string_rect (s, x, y, w, h);
4113 /* Draw image glyph string S.
4115 s->y
4116 s->x +-------------------------
4117 | s->face->box
4119 | +-------------------------
4120 | | s->img->vmargin
4122 | | +-------------------
4123 | | | the image
4127 static void
4128 x_draw_image_glyph_string (s)
4129 struct glyph_string *s;
4131 int x, y;
4132 int box_line_hwidth = abs (s->face->box_line_width);
4133 int box_line_vwidth = max (s->face->box_line_width, 0);
4134 int height;
4135 HBITMAP pixmap = 0;
4137 height = s->height - 2 * box_line_vwidth;
4139 /* Fill background with face under the image. Do it only if row is
4140 taller than image or if image has a clip mask to reduce
4141 flickering. */
4142 s->stippled_p = s->face->stipple != 0;
4143 if (height > s->img->height
4144 || s->img->hmargin
4145 || s->img->vmargin
4146 #if 0 /* TODO: image mask */
4147 || s->img->mask
4148 #endif
4149 || s->img->pixmap == 0
4150 || s->width != s->background_width)
4152 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4153 x = s->x + box_line_hwidth;
4154 else
4155 x = s->x;
4157 y = s->y + box_line_vwidth;
4158 #if 0 /* TODO: image mask */
4159 if (s->img->mask)
4161 /* Create a pixmap as large as the glyph string. Fill it
4162 with the background color. Copy the image to it, using
4163 its mask. Copy the temporary pixmap to the display. */
4164 Screen *screen = FRAME_X_SCREEN (s->f);
4165 int depth = DefaultDepthOfScreen (screen);
4167 /* Create a pixmap as large as the glyph string. */
4168 pixmap = XCreatePixmap (s->display, s->window,
4169 s->background_width,
4170 s->height, depth);
4172 /* Don't clip in the following because we're working on the
4173 pixmap. */
4174 XSetClipMask (s->display, s->gc, None);
4176 /* Fill the pixmap with the background color/stipple. */
4177 if (s->stippled_p)
4179 /* Fill background with a stipple pattern. */
4180 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4181 XFillRectangle (s->display, pixmap, s->gc,
4182 0, 0, s->background_width, s->height);
4183 XSetFillStyle (s->display, s->gc, FillSolid);
4185 else
4187 XGCValues xgcv;
4188 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4189 &xgcv);
4190 XSetForeground (s->display, s->gc, xgcv.background);
4191 XFillRectangle (s->display, pixmap, s->gc,
4192 0, 0, s->background_width, s->height);
4193 XSetForeground (s->display, s->gc, xgcv.foreground);
4196 else
4197 #endif
4198 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4200 s->background_filled_p = 1;
4203 /* Draw the foreground. */
4204 if (pixmap != 0)
4206 w32_draw_image_foreground_1 (s, pixmap);
4207 x_set_glyph_string_clipping (s);
4209 HDC compat_hdc = CreateCompatibleDC (s->hdc);
4210 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4211 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
4212 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
4214 SetTextColor (s->hdc, s->gc->foreground);
4215 SetBkColor (s->hdc, s->gc->background);
4216 #if 0 /* From w32bdf.c (which is from Meadow). */
4217 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4218 compat_hdc, 0, 0, SRCCOPY);
4219 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4220 compat_hdc, 0, 0, 0xB8074A);
4221 #else
4222 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4223 compat_hdc, 0, 0, 0xE20746);
4224 #endif
4225 SelectObject (s->hdc, orig_brush);
4226 DeleteObject (fg_brush);
4227 SelectObject (compat_hdc, orig_obj);
4228 DeleteDC (compat_hdc);
4230 DeleteObject (pixmap);
4231 pixmap = 0;
4233 else
4234 x_draw_image_foreground (s);
4236 /* If we must draw a relief around the image, do it. */
4237 if (s->img->relief
4238 || s->hl == DRAW_IMAGE_RAISED
4239 || s->hl == DRAW_IMAGE_SUNKEN)
4240 x_draw_image_relief (s);
4244 /* Draw stretch glyph string S. */
4246 static void
4247 x_draw_stretch_glyph_string (s)
4248 struct glyph_string *s;
4250 xassert (s->first_glyph->type == STRETCH_GLYPH);
4251 s->stippled_p = s->face->stipple != 0;
4253 if (s->hl == DRAW_CURSOR
4254 && !x_stretch_cursor_p)
4256 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4257 as wide as the stretch glyph. */
4258 int width = min (CANON_X_UNIT (s->f), s->background_width);
4260 /* Draw cursor. */
4261 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4263 /* Clear rest using the GC of the original non-cursor face. */
4264 if (width < s->background_width)
4266 XGCValues *gc = s->face->gc;
4267 int x = s->x + width, y = s->y;
4268 int w = s->background_width - width, h = s->height;
4269 RECT r;
4270 HDC hdc = s->hdc;
4271 w32_get_glyph_string_clip_rect (s, &r);
4272 w32_set_clip_rectangle (hdc, &r);
4274 #if 0 /* TODO: stipple */
4275 if (s->face->stipple)
4277 /* Fill background with a stipple pattern. */
4278 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4279 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4280 XSetFillStyle (s->display, gc, FillSolid);
4282 else
4283 #endif
4285 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
4289 else
4290 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4291 s->height);
4293 s->background_filled_p = 1;
4297 /* Draw glyph string S. */
4299 static void
4300 x_draw_glyph_string (s)
4301 struct glyph_string *s;
4303 int relief_drawn_p = 0;
4305 /* If S draws into the background of its successor, draw the
4306 background of the successor first so that S can draw into it.
4307 This makes S->next use XDrawString instead of XDrawImageString. */
4308 if (s->next && s->right_overhang && !s->for_overlaps_p)
4310 xassert (s->next->img == NULL);
4311 x_set_glyph_string_gc (s->next);
4312 x_set_glyph_string_clipping (s->next);
4313 x_draw_glyph_string_background (s->next, 1);
4316 /* Set up S->gc, set clipping and draw S. */
4317 x_set_glyph_string_gc (s);
4318 x_set_glyph_string_clipping (s);
4320 /* Draw relief (if any) in advance for char/composition so that the
4321 glyph string can be drawn over it. */
4322 if (!s->for_overlaps_p
4323 && s->face->box != FACE_NO_BOX
4324 && (s->first_glyph->type == CHAR_GLYPH
4325 || s->first_glyph->type == COMPOSITE_GLYPH))
4328 x_draw_glyph_string_background (s, 1);
4329 x_draw_glyph_string_box (s);
4330 relief_drawn_p = 1;
4333 switch (s->first_glyph->type)
4335 case IMAGE_GLYPH:
4336 x_draw_image_glyph_string (s);
4337 break;
4339 case STRETCH_GLYPH:
4340 x_draw_stretch_glyph_string (s);
4341 break;
4343 case CHAR_GLYPH:
4344 if (s->for_overlaps_p)
4345 s->background_filled_p = 1;
4346 else
4347 x_draw_glyph_string_background (s, 0);
4348 x_draw_glyph_string_foreground (s);
4349 break;
4351 case COMPOSITE_GLYPH:
4352 if (s->for_overlaps_p || s->gidx > 0)
4353 s->background_filled_p = 1;
4354 else
4355 x_draw_glyph_string_background (s, 1);
4356 x_draw_composite_glyph_string_foreground (s);
4357 break;
4359 default:
4360 abort ();
4363 if (!s->for_overlaps_p)
4365 /* Draw underline. */
4366 if (s->face->underline_p
4367 && (s->font->bdf || !s->font->tm.tmUnderlined))
4369 unsigned long h = 1;
4370 unsigned long dy = s->height - h;
4372 if (s->face->underline_defaulted_p)
4374 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4375 s->y + dy, s->width, 1);
4377 else
4379 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4380 s->y + dy, s->width, 1);
4384 /* Draw overline. */
4385 if (s->face->overline_p)
4387 unsigned long dy = 0, h = 1;
4389 if (s->face->overline_color_defaulted_p)
4391 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4392 s->y + dy, s->width, h);
4394 else
4396 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4397 s->y + dy, s->width, h);
4401 /* Draw strike-through. */
4402 if (s->face->strike_through_p
4403 && (s->font->bdf || !s->font->tm.tmStruckOut))
4405 unsigned long h = 1;
4406 unsigned long dy = (s->height - h) / 2;
4408 if (s->face->strike_through_color_defaulted_p)
4410 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
4411 s->width, h);
4413 else
4415 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4416 s->y + dy, s->width, h);
4420 /* Draw relief. */
4421 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4422 x_draw_glyph_string_box (s);
4425 /* Reset clipping. */
4426 w32_set_clip_rectangle (s->hdc, NULL);
4430 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4431 struct face **, int));
4434 /* Fill glyph string S with composition components specified by S->cmp.
4436 FACES is an array of faces for all components of this composition.
4437 S->gidx is the index of the first component for S.
4438 OVERLAPS_P non-zero means S should draw the foreground only, and
4439 use its physical height for clipping.
4441 Value is the index of a component not in S. */
4443 static int
4444 x_fill_composite_glyph_string (s, faces, overlaps_p)
4445 struct glyph_string *s;
4446 struct face **faces;
4447 int overlaps_p;
4449 int i;
4451 xassert (s);
4453 s->for_overlaps_p = overlaps_p;
4455 s->face = faces[s->gidx];
4456 s->font = s->face->font;
4457 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4459 /* For all glyphs of this composition, starting at the offset
4460 S->gidx, until we reach the end of the definition or encounter a
4461 glyph that requires the different face, add it to S. */
4462 ++s->nchars;
4463 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4464 ++s->nchars;
4466 /* All glyph strings for the same composition has the same width,
4467 i.e. the width set for the first component of the composition. */
4469 s->width = s->first_glyph->pixel_width;
4471 /* If the specified font could not be loaded, use the frame's
4472 default font, but record the fact that we couldn't load it in
4473 the glyph string so that we can draw rectangles for the
4474 characters of the glyph string. */
4475 if (s->font == NULL)
4477 s->font_not_found_p = 1;
4478 s->font = FRAME_FONT (s->f);
4481 /* Adjust base line for subscript/superscript text. */
4482 s->ybase += s->first_glyph->voffset;
4484 xassert (s->face && s->face->gc);
4486 /* This glyph string must always be drawn with 16-bit functions. */
4487 s->two_byte_p = 1;
4489 return s->gidx + s->nchars;
4493 /* Fill glyph string S from a sequence of character glyphs.
4495 FACE_ID is the face id of the string. START is the index of the
4496 first glyph to consider, END is the index of the last + 1.
4497 OVERLAPS_P non-zero means S should draw the foreground only, and
4498 use its physical height for clipping.
4500 Value is the index of the first glyph not in S. */
4502 static int
4503 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4504 struct glyph_string *s;
4505 int face_id;
4506 int start, end, overlaps_p;
4508 struct glyph *glyph, *last;
4509 int voffset;
4510 int glyph_not_available_p;
4512 xassert (s->f == XFRAME (s->w->frame));
4513 xassert (s->nchars == 0);
4514 xassert (start >= 0 && end > start);
4516 s->for_overlaps_p = overlaps_p;
4517 glyph = s->row->glyphs[s->area] + start;
4518 last = s->row->glyphs[s->area] + end;
4519 voffset = glyph->voffset;
4521 glyph_not_available_p = glyph->glyph_not_available_p;
4523 while (glyph < last
4524 && glyph->type == CHAR_GLYPH
4525 && glyph->voffset == voffset
4526 /* Same face id implies same font, nowadays. */
4527 && glyph->face_id == face_id
4528 && glyph->glyph_not_available_p == glyph_not_available_p)
4530 int two_byte_p;
4532 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4533 s->char2b + s->nchars,
4534 &two_byte_p);
4535 s->two_byte_p = two_byte_p;
4536 ++s->nchars;
4537 xassert (s->nchars <= end - start);
4538 s->width += glyph->pixel_width;
4539 ++glyph;
4542 s->font = s->face->font;
4543 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4545 /* If the specified font could not be loaded, use the frame's font,
4546 but record the fact that we couldn't load it in
4547 S->font_not_found_p so that we can draw rectangles for the
4548 characters of the glyph string. */
4549 if (s->font == NULL || glyph_not_available_p)
4551 s->font_not_found_p = 1;
4552 s->font = FRAME_FONT (s->f);
4555 /* Adjust base line for subscript/superscript text. */
4556 s->ybase += voffset;
4558 xassert (s->face && s->face->gc);
4559 return glyph - s->row->glyphs[s->area];
4563 /* Fill glyph string S from image glyph S->first_glyph. */
4565 static void
4566 x_fill_image_glyph_string (s)
4567 struct glyph_string *s;
4569 xassert (s->first_glyph->type == IMAGE_GLYPH);
4570 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4571 xassert (s->img);
4572 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4573 s->font = s->face->font;
4574 s->width = s->first_glyph->pixel_width;
4576 /* Adjust base line for subscript/superscript text. */
4577 s->ybase += s->first_glyph->voffset;
4581 /* Fill glyph string S from a sequence of stretch glyphs.
4583 ROW is the glyph row in which the glyphs are found, AREA is the
4584 area within the row. START is the index of the first glyph to
4585 consider, END is the index of the last + 1.
4587 Value is the index of the first glyph not in S. */
4589 static int
4590 x_fill_stretch_glyph_string (s, row, area, start, end)
4591 struct glyph_string *s;
4592 struct glyph_row *row;
4593 enum glyph_row_area area;
4594 int start, end;
4596 struct glyph *glyph, *last;
4597 int voffset, face_id;
4599 xassert (s->first_glyph->type == STRETCH_GLYPH);
4601 glyph = s->row->glyphs[s->area] + start;
4602 last = s->row->glyphs[s->area] + end;
4603 face_id = glyph->face_id;
4604 s->face = FACE_FROM_ID (s->f, face_id);
4605 s->font = s->face->font;
4606 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4607 s->width = glyph->pixel_width;
4608 voffset = glyph->voffset;
4610 for (++glyph;
4611 (glyph < last
4612 && glyph->type == STRETCH_GLYPH
4613 && glyph->voffset == voffset
4614 && glyph->face_id == face_id);
4615 ++glyph)
4616 s->width += glyph->pixel_width;
4618 /* Adjust base line for subscript/superscript text. */
4619 s->ybase += voffset;
4621 xassert (s->face && s->face->gc);
4622 return glyph - s->row->glyphs[s->area];
4626 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4627 of XChar2b structures for S; it can't be allocated in
4628 x_init_glyph_string because it must be allocated via `alloca'. W
4629 is the window on which S is drawn. ROW and AREA are the glyph row
4630 and area within the row from which S is constructed. START is the
4631 index of the first glyph structure covered by S. HL is a
4632 face-override for drawing S. */
4634 static void
4635 w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4636 struct glyph_string *s;
4637 HDC hdc;
4638 wchar_t *char2b;
4639 struct window *w;
4640 struct glyph_row *row;
4641 enum glyph_row_area area;
4642 int start;
4643 enum draw_glyphs_face hl;
4645 bzero (s, sizeof *s);
4646 s->w = w;
4647 s->f = XFRAME (w->frame);
4648 s->hdc = hdc;
4649 s->window = FRAME_W32_WINDOW (s->f);
4650 s->char2b = char2b;
4651 s->hl = hl;
4652 s->row = row;
4653 s->area = area;
4654 s->first_glyph = row->glyphs[area] + start;
4655 s->height = row->height;
4656 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4658 /* Display the internal border below the tool-bar window. */
4659 if (s->w == XWINDOW (s->f->tool_bar_window))
4660 s->y -= s->f->output_data.w32->internal_border_width;
4662 s->ybase = s->y + row->ascent;
4666 /* Set background width of glyph string S. START is the index of the
4667 first glyph following S. LAST_X is the right-most x-position + 1
4668 in the drawing area. */
4670 static INLINE void
4671 x_set_glyph_string_background_width (s, start, last_x)
4672 struct glyph_string *s;
4673 int start;
4674 int last_x;
4676 /* If the face of this glyph string has to be drawn to the end of
4677 the drawing area, set S->extends_to_end_of_line_p. */
4678 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4680 if (start == s->row->used[s->area]
4681 && s->hl == DRAW_NORMAL_TEXT
4682 && s->area == TEXT_AREA
4683 && (s->row->fill_line_p
4684 || s->face->background != default_face->background
4685 || s->face->stipple != default_face->stipple))
4686 s->extends_to_end_of_line_p = 1;
4688 /* If S extends its face to the end of the line, set its
4689 background_width to the distance to the right edge of the drawing
4690 area. */
4691 if (s->extends_to_end_of_line_p)
4692 s->background_width = last_x - s->x + 1;
4693 else
4694 s->background_width = s->width;
4698 /* Add a glyph string for a stretch glyph to the list of strings
4699 between HEAD and TAIL. START is the index of the stretch glyph in
4700 row area AREA of glyph row ROW. END is the index of the last glyph
4701 in that glyph row area. X is the current output position assigned
4702 to the new glyph string constructed. HL overrides that face of the
4703 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4704 is the right-most x-position of the drawing area. */
4706 #define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4707 do \
4709 s = (struct glyph_string *) alloca (sizeof *s); \
4710 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4711 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4712 x_append_glyph_string (&HEAD, &TAIL, s); \
4713 s->x = (X); \
4715 while (0)
4718 /* Add a glyph string for an image glyph to the list of strings
4719 between HEAD and TAIL. START is the index of the image glyph in
4720 row area AREA of glyph row ROW. END is the index of the last glyph
4721 in that glyph row area. X is the current output position assigned
4722 to the new glyph string constructed. HL overrides that face of the
4723 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4724 is the right-most x-position of the drawing area. */
4726 #define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4727 do \
4729 s = (struct glyph_string *) alloca (sizeof *s); \
4730 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4731 x_fill_image_glyph_string (s); \
4732 x_append_glyph_string (&HEAD, &TAIL, s); \
4733 ++START; \
4734 s->x = (X); \
4736 while (0)
4739 /* Add a glyph string for a sequence of character glyphs to the list
4740 of strings between HEAD and TAIL. START is the index of the first
4741 glyph in row area AREA of glyph row ROW that is part of the new
4742 glyph string. END is the index of the last glyph in that glyph row
4743 area. X is the current output position assigned to the new glyph
4744 string constructed. HL overrides that face of the glyph; e.g. it
4745 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4746 right-most x-position of the drawing area. */
4748 #define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4749 do \
4751 int c, face_id; \
4752 wchar_t *char2b; \
4754 c = (ROW)->glyphs[AREA][START].u.ch; \
4755 face_id = (ROW)->glyphs[AREA][START].face_id; \
4757 s = (struct glyph_string *) alloca (sizeof *s); \
4758 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4759 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4760 x_append_glyph_string (&HEAD, &TAIL, s); \
4761 s->x = (X); \
4762 START = x_fill_glyph_string (s, face_id, START, END, \
4763 OVERLAPS_P); \
4765 while (0)
4768 /* Add a glyph string for a composite sequence to the list of strings
4769 between HEAD and TAIL. START is the index of the first glyph in
4770 row area AREA of glyph row ROW that is part of the new glyph
4771 string. END is the index of the last glyph in that glyph row area.
4772 X is the current output position assigned to the new glyph string
4773 constructed. HL overrides that face of the glyph; e.g. it is
4774 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4775 x-position of the drawing area. */
4777 #define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4778 do { \
4779 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4780 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4781 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4782 struct composition *cmp = composition_table[cmp_id]; \
4783 int glyph_len = cmp->glyph_len; \
4784 wchar_t *char2b; \
4785 struct face **faces; \
4786 struct glyph_string *first_s = NULL; \
4787 int n; \
4789 base_face = base_face->ascii_face; \
4790 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4791 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4792 /* At first, fill in `char2b' and `faces'. */ \
4793 for (n = 0; n < glyph_len; n++) \
4795 int c = COMPOSITION_GLYPH (cmp, n); \
4796 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4797 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4798 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4799 this_face_id, char2b + n, 1); \
4802 /* Make glyph_strings for each glyph sequence that is drawable by \
4803 the same face, and append them to HEAD/TAIL. */ \
4804 for (n = 0; n < cmp->glyph_len;) \
4806 s = (struct glyph_string *) alloca (sizeof *s); \
4807 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4808 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4809 s->cmp = cmp; \
4810 s->gidx = n; \
4811 s->x = (X); \
4813 if (n == 0) \
4814 first_s = s; \
4816 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4819 ++START; \
4820 s = first_s; \
4821 } while (0)
4824 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4825 of AREA of glyph row ROW on window W between indices START and END.
4826 HL overrides the face for drawing glyph strings, e.g. it is
4827 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4828 x-positions of the drawing area.
4830 This is an ugly monster macro construct because we must use alloca
4831 to allocate glyph strings (because x_draw_glyphs can be called
4832 asynchronously). */
4834 #define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4835 do \
4837 HEAD = TAIL = NULL; \
4838 while (START < END) \
4840 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4841 switch (first_glyph->type) \
4843 case CHAR_GLYPH: \
4844 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4845 HEAD, TAIL, HL, X, LAST_X, \
4846 OVERLAPS_P); \
4847 break; \
4849 case COMPOSITE_GLYPH: \
4850 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4851 END, HEAD, TAIL, HL, X, \
4852 LAST_X, OVERLAPS_P); \
4853 break; \
4855 case STRETCH_GLYPH: \
4856 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4857 HEAD, TAIL, HL, X, LAST_X); \
4858 break; \
4860 case IMAGE_GLYPH: \
4861 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4862 HEAD, TAIL, HL, X, LAST_X); \
4863 break; \
4865 default: \
4866 abort (); \
4869 x_set_glyph_string_background_width (s, START, LAST_X); \
4870 (X) += s->width; \
4873 while (0)
4876 /* Draw glyphs between START and END in AREA of ROW on window W,
4877 starting at x-position X. X is relative to AREA in W. HL is a
4878 face-override with the following meaning:
4880 DRAW_NORMAL_TEXT draw normally
4881 DRAW_CURSOR draw in cursor face
4882 DRAW_MOUSE_FACE draw in mouse face.
4883 DRAW_INVERSE_VIDEO draw in mode line face
4884 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4885 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4887 If REAL_START is non-null, return in *REAL_START the real starting
4888 position for display. This can be different from START in case
4889 overlapping glyphs must be displayed. If REAL_END is non-null,
4890 return in *REAL_END the real end position for display. This can be
4891 different from END in case overlapping glyphs must be displayed.
4893 If OVERLAPS_P is non-zero, draw only the foreground of characters
4894 and clip to the physical height of ROW.
4896 Value is the x-position reached, relative to AREA of W. */
4898 static int
4899 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4900 overlaps_p)
4901 struct window *w;
4902 int x;
4903 struct glyph_row *row;
4904 enum glyph_row_area area;
4905 int start, end;
4906 enum draw_glyphs_face hl;
4907 int *real_start, *real_end;
4908 int overlaps_p;
4910 struct glyph_string *head, *tail;
4911 struct glyph_string *s;
4912 int last_x, area_width;
4913 int x_reached;
4914 int i, j;
4915 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4917 /* Let's rather be paranoid than getting a SEGV. */
4918 end = min (end, row->used[area]);
4919 start = max (0, start);
4920 start = min (end, start);
4921 if (real_start)
4922 *real_start = start;
4923 if (real_end)
4924 *real_end = end;
4926 /* Translate X to frame coordinates. Set last_x to the right
4927 end of the drawing area. */
4928 if (row->full_width_p)
4930 /* X is relative to the left edge of W, without scroll bars
4931 or flag areas. */
4932 struct frame *f = XFRAME (WINDOW_FRAME (w));
4933 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4934 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4936 x += window_left_x;
4937 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4938 last_x = window_left_x + area_width;
4940 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4942 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4943 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4944 last_x += width;
4945 else
4946 x -= width;
4949 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4950 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4952 else
4954 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4955 area_width = window_box_width (w, area);
4956 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4959 /* Build a doubly-linked list of glyph_string structures between
4960 head and tail from what we have to draw. Note that the macro
4961 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4962 the reason we use a separate variable `i'. */
4963 i = start;
4964 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4965 overlaps_p);
4966 if (tail)
4967 x_reached = tail->x + tail->background_width;
4968 else
4969 x_reached = x;
4971 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4972 the row, redraw some glyphs in front or following the glyph
4973 strings built above. */
4974 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4976 int dummy_x = 0;
4977 struct glyph_string *h, *t;
4979 /* Compute overhangs for all glyph strings. */
4980 for (s = head; s; s = s->next)
4981 x_compute_glyph_string_overhangs (s);
4983 /* Prepend glyph strings for glyphs in front of the first glyph
4984 string that are overwritten because of the first glyph
4985 string's left overhang. The background of all strings
4986 prepended must be drawn because the first glyph string
4987 draws over it. */
4988 i = x_left_overwritten (head);
4989 if (i >= 0)
4991 j = i;
4992 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
4993 DRAW_NORMAL_TEXT, dummy_x, last_x,
4994 overlaps_p);
4995 start = i;
4996 if (real_start)
4997 *real_start = start;
4998 x_compute_overhangs_and_x (t, head->x, 1);
4999 x_prepend_glyph_string_lists (&head, &tail, h, t);
5002 /* Prepend glyph strings for glyphs in front of the first glyph
5003 string that overwrite that glyph string because of their
5004 right overhang. For these strings, only the foreground must
5005 be drawn, because it draws over the glyph string at `head'.
5006 The background must not be drawn because this would overwrite
5007 right overhangs of preceding glyphs for which no glyph
5008 strings exist. */
5009 i = x_left_overwriting (head);
5010 if (i >= 0)
5012 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
5013 DRAW_NORMAL_TEXT, dummy_x, last_x,
5014 overlaps_p);
5015 for (s = h; s; s = s->next)
5016 s->background_filled_p = 1;
5017 if (real_start)
5018 *real_start = i;
5019 x_compute_overhangs_and_x (t, head->x, 1);
5020 x_prepend_glyph_string_lists (&head, &tail, h, t);
5023 /* Append glyphs strings for glyphs following the last glyph
5024 string tail that are overwritten by tail. The background of
5025 these strings has to be drawn because tail's foreground draws
5026 over it. */
5027 i = x_right_overwritten (tail);
5028 if (i >= 0)
5030 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5031 DRAW_NORMAL_TEXT, x, last_x,
5032 overlaps_p);
5033 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5034 x_append_glyph_string_lists (&head, &tail, h, t);
5035 if (real_end)
5036 *real_end = i;
5039 /* Append glyph strings for glyphs following the last glyph
5040 string tail that overwrite tail. The foreground of such
5041 glyphs has to be drawn because it writes into the background
5042 of tail. The background must not be drawn because it could
5043 paint over the foreground of following glyphs. */
5044 i = x_right_overwriting (tail);
5045 if (i >= 0)
5047 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5048 DRAW_NORMAL_TEXT, x, last_x,
5049 overlaps_p);
5050 for (s = h; s; s = s->next)
5051 s->background_filled_p = 1;
5052 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5053 x_append_glyph_string_lists (&head, &tail, h, t);
5054 if (real_end)
5055 *real_end = i;
5059 /* Draw all strings. */
5060 for (s = head; s; s = s->next)
5061 x_draw_glyph_string (s);
5063 /* Value is the x-position up to which drawn, relative to AREA of W.
5064 This doesn't include parts drawn because of overhangs. */
5065 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5066 if (!row->full_width_p)
5068 if (area > LEFT_MARGIN_AREA)
5069 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5070 if (area > TEXT_AREA)
5071 x_reached -= window_box_width (w, TEXT_AREA);
5074 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
5076 return x_reached;
5080 /* Fix the display of area AREA of overlapping row ROW in window W. */
5082 static void
5083 x_fix_overlapping_area (w, row, area)
5084 struct window *w;
5085 struct glyph_row *row;
5086 enum glyph_row_area area;
5088 int i, x;
5090 BLOCK_INPUT;
5092 if (area == LEFT_MARGIN_AREA)
5093 x = 0;
5094 else if (area == TEXT_AREA)
5095 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5096 else
5097 x = (window_box_width (w, LEFT_MARGIN_AREA)
5098 + window_box_width (w, TEXT_AREA));
5100 for (i = 0; i < row->used[area];)
5102 if (row->glyphs[area][i].overlaps_vertically_p)
5104 int start = i, start_x = x;
5108 x += row->glyphs[area][i].pixel_width;
5109 ++i;
5111 while (i < row->used[area]
5112 && row->glyphs[area][i].overlaps_vertically_p);
5114 x_draw_glyphs (w, start_x, row, area, start, i,
5115 (row->inverse_p
5116 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5117 NULL, NULL, 1);
5119 else
5121 x += row->glyphs[area][i].pixel_width;
5122 ++i;
5126 UNBLOCK_INPUT;
5130 /* Output LEN glyphs starting at START at the nominal cursor position.
5131 Advance the nominal cursor over the text. The global variable
5132 updated_window contains the window being updated, updated_row is
5133 the glyph row being updated, and updated_area is the area of that
5134 row being updated. */
5136 static void
5137 x_write_glyphs (start, len)
5138 struct glyph *start;
5139 int len;
5141 int x, hpos, real_start, real_end;
5143 xassert (updated_window && updated_row);
5144 BLOCK_INPUT;
5146 /* Write glyphs. */
5148 hpos = start - updated_row->glyphs[updated_area];
5149 x = x_draw_glyphs (updated_window, output_cursor.x,
5150 updated_row, updated_area,
5151 hpos, hpos + len,
5152 (updated_row->inverse_p
5153 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5154 &real_start, &real_end, 0);
5156 /* If we drew over the cursor, note that it is not visible any more. */
5157 note_overwritten_text_cursor (updated_window, real_start,
5158 real_end - real_start);
5160 UNBLOCK_INPUT;
5162 /* Advance the output cursor. */
5163 output_cursor.hpos += len;
5164 output_cursor.x = x;
5168 /* Insert LEN glyphs from START at the nominal cursor position. */
5170 static void
5171 x_insert_glyphs (start, len)
5172 struct glyph *start;
5173 register int len;
5175 struct frame *f;
5176 struct window *w;
5177 int line_height, shift_by_width, shifted_region_width;
5178 struct glyph_row *row;
5179 struct glyph *glyph;
5180 int frame_x, frame_y, hpos, real_start, real_end;
5181 HDC hdc;
5183 xassert (updated_window && updated_row);
5184 BLOCK_INPUT;
5185 w = updated_window;
5186 f = XFRAME (WINDOW_FRAME (w));
5187 hdc = get_frame_dc (f);
5189 /* Get the height of the line we are in. */
5190 row = updated_row;
5191 line_height = row->height;
5193 /* Get the width of the glyphs to insert. */
5194 shift_by_width = 0;
5195 for (glyph = start; glyph < start + len; ++glyph)
5196 shift_by_width += glyph->pixel_width;
5198 /* Get the width of the region to shift right. */
5199 shifted_region_width = (window_box_width (w, updated_area)
5200 - output_cursor.x
5201 - shift_by_width);
5203 /* Shift right. */
5204 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5205 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5206 BitBlt (hdc, frame_x + shift_by_width, frame_y,
5207 shifted_region_width, line_height,
5208 hdc, frame_x, frame_y, SRCCOPY);
5210 /* Write the glyphs. */
5211 hpos = start - row->glyphs[updated_area];
5212 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5213 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5214 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5216 /* Advance the output cursor. */
5217 output_cursor.hpos += len;
5218 output_cursor.x += shift_by_width;
5219 release_frame_dc (f, hdc);
5221 UNBLOCK_INPUT;
5225 /* Delete N glyphs at the nominal cursor position. Not implemented
5226 for X frames. */
5228 static void
5229 x_delete_glyphs (n)
5230 register int n;
5232 struct frame *f;
5234 if (updating_frame)
5235 f = updating_frame;
5236 else
5237 f = SELECTED_FRAME ();
5239 if (! FRAME_W32_P (f))
5240 return;
5242 abort ();
5246 /* Erase the current text line from the nominal cursor position
5247 (inclusive) to pixel column TO_X (exclusive). The idea is that
5248 everything from TO_X onward is already erased.
5250 TO_X is a pixel position relative to updated_area of
5251 updated_window. TO_X == -1 means clear to the end of this area. */
5253 static void
5254 x_clear_end_of_line (to_x)
5255 int to_x;
5257 struct frame *f;
5258 struct window *w = updated_window;
5259 int max_x, min_y, max_y;
5260 int from_x, from_y, to_y;
5262 xassert (updated_window && updated_row);
5263 f = XFRAME (w->frame);
5265 if (updated_row->full_width_p)
5267 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5268 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5269 && !w->pseudo_window_p)
5270 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5272 else
5273 max_x = window_box_width (w, updated_area);
5274 max_y = window_text_bottom_y (w);
5276 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5277 of window. For TO_X > 0, truncate to end of drawing area. */
5278 if (to_x == 0)
5279 return;
5280 else if (to_x < 0)
5281 to_x = max_x;
5282 else
5283 to_x = min (to_x, max_x);
5285 to_y = min (max_y, output_cursor.y + updated_row->height);
5287 /* Notice if the cursor will be cleared by this operation. */
5288 if (!updated_row->full_width_p)
5289 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5291 from_x = output_cursor.x;
5293 /* Translate to frame coordinates. */
5294 if (updated_row->full_width_p)
5296 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5297 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5299 else
5301 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5302 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5305 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5306 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5307 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5309 /* Prevent inadvertently clearing to end of the X window. */
5310 if (to_x > from_x && to_y > from_y)
5312 HDC hdc;
5313 BLOCK_INPUT;
5314 hdc = get_frame_dc (f);
5316 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
5317 release_frame_dc (f, hdc);
5318 UNBLOCK_INPUT;
5323 /* Clear entire frame. If updating_frame is non-null, clear that
5324 frame. Otherwise clear the selected frame. */
5326 static void
5327 x_clear_frame ()
5329 struct frame *f;
5331 if (updating_frame)
5332 f = updating_frame;
5333 else
5334 f = SELECTED_FRAME ();
5336 if (! FRAME_W32_P (f))
5337 return;
5339 /* Clearing the frame will erase any cursor, so mark them all as no
5340 longer visible. */
5341 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5342 output_cursor.hpos = output_cursor.vpos = 0;
5343 output_cursor.x = -1;
5345 /* We don't set the output cursor here because there will always
5346 follow an explicit cursor_to. */
5347 BLOCK_INPUT;
5349 w32_clear_window (f);
5351 /* We have to clear the scroll bars, too. If we have changed
5352 colors or something like that, then they should be notified. */
5353 x_scroll_bar_clear (f);
5355 UNBLOCK_INPUT;
5359 /* Make audible bell. */
5361 static void
5362 w32_ring_bell (void)
5364 struct frame *f;
5366 f = SELECTED_FRAME ();
5368 BLOCK_INPUT;
5370 if (FRAME_W32_P (f) && visible_bell)
5372 int i;
5373 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
5375 for (i = 0; i < 5; i++)
5377 FlashWindow (hwnd, TRUE);
5378 Sleep (10);
5380 FlashWindow (hwnd, FALSE);
5382 else
5383 w32_sys_ring_bell ();
5385 UNBLOCK_INPUT;
5389 /* Specify how many text lines, from the top of the window,
5390 should be affected by insert-lines and delete-lines operations.
5391 This, and those operations, are used only within an update
5392 that is bounded by calls to x_update_begin and x_update_end. */
5394 static void
5395 w32_set_terminal_window (n)
5396 register int n;
5398 /* This function intentionally left blank. */
5403 /***********************************************************************
5404 Line Dance
5405 ***********************************************************************/
5407 /* Perform an insert-lines or delete-lines operation, inserting N
5408 lines or deleting -N lines at vertical position VPOS. */
5410 static void
5411 x_ins_del_lines (vpos, n)
5412 int vpos, n;
5414 struct frame *f;
5416 if (updating_frame)
5417 f = updating_frame;
5418 else
5419 f = SELECTED_FRAME ();
5421 if (! FRAME_W32_P (f))
5422 return;
5424 abort ();
5428 /* Scroll part of the display as described by RUN. */
5430 static void
5431 x_scroll_run (w, run)
5432 struct window *w;
5433 struct run *run;
5435 struct frame *f = XFRAME (w->frame);
5436 int x, y, width, height, from_y, to_y, bottom_y;
5437 HDC hdc = get_frame_dc (f);
5439 /* Get frame-relative bounding box of the text display area of W,
5440 without mode lines. Include in this box the flags areas to the
5441 left and right of W. */
5442 window_box (w, -1, &x, &y, &width, &height);
5443 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5444 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5446 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5447 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5448 bottom_y = y + height;
5450 if (to_y < from_y)
5452 /* Scrolling up. Make sure we don't copy part of the mode
5453 line at the bottom. */
5454 if (from_y + run->height > bottom_y)
5455 height = bottom_y - from_y;
5456 else
5457 height = run->height;
5459 else
5461 /* Scolling down. Make sure we don't copy over the mode line.
5462 at the bottom. */
5463 if (to_y + run->height > bottom_y)
5464 height = bottom_y - to_y;
5465 else
5466 height = run->height;
5469 BLOCK_INPUT;
5471 /* Cursor off. Will be switched on again in x_update_window_end. */
5472 updated_window = w;
5473 x_clear_cursor (w);
5475 BitBlt (hdc, x, to_y, width, height, hdc, x, from_y, SRCCOPY);
5477 UNBLOCK_INPUT;
5478 release_frame_dc (f, hdc);
5483 /***********************************************************************
5484 Exposure Events
5485 ***********************************************************************/
5487 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5488 corner of the exposed rectangle. W and H are width and height of
5489 the exposed area. All are pixel values. W or H zero means redraw
5490 the entire frame. */
5492 static void
5493 expose_frame (f, x, y, w, h)
5494 struct frame *f;
5495 int x, y, w, h;
5497 RECT r;
5499 TRACE ((stderr, "expose_frame "));
5501 /* No need to redraw if frame will be redrawn soon. */
5502 if (FRAME_GARBAGED_P (f))
5504 TRACE ((stderr, " garbaged\n"));
5505 return;
5508 /* If basic faces haven't been realized yet, there is no point in
5509 trying to redraw anything. This can happen when we get an expose
5510 event while Emacs is starting, e.g. by moving another window. */
5511 if (FRAME_FACE_CACHE (f) == NULL
5512 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5514 TRACE ((stderr, " no faces\n"));
5515 return;
5518 if (w == 0 || h == 0)
5520 r.left = r.top = 0;
5521 r.right = CANON_X_UNIT (f) * f->width;
5522 r.bottom = CANON_Y_UNIT (f) * f->height;
5524 else
5526 r.left = x;
5527 r.top = y;
5528 r.right = x + w;
5529 r.bottom = y + h;
5532 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
5533 expose_window_tree (XWINDOW (f->root_window), &r);
5535 if (WINDOWP (f->tool_bar_window))
5537 struct window *w = XWINDOW (f->tool_bar_window);
5538 RECT window_rect;
5539 RECT intersection_rect;
5540 int window_x, window_y, window_width, window_height;
5542 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5543 window_rect.left = window_x;
5544 window_rect.top = window_y;
5545 window_rect.right = window_x + window_width;
5546 window_rect.bottom = window_y + window_height;
5548 if (IntersectRect (&intersection_rect, &r, &window_rect))
5549 expose_window (w, &intersection_rect);
5554 /* Redraw (parts) of all windows in the window tree rooted at W that
5555 intersect R. R contains frame pixel coordinates. */
5557 static void
5558 expose_window_tree (w, r)
5559 struct window *w;
5560 RECT *r;
5562 while (w)
5564 if (!NILP (w->hchild))
5565 expose_window_tree (XWINDOW (w->hchild), r);
5566 else if (!NILP (w->vchild))
5567 expose_window_tree (XWINDOW (w->vchild), r);
5568 else
5570 RECT window_rect;
5571 RECT intersection_rect;
5572 struct frame *f = XFRAME (w->frame);
5573 int window_x, window_y, window_width, window_height;
5575 /* Frame-relative pixel rectangle of W. */
5576 window_box (w, -1, &window_x, &window_y, &window_width,
5577 &window_height);
5578 window_rect.left
5579 = (window_x
5580 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5581 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5582 window_rect.top = window_y;
5583 window_rect.right = window_rect.left
5584 + (window_width
5585 + FRAME_X_FLAGS_AREA_WIDTH (f)
5586 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5587 window_rect.bottom = window_rect.top
5588 + window_height + CURRENT_MODE_LINE_HEIGHT (w);
5590 if (IntersectRect (&intersection_rect, r, &window_rect))
5591 expose_window (w, &intersection_rect);
5594 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5599 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5600 which intersects rectangle R. R is in window-relative coordinates. */
5602 static void
5603 expose_area (w, row, r, area)
5604 struct window *w;
5605 struct glyph_row *row;
5606 RECT *r;
5607 enum glyph_row_area area;
5609 struct glyph *first = row->glyphs[area];
5610 struct glyph *end = row->glyphs[area] + row->used[area];
5611 struct glyph *last;
5612 int first_x, start_x, x;
5614 if (area == TEXT_AREA && row->fill_line_p)
5615 /* If row extends face to end of line write the whole line. */
5616 x_draw_glyphs (w, 0, row, area,
5617 0, row->used[area],
5618 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5619 NULL, NULL, 0);
5620 else
5622 /* Set START_X to the window-relative start position for drawing glyphs of
5623 AREA. The first glyph of the text area can be partially visible.
5624 The first glyphs of other areas cannot. */
5625 if (area == LEFT_MARGIN_AREA)
5626 start_x = 0;
5627 else if (area == TEXT_AREA)
5628 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5629 else
5630 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5631 + window_box_width (w, TEXT_AREA));
5632 x = start_x;
5634 /* Find the first glyph that must be redrawn. */
5635 while (first < end
5636 && x + first->pixel_width < r->left)
5638 x += first->pixel_width;
5639 ++first;
5642 /* Find the last one. */
5643 last = first;
5644 first_x = x;
5645 while (last < end
5646 && x < r->right)
5648 x += last->pixel_width;
5649 ++last;
5652 /* Repaint. */
5653 if (last > first)
5654 x_draw_glyphs (w, first_x - start_x, row, area,
5655 first - row->glyphs[area],
5656 last - row->glyphs[area],
5657 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5658 NULL, NULL, 0);
5663 /* Redraw the parts of the glyph row ROW on window W intersecting
5664 rectangle R. R is in window-relative coordinates. */
5666 static void
5667 expose_line (w, row, r)
5668 struct window *w;
5669 struct glyph_row *row;
5670 RECT *r;
5672 xassert (row->enabled_p);
5674 if (row->mode_line_p || w->pseudo_window_p)
5675 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5676 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5677 NULL, NULL, 0);
5678 else
5680 if (row->used[LEFT_MARGIN_AREA])
5681 expose_area (w, row, r, LEFT_MARGIN_AREA);
5682 if (row->used[TEXT_AREA])
5683 expose_area (w, row, r, TEXT_AREA);
5684 if (row->used[RIGHT_MARGIN_AREA])
5685 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5686 x_draw_row_bitmaps (w, row);
5691 /* Return non-zero if W's cursor intersects rectangle R. */
5693 static int
5694 x_phys_cursor_in_rect_p (w, r)
5695 struct window *w;
5696 RECT *r;
5698 RECT cr, result;
5699 struct glyph *cursor_glyph;
5701 cursor_glyph = get_phys_cursor_glyph (w);
5702 if (cursor_glyph)
5704 cr.left = w->phys_cursor.x;
5705 cr.top = w->phys_cursor.y;
5706 cr.right = cr.left + cursor_glyph->pixel_width;
5707 cr.bottom = cr.top + w->phys_cursor_height;
5708 return IntersectRect (&result, &cr, r);
5710 else
5711 return 0;
5715 /* Redraw a rectangle of window W. R is a rectangle in window
5716 relative coordinates. Call this function with input blocked. */
5718 static void
5719 expose_window (w, r)
5720 struct window *w;
5721 RECT *r;
5723 struct glyph_row *row;
5724 int y;
5725 int yb = window_text_bottom_y (w);
5726 int cursor_cleared_p;
5728 /* If window is not yet fully initialized, do nothing. This can
5729 happen when toolkit scroll bars are used and a window is split.
5730 Reconfiguring the scroll bar will generate an expose for a newly
5731 created window. */
5732 if (w->current_matrix == NULL || w == updated_window)
5733 return;
5735 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5736 r->left, r->top, r->right, r->bottom));
5738 /* Convert to window coordinates. */
5739 r->left = FRAME_TO_WINDOW_PIXEL_X (w, r->left);
5740 r->top = FRAME_TO_WINDOW_PIXEL_Y (w, r->top);
5741 r->right = FRAME_TO_WINDOW_PIXEL_X (w, r->right);
5742 r->bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r->bottom);
5744 /* Turn off the cursor. */
5745 if (!w->pseudo_window_p
5746 && x_phys_cursor_in_rect_p (w, r))
5748 x_clear_cursor (w);
5749 cursor_cleared_p = 1;
5751 else
5752 cursor_cleared_p = 0;
5754 /* Find the first row intersecting the rectangle R. */
5755 row = w->current_matrix->rows;
5756 y = 0;
5757 while (row->enabled_p
5758 && y < yb
5759 && y + row->height < r->top)
5761 y += row->height;
5762 ++row;
5765 /* Display the text in the rectangle, one text line at a time. */
5766 while (row->enabled_p
5767 && y < yb
5768 && y < r->bottom)
5770 expose_line (w, row, r);
5771 y += row->height;
5772 ++row;
5775 /* Display the mode line if there is one. */
5776 if (WINDOW_WANTS_MODELINE_P (w)
5777 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5778 row->enabled_p)
5779 && row->y < r->bottom)
5780 expose_line (w, row, r);
5782 if (!w->pseudo_window_p)
5784 /* Draw border between windows. */
5785 x_draw_vertical_border (w);
5787 /* Turn the cursor on again. */
5788 if (cursor_cleared_p)
5789 x_update_window_cursor (w, 1);
5794 static void
5795 frame_highlight (f)
5796 struct frame *f;
5798 x_update_cursor (f, 1);
5801 static void
5802 frame_unhighlight (f)
5803 struct frame *f;
5805 x_update_cursor (f, 1);
5808 /* The focus has changed. Update the frames as necessary to reflect
5809 the new situation. Note that we can't change the selected frame
5810 here, because the Lisp code we are interrupting might become confused.
5811 Each event gets marked with the frame in which it occurred, so the
5812 Lisp code can tell when the switch took place by examining the events. */
5814 static void
5815 x_new_focus_frame (dpyinfo, frame)
5816 struct w32_display_info *dpyinfo;
5817 struct frame *frame;
5819 struct frame *old_focus = dpyinfo->w32_focus_frame;
5821 if (frame != dpyinfo->w32_focus_frame)
5823 /* Set this before calling other routines, so that they see
5824 the correct value of w32_focus_frame. */
5825 dpyinfo->w32_focus_frame = frame;
5827 if (old_focus && old_focus->auto_lower)
5828 x_lower_frame (old_focus);
5830 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5831 pending_autoraise_frame = dpyinfo->w32_focus_frame;
5832 else
5833 pending_autoraise_frame = 0;
5836 x_frame_rehighlight (dpyinfo);
5839 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5841 void
5842 x_mouse_leave (dpyinfo)
5843 struct w32_display_info *dpyinfo;
5845 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
5848 /* The focus has changed, or we have redirected a frame's focus to
5849 another frame (this happens when a frame uses a surrogate
5850 mini-buffer frame). Shift the highlight as appropriate.
5852 The FRAME argument doesn't necessarily have anything to do with which
5853 frame is being highlighted or un-highlighted; we only use it to find
5854 the appropriate X display info. */
5856 static void
5857 w32_frame_rehighlight (frame)
5858 struct frame *frame;
5860 if (! FRAME_W32_P (frame))
5861 return;
5862 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
5865 static void
5866 x_frame_rehighlight (dpyinfo)
5867 struct w32_display_info *dpyinfo;
5869 struct frame *old_highlight = dpyinfo->w32_highlight_frame;
5871 if (dpyinfo->w32_focus_frame)
5873 dpyinfo->w32_highlight_frame
5874 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5875 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5876 : dpyinfo->w32_focus_frame);
5877 if (! FRAME_LIVE_P (dpyinfo->w32_highlight_frame))
5879 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
5880 dpyinfo->w32_highlight_frame = dpyinfo->w32_focus_frame;
5883 else
5884 dpyinfo->w32_highlight_frame = 0;
5886 if (dpyinfo->w32_highlight_frame != old_highlight)
5888 if (old_highlight)
5889 frame_unhighlight (old_highlight);
5890 if (dpyinfo->w32_highlight_frame)
5891 frame_highlight (dpyinfo->w32_highlight_frame);
5895 /* Keyboard processing - modifier keys, etc. */
5897 /* Convert a keysym to its name. */
5899 char *
5900 x_get_keysym_name (keysym)
5901 int keysym;
5903 /* Make static so we can always return it */
5904 static char value[100];
5906 BLOCK_INPUT;
5907 GetKeyNameText (keysym, value, 100);
5908 UNBLOCK_INPUT;
5910 return value;
5915 /* Mouse clicks and mouse movement. Rah. */
5917 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5918 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5919 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5920 not force the value into range. */
5922 void
5923 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5924 FRAME_PTR f;
5925 register int pix_x, pix_y;
5926 register int *x, *y;
5927 RECT *bounds;
5928 int noclip;
5930 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
5931 if (NILP (Vwindow_system))
5933 *x = pix_x;
5934 *y = pix_y;
5935 return;
5938 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5939 even for negative values. */
5940 if (pix_x < 0)
5941 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
5942 if (pix_y < 0)
5943 pix_y -= (f)->output_data.w32->line_height - 1;
5945 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5946 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5948 if (bounds)
5950 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
5951 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
5952 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
5953 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
5956 if (!noclip)
5958 if (pix_x < 0)
5959 pix_x = 0;
5960 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5961 pix_x = FRAME_WINDOW_WIDTH (f);
5963 if (pix_y < 0)
5964 pix_y = 0;
5965 else if (pix_y > f->height)
5966 pix_y = f->height;
5969 *x = pix_x;
5970 *y = pix_y;
5974 /* Given HPOS/VPOS in the current matrix of W, return corresponding
5975 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5976 can't tell the positions because W's display is not up to date,
5977 return 0. */
5980 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5981 struct window *w;
5982 int hpos, vpos;
5983 int *frame_x, *frame_y;
5985 int success_p;
5987 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5988 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5990 if (display_completed)
5992 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5993 struct glyph *glyph = row->glyphs[TEXT_AREA];
5994 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5996 *frame_y = row->y;
5997 *frame_x = row->x;
5998 while (glyph < end)
6000 *frame_x += glyph->pixel_width;
6001 ++glyph;
6004 success_p = 1;
6006 else
6008 *frame_y = *frame_x = 0;
6009 success_p = 0;
6012 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6013 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6014 return success_p;
6017 BOOL
6018 parse_button (message, pbutton, pup)
6019 int message;
6020 int * pbutton;
6021 int * pup;
6023 int button = 0;
6024 int up = 0;
6026 switch (message)
6028 case WM_LBUTTONDOWN:
6029 button = 0;
6030 up = 0;
6031 break;
6032 case WM_LBUTTONUP:
6033 button = 0;
6034 up = 1;
6035 break;
6036 case WM_MBUTTONDOWN:
6037 if (NILP (Vw32_swap_mouse_buttons))
6038 button = 1;
6039 else
6040 button = 2;
6041 up = 0;
6042 break;
6043 case WM_MBUTTONUP:
6044 if (NILP (Vw32_swap_mouse_buttons))
6045 button = 1;
6046 else
6047 button = 2;
6048 up = 1;
6049 break;
6050 case WM_RBUTTONDOWN:
6051 if (NILP (Vw32_swap_mouse_buttons))
6052 button = 2;
6053 else
6054 button = 1;
6055 up = 0;
6056 break;
6057 case WM_RBUTTONUP:
6058 if (NILP (Vw32_swap_mouse_buttons))
6059 button = 2;
6060 else
6061 button = 1;
6062 up = 1;
6063 break;
6064 default:
6065 return (FALSE);
6068 if (pup) *pup = up;
6069 if (pbutton) *pbutton = button;
6071 return (TRUE);
6075 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6077 If the event is a button press, then note that we have grabbed
6078 the mouse. */
6080 static Lisp_Object
6081 construct_mouse_click (result, msg, f)
6082 struct input_event *result;
6083 W32Msg *msg;
6084 struct frame *f;
6086 int button;
6087 int up;
6089 parse_button (msg->msg.message, &button, &up);
6091 /* Make the event type no_event; we'll change that when we decide
6092 otherwise. */
6093 result->kind = mouse_click;
6094 result->code = button;
6095 result->timestamp = msg->msg.time;
6096 result->modifiers = (msg->dwModifiers
6097 | (up
6098 ? up_modifier
6099 : down_modifier));
6101 XSETINT (result->x, LOWORD (msg->msg.lParam));
6102 XSETINT (result->y, HIWORD (msg->msg.lParam));
6103 XSETFRAME (result->frame_or_window, f);
6104 result->arg = Qnil;
6105 return Qnil;
6108 static Lisp_Object
6109 construct_mouse_wheel (result, msg, f)
6110 struct input_event *result;
6111 W32Msg *msg;
6112 struct frame *f;
6114 POINT p;
6115 result->kind = mouse_wheel;
6116 result->code = (short) HIWORD (msg->msg.wParam);
6117 result->timestamp = msg->msg.time;
6118 result->modifiers = msg->dwModifiers;
6119 p.x = LOWORD (msg->msg.lParam);
6120 p.y = HIWORD (msg->msg.lParam);
6121 ScreenToClient (msg->msg.hwnd, &p);
6122 XSETINT (result->x, p.x);
6123 XSETINT (result->y, p.y);
6124 XSETFRAME (result->frame_or_window, f);
6125 result->arg = Qnil;
6126 return Qnil;
6129 static Lisp_Object
6130 construct_drag_n_drop (result, msg, f)
6131 struct input_event *result;
6132 W32Msg *msg;
6133 struct frame *f;
6135 Lisp_Object files;
6136 Lisp_Object frame;
6137 HDROP hdrop;
6138 POINT p;
6139 WORD num_files;
6140 char *name;
6141 int i, len;
6143 result->kind = drag_n_drop;
6144 result->code = 0;
6145 result->timestamp = msg->msg.time;
6146 result->modifiers = msg->dwModifiers;
6148 hdrop = (HDROP) msg->msg.wParam;
6149 DragQueryPoint (hdrop, &p);
6151 #if 0
6152 p.x = LOWORD (msg->msg.lParam);
6153 p.y = HIWORD (msg->msg.lParam);
6154 ScreenToClient (msg->msg.hwnd, &p);
6155 #endif
6157 XSETINT (result->x, p.x);
6158 XSETINT (result->y, p.y);
6160 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
6161 files = Qnil;
6163 for (i = 0; i < num_files; i++)
6165 len = DragQueryFile (hdrop, i, NULL, 0);
6166 if (len <= 0)
6167 continue;
6168 name = alloca (len + 1);
6169 DragQueryFile (hdrop, i, name, len + 1);
6170 files = Fcons (build_string (name), files);
6173 DragFinish (hdrop);
6175 XSETFRAME (frame, f);
6176 result->frame_or_window = Fcons (frame, files);
6177 result->arg = Qnil;
6178 return Qnil;
6182 /* Function to report a mouse movement to the mainstream Emacs code.
6183 The input handler calls this.
6185 We have received a mouse movement event, which is given in *event.
6186 If the mouse is over a different glyph than it was last time, tell
6187 the mainstream emacs code by setting mouse_moved. If not, ask for
6188 another motion event, so we can check again the next time it moves. */
6190 static MSG last_mouse_motion_event;
6191 static Lisp_Object last_mouse_motion_frame;
6193 static void
6194 note_mouse_movement (frame, msg)
6195 FRAME_PTR frame;
6196 MSG *msg;
6198 last_mouse_movement_time = msg->time;
6199 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
6200 XSETFRAME (last_mouse_motion_frame, frame);
6202 if (msg->hwnd != FRAME_W32_WINDOW (frame))
6204 frame->mouse_moved = 1;
6205 last_mouse_scroll_bar = Qnil;
6206 note_mouse_highlight (frame, -1, -1);
6209 /* Has the mouse moved off the glyph it was on at the last sighting? */
6210 else if (LOWORD (msg->lParam) < last_mouse_glyph.left
6211 || LOWORD (msg->lParam) > last_mouse_glyph.right
6212 || HIWORD (msg->lParam) < last_mouse_glyph.top
6213 || HIWORD (msg->lParam) > last_mouse_glyph.bottom)
6215 frame->mouse_moved = 1;
6216 last_mouse_scroll_bar = Qnil;
6217 note_mouse_highlight (frame, LOWORD (msg->lParam), HIWORD (msg->lParam));
6221 /* This is used for debugging, to turn off note_mouse_highlight. */
6223 int disable_mouse_highlight;
6227 /************************************************************************
6228 Mouse Face
6229 ************************************************************************/
6231 /* Find the glyph under window-relative coordinates X/Y in window W.
6232 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6233 strings. Return in *HPOS and *VPOS the row and column number of
6234 the glyph found. Return in *AREA the glyph area containing X.
6235 Value is a pointer to the glyph found or null if X/Y is not on
6236 text, or we can't tell because W's current matrix is not up to
6237 date. */
6239 static struct glyph *
6240 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6241 struct window *w;
6242 int x, y;
6243 int *hpos, *vpos, *area;
6245 struct glyph *glyph, *end;
6246 struct glyph_row *row = NULL;
6247 int x0, i, left_area_width;
6249 /* Find row containing Y. Give up if some row is not enabled. */
6250 for (i = 0; i < w->current_matrix->nrows; ++i)
6252 row = MATRIX_ROW (w->current_matrix, i);
6253 if (!row->enabled_p)
6254 return NULL;
6255 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6256 break;
6259 *vpos = i;
6260 *hpos = 0;
6262 /* Give up if Y is not in the window. */
6263 if (i == w->current_matrix->nrows)
6264 return NULL;
6266 /* Get the glyph area containing X. */
6267 if (w->pseudo_window_p)
6269 *area = TEXT_AREA;
6270 x0 = 0;
6272 else
6274 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6275 if (x < left_area_width)
6277 *area = LEFT_MARGIN_AREA;
6278 x0 = 0;
6280 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6282 *area = TEXT_AREA;
6283 x0 = row->x + left_area_width;
6285 else
6287 *area = RIGHT_MARGIN_AREA;
6288 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6292 /* Find glyph containing X. */
6293 glyph = row->glyphs[*area];
6294 end = glyph + row->used[*area];
6295 while (glyph < end)
6297 if (x < x0 + glyph->pixel_width)
6299 if (w->pseudo_window_p)
6300 break;
6301 else if (BUFFERP (glyph->object))
6302 break;
6305 x0 += glyph->pixel_width;
6306 ++glyph;
6309 if (glyph == end)
6310 return NULL;
6312 *hpos = glyph - row->glyphs[*area];
6313 return glyph;
6317 /* Convert frame-relative x/y to coordinates relative to window W.
6318 Takes pseudo-windows into account. */
6320 static void
6321 frame_to_window_pixel_xy (w, x, y)
6322 struct window *w;
6323 int *x, *y;
6325 if (w->pseudo_window_p)
6327 /* A pseudo-window is always full-width, and starts at the
6328 left edge of the frame, plus a frame border. */
6329 struct frame *f = XFRAME (w->frame);
6330 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6331 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6333 else
6335 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6336 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6341 /* Take proper action when mouse has moved to the mode or header line of
6342 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6343 mode line. X is relative to the start of the text display area of
6344 W, so the width of bitmap areas and scroll bars must be subtracted
6345 to get a position relative to the start of the mode line. */
6347 static void
6348 note_mode_line_highlight (w, x, mode_line_p)
6349 struct window *w;
6350 int x, mode_line_p;
6352 struct frame *f = XFRAME (w->frame);
6353 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6354 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6355 struct glyph_row *row;
6357 if (mode_line_p)
6358 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6359 else
6360 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6362 if (row->enabled_p)
6364 struct glyph *glyph, *end;
6365 Lisp_Object help, map;
6366 int x0;
6368 /* Find the glyph under X. */
6369 glyph = row->glyphs[TEXT_AREA];
6370 end = glyph + row->used[TEXT_AREA];
6371 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6372 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6374 while (glyph < end
6375 && x >= x0 + glyph->pixel_width)
6377 x0 += glyph->pixel_width;
6378 ++glyph;
6381 if (glyph < end
6382 && STRINGP (glyph->object)
6383 && XSTRING (glyph->object)->intervals
6384 && glyph->charpos >= 0
6385 && glyph->charpos < XSTRING (glyph->object)->size)
6387 /* If we're on a string with `help-echo' text property,
6388 arrange for the help to be displayed. This is done by
6389 setting the global variable help_echo to the help string. */
6390 help = Fget_text_property (make_number (glyph->charpos),
6391 Qhelp_echo, glyph->object);
6392 if (!NILP (help))
6394 help_echo = help;
6395 XSETWINDOW (help_echo_window, w);
6396 help_echo_object = glyph->object;
6397 help_echo_pos = glyph->charpos;
6400 /* Change the mouse pointer according to what is under X/Y. */
6401 map = Fget_text_property (make_number (glyph->charpos),
6402 Qlocal_map, glyph->object);
6403 if (KEYMAPP (map))
6404 cursor = f->output_data.w32->nontext_cursor;
6405 else
6407 map = Fget_text_property (make_number (glyph->charpos),
6408 Qkeymap, glyph->object);
6409 if (KEYMAPP (map))
6410 cursor = f->output_data.w32->nontext_cursor;
6415 #if 0 /* TODO: mouse cursor */
6416 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6417 #endif
6421 /* Take proper action when the mouse has moved to position X, Y on
6422 frame F as regards highlighting characters that have mouse-face
6423 properties. Also de-highlighting chars where the mouse was before.
6424 X and Y can be negative or out of range. */
6426 static void
6427 note_mouse_highlight (f, x, y)
6428 struct frame *f;
6429 int x, y;
6431 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6432 int portion;
6433 Lisp_Object window;
6434 struct window *w;
6436 /* When a menu is active, don't highlight because this looks odd. */
6437 if (popup_activated ())
6438 return;
6440 if (disable_mouse_highlight
6441 || !f->glyphs_initialized_p)
6442 return;
6444 dpyinfo->mouse_face_mouse_x = x;
6445 dpyinfo->mouse_face_mouse_y = y;
6446 dpyinfo->mouse_face_mouse_frame = f;
6448 if (dpyinfo->mouse_face_defer)
6449 return;
6451 if (gc_in_progress)
6453 dpyinfo->mouse_face_deferred_gc = 1;
6454 return;
6457 /* Which window is that in? */
6458 window = window_from_coordinates (f, x, y, &portion, 1);
6460 /* If we were displaying active text in another window, clear that. */
6461 if (! EQ (window, dpyinfo->mouse_face_window))
6462 clear_mouse_face (dpyinfo);
6464 /* Not on a window -> return. */
6465 if (!WINDOWP (window))
6466 return;
6468 /* Convert to window-relative pixel coordinates. */
6469 w = XWINDOW (window);
6470 frame_to_window_pixel_xy (w, &x, &y);
6472 /* Handle tool-bar window differently since it doesn't display a
6473 buffer. */
6474 if (EQ (window, f->tool_bar_window))
6476 note_tool_bar_highlight (f, x, y);
6477 return;
6480 if (portion == 1 || portion == 3)
6482 /* Mouse is on the mode or top line. */
6483 note_mode_line_highlight (w, x, portion == 1);
6484 return;
6486 #if 0 /* TODO: mouse cursor */
6487 else if (portion == 2)
6488 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6489 f->output_data.x->horizontal_drag_cursor);
6490 else
6491 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6492 f->output_data.x->text_cursor);
6493 #endif
6495 /* Are we in a window whose display is up to date?
6496 And verify the buffer's text has not changed. */
6497 if (/* Within text portion of the window. */
6498 portion == 0
6499 && EQ (w->window_end_valid, w->buffer)
6500 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6501 && (XFASTINT (w->last_overlay_modified)
6502 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6504 int hpos, vpos, pos, i, area;
6505 struct glyph *glyph;
6507 /* Find the glyph under X/Y. */
6508 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6510 /* Clear mouse face if X/Y not over text. */
6511 if (glyph == NULL
6512 || area != TEXT_AREA
6513 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6515 clear_mouse_face (dpyinfo);
6516 return;
6519 pos = glyph->charpos;
6520 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6522 /* Check for mouse-face and help-echo. */
6524 Lisp_Object mouse_face, overlay, position;
6525 Lisp_Object *overlay_vec;
6526 int len, noverlays;
6527 struct buffer *obuf;
6528 int obegv, ozv;
6530 /* If we get an out-of-range value, return now; avoid an error. */
6531 if (pos > BUF_Z (XBUFFER (w->buffer)))
6532 return;
6534 /* Make the window's buffer temporarily current for
6535 overlays_at and compute_char_face. */
6536 obuf = current_buffer;
6537 current_buffer = XBUFFER (w->buffer);
6538 obegv = BEGV;
6539 ozv = ZV;
6540 BEGV = BEG;
6541 ZV = Z;
6543 /* Is this char mouse-active or does it have help-echo? */
6544 XSETINT (position, pos);
6546 /* Put all the overlays we want in a vector in overlay_vec.
6547 Store the length in len. If there are more than 10, make
6548 enough space for all, and try again. */
6549 len = 10;
6550 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6551 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6552 if (noverlays > len)
6554 len = noverlays;
6555 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6556 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6559 /* Sort overlays into increasing priority order. */
6560 noverlays = sort_overlays (overlay_vec, noverlays, w);
6562 /* Check mouse-face highlighting. */
6563 if (! (EQ (window, dpyinfo->mouse_face_window)
6564 && vpos >= dpyinfo->mouse_face_beg_row
6565 && vpos <= dpyinfo->mouse_face_end_row
6566 && (vpos > dpyinfo->mouse_face_beg_row
6567 || hpos >= dpyinfo->mouse_face_beg_col)
6568 && (vpos < dpyinfo->mouse_face_end_row
6569 || hpos < dpyinfo->mouse_face_end_col
6570 || dpyinfo->mouse_face_past_end)))
6572 /* Clear the display of the old active region, if any. */
6573 clear_mouse_face (dpyinfo);
6575 /* Find the highest priority overlay that has a mouse-face prop. */
6576 overlay = Qnil;
6577 for (i = noverlays - 1; i >= 0; --i)
6579 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6580 if (!NILP (mouse_face))
6582 overlay = overlay_vec[i];
6583 break;
6587 /* If no overlay applies, get a text property. */
6588 if (NILP (overlay))
6589 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6591 /* Handle the overlay case. */
6592 if (! NILP (overlay))
6594 /* Find the range of text around this char that
6595 should be active. */
6596 Lisp_Object before, after;
6597 int ignore;
6599 before = Foverlay_start (overlay);
6600 after = Foverlay_end (overlay);
6601 /* Record this as the current active region. */
6602 fast_find_position (w, XFASTINT (before),
6603 &dpyinfo->mouse_face_beg_col,
6604 &dpyinfo->mouse_face_beg_row,
6605 &dpyinfo->mouse_face_beg_x,
6606 &dpyinfo->mouse_face_beg_y);
6607 dpyinfo->mouse_face_past_end
6608 = !fast_find_position (w, XFASTINT (after),
6609 &dpyinfo->mouse_face_end_col,
6610 &dpyinfo->mouse_face_end_row,
6611 &dpyinfo->mouse_face_end_x,
6612 &dpyinfo->mouse_face_end_y);
6613 dpyinfo->mouse_face_window = window;
6614 dpyinfo->mouse_face_face_id
6615 = face_at_buffer_position (w, pos, 0, 0,
6616 &ignore, pos + 1, 1);
6618 /* Display it as active. */
6619 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6621 /* Handle the text property case. */
6622 else if (! NILP (mouse_face))
6624 /* Find the range of text around this char that
6625 should be active. */
6626 Lisp_Object before, after, beginning, end;
6627 int ignore;
6629 beginning = Fmarker_position (w->start);
6630 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6631 - XFASTINT (w->window_end_pos)));
6632 before
6633 = Fprevious_single_property_change (make_number (pos + 1),
6634 Qmouse_face,
6635 w->buffer, beginning);
6636 after
6637 = Fnext_single_property_change (position, Qmouse_face,
6638 w->buffer, end);
6639 /* Record this as the current active region. */
6640 fast_find_position (w, XFASTINT (before),
6641 &dpyinfo->mouse_face_beg_col,
6642 &dpyinfo->mouse_face_beg_row,
6643 &dpyinfo->mouse_face_beg_x,
6644 &dpyinfo->mouse_face_beg_y);
6645 dpyinfo->mouse_face_past_end
6646 = !fast_find_position (w, XFASTINT (after),
6647 &dpyinfo->mouse_face_end_col,
6648 &dpyinfo->mouse_face_end_row,
6649 &dpyinfo->mouse_face_end_x,
6650 &dpyinfo->mouse_face_end_y);
6651 dpyinfo->mouse_face_window = window;
6652 dpyinfo->mouse_face_face_id
6653 = face_at_buffer_position (w, pos, 0, 0,
6654 &ignore, pos + 1, 1);
6656 /* Display it as active. */
6657 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6661 /* Look for a `help-echo' property. */
6663 Lisp_Object help, overlay;
6665 /* Check overlays first. */
6666 help = overlay = Qnil;
6667 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6669 overlay = overlay_vec[i];
6670 help = Foverlay_get (overlay, Qhelp_echo);
6673 if (!NILP (help))
6675 help_echo = help;
6676 help_echo_window = window;
6677 help_echo_object = overlay;
6678 help_echo_pos = pos;
6680 else
6682 /* Try text properties. */
6683 if ((STRINGP (glyph->object)
6684 && glyph->charpos >= 0
6685 && glyph->charpos < XSTRING (glyph->object)->size)
6686 || (BUFFERP (glyph->object)
6687 && glyph->charpos >= BEGV
6688 && glyph->charpos < ZV))
6689 help = Fget_text_property (make_number (glyph->charpos),
6690 Qhelp_echo, glyph->object);
6692 if (!NILP (help))
6694 help_echo = help;
6695 help_echo_window = window;
6696 help_echo_object = glyph->object;
6697 help_echo_pos = glyph->charpos;
6702 BEGV = obegv;
6703 ZV = ozv;
6704 current_buffer = obuf;
6709 static void
6710 redo_mouse_highlight ()
6712 if (!NILP (last_mouse_motion_frame)
6713 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6714 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6715 LOWORD (last_mouse_motion_event.lParam),
6716 HIWORD (last_mouse_motion_event.lParam));
6721 /***********************************************************************
6722 Tool-bars
6723 ***********************************************************************/
6725 static int x_tool_bar_item P_ ((struct frame *, int, int,
6726 struct glyph **, int *, int *, int *));
6728 /* Tool-bar item index of the item on which a mouse button was pressed
6729 or -1. */
6731 static int last_tool_bar_item;
6734 /* Get information about the tool-bar item at position X/Y on frame F.
6735 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6736 the current matrix of the tool-bar window of F, or NULL if not
6737 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6738 item in F->tool_bar_items. Value is
6740 -1 if X/Y is not on a tool-bar item
6741 0 if X/Y is on the same item that was highlighted before.
6742 1 otherwise. */
6744 static int
6745 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6746 struct frame *f;
6747 int x, y;
6748 struct glyph **glyph;
6749 int *hpos, *vpos, *prop_idx;
6751 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6752 struct window *w = XWINDOW (f->tool_bar_window);
6753 int area;
6755 /* Find the glyph under X/Y. */
6756 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6757 if (*glyph == NULL)
6758 return -1;
6760 /* Get the start of this tool-bar item's properties in
6761 f->tool_bar_items. */
6762 if (!tool_bar_item_info (f, *glyph, prop_idx))
6763 return -1;
6765 /* Is mouse on the highlighted item? */
6766 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6767 && *vpos >= dpyinfo->mouse_face_beg_row
6768 && *vpos <= dpyinfo->mouse_face_end_row
6769 && (*vpos > dpyinfo->mouse_face_beg_row
6770 || *hpos >= dpyinfo->mouse_face_beg_col)
6771 && (*vpos < dpyinfo->mouse_face_end_row
6772 || *hpos < dpyinfo->mouse_face_end_col
6773 || dpyinfo->mouse_face_past_end))
6774 return 0;
6776 return 1;
6780 /* Handle mouse button event on the tool-bar of frame F, at
6781 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6782 or ButtonRelase. */
6784 static void
6785 w32_handle_tool_bar_click (f, button_event)
6786 struct frame *f;
6787 struct input_event *button_event;
6789 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6790 struct window *w = XWINDOW (f->tool_bar_window);
6791 int hpos, vpos, prop_idx;
6792 struct glyph *glyph;
6793 Lisp_Object enabled_p;
6794 int x = XFASTINT (button_event->x);
6795 int y = XFASTINT (button_event->y);
6797 /* If not on the highlighted tool-bar item, return. */
6798 frame_to_window_pixel_xy (w, &x, &y);
6799 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6800 return;
6802 /* If item is disabled, do nothing. */
6803 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6804 if (NILP (enabled_p))
6805 return;
6807 if (button_event->kind == mouse_click)
6809 /* Show item in pressed state. */
6810 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6811 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6812 last_tool_bar_item = prop_idx;
6814 else
6816 Lisp_Object key, frame;
6817 struct input_event event;
6819 /* Show item in released state. */
6820 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6821 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6823 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
6825 XSETFRAME (frame, f);
6826 event.kind = TOOL_BAR_EVENT;
6827 event.frame_or_window = frame;
6828 event.arg = frame;
6829 kbd_buffer_store_event (&event);
6831 event.kind = TOOL_BAR_EVENT;
6832 event.frame_or_window = frame;
6833 event.arg = key;
6834 event.modifiers = button_event->modifiers;
6835 kbd_buffer_store_event (&event);
6836 last_tool_bar_item = -1;
6841 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6842 tool-bar window-relative coordinates X/Y. Called from
6843 note_mouse_highlight. */
6845 static void
6846 note_tool_bar_highlight (f, x, y)
6847 struct frame *f;
6848 int x, y;
6850 Lisp_Object window = f->tool_bar_window;
6851 struct window *w = XWINDOW (window);
6852 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6853 int hpos, vpos;
6854 struct glyph *glyph;
6855 struct glyph_row *row;
6856 int i;
6857 Lisp_Object enabled_p;
6858 int prop_idx;
6859 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6860 int mouse_down_p, rc;
6862 /* Function note_mouse_highlight is called with negative x(y
6863 values when mouse moves outside of the frame. */
6864 if (x <= 0 || y <= 0)
6866 clear_mouse_face (dpyinfo);
6867 return;
6870 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6871 if (rc < 0)
6873 /* Not on tool-bar item. */
6874 clear_mouse_face (dpyinfo);
6875 return;
6877 else if (rc == 0)
6878 /* On same tool-bar item as before. */
6879 goto set_help_echo;
6881 clear_mouse_face (dpyinfo);
6883 /* Mouse is down, but on different tool-bar item? */
6884 mouse_down_p = (dpyinfo->grabbed
6885 && f == last_mouse_frame
6886 && FRAME_LIVE_P (f));
6887 if (mouse_down_p
6888 && last_tool_bar_item != prop_idx)
6889 return;
6891 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6892 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6894 /* If tool-bar item is not enabled, don't highlight it. */
6895 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6896 if (!NILP (enabled_p))
6898 /* Compute the x-position of the glyph. In front and past the
6899 image is a space. We include this is the highlighted area. */
6900 row = MATRIX_ROW (w->current_matrix, vpos);
6901 for (i = x = 0; i < hpos; ++i)
6902 x += row->glyphs[TEXT_AREA][i].pixel_width;
6904 /* Record this as the current active region. */
6905 dpyinfo->mouse_face_beg_col = hpos;
6906 dpyinfo->mouse_face_beg_row = vpos;
6907 dpyinfo->mouse_face_beg_x = x;
6908 dpyinfo->mouse_face_beg_y = row->y;
6909 dpyinfo->mouse_face_past_end = 0;
6911 dpyinfo->mouse_face_end_col = hpos + 1;
6912 dpyinfo->mouse_face_end_row = vpos;
6913 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6914 dpyinfo->mouse_face_end_y = row->y;
6915 dpyinfo->mouse_face_window = window;
6916 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6918 /* Display it as active. */
6919 show_mouse_face (dpyinfo, draw);
6920 dpyinfo->mouse_face_image_state = draw;
6923 set_help_echo:
6925 /* Set help_echo to a help string.to display for this tool-bar item.
6926 w32_read_socket does the rest. */
6927 help_echo_object = help_echo_window = Qnil;
6928 help_echo_pos = -1;
6929 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
6930 if (NILP (help_echo))
6931 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
6936 /* Find the glyph matrix position of buffer position POS in window W.
6937 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6938 current glyphs must be up to date. If POS is above window start
6939 return (0, 0, 0, 0). If POS is after end of W, return end of
6940 last line in W. */
6942 static int
6943 fast_find_position (w, pos, hpos, vpos, x, y)
6944 struct window *w;
6945 int pos;
6946 int *hpos, *vpos, *x, *y;
6948 int i;
6949 int lastcol;
6950 int maybe_next_line_p = 0;
6951 int line_start_position;
6952 int yb = window_text_bottom_y (w);
6953 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6954 struct glyph_row *best_row = row;
6955 int row_vpos = 0, best_row_vpos = 0;
6956 int current_x;
6958 while (row->y < yb)
6960 if (row->used[TEXT_AREA])
6961 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6962 else
6963 line_start_position = 0;
6965 if (line_start_position > pos)
6966 break;
6967 /* If the position sought is the end of the buffer,
6968 don't include the blank lines at the bottom of the window. */
6969 else if (line_start_position == pos
6970 && pos == BUF_ZV (XBUFFER (w->buffer)))
6972 maybe_next_line_p = 1;
6973 break;
6975 else if (line_start_position > 0)
6977 best_row = row;
6978 best_row_vpos = row_vpos;
6981 if (row->y + row->height >= yb)
6982 break;
6984 ++row;
6985 ++row_vpos;
6988 /* Find the right column within BEST_ROW. */
6989 lastcol = 0;
6990 current_x = best_row->x;
6991 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6993 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6994 int charpos;
6996 charpos = glyph->charpos;
6997 if (charpos == pos)
6999 *hpos = i;
7000 *vpos = best_row_vpos;
7001 *x = current_x;
7002 *y = best_row->y;
7003 return 1;
7005 else if (charpos > pos)
7006 break;
7007 else if (charpos > 0)
7008 lastcol = i;
7010 current_x += glyph->pixel_width;
7013 /* If we're looking for the end of the buffer,
7014 and we didn't find it in the line we scanned,
7015 use the start of the following line. */
7016 if (maybe_next_line_p)
7018 ++best_row;
7019 ++best_row_vpos;
7020 lastcol = 0;
7021 current_x = best_row->x;
7024 *vpos = best_row_vpos;
7025 *hpos = lastcol + 1;
7026 *x = current_x;
7027 *y = best_row->y;
7028 return 0;
7032 /* Display the active region described by mouse_face_*
7033 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7035 static void
7036 show_mouse_face (dpyinfo, draw)
7037 struct w32_display_info *dpyinfo;
7038 enum draw_glyphs_face draw;
7040 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7041 struct frame *f = XFRAME (WINDOW_FRAME (w));
7042 int i;
7043 int cursor_off_p = 0;
7044 struct cursor_pos saved_cursor;
7046 saved_cursor = output_cursor;
7048 /* If window is in the process of being destroyed, don't bother
7049 to do anything. */
7050 if (w->current_matrix == NULL)
7051 goto set_x_cursor;
7053 /* Recognize when we are called to operate on rows that don't exist
7054 anymore. This can happen when a window is split. */
7055 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7056 goto set_x_cursor;
7058 set_output_cursor (&w->phys_cursor);
7060 /* Note that mouse_face_beg_row etc. are window relative. */
7061 for (i = dpyinfo->mouse_face_beg_row;
7062 i <= dpyinfo->mouse_face_end_row;
7063 i++)
7065 int start_hpos, end_hpos, start_x;
7066 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7068 /* Don't do anything if row doesn't have valid contents. */
7069 if (!row->enabled_p)
7070 continue;
7072 /* For all but the first row, the highlight starts at column 0. */
7073 if (i == dpyinfo->mouse_face_beg_row)
7075 start_hpos = dpyinfo->mouse_face_beg_col;
7076 start_x = dpyinfo->mouse_face_beg_x;
7078 else
7080 start_hpos = 0;
7081 start_x = 0;
7084 if (i == dpyinfo->mouse_face_end_row)
7085 end_hpos = dpyinfo->mouse_face_end_col;
7086 else
7087 end_hpos = row->used[TEXT_AREA];
7089 /* If the cursor's in the text we are about to rewrite, turn the
7090 cursor off. */
7091 if (!w->pseudo_window_p
7092 && i == output_cursor.vpos
7093 && output_cursor.hpos >= start_hpos - 1
7094 && output_cursor.hpos <= end_hpos)
7096 x_update_window_cursor (w, 0);
7097 cursor_off_p = 1;
7100 if (end_hpos > start_hpos)
7102 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7103 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7104 start_hpos, end_hpos, draw, NULL, NULL, 0);
7108 /* If we turned the cursor off, turn it back on. */
7109 if (cursor_off_p)
7110 x_display_cursor (w, 1,
7111 output_cursor.hpos, output_cursor.vpos,
7112 output_cursor.x, output_cursor.y);
7114 output_cursor = saved_cursor;
7116 set_x_cursor:
7117 #if 0 /* TODO: mouse cursor */
7118 /* Change the mouse cursor. */
7119 if (draw == DRAW_NORMAL_TEXT)
7120 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7121 f->output_data.x->text_cursor);
7122 else if (draw == DRAW_MOUSE_FACE)
7123 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7124 f->output_data.x->cross_cursor);
7125 else
7126 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7127 f->output_data.x->nontext_cursor);
7128 #endif
7132 /* Clear out the mouse-highlighted active region.
7133 Redraw it un-highlighted first. */
7135 void
7136 clear_mouse_face (dpyinfo)
7137 struct w32_display_info *dpyinfo;
7139 #if 0 /* This prevents redrawing tool bar items when changing from one
7140 to another while a tooltip is open, so don't do it. */
7141 if (!NILP (tip_frame))
7142 return;
7143 #endif
7145 if (! NILP (dpyinfo->mouse_face_window))
7146 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7148 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7149 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7150 dpyinfo->mouse_face_window = Qnil;
7154 /* Clear any mouse-face on window W. This function is part of the
7155 redisplay interface, and is called from try_window_id and similar
7156 functions to ensure the mouse-highlight is off. */
7158 static void
7159 x_clear_mouse_face (w)
7160 struct window *w;
7162 struct w32_display_info *dpyinfo
7163 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
7164 Lisp_Object window;
7166 BLOCK_INPUT;
7167 XSETWINDOW (window, w);
7168 if (EQ (window, dpyinfo->mouse_face_window))
7169 clear_mouse_face (dpyinfo);
7170 UNBLOCK_INPUT;
7174 /* Just discard the mouse face information for frame F, if any.
7175 This is used when the size of F is changed. */
7177 void
7178 cancel_mouse_face (f)
7179 FRAME_PTR f;
7181 Lisp_Object window;
7182 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7184 window = dpyinfo->mouse_face_window;
7185 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7187 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7188 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7189 dpyinfo->mouse_face_window = Qnil;
7193 static struct scroll_bar *x_window_to_scroll_bar ();
7194 static void x_scroll_bar_report_motion ();
7196 /* Return the current position of the mouse.
7197 *fp should be a frame which indicates which display to ask about.
7199 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7200 and *part to the frame, window, and scroll bar part that the mouse
7201 is over. Set *x and *y to the portion and whole of the mouse's
7202 position on the scroll bar.
7204 If the mouse movement started elsewhere, set *fp to the frame the
7205 mouse is on, *bar_window to nil, and *x and *y to the character cell
7206 the mouse is over.
7208 Set *time to the server time-stamp for the time at which the mouse
7209 was at this position.
7211 Don't store anything if we don't have a valid set of values to report.
7213 This clears the mouse_moved flag, so we can wait for the next mouse
7214 movement. */
7216 static void
7217 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
7218 FRAME_PTR *fp;
7219 int insist;
7220 Lisp_Object *bar_window;
7221 enum scroll_bar_part *part;
7222 Lisp_Object *x, *y;
7223 unsigned long *time;
7225 FRAME_PTR f1;
7227 BLOCK_INPUT;
7229 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7230 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7231 else
7233 POINT pt;
7235 Lisp_Object frame, tail;
7237 /* Clear the mouse-moved flag for every frame on this display. */
7238 FOR_EACH_FRAME (tail, frame)
7239 XFRAME (frame)->mouse_moved = 0;
7241 last_mouse_scroll_bar = Qnil;
7243 GetCursorPos (&pt);
7245 /* Now we have a position on the root; find the innermost window
7246 containing the pointer. */
7248 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7249 && FRAME_LIVE_P (last_mouse_frame))
7251 /* If mouse was grabbed on a frame, give coords for that frame
7252 even if the mouse is now outside it. */
7253 f1 = last_mouse_frame;
7255 else
7257 /* Is window under mouse one of our frames? */
7258 f1 = x_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
7259 WindowFromPoint (pt));
7262 /* If not, is it one of our scroll bars? */
7263 if (! f1)
7265 struct scroll_bar *bar
7266 = x_window_to_scroll_bar (WindowFromPoint (pt));
7268 if (bar)
7270 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7274 if (f1 == 0 && insist > 0)
7275 f1 = SELECTED_FRAME ();
7277 if (f1)
7279 /* Ok, we found a frame. Store all the values.
7280 last_mouse_glyph is a rectangle used to reduce the
7281 generation of mouse events. To not miss any motion
7282 events, we must divide the frame into rectangles of the
7283 size of the smallest character that could be displayed
7284 on it, i.e. into the same rectangles that matrices on
7285 the frame are divided into. */
7287 #if OLD_REDISPLAY_CODE
7288 int ignore1, ignore2;
7290 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7292 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
7293 &last_mouse_glyph,
7294 FRAME_W32_DISPLAY_INFO (f1)->grabbed
7295 || insist);
7296 #else
7297 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7299 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7300 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7301 int x = pt.x;
7302 int y = pt.y;
7304 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7305 round down even for negative values. */
7306 if (x < 0)
7307 x -= width - 1;
7308 if (y < 0)
7309 y -= height - 1;
7311 last_mouse_glyph.left = (x + width - 1) / width * width;
7312 last_mouse_glyph.top = (y + height - 1) / height * height;
7313 last_mouse_glyph.right = last_mouse_glyph.left + width;
7314 last_mouse_glyph.bottom = last_mouse_glyph.top + height;
7316 #endif
7318 *bar_window = Qnil;
7319 *part = 0;
7320 *fp = f1;
7321 XSETINT (*x, pt.x);
7322 XSETINT (*y, pt.y);
7323 *time = last_mouse_movement_time;
7328 UNBLOCK_INPUT;
7332 /* Scroll bar support. */
7334 /* Given a window ID, find the struct scroll_bar which manages it.
7335 This can be called in GC, so we have to make sure to strip off mark
7336 bits. */
7338 static struct scroll_bar *
7339 x_window_to_scroll_bar (window_id)
7340 Window window_id;
7342 Lisp_Object tail;
7344 for (tail = Vframe_list;
7345 XGCTYPE (tail) == Lisp_Cons;
7346 tail = XCDR (tail))
7348 Lisp_Object frame, bar, condemned;
7350 frame = XCAR (tail);
7351 /* All elements of Vframe_list should be frames. */
7352 if (! GC_FRAMEP (frame))
7353 abort ();
7355 /* Scan this frame's scroll bar list for a scroll bar with the
7356 right window ID. */
7357 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7358 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7359 /* This trick allows us to search both the ordinary and
7360 condemned scroll bar lists with one loop. */
7361 ! GC_NILP (bar) || (bar = condemned,
7362 condemned = Qnil,
7363 ! GC_NILP (bar));
7364 bar = XSCROLL_BAR (bar)->next)
7365 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
7366 return XSCROLL_BAR (bar);
7369 return 0;
7374 /* Set the thumb size and position of scroll bar BAR. We are currently
7375 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7377 static void
7378 w32_set_scroll_bar_thumb (bar, portion, position, whole)
7379 struct scroll_bar *bar;
7380 int portion, position, whole;
7382 Window w = SCROLL_BAR_W32_WINDOW (bar);
7383 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7384 int sb_page, sb_pos;
7385 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
7387 if (whole)
7389 /* Position scroll bar at rock bottom if the bottom of the
7390 buffer is visible. This avoids shinking the thumb away
7391 to nothing if it is held at the bottom of the buffer. */
7392 if (position + portion >= whole)
7394 sb_page = range * (whole - position) / whole
7395 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7396 sb_pos = range;
7399 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7400 sb_pos = position * range / whole;
7402 else
7404 sb_page = range;
7405 sb_pos = 0;
7408 BLOCK_INPUT;
7410 if (pfnSetScrollInfo)
7412 SCROLLINFO si;
7414 si.cbSize = sizeof (si);
7415 /* Only update page size if currently dragging, to reduce
7416 flicker effects. */
7417 if (draggingp)
7418 si.fMask = SIF_PAGE;
7419 else
7420 si.fMask = SIF_PAGE | SIF_POS;
7421 si.nPage = sb_page;
7422 si.nPos = sb_pos;
7424 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
7426 else
7427 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
7429 UNBLOCK_INPUT;
7433 /************************************************************************
7434 Scroll bars, general
7435 ************************************************************************/
7437 HWND
7438 my_create_scrollbar (f, bar)
7439 struct frame * f;
7440 struct scroll_bar * bar;
7442 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
7443 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
7444 (LPARAM) bar);
7447 //#define ATTACH_THREADS
7449 BOOL
7450 my_show_window (FRAME_PTR f, HWND hwnd, int how)
7452 #ifndef ATTACH_THREADS
7453 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
7454 (WPARAM) hwnd, (LPARAM) how);
7455 #else
7456 return ShowWindow (hwnd, how);
7457 #endif
7460 void
7461 my_set_window_pos (HWND hwnd, HWND hwndAfter,
7462 int x, int y, int cx, int cy, UINT flags)
7464 #ifndef ATTACH_THREADS
7465 WINDOWPOS pos;
7466 pos.hwndInsertAfter = hwndAfter;
7467 pos.x = x;
7468 pos.y = y;
7469 pos.cx = cx;
7470 pos.cy = cy;
7471 pos.flags = flags;
7472 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
7473 #else
7474 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
7475 #endif
7478 void
7479 my_set_focus (f, hwnd)
7480 struct frame * f;
7481 HWND hwnd;
7483 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
7484 (WPARAM) hwnd, 0);
7487 void
7488 my_set_foreground_window (hwnd)
7489 HWND hwnd;
7491 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
7494 void
7495 my_destroy_window (f, hwnd)
7496 struct frame * f;
7497 HWND hwnd;
7499 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
7500 (WPARAM) hwnd, 0);
7503 /* Create a scroll bar and return the scroll bar vector for it. W is
7504 the Emacs window on which to create the scroll bar. TOP, LEFT,
7505 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7506 scroll bar. */
7508 static struct scroll_bar *
7509 x_scroll_bar_create (w, top, left, width, height)
7510 struct window *w;
7511 int top, left, width, height;
7513 struct frame *f = XFRAME (WINDOW_FRAME (w));
7514 HWND hwnd;
7515 struct scroll_bar *bar
7516 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7518 BLOCK_INPUT;
7520 XSETWINDOW (bar->window, w);
7521 XSETINT (bar->top, top);
7522 XSETINT (bar->left, left);
7523 XSETINT (bar->width, width);
7524 XSETINT (bar->height, height);
7525 XSETINT (bar->start, 0);
7526 XSETINT (bar->end, 0);
7527 bar->dragging = Qnil;
7529 /* Requires geometry to be set before call to create the real window */
7531 hwnd = my_create_scrollbar (f, bar);
7533 if (pfnSetScrollInfo)
7535 SCROLLINFO si;
7537 si.cbSize = sizeof (si);
7538 si.fMask = SIF_ALL;
7539 si.nMin = 0;
7540 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7541 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7542 si.nPage = si.nMax;
7543 si.nPos = 0;
7545 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7547 else
7549 SetScrollRange (hwnd, SB_CTL, 0,
7550 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7551 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
7554 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
7556 /* Add bar to its frame's list of scroll bars. */
7557 bar->next = FRAME_SCROLL_BARS (f);
7558 bar->prev = Qnil;
7559 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7560 if (! NILP (bar->next))
7561 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7563 UNBLOCK_INPUT;
7565 return bar;
7569 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7570 nil. */
7572 static void
7573 x_scroll_bar_remove (bar)
7574 struct scroll_bar *bar;
7576 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7578 BLOCK_INPUT;
7580 /* Destroy the window. */
7581 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
7583 /* Disassociate this scroll bar from its window. */
7584 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
7586 UNBLOCK_INPUT;
7589 /* Set the handle of the vertical scroll bar for WINDOW to indicate
7590 that we are displaying PORTION characters out of a total of WHOLE
7591 characters, starting at POSITION. If WINDOW has no scroll bar,
7592 create one. */
7593 static void
7594 w32_set_vertical_scroll_bar (w, portion, whole, position)
7595 struct window *w;
7596 int portion, whole, position;
7598 struct frame *f = XFRAME (w->frame);
7599 struct scroll_bar *bar;
7600 int top, height, left, sb_left, width, sb_width;
7601 int window_x, window_y, window_width, window_height;
7603 /* Get window dimensions. */
7604 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7605 top = window_y;
7606 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7607 height = window_height;
7609 /* Compute the left edge of the scroll bar area. */
7610 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7611 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7612 else
7613 left = XFASTINT (w->left);
7614 left *= CANON_X_UNIT (f);
7615 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7617 /* Compute the width of the scroll bar which might be less than
7618 the width of the area reserved for the scroll bar. */
7619 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7620 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
7621 else
7622 sb_width = width;
7624 /* Compute the left edge of the scroll bar. */
7625 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7626 sb_left = left + width - sb_width - (width - sb_width) / 2;
7627 else
7628 sb_left = left + (width - sb_width) / 2;
7630 /* Does the scroll bar exist yet? */
7631 if (NILP (w->vertical_scroll_bar))
7633 HDC hdc;
7634 BLOCK_INPUT;
7635 hdc = get_frame_dc (f);
7636 w32_clear_area (f, hdc, left, top, width, height);
7637 release_frame_dc (f, hdc);
7638 UNBLOCK_INPUT;
7640 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
7642 else
7644 /* It may just need to be moved and resized. */
7645 HWND hwnd;
7647 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7648 hwnd = SCROLL_BAR_W32_WINDOW (bar);
7650 /* If already correctly positioned, do nothing. */
7651 if ( XINT (bar->left) == sb_left
7652 && XINT (bar->top) == top
7653 && XINT (bar->width) == sb_width
7654 && XINT (bar->height) == height )
7656 /* Redraw after clear_frame. */
7657 if (!my_show_window (f, hwnd, SW_NORMAL))
7658 InvalidateRect (hwnd, NULL, FALSE);
7660 else
7662 HDC hdc;
7663 BLOCK_INPUT;
7665 hdc = get_frame_dc (f);
7666 /* Since Windows scroll bars are smaller than the space reserved
7667 for them on the frame, we have to clear "under" them. */
7668 w32_clear_area (f, hdc,
7669 left,
7670 top,
7671 width,
7672 height);
7673 release_frame_dc (f, hdc);
7675 /* Make sure scroll bar is "visible" before moving, to ensure the
7676 area of the parent window now exposed will be refreshed. */
7677 my_show_window (f, hwnd, SW_HIDE);
7678 MoveWindow (hwnd, sb_left, top,
7679 sb_width, height, TRUE);
7680 if (pfnSetScrollInfo)
7682 SCROLLINFO si;
7684 si.cbSize = sizeof (si);
7685 si.fMask = SIF_RANGE;
7686 si.nMin = 0;
7687 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7688 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7690 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7692 else
7693 SetScrollRange (hwnd, SB_CTL, 0,
7694 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7695 my_show_window (f, hwnd, SW_NORMAL);
7696 // InvalidateRect (w, NULL, FALSE);
7698 /* Remember new settings. */
7699 XSETINT (bar->left, sb_left);
7700 XSETINT (bar->top, top);
7701 XSETINT (bar->width, sb_width);
7702 XSETINT (bar->height, height);
7704 UNBLOCK_INPUT;
7707 w32_set_scroll_bar_thumb (bar, portion, position, whole);
7709 XSETVECTOR (w->vertical_scroll_bar, bar);
7713 /* The following three hooks are used when we're doing a thorough
7714 redisplay of the frame. We don't explicitly know which scroll bars
7715 are going to be deleted, because keeping track of when windows go
7716 away is a real pain - "Can you say set-window-configuration, boys
7717 and girls?" Instead, we just assert at the beginning of redisplay
7718 that *all* scroll bars are to be removed, and then save a scroll bar
7719 from the fiery pit when we actually redisplay its window. */
7721 /* Arrange for all scroll bars on FRAME to be removed at the next call
7722 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
7723 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7725 static void
7726 w32_condemn_scroll_bars (frame)
7727 FRAME_PTR frame;
7729 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7730 while (! NILP (FRAME_SCROLL_BARS (frame)))
7732 Lisp_Object bar;
7733 bar = FRAME_SCROLL_BARS (frame);
7734 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7735 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7736 XSCROLL_BAR (bar)->prev = Qnil;
7737 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7738 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7739 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7744 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7745 Note that WINDOW isn't necessarily condemned at all. */
7747 static void
7748 w32_redeem_scroll_bar (window)
7749 struct window *window;
7751 struct scroll_bar *bar;
7752 struct frame *f;
7754 /* We can't redeem this window's scroll bar if it doesn't have one. */
7755 if (NILP (window->vertical_scroll_bar))
7756 abort ();
7758 bar = XSCROLL_BAR (window->vertical_scroll_bar);
7760 /* Unlink it from the condemned list. */
7761 f = XFRAME (WINDOW_FRAME (window));
7762 if (NILP (bar->prev))
7764 /* If the prev pointer is nil, it must be the first in one of
7765 the lists. */
7766 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
7767 /* It's not condemned. Everything's fine. */
7768 return;
7769 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7770 window->vertical_scroll_bar))
7771 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
7772 else
7773 /* If its prev pointer is nil, it must be at the front of
7774 one or the other! */
7775 abort ();
7777 else
7778 XSCROLL_BAR (bar->prev)->next = bar->next;
7780 if (! NILP (bar->next))
7781 XSCROLL_BAR (bar->next)->prev = bar->prev;
7783 bar->next = FRAME_SCROLL_BARS (f);
7784 bar->prev = Qnil;
7785 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7786 if (! NILP (bar->next))
7787 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7790 /* Remove all scroll bars on FRAME that haven't been saved since the
7791 last call to `*condemn_scroll_bars_hook'. */
7793 static void
7794 w32_judge_scroll_bars (f)
7795 FRAME_PTR f;
7797 Lisp_Object bar, next;
7799 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7801 /* Clear out the condemned list now so we won't try to process any
7802 more events on the hapless scroll bars. */
7803 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
7805 for (; ! NILP (bar); bar = next)
7807 struct scroll_bar *b = XSCROLL_BAR (bar);
7809 x_scroll_bar_remove (b);
7811 next = b->next;
7812 b->next = b->prev = Qnil;
7815 /* Now there should be no references to the condemned scroll bars,
7816 and they should get garbage-collected. */
7819 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7820 is set to something other than no_event, it is enqueued.
7822 This may be called from a signal handler, so we have to ignore GC
7823 mark bits. */
7825 static int
7826 w32_scroll_bar_handle_click (bar, msg, emacs_event)
7827 struct scroll_bar *bar;
7828 W32Msg *msg;
7829 struct input_event *emacs_event;
7831 if (! GC_WINDOWP (bar->window))
7832 abort ();
7834 emacs_event->kind = w32_scroll_bar_click;
7835 emacs_event->code = 0;
7836 /* not really meaningful to distinguish up/down */
7837 emacs_event->modifiers = msg->dwModifiers;
7838 emacs_event->frame_or_window = bar->window;
7839 emacs_event->arg = Qnil;
7840 emacs_event->timestamp = msg->msg.time;
7843 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7844 int y;
7845 int dragging = !NILP (bar->dragging);
7847 if (pfnGetScrollInfo)
7849 SCROLLINFO si;
7851 si.cbSize = sizeof (si);
7852 si.fMask = SIF_POS;
7854 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
7855 y = si.nPos;
7857 else
7858 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
7860 bar->dragging = Qnil;
7863 last_mouse_scroll_bar_pos = msg->msg.wParam;
7865 switch (LOWORD (msg->msg.wParam))
7867 case SB_LINEDOWN:
7868 emacs_event->part = scroll_bar_down_arrow;
7869 break;
7870 case SB_LINEUP:
7871 emacs_event->part = scroll_bar_up_arrow;
7872 break;
7873 case SB_PAGEUP:
7874 emacs_event->part = scroll_bar_above_handle;
7875 break;
7876 case SB_PAGEDOWN:
7877 emacs_event->part = scroll_bar_below_handle;
7878 break;
7879 case SB_TOP:
7880 emacs_event->part = scroll_bar_handle;
7881 y = 0;
7882 break;
7883 case SB_BOTTOM:
7884 emacs_event->part = scroll_bar_handle;
7885 y = top_range;
7886 break;
7887 case SB_THUMBTRACK:
7888 case SB_THUMBPOSITION:
7889 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7890 y = HIWORD (msg->msg.wParam);
7891 bar->dragging = Qt;
7892 emacs_event->part = scroll_bar_handle;
7894 /* "Silently" update current position. */
7895 if (pfnSetScrollInfo)
7897 SCROLLINFO si;
7899 si.cbSize = sizeof (si);
7900 si.fMask = SIF_POS;
7901 si.nPos = y;
7902 /* Remember apparent position (we actually lag behind the real
7903 position, so don't set that directly. */
7904 last_scroll_bar_drag_pos = y;
7906 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
7908 else
7909 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
7910 break;
7911 case SB_ENDSCROLL:
7912 /* If this is the end of a drag sequence, then reset the scroll
7913 handle size to normal and do a final redraw. Otherwise do
7914 nothing. */
7915 if (dragging)
7917 if (pfnSetScrollInfo)
7919 SCROLLINFO si;
7920 int start = XINT (bar->start);
7921 int end = XINT (bar->end);
7923 si.cbSize = sizeof (si);
7924 si.fMask = SIF_PAGE | SIF_POS;
7925 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7926 si.nPos = last_scroll_bar_drag_pos;
7927 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
7929 else
7930 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
7932 /* fall through */
7933 default:
7934 emacs_event->kind = no_event;
7935 return FALSE;
7938 XSETINT (emacs_event->x, y);
7939 XSETINT (emacs_event->y, top_range);
7941 return TRUE;
7945 /* Return information to the user about the current position of the mouse
7946 on the scroll bar. */
7948 static void
7949 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
7950 FRAME_PTR *fp;
7951 Lisp_Object *bar_window;
7952 enum scroll_bar_part *part;
7953 Lisp_Object *x, *y;
7954 unsigned long *time;
7956 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
7957 Window w = SCROLL_BAR_W32_WINDOW (bar);
7958 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7959 int pos;
7960 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7962 BLOCK_INPUT;
7964 *fp = f;
7965 *bar_window = bar->window;
7967 if (pfnGetScrollInfo)
7969 SCROLLINFO si;
7971 si.cbSize = sizeof (si);
7972 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
7974 pfnGetScrollInfo (w, SB_CTL, &si);
7975 pos = si.nPos;
7976 top_range = si.nMax - si.nPage + 1;
7978 else
7979 pos = GetScrollPos (w, SB_CTL);
7981 switch (LOWORD (last_mouse_scroll_bar_pos))
7983 case SB_THUMBPOSITION:
7984 case SB_THUMBTRACK:
7985 *part = scroll_bar_handle;
7986 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7987 pos = HIWORD (last_mouse_scroll_bar_pos);
7988 break;
7989 case SB_LINEDOWN:
7990 *part = scroll_bar_handle;
7991 pos++;
7992 break;
7993 default:
7994 *part = scroll_bar_handle;
7995 break;
7998 XSETINT (*x, pos);
7999 XSETINT (*y, top_range);
8001 f->mouse_moved = 0;
8002 last_mouse_scroll_bar = Qnil;
8004 *time = last_mouse_movement_time;
8006 UNBLOCK_INPUT;
8010 /* The screen has been cleared so we may have changed foreground or
8011 background colors, and the scroll bars may need to be redrawn.
8012 Clear out the scroll bars, and ask for expose events, so we can
8013 redraw them. */
8015 void
8016 x_scroll_bar_clear (f)
8017 FRAME_PTR f;
8019 Lisp_Object bar;
8021 /* We can have scroll bars even if this is 0,
8022 if we just turned off scroll bar mode.
8023 But in that case we should not clear them. */
8024 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8025 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8026 bar = XSCROLL_BAR (bar)->next)
8028 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
8029 HDC hdc = GetDC (window);
8030 RECT rect;
8032 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
8033 arranges to refresh the scroll bar if hidden. */
8034 my_show_window (f, window, SW_HIDE);
8036 GetClientRect (window, &rect);
8037 select_palette (f, hdc);
8038 w32_clear_rect (f, hdc, &rect);
8039 deselect_palette (f, hdc);
8041 ReleaseDC (window, hdc);
8046 /* The main W32 event-reading loop - w32_read_socket. */
8048 /* Time stamp of enter window event. This is only used by w32_read_socket,
8049 but we have to put it out here, since static variables within functions
8050 sometimes don't work. */
8052 static Time enter_timestamp;
8054 /* Record the last 100 characters stored
8055 to help debug the loss-of-chars-during-GC problem. */
8057 static int temp_index;
8058 static short temp_buffer[100];
8061 /* Read events coming from the W32 shell.
8062 This routine is called by the SIGIO handler.
8063 We return as soon as there are no more events to be read.
8065 Events representing keys are stored in buffer BUFP,
8066 which can hold up to NUMCHARS characters.
8067 We return the number of characters stored into the buffer,
8068 thus pretending to be `read'.
8070 EXPECTED is nonzero if the caller knows input is available.
8072 Some of these messages are reposted back to the message queue since the
8073 system calls the windows proc directly in a context where we cannot return
8074 the data nor can we guarantee the state we are in. So if we dispatch them
8075 we will get into an infinite loop. To prevent this from ever happening we
8076 will set a variable to indicate we are in the read_socket call and indicate
8077 which message we are processing since the windows proc gets called
8078 recursively with different messages by the system.
8082 w32_read_socket (sd, bufp, numchars, expected)
8083 register int sd;
8084 /* register */ struct input_event *bufp;
8085 /* register */ int numchars;
8086 int expected;
8088 int count = 0;
8089 int check_visibility = 0;
8090 W32Msg msg;
8091 struct frame *f;
8092 struct w32_display_info *dpyinfo = &one_w32_display_info;
8094 if (interrupt_input_blocked)
8096 interrupt_input_pending = 1;
8097 return -1;
8100 interrupt_input_pending = 0;
8101 BLOCK_INPUT;
8103 /* So people can tell when we have read the available input. */
8104 input_signal_count++;
8106 if (numchars <= 0)
8107 abort (); /* Don't think this happens. */
8109 /* TODO: tooltips, tool-bars, ghostscript integration, mouse
8110 cursors. */
8111 while (get_next_msg (&msg, FALSE))
8113 switch (msg.msg.message)
8115 case WM_PAINT:
8116 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8118 if (f)
8120 if (msg.rect.right == msg.rect.left ||
8121 msg.rect.bottom == msg.rect.top)
8123 /* We may get paint messages even though the client
8124 area is clipped - these are not expose events. */
8125 DebPrint (("clipped frame %04x (%s) got WM_PAINT\n", f,
8126 XSTRING (f->name)->data));
8128 else if (f->async_visible != 1)
8130 /* Definitely not obscured, so mark as visible. */
8131 f->async_visible = 1;
8132 f->async_iconified = 0;
8133 SET_FRAME_GARBAGED (f);
8134 DebPrint (("frame %04x (%s) reexposed\n", f,
8135 XSTRING (f->name)->data));
8137 /* WM_PAINT serves as MapNotify as well, so report
8138 visibility changes properly. */
8139 if (f->iconified)
8141 bufp->kind = deiconify_event;
8142 XSETFRAME (bufp->frame_or_window, f);
8143 bufp->arg = Qnil;
8144 bufp++;
8145 count++;
8146 numchars--;
8148 else if (! NILP (Vframe_list)
8149 && ! NILP (XCDR (Vframe_list)))
8150 /* Force a redisplay sooner or later to update the
8151 frame titles in case this is the second frame. */
8152 record_asynch_buffer_change ();
8154 else
8156 HDC hdc = get_frame_dc (f);
8158 /* Erase background again for safety. */
8159 w32_clear_rect (f, hdc, &msg.rect);
8160 release_frame_dc (f, hdc);
8161 expose_frame (f,
8162 msg.rect.left,
8163 msg.rect.top,
8164 msg.rect.right - msg.rect.left,
8165 msg.rect.bottom - msg.rect.top);
8168 break;
8170 case WM_INPUTLANGCHANGE:
8171 /* Generate a language change event. */
8172 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8174 if (f)
8176 if (numchars == 0)
8177 abort ();
8179 bufp->kind = language_change_event;
8180 XSETFRAME (bufp->frame_or_window, f);
8181 bufp->arg = Qnil;
8182 bufp->code = msg.msg.wParam;
8183 bufp->modifiers = msg.msg.lParam & 0xffff;
8184 bufp++;
8185 count++;
8186 numchars--;
8188 break;
8190 case WM_KEYDOWN:
8191 case WM_SYSKEYDOWN:
8192 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8194 if (f && !f->iconified)
8196 if (temp_index == sizeof temp_buffer / sizeof (short))
8197 temp_index = 0;
8198 temp_buffer[temp_index++] = msg.msg.wParam;
8199 bufp->kind = non_ascii_keystroke;
8200 bufp->code = msg.msg.wParam;
8201 bufp->modifiers = msg.dwModifiers;
8202 XSETFRAME (bufp->frame_or_window, f);
8203 bufp->arg = Qnil;
8204 bufp->timestamp = msg.msg.time;
8205 bufp++;
8206 numchars--;
8207 count++;
8209 break;
8211 case WM_SYSCHAR:
8212 case WM_CHAR:
8213 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8215 if (f && !f->iconified)
8217 if (temp_index == sizeof temp_buffer / sizeof (short))
8218 temp_index = 0;
8219 temp_buffer[temp_index++] = msg.msg.wParam;
8220 bufp->kind = ascii_keystroke;
8221 bufp->code = msg.msg.wParam;
8222 bufp->modifiers = msg.dwModifiers;
8223 XSETFRAME (bufp->frame_or_window, f);
8224 bufp->arg = Qnil;
8225 bufp->timestamp = msg.msg.time;
8226 bufp++;
8227 numchars--;
8228 count++;
8230 break;
8232 case WM_MOUSEMOVE:
8233 previous_help_echo = help_echo;
8234 help_echo = help_echo_object = help_echo_window = Qnil;
8235 help_echo_pos = -1;
8237 if (dpyinfo->grabbed && last_mouse_frame
8238 && FRAME_LIVE_P (last_mouse_frame))
8239 f = last_mouse_frame;
8240 else
8241 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8243 if (f)
8244 note_mouse_movement (f, &msg.msg);
8245 else
8247 /* If we move outside the frame, then we're
8248 certainly no longer on any text in the frame. */
8249 clear_mouse_face (dpyinfo);
8252 /* If the contents of the global variable help_echo
8253 has changed, generate a HELP_EVENT. */
8254 if (!NILP (help_echo)
8255 || !NILP (previous_help_echo))
8257 Lisp_Object frame;
8258 int n;
8260 if (f)
8261 XSETFRAME (frame, f);
8262 else
8263 frame = Qnil;
8265 any_help_event_p = 1;
8266 n = gen_help_event (bufp, numchars, help_echo, frame,
8267 help_echo_window, help_echo_object,
8268 help_echo_pos);
8269 bufp += n, count += n, numchars -= n;
8271 break;
8273 case WM_LBUTTONDOWN:
8274 case WM_LBUTTONUP:
8275 case WM_MBUTTONDOWN:
8276 case WM_MBUTTONUP:
8277 case WM_RBUTTONDOWN:
8278 case WM_RBUTTONUP:
8280 /* If we decide we want to generate an event to be seen
8281 by the rest of Emacs, we put it here. */
8282 struct input_event emacs_event;
8283 int tool_bar_p = 0;
8284 int button;
8285 int up;
8287 emacs_event.kind = no_event;
8289 if (dpyinfo->grabbed && last_mouse_frame
8290 && FRAME_LIVE_P (last_mouse_frame))
8291 f = last_mouse_frame;
8292 else
8293 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8295 if (f)
8297 construct_mouse_click (&emacs_event, &msg, f);
8299 /* Is this in the tool-bar? */
8300 if (WINDOWP (f->tool_bar_window)
8301 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
8303 Lisp_Object window;
8304 int p;
8306 /* Set x and y. */
8307 window = window_from_coordinates (f,
8308 emacs_event.x,
8309 emacs_event.y,
8310 &p, 1);
8311 if (EQ (window, f->tool_bar_window))
8313 w32_handle_tool_bar_click (f, &emacs_event);
8314 tool_bar_p = 1;
8318 if (!tool_bar_p)
8319 if (!dpyinfo->w32_focus_frame
8320 || f == dpyinfo->w32_focus_frame
8321 && (numchars >= 1))
8323 construct_mouse_click (bufp, &msg, f);
8324 bufp++;
8325 count++;
8326 numchars--;
8330 parse_button (msg.msg.message, &button, &up);
8332 if (up)
8334 dpyinfo->grabbed &= ~ (1 << button);
8336 else
8338 dpyinfo->grabbed |= (1 << button);
8339 last_mouse_frame = f;
8340 /* Ignore any mouse motion that happened
8341 before this event; any subsequent mouse-movement
8342 Emacs events should reflect only motion after
8343 the ButtonPress. */
8344 if (f != 0)
8345 f->mouse_moved = 0;
8347 if (!tool_bar_p)
8348 last_tool_bar_item = -1;
8350 break;
8353 case WM_MOUSEWHEEL:
8354 if (dpyinfo->grabbed && last_mouse_frame
8355 && FRAME_LIVE_P (last_mouse_frame))
8356 f = last_mouse_frame;
8357 else
8358 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8360 if (f)
8362 if ((!dpyinfo->w32_focus_frame
8363 || f == dpyinfo->w32_focus_frame)
8364 && (numchars >= 1))
8366 construct_mouse_wheel (bufp, &msg, f);
8367 bufp++;
8368 count++;
8369 numchars--;
8372 break;
8374 case WM_MENUSELECT:
8376 HMENU menu = (HMENU) msg.msg.lParam;
8377 UINT menu_item = (UINT) LOWORD (msg.msg.wParam);
8378 UINT flags = (UINT) HIWORD (msg.msg.wParam);
8380 w32_menu_display_help (menu, menu_item, flags);
8382 break;
8384 case WM_DROPFILES:
8385 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8387 if (f)
8389 construct_drag_n_drop (bufp, &msg, f);
8390 bufp++;
8391 count++;
8392 numchars--;
8394 break;
8396 case WM_VSCROLL:
8398 struct scroll_bar *bar =
8399 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
8401 if (bar && numchars >= 1)
8403 if (w32_scroll_bar_handle_click (bar, &msg, bufp))
8405 bufp++;
8406 count++;
8407 numchars--;
8410 break;
8413 case WM_WINDOWPOSCHANGED:
8414 case WM_ACTIVATE:
8415 case WM_ACTIVATEAPP:
8416 check_visibility = 1;
8417 break;
8419 case WM_MOVE:
8420 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8422 if (f && !f->async_iconified)
8424 int x, y;
8426 x_real_positions (f, &x, &y);
8427 f->output_data.w32->left_pos = x;
8428 f->output_data.w32->top_pos = y;
8431 check_visibility = 1;
8432 break;
8434 case WM_SHOWWINDOW:
8435 /* If window has been obscured or exposed by another window
8436 being maximised or minimised/restored, then recheck
8437 visibility of all frames. Direct changes to our own
8438 windows get handled by WM_SIZE. */
8439 #if 0
8440 if (msg.msg.lParam != 0)
8441 check_visibility = 1;
8442 else
8444 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8445 f->async_visible = msg.msg.wParam;
8447 #endif
8449 check_visibility = 1;
8450 break;
8452 case WM_SIZE:
8453 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8455 /* Inform lisp of whether frame has been iconified etc. */
8456 if (f)
8458 switch (msg.msg.wParam)
8460 case SIZE_MINIMIZED:
8461 f->async_visible = 0;
8462 f->async_iconified = 1;
8464 bufp->kind = iconify_event;
8465 XSETFRAME (bufp->frame_or_window, f);
8466 bufp->arg = Qnil;
8467 bufp++;
8468 count++;
8469 numchars--;
8470 break;
8472 case SIZE_MAXIMIZED:
8473 case SIZE_RESTORED:
8474 f->async_visible = 1;
8475 f->async_iconified = 0;
8477 /* wait_reading_process_input will notice this and update
8478 the frame's display structures. */
8479 SET_FRAME_GARBAGED (f);
8481 if (f->iconified)
8483 int x, y;
8485 /* Reset top and left positions of the Window
8486 here since Windows sends a WM_MOVE message
8487 BEFORE telling us the Window is minimized
8488 when the Window is iconified, with 3000,3000
8489 as the co-ords. */
8490 x_real_positions (f, &x, &y);
8491 f->output_data.w32->left_pos = x;
8492 f->output_data.w32->top_pos = y;
8494 bufp->kind = deiconify_event;
8495 XSETFRAME (bufp->frame_or_window, f);
8496 bufp->arg = Qnil;
8497 bufp++;
8498 count++;
8499 numchars--;
8501 else if (! NILP (Vframe_list)
8502 && ! NILP (XCDR (Vframe_list)))
8503 /* Force a redisplay sooner or later
8504 to update the frame titles
8505 in case this is the second frame. */
8506 record_asynch_buffer_change ();
8507 break;
8511 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
8513 RECT rect;
8514 int rows;
8515 int columns;
8516 int width;
8517 int height;
8519 GetClientRect (msg.msg.hwnd, &rect);
8521 height = rect.bottom - rect.top;
8522 width = rect.right - rect.left;
8524 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
8525 columns = PIXEL_TO_CHAR_WIDTH (f, width);
8527 /* TODO: Clip size to the screen dimensions. */
8529 /* Even if the number of character rows and columns has
8530 not changed, the font size may have changed, so we need
8531 to check the pixel dimensions as well. */
8533 if (columns != f->width
8534 || rows != f->height
8535 || width != f->output_data.w32->pixel_width
8536 || height != f->output_data.w32->pixel_height)
8538 change_frame_size (f, rows, columns, 0, 1, 0);
8539 SET_FRAME_GARBAGED (f);
8540 cancel_mouse_face (f);
8541 f->output_data.w32->pixel_width = width;
8542 f->output_data.w32->pixel_height = height;
8543 f->output_data.w32->win_gravity = NorthWestGravity;
8547 check_visibility = 1;
8548 break;
8550 case WM_SETFOCUS:
8551 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
8553 dpyinfo->w32_focus_event_frame = f;
8555 if (f)
8556 x_new_focus_frame (dpyinfo, f);
8559 dpyinfo->grabbed = 0;
8560 check_visibility = 1;
8561 break;
8563 case WM_KILLFOCUS:
8564 /* TODO: some of this belongs in MOUSE_LEAVE */
8565 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
8567 if (f)
8569 if (f == dpyinfo->w32_focus_event_frame)
8570 dpyinfo->w32_focus_event_frame = 0;
8572 if (f == dpyinfo->w32_focus_frame)
8573 x_new_focus_frame (dpyinfo, 0);
8575 if (f == dpyinfo->mouse_face_mouse_frame)
8577 /* If we move outside the frame, then we're
8578 certainly no longer on any text in the frame. */
8579 clear_mouse_face (dpyinfo);
8580 dpyinfo->mouse_face_mouse_frame = 0;
8583 /* Generate a nil HELP_EVENT to cancel a help-echo.
8584 Do it only if there's something to cancel.
8585 Otherwise, the startup message is cleared when
8586 the mouse leaves the frame. */
8587 if (any_help_event_p)
8589 Lisp_Object frame;
8590 int n;
8592 XSETFRAME (frame, f);
8593 help_echo = Qnil;
8594 n = gen_help_event (bufp, numchars,
8595 Qnil, frame, Qnil, Qnil, 0);
8596 bufp += n, count += n, numchars -=n;
8600 dpyinfo->grabbed = 0;
8601 check_visibility = 1;
8602 break;
8604 case WM_CLOSE:
8605 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8607 if (f)
8609 if (numchars == 0)
8610 abort ();
8612 bufp->kind = delete_window_event;
8613 XSETFRAME (bufp->frame_or_window, f);
8614 bufp->arg = Qnil;
8615 bufp++;
8616 count++;
8617 numchars--;
8619 break;
8621 case WM_INITMENU:
8622 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8624 if (f)
8626 if (numchars == 0)
8627 abort ();
8629 bufp->kind = menu_bar_activate_event;
8630 XSETFRAME (bufp->frame_or_window, f);
8631 bufp->arg = Qnil;
8632 bufp++;
8633 count++;
8634 numchars--;
8636 break;
8638 case WM_COMMAND:
8639 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8641 if (f)
8643 extern void menubar_selection_callback
8644 (FRAME_PTR f, void * client_data);
8645 menubar_selection_callback (f, (void *)msg.msg.wParam);
8648 check_visibility = 1;
8649 break;
8651 case WM_DISPLAYCHANGE:
8652 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8654 if (f)
8656 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
8657 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
8658 dpyinfo->n_cbits = msg.msg.wParam;
8659 DebPrint (("display change: %d %d\n", dpyinfo->width,
8660 dpyinfo->height));
8663 check_visibility = 1;
8664 break;
8666 default:
8667 /* Check for messages registered at runtime. */
8668 if (msg.msg.message == msh_mousewheel)
8670 if (dpyinfo->grabbed && last_mouse_frame
8671 && FRAME_LIVE_P (last_mouse_frame))
8672 f = last_mouse_frame;
8673 else
8674 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8676 if (f)
8678 if ((!dpyinfo->w32_focus_frame
8679 || f == dpyinfo->w32_focus_frame)
8680 && (numchars >= 1))
8682 construct_mouse_wheel (bufp, &msg, f);
8683 bufp++;
8684 count++;
8685 numchars--;
8689 break;
8693 /* If the focus was just given to an autoraising frame,
8694 raise it now. */
8695 /* ??? This ought to be able to handle more than one such frame. */
8696 if (pending_autoraise_frame)
8698 x_raise_frame (pending_autoraise_frame);
8699 pending_autoraise_frame = 0;
8702 /* Check which frames are still visisble, if we have enqueued any user
8703 events or been notified of events that may affect visibility. We
8704 do this here because there doesn't seem to be any direct
8705 notification from Windows that the visibility of a window has
8706 changed (at least, not in all cases). */
8707 if (count > 0 || check_visibility)
8709 Lisp_Object tail, frame;
8711 FOR_EACH_FRAME (tail, frame)
8713 FRAME_PTR f = XFRAME (frame);
8714 /* Check "visible" frames and mark each as obscured or not.
8715 Note that async_visible is nonzero for unobscured and
8716 obscured frames, but zero for hidden and iconified frames. */
8717 if (FRAME_W32_P (f) && f->async_visible)
8719 RECT clipbox;
8720 HDC hdc = get_frame_dc (f);
8721 GetClipBox (hdc, &clipbox);
8722 release_frame_dc (f, hdc);
8724 if (clipbox.right == clipbox.left
8725 || clipbox.bottom == clipbox.top)
8727 /* Frame has become completely obscured so mark as
8728 such (we do this by setting async_visible to 2 so
8729 that FRAME_VISIBLE_P is still true, but redisplay
8730 will skip it). */
8731 f->async_visible = 2;
8733 if (!FRAME_OBSCURED_P (f))
8735 DebPrint (("frame %04x (%s) obscured\n", f,
8736 XSTRING (f->name)->data));
8739 else
8741 /* Frame is not obscured, so mark it as such. */
8742 f->async_visible = 1;
8744 if (FRAME_OBSCURED_P (f))
8746 SET_FRAME_GARBAGED (f);
8747 DebPrint (("frame %04x (%s) reexposed\n", f,
8748 XSTRING (f->name)->data));
8750 /* Force a redisplay sooner or later. */
8751 record_asynch_buffer_change ();
8758 UNBLOCK_INPUT;
8759 return count;
8765 /***********************************************************************
8766 Text Cursor
8767 ***********************************************************************/
8769 /* Note if the text cursor of window W has been overwritten by a
8770 drawing operation that outputs N glyphs starting at HPOS in the
8771 line given by output_cursor.vpos. N < 0 means all the rest of the
8772 line after HPOS has been written. */
8774 static void
8775 note_overwritten_text_cursor (w, hpos, n)
8776 struct window *w;
8777 int hpos, n;
8779 if (updated_area == TEXT_AREA
8780 && output_cursor.vpos == w->phys_cursor.vpos
8781 && hpos <= w->phys_cursor.hpos
8782 && (n < 0
8783 || hpos + n > w->phys_cursor.hpos))
8784 w->phys_cursor_on_p = 0;
8788 /* Set clipping for output in glyph row ROW. W is the window in which
8789 we operate. GC is the graphics context to set clipping in.
8790 WHOLE_LINE_P non-zero means include the areas used for truncation
8791 mark display and alike in the clipping rectangle.
8793 ROW may be a text row or, e.g., a mode line. Text rows must be
8794 clipped to the interior of the window dedicated to text display,
8795 mode lines must be clipped to the whole window. */
8797 static void
8798 w32_clip_to_row (w, row, hdc, whole_line_p)
8799 struct window *w;
8800 struct glyph_row *row;
8801 HDC hdc;
8802 int whole_line_p;
8804 struct frame *f = XFRAME (WINDOW_FRAME (w));
8805 RECT clip_rect;
8806 int window_x, window_y, window_width, window_height;
8808 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8810 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
8811 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8812 clip_rect.top = max (clip_rect.top, window_y);
8813 clip_rect.right = clip_rect.left + window_width;
8814 clip_rect.bottom = clip_rect.top + row->visible_height;
8816 /* If clipping to the whole line, including trunc marks, extend
8817 the rectangle to the left and increase its width. */
8818 if (whole_line_p)
8820 clip_rect.left -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
8821 clip_rect.right += FRAME_X_FLAGS_AREA_WIDTH (f);
8824 w32_set_clip_rectangle (hdc, &clip_rect);
8828 /* Draw a hollow box cursor on window W in glyph row ROW. */
8830 static void
8831 x_draw_hollow_cursor (w, row)
8832 struct window *w;
8833 struct glyph_row *row;
8835 struct frame *f = XFRAME (WINDOW_FRAME (w));
8836 HDC hdc;
8837 RECT rect;
8838 int wd;
8839 struct glyph *cursor_glyph;
8840 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
8842 /* Compute frame-relative coordinates from window-relative
8843 coordinates. */
8844 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8845 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
8846 + row->ascent - w->phys_cursor_ascent);
8847 rect.bottom = rect.top + row->height - 1;
8849 /* Get the glyph the cursor is on. If we can't tell because
8850 the current matrix is invalid or such, give up. */
8851 cursor_glyph = get_phys_cursor_glyph (w);
8852 if (cursor_glyph == NULL)
8853 return;
8855 /* Compute the width of the rectangle to draw. If on a stretch
8856 glyph, and `x-stretch-block-cursor' is nil, don't draw a
8857 rectangle as wide as the glyph, but use a canonical character
8858 width instead. */
8859 wd = cursor_glyph->pixel_width - 1;
8860 if (cursor_glyph->type == STRETCH_GLYPH
8861 && !x_stretch_cursor_p)
8862 wd = min (CANON_X_UNIT (f), wd);
8864 rect.right = rect.left + wd;
8865 hdc = get_frame_dc (f);
8866 FrameRect (hdc, &rect, hb);
8867 DeleteObject (hb);
8869 release_frame_dc (f, hdc);
8873 /* Draw a bar cursor on window W in glyph row ROW.
8875 Implementation note: One would like to draw a bar cursor with an
8876 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8877 Unfortunately, I didn't find a font yet that has this property set.
8878 --gerd. */
8880 static void
8881 x_draw_bar_cursor (w, row, width)
8882 struct window *w;
8883 struct glyph_row *row;
8884 int width;
8886 struct frame *f = XFRAME (w->frame);
8887 struct glyph *cursor_glyph;
8888 int x;
8889 HDC hdc;
8891 /* If cursor is out of bounds, don't draw garbage. This can happen
8892 in mini-buffer windows when switching between echo area glyphs
8893 and mini-buffer. */
8894 cursor_glyph = get_phys_cursor_glyph (w);
8895 if (cursor_glyph == NULL)
8896 return;
8898 /* If on an image, draw like a normal cursor. That's usually better
8899 visible than drawing a bar, esp. if the image is large so that
8900 the bar might not be in the window. */
8901 if (cursor_glyph->type == IMAGE_GLYPH)
8903 struct glyph_row *row;
8904 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8905 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
8907 else
8909 if (width < 0)
8910 width = f->output_data.w32->cursor_width;
8912 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8913 hdc = get_frame_dc (f);
8914 w32_clip_to_row (w, row, hdc, 0);
8915 w32_fill_area (f, hdc, f->output_data.w32->cursor_pixel,
8917 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8918 min (cursor_glyph->pixel_width, width),
8919 row->height);
8920 release_frame_dc (f, hdc);
8925 /* Clear the cursor of window W to background color, and mark the
8926 cursor as not shown. This is used when the text where the cursor
8927 is is about to be rewritten. */
8929 static void
8930 x_clear_cursor (w)
8931 struct window *w;
8933 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
8934 x_update_window_cursor (w, 0);
8938 /* Draw the cursor glyph of window W in glyph row ROW. See the
8939 comment of x_draw_glyphs for the meaning of HL. */
8941 static void
8942 x_draw_phys_cursor_glyph (w, row, hl)
8943 struct window *w;
8944 struct glyph_row *row;
8945 enum draw_glyphs_face hl;
8947 /* If cursor hpos is out of bounds, don't draw garbage. This can
8948 happen in mini-buffer windows when switching between echo area
8949 glyphs and mini-buffer. */
8950 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
8952 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
8953 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
8954 hl, 0, 0, 0);
8956 /* When we erase the cursor, and ROW is overlapped by other
8957 rows, make sure that these overlapping parts of other rows
8958 are redrawn. */
8959 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
8961 if (row > w->current_matrix->rows
8962 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
8963 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
8965 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
8966 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
8967 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
8973 /* Erase the image of a cursor of window W from the screen. */
8975 static void
8976 x_erase_phys_cursor (w)
8977 struct window *w;
8979 struct frame *f = XFRAME (w->frame);
8980 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8981 int hpos = w->phys_cursor.hpos;
8982 int vpos = w->phys_cursor.vpos;
8983 int mouse_face_here_p = 0;
8984 struct glyph_matrix *active_glyphs = w->current_matrix;
8985 struct glyph_row *cursor_row;
8986 struct glyph *cursor_glyph;
8987 enum draw_glyphs_face hl;
8989 /* No cursor displayed or row invalidated => nothing to do on the
8990 screen. */
8991 if (w->phys_cursor_type == NO_CURSOR)
8992 goto mark_cursor_off;
8994 /* VPOS >= active_glyphs->nrows means that window has been resized.
8995 Don't bother to erase the cursor. */
8996 if (vpos >= active_glyphs->nrows)
8997 goto mark_cursor_off;
8999 /* If row containing cursor is marked invalid, there is nothing we
9000 can do. */
9001 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9002 if (!cursor_row->enabled_p)
9003 goto mark_cursor_off;
9005 /* This can happen when the new row is shorter than the old one.
9006 In this case, either x_draw_glyphs or clear_end_of_line
9007 should have cleared the cursor. Note that we wouldn't be
9008 able to erase the cursor in this case because we don't have a
9009 cursor glyph at hand. */
9010 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9011 goto mark_cursor_off;
9013 /* If the cursor is in the mouse face area, redisplay that when
9014 we clear the cursor. */
9015 if (! NILP (dpyinfo->mouse_face_window)
9016 && w == XWINDOW (dpyinfo->mouse_face_window)
9017 && (vpos > dpyinfo->mouse_face_beg_row
9018 || (vpos == dpyinfo->mouse_face_beg_row
9019 && hpos >= dpyinfo->mouse_face_beg_col))
9020 && (vpos < dpyinfo->mouse_face_end_row
9021 || (vpos == dpyinfo->mouse_face_end_row
9022 && hpos < dpyinfo->mouse_face_end_col))
9023 /* Don't redraw the cursor's spot in mouse face if it is at the
9024 end of a line (on a newline). The cursor appears there, but
9025 mouse highlighting does not. */
9026 && cursor_row->used[TEXT_AREA] > hpos)
9027 mouse_face_here_p = 1;
9029 /* Maybe clear the display under the cursor. */
9030 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
9032 int x;
9033 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9034 HDC hdc;
9036 cursor_glyph = get_phys_cursor_glyph (w);
9037 if (cursor_glyph == NULL)
9038 goto mark_cursor_off;
9040 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9042 hdc = get_frame_dc (f);
9043 w32_clear_area (f, hdc, x,
9044 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9045 cursor_row->y)),
9046 cursor_glyph->pixel_width,
9047 cursor_row->visible_height);
9048 release_frame_dc (f, hdc);
9051 /* Erase the cursor by redrawing the character underneath it. */
9052 if (mouse_face_here_p)
9053 hl = DRAW_MOUSE_FACE;
9054 else if (cursor_row->inverse_p)
9055 hl = DRAW_INVERSE_VIDEO;
9056 else
9057 hl = DRAW_NORMAL_TEXT;
9058 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9060 mark_cursor_off:
9061 w->phys_cursor_on_p = 0;
9062 w->phys_cursor_type = NO_CURSOR;
9066 /* Display or clear cursor of window W. If ON is zero, clear the
9067 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9068 where to put the cursor is specified by HPOS, VPOS, X and Y. */
9070 void
9071 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9072 struct window *w;
9073 int on, hpos, vpos, x, y;
9075 struct frame *f = XFRAME (w->frame);
9076 int new_cursor_type;
9077 int new_cursor_width;
9078 struct glyph_matrix *current_glyphs;
9079 struct glyph_row *glyph_row;
9080 struct glyph *glyph;
9082 /* This is pointless on invisible frames, and dangerous on garbaged
9083 windows and frames; in the latter case, the frame or window may
9084 be in the midst of changing its size, and x and y may be off the
9085 window. */
9086 if (! FRAME_VISIBLE_P (f)
9087 || FRAME_GARBAGED_P (f)
9088 || vpos >= w->current_matrix->nrows
9089 || hpos >= w->current_matrix->matrix_w)
9090 return;
9092 /* If cursor is off and we want it off, return quickly. */
9093 if (!on && !w->phys_cursor_on_p)
9094 return;
9096 current_glyphs = w->current_matrix;
9097 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9098 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9100 /* If cursor row is not enabled, we don't really know where to
9101 display the cursor. */
9102 if (!glyph_row->enabled_p)
9104 w->phys_cursor_on_p = 0;
9105 return;
9108 xassert (interrupt_input_blocked);
9110 /* Set new_cursor_type to the cursor we want to be displayed. In a
9111 mini-buffer window, we want the cursor only to appear if we are
9112 reading input from this window. For the selected window, we want
9113 the cursor type given by the frame parameter. If explicitly
9114 marked off, draw no cursor. In all other cases, we want a hollow
9115 box cursor. */
9116 new_cursor_width = -1;
9117 if (cursor_in_echo_area
9118 && FRAME_HAS_MINIBUF_P (f)
9119 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
9121 if (w == XWINDOW (echo_area_window))
9122 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9123 else
9124 new_cursor_type = HOLLOW_BOX_CURSOR;
9126 else
9128 if (f != FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame
9129 || w != XWINDOW (f->selected_window))
9131 extern int cursor_in_non_selected_windows;
9133 if (MINI_WINDOW_P (w)
9134 || !cursor_in_non_selected_windows
9135 || NILP (XBUFFER (w->buffer)->cursor_type))
9136 new_cursor_type = NO_CURSOR;
9137 else
9138 new_cursor_type = HOLLOW_BOX_CURSOR;
9140 else if (w->cursor_off_p)
9141 new_cursor_type = NO_CURSOR;
9142 else
9144 struct buffer *b = XBUFFER (w->buffer);
9146 if (EQ (b->cursor_type, Qt))
9147 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9148 else
9149 new_cursor_type = x_specified_cursor_type (b->cursor_type,
9150 &new_cursor_width);
9154 /* If cursor is currently being shown and we don't want it to be or
9155 it is in the wrong place, or the cursor type is not what we want,
9156 erase it. */
9157 if (w->phys_cursor_on_p
9158 && (!on
9159 || w->phys_cursor.x != x
9160 || w->phys_cursor.y != y
9161 || new_cursor_type != w->phys_cursor_type))
9162 x_erase_phys_cursor (w);
9164 /* If the cursor is now invisible and we want it to be visible,
9165 display it. */
9166 if (on && !w->phys_cursor_on_p)
9168 w->phys_cursor_ascent = glyph_row->ascent;
9169 w->phys_cursor_height = glyph_row->height;
9171 /* Set phys_cursor_.* before x_draw_.* is called because some
9172 of them may need the information. */
9173 w->phys_cursor.x = x;
9174 w->phys_cursor.y = glyph_row->y;
9175 w->phys_cursor.hpos = hpos;
9176 w->phys_cursor.vpos = vpos;
9177 w->phys_cursor_type = new_cursor_type;
9178 w->phys_cursor_on_p = 1;
9180 switch (new_cursor_type)
9182 case HOLLOW_BOX_CURSOR:
9183 x_draw_hollow_cursor (w, glyph_row);
9184 break;
9186 case FILLED_BOX_CURSOR:
9187 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9188 break;
9190 case BAR_CURSOR:
9191 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
9192 break;
9194 case NO_CURSOR:
9195 break;
9197 default:
9198 abort ();
9204 /* Display the cursor on window W, or clear it. X and Y are window
9205 relative pixel coordinates. HPOS and VPOS are glyph matrix
9206 positions. If W is not the selected window, display a hollow
9207 cursor. ON non-zero means display the cursor at X, Y which
9208 correspond to HPOS, VPOS, otherwise it is cleared. */
9210 void
9211 x_display_cursor (w, on, hpos, vpos, x, y)
9212 struct window *w;
9213 int on, hpos, vpos, x, y;
9215 BLOCK_INPUT;
9216 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9217 UNBLOCK_INPUT;
9221 /* Display the cursor on window W, or clear it, according to ON_P.
9222 Don't change the cursor's position. */
9224 void
9225 x_update_cursor (f, on_p)
9226 struct frame *f;
9227 int on_p;
9229 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9233 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
9234 in the window tree rooted at W. */
9236 static void
9237 x_update_cursor_in_window_tree (w, on_p)
9238 struct window *w;
9239 int on_p;
9241 while (w)
9243 if (!NILP (w->hchild))
9244 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9245 else if (!NILP (w->vchild))
9246 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9247 else
9248 x_update_window_cursor (w, on_p);
9250 w = NILP (w->next) ? 0 : XWINDOW (w->next);
9255 /* Switch the display of W's cursor on or off, according to the value
9256 of ON. */
9258 static void
9259 x_update_window_cursor (w, on)
9260 struct window *w;
9261 int on;
9263 /* Don't update cursor in windows whose frame is in the process
9264 of being deleted. */
9265 if (w->current_matrix)
9267 BLOCK_INPUT;
9268 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9269 w->phys_cursor.vpos, w->phys_cursor.x,
9270 w->phys_cursor.y);
9271 UNBLOCK_INPUT;
9278 /* Icons. */
9281 x_bitmap_icon (f, icon)
9282 struct frame *f;
9283 Lisp_Object icon;
9285 HANDLE hicon;
9287 if (FRAME_W32_WINDOW (f) == 0)
9288 return 1;
9290 if (NILP (icon))
9291 hicon = LoadIcon (hinst, EMACS_CLASS);
9292 else if (STRINGP (icon))
9293 hicon = LoadImage (NULL, (LPCTSTR) XSTRING (icon)->data, IMAGE_ICON, 0, 0,
9294 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9295 else if (SYMBOLP (icon))
9297 LPCTSTR name;
9299 if (EQ (icon, intern ("application")))
9300 name = (LPCTSTR) IDI_APPLICATION;
9301 else if (EQ (icon, intern ("hand")))
9302 name = (LPCTSTR) IDI_HAND;
9303 else if (EQ (icon, intern ("question")))
9304 name = (LPCTSTR) IDI_QUESTION;
9305 else if (EQ (icon, intern ("exclamation")))
9306 name = (LPCTSTR) IDI_EXCLAMATION;
9307 else if (EQ (icon, intern ("asterisk")))
9308 name = (LPCTSTR) IDI_ASTERISK;
9309 else if (EQ (icon, intern ("winlogo")))
9310 name = (LPCTSTR) IDI_WINLOGO;
9311 else
9312 return 1;
9314 hicon = LoadIcon (NULL, name);
9316 else
9317 return 1;
9319 if (hicon == NULL)
9320 return 1;
9322 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
9323 (LPARAM) hicon);
9325 return 0;
9329 /************************************************************************
9330 Handling X errors
9331 ************************************************************************/
9333 /* Display Error Handling functions not used on W32. Listing them here
9334 helps diff stay in step when comparing w32term.c with xterm.c.
9336 x_error_catcher (display, error)
9337 x_catch_errors (dpy)
9338 x_catch_errors_unwind (old_val)
9339 x_check_errors (dpy, format)
9340 x_had_errors_p (dpy)
9341 x_clear_errors (dpy)
9342 x_uncatch_errors (dpy, count)
9343 x_trace_wire ()
9344 x_connection_signal (signalnum)
9345 x_connection_closed (dpy, error_message)
9346 x_error_quitter (display, error)
9347 x_error_handler (display, error)
9348 x_io_error_quitter (display)
9353 /* Changing the font of the frame. */
9355 /* Give frame F the font named FONTNAME as its default font, and
9356 return the full name of that font. FONTNAME may be a wildcard
9357 pattern; in that case, we choose some font that fits the pattern.
9358 The return value shows which font we chose. */
9360 Lisp_Object
9361 x_new_font (f, fontname)
9362 struct frame *f;
9363 register char *fontname;
9365 struct font_info *fontp
9366 = FS_LOAD_FONT (f, 0, fontname, -1);
9368 if (!fontp)
9369 return Qnil;
9371 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
9372 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
9373 FRAME_FONTSET (f) = -1;
9375 /* Compute the scroll bar width in character columns. */
9376 if (f->scroll_bar_pixel_width > 0)
9378 int wid = FONT_WIDTH (FRAME_FONT (f));
9379 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
9381 else
9383 int wid = FONT_WIDTH (FRAME_FONT (f));
9384 f->scroll_bar_cols = (14 + wid - 1) / wid;
9387 /* Now make the frame display the given font. */
9388 if (FRAME_W32_WINDOW (f) != 0)
9390 frame_update_line_height (f);
9391 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9392 x_set_window_size (f, 0, f->width, f->height);
9394 else
9395 /* If we are setting a new frame's font for the first time,
9396 there are no faces yet, so this font's height is the line height. */
9397 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
9399 return build_string (fontp->full_name);
9402 /* Give frame F the fontset named FONTSETNAME as its default font, and
9403 return the full name of that fontset. FONTSETNAME may be a wildcard
9404 pattern; in that case, we choose some fontset that fits the pattern.
9405 The return value shows which fontset we chose. */
9407 Lisp_Object
9408 x_new_fontset (f, fontsetname)
9409 struct frame *f;
9410 char *fontsetname;
9412 int fontset = fs_query_fontset (build_string (fontsetname), 0);
9413 Lisp_Object result;
9415 if (fontset < 0)
9416 return Qnil;
9418 if (FRAME_FONTSET (f) == fontset)
9419 /* This fontset is already set in frame F. There's nothing more
9420 to do. */
9421 return fontset_name (fontset);
9423 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
9425 if (!STRINGP (result))
9426 /* Can't load ASCII font. */
9427 return Qnil;
9429 /* Since x_new_font doesn't update any fontset information, do it now. */
9430 FRAME_FONTSET(f) = fontset;
9432 return build_string (fontsetname);
9436 /***********************************************************************
9437 TODO: W32 Input Methods
9438 ***********************************************************************/
9439 /* Listing missing functions from xterm.c helps diff stay in step.
9441 xim_destroy_callback (xim, client_data, call_data)
9442 xim_open_dpy (dpyinfo, resource_name)
9443 struct xim_inst_t
9444 xim_instantiate_callback (display, client_data, call_data)
9445 xim_initialize (dpyinfo, resource_name)
9446 xim_close_dpy (dpyinfo)
9451 /* Calculate the absolute position in frame F
9452 from its current recorded position values and gravity. */
9454 void
9455 x_calc_absolute_position (f)
9456 struct frame *f;
9458 POINT pt;
9459 int flags = f->output_data.w32->size_hint_flags;
9461 pt.x = pt.y = 0;
9463 /* Find the position of the outside upper-left corner of
9464 the inner window, with respect to the outer window.
9465 But do this only if we will need the results. */
9466 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
9468 BLOCK_INPUT;
9469 MapWindowPoints (FRAME_W32_WINDOW (f),
9470 f->output_data.w32->parent_desc,
9471 &pt, 1);
9472 UNBLOCK_INPUT;
9476 RECT rt;
9477 rt.left = rt.right = rt.top = rt.bottom = 0;
9479 BLOCK_INPUT;
9480 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
9481 FRAME_EXTERNAL_MENU_BAR (f));
9482 UNBLOCK_INPUT;
9484 pt.x += (rt.right - rt.left);
9485 pt.y += (rt.bottom - rt.top);
9488 /* Treat negative positions as relative to the leftmost bottommost
9489 position that fits on the screen. */
9490 if (flags & XNegative)
9491 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
9492 - 2 * f->output_data.w32->border_width - pt.x
9493 - PIXEL_WIDTH (f)
9494 + f->output_data.w32->left_pos);
9496 if (flags & YNegative)
9497 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
9498 - 2 * f->output_data.w32->border_width - pt.y
9499 - PIXEL_HEIGHT (f)
9500 + f->output_data.w32->top_pos);
9501 /* The left_pos and top_pos
9502 are now relative to the top and left screen edges,
9503 so the flags should correspond. */
9504 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
9507 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9508 to really change the position, and 0 when calling from
9509 x_make_frame_visible (in that case, XOFF and YOFF are the current
9510 position values). It is -1 when calling from x_set_frame_parameters,
9511 which means, do adjust for borders but don't change the gravity. */
9513 void
9514 x_set_offset (f, xoff, yoff, change_gravity)
9515 struct frame *f;
9516 register int xoff, yoff;
9517 int change_gravity;
9519 int modified_top, modified_left;
9521 if (change_gravity > 0)
9523 f->output_data.w32->top_pos = yoff;
9524 f->output_data.w32->left_pos = xoff;
9525 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
9526 if (xoff < 0)
9527 f->output_data.w32->size_hint_flags |= XNegative;
9528 if (yoff < 0)
9529 f->output_data.w32->size_hint_flags |= YNegative;
9530 f->output_data.w32->win_gravity = NorthWestGravity;
9532 x_calc_absolute_position (f);
9534 BLOCK_INPUT;
9535 x_wm_set_size_hint (f, (long) 0, 0);
9537 modified_left = f->output_data.w32->left_pos;
9538 modified_top = f->output_data.w32->top_pos;
9540 my_set_window_pos (FRAME_W32_WINDOW (f),
9541 NULL,
9542 modified_left, modified_top,
9543 0, 0,
9544 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
9545 UNBLOCK_INPUT;
9548 /* Call this to change the size of frame F's x-window.
9549 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9550 for this size change and subsequent size changes.
9551 Otherwise we leave the window gravity unchanged. */
9553 void
9554 x_set_window_size (f, change_gravity, cols, rows)
9555 struct frame *f;
9556 int change_gravity;
9557 int cols, rows;
9559 int pixelwidth, pixelheight;
9561 BLOCK_INPUT;
9563 check_frame_size (f, &rows, &cols);
9564 f->output_data.w32->vertical_scroll_bar_extra
9565 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9567 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
9568 f->output_data.w32->flags_areas_extra
9569 = FRAME_FLAGS_AREA_WIDTH (f);
9570 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
9571 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
9573 f->output_data.w32->win_gravity = NorthWestGravity;
9574 x_wm_set_size_hint (f, (long) 0, 0);
9577 RECT rect;
9579 rect.left = rect.top = 0;
9580 rect.right = pixelwidth;
9581 rect.bottom = pixelheight;
9583 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
9584 FRAME_EXTERNAL_MENU_BAR (f));
9586 my_set_window_pos (FRAME_W32_WINDOW (f),
9587 NULL,
9588 0, 0,
9589 rect.right - rect.left,
9590 rect.bottom - rect.top,
9591 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
9594 /* Now, strictly speaking, we can't be sure that this is accurate,
9595 but the window manager will get around to dealing with the size
9596 change request eventually, and we'll hear how it went when the
9597 ConfigureNotify event gets here.
9599 We could just not bother storing any of this information here,
9600 and let the ConfigureNotify event set everything up, but that
9601 might be kind of confusing to the Lisp code, since size changes
9602 wouldn't be reported in the frame parameters until some random
9603 point in the future when the ConfigureNotify event arrives.
9605 We pass 1 for DELAY since we can't run Lisp code inside of
9606 a BLOCK_INPUT. */
9607 change_frame_size (f, rows, cols, 0, 1, 0);
9608 PIXEL_WIDTH (f) = pixelwidth;
9609 PIXEL_HEIGHT (f) = pixelheight;
9611 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9612 receive in the ConfigureNotify event; if we get what we asked
9613 for, then the event won't cause the screen to become garbaged, so
9614 we have to make sure to do it here. */
9615 SET_FRAME_GARBAGED (f);
9617 /* If cursor was outside the new size, mark it as off. */
9618 mark_window_cursors_off (XWINDOW (f->root_window));
9620 /* Clear out any recollection of where the mouse highlighting was,
9621 since it might be in a place that's outside the new frame size.
9622 Actually checking whether it is outside is a pain in the neck,
9623 so don't try--just let the highlighting be done afresh with new size. */
9624 cancel_mouse_face (f);
9626 UNBLOCK_INPUT;
9629 /* Mouse warping. */
9631 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
9633 void
9634 x_set_mouse_position (f, x, y)
9635 struct frame *f;
9636 int x, y;
9638 int pix_x, pix_y;
9640 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
9641 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
9643 if (pix_x < 0) pix_x = 0;
9644 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
9646 if (pix_y < 0) pix_y = 0;
9647 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
9649 x_set_mouse_pixel_position (f, pix_x, pix_y);
9652 void
9653 x_set_mouse_pixel_position (f, pix_x, pix_y)
9654 struct frame *f;
9655 int pix_x, pix_y;
9657 RECT rect;
9658 POINT pt;
9660 BLOCK_INPUT;
9662 GetClientRect (FRAME_W32_WINDOW (f), &rect);
9663 pt.x = rect.left + pix_x;
9664 pt.y = rect.top + pix_y;
9665 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
9667 SetCursorPos (pt.x, pt.y);
9669 UNBLOCK_INPUT;
9673 /* focus shifting, raising and lowering. */
9675 void
9676 x_focus_on_frame (f)
9677 struct frame *f;
9679 struct w32_display_info *dpyinfo = &one_w32_display_info;
9681 /* Give input focus to frame. */
9682 BLOCK_INPUT;
9683 #if 0
9684 /* Try not to change its Z-order if possible. */
9685 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
9686 my_set_focus (f, FRAME_W32_WINDOW (f));
9687 else
9688 #endif
9689 my_set_foreground_window (FRAME_W32_WINDOW (f));
9690 UNBLOCK_INPUT;
9693 void
9694 x_unfocus_frame (f)
9695 struct frame *f;
9699 /* Raise frame F. */
9700 void
9701 x_raise_frame (f)
9702 struct frame *f;
9704 BLOCK_INPUT;
9706 /* Strictly speaking, raise-frame should only change the frame's Z
9707 order, leaving input focus unchanged. This is reasonable behaviour
9708 on X where the usual policy is point-to-focus. However, this
9709 behaviour would be very odd on Windows where the usual policy is
9710 click-to-focus.
9712 On X, if the mouse happens to be over the raised frame, it gets
9713 input focus anyway (so the window with focus will never be
9714 completely obscured) - if not, then just moving the mouse over it
9715 is sufficient to give it focus. On Windows, the user must actually
9716 click on the frame (preferrably the title bar so as not to move
9717 point), which is more awkward. Also, no other Windows program
9718 raises a window to the top but leaves another window (possibly now
9719 completely obscured) with input focus.
9721 Because there is a system setting on Windows that allows the user
9722 to choose the point to focus policy, we make the strict semantics
9723 optional, but by default we grab focus when raising. */
9725 if (NILP (Vw32_grab_focus_on_raise))
9727 /* The obvious call to my_set_window_pos doesn't work if Emacs is
9728 not already the foreground application: the frame is raised
9729 above all other frames belonging to us, but not above the
9730 current top window. To achieve that, we have to resort to this
9731 more cumbersome method. */
9733 HDWP handle = BeginDeferWindowPos (2);
9734 if (handle)
9736 DeferWindowPos (handle,
9737 FRAME_W32_WINDOW (f),
9738 HWND_TOP,
9739 0, 0, 0, 0,
9740 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9742 DeferWindowPos (handle,
9743 GetForegroundWindow (),
9744 FRAME_W32_WINDOW (f),
9745 0, 0, 0, 0,
9746 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9748 EndDeferWindowPos (handle);
9751 else
9753 my_set_foreground_window (FRAME_W32_WINDOW (f));
9756 UNBLOCK_INPUT;
9759 /* Lower frame F. */
9760 void
9761 x_lower_frame (f)
9762 struct frame *f;
9764 BLOCK_INPUT;
9765 my_set_window_pos (FRAME_W32_WINDOW (f),
9766 HWND_BOTTOM,
9767 0, 0, 0, 0,
9768 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9769 UNBLOCK_INPUT;
9772 static void
9773 w32_frame_raise_lower (f, raise_flag)
9774 FRAME_PTR f;
9775 int raise_flag;
9777 if (! FRAME_W32_P (f))
9778 return;
9780 if (raise_flag)
9781 x_raise_frame (f);
9782 else
9783 x_lower_frame (f);
9786 /* Change of visibility. */
9788 /* This tries to wait until the frame is really visible.
9789 However, if the window manager asks the user where to position
9790 the frame, this will return before the user finishes doing that.
9791 The frame will not actually be visible at that time,
9792 but it will become visible later when the window manager
9793 finishes with it. */
9795 void
9796 x_make_frame_visible (f)
9797 struct frame *f;
9799 Lisp_Object type;
9801 BLOCK_INPUT;
9803 type = x_icon_type (f);
9804 if (!NILP (type))
9805 x_bitmap_icon (f, type);
9807 if (! FRAME_VISIBLE_P (f))
9809 /* We test FRAME_GARBAGED_P here to make sure we don't
9810 call x_set_offset a second time
9811 if we get to x_make_frame_visible a second time
9812 before the window gets really visible. */
9813 if (! FRAME_ICONIFIED_P (f)
9814 && ! f->output_data.w32->asked_for_visible)
9815 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
9817 f->output_data.w32->asked_for_visible = 1;
9819 // my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW);
9820 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
9823 /* Synchronize to ensure Emacs knows the frame is visible
9824 before we do anything else. We do this loop with input not blocked
9825 so that incoming events are handled. */
9827 Lisp_Object frame;
9828 int count;
9830 /* This must come after we set COUNT. */
9831 UNBLOCK_INPUT;
9833 XSETFRAME (frame, f);
9835 /* Wait until the frame is visible. Process X events until a
9836 MapNotify event has been seen, or until we think we won't get a
9837 MapNotify at all.. */
9838 for (count = input_signal_count + 10;
9839 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9841 /* Force processing of queued events. */
9842 /* TODO: x_sync equivalent? */
9844 /* Machines that do polling rather than SIGIO have been observed
9845 to go into a busy-wait here. So we'll fake an alarm signal
9846 to let the handler know that there's something to be read.
9847 We used to raise a real alarm, but it seems that the handler
9848 isn't always enabled here. This is probably a bug. */
9849 if (input_polling_used ())
9851 /* It could be confusing if a real alarm arrives while processing
9852 the fake one. Turn it off and let the handler reset it. */
9853 int old_poll_suppress_count = poll_suppress_count;
9854 poll_suppress_count = 1;
9855 poll_for_input_1 ();
9856 poll_suppress_count = old_poll_suppress_count;
9859 FRAME_SAMPLE_VISIBILITY (f);
9863 /* Change from mapped state to withdrawn state. */
9865 /* Make the frame visible (mapped and not iconified). */
9867 x_make_frame_invisible (f)
9868 struct frame *f;
9870 /* Don't keep the highlight on an invisible frame. */
9871 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9872 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
9874 BLOCK_INPUT;
9876 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
9878 /* We can't distinguish this from iconification
9879 just by the event that we get from the server.
9880 So we can't win using the usual strategy of letting
9881 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9882 and synchronize with the server to make sure we agree. */
9883 f->visible = 0;
9884 FRAME_ICONIFIED_P (f) = 0;
9885 f->async_visible = 0;
9886 f->async_iconified = 0;
9888 UNBLOCK_INPUT;
9891 /* Change window state from mapped to iconified. */
9893 void
9894 x_iconify_frame (f)
9895 struct frame *f;
9897 Lisp_Object type;
9899 /* Don't keep the highlight on an invisible frame. */
9900 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9901 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
9903 if (f->async_iconified)
9904 return;
9906 BLOCK_INPUT;
9908 type = x_icon_type (f);
9909 if (!NILP (type))
9910 x_bitmap_icon (f, type);
9912 /* Simulate the user minimizing the frame. */
9913 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
9915 UNBLOCK_INPUT;
9919 /* Free X resources of frame F. */
9921 void
9922 x_free_frame_resources (f)
9923 struct frame *f;
9925 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9927 BLOCK_INPUT;
9929 if (FRAME_W32_WINDOW (f))
9930 my_destroy_window (f, FRAME_W32_WINDOW (f));
9932 free_frame_menubar (f);
9934 unload_color (f, f->output_data.x->foreground_pixel);
9935 unload_color (f, f->output_data.x->background_pixel);
9936 unload_color (f, f->output_data.w32->cursor_pixel);
9937 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
9938 unload_color (f, f->output_data.w32->border_pixel);
9939 unload_color (f, f->output_data.w32->mouse_pixel);
9940 if (f->output_data.w32->white_relief.allocated_p)
9941 unload_color (f, f->output_data.w32->white_relief.pixel);
9942 if (f->output_data.w32->black_relief.allocated_p)
9943 unload_color (f, f->output_data.w32->black_relief.pixel);
9945 if (FRAME_FACE_CACHE (f))
9946 free_frame_faces (f);
9948 xfree (f->output_data.w32);
9949 f->output_data.w32 = NULL;
9951 if (f == dpyinfo->w32_focus_frame)
9952 dpyinfo->w32_focus_frame = 0;
9953 if (f == dpyinfo->w32_focus_event_frame)
9954 dpyinfo->w32_focus_event_frame = 0;
9955 if (f == dpyinfo->w32_highlight_frame)
9956 dpyinfo->w32_highlight_frame = 0;
9958 if (f == dpyinfo->mouse_face_mouse_frame)
9960 dpyinfo->mouse_face_beg_row
9961 = dpyinfo->mouse_face_beg_col = -1;
9962 dpyinfo->mouse_face_end_row
9963 = dpyinfo->mouse_face_end_col = -1;
9964 dpyinfo->mouse_face_window = Qnil;
9965 dpyinfo->mouse_face_deferred_gc = 0;
9966 dpyinfo->mouse_face_mouse_frame = 0;
9969 UNBLOCK_INPUT;
9973 /* Destroy the window of frame F. */
9975 x_destroy_window (f)
9976 struct frame *f;
9978 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9980 x_free_frame_resources (f);
9982 dpyinfo->reference_count--;
9986 /* Setting window manager hints. */
9988 /* Set the normal size hints for the window manager, for frame F.
9989 FLAGS is the flags word to use--or 0 meaning preserve the flags
9990 that the window now has.
9991 If USER_POSITION is nonzero, we set the USPosition
9992 flag (this is useful when FLAGS is 0). */
9993 void
9994 x_wm_set_size_hint (f, flags, user_position)
9995 struct frame *f;
9996 long flags;
9997 int user_position;
9999 Window window = FRAME_W32_WINDOW (f);
10001 enter_crit ();
10003 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
10004 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
10005 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
10006 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
10008 leave_crit ();
10011 /* Window manager things */
10012 x_wm_set_icon_position (f, icon_x, icon_y)
10013 struct frame *f;
10014 int icon_x, icon_y;
10016 #if 0
10017 Window window = FRAME_W32_WINDOW (f);
10019 f->display.x->wm_hints.flags |= IconPositionHint;
10020 f->display.x->wm_hints.icon_x = icon_x;
10021 f->display.x->wm_hints.icon_y = icon_y;
10023 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
10024 #endif
10028 /***********************************************************************
10029 Fonts
10030 ***********************************************************************/
10032 /* The following functions are listed here to help diff stay in step
10033 with xterm.c. See w32fns.c for definitions.
10035 x_get_font_info (f, font_idx)
10036 x_list_fonts (f, pattern, size, maxnames)
10040 #if GLYPH_DEBUG
10042 /* Check that FONT is valid on frame F. It is if it can be found in F's
10043 font table. */
10045 static void
10046 x_check_font (f, font)
10047 struct frame *f;
10048 XFontStruct *font;
10050 int i;
10051 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10053 xassert (font != NULL);
10055 for (i = 0; i < dpyinfo->n_fonts; i++)
10056 if (dpyinfo->font_table[i].name
10057 && font == dpyinfo->font_table[i].font)
10058 break;
10060 xassert (i < dpyinfo->n_fonts);
10063 #endif /* GLYPH_DEBUG != 0 */
10065 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10066 Note: There are (broken) X fonts out there with invalid XFontStruct
10067 min_bounds contents. For example, handa@etl.go.jp reports that
10068 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10069 have font->min_bounds.width == 0. */
10071 static INLINE void
10072 x_font_min_bounds (font, w, h)
10073 XFontStruct *font;
10074 int *w, *h;
10077 * TODO: Windows does not appear to offer min bound, only
10078 * average and maximum width, and maximum height.
10080 *h = FONT_HEIGHT (font);
10081 *w = FONT_WIDTH (font);
10085 /* Compute the smallest character width and smallest font height over
10086 all fonts available on frame F. Set the members smallest_char_width
10087 and smallest_font_height in F's x_display_info structure to
10088 the values computed. Value is non-zero if smallest_font_height or
10089 smallest_char_width become smaller than they were before. */
10092 x_compute_min_glyph_bounds (f)
10093 struct frame *f;
10095 int i;
10096 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10097 XFontStruct *font;
10098 int old_width = dpyinfo->smallest_char_width;
10099 int old_height = dpyinfo->smallest_font_height;
10101 dpyinfo->smallest_font_height = 100000;
10102 dpyinfo->smallest_char_width = 100000;
10104 for (i = 0; i < dpyinfo->n_fonts; ++i)
10105 if (dpyinfo->font_table[i].name)
10107 struct font_info *fontp = dpyinfo->font_table + i;
10108 int w, h;
10110 font = (XFontStruct *) fontp->font;
10111 xassert (font != (XFontStruct *) ~0);
10112 x_font_min_bounds (font, &w, &h);
10114 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10115 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10118 xassert (dpyinfo->smallest_char_width > 0
10119 && dpyinfo->smallest_font_height > 0);
10121 return (dpyinfo->n_fonts == 1
10122 || dpyinfo->smallest_char_width < old_width
10123 || dpyinfo->smallest_font_height < old_height);
10126 /* The following functions are listed here to help diff stay in step
10127 with xterm.c. See w32fns.c for definitions.
10129 x_load_font (f, fontname, size)
10130 x_query_font (f, fontname)
10131 x_find_ccl_program (fontp)
10135 /***********************************************************************
10136 Initialization
10137 ***********************************************************************/
10139 static int w32_initialized = 0;
10141 void
10142 w32_initialize_display_info (display_name)
10143 Lisp_Object display_name;
10145 struct w32_display_info *dpyinfo = &one_w32_display_info;
10147 bzero (dpyinfo, sizeof (*dpyinfo));
10149 /* Put it on w32_display_name_list. */
10150 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
10151 w32_display_name_list);
10152 dpyinfo->name_list_element = XCAR (w32_display_name_list);
10154 dpyinfo->w32_id_name
10155 = (char *) xmalloc (XSTRING (Vinvocation_name)->size
10156 + XSTRING (Vsystem_name)->size
10157 + 2);
10158 sprintf (dpyinfo->w32_id_name, "%s@%s",
10159 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
10161 /* Default Console mode values - overridden when running in GUI mode
10162 with values obtained from system metrics. */
10163 dpyinfo->resx = 1;
10164 dpyinfo->resy = 1;
10165 dpyinfo->height_in = 1;
10166 dpyinfo->width_in = 1;
10167 dpyinfo->n_planes = 1;
10168 dpyinfo->n_cbits = 4;
10169 dpyinfo->n_fonts = 0;
10170 dpyinfo->smallest_font_height = 1;
10171 dpyinfo->smallest_char_width = 1;
10173 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10174 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10175 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10176 dpyinfo->mouse_face_window = Qnil;
10178 /* TODO: dpyinfo->gray */
10182 struct w32_display_info *
10183 w32_term_init (display_name, xrm_option, resource_name)
10184 Lisp_Object display_name;
10185 char *xrm_option;
10186 char *resource_name;
10188 struct w32_display_info *dpyinfo;
10189 HDC hdc;
10191 BLOCK_INPUT;
10193 if (!w32_initialized)
10195 w32_initialize ();
10196 w32_initialized = 1;
10200 int argc = 0;
10201 char *argv[3];
10203 argv[0] = "";
10204 argc = 1;
10205 if (xrm_option)
10207 argv[argc++] = "-xrm";
10208 argv[argc++] = xrm_option;
10212 w32_initialize_display_info (display_name);
10214 dpyinfo = &one_w32_display_info;
10216 /* Put this display on the chain. */
10217 dpyinfo->next = x_display_list;
10218 x_display_list = dpyinfo;
10220 hdc = GetDC (GetDesktopWindow ());
10222 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
10223 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
10224 dpyinfo->root_window = GetDesktopWindow ();
10225 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
10226 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
10227 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
10228 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
10229 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
10230 dpyinfo->image_cache = make_image_cache ();
10231 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
10232 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
10233 ReleaseDC (GetDesktopWindow (), hdc);
10235 /* initialise palette with white and black */
10237 COLORREF color;
10238 w32_defined_color (0, "white", &color, 1);
10239 w32_defined_color (0, "black", &color, 1);
10242 /* Create Row Bitmaps and store them for later use. */
10243 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
10244 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
10245 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
10246 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
10247 1, continued_bits);
10248 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
10249 1, 1, continuation_bits);
10250 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
10252 #ifndef F_SETOWN_BUG
10253 #ifdef F_SETOWN
10254 #ifdef F_SETOWN_SOCK_NEG
10255 /* stdin is a socket here */
10256 fcntl (connection, F_SETOWN, -getpid ());
10257 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10258 fcntl (connection, F_SETOWN, getpid ());
10259 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10260 #endif /* ! defined (F_SETOWN) */
10261 #endif /* F_SETOWN_BUG */
10263 #ifdef SIGIO
10264 if (interrupt_input)
10265 init_sigio (connection);
10266 #endif /* ! defined (SIGIO) */
10268 UNBLOCK_INPUT;
10270 return dpyinfo;
10273 /* Get rid of display DPYINFO, assuming all frames are already gone. */
10275 void
10276 x_delete_display (dpyinfo)
10277 struct w32_display_info *dpyinfo;
10279 /* Discard this display from w32_display_name_list and w32_display_list.
10280 We can't use Fdelq because that can quit. */
10281 if (! NILP (w32_display_name_list)
10282 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
10283 w32_display_name_list = XCDR (w32_display_name_list);
10284 else
10286 Lisp_Object tail;
10288 tail = w32_display_name_list;
10289 while (CONSP (tail) && CONSP (XCDR (tail)))
10291 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10293 XCDR (tail) = XCDR (XCDR (tail));
10294 break;
10296 tail = XCDR (tail);
10300 /* free palette table */
10302 struct w32_palette_entry * plist;
10304 plist = dpyinfo->color_list;
10305 while (plist)
10307 struct w32_palette_entry * pentry = plist;
10308 plist = plist->next;
10309 xfree (pentry);
10311 dpyinfo->color_list = NULL;
10312 if (dpyinfo->palette)
10313 DeleteObject(dpyinfo->palette);
10315 xfree (dpyinfo->font_table);
10316 xfree (dpyinfo->w32_id_name);
10318 /* Destroy row bitmaps. */
10319 DeleteObject (left_bmp);
10320 DeleteObject (ov_bmp);
10321 DeleteObject (right_bmp);
10322 DeleteObject (continued_bmp);
10323 DeleteObject (continuation_bmp);
10324 DeleteObject (zv_bmp);
10327 /* Set up use of W32. */
10329 DWORD w32_msg_worker ();
10331 void
10332 x_flush (struct frame * f)
10333 { /* Nothing to do */ }
10335 static struct redisplay_interface w32_redisplay_interface =
10337 x_produce_glyphs,
10338 x_write_glyphs,
10339 x_insert_glyphs,
10340 x_clear_end_of_line,
10341 x_scroll_run,
10342 x_after_update_window_line,
10343 x_update_window_begin,
10344 x_update_window_end,
10345 w32_cursor_to,
10346 x_flush,
10347 x_clear_mouse_face,
10348 x_get_glyph_overhangs,
10349 x_fix_overlapping_area
10352 void
10353 w32_initialize ()
10355 rif = &w32_redisplay_interface;
10357 /* MSVC does not type K&R functions with no arguments correctly, and
10358 so we must explicitly cast them. */
10359 clear_frame_hook = (void (*)(void)) x_clear_frame;
10360 ring_bell_hook = (void (*)(void)) w32_ring_bell;
10361 update_begin_hook = x_update_begin;
10362 update_end_hook = x_update_end;
10364 read_socket_hook = w32_read_socket;
10366 frame_up_to_date_hook = w32_frame_up_to_date;
10368 mouse_position_hook = w32_mouse_position;
10369 frame_rehighlight_hook = w32_frame_rehighlight;
10370 frame_raise_lower_hook = w32_frame_raise_lower;
10371 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
10372 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
10373 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
10374 judge_scroll_bars_hook = w32_judge_scroll_bars;
10375 estimate_mode_line_height_hook = x_estimate_mode_line_height;
10377 scroll_region_ok = 1; /* we'll scroll partial frames */
10378 char_ins_del_ok = 1;
10379 line_ins_del_ok = 1; /* we'll just blt 'em */
10380 fast_clear_end_of_line = 1; /* X does this well */
10381 memory_below_frame = 0; /* we don't remember what scrolls
10382 off the bottom */
10383 baud_rate = 19200;
10385 last_tool_bar_item = -1;
10386 any_help_event_p = 0;
10388 /* Initialize input mode: interrupt_input off, no flow control, allow
10389 8 bit character input, standard quit char. */
10390 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
10392 /* Create the window thread - it will terminate itself or when the app terminates */
10394 init_crit ();
10396 dwMainThreadId = GetCurrentThreadId ();
10397 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
10398 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
10400 /* Wait for thread to start */
10403 MSG msg;
10405 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
10407 hWindowsThread = CreateThread (NULL, 0,
10408 (LPTHREAD_START_ROUTINE) w32_msg_worker,
10409 0, 0, &dwWindowsThreadId);
10411 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
10414 /* It is desirable that mainThread should have the same notion of
10415 focus window and active window as windowsThread. Unfortunately, the
10416 following call to AttachThreadInput, which should do precisely what
10417 we need, causes major problems when Emacs is linked as a console
10418 program. Unfortunately, we have good reasons for doing that, so
10419 instead we need to send messages to windowsThread to make some API
10420 calls for us (ones that affect, or depend on, the active/focus
10421 window state. */
10422 #ifdef ATTACH_THREADS
10423 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
10424 #endif
10426 /* Dynamically link to optional system components. */
10428 HANDLE user_lib = LoadLibrary ("user32.dll");
10430 #define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
10432 /* New proportional scroll bar functions. */
10433 LOAD_PROC (SetScrollInfo);
10434 LOAD_PROC (GetScrollInfo);
10436 #undef LOAD_PROC
10438 FreeLibrary (user_lib);
10440 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
10441 otherwise use the fixed height. */
10442 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
10443 GetSystemMetrics (SM_CYVTHUMB);
10445 /* For either kind of scroll bar, take account of the arrows; these
10446 effectively form the border of the main scroll bar range. */
10447 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
10448 = GetSystemMetrics (SM_CYVSCROLL);
10452 void
10453 syms_of_w32term ()
10455 staticpro (&w32_display_name_list);
10456 w32_display_name_list = Qnil;
10458 staticpro (&last_mouse_scroll_bar);
10459 last_mouse_scroll_bar = Qnil;
10461 staticpro (&Qvendor_specific_keysyms);
10462 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10464 DEFVAR_INT ("w32-num-mouse-buttons",
10465 &Vw32_num_mouse_buttons,
10466 "Number of physical mouse buttons.");
10467 Vw32_num_mouse_buttons = Qnil;
10469 DEFVAR_LISP ("w32-swap-mouse-buttons",
10470 &Vw32_swap_mouse_buttons,
10471 "Swap the mapping of middle and right mouse buttons.\n\
10472 When nil, middle button is mouse-2 and right button is mouse-3.");
10473 Vw32_swap_mouse_buttons = Qnil;
10475 DEFVAR_LISP ("w32-grab-focus-on-raise",
10476 &Vw32_grab_focus_on_raise,
10477 "Raised frame grabs input focus.\n\
10478 When t, `raise-frame' grabs input focus as well. This fits well\n\
10479 with the normal Windows click-to-focus policy, but might not be\n\
10480 desirable when using a point-to-focus policy.");
10481 Vw32_grab_focus_on_raise = Qt;
10483 DEFVAR_LISP ("w32-capslock-is-shiftlock",
10484 &Vw32_capslock_is_shiftlock,
10485 "Apply CapsLock state to non character input keys.\n\
10486 When nil, CapsLock only affects normal character input keys.");
10487 Vw32_capslock_is_shiftlock = Qnil;
10489 DEFVAR_LISP ("w32-recognize-altgr",
10490 &Vw32_recognize_altgr,
10491 "Recognize right-alt and left-ctrl as AltGr.\n\
10492 When nil, the right-alt and left-ctrl key combination is\n\
10493 interpreted normally.");
10494 Vw32_recognize_altgr = Qt;
10496 DEFVAR_BOOL ("w32-enable-unicode-output",
10497 &w32_enable_unicode_output,
10498 "Enable the use of Unicode for text output if non-nil.\n\
10499 Unicode output may prevent some third party applications for displaying\n\
10500 Far-East Languages on Windows 95/98 from working properly.\n\
10501 NT uses Unicode internally anyway, so this flag will probably have no\n\
10502 affect on NT machines.");
10503 w32_enable_unicode_output = 1;
10505 help_echo = Qnil;
10506 staticpro (&help_echo);
10507 help_echo_object = Qnil;
10508 staticpro (&help_echo_object);
10509 help_echo_window = Qnil;
10510 staticpro (&help_echo_window);
10511 previous_help_echo = Qnil;
10512 staticpro (&previous_help_echo);
10513 help_echo_pos = -1;
10515 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
10516 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
10517 For example, if a block cursor is over a tab, it will be drawn as\n\
10518 wide as that tab on the display.");
10519 x_stretch_cursor_p = 0;
10521 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10522 "If not nil, Emacs uses toolkit scroll bars.");
10523 Vx_toolkit_scroll_bars = Qt;
10525 staticpro (&last_mouse_motion_frame);
10526 last_mouse_motion_frame = Qnil;