*** empty log message ***
[emacs.git] / src / w32term.c
blobcba210ebab54d3519d93c9be910887c7d40d2f50
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
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 #undef min
60 #undef max
61 #define min(x, y) (((x) < (y)) ? (x) : (y))
62 #define max(x, y) (((x) > (y)) ? (x) : (y))
64 #define abs(x) ((x) < 0 ? -(x) : (x))
66 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
69 /* Bitmaps for truncated lines. */
71 enum bitmap_type
73 NO_BITMAP,
74 LEFT_TRUNCATION_BITMAP,
75 RIGHT_TRUNCATION_BITMAP,
76 OVERLAY_ARROW_BITMAP,
77 CONTINUED_LINE_BITMAP,
78 CONTINUATION_LINE_BITMAP,
79 ZV_LINE_BITMAP
82 enum w32_char_font_type
84 UNKNOWN_FONT,
85 ANSI_FONT,
86 UNICODE_FONT,
87 BDF_FONT
90 /* Bitmaps are all unsigned short, as Windows requires bitmap data to
91 be Word aligned. For some reason they are horizontally reflected
92 compared to how they appear on X, so changes in xterm.c should be
93 reflected here. */
95 /* Bitmap drawn to indicate lines not displaying text if
96 `indicate-empty-lines' is non-nil. */
98 #define zv_width 8
99 #define zv_height 8
100 static unsigned short zv_bits[] = {
101 0x00, 0x00, 0x78, 0x78, 0x78, 0x78, 0x00, 0x00};
102 static HBITMAP zv_bmp;
104 /* An arrow like this: `<-'. */
106 #define left_width 8
107 #define left_height 8
108 static unsigned short left_bits[] = {
109 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
110 static HBITMAP left_bmp;
112 /* Right truncation arrow bitmap `->'. */
114 #define right_width 8
115 #define right_height 8
116 static unsigned short right_bits[] = {
117 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
118 static HBITMAP right_bmp;
120 /* Marker for continued lines. */
122 #define continued_width 8
123 #define continued_height 8
124 static unsigned short continued_bits[] = {
125 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
126 static HBITMAP continued_bmp;
128 /* Marker for continuation lines. */
130 #define continuation_width 8
131 #define continuation_height 8
132 static unsigned short continuation_bits[] = {
133 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
134 static HBITMAP continuation_bmp;
136 /* Overlay arrow bitmap. */
138 #if 0
139 /* A bomb. */
140 #define ov_width 8
141 #define ov_height 8
142 static unsigned short ov_bits[] = {
143 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
144 #else
145 /* A triangular arrow. */
146 #define ov_width 8
147 #define ov_height 8
148 static unsigned short ov_bits[] = {
149 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
150 #endif
151 static HBITMAP ov_bmp;
153 extern Lisp_Object Qhelp_echo;
156 /* Non-zero means Emacs uses toolkit scroll bars. */
158 int x_toolkit_scroll_bars_p;
160 /* If a string, w32_read_socket generates an event to display that string.
161 (The display is done in read_char.) */
163 static Lisp_Object help_echo;
164 static Lisp_Object help_echo_window;
165 static Lisp_Object help_echo_object;
166 static int help_echo_pos;
168 /* Temporary variable for w32_read_socket. */
170 static Lisp_Object previous_help_echo;
172 /* Non-zero means that a HELP_EVENT has been generated since Emacs
173 start. */
175 static int any_help_event_p;
177 /* Non-zero means draw block and hollow cursor as wide as the glyph
178 under it. For example, if a block cursor is over a tab, it will be
179 drawn as wide as that tab on the display. */
181 int x_stretch_cursor_p;
183 extern unsigned int msh_mousewheel;
185 extern void free_frame_menubar ();
187 extern void w32_menu_display_help (HMENU menu, UINT menu_item, UINT flags);
189 extern int w32_codepage_for_font (char *fontname);
191 extern Lisp_Object Vwindow_system;
193 #define x_any_window_to_frame x_window_to_frame
194 #define x_top_window_to_frame x_window_to_frame
197 /* This is display since w32 does not support multiple ones. */
198 struct w32_display_info one_w32_display_info;
200 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
201 one for each element of w32_display_list and in the same order.
202 NAME is the name of the frame.
203 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
204 Lisp_Object w32_display_name_list;
206 /* Frame being updated by update_frame. This is declared in term.c.
207 This is set by update_begin and looked at by all the
208 w32 functions. It is zero while not inside an update.
209 In that case, the w32 functions assume that `SELECTED_FRAME ()'
210 is the frame to apply to. */
211 extern struct frame *updating_frame;
213 /* This is a frame waiting to be autoraised, within w32_read_socket. */
214 struct frame *pending_autoraise_frame;
216 /* Nominal cursor position -- where to draw output.
217 HPOS and VPOS are window relative glyph matrix coordinates.
218 X and Y are window relative pixel coordinates. */
220 struct cursor_pos output_cursor;
222 /* Flag to enable Unicode output in case users wish to use programs
223 like Twinbridge on '95 rather than installed system level support
224 for Far East languages. */
225 int w32_enable_unicode_output;
227 DWORD dwWindowsThreadId = 0;
228 HANDLE hWindowsThread = NULL;
229 DWORD dwMainThreadId = 0;
230 HANDLE hMainThread = NULL;
232 #ifndef SIF_ALL
233 /* These definitions are new with Windows 95. */
234 #define SIF_RANGE 0x0001
235 #define SIF_PAGE 0x0002
236 #define SIF_POS 0x0004
237 #define SIF_DISABLENOSCROLL 0x0008
238 #define SIF_TRACKPOS 0x0010
239 #define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
241 typedef struct tagSCROLLINFO
243 UINT cbSize;
244 UINT fMask;
245 int nMin;
246 int nMax;
247 UINT nPage;
248 int nPos;
249 int nTrackPos;
250 } SCROLLINFO, FAR *LPSCROLLINFO;
251 typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
252 #endif /* SIF_ALL */
254 /* Dynamic linking to new proportional scroll bar functions. */
255 int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
256 BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
258 int vertical_scroll_bar_min_handle;
259 int vertical_scroll_bar_top_border;
260 int vertical_scroll_bar_bottom_border;
262 int last_scroll_bar_drag_pos;
264 /* Mouse movement. */
266 /* Where the mouse was last time we reported a mouse event. */
268 FRAME_PTR last_mouse_frame;
269 static RECT last_mouse_glyph;
270 static Lisp_Object last_mouse_press_frame;
272 Lisp_Object Vw32_num_mouse_buttons;
274 Lisp_Object Vw32_swap_mouse_buttons;
276 /* Control whether x_raise_frame also sets input focus. */
277 Lisp_Object Vw32_grab_focus_on_raise;
279 /* Control whether Caps Lock affects non-ascii characters. */
280 Lisp_Object Vw32_capslock_is_shiftlock;
282 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
283 Lisp_Object Vw32_recognize_altgr;
285 /* The scroll bar in which the last motion event occurred.
287 If the last motion event occurred in a scroll bar, we set this
288 so w32_mouse_position can know whether to report a scroll bar motion or
289 an ordinary motion.
291 If the last motion event didn't occur in a scroll bar, we set this
292 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
293 static Lisp_Object last_mouse_scroll_bar;
294 static int last_mouse_scroll_bar_pos;
296 /* This is a hack. We would really prefer that w32_mouse_position would
297 return the time associated with the position it returns, but there
298 doesn't seem to be any way to wrest the time-stamp from the server
299 along with the position query. So, we just keep track of the time
300 of the last movement we received, and return that in hopes that
301 it's somewhat accurate. */
303 static Time last_mouse_movement_time;
305 /* Incremented by w32_read_socket whenever it really tries to read
306 events. */
308 #ifdef __STDC__
309 static int volatile input_signal_count;
310 #else
311 static int input_signal_count;
312 #endif
314 extern Lisp_Object Vcommand_line_args, Vsystem_name;
316 extern Lisp_Object Qface, Qmouse_face;
318 #ifndef USE_CRT_DLL
319 extern int errno;
320 #endif
322 /* A mask of extra modifier bits to put into every keyboard char. */
324 extern int extra_keyboard_modifiers;
326 /* Enumeration for overriding/changing the face to use for drawing
327 glyphs in x_draw_glyphs. */
329 enum draw_glyphs_face
331 DRAW_NORMAL_TEXT,
332 DRAW_INVERSE_VIDEO,
333 DRAW_CURSOR,
334 DRAW_MOUSE_FACE,
335 DRAW_IMAGE_RAISED,
336 DRAW_IMAGE_SUNKEN
339 static void x_update_window_end P_ ((struct window *, int, int));
340 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
341 void w32_delete_display P_ ((struct w32_display_info *));
342 static int fast_find_position P_ ((struct window *, int, int *, int *,
343 int *, int *));
344 static void set_output_cursor P_ ((struct cursor_pos *));
345 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
346 int *, int *, int *));
347 static void note_mode_line_highlight P_ ((struct window *, int, int));
348 static void x_check_font P_ ((struct frame *, XFontStruct *));
349 static void note_mouse_highlight P_ ((struct frame *, int, int));
350 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
351 static void w32_handle_tool_bar_click P_ ((struct frame *,
352 struct input_event *));
353 static void show_mouse_face P_ ((struct w32_display_info *,
354 enum draw_glyphs_face));
355 void clear_mouse_face P_ ((struct w32_display_info *));
357 void x_lower_frame P_ ((struct frame *));
358 void x_scroll_bar_clear P_ ((struct frame *));
359 void x_wm_set_size_hint P_ ((struct frame *, long, int));
360 void x_raise_frame P_ ((struct frame *));
361 void x_set_window_size P_ ((struct frame *, int, int, int));
362 void x_wm_set_window_state P_ ((struct frame *, int));
363 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
364 void w32_initialize P_ ((void));
365 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
366 int x_compute_min_glyph_bounds P_ ((struct frame *));
367 static void x_draw_phys_cursor_glyph P_ ((struct window *,
368 struct glyph_row *,
369 enum draw_glyphs_face));
370 static void x_update_end P_ ((struct frame *));
371 static void w32_frame_up_to_date P_ ((struct frame *));
372 static void w32_reassert_line_highlight P_ ((int, int));
373 static void x_change_line_highlight P_ ((int, int, int, int));
374 static void w32_set_terminal_modes P_ ((void));
375 static void w32_reset_terminal_modes P_ ((void));
376 static void w32_cursor_to P_ ((int, int, int, int));
377 static void x_write_glyphs P_ ((struct glyph *, int));
378 static void x_clear_end_of_line P_ ((int));
379 static void x_clear_frame P_ ((void));
380 static void x_clear_cursor P_ ((struct window *));
381 static void frame_highlight P_ ((struct frame *));
382 static void frame_unhighlight P_ ((struct frame *));
383 static void w32_new_focus_frame P_ ((struct w32_display_info *,
384 struct frame *));
385 static void w32_frame_rehighlight P_ ((struct frame *));
386 static void x_frame_rehighlight P_ ((struct w32_display_info *));
387 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
388 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
389 static void expose_frame P_ ((struct frame *, int, int, int, int));
390 static void expose_window_tree P_ ((struct window *, RECT *));
391 static void expose_window P_ ((struct window *, RECT *));
392 static void expose_area P_ ((struct window *, struct glyph_row *,
393 RECT *, enum glyph_row_area));
394 static void expose_line P_ ((struct window *, struct glyph_row *,
395 RECT *));
396 void x_update_cursor P_ ((struct frame *, int));
397 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
398 static void x_update_window_cursor P_ ((struct window *, int));
399 static void x_erase_phys_cursor P_ ((struct window *));
400 void x_display_cursor P_ ((struct window *w, int, int, int, int, int));
401 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
402 static void w32_draw_bitmap P_ ((struct window *, HDC hdc, struct glyph_row *,
403 enum bitmap_type));
404 static int x_phys_cursor_in_rect_p P_ ((struct window *, RECT *));
405 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
406 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
407 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *,
408 HDC, int));
410 static Lisp_Object Qvendor_specific_keysyms;
413 /***********************************************************************
414 Debugging
415 ***********************************************************************/
417 #if 0
419 /* This is a function useful for recording debugging information about
420 the sequence of occurrences in this file. */
422 struct record
424 char *locus;
425 int type;
428 struct record event_record[100];
430 int event_record_index;
432 record_event (locus, type)
433 char *locus;
434 int type;
436 if (event_record_index == sizeof (event_record) / sizeof (struct record))
437 event_record_index = 0;
439 event_record[event_record_index].locus = locus;
440 event_record[event_record_index].type = type;
441 event_record_index++;
444 #endif /* 0 */
447 void XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
448 XGCValues *xgcv)
450 if (mask & GCForeground)
451 gc->foreground = xgcv->foreground;
452 if (mask & GCBackground)
453 gc->background = xgcv->background;
454 if (mask & GCFont)
455 gc->font = xgcv->font;
458 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
459 XGCValues *xgcv)
461 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
462 bzero (gc, sizeof (XGCValues));
464 XChangeGC (ignore, gc, mask, xgcv);
466 return gc;
469 void XGetGCValues (void* ignore, XGCValues *gc,
470 unsigned long mask, XGCValues *xgcv)
472 XChangeGC (ignore, xgcv, mask, gc);
475 static void
476 w32_set_clip_rectangle (HDC hdc, RECT *rect)
478 if (rect)
480 HRGN clip_region = CreateRectRgnIndirect (rect);
481 SelectClipRgn (hdc, clip_region);
482 DeleteObject (clip_region);
484 else
485 SelectClipRgn (hdc, NULL);
489 /* Draw a hollow rectangle at the specified position. */
490 void
491 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
492 int width, int height)
494 HBRUSH hb, oldhb;
495 HPEN hp, oldhp;
497 hb = CreateSolidBrush (gc->background);
498 hp = CreatePen (PS_SOLID, 0, gc->foreground);
499 oldhb = SelectObject (hdc, hb);
500 oldhp = SelectObject (hdc, hp);
502 Rectangle (hdc, x, y, x + width, y + height);
504 SelectObject (hdc, oldhb);
505 SelectObject (hdc, oldhp);
506 DeleteObject (hb);
507 DeleteObject (hp);
510 /* Draw a filled rectangle at the specified position. */
511 void
512 w32_fill_rect (f, hdc, pix, lprect)
513 FRAME_PTR f;
514 HDC hdc;
515 COLORREF pix;
516 RECT * lprect;
518 HBRUSH hb;
519 RECT rect;
521 hb = CreateSolidBrush (pix);
522 FillRect (hdc, lprect, hb);
523 DeleteObject (hb);
526 void
527 w32_clear_window (f)
528 FRAME_PTR f;
530 RECT rect;
531 HDC hdc = get_frame_dc (f);
533 GetClientRect (FRAME_W32_WINDOW (f), &rect);
534 w32_clear_rect (f, hdc, &rect);
535 release_frame_dc (f, hdc);
539 /***********************************************************************
540 Starting and ending an update
541 ***********************************************************************/
543 /* Start an update of frame F. This function is installed as a hook
544 for update_begin, i.e. it is called when update_begin is called.
545 This function is called prior to calls to x_update_window_begin for
546 each window being updated. Currently, there is nothing to do here
547 because all interesting stuff is done on a window basis. */
549 static void
550 x_update_begin (f)
551 struct frame *f;
553 /* Nothing to do. We have to do something though, otherwise the
554 function gets optimized away and the hook is no longer valid. */
555 struct frame *cf = f;
559 /* Start update of window W. Set the global variable updated_window
560 to the window being updated and set output_cursor to the cursor
561 position of W. */
563 static void
564 x_update_window_begin (w)
565 struct window *w;
567 struct frame *f = XFRAME (WINDOW_FRAME (w));
568 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
570 updated_window = w;
571 set_output_cursor (&w->cursor);
573 BLOCK_INPUT;
575 /* Regenerate display palette before drawing if list of requested
576 colors has changed. */
577 if (display_info->regen_palette)
579 w32_regenerate_palette (f);
580 display_info->regen_palette = FALSE;
583 if (f == display_info->mouse_face_mouse_frame)
585 /* Don't do highlighting for mouse motion during the update. */
586 display_info->mouse_face_defer = 1;
588 /* If F needs to be redrawn, simply forget about any prior mouse
589 highlighting. */
590 if (FRAME_GARBAGED_P (f))
591 display_info->mouse_face_window = Qnil;
593 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
594 their mouse_face_p flag set, which means that they are always
595 unequal to rows in a desired matrix which never have that
596 flag set. So, rows containing mouse-face glyphs are never
597 scrolled, and we don't have to switch the mouse highlight off
598 here to prevent it from being scrolled. */
600 /* Can we tell that this update does not affect the window
601 where the mouse highlight is? If so, no need to turn off.
602 Likewise, don't do anything if the frame is garbaged;
603 in that case, the frame's current matrix that we would use
604 is all wrong, and we will redisplay that line anyway. */
605 if (!NILP (display_info->mouse_face_window)
606 && w == XWINDOW (display_info->mouse_face_window))
608 int i;
610 for (i = 0; i < w->desired_matrix->nrows; ++i)
611 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
612 break;
614 if (i < w->desired_matrix->nrows)
615 clear_mouse_face (display_info);
617 #endif /* 0 */
620 UNBLOCK_INPUT;
624 /* Draw a vertical window border to the right of window W if W doesn't
625 have vertical scroll bars. */
627 static void
628 x_draw_vertical_border (w)
629 struct window *w;
631 struct frame *f = XFRAME (WINDOW_FRAME (w));
633 /* Redraw borders between horizontally adjacent windows. Don't
634 do it for frames with vertical scroll bars because either the
635 right scroll bar of a window, or the left scroll bar of its
636 neighbor will suffice as a border. */
637 if (!WINDOW_RIGHTMOST_P (w)
638 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
640 RECT r;
641 HDC hdc;
643 window_box_edges (w, -1, &r.left, &r.top, &r.right, &r.bottom);
644 r.left = r.right + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
645 r.right = r.left + 1;
646 r.bottom -= 1;
648 hdc = get_frame_dc (f);
649 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), r);
650 release_frame_dc (f, hdc);
655 /* End update of window W (which is equal to updated_window).
657 Draw vertical borders between horizontally adjacent windows, and
658 display W's cursor if CURSOR_ON_P is non-zero.
660 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
661 glyphs in mouse-face were overwritten. In that case we have to
662 make sure that the mouse-highlight is properly redrawn.
664 W may be a menu bar pseudo-window in case we don't have X toolkit
665 support. Such windows don't have a cursor, so don't display it
666 here. */
668 static void
669 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
670 struct window *w;
671 int cursor_on_p, mouse_face_overwritten_p;
673 if (!w->pseudo_window_p)
675 struct w32_display_info *dpyinfo
676 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
678 BLOCK_INPUT;
680 /* If a row with mouse-face was overwritten, arrange for
681 XTframe_up_to_date to redisplay the mouse highlight. */
682 if (mouse_face_overwritten_p)
684 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
685 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
686 dpyinfo->mouse_face_window = Qnil;
689 if (cursor_on_p)
690 x_display_and_set_cursor (w, 1, output_cursor.hpos,
691 output_cursor.vpos,
692 output_cursor.x, output_cursor.y);
693 x_draw_vertical_border (w);
694 UNBLOCK_INPUT;
697 updated_window = NULL;
701 /* End update of frame F. This function is installed as a hook in
702 update_end. */
704 static void
705 x_update_end (f)
706 struct frame *f;
708 /* Mouse highlight may be displayed again. */
709 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
713 /* This function is called from various places in xdisp.c whenever a
714 complete update has been performed. The global variable
715 updated_window is not available here. */
717 static void
718 w32_frame_up_to_date (f)
719 struct frame *f;
721 if (FRAME_W32_P (f))
723 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
724 if (dpyinfo->mouse_face_deferred_gc
725 || f == dpyinfo->mouse_face_mouse_frame)
727 BLOCK_INPUT;
728 if (dpyinfo->mouse_face_mouse_frame)
729 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
730 dpyinfo->mouse_face_mouse_x,
731 dpyinfo->mouse_face_mouse_y);
732 dpyinfo->mouse_face_deferred_gc = 0;
733 UNBLOCK_INPUT;
739 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
740 arrow bitmaps, or clear the areas where they would be displayed
741 before DESIRED_ROW is made current. The window being updated is
742 found in updated_window. This function It is called from
743 update_window_line only if it is known that there are differences
744 between bitmaps to be drawn between current row and DESIRED_ROW. */
746 static void
747 x_after_update_window_line (desired_row)
748 struct glyph_row *desired_row;
750 struct window *w = updated_window;
752 xassert (w);
754 if (!desired_row->mode_line_p && !w->pseudo_window_p)
756 BLOCK_INPUT;
757 x_draw_row_bitmaps (w, desired_row);
759 /* When a window has disappeared, make sure that no rest of
760 full-width rows stays visible in the internal border. */
761 if (windows_or_buffers_changed)
763 struct frame *f = XFRAME (w->frame);
764 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
765 int height = desired_row->visible_height;
766 int x = (window_box_right (w, -1)
767 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
768 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
769 HDC hdc = get_frame_dc (f);
771 w32_clear_area (f, hdc, x, y, width, height);
772 release_frame_dc (f, hdc);
775 UNBLOCK_INPUT;
780 /* Draw the bitmap WHICH in one of the areas to the left or right of
781 window W. ROW is the glyph row for which to display the bitmap; it
782 determines the vertical position at which the bitmap has to be
783 drawn. */
785 static void
786 w32_draw_bitmap (w, hdc, row, which)
787 struct window *w;
788 HDC hdc;
789 struct glyph_row *row;
790 enum bitmap_type which;
792 struct frame *f = XFRAME (WINDOW_FRAME (w));
793 Window window = FRAME_W32_WINDOW (f);
794 HDC compat_hdc;
795 int x, y, wd, h, dy;
796 HBITMAP pixmap;
797 HBRUSH fg_brush, orig_brush;
798 HANDLE horig_obj;
799 struct face *face;
801 /* Must clip because of partially visible lines. */
802 w32_clip_to_row (w, row, hdc, 1);
804 switch (which)
806 case LEFT_TRUNCATION_BITMAP:
807 wd = left_width;
808 h = left_height;
809 pixmap = left_bmp;
810 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
811 - wd
812 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
813 break;
815 case OVERLAY_ARROW_BITMAP:
816 wd = ov_width;
817 h = ov_height;
818 pixmap = ov_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 RIGHT_TRUNCATION_BITMAP:
825 wd = right_width;
826 h = right_height;
827 pixmap = right_bmp;
828 x = window_box_right (w, -1);
829 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
830 break;
832 case CONTINUED_LINE_BITMAP:
833 wd = continued_width;
834 h = continued_height;
835 pixmap = continued_bmp;
836 x = window_box_right (w, -1);
837 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
838 break;
840 case CONTINUATION_LINE_BITMAP:
841 wd = continuation_width;
842 h = continuation_height;
843 pixmap = continuation_bmp;
844 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
845 - wd
846 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
847 break;
849 case ZV_LINE_BITMAP:
850 wd = zv_width;
851 h = zv_height;
852 pixmap = zv_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 default:
859 abort ();
862 /* Convert to frame coordinates. Set dy to the offset in the row to
863 start drawing the bitmap. */
864 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
865 dy = (row->height - h) / 2;
867 /* Draw the bitmap. */
868 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
870 compat_hdc = CreateCompatibleDC (hdc);
871 SaveDC (hdc);
872 fg_brush = CreateSolidBrush (FRAME_FOREGROUND_PIXEL (f));
873 orig_brush = SelectObject (hdc, fg_brush);
874 horig_obj = SelectObject (compat_hdc, pixmap);
875 SetTextColor (hdc, FRAME_BACKGROUND_PIXEL (f));
876 SetBkColor (hdc, FRAME_FOREGROUND_PIXEL (f));
877 #if 0 /* From w32bdf.c (which is from Meadow). */
878 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, SRCCOPY);
879 #else
880 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, 0xB8074A);
881 #endif
882 SelectObject (compat_hdc, horig_obj);
883 SelectObject (hdc, orig_brush);
884 DeleteObject (fg_brush);
885 DeleteDC (compat_hdc);
886 RestoreDC (hdc, -1);
890 /* Draw flags bitmaps for glyph row ROW on window W. Call this
891 function with input blocked. */
893 static void
894 x_draw_row_bitmaps (w, row)
895 struct window *w;
896 struct glyph_row *row;
898 struct frame *f = XFRAME (w->frame);
899 enum bitmap_type bitmap;
900 struct face *face;
901 int header_line_height = -1;
902 HDC hdc = get_frame_dc (f);
904 xassert (interrupt_input_blocked);
906 /* If row is completely invisible, because of vscrolling, we
907 don't have to draw anything. */
908 if (row->visible_height <= 0)
909 return;
911 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
912 PREPARE_FACE_FOR_DISPLAY (f, face);
914 /* Decide which bitmap to draw at the left side. */
915 if (row->overlay_arrow_p)
916 bitmap = OVERLAY_ARROW_BITMAP;
917 else if (row->truncated_on_left_p)
918 bitmap = LEFT_TRUNCATION_BITMAP;
919 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
920 bitmap = CONTINUATION_LINE_BITMAP;
921 else if (row->indicate_empty_line_p)
922 bitmap = ZV_LINE_BITMAP;
923 else
924 bitmap = NO_BITMAP;
926 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
927 the flags area. */
928 if (bitmap == NO_BITMAP
929 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
930 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
932 /* If W has a vertical border to its left, don't draw over it. */
933 int border = ((XFASTINT (w->left) > 0
934 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
935 ? 1 : 0);
936 int left = window_box_left (w, -1);
938 if (header_line_height < 0)
939 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
941 w32_fill_area (f, hdc, face->background,
942 left - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) + border,
943 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
944 row->y)),
945 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
946 row->visible_height);
949 /* Draw the left bitmap. */
950 if (bitmap != NO_BITMAP)
951 w32_draw_bitmap (w, hdc, row, bitmap);
953 /* Decide which bitmap to draw at the right side. */
954 if (row->truncated_on_right_p)
955 bitmap = RIGHT_TRUNCATION_BITMAP;
956 else if (row->continued_p)
957 bitmap = CONTINUED_LINE_BITMAP;
958 else
959 bitmap = NO_BITMAP;
961 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
962 the flags area. */
963 if (bitmap == NO_BITMAP
964 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
965 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
967 int right = window_box_right (w, -1);
969 if (header_line_height < 0)
970 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
972 w32_fill_area (f, hdc, face->background,
973 right,
974 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
975 row->y)),
976 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
977 row->visible_height);
980 /* Draw the right bitmap. */
981 if (bitmap != NO_BITMAP)
982 w32_draw_bitmap (w, hdc, row, bitmap);
984 release_frame_dc (f, hdc);
988 /***********************************************************************
989 Line Highlighting
990 ***********************************************************************/
992 /* External interface to control of standout mode. Not used for W32
993 frames. Aborts when called. */
995 static void
996 w32_reassert_line_highlight (new, vpos)
997 int new, vpos;
999 abort ();
1002 /* Call this when about to modify line at position VPOS and change
1003 whether it is highlighted. Not used for W32 frames. Aborts when
1004 called. */
1006 static void
1007 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1008 int new_highlight, vpos, y, first_unused_hpos;
1010 abort ();
1013 /* This is called when starting Emacs and when restarting after
1014 suspend. When starting Emacs, no window is mapped. And nothing
1015 must be done to Emacs's own window if it is suspended (though that
1016 rarely happens). */
1018 static void
1019 w32_set_terminal_modes (void)
1023 /* This is called when exiting or suspending Emacs. Exiting will make
1024 the W32 windows go away, and suspending requires no action. */
1026 static void
1027 w32_reset_terminal_modes (void)
1033 /***********************************************************************
1034 Output Cursor
1035 ***********************************************************************/
1037 /* Set the global variable output_cursor to CURSOR. All cursor
1038 positions are relative to updated_window. */
1040 static void
1041 set_output_cursor (cursor)
1042 struct cursor_pos *cursor;
1044 output_cursor.hpos = cursor->hpos;
1045 output_cursor.vpos = cursor->vpos;
1046 output_cursor.x = cursor->x;
1047 output_cursor.y = cursor->y;
1051 /* Set a nominal cursor position.
1053 HPOS and VPOS are column/row positions in a window glyph matrix. X
1054 and Y are window text area relative pixel positions.
1056 If this is done during an update, updated_window will contain the
1057 window that is being updated and the position is the future output
1058 cursor position for that window. If updated_window is null, use
1059 selected_window and display the cursor at the given position. */
1061 static void
1062 w32_cursor_to (vpos, hpos, y, x)
1063 int vpos, hpos, y, x;
1065 struct window *w;
1067 /* If updated_window is not set, work on selected_window. */
1068 if (updated_window)
1069 w = updated_window;
1070 else
1071 w = XWINDOW (selected_window);
1073 /* Set the output cursor. */
1074 output_cursor.hpos = hpos;
1075 output_cursor.vpos = vpos;
1076 output_cursor.x = x;
1077 output_cursor.y = y;
1079 /* If not called as part of an update, really display the cursor.
1080 This will also set the cursor position of W. */
1081 if (updated_window == NULL)
1083 BLOCK_INPUT;
1084 x_display_cursor (w, 1, hpos, vpos, x, y);
1085 UNBLOCK_INPUT;
1091 /***********************************************************************
1092 Display Iterator
1093 ***********************************************************************/
1095 /* Function prototypes of this page. */
1097 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1098 struct glyph *,
1099 wchar_t *,
1100 int *));
1101 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1102 int, wchar_t *, int));
1103 static XCharStruct *w32_per_char_metric P_ ((HDC hdc, XFontStruct *,
1104 wchar_t *,
1105 enum w32_char_font_type));
1106 static enum w32_char_font_type
1107 w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
1108 static void x_append_glyph P_ ((struct it *));
1109 static void x_append_composite_glyph P_ ((struct it *));
1110 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1111 int, int, double));
1112 static void x_produce_glyphs P_ ((struct it *));
1113 static void x_produce_image_glyph P_ ((struct it *it));
1116 /* Dealing with bits of wchar_t as if they were an XChar2B. */
1117 #define BUILD_WCHAR_T(byte1, byte2) \
1118 ((wchar_t)((((byte1) & 0x00ff) << 8) | ((byte2) & 0x00ff)))
1121 #define BYTE1(ch) \
1122 (((ch) & 0xff00) >> 8)
1124 #define BYTE2(ch) \
1125 ((ch) & 0x00ff)
1128 /* NTEMACS_TODO: Add support for bdf fonts back in. */
1130 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
1131 If CHAR2B is not contained in FONT, the font's default character
1132 metric is returned. */
1134 static XCharStruct *
1135 w32_per_char_metric (hdc, font, char2b, font_type)
1136 HDC hdc;
1137 XFontStruct *font;
1138 wchar_t *char2b;
1139 enum w32_char_font_type font_type;
1141 /* The result metric information. */
1142 XCharStruct *pcm;
1143 BOOL retval;
1145 xassert (font && char2b);
1147 if (font_type == UNKNOWN_FONT)
1149 if (font->bdf)
1150 font_type = BDF_FONT;
1151 else if (!w32_enable_unicode_output)
1152 font_type = ANSI_FONT;
1153 else
1154 font_type = UNICODE_FONT;
1157 pcm = (XCharStruct *) xmalloc (sizeof (XCharStruct));
1159 if (font->hfont)
1160 SelectObject (hdc, font->hfont);
1162 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
1164 ABC char_widths;
1166 if (font_type == UNICODE_FONT)
1167 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
1168 else if (font_type == ANSI_FONT)
1169 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
1171 if (retval)
1173 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
1174 pcm->lbearing = char_widths.abcA;
1175 pcm->rbearing = pcm->width - char_widths.abcC;
1177 else
1179 /* Windows 9x does not implement GetCharABCWidthsW, so if that
1180 failed, try GetTextExtentPoint32W, which is implemented and
1181 at least gives us some of the info we are after (total
1182 character width). */
1183 SIZE sz;
1185 if (font_type == UNICODE_FONT)
1186 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
1188 if (retval)
1190 pcm->width = sz.cx;
1191 pcm->rbearing = sz.cx;
1192 pcm->lbearing = 0;
1194 else
1196 xfree (pcm);
1197 return NULL;
1201 else
1203 /* Do our best to deduce the desired metrics data for non-Truetype
1204 fonts (generally, raster fonts). */
1205 INT char_width;
1207 retval = GetCharWidth (hdc, *char2b, *char2b, &char_width);
1208 if (retval)
1210 pcm->width = char_width;
1211 pcm->rbearing = char_width;
1212 pcm->lbearing = 0;
1214 else
1216 xfree (pcm);
1217 return NULL;
1221 pcm->ascent = FONT_BASE (font);
1222 pcm->descent = FONT_DESCENT (font);
1224 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1226 xfree (pcm);
1227 pcm = NULL;
1230 return pcm;
1234 /* Determine if a font is double byte. */
1235 int w32_font_is_double_byte (XFontStruct *font)
1237 return font->double_byte_p;
1241 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1242 the two-byte form of C. Encoding is returned in *CHAR2B. */
1244 static INLINE enum w32_char_font_type
1245 w32_encode_char (c, char2b, font_info, two_byte_p)
1246 int c;
1247 wchar_t *char2b;
1248 struct font_info *font_info;
1249 int * two_byte_p;
1251 int charset = CHAR_CHARSET (c);
1252 int codepage;
1253 int unicode_p = 0;
1255 XFontStruct *font = font_info->font;
1257 xassert(two_byte_p);
1259 *two_byte_p = w32_font_is_double_byte (font);
1261 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1262 This may be either a program in a special encoder language or a
1263 fixed encoding. */
1264 if (font_info->font_encoder)
1266 /* It's a program. */
1267 struct ccl_program *ccl = font_info->font_encoder;
1269 if (CHARSET_DIMENSION (charset) == 1)
1271 ccl->reg[0] = charset;
1272 ccl->reg[1] = BYTE2 (*char2b);
1274 else
1276 ccl->reg[0] = charset;
1277 ccl->reg[1] = BYTE1 (*char2b);
1278 ccl->reg[2] = BYTE2 (*char2b);
1281 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1283 /* We assume that MSBs are appropriately set/reset by CCL
1284 program. */
1285 if (!*two_byte_p) /* 1-byte font */
1286 *char2b = BUILD_WCHAR_T (0, ccl->reg[1]);
1287 else
1288 *char2b = BUILD_WCHAR_T (ccl->reg[1], ccl->reg[2]);
1290 else if (font_info->encoding[charset])
1292 /* Fixed encoding scheme. See fontset.h for the meaning of the
1293 encoding numbers. */
1294 int enc = font_info->encoding[charset];
1296 if ((enc == 1 || enc == 2)
1297 && CHARSET_DIMENSION (charset) == 2)
1298 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b) | 0x80, BYTE2 (*char2b));
1300 if (enc == 1 || enc == 3
1301 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1302 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b), BYTE2 (*char2b) | 0x80);
1303 else if (enc == 4)
1305 int sjis1, sjis2;
1307 ENCODE_SJIS (BYTE1 (*char2b), BYTE2 (*char2b),
1308 sjis1, sjis2);
1309 *char2b = BUILD_WCHAR_T (sjis1, sjis2);
1312 codepage = w32_codepage_for_font (font_info->name);
1314 /* If charset is not ASCII or Latin-1, may need to move it into
1315 Unicode space. */
1316 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
1317 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1)
1319 char temp[3];
1320 temp[0] = BYTE1 (*char2b);
1321 temp[1] = BYTE2 (*char2b);
1322 temp[2] = '\0';
1323 if (temp[0])
1324 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1325 else
1326 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1327 unicode_p = 1;
1328 *two_byte_p = 1;
1330 if (!font)
1331 return UNKNOWN_FONT;
1332 else if (font->bdf)
1333 return BDF_FONT;
1334 else if (unicode_p)
1335 return UNICODE_FONT;
1336 else
1337 return ANSI_FONT;
1341 /* Get face and two-byte form of character C in face FACE_ID on frame
1342 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1343 means we want to display multibyte text. Value is a pointer to a
1344 realized face that is ready for display. */
1346 static INLINE struct face *
1347 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1348 struct frame *f;
1349 int c, face_id;
1350 wchar_t *char2b;
1351 int multibyte_p;
1353 struct face *face = FACE_FROM_ID (f, face_id);
1355 if (!multibyte_p)
1357 /* Unibyte case. We don't have to encode, but we have to make
1358 sure to use a face suitable for unibyte. */
1359 *char2b = BUILD_WCHAR_T (0, c);
1360 face_id = FACE_FOR_CHAR (f, face, c);
1361 face = FACE_FROM_ID (f, face_id);
1363 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1365 /* Case of ASCII in a face known to fit ASCII. */
1366 *char2b = BUILD_WCHAR_T (0, c);
1368 else
1370 int c1, c2, charset;
1372 /* Split characters into bytes. If c2 is -1 afterwards, C is
1373 really a one-byte character so that byte1 is zero. */
1374 SPLIT_CHAR (c, charset, c1, c2);
1375 if (c2 > 0)
1376 *char2b = BUILD_WCHAR_T (c1, c2);
1377 else
1378 *char2b = BUILD_WCHAR_T (0, c1);
1380 /* Maybe encode the character in *CHAR2B. */
1381 if (face->font != NULL)
1383 struct font_info *font_info
1384 = FONT_INFO_FROM_ID (f, face->font_info_id);
1385 if (font_info)
1386 w32_encode_char (c, char2b, font_info, &multibyte_p);
1390 /* Make sure X resources of the face are allocated. */
1391 xassert (face != NULL);
1392 PREPARE_FACE_FOR_DISPLAY (f, face);
1394 return face;
1398 /* Get face and two-byte form of character glyph GLYPH on frame F.
1399 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1400 a pointer to a realized face that is ready for display. */
1402 static INLINE struct face *
1403 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1404 struct frame *f;
1405 struct glyph *glyph;
1406 wchar_t *char2b;
1407 int *two_byte_p;
1409 struct face *face;
1410 int dummy = 0;
1412 xassert (glyph->type == CHAR_GLYPH);
1413 face = FACE_FROM_ID (f, glyph->face_id);
1415 if (two_byte_p)
1416 *two_byte_p = 0;
1417 else
1418 two_byte_p = &dummy;
1420 if (!glyph->multibyte_p)
1422 /* Unibyte case. We don't have to encode, but we have to make
1423 sure to use a face suitable for unibyte. */
1424 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1426 else if (glyph->u.ch < 128
1427 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1429 /* Case of ASCII in a face known to fit ASCII. */
1430 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1432 else
1434 int c1, c2, charset;
1436 /* Split characters into bytes. If c2 is -1 afterwards, C is
1437 really a one-byte character so that byte1 is zero. */
1438 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1439 if (c2 > 0)
1440 *char2b = BUILD_WCHAR_T (c1, c2);
1441 else
1442 *char2b = BUILD_WCHAR_T (0, c1);
1444 /* Maybe encode the character in *CHAR2B. */
1445 if (charset != CHARSET_ASCII)
1447 struct font_info *font_info
1448 = FONT_INFO_FROM_ID (f, face->font_info_id);
1449 if (font_info)
1451 glyph->w32_font_type
1452 = w32_encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
1457 /* Make sure X resources of the face are allocated. */
1458 xassert (face != NULL);
1459 PREPARE_FACE_FOR_DISPLAY (f, face);
1460 return face;
1464 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1465 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1467 static INLINE void
1468 x_append_glyph (it)
1469 struct it *it;
1471 struct glyph *glyph;
1472 enum glyph_row_area area = it->area;
1474 xassert (it->glyph_row);
1475 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1477 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1478 if (glyph < it->glyph_row->glyphs[area + 1])
1480 glyph->charpos = CHARPOS (it->position);
1481 glyph->object = it->object;
1482 glyph->pixel_width = it->pixel_width;
1483 glyph->voffset = it->voffset;
1484 glyph->type = CHAR_GLYPH;
1485 glyph->multibyte_p = it->multibyte_p;
1486 glyph->left_box_line_p = it->start_of_box_run_p;
1487 glyph->right_box_line_p = it->end_of_box_run_p;
1488 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1489 || it->phys_descent > it->descent);
1490 glyph->padding_p = 0;
1491 glyph->glyph_not_available_p = it->glyph_not_available_p;
1492 glyph->face_id = it->face_id;
1493 glyph->u.ch = it->char_to_display;
1494 glyph->w32_font_type = UNKNOWN_FONT;
1495 ++it->glyph_row->used[area];
1499 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1500 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1502 static INLINE void
1503 x_append_composite_glyph (it)
1504 struct it *it;
1506 struct glyph *glyph;
1507 enum glyph_row_area area = it->area;
1509 xassert (it->glyph_row);
1511 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1512 if (glyph < it->glyph_row->glyphs[area + 1])
1514 glyph->charpos = CHARPOS (it->position);
1515 glyph->object = it->object;
1516 glyph->pixel_width = it->pixel_width;
1517 glyph->voffset = it->voffset;
1518 glyph->type = COMPOSITE_GLYPH;
1519 glyph->multibyte_p = it->multibyte_p;
1520 glyph->left_box_line_p = it->start_of_box_run_p;
1521 glyph->right_box_line_p = it->end_of_box_run_p;
1522 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1523 || it->phys_descent > it->descent);
1524 glyph->padding_p = 0;
1525 glyph->glyph_not_available_p = 0;
1526 glyph->face_id = it->face_id;
1527 glyph->u.cmp_id = it->cmp_id;
1528 glyph->w32_font_type = UNKNOWN_FONT;
1529 ++it->glyph_row->used[area];
1534 /* Change IT->ascent and IT->height according to the setting of
1535 IT->voffset. */
1537 static INLINE void
1538 take_vertical_position_into_account (it)
1539 struct it *it;
1541 if (it->voffset)
1543 if (it->voffset < 0)
1544 /* Increase the ascent so that we can display the text higher
1545 in the line. */
1546 it->ascent += abs (it->voffset);
1547 else
1548 /* Increase the descent so that we can display the text lower
1549 in the line. */
1550 it->descent += it->voffset;
1555 /* Produce glyphs/get display metrics for the image IT is loaded with.
1556 See the description of struct display_iterator in dispextern.h for
1557 an overview of struct display_iterator. */
1559 static void
1560 x_produce_image_glyph (it)
1561 struct it *it;
1563 struct image *img;
1564 struct face *face;
1566 xassert (it->what == IT_IMAGE);
1568 face = FACE_FROM_ID (it->f, it->face_id);
1569 img = IMAGE_FROM_ID (it->f, it->image_id);
1570 xassert (img);
1572 /* Make sure X resources of the face and image are loaded. */
1573 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1574 prepare_image_for_display (it->f, img);
1576 it->ascent = it->phys_ascent = image_ascent (img, face);
1577 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1578 it->pixel_width = img->width + 2 * img->margin;
1580 it->nglyphs = 1;
1582 if (face->box != FACE_NO_BOX)
1584 it->ascent += face->box_line_width;
1585 it->descent += face->box_line_width;
1587 if (it->start_of_box_run_p)
1588 it->pixel_width += face->box_line_width;
1589 if (it->end_of_box_run_p)
1590 it->pixel_width += face->box_line_width;
1593 take_vertical_position_into_account (it);
1595 if (it->glyph_row)
1597 struct glyph *glyph;
1598 enum glyph_row_area area = it->area;
1600 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1601 if (glyph < it->glyph_row->glyphs[area + 1])
1603 glyph->charpos = CHARPOS (it->position);
1604 glyph->object = it->object;
1605 glyph->pixel_width = it->pixel_width;
1606 glyph->voffset = it->voffset;
1607 glyph->type = IMAGE_GLYPH;
1608 glyph->multibyte_p = it->multibyte_p;
1609 glyph->left_box_line_p = it->start_of_box_run_p;
1610 glyph->right_box_line_p = it->end_of_box_run_p;
1611 glyph->overlaps_vertically_p = 0;
1612 glyph->padding_p = 0;
1613 glyph->glyph_not_available_p = 0;
1614 glyph->face_id = it->face_id;
1615 glyph->u.img_id = img->id;
1616 glyph->w32_font_type = UNKNOWN_FONT;
1617 ++it->glyph_row->used[area];
1623 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1624 of the glyph, WIDTH and HEIGHT are the width and height of the
1625 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1626 ascent of the glyph (0 <= ASCENT <= 1). */
1628 static void
1629 x_append_stretch_glyph (it, object, width, height, ascent)
1630 struct it *it;
1631 Lisp_Object object;
1632 int width, height;
1633 double ascent;
1635 struct glyph *glyph;
1636 enum glyph_row_area area = it->area;
1638 xassert (ascent >= 0 && ascent <= 1);
1640 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1641 if (glyph < it->glyph_row->glyphs[area + 1])
1643 glyph->charpos = CHARPOS (it->position);
1644 glyph->object = object;
1645 glyph->pixel_width = width;
1646 glyph->voffset = it->voffset;
1647 glyph->type = STRETCH_GLYPH;
1648 glyph->multibyte_p = it->multibyte_p;
1649 glyph->left_box_line_p = it->start_of_box_run_p;
1650 glyph->right_box_line_p = it->end_of_box_run_p;
1651 glyph->overlaps_vertically_p = 0;
1652 glyph->padding_p = 0;
1653 glyph->glyph_not_available_p = 0;
1654 glyph->face_id = it->face_id;
1655 glyph->u.stretch.ascent = height * ascent;
1656 glyph->u.stretch.height = height;
1657 glyph->w32_font_type = UNKNOWN_FONT;
1658 ++it->glyph_row->used[area];
1663 /* Produce a stretch glyph for iterator IT. IT->object is the value
1664 of the glyph property displayed. The value must be a list
1665 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1666 being recognized:
1668 1. `:width WIDTH' specifies that the space should be WIDTH *
1669 canonical char width wide. WIDTH may be an integer or floating
1670 point number.
1672 2. `:relative-width FACTOR' specifies that the width of the stretch
1673 should be computed from the width of the first character having the
1674 `glyph' property, and should be FACTOR times that width.
1676 3. `:align-to HPOS' specifies that the space should be wide enough
1677 to reach HPOS, a value in canonical character units.
1679 Exactly one of the above pairs must be present.
1681 4. `:height HEIGHT' specifies that the height of the stretch produced
1682 should be HEIGHT, measured in canonical character units.
1684 5. `:relative-height FACTOR' specifies that the height of the the
1685 stretch should be FACTOR times the height of the characters having
1686 the glyph property.
1688 Either none or exactly one of 4 or 5 must be present.
1690 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1691 of the stretch should be used for the ascent of the stretch.
1692 ASCENT must be in the range 0 <= ASCENT <= 100. */
1694 #define NUMVAL(X) \
1695 ((INTEGERP (X) || FLOATP (X)) \
1696 ? XFLOATINT (X) \
1697 : - 1)
1700 static void
1701 x_produce_stretch_glyph (it)
1702 struct it *it;
1704 /* (space :width WIDTH :height HEIGHT. */
1705 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1706 extern Lisp_Object QCrelative_width, QCrelative_height;
1707 extern Lisp_Object QCalign_to;
1708 Lisp_Object prop, plist;
1709 double width = 0, height = 0, ascent = 0;
1710 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1711 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1713 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1715 /* List should start with `space'. */
1716 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1717 plist = XCDR (it->object);
1719 /* Compute the width of the stretch. */
1720 if (prop = Fplist_get (plist, QCwidth),
1721 NUMVAL (prop) > 0)
1722 /* Absolute width `:width WIDTH' specified and valid. */
1723 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1724 else if (prop = Fplist_get (plist, QCrelative_width),
1725 NUMVAL (prop) > 0)
1727 /* Relative width `:relative-width FACTOR' specified and valid.
1728 Compute the width of the characters having the `glyph'
1729 property. */
1730 struct it it2;
1731 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1733 it2 = *it;
1734 if (it->multibyte_p)
1736 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1737 - IT_BYTEPOS (*it));
1738 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1740 else
1741 it2.c = *p, it2.len = 1;
1743 it2.glyph_row = NULL;
1744 it2.what = IT_CHARACTER;
1745 x_produce_glyphs (&it2);
1746 width = NUMVAL (prop) * it2.pixel_width;
1748 else if (prop = Fplist_get (plist, QCalign_to),
1749 NUMVAL (prop) > 0)
1750 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1751 else
1752 /* Nothing specified -> width defaults to canonical char width. */
1753 width = CANON_X_UNIT (it->f);
1755 /* Compute height. */
1756 if (prop = Fplist_get (plist, QCheight),
1757 NUMVAL (prop) > 0)
1758 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1759 else if (prop = Fplist_get (plist, QCrelative_height),
1760 NUMVAL (prop) > 0)
1761 height = FONT_HEIGHT (font) * NUMVAL (prop);
1762 else
1763 height = FONT_HEIGHT (font);
1765 /* Compute percentage of height used for ascent. If
1766 `:ascent ASCENT' is present and valid, use that. Otherwise,
1767 derive the ascent from the font in use. */
1768 if (prop = Fplist_get (plist, QCascent),
1769 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1770 ascent = NUMVAL (prop) / 100.0;
1771 else
1772 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
1774 if (width <= 0)
1775 width = 1;
1776 if (height <= 0)
1777 height = 1;
1779 if (it->glyph_row)
1781 Lisp_Object object = it->stack[it->sp - 1].string;
1782 if (!STRINGP (object))
1783 object = it->w->buffer;
1784 x_append_stretch_glyph (it, object, width, height, ascent);
1787 it->pixel_width = width;
1788 it->ascent = it->phys_ascent = height * ascent;
1789 it->descent = it->phys_descent = height - it->ascent;
1790 it->nglyphs = 1;
1792 if (face->box != FACE_NO_BOX)
1794 it->ascent += face->box_line_width;
1795 it->descent += face->box_line_width;
1797 if (it->start_of_box_run_p)
1798 it->pixel_width += face->box_line_width;
1799 if (it->end_of_box_run_p)
1800 it->pixel_width += face->box_line_width;
1803 take_vertical_position_into_account (it);
1806 /* Return proper value to be used as baseline offset of font that has
1807 ASCENT and DESCENT to draw characters by the font at the vertical
1808 center of the line of frame F.
1810 Here, out task is to find the value of BOFF in the following figure;
1812 -------------------------+-----------+-
1813 -+-+---------+-+ | |
1814 | | | | | |
1815 | | | | F_ASCENT F_HEIGHT
1816 | | | ASCENT | |
1817 HEIGHT | | | | |
1818 | | |-|-+------+-----------|------- baseline
1819 | | | | BOFF | |
1820 | |---------|-+-+ | |
1821 | | | DESCENT | |
1822 -+-+---------+-+ F_DESCENT |
1823 -------------------------+-----------+-
1825 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1826 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1827 DESCENT = FONT->descent
1828 HEIGHT = FONT_HEIGHT (FONT)
1829 F_DESCENT = (F->output_data.x->font->descent
1830 - F->output_data.x->baseline_offset)
1831 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1834 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1835 (FONT_DESCENT (FONT) \
1836 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1837 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
1839 /* Produce glyphs/get display metrics for the display element IT is
1840 loaded with. See the description of struct display_iterator in
1841 dispextern.h for an overview of struct display_iterator. */
1843 static void
1844 x_produce_glyphs (it)
1845 struct it *it;
1847 it->glyph_not_available_p = 0;
1849 if (it->what == IT_CHARACTER)
1851 wchar_t char2b;
1852 XFontStruct *font;
1853 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1854 XCharStruct *pcm;
1855 int font_not_found_p;
1856 struct font_info *font_info;
1857 int boff; /* baseline offset */
1858 HDC hdc;
1860 hdc = get_frame_dc (it->f);
1862 /* Maybe translate single-byte characters to multibyte, or the
1863 other way. */
1864 it->char_to_display = it->c;
1865 if (!ASCII_BYTE_P (it->c))
1867 if (unibyte_display_via_language_environment
1868 && SINGLE_BYTE_CHAR_P (it->c)
1869 && (it->c >= 0240
1870 || !NILP (Vnonascii_translation_table)))
1872 it->char_to_display = unibyte_char_to_multibyte (it->c);
1873 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1874 face = FACE_FROM_ID (it->f, it->face_id);
1876 else if (!SINGLE_BYTE_CHAR_P (it->c)
1877 && !it->multibyte_p)
1879 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1880 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1881 face = FACE_FROM_ID (it->f, it->face_id);
1885 /* Get font to use. Encode IT->char_to_display. */
1886 x_get_char_face_and_encoding (it->f, it->char_to_display,
1887 it->face_id, &char2b,
1888 it->multibyte_p);
1889 font = face->font;
1891 /* When no suitable font found, use the default font. */
1892 font_not_found_p = font == NULL;
1893 if (font_not_found_p)
1895 font = FRAME_FONT (it->f);
1896 boff = it->f->output_data.w32->baseline_offset;
1897 font_info = NULL;
1899 else
1901 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1902 boff = font_info->baseline_offset;
1903 if (font_info->vertical_centering)
1904 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1907 if (font->hfont)
1908 SelectObject (hdc, font->hfont);
1910 if (it->char_to_display >= ' '
1911 && (!it->multibyte_p || it->char_to_display < 128))
1913 /* Either unibyte or ASCII. */
1914 int stretched_p;
1916 it->nglyphs = 1;
1918 pcm = w32_per_char_metric (hdc, font, &char2b,
1919 font->bdf ? BDF_FONT : ANSI_FONT);
1920 it->ascent = FONT_BASE (font) + boff;
1921 it->descent = FONT_DESCENT (font) - boff;
1923 if (pcm)
1925 it->phys_ascent = pcm->ascent + boff;
1926 it->phys_descent = pcm->descent - boff;
1927 it->pixel_width = pcm->width;
1929 else
1931 it->glyph_not_available_p = 1;
1932 it->phys_ascent = FONT_BASE(font) + boff;
1933 it->phys_descent = FONT_DESCENT(font) - boff;
1934 it->pixel_width = FONT_WIDTH(font);
1937 /* If this is a space inside a region of text with
1938 `space-width' property, change its width. */
1939 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1940 if (stretched_p)
1941 it->pixel_width *= XFLOATINT (it->space_width);
1943 /* If face has a box, add the box thickness to the character
1944 height. If character has a box line to the left and/or
1945 right, add the box line width to the character's width. */
1946 if (face->box != FACE_NO_BOX)
1948 int thick = face->box_line_width;
1950 it->ascent += thick;
1951 it->descent += thick;
1953 if (it->start_of_box_run_p)
1954 it->pixel_width += thick;
1955 if (it->end_of_box_run_p)
1956 it->pixel_width += thick;
1959 /* If face has an overline, add the height of the overline
1960 (1 pixel) and a 1 pixel margin to the character height. */
1961 if (face->overline_p)
1962 it->ascent += 2;
1964 take_vertical_position_into_account (it);
1966 /* If we have to actually produce glyphs, do it. */
1967 if (it->glyph_row)
1969 if (stretched_p)
1971 /* Translate a space with a `space-width' property
1972 into a stretch glyph. */
1973 double ascent = (double) FONT_BASE (font)
1974 / FONT_HEIGHT (font);
1975 x_append_stretch_glyph (it, it->object, it->pixel_width,
1976 it->ascent + it->descent, ascent);
1978 else
1979 x_append_glyph (it);
1981 /* If characters with lbearing or rbearing are displayed
1982 in this line, record that fact in a flag of the
1983 glyph row. This is used to optimize X output code. */
1984 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1985 it->glyph_row->contains_overlapping_glyphs_p = 1;
1986 if (pcm)
1987 xfree (pcm);
1990 else if (it->char_to_display == '\n')
1992 /* A newline has no width but we need the height of the line. */
1993 it->pixel_width = 0;
1994 it->nglyphs = 0;
1995 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
1996 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
1998 if (face->box != FACE_NO_BOX)
2000 int thick = face->box_line_width;
2001 it->ascent += thick;
2002 it->descent += thick;
2005 else if (it->char_to_display == '\t')
2007 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
2008 int x = it->current_x + it->continuation_lines_width;
2009 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2011 it->pixel_width = next_tab_x - x;
2012 it->nglyphs = 1;
2013 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2014 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2016 if (it->glyph_row)
2018 double ascent = (double) it->ascent / (it->ascent + it->descent);
2019 x_append_stretch_glyph (it, it->object, it->pixel_width,
2020 it->ascent + it->descent, ascent);
2023 else
2025 /* A multi-byte character.
2026 If we found a font, this font should give us the right
2027 metrics. If we didn't find a font, use the frame's
2028 default font and calculate the width of the character
2029 from the charset width; this is what old redisplay code
2030 did. */
2031 pcm = w32_per_char_metric (hdc, font, &char2b,
2032 font->bdf ? BDF_FONT : UNICODE_FONT);
2034 if (font_not_found_p || !pcm)
2036 int charset = CHAR_CHARSET (it->char_to_display);
2038 it->glyph_not_available_p = 1;
2039 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2040 * CHARSET_WIDTH (charset));
2041 it->phys_ascent = FONT_BASE (font) + boff;
2042 it->phys_descent = FONT_DESCENT (font) - boff;
2044 else
2046 it->pixel_width = pcm->width;
2047 it->phys_ascent = pcm->ascent + boff;
2048 it->phys_descent = pcm->descent - boff;
2049 if (it->glyph_row
2050 && (pcm->lbearing < 0
2051 || pcm->rbearing > pcm->width))
2052 it->glyph_row->contains_overlapping_glyphs_p = 1;
2054 it->nglyphs = 1;
2055 it->ascent = FONT_BASE (font) + boff;
2056 it->descent = FONT_DESCENT (font) - boff;
2058 if (face->box != FACE_NO_BOX)
2060 int thick = face->box_line_width;
2061 it->ascent += thick;
2062 it->descent += thick;
2064 if (it->start_of_box_run_p)
2065 it->pixel_width += thick;
2066 if (it->end_of_box_run_p)
2067 it->pixel_width += thick;
2070 /* If face has an overline, add the height of the overline
2071 (1 pixel) and a 1 pixel margin to the character height. */
2072 if (face->overline_p)
2073 it->ascent += 2;
2075 take_vertical_position_into_account (it);
2077 if (it->glyph_row)
2078 x_append_glyph (it);
2080 if (pcm)
2081 xfree (pcm);
2083 release_frame_dc (it->f, hdc);
2085 else if (it->what == IT_COMPOSITION)
2087 /* NTEMACS_TODO: Composite glyphs. */
2089 else if (it->what == IT_IMAGE)
2090 x_produce_image_glyph (it);
2091 else if (it->what == IT_STRETCH)
2092 x_produce_stretch_glyph (it);
2094 /* Accumulate dimensions. */
2095 xassert (it->ascent >= 0 && it->descent > 0);
2096 if (it->area == TEXT_AREA)
2097 it->current_x += it->pixel_width;
2099 it->descent += it->extra_line_spacing;
2101 it->max_ascent = max (it->max_ascent, it->ascent);
2102 it->max_descent = max (it->max_descent, it->descent);
2103 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2104 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2108 /* Estimate the pixel height of the mode or top line on frame F.
2109 FACE_ID specifies what line's height to estimate. */
2112 x_estimate_mode_line_height (f, face_id)
2113 struct frame *f;
2114 enum face_id face_id;
2116 int height = 1;
2118 /* This function is called so early when Emacs starts that the face
2119 cache and mode line face are not yet initialized. */
2120 if (FRAME_FACE_CACHE (f))
2122 struct face *face = FACE_FROM_ID (f, face_id);
2123 if (face)
2124 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2127 return height;
2132 BOOL
2133 w32_use_unicode_for_codepage (codepage)
2134 int codepage;
2136 /* If the current codepage is supported, use Unicode for output. */
2137 return (w32_enable_unicode_output
2138 && codepage != CP_DEFAULT && IsValidCodePage (codepage));
2142 /***********************************************************************
2143 Glyph display
2144 ***********************************************************************/
2146 /* A sequence of glyphs to be drawn in the same face.
2148 This data structure is not really completely X specific, so it
2149 could possibly, at least partially, be useful for other systems. It
2150 is currently not part of the external redisplay interface because
2151 it's not clear what other systems will need. */
2153 struct glyph_string
2155 /* X-origin of the string. */
2156 int x;
2158 /* Y-origin and y-position of the base line of this string. */
2159 int y, ybase;
2161 /* The width of the string, not including a face extension. */
2162 int width;
2164 /* The width of the string, including a face extension. */
2165 int background_width;
2167 /* The height of this string. This is the height of the line this
2168 string is drawn in, and can be different from the height of the
2169 font the string is drawn in. */
2170 int height;
2172 /* Number of pixels this string overwrites in front of its x-origin.
2173 This number is zero if the string has an lbearing >= 0; it is
2174 -lbearing, if the string has an lbearing < 0. */
2175 int left_overhang;
2177 /* Number of pixels this string overwrites past its right-most
2178 nominal x-position, i.e. x + width. Zero if the string's
2179 rbearing is <= its nominal width, rbearing - width otherwise. */
2180 int right_overhang;
2182 /* The frame on which the glyph string is drawn. */
2183 struct frame *f;
2185 /* The window on which the glyph string is drawn. */
2186 struct window *w;
2188 /* X display and window for convenience. */
2189 Window window;
2191 /* The glyph row for which this string was built. It determines the
2192 y-origin and height of the string. */
2193 struct glyph_row *row;
2195 /* The area within row. */
2196 enum glyph_row_area area;
2198 /* Characters to be drawn, and number of characters. */
2199 wchar_t *char2b;
2200 int nchars;
2202 /* A face-override for drawing cursors, mouse face and similar. */
2203 enum draw_glyphs_face hl;
2205 /* Face in which this string is to be drawn. */
2206 struct face *face;
2208 /* Font in which this string is to be drawn. */
2209 XFontStruct *font;
2211 /* Font info for this string. */
2212 struct font_info *font_info;
2214 /* Non-null means this string describes (part of) a composition.
2215 All characters from char2b are drawn composed. */
2216 struct composition *cmp;
2218 /* Index of this glyph string's first character in the glyph
2219 definition of CMP. If this is zero, this glyph string describes
2220 the first character of a composition. */
2221 int gidx;
2223 /* 1 means this glyph strings face has to be drawn to the right end
2224 of the window's drawing area. */
2225 unsigned extends_to_end_of_line_p : 1;
2227 /* 1 means the background of this string has been drawn. */
2228 unsigned background_filled_p : 1;
2230 /* 1 means glyph string must be drawn with 16-bit functions. */
2231 unsigned two_byte_p : 1;
2233 /* 1 means that the original font determined for drawing this glyph
2234 string could not be loaded. The member `font' has been set to
2235 the frame's default font in this case. */
2236 unsigned font_not_found_p : 1;
2238 /* 1 means that the face in which this glyph string is drawn has a
2239 stipple pattern. */
2240 unsigned stippled_p : 1;
2242 /* 1 means only the foreground of this glyph string must be drawn,
2243 and we should use the physical height of the line this glyph
2244 string appears in as clip rect. */
2245 unsigned for_overlaps_p : 1;
2247 /* The GC to use for drawing this glyph string. */
2248 XGCValues *gc;
2250 HDC hdc;
2252 /* A pointer to the first glyph in the string. This glyph
2253 corresponds to char2b[0]. Needed to draw rectangles if
2254 font_not_found_p is 1. */
2255 struct glyph *first_glyph;
2257 /* Image, if any. */
2258 struct image *img;
2260 struct glyph_string *next, *prev;
2264 /* Encapsulate the different ways of displaying text under W32. */
2266 void W32_TEXTOUT(s, x, y,chars,nchars)
2267 struct glyph_string * s;
2268 int x, y;
2269 wchar_t * chars;
2270 int nchars;
2272 int charset_dim = w32_font_is_double_byte (s->gc->font) ? 2 : 1;
2273 if (s->gc->font->bdf)
2274 w32_BDF_TextOut (s->gc->font->bdf, s->hdc,
2275 x, y, (char *) chars, charset_dim, nchars, 0);
2276 else if (s->first_glyph->w32_font_type == UNICODE_FONT)
2277 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
2278 else
2279 ExtTextOut (s->hdc, x, y, 0, NULL, (char *) chars,
2280 nchars * charset_dim, NULL);
2283 #if 0
2285 static void
2286 x_dump_glyph_string (s)
2287 struct glyph_string *s;
2289 fprintf (stderr, "glyph string\n");
2290 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2291 s->x, s->y, s->width, s->height);
2292 fprintf (stderr, " ybase = %d\n", s->ybase);
2293 fprintf (stderr, " hl = %d\n", s->hl);
2294 fprintf (stderr, " left overhang = %d, right = %d\n",
2295 s->left_overhang, s->right_overhang);
2296 fprintf (stderr, " nchars = %d\n", s->nchars);
2297 fprintf (stderr, " extends to end of line = %d\n",
2298 s->extends_to_end_of_line_p);
2299 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2300 fprintf (stderr, " bg width = %d\n", s->background_width);
2303 #endif /* GLYPH_DEBUG */
2307 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2308 struct glyph_string **,
2309 struct glyph_string *,
2310 struct glyph_string *));
2311 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2312 struct glyph_string **,
2313 struct glyph_string *,
2314 struct glyph_string *));
2315 static void x_append_glyph_string P_ ((struct glyph_string **,
2316 struct glyph_string **,
2317 struct glyph_string *));
2318 static int x_left_overwritten P_ ((struct glyph_string *));
2319 static int x_left_overwriting P_ ((struct glyph_string *));
2320 static int x_right_overwritten P_ ((struct glyph_string *));
2321 static int x_right_overwriting P_ ((struct glyph_string *));
2322 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int,
2323 int, int));
2324 static void w32_init_glyph_string P_ ((struct glyph_string *, HDC hdc,
2325 wchar_t *, struct window *,
2326 struct glyph_row *,
2327 enum glyph_row_area, int,
2328 enum draw_glyphs_face));
2329 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2330 enum glyph_row_area, int, int,
2331 enum draw_glyphs_face, int *, int *, int));
2332 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2333 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2334 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2335 int));
2336 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2337 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2338 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2339 static void x_draw_glyph_string P_ ((struct glyph_string *));
2340 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2341 static void x_set_cursor_gc P_ ((struct glyph_string *));
2342 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2343 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2344 static void w32_get_glyph_overhangs P_ ((HDC hdc, struct glyph *,
2345 struct frame *,
2346 int *, int *));
2347 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2348 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
2349 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
2350 double, int, COLORREF));
2351 static void x_setup_relief_colors P_ ((struct glyph_string *));
2352 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2353 static void x_draw_image_relief P_ ((struct glyph_string *));
2354 static void x_draw_image_foreground P_ ((struct glyph_string *));
2355 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
2356 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2357 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2358 int, int, int));
2359 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2360 int, int, int, int, RECT *));
2361 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2362 int, int, int, RECT *));
2363 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2364 enum glyph_row_area));
2367 /* Append the list of glyph strings with head H and tail T to the list
2368 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2370 static INLINE void
2371 x_append_glyph_string_lists (head, tail, h, t)
2372 struct glyph_string **head, **tail;
2373 struct glyph_string *h, *t;
2375 if (h)
2377 if (*head)
2378 (*tail)->next = h;
2379 else
2380 *head = h;
2381 h->prev = *tail;
2382 *tail = t;
2387 /* Prepend the list of glyph strings with head H and tail T to the
2388 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2389 result. */
2391 static INLINE void
2392 x_prepend_glyph_string_lists (head, tail, h, t)
2393 struct glyph_string **head, **tail;
2394 struct glyph_string *h, *t;
2396 if (h)
2398 if (*head)
2399 (*head)->prev = t;
2400 else
2401 *tail = t;
2402 t->next = *head;
2403 *head = h;
2408 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2409 Set *HEAD and *TAIL to the resulting list. */
2411 static INLINE void
2412 x_append_glyph_string (head, tail, s)
2413 struct glyph_string **head, **tail;
2414 struct glyph_string *s;
2416 s->next = s->prev = NULL;
2417 x_append_glyph_string_lists (head, tail, s, s);
2421 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2422 face. */
2424 static void
2425 x_set_cursor_gc (s)
2426 struct glyph_string *s;
2428 if (s->font == FRAME_FONT (s->f)
2429 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2430 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2431 && !s->cmp)
2432 s->gc = s->f->output_data.w32->cursor_gc;
2433 else
2435 /* Cursor on non-default face: must merge. */
2436 XGCValues xgcv;
2437 unsigned long mask;
2439 xgcv.background = s->f->output_data.w32->cursor_pixel;
2440 xgcv.foreground = s->face->background;
2442 /* If the glyph would be invisible, try a different foreground. */
2443 if (xgcv.foreground == xgcv.background)
2444 xgcv.foreground = s->face->foreground;
2445 if (xgcv.foreground == xgcv.background)
2446 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
2447 if (xgcv.foreground == xgcv.background)
2448 xgcv.foreground = s->face->foreground;
2450 /* Make sure the cursor is distinct from text in this face. */
2451 if (xgcv.background == s->face->background
2452 && xgcv.foreground == s->face->foreground)
2454 xgcv.background = s->face->foreground;
2455 xgcv.foreground = s->face->background;
2458 IF_DEBUG (x_check_font (s->f, s->font));
2459 xgcv.font = s->font;
2460 mask = GCForeground | GCBackground | GCFont;
2462 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2463 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2464 mask, &xgcv);
2465 else
2466 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2467 = XCreateGC (NULL, s->window, mask, &xgcv);
2469 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2474 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2476 static void
2477 x_set_mouse_face_gc (s)
2478 struct glyph_string *s;
2480 int face_id;
2481 struct face *face;
2483 /* What face has to be used for the mouse face? */
2484 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
2485 face = FACE_FROM_ID (s->f, face_id);
2486 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2487 s->face = FACE_FROM_ID (s->f, face_id);
2488 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2490 /* If font in this face is same as S->font, use it. */
2491 if (s->font == s->face->font)
2492 s->gc = s->face->gc;
2493 else
2495 /* Otherwise construct scratch_cursor_gc with values from FACE
2496 but font FONT. */
2497 XGCValues xgcv;
2498 unsigned long mask;
2500 xgcv.background = s->face->background;
2501 xgcv.foreground = s->face->foreground;
2502 IF_DEBUG (x_check_font (s->f, s->font));
2503 xgcv.font = s->font;
2504 mask = GCForeground | GCBackground | GCFont;
2506 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2507 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2508 mask, &xgcv);
2509 else
2510 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2511 = XCreateGC (NULL, s->window, mask, &xgcv);
2513 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2516 xassert (s->gc != 0);
2520 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2521 Faces to use in the mode line have already been computed when the
2522 matrix was built, so there isn't much to do, here. */
2524 static INLINE void
2525 x_set_mode_line_face_gc (s)
2526 struct glyph_string *s;
2528 s->gc = s->face->gc;
2532 /* Set S->gc of glyph string S for drawing that glyph string. Set
2533 S->stippled_p to a non-zero value if the face of S has a stipple
2534 pattern. */
2536 static INLINE void
2537 x_set_glyph_string_gc (s)
2538 struct glyph_string *s;
2540 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2542 if (s->hl == DRAW_NORMAL_TEXT)
2544 s->gc = s->face->gc;
2545 s->stippled_p = s->face->stipple != 0;
2547 else if (s->hl == DRAW_INVERSE_VIDEO)
2549 x_set_mode_line_face_gc (s);
2550 s->stippled_p = s->face->stipple != 0;
2552 else if (s->hl == DRAW_CURSOR)
2554 x_set_cursor_gc (s);
2555 s->stippled_p = 0;
2557 else if (s->hl == DRAW_MOUSE_FACE)
2559 x_set_mouse_face_gc (s);
2560 s->stippled_p = s->face->stipple != 0;
2562 else if (s->hl == DRAW_IMAGE_RAISED
2563 || s->hl == DRAW_IMAGE_SUNKEN)
2565 s->gc = s->face->gc;
2566 s->stippled_p = s->face->stipple != 0;
2568 else
2570 s->gc = s->face->gc;
2571 s->stippled_p = s->face->stipple != 0;
2574 /* GC must have been set. */
2575 xassert (s->gc != 0);
2579 /* Return in *R the clipping rectangle for glyph string S. */
2581 static void
2582 w32_get_glyph_string_clip_rect (s, r)
2583 struct glyph_string *s;
2584 RECT *r;
2586 int r_height, r_width;
2588 if (s->row->full_width_p)
2590 /* Draw full-width. X coordinates are relative to S->w->left. */
2591 int canon_x = CANON_X_UNIT (s->f);
2593 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2594 r_width = XFASTINT (s->w->width) * canon_x;
2596 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2598 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2599 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2600 r->left -= width;
2603 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2605 /* Unless displaying a mode or menu bar line, which are always
2606 fully visible, clip to the visible part of the row. */
2607 if (s->w->pseudo_window_p)
2608 r_height = s->row->visible_height;
2609 else
2610 r_height = s->height;
2612 else
2614 /* This is a text line that may be partially visible. */
2615 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2616 r_width = window_box_width (s->w, s->area);
2617 r_height = s->row->visible_height;
2620 /* Don't use S->y for clipping because it doesn't take partially
2621 visible lines into account. For example, it can be negative for
2622 partially visible lines at the top of a window. */
2623 if (!s->row->full_width_p
2624 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2625 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2626 else
2627 r->top = max (0, s->row->y);
2629 /* If drawing a tool-bar window, draw it over the internal border
2630 at the top of the window. */
2631 if (s->w == XWINDOW (s->f->tool_bar_window))
2632 r->top -= s->f->output_data.w32->internal_border_width;
2634 /* If S draws overlapping rows, it's sufficient to use the top and
2635 bottom of the window for clipping because this glyph string
2636 intentionally draws over other lines. */
2637 if (s->for_overlaps_p)
2639 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2640 r_height = window_text_bottom_y (s->w) - r->top;
2643 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
2645 r->bottom = r->top + r_height;
2646 r->right = r->left + r_width;
2650 /* Set clipping for output of glyph string S. S may be part of a mode
2651 line or menu if we don't have X toolkit support. */
2653 static INLINE void
2654 x_set_glyph_string_clipping (s)
2655 struct glyph_string *s;
2657 RECT r;
2658 w32_get_glyph_string_clip_rect (s, &r);
2659 w32_set_clip_rectangle (s->hdc, &r);
2663 /* Compute left and right overhang of glyph string S. If S is a glyph
2664 string for a composition, assume overhangs don't exist. */
2666 static INLINE void
2667 x_compute_glyph_string_overhangs (s)
2668 struct glyph_string *s;
2670 /* NTEMACS_TODO: Windows does not appear to have a method for
2671 getting this info without getting the ABC widths for each
2672 individual character and working it out manually. */
2676 /* Compute overhangs and x-positions for glyph string S and its
2677 predecessors, or successors. X is the starting x-position for S.
2678 BACKWARD_P non-zero means process predecessors. */
2680 static void
2681 x_compute_overhangs_and_x (s, x, backward_p)
2682 struct glyph_string *s;
2683 int x;
2684 int backward_p;
2686 if (backward_p)
2688 while (s)
2690 x_compute_glyph_string_overhangs (s);
2691 x -= s->width;
2692 s->x = x;
2693 s = s->prev;
2696 else
2698 while (s)
2700 x_compute_glyph_string_overhangs (s);
2701 s->x = x;
2702 x += s->width;
2703 s = s->next;
2709 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2710 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2711 assumed to be zero. */
2713 static void
2714 w32_get_glyph_overhangs (hdc, glyph, f, left, right)
2715 HDC hdc;
2716 struct glyph *glyph;
2717 struct frame *f;
2718 int *left, *right;
2720 int c;
2722 *left = *right = 0;
2724 if (glyph->type == CHAR_GLYPH)
2726 XFontStruct *font;
2727 struct face *face;
2728 wchar_t char2b;
2729 XCharStruct *pcm;
2731 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2732 font = face->font;
2734 if (font
2735 && (pcm = w32_per_char_metric (hdc, font, &char2b,
2736 glyph->w32_font_type)))
2738 if (pcm->rbearing > pcm->width)
2739 *right = pcm->rbearing - pcm->width;
2740 if (pcm->lbearing < 0)
2741 *left = -pcm->lbearing;
2742 xfree (pcm);
2748 static void
2749 x_get_glyph_overhangs (glyph, f, left, right)
2750 struct glyph *glyph;
2751 struct frame *f;
2752 int *left, *right;
2754 HDC hdc = get_frame_dc (f);
2755 /* Convert to unicode! */
2756 w32_get_glyph_overhangs (hdc, glyph, f, left, right);
2757 release_frame_dc (f, hdc);
2761 /* Return the index of the first glyph preceding glyph string S that
2762 is overwritten by S because of S's left overhang. Value is -1
2763 if no glyphs are overwritten. */
2765 static int
2766 x_left_overwritten (s)
2767 struct glyph_string *s;
2769 int k;
2771 if (s->left_overhang)
2773 int x = 0, i;
2774 struct glyph *glyphs = s->row->glyphs[s->area];
2775 int first = s->first_glyph - glyphs;
2777 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2778 x -= glyphs[i].pixel_width;
2780 k = i + 1;
2782 else
2783 k = -1;
2785 return k;
2789 /* Return the index of the first glyph preceding glyph string S that
2790 is overwriting S because of its right overhang. Value is -1 if no
2791 glyph in front of S overwrites S. */
2793 static int
2794 x_left_overwriting (s)
2795 struct glyph_string *s;
2797 int i, k, x;
2798 struct glyph *glyphs = s->row->glyphs[s->area];
2799 int first = s->first_glyph - glyphs;
2801 k = -1;
2802 x = 0;
2803 for (i = first - 1; i >= 0; --i)
2805 int left, right;
2806 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
2807 if (x + right > 0)
2808 k = i;
2809 x -= glyphs[i].pixel_width;
2812 return k;
2816 /* Return the index of the last glyph following glyph string S that is
2817 not overwritten by S because of S's right overhang. Value is -1 if
2818 no such glyph is found. */
2820 static int
2821 x_right_overwritten (s)
2822 struct glyph_string *s;
2824 int k = -1;
2826 if (s->right_overhang)
2828 int x = 0, i;
2829 struct glyph *glyphs = s->row->glyphs[s->area];
2830 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2831 int end = s->row->used[s->area];
2833 for (i = first; i < end && s->right_overhang > x; ++i)
2834 x += glyphs[i].pixel_width;
2836 k = i;
2839 return k;
2843 /* Return the index of the last glyph following glyph string S that
2844 overwrites S because of its left overhang. Value is negative
2845 if no such glyph is found. */
2847 static int
2848 x_right_overwriting (s)
2849 struct glyph_string *s;
2851 int i, k, x;
2852 int end = s->row->used[s->area];
2853 struct glyph *glyphs = s->row->glyphs[s->area];
2854 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2856 k = -1;
2857 x = 0;
2858 for (i = first; i < end; ++i)
2860 int left, right;
2861 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
2862 if (x - left < 0)
2863 k = i;
2864 x += glyphs[i].pixel_width;
2867 return k;
2871 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2873 static INLINE void
2874 x_clear_glyph_string_rect (s, x, y, w, h)
2875 struct glyph_string *s;
2876 int x, y, w, h;
2878 int real_x = x;
2879 int real_y = y;
2880 int real_w = w;
2881 int real_h = h;
2882 #if 0
2883 /* Take clipping into account. */
2884 if (s->gc->clip_mask == Rect)
2886 real_x = max (real_x, s->gc->clip_rectangle.left);
2887 real_y = max (real_y, s->gc->clip_rectangle.top);
2888 real_w = min (real_w, s->gc->clip_rectangle.right
2889 - s->gc->clip_rectangle.left);
2890 real_h = min (real_h, s->gc->clip_rectangle.bottom
2891 - s->gc->clip_rectangle.top);
2893 #endif
2894 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
2895 real_w, real_h);
2899 /* Draw the background of glyph_string S. If S->background_filled_p
2900 is non-zero don't draw it. FORCE_P non-zero means draw the
2901 background even if it wouldn't be drawn normally. This is used
2902 when a string preceding S draws into the background of S, or S
2903 contains the first component of a composition. */
2905 static void
2906 x_draw_glyph_string_background (s, force_p)
2907 struct glyph_string *s;
2908 int force_p;
2910 /* Nothing to do if background has already been drawn or if it
2911 shouldn't be drawn in the first place. */
2912 if (!s->background_filled_p)
2914 #if 0 /* NTEMACS_TODO: stipple */
2915 if (s->stippled_p)
2917 /* Fill background with a stipple pattern. */
2918 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2919 XFillRectangle (s->display, s->window, s->gc, s->x,
2920 s->y + s->face->box_line_width,
2921 s->background_width,
2922 s->height - 2 * s->face->box_line_width);
2923 XSetFillStyle (s->display, s->gc, FillSolid);
2924 s->background_filled_p = 1;
2926 else
2927 #endif
2928 if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
2929 || s->font_not_found_p
2930 || s->extends_to_end_of_line_p
2931 || force_p)
2933 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
2934 s->background_width,
2935 s->height - 2 * s->face->box_line_width);
2936 s->background_filled_p = 1;
2942 /* Draw the foreground of glyph string S. */
2944 static void
2945 x_draw_glyph_string_foreground (s)
2946 struct glyph_string *s;
2948 int i, x;
2950 /* If first glyph of S has a left box line, start drawing the text
2951 of S to the right of that box line. */
2952 if (s->face->box != FACE_NO_BOX
2953 && s->first_glyph->left_box_line_p)
2954 x = s->x + s->face->box_line_width;
2955 else
2956 x = s->x;
2958 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
2959 SetBkMode (s->hdc, TRANSPARENT);
2960 else
2961 SetBkMode (s->hdc, OPAQUE);
2963 SetTextColor (s->hdc, s->gc->foreground);
2964 SetBkColor (s->hdc, s->gc->background);
2965 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
2967 if (s->font && s->font->hfont)
2968 SelectObject (s->hdc, s->font->hfont);
2970 /* Draw characters of S as rectangles if S's font could not be
2971 loaded. */
2972 if (s->font_not_found_p)
2974 for (i = 0; i < s->nchars; ++i)
2976 struct glyph *g = s->first_glyph + i;
2978 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
2979 s->height - 1);
2980 x += g->pixel_width;
2983 else
2985 char *char1b = (char *) s->char2b;
2986 int boff = s->font_info->baseline_offset;
2988 if (s->font_info->vertical_centering)
2989 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
2991 /* If we can use 8-bit functions, condense S->char2b. */
2992 if (!s->two_byte_p)
2993 for (i = 0; i < s->nchars; ++i)
2994 char1b[i] = BYTE2 (s->char2b[i]);
2996 /* Draw text with TextOut and friends. */
2997 W32_TEXTOUT (s, x, s->ybase - boff, s->char2b, s->nchars);
3001 /* Draw the foreground of composite glyph string S. */
3003 static void
3004 x_draw_composite_glyph_string_foreground (s)
3005 struct glyph_string *s;
3007 int i, x;
3009 /* If first glyph of S has a left box line, start drawing the text
3010 of S to the right of that box line. */
3011 if (s->face->box != FACE_NO_BOX
3012 && s->first_glyph->left_box_line_p)
3013 x = s->x + s->face->box_line_width;
3014 else
3015 x = s->x;
3017 /* S is a glyph string for a composition. S->gidx is the index of
3018 the first character drawn for glyphs of this composition.
3019 S->gidx == 0 means we are drawing the very first character of
3020 this composition. */
3022 SetTextColor (s->hdc, s->gc->foreground);
3023 SetBkColor (s->hdc, s->gc->background);
3024 SetBkMode (s->hdc, TRANSPARENT);
3025 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3027 /* Draw a rectangle for the composition if the font for the very
3028 first character of the composition could not be loaded. */
3029 if (s->font_not_found_p)
3031 if (s->gidx == 0)
3032 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
3033 s->height - 1);
3035 else
3037 for (i = 0; i < s->nchars; i++, ++s->gidx)
3038 W32_TEXTOUT (s, x + s->cmp->offsets[s->gidx * 2],
3039 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3040 s->char2b + i, 1);
3044 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
3045 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3046 If this produces the same color as COLOR, try a color where all RGB
3047 values have DELTA added. Return the allocated color in *COLOR.
3048 DISPLAY is the X display, CMAP is the colormap to operate on.
3049 Value is non-zero if successful. */
3051 static int
3052 w32_alloc_lighter_color (f, color, factor, delta)
3053 struct frame *f;
3054 COLORREF *color;
3055 double factor;
3056 int delta;
3058 COLORREF new;
3060 /* Change RGB values by specified FACTOR. Avoid overflow! */
3061 xassert (factor >= 0);
3062 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
3063 min (0xff, factor * GetGValue (*color)),
3064 min (0xff, factor * GetBValue (*color)));
3065 if (new == *color)
3066 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
3067 max (0, min (0xff, delta + GetGValue (*color))),
3068 max (0, min (0xff, delta + GetBValue (*color))));
3070 /* NTEMACS_TODO: Map to palette and retry with delta if same? */
3071 /* NTEMACS_TODO: Free colors (if using palette)? */
3073 if (new == *color)
3074 return 0;
3076 *color = new;
3078 return 1;
3082 /* Set up the foreground color for drawing relief lines of glyph
3083 string S. RELIEF is a pointer to a struct relief containing the GC
3084 with which lines will be drawn. Use a color that is FACTOR or
3085 DELTA lighter or darker than the relief's background which is found
3086 in S->f->output_data.x->relief_background. If such a color cannot
3087 be allocated, use DEFAULT_PIXEL, instead. */
3089 static void
3090 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
3091 struct frame *f;
3092 struct relief *relief;
3093 double factor;
3094 int delta;
3095 COLORREF default_pixel;
3097 XGCValues xgcv;
3098 struct w32_output *di = f->output_data.w32;
3099 unsigned long mask = GCForeground;
3100 COLORREF pixel;
3101 COLORREF background = di->relief_background;
3102 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3104 /* NTEMACS_TODO: Free colors (if using palette)? */
3106 /* Allocate new color. */
3107 xgcv.foreground = default_pixel;
3108 pixel = background;
3109 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
3111 relief->allocated_p = 1;
3112 xgcv.foreground = relief->pixel = pixel;
3115 if (relief->gc == 0)
3117 #if 0 /* NTEMACS_TODO: stipple */
3118 xgcv.stipple = dpyinfo->gray;
3119 mask |= GCStipple;
3120 #endif
3121 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
3123 else
3124 XChangeGC (NULL, relief->gc, mask, &xgcv);
3128 /* Set up colors for the relief lines around glyph string S. */
3130 static void
3131 x_setup_relief_colors (s)
3132 struct glyph_string *s;
3134 struct w32_output *di = s->f->output_data.w32;
3135 COLORREF color;
3137 if (s->face->use_box_color_for_shadows_p)
3138 color = s->face->box_color;
3139 else
3140 color = s->gc->background;
3142 if (di->white_relief.gc == 0
3143 || color != di->relief_background)
3145 di->relief_background = color;
3146 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3147 WHITE_PIX_DEFAULT (s->f));
3148 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3149 BLACK_PIX_DEFAULT (s->f));
3154 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3155 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3156 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3157 relief. LEFT_P non-zero means draw a relief on the left side of
3158 the rectangle. RIGHT_P non-zero means draw a relief on the right
3159 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3160 when drawing. */
3162 static void
3163 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3164 raised_p, left_p, right_p, clip_rect)
3165 struct frame *f;
3166 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3167 RECT *clip_rect;
3169 int i;
3170 XGCValues gc;
3171 HDC hdc = get_frame_dc (f);
3173 if (raised_p)
3174 gc.foreground = PALETTERGB (255, 255, 255);
3175 else
3176 gc.foreground = PALETTERGB (0, 0, 0);
3178 w32_set_clip_rectangle (hdc, clip_rect);
3180 /* Top. */
3181 for (i = 0; i < width; ++i)
3183 w32_fill_area (f, hdc, gc.foreground,
3184 left_x + i * left_p, top_y + i,
3185 (right_x + 1 - i * right_p) - (left_x + i * left_p), 1);
3188 /* Left. */
3189 if (left_p)
3190 for (i = 0; i < width; ++i)
3192 w32_fill_area (f, hdc, gc.foreground,
3193 left_x + i, top_y + i, 1,
3194 (bottom_y - i) - (top_y + i));
3197 w32_set_clip_rectangle (hdc, NULL);
3199 if (raised_p)
3200 gc.foreground = PALETTERGB (0, 0, 0);
3201 else
3202 gc.foreground = PALETTERGB (255, 255, 255);
3204 w32_set_clip_rectangle (hdc, clip_rect);
3206 /* Bottom. */
3207 for (i = 0; i < width; ++i)
3209 w32_fill_area (f, hdc, gc.foreground,
3210 left_x + i * left_p, bottom_y - i,
3211 (right_x + 1 - i * right_p) - left_x + i * left_p, 1);
3214 /* Right. */
3215 if (right_p)
3216 for (i = 0; i < width; ++i)
3218 w32_fill_area (f, hdc, gc.foreground,
3219 right_x - i, top_y + i + 1, 1,
3220 (bottom_y - i) - (top_y + i + 1));
3223 w32_set_clip_rectangle (hdc, NULL);
3225 release_frame_dc (f, hdc);
3229 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3230 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3231 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3232 left side of the rectangle. RIGHT_P non-zero means draw a line
3233 on the right side of the rectangle. CLIP_RECT is the clipping
3234 rectangle to use when drawing. */
3236 static void
3237 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3238 left_p, right_p, clip_rect)
3239 struct glyph_string *s;
3240 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3241 RECT *clip_rect;
3243 w32_set_clip_rectangle (s->hdc, clip_rect);
3245 /* Top. */
3246 w32_fill_area (s->f, s->hdc, s->face->box_color,
3247 left_x, top_y, right_x - left_x, width);
3249 /* Left. */
3250 if (left_p)
3252 w32_fill_area (s->f, s->hdc, s->face->box_color,
3253 left_x, top_y, width, bottom_y - top_y);
3256 /* Bottom. */
3257 w32_fill_area (s->f, s->hdc, s->face->box_color,
3258 left_x, bottom_y - width, right_x - left_x, width);
3260 /* Right. */
3261 if (right_p)
3263 w32_fill_area (s->f, s->hdc, s->face->box_color,
3264 right_x - width, top_y, width, bottom_y - top_y);
3267 w32_set_clip_rectangle (s->hdc, NULL);
3271 /* Draw a box around glyph string S. */
3273 static void
3274 x_draw_glyph_string_box (s)
3275 struct glyph_string *s;
3277 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3278 int left_p, right_p;
3279 struct glyph *last_glyph;
3280 RECT clip_rect;
3282 last_x = window_box_right (s->w, s->area);
3283 if (s->row->full_width_p
3284 && !s->w->pseudo_window_p)
3286 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3287 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3288 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3291 /* The glyph that may have a right box line. */
3292 last_glyph = (s->cmp || s->img
3293 ? s->first_glyph
3294 : s->first_glyph + s->nchars - 1);
3296 width = s->face->box_line_width;
3297 raised_p = s->face->box == FACE_RAISED_BOX;
3298 left_x = s->x;
3299 right_x = ((s->row->full_width_p
3300 ? last_x - 1
3301 : min (last_x, s->x + s->background_width) - 1));
3302 top_y = s->y;
3303 bottom_y = top_y + s->height - 1;
3305 left_p = (s->first_glyph->left_box_line_p
3306 || (s->hl == DRAW_MOUSE_FACE
3307 && (s->prev == NULL
3308 || s->prev->hl != s->hl)));
3309 right_p = (last_glyph->right_box_line_p
3310 || (s->hl == DRAW_MOUSE_FACE
3311 && (s->next == NULL
3312 || s->next->hl != s->hl)));
3314 w32_get_glyph_string_clip_rect (s, &clip_rect);
3316 if (s->face->box == FACE_SIMPLE_BOX)
3317 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3318 left_p, right_p, &clip_rect);
3319 else
3321 x_setup_relief_colors (s);
3322 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3323 width, raised_p, left_p, right_p, &clip_rect);
3328 /* Draw foreground of image glyph string S. */
3330 static void
3331 x_draw_image_foreground (s)
3332 struct glyph_string *s;
3334 int x;
3335 int y = s->ybase - image_ascent (s->img, s->face);
3337 /* If first glyph of S has a left box line, start drawing it to the
3338 right of that line. */
3339 if (s->face->box != FACE_NO_BOX
3340 && s->first_glyph->left_box_line_p)
3341 x = s->x + s->face->box_line_width;
3342 else
3343 x = s->x;
3345 /* If there is a margin around the image, adjust x- and y-position
3346 by that margin. */
3347 if (s->img->margin)
3349 x += s->img->margin;
3350 y += s->img->margin;
3353 SaveDC (s->hdc);
3355 if (s->img->pixmap)
3357 #if 0 /* NTEMACS_TODO: image mask */
3358 if (s->img->mask)
3360 /* We can't set both a clip mask and use XSetClipRectangles
3361 because the latter also sets a clip mask. We also can't
3362 trust on the shape extension to be available
3363 (XShapeCombineRegion). So, compute the rectangle to draw
3364 manually. */
3365 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3366 | GCFunction);
3367 XGCValues xgcv;
3368 XRectangle clip_rect, image_rect, r;
3370 xgcv.clip_mask = s->img->mask;
3371 xgcv.clip_x_origin = x;
3372 xgcv.clip_y_origin = y;
3373 xgcv.function = GXcopy;
3374 XChangeGC (s->display, s->gc, mask, &xgcv);
3376 w32_get_glyph_string_clip_rect (s, &clip_rect);
3377 image_rect.x = x;
3378 image_rect.y = y;
3379 image_rect.width = s->img->width;
3380 image_rect.height = s->img->height;
3381 if (IntersectRect (&r, &clip_rect, &image_rect))
3382 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3383 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3385 else
3386 #endif
3388 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3389 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3390 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3391 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3392 x_set_glyph_string_clipping (s);
3394 SetTextColor (s->hdc, s->gc->foreground);
3395 SetBkColor (s->hdc, s->gc->background);
3396 #if 0 /* From w32bdf.c (which is from Meadow). */
3397 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3398 compat_hdc, 0, 0, SRCCOPY);
3399 #else
3400 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3401 compat_hdc, 0, 0, 0xB8074A);
3402 #endif
3403 SelectObject (s->hdc, orig_brush);
3404 DeleteObject (fg_brush);
3405 SelectObject (compat_hdc, orig_obj);
3406 DeleteDC (compat_hdc);
3408 /* When the image has a mask, we can expect that at
3409 least part of a mouse highlight or a block cursor will
3410 be visible. If the image doesn't have a mask, make
3411 a block cursor visible by drawing a rectangle around
3412 the image. I believe it's looking better if we do
3413 nothing here for mouse-face. */
3414 if (s->hl == DRAW_CURSOR)
3415 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width - 1,
3416 s->img->height - 1);
3417 w32_set_clip_rectangle(s->hdc, NULL);
3420 else
3421 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width -1,
3422 s->img->height - 1);
3424 RestoreDC (s->hdc ,-1);
3429 /* Draw a relief around the image glyph string S. */
3431 static void
3432 x_draw_image_relief (s)
3433 struct glyph_string *s;
3435 int x0, y0, x1, y1, thick, raised_p;
3436 RECT r;
3437 int x;
3438 int y = s->ybase - image_ascent (s->img, s->face);
3440 /* If first glyph of S has a left box line, start drawing it to the
3441 right of that line. */
3442 if (s->face->box != FACE_NO_BOX
3443 && s->first_glyph->left_box_line_p)
3444 x = s->x + s->face->box_line_width;
3445 else
3446 x = s->x;
3448 /* If there is a margin around the image, adjust x- and y-position
3449 by that margin. */
3450 if (s->img->margin)
3452 x += s->img->margin;
3453 y += s->img->margin;
3456 if (s->hl == DRAW_IMAGE_SUNKEN
3457 || s->hl == DRAW_IMAGE_RAISED)
3459 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3460 raised_p = s->hl == DRAW_IMAGE_RAISED;
3462 else
3464 thick = abs (s->img->relief);
3465 raised_p = s->img->relief > 0;
3468 x0 = x - thick;
3469 y0 = y - thick;
3470 x1 = x + s->img->width + thick - 1;
3471 y1 = y + s->img->height + thick - 1;
3473 x_setup_relief_colors (s);
3474 w32_get_glyph_string_clip_rect (s, &r);
3475 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3479 /* Draw the foreground of image glyph string S to PIXMAP. */
3481 static void
3482 w32_draw_image_foreground_1 (s, pixmap)
3483 struct glyph_string *s;
3484 HBITMAP pixmap;
3486 HDC hdc = CreateCompatibleDC (s->hdc);
3487 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
3488 int x;
3489 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3491 /* If first glyph of S has a left box line, start drawing it to the
3492 right of that line. */
3493 if (s->face->box != FACE_NO_BOX
3494 && s->first_glyph->left_box_line_p)
3495 x = s->face->box_line_width;
3496 else
3497 x = 0;
3499 /* If there is a margin around the image, adjust x- and y-position
3500 by that margin. */
3501 if (s->img->margin)
3503 x += s->img->margin;
3504 y += s->img->margin;
3507 if (s->img->pixmap)
3509 #if 0 /* NTEMACS_TODO: image mask */
3510 if (s->img->mask)
3512 /* We can't set both a clip mask and use XSetClipRectangles
3513 because the latter also sets a clip mask. We also can't
3514 trust on the shape extension to be available
3515 (XShapeCombineRegion). So, compute the rectangle to draw
3516 manually. */
3517 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3518 | GCFunction);
3519 XGCValues xgcv;
3521 xgcv.clip_mask = s->img->mask;
3522 xgcv.clip_x_origin = x;
3523 xgcv.clip_y_origin = y;
3524 xgcv.function = GXcopy;
3525 XChangeGC (s->display, s->gc, mask, &xgcv);
3527 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3528 0, 0, s->img->width, s->img->height, x, y);
3529 XSetClipMask (s->display, s->gc, None);
3531 else
3532 #endif
3534 HDC compat_hdc = CreateCompatibleDC (hdc);
3535 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3536 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
3537 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3539 SetTextColor (hdc, s->gc->foreground);
3540 SetBkColor (hdc, s->gc->background);
3541 #if 0 /* From w32bdf.c (which is from Meadow). */
3542 BitBlt (hdc, x, y, s->img->width, s->img->height,
3543 compat_hdc, 0, 0, SRCCOPY);
3544 #else
3545 BitBlt (hdc, x, y, s->img->width, s->img->height,
3546 compat_hdc, 0, 0, 0xB8074A);
3547 #endif
3548 SelectObject (hdc, orig_brush);
3549 DeleteObject (fg_brush);
3550 SelectObject (compat_hdc, orig_obj);
3551 DeleteDC (compat_hdc);
3553 /* When the image has a mask, we can expect that at
3554 least part of a mouse highlight or a block cursor will
3555 be visible. If the image doesn't have a mask, make
3556 a block cursor visible by drawing a rectangle around
3557 the image. I believe it's looking better if we do
3558 nothing here for mouse-face. */
3559 if (s->hl == DRAW_CURSOR)
3560 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
3561 s->img->height - 1);
3564 else
3565 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
3566 s->img->height - 1);
3568 SelectObject (hdc, orig_hdc_obj);
3569 DeleteDC (hdc);
3573 /* Draw part of the background of glyph string S. X, Y, W, and H
3574 give the rectangle to draw. */
3576 static void
3577 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3578 struct glyph_string *s;
3579 int x, y, w, h;
3581 #if 0 /* NTEMACS_TODO: stipple */
3582 if (s->stippled_p)
3584 /* Fill background with a stipple pattern. */
3585 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3586 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3587 XSetFillStyle (s->display, s->gc, FillSolid);
3589 else
3590 #endif
3591 x_clear_glyph_string_rect (s, x, y, w, h);
3595 /* Draw image glyph string S.
3597 s->y
3598 s->x +-------------------------
3599 | s->face->box
3601 | +-------------------------
3602 | | s->img->margin
3604 | | +-------------------
3605 | | | the image
3609 static void
3610 x_draw_image_glyph_string (s)
3611 struct glyph_string *s;
3613 int x, y;
3614 int box_line_width = s->face->box_line_width;
3615 int margin = s->img->margin;
3616 int height;
3617 HBITMAP pixmap = 0;
3619 height = s->height - 2 * box_line_width;
3621 /* Fill background with face under the image. Do it only if row is
3622 taller than image or if image has a clip mask to reduce
3623 flickering. */
3624 s->stippled_p = s->face->stipple != 0;
3625 if (height > s->img->height
3626 || margin
3627 #if 0 /* NTEMACS_TODO: image mask */
3628 || s->img->mask
3629 #endif
3630 || s->img->pixmap == 0
3631 || s->width != s->background_width)
3633 if (box_line_width && s->first_glyph->left_box_line_p)
3634 x = s->x + box_line_width;
3635 else
3636 x = s->x;
3638 y = s->y + box_line_width;
3639 #if 0 /* NTEMACS_TODO: image mask */
3640 if (s->img->mask)
3642 /* Create a pixmap as large as the glyph string Fill it with
3643 the background color. Copy the image to it, using its
3644 mask. Copy the temporary pixmap to the display. */
3645 Screen *screen = FRAME_X_SCREEN (s->f);
3646 int depth = DefaultDepthOfScreen (screen);
3648 /* Create a pixmap as large as the glyph string. */
3649 pixmap = XCreatePixmap (s->display, s->window,
3650 s->background_width,
3651 s->height, depth);
3653 /* Don't clip in the following because we're working on the
3654 pixmap. */
3655 XSetClipMask (s->display, s->gc, None);
3657 /* Fill the pixmap with the background color/stipple. */
3658 if (s->stippled_p)
3660 /* Fill background with a stipple pattern. */
3661 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3662 XFillRectangle (s->display, pixmap, s->gc,
3663 0, 0, s->background_width, s->height);
3664 XSetFillStyle (s->display, s->gc, FillSolid);
3666 else
3668 XGCValues xgcv;
3669 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3670 &xgcv);
3671 XSetForeground (s->display, s->gc, xgcv.background);
3672 XFillRectangle (s->display, pixmap, s->gc,
3673 0, 0, s->background_width, s->height);
3674 XSetForeground (s->display, s->gc, xgcv.foreground);
3677 else
3678 #endif
3679 /* Implementation idea: Is it possible to construct a mask?
3680 We could look at the color at the margins of the image, and
3681 say that this color is probably the background color of the
3682 image. */
3683 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3685 s->background_filled_p = 1;
3688 /* Draw the foreground. */
3689 if (pixmap != 0)
3691 w32_draw_image_foreground_1 (s, pixmap);
3692 x_set_glyph_string_clipping (s);
3694 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3695 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3696 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3697 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
3699 SetTextColor (s->hdc, s->gc->foreground);
3700 SetBkColor (s->hdc, s->gc->background);
3701 #if 0 /* From w32bdf.c (which is from Meadow). */
3702 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
3703 compat_hdc, 0, 0, SRCCOPY);
3704 #else
3705 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
3706 compat_hdc, 0, 0, 0xB8074A);
3707 #endif
3708 SelectObject (s->hdc, orig_brush);
3709 DeleteObject (fg_brush);
3710 SelectObject (compat_hdc, orig_obj);
3711 DeleteDC (compat_hdc);
3713 DeleteObject (pixmap);
3714 pixmap = 0;
3716 else
3717 x_draw_image_foreground (s);
3719 /* If we must draw a relief around the image, do it. */
3720 if (s->img->relief
3721 || s->hl == DRAW_IMAGE_RAISED
3722 || s->hl == DRAW_IMAGE_SUNKEN)
3723 x_draw_image_relief (s);
3727 /* Draw stretch glyph string S. */
3729 static void
3730 x_draw_stretch_glyph_string (s)
3731 struct glyph_string *s;
3733 xassert (s->first_glyph->type == STRETCH_GLYPH);
3734 s->stippled_p = s->face->stipple != 0;
3736 if (s->hl == DRAW_CURSOR
3737 && !x_stretch_cursor_p)
3739 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3740 as wide as the stretch glyph. */
3741 int width = min (CANON_X_UNIT (s->f), s->background_width);
3743 /* Draw cursor. */
3744 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3746 /* Clear rest using the GC of the original non-cursor face. */
3747 if (width < s->background_width)
3749 XGCValues *gc = s->face->gc;
3750 int x = s->x + width, y = s->y;
3751 int w = s->background_width - width, h = s->height;
3752 RECT r;
3753 HDC hdc = s->hdc;
3754 w32_get_glyph_string_clip_rect (s, &r);
3755 w32_set_clip_rectangle (hdc, &r);
3757 #if 0 /* NTEMACS_TODO: stipple */
3758 if (s->face->stipple)
3760 /* Fill background with a stipple pattern. */
3761 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3762 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3763 XSetFillStyle (s->display, gc, FillSolid);
3765 else
3766 #endif
3768 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
3772 else
3773 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3774 s->height);
3776 s->background_filled_p = 1;
3780 /* Draw glyph string S. */
3782 static void
3783 x_draw_glyph_string (s)
3784 struct glyph_string *s;
3786 /* If S draws into the background of its successor, draw the
3787 background of the successor first so that S can draw into it.
3788 This makes S->next use XDrawString instead of XDrawImageString. */
3789 if (s->next && s->right_overhang && !s->for_overlaps_p)
3791 xassert (s->next->img == NULL);
3792 x_set_glyph_string_gc (s->next);
3793 x_set_glyph_string_clipping (s->next);
3794 x_draw_glyph_string_background (s->next, 1);
3797 /* Set up S->gc, set clipping and draw S. */
3798 x_set_glyph_string_gc (s);
3799 x_set_glyph_string_clipping (s);
3801 switch (s->first_glyph->type)
3803 case IMAGE_GLYPH:
3804 x_draw_image_glyph_string (s);
3805 break;
3807 case STRETCH_GLYPH:
3808 x_draw_stretch_glyph_string (s);
3809 break;
3811 case CHAR_GLYPH:
3812 if (s->for_overlaps_p)
3813 s->background_filled_p = 1;
3814 else
3815 x_draw_glyph_string_background (s, 0);
3816 x_draw_glyph_string_foreground (s);
3817 break;
3819 case COMPOSITE_GLYPH:
3820 if (s->for_overlaps_p || s->gidx > 0)
3821 s->background_filled_p = 1;
3822 else
3823 x_draw_glyph_string_background (s, 1);
3824 x_draw_composite_glyph_string_foreground (s);
3825 break;
3827 default:
3828 abort ();
3831 if (!s->for_overlaps_p)
3833 /* Draw underline. */
3834 if (s->face->underline_p
3835 && (s->font->bdf || !s->font->tm.tmUnderlined))
3837 unsigned long h = 1;
3838 unsigned long dy = s->height - h;
3840 if (s->face->underline_defaulted_p)
3842 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
3843 s->y + dy, s->width, 1);
3845 else
3847 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
3848 s->y + dy, s->width, 1);
3852 /* Draw overline. */
3853 if (s->face->overline_p)
3855 unsigned long dy = 0, h = 1;
3857 if (s->face->overline_color_defaulted_p)
3859 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
3860 s->y + dy, s->width, h);
3862 else
3864 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
3865 s->y + dy, s->width, h);
3869 /* Draw strike-through. */
3870 if (s->face->strike_through_p
3871 && (s->font->bdf || !s->font->tm.tmStruckOut))
3873 unsigned long h = 1;
3874 unsigned long dy = (s->height - h) / 2;
3876 if (s->face->strike_through_color_defaulted_p)
3878 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
3879 s->width, h);
3881 else
3883 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
3884 s->y + dy, s->width, h);
3888 /* Draw relief. */
3889 if (s->face->box != FACE_NO_BOX)
3890 x_draw_glyph_string_box (s);
3893 /* Reset clipping. */
3894 w32_set_clip_rectangle (s->hdc, NULL);
3898 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
3899 struct face **, int));
3902 /* Load glyph string S with a composition components specified by S->cmp.
3903 FACES is an array of faces for all components of this composition.
3904 S->gidx is the index of the first component for S.
3905 OVERLAPS_P non-zero means S should draw the foreground only, and
3906 use its lines physical height for clipping.
3908 Value is the index of a component not in S. */
3910 static int
3911 x_fill_composite_glyph_string (s, faces, overlaps_p)
3912 struct glyph_string *s;
3913 struct face **faces;
3914 int overlaps_p;
3916 int i;
3918 xassert (s);
3920 s->for_overlaps_p = overlaps_p;
3922 s->face = faces[s->gidx];
3923 s->font = s->face->font;
3924 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
3926 /* For all glyphs of this composition, starting at the offset
3927 S->gidx, until we reach the end of the definition or encounter a
3928 glyph that requires the different face, add it to S. */
3929 ++s->nchars;
3930 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
3931 ++s->nchars;
3933 /* All glyph strings for the same composition has the same width,
3934 i.e. the width set for the first component of the composition. */
3936 s->width = s->first_glyph->pixel_width;
3938 /* If the specified font could not be loaded, use the frame's
3939 default font, but record the fact that we couldn't load it in
3940 the glyph string so that we can draw rectangles for the
3941 characters of the glyph string. */
3942 if (s->font == NULL)
3944 s->font_not_found_p = 1;
3945 s->font = FRAME_FONT (s->f);
3948 /* Adjust base line for subscript/superscript text. */
3949 s->ybase += s->first_glyph->voffset;
3951 xassert (s->face && s->face->gc);
3953 /* This glyph string must always be drawn with 16-bit functions. */
3954 s->two_byte_p = 1;
3956 return s->gidx + s->nchars;
3960 /* Load glyph string S with a sequence of characters.
3961 FACE_ID is the face id of the string. START is the index of the
3962 first glyph to consider, END is the index of the last + 1.
3963 OVERLAPS_P non-zero means S should draw the foreground only, and
3964 use its lines physical height for clipping.
3966 Value is the index of the first glyph not in S. */
3968 static int
3969 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
3970 struct glyph_string *s;
3971 int face_id;
3972 int start, end, overlaps_p;
3974 struct glyph *glyph, *last;
3975 int voffset;
3976 int glyph_not_available_p;
3978 xassert (s->f == XFRAME (s->w->frame));
3979 xassert (s->nchars == 0);
3980 xassert (start >= 0 && end > start);
3982 s->for_overlaps_p = overlaps_p;
3983 glyph = s->row->glyphs[s->area] + start;
3984 last = s->row->glyphs[s->area] + end;
3985 voffset = glyph->voffset;
3987 glyph_not_available_p = glyph->glyph_not_available_p;
3989 while (glyph < last
3990 && glyph->type == CHAR_GLYPH
3991 && glyph->voffset == voffset
3992 /* Same face id implies same font, nowadays. */
3993 && glyph->face_id == face_id
3994 && glyph->glyph_not_available_p == glyph_not_available_p)
3996 int two_byte_p;
3998 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
3999 s->char2b + s->nchars,
4000 &two_byte_p);
4001 s->two_byte_p = two_byte_p;
4002 ++s->nchars;
4003 xassert (s->nchars <= end - start);
4004 s->width += glyph->pixel_width;
4005 ++glyph;
4008 s->font = s->face->font;
4009 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4011 /* If the specified font could not be loaded, use the frame's font,
4012 but record the fact that we couldn't load it in
4013 S->font_not_found_p so that we can draw rectangles for the
4014 characters of the glyph string. */
4015 if (s->font == NULL || glyph_not_available_p)
4017 s->font_not_found_p = 1;
4018 s->font = FRAME_FONT (s->f);
4021 /* Adjust base line for subscript/superscript text. */
4022 s->ybase += voffset;
4024 xassert (s->face && s->face->gc);
4025 return glyph - s->row->glyphs[s->area];
4029 /* Fill glyph string S from image glyph S->first_glyph. */
4031 static void
4032 x_fill_image_glyph_string (s)
4033 struct glyph_string *s;
4035 xassert (s->first_glyph->type == IMAGE_GLYPH);
4036 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4037 xassert (s->img);
4038 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4039 s->font = s->face->font;
4040 s->width = s->first_glyph->pixel_width;
4042 /* Adjust base line for subscript/superscript text. */
4043 s->ybase += s->first_glyph->voffset;
4047 /* Fill glyph string S from a sequence of stretch glyphs.
4049 ROW is the glyph row in which the glyphs are found, AREA is the
4050 area within the row. START is the index of the first glyph to
4051 consider, END is the index of the last + 1.
4053 Value is the index of the first glyph not in S. */
4055 static int
4056 x_fill_stretch_glyph_string (s, row, area, start, end)
4057 struct glyph_string *s;
4058 struct glyph_row *row;
4059 enum glyph_row_area area;
4060 int start, end;
4062 struct glyph *glyph, *last;
4063 int voffset, face_id;
4065 xassert (s->first_glyph->type == STRETCH_GLYPH);
4067 glyph = s->row->glyphs[s->area] + start;
4068 last = s->row->glyphs[s->area] + end;
4069 face_id = glyph->face_id;
4070 s->face = FACE_FROM_ID (s->f, face_id);
4071 s->font = s->face->font;
4072 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4073 s->width = glyph->pixel_width;
4074 voffset = glyph->voffset;
4076 for (++glyph;
4077 (glyph < last
4078 && glyph->type == STRETCH_GLYPH
4079 && glyph->voffset == voffset
4080 && glyph->face_id == face_id);
4081 ++glyph)
4082 s->width += glyph->pixel_width;
4084 /* Adjust base line for subscript/superscript text. */
4085 s->ybase += voffset;
4087 xassert (s->face && s->face->gc);
4088 return glyph - s->row->glyphs[s->area];
4092 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4093 of XChar2b structures for S; it can't be allocated in
4094 x_init_glyph_string because it must be allocated via `alloca'. W
4095 is the window on which S is drawn. ROW and AREA are the glyph row
4096 and area within the row from which S is constructed. START is the
4097 index of the first glyph structure covered by S. HL is a
4098 face-override for drawing S. */
4100 static void
4101 w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4102 struct glyph_string *s;
4103 HDC hdc;
4104 wchar_t *char2b;
4105 struct window *w;
4106 struct glyph_row *row;
4107 enum glyph_row_area area;
4108 int start;
4109 enum draw_glyphs_face hl;
4111 bzero (s, sizeof *s);
4112 s->w = w;
4113 s->f = XFRAME (w->frame);
4114 s->hdc = hdc;
4115 s->window = FRAME_W32_WINDOW (s->f);
4116 s->char2b = char2b;
4117 s->hl = hl;
4118 s->row = row;
4119 s->area = area;
4120 s->first_glyph = row->glyphs[area] + start;
4121 s->height = row->height;
4122 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4124 /* Display the internal border below the tool-bar window. */
4125 if (s->w == XWINDOW (s->f->tool_bar_window))
4126 s->y -= s->f->output_data.w32->internal_border_width;
4128 s->ybase = s->y + row->ascent;
4132 /* Set background width of glyph string S. START is the index of the
4133 first glyph following S. LAST_X is the right-most x-position + 1
4134 in the drawing area. */
4136 static INLINE void
4137 x_set_glyph_string_background_width (s, start, last_x)
4138 struct glyph_string *s;
4139 int start;
4140 int last_x;
4142 /* If the face of this glyph string has to be drawn to the end of
4143 the drawing area, set S->extends_to_end_of_line_p. */
4144 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4146 if (start == s->row->used[s->area]
4147 && s->hl == DRAW_NORMAL_TEXT
4148 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4149 || s->face->background != default_face->background
4150 || s->face->stipple != default_face->stipple))
4151 s->extends_to_end_of_line_p = 1;
4153 /* If S extends its face to the end of the line, set its
4154 background_width to the distance to the right edge of the drawing
4155 area. */
4156 if (s->extends_to_end_of_line_p)
4157 s->background_width = last_x - s->x + 1;
4158 else
4159 s->background_width = s->width;
4163 /* Add a glyph string for a stretch glyph to the list of strings
4164 between HEAD and TAIL. START is the index of the stretch glyph in
4165 row area AREA of glyph row ROW. END is the index of the last glyph
4166 in that glyph row area. X is the current output position assigned
4167 to the new glyph string constructed. HL overrides that face of the
4168 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4169 is the right-most x-position of the drawing area. */
4171 #define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4172 do \
4174 s = (struct glyph_string *) alloca (sizeof *s); \
4175 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4176 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4177 x_append_glyph_string (&HEAD, &TAIL, s); \
4178 s->x = (X); \
4180 while (0)
4183 /* Add a glyph string for an image glyph to the list of strings
4184 between HEAD and TAIL. START is the index of the image glyph in
4185 row area AREA of glyph row ROW. END is the index of the last glyph
4186 in that glyph row area. X is the current output position assigned
4187 to the new glyph string constructed. HL overrides that face of the
4188 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4189 is the right-most x-position of the drawing area. */
4191 #define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4192 do \
4194 s = (struct glyph_string *) alloca (sizeof *s); \
4195 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4196 x_fill_image_glyph_string (s); \
4197 x_append_glyph_string (&HEAD, &TAIL, s); \
4198 ++START; \
4199 s->x = (X); \
4201 while (0)
4204 /* Add a glyph string for a sequence of character glyphs to the list
4205 of strings between HEAD and TAIL. START is the index of the first
4206 glyph in row area AREA of glyph row ROW that is part of the new
4207 glyph string. END is the index of the last glyph in that glyph row
4208 area. X is the current output position assigned to the new glyph
4209 string constructed. HL overrides that face of the glyph; e.g. it
4210 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4211 right-most x-position of the drawing area. */
4213 #define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4214 do \
4216 int c, face_id; \
4217 wchar_t *char2b; \
4219 c = (ROW)->glyphs[AREA][START].u.ch; \
4220 face_id = (ROW)->glyphs[AREA][START].face_id; \
4222 s = (struct glyph_string *) alloca (sizeof *s); \
4223 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4224 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4225 x_append_glyph_string (&HEAD, &TAIL, s); \
4226 s->x = (X); \
4227 START = x_fill_glyph_string (s, face_id, START, END, \
4228 OVERLAPS_P); \
4230 while (0)
4233 /* Add a glyph string for a composite sequence to the list of strings
4234 between HEAD and TAIL. START is the index of the first glyph in
4235 row area AREA of glyph row ROW that is part of the new glyph
4236 string. END is the index of the last glyph in that glyph row area.
4237 X is the current output position assigned to the new glyph string
4238 constructed. HL overrides that face of the glyph; e.g. it is
4239 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4240 x-position of the drawing area. */
4242 #define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4243 do { \
4244 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4245 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4246 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4247 struct composition *cmp = composition_table[cmp_id]; \
4248 int glyph_len = cmp->glyph_len; \
4249 wchar_t *char2b; \
4250 struct face **faces; \
4251 struct glyph_string *first_s = NULL; \
4252 int n; \
4254 base_face = base_face->ascii_face; \
4255 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4256 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4257 /* At first, fill in `char2b' and `faces'. */ \
4258 for (n = 0; n < glyph_len; n++) \
4260 int c = COMPOSITION_GLYPH (cmp, n); \
4261 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4262 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4263 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4264 this_face_id, char2b + n, 1); \
4267 /* Make glyph_strings for each glyph sequence that is drawable by \
4268 the same face, and append them to HEAD/TAIL. */ \
4269 for (n = 0; n < cmp->glyph_len;) \
4271 s = (struct glyph_string *) alloca (sizeof *s); \
4272 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4273 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4274 s->cmp = cmp; \
4275 s->gidx = n; \
4276 s->x = (X); \
4278 if (n == 0) \
4279 first_s = s; \
4281 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4284 ++START; \
4285 s = first_s; \
4286 } while (0)
4289 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4290 of AREA of glyph row ROW on window W between indices START and END.
4291 HL overrides the face for drawing glyph strings, e.g. it is
4292 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4293 x-positions of the drawing area.
4295 This is an ugly monster macro construct because we must use alloca
4296 to allocate glyph strings (because x_draw_glyphs can be called
4297 asynchronously). */
4299 #define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4300 do \
4302 HEAD = TAIL = NULL; \
4303 while (START < END) \
4305 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4306 switch (first_glyph->type) \
4308 case CHAR_GLYPH: \
4309 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4310 HEAD, TAIL, HL, X, LAST_X, \
4311 OVERLAPS_P); \
4312 break; \
4314 case COMPOSITE_GLYPH: \
4315 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4316 END, HEAD, TAIL, HL, X, \
4317 LAST_X, OVERLAPS_P); \
4318 break; \
4320 case STRETCH_GLYPH: \
4321 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4322 HEAD, TAIL, HL, X, LAST_X); \
4323 break; \
4325 case IMAGE_GLYPH: \
4326 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4327 HEAD, TAIL, HL, X, LAST_X); \
4328 break; \
4330 default: \
4331 abort (); \
4334 x_set_glyph_string_background_width (s, START, LAST_X); \
4335 (X) += s->width; \
4338 while (0)
4341 /* Draw glyphs between START and END in AREA of ROW on window W,
4342 starting at x-position X. X is relative to AREA in W. HL is a
4343 face-override with the following meaning:
4345 DRAW_NORMAL_TEXT draw normally
4346 DRAW_CURSOR draw in cursor face
4347 DRAW_MOUSE_FACE draw in mouse face.
4348 DRAW_INVERSE_VIDEO draw in mode line face
4349 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4350 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4352 If REAL_START is non-null, return in *REAL_START the real starting
4353 position for display. This can be different from START in case
4354 overlapping glyphs must be displayed. If REAL_END is non-null,
4355 return in *REAL_END the real end position for display. This can be
4356 different from END in case overlapping glyphs must be displayed.
4358 If OVERLAPS_P is non-zero, draw only the foreground of characters
4359 and clip to the physical height of ROW.
4361 Value is the x-position reached, relative to AREA of W. */
4363 static int
4364 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4365 overlaps_p)
4366 struct window *w;
4367 int x;
4368 struct glyph_row *row;
4369 enum glyph_row_area area;
4370 int start, end;
4371 enum draw_glyphs_face hl;
4372 int *real_start, *real_end;
4373 int overlaps_p;
4375 struct glyph_string *head, *tail;
4376 struct glyph_string *s;
4377 int last_x, area_width;
4378 int x_reached;
4379 int i, j;
4380 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4382 /* Let's rather be paranoid than getting a SEGV. */
4383 start = max (0, start);
4384 end = min (end, row->used[area]);
4385 if (real_start)
4386 *real_start = start;
4387 if (real_end)
4388 *real_end = end;
4390 /* Translate X to frame coordinates. Set last_x to the right
4391 end of the drawing area. */
4392 if (row->full_width_p)
4394 /* X is relative to the left edge of W, without scroll bars
4395 or flag areas. */
4396 struct frame *f = XFRAME (WINDOW_FRAME (w));
4397 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4398 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4400 x += window_left_x;
4401 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4402 last_x = window_left_x + area_width;
4404 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4406 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4407 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4408 last_x += width;
4409 else
4410 x -= width;
4413 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4414 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4416 else
4418 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4419 area_width = window_box_width (w, area);
4420 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4423 /* Build a doubly-linked list of glyph_string structures between
4424 head and tail from what we have to draw. Note that the macro
4425 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4426 the reason we use a separate variable `i'. */
4427 i = start;
4428 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4429 overlaps_p);
4430 if (tail)
4431 x_reached = tail->x + tail->background_width;
4432 else
4433 x_reached = x;
4435 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4436 the row, redraw some glyphs in front or following the glyph
4437 strings built above. */
4438 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4440 int dummy_x = 0;
4441 struct glyph_string *h, *t;
4443 /* Compute overhangs for all glyph strings. */
4444 for (s = head; s; s = s->next)
4445 x_compute_glyph_string_overhangs (s);
4447 /* Prepend glyph strings for glyphs in front of the first glyph
4448 string that are overwritten because of the first glyph
4449 string's left overhang. The background of all strings
4450 prepended must be drawn because the first glyph string
4451 draws over it. */
4452 i = x_left_overwritten (head);
4453 if (i >= 0)
4455 j = i;
4456 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
4457 DRAW_NORMAL_TEXT, dummy_x, last_x,
4458 overlaps_p);
4459 start = i;
4460 if (real_start)
4461 *real_start = start;
4462 x_compute_overhangs_and_x (t, head->x, 1);
4463 x_prepend_glyph_string_lists (&head, &tail, h, t);
4466 /* Prepend glyph strings for glyphs in front of the first glyph
4467 string that overwrite that glyph string because of their
4468 right overhang. For these strings, only the foreground must
4469 be drawn, because it draws over the glyph string at `head'.
4470 The background must not be drawn because this would overwrite
4471 right overhangs of preceding glyphs for which no glyph
4472 strings exist. */
4473 i = x_left_overwriting (head);
4474 if (i >= 0)
4476 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
4477 DRAW_NORMAL_TEXT, dummy_x, last_x,
4478 overlaps_p);
4479 for (s = h; s; s = s->next)
4480 s->background_filled_p = 1;
4481 if (real_start)
4482 *real_start = i;
4483 x_compute_overhangs_and_x (t, head->x, 1);
4484 x_prepend_glyph_string_lists (&head, &tail, h, t);
4487 /* Append glyphs strings for glyphs following the last glyph
4488 string tail that are overwritten by tail. The background of
4489 these strings has to be drawn because tail's foreground draws
4490 over it. */
4491 i = x_right_overwritten (tail);
4492 if (i >= 0)
4494 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
4495 DRAW_NORMAL_TEXT, x, last_x,
4496 overlaps_p);
4497 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4498 x_append_glyph_string_lists (&head, &tail, h, t);
4499 if (real_end)
4500 *real_end = i;
4503 /* Append glyph strings for glyphs following the last glyph
4504 string tail that overwrite tail. The foreground of such
4505 glyphs has to be drawn because it writes into the background
4506 of tail. The background must not be drawn because it could
4507 paint over the foreground of following glyphs. */
4508 i = x_right_overwriting (tail);
4509 if (i >= 0)
4511 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
4512 DRAW_NORMAL_TEXT, x, last_x,
4513 overlaps_p);
4514 for (s = h; s; s = s->next)
4515 s->background_filled_p = 1;
4516 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4517 x_append_glyph_string_lists (&head, &tail, h, t);
4518 if (real_end)
4519 *real_end = i;
4523 /* Draw all strings. */
4524 for (s = head; s; s = s->next)
4525 x_draw_glyph_string (s);
4527 /* Value is the x-position up to which drawn, relative to AREA of W.
4528 This doesn't include parts drawn because of overhangs. */
4529 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4530 if (!row->full_width_p)
4532 if (area > LEFT_MARGIN_AREA)
4533 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4534 if (area > TEXT_AREA)
4535 x_reached -= window_box_width (w, TEXT_AREA);
4538 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
4540 return x_reached;
4544 /* Fix the display of area AREA of overlapping row ROW in window W. */
4546 static void
4547 x_fix_overlapping_area (w, row, area)
4548 struct window *w;
4549 struct glyph_row *row;
4550 enum glyph_row_area area;
4552 int i, x;
4554 BLOCK_INPUT;
4556 if (area == LEFT_MARGIN_AREA)
4557 x = 0;
4558 else if (area == TEXT_AREA)
4559 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4560 else
4561 x = (window_box_width (w, LEFT_MARGIN_AREA)
4562 + window_box_width (w, TEXT_AREA));
4564 for (i = 0; i < row->used[area];)
4566 if (row->glyphs[area][i].overlaps_vertically_p)
4568 int start = i, start_x = x;
4572 x += row->glyphs[area][i].pixel_width;
4573 ++i;
4575 while (i < row->used[area]
4576 && row->glyphs[area][i].overlaps_vertically_p);
4578 x_draw_glyphs (w, start_x, row, area, start, i,
4579 (row->inverse_p
4580 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4581 NULL, NULL, 1);
4583 else
4585 x += row->glyphs[area][i].pixel_width;
4586 ++i;
4590 UNBLOCK_INPUT;
4594 /* Output LEN glyphs starting at START at the nominal cursor position.
4595 Advance the nominal cursor over the text. The global variable
4596 updated_window contains the window being updated, updated_row is
4597 the glyph row being updated, and updated_area is the area of that
4598 row being updated. */
4600 static void
4601 x_write_glyphs (start, len)
4602 struct glyph *start;
4603 int len;
4605 int x, hpos, real_start, real_end;
4607 xassert (updated_window && updated_row);
4608 BLOCK_INPUT;
4610 /* Write glyphs. */
4612 hpos = start - updated_row->glyphs[updated_area];
4613 x = x_draw_glyphs (updated_window, output_cursor.x,
4614 updated_row, updated_area,
4615 hpos, hpos + len,
4616 (updated_row->inverse_p
4617 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4618 &real_start, &real_end, 0);
4620 /* If we drew over the cursor, note that it is not visible any more. */
4621 note_overwritten_text_cursor (updated_window, real_start,
4622 real_end - real_start);
4624 UNBLOCK_INPUT;
4626 /* Advance the output cursor. */
4627 output_cursor.hpos += len;
4628 output_cursor.x = x;
4632 /* Insert LEN glyphs from START at the nominal cursor position. */
4634 static void
4635 x_insert_glyphs (start, len)
4636 struct glyph *start;
4637 register int len;
4639 struct frame *f;
4640 struct window *w;
4641 int line_height, shift_by_width, shifted_region_width;
4642 struct glyph_row *row;
4643 struct glyph *glyph;
4644 int frame_x, frame_y, hpos, real_start, real_end;
4645 HDC hdc;
4647 xassert (updated_window && updated_row);
4648 BLOCK_INPUT;
4649 w = updated_window;
4650 f = XFRAME (WINDOW_FRAME (w));
4651 hdc = get_frame_dc (f);
4653 /* Get the height of the line we are in. */
4654 row = updated_row;
4655 line_height = row->height;
4657 /* Get the width of the glyphs to insert. */
4658 shift_by_width = 0;
4659 for (glyph = start; glyph < start + len; ++glyph)
4660 shift_by_width += glyph->pixel_width;
4662 /* Get the width of the region to shift right. */
4663 shifted_region_width = (window_box_width (w, updated_area)
4664 - output_cursor.x
4665 - shift_by_width);
4667 /* Shift right. */
4668 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4669 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4670 BitBlt (hdc, frame_x + shift_by_width, frame_y,
4671 shifted_region_width, line_height,
4672 hdc, frame_x, frame_y, SRCCOPY);
4674 /* Write the glyphs. */
4675 hpos = start - row->glyphs[updated_area];
4676 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4677 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4678 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4680 /* Advance the output cursor. */
4681 output_cursor.hpos += len;
4682 output_cursor.x += shift_by_width;
4683 release_frame_dc (f, hdc);
4685 UNBLOCK_INPUT;
4689 /* Delete N glyphs at the nominal cursor position. Not implemented
4690 for X frames. */
4692 static void
4693 x_delete_glyphs (n)
4694 register int n;
4696 abort ();
4700 /* Erase the current text line from the nominal cursor position
4701 (inclusive) to pixel column TO_X (exclusive). The idea is that
4702 everything from TO_X onward is already erased.
4704 TO_X is a pixel position relative to updated_area of
4705 updated_window. TO_X == -1 means clear to the end of this area. */
4707 static void
4708 x_clear_end_of_line (to_x)
4709 int to_x;
4711 struct frame *f;
4712 struct window *w = updated_window;
4713 int max_x, min_y, max_y;
4714 int from_x, from_y, to_y;
4716 xassert (updated_window && updated_row);
4717 f = XFRAME (w->frame);
4719 if (updated_row->full_width_p)
4721 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4722 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4723 && !w->pseudo_window_p)
4724 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4726 else
4727 max_x = window_box_width (w, updated_area);
4728 max_y = window_text_bottom_y (w);
4730 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4731 of window. For TO_X > 0, truncate to end of drawing area. */
4732 if (to_x == 0)
4733 return;
4734 else if (to_x < 0)
4735 to_x = max_x;
4736 else
4737 to_x = min (to_x, max_x);
4739 to_y = min (max_y, output_cursor.y + updated_row->height);
4741 /* Notice if the cursor will be cleared by this operation. */
4742 if (!updated_row->full_width_p)
4743 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
4745 from_x = output_cursor.x;
4747 /* Translate to frame coordinates. */
4748 if (updated_row->full_width_p)
4750 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4751 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4753 else
4755 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4756 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4759 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
4760 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4761 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4763 /* Prevent inadvertently clearing to end of the X window. */
4764 if (to_x > from_x && to_y > from_y)
4766 HDC hdc;
4767 BLOCK_INPUT;
4768 hdc = get_frame_dc (f);
4770 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
4771 release_frame_dc (f, hdc);
4772 UNBLOCK_INPUT;
4777 /* Clear entire frame. If updating_frame is non-null, clear that
4778 frame. Otherwise clear the selected frame. */
4780 static void
4781 x_clear_frame ()
4783 struct frame *f;
4785 if (updating_frame)
4786 f = updating_frame;
4787 else
4788 f = SELECTED_FRAME ();
4790 /* Clearing the frame will erase any cursor, so mark them all as no
4791 longer visible. */
4792 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4793 output_cursor.hpos = output_cursor.vpos = 0;
4794 output_cursor.x = -1;
4796 /* We don't set the output cursor here because there will always
4797 follow an explicit cursor_to. */
4798 BLOCK_INPUT;
4800 w32_clear_window (f);
4802 /* We have to clear the scroll bars, too. If we have changed
4803 colors or something like that, then they should be notified. */
4804 x_scroll_bar_clear (f);
4806 UNBLOCK_INPUT;
4810 /* Make audible bell. */
4812 static void
4813 w32_ring_bell (void)
4815 BLOCK_INPUT;
4817 if (visible_bell)
4819 int i;
4820 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
4822 for (i = 0; i < 5; i++)
4824 FlashWindow (hwnd, TRUE);
4825 Sleep (10);
4827 FlashWindow (hwnd, FALSE);
4829 else
4830 w32_sys_ring_bell ();
4832 UNBLOCK_INPUT;
4836 /* Specify how many text lines, from the top of the window,
4837 should be affected by insert-lines and delete-lines operations.
4838 This, and those operations, are used only within an update
4839 that is bounded by calls to x_update_begin and x_update_end. */
4841 static void
4842 w32_set_terminal_window (n)
4843 register int n;
4845 /* This function intentionally left blank. */
4850 /***********************************************************************
4851 Line Dance
4852 ***********************************************************************/
4854 /* Perform an insert-lines or delete-lines operation, inserting N
4855 lines or deleting -N lines at vertical position VPOS. */
4857 static void
4858 x_ins_del_lines (vpos, n)
4859 int vpos, n;
4861 abort ();
4865 /* Scroll part of the display as described by RUN. */
4867 static void
4868 x_scroll_run (w, run)
4869 struct window *w;
4870 struct run *run;
4872 struct frame *f = XFRAME (w->frame);
4873 int x, y, width, height, from_y, to_y, bottom_y;
4874 HDC hdc = get_frame_dc (f);
4876 /* Get frame-relative bounding box of the text display area of W,
4877 without mode lines. Include in this box the flags areas to the
4878 left and right of W. */
4879 window_box (w, -1, &x, &y, &width, &height);
4880 width += FRAME_X_FLAGS_AREA_WIDTH (f);
4881 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
4883 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4884 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4885 bottom_y = y + height;
4887 if (to_y < from_y)
4889 /* Scrolling up. Make sure we don't copy part of the mode
4890 line at the bottom. */
4891 if (from_y + run->height > bottom_y)
4892 height = bottom_y - from_y;
4893 else
4894 height = run->height;
4896 else
4898 /* Scolling down. Make sure we don't copy over the mode line.
4899 at the bottom. */
4900 if (to_y + run->height > bottom_y)
4901 height = bottom_y - to_y;
4902 else
4903 height = run->height;
4906 BLOCK_INPUT;
4908 /* Cursor off. Will be switched on again in x_update_window_end. */
4909 updated_window = w;
4910 x_clear_cursor (w);
4912 BitBlt (hdc, x, to_y, width, height, hdc, x, from_y, SRCCOPY);
4914 UNBLOCK_INPUT;
4915 release_frame_dc (f, hdc);
4920 /***********************************************************************
4921 Exposure Events
4922 ***********************************************************************/
4924 /* Redisplay an exposed area of frame F. X and Y are the upper-left
4925 corner of the exposed rectangle. W and H are width and height of
4926 the exposed area. All are pixel values. W or H zero means redraw
4927 the entire frame. */
4929 static void
4930 expose_frame (f, x, y, w, h)
4931 struct frame *f;
4932 int x, y, w, h;
4934 RECT r;
4936 TRACE ((stderr, "expose_frame "));
4938 /* No need to redraw if frame will be redrawn soon. */
4939 if (FRAME_GARBAGED_P (f))
4941 TRACE ((stderr, " garbaged\n"));
4942 return;
4945 /* If basic faces haven't been realized yet, there is no point in
4946 trying to redraw anything. This can happen when we get an expose
4947 event while Emacs is starting, e.g. by moving another window. */
4948 if (FRAME_FACE_CACHE (f) == NULL
4949 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
4951 TRACE ((stderr, " no faces\n"));
4952 return;
4955 if (w == 0 || h == 0)
4957 r.left = r.top = 0;
4958 r.right = CANON_X_UNIT (f) * f->width;
4959 r.bottom = CANON_Y_UNIT (f) * f->height;
4961 else
4963 r.left = x;
4964 r.top = y;
4965 r.right = x + w;
4966 r.bottom = y + h;
4969 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
4970 expose_window_tree (XWINDOW (f->root_window), &r);
4972 if (WINDOWP (f->tool_bar_window))
4974 struct window *w = XWINDOW (f->tool_bar_window);
4975 RECT window_rect;
4976 RECT intersection_rect;
4977 int window_x, window_y, window_width, window_height;
4979 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
4980 window_rect.left = window_x;
4981 window_rect.top = window_y;
4982 window_rect.right = window_x + window_width;
4983 window_rect.bottom = window_y + window_height;
4985 if (IntersectRect (&intersection_rect, &r, &window_rect))
4986 expose_window (w, &intersection_rect);
4991 /* Redraw (parts) of all windows in the window tree rooted at W that
4992 intersect R. R contains frame pixel coordinates. */
4994 static void
4995 expose_window_tree (w, r)
4996 struct window *w;
4997 RECT *r;
4999 while (w)
5001 if (!NILP (w->hchild))
5002 expose_window_tree (XWINDOW (w->hchild), r);
5003 else if (!NILP (w->vchild))
5004 expose_window_tree (XWINDOW (w->vchild), r);
5005 else
5007 RECT window_rect;
5008 RECT intersection_rect;
5009 struct frame *f = XFRAME (w->frame);
5010 int window_x, window_y, window_width, window_height;
5012 /* Frame-relative pixel rectangle of W. */
5013 window_box (w, -1, &window_x, &window_y, &window_width,
5014 &window_height);
5015 window_rect.left
5016 = (window_x
5017 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5018 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5019 window_rect.top = window_y;
5020 window_rect.right = window_rect.left
5021 + (window_width
5022 + FRAME_X_FLAGS_AREA_WIDTH (f)
5023 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5024 window_rect.bottom = window_rect.top
5025 + window_height + CURRENT_MODE_LINE_HEIGHT (w);
5027 if (IntersectRect (&intersection_rect, r, &window_rect))
5028 expose_window (w, &intersection_rect);
5031 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5036 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5037 which intersects rectangle R. R is in window-relative coordinates. */
5039 static void
5040 expose_area (w, row, r, area)
5041 struct window *w;
5042 struct glyph_row *row;
5043 RECT *r;
5044 enum glyph_row_area area;
5046 int x;
5047 struct glyph *first = row->glyphs[area];
5048 struct glyph *end = row->glyphs[area] + row->used[area];
5049 struct glyph *last;
5050 int first_x;
5052 /* Set x to the window-relative start position for drawing glyphs of
5053 AREA. The first glyph of the text area can be partially visible.
5054 The first glyphs of other areas cannot. */
5055 if (area == LEFT_MARGIN_AREA)
5056 x = 0;
5057 else if (area == TEXT_AREA)
5058 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5059 else
5060 x = (window_box_width (w, LEFT_MARGIN_AREA)
5061 + window_box_width (w, TEXT_AREA));
5063 if (area == TEXT_AREA && row->fill_line_p)
5064 /* If row extends face to end of line write the whole line. */
5065 x_draw_glyphs (w, x, row, area,
5066 0, row->used[area],
5067 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5068 NULL, NULL, 0);
5069 else
5071 /* Find the first glyph that must be redrawn. */
5072 while (first < end
5073 && x + first->pixel_width < r->left)
5075 x += first->pixel_width;
5076 ++first;
5079 /* Find the last one. */
5080 last = first;
5081 first_x = x;
5082 while (last < end
5083 && x < r->right)
5085 x += last->pixel_width;
5086 ++last;
5089 /* Repaint. */
5090 if (last > first)
5091 x_draw_glyphs (w, first_x, row, area,
5092 first - row->glyphs[area],
5093 last - row->glyphs[area],
5094 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5095 NULL, NULL, 0);
5100 /* Redraw the parts of the glyph row ROW on window W intersecting
5101 rectangle R. R is in window-relative coordinates. */
5103 static void
5104 expose_line (w, row, r)
5105 struct window *w;
5106 struct glyph_row *row;
5107 RECT *r;
5109 xassert (row->enabled_p);
5111 if (row->mode_line_p || w->pseudo_window_p)
5112 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5113 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5114 NULL, NULL, 0);
5115 else
5117 if (row->used[LEFT_MARGIN_AREA])
5118 expose_area (w, row, r, LEFT_MARGIN_AREA);
5119 if (row->used[TEXT_AREA])
5120 expose_area (w, row, r, TEXT_AREA);
5121 if (row->used[RIGHT_MARGIN_AREA])
5122 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5123 x_draw_row_bitmaps (w, row);
5128 /* Return non-zero if W's cursor intersects rectangle R. */
5130 static int
5131 x_phys_cursor_in_rect_p (w, r)
5132 struct window *w;
5133 RECT *r;
5135 RECT cr, result;
5136 struct glyph *cursor_glyph;
5138 cursor_glyph = get_phys_cursor_glyph (w);
5139 if (cursor_glyph)
5141 cr.left = w->phys_cursor.x;
5142 cr.top = w->phys_cursor.y;
5143 cr.right = cr.left + cursor_glyph->pixel_width;
5144 cr.bottom = cr.top + w->phys_cursor_height;
5145 return IntersectRect (&result, &cr, r);
5147 else
5148 return 0;
5152 /* Redraw a rectangle of window W. R is a rectangle in window
5153 relative coordinates. Call this function with input blocked. */
5155 static void
5156 expose_window (w, r)
5157 struct window *w;
5158 RECT *r;
5160 struct glyph_row *row;
5161 int y;
5162 int yb = window_text_bottom_y (w);
5163 int cursor_cleared_p;
5165 /* If window is not yet fully initialized, do nothing. This can
5166 happen when toolkit scroll bars are used and a window is split.
5167 Reconfiguring the scroll bar will generate an expose for a newly
5168 created window. */
5169 if (w->current_matrix == NULL)
5170 return;
5172 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5173 r->left, r->top, r->right, r->bottom));
5175 /* Convert to window coordinates. */
5176 r->left = FRAME_TO_WINDOW_PIXEL_X (w, r->left);
5177 r->top = FRAME_TO_WINDOW_PIXEL_Y (w, r->top);
5178 r->right = FRAME_TO_WINDOW_PIXEL_X (w, r->right);
5179 r->bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r->bottom);
5181 /* Turn off the cursor. */
5182 if (!w->pseudo_window_p
5183 && x_phys_cursor_in_rect_p (w, r))
5185 x_clear_cursor (w);
5186 cursor_cleared_p = 1;
5188 else
5189 cursor_cleared_p = 0;
5191 /* Find the first row intersecting the rectangle R. */
5192 row = w->current_matrix->rows;
5193 y = 0;
5194 while (row->enabled_p
5195 && y < yb
5196 && y + row->height < r->top)
5198 y += row->height;
5199 ++row;
5202 /* Display the text in the rectangle, one text line at a time. */
5203 while (row->enabled_p
5204 && y < yb
5205 && y < r->bottom)
5207 expose_line (w, row, r);
5208 y += row->height;
5209 ++row;
5212 /* Display the mode line if there is one. */
5213 if (WINDOW_WANTS_MODELINE_P (w)
5214 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5215 row->enabled_p)
5216 && row->y < r->bottom)
5217 expose_line (w, row, r);
5219 if (!w->pseudo_window_p)
5221 /* Draw border between windows. */
5222 x_draw_vertical_border (w);
5224 /* Turn the cursor on again. */
5225 if (cursor_cleared_p)
5226 x_update_window_cursor (w, 1);
5231 static void
5232 frame_highlight (f)
5233 struct frame *f;
5235 x_update_cursor (f, 1);
5238 static void
5239 frame_unhighlight (f)
5240 struct frame *f;
5242 x_update_cursor (f, 1);
5245 /* The focus has changed. Update the frames as necessary to reflect
5246 the new situation. Note that we can't change the selected frame
5247 here, because the Lisp code we are interrupting might become confused.
5248 Each event gets marked with the frame in which it occurred, so the
5249 Lisp code can tell when the switch took place by examining the events. */
5251 static void
5252 x_new_focus_frame (dpyinfo, frame)
5253 struct w32_display_info *dpyinfo;
5254 struct frame *frame;
5256 struct frame *old_focus = dpyinfo->w32_focus_frame;
5258 if (frame != dpyinfo->w32_focus_frame)
5260 /* Set this before calling other routines, so that they see
5261 the correct value of w32_focus_frame. */
5262 dpyinfo->w32_focus_frame = frame;
5264 if (old_focus && old_focus->auto_lower)
5265 x_lower_frame (old_focus);
5267 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5268 pending_autoraise_frame = dpyinfo->w32_focus_frame;
5269 else
5270 pending_autoraise_frame = 0;
5273 x_frame_rehighlight (dpyinfo);
5276 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5278 void
5279 x_mouse_leave (dpyinfo)
5280 struct w32_display_info *dpyinfo;
5282 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
5285 /* The focus has changed, or we have redirected a frame's focus to
5286 another frame (this happens when a frame uses a surrogate
5287 mini-buffer frame). Shift the highlight as appropriate.
5289 The FRAME argument doesn't necessarily have anything to do with which
5290 frame is being highlighted or un-highlighted; we only use it to find
5291 the appropriate X display info. */
5293 static void
5294 w32_frame_rehighlight (frame)
5295 struct frame *frame;
5297 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
5300 static void
5301 x_frame_rehighlight (dpyinfo)
5302 struct w32_display_info *dpyinfo;
5304 struct frame *old_highlight = dpyinfo->w32_highlight_frame;
5306 if (dpyinfo->w32_focus_frame)
5308 dpyinfo->w32_highlight_frame
5309 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5310 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5311 : dpyinfo->w32_focus_frame);
5312 if (! FRAME_LIVE_P (dpyinfo->w32_highlight_frame))
5314 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
5315 dpyinfo->w32_highlight_frame = dpyinfo->w32_focus_frame;
5318 else
5319 dpyinfo->w32_highlight_frame = 0;
5321 if (dpyinfo->w32_highlight_frame != old_highlight)
5323 if (old_highlight)
5324 frame_unhighlight (old_highlight);
5325 if (dpyinfo->w32_highlight_frame)
5326 frame_highlight (dpyinfo->w32_highlight_frame);
5330 /* Keyboard processing - modifier keys, etc. */
5332 /* Convert a keysym to its name. */
5334 char *
5335 x_get_keysym_name (keysym)
5336 int keysym;
5338 /* Make static so we can always return it */
5339 static char value[100];
5341 BLOCK_INPUT;
5342 GetKeyNameText(keysym, value, 100);
5343 UNBLOCK_INPUT;
5345 return value;
5350 /* Mouse clicks and mouse movement. Rah. */
5352 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5353 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5354 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5355 not force the value into range. */
5357 void
5358 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5359 FRAME_PTR f;
5360 register int pix_x, pix_y;
5361 register int *x, *y;
5362 RECT *bounds;
5363 int noclip;
5365 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
5366 if (NILP (Vwindow_system))
5368 *x = pix_x;
5369 *y = pix_y;
5370 return;
5373 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5374 even for negative values. */
5375 if (pix_x < 0)
5376 pix_x -= FONT_WIDTH (FRAME_FONT(f)) - 1;
5377 if (pix_y < 0)
5378 pix_y -= (f)->output_data.w32->line_height - 1;
5380 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5381 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5383 if (bounds)
5385 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
5386 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
5387 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT(f)) - 1;
5388 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
5391 if (!noclip)
5393 if (pix_x < 0)
5394 pix_x = 0;
5395 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5396 pix_x = FRAME_WINDOW_WIDTH (f);
5398 if (pix_y < 0)
5399 pix_y = 0;
5400 else if (pix_y > f->height)
5401 pix_y = f->height;
5404 *x = pix_x;
5405 *y = pix_y;
5409 /* Given HPOS/VPOS in the current matrix of W, return corresponding
5410 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5411 can't tell the positions because W's display is not up to date,
5412 return 0. */
5415 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5416 struct window *w;
5417 int hpos, vpos;
5418 int *frame_x, *frame_y;
5420 int success_p;
5422 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5423 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5425 if (display_completed)
5427 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5428 struct glyph *glyph = row->glyphs[TEXT_AREA];
5429 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5431 *frame_y = row->y;
5432 *frame_x = row->x;
5433 while (glyph < end)
5435 *frame_x += glyph->pixel_width;
5436 ++glyph;
5439 success_p = 1;
5441 else
5443 *frame_y = *frame_x = 0;
5444 success_p = 0;
5447 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5448 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5449 return success_p;
5452 BOOL
5453 parse_button (message, pbutton, pup)
5454 int message;
5455 int * pbutton;
5456 int * pup;
5458 int button = 0;
5459 int up = 0;
5461 switch (message)
5463 case WM_LBUTTONDOWN:
5464 button = 0;
5465 up = 0;
5466 break;
5467 case WM_LBUTTONUP:
5468 button = 0;
5469 up = 1;
5470 break;
5471 case WM_MBUTTONDOWN:
5472 if (NILP (Vw32_swap_mouse_buttons))
5473 button = 1;
5474 else
5475 button = 2;
5476 up = 0;
5477 break;
5478 case WM_MBUTTONUP:
5479 if (NILP (Vw32_swap_mouse_buttons))
5480 button = 1;
5481 else
5482 button = 2;
5483 up = 1;
5484 break;
5485 case WM_RBUTTONDOWN:
5486 if (NILP (Vw32_swap_mouse_buttons))
5487 button = 2;
5488 else
5489 button = 1;
5490 up = 0;
5491 break;
5492 case WM_RBUTTONUP:
5493 if (NILP (Vw32_swap_mouse_buttons))
5494 button = 2;
5495 else
5496 button = 1;
5497 up = 1;
5498 break;
5499 default:
5500 return (FALSE);
5503 if (pup) *pup = up;
5504 if (pbutton) *pbutton = button;
5506 return (TRUE);
5510 /* Prepare a mouse-event in *RESULT for placement in the input queue.
5512 If the event is a button press, then note that we have grabbed
5513 the mouse. */
5515 static Lisp_Object
5516 construct_mouse_click (result, msg, f)
5517 struct input_event *result;
5518 W32Msg *msg;
5519 struct frame *f;
5521 int button;
5522 int up;
5524 parse_button (msg->msg.message, &button, &up);
5526 /* Make the event type no_event; we'll change that when we decide
5527 otherwise. */
5528 result->kind = mouse_click;
5529 result->code = button;
5530 result->timestamp = msg->msg.time;
5531 result->modifiers = (msg->dwModifiers
5532 | (up
5533 ? up_modifier
5534 : down_modifier));
5536 XSETINT (result->x, LOWORD (msg->msg.lParam));
5537 XSETINT (result->y, HIWORD (msg->msg.lParam));
5538 XSETFRAME (result->frame_or_window, f);
5539 result->arg = Qnil;
5540 return Qnil;
5543 static Lisp_Object
5544 construct_mouse_wheel (result, msg, f)
5545 struct input_event *result;
5546 W32Msg *msg;
5547 struct frame *f;
5549 POINT p;
5550 result->kind = mouse_wheel;
5551 result->code = (short) HIWORD (msg->msg.wParam);
5552 result->timestamp = msg->msg.time;
5553 result->modifiers = msg->dwModifiers;
5554 p.x = LOWORD (msg->msg.lParam);
5555 p.y = HIWORD (msg->msg.lParam);
5556 ScreenToClient(msg->msg.hwnd, &p);
5557 XSETINT (result->x, p.x);
5558 XSETINT (result->y, p.y);
5559 XSETFRAME (result->frame_or_window, f);
5560 result->arg = Qnil;
5561 return Qnil;
5564 static Lisp_Object
5565 construct_drag_n_drop (result, msg, f)
5566 struct input_event *result;
5567 W32Msg *msg;
5568 struct frame *f;
5570 Lisp_Object files;
5571 Lisp_Object frame;
5572 HDROP hdrop;
5573 POINT p;
5574 WORD num_files;
5575 char *name;
5576 int i, len;
5578 result->kind = drag_n_drop;
5579 result->code = 0;
5580 result->timestamp = msg->msg.time;
5581 result->modifiers = msg->dwModifiers;
5583 hdrop = (HDROP) msg->msg.wParam;
5584 DragQueryPoint (hdrop, &p);
5586 #if 0
5587 p.x = LOWORD (msg->msg.lParam);
5588 p.y = HIWORD (msg->msg.lParam);
5589 ScreenToClient (msg->msg.hwnd, &p);
5590 #endif
5592 XSETINT (result->x, p.x);
5593 XSETINT (result->y, p.y);
5595 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
5596 files = Qnil;
5598 for (i = 0; i < num_files; i++)
5600 len = DragQueryFile (hdrop, i, NULL, 0);
5601 if (len <= 0)
5602 continue;
5603 name = alloca (len + 1);
5604 DragQueryFile (hdrop, i, name, len + 1);
5605 files = Fcons (build_string (name), files);
5608 DragFinish (hdrop);
5610 XSETFRAME (frame, f);
5611 result->frame_or_window = Fcons (frame, files);
5612 result->arg = Qnil;
5613 return Qnil;
5617 /* Function to report a mouse movement to the mainstream Emacs code.
5618 The input handler calls this.
5620 We have received a mouse movement event, which is given in *event.
5621 If the mouse is over a different glyph than it was last time, tell
5622 the mainstream emacs code by setting mouse_moved. If not, ask for
5623 another motion event, so we can check again the next time it moves. */
5625 static MSG last_mouse_motion_event;
5626 static Lisp_Object last_mouse_motion_frame;
5628 static void
5629 note_mouse_movement (frame, msg)
5630 FRAME_PTR frame;
5631 MSG *msg;
5633 last_mouse_movement_time = msg->time;
5634 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
5635 XSETFRAME (last_mouse_motion_frame, frame);
5637 if (msg->hwnd != FRAME_W32_WINDOW (frame))
5639 frame->mouse_moved = 1;
5640 last_mouse_scroll_bar = Qnil;
5641 note_mouse_highlight (frame, -1, -1);
5644 /* Has the mouse moved off the glyph it was on at the last sighting? */
5645 else if (LOWORD (msg->lParam) < last_mouse_glyph.left
5646 || LOWORD (msg->lParam) > last_mouse_glyph.right
5647 || HIWORD (msg->lParam) < last_mouse_glyph.top
5648 || HIWORD (msg->lParam) > last_mouse_glyph.bottom)
5650 frame->mouse_moved = 1;
5651 last_mouse_scroll_bar = Qnil;
5653 note_mouse_highlight (frame, LOWORD (msg->lParam), HIWORD (msg->lParam));
5657 /* This is used for debugging, to turn off note_mouse_highlight. */
5659 int disable_mouse_highlight;
5663 /************************************************************************
5664 Mouse Face
5665 ************************************************************************/
5667 /* Find the glyph under window-relative coordinates X/Y in window W.
5668 Consider only glyphs from buffer text, i.e. no glyphs from overlay
5669 strings. Return in *HPOS and *VPOS the row and column number of
5670 the glyph found. Return in *AREA the glyph area containing X.
5671 Value is a pointer to the glyph found or null if X/Y is not on
5672 text, or we can't tell because W's current matrix is not up to
5673 date. */
5675 static struct glyph *
5676 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
5677 struct window *w;
5678 int x, y;
5679 int *hpos, *vpos, *area;
5681 struct glyph *glyph, *end;
5682 struct glyph_row *row = NULL;
5683 int x0, i, left_area_width;
5685 /* Find row containing Y. Give up if some row is not enabled. */
5686 for (i = 0; i < w->current_matrix->nrows; ++i)
5688 row = MATRIX_ROW (w->current_matrix, i);
5689 if (!row->enabled_p)
5690 return NULL;
5691 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
5692 break;
5695 *vpos = i;
5696 *hpos = 0;
5698 /* Give up if Y is not in the window. */
5699 if (i == w->current_matrix->nrows)
5700 return NULL;
5702 /* Get the glyph area containing X. */
5703 if (w->pseudo_window_p)
5705 *area = TEXT_AREA;
5706 x0 = 0;
5708 else
5710 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5711 if (x < left_area_width)
5713 *area = LEFT_MARGIN_AREA;
5714 x0 = 0;
5716 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
5718 *area = TEXT_AREA;
5719 x0 = row->x + left_area_width;
5721 else
5723 *area = RIGHT_MARGIN_AREA;
5724 x0 = left_area_width + window_box_width (w, TEXT_AREA);
5728 /* Find glyph containing X. */
5729 glyph = row->glyphs[*area];
5730 end = glyph + row->used[*area];
5731 while (glyph < end)
5733 if (x < x0 + glyph->pixel_width)
5735 if (w->pseudo_window_p)
5736 break;
5737 else if (BUFFERP (glyph->object))
5738 break;
5741 x0 += glyph->pixel_width;
5742 ++glyph;
5745 if (glyph == end)
5746 return NULL;
5748 *hpos = glyph - row->glyphs[*area];
5749 return glyph;
5753 /* Convert frame-relative x/y to coordinates relative to window W.
5754 Takes pseudo-windows into account. */
5756 static void
5757 frame_to_window_pixel_xy (w, x, y)
5758 struct window *w;
5759 int *x, *y;
5761 if (w->pseudo_window_p)
5763 /* A pseudo-window is always full-width, and starts at the
5764 left edge of the frame, plus a frame border. */
5765 struct frame *f = XFRAME (w->frame);
5766 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
5767 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5769 else
5771 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
5772 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
5777 /* Take proper action when mouse has moved to the mode or top line of
5778 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
5779 mode line. X is relative to the start of the text display area of
5780 W, so the width of bitmap areas and scroll bars must be subtracted
5781 to get a position relative to the start of the mode line. */
5783 static void
5784 note_mode_line_highlight (w, x, mode_line_p)
5785 struct window *w;
5786 int x, mode_line_p;
5788 struct frame *f = XFRAME (w->frame);
5789 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5790 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
5791 struct glyph_row *row;
5793 if (mode_line_p)
5794 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5795 else
5796 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5798 if (row->enabled_p)
5800 struct glyph *glyph, *end;
5801 Lisp_Object help, map;
5802 int x0;
5804 /* Find the glyph under X. */
5805 glyph = row->glyphs[TEXT_AREA];
5806 end = glyph + row->used[TEXT_AREA];
5807 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
5808 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
5809 while (glyph < end
5810 && x >= x0 + glyph->pixel_width)
5812 x0 += glyph->pixel_width;
5813 ++glyph;
5816 if (glyph < end
5817 && STRINGP (glyph->object)
5818 && XSTRING (glyph->object)->intervals
5819 && glyph->charpos >= 0
5820 && glyph->charpos < XSTRING (glyph->object)->size)
5822 /* If we're on a string with `help-echo' text property,
5823 arrange for the help to be displayed. This is done by
5824 setting the global variable help_echo to the help string. */
5825 help = Fget_text_property (make_number (glyph->charpos),
5826 Qhelp_echo, glyph->object);
5827 if (!NILP (help))
5829 help_echo = help;
5830 XSETWINDOW (help_echo_window, w);
5831 help_echo_object = glyph->object;
5832 help_echo_pos = glyph->charpos;
5835 /* Change the mouse pointer according to what is under X/Y. */
5836 map = Fget_text_property (make_number (glyph->charpos),
5837 Qlocal_map, glyph->object);
5838 if (!NILP (Fkeymapp (map)))
5839 cursor = f->output_data.w32->nontext_cursor;
5843 #if 0 /* NTEMACS_TODO: mouse cursor */
5844 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
5845 #endif
5849 /* Take proper action when the mouse has moved to position X, Y on
5850 frame F as regards highlighting characters that have mouse-face
5851 properties. Also de-highlighting chars where the mouse was before.
5852 X and Y can be negative or out of range. */
5854 static void
5855 note_mouse_highlight (f, x, y)
5856 struct frame *f;
5857 int x, y;
5859 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5860 int portion;
5861 Lisp_Object window;
5862 struct window *w;
5864 /* When a menu is active, don't highlight because this looks odd. */
5865 if (popup_activated ())
5866 return;
5868 if (disable_mouse_highlight
5869 || !f->glyphs_initialized_p)
5870 return;
5872 dpyinfo->mouse_face_mouse_x = x;
5873 dpyinfo->mouse_face_mouse_y = y;
5874 dpyinfo->mouse_face_mouse_frame = f;
5876 if (dpyinfo->mouse_face_defer)
5877 return;
5879 if (gc_in_progress)
5881 dpyinfo->mouse_face_deferred_gc = 1;
5882 return;
5885 /* Which window is that in? */
5886 window = window_from_coordinates (f, x, y, &portion, 1);
5888 /* If we were displaying active text in another window, clear that. */
5889 if (! EQ (window, dpyinfo->mouse_face_window))
5890 clear_mouse_face (dpyinfo);
5892 /* Not on a window -> return. */
5893 if (!WINDOWP (window))
5894 return;
5896 /* Convert to window-relative pixel coordinates. */
5897 w = XWINDOW (window);
5898 frame_to_window_pixel_xy (w, &x, &y);
5900 /* Handle tool-bar window differently since it doesn't display a
5901 buffer. */
5902 if (EQ (window, f->tool_bar_window))
5904 note_tool_bar_highlight (f, x, y);
5905 return;
5908 if (portion == 1 || portion == 3)
5910 /* Mouse is on the mode or top line. */
5911 note_mode_line_highlight (w, x, portion == 1);
5912 return;
5914 #if 0 /* NTEMACS_TODO: mouse cursor */
5915 else
5916 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5917 f->output_data.x->text_cursor);
5918 #endif
5920 /* Are we in a window whose display is up to date?
5921 And verify the buffer's text has not changed. */
5922 if (/* Within text portion of the window. */
5923 portion == 0
5924 && EQ (w->window_end_valid, w->buffer)
5925 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
5926 && (XFASTINT (w->last_overlay_modified)
5927 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
5929 int hpos, vpos, pos, i, area;
5930 struct glyph *glyph;
5932 /* Find the glyph under X/Y. */
5933 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
5935 /* Clear mouse face if X/Y not over text. */
5936 if (glyph == NULL
5937 || area != TEXT_AREA
5938 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
5940 clear_mouse_face (dpyinfo);
5941 return;
5944 pos = glyph->charpos;
5945 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
5947 /* Check for mouse-face and help-echo. */
5949 Lisp_Object mouse_face, overlay, position;
5950 Lisp_Object *overlay_vec;
5951 int len, noverlays;
5952 struct buffer *obuf;
5953 int obegv, ozv;
5955 /* If we get an out-of-range value, return now; avoid an error. */
5956 if (pos > BUF_Z (XBUFFER (w->buffer)))
5957 return;
5959 /* Make the window's buffer temporarily current for
5960 overlays_at and compute_char_face. */
5961 obuf = current_buffer;
5962 current_buffer = XBUFFER (w->buffer);
5963 obegv = BEGV;
5964 ozv = ZV;
5965 BEGV = BEG;
5966 ZV = Z;
5968 /* Is this char mouse-active or does it have help-echo? */
5969 XSETINT (position, pos);
5971 /* Put all the overlays we want in a vector in overlay_vec.
5972 Store the length in len. If there are more than 10, make
5973 enough space for all, and try again. */
5974 len = 10;
5975 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
5976 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
5977 if (noverlays > len)
5979 len = noverlays;
5980 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
5981 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
5984 /* Sort overlays into increasing priority order. */
5985 noverlays = sort_overlays (overlay_vec, noverlays, w);
5987 /* Check mouse-face highlighting. */
5988 if (! (EQ (window, dpyinfo->mouse_face_window)
5989 && vpos >= dpyinfo->mouse_face_beg_row
5990 && vpos <= dpyinfo->mouse_face_end_row
5991 && (vpos > dpyinfo->mouse_face_beg_row
5992 || hpos >= dpyinfo->mouse_face_beg_col)
5993 && (vpos < dpyinfo->mouse_face_end_row
5994 || hpos < dpyinfo->mouse_face_end_col
5995 || dpyinfo->mouse_face_past_end)))
5997 /* Clear the display of the old active region, if any. */
5998 clear_mouse_face (dpyinfo);
6000 /* Find the highest priority overlay that has a mouse-face prop. */
6001 overlay = Qnil;
6002 for (i = noverlays - 1; i >= 0; --i)
6004 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6005 if (!NILP (mouse_face))
6007 overlay = overlay_vec[i];
6008 break;
6012 /* If no overlay applies, get a text property. */
6013 if (NILP (overlay))
6014 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6016 /* Handle the overlay case. */
6017 if (! NILP (overlay))
6019 /* Find the range of text around this char that
6020 should be active. */
6021 Lisp_Object before, after;
6022 int ignore;
6024 before = Foverlay_start (overlay);
6025 after = Foverlay_end (overlay);
6026 /* Record this as the current active region. */
6027 fast_find_position (w, XFASTINT (before),
6028 &dpyinfo->mouse_face_beg_col,
6029 &dpyinfo->mouse_face_beg_row,
6030 &dpyinfo->mouse_face_beg_x,
6031 &dpyinfo->mouse_face_beg_y);
6032 dpyinfo->mouse_face_past_end
6033 = !fast_find_position (w, XFASTINT (after),
6034 &dpyinfo->mouse_face_end_col,
6035 &dpyinfo->mouse_face_end_row,
6036 &dpyinfo->mouse_face_end_x,
6037 &dpyinfo->mouse_face_end_y);
6038 dpyinfo->mouse_face_window = window;
6039 dpyinfo->mouse_face_face_id
6040 = face_at_buffer_position (w, pos, 0, 0,
6041 &ignore, pos + 1, 1);
6043 /* Display it as active. */
6044 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6046 /* Handle the text property case. */
6047 else if (! NILP (mouse_face))
6049 /* Find the range of text around this char that
6050 should be active. */
6051 Lisp_Object before, after, beginning, end;
6052 int ignore;
6054 beginning = Fmarker_position (w->start);
6055 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6056 - XFASTINT (w->window_end_pos)));
6057 before
6058 = Fprevious_single_property_change (make_number (pos + 1),
6059 Qmouse_face,
6060 w->buffer, beginning);
6061 after
6062 = Fnext_single_property_change (position, Qmouse_face,
6063 w->buffer, end);
6064 /* Record this as the current active region. */
6065 fast_find_position (w, XFASTINT (before),
6066 &dpyinfo->mouse_face_beg_col,
6067 &dpyinfo->mouse_face_beg_row,
6068 &dpyinfo->mouse_face_beg_x,
6069 &dpyinfo->mouse_face_beg_y);
6070 dpyinfo->mouse_face_past_end
6071 = !fast_find_position (w, XFASTINT (after),
6072 &dpyinfo->mouse_face_end_col,
6073 &dpyinfo->mouse_face_end_row,
6074 &dpyinfo->mouse_face_end_x,
6075 &dpyinfo->mouse_face_end_y);
6076 dpyinfo->mouse_face_window = window;
6077 dpyinfo->mouse_face_face_id
6078 = face_at_buffer_position (w, pos, 0, 0,
6079 &ignore, pos + 1, 1);
6081 /* Display it as active. */
6082 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6086 /* Look for a `help-echo' property. */
6088 Lisp_Object help, overlay;
6090 /* Check overlays first. */
6091 help = Qnil;
6092 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6094 overlay = overlay_vec[i];
6095 help = Foverlay_get (overlay, Qhelp_echo);
6098 if (!NILP (help))
6100 help_echo = help;
6101 help_echo_window = window;
6102 help_echo_object = overlay;
6103 help_echo_pos = pos;
6105 else
6107 /* Try text properties. */
6108 if ((STRINGP (glyph->object)
6109 && glyph->charpos >= 0
6110 && glyph->charpos < XSTRING (glyph->object)->size)
6111 || (BUFFERP (glyph->object)
6112 && glyph->charpos >= BEGV
6113 && glyph->charpos < ZV))
6114 help = Fget_text_property (make_number (glyph->charpos),
6115 Qhelp_echo, glyph->object);
6117 if (!NILP (help))
6119 help_echo = help;
6120 help_echo_window = window;
6121 help_echo_object = glyph->object;
6122 help_echo_pos = glyph->charpos;
6127 BEGV = obegv;
6128 ZV = ozv;
6129 current_buffer = obuf;
6134 static void
6135 redo_mouse_highlight ()
6137 if (!NILP (last_mouse_motion_frame)
6138 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6139 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6140 LOWORD (last_mouse_motion_event.lParam),
6141 HIWORD (last_mouse_motion_event.lParam));
6146 /***********************************************************************
6147 Tool-bars
6148 ***********************************************************************/
6150 static int x_tool_bar_item P_ ((struct frame *, int, int,
6151 struct glyph **, int *, int *, int *));
6153 /* Tool-bar item index of the item on which a mouse button was pressed
6154 or -1. */
6156 static int last_tool_bar_item;
6159 /* Get information about the tool-bar item at position X/Y on frame F.
6160 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6161 the current matrix of the tool-bar window of F, or NULL if not
6162 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6163 item in F->current_tool_bar_items. Value is
6165 -1 if X/Y is not on a tool-bar item
6166 0 if X/Y is on the same item that was highlighted before.
6167 1 otherwise. */
6169 static int
6170 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6171 struct frame *f;
6172 int x, y;
6173 struct glyph **glyph;
6174 int *hpos, *vpos, *prop_idx;
6176 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6177 struct window *w = XWINDOW (f->tool_bar_window);
6178 int area;
6180 /* Find the glyph under X/Y. */
6181 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6182 if (*glyph == NULL)
6183 return -1;
6185 /* Get the start of this tool-bar item's properties in
6186 f->current_tool_bar_items. */
6187 if (!tool_bar_item_info (f, *glyph, prop_idx))
6188 return -1;
6190 /* Is mouse on the highlighted item? */
6191 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6192 && *vpos >= dpyinfo->mouse_face_beg_row
6193 && *vpos <= dpyinfo->mouse_face_end_row
6194 && (*vpos > dpyinfo->mouse_face_beg_row
6195 || *hpos >= dpyinfo->mouse_face_beg_col)
6196 && (*vpos < dpyinfo->mouse_face_end_row
6197 || *hpos < dpyinfo->mouse_face_end_col
6198 || dpyinfo->mouse_face_past_end))
6199 return 0;
6201 return 1;
6205 /* Handle mouse button event on the tool-bar of frame F, at
6206 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6207 or ButtonRelase. */
6209 static void
6210 w32_handle_tool_bar_click (f, button_event)
6211 struct frame *f;
6212 struct input_event *button_event;
6214 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6215 struct window *w = XWINDOW (f->tool_bar_window);
6216 int hpos, vpos, prop_idx;
6217 struct glyph *glyph;
6218 Lisp_Object enabled_p;
6219 int x = XFASTINT (button_event->x);
6220 int y = XFASTINT (button_event->y);
6222 /* If not on the highlighted tool-bar item, return. */
6223 frame_to_window_pixel_xy (w, &x, &y);
6224 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6225 return;
6227 /* If item is disabled, do nothing. */
6228 enabled_p = (XVECTOR (f->current_tool_bar_items)
6229 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6230 if (NILP (enabled_p))
6231 return;
6233 if (button_event->kind == mouse_click)
6235 /* Show item in pressed state. */
6236 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6237 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6238 last_tool_bar_item = prop_idx;
6240 else
6242 Lisp_Object key, frame;
6243 struct input_event event;
6245 /* Show item in released state. */
6246 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6247 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6249 key = (XVECTOR (f->current_tool_bar_items)
6250 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6252 XSETFRAME (frame, f);
6253 event.kind = TOOL_BAR_EVENT;
6254 event.frame_or_window = frame;
6255 event.arg = frame;
6256 kbd_buffer_store_event (&event);
6258 event.kind = TOOL_BAR_EVENT;
6259 event.frame_or_window = frame;
6260 event.arg = key;
6261 event.modifiers = button_event->modifiers;
6262 kbd_buffer_store_event (&event);
6263 last_tool_bar_item = -1;
6268 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6269 tool-bar window-relative coordinates X/Y. Called from
6270 note_mouse_highlight. */
6272 static void
6273 note_tool_bar_highlight (f, x, y)
6274 struct frame *f;
6275 int x, y;
6277 Lisp_Object window = f->tool_bar_window;
6278 struct window *w = XWINDOW (window);
6279 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6280 int hpos, vpos;
6281 struct glyph *glyph;
6282 struct glyph_row *row;
6283 int i;
6284 Lisp_Object enabled_p;
6285 int prop_idx;
6286 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6287 int mouse_down_p, rc;
6289 /* Function note_mouse_highlight is called with negative x(y
6290 values when mouse moves outside of the frame. */
6291 if (x <= 0 || y <= 0)
6293 clear_mouse_face (dpyinfo);
6294 return;
6297 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6298 if (rc < 0)
6300 /* Not on tool-bar item. */
6301 clear_mouse_face (dpyinfo);
6302 return;
6304 else if (rc == 0)
6305 /* On same tool-bar item as before. */
6306 goto set_help_echo;
6308 clear_mouse_face (dpyinfo);
6310 /* Mouse is down, but on different tool-bar item? */
6311 mouse_down_p = (dpyinfo->grabbed
6312 && f == last_mouse_frame
6313 && FRAME_LIVE_P (f));
6314 if (mouse_down_p
6315 && last_tool_bar_item != prop_idx)
6316 return;
6318 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6319 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6321 /* If tool-bar item is not enabled, don't highlight it. */
6322 enabled_p = (XVECTOR (f->current_tool_bar_items)
6323 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6324 if (!NILP (enabled_p))
6326 /* Compute the x-position of the glyph. In front and past the
6327 image is a space. We include this is the highlighted area. */
6328 row = MATRIX_ROW (w->current_matrix, vpos);
6329 for (i = x = 0; i < hpos; ++i)
6330 x += row->glyphs[TEXT_AREA][i].pixel_width;
6332 /* Record this as the current active region. */
6333 dpyinfo->mouse_face_beg_col = hpos;
6334 dpyinfo->mouse_face_beg_row = vpos;
6335 dpyinfo->mouse_face_beg_x = x;
6336 dpyinfo->mouse_face_beg_y = row->y;
6337 dpyinfo->mouse_face_past_end = 0;
6339 dpyinfo->mouse_face_end_col = hpos + 1;
6340 dpyinfo->mouse_face_end_row = vpos;
6341 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6342 dpyinfo->mouse_face_end_y = row->y;
6343 dpyinfo->mouse_face_window = window;
6344 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6346 /* Display it as active. */
6347 show_mouse_face (dpyinfo, draw);
6348 dpyinfo->mouse_face_image_state = draw;
6351 set_help_echo:
6353 /* Set help_echo to a help string.to display for this tool-bar item.
6354 w32_read_socket does the rest. */
6355 help_echo_object = help_echo_window = Qnil;
6356 help_echo_pos = -1;
6357 help_echo = (XVECTOR (f->current_tool_bar_items)
6358 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6359 if (NILP (help_echo))
6360 help_echo = (XVECTOR (f->current_tool_bar_items)
6361 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6366 /* Find the glyph matrix position of buffer position POS in window W.
6367 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6368 current glyphs must be up to date. If POS is above window start
6369 return (0, 0, 0, 0). If POS is after end of W, return end of
6370 last line in W. */
6372 static int
6373 fast_find_position (w, pos, hpos, vpos, x, y)
6374 struct window *w;
6375 int pos;
6376 int *hpos, *vpos, *x, *y;
6378 int i;
6379 int lastcol;
6380 int maybe_next_line_p = 0;
6381 int line_start_position;
6382 int yb = window_text_bottom_y (w);
6383 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6384 struct glyph_row *best_row = row;
6385 int row_vpos = 0, best_row_vpos = 0;
6386 int current_x;
6388 while (row->y < yb)
6390 if (row->used[TEXT_AREA])
6391 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6392 else
6393 line_start_position = 0;
6395 if (line_start_position > pos)
6396 break;
6397 /* If the position sought is the end of the buffer,
6398 don't include the blank lines at the bottom of the window. */
6399 else if (line_start_position == pos
6400 && pos == BUF_ZV (XBUFFER (w->buffer)))
6402 maybe_next_line_p = 1;
6403 break;
6405 else if (line_start_position > 0)
6407 best_row = row;
6408 best_row_vpos = row_vpos;
6411 if (row->y + row->height >= yb)
6412 break;
6414 ++row;
6415 ++row_vpos;
6418 /* Find the right column within BEST_ROW. */
6419 lastcol = 0;
6420 current_x = best_row->x;
6421 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6423 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6424 int charpos;
6426 charpos = glyph->charpos;
6427 if (charpos == pos)
6429 *hpos = i;
6430 *vpos = best_row_vpos;
6431 *x = current_x;
6432 *y = best_row->y;
6433 return 1;
6435 else if (charpos > pos)
6436 break;
6437 else if (charpos > 0)
6438 lastcol = i;
6440 current_x += glyph->pixel_width;
6443 /* If we're looking for the end of the buffer,
6444 and we didn't find it in the line we scanned,
6445 use the start of the following line. */
6446 if (maybe_next_line_p)
6448 ++best_row;
6449 ++best_row_vpos;
6450 lastcol = 0;
6451 current_x = best_row->x;
6454 *vpos = best_row_vpos;
6455 *hpos = lastcol + 1;
6456 *x = current_x;
6457 *y = best_row->y;
6458 return 0;
6462 /* Display the active region described by mouse_face_*
6463 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6465 static void
6466 show_mouse_face (dpyinfo, draw)
6467 struct w32_display_info *dpyinfo;
6468 enum draw_glyphs_face draw;
6470 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6471 struct frame *f = XFRAME (WINDOW_FRAME (w));
6472 int i;
6473 int cursor_off_p = 0;
6474 struct cursor_pos saved_cursor;
6476 saved_cursor = output_cursor;
6478 /* If window is in the process of being destroyed, don't bother
6479 to do anything. */
6480 if (w->current_matrix == NULL)
6481 goto set_x_cursor;
6483 /* Recognize when we are called to operate on rows that don't exist
6484 anymore. This can happen when a window is split. */
6485 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6486 goto set_x_cursor;
6488 set_output_cursor (&w->phys_cursor);
6490 /* Note that mouse_face_beg_row etc. are window relative. */
6491 for (i = dpyinfo->mouse_face_beg_row;
6492 i <= dpyinfo->mouse_face_end_row;
6493 i++)
6495 int start_hpos, end_hpos, start_x;
6496 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6498 /* Don't do anything if row doesn't have valid contents. */
6499 if (!row->enabled_p)
6500 continue;
6502 /* For all but the first row, the highlight starts at column 0. */
6503 if (i == dpyinfo->mouse_face_beg_row)
6505 start_hpos = dpyinfo->mouse_face_beg_col;
6506 start_x = dpyinfo->mouse_face_beg_x;
6508 else
6510 start_hpos = 0;
6511 start_x = 0;
6514 if (i == dpyinfo->mouse_face_end_row)
6515 end_hpos = dpyinfo->mouse_face_end_col;
6516 else
6517 end_hpos = row->used[TEXT_AREA];
6519 /* If the cursor's in the text we are about to rewrite, turn the
6520 cursor off. */
6521 if (!w->pseudo_window_p
6522 && i == output_cursor.vpos
6523 && output_cursor.hpos >= start_hpos - 1
6524 && output_cursor.hpos <= end_hpos)
6526 x_update_window_cursor (w, 0);
6527 cursor_off_p = 1;
6530 if (end_hpos > start_hpos)
6532 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
6533 x_draw_glyphs (w, start_x, row, TEXT_AREA,
6534 start_hpos, end_hpos, draw, NULL, NULL, 0);
6538 /* If we turned the cursor off, turn it back on. */
6539 if (cursor_off_p)
6540 x_display_cursor (w, 1,
6541 output_cursor.hpos, output_cursor.vpos,
6542 output_cursor.x, output_cursor.y);
6544 output_cursor = saved_cursor;
6546 set_x_cursor:
6547 #if 0 /* NTEMACS_TODO: mouse cursor */
6548 /* Change the mouse cursor. */
6549 if (draw == DRAW_NORMAL_TEXT)
6550 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6551 f->output_data.x->text_cursor);
6552 else if (draw == DRAW_MOUSE_FACE)
6553 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6554 f->output_data.x->cross_cursor);
6555 else
6556 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6557 f->output_data.x->nontext_cursor);
6558 #endif
6562 /* Clear out the mouse-highlighted active region.
6563 Redraw it un-highlighted first. */
6565 void
6566 clear_mouse_face (dpyinfo)
6567 struct w32_display_info *dpyinfo;
6569 if (tip_frame)
6570 return;
6572 if (! NILP (dpyinfo->mouse_face_window))
6573 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
6575 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6576 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6577 dpyinfo->mouse_face_window = Qnil;
6581 /* Clear any mouse-face on window W. This function is part of the
6582 redisplay interface, and is called from try_window_id and similar
6583 functions to ensure the mouse-highlight is off. */
6585 static void
6586 x_clear_mouse_face (w)
6587 struct window *w;
6589 struct w32_display_info *dpyinfo
6590 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
6591 Lisp_Object window;
6593 XSETWINDOW (window, w);
6594 if (EQ (window, dpyinfo->mouse_face_window))
6595 clear_mouse_face (dpyinfo);
6599 /* Just discard the mouse face information for frame F, if any.
6600 This is used when the size of F is changed. */
6602 void
6603 cancel_mouse_face (f)
6604 FRAME_PTR f;
6606 Lisp_Object window;
6607 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6609 window = dpyinfo->mouse_face_window;
6610 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6612 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6613 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6614 dpyinfo->mouse_face_window = Qnil;
6618 static struct scroll_bar *x_window_to_scroll_bar ();
6619 static void x_scroll_bar_report_motion ();
6621 /* Return the current position of the mouse.
6622 *fp should be a frame which indicates which display to ask about.
6624 If the mouse movement started in a scroll bar, set *fp, *bar_window,
6625 and *part to the frame, window, and scroll bar part that the mouse
6626 is over. Set *x and *y to the portion and whole of the mouse's
6627 position on the scroll bar.
6629 If the mouse movement started elsewhere, set *fp to the frame the
6630 mouse is on, *bar_window to nil, and *x and *y to the character cell
6631 the mouse is over.
6633 Set *time to the server time-stamp for the time at which the mouse
6634 was at this position.
6636 Don't store anything if we don't have a valid set of values to report.
6638 This clears the mouse_moved flag, so we can wait for the next mouse
6639 movement. */
6641 static void
6642 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
6643 FRAME_PTR *fp;
6644 int insist;
6645 Lisp_Object *bar_window;
6646 enum scroll_bar_part *part;
6647 Lisp_Object *x, *y;
6648 unsigned long *time;
6650 FRAME_PTR f1;
6652 BLOCK_INPUT;
6654 if (! NILP (last_mouse_scroll_bar) && insist == 0)
6655 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
6656 else
6658 POINT pt;
6660 Lisp_Object frame, tail;
6662 /* Clear the mouse-moved flag for every frame on this display. */
6663 FOR_EACH_FRAME (tail, frame)
6664 XFRAME (frame)->mouse_moved = 0;
6666 last_mouse_scroll_bar = Qnil;
6668 GetCursorPos (&pt);
6670 /* Now we have a position on the root; find the innermost window
6671 containing the pointer. */
6673 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
6674 && FRAME_LIVE_P (last_mouse_frame))
6676 /* If mouse was grabbed on a frame, give coords for that frame
6677 even if the mouse is now outside it. */
6678 f1 = last_mouse_frame;
6680 else
6682 /* Is window under mouse one of our frames? */
6683 f1 = x_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp), WindowFromPoint(pt));
6686 /* If not, is it one of our scroll bars? */
6687 if (! f1)
6689 struct scroll_bar *bar = x_window_to_scroll_bar (WindowFromPoint(pt));
6691 if (bar)
6693 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6697 if (f1 == 0 && insist > 0)
6698 f1 = SELECTED_FRAME ();
6700 if (f1)
6702 /* Ok, we found a frame. Store all the values.
6703 last_mouse_glyph is a rectangle used to reduce the
6704 generation of mouse events. To not miss any motion
6705 events, we must divide the frame into rectangles of the
6706 size of the smallest character that could be displayed
6707 on it, i.e. into the same rectangles that matrices on
6708 the frame are divided into. */
6710 #if OLD_REDISPLAY_CODE
6711 int ignore1, ignore2;
6713 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
6715 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
6716 &last_mouse_glyph,
6717 FRAME_W32_DISPLAY_INFO (f1)->grabbed
6718 || insist);
6719 #else
6720 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
6722 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
6723 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
6724 int x = pt.x;
6725 int y = pt.y;
6727 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
6728 round down even for negative values. */
6729 if (x < 0)
6730 x -= width - 1;
6731 if (y < 0)
6732 y -= height - 1;
6734 last_mouse_glyph.left = (x + width - 1) / width * width;
6735 last_mouse_glyph.top = (y + height - 1) / height * height;
6736 last_mouse_glyph.right = last_mouse_glyph.left + width;
6737 last_mouse_glyph.bottom = last_mouse_glyph.top + height;
6739 #endif
6741 *bar_window = Qnil;
6742 *part = 0;
6743 *fp = f1;
6744 XSETINT (*x, pt.x);
6745 XSETINT (*y, pt.y);
6746 *time = last_mouse_movement_time;
6751 UNBLOCK_INPUT;
6755 /* Scroll bar support. */
6757 /* Given a window ID, find the struct scroll_bar which manages it.
6758 This can be called in GC, so we have to make sure to strip off mark
6759 bits. */
6761 static struct scroll_bar *
6762 x_window_to_scroll_bar (window_id)
6763 Window window_id;
6765 Lisp_Object tail;
6767 for (tail = Vframe_list;
6768 XGCTYPE (tail) == Lisp_Cons;
6769 tail = XCDR (tail))
6771 Lisp_Object frame, bar, condemned;
6773 frame = XCAR (tail);
6774 /* All elements of Vframe_list should be frames. */
6775 if (! GC_FRAMEP (frame))
6776 abort ();
6778 /* Scan this frame's scroll bar list for a scroll bar with the
6779 right window ID. */
6780 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
6781 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
6782 /* This trick allows us to search both the ordinary and
6783 condemned scroll bar lists with one loop. */
6784 ! GC_NILP (bar) || (bar = condemned,
6785 condemned = Qnil,
6786 ! GC_NILP (bar));
6787 bar = XSCROLL_BAR (bar)->next)
6788 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
6789 return XSCROLL_BAR (bar);
6792 return 0;
6797 /* Set the thumb size and position of scroll bar BAR. We are currently
6798 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6800 static void
6801 w32_set_scroll_bar_thumb (bar, portion, position, whole)
6802 struct scroll_bar *bar;
6803 int portion, position, whole;
6805 Window w = SCROLL_BAR_W32_WINDOW (bar);
6806 int range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
6807 int sb_page, sb_pos;
6808 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
6810 if (whole)
6812 /* Position scroll bar at rock bottom if the bottom of the
6813 buffer is visible. This avoids shinking the thumb away
6814 to nothing if it is held at the bottom of the buffer. */
6815 if (position + portion >= whole)
6817 sb_page = range * (whole - position) / whole
6818 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
6819 sb_pos = range;
6822 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
6823 sb_pos = position * range / whole;
6825 else
6827 sb_page = range;
6828 sb_pos = 0;
6831 BLOCK_INPUT;
6833 if (pfnSetScrollInfo)
6835 SCROLLINFO si;
6837 si.cbSize = sizeof (si);
6838 /* Only update page size if currently dragging, to reduce
6839 flicker effects. */
6840 if (draggingp)
6841 si.fMask = SIF_PAGE;
6842 else
6843 si.fMask = SIF_PAGE | SIF_POS;
6844 si.nPage = sb_page;
6845 si.nPos = sb_pos;
6847 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
6849 else
6850 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
6852 UNBLOCK_INPUT;
6856 /************************************************************************
6857 Scroll bars, general
6858 ************************************************************************/
6860 HWND
6861 my_create_scrollbar (f, bar)
6862 struct frame * f;
6863 struct scroll_bar * bar;
6865 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
6866 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
6867 (LPARAM) bar);
6870 //#define ATTACH_THREADS
6872 BOOL
6873 my_show_window (FRAME_PTR f, HWND hwnd, int how)
6875 #ifndef ATTACH_THREADS
6876 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
6877 (WPARAM) hwnd, (LPARAM) how);
6878 #else
6879 return ShowWindow (hwnd, how);
6880 #endif
6883 void
6884 my_set_window_pos (HWND hwnd, HWND hwndAfter,
6885 int x, int y, int cx, int cy, UINT flags)
6887 #ifndef ATTACH_THREADS
6888 WINDOWPOS pos;
6889 pos.hwndInsertAfter = hwndAfter;
6890 pos.x = x;
6891 pos.y = y;
6892 pos.cx = cx;
6893 pos.cy = cy;
6894 pos.flags = flags;
6895 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
6896 #else
6897 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
6898 #endif
6901 void
6902 my_set_focus (f, hwnd)
6903 struct frame * f;
6904 HWND hwnd;
6906 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
6907 (WPARAM) hwnd, 0);
6910 void
6911 my_set_foreground_window (hwnd)
6912 HWND hwnd;
6914 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
6917 void
6918 my_destroy_window (f, hwnd)
6919 struct frame * f;
6920 HWND hwnd;
6922 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
6923 (WPARAM) hwnd, 0);
6926 /* Create a scroll bar and return the scroll bar vector for it. W is
6927 the Emacs window on which to create the scroll bar. TOP, LEFT,
6928 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
6929 scroll bar. */
6931 static struct scroll_bar *
6932 x_scroll_bar_create (w, top, left, width, height)
6933 struct window *w;
6934 int top, left, width, height;
6936 struct frame *f = XFRAME (WINDOW_FRAME (w));
6937 HWND hwnd;
6938 struct scroll_bar *bar
6939 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
6941 BLOCK_INPUT;
6943 XSETWINDOW (bar->window, w);
6944 XSETINT (bar->top, top);
6945 XSETINT (bar->left, left);
6946 XSETINT (bar->width, width);
6947 XSETINT (bar->height, height);
6948 XSETINT (bar->start, 0);
6949 XSETINT (bar->end, 0);
6950 bar->dragging = Qnil;
6952 /* Requires geometry to be set before call to create the real window */
6954 hwnd = my_create_scrollbar (f, bar);
6956 if (pfnSetScrollInfo)
6958 SCROLLINFO si;
6960 si.cbSize = sizeof (si);
6961 si.fMask = SIF_ALL;
6962 si.nMin = 0;
6963 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
6964 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
6965 si.nPage = si.nMax;
6966 si.nPos = 0;
6968 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
6970 else
6972 SetScrollRange (hwnd, SB_CTL, 0,
6973 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
6974 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
6977 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
6979 /* Add bar to its frame's list of scroll bars. */
6980 bar->next = FRAME_SCROLL_BARS (f);
6981 bar->prev = Qnil;
6982 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
6983 if (! NILP (bar->next))
6984 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6986 UNBLOCK_INPUT;
6988 return bar;
6992 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6993 nil. */
6995 static void
6996 x_scroll_bar_remove (bar)
6997 struct scroll_bar *bar;
6999 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7001 BLOCK_INPUT;
7003 /* Destroy the window. */
7004 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
7006 /* Disassociate this scroll bar from its window. */
7007 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
7009 UNBLOCK_INPUT;
7012 /* Set the handle of the vertical scroll bar for WINDOW to indicate
7013 that we are displaying PORTION characters out of a total of WHOLE
7014 characters, starting at POSITION. If WINDOW has no scroll bar,
7015 create one. */
7016 static void
7017 w32_set_vertical_scroll_bar (w, portion, whole, position)
7018 struct window *w;
7019 int portion, whole, position;
7021 struct frame *f = XFRAME (w->frame);
7022 struct scroll_bar *bar;
7023 int top, height, left, sb_left, width, sb_width;
7024 int window_x, window_y, window_width, window_height;
7026 /* Get window dimensions. */
7027 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7028 top = window_y;
7029 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7030 height = window_height;
7032 /* Compute the left edge of the scroll bar area. */
7033 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7034 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7035 else
7036 left = XFASTINT (w->left);
7037 left *= CANON_X_UNIT (f);
7038 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7040 /* Compute the width of the scroll bar which might be less than
7041 the width of the area reserved for the scroll bar. */
7042 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7043 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
7044 else
7045 sb_width = width;
7047 /* Compute the left edge of the scroll bar. */
7048 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7049 sb_left = left + width - sb_width - (width - sb_width) / 2;
7050 else
7051 sb_left = left + (width - sb_width) / 2;
7053 /* Does the scroll bar exist yet? */
7054 if (NILP (w->vertical_scroll_bar))
7056 HDC hdc;
7057 BLOCK_INPUT;
7058 hdc = get_frame_dc (f);
7059 w32_clear_area (f, hdc, left, top, width, height);
7060 release_frame_dc (f, hdc);
7061 UNBLOCK_INPUT;
7063 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
7065 else
7067 /* It may just need to be moved and resized. */
7068 HWND hwnd;
7070 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7071 hwnd = SCROLL_BAR_W32_WINDOW (bar);
7073 /* If already correctly positioned, do nothing. */
7074 if ( XINT (bar->left) == sb_left
7075 && XINT (bar->top) == top
7076 && XINT (bar->width) == sb_width
7077 && XINT (bar->height) == height )
7079 /* Redraw after clear_frame. */
7080 if (!my_show_window (f, hwnd, SW_NORMAL))
7081 InvalidateRect (hwnd, NULL, FALSE);
7083 else
7085 HDC hdc;
7086 BLOCK_INPUT;
7088 hdc = get_frame_dc (f);
7089 /* Since Windows scroll bars are smaller than the space reserved
7090 for them on the frame, we have to clear "under" them. */
7091 w32_clear_area (f, hdc,
7092 left,
7093 top,
7094 width,
7095 height);
7096 release_frame_dc (f, hdc);
7098 /* Make sure scroll bar is "visible" before moving, to ensure the
7099 area of the parent window now exposed will be refreshed. */
7100 my_show_window (f, hwnd, SW_HIDE);
7101 MoveWindow (hwnd, sb_left, top,
7102 sb_width, height, TRUE);
7103 if (pfnSetScrollInfo)
7105 SCROLLINFO si;
7107 si.cbSize = sizeof (si);
7108 si.fMask = SIF_RANGE;
7109 si.nMin = 0;
7110 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7111 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7113 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7115 else
7116 SetScrollRange (hwnd, SB_CTL, 0,
7117 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7118 my_show_window (f, hwnd, SW_NORMAL);
7119 // InvalidateRect (w, NULL, FALSE);
7121 /* Remember new settings. */
7122 XSETINT (bar->left, sb_left);
7123 XSETINT (bar->top, top);
7124 XSETINT (bar->width, sb_width);
7125 XSETINT (bar->height, height);
7127 UNBLOCK_INPUT;
7130 w32_set_scroll_bar_thumb (bar, portion, position, whole);
7132 XSETVECTOR (w->vertical_scroll_bar, bar);
7136 /* The following three hooks are used when we're doing a thorough
7137 redisplay of the frame. We don't explicitly know which scroll bars
7138 are going to be deleted, because keeping track of when windows go
7139 away is a real pain - "Can you say set-window-configuration, boys
7140 and girls?" Instead, we just assert at the beginning of redisplay
7141 that *all* scroll bars are to be removed, and then save a scroll bar
7142 from the fiery pit when we actually redisplay its window. */
7144 /* Arrange for all scroll bars on FRAME to be removed at the next call
7145 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
7146 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7148 static void
7149 w32_condemn_scroll_bars (frame)
7150 FRAME_PTR frame;
7152 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7153 while (! NILP (FRAME_SCROLL_BARS (frame)))
7155 Lisp_Object bar;
7156 bar = FRAME_SCROLL_BARS (frame);
7157 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7158 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7159 XSCROLL_BAR (bar)->prev = Qnil;
7160 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7161 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7162 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7166 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7167 Note that WINDOW isn't necessarily condemned at all. */
7168 static void
7169 w32_redeem_scroll_bar (window)
7170 struct window *window;
7172 struct scroll_bar *bar;
7174 /* We can't redeem this window's scroll bar if it doesn't have one. */
7175 if (NILP (window->vertical_scroll_bar))
7176 abort ();
7178 bar = XSCROLL_BAR (window->vertical_scroll_bar);
7180 /* Unlink it from the condemned list. */
7182 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
7184 if (NILP (bar->prev))
7186 /* If the prev pointer is nil, it must be the first in one of
7187 the lists. */
7188 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
7189 /* It's not condemned. Everything's fine. */
7190 return;
7191 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7192 window->vertical_scroll_bar))
7193 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
7194 else
7195 /* If its prev pointer is nil, it must be at the front of
7196 one or the other! */
7197 abort ();
7199 else
7200 XSCROLL_BAR (bar->prev)->next = bar->next;
7202 if (! NILP (bar->next))
7203 XSCROLL_BAR (bar->next)->prev = bar->prev;
7205 bar->next = FRAME_SCROLL_BARS (f);
7206 bar->prev = Qnil;
7207 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7208 if (! NILP (bar->next))
7209 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7213 /* Remove all scroll bars on FRAME that haven't been saved since the
7214 last call to `*condemn_scroll_bars_hook'. */
7216 static void
7217 w32_judge_scroll_bars (f)
7218 FRAME_PTR f;
7220 Lisp_Object bar, next;
7222 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7224 /* Clear out the condemned list now so we won't try to process any
7225 more events on the hapless scroll bars. */
7226 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
7228 for (; ! NILP (bar); bar = next)
7230 struct scroll_bar *b = XSCROLL_BAR (bar);
7232 x_scroll_bar_remove (b);
7234 next = b->next;
7235 b->next = b->prev = Qnil;
7238 /* Now there should be no references to the condemned scroll bars,
7239 and they should get garbage-collected. */
7242 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7243 is set to something other than no_event, it is enqueued.
7245 This may be called from a signal handler, so we have to ignore GC
7246 mark bits. */
7248 static int
7249 x_scroll_bar_handle_click (bar, msg, emacs_event)
7250 struct scroll_bar *bar;
7251 W32Msg *msg;
7252 struct input_event *emacs_event;
7254 if (! GC_WINDOWP (bar->window))
7255 abort ();
7257 emacs_event->kind = w32_scroll_bar_click;
7258 emacs_event->code = 0;
7259 /* not really meaningful to distinguish up/down */
7260 emacs_event->modifiers = msg->dwModifiers;
7261 emacs_event->frame_or_window = bar->window;
7262 emacs_event->arg = Qnil;
7263 emacs_event->timestamp = msg->msg.time;
7266 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7267 int y;
7268 int dragging = !NILP (bar->dragging);
7270 if (pfnGetScrollInfo)
7272 SCROLLINFO si;
7274 si.cbSize = sizeof (si);
7275 si.fMask = SIF_POS;
7277 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
7278 y = si.nPos;
7280 else
7281 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
7283 bar->dragging = Qnil;
7286 last_mouse_scroll_bar_pos = msg->msg.wParam;
7288 switch (LOWORD (msg->msg.wParam))
7290 case SB_LINEDOWN:
7291 emacs_event->part = scroll_bar_down_arrow;
7292 break;
7293 case SB_LINEUP:
7294 emacs_event->part = scroll_bar_up_arrow;
7295 break;
7296 case SB_PAGEUP:
7297 emacs_event->part = scroll_bar_above_handle;
7298 break;
7299 case SB_PAGEDOWN:
7300 emacs_event->part = scroll_bar_below_handle;
7301 break;
7302 case SB_TOP:
7303 emacs_event->part = scroll_bar_handle;
7304 y = 0;
7305 break;
7306 case SB_BOTTOM:
7307 emacs_event->part = scroll_bar_handle;
7308 y = top_range;
7309 break;
7310 case SB_THUMBTRACK:
7311 case SB_THUMBPOSITION:
7312 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7313 y = HIWORD (msg->msg.wParam);
7314 bar->dragging = Qt;
7315 emacs_event->part = scroll_bar_handle;
7317 /* "Silently" update current position. */
7318 if (pfnSetScrollInfo)
7320 SCROLLINFO si;
7322 si.cbSize = sizeof (si);
7323 si.fMask = SIF_POS;
7324 si.nPos = y;
7325 /* Remember apparent position (we actually lag behind the real
7326 position, so don't set that directly. */
7327 last_scroll_bar_drag_pos = y;
7329 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
7331 else
7332 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
7333 break;
7334 case SB_ENDSCROLL:
7335 /* If this is the end of a drag sequence, then reset the scroll
7336 handle size to normal and do a final redraw. Otherwise do
7337 nothing. */
7338 if (dragging)
7340 if (pfnSetScrollInfo)
7342 SCROLLINFO si;
7343 int start = XINT (bar->start);
7344 int end = XINT (bar->end);
7346 si.cbSize = sizeof (si);
7347 si.fMask = SIF_PAGE | SIF_POS;
7348 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7349 si.nPos = last_scroll_bar_drag_pos;
7350 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
7352 else
7353 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
7355 /* fall through */
7356 default:
7357 emacs_event->kind = no_event;
7358 return FALSE;
7361 XSETINT (emacs_event->x, y);
7362 XSETINT (emacs_event->y, top_range);
7364 return TRUE;
7368 /* Return information to the user about the current position of the mouse
7369 on the scroll bar. */
7371 static void
7372 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
7373 FRAME_PTR *fp;
7374 Lisp_Object *bar_window;
7375 enum scroll_bar_part *part;
7376 Lisp_Object *x, *y;
7377 unsigned long *time;
7379 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
7380 Window w = SCROLL_BAR_W32_WINDOW (bar);
7381 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7382 int pos;
7383 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7385 BLOCK_INPUT;
7387 *fp = f;
7388 *bar_window = bar->window;
7390 if (pfnGetScrollInfo)
7392 SCROLLINFO si;
7394 si.cbSize = sizeof (si);
7395 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
7397 pfnGetScrollInfo (w, SB_CTL, &si);
7398 pos = si.nPos;
7399 top_range = si.nMax - si.nPage + 1;
7401 else
7402 pos = GetScrollPos (w, SB_CTL);
7404 switch (LOWORD (last_mouse_scroll_bar_pos))
7406 case SB_THUMBPOSITION:
7407 case SB_THUMBTRACK:
7408 *part = scroll_bar_handle;
7409 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7410 pos = HIWORD (last_mouse_scroll_bar_pos);
7411 break;
7412 case SB_LINEDOWN:
7413 *part = scroll_bar_handle;
7414 pos++;
7415 break;
7416 default:
7417 *part = scroll_bar_handle;
7418 break;
7421 XSETINT(*x, pos);
7422 XSETINT(*y, top_range);
7424 f->mouse_moved = 0;
7425 last_mouse_scroll_bar = Qnil;
7427 *time = last_mouse_movement_time;
7429 UNBLOCK_INPUT;
7433 /* The screen has been cleared so we may have changed foreground or
7434 background colors, and the scroll bars may need to be redrawn.
7435 Clear out the scroll bars, and ask for expose events, so we can
7436 redraw them. */
7438 void
7439 x_scroll_bar_clear (f)
7440 FRAME_PTR f;
7442 Lisp_Object bar;
7444 /* We can have scroll bars even if this is 0,
7445 if we just turned off scroll bar mode.
7446 But in that case we should not clear them. */
7447 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7448 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7449 bar = XSCROLL_BAR (bar)->next)
7451 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
7452 HDC hdc = GetDC (window);
7453 RECT rect;
7455 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
7456 arranges to refresh the scroll bar if hidden. */
7457 my_show_window (f, window, SW_HIDE);
7459 GetClientRect (window, &rect);
7460 select_palette (f, hdc);
7461 w32_clear_rect (f, hdc, &rect);
7462 deselect_palette (f, hdc);
7464 ReleaseDC (window, hdc);
7468 show_scroll_bars (f, how)
7469 FRAME_PTR f;
7470 int how;
7472 Lisp_Object bar;
7474 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7475 bar = XSCROLL_BAR (bar)->next)
7477 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
7478 my_show_window (f, window, how);
7483 /* The main W32 event-reading loop - w32_read_socket. */
7485 /* Time stamp of enter window event. This is only used by w32_read_socket,
7486 but we have to put it out here, since static variables within functions
7487 sometimes don't work. */
7489 static Time enter_timestamp;
7491 /* Record the last 100 characters stored
7492 to help debug the loss-of-chars-during-GC problem. */
7494 static int temp_index;
7495 static short temp_buffer[100];
7498 /* Read events coming from the W32 shell.
7499 This routine is called by the SIGIO handler.
7500 We return as soon as there are no more events to be read.
7502 Events representing keys are stored in buffer BUFP,
7503 which can hold up to NUMCHARS characters.
7504 We return the number of characters stored into the buffer,
7505 thus pretending to be `read'.
7507 EXPECTED is nonzero if the caller knows input is available.
7509 Some of these messages are reposted back to the message queue since the
7510 system calls the windows proc directly in a context where we cannot return
7511 the data nor can we guarantee the state we are in. So if we dispatch them
7512 we will get into an infinite loop. To prevent this from ever happening we
7513 will set a variable to indicate we are in the read_socket call and indicate
7514 which message we are processing since the windows proc gets called
7515 recursively with different messages by the system.
7519 w32_read_socket (sd, bufp, numchars, expected)
7520 register int sd;
7521 /* register */ struct input_event *bufp;
7522 /* register */ int numchars;
7523 int expected;
7525 int count = 0;
7526 int check_visibility = 0;
7527 W32Msg msg;
7528 struct frame *f;
7529 struct w32_display_info *dpyinfo = &one_w32_display_info;
7531 if (interrupt_input_blocked)
7533 interrupt_input_pending = 1;
7534 return -1;
7537 interrupt_input_pending = 0;
7538 BLOCK_INPUT;
7540 /* So people can tell when we have read the available input. */
7541 input_signal_count++;
7543 if (numchars <= 0)
7544 abort (); /* Don't think this happens. */
7546 /* NTEMACS_TODO: tooltips, tool-bars, ghostscript integration, mouse
7547 cursors. */
7548 while (get_next_msg (&msg, FALSE))
7550 switch (msg.msg.message)
7552 case WM_PAINT:
7553 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7555 if (f)
7557 if (msg.rect.right == msg.rect.left ||
7558 msg.rect.bottom == msg.rect.top)
7560 /* We may get paint messages even though the client
7561 area is clipped - these are not expose events. */
7562 DebPrint (("clipped frame %04x (%s) got WM_PAINT\n", f,
7563 XSTRING (f->name)->data));
7565 else if (f->async_visible != 1)
7567 /* Definitely not obscured, so mark as visible. */
7568 f->async_visible = 1;
7569 f->async_iconified = 0;
7570 SET_FRAME_GARBAGED (f);
7571 DebPrint (("frame %04x (%s) reexposed\n", f,
7572 XSTRING (f->name)->data));
7574 /* WM_PAINT serves as MapNotify as well, so report
7575 visibility changes properly. */
7576 if (f->iconified)
7578 bufp->kind = deiconify_event;
7579 XSETFRAME (bufp->frame_or_window, f);
7580 bufp->arg = Qnil;
7581 bufp++;
7582 count++;
7583 numchars--;
7585 else if (! NILP(Vframe_list)
7586 && ! NILP (XCDR (Vframe_list)))
7587 /* Force a redisplay sooner or later to update the
7588 frame titles in case this is the second frame. */
7589 record_asynch_buffer_change ();
7591 else
7593 HDC hdc = get_frame_dc (f);
7595 /* Erase background again for safety. */
7596 w32_clear_rect (f, hdc, &msg.rect);
7597 release_frame_dc (f, hdc);
7598 expose_frame (f,
7599 msg.rect.left,
7600 msg.rect.top,
7601 msg.rect.right - msg.rect.left,
7602 msg.rect.bottom - msg.rect.top);
7605 break;
7607 case WM_INPUTLANGCHANGE:
7608 /* Generate a language change event. */
7609 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7611 if (f)
7613 if (numchars == 0)
7614 abort ();
7616 bufp->kind = language_change_event;
7617 XSETFRAME (bufp->frame_or_window, f);
7618 bufp->arg = Qnil;
7619 bufp->code = msg.msg.wParam;
7620 bufp->modifiers = msg.msg.lParam & 0xffff;
7621 bufp++;
7622 count++;
7623 numchars--;
7625 break;
7627 case WM_KEYDOWN:
7628 case WM_SYSKEYDOWN:
7629 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7631 if (f && !f->iconified)
7633 if (temp_index == sizeof temp_buffer / sizeof (short))
7634 temp_index = 0;
7635 temp_buffer[temp_index++] = msg.msg.wParam;
7636 bufp->kind = non_ascii_keystroke;
7637 bufp->code = msg.msg.wParam;
7638 bufp->modifiers = msg.dwModifiers;
7639 XSETFRAME (bufp->frame_or_window, f);
7640 bufp->arg = Qnil;
7641 bufp->timestamp = msg.msg.time;
7642 bufp++;
7643 numchars--;
7644 count++;
7646 break;
7648 case WM_SYSCHAR:
7649 case WM_CHAR:
7650 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7652 if (f && !f->iconified)
7654 if (temp_index == sizeof temp_buffer / sizeof (short))
7655 temp_index = 0;
7656 temp_buffer[temp_index++] = msg.msg.wParam;
7657 bufp->kind = ascii_keystroke;
7658 bufp->code = msg.msg.wParam;
7659 bufp->modifiers = msg.dwModifiers;
7660 XSETFRAME (bufp->frame_or_window, f);
7661 bufp->arg = Qnil;
7662 bufp->timestamp = msg.msg.time;
7663 bufp++;
7664 numchars--;
7665 count++;
7667 break;
7669 case WM_MOUSEMOVE:
7670 previous_help_echo = help_echo;
7671 help_echo = help_echo_object = help_echo_window = Qnil;
7672 help_echo_pos = -1;
7674 if (dpyinfo->grabbed && last_mouse_frame
7675 && FRAME_LIVE_P (last_mouse_frame))
7676 f = last_mouse_frame;
7677 else
7678 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7680 if (f)
7681 note_mouse_movement (f, &msg.msg);
7682 else
7684 /* If we move outside the frame, then we're
7685 certainly no longer on any text in the frame. */
7686 clear_mouse_face (FRAME_W32_DISPLAY_INFO (f));
7689 /* If the contents of the global variable help_echo
7690 has changed, generate a HELP_EVENT. */
7691 if (!NILP (help_echo)
7692 || !NILP (previous_help_echo))
7694 Lisp_Object frame;
7695 int n;
7697 if (f)
7698 XSETFRAME (frame, f);
7699 else
7700 frame = Qnil;
7702 any_help_event_p = 1;
7703 n = gen_help_event (bufp, numchars, help_echo, frame,
7704 help_echo_window, help_echo_object,
7705 help_echo_pos);
7706 bufp += n, count += n, numchars -= n;
7708 break;
7710 case WM_LBUTTONDOWN:
7711 case WM_LBUTTONUP:
7712 case WM_MBUTTONDOWN:
7713 case WM_MBUTTONUP:
7714 case WM_RBUTTONDOWN:
7715 case WM_RBUTTONUP:
7717 /* If we decide we want to generate an event to be seen
7718 by the rest of Emacs, we put it here. */
7719 struct input_event emacs_event;
7720 int tool_bar_p = 0;
7721 int button;
7722 int up;
7724 emacs_event.kind = no_event;
7726 if (dpyinfo->grabbed && last_mouse_frame
7727 && FRAME_LIVE_P (last_mouse_frame))
7728 f = last_mouse_frame;
7729 else
7730 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7732 if (f)
7734 construct_mouse_click (&emacs_event, &msg, f);
7736 /* Is this in the tool-bar? */
7737 if (WINDOWP (f->tool_bar_window)
7738 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
7740 Lisp_Object window;
7741 int p, x, y;
7743 /* Set x and y. */
7744 window = window_from_coordinates (f,
7745 emacs_event.x,
7746 emacs_event.y,
7747 &p, 1);
7748 if (EQ (window, f->tool_bar_window))
7750 w32_handle_tool_bar_click (f, &emacs_event);
7751 tool_bar_p = 1;
7755 if (!tool_bar_p)
7756 if (!dpyinfo->w32_focus_frame
7757 || f == dpyinfo->w32_focus_frame
7758 && (numchars >= 1))
7760 construct_mouse_click (bufp, &msg, f);
7761 bufp++;
7762 count++;
7763 numchars--;
7767 parse_button (msg.msg.message, &button, &up);
7769 if (up)
7771 dpyinfo->grabbed &= ~ (1 << button);
7773 else
7775 dpyinfo->grabbed |= (1 << button);
7776 last_mouse_frame = f;
7777 /* Ignore any mouse motion that happened
7778 before this event; any subsequent mouse-movement
7779 Emacs events should reflect only motion after
7780 the ButtonPress. */
7781 if (f != 0)
7782 f->mouse_moved = 0;
7784 if (!tool_bar_p)
7785 last_tool_bar_item = -1;
7787 break;
7790 case WM_MOUSEWHEEL:
7791 if (dpyinfo->grabbed && last_mouse_frame
7792 && FRAME_LIVE_P (last_mouse_frame))
7793 f = last_mouse_frame;
7794 else
7795 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7797 if (f)
7799 if ((!dpyinfo->w32_focus_frame
7800 || f == dpyinfo->w32_focus_frame)
7801 && (numchars >= 1))
7803 construct_mouse_wheel (bufp, &msg, f);
7804 bufp++;
7805 count++;
7806 numchars--;
7809 break;
7811 case WM_MENUSELECT:
7813 HMENU menu = (HMENU) msg.msg.lParam;
7814 UINT menu_item = (UINT) LOWORD (msg.msg.wParam);
7815 UINT flags = (UINT) HIWORD (msg.msg.wParam);
7817 w32_menu_display_help (menu, menu_item, flags);
7819 break;
7821 case WM_DROPFILES:
7822 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7824 if (f)
7826 construct_drag_n_drop (bufp, &msg, f);
7827 bufp++;
7828 count++;
7829 numchars--;
7831 break;
7833 case WM_VSCROLL:
7835 struct scroll_bar *bar =
7836 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
7838 if (bar && numchars >= 1)
7840 if (x_scroll_bar_handle_click (bar, &msg, bufp))
7842 bufp++;
7843 count++;
7844 numchars--;
7847 break;
7850 case WM_WINDOWPOSCHANGED:
7851 case WM_ACTIVATE:
7852 case WM_ACTIVATEAPP:
7853 check_visibility = 1;
7854 break;
7856 case WM_MOVE:
7857 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7859 if (f && !f->async_iconified)
7861 int x, y;
7863 x_real_positions (f, &x, &y);
7864 f->output_data.w32->left_pos = x;
7865 f->output_data.w32->top_pos = y;
7868 check_visibility = 1;
7869 break;
7871 case WM_SHOWWINDOW:
7872 /* If window has been obscured or exposed by another window
7873 being maximised or minimised/restored, then recheck
7874 visibility of all frames. Direct changes to our own
7875 windows get handled by WM_SIZE. */
7876 #if 0
7877 if (msg.msg.lParam != 0)
7878 check_visibility = 1;
7879 else
7881 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7882 f->async_visible = msg.msg.wParam;
7884 #endif
7886 check_visibility = 1;
7887 break;
7889 case WM_SIZE:
7890 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
7892 /* Inform lisp of whether frame has been iconified etc. */
7893 if (f)
7895 switch (msg.msg.wParam)
7897 case SIZE_MINIMIZED:
7898 f->async_visible = 0;
7899 f->async_iconified = 1;
7901 bufp->kind = iconify_event;
7902 XSETFRAME (bufp->frame_or_window, f);
7903 bufp->arg = Qnil;
7904 bufp++;
7905 count++;
7906 numchars--;
7907 break;
7909 case SIZE_MAXIMIZED:
7910 case SIZE_RESTORED:
7911 f->async_visible = 1;
7912 f->async_iconified = 0;
7914 /* wait_reading_process_input will notice this and update
7915 the frame's display structures. */
7916 SET_FRAME_GARBAGED (f);
7918 if (f->iconified)
7920 int x, y;
7922 /* Reset top and left positions of the Window
7923 here since Windows sends a WM_MOVE message
7924 BEFORE telling us the Window is minimized
7925 when the Window is iconified, with 3000,3000
7926 as the co-ords. */
7927 x_real_positions (f, &x, &y);
7928 f->output_data.w32->left_pos = x;
7929 f->output_data.w32->top_pos = y;
7931 bufp->kind = deiconify_event;
7932 XSETFRAME (bufp->frame_or_window, f);
7933 bufp->arg = Qnil;
7934 bufp++;
7935 count++;
7936 numchars--;
7938 else if (! NILP (Vframe_list)
7939 && ! NILP (XCDR (Vframe_list)))
7940 /* Force a redisplay sooner or later
7941 to update the frame titles
7942 in case this is the second frame. */
7943 record_asynch_buffer_change ();
7944 break;
7948 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
7950 RECT rect;
7951 int rows;
7952 int columns;
7953 int width;
7954 int height;
7956 GetClientRect(msg.msg.hwnd, &rect);
7958 height = rect.bottom - rect.top;
7959 width = rect.right - rect.left;
7961 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
7962 columns = PIXEL_TO_CHAR_WIDTH (f, width);
7964 /* TODO: Clip size to the screen dimensions. */
7966 /* Even if the number of character rows and columns has
7967 not changed, the font size may have changed, so we need
7968 to check the pixel dimensions as well. */
7970 if (columns != f->width
7971 || rows != f->height
7972 || width != f->output_data.w32->pixel_width
7973 || height != f->output_data.w32->pixel_height)
7975 change_frame_size (f, rows, columns, 0, 1, 0);
7976 SET_FRAME_GARBAGED (f);
7977 cancel_mouse_face (f);
7978 f->output_data.w32->pixel_width = width;
7979 f->output_data.w32->pixel_height = height;
7980 f->output_data.w32->win_gravity = NorthWestGravity;
7984 check_visibility = 1;
7985 break;
7987 case WM_SETFOCUS:
7988 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
7990 dpyinfo->w32_focus_event_frame = f;
7992 if (f)
7993 x_new_focus_frame (dpyinfo, f);
7996 dpyinfo->grabbed = 0;
7997 check_visibility = 1;
7998 break;
8000 case WM_KILLFOCUS:
8001 /* NTEMACS_TODO: some of this belongs in MOUSE_LEAVE */
8002 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
8004 if (f)
8006 Lisp_Object frame;
8008 if (f == dpyinfo->w32_focus_event_frame)
8009 dpyinfo->w32_focus_event_frame = 0;
8011 if (f == dpyinfo->w32_focus_frame)
8012 x_new_focus_frame (dpyinfo, 0);
8014 if (f == dpyinfo->mouse_face_mouse_frame)
8016 /* If we move outside the frame, then we're
8017 certainly no longer on any text in the frame. */
8018 clear_mouse_face (dpyinfo);
8019 dpyinfo->mouse_face_mouse_frame = 0;
8022 /* Generate a nil HELP_EVENT to cancel a help-echo.
8023 Do it only if there's something to cancel.
8024 Otherwise, the startup message is cleared when
8025 the mouse leaves the frame. */
8026 if (any_help_event_p)
8028 int n;
8030 XSETFRAME (frame, f);
8031 n = gen_help_event (bufp, numchars, Qnil, frame,
8032 Qnil, Qnil, 0);
8033 bufp += n, count += n, numchars -=n;
8037 dpyinfo->grabbed = 0;
8038 check_visibility = 1;
8039 break;
8041 case WM_CLOSE:
8042 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8044 if (f)
8046 if (numchars == 0)
8047 abort ();
8049 bufp->kind = delete_window_event;
8050 XSETFRAME (bufp->frame_or_window, f);
8051 bufp->arg = Qnil;
8052 bufp++;
8053 count++;
8054 numchars--;
8056 break;
8058 case WM_INITMENU:
8059 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8061 if (f)
8063 if (numchars == 0)
8064 abort ();
8066 bufp->kind = menu_bar_activate_event;
8067 XSETFRAME (bufp->frame_or_window, f);
8068 bufp->arg = Qnil;
8069 bufp++;
8070 count++;
8071 numchars--;
8073 break;
8075 case WM_COMMAND:
8076 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8078 if (f)
8080 extern void menubar_selection_callback
8081 (FRAME_PTR f, void * client_data);
8082 menubar_selection_callback (f, (void *)msg.msg.wParam);
8085 check_visibility = 1;
8086 break;
8088 case WM_DISPLAYCHANGE:
8089 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8091 if (f)
8093 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
8094 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
8095 dpyinfo->n_cbits = msg.msg.wParam;
8096 DebPrint (("display change: %d %d\n", dpyinfo->width,
8097 dpyinfo->height));
8100 check_visibility = 1;
8101 break;
8103 default:
8104 /* Check for messages registered at runtime. */
8105 if (msg.msg.message == msh_mousewheel)
8107 if (dpyinfo->grabbed && last_mouse_frame
8108 && FRAME_LIVE_P (last_mouse_frame))
8109 f = last_mouse_frame;
8110 else
8111 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8113 if (f)
8115 if ((!dpyinfo->w32_focus_frame
8116 || f == dpyinfo->w32_focus_frame)
8117 && (numchars >= 1))
8119 construct_mouse_wheel (bufp, &msg, f);
8120 bufp++;
8121 count++;
8122 numchars--;
8126 break;
8130 /* If the focus was just given to an autoraising frame,
8131 raise it now. */
8132 /* ??? This ought to be able to handle more than one such frame. */
8133 if (pending_autoraise_frame)
8135 x_raise_frame (pending_autoraise_frame);
8136 pending_autoraise_frame = 0;
8139 /* Check which frames are still visisble, if we have enqueued any user
8140 events or been notified of events that may affect visibility. We
8141 do this here because there doesn't seem to be any direct
8142 notification from Windows that the visibility of a window has
8143 changed (at least, not in all cases). */
8144 if (count > 0 || check_visibility)
8146 Lisp_Object tail, frame;
8148 FOR_EACH_FRAME (tail, frame)
8150 FRAME_PTR f = XFRAME (frame);
8151 /* Check "visible" frames and mark each as obscured or not.
8152 Note that async_visible is nonzero for unobscured and
8153 obscured frames, but zero for hidden and iconified frames. */
8154 if (FRAME_W32_P (f) && f->async_visible)
8156 RECT clipbox;
8157 HDC hdc = get_frame_dc (f);
8158 GetClipBox (hdc, &clipbox);
8159 release_frame_dc (f, hdc);
8161 if (clipbox.right == clipbox.left
8162 || clipbox.bottom == clipbox.top)
8164 /* Frame has become completely obscured so mark as
8165 such (we do this by setting async_visible to 2 so
8166 that FRAME_VISIBLE_P is still true, but redisplay
8167 will skip it). */
8168 f->async_visible = 2;
8170 if (!FRAME_OBSCURED_P (f))
8172 DebPrint (("frame %04x (%s) obscured\n", f,
8173 XSTRING (f->name)->data));
8176 else
8178 /* Frame is not obscured, so mark it as such. */
8179 f->async_visible = 1;
8181 if (FRAME_OBSCURED_P (f))
8183 SET_FRAME_GARBAGED (f);
8184 DebPrint (("frame %04x (%s) reexposed\n", f,
8185 XSTRING (f->name)->data));
8187 /* Force a redisplay sooner or later. */
8188 record_asynch_buffer_change ();
8195 UNBLOCK_INPUT;
8196 return count;
8202 /***********************************************************************
8203 Text Cursor
8204 ***********************************************************************/
8206 /* Note if the text cursor of window W has been overwritten by a
8207 drawing operation that outputs N glyphs starting at HPOS in the
8208 line given by output_cursor.vpos. N < 0 means all the rest of the
8209 line after HPOS has been written. */
8211 static void
8212 note_overwritten_text_cursor (w, hpos, n)
8213 struct window *w;
8214 int hpos, n;
8216 if (updated_area == TEXT_AREA
8217 && output_cursor.vpos == w->phys_cursor.vpos
8218 && hpos <= w->phys_cursor.hpos
8219 && (n < 0
8220 || hpos + n > w->phys_cursor.hpos))
8221 w->phys_cursor_on_p = 0;
8225 /* Set clipping for output in glyph row ROW. W is the window in which
8226 we operate. GC is the graphics context to set clipping in.
8227 WHOLE_LINE_P non-zero means include the areas used for truncation
8228 mark display and alike in the clipping rectangle.
8230 ROW may be a text row or, e.g., a mode line. Text rows must be
8231 clipped to the interior of the window dedicated to text display,
8232 mode lines must be clipped to the whole window. */
8234 static void
8235 w32_clip_to_row (w, row, hdc, whole_line_p)
8236 struct window *w;
8237 struct glyph_row *row;
8238 HDC hdc;
8239 int whole_line_p;
8241 struct frame *f = XFRAME (WINDOW_FRAME (w));
8242 RECT clip_rect;
8243 int window_x, window_y, window_width, window_height;
8245 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8247 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
8248 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8249 clip_rect.top = max (clip_rect.top, window_y);
8250 clip_rect.right = clip_rect.left + window_width;
8251 clip_rect.bottom = clip_rect.top + row->visible_height;
8253 /* If clipping to the whole line, including trunc marks, extend
8254 the rectangle to the left and increase its width. */
8255 if (whole_line_p)
8257 clip_rect.left -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
8258 clip_rect.right += FRAME_X_FLAGS_AREA_WIDTH (f);
8261 w32_set_clip_rectangle (hdc, &clip_rect);
8265 /* Draw a hollow box cursor on window W in glyph row ROW. */
8267 static void
8268 x_draw_hollow_cursor (w, row)
8269 struct window *w;
8270 struct glyph_row *row;
8272 struct frame *f = XFRAME (WINDOW_FRAME (w));
8273 HDC hdc = get_frame_dc (f);
8274 RECT rect;
8275 int wd;
8276 struct glyph *cursor_glyph;
8277 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
8279 /* Compute frame-relative coordinates from window-relative
8280 coordinates. */
8281 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8282 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
8283 + row->ascent - w->phys_cursor_ascent);
8284 rect.bottom = rect.top + row->height - 1;
8286 /* Get the glyph the cursor is on. If we can't tell because
8287 the current matrix is invalid or such, give up. */
8288 cursor_glyph = get_phys_cursor_glyph (w);
8289 if (cursor_glyph == NULL)
8290 return;
8292 /* Compute the width of the rectangle to draw. If on a stretch
8293 glyph, and `x-stretch-block-cursor' is nil, don't draw a
8294 rectangle as wide as the glyph, but use a canonical character
8295 width instead. */
8296 wd = cursor_glyph->pixel_width - 1;
8297 if (cursor_glyph->type == STRETCH_GLYPH
8298 && !x_stretch_cursor_p)
8299 wd = min (CANON_X_UNIT (f), wd);
8301 rect.right = rect.left + wd;
8303 FrameRect (hdc, &rect, hb);
8304 DeleteObject (hb);
8306 release_frame_dc (f, hdc);
8310 /* Draw a bar cursor on window W in glyph row ROW.
8312 Implementation note: One would like to draw a bar cursor with an
8313 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8314 Unfortunately, I didn't find a font yet that has this property set.
8315 --gerd. */
8317 static void
8318 x_draw_bar_cursor (w, row, width)
8319 struct window *w;
8320 struct glyph_row *row;
8321 int width;
8323 /* If cursor hpos is out of bounds, don't draw garbage. This can
8324 happen in mini-buffer windows when switching between echo area
8325 glyphs and mini-buffer. */
8326 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
8328 struct frame *f = XFRAME (w->frame);
8329 struct glyph *cursor_glyph;
8330 int x;
8331 HDC hdc;
8333 cursor_glyph = get_phys_cursor_glyph (w);
8334 if (cursor_glyph == NULL)
8335 return;
8337 /* If on an image, draw like a normal cursor. That's usually better
8338 visible than drawing a bar, esp. if the image is large so that
8339 the bar might not be in the window. */
8340 if (cursor_glyph->type == IMAGE_GLYPH)
8342 struct glyph_row *row;
8343 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8344 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
8346 else
8349 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8351 if (width < 0)
8352 width = f->output_data.w32->cursor_width;
8354 hdc = get_frame_dc (f);
8355 w32_fill_area (f, hdc, f->output_data.w32->cursor_pixel,
8357 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8358 min (cursor_glyph->pixel_width, width),
8359 row->height);
8360 release_frame_dc (f, hdc);
8366 /* Clear the cursor of window W to background color, and mark the
8367 cursor as not shown. This is used when the text where the cursor
8368 is is about to be rewritten. */
8370 static void
8371 x_clear_cursor (w)
8372 struct window *w;
8374 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
8375 x_update_window_cursor (w, 0);
8379 /* Draw the cursor glyph of window W in glyph row ROW. See the
8380 comment of x_draw_glyphs for the meaning of HL. */
8382 static void
8383 x_draw_phys_cursor_glyph (w, row, hl)
8384 struct window *w;
8385 struct glyph_row *row;
8386 enum draw_glyphs_face hl;
8388 /* If cursor hpos is out of bounds, don't draw garbage. This can
8389 happen in mini-buffer windows when switching between echo area
8390 glyphs and mini-buffer. */
8391 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
8393 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
8394 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
8395 hl, 0, 0, 0);
8397 /* When we erase the cursor, and ROW is overlapped by other
8398 rows, make sure that these overlapping parts of other rows
8399 are redrawn. */
8400 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
8402 if (row > w->current_matrix->rows
8403 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
8404 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
8406 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
8407 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
8408 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
8414 /* Erase the image of a cursor of window W from the screen. */
8416 static void
8417 x_erase_phys_cursor (w)
8418 struct window *w;
8420 struct frame *f = XFRAME (w->frame);
8421 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8422 int hpos = w->phys_cursor.hpos;
8423 int vpos = w->phys_cursor.vpos;
8424 int mouse_face_here_p = 0;
8425 struct glyph_matrix *active_glyphs = w->current_matrix;
8426 struct glyph_row *cursor_row;
8427 struct glyph *cursor_glyph;
8428 enum draw_glyphs_face hl;
8430 /* No cursor displayed or row invalidated => nothing to do on the
8431 screen. */
8432 if (w->phys_cursor_type == NO_CURSOR)
8433 goto mark_cursor_off;
8435 /* VPOS >= active_glyphs->nrows means that window has been resized.
8436 Don't bother to erase the cursor. */
8437 if (vpos >= active_glyphs->nrows)
8438 goto mark_cursor_off;
8440 /* If row containing cursor is marked invalid, there is nothing we
8441 can do. */
8442 cursor_row = MATRIX_ROW (active_glyphs, vpos);
8443 if (!cursor_row->enabled_p)
8444 goto mark_cursor_off;
8446 /* This can happen when the new row is shorter than the old one.
8447 In this case, either x_draw_glyphs or clear_end_of_line
8448 should have cleared the cursor. Note that we wouldn't be
8449 able to erase the cursor in this case because we don't have a
8450 cursor glyph at hand. */
8451 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
8452 goto mark_cursor_off;
8454 /* If the cursor is in the mouse face area, redisplay that when
8455 we clear the cursor. */
8456 if (w == XWINDOW (dpyinfo->mouse_face_window)
8457 && (vpos > dpyinfo->mouse_face_beg_row
8458 || (vpos == dpyinfo->mouse_face_beg_row
8459 && hpos >= dpyinfo->mouse_face_beg_col))
8460 && (vpos < dpyinfo->mouse_face_end_row
8461 || (vpos == dpyinfo->mouse_face_end_row
8462 && hpos < dpyinfo->mouse_face_end_col))
8463 /* Don't redraw the cursor's spot in mouse face if it is at the
8464 end of a line (on a newline). The cursor appears there, but
8465 mouse highlighting does not. */
8466 && cursor_row->used[TEXT_AREA] > hpos)
8467 mouse_face_here_p = 1;
8469 /* Maybe clear the display under the cursor. */
8470 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
8472 int x;
8473 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8474 HDC hdc;
8476 cursor_glyph = get_phys_cursor_glyph (w);
8477 if (cursor_glyph == NULL)
8478 goto mark_cursor_off;
8480 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8482 hdc = get_frame_dc (f);
8483 w32_clear_area (f, hdc, x,
8484 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
8485 cursor_row->y)),
8486 cursor_glyph->pixel_width,
8487 cursor_row->visible_height);
8488 release_frame_dc (f, hdc);
8491 /* Erase the cursor by redrawing the character underneath it. */
8492 if (mouse_face_here_p)
8493 hl = DRAW_MOUSE_FACE;
8494 else if (cursor_row->inverse_p)
8495 hl = DRAW_INVERSE_VIDEO;
8496 else
8497 hl = DRAW_NORMAL_TEXT;
8498 x_draw_phys_cursor_glyph (w, cursor_row, hl);
8500 mark_cursor_off:
8501 w->phys_cursor_on_p = 0;
8502 w->phys_cursor_type = NO_CURSOR;
8506 /* Display or clear cursor of window W. If ON is zero, clear the
8507 cursor. If it is non-zero, display the cursor. If ON is nonzero,
8508 where to put the cursor is specified by HPOS, VPOS, X and Y. */
8510 void
8511 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
8512 struct window *w;
8513 int on, hpos, vpos, x, y;
8515 struct frame *f = XFRAME (w->frame);
8516 int new_cursor_type;
8517 int new_cursor_width;
8518 struct glyph_matrix *current_glyphs;
8519 struct glyph_row *glyph_row;
8520 struct glyph *glyph;
8522 /* This is pointless on invisible frames, and dangerous on garbaged
8523 windows and frames; in the latter case, the frame or window may
8524 be in the midst of changing its size, and x and y may be off the
8525 window. */
8526 if (! FRAME_VISIBLE_P (f)
8527 || FRAME_GARBAGED_P (f)
8528 || vpos >= w->current_matrix->nrows
8529 || hpos >= w->current_matrix->matrix_w)
8530 return;
8532 /* If cursor is off and we want it off, return quickly. */
8533 if (!on && !w->phys_cursor_on_p)
8534 return;
8536 current_glyphs = w->current_matrix;
8537 glyph_row = MATRIX_ROW (current_glyphs, vpos);
8538 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
8540 /* If cursor row is not enabled, we don't really know where to
8541 display the cursor. */
8542 if (!glyph_row->enabled_p)
8544 w->phys_cursor_on_p = 0;
8545 return;
8548 xassert (interrupt_input_blocked);
8550 /* Set new_cursor_type to the cursor we want to be displayed. In a
8551 mini-buffer window, we want the cursor only to appear if we are
8552 reading input from this window. For the selected window, we want
8553 the cursor type given by the frame parameter. If explicitly
8554 marked off, draw no cursor. In all other cases, we want a hollow
8555 box cursor. */
8556 new_cursor_width = -1;
8557 if (cursor_in_echo_area
8558 && FRAME_HAS_MINIBUF_P (f)
8559 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
8561 if (w == XWINDOW (echo_area_window))
8562 new_cursor_type = FRAME_DESIRED_CURSOR (f);
8563 else
8564 new_cursor_type = HOLLOW_BOX_CURSOR;
8566 else
8568 if (w != XWINDOW (selected_window)
8569 || f != FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame)
8571 extern int cursor_in_non_selected_windows;
8573 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
8574 new_cursor_type = NO_CURSOR;
8575 else
8576 new_cursor_type = HOLLOW_BOX_CURSOR;
8578 else if (w->cursor_off_p)
8579 new_cursor_type = NO_CURSOR;
8580 else
8582 struct buffer *b = XBUFFER (w->buffer);
8584 if (EQ (b->cursor_type, Qt))
8585 new_cursor_type = FRAME_DESIRED_CURSOR (f);
8586 else
8587 new_cursor_type = x_specified_cursor_type (b->cursor_type,
8588 &new_cursor_width);
8592 /* If cursor is currently being shown and we don't want it to be or
8593 it is in the wrong place, or the cursor type is not what we want,
8594 erase it. */
8595 if (w->phys_cursor_on_p
8596 && (!on
8597 || w->phys_cursor.x != x
8598 || w->phys_cursor.y != y
8599 || new_cursor_type != w->phys_cursor_type))
8600 x_erase_phys_cursor (w);
8602 /* If the cursor is now invisible and we want it to be visible,
8603 display it. */
8604 if (on && !w->phys_cursor_on_p)
8606 w->phys_cursor_ascent = glyph_row->ascent;
8607 w->phys_cursor_height = glyph_row->height;
8609 /* Set phys_cursor_.* before x_draw_.* is called because some
8610 of them may need the information. */
8611 w->phys_cursor.x = x;
8612 w->phys_cursor.y = glyph_row->y;
8613 w->phys_cursor.hpos = hpos;
8614 w->phys_cursor.vpos = vpos;
8615 w->phys_cursor_type = new_cursor_type;
8616 w->phys_cursor_on_p = 1;
8618 switch (new_cursor_type)
8620 case HOLLOW_BOX_CURSOR:
8621 x_draw_hollow_cursor (w, glyph_row);
8622 break;
8624 case FILLED_BOX_CURSOR:
8625 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8626 break;
8628 case BAR_CURSOR:
8629 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
8630 break;
8632 case NO_CURSOR:
8633 break;
8635 default:
8636 abort ();
8642 /* Display the cursor on window W, or clear it. X and Y are window
8643 relative pixel coordinates. HPOS and VPOS are glyph matrix
8644 positions. If W is not the selected window, display a hollow
8645 cursor. ON non-zero means display the cursor at X, Y which
8646 correspond to HPOS, VPOS, otherwise it is cleared. */
8648 void
8649 x_display_cursor (w, on, hpos, vpos, x, y)
8650 struct window *w;
8651 int on, hpos, vpos, x, y;
8653 BLOCK_INPUT;
8654 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
8655 UNBLOCK_INPUT;
8659 /* Display the cursor on window W, or clear it, according to ON_P.
8660 Don't change the cursor's position. */
8662 void
8663 x_update_cursor (f, on_p)
8664 struct frame *f;
8665 int on_p;
8667 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
8671 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
8672 in the window tree rooted at W. */
8674 static void
8675 x_update_cursor_in_window_tree (w, on_p)
8676 struct window *w;
8677 int on_p;
8679 while (w)
8681 if (!NILP (w->hchild))
8682 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
8683 else if (!NILP (w->vchild))
8684 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
8685 else
8686 x_update_window_cursor (w, on_p);
8688 w = NILP (w->next) ? 0 : XWINDOW (w->next);
8693 /* Switch the display of W's cursor on or off, according to the value
8694 of ON. */
8696 static void
8697 x_update_window_cursor (w, on)
8698 struct window *w;
8699 int on;
8701 /* Don't update cursor in windows whose frame is in the process
8702 of being deleted. */
8703 if (w->current_matrix)
8705 BLOCK_INPUT;
8706 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
8707 w->phys_cursor.vpos, w->phys_cursor.x,
8708 w->phys_cursor.y);
8709 UNBLOCK_INPUT;
8716 /* Icons. */
8719 x_bitmap_icon (f, icon)
8720 struct frame *f;
8721 Lisp_Object icon;
8723 int mask, bitmap_id;
8724 Window icon_window;
8725 HANDLE hicon;
8727 if (FRAME_W32_WINDOW (f) == 0)
8728 return 1;
8730 if (NILP (icon))
8731 hicon = LoadIcon (hinst, EMACS_CLASS);
8732 else if (STRINGP (icon))
8733 hicon = LoadImage (NULL, (LPCTSTR) XSTRING (icon)->data, IMAGE_ICON, 0, 0,
8734 LR_DEFAULTSIZE | LR_LOADFROMFILE);
8735 else if (SYMBOLP (icon))
8737 LPCTSTR name;
8739 if (EQ (icon, intern ("application")))
8740 name = (LPCTSTR) IDI_APPLICATION;
8741 else if (EQ (icon, intern ("hand")))
8742 name = (LPCTSTR) IDI_HAND;
8743 else if (EQ (icon, intern ("question")))
8744 name = (LPCTSTR) IDI_QUESTION;
8745 else if (EQ (icon, intern ("exclamation")))
8746 name = (LPCTSTR) IDI_EXCLAMATION;
8747 else if (EQ (icon, intern ("asterisk")))
8748 name = (LPCTSTR) IDI_ASTERISK;
8749 else if (EQ (icon, intern ("winlogo")))
8750 name = (LPCTSTR) IDI_WINLOGO;
8751 else
8752 return 1;
8754 hicon = LoadIcon (NULL, name);
8756 else
8757 return 1;
8759 if (hicon == NULL)
8760 return 1;
8762 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
8763 (LPARAM) hicon);
8765 return 0;
8769 /* Changing the font of the frame. */
8771 /* Give frame F the font named FONTNAME as its default font, and
8772 return the full name of that font. FONTNAME may be a wildcard
8773 pattern; in that case, we choose some font that fits the pattern.
8774 The return value shows which font we chose. */
8776 Lisp_Object
8777 x_new_font (f, fontname)
8778 struct frame *f;
8779 register char *fontname;
8781 struct font_info *fontp
8782 = FS_LOAD_FONT (f, 0, fontname, -1);
8784 if (!fontp)
8785 return Qnil;
8787 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8788 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8789 FRAME_FONTSET (f) = -1;
8791 /* Compute the scroll bar width in character columns. */
8792 if (f->scroll_bar_pixel_width > 0)
8794 int wid = FONT_WIDTH (FRAME_FONT (f));
8795 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
8797 else
8799 int wid = FONT_WIDTH (FRAME_FONT (f));
8800 f->scroll_bar_cols = (14 + wid - 1) / wid;
8803 /* Now make the frame display the given font. */
8804 if (FRAME_W32_WINDOW (f) != 0)
8806 frame_update_line_height (f);
8807 x_set_window_size (f, 0, f->width, f->height);
8809 else
8810 /* If we are setting a new frame's font for the first time,
8811 there are no faces yet, so this font's height is the line height. */
8812 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
8814 return build_string (fontp->full_name);
8817 /* Give frame F the fontset named FONTSETNAME as its default font, and
8818 return the full name of that fontset. FONTSETNAME may be a wildcard
8819 pattern; in that case, we choose some fontset that fits the pattern.
8820 The return value shows which fontset we chose. */
8822 Lisp_Object
8823 x_new_fontset (f, fontsetname)
8824 struct frame *f;
8825 char *fontsetname;
8827 int fontset = fs_query_fontset (build_string (fontsetname), 0);
8828 Lisp_Object result;
8829 char *fontname;
8831 if (fontset < 0)
8832 return Qnil;
8834 if (FRAME_FONTSET (f) == fontset)
8835 /* This fontset is already set in frame F. There's nothing more
8836 to do. */
8837 return fontset_name (fontset);
8839 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
8841 if (!STRINGP (result))
8842 /* Can't load ASCII font. */
8843 return Qnil;
8845 /* Since x_new_font doesn't update any fontset information, do it now. */
8846 FRAME_FONTSET(f) = fontset;
8848 return build_string (fontsetname);
8852 #if GLYPH_DEBUG
8854 /* Check that FONT is valid on frame F. It is if it can be found in F's
8855 font table. */
8857 static void
8858 x_check_font (f, font)
8859 struct frame *f;
8860 XFontStruct *font;
8862 int i;
8863 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8865 xassert (font != NULL);
8867 for (i = 0; i < dpyinfo->n_fonts; i++)
8868 if (dpyinfo->font_table[i].name
8869 && font == dpyinfo->font_table[i].font)
8870 break;
8872 xassert (i < dpyinfo->n_fonts);
8875 #endif /* GLYPH_DEBUG != 0 */
8877 /* Set *W to the minimum width, *H to the minimum font height of FONT.
8878 Note: There are (broken) X fonts out there with invalid XFontStruct
8879 min_bounds contents. For example, handa@etl.go.jp reports that
8880 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
8881 have font->min_bounds.width == 0. */
8883 static INLINE void
8884 x_font_min_bounds (font, w, h)
8885 XFontStruct *font;
8886 int *w, *h;
8889 * NTEMACS_TODO: Windows does not appear to offer min bound, only
8890 * average and maximum width, and maximum height.
8892 *h = FONT_HEIGHT (font);
8893 *w = FONT_WIDTH (font);
8897 /* Compute the smallest character width and smallest font height over
8898 all fonts available on frame F. Set the members smallest_char_width
8899 and smallest_font_height in F's x_display_info structure to
8900 the values computed. Value is non-zero if smallest_font_height or
8901 smallest_char_width become smaller than they were before. */
8904 x_compute_min_glyph_bounds (f)
8905 struct frame *f;
8907 int i;
8908 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8909 XFontStruct *font;
8910 int old_width = dpyinfo->smallest_char_width;
8911 int old_height = dpyinfo->smallest_font_height;
8913 dpyinfo->smallest_font_height = 100000;
8914 dpyinfo->smallest_char_width = 100000;
8916 for (i = 0; i < dpyinfo->n_fonts; ++i)
8917 if (dpyinfo->font_table[i].name)
8919 struct font_info *fontp = dpyinfo->font_table + i;
8920 int w, h;
8922 font = (XFontStruct *) fontp->font;
8923 xassert (font != (XFontStruct *) ~0);
8924 x_font_min_bounds (font, &w, &h);
8926 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
8927 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
8930 xassert (dpyinfo->smallest_char_width > 0
8931 && dpyinfo->smallest_font_height > 0);
8933 return (dpyinfo->n_fonts == 1
8934 || dpyinfo->smallest_char_width < old_width
8935 || dpyinfo->smallest_font_height < old_height);
8939 /* Calculate the absolute position in frame F
8940 from its current recorded position values and gravity. */
8942 void
8943 x_calc_absolute_position (f)
8944 struct frame *f;
8946 Window child;
8947 POINT pt;
8948 int flags = f->output_data.w32->size_hint_flags;
8950 pt.x = pt.y = 0;
8952 /* Find the position of the outside upper-left corner of
8953 the inner window, with respect to the outer window. */
8954 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
8956 BLOCK_INPUT;
8957 MapWindowPoints (FRAME_W32_WINDOW (f),
8958 f->output_data.w32->parent_desc,
8959 &pt, 1);
8960 UNBLOCK_INPUT;
8964 RECT rt;
8965 rt.left = rt.right = rt.top = rt.bottom = 0;
8967 BLOCK_INPUT;
8968 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
8969 FRAME_EXTERNAL_MENU_BAR (f));
8970 UNBLOCK_INPUT;
8972 pt.x += (rt.right - rt.left);
8973 pt.y += (rt.bottom - rt.top);
8976 /* Treat negative positions as relative to the leftmost bottommost
8977 position that fits on the screen. */
8978 if (flags & XNegative)
8979 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
8980 - 2 * f->output_data.w32->border_width - pt.x
8981 - PIXEL_WIDTH (f)
8982 + f->output_data.w32->left_pos);
8984 if (flags & YNegative)
8985 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
8986 - 2 * f->output_data.w32->border_width - pt.y
8987 - PIXEL_HEIGHT (f)
8988 + f->output_data.w32->top_pos);
8989 /* The left_pos and top_pos
8990 are now relative to the top and left screen edges,
8991 so the flags should correspond. */
8992 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
8995 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8996 to really change the position, and 0 when calling from
8997 x_make_frame_visible (in that case, XOFF and YOFF are the current
8998 position values). It is -1 when calling from x_set_frame_parameters,
8999 which means, do adjust for borders but don't change the gravity. */
9001 void
9002 x_set_offset (f, xoff, yoff, change_gravity)
9003 struct frame *f;
9004 register int xoff, yoff;
9005 int change_gravity;
9007 int modified_top, modified_left;
9009 if (change_gravity > 0)
9011 f->output_data.w32->top_pos = yoff;
9012 f->output_data.w32->left_pos = xoff;
9013 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
9014 if (xoff < 0)
9015 f->output_data.w32->size_hint_flags |= XNegative;
9016 if (yoff < 0)
9017 f->output_data.w32->size_hint_flags |= YNegative;
9018 f->output_data.w32->win_gravity = NorthWestGravity;
9020 x_calc_absolute_position (f);
9022 BLOCK_INPUT;
9023 x_wm_set_size_hint (f, (long) 0, 0);
9025 modified_left = f->output_data.w32->left_pos;
9026 modified_top = f->output_data.w32->top_pos;
9028 my_set_window_pos (FRAME_W32_WINDOW (f),
9029 NULL,
9030 modified_left, modified_top,
9031 0, 0,
9032 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
9033 UNBLOCK_INPUT;
9036 /* Call this to change the size of frame F's x-window.
9037 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9038 for this size change and subsequent size changes.
9039 Otherwise we leave the window gravity unchanged. */
9040 void
9041 x_set_window_size (f, change_gravity, cols, rows)
9042 struct frame *f;
9043 int change_gravity;
9044 int cols, rows;
9046 int pixelwidth, pixelheight;
9048 BLOCK_INPUT;
9050 check_frame_size (f, &rows, &cols);
9051 f->output_data.w32->vertical_scroll_bar_extra
9052 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9054 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
9055 f->output_data.w32->flags_areas_extra
9056 = FRAME_FLAGS_AREA_WIDTH (f);
9057 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
9058 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
9060 f->output_data.w32->win_gravity = NorthWestGravity;
9061 x_wm_set_size_hint (f, (long) 0, 0);
9064 RECT rect;
9066 rect.left = rect.top = 0;
9067 rect.right = pixelwidth;
9068 rect.bottom = pixelheight;
9070 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
9071 FRAME_EXTERNAL_MENU_BAR (f));
9073 my_set_window_pos (FRAME_W32_WINDOW (f),
9074 NULL,
9075 0, 0,
9076 rect.right - rect.left,
9077 rect.bottom - rect.top,
9078 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
9081 /* Now, strictly speaking, we can't be sure that this is accurate,
9082 but the window manager will get around to dealing with the size
9083 change request eventually, and we'll hear how it went when the
9084 ConfigureNotify event gets here.
9086 We could just not bother storing any of this information here,
9087 and let the ConfigureNotify event set everything up, but that
9088 might be kind of confusing to the Lisp code, since size changes
9089 wouldn't be reported in the frame parameters until some random
9090 point in the future when the ConfigureNotify event arrives.
9092 We pass 1 for DELAY since we can't run Lisp code inside of
9093 a BLOCK_INPUT. */
9094 change_frame_size (f, rows, cols, 0, 1, 0);
9095 PIXEL_WIDTH (f) = pixelwidth;
9096 PIXEL_HEIGHT (f) = pixelheight;
9098 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9099 receive in the ConfigureNotify event; if we get what we asked
9100 for, then the event won't cause the screen to become garbaged, so
9101 we have to make sure to do it here. */
9102 SET_FRAME_GARBAGED (f);
9104 /* If cursor was outside the new size, mark it as off. */
9105 mark_window_cursors_off (XWINDOW (f->root_window));
9107 /* Clear out any recollection of where the mouse highlighting was,
9108 since it might be in a place that's outside the new frame size.
9109 Actually checking whether it is outside is a pain in the neck,
9110 so don't try--just let the highlighting be done afresh with new size. */
9111 cancel_mouse_face (f);
9113 UNBLOCK_INPUT;
9116 /* Mouse warping. */
9118 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
9120 void
9121 x_set_mouse_position (f, x, y)
9122 struct frame *f;
9123 int x, y;
9125 int pix_x, pix_y;
9127 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
9128 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
9130 if (pix_x < 0) pix_x = 0;
9131 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
9133 if (pix_y < 0) pix_y = 0;
9134 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
9136 x_set_mouse_pixel_position (f, pix_x, pix_y);
9139 void
9140 x_set_mouse_pixel_position (f, pix_x, pix_y)
9141 struct frame *f;
9142 int pix_x, pix_y;
9144 RECT rect;
9145 POINT pt;
9147 BLOCK_INPUT;
9149 GetClientRect (FRAME_W32_WINDOW (f), &rect);
9150 pt.x = rect.left + pix_x;
9151 pt.y = rect.top + pix_y;
9152 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
9154 SetCursorPos (pt.x, pt.y);
9156 UNBLOCK_INPUT;
9160 /* focus shifting, raising and lowering. */
9162 void
9163 x_focus_on_frame (f)
9164 struct frame *f;
9166 struct w32_display_info *dpyinfo = &one_w32_display_info;
9168 /* Give input focus to frame. */
9169 BLOCK_INPUT;
9170 #if 0
9171 /* Try not to change its Z-order if possible. */
9172 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
9173 my_set_focus (f, FRAME_W32_WINDOW (f));
9174 else
9175 #endif
9176 my_set_foreground_window (FRAME_W32_WINDOW (f));
9177 UNBLOCK_INPUT;
9180 void
9181 x_unfocus_frame (f)
9182 struct frame *f;
9186 /* Raise frame F. */
9187 void
9188 x_raise_frame (f)
9189 struct frame *f;
9191 BLOCK_INPUT;
9193 /* Strictly speaking, raise-frame should only change the frame's Z
9194 order, leaving input focus unchanged. This is reasonable behaviour
9195 on X where the usual policy is point-to-focus. However, this
9196 behaviour would be very odd on Windows where the usual policy is
9197 click-to-focus.
9199 On X, if the mouse happens to be over the raised frame, it gets
9200 input focus anyway (so the window with focus will never be
9201 completely obscured) - if not, then just moving the mouse over it
9202 is sufficient to give it focus. On Windows, the user must actually
9203 click on the frame (preferrably the title bar so as not to move
9204 point), which is more awkward. Also, no other Windows program
9205 raises a window to the top but leaves another window (possibly now
9206 completely obscured) with input focus.
9208 Because there is a system setting on Windows that allows the user
9209 to choose the point to focus policy, we make the strict semantics
9210 optional, but by default we grab focus when raising. */
9212 if (NILP (Vw32_grab_focus_on_raise))
9214 /* The obvious call to my_set_window_pos doesn't work if Emacs is
9215 not already the foreground application: the frame is raised
9216 above all other frames belonging to us, but not above the
9217 current top window. To achieve that, we have to resort to this
9218 more cumbersome method. */
9220 HDWP handle = BeginDeferWindowPos (2);
9221 if (handle)
9223 DeferWindowPos (handle,
9224 FRAME_W32_WINDOW (f),
9225 HWND_TOP,
9226 0, 0, 0, 0,
9227 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9229 DeferWindowPos (handle,
9230 GetForegroundWindow (),
9231 FRAME_W32_WINDOW (f),
9232 0, 0, 0, 0,
9233 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9235 EndDeferWindowPos (handle);
9238 else
9240 my_set_foreground_window (FRAME_W32_WINDOW (f));
9243 UNBLOCK_INPUT;
9246 /* Lower frame F. */
9247 void
9248 x_lower_frame (f)
9249 struct frame *f;
9251 BLOCK_INPUT;
9252 my_set_window_pos (FRAME_W32_WINDOW (f),
9253 HWND_BOTTOM,
9254 0, 0, 0, 0,
9255 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9256 UNBLOCK_INPUT;
9259 static void
9260 w32_frame_raise_lower (f, raise_flag)
9261 FRAME_PTR f;
9262 int raise_flag;
9264 if (raise_flag)
9265 x_raise_frame (f);
9266 else
9267 x_lower_frame (f);
9270 /* Change of visibility. */
9272 /* This tries to wait until the frame is really visible.
9273 However, if the window manager asks the user where to position
9274 the frame, this will return before the user finishes doing that.
9275 The frame will not actually be visible at that time,
9276 but it will become visible later when the window manager
9277 finishes with it. */
9279 void
9280 x_make_frame_visible (f)
9281 struct frame *f;
9283 Lisp_Object type;
9285 BLOCK_INPUT;
9287 type = x_icon_type (f);
9288 if (!NILP (type))
9289 x_bitmap_icon (f, type);
9291 if (! FRAME_VISIBLE_P (f))
9293 /* We test FRAME_GARBAGED_P here to make sure we don't
9294 call x_set_offset a second time
9295 if we get to x_make_frame_visible a second time
9296 before the window gets really visible. */
9297 if (! FRAME_ICONIFIED_P (f)
9298 && ! f->output_data.w32->asked_for_visible)
9299 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
9301 f->output_data.w32->asked_for_visible = 1;
9303 // my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW);
9304 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
9307 /* Synchronize to ensure Emacs knows the frame is visible
9308 before we do anything else. We do this loop with input not blocked
9309 so that incoming events are handled. */
9311 Lisp_Object frame;
9312 int count;
9314 /* This must come after we set COUNT. */
9315 UNBLOCK_INPUT;
9317 XSETFRAME (frame, f);
9319 /* Wait until the frame is visible. Process X events until a
9320 MapNotify event has been seen, or until we think we won't get a
9321 MapNotify at all.. */
9322 for (count = input_signal_count + 10;
9323 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9325 /* Force processing of queued events. */
9326 /* NTEMACS_TODO: x_sync equivalent? */
9328 /* Machines that do polling rather than SIGIO have been observed
9329 to go into a busy-wait here. So we'll fake an alarm signal
9330 to let the handler know that there's something to be read.
9331 We used to raise a real alarm, but it seems that the handler
9332 isn't always enabled here. This is probably a bug. */
9333 if (input_polling_used ())
9335 /* It could be confusing if a real alarm arrives while processing
9336 the fake one. Turn it off and let the handler reset it. */
9337 int old_poll_suppress_count = poll_suppress_count;
9338 poll_suppress_count = 1;
9339 poll_for_input_1 ();
9340 poll_suppress_count = old_poll_suppress_count;
9343 FRAME_SAMPLE_VISIBILITY (f);
9347 /* Change from mapped state to withdrawn state. */
9349 /* Make the frame visible (mapped and not iconified). */
9351 x_make_frame_invisible (f)
9352 struct frame *f;
9354 /* Don't keep the highlight on an invisible frame. */
9355 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9356 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
9358 BLOCK_INPUT;
9360 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
9362 /* We can't distinguish this from iconification
9363 just by the event that we get from the server.
9364 So we can't win using the usual strategy of letting
9365 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9366 and synchronize with the server to make sure we agree. */
9367 f->visible = 0;
9368 FRAME_ICONIFIED_P (f) = 0;
9369 f->async_visible = 0;
9370 f->async_iconified = 0;
9372 UNBLOCK_INPUT;
9375 /* Change window state from mapped to iconified. */
9377 void
9378 x_iconify_frame (f)
9379 struct frame *f;
9381 int result;
9382 Lisp_Object type;
9384 /* Don't keep the highlight on an invisible frame. */
9385 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9386 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
9388 if (f->async_iconified)
9389 return;
9391 BLOCK_INPUT;
9393 type = x_icon_type (f);
9394 if (!NILP (type))
9395 x_bitmap_icon (f, type);
9397 /* Simulate the user minimizing the frame. */
9398 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
9400 UNBLOCK_INPUT;
9403 /* Destroy the window of frame F. */
9405 x_destroy_window (f)
9406 struct frame *f;
9408 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9410 BLOCK_INPUT;
9412 my_destroy_window (f, FRAME_W32_WINDOW (f));
9413 free_frame_menubar (f);
9414 free_frame_faces (f);
9416 xfree (f->output_data.w32);
9417 f->output_data.w32 = 0;
9418 if (f == dpyinfo->w32_focus_frame)
9419 dpyinfo->w32_focus_frame = 0;
9420 if (f == dpyinfo->w32_focus_event_frame)
9421 dpyinfo->w32_focus_event_frame = 0;
9422 if (f == dpyinfo->w32_highlight_frame)
9423 dpyinfo->w32_highlight_frame = 0;
9425 dpyinfo->reference_count--;
9427 if (f == dpyinfo->mouse_face_mouse_frame)
9429 dpyinfo->mouse_face_beg_row
9430 = dpyinfo->mouse_face_beg_col = -1;
9431 dpyinfo->mouse_face_end_row
9432 = dpyinfo->mouse_face_end_col = -1;
9433 dpyinfo->mouse_face_window = Qnil;
9436 UNBLOCK_INPUT;
9439 /* Setting window manager hints. */
9441 /* Set the normal size hints for the window manager, for frame F.
9442 FLAGS is the flags word to use--or 0 meaning preserve the flags
9443 that the window now has.
9444 If USER_POSITION is nonzero, we set the USPosition
9445 flag (this is useful when FLAGS is 0). */
9446 void
9447 x_wm_set_size_hint (f, flags, user_position)
9448 struct frame *f;
9449 long flags;
9450 int user_position;
9452 Window window = FRAME_W32_WINDOW (f);
9454 enter_crit ();
9456 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
9457 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
9458 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
9459 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
9461 leave_crit ();
9464 /* Window manager things */
9465 x_wm_set_icon_position (f, icon_x, icon_y)
9466 struct frame *f;
9467 int icon_x, icon_y;
9469 #if 0
9470 Window window = FRAME_W32_WINDOW (f);
9472 f->display.x->wm_hints.flags |= IconPositionHint;
9473 f->display.x->wm_hints.icon_x = icon_x;
9474 f->display.x->wm_hints.icon_y = icon_y;
9476 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
9477 #endif
9482 /***********************************************************************
9483 Initialization
9484 ***********************************************************************/
9486 static int w32_initialized = 0;
9488 void
9489 w32_initialize_display_info (display_name)
9490 Lisp_Object display_name;
9492 struct w32_display_info *dpyinfo = &one_w32_display_info;
9494 bzero (dpyinfo, sizeof (*dpyinfo));
9496 /* Put it on w32_display_name_list. */
9497 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
9498 w32_display_name_list);
9499 dpyinfo->name_list_element = XCAR (w32_display_name_list);
9501 dpyinfo->w32_id_name
9502 = (char *) xmalloc (XSTRING (Vinvocation_name)->size
9503 + XSTRING (Vsystem_name)->size
9504 + 2);
9505 sprintf (dpyinfo->w32_id_name, "%s@%s",
9506 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
9508 /* Default Console mode values - overridden when running in GUI mode
9509 with values obtained from system metrics. */
9510 dpyinfo->resx = 1;
9511 dpyinfo->resy = 1;
9512 dpyinfo->height_in = 1;
9513 dpyinfo->width_in = 1;
9514 dpyinfo->n_planes = 1;
9515 dpyinfo->n_cbits = 4;
9516 dpyinfo->n_fonts = 0;
9517 dpyinfo->smallest_font_height = 1;
9518 dpyinfo->smallest_char_width = 1;
9520 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
9521 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
9522 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
9523 dpyinfo->mouse_face_window = Qnil;
9525 /* NTEMACS_TODO: dpyinfo->gray */
9529 struct w32_display_info *
9530 w32_term_init (display_name, xrm_option, resource_name)
9531 Lisp_Object display_name;
9532 char *xrm_option;
9533 char *resource_name;
9535 struct w32_display_info *dpyinfo;
9536 HDC hdc;
9538 BLOCK_INPUT;
9540 if (!w32_initialized)
9542 w32_initialize ();
9543 w32_initialized = 1;
9547 int argc = 0;
9548 char *argv[3];
9550 argv[0] = "";
9551 argc = 1;
9552 if (xrm_option)
9554 argv[argc++] = "-xrm";
9555 argv[argc++] = xrm_option;
9559 w32_initialize_display_info (display_name);
9561 dpyinfo = &one_w32_display_info;
9563 hdc = GetDC (GetDesktopWindow ());
9565 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
9566 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
9567 dpyinfo->root_window = GetDesktopWindow ();
9568 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
9569 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
9570 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
9571 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
9572 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
9573 dpyinfo->image_cache = make_image_cache ();
9574 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
9575 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
9576 ReleaseDC (GetDesktopWindow (), hdc);
9578 /* initialise palette with white and black */
9580 COLORREF color;
9581 w32_defined_color (0, "white", &color, 1);
9582 w32_defined_color (0, "black", &color, 1);
9585 /* Create Row Bitmaps and store them for later use. */
9586 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
9587 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
9588 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
9589 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
9590 1, continued_bits);
9591 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
9592 1, 1, continuation_bits);
9593 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
9595 #ifndef F_SETOWN_BUG
9596 #ifdef F_SETOWN
9597 #ifdef F_SETOWN_SOCK_NEG
9598 /* stdin is a socket here */
9599 fcntl (connection, F_SETOWN, -getpid ());
9600 #else /* ! defined (F_SETOWN_SOCK_NEG) */
9601 fcntl (connection, F_SETOWN, getpid ());
9602 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
9603 #endif /* ! defined (F_SETOWN) */
9604 #endif /* F_SETOWN_BUG */
9606 #ifdef SIGIO
9607 if (interrupt_input)
9608 init_sigio (connection);
9609 #endif /* ! defined (SIGIO) */
9611 UNBLOCK_INPUT;
9613 return dpyinfo;
9616 /* Get rid of display DPYINFO, assuming all frames are already gone. */
9618 void
9619 x_delete_display (dpyinfo)
9620 struct w32_display_info *dpyinfo;
9622 /* Discard this display from w32_display_name_list and w32_display_list.
9623 We can't use Fdelq because that can quit. */
9624 if (! NILP (w32_display_name_list)
9625 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
9626 w32_display_name_list = XCDR (w32_display_name_list);
9627 else
9629 Lisp_Object tail;
9631 tail = w32_display_name_list;
9632 while (CONSP (tail) && CONSP (XCDR (tail)))
9634 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
9636 XCDR (tail) = XCDR (XCDR (tail));
9637 break;
9639 tail = XCDR (tail);
9643 /* free palette table */
9645 struct w32_palette_entry * plist;
9647 plist = dpyinfo->color_list;
9648 while (plist)
9650 struct w32_palette_entry * pentry = plist;
9651 plist = plist->next;
9652 xfree(pentry);
9654 dpyinfo->color_list = NULL;
9655 if (dpyinfo->palette)
9656 DeleteObject(dpyinfo->palette);
9658 xfree (dpyinfo->font_table);
9659 xfree (dpyinfo->w32_id_name);
9661 /* Destroy row bitmaps. */
9662 DeleteObject (left_bmp);
9663 DeleteObject (ov_bmp);
9664 DeleteObject (right_bmp);
9665 DeleteObject (continued_bmp);
9666 DeleteObject (continuation_bmp);
9667 DeleteObject (zv_bmp);
9670 /* Set up use of W32. */
9672 DWORD w32_msg_worker ();
9674 void
9675 x_flush (struct frame * f)
9676 { /* Nothing to do */ }
9678 static struct redisplay_interface w32_redisplay_interface =
9680 x_produce_glyphs,
9681 x_write_glyphs,
9682 x_insert_glyphs,
9683 x_clear_end_of_line,
9684 x_scroll_run,
9685 x_after_update_window_line,
9686 x_update_window_begin,
9687 x_update_window_end,
9688 w32_cursor_to,
9689 x_flush,
9690 x_clear_mouse_face,
9691 x_get_glyph_overhangs,
9692 x_fix_overlapping_area
9695 void
9696 w32_initialize ()
9698 rif = &w32_redisplay_interface;
9700 /* MSVC does not type K&R functions with no arguments correctly, and
9701 so we must explicitly cast them. */
9702 clear_frame_hook = (void (*)(void)) x_clear_frame;
9703 ins_del_lines_hook = x_ins_del_lines;
9704 change_line_highlight_hook = x_change_line_highlight;
9705 delete_glyphs_hook = x_delete_glyphs;
9706 ring_bell_hook = (void (*)(void)) w32_ring_bell;
9707 reset_terminal_modes_hook = (void (*)(void)) w32_reset_terminal_modes;
9708 set_terminal_modes_hook = (void (*)(void)) w32_set_terminal_modes;
9709 update_begin_hook = x_update_begin;
9710 update_end_hook = x_update_end;
9711 set_terminal_window_hook = w32_set_terminal_window;
9712 read_socket_hook = w32_read_socket;
9713 frame_up_to_date_hook = w32_frame_up_to_date;
9714 reassert_line_highlight_hook = w32_reassert_line_highlight;
9715 mouse_position_hook = w32_mouse_position;
9716 frame_rehighlight_hook = w32_frame_rehighlight;
9717 frame_raise_lower_hook = w32_frame_raise_lower;
9718 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
9719 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
9720 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
9721 judge_scroll_bars_hook = w32_judge_scroll_bars;
9722 estimate_mode_line_height_hook = x_estimate_mode_line_height;
9724 scroll_region_ok = 1; /* we'll scroll partial frames */
9725 char_ins_del_ok = 0; /* just as fast to write the line */
9726 line_ins_del_ok = 1; /* we'll just blt 'em */
9727 fast_clear_end_of_line = 1; /* X does this well */
9728 memory_below_frame = 0; /* we don't remember what scrolls
9729 off the bottom */
9730 baud_rate = 19200;
9732 last_tool_bar_item = -1;
9733 any_help_event_p = 0;
9735 /* Initialize input mode: interrupt_input off, no flow control, allow
9736 8 bit character input, standard quit char. */
9737 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
9739 /* Create the window thread - it will terminate itself or when the app terminates */
9741 init_crit ();
9743 dwMainThreadId = GetCurrentThreadId ();
9744 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
9745 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
9747 /* Wait for thread to start */
9750 MSG msg;
9752 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
9754 hWindowsThread = CreateThread (NULL, 0,
9755 (LPTHREAD_START_ROUTINE) w32_msg_worker,
9756 0, 0, &dwWindowsThreadId);
9758 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
9761 /* It is desirable that mainThread should have the same notion of
9762 focus window and active window as windowsThread. Unfortunately, the
9763 following call to AttachThreadInput, which should do precisely what
9764 we need, causes major problems when Emacs is linked as a console
9765 program. Unfortunately, we have good reasons for doing that, so
9766 instead we need to send messages to windowsThread to make some API
9767 calls for us (ones that affect, or depend on, the active/focus
9768 window state. */
9769 #ifdef ATTACH_THREADS
9770 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
9771 #endif
9773 /* Dynamically link to optional system components. */
9775 HANDLE user_lib = LoadLibrary ("user32.dll");
9777 #define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
9779 /* New proportional scroll bar functions. */
9780 LOAD_PROC( SetScrollInfo );
9781 LOAD_PROC( GetScrollInfo );
9783 #undef LOAD_PROC
9785 FreeLibrary (user_lib);
9787 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
9788 otherwise use the fixed height. */
9789 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
9790 GetSystemMetrics (SM_CYVTHUMB);
9792 /* For either kind of scroll bar, take account of the arrows; these
9793 effectively form the border of the main scroll bar range. */
9794 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
9795 = GetSystemMetrics (SM_CYVSCROLL);
9799 void
9800 syms_of_w32term ()
9802 Lisp_Object codepage;
9804 staticpro (&w32_display_name_list);
9805 w32_display_name_list = Qnil;
9807 staticpro (&last_mouse_scroll_bar);
9808 last_mouse_scroll_bar = Qnil;
9810 staticpro (&Qvendor_specific_keysyms);
9811 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
9813 DEFVAR_INT ("w32-num-mouse-buttons",
9814 &Vw32_num_mouse_buttons,
9815 "Number of physical mouse buttons.");
9816 Vw32_num_mouse_buttons = Qnil;
9818 DEFVAR_LISP ("w32-swap-mouse-buttons",
9819 &Vw32_swap_mouse_buttons,
9820 "Swap the mapping of middle and right mouse buttons.\n\
9821 When nil, middle button is mouse-2 and right button is mouse-3.");
9822 Vw32_swap_mouse_buttons = Qnil;
9824 DEFVAR_LISP ("w32-grab-focus-on-raise",
9825 &Vw32_grab_focus_on_raise,
9826 "Raised frame grabs input focus.\n\
9827 When t, `raise-frame' grabs input focus as well. This fits well\n\
9828 with the normal Windows click-to-focus policy, but might not be\n\
9829 desirable when using a point-to-focus policy.");
9830 Vw32_grab_focus_on_raise = Qt;
9832 DEFVAR_LISP ("w32-capslock-is-shiftlock",
9833 &Vw32_capslock_is_shiftlock,
9834 "Apply CapsLock state to non character input keys.\n\
9835 When nil, CapsLock only affects normal character input keys.");
9836 Vw32_capslock_is_shiftlock = Qnil;
9838 DEFVAR_LISP ("w32-recognize-altgr",
9839 &Vw32_recognize_altgr,
9840 "Recognize right-alt and left-ctrl as AltGr.\n\
9841 When nil, the right-alt and left-ctrl key combination is\n\
9842 interpreted normally.");
9843 Vw32_recognize_altgr = Qt;
9845 DEFVAR_BOOL ("w32-enable-unicode-output",
9846 &w32_enable_unicode_output,
9847 "Enable the use of Unicode for text output if non-nil.\n\
9848 Unicode output may prevent some third party applications for displaying\n\
9849 Far-East Languages on Windows 95/98 from working properly.\n\
9850 NT uses Unicode internally anyway, so this flag will probably have no\n\
9851 affect on NT machines.");
9852 w32_enable_unicode_output = 1;
9854 help_echo = Qnil;
9855 staticpro (&help_echo);
9856 help_echo_object = Qnil;
9857 staticpro (&help_echo_object);
9858 help_echo_window = Qnil;
9859 staticpro (&help_echo_window);
9860 previous_help_echo = Qnil;
9861 staticpro (&previous_help_echo);
9862 help_echo_pos = -1;
9864 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
9865 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
9866 For example, if a block cursor is over a tab, it will be drawn as\n\
9867 wide as that tab on the display.");
9868 x_stretch_cursor_p = 0;
9870 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
9871 "If not nil, Emacs uses toolkit scroll bars.");
9872 x_toolkit_scroll_bars_p = 1;
9874 staticpro (&last_mouse_motion_frame);
9875 last_mouse_motion_frame = Qnil;