Implement HiDPI support for underwave on MS-Windows
[emacs.git] / src / w32term.c
blob6d2fa335859f42ab4416a3d615318c6994ced9bf
1 /* Implementation of GUI terminal on the Microsoft Windows API.
3 Copyright (C) 1989, 1993-2017 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 3 of the License, or (at
10 your option) 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. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include "lisp.h"
24 #include "blockinput.h"
25 #include "w32term.h"
26 #include "w32common.h" /* for OS version info */
28 #include <ctype.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #ifdef CYGWIN
32 #include <fcntl.h> /* for O_RDWR */
33 #endif
34 #include <imm.h>
35 #include <math.h>
37 #include "coding.h"
38 #include "frame.h"
39 #include "fontset.h"
40 #include "termhooks.h"
41 #include "termopts.h"
42 #include "termchar.h"
43 #include "buffer.h"
44 #include "window.h"
45 #include "keyboard.h"
46 #include "menu.h" /* for w32_menu_show */
48 #ifdef WINDOWSNT
49 #include "w32.h" /* for filename_from_utf16, filename_from_ansi */
50 #endif
52 #ifndef WINDOWSNT
53 #include <io.h> /* for get_osfhandle */
54 #endif
56 #include <shellapi.h>
58 #include "font.h"
59 #include "w32font.h"
61 #if 0 /* TODO: stipple */
62 #include "bitmaps/gray.xbm"
63 #endif
65 /* Fringe bitmaps. */
67 static int max_fringe_bmp = 0;
68 static HBITMAP *fringe_bmp = 0;
70 /* Temporary variables for w32_read_socket. */
72 static int last_mousemove_x = 0;
73 static int last_mousemove_y = 0;
75 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
76 #ifndef GET_WHEEL_DELTA_WPARAM
77 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
78 #endif
80 /* Non-zero means that a HELP_EVENT has been generated since Emacs
81 start. */
83 static int any_help_event_p;
85 extern unsigned int msh_mousewheel;
87 extern int w32_codepage_for_font (char *fontname);
88 extern Cursor w32_load_cursor (LPCTSTR name);
90 #define x_any_window_to_frame x_window_to_frame
91 #define x_top_window_to_frame x_window_to_frame
94 /* This is display since w32 does not support multiple ones. */
95 struct w32_display_info one_w32_display_info;
96 struct w32_display_info *x_display_list;
98 #if _WIN32_WINNT < 0x0500 && !defined(MINGW_W64)
99 /* Pre Windows 2000, this was not available, but define it here so
100 that Emacs compiled on such a platform will run on newer versions.
101 MinGW64 defines these unconditionally, so avoid redefining. */
103 typedef struct tagWCRANGE
105 WCHAR wcLow;
106 USHORT cGlyphs;
107 } WCRANGE;
109 typedef struct tagGLYPHSET
111 DWORD cbThis;
112 DWORD flAccel;
113 DWORD cGlyphsSupported;
114 DWORD cRanges;
115 WCRANGE ranges[1];
116 } GLYPHSET;
118 #endif /* compiling for pre-Win2k */
120 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
121 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
123 #ifndef LWA_ALPHA
124 #define LWA_ALPHA 0x02
125 #endif
126 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
127 later targets by MSVC headers. */
128 #ifndef WS_EX_LAYERED
129 #define WS_EX_LAYERED 0x80000
130 #endif
132 /* SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN are not defined on 95 and
133 NT4. */
134 #ifndef SM_CXVIRTUALSCREEN
135 #define SM_CXVIRTUALSCREEN 78
136 #endif
137 #ifndef SM_CYVIRTUALSCREEN
138 #define SM_CYVIRTUALSCREEN 79
139 #endif
141 /* The handle of the frame that currently owns the system caret. */
142 HWND w32_system_caret_hwnd;
143 int w32_system_caret_height;
144 int w32_system_caret_x;
145 int w32_system_caret_y;
146 struct window *w32_system_caret_window;
147 int w32_system_caret_hdr_height;
148 int w32_system_caret_mode_height;
149 DWORD dwWindowsThreadId = 0;
150 HANDLE hWindowsThread = NULL;
151 DWORD dwMainThreadId = 0;
152 HANDLE hMainThread = NULL;
154 int vertical_scroll_bar_min_handle;
155 int horizontal_scroll_bar_min_handle;
156 int vertical_scroll_bar_top_border;
157 int vertical_scroll_bar_bottom_border;
158 int horizontal_scroll_bar_left_border;
159 int horizontal_scroll_bar_right_border;
161 int last_scroll_bar_drag_pos;
163 /* Keyboard code page - may be changed by language-change events. */
164 int w32_keyboard_codepage;
166 /* Incremented by w32_read_socket whenever it really tries to read
167 events. */
168 static int volatile input_signal_count;
170 #ifdef CYGWIN
171 int w32_message_fd = -1;
172 #endif /* CYGWIN */
174 static void w32_handle_tool_bar_click (struct frame *,
175 struct input_event *);
176 static void w32_define_cursor (Window, Cursor);
178 void x_lower_frame (struct frame *);
179 void x_scroll_bar_clear (struct frame *);
180 void x_raise_frame (struct frame *);
181 void x_wm_set_window_state (struct frame *, int);
182 void x_wm_set_icon_pixmap (struct frame *, int);
183 static void w32_initialize (void);
184 static void x_update_end (struct frame *);
185 static void w32_frame_up_to_date (struct frame *);
186 static void x_clear_frame (struct frame *);
187 static void frame_highlight (struct frame *);
188 static void frame_unhighlight (struct frame *);
189 static void x_new_focus_frame (struct w32_display_info *,
190 struct frame *);
191 static void x_focus_changed (int, int, struct w32_display_info *,
192 struct frame *, struct input_event *);
193 static void w32_detect_focus_change (struct w32_display_info *,
194 W32Msg *, struct input_event *);
195 static void w32_frame_rehighlight (struct frame *);
196 static void x_frame_rehighlight (struct w32_display_info *);
197 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
198 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
199 enum text_cursor_kinds);
200 static void w32_clip_to_row (struct window *, struct glyph_row *,
201 enum glyph_row_area, HDC);
202 static BOOL my_show_window (struct frame *, HWND, int);
203 static void my_set_window_pos (HWND, HWND, int, int, int, int, UINT);
204 #if 0
205 static void my_set_focus (struct frame *, HWND);
206 #endif
207 static void my_set_foreground_window (HWND);
208 static void my_destroy_window (struct frame *, HWND);
209 static void w32fullscreen_hook (struct frame *);
211 #ifdef GLYPH_DEBUG
212 static void x_check_font (struct frame *, struct font *);
213 #endif
216 /***********************************************************************
217 Debugging
218 ***********************************************************************/
220 #if 0
222 /* This is a function useful for recording debugging information about
223 the sequence of occurrences in this file. */
225 struct record
227 char *locus;
228 int type;
231 struct record event_record[100];
233 int event_record_index;
235 record_event (char *locus, int type)
237 if (event_record_index == sizeof (event_record) / sizeof (struct record))
238 event_record_index = 0;
240 event_record[event_record_index].locus = locus;
241 event_record[event_record_index].type = type;
242 event_record_index++;
245 #endif /* 0 */
248 static void
249 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
250 XGCValues *xgcv)
252 if (mask & GCForeground)
253 gc->foreground = xgcv->foreground;
254 if (mask & GCBackground)
255 gc->background = xgcv->background;
256 if (mask & GCFont)
257 gc->font = xgcv->font;
260 XGCValues *
261 XCreateGC (void *ignore, HWND wignore, unsigned long mask, XGCValues *xgcv)
263 XGCValues *gc = xzalloc (sizeof (XGCValues));
265 XChangeGC (ignore, gc, mask, xgcv);
267 return gc;
270 #if 0 /* unused for now, see x_draw_image_glyph_string below */
271 static void
272 XGetGCValues (void *ignore, XGCValues *gc,
273 unsigned long mask, XGCValues *xgcv)
275 XChangeGC (ignore, xgcv, mask, gc);
277 #endif
279 static void
280 w32_set_clip_rectangle (HDC hdc, RECT *rect)
282 if (rect)
284 HRGN clip_region = CreateRectRgnIndirect (rect);
285 SelectClipRgn (hdc, clip_region);
286 DeleteObject (clip_region);
288 else
289 SelectClipRgn (hdc, NULL);
292 /* Restore clipping rectangle in S */
293 static void
294 w32_restore_glyph_string_clip (struct glyph_string *s)
296 RECT *r = s->clip;
297 int n = s->num_clips;
299 if (n == 1)
300 w32_set_clip_rectangle (s->hdc, r);
301 else if (n > 1)
303 HRGN clip1 = CreateRectRgnIndirect (r);
304 HRGN clip2 = CreateRectRgnIndirect (r + 1);
305 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
306 SelectClipRgn (s->hdc, clip1);
307 DeleteObject (clip1);
308 DeleteObject (clip2);
312 static void
313 x_get_scale_factor(struct w32_display_info *dpyinfo, int *scale_x, int *scale_y)
315 const int base_res = 96;
317 *scale_x = *scale_y = 1;
319 if (dpyinfo)
321 *scale_x = floor (dpyinfo->resx / base_res);
322 *scale_y = floor (dpyinfo->resy / base_res);
327 Draw a wavy line under S. The wave fills wave_height pixels from y0.
329 x0 wave_length = 2
331 y0 * * * * *
332 |* * * * * * * * *
333 wave_height = 3 | * * * *
337 static void
338 w32_draw_underwave (struct glyph_string *s, COLORREF color)
340 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (s->f);
342 int scale_x, scale_y;
343 x_get_scale_factor (dpyinfo, &scale_x, &scale_y);
345 int wave_height = 3 * scale_y, wave_length = 2 * scale_x, thickness = scale_y;
346 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
347 XRectangle wave_clip, string_clip, final_clip;
348 RECT w32_final_clip, w32_string_clip;
349 HPEN hp, oldhp;
351 dx = wave_length;
352 dy = wave_height - 1;
353 x0 = s->x;
354 y0 = s->ybase + wave_height / 2 - scale_y;
355 width = s->width;
356 xmax = x0 + width;
358 /* Find and set clipping rectangle */
360 wave_clip.x = x0;
361 wave_clip.y = y0;
362 wave_clip.width = width;
363 wave_clip.height = wave_height;
365 get_glyph_string_clip_rect (s, &w32_string_clip);
366 CONVERT_TO_XRECT (string_clip, w32_string_clip);
368 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
369 return;
371 hp = CreatePen (PS_SOLID, thickness, color);
372 oldhp = SelectObject (s->hdc, hp);
373 CONVERT_FROM_XRECT (final_clip, w32_final_clip);
374 w32_set_clip_rectangle (s->hdc, &w32_final_clip);
376 /* Draw the waves */
378 x1 = x0 - (x0 % dx);
379 x2 = x1 + dx;
380 odd = (x1/dx) % 2;
381 y1 = y2 = y0;
383 if (odd)
384 y1 += dy;
385 else
386 y2 += dy;
388 MoveToEx (s->hdc, x1, y1, NULL);
390 while (x1 <= xmax)
392 LineTo (s->hdc, x2, y2);
393 x1 = x2, y1 = y2;
394 x2 += dx, y2 = y0 + odd*dy;
395 odd = !odd;
398 /* Restore previous pen and clipping rectangle(s) */
399 w32_restore_glyph_string_clip (s);
400 SelectObject (s->hdc, oldhp);
401 DeleteObject (hp);
404 /* Draw a hollow rectangle at the specified position. */
405 static void
406 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
407 int width, int height)
409 HBRUSH hb, oldhb;
410 HPEN hp, oldhp;
412 hb = CreateSolidBrush (gc->background);
413 hp = CreatePen (PS_SOLID, 0, gc->foreground);
414 oldhb = SelectObject (hdc, hb);
415 oldhp = SelectObject (hdc, hp);
417 /* We enlarge WIDTH and HEIGHT by 1 to be bug-compatible to the
418 brain-dead design of XDrawRectangle, which draws a rectangle that
419 is 1 pixel wider and higher than its arguments WIDTH and HEIGHT.
420 This allows us to keep the code that calls this function similar
421 to the corresponding code in xterm.c. For the details, see
422 http://lists.gnu.org/archives/html/emacs-devel/2014-10/msg00546.html. */
423 Rectangle (hdc, x, y, x + width + 1, y + height + 1);
425 SelectObject (hdc, oldhb);
426 SelectObject (hdc, oldhp);
427 DeleteObject (hb);
428 DeleteObject (hp);
431 /* Draw a filled rectangle at the specified position. */
432 void
433 w32_fill_rect (struct frame *f, HDC hdc, COLORREF pix, RECT *lprect)
435 HBRUSH hb;
437 hb = CreateSolidBrush (pix);
438 FillRect (hdc, lprect, hb);
439 DeleteObject (hb);
442 void
443 w32_clear_window (struct frame *f)
445 RECT rect;
446 HDC hdc = get_frame_dc (f);
448 /* Under certain conditions, this can be called at startup with
449 a console frame pointer before the GUI frame is created. An HDC
450 of 0 indicates this. */
451 if (hdc)
453 GetClientRect (FRAME_W32_WINDOW (f), &rect);
454 w32_clear_rect (f, hdc, &rect);
457 release_frame_dc (f, hdc);
460 #define OPAQUE_FRAME 255
462 void
463 x_set_frame_alpha (struct frame *f)
465 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
466 double alpha = 1.0;
467 double alpha_min = 1.0;
468 BYTE opac;
469 LONG ex_style;
470 HWND window = FRAME_W32_WINDOW (f);
472 /* Older versions of Windows do not support transparency. */
473 if (!pfnSetLayeredWindowAttributes)
474 return;
476 if (dpyinfo->w32_focus_frame == f)
477 alpha = f->alpha[0];
478 else
479 alpha = f->alpha[1];
481 if (FLOATP (Vframe_alpha_lower_limit))
482 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
483 else if (INTEGERP (Vframe_alpha_lower_limit))
484 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
486 if (alpha < 0.0)
487 return;
488 else if (alpha > 1.0)
489 alpha = 1.0;
490 else if (alpha < alpha_min && alpha_min <= 1.0)
491 alpha = alpha_min;
493 opac = alpha * OPAQUE_FRAME;
495 ex_style = GetWindowLong (window, GWL_EXSTYLE);
497 if (opac == OPAQUE_FRAME)
498 ex_style &= ~WS_EX_LAYERED;
499 else
500 ex_style |= WS_EX_LAYERED;
502 SetWindowLong (window, GWL_EXSTYLE, ex_style);
504 if (opac != OPAQUE_FRAME)
505 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
509 x_display_pixel_height (struct w32_display_info *dpyinfo)
511 int pixels = GetSystemMetrics (SM_CYVIRTUALSCREEN);
513 if (pixels == 0)
514 /* Fallback for Windows 95 or NT 4.0. */
515 pixels = GetSystemMetrics (SM_CYSCREEN);
517 return pixels;
521 x_display_pixel_width (struct w32_display_info *dpyinfo)
523 int pixels = GetSystemMetrics (SM_CXVIRTUALSCREEN);
525 if (pixels == 0)
526 /* Fallback for Windows 95 or NT 4.0. */
527 pixels = GetSystemMetrics (SM_CXSCREEN);
529 return pixels;
533 /***********************************************************************
534 Starting and ending an update
535 ***********************************************************************/
537 /* Start an update of frame F. This function is installed as a hook
538 for update_begin, i.e. it is called when update_begin is called.
539 This function is called prior to calls to x_update_window_begin for
540 each window being updated. */
542 static void
543 x_update_begin (struct frame *f)
545 struct w32_display_info *display_info = FRAME_DISPLAY_INFO (f);
547 if (! FRAME_W32_P (f))
548 return;
550 /* Regenerate display palette before drawing if list of requested
551 colors has changed. */
552 if (display_info->regen_palette)
554 w32_regenerate_palette (f);
555 display_info->regen_palette = FALSE;
560 /* Start update of window W. */
562 static void
563 x_update_window_begin (struct window *w)
565 struct frame *f = XFRAME (WINDOW_FRAME (w));
566 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
568 /* Hide the system caret during an update. */
569 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
571 SendMessageTimeout (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0,
572 0, 6000, NULL);
575 w->output_cursor = w->cursor;
577 block_input ();
579 if (f == hlinfo->mouse_face_mouse_frame)
581 /* Don't do highlighting for mouse motion during the update. */
582 hlinfo->mouse_face_defer = true;
584 /* If F needs to be redrawn, simply forget about any prior mouse
585 highlighting. */
586 if (FRAME_GARBAGED_P (f))
587 hlinfo->mouse_face_window = Qnil;
589 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
590 their mouse_face_p flag set, which means that they are always
591 unequal to rows in a desired matrix which never have that
592 flag set. So, rows containing mouse-face glyphs are never
593 scrolled, and we don't have to switch the mouse highlight off
594 here to prevent it from being scrolled. */
596 /* Can we tell that this update does not affect the window
597 where the mouse highlight is? If so, no need to turn off.
598 Likewise, don't do anything if the frame is garbaged;
599 in that case, the frame's current matrix that we would use
600 is all wrong, and we will redisplay that line anyway. */
601 if (!NILP (hlinfo->mouse_face_window)
602 && w == XWINDOW (hlinfo->mouse_face_window))
604 int i;
606 for (i = 0; i < w->desired_matrix->nrows; ++i)
607 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
608 break;
610 if (i < w->desired_matrix->nrows)
611 clear_mouse_face (hlinfo);
613 #endif /* 0 */
616 unblock_input ();
619 /* Draw a vertical window border from (x,y0) to (x,y1) */
621 static void
622 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
624 struct frame *f = XFRAME (WINDOW_FRAME (w));
625 RECT r;
626 HDC hdc;
627 struct face *face;
629 r.left = x;
630 r.right = x + 1;
631 r.top = y0;
632 r.bottom = y1;
634 hdc = get_frame_dc (f);
635 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
636 if (face)
637 w32_fill_rect (f, hdc, face->foreground, &r);
638 else
639 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
641 release_frame_dc (f, hdc);
645 /* Draw a window divider from (x0, y0) to (x1, y1) */
647 static void
648 w32_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
650 struct frame *f = XFRAME (WINDOW_FRAME (w));
651 HDC hdc = get_frame_dc (f);
652 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
653 struct face *face_first
654 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
655 struct face *face_last
656 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
657 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
658 unsigned long color_first = (face_first
659 ? face_first->foreground
660 : FRAME_FOREGROUND_PIXEL (f));
661 unsigned long color_last = (face_last
662 ? face_last->foreground
663 : FRAME_FOREGROUND_PIXEL (f));
665 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
666 /* Vertical. */
668 w32_fill_area_abs (f, hdc, color_first, x0, y0, x0 + 1, y1);
669 w32_fill_area_abs (f, hdc, color, x0 + 1, y0, x1 - 1, y1);
670 w32_fill_area_abs (f, hdc, color_last, x1 - 1, y0, x1, y1);
672 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
673 /* Horizontal. */
675 w32_fill_area_abs (f, hdc, color_first, x0, y0, x1, y0 + 1);
676 w32_fill_area_abs (f, hdc, color, x0, y0 + 1, x1, y1 - 1);
677 w32_fill_area_abs (f, hdc, color_last, x0, y1 - 1, x1, y1);
679 else
680 w32_fill_area_abs (f, hdc, color, x0, y0, x1, y1);
682 release_frame_dc (f, hdc);
685 /* End update of window W.
687 Draw vertical borders between horizontally adjacent windows, and
688 display W's cursor if CURSOR_ON_P is non-zero.
690 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
691 glyphs in mouse-face were overwritten. In that case we have to
692 make sure that the mouse-highlight is properly redrawn.
694 W may be a menu bar pseudo-window in case we don't have X toolkit
695 support. Such windows don't have a cursor, so don't display it
696 here. */
698 static void
699 x_update_window_end (struct window *w, bool cursor_on_p,
700 bool mouse_face_overwritten_p)
702 if (!w->pseudo_window_p)
704 block_input ();
706 if (cursor_on_p)
707 display_and_set_cursor (w, true,
708 w->output_cursor.hpos, w->output_cursor.vpos,
709 w->output_cursor.x, w->output_cursor.y);
711 if (draw_window_fringes (w, true))
713 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
714 x_draw_right_divider (w);
715 else
716 x_draw_vertical_border (w);
719 unblock_input ();
722 /* If a row with mouse-face was overwritten, arrange for
723 XTframe_up_to_date to redisplay the mouse highlight. */
724 if (mouse_face_overwritten_p)
726 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
728 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
729 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
730 hlinfo->mouse_face_window = Qnil;
733 /* Unhide the caret. This won't actually show the cursor, unless it
734 was visible before the corresponding call to HideCaret in
735 x_update_window_begin. */
736 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
738 SendMessageTimeout (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0,
739 0, 6000, NULL);
744 /* End update of frame F. This function is installed as a hook in
745 update_end. */
747 static void
748 x_update_end (struct frame *f)
750 if (! FRAME_W32_P (f))
751 return;
753 /* Mouse highlight may be displayed again. */
754 MOUSE_HL_INFO (f)->mouse_face_defer = false;
758 /* This function is called from various places in xdisp.c
759 whenever a complete update has been performed. */
761 static void
762 w32_frame_up_to_date (struct frame *f)
764 if (FRAME_W32_P (f))
765 FRAME_MOUSE_UPDATE (f);
769 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
770 arrow bitmaps, or clear the fringes if no bitmaps are required
771 before DESIRED_ROW is made current. This function is called from
772 update_window_line only if it is known that there are differences
773 between bitmaps to be drawn between current row and DESIRED_ROW. */
775 static void
776 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
778 struct frame *f;
779 int width, height;
781 eassert (w);
783 if (!desired_row->mode_line_p && !w->pseudo_window_p)
784 desired_row->redraw_fringe_bitmaps_p = true;
786 /* When a window has disappeared, make sure that no rest of
787 full-width rows stays visible in the internal border. Could
788 check here if updated window is the leftmost/rightmost window,
789 but I guess it's not worth doing since vertically split windows
790 are almost never used, internal border is rarely set, and the
791 overhead is very small. */
792 if (windows_or_buffers_changed
793 && desired_row->full_width_p
794 && (f = XFRAME (w->frame),
795 width = FRAME_INTERNAL_BORDER_WIDTH (f),
796 width != 0)
797 && (height = desired_row->visible_height,
798 height > 0))
800 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
802 block_input ();
804 HDC hdc = get_frame_dc (f);
805 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
807 if (face)
809 /* Fill border with internal border face. */
810 unsigned long color = face->background;
812 w32_fill_area (f, hdc, color, 0, y, width, height);
813 w32_fill_area (f, hdc, color, FRAME_PIXEL_WIDTH (f) - width,
814 y, width, height);
816 else
818 w32_clear_area (f, hdc, 0, y, width, height);
819 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
820 y, width, height);
822 release_frame_dc (f, hdc);
824 unblock_input ();
829 /* Draw the bitmap WHICH in one of the left or right fringes of
830 window W. ROW is the glyph row for which to display the bitmap; it
831 determines the vertical position at which the bitmap has to be
832 drawn. */
834 static void
835 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
836 struct draw_fringe_bitmap_params *p)
838 struct frame *f = XFRAME (WINDOW_FRAME (w));
839 HDC hdc;
840 struct face *face = p->face;
842 hdc = get_frame_dc (f);
844 /* Must clip because of partially visible lines. */
845 w32_clip_to_row (w, row, ANY_AREA, hdc);
847 if (p->bx >= 0 && !p->overlay_p)
848 w32_fill_area (f, hdc, face->background,
849 p->bx, p->by, p->nx, p->ny);
851 if (p->which && p->which < max_fringe_bmp)
853 HBITMAP pixmap = fringe_bmp[p->which];
854 HDC compat_hdc;
855 HANDLE horig_obj;
857 compat_hdc = CreateCompatibleDC (hdc);
859 SaveDC (hdc);
861 horig_obj = SelectObject (compat_hdc, pixmap);
863 /* Paint overlays transparently. */
864 if (p->overlay_p)
866 HBRUSH h_brush, h_orig_brush;
868 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
869 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
870 h_brush = CreateSolidBrush (face->foreground);
871 h_orig_brush = SelectObject (hdc, h_brush);
873 BitBlt (hdc, p->x, p->y, p->wd, p->h,
874 compat_hdc, 0, p->dh,
875 DSTINVERT);
876 BitBlt (hdc, p->x, p->y, p->wd, p->h,
877 compat_hdc, 0, p->dh,
878 0x2E064A);
879 BitBlt (hdc, p->x, p->y, p->wd, p->h,
880 compat_hdc, 0, p->dh,
881 DSTINVERT);
883 SelectObject (hdc, h_orig_brush);
884 DeleteObject (h_brush);
886 else
888 SetTextColor (hdc, face->background);
889 SetBkColor (hdc, (p->cursor_p
890 ? f->output_data.w32->cursor_pixel
891 : face->foreground));
893 BitBlt (hdc, p->x, p->y, p->wd, p->h,
894 compat_hdc, 0, p->dh,
895 SRCCOPY);
898 SelectObject (compat_hdc, horig_obj);
899 DeleteDC (compat_hdc);
900 RestoreDC (hdc, -1);
903 w32_set_clip_rectangle (hdc, NULL);
905 release_frame_dc (f, hdc);
908 static void
909 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
911 if (which >= max_fringe_bmp)
913 int i = max_fringe_bmp;
914 max_fringe_bmp = which + 20;
915 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
916 while (i < max_fringe_bmp)
917 fringe_bmp[i++] = 0;
920 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
923 static void
924 w32_destroy_fringe_bitmap (int which)
926 if (which >= max_fringe_bmp)
927 return;
929 if (fringe_bmp[which])
930 DeleteObject (fringe_bmp[which]);
931 fringe_bmp[which] = 0;
934 /***********************************************************************
935 Display Iterator
936 ***********************************************************************/
938 /* Function prototypes of this page. */
940 static void x_set_glyph_string_clipping (struct glyph_string *);
941 static void x_set_glyph_string_gc (struct glyph_string *);
942 static void x_draw_glyph_string_background (struct glyph_string *,
943 bool);
944 static void x_draw_glyph_string_foreground (struct glyph_string *);
945 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
946 static void x_draw_glyph_string_box (struct glyph_string *);
947 static void x_draw_glyph_string (struct glyph_string *);
948 static void x_set_cursor_gc (struct glyph_string *);
949 static void x_set_mode_line_face_gc (struct glyph_string *);
950 static void x_set_mouse_face_gc (struct glyph_string *);
951 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
952 static void w32_setup_relief_color (struct frame *, struct relief *,
953 double, int, COLORREF);
954 static void x_setup_relief_colors (struct glyph_string *);
955 static void x_draw_image_glyph_string (struct glyph_string *);
956 static void x_draw_image_relief (struct glyph_string *);
957 static void x_draw_image_foreground (struct glyph_string *);
958 static void w32_draw_image_foreground_1 (struct glyph_string *, HBITMAP);
959 static void x_clear_glyph_string_rect (struct glyph_string *, int,
960 int, int, int);
961 static void w32_draw_relief_rect (struct frame *, int, int, int, int,
962 int, int, int, int, int, int,
963 RECT *);
964 static void w32_draw_box_rect (struct glyph_string *, int, int, int, int,
965 int, bool, bool, RECT *);
968 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
969 face. */
971 static void
972 x_set_cursor_gc (struct glyph_string *s)
974 if (s->font == FRAME_FONT (s->f)
975 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
976 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
977 /* Sometimes we are not called for each change in the default
978 face's background color (e.g., bug#26851), so the additional
979 test in the next line gives us a chance to resync. */
980 && s->f->output_data.w32->cursor_gc->foreground == s->face->background
981 && !s->cmp)
982 s->gc = s->f->output_data.w32->cursor_gc;
983 else
985 /* Cursor on non-default face: must merge. */
986 XGCValues xgcv;
987 unsigned long mask;
989 xgcv.background = s->f->output_data.w32->cursor_pixel;
990 xgcv.foreground = s->face->background;
992 /* If the glyph would be invisible, try a different foreground. */
993 if (xgcv.foreground == xgcv.background)
994 xgcv.foreground = s->face->foreground;
995 if (xgcv.foreground == xgcv.background)
996 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
997 if (xgcv.foreground == xgcv.background)
998 xgcv.foreground = s->face->foreground;
1000 /* Make sure the cursor is distinct from text in this face. */
1001 if (xgcv.background == s->face->background
1002 && xgcv.foreground == s->face->foreground)
1004 xgcv.background = s->face->foreground;
1005 xgcv.foreground = s->face->background;
1008 IF_DEBUG (x_check_font (s->f, s->font));
1009 xgcv.font = s->font;
1010 mask = GCForeground | GCBackground | GCFont;
1012 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1013 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1014 mask, &xgcv);
1015 else
1016 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1017 = XCreateGC (NULL, FRAME_W32_WINDOW (s->f), mask, &xgcv);
1019 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1024 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1026 static void
1027 x_set_mouse_face_gc (struct glyph_string *s)
1029 int face_id;
1030 struct face *face;
1032 /* What face has to be used last for the mouse face? */
1033 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1034 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1035 if (face == NULL)
1036 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1038 if (s->first_glyph->type == CHAR_GLYPH)
1039 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1040 else
1041 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1042 s->face = FACE_FROM_ID (s->f, face_id);
1043 prepare_face_for_display (s->f, s->face);
1045 /* If font in this face is same as S->font, use it. */
1046 if (s->font == s->face->font)
1047 s->gc = s->face->gc;
1048 else
1050 /* Otherwise construct scratch_cursor_gc with values from FACE
1051 but font FONT. */
1052 XGCValues xgcv;
1053 unsigned long mask;
1055 xgcv.background = s->face->background;
1056 xgcv.foreground = s->face->foreground;
1057 IF_DEBUG (x_check_font (s->f, s->font));
1058 xgcv.font = s->font;
1059 mask = GCForeground | GCBackground | GCFont;
1061 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1062 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1063 mask, &xgcv);
1064 else
1065 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1066 = XCreateGC (NULL, FRAME_W32_WINDOW (s->f), mask, &xgcv);
1068 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1071 eassert (s->gc != 0);
1075 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1076 Faces to use in the mode line have already been computed when the
1077 matrix was built, so there isn't much to do, here. */
1079 static inline void
1080 x_set_mode_line_face_gc (struct glyph_string *s)
1082 s->gc = s->face->gc;
1086 /* Set S->gc of glyph string S for drawing that glyph string. Set
1087 S->stippled_p to a non-zero value if the face of S has a stipple
1088 pattern. */
1090 static inline void
1091 x_set_glyph_string_gc (struct glyph_string *s)
1093 prepare_face_for_display (s->f, s->face);
1095 if (s->hl == DRAW_NORMAL_TEXT)
1097 s->gc = s->face->gc;
1098 s->stippled_p = s->face->stipple != 0;
1100 else if (s->hl == DRAW_INVERSE_VIDEO)
1102 x_set_mode_line_face_gc (s);
1103 s->stippled_p = s->face->stipple != 0;
1105 else if (s->hl == DRAW_CURSOR)
1107 x_set_cursor_gc (s);
1108 s->stippled_p = false;
1110 else if (s->hl == DRAW_MOUSE_FACE)
1112 x_set_mouse_face_gc (s);
1113 s->stippled_p = s->face->stipple != 0;
1115 else if (s->hl == DRAW_IMAGE_RAISED
1116 || s->hl == DRAW_IMAGE_SUNKEN)
1118 s->gc = s->face->gc;
1119 s->stippled_p = s->face->stipple != 0;
1121 else
1122 emacs_abort ();
1124 /* GC must have been set. */
1125 eassert (s->gc != 0);
1129 /* Set clipping for output of glyph string S. S may be part of a mode
1130 line or menu if we don't have X toolkit support. */
1132 static inline void
1133 x_set_glyph_string_clipping (struct glyph_string *s)
1135 RECT *r = s->clip;
1136 int n = get_glyph_string_clip_rects (s, r, 2);
1138 if (n == 1)
1139 w32_set_clip_rectangle (s->hdc, r);
1140 else if (n > 1)
1142 HRGN clip1 = CreateRectRgnIndirect (r);
1143 HRGN clip2 = CreateRectRgnIndirect (r + 1);
1144 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
1145 SelectClipRgn (s->hdc, clip1);
1146 DeleteObject (clip1);
1147 DeleteObject (clip2);
1149 s->num_clips = n;
1152 /* Set SRC's clipping for output of glyph string DST. This is called
1153 when we are drawing DST's left_overhang or right_overhang only in
1154 the area of SRC. */
1156 static void
1157 x_set_glyph_string_clipping_exactly (struct glyph_string *src,
1158 struct glyph_string *dst)
1160 RECT r;
1162 r.left = src->x;
1163 r.right = r.left + src->width;
1164 r.top = src->y;
1165 r.bottom = r.top + src->height;
1166 dst->clip[0] = r;
1167 dst->num_clips = 1;
1168 w32_set_clip_rectangle (dst->hdc, &r);
1171 /* RIF:
1172 Compute left and right overhang of glyph string S. */
1174 static void
1175 w32_compute_glyph_string_overhangs (struct glyph_string *s)
1177 if (s->cmp == NULL
1178 && s->first_glyph->type == CHAR_GLYPH
1179 && !s->font_not_found_p)
1181 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1182 struct font *font = s->font;
1183 struct font_metrics metrics;
1184 int i;
1186 for (i = 0; i < s->nchars; i++)
1187 code[i] = s->char2b[i];
1188 font->driver->text_extents (font, code, s->nchars, &metrics);
1189 s->right_overhang = (metrics.rbearing > metrics.width
1190 ? metrics.rbearing - metrics.width : 0);
1191 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1193 else if (s->cmp)
1195 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1196 s->left_overhang = -s->cmp->lbearing;
1200 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1202 static inline void
1203 x_clear_glyph_string_rect (struct glyph_string *s,
1204 int x, int y, int w, int h)
1206 int real_x = x;
1207 int real_y = y;
1208 int real_w = w;
1209 int real_h = h;
1210 #if 0
1211 /* Take clipping into account. */
1212 if (s->gc->clip_mask == Rect)
1214 real_x = max (real_x, s->gc->clip_rectangle.left);
1215 real_y = max (real_y, s->gc->clip_rectangle.top);
1216 real_w = min (real_w, s->gc->clip_rectangle.right
1217 - s->gc->clip_rectangle.left);
1218 real_h = min (real_h, s->gc->clip_rectangle.bottom
1219 - s->gc->clip_rectangle.top);
1221 #endif
1222 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1223 real_w, real_h);
1227 /* Draw the background of glyph_string S. If S->background_filled_p
1228 is non-zero don't draw it. FORCE_P non-zero means draw the
1229 background even if it wouldn't be drawn normally. This is used
1230 when a string preceding S draws into the background of S, or S
1231 contains the first component of a composition. */
1233 static void
1234 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1236 /* Nothing to do if background has already been drawn or if it
1237 shouldn't be drawn in the first place. */
1238 if (!s->background_filled_p)
1240 int box_line_width = max (s->face->box_line_width, 0);
1242 #if 0 /* TODO: stipple */
1243 if (s->stippled_p)
1245 /* Fill background with a stipple pattern. */
1246 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1247 XFillRectangle (s->display, FRAME_W32_WINDOW (s->f), s->gc, s->x,
1248 s->y + box_line_width,
1249 s->background_width,
1250 s->height - 2 * box_line_width);
1251 XSetFillStyle (s->display, s->gc, FillSolid);
1252 s->background_filled_p = true;
1254 else
1255 #endif
1256 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1257 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1258 font dimensions, since the actual glyphs might be
1259 much smaller. So in that case we always clear the
1260 rectangle with background color. */
1261 || FONT_TOO_HIGH (s->font)
1262 || s->font_not_found_p
1263 || s->extends_to_end_of_line_p
1264 || force_p)
1266 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1267 s->background_width,
1268 s->height - 2 * box_line_width);
1269 s->background_filled_p = true;
1275 /* Draw the foreground of glyph string S. */
1277 static void
1278 x_draw_glyph_string_foreground (struct glyph_string *s)
1280 int i, x;
1282 /* If first glyph of S has a left box line, start drawing the text
1283 of S to the right of that box line. */
1284 if (s->face->box != FACE_NO_BOX
1285 && s->first_glyph->left_box_line_p)
1286 x = s->x + eabs (s->face->box_line_width);
1287 else
1288 x = s->x;
1290 SetTextColor (s->hdc, s->gc->foreground);
1291 SetBkColor (s->hdc, s->gc->background);
1292 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1294 /* Draw characters of S as rectangles if S's font could not be
1295 loaded. */
1296 if (s->font_not_found_p)
1298 for (i = 0; i < s->nchars; ++i)
1300 struct glyph *g = s->first_glyph + i;
1302 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1303 s->height - 1);
1304 x += g->pixel_width;
1307 else
1309 struct font *font = s->font;
1310 int boff = font->baseline_offset;
1311 int y;
1312 HFONT old_font;
1314 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1316 if (font->vertical_centering)
1317 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1319 y = s->ybase - boff;
1320 if (s->for_overlaps
1321 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1322 font->driver->draw (s, 0, s->nchars, x, y, false);
1323 else
1324 font->driver->draw (s, 0, s->nchars, x, y, true);
1325 if (s->face->overstrike)
1326 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1328 SelectObject (s->hdc, old_font);
1332 /* Draw the foreground of composite glyph string S. */
1334 static void
1335 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1337 int i, j, x;
1338 struct font *font = s->font;
1340 /* If first glyph of S has a left box line, start drawing the text
1341 of S to the right of that box line. */
1342 if (s->face && s->face->box != FACE_NO_BOX
1343 && s->first_glyph->left_box_line_p)
1344 x = s->x + eabs (s->face->box_line_width);
1345 else
1346 x = s->x;
1348 /* S is a glyph string for a composition. S->cmp_from is the index
1349 of the first character drawn for glyphs of this composition.
1350 S->cmp_from == 0 means we are drawing the very first character of
1351 this composition. */
1353 SetTextColor (s->hdc, s->gc->foreground);
1354 SetBkColor (s->hdc, s->gc->background);
1355 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1357 /* Draw a rectangle for the composition if the font for the very
1358 first character of the composition could not be loaded. */
1359 if (s->font_not_found_p)
1361 if (s->cmp_from == 0)
1362 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1363 s->height - 1);
1365 else if (! s->first_glyph->u.cmp.automatic)
1367 int y = s->ybase;
1368 HFONT old_font;
1370 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1372 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1373 /* TAB in a composition means display glyphs with padding
1374 space on the left or right. */
1375 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1377 int xx = x + s->cmp->offsets[j * 2];
1378 int yy = y - s->cmp->offsets[j * 2 + 1];
1380 font->driver->draw (s, j, j + 1, xx, yy, false);
1381 if (s->face->overstrike)
1382 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1384 SelectObject (s->hdc, old_font);
1386 else
1388 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1389 Lisp_Object glyph;
1390 int y = s->ybase;
1391 int width = 0;
1392 HFONT old_font;
1394 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1396 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1398 glyph = LGSTRING_GLYPH (gstring, i);
1399 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1400 width += LGLYPH_WIDTH (glyph);
1401 else
1403 int xoff, yoff, wadjust;
1405 if (j < i)
1407 font->driver->draw (s, j, i, x, y, false);
1408 x += width;
1410 xoff = LGLYPH_XOFF (glyph);
1411 yoff = LGLYPH_YOFF (glyph);
1412 wadjust = LGLYPH_WADJUST (glyph);
1413 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1414 x += wadjust;
1415 j = i + 1;
1416 width = 0;
1419 if (j < i)
1420 font->driver->draw (s, j, i, x, y, false);
1422 SelectObject (s->hdc, old_font);
1427 /* Draw the foreground of glyph string S for glyphless characters. */
1429 static void
1430 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1432 struct glyph *glyph = s->first_glyph;
1433 XChar2b char2b[8];
1434 int x, i, j;
1435 bool with_background;
1437 /* If first glyph of S has a left box line, start drawing the text
1438 of S to the right of that box line. */
1439 if (s->face->box != FACE_NO_BOX
1440 && s->first_glyph->left_box_line_p)
1441 x = s->x + eabs (s->face->box_line_width);
1442 else
1443 x = s->x;
1445 SetTextColor (s->hdc, s->gc->foreground);
1446 SetBkColor (s->hdc, s->gc->background);
1447 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1449 s->char2b = char2b;
1450 with_background = ((s->for_overlaps
1451 || (s->background_filled_p && s->hl != DRAW_CURSOR))) == 0;
1452 for (i = 0; i < s->nchars; i++, glyph++)
1454 char buf[7], *str = NULL;
1455 int len = glyph->u.glyphless.len;
1457 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1459 if (len > 0
1460 && CHAR_TABLE_P (Vglyphless_char_display)
1461 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1462 >= 1))
1464 Lisp_Object acronym
1465 = (! glyph->u.glyphless.for_no_font
1466 ? CHAR_TABLE_REF (Vglyphless_char_display,
1467 glyph->u.glyphless.ch)
1468 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1469 if (STRINGP (acronym))
1470 str = SSDATA (acronym);
1473 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1475 sprintf ((char *) buf, "%0*X",
1476 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1477 (unsigned int) glyph->u.glyphless.ch);
1478 str = buf;
1481 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1482 w32_draw_rectangle (s->hdc, s->gc,
1483 x, s->ybase - glyph->ascent,
1484 glyph->pixel_width - 1,
1485 glyph->ascent + glyph->descent - 1);
1486 if (str)
1488 struct font *font = s->font;
1489 int upper_len = (len + 1) / 2;
1490 unsigned code;
1491 HFONT old_font;
1493 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1494 /* It is certain that all LEN characters in STR are ASCII. */
1495 for (j = 0; j < len; j++)
1497 code = font->driver->encode_char (font, str[j]);
1498 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1500 font->driver->draw (s, 0, upper_len,
1501 x + glyph->slice.glyphless.upper_xoff,
1502 s->ybase + glyph->slice.glyphless.upper_yoff,
1503 with_background);
1504 font->driver->draw (s, upper_len, len,
1505 x + glyph->slice.glyphless.lower_xoff,
1506 s->ybase + glyph->slice.glyphless.lower_yoff,
1507 with_background);
1508 SelectObject (s->hdc, old_font);
1510 x += glyph->pixel_width;
1515 /* Brightness beyond which a color won't have its highlight brightness
1516 boosted.
1518 Nominally, highlight colors for `3d' faces are calculated by
1519 brightening an object's color by a constant scale factor, but this
1520 doesn't yield good results for dark colors, so for colors whose
1521 brightness is less than this value (on a scale of 0-255) have to
1522 use an additional additive factor.
1524 The value here is set so that the default menu-bar/mode-line color
1525 (grey75) will not have its highlights changed at all. */
1526 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1529 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1530 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1531 If this produces the same color as COLOR, try a color where all RGB
1532 values have DELTA added. Return the allocated color in *COLOR.
1533 DISPLAY is the X display, CMAP is the colormap to operate on.
1534 Value is non-zero if successful. */
1536 static int
1537 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1538 double factor, int delta)
1540 COLORREF new;
1541 long bright;
1543 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1544 delta /= 256;
1546 /* Change RGB values by specified FACTOR. Avoid overflow! */
1547 eassert (factor >= 0);
1548 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1549 min (0xff, factor * GetGValue (*color)),
1550 min (0xff, factor * GetBValue (*color)));
1552 /* Calculate brightness of COLOR. */
1553 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1554 + GetBValue (*color)) / 6;
1556 /* We only boost colors that are darker than
1557 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1558 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1559 /* Make an additive adjustment to NEW, because it's dark enough so
1560 that scaling by FACTOR alone isn't enough. */
1562 /* How far below the limit this color is (0 - 1, 1 being darker). */
1563 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1564 /* The additive adjustment. */
1565 int min_delta = delta * dimness * factor / 2;
1567 if (factor < 1)
1568 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1569 max (0, min (0xff, min_delta - GetGValue (*color))),
1570 max (0, min (0xff, min_delta - GetBValue (*color))));
1571 else
1572 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1573 max (0, min (0xff, min_delta + GetGValue (*color))),
1574 max (0, min (0xff, min_delta + GetBValue (*color))));
1577 if (new == *color)
1578 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1579 max (0, min (0xff, delta + GetGValue (*color))),
1580 max (0, min (0xff, delta + GetBValue (*color))));
1582 /* TODO: Map to palette and retry with delta if same? */
1583 /* TODO: Free colors (if using palette)? */
1585 if (new == *color)
1586 return 0;
1588 *color = new;
1590 return 1;
1593 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1594 colors in COLORS. On W32, we no longer try to map colors to
1595 a palette. */
1596 void
1597 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1599 int i;
1601 for (i = 0; i < ncolors; i++)
1603 DWORD pixel = colors[i].pixel;
1604 /* Convert to a 16 bit value in range 0 - 0xffff. */
1605 colors[i].red = GetRValue (pixel) * 257;
1606 colors[i].green = GetGValue (pixel) * 257;
1607 colors[i].blue = GetBValue (pixel) * 257;
1611 void
1612 x_query_color (struct frame *f, XColor *color)
1614 x_query_colors (f, color, 1);
1618 /* Set up the foreground color for drawing relief lines of glyph
1619 string S. RELIEF is a pointer to a struct relief containing the GC
1620 with which lines will be drawn. Use a color that is FACTOR or
1621 DELTA lighter or darker than the relief's background which is found
1622 in S->f->output_data.x->relief_background. If such a color cannot
1623 be allocated, use DEFAULT_PIXEL, instead. */
1625 static void
1626 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1627 int delta, COLORREF default_pixel)
1629 XGCValues xgcv;
1630 struct w32_output *di = f->output_data.w32;
1631 unsigned long mask = GCForeground;
1632 COLORREF pixel;
1633 COLORREF background = di->relief_background;
1634 #if 0
1635 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1636 #endif
1638 /* TODO: Free colors (if using palette)? */
1640 /* Allocate new color. */
1641 xgcv.foreground = default_pixel;
1642 pixel = background;
1643 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1644 xgcv.foreground = relief->pixel = pixel;
1646 if (relief->gc == 0)
1648 #if 0 /* TODO: stipple */
1649 xgcv.stipple = dpyinfo->gray;
1650 mask |= GCStipple;
1651 #endif
1652 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1654 else
1655 XChangeGC (NULL, relief->gc, mask, &xgcv);
1659 /* Set up colors for the relief lines around glyph string S. */
1661 static void
1662 x_setup_relief_colors (struct glyph_string *s)
1664 struct w32_output *di = s->f->output_data.w32;
1665 COLORREF color;
1667 if (s->face->use_box_color_for_shadows_p)
1668 color = s->face->box_color;
1669 else if (s->first_glyph->type == IMAGE_GLYPH
1670 && s->img->pixmap
1671 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1672 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1673 else
1674 color = s->gc->background;
1676 if (di->white_relief.gc == 0
1677 || color != di->relief_background)
1679 di->relief_background = color;
1680 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1681 WHITE_PIX_DEFAULT (s->f));
1682 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1683 BLACK_PIX_DEFAULT (s->f));
1688 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1689 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1690 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1691 relief. LEFT_P non-zero means draw a relief on the left side of
1692 the rectangle. RIGHT_P non-zero means draw a relief on the right
1693 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1694 when drawing. */
1696 static void
1697 w32_draw_relief_rect (struct frame *f,
1698 int left_x, int top_y, int right_x, int bottom_y,
1699 int width, int raised_p,
1700 int top_p, int bot_p, int left_p, int right_p,
1701 RECT *clip_rect)
1703 int i;
1704 XGCValues gc;
1705 HDC hdc = get_frame_dc (f);
1707 if (raised_p)
1708 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1709 else
1710 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1712 w32_set_clip_rectangle (hdc, clip_rect);
1714 /* Top. */
1715 if (top_p)
1716 for (i = 0; i < width; ++i)
1717 w32_fill_area (f, hdc, gc.foreground,
1718 left_x + i * left_p, top_y + i,
1719 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1721 /* Left. */
1722 if (left_p)
1723 for (i = 0; i < width; ++i)
1724 w32_fill_area (f, hdc, gc.foreground,
1725 left_x + i, top_y + (i + 1) * top_p, 1,
1726 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1728 if (raised_p)
1729 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1730 else
1731 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1733 /* Bottom. */
1734 if (bot_p)
1735 for (i = 0; i < width; ++i)
1736 w32_fill_area (f, hdc, gc.foreground,
1737 left_x + i * left_p, bottom_y - i,
1738 right_x - left_x - i * (left_p + right_p) + 1, 1);
1740 /* Right. */
1741 if (right_p)
1742 for (i = 0; i < width; ++i)
1743 w32_fill_area (f, hdc, gc.foreground,
1744 right_x - i, top_y + (i + 1) * top_p, 1,
1745 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1747 w32_set_clip_rectangle (hdc, NULL);
1749 release_frame_dc (f, hdc);
1753 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1754 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1755 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1756 left side of the rectangle. RIGHT_P non-zero means draw a line
1757 on the right side of the rectangle. CLIP_RECT is the clipping
1758 rectangle to use when drawing. */
1760 static void
1761 w32_draw_box_rect (struct glyph_string *s,
1762 int left_x, int top_y, int right_x, int bottom_y, int width,
1763 bool left_p, bool right_p, RECT *clip_rect)
1765 w32_set_clip_rectangle (s->hdc, clip_rect);
1767 /* Top. */
1768 w32_fill_area (s->f, s->hdc, s->face->box_color,
1769 left_x, top_y, right_x - left_x + 1, width);
1771 /* Left. */
1772 if (left_p)
1774 w32_fill_area (s->f, s->hdc, s->face->box_color,
1775 left_x, top_y, width, bottom_y - top_y + 1);
1778 /* Bottom. */
1779 w32_fill_area (s->f, s->hdc, s->face->box_color,
1780 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1782 /* Right. */
1783 if (right_p)
1785 w32_fill_area (s->f, s->hdc, s->face->box_color,
1786 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1789 w32_set_clip_rectangle (s->hdc, NULL);
1793 /* Draw a box around glyph string S. */
1795 static void
1796 x_draw_glyph_string_box (struct glyph_string *s)
1798 int width, left_x, right_x, top_y, bottom_y, last_x;
1799 bool left_p, right_p, raised_p;
1800 struct glyph *last_glyph;
1801 RECT clip_rect;
1803 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1804 ? WINDOW_RIGHT_EDGE_X (s->w)
1805 : window_box_right (s->w, s->area));
1807 /* The glyph that may have a right box line. */
1808 last_glyph = (s->cmp || s->img
1809 ? s->first_glyph
1810 : s->first_glyph + s->nchars - 1);
1812 width = eabs (s->face->box_line_width);
1813 raised_p = s->face->box == FACE_RAISED_BOX;
1814 left_x = s->x;
1815 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1816 ? last_x - 1
1817 : min (last_x, s->x + s->background_width) - 1));
1818 top_y = s->y;
1819 bottom_y = top_y + s->height - 1;
1821 left_p = (s->first_glyph->left_box_line_p
1822 || (s->hl == DRAW_MOUSE_FACE
1823 && (s->prev == NULL
1824 || s->prev->hl != s->hl)));
1825 right_p = (last_glyph->right_box_line_p
1826 || (s->hl == DRAW_MOUSE_FACE
1827 && (s->next == NULL
1828 || s->next->hl != s->hl)));
1830 get_glyph_string_clip_rect (s, &clip_rect);
1832 if (s->face->box == FACE_SIMPLE_BOX)
1833 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1834 left_p, right_p, &clip_rect);
1835 else
1837 x_setup_relief_colors (s);
1838 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1839 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1844 /* Draw foreground of image glyph string S. */
1846 static void
1847 x_draw_image_foreground (struct glyph_string *s)
1849 int x = s->x;
1850 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1852 /* If first glyph of S has a left box line, start drawing it to the
1853 right of that line. */
1854 if (s->face->box != FACE_NO_BOX
1855 && s->first_glyph->left_box_line_p
1856 && s->slice.x == 0)
1857 x += eabs (s->face->box_line_width);
1859 /* If there is a margin around the image, adjust x- and y-position
1860 by that margin. */
1861 if (s->slice.x == 0)
1862 x += s->img->hmargin;
1863 if (s->slice.y == 0)
1864 y += s->img->vmargin;
1866 SaveDC (s->hdc);
1868 if (s->img->pixmap)
1870 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1871 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1872 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1873 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1874 SetBkColor (compat_hdc, RGB (255, 255, 255));
1875 SetTextColor (s->hdc, RGB (0, 0, 0));
1876 x_set_glyph_string_clipping (s);
1878 if (s->img->mask)
1880 HDC mask_dc = CreateCompatibleDC (s->hdc);
1881 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1883 SetTextColor (s->hdc, RGB (255, 255, 255));
1884 SetBkColor (s->hdc, RGB (0, 0, 0));
1886 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1887 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1888 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1889 mask_dc, s->slice.x, s->slice.y, SRCAND);
1890 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1891 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1893 SelectObject (mask_dc, mask_orig_obj);
1894 DeleteDC (mask_dc);
1896 else
1898 SetTextColor (s->hdc, s->gc->foreground);
1899 SetBkColor (s->hdc, s->gc->background);
1901 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1902 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1904 /* When the image has a mask, we can expect that at
1905 least part of a mouse highlight or a block cursor will
1906 be visible. If the image doesn't have a mask, make
1907 a block cursor visible by drawing a rectangle around
1908 the image. I believe it's looking better if we do
1909 nothing here for mouse-face. */
1910 if (s->hl == DRAW_CURSOR)
1912 int r = s->img->relief;
1913 if (r < 0) r = -r;
1914 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1915 s->slice.width + r*2 - 1,
1916 s->slice.height + r*2 - 1);
1920 w32_set_clip_rectangle (s->hdc, NULL);
1921 SelectObject (s->hdc, orig_brush);
1922 DeleteObject (fg_brush);
1923 SelectObject (compat_hdc, orig_obj);
1924 DeleteDC (compat_hdc);
1926 else
1927 w32_draw_rectangle (s->hdc, s->gc, x, y,
1928 s->slice.width - 1, s->slice.height - 1);
1930 RestoreDC (s->hdc ,-1);
1934 /* Draw a relief around the image glyph string S. */
1936 static void
1937 x_draw_image_relief (struct glyph_string *s)
1939 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
1940 int extra_x, extra_y;
1941 RECT r;
1942 int x = s->x;
1943 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1945 /* If first glyph of S has a left box line, start drawing it to the
1946 right of that line. */
1947 if (s->face->box != FACE_NO_BOX
1948 && s->first_glyph->left_box_line_p
1949 && s->slice.x == 0)
1950 x += eabs (s->face->box_line_width);
1952 /* If there is a margin around the image, adjust x- and y-position
1953 by that margin. */
1954 if (s->slice.x == 0)
1955 x += s->img->hmargin;
1956 if (s->slice.y == 0)
1957 y += s->img->vmargin;
1959 if (s->hl == DRAW_IMAGE_SUNKEN
1960 || s->hl == DRAW_IMAGE_RAISED)
1962 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief
1963 : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1964 raised_p = s->hl == DRAW_IMAGE_RAISED;
1966 else
1968 thick = eabs (s->img->relief);
1969 raised_p = s->img->relief > 0;
1972 x1 = x + s->slice.width - 1;
1973 y1 = y + s->slice.height - 1;
1975 extra_x = extra_y = 0;
1976 if (s->face->id == TOOL_BAR_FACE_ID)
1978 if (CONSP (Vtool_bar_button_margin)
1979 && INTEGERP (XCAR (Vtool_bar_button_margin))
1980 && INTEGERP (XCDR (Vtool_bar_button_margin)))
1982 extra_x = XINT (XCAR (Vtool_bar_button_margin));
1983 extra_y = XINT (XCDR (Vtool_bar_button_margin));
1985 else if (INTEGERP (Vtool_bar_button_margin))
1986 extra_x = extra_y = XINT (Vtool_bar_button_margin);
1989 top_p = bot_p = left_p = right_p = 0;
1991 if (s->slice.x == 0)
1992 x -= thick + extra_x, left_p = 1;
1993 if (s->slice.y == 0)
1994 y -= thick + extra_y, top_p = 1;
1995 if (s->slice.x + s->slice.width == s->img->width)
1996 x1 += thick + extra_x, right_p = 1;
1997 if (s->slice.y + s->slice.height == s->img->height)
1998 y1 += thick + extra_y, bot_p = 1;
2000 x_setup_relief_colors (s);
2001 get_glyph_string_clip_rect (s, &r);
2002 w32_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2003 top_p, bot_p, left_p, right_p, &r);
2007 /* Draw the foreground of image glyph string S to PIXMAP. */
2009 static void
2010 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
2012 HDC hdc = CreateCompatibleDC (s->hdc);
2013 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
2014 int x = 0;
2015 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2017 /* If first glyph of S has a left box line, start drawing it to the
2018 right of that line. */
2019 if (s->face->box != FACE_NO_BOX
2020 && s->first_glyph->left_box_line_p
2021 && s->slice.x == 0)
2022 x += eabs (s->face->box_line_width);
2024 /* If there is a margin around the image, adjust x- and y-position
2025 by that margin. */
2026 if (s->slice.x == 0)
2027 x += s->img->hmargin;
2028 if (s->slice.y == 0)
2029 y += s->img->vmargin;
2031 if (s->img->pixmap)
2033 HDC compat_hdc = CreateCompatibleDC (hdc);
2034 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2035 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
2036 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
2038 if (s->img->mask)
2040 HDC mask_dc = CreateCompatibleDC (hdc);
2041 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
2043 SetTextColor (hdc, RGB (0, 0, 0));
2044 SetBkColor (hdc, RGB (255, 255, 255));
2045 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2046 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2047 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2048 mask_dc, s->slice.x, s->slice.y, SRCAND);
2049 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2050 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2052 SelectObject (mask_dc, mask_orig_obj);
2053 DeleteDC (mask_dc);
2055 else
2057 SetTextColor (hdc, s->gc->foreground);
2058 SetBkColor (hdc, s->gc->background);
2060 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2061 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2063 /* When the image has a mask, we can expect that at
2064 least part of a mouse highlight or a block cursor will
2065 be visible. If the image doesn't have a mask, make
2066 a block cursor visible by drawing a rectangle around
2067 the image. I believe it's looking better if we do
2068 nothing here for mouse-face. */
2069 if (s->hl == DRAW_CURSOR)
2071 int r = s->img->relief;
2072 if (r < 0) r = -r;
2073 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2074 s->slice.width + r*2 - 1,
2075 s->slice.height + r*2 - 1);
2079 SelectObject (hdc, orig_brush);
2080 DeleteObject (fg_brush);
2081 SelectObject (compat_hdc, orig_obj);
2082 DeleteDC (compat_hdc);
2084 else
2085 w32_draw_rectangle (hdc, s->gc, x, y,
2086 s->slice.width - 1, s->slice.height - 1);
2088 SelectObject (hdc, orig_hdc_obj);
2089 DeleteDC (hdc);
2093 /* Draw part of the background of glyph string S. X, Y, W, and H
2094 give the rectangle to draw. */
2096 static void
2097 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2099 #if 0 /* TODO: stipple */
2100 if (s->stippled_p)
2102 /* Fill background with a stipple pattern. */
2103 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2104 XFillRectangle (s->display, FRAME_W32_WINDOW (s->f), s->gc, x, y, w, h);
2105 XSetFillStyle (s->display, s->gc, FillSolid);
2107 else
2108 #endif
2109 x_clear_glyph_string_rect (s, x, y, w, h);
2113 /* Draw image glyph string S.
2115 s->y
2116 s->x +-------------------------
2117 | s->face->box
2119 | +-------------------------
2120 | | s->img->vmargin
2122 | | +-------------------
2123 | | | the image
2127 static void
2128 x_draw_image_glyph_string (struct glyph_string *s)
2130 int x, y;
2131 int box_line_hwidth = eabs (s->face->box_line_width);
2132 int box_line_vwidth = max (s->face->box_line_width, 0);
2133 int height, width;
2134 HBITMAP pixmap = 0;
2136 height = s->height;
2137 if (s->slice.y == 0)
2138 height -= box_line_vwidth;
2139 if (s->slice.y + s->slice.height >= s->img->height)
2140 height -= box_line_vwidth;
2142 /* Fill background with face under the image. Do it only if row is
2143 taller than image or if image has a clip mask to reduce
2144 flickering. */
2145 s->stippled_p = s->face->stipple != 0;
2146 if (height > s->slice.height
2147 || s->img->hmargin
2148 || s->img->vmargin
2149 || s->img->mask
2150 || s->img->pixmap == 0
2151 || s->width != s->background_width)
2153 width = s->background_width;
2154 x = s->x;
2155 if (s->first_glyph->left_box_line_p
2156 && s->slice.x == 0)
2158 x += box_line_hwidth;
2159 width -= box_line_hwidth;
2162 y = s->y;
2163 if (s->slice.y == 0)
2164 y += box_line_vwidth;
2166 #if 0 /* TODO: figure out if we need to do this on Windows. */
2167 if (s->img->mask)
2169 /* Create a pixmap as large as the glyph string. Fill it
2170 with the background color. Copy the image to it, using
2171 its mask. Copy the temporary pixmap to the display. */
2172 Screen *screen = FRAME_X_SCREEN (s->f);
2173 int depth = DefaultDepthOfScreen (screen);
2175 /* Create a pixmap as large as the glyph string. */
2176 pixmap = XCreatePixmap (s->display, FRAME_W32_WINDOW (s->f),
2177 s->background_width,
2178 s->height, depth);
2180 /* Don't clip in the following because we're working on the
2181 pixmap. */
2182 XSetClipMask (s->display, s->gc, None);
2184 /* Fill the pixmap with the background color/stipple. */
2185 if (s->stippled_p)
2187 /* Fill background with a stipple pattern. */
2188 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2189 XFillRectangle (s->display, pixmap, s->gc,
2190 0, 0, s->background_width, s->height);
2191 XSetFillStyle (s->display, s->gc, FillSolid);
2193 else
2195 XGCValues xgcv;
2196 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2197 &xgcv);
2198 XSetForeground (s->display, s->gc, xgcv.background);
2199 XFillRectangle (s->display, pixmap, s->gc,
2200 0, 0, s->background_width, s->height);
2201 XSetForeground (s->display, s->gc, xgcv.foreground);
2204 else
2205 #endif
2206 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2208 s->background_filled_p = true;
2211 /* Draw the foreground. */
2212 if (pixmap != 0)
2214 w32_draw_image_foreground_1 (s, pixmap);
2215 x_set_glyph_string_clipping (s);
2217 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2218 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2219 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2220 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2222 SetTextColor (s->hdc, s->gc->foreground);
2223 SetBkColor (s->hdc, s->gc->background);
2224 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2225 compat_hdc, 0, 0, SRCCOPY);
2227 SelectObject (s->hdc, orig_brush);
2228 DeleteObject (fg_brush);
2229 SelectObject (compat_hdc, orig_obj);
2230 DeleteDC (compat_hdc);
2232 DeleteObject (pixmap);
2233 pixmap = 0;
2235 else
2236 x_draw_image_foreground (s);
2238 /* If we must draw a relief around the image, do it. */
2239 if (s->img->relief
2240 || s->hl == DRAW_IMAGE_RAISED
2241 || s->hl == DRAW_IMAGE_SUNKEN)
2242 x_draw_image_relief (s);
2246 /* Draw stretch glyph string S. */
2248 static void
2249 x_draw_stretch_glyph_string (struct glyph_string *s)
2251 eassert (s->first_glyph->type == STRETCH_GLYPH);
2253 if (s->hl == DRAW_CURSOR
2254 && !x_stretch_cursor_p)
2256 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2257 wide as the stretch glyph. */
2258 int width, background_width = s->background_width;
2259 int x = s->x;
2261 if (!s->row->reversed_p)
2263 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2265 if (x < left_x)
2267 background_width -= left_x - x;
2268 x = left_x;
2271 else
2273 /* In R2L rows, draw the cursor on the right edge of the
2274 stretch glyph. */
2275 int right_x = window_box_right (s->w, TEXT_AREA);
2277 if (x + background_width > right_x)
2278 background_width -= x - right_x;
2279 x += background_width;
2281 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2282 if (s->row->reversed_p)
2283 x -= width;
2285 /* Draw cursor. */
2286 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2288 /* Clear rest using the GC of the original non-cursor face. */
2289 if (width < background_width)
2291 XGCValues *gc = s->face->gc;
2292 int y = s->y;
2293 int w = background_width - width, h = s->height;
2294 RECT r;
2295 HDC hdc = s->hdc;
2297 if (!s->row->reversed_p)
2298 x += width;
2299 else
2300 x = s->x;
2301 if (s->row->mouse_face_p
2302 && cursor_in_mouse_face_p (s->w))
2304 x_set_mouse_face_gc (s);
2305 gc = s->gc;
2307 else
2308 gc = s->face->gc;
2310 get_glyph_string_clip_rect (s, &r);
2311 w32_set_clip_rectangle (hdc, &r);
2313 #if 0 /* TODO: stipple */
2314 if (s->face->stipple)
2316 /* Fill background with a stipple pattern. */
2317 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2318 XFillRectangle (s->display, FRAME_W32_WINDOW (s->f), gc, x, y, w, h);
2319 XSetFillStyle (s->display, gc, FillSolid);
2321 else
2322 #endif
2324 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2328 else if (!s->background_filled_p)
2330 int background_width = s->background_width;
2331 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2333 /* Don't draw into left margin, fringe or scrollbar area
2334 except for header line and mode line. */
2335 if (x < left_x && !s->row->mode_line_p)
2337 background_width -= left_x - x;
2338 x = left_x;
2340 if (background_width > 0)
2341 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2344 s->background_filled_p = true;
2348 /* Draw glyph string S. */
2350 static void
2351 x_draw_glyph_string (struct glyph_string *s)
2353 bool relief_drawn_p = 0;
2355 /* If S draws into the background of its successor, draw the
2356 background of the successor first so that S can draw into it.
2357 This makes S->next use XDrawString instead of XDrawImageString. */
2358 if (s->next && s->right_overhang && !s->for_overlaps)
2360 int width;
2361 struct glyph_string *next;
2362 for (width = 0, next = s->next;
2363 next && width < s->right_overhang;
2364 width += next->width, next = next->next)
2365 if (next->first_glyph->type != IMAGE_GLYPH)
2367 x_set_glyph_string_gc (next);
2368 x_set_glyph_string_clipping (next);
2369 if (next->first_glyph->type == STRETCH_GLYPH)
2370 x_draw_stretch_glyph_string (next);
2371 else
2372 x_draw_glyph_string_background (next, true);
2373 next->num_clips = 0;
2377 /* Set up S->gc, set clipping and draw S. */
2378 x_set_glyph_string_gc (s);
2380 /* Draw relief (if any) in advance for char/composition so that the
2381 glyph string can be drawn over it. */
2382 if (!s->for_overlaps
2383 && s->face->box != FACE_NO_BOX
2384 && (s->first_glyph->type == CHAR_GLYPH
2385 || s->first_glyph->type == COMPOSITE_GLYPH))
2388 x_set_glyph_string_clipping (s);
2389 x_draw_glyph_string_background (s, true);
2390 x_draw_glyph_string_box (s);
2391 x_set_glyph_string_clipping (s);
2392 relief_drawn_p = 1;
2394 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2395 && !s->clip_tail
2396 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2397 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2398 /* We must clip just this glyph. left_overhang part has already
2399 drawn when s->prev was drawn, and right_overhang part will be
2400 drawn later when s->next is drawn. */
2401 x_set_glyph_string_clipping_exactly (s, s);
2402 else
2403 x_set_glyph_string_clipping (s);
2405 switch (s->first_glyph->type)
2407 case IMAGE_GLYPH:
2408 x_draw_image_glyph_string (s);
2409 break;
2411 case STRETCH_GLYPH:
2412 x_draw_stretch_glyph_string (s);
2413 break;
2415 case CHAR_GLYPH:
2416 if (s->for_overlaps)
2417 s->background_filled_p = true;
2418 else
2419 x_draw_glyph_string_background (s, false);
2420 x_draw_glyph_string_foreground (s);
2421 break;
2423 case COMPOSITE_GLYPH:
2424 if (s->for_overlaps || (s->cmp_from > 0
2425 && ! s->first_glyph->u.cmp.automatic))
2426 s->background_filled_p = true;
2427 else
2428 x_draw_glyph_string_background (s, true);
2429 x_draw_composite_glyph_string_foreground (s);
2430 break;
2432 case GLYPHLESS_GLYPH:
2433 if (s->for_overlaps)
2434 s->background_filled_p = true;
2435 else
2436 x_draw_glyph_string_background (s, false);
2437 x_draw_glyphless_glyph_string_foreground (s);
2438 break;
2440 default:
2441 emacs_abort ();
2444 if (!s->for_overlaps)
2446 /* Draw underline. */
2447 if (s->face->underline_p)
2449 if (s->face->underline_type == FACE_UNDER_WAVE)
2451 COLORREF color;
2453 if (s->face->underline_defaulted_p)
2454 color = s->gc->foreground;
2455 else
2456 color = s->face->underline_color;
2458 w32_draw_underwave (s, color);
2460 else if (s->face->underline_type == FACE_UNDER_LINE)
2462 unsigned long thickness, position;
2463 int y;
2465 if (s->prev && s->prev->face->underline_p
2466 && s->prev->face->underline_type == FACE_UNDER_LINE)
2468 /* We use the same underline style as the previous one. */
2469 thickness = s->prev->underline_thickness;
2470 position = s->prev->underline_position;
2472 else
2474 struct font *font = font_for_underline_metrics (s);
2476 /* Get the underline thickness. Default is 1 pixel. */
2477 if (font && font->underline_thickness > 0)
2478 thickness = font->underline_thickness;
2479 else
2480 thickness = 1;
2481 if (x_underline_at_descent_line || !font)
2482 position = (s->height - thickness) - (s->ybase - s->y);
2483 else
2485 /* Get the underline position. This is the recommended
2486 vertical offset in pixels from the baseline to the top of
2487 the underline. This is a signed value according to the
2488 specs, and its default is
2490 ROUND ((maximum_descent) / 2), with
2491 ROUND (x) = floor (x + 0.5) */
2493 if (x_use_underline_position_properties
2494 && font->underline_position >= 0)
2495 position = font->underline_position;
2496 else
2497 position = (font->descent + 1) / 2;
2499 position = max (position, underline_minimum_offset);
2501 /* Check the sanity of thickness and position. We should
2502 avoid drawing underline out of the current line area. */
2503 if (s->y + s->height <= s->ybase + position)
2504 position = (s->height - 1) - (s->ybase - s->y);
2505 if (s->y + s->height < s->ybase + position + thickness)
2506 thickness = (s->y + s->height) - (s->ybase + position);
2507 s->underline_thickness = thickness;
2508 s->underline_position = position;
2509 y = s->ybase + position;
2510 if (s->face->underline_defaulted_p)
2512 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2513 y, s->width, 1);
2515 else
2517 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2518 y, s->width, 1);
2522 /* Draw overline. */
2523 if (s->face->overline_p)
2525 unsigned long dy = 0, h = 1;
2527 if (s->face->overline_color_defaulted_p)
2529 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2530 s->y + dy, s->width, h);
2532 else
2534 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2535 s->y + dy, s->width, h);
2539 /* Draw strike-through. */
2540 if (s->face->strike_through_p
2541 && !FONT_TEXTMETRIC (s->font).tmStruckOut)
2543 /* Y-coordinate and height of the glyph string's first
2544 glyph. We cannot use s->y and s->height because those
2545 could be larger if there are taller display elements
2546 (e.g., characters displayed with a larger font) in the
2547 same glyph row. */
2548 int glyph_y = s->ybase - s->first_glyph->ascent;
2549 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
2550 /* Strike-through width and offset from the glyph string's
2551 top edge. */
2552 unsigned long h = 1;
2553 unsigned long dy = (glyph_height - h) / 2;
2555 if (s->face->strike_through_color_defaulted_p)
2557 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2558 glyph_y + dy, s->width, h);
2560 else
2562 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2563 glyph_y + dy, s->width, h);
2567 /* Draw relief if not yet drawn. */
2568 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2569 x_draw_glyph_string_box (s);
2571 if (s->prev)
2573 struct glyph_string *prev;
2575 for (prev = s->prev; prev; prev = prev->prev)
2576 if (prev->hl != s->hl
2577 && prev->x + prev->width + prev->right_overhang > s->x)
2579 /* As prev was drawn while clipped to its own area, we
2580 must draw the right_overhang part using s->hl now. */
2581 enum draw_glyphs_face save = prev->hl;
2583 prev->hl = s->hl;
2584 x_set_glyph_string_gc (prev);
2585 x_set_glyph_string_clipping_exactly (s, prev);
2586 if (prev->first_glyph->type == CHAR_GLYPH)
2587 x_draw_glyph_string_foreground (prev);
2588 else
2589 x_draw_composite_glyph_string_foreground (prev);
2590 w32_set_clip_rectangle (prev->hdc, NULL);
2591 prev->hl = save;
2592 prev->num_clips = 0;
2596 if (s->next)
2598 struct glyph_string *next;
2600 for (next = s->next; next; next = next->next)
2601 if (next->hl != s->hl
2602 && next->x - next->left_overhang < s->x + s->width)
2604 /* As next will be drawn while clipped to its own area,
2605 we must draw the left_overhang part using s->hl now. */
2606 enum draw_glyphs_face save = next->hl;
2608 next->hl = s->hl;
2609 x_set_glyph_string_gc (next);
2610 x_set_glyph_string_clipping_exactly (s, next);
2611 if (next->first_glyph->type == CHAR_GLYPH)
2612 x_draw_glyph_string_foreground (next);
2613 else
2614 x_draw_composite_glyph_string_foreground (next);
2615 w32_set_clip_rectangle (next->hdc, NULL);
2616 next->hl = save;
2617 next->num_clips = 0;
2618 next->clip_head = s->next;
2623 /* Reset clipping. */
2624 w32_set_clip_rectangle (s->hdc, NULL);
2625 s->num_clips = 0;
2629 /* Shift display to make room for inserted glyphs. */
2631 static void
2632 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2633 int width, int height, int shift_by)
2635 HDC hdc;
2637 hdc = get_frame_dc (f);
2638 BitBlt (hdc, x + shift_by, y, width, height,
2639 hdc, x, y, SRCCOPY);
2641 release_frame_dc (f, hdc);
2645 /* Delete N glyphs at the nominal cursor position. Not implemented
2646 for X frames. */
2648 static void
2649 x_delete_glyphs (struct frame *f, register int n)
2651 if (! FRAME_W32_P (f))
2652 return;
2654 emacs_abort ();
2658 /* Clear entire frame. */
2660 static void
2661 x_clear_frame (struct frame *f)
2663 if (! FRAME_W32_P (f))
2664 return;
2666 /* Clearing the frame will erase any cursor, so mark them all as no
2667 longer visible. */
2668 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2670 block_input ();
2672 w32_clear_window (f);
2674 /* We have to clear the scroll bars, too. If we have changed
2675 colors or something like that, then they should be notified. */
2676 x_scroll_bar_clear (f);
2678 unblock_input ();
2682 /* Make audible bell. */
2684 static void
2685 w32_ring_bell (struct frame *f)
2687 block_input ();
2689 if (FRAME_W32_P (f) && visible_bell)
2691 int i;
2692 HWND hwnd = FRAME_W32_WINDOW (f);
2694 for (i = 0; i < 5; i++)
2696 FlashWindow (hwnd, TRUE);
2697 Sleep (10);
2699 FlashWindow (hwnd, FALSE);
2701 else
2702 w32_sys_ring_bell (f);
2704 unblock_input ();
2707 /***********************************************************************
2708 Line Dance
2709 ***********************************************************************/
2711 /* Perform an insert-lines or delete-lines operation, inserting N
2712 lines or deleting -N lines at vertical position VPOS. */
2714 static void
2715 x_ins_del_lines (struct frame *f, int vpos, int n)
2717 if (! FRAME_W32_P (f))
2718 return;
2720 emacs_abort ();
2724 /* Scroll part of the display as described by RUN. */
2726 static void
2727 x_scroll_run (struct window *w, struct run *run)
2729 struct frame *f = XFRAME (w->frame);
2730 int x, y, width, height, from_y, to_y, bottom_y;
2731 HWND hwnd = FRAME_W32_WINDOW (f);
2732 HRGN expect_dirty;
2734 /* Get frame-relative bounding box of the text display area of W,
2735 without mode lines. Include in this box the left and right
2736 fringes of W. */
2737 window_box (w, ANY_AREA, &x, &y, &width, &height);
2739 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2740 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2741 bottom_y = y + height;
2743 if (to_y < from_y)
2745 /* Scrolling up. Make sure we don't copy part of the mode
2746 line at the bottom. */
2747 if (from_y + run->height > bottom_y)
2748 height = bottom_y - from_y;
2749 else
2750 height = run->height;
2751 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2753 else
2755 /* Scrolling down. Make sure we don't copy over the mode line.
2756 at the bottom. */
2757 if (to_y + run->height > bottom_y)
2758 height = bottom_y - to_y;
2759 else
2760 height = run->height;
2761 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2764 block_input ();
2766 /* Cursor off. Will be switched on again in x_update_window_end. */
2767 x_clear_cursor (w);
2770 RECT from;
2771 RECT to;
2772 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2773 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2775 from.left = to.left = x;
2776 from.right = to.right = x + width;
2777 from.top = from_y;
2778 from.bottom = from_y + height;
2779 to.top = y;
2780 to.bottom = bottom_y;
2782 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2783 NULL, SW_INVALIDATE);
2785 /* Combine this with what we expect to be dirty. This covers the
2786 case where not all of the region we expect is actually dirty. */
2787 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2789 /* If the dirty region is not what we expected, redraw the entire frame. */
2790 if (!EqualRgn (combined, expect_dirty))
2791 SET_FRAME_GARBAGED (f);
2793 DeleteObject (dirty);
2794 DeleteObject (combined);
2797 unblock_input ();
2798 DeleteObject (expect_dirty);
2803 /***********************************************************************
2804 Exposure Events
2805 ***********************************************************************/
2807 static void
2808 frame_highlight (struct frame *f)
2810 x_update_cursor (f, 1);
2811 x_set_frame_alpha (f);
2814 static void
2815 frame_unhighlight (struct frame *f)
2817 x_update_cursor (f, 1);
2818 x_set_frame_alpha (f);
2821 /* The focus has changed. Update the frames as necessary to reflect
2822 the new situation. Note that we can't change the selected frame
2823 here, because the Lisp code we are interrupting might become confused.
2824 Each event gets marked with the frame in which it occurred, so the
2825 Lisp code can tell when the switch took place by examining the events. */
2827 static void
2828 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2830 struct frame *old_focus = dpyinfo->w32_focus_frame;
2832 if (frame != dpyinfo->w32_focus_frame)
2834 /* Set this before calling other routines, so that they see
2835 the correct value of w32_focus_frame. */
2836 dpyinfo->w32_focus_frame = frame;
2838 if (old_focus && old_focus->auto_lower)
2839 x_lower_frame (old_focus);
2841 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2842 dpyinfo->w32_pending_autoraise_frame = dpyinfo->w32_focus_frame;
2843 else
2844 dpyinfo->w32_pending_autoraise_frame = NULL;
2847 x_frame_rehighlight (dpyinfo);
2851 /* Handle FocusIn and FocusOut state changes for FRAME.
2852 If FRAME has focus and there exists more than one frame, puts
2853 a FOCUS_IN_EVENT into *BUFP. */
2855 static void
2856 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2857 struct frame *frame, struct input_event *bufp)
2859 if (type == WM_SETFOCUS)
2861 if (dpyinfo->w32_focus_event_frame != frame)
2863 x_new_focus_frame (dpyinfo, frame);
2864 dpyinfo->w32_focus_event_frame = frame;
2866 /* Don't stop displaying the initial startup message
2867 for a switch-frame event we don't need. */
2868 if (NILP (Vterminal_frame)
2869 && CONSP (Vframe_list)
2870 && !NILP (XCDR (Vframe_list)))
2872 bufp->arg = Qt;
2874 else
2876 bufp->arg = Qnil;
2879 bufp->kind = FOCUS_IN_EVENT;
2880 XSETFRAME (bufp->frame_or_window, frame);
2883 frame->output_data.x->focus_state |= state;
2885 /* TODO: IME focus? */
2887 else if (type == WM_KILLFOCUS)
2889 frame->output_data.x->focus_state &= ~state;
2891 if (dpyinfo->w32_focus_event_frame == frame)
2893 dpyinfo->w32_focus_event_frame = 0;
2894 x_new_focus_frame (dpyinfo, 0);
2896 bufp->kind = FOCUS_OUT_EVENT;
2897 XSETFRAME (bufp->frame_or_window, frame);
2900 /* TODO: IME focus? */
2905 /* The focus may have changed. Figure out if it is a real focus change,
2906 by checking both FocusIn/Out and Enter/LeaveNotify events.
2908 Returns FOCUS_IN_EVENT event in *BUFP. */
2910 static void
2911 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2912 struct input_event *bufp)
2914 struct frame *frame;
2916 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2917 if (! frame)
2918 return;
2920 /* On w32, this is only called from focus events, so no switch needed. */
2921 x_focus_changed (event->msg.message,
2922 (event->msg.message == WM_KILLFOCUS ?
2923 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2924 dpyinfo, frame, bufp);
2928 #if 0 /* unused */
2929 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2931 static void
2932 x_mouse_leave (struct w32_display_info *dpyinfo)
2934 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2936 #endif
2938 /* The focus has changed, or we have redirected a frame's focus to
2939 another frame (this happens when a frame uses a surrogate
2940 mini-buffer frame). Shift the highlight as appropriate.
2942 The FRAME argument doesn't necessarily have anything to do with which
2943 frame is being highlighted or un-highlighted; we only use it to find
2944 the appropriate X display info. */
2946 static void
2947 w32_frame_rehighlight (struct frame *frame)
2949 if (! FRAME_W32_P (frame))
2950 return;
2951 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
2954 static void
2955 x_frame_rehighlight (struct w32_display_info *dpyinfo)
2957 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2959 if (dpyinfo->w32_focus_frame)
2961 dpyinfo->x_highlight_frame
2962 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2963 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2964 : dpyinfo->w32_focus_frame);
2965 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2967 fset_focus_frame (dpyinfo->w32_focus_frame, Qnil);
2968 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2971 else
2972 dpyinfo->x_highlight_frame = 0;
2974 if (dpyinfo->x_highlight_frame != old_highlight)
2976 if (old_highlight)
2977 frame_unhighlight (old_highlight);
2978 if (dpyinfo->x_highlight_frame)
2979 frame_highlight (dpyinfo->x_highlight_frame);
2983 /* Keyboard processing - modifier keys, etc. */
2985 /* Convert a keysym to its name. */
2987 char *
2988 x_get_keysym_name (int keysym)
2990 /* Make static so we can always return it */
2991 static char value[100];
2993 block_input ();
2994 GetKeyNameText (keysym, value, 100);
2995 unblock_input ();
2997 return value;
3000 static int
3001 codepage_for_locale (LCID locale)
3003 char cp[20];
3005 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
3006 return atoi (cp);
3007 else
3008 return CP_ACP;
3012 /* Mouse clicks and mouse movement. Rah. */
3014 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
3015 the state in PUP. XBUTTON provides extra information for extended mouse
3016 button messages. Returns FALSE if unable to parse the message. */
3017 BOOL
3018 parse_button (int message, int xbutton, int * pbutton, int * pup)
3020 int button = 0;
3021 int up = 0;
3023 switch (message)
3025 case WM_LBUTTONDOWN:
3026 button = 0;
3027 up = 0;
3028 break;
3029 case WM_LBUTTONUP:
3030 button = 0;
3031 up = 1;
3032 break;
3033 case WM_MBUTTONDOWN:
3034 if (NILP (Vw32_swap_mouse_buttons))
3035 button = 1;
3036 else
3037 button = 2;
3038 up = 0;
3039 break;
3040 case WM_MBUTTONUP:
3041 if (NILP (Vw32_swap_mouse_buttons))
3042 button = 1;
3043 else
3044 button = 2;
3045 up = 1;
3046 break;
3047 case WM_RBUTTONDOWN:
3048 if (NILP (Vw32_swap_mouse_buttons))
3049 button = 2;
3050 else
3051 button = 1;
3052 up = 0;
3053 break;
3054 case WM_RBUTTONUP:
3055 if (NILP (Vw32_swap_mouse_buttons))
3056 button = 2;
3057 else
3058 button = 1;
3059 up = 1;
3060 break;
3061 case WM_XBUTTONDOWN:
3062 button = xbutton + 2;
3063 up = 0;
3064 break;
3065 case WM_XBUTTONUP:
3066 button = xbutton + 2;
3067 up = 1;
3068 break;
3069 default:
3070 return (FALSE);
3073 if (pup) *pup = up;
3074 if (pbutton) *pbutton = button;
3076 return (TRUE);
3080 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3082 If the event is a button press, then note that we have grabbed
3083 the mouse. */
3085 static Lisp_Object
3086 construct_mouse_click (struct input_event *result, W32Msg *msg, struct frame *f)
3088 int button;
3089 int up;
3091 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3092 &button, &up);
3094 /* Make the event type NO_EVENT; we'll change that when we decide
3095 otherwise. */
3096 result->kind = MOUSE_CLICK_EVENT;
3097 result->code = button;
3098 result->timestamp = msg->msg.time;
3099 result->modifiers = (msg->dwModifiers
3100 | (up
3101 ? up_modifier
3102 : down_modifier));
3104 XSETINT (result->x, LOWORD (msg->msg.lParam));
3105 XSETINT (result->y, HIWORD (msg->msg.lParam));
3106 XSETFRAME (result->frame_or_window, f);
3107 result->arg = Qnil;
3108 return Qnil;
3111 static Lisp_Object
3112 construct_mouse_wheel (struct input_event *result, W32Msg *msg, struct frame *f)
3114 POINT p;
3115 int delta;
3117 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3118 : WHEEL_EVENT;
3119 result->code = 0;
3120 result->timestamp = msg->msg.time;
3122 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3123 forward, away from the user (up); a negative value indicates that
3124 the wheel was rotated backward, toward the user (down). */
3125 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3127 /* The up and down modifiers indicate if the wheel was rotated up or
3128 down based on WHEEL_DELTA value. */
3129 result->modifiers = (msg->dwModifiers
3130 | ((delta < 0 ) ? down_modifier : up_modifier));
3132 /* With multiple monitors, we can legitimately get negative
3133 coordinates, so cast to short to interpret them correctly. */
3134 p.x = (short) LOWORD (msg->msg.lParam);
3135 p.y = (short) HIWORD (msg->msg.lParam);
3136 /* For the case that F's w32 window is not msg->msg.hwnd. */
3137 ScreenToClient (FRAME_W32_WINDOW (f), &p);
3138 XSETINT (result->x, p.x);
3139 XSETINT (result->y, p.y);
3140 XSETFRAME (result->frame_or_window, f);
3141 result->arg = Qnil;
3142 return Qnil;
3145 static Lisp_Object
3146 construct_drag_n_drop (struct input_event *result, W32Msg *msg, struct frame *f)
3148 Lisp_Object files;
3149 Lisp_Object frame;
3150 HDROP hdrop;
3151 POINT p;
3152 WORD num_files;
3153 wchar_t name_w[MAX_PATH];
3154 #ifdef NTGUI_UNICODE
3155 const int use_unicode = 1;
3156 #else
3157 int use_unicode = w32_unicode_filenames;
3158 char name_a[MAX_PATH];
3159 char file[MAX_UTF8_PATH];
3160 #endif
3161 int i;
3163 result->kind = DRAG_N_DROP_EVENT;
3164 result->code = 0;
3165 result->timestamp = msg->msg.time;
3166 result->modifiers = msg->dwModifiers;
3168 hdrop = (HDROP) msg->msg.wParam;
3169 DragQueryPoint (hdrop, &p);
3171 #if 0
3172 p.x = LOWORD (msg->msg.lParam);
3173 p.y = HIWORD (msg->msg.lParam);
3174 ScreenToClient (msg->msg.hwnd, &p);
3175 #endif
3177 XSETINT (result->x, p.x);
3178 XSETINT (result->y, p.y);
3180 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3181 files = Qnil;
3183 for (i = 0; i < num_files; i++)
3185 if (use_unicode)
3187 eassert (DragQueryFileW (hdrop, i, NULL, 0) < MAX_PATH);
3188 /* If DragQueryFile returns zero, it failed to fetch a file
3189 name. */
3190 if (DragQueryFileW (hdrop, i, name_w, MAX_PATH) == 0)
3191 continue;
3192 #ifdef NTGUI_UNICODE
3193 files = Fcons (from_unicode_buffer (name_w), files);
3194 #else
3195 filename_from_utf16 (name_w, file);
3196 files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
3197 #endif /* NTGUI_UNICODE */
3199 #ifndef NTGUI_UNICODE
3200 else
3202 eassert (DragQueryFileA (hdrop, i, NULL, 0) < MAX_PATH);
3203 if (DragQueryFileA (hdrop, i, name_a, MAX_PATH) == 0)
3204 continue;
3205 filename_from_ansi (name_a, file);
3206 files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
3208 #endif
3211 DragFinish (hdrop);
3213 XSETFRAME (frame, f);
3214 result->frame_or_window = frame;
3215 result->arg = files;
3216 return Qnil;
3220 #if HAVE_W32NOTIFY
3222 /* File event notifications (see w32notify.c). */
3224 Lisp_Object
3225 lispy_file_action (DWORD action)
3227 static char unknown_fmt[] = "unknown-action(%d)";
3228 Lisp_Object retval;
3230 switch (action)
3232 case FILE_ACTION_ADDED:
3233 retval = Qadded;
3234 break;
3235 case FILE_ACTION_REMOVED:
3236 retval = Qremoved;
3237 break;
3238 case FILE_ACTION_MODIFIED:
3239 retval = Qmodified;
3240 break;
3241 case FILE_ACTION_RENAMED_OLD_NAME:
3242 retval = Qrenamed_from;
3243 break;
3244 case FILE_ACTION_RENAMED_NEW_NAME:
3245 retval = Qrenamed_to;
3246 break;
3247 default:
3249 char buf[sizeof(unknown_fmt) - 1 + INT_STRLEN_BOUND (DWORD)];
3251 sprintf (buf, unknown_fmt, action);
3252 retval = intern (buf);
3254 break;
3257 return retval;
3260 #ifdef WINDOWSNT
3261 /* Put file notifications into the Emacs input event queue. This
3262 function runs when the WM_EMACS_FILENOTIFY message arrives from a
3263 watcher thread. */
3264 static void
3265 queue_notifications (struct input_event *event, W32Msg *msg, struct frame *f,
3266 int *evcount)
3268 struct notifications_set *ns = NULL;
3269 Lisp_Object frame;
3270 int done = 0;
3272 /* We cannot process notification before Emacs is fully initialized,
3273 since we need the UTF-16LE coding-system to be set up. */
3274 if (!initialized)
3275 return;
3277 XSETFRAME (frame, f);
3279 while (!done)
3281 ns = NULL;
3283 /* Find out if there is a record available in the linked list of
3284 notifications sets. If so, unlink the set from the linked
3285 list. Use critical section. */
3286 enter_crit ();
3287 if (notifications_set_head->next != notifications_set_head)
3289 ns = notifications_set_head->next;
3290 ns->prev->next = ns->next;
3291 ns->next->prev = ns->prev;
3293 else
3294 done = 1;
3295 leave_crit();
3297 if (ns)
3299 BYTE *p = ns->notifications;
3300 FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
3301 const DWORD min_size
3302 = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
3303 DWORD info_size = ns->size;
3304 Lisp_Object cs = Qutf_16le;
3305 Lisp_Object obj = w32_get_watch_object (ns->desc);
3307 /* notifications size could be zero when the buffer of
3308 notifications overflowed on the OS level, or when the
3309 directory being watched was itself deleted. Do nothing in
3310 that case. */
3311 if (info_size
3312 && !NILP (obj) && CONSP (obj))
3314 Lisp_Object callback = XCDR (obj);
3316 while (info_size >= min_size)
3318 Lisp_Object utf_16_fn
3319 = make_unibyte_string ((char *)fni->FileName,
3320 fni->FileNameLength);
3321 /* Note: mule-conf is preloaded, so utf-16le must
3322 already be defined at this point. */
3323 Lisp_Object fname
3324 = code_convert_string_norecord (utf_16_fn, cs, 0);
3325 Lisp_Object action = lispy_file_action (fni->Action);
3327 event->kind = FILE_NOTIFY_EVENT;
3328 event->timestamp = msg->msg.time;
3329 event->modifiers = 0;
3330 event->frame_or_window = callback;
3331 event->arg = list3 (make_pointer_integer (ns->desc),
3332 action, fname);
3333 kbd_buffer_store_event (event);
3334 (*evcount)++;
3335 if (!fni->NextEntryOffset)
3336 break;
3337 p += fni->NextEntryOffset;
3338 fni = (PFILE_NOTIFY_INFORMATION)p;
3339 info_size -= fni->NextEntryOffset;
3342 /* Free this notifications set. */
3343 xfree (ns->notifications);
3344 xfree (ns);
3347 /* We've stuffed all the events ourselves, so w32_read_socket shouldn't. */
3348 event->kind = NO_EVENT;
3350 #endif /* WINDOWSNT */
3351 #endif /* HAVE_W32NOTIFY */
3354 /* Function to report a mouse movement to the mainstream Emacs code.
3355 The input handler calls this.
3357 We have received a mouse movement event, which is given in *event.
3358 If the mouse is over a different glyph than it was last time, tell
3359 the mainstream emacs code by setting mouse_moved. If not, ask for
3360 another motion event, so we can check again the next time it moves. */
3362 static int
3363 note_mouse_movement (struct frame *frame, MSG *msg)
3365 struct w32_display_info *dpyinfo;
3366 int mouse_x = LOWORD (msg->lParam);
3367 int mouse_y = HIWORD (msg->lParam);
3368 RECT *r;
3370 if (!FRAME_X_OUTPUT (frame))
3371 return 0;
3373 dpyinfo = FRAME_DISPLAY_INFO (frame);
3374 dpyinfo->last_mouse_movement_time = msg->time;
3375 dpyinfo->last_mouse_motion_frame = frame;
3376 dpyinfo->last_mouse_motion_x = mouse_x;
3377 dpyinfo->last_mouse_motion_y = mouse_y;
3379 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3381 frame->mouse_moved = true;
3382 dpyinfo->last_mouse_scroll_bar = NULL;
3383 note_mouse_highlight (frame, -1, -1);
3384 dpyinfo->last_mouse_glyph_frame = NULL;
3385 return 1;
3388 /* Has the mouse moved off the glyph it was on at the last sighting? */
3389 r = &dpyinfo->last_mouse_glyph;
3390 if (frame != dpyinfo->last_mouse_glyph_frame
3391 || mouse_x < r->left || mouse_x >= r->right
3392 || mouse_y < r->top || mouse_y >= r->bottom)
3394 frame->mouse_moved = true;
3395 dpyinfo->last_mouse_scroll_bar = NULL;
3396 note_mouse_highlight (frame, mouse_x, mouse_y);
3397 /* Remember the mouse position here, as w32_mouse_position only
3398 gets called when mouse tracking is enabled but we also need
3399 to keep track of the mouse for help_echo and highlighting at
3400 other times. */
3401 remember_mouse_glyph (frame, mouse_x, mouse_y, r);
3402 dpyinfo->last_mouse_glyph_frame = frame;
3403 return 1;
3406 return 0;
3410 /************************************************************************
3411 Mouse Face
3412 ************************************************************************/
3414 static struct scroll_bar *x_window_to_scroll_bar (Window, int);
3415 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3416 enum scroll_bar_part *,
3417 Lisp_Object *, Lisp_Object *,
3418 Time *);
3419 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3420 enum scroll_bar_part *,
3421 Lisp_Object *, Lisp_Object *,
3422 Time *);
3423 static void
3424 w32_define_cursor (Window window, Cursor cursor)
3426 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3429 /* Return the current position of the mouse.
3430 *fp should be a frame which indicates which display to ask about.
3432 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3433 and *part to the frame, window, and scroll bar part that the mouse
3434 is over. Set *x and *y to the portion and whole of the mouse's
3435 position on the scroll bar.
3437 If the mouse movement started elsewhere, set *fp to the frame the
3438 mouse is on, *bar_window to nil, and *x and *y to the character cell
3439 the mouse is over.
3441 Set *time to the server time-stamp for the time at which the mouse
3442 was at this position.
3444 Don't store anything if we don't have a valid set of values to report.
3446 This clears the mouse_moved flag, so we can wait for the next mouse
3447 movement. */
3449 static void
3450 w32_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3451 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3452 Time *time)
3454 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3456 block_input ();
3458 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3460 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3462 if (bar->horizontal)
3463 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3464 else
3465 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3467 else
3469 POINT pt;
3470 Lisp_Object frame, tail;
3471 struct frame *f1 = NULL;
3473 /* Clear the mouse-moved flag for every frame on this display. */
3474 FOR_EACH_FRAME (tail, frame)
3475 XFRAME (frame)->mouse_moved = false;
3477 dpyinfo->last_mouse_scroll_bar = NULL;
3479 GetCursorPos (&pt);
3481 /* Now we have a position on the root; find the innermost window
3482 containing the pointer. */
3484 /* If mouse was grabbed on a frame, give coords for that
3485 frame even if the mouse is now outside it. Otherwise
3486 check for window under mouse on one of our frames. */
3487 if (x_mouse_grabbed (dpyinfo))
3488 f1 = dpyinfo->last_mouse_frame;
3489 else
3491 HWND wfp = WindowFromPoint (pt);
3493 if (wfp)
3495 f1 = x_any_window_to_frame (dpyinfo, wfp);
3496 if (f1)
3498 HWND cwfp = ChildWindowFromPoint (wfp, pt);
3500 if (cwfp)
3502 struct frame *f2 = x_any_window_to_frame (dpyinfo, cwfp);
3504 /* If a child window was found, make sure that its
3505 frame is a child frame (Bug#26615, maybe). */
3506 if (f2 && FRAME_PARENT_FRAME (f2))
3507 f1 = f2;
3513 /* If not, is it one of our scroll bars? */
3514 if (! f1)
3516 struct scroll_bar *bar
3517 = x_window_to_scroll_bar (WindowFromPoint (pt), 2);
3519 if (bar)
3520 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3523 if (f1 == 0 && insist > 0)
3524 f1 = SELECTED_FRAME ();
3526 if (f1)
3528 /* Ok, we found a frame. Store all the values.
3529 last_mouse_glyph is a rectangle used to reduce the
3530 generation of mouse events. To not miss any motion
3531 events, we must divide the frame into rectangles of the
3532 size of the smallest character that could be displayed
3533 on it, i.e. into the same rectangles that matrices on
3534 the frame are divided into. */
3536 dpyinfo = FRAME_DISPLAY_INFO (f1);
3537 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3538 remember_mouse_glyph (f1, pt.x, pt.y, &dpyinfo->last_mouse_glyph);
3539 dpyinfo->last_mouse_glyph_frame = f1;
3541 *bar_window = Qnil;
3542 *part = scroll_bar_above_handle;
3543 *fp = f1;
3544 XSETINT (*x, pt.x);
3545 XSETINT (*y, pt.y);
3546 *time = dpyinfo->last_mouse_movement_time;
3551 unblock_input ();
3555 /***********************************************************************
3556 Tool-bars
3557 ***********************************************************************/
3559 /* Handle mouse button event on the tool-bar of frame F, at
3560 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
3561 or ButtonRelease. */
3563 static void
3564 w32_handle_tool_bar_click (struct frame *f, struct input_event *button_event)
3566 int x = XFASTINT (button_event->x);
3567 int y = XFASTINT (button_event->y);
3569 if (button_event->modifiers & down_modifier)
3570 handle_tool_bar_click (f, x, y, 1, 0);
3571 else
3572 handle_tool_bar_click (f, x, y, 0,
3573 button_event->modifiers & ~up_modifier);
3578 /***********************************************************************
3579 Scroll bars
3580 ***********************************************************************/
3582 /* Scroll bar support. */
3584 /* Given a window ID, find the struct scroll_bar which manages it
3585 vertically. This can be called in GC, so we have to make sure to
3586 strip off mark bits. */
3588 static struct scroll_bar *
3589 x_window_to_scroll_bar (Window window_id, int type)
3591 Lisp_Object tail, frame;
3593 FOR_EACH_FRAME (tail, frame)
3595 Lisp_Object bar, condemned;
3597 /* Scan this frame's scroll bar list for a scroll bar with the
3598 right window ID. */
3599 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3600 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3601 /* This trick allows us to search both the ordinary and
3602 condemned scroll bar lists with one loop. */
3603 ! NILP (bar) || (bar = condemned,
3604 condemned = Qnil,
3605 ! NILP (bar));
3606 bar = XSCROLL_BAR (bar)->next)
3607 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id
3608 && (type = 2
3609 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
3610 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
3611 return XSCROLL_BAR (bar);
3614 return 0;
3619 /* Set the thumb size and position of vertical scroll bar BAR. We are currently
3620 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3622 static void
3623 w32_set_scroll_bar_thumb (struct scroll_bar *bar,
3624 int portion, int position, int whole)
3626 Window w = SCROLL_BAR_W32_WINDOW (bar);
3627 /* We use the whole scroll-bar height in the calculations below, to
3628 avoid strange effects like scrolling backwards when just clicking
3629 on the handle (without moving it). */
3630 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height)
3631 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3632 int sb_page, sb_pos;
3633 BOOL draggingp = bar->dragging ? TRUE : FALSE;
3634 SCROLLINFO si;
3636 /* We used to change the nPage setting while dragging the handle,
3637 but that had very strange effects (such as scrolling backwards
3638 while dragging downwards).
3640 Now, we don't change the nPage setting while dragging unless we
3641 get near to the end of the buffer, in which case we often have to
3642 resize the handle to "go all the way". */
3644 if (draggingp)
3646 int near_bottom_p;
3647 block_input ();
3648 si.cbSize = sizeof (si);
3649 si.fMask = SIF_POS | SIF_PAGE;
3650 GetScrollInfo (w, SB_CTL, &si);
3651 near_bottom_p = si.nPos + si.nPage >= range;
3652 unblock_input ();
3653 if (!near_bottom_p)
3654 return;
3657 if (whole)
3659 /* Position scroll bar at rock bottom if the bottom of the
3660 buffer is visible. This avoids shrinking the thumb away
3661 to nothing if it is held at the bottom of the buffer. */
3662 if (position + portion >= whole && !draggingp)
3664 sb_page = range * (whole - position) / whole;
3665 sb_pos = range;
3667 else
3669 sb_pos = position * range / whole;
3670 sb_page = (min (portion, (whole - position)) * range) / whole;
3673 else
3675 sb_page = range;
3676 sb_pos = 0;
3679 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3681 block_input ();
3683 si.cbSize = sizeof (si);
3684 si.fMask = SIF_PAGE | SIF_POS;
3685 si.nPage = sb_page;
3686 si.nPos = sb_pos;
3688 SetScrollInfo (w, SB_CTL, &si, TRUE);
3690 unblock_input ();
3693 /* Set the thumb size and position of horizontal scroll bar BAR. We are currently
3694 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3696 static void
3697 w32_set_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3698 int portion, int position, int whole)
3700 Window w = SCROLL_BAR_W32_WINDOW (bar);
3701 SCROLLINFO si;
3703 block_input ();
3705 si.cbSize = sizeof (si);
3706 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
3707 si.nMin = 0;
3708 si.nMax = whole;
3709 /* Allow nPage to be one larger than nPos so we don't allow the scrolling
3710 of an already fully visible buffer. */
3711 si.nPage = min (portion, si.nMax) + 1;
3712 si.nPos = min (position, si.nMax);
3713 SetScrollInfo (w, SB_CTL, &si, TRUE);
3715 unblock_input ();
3719 /************************************************************************
3720 Scroll bars, general
3721 ************************************************************************/
3723 static HWND
3724 my_create_vscrollbar (struct frame * f, struct scroll_bar * bar)
3726 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3727 WM_EMACS_CREATEVSCROLLBAR, (WPARAM) f,
3728 (LPARAM) bar);
3731 static HWND
3732 my_create_hscrollbar (struct frame * f, struct scroll_bar * bar)
3734 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3735 WM_EMACS_CREATEHSCROLLBAR, (WPARAM) f,
3736 (LPARAM) bar);
3739 /*#define ATTACH_THREADS*/
3741 static BOOL
3742 my_show_window (struct frame *f, HWND hwnd, int how)
3744 #ifndef ATTACH_THREADS
3745 return SendMessageTimeout (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3746 (WPARAM) hwnd, (LPARAM) how, 0, 6000, NULL);
3747 #else
3748 return ShowWindow (hwnd, how);
3749 #endif
3752 static void
3753 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3754 int x, int y, int cx, int cy, UINT flags)
3756 #ifndef ATTACH_THREADS
3757 WINDOWPOS pos;
3758 pos.hwndInsertAfter = hwndAfter;
3759 pos.x = x;
3760 pos.y = y;
3761 pos.cx = cx;
3762 pos.cy = cy;
3763 pos.flags = flags;
3764 SendMessageTimeout (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0,
3765 0, 6000, NULL);
3766 #else
3767 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3768 #endif
3771 #if 0
3772 static void
3773 my_set_focus (struct frame * f, HWND hwnd)
3775 SendMessageTimeout (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3776 (WPARAM) hwnd, 0, 0, 6000, NULL);
3778 #endif
3780 static void
3781 my_set_foreground_window (HWND hwnd)
3783 SendMessageTimeout (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0,
3784 0, 6000, NULL);
3788 static void
3789 my_destroy_window (struct frame * f, HWND hwnd)
3791 SendMessageTimeout (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3792 (WPARAM) hwnd, 0, 0, 6000, NULL);
3795 static void
3796 my_bring_window_to_top (HWND hwnd)
3798 SendMessageTimeout (hwnd, WM_EMACS_BRINGTOTOP, (WPARAM) hwnd, 0,
3799 0, 6000, NULL);
3802 /* Create a scroll bar and return the scroll bar vector for it. W is
3803 the Emacs window on which to create the scroll bar. TOP, LEFT,
3804 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
3805 scroll bar. */
3807 static struct scroll_bar *
3808 x_scroll_bar_create (struct window *w, int left, int top, int width, int height, bool horizontal)
3810 struct frame *f = XFRAME (WINDOW_FRAME (w));
3811 HWND hwnd;
3812 SCROLLINFO si;
3813 struct scroll_bar *bar
3814 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, top, PVEC_OTHER);
3815 Lisp_Object barobj;
3817 block_input ();
3819 XSETWINDOW (bar->window, w);
3820 bar->top = top;
3821 bar->left = left;
3822 bar->width = width;
3823 bar->height = height;
3824 bar->start = 0;
3825 bar->end = 0;
3826 bar->dragging = 0;
3827 bar->horizontal = horizontal;
3829 /* Requires geometry to be set before call to create the real window */
3831 if (horizontal)
3832 hwnd = my_create_hscrollbar (f, bar);
3833 else
3834 hwnd = my_create_vscrollbar (f, bar);
3836 si.cbSize = sizeof (si);
3837 si.fMask = SIF_ALL;
3838 si.nMin = 0;
3839 if (horizontal)
3840 si.nMax = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width)
3841 + HORIZONTAL_SCROLL_BAR_MIN_HANDLE;
3842 else
3843 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3844 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3845 si.nPage = si.nMax;
3846 si.nPos = 0;
3848 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3850 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3852 /* Add bar to its frame's list of scroll bars. */
3853 bar->next = FRAME_SCROLL_BARS (f);
3854 bar->prev = Qnil;
3855 XSETVECTOR (barobj, bar);
3856 fset_scroll_bars (f, barobj);
3857 if (! NILP (bar->next))
3858 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3860 unblock_input ();
3862 return bar;
3866 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3867 nil. */
3869 static void
3870 x_scroll_bar_remove (struct scroll_bar *bar)
3872 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3874 block_input ();
3876 /* Destroy the window. */
3877 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3879 /* Dissociate this scroll bar from its window. */
3880 if (bar->horizontal)
3881 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
3882 else
3883 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
3885 unblock_input ();
3888 /* Set the handle of the vertical scroll bar for WINDOW to indicate that
3889 we are displaying PORTION characters out of a total of WHOLE
3890 characters, starting at POSITION. If WINDOW has no vertical scroll
3891 bar, create one. */
3892 static void
3893 w32_set_vertical_scroll_bar (struct window *w,
3894 int portion, int whole, int position)
3896 struct frame *f = XFRAME (w->frame);
3897 Lisp_Object barobj;
3898 struct scroll_bar *bar;
3899 int top, height, left, width;
3900 int window_y, window_height;
3902 /* Get window dimensions. */
3903 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
3904 top = window_y;
3905 height = window_height;
3907 /* Compute the left edge and the width of the scroll bar area. */
3908 left = WINDOW_SCROLL_BAR_AREA_X (w);
3909 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
3911 /* Does the scroll bar exist yet? */
3912 if (NILP (w->vertical_scroll_bar))
3914 HDC hdc;
3915 block_input ();
3916 if (width > 0 && height > 0)
3918 hdc = get_frame_dc (f);
3919 w32_clear_area (f, hdc, left, top, width, height);
3920 release_frame_dc (f, hdc);
3922 unblock_input ();
3924 bar = x_scroll_bar_create (w, left, top, width, height, false);
3926 else
3928 /* It may just need to be moved and resized. */
3929 HWND hwnd;
3931 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3932 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3934 /* If already correctly positioned, do nothing. */
3935 if (bar->left == left
3936 && bar->top == top
3937 && bar->width == width
3938 && bar->height == height)
3940 /* Redraw after clear_frame. */
3941 if (!my_show_window (f, hwnd, SW_NORMAL))
3942 InvalidateRect (hwnd, NULL, FALSE);
3944 else
3946 HDC hdc;
3947 SCROLLINFO si;
3949 block_input ();
3950 if (width && height)
3952 hdc = get_frame_dc (f);
3953 /* Since Windows scroll bars are smaller than the space reserved
3954 for them on the frame, we have to clear "under" them. */
3955 w32_clear_area (f, hdc, left, top, width, height);
3956 release_frame_dc (f, hdc);
3957 x_clear_under_internal_border (f);
3959 /* Make sure scroll bar is "visible" before moving, to ensure the
3960 area of the parent window now exposed will be refreshed. */
3961 my_show_window (f, hwnd, SW_HIDE);
3962 /** MoveWindow (hwnd, left, top, width, max (height, 1), TRUE); **/
3963 /* Try to not draw over child frames. */
3964 SetWindowPos (hwnd, HWND_BOTTOM, left, top, width, max (height, 1),
3965 SWP_FRAMECHANGED);
3967 si.cbSize = sizeof (si);
3968 si.fMask = SIF_RANGE;
3969 si.nMin = 0;
3970 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3971 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3973 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3975 my_show_window (f, hwnd, SW_NORMAL);
3976 /* InvalidateRect (w, NULL, FALSE); */
3978 /* Remember new settings. */
3979 bar->left = left;
3980 bar->top = top;
3981 bar->width = width;
3982 bar->height = height;
3984 unblock_input ();
3987 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3988 XSETVECTOR (barobj, bar);
3989 wset_vertical_scroll_bar (w, barobj);
3992 /* Set the handle of the horizontal scroll bar for WINDOW to indicate
3993 that we are displaying PORTION characters out of a total of WHOLE
3994 characters, starting at POSITION. If WINDOW has no horizontal scroll
3995 bar, create one. */
3996 static void
3997 w32_set_horizontal_scroll_bar (struct window *w,
3998 int portion, int whole, int position)
4000 struct frame *f = XFRAME (w->frame);
4001 Lisp_Object barobj;
4002 struct scroll_bar *bar;
4003 int top, height, left, width;
4004 int window_x, window_width;
4005 int clear_left = WINDOW_LEFT_EDGE_X (w);
4006 int clear_width = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
4008 /* Get window dimensions. */
4009 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
4010 left = window_x;
4011 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
4012 width = window_width;
4013 top = WINDOW_SCROLL_BAR_AREA_Y (w);
4015 /* Does the scroll bar exist yet? */
4016 if (NILP (w->horizontal_scroll_bar))
4018 HDC hdc;
4019 block_input ();
4020 if (width > 0 && height > 0)
4022 hdc = get_frame_dc (f);
4023 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
4024 release_frame_dc (f, hdc);
4026 unblock_input ();
4028 bar = x_scroll_bar_create (w, left, top, width, height, true);
4030 else
4032 /* It may just need to be moved and resized. */
4033 HWND hwnd;
4035 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4036 hwnd = SCROLL_BAR_W32_WINDOW (bar);
4038 /* If already correctly positioned, do nothing. */
4039 if (bar->left == left && bar->top == top
4040 && bar->width == width && bar->height == height)
4042 /* Redraw after clear_frame. */
4043 if (!my_show_window (f, hwnd, SW_NORMAL))
4044 InvalidateRect (hwnd, NULL, FALSE);
4046 else
4048 HDC hdc;
4049 SCROLLINFO si;
4051 block_input ();
4052 if (width && height)
4054 hdc = get_frame_dc (f);
4055 /* Since Windows scroll bars are smaller than the space reserved
4056 for them on the frame, we have to clear "under" them. */
4057 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
4058 release_frame_dc (f, hdc);
4059 x_clear_under_internal_border (f);
4061 /* Make sure scroll bar is "visible" before moving, to ensure the
4062 area of the parent window now exposed will be refreshed. */
4063 my_show_window (f, hwnd, SW_HIDE);
4064 /** MoveWindow (hwnd, left, top, width, max (height, 1), TRUE); **/
4065 /* Try to not draw over child frames. */
4066 SetWindowPos (hwnd, HWND_BOTTOM, left, top, max (width, 1), height,
4067 SWP_FRAMECHANGED);
4069 /* +++ SetScrollInfo +++ */
4070 si.cbSize = sizeof (si);
4071 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
4072 si.nMin = 0;
4073 si.nMax = whole;
4074 si.nPage = min (portion, si.nMax) + 1;
4075 si.nPos = min (position, si.nMax);
4076 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
4078 my_show_window (f, hwnd, SW_NORMAL);
4079 /* InvalidateRect (w, NULL, FALSE); */
4081 /* Remember new settings. */
4082 bar->left = left;
4083 bar->top = top;
4084 bar->width = width;
4085 bar->height = height;
4087 unblock_input ();
4091 w32_set_horizontal_scroll_bar_thumb (bar, portion, position, whole);
4092 XSETVECTOR (barobj, bar);
4093 wset_horizontal_scroll_bar (w, barobj);
4097 /* The following three hooks are used when we're doing a thorough
4098 redisplay of the frame. We don't explicitly know which scroll bars
4099 are going to be deleted, because keeping track of when windows go
4100 away is a real pain - "Can you say set-window-configuration, boys
4101 and girls?" Instead, we just assert at the beginning of redisplay
4102 that *all* scroll bars are to be removed, and then save a scroll bar
4103 from the fiery pit when we actually redisplay its window. */
4105 /* Arrange for all scroll bars on FRAME to be removed at the next call
4106 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
4107 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
4109 static void
4110 w32_condemn_scroll_bars (struct frame *frame)
4112 if (!NILP (FRAME_SCROLL_BARS (frame)))
4114 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
4116 /* Prepend scrollbars to already condemned ones. */
4117 Lisp_Object last = FRAME_SCROLL_BARS (frame);
4119 while (!NILP (XSCROLL_BAR (last)->next))
4120 last = XSCROLL_BAR (last)->next;
4122 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
4123 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
4126 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
4127 fset_scroll_bars (frame, Qnil);
4132 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
4133 Note that WINDOW isn't necessarily condemned at all. */
4135 static void
4136 w32_redeem_scroll_bar (struct window *w)
4138 struct scroll_bar *bar;
4139 Lisp_Object barobj;
4140 struct frame *f;
4142 /* We can't redeem this window's scroll bar if it doesn't have one. */
4143 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
4144 emacs_abort ();
4146 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4148 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4149 /* Unlink it from the condemned list. */
4150 f = XFRAME (WINDOW_FRAME (w));
4151 if (NILP (bar->prev))
4153 /* If the prev pointer is nil, it must be the first in one of
4154 the lists. */
4155 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
4156 /* It's not condemned. Everything's fine. */
4157 goto horizontal;
4158 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4159 w->vertical_scroll_bar))
4160 fset_condemned_scroll_bars (f, bar->next);
4161 else
4162 /* If its prev pointer is nil, it must be at the front of
4163 one or the other! */
4164 emacs_abort ();
4166 else
4167 XSCROLL_BAR (bar->prev)->next = bar->next;
4169 if (! NILP (bar->next))
4170 XSCROLL_BAR (bar->next)->prev = bar->prev;
4172 bar->next = FRAME_SCROLL_BARS (f);
4173 bar->prev = Qnil;
4174 XSETVECTOR (barobj, bar);
4175 fset_scroll_bars (f, barobj);
4176 if (! NILP (bar->next))
4177 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4180 horizontal:
4181 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
4183 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4184 /* Unlink it from the condemned list. */
4185 f = XFRAME (WINDOW_FRAME (w));
4186 if (NILP (bar->prev))
4188 /* If the prev pointer is nil, it must be the first in one of
4189 the lists. */
4190 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
4191 /* It's not condemned. Everything's fine. */
4192 return;
4193 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4194 w->horizontal_scroll_bar))
4195 fset_condemned_scroll_bars (f, bar->next);
4196 else
4197 /* If its prev pointer is nil, it must be at the front of
4198 one or the other! */
4199 emacs_abort ();
4201 else
4202 XSCROLL_BAR (bar->prev)->next = bar->next;
4204 if (! NILP (bar->next))
4205 XSCROLL_BAR (bar->next)->prev = bar->prev;
4207 bar->next = FRAME_SCROLL_BARS (f);
4208 bar->prev = Qnil;
4209 XSETVECTOR (barobj, bar);
4210 fset_scroll_bars (f, barobj);
4211 if (! NILP (bar->next))
4212 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4216 /* Remove all scroll bars on FRAME that haven't been saved since the
4217 last call to `*condemn_scroll_bars_hook'. */
4219 static void
4220 w32_judge_scroll_bars (struct frame *f)
4222 Lisp_Object bar, next;
4224 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
4226 /* Clear out the condemned list now so we won't try to process any
4227 more events on the hapless scroll bars. */
4228 fset_condemned_scroll_bars (f, Qnil);
4230 for (; ! NILP (bar); bar = next)
4232 struct scroll_bar *b = XSCROLL_BAR (bar);
4234 x_scroll_bar_remove (b);
4236 next = b->next;
4237 b->next = b->prev = Qnil;
4240 /* Now there should be no references to the condemned scroll bars,
4241 and they should get garbage-collected. */
4244 /* Handle a mouse click on the vertical scroll bar BAR. If
4245 *EMACS_EVENT's kind is set to something other than NO_EVENT, it is
4246 enqueued.
4248 This may be called from a signal handler, so we have to ignore GC
4249 mark bits. */
4251 static int
4252 w32_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4253 struct input_event *emacs_event)
4255 if (! WINDOWP (bar->window))
4256 emacs_abort ();
4258 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
4259 emacs_event->code = 0;
4260 /* not really meaningful to distinguish up/down */
4261 emacs_event->modifiers = msg->dwModifiers;
4262 emacs_event->frame_or_window = bar->window;
4263 emacs_event->arg = Qnil;
4264 emacs_event->timestamp = msg->msg.time;
4267 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4268 int y;
4269 int dragging = bar->dragging;
4270 SCROLLINFO si;
4271 int sb_event = LOWORD (msg->msg.wParam);
4273 si.cbSize = sizeof (si);
4274 if (sb_event == SB_THUMBTRACK)
4275 si.fMask = SIF_TRACKPOS;
4276 else
4277 si.fMask = SIF_POS;
4279 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4280 if (sb_event == SB_THUMBTRACK)
4281 y = si.nTrackPos;
4282 else
4283 y = si.nPos;
4285 bar->dragging = 0;
4286 struct frame *f; /* Value is not used. */
4287 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4289 switch (sb_event)
4291 case SB_LINEDOWN:
4292 emacs_event->part = scroll_bar_down_arrow;
4293 break;
4294 case SB_LINEUP:
4295 emacs_event->part = scroll_bar_up_arrow;
4296 break;
4297 case SB_PAGEUP:
4298 emacs_event->part = scroll_bar_above_handle;
4299 break;
4300 case SB_PAGEDOWN:
4301 emacs_event->part = scroll_bar_below_handle;
4302 break;
4303 case SB_TOP:
4304 emacs_event->part = scroll_bar_handle;
4305 y = 0;
4306 break;
4307 case SB_BOTTOM:
4308 emacs_event->part = scroll_bar_handle;
4309 y = top_range;
4310 break;
4311 case SB_THUMBTRACK:
4312 case SB_THUMBPOSITION:
4313 bar->dragging = 1; /* ??????? */
4314 emacs_event->part = scroll_bar_handle;
4316 /* "Silently" update current position. */
4318 SCROLLINFO si;
4320 si.cbSize = sizeof (si);
4321 si.fMask = SIF_POS;
4322 si.nPos = y;
4323 /* Remember apparent position (we actually lag behind the real
4324 position, so don't set that directly). */
4325 last_scroll_bar_drag_pos = y;
4327 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4329 break;
4330 case SB_ENDSCROLL:
4331 /* If this is the end of a drag sequence, then reset the scroll
4332 handle size to normal and do a final redraw. Otherwise do
4333 nothing. */
4334 if (dragging)
4336 SCROLLINFO si;
4337 int start = bar->start;
4338 int end = bar->end;
4340 si.cbSize = sizeof (si);
4341 si.fMask = SIF_PAGE | SIF_POS;
4342 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
4343 si.nPos = last_scroll_bar_drag_pos;
4344 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4346 /* fall through */
4347 default:
4348 emacs_event->kind = NO_EVENT;
4349 return FALSE;
4352 XSETINT (emacs_event->x, y);
4353 XSETINT (emacs_event->y, top_range);
4355 return TRUE;
4359 /* Handle a mouse click on the horizontal scroll bar BAR. If
4360 *EMACS_EVENT's kind is set to something other than NO_EVENT, it is
4361 enqueued.
4363 This may be called from a signal handler, so we have to ignore GC
4364 mark bits. */
4366 static int
4367 w32_horizontal_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4368 struct input_event *emacs_event)
4370 if (! WINDOWP (bar->window))
4371 emacs_abort ();
4373 emacs_event->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4374 emacs_event->code = 0;
4375 /* not really meaningful to distinguish left/right */
4376 emacs_event->modifiers = msg->dwModifiers;
4377 emacs_event->frame_or_window = bar->window;
4378 emacs_event->arg = Qnil;
4379 emacs_event->timestamp = msg->msg.time;
4382 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4383 int x, y;
4384 int dragging = bar->dragging;
4385 SCROLLINFO si;
4386 int sb_event = LOWORD (msg->msg.wParam);
4388 si.cbSize = sizeof (si);
4389 if (sb_event == SB_THUMBTRACK)
4390 si.fMask = SIF_TRACKPOS | SIF_PAGE | SIF_RANGE;
4391 else
4392 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4394 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4395 if (sb_event == SB_THUMBTRACK)
4396 x = si.nTrackPos;
4397 else
4398 x = si.nPos;
4399 y = si.nMax - si.nPage;
4401 bar->dragging = 0;
4402 struct frame *f; /* Value is not used. */
4403 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4405 switch (sb_event)
4407 case SB_LINELEFT:
4408 emacs_event->part = scroll_bar_left_arrow;
4409 break;
4410 case SB_LINERIGHT:
4411 emacs_event->part = scroll_bar_right_arrow;
4412 break;
4413 case SB_PAGELEFT:
4414 emacs_event->part = scroll_bar_before_handle;
4415 break;
4416 case SB_PAGERIGHT:
4417 emacs_event->part = scroll_bar_after_handle;
4418 break;
4419 case SB_LEFT:
4420 emacs_event->part = scroll_bar_horizontal_handle;
4421 x = 0;
4422 break;
4423 case SB_RIGHT:
4424 emacs_event->part = scroll_bar_horizontal_handle;
4425 x = left_range;
4426 break;
4427 case SB_THUMBTRACK:
4428 case SB_THUMBPOSITION:
4429 bar->dragging = 1;
4430 emacs_event->part = scroll_bar_horizontal_handle;
4432 /* "Silently" update current position. */
4434 SCROLLINFO si;
4436 si.cbSize = sizeof (si);
4437 si.fMask = SIF_POS;
4438 si.nPos = min (x, XWINDOW (bar->window)->hscroll_whole - 1);
4439 /* Remember apparent position (we actually lag behind the real
4440 position, so don't set that directly). */
4441 last_scroll_bar_drag_pos = x;
4443 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4445 break;
4446 case SB_ENDSCROLL:
4447 /* If this is the end of a drag sequence, then reset the scroll
4448 handle size to normal and do a final redraw. Otherwise do
4449 nothing. */
4450 if (dragging)
4452 SCROLLINFO si;
4454 si.cbSize = sizeof (si);
4455 si.fMask = SIF_POS;
4456 si.nPos = min (last_scroll_bar_drag_pos,
4457 XWINDOW (bar->window)->hscroll_whole - 1);
4458 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4460 /* fall through */
4461 default:
4462 emacs_event->kind = NO_EVENT;
4463 return FALSE;
4466 XSETINT (emacs_event->x, x);
4467 XSETINT (emacs_event->y, y);
4469 return TRUE;
4473 /* Return information to the user about the current position of the mouse
4474 on the vertical scroll bar. */
4475 static void
4476 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4477 enum scroll_bar_part *part,
4478 Lisp_Object *x, Lisp_Object *y,
4479 Time *time)
4481 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4482 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4483 Window w = SCROLL_BAR_W32_WINDOW (bar);
4484 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4485 int pos;
4486 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4487 SCROLLINFO si;
4488 int sb_event = LOWORD (dpyinfo->last_mouse_scroll_bar_pos);
4490 block_input ();
4492 *fp = f;
4493 *bar_window = bar->window;
4495 si.cbSize = sizeof (si);
4496 if (sb_event == SB_THUMBTRACK)
4497 si.fMask = SIF_TRACKPOS | SIF_PAGE | SIF_RANGE;
4498 else
4499 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4501 GetScrollInfo (w, SB_CTL, &si);
4502 if (sb_event == SB_THUMBTRACK)
4503 pos = si.nTrackPos;
4504 else
4505 pos = si.nPos;
4506 top_range = si.nMax - si.nPage + 1;
4508 *part = scroll_bar_handle;
4509 if (sb_event == SB_LINEDOWN)
4510 pos++;
4512 XSETINT (*x, pos);
4513 XSETINT (*y, top_range);
4515 f->mouse_moved = false;
4516 dpyinfo->last_mouse_scroll_bar = NULL;
4518 *time = dpyinfo->last_mouse_movement_time;
4520 unblock_input ();
4523 /* Return information to the user about the current position of the mouse
4524 on the horizontal scroll bar. */
4525 static void
4526 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4527 enum scroll_bar_part *part,
4528 Lisp_Object *x, Lisp_Object *y,
4529 Time *time)
4531 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4532 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4533 Window w = SCROLL_BAR_W32_WINDOW (bar);
4534 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4535 int pos;
4536 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4537 SCROLLINFO si;
4538 int sb_event = LOWORD (dpyinfo->last_mouse_scroll_bar_pos);
4540 block_input ();
4542 *fp = f;
4543 *bar_window = bar->window;
4545 si.cbSize = sizeof (si);
4546 if (sb_event == SB_THUMBTRACK)
4547 si.fMask = SIF_TRACKPOS | SIF_PAGE | SIF_RANGE;
4548 else
4549 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4551 GetScrollInfo (w, SB_CTL, &si);
4552 if (sb_event == SB_THUMBTRACK)
4553 pos = si.nTrackPos;
4554 else
4555 pos = si.nPos;
4556 left_range = si.nMax - si.nPage + 1;
4558 *part = scroll_bar_handle;
4559 if (sb_event == SB_LINERIGHT)
4560 pos++;
4563 XSETINT (*y, pos);
4564 XSETINT (*x, left_range);
4566 f->mouse_moved = false;
4567 dpyinfo->last_mouse_scroll_bar = NULL;
4569 *time = dpyinfo->last_mouse_movement_time;
4571 unblock_input ();
4575 /* The screen has been cleared so we may have changed foreground or
4576 background colors, and the scroll bars may need to be redrawn.
4577 Clear out the scroll bars, and ask for expose events, so we can
4578 redraw them. */
4580 void
4581 x_scroll_bar_clear (struct frame *f)
4583 Lisp_Object bar;
4585 /* We can have scroll bars even if this is 0,
4586 if we just turned off scroll bar mode.
4587 But in that case we should not clear them. */
4588 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4589 || FRAME_HAS_HORIZONTAL_SCROLL_BARS (f))
4590 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4591 bar = XSCROLL_BAR (bar)->next)
4593 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4594 HDC hdc = GetDC (window);
4595 RECT rect;
4597 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4598 arranges to refresh the scroll bar if hidden. */
4599 my_show_window (f, window, SW_HIDE);
4601 GetClientRect (window, &rect);
4602 select_palette (f, hdc);
4603 w32_clear_rect (f, hdc, &rect);
4604 x_clear_under_internal_border (f);
4605 deselect_palette (f, hdc);
4607 ReleaseDC (window, hdc);
4611 /* The main W32 event-reading loop - w32_read_socket. */
4613 /* Record the last 100 characters stored
4614 to help debug the loss-of-chars-during-GC problem. */
4616 static int temp_index;
4617 static short temp_buffer[100];
4619 /* Temporarily store lead byte of DBCS input sequences. */
4620 static char dbcs_lead = 0;
4622 /* Read events coming from the W32 shell.
4623 This routine is called by the SIGIO handler.
4624 We return as soon as there are no more events to be read.
4626 For an overview of how Emacs input works on MS-Windows, see the
4627 commentary before w32_msg_pump in w32fns.c.
4629 We return the number of characters stored into the buffer,
4630 thus pretending to be `read'.
4632 Some of these messages are reposted back to the message queue since the
4633 system calls the windows proc directly in a context where we cannot return
4634 the data nor can we guarantee the state we are in. So if we dispatch them
4635 we will get into an infinite loop. To prevent this from ever happening we
4636 will set a variable to indicate we are in the read_socket call and indicate
4637 which message we are processing since the windows proc gets called
4638 recursively with different messages by the system.
4641 extern void menubar_selection_callback (struct frame *, void *);
4643 static int
4644 w32_read_socket (struct terminal *terminal,
4645 struct input_event *hold_quit)
4647 int count = 0;
4648 int check_visibility = 0;
4649 W32Msg msg;
4650 struct frame *f;
4651 struct w32_display_info *dpyinfo = &one_w32_display_info;
4652 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
4654 block_input ();
4656 /* So people can tell when we have read the available input. */
4657 input_signal_count++;
4659 /* Process any incoming thread messages. */
4660 drain_message_queue ();
4662 /* TODO: ghostscript integration. */
4663 while (get_next_msg (&msg, FALSE))
4665 struct input_event inev;
4666 int do_help = 0;
4668 /* DebPrint (("w32_read_socket: %s time:%u\n", */
4669 /* w32_name_of_message (msg.msg.message), */
4670 /* msg.msg.time)); */
4672 EVENT_INIT (inev);
4673 inev.kind = NO_EVENT;
4674 inev.arg = Qnil;
4676 switch (msg.msg.message)
4678 case WM_EMACS_PAINT:
4679 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4681 if (f)
4683 if (msg.rect.right == msg.rect.left ||
4684 msg.rect.bottom == msg.rect.top)
4686 /* We may get paint messages even though the client
4687 area is clipped - these are not expose events. */
4688 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4689 SDATA (f->name)));
4691 else if (FRAME_VISIBLE_P (f) != 1)
4693 bool iconified = FRAME_ICONIFIED_P (f);
4695 /* Definitely not obscured, so mark as visible. */
4696 SET_FRAME_VISIBLE (f, 1);
4697 SET_FRAME_ICONIFIED (f, false);
4698 SET_FRAME_GARBAGED (f);
4699 if (!f->output_data.w32->asked_for_visible)
4700 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4701 SDATA (f->name)));
4703 /* WM_PAINT serves as MapNotify as well, so report
4704 visibility changes properly. */
4705 if (iconified)
4707 inev.kind = DEICONIFY_EVENT;
4708 XSETFRAME (inev.frame_or_window, f);
4710 else if (!NILP (Vframe_list) && !NILP (XCDR (Vframe_list)))
4711 /* Force a redisplay sooner or later to update the
4712 frame titles in case this is the second frame. */
4713 record_asynch_buffer_change ();
4715 else
4717 /* Erase background again for safety. But don't do
4718 that if the frame's 'garbaged' flag is set, since
4719 in that case expose_frame will do nothing, and if
4720 the various redisplay flags happen to be unset,
4721 we are left with a blank frame. */
4722 if (!FRAME_GARBAGED_P (f) || FRAME_PARENT_FRAME (f))
4724 HDC hdc = get_frame_dc (f);
4726 w32_clear_rect (f, hdc, &msg.rect);
4727 release_frame_dc (f, hdc);
4729 expose_frame (f,
4730 msg.rect.left,
4731 msg.rect.top,
4732 msg.rect.right - msg.rect.left,
4733 msg.rect.bottom - msg.rect.top);
4734 x_clear_under_internal_border (f);
4737 break;
4739 case WM_INPUTLANGCHANGE:
4740 /* Generate a language change event. */
4741 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4743 /* lParam contains the input language ID in its low 16 bits.
4744 Use it to update our record of the keyboard codepage. */
4745 w32_keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4746 & 0xffff));
4748 if (f)
4750 inev.kind = LANGUAGE_CHANGE_EVENT;
4751 XSETFRAME (inev.frame_or_window, f);
4752 inev.code = w32_keyboard_codepage;
4753 inev.modifiers = msg.msg.lParam & 0xffff;
4755 break;
4757 case WM_KEYDOWN:
4758 case WM_SYSKEYDOWN:
4759 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4761 if (f && !FRAME_ICONIFIED_P (f))
4763 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4764 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4766 clear_mouse_face (hlinfo);
4767 hlinfo->mouse_face_hidden = true;
4770 if (temp_index == sizeof temp_buffer / sizeof (short))
4771 temp_index = 0;
4772 temp_buffer[temp_index++] = msg.msg.wParam;
4773 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4774 inev.code = msg.msg.wParam;
4775 inev.modifiers = msg.dwModifiers;
4776 XSETFRAME (inev.frame_or_window, f);
4777 inev.timestamp = msg.msg.time;
4779 break;
4781 case WM_UNICHAR:
4782 case WM_SYSCHAR:
4783 case WM_CHAR:
4784 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4786 if (f && !FRAME_ICONIFIED_P (f))
4788 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4789 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4791 clear_mouse_face (hlinfo);
4792 hlinfo->mouse_face_hidden = true;
4795 if (temp_index == sizeof temp_buffer / sizeof (short))
4796 temp_index = 0;
4797 temp_buffer[temp_index++] = msg.msg.wParam;
4799 inev.modifiers = msg.dwModifiers;
4800 XSETFRAME (inev.frame_or_window, f);
4801 inev.timestamp = msg.msg.time;
4803 if (msg.msg.message == WM_UNICHAR)
4805 inev.code = msg.msg.wParam;
4807 else if (msg.msg.wParam < 256)
4809 wchar_t code;
4810 char dbcs[2];
4811 dbcs[0] = 0;
4812 dbcs[1] = (char) msg.msg.wParam;
4814 if (dbcs_lead)
4816 dbcs[0] = dbcs_lead;
4817 dbcs_lead = 0;
4818 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4819 dbcs, 2, &code, 1))
4821 /* Garbage */
4822 DebPrint (("Invalid DBCS sequence: %d %d\n",
4823 dbcs[0], dbcs[1]));
4824 inev.kind = NO_EVENT;
4825 break;
4828 else if (IsDBCSLeadByteEx (w32_keyboard_codepage,
4829 (BYTE) msg.msg.wParam))
4831 dbcs_lead = (char) msg.msg.wParam;
4832 inev.kind = NO_EVENT;
4833 break;
4835 else
4837 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4838 &dbcs[1], 1, &code, 1))
4840 /* What to do with garbage? */
4841 DebPrint (("Invalid character: %d\n", dbcs[1]));
4842 inev.kind = NO_EVENT;
4843 break;
4846 inev.code = code;
4848 else
4850 /* Windows shouldn't generate WM_CHAR events above 0xFF
4851 in non-Unicode message handlers. */
4852 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4853 inev.kind = NO_EVENT;
4854 break;
4856 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4857 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4859 break;
4861 case WM_APPCOMMAND:
4862 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4864 if (f && !FRAME_ICONIFIED_P (f))
4866 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4867 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4869 clear_mouse_face (hlinfo);
4870 hlinfo->mouse_face_hidden = true;
4873 if (temp_index == sizeof temp_buffer / sizeof (short))
4874 temp_index = 0;
4875 temp_buffer[temp_index++] = msg.msg.wParam;
4876 inev.kind = MULTIMEDIA_KEY_EVENT;
4877 inev.code = GET_APPCOMMAND_LPARAM (msg.msg.lParam);
4878 inev.modifiers = msg.dwModifiers;
4879 XSETFRAME (inev.frame_or_window, f);
4880 inev.timestamp = msg.msg.time;
4882 break;
4884 case WM_MOUSEMOVE:
4885 /* Ignore non-movement. */
4887 int x = LOWORD (msg.msg.lParam);
4888 int y = HIWORD (msg.msg.lParam);
4889 if (x == last_mousemove_x && y == last_mousemove_y)
4890 break;
4891 last_mousemove_x = x;
4892 last_mousemove_y = y;
4895 previous_help_echo_string = help_echo_string;
4896 help_echo_string = Qnil;
4898 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4899 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4901 if (hlinfo->mouse_face_hidden)
4903 hlinfo->mouse_face_hidden = false;
4904 clear_mouse_face (hlinfo);
4907 if (f)
4909 /* Maybe generate SELECT_WINDOW_EVENTs for
4910 `mouse-autoselect-window'. */
4911 if (!NILP (Vmouse_autoselect_window)
4912 && (f == XFRAME (selected_frame)
4913 /* Switch to f from another frame iff
4914 focus_follows_mouse is set and f accepts
4915 focus. */
4916 || (!NILP (focus_follows_mouse)
4917 && !FRAME_NO_ACCEPT_FOCUS (f))))
4919 static Lisp_Object last_mouse_window;
4920 Lisp_Object window = window_from_coordinates
4921 (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0);
4923 /* Window will be selected only when it is not
4924 selected now and last mouse movement event was
4925 not in it. Minibuffer window will be selected
4926 only when it is active. */
4927 if (WINDOWP (window)
4928 && !EQ (window, last_mouse_window)
4929 && !EQ (window, selected_window))
4931 inev.kind = SELECT_WINDOW_EVENT;
4932 inev.frame_or_window = window;
4935 /* Remember the last window where we saw the mouse. */
4936 last_mouse_window = window;
4939 if (!note_mouse_movement (f, &msg.msg))
4940 help_echo_string = previous_help_echo_string;
4942 else
4944 /* If we move outside the frame, then we're
4945 certainly no longer on any text in the frame. */
4946 clear_mouse_face (hlinfo);
4949 /* If the contents of the global variable help_echo_string
4950 has changed, generate a HELP_EVENT. */
4951 #if 0 /* The below is an invalid comparison when CHECK_LISP_OBJECT_TYPE.
4952 But it was originally changed to this to fix a bug, so I have
4953 not removed it completely in case the bug is still there. */
4954 if (help_echo_string != previous_help_echo_string ||
4955 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4956 #else /* This is what xterm.c does. */
4957 if (!NILP (help_echo_string)
4958 || !NILP (previous_help_echo_string))
4959 do_help = 1;
4960 #endif
4961 break;
4963 case WM_LBUTTONDOWN:
4964 case WM_LBUTTONUP:
4965 case WM_MBUTTONDOWN:
4966 case WM_MBUTTONUP:
4967 case WM_RBUTTONDOWN:
4968 case WM_RBUTTONUP:
4969 case WM_XBUTTONDOWN:
4970 case WM_XBUTTONUP:
4972 /* If we decide we want to generate an event to be seen
4973 by the rest of Emacs, we put it here. */
4974 bool tool_bar_p = 0;
4975 int button;
4976 int up;
4978 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4979 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4981 if (f)
4983 construct_mouse_click (&inev, &msg, f);
4985 /* Is this in the tool-bar? */
4986 if (WINDOWP (f->tool_bar_window)
4987 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4989 Lisp_Object window;
4990 int x = XFASTINT (inev.x);
4991 int y = XFASTINT (inev.y);
4993 window = window_from_coordinates (f, x, y, 0, 1);
4995 if (EQ (window, f->tool_bar_window))
4997 w32_handle_tool_bar_click (f, &inev);
4998 tool_bar_p = 1;
5002 if (tool_bar_p
5003 || (dpyinfo->w32_focus_frame
5004 && f != dpyinfo->w32_focus_frame
5005 /* This does not help when the click happens in
5006 a grand-parent frame. */
5007 && !frame_ancestor_p (f, dpyinfo->w32_focus_frame)))
5008 inev.kind = NO_EVENT;
5011 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
5012 &button, &up);
5014 if (up)
5016 dpyinfo->grabbed &= ~ (1 << button);
5018 else
5020 dpyinfo->grabbed |= (1 << button);
5021 dpyinfo->last_mouse_frame = f;
5022 /* Ignore any mouse motion that happened
5023 before this event; any subsequent mouse-movement
5024 Emacs events should reflect only motion after
5025 the ButtonPress. */
5026 if (f != 0)
5028 f->mouse_moved = false;
5029 if (!tool_bar_p)
5030 f->last_tool_bar_item = -1;
5033 break;
5036 case WM_MOUSEWHEEL:
5037 case WM_MOUSEHWHEEL:
5039 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
5040 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
5042 if (f)
5044 if (!dpyinfo->w32_focus_frame
5045 || f == dpyinfo->w32_focus_frame)
5046 /* Emit an Emacs wheel-up/down event. */
5048 construct_mouse_wheel (&inev, &msg, f);
5050 /* Ignore any mouse motion that happened before this
5051 event; any subsequent mouse-movement Emacs events
5052 should reflect only motion after the ButtonPress. */
5053 f->mouse_moved = false;
5054 f->last_tool_bar_item = -1;
5055 dpyinfo->last_mouse_frame = f;
5057 else if (FRAME_NO_ACCEPT_FOCUS (f)
5058 && !x_mouse_grabbed (dpyinfo))
5060 Lisp_Object frame1 = get_frame_param (f, Qmouse_wheel_frame);
5061 struct frame *f1 = FRAMEP (frame1) ? XFRAME (frame1) : NULL;
5063 if (f1 && FRAME_LIVE_P (f1) && FRAME_W32_P (f1))
5065 construct_mouse_wheel (&inev, &msg, f1);
5066 f1->mouse_moved = false;
5067 f1->last_tool_bar_item = -1;
5068 dpyinfo->last_mouse_frame = f1;
5070 else
5071 dpyinfo->last_mouse_frame = f;
5073 else
5074 dpyinfo->last_mouse_frame = f;
5076 else
5077 dpyinfo->last_mouse_frame = f;
5079 break;
5081 case WM_DROPFILES:
5082 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5084 if (f)
5085 construct_drag_n_drop (&inev, &msg, f);
5086 break;
5088 case WM_HSCROLL:
5090 struct scroll_bar *bar =
5091 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 1);
5093 if (bar)
5094 w32_horizontal_scroll_bar_handle_click (bar, &msg, &inev);
5095 break;
5098 case WM_VSCROLL:
5100 struct scroll_bar *bar =
5101 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 0);
5103 if (bar)
5104 w32_scroll_bar_handle_click (bar, &msg, &inev);
5105 break;
5108 case WM_WINDOWPOSCHANGED:
5109 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5111 if (f)
5113 RECT rect;
5114 int /* rows, columns, */ width, height, text_width, text_height;
5116 if (GetClientRect (msg.msg.hwnd, &rect)
5117 /* GetClientRect evidently returns (0, 0, 0, 0) if
5118 called on a minimized frame. Such "dimensions"
5119 aren't useful anyway. */
5120 && !(rect.bottom == 0
5121 && rect.top == 0
5122 && rect.left == 0
5123 && rect.right == 0))
5125 height = rect.bottom - rect.top;
5126 width = rect.right - rect.left;
5127 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
5128 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
5129 /* rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); */
5130 /* columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); */
5132 /* TODO: Clip size to the screen dimensions. */
5134 /* Even if the number of character rows and columns
5135 has not changed, the font size may have changed,
5136 so we need to check the pixel dimensions as well. */
5138 if (width != FRAME_PIXEL_WIDTH (f)
5139 || height != FRAME_PIXEL_HEIGHT (f)
5140 || text_width != FRAME_TEXT_WIDTH (f)
5141 || text_height != FRAME_TEXT_HEIGHT (f))
5143 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
5144 SET_FRAME_GARBAGED (f);
5145 cancel_mouse_face (f);
5146 f->win_gravity = NorthWestGravity;
5151 check_visibility = 1;
5152 break;
5154 case WM_ACTIVATE:
5155 case WM_ACTIVATEAPP:
5156 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5157 if (f)
5159 /* Run the full-screen hook function also when we are
5160 being activated, to actually install the required
5161 size in effect, if the WAIT flag is set. This is
5162 because when the hook is run from x_set_fullscreen,
5163 the frame might not yet be visible, if that call is a
5164 result of make-frame, and in that case the hook just
5165 sets the WAIT flag. */
5166 if ((msg.msg.message == WM_WINDOWPOSCHANGED || msg.msg.wParam)
5167 && (f->want_fullscreen & FULLSCREEN_WAIT))
5169 /* Must set visibility right here since otherwise
5170 w32fullscreen_hook returns immediately. */
5171 SET_FRAME_VISIBLE (f, 1);
5172 w32fullscreen_hook (f);
5176 check_visibility = 1;
5177 break;
5179 case WM_MOVE:
5180 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5182 if (f && FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P(f))
5184 x_real_positions (f, &f->left_pos, &f->top_pos);
5185 inev.kind = MOVE_FRAME_EVENT;
5186 XSETFRAME (inev.frame_or_window, f);
5189 check_visibility = 1;
5190 break;
5192 case WM_SHOWWINDOW:
5193 /* wParam non-zero means Window is about to be shown, 0 means
5194 about to be hidden. */
5195 /* Redo the mouse-highlight after the tooltip has gone. */
5196 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
5198 tip_window = NULL;
5199 x_redo_mouse_highlight (dpyinfo);
5202 /* If window has been obscured or exposed by another window
5203 being maximized or minimized/restored, then recheck
5204 visibility of all frames. Direct changes to our own
5205 windows get handled by WM_SIZE. */
5206 #if 0
5207 if (msg.msg.lParam != 0)
5208 check_visibility = 1;
5209 else
5211 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5212 f->async_visible = msg.msg.wParam;
5214 #endif
5216 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5217 if (f)
5218 x_clear_under_internal_border (f);
5220 check_visibility = 1;
5221 break;
5223 case WM_SIZE:
5224 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5226 /* Inform lisp of whether frame has been iconified etc. */
5227 if (f)
5229 switch (msg.msg.wParam)
5231 case SIZE_MINIMIZED:
5232 SET_FRAME_VISIBLE (f, 0);
5233 SET_FRAME_ICONIFIED (f, true);
5235 inev.kind = ICONIFY_EVENT;
5236 XSETFRAME (inev.frame_or_window, f);
5237 break;
5239 case SIZE_MAXIMIZED:
5241 bool iconified = FRAME_ICONIFIED_P (f);
5242 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
5244 SET_FRAME_VISIBLE (f, 1);
5245 SET_FRAME_ICONIFIED (f, false);
5247 /* wait_reading_process_output will notice this
5248 and update the frame's display structures. */
5249 SET_FRAME_GARBAGED (f);
5251 if (iconified)
5253 int x, y;
5255 /* Reset top and left positions of the Window
5256 here since Windows sends a WM_MOVE message
5257 BEFORE telling us the Window is minimized
5258 when the Window is iconified, with 3000,3000
5259 as the co-ords. */
5260 x_real_positions (f, &x, &y);
5261 f->left_pos = x;
5262 f->top_pos = y;
5264 inev.kind = DEICONIFY_EVENT;
5265 XSETFRAME (inev.frame_or_window, f);
5267 else if (! NILP (Vframe_list)
5268 && ! NILP (XCDR (Vframe_list)))
5269 /* Force a redisplay sooner or later
5270 to update the frame titles
5271 in case this is the second frame. */
5272 record_asynch_buffer_change ();
5274 /* Windows can send us a SIZE_MAXIMIZED message even
5275 when fullscreen is fullboth. The following is a
5276 simple hack to check that based on the fact that
5277 only a maximized fullscreen frame should have both
5278 top/left outside the screen. */
5279 if (EQ (fullscreen, Qfullwidth) || EQ (fullscreen, Qfullheight)
5280 || NILP (fullscreen))
5282 int x, y;
5284 x_real_positions (f, &x, &y);
5285 if (x < 0 && y < 0)
5286 store_frame_param (f, Qfullscreen, Qmaximized);
5290 break;
5292 case SIZE_RESTORED:
5294 bool iconified = FRAME_ICONIFIED_P (f);
5296 /* The following was made unconditional in a
5297 pathetic attempt to fix bug#16967 in revision
5298 116716 but, considered counterproductive was made
5299 conditional again in revision 116727. martin */
5300 if (iconified)
5301 SET_FRAME_VISIBLE (f, 1);
5302 SET_FRAME_ICONIFIED (f, false);
5304 /* wait_reading_process_output will notice this
5305 and update the frame's display structures. */
5306 SET_FRAME_GARBAGED (f);
5308 if (iconified)
5310 /* Reset top and left positions of the Window
5311 here since Windows sends a WM_MOVE message
5312 BEFORE telling us the Window is minimized
5313 when the Window is iconified, with 3000,3000
5314 as the co-ords. */
5315 x_real_positions (f, &f->left_pos, &f->top_pos);
5317 inev.kind = DEICONIFY_EVENT;
5318 XSETFRAME (inev.frame_or_window, f);
5320 else if (! NILP (Vframe_list)
5321 && ! NILP (XCDR (Vframe_list)))
5322 /* Force a redisplay sooner or later
5323 to update the frame titles
5324 in case this is the second frame. */
5325 record_asynch_buffer_change ();
5328 if (EQ (get_frame_param (f, Qfullscreen), Qmaximized))
5329 store_frame_param (f, Qfullscreen, Qnil);
5331 break;
5335 if (f && !FRAME_ICONIFIED_P (f) && msg.msg.wParam != SIZE_MINIMIZED)
5337 RECT rect;
5338 int /* rows, columns, */ width, height, text_width, text_height;
5340 if (GetClientRect (msg.msg.hwnd, &rect)
5341 /* GetClientRect evidently returns (0, 0, 0, 0) if
5342 called on a minimized frame. Such "dimensions"
5343 aren't useful anyway. */
5344 && !(rect.bottom == 0
5345 && rect.top == 0
5346 && rect.left == 0
5347 && rect.right == 0))
5349 height = rect.bottom - rect.top;
5350 width = rect.right - rect.left;
5351 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
5352 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
5353 /* rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); */
5354 /* columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); */
5356 /* TODO: Clip size to the screen dimensions. */
5358 /* Even if the number of character rows and columns
5359 has not changed, the font size may have changed,
5360 so we need to check the pixel dimensions as well. */
5362 if (width != FRAME_PIXEL_WIDTH (f)
5363 || height != FRAME_PIXEL_HEIGHT (f)
5364 || text_width != FRAME_TEXT_WIDTH (f)
5365 || text_height != FRAME_TEXT_HEIGHT (f))
5367 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
5368 SET_FRAME_GARBAGED (f);
5369 cancel_mouse_face (f);
5370 f->win_gravity = NorthWestGravity;
5375 check_visibility = 1;
5376 break;
5378 case WM_MOUSELEAVE:
5379 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
5380 if (f)
5382 if (f == hlinfo->mouse_face_mouse_frame)
5384 /* If we move outside the frame, then we're
5385 certainly no longer on any text in the frame. */
5386 clear_mouse_face (hlinfo);
5387 hlinfo->mouse_face_mouse_frame = 0;
5390 /* Generate a nil HELP_EVENT to cancel a help-echo.
5391 Do it only if there's something to cancel.
5392 Otherwise, the startup message is cleared when
5393 the mouse leaves the frame. */
5394 if (any_help_event_p)
5395 do_help = -1;
5397 break;
5399 case WM_SETFOCUS:
5400 w32_detect_focus_change (dpyinfo, &msg, &inev);
5402 dpyinfo->grabbed = 0;
5403 check_visibility = 1;
5404 break;
5406 case WM_KILLFOCUS:
5407 w32_detect_focus_change (dpyinfo, &msg, &inev);
5408 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
5410 if (f)
5412 if (f == hlinfo->mouse_face_mouse_frame)
5414 /* If we move outside the frame, then we're
5415 certainly no longer on any text in the frame. */
5416 clear_mouse_face (hlinfo);
5417 hlinfo->mouse_face_mouse_frame = 0;
5420 /* Generate a nil HELP_EVENT to cancel a help-echo.
5421 Do it only if there's something to cancel.
5422 Otherwise, the startup message is cleared when
5423 the mouse leaves the frame. */
5424 if (any_help_event_p)
5425 do_help = -1;
5428 dpyinfo->grabbed = 0;
5429 check_visibility = 1;
5430 break;
5432 case WM_CLOSE:
5433 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5435 if (f)
5437 inev.kind = DELETE_WINDOW_EVENT;
5438 XSETFRAME (inev.frame_or_window, f);
5440 break;
5442 case WM_ENDSESSION:
5443 inev.kind = END_SESSION_EVENT;
5444 break;
5446 case WM_INITMENU:
5447 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5449 if (f)
5451 inev.kind = MENU_BAR_ACTIVATE_EVENT;
5452 XSETFRAME (inev.frame_or_window, f);
5454 break;
5456 case WM_COMMAND:
5457 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5459 if (f)
5461 menubar_selection_callback (f, (void *)msg.msg.wParam);
5464 check_visibility = 1;
5465 break;
5467 case WM_DISPLAYCHANGE:
5468 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5470 if (f)
5472 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
5474 dpyinfo->n_cbits = msg.msg.wParam;
5475 /* The new display could have a different resolution, in
5476 which case we must reconsider what fullscreen means.
5477 The following code is untested yet. */
5478 if (!NILP (fullscreen))
5480 x_set_fullscreen (f, fullscreen, fullscreen);
5481 w32fullscreen_hook (f);
5484 DebPrint (("display change: %d %d\n",
5485 (short) LOWORD (msg.msg.lParam),
5486 (short) HIWORD (msg.msg.lParam)));
5489 check_visibility = 1;
5490 break;
5492 #if HAVE_W32NOTIFY
5493 case WM_EMACS_FILENOTIFY:
5494 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5495 if (f)
5496 queue_notifications (&inev, &msg, f, &count);
5497 break;
5498 #endif
5500 default:
5501 /* Check for messages registered at runtime. */
5502 if (msg.msg.message == msh_mousewheel)
5504 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
5505 msg.msg.message = WM_MOUSEWHEEL;
5506 prepend_msg (&msg);
5508 break;
5511 if (inev.kind != NO_EVENT)
5513 kbd_buffer_store_event_hold (&inev, hold_quit);
5514 count++;
5517 if (do_help
5518 && !(hold_quit && hold_quit->kind != NO_EVENT))
5520 Lisp_Object frame;
5522 if (f)
5523 XSETFRAME (frame, f);
5524 else
5525 frame = Qnil;
5527 if (do_help > 0)
5529 if (NILP (help_echo_string))
5531 help_echo_object = help_echo_window = Qnil;
5532 help_echo_pos = -1;
5535 any_help_event_p = 1;
5536 gen_help_event (help_echo_string, frame, help_echo_window,
5537 help_echo_object, help_echo_pos);
5539 else
5541 help_echo_string = Qnil;
5542 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
5544 count++;
5548 /* If the focus was just given to an autoraising frame,
5549 raise it now. FIXME: handle more than one such frame. */
5550 if (dpyinfo->w32_pending_autoraise_frame)
5552 x_raise_frame (dpyinfo->w32_pending_autoraise_frame);
5553 dpyinfo->w32_pending_autoraise_frame = NULL;
5556 /* Check which frames are still visible, if we have enqueued any user
5557 events or been notified of events that may affect visibility. We
5558 do this here because there doesn't seem to be any direct
5559 notification from Windows that the visibility of a window has
5560 changed (at least, not in all cases). */
5561 if (count > 0 || check_visibility)
5563 Lisp_Object tail, frame;
5565 FOR_EACH_FRAME (tail, frame)
5567 struct frame *f = XFRAME (frame);
5568 /* The tooltip has been drawn already. Avoid the
5569 SET_FRAME_GARBAGED below. */
5570 if (EQ (frame, tip_frame))
5571 continue;
5573 /* Check "visible" frames and mark each as obscured or not.
5574 Note that visible is nonzero for unobscured and obscured
5575 frames, but zero for hidden and iconified frames. */
5576 if (FRAME_W32_P (f) && FRAME_VISIBLE_P (f))
5578 RECT clipbox;
5579 HDC hdc;
5580 bool obscured;
5582 enter_crit ();
5583 /* Query clipping rectangle for the entire window area
5584 (GetWindowDC), not just the client portion (GetDC).
5585 Otherwise, the scrollbars and menubar aren't counted as
5586 part of the visible area of the frame, and we may think
5587 the frame is obscured when really a scrollbar is still
5588 visible and gets WM_PAINT messages above. */
5589 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
5590 GetClipBox (hdc, &clipbox);
5591 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
5592 leave_crit ();
5594 obscured = FRAME_OBSCURED_P (f);
5596 if (clipbox.right == clipbox.left || clipbox.bottom == clipbox.top)
5598 /* Frame has become completely obscured so mark as such (we
5599 do this by setting visible to 2 so that FRAME_VISIBLE_P
5600 is still true, but redisplay will skip it). */
5601 SET_FRAME_VISIBLE (f, 2);
5603 if (!obscured)
5604 DebPrint (("frame %p (%s) obscured\n", f, SDATA (f->name)));
5606 else
5608 /* Frame is not obscured, so mark it as such. */
5609 SET_FRAME_VISIBLE (f, 1);
5611 if (obscured)
5613 SET_FRAME_GARBAGED (f);
5614 DebPrint (("obscured frame %p (%s) found to be visible\n",
5615 f, SDATA (f->name)));
5617 /* Force a redisplay sooner or later. */
5618 record_asynch_buffer_change ();
5625 unblock_input ();
5626 return count;
5631 /***********************************************************************
5632 Text Cursor
5633 ***********************************************************************/
5635 /* Set clipping for output in glyph row ROW. W is the window in which
5636 we operate. GC is the graphics context to set clipping in.
5638 ROW may be a text row or, e.g., a mode line. Text rows must be
5639 clipped to the interior of the window dedicated to text display,
5640 mode lines must be clipped to the whole window. */
5642 static void
5643 w32_clip_to_row (struct window *w, struct glyph_row *row,
5644 enum glyph_row_area area, HDC hdc)
5646 RECT clip_rect;
5647 int window_x, window_y, window_width;
5649 window_box (w, area, &window_x, &window_y, &window_width, 0);
5651 clip_rect.left = window_x;
5652 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5653 clip_rect.top = max (clip_rect.top, window_y);
5654 clip_rect.right = clip_rect.left + window_width;
5655 clip_rect.bottom = clip_rect.top + row->visible_height;
5657 w32_set_clip_rectangle (hdc, &clip_rect);
5661 /* Draw a hollow box cursor on window W in glyph row ROW. */
5663 static void
5664 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
5666 struct frame *f = XFRAME (WINDOW_FRAME (w));
5667 HDC hdc;
5668 RECT rect;
5669 int left, top, h;
5670 struct glyph *cursor_glyph;
5671 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5673 /* Get the glyph the cursor is on. If we can't tell because
5674 the current matrix is invalid or such, give up. */
5675 cursor_glyph = get_phys_cursor_glyph (w);
5676 if (cursor_glyph == NULL)
5678 DeleteObject (hb);
5679 return;
5682 /* Compute frame-relative coordinates for phys cursor. */
5683 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
5684 rect.left = left;
5685 /* When on R2L character, show cursor at the right edge of the
5686 glyph, unless the cursor box is as wide as the glyph or wider
5687 (the latter happens when x-stretch-cursor is non-nil). */
5688 if ((cursor_glyph->resolved_level & 1) != 0
5689 && cursor_glyph->pixel_width > w->phys_cursor_width)
5690 rect.left += cursor_glyph->pixel_width - w->phys_cursor_width;
5691 rect.top = top;
5692 rect.bottom = rect.top + h;
5693 rect.right = rect.left + w->phys_cursor_width;
5695 hdc = get_frame_dc (f);
5696 /* Set clipping, draw the rectangle, and reset clipping again. */
5697 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5698 FrameRect (hdc, &rect, hb);
5699 DeleteObject (hb);
5700 w32_set_clip_rectangle (hdc, NULL);
5701 release_frame_dc (f, hdc);
5705 /* Draw a bar cursor on window W in glyph row ROW.
5707 Implementation note: One would like to draw a bar cursor with an
5708 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5709 Unfortunately, I didn't find a font yet that has this property set.
5710 --gerd. */
5712 static void
5713 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
5714 int width, enum text_cursor_kinds kind)
5716 struct frame *f = XFRAME (w->frame);
5717 struct glyph *cursor_glyph;
5719 /* If cursor is out of bounds, don't draw garbage. This can happen
5720 in mini-buffer windows when switching between echo area glyphs
5721 and mini-buffer. */
5722 cursor_glyph = get_phys_cursor_glyph (w);
5723 if (cursor_glyph == NULL)
5724 return;
5726 /* If on an image, draw like a normal cursor. That's usually better
5727 visible than drawing a bar, esp. if the image is large so that
5728 the bar might not be in the window. */
5729 if (cursor_glyph->type == IMAGE_GLYPH)
5731 struct glyph_row *row;
5732 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5733 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5735 else
5737 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5738 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5739 int x;
5740 HDC hdc;
5742 /* If the glyph's background equals the color we normally draw
5743 the bar cursor in, the bar cursor in its normal color is
5744 invisible. Use the glyph's foreground color instead in this
5745 case, on the assumption that the glyph's colors are chosen so
5746 that the glyph is legible. */
5747 if (face->background == cursor_color)
5748 cursor_color = face->foreground;
5750 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5752 hdc = get_frame_dc (f);
5753 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5755 if (kind == BAR_CURSOR)
5757 if (width < 0)
5758 width = FRAME_CURSOR_WIDTH (f);
5759 width = min (cursor_glyph->pixel_width, width);
5761 w->phys_cursor_width = width;
5763 /* If the character under cursor is R2L, draw the bar cursor
5764 on the right of its glyph, rather than on the left. */
5765 if ((cursor_glyph->resolved_level & 1) != 0)
5766 x += cursor_glyph->pixel_width - width;
5768 w32_fill_area (f, hdc, cursor_color, x,
5769 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5770 width, row->height);
5772 else /* HBAR_CURSOR */
5774 int dummy_x, dummy_y, dummy_h;
5776 if (width < 0)
5777 width = row->height;
5779 width = min (row->height, width);
5781 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5782 &dummy_y, &dummy_h);
5783 if ((cursor_glyph->resolved_level & 1) != 0
5784 && cursor_glyph->pixel_width > w->phys_cursor_width)
5785 x += cursor_glyph->pixel_width - w->phys_cursor_width;
5786 w32_fill_area (f, hdc, cursor_color, x,
5787 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5788 row->height - width),
5789 w->phys_cursor_width, width);
5792 w32_set_clip_rectangle (hdc, NULL);
5793 release_frame_dc (f, hdc);
5798 /* RIF: Define cursor CURSOR on frame F. */
5800 static void
5801 w32_define_frame_cursor (struct frame *f, Cursor cursor)
5803 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5807 /* RIF: Clear area on frame F. */
5809 static void
5810 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
5812 HDC hdc;
5814 hdc = get_frame_dc (f);
5815 w32_clear_area (f, hdc, x, y, width, height);
5816 release_frame_dc (f, hdc);
5819 /* RIF: Draw or clear cursor on window W. */
5821 static void
5822 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
5823 int x, int y, enum text_cursor_kinds cursor_type,
5824 int cursor_width, bool on_p, bool active_p)
5826 if (on_p)
5828 /* If the user wants to use the system caret, make sure our own
5829 cursor remains invisible. */
5830 if (w32_use_visible_system_caret)
5832 /* Call to erase_phys_cursor here seems to use the
5833 wrong values of w->phys_cursor, as they have been
5834 overwritten before this function was called. */
5835 if (w->phys_cursor_type != NO_CURSOR)
5836 erase_phys_cursor (w);
5838 cursor_type = w->phys_cursor_type = NO_CURSOR;
5839 w->phys_cursor_width = -1;
5841 else
5843 w->phys_cursor_type = cursor_type;
5846 w->phys_cursor_on_p = true;
5848 /* If this is the active cursor, we need to track it with the
5849 system caret, so third party software like screen magnifiers
5850 and speech synthesizers can follow the cursor. */
5851 if (active_p)
5853 struct frame *f = XFRAME (WINDOW_FRAME (w));
5854 HWND hwnd = FRAME_W32_WINDOW (f);
5856 w32_system_caret_x
5857 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5858 w32_system_caret_y
5859 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5860 + glyph_row->ascent - w->phys_cursor_ascent);
5861 w32_system_caret_window = w;
5862 w32_system_caret_hdr_height = WINDOW_HEADER_LINE_HEIGHT (w);
5863 w32_system_caret_mode_height = WINDOW_MODE_LINE_HEIGHT (w);
5865 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
5867 /* If the size of the active cursor changed, destroy the old
5868 system caret. */
5869 if (w32_system_caret_hwnd
5870 && (w32_system_caret_height != w->phys_cursor_height))
5871 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5873 w32_system_caret_height = w->phys_cursor_height;
5875 /* Move the system caret. */
5876 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5879 if (glyph_row->exact_window_width_line_p
5880 && (glyph_row->reversed_p
5881 ? (w->phys_cursor.hpos < 0)
5882 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5884 glyph_row->cursor_in_fringe_p = true;
5885 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5886 return;
5889 switch (cursor_type)
5891 case HOLLOW_BOX_CURSOR:
5892 x_draw_hollow_cursor (w, glyph_row);
5893 break;
5895 case FILLED_BOX_CURSOR:
5896 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5897 break;
5899 case BAR_CURSOR:
5900 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5901 break;
5903 case HBAR_CURSOR:
5904 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5905 break;
5907 case NO_CURSOR:
5908 w->phys_cursor_width = 0;
5909 break;
5911 default:
5912 emacs_abort ();
5919 /* Icons. */
5921 bool
5922 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5924 HANDLE main_icon;
5925 HANDLE small_icon = NULL;
5927 if (FRAME_W32_WINDOW (f) == 0)
5928 return 1;
5930 if (NILP (icon))
5931 main_icon = LoadIcon (hinst, EMACS_CLASS);
5932 else if (STRINGP (icon))
5934 /* Load the main icon from the named file. */
5935 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5936 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5937 /* Try to load a small icon to go with it. */
5938 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5939 GetSystemMetrics (SM_CXSMICON),
5940 GetSystemMetrics (SM_CYSMICON),
5941 LR_LOADFROMFILE);
5943 else if (SYMBOLP (icon))
5945 LPCTSTR name;
5947 if (EQ (icon, intern ("application")))
5948 name = (LPCTSTR) IDI_APPLICATION;
5949 else if (EQ (icon, intern ("hand")))
5950 name = (LPCTSTR) IDI_HAND;
5951 else if (EQ (icon, intern ("question")))
5952 name = (LPCTSTR) IDI_QUESTION;
5953 else if (EQ (icon, intern ("exclamation")))
5954 name = (LPCTSTR) IDI_EXCLAMATION;
5955 else if (EQ (icon, intern ("asterisk")))
5956 name = (LPCTSTR) IDI_ASTERISK;
5957 else if (EQ (icon, intern ("winlogo")))
5958 name = (LPCTSTR) IDI_WINLOGO;
5959 else
5960 return 1;
5962 main_icon = LoadIcon (NULL, name);
5964 else
5965 return 1;
5967 if (main_icon == NULL)
5968 return 1;
5970 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5971 (LPARAM) main_icon);
5973 /* If there is a small icon that goes with it, set that too. */
5974 if (small_icon)
5975 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5976 (LPARAM) small_icon);
5978 return 0;
5982 /************************************************************************
5983 Handling X errors
5984 ************************************************************************/
5986 /* Display Error Handling functions not used on W32. Listing them here
5987 helps diff stay in step when comparing w32term.c with xterm.c.
5989 x_error_catcher (display, error)
5990 x_catch_errors (dpy)
5991 x_catch_errors_unwind (old_val)
5992 x_check_errors (dpy, format)
5993 x_fully_uncatch_errors ()
5994 x_had_errors_p (dpy)
5995 x_clear_errors (dpy)
5996 x_uncatch_errors (dpy, count)
5997 x_trace_wire ()
5998 x_connection_signal (signalnum)
5999 x_connection_closed (dpy, error_message)
6000 x_error_quitter (display, error)
6001 x_error_handler (display, error)
6002 x_io_error_quitter (display)
6007 /* Changing the font of the frame. */
6009 Lisp_Object
6010 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
6012 struct font *font = XFONT_OBJECT (font_object);
6013 int unit, font_ascent, font_descent;
6015 if (fontset < 0)
6016 fontset = fontset_from_font (font_object);
6017 FRAME_FONTSET (f) = fontset;
6018 if (FRAME_FONT (f) == font)
6019 /* This font is already set in frame F. There's nothing more to
6020 do. */
6021 return font_object;
6023 FRAME_FONT (f) = font;
6024 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
6025 FRAME_COLUMN_WIDTH (f) = unit = font->average_width;
6026 get_font_ascent_descent (font, &font_ascent, &font_descent);
6027 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
6029 /* Compute number of scrollbar columns. */
6030 unit = FRAME_COLUMN_WIDTH (f);
6031 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
6032 FRAME_CONFIG_SCROLL_BAR_COLS (f)
6033 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
6034 else
6036 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
6037 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) =
6038 FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
6041 /* Now make the frame display the given font. */
6042 if (FRAME_X_WINDOW (f) != 0)
6044 /* Don't change the size of a tip frame; there's no point in
6045 doing it because it's done in Fx_show_tip, and it leads to
6046 problems because the tip frame has no widget. */
6047 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
6048 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
6049 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
6050 false, Qfont);
6053 /* X version sets font of input methods here also. */
6055 return font_object;
6059 /***********************************************************************
6060 TODO: W32 Input Methods
6061 ***********************************************************************/
6062 /* Listing missing functions from xterm.c helps diff stay in step.
6064 xim_destroy_callback (xim, client_data, call_data)
6065 xim_open_dpy (dpyinfo, resource_name)
6066 struct xim_inst_t
6067 xim_instantiate_callback (display, client_data, call_data)
6068 xim_initialize (dpyinfo, resource_name)
6069 xim_close_dpy (dpyinfo)
6074 /* Calculate the absolute position in frame F
6075 from its current recorded position values and gravity. */
6077 static void
6078 x_calc_absolute_position (struct frame *f)
6080 int flags = f->size_hint_flags;
6082 /* The sum of the widths of the frame's left and right borders, and
6083 the sum of the heights of the frame's top and bottom borders (in
6084 pixels) drawn by Windows. */
6085 unsigned int left_right_borders_width, top_bottom_borders_height;
6087 /* Try to get the actual values of these two variables. We compute
6088 the border width (height) by subtracting the width (height) of
6089 the frame's client area from the width (height) of the frame's
6090 entire window. */
6091 WINDOWPLACEMENT wp = { 0 };
6092 RECT client_rect = { 0 };
6094 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
6095 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
6097 left_right_borders_width =
6098 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
6099 (client_rect.right - client_rect.left);
6101 top_bottom_borders_height =
6102 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
6103 (client_rect.bottom - client_rect.top);
6105 else
6107 /* Use sensible default values. */
6108 left_right_borders_width = 8;
6109 top_bottom_borders_height = 32;
6112 /* With multiple monitors, we can legitimately get negative
6113 coordinates (for monitors above or to the left of the primary
6114 monitor). Find the display origin to ensure negative positions
6115 are computed correctly (Bug#21173). */
6116 int display_left = 0;
6117 int display_top = 0;
6118 struct frame *p = FRAME_PARENT_FRAME (f);
6120 if (!p && flags & (XNegative | YNegative))
6122 Lisp_Object list;
6124 list = Fw32_display_monitor_attributes_list (Qnil);
6125 while (CONSP (list))
6127 Lisp_Object attributes = CAR(list);
6128 Lisp_Object geometry;
6129 Lisp_Object monitor_left, monitor_top;
6131 list = CDR(list);
6133 geometry = Fassoc (Qgeometry, attributes, Qnil);
6134 if (!NILP (geometry))
6136 monitor_left = Fnth (make_number (1), geometry);
6137 monitor_top = Fnth (make_number (2), geometry);
6139 display_left = min (display_left, XINT (monitor_left));
6140 display_top = min (display_top, XINT (monitor_top));
6145 /* Treat negative positions as relative to the rightmost bottommost
6146 position that fits on the screen or parent frame.
6148 I see no need for subtracting 1 from the border widths - is there
6149 any on the remaining platforms? Here these subtractions did put
6150 the last pixel line/column of a frame off-display when, for
6151 example, a (set-frame-parameter nil 'left '(- 0)) specification was
6152 used - martin 20017-05-05. */
6153 if (flags & XNegative)
6155 if (p)
6156 f->left_pos = (FRAME_PIXEL_WIDTH (p)
6157 - FRAME_PIXEL_WIDTH (f)
6158 + f->left_pos
6159 - left_right_borders_width);
6160 else
6161 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
6162 + display_left
6163 - FRAME_PIXEL_WIDTH (f)
6164 + f->left_pos
6165 - left_right_borders_width);
6168 if (flags & YNegative)
6170 if (p)
6171 f->top_pos = (FRAME_PIXEL_HEIGHT (p)
6172 - FRAME_PIXEL_HEIGHT (f)
6173 + f->top_pos
6174 - top_bottom_borders_height);
6175 else
6176 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
6177 + display_top
6178 - FRAME_PIXEL_HEIGHT (f)
6179 + f->top_pos
6180 - top_bottom_borders_height);
6183 /* The left_pos and top_pos are now relative to the top and left
6184 screen edges, so the flags should correspond. */
6185 f->size_hint_flags &= ~ (XNegative | YNegative);
6188 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
6189 to really change the position, and 0 when calling from
6190 x_make_frame_visible (in that case, XOFF and YOFF are the current
6191 position values). It is -1 when calling from x_set_frame_parameters,
6192 which means, do adjust for borders but don't change the gravity. */
6194 void
6195 x_set_offset (struct frame *f, register int xoff, register int yoff,
6196 int change_gravity)
6198 int modified_top, modified_left;
6200 if (change_gravity > 0)
6202 f->top_pos = yoff;
6203 f->left_pos = xoff;
6204 f->size_hint_flags &= ~ (XNegative | YNegative);
6205 if (xoff < 0)
6206 f->size_hint_flags |= XNegative;
6207 if (yoff < 0)
6208 f->size_hint_flags |= YNegative;
6209 f->win_gravity = NorthWestGravity;
6211 x_calc_absolute_position (f);
6213 block_input ();
6214 x_wm_set_size_hint (f, (long) 0, false);
6216 modified_left = f->left_pos;
6217 modified_top = f->top_pos;
6219 if (!FRAME_PARENT_FRAME (f))
6220 my_set_window_pos (FRAME_W32_WINDOW (f), NULL,
6221 modified_left, modified_top,
6222 0, 0,
6223 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
6224 else
6225 my_set_window_pos (FRAME_W32_WINDOW (f), HWND_TOP,
6226 modified_left, modified_top,
6227 0, 0,
6228 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
6229 unblock_input ();
6232 static void
6233 w32fullscreen_hook (struct frame *f)
6235 if (FRAME_VISIBLE_P (f))
6237 HWND hwnd = FRAME_W32_WINDOW(f);
6238 DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
6239 RECT rect;
6240 enum fullscreen_type prev_fsmode = FRAME_PREV_FSMODE (f);
6242 block_input();
6243 f->want_fullscreen &= ~FULLSCREEN_WAIT;
6245 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
6246 GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6248 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
6250 SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
6251 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6253 else if (FRAME_PREV_FSMODE (f) == FULLSCREEN_HEIGHT
6254 || FRAME_PREV_FSMODE (f) == FULLSCREEN_WIDTH)
6255 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6257 FRAME_PREV_FSMODE (f) = f->want_fullscreen;
6259 if (f->want_fullscreen == FULLSCREEN_NONE)
6260 ShowWindow (hwnd, SW_SHOWNORMAL);
6261 else if (f->want_fullscreen == FULLSCREEN_MAXIMIZED)
6263 if (prev_fsmode == FULLSCREEN_BOTH || prev_fsmode == FULLSCREEN_WIDTH
6264 || prev_fsmode == FULLSCREEN_HEIGHT)
6265 /* Make window normal since otherwise the subsequent
6266 maximization might fail in some cases. */
6267 ShowWindow (hwnd, SW_SHOWNORMAL);
6268 ShowWindow (hwnd, SW_MAXIMIZE);
6270 else if (f->want_fullscreen == FULLSCREEN_BOTH)
6272 int menu_bar_height = GetSystemMetrics (SM_CYMENU);
6274 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6275 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6276 SetWindowLong (hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
6277 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
6278 rect.right - rect.left, rect.bottom - rect.top,
6279 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
6280 change_frame_size
6281 (f, FRAME_PIXEL_TO_TEXT_WIDTH (f, rect.right - rect.left),
6282 FRAME_PIXEL_TO_TEXT_HEIGHT (f, (rect.bottom - rect.top
6283 - menu_bar_height)),
6284 0, 1, 0, 1);
6286 else
6288 ShowWindow (hwnd, SW_SHOWNORMAL);
6289 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6290 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6291 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
6292 rect.right - rect.left, rect.bottom - rect.top, 0);
6294 if (f->want_fullscreen == FULLSCREEN_WIDTH)
6296 int border_width = GetSystemMetrics (SM_CXFRAME);
6298 change_frame_size
6299 (f, (FRAME_PIXEL_TO_TEXT_WIDTH
6300 (f, rect.right - rect.left - 2 * border_width)),
6301 0, 0, 1, 0, 1);
6303 else
6305 int border_height = GetSystemMetrics (SM_CYFRAME);
6306 /* Won't work for wrapped menu bar. */
6307 int menu_bar_height = GetSystemMetrics (SM_CYMENU);
6308 int title_height = GetSystemMetrics (SM_CYCAPTION);
6310 change_frame_size
6311 (f, 0, (FRAME_PIXEL_TO_TEXT_HEIGHT
6312 (f, rect.bottom - rect.top - 2 * border_height
6313 - title_height - menu_bar_height)),
6314 0, 1, 0, 1);
6318 f->want_fullscreen = FULLSCREEN_NONE;
6319 unblock_input ();
6321 if (f->want_fullscreen == FULLSCREEN_BOTH
6322 || f->want_fullscreen == FULLSCREEN_WIDTH
6323 || f->want_fullscreen == FULLSCREEN_HEIGHT)
6324 do_pending_window_change (0);
6327 else
6328 f->want_fullscreen |= FULLSCREEN_WAIT;
6331 /* Call this to change the size of frame F's x-window.
6332 If CHANGE_GRAVITY, change to top-left-corner window gravity
6333 for this size change and subsequent size changes.
6334 Otherwise we leave the window gravity unchanged. */
6336 void
6337 x_set_window_size (struct frame *f, bool change_gravity,
6338 int width, int height, bool pixelwise)
6340 int pixelwidth, pixelheight;
6341 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
6342 RECT rect;
6343 MENUBARINFO info;
6344 int menu_bar_height;
6346 block_input ();
6348 /* Get the height of the menu bar here. It's used below to detect
6349 whether the menu bar is wrapped. It's also used to specify the
6350 third argument for AdjustWindowRect. See bug#22105. */
6351 info.cbSize = sizeof (info);
6352 info.rcBar.top = info.rcBar.bottom = 0;
6353 GetMenuBarInfo (FRAME_W32_WINDOW (f), 0xFFFFFFFD, 0, &info);
6354 menu_bar_height = info.rcBar.bottom - info.rcBar.top;
6356 if (pixelwise)
6358 pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
6359 pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
6361 else
6363 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width);
6364 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height);
6367 if (w32_add_wrapped_menu_bar_lines)
6369 /* When the menu bar wraps sending a SetWindowPos shrinks the
6370 height of the frame then the wrapped menu bar lines are not
6371 accounted for (Bug#15174 and Bug#18720). Here we add these
6372 extra lines to the frame height. */
6373 int default_menu_bar_height;
6375 /* Why is (apparently) SM_CYMENUSIZE needed here instead of
6376 SM_CYMENU ?? */
6377 default_menu_bar_height = GetSystemMetrics (SM_CYMENUSIZE);
6379 if ((default_menu_bar_height > 0)
6380 && (menu_bar_height > default_menu_bar_height)
6381 && ((menu_bar_height % default_menu_bar_height) == 0))
6382 pixelheight = pixelheight + menu_bar_height - default_menu_bar_height;
6385 f->win_gravity = NorthWestGravity;
6386 x_wm_set_size_hint (f, (long) 0, false);
6388 rect.left = rect.top = 0;
6389 rect.right = pixelwidth;
6390 rect.bottom = pixelheight;
6392 AdjustWindowRect (&rect, f->output_data.w32->dwStyle, menu_bar_height > 0);
6394 if (!(f->after_make_frame)
6395 && !(f->want_fullscreen & FULLSCREEN_WAIT)
6396 && FRAME_VISIBLE_P (f))
6398 RECT window_rect;
6400 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
6402 if (EQ (fullscreen, Qmaximized)
6403 || EQ (fullscreen, Qfullboth)
6404 || EQ (fullscreen, Qfullwidth))
6406 rect.left = window_rect.left;
6407 rect.right = window_rect.right;
6408 pixelwidth = 0;
6410 if (EQ (fullscreen, Qmaximized)
6411 || EQ (fullscreen, Qfullboth)
6412 || EQ (fullscreen, Qfullheight))
6414 rect.top = window_rect.top;
6415 rect.bottom = window_rect.bottom;
6416 pixelheight = 0;
6420 if (pixelwidth > 0 || pixelheight > 0)
6422 frame_size_history_add
6423 (f, Qx_set_window_size_1, width, height,
6424 list2 (Fcons (make_number (pixelwidth),
6425 make_number (pixelheight)),
6426 Fcons (make_number (rect.right - rect.left),
6427 make_number (rect.bottom - rect.top))));
6429 if (!FRAME_PARENT_FRAME (f))
6430 my_set_window_pos (FRAME_W32_WINDOW (f), NULL,
6431 0, 0,
6432 rect.right - rect.left,
6433 rect.bottom - rect.top,
6434 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
6435 else
6436 my_set_window_pos (FRAME_W32_WINDOW (f), HWND_TOP,
6437 0, 0,
6438 rect.right - rect.left,
6439 rect.bottom - rect.top,
6440 SWP_NOMOVE | SWP_NOACTIVATE);
6442 change_frame_size (f,
6443 ((pixelwidth == 0)
6444 ? 0 : FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth)),
6445 ((pixelheight == 0)
6446 ? 0 : FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight)),
6447 0, 1, 0, 1);
6448 SET_FRAME_GARBAGED (f);
6450 /* If cursor was outside the new size, mark it as off. */
6451 mark_window_cursors_off (XWINDOW (f->root_window));
6453 /* Clear out any recollection of where the mouse highlighting was,
6454 since it might be in a place that's outside the new frame size.
6455 Actually checking whether it is outside is a pain in the neck,
6456 so don't try--just let the highlighting be done afresh with new
6457 size. */
6458 cancel_mouse_face (f);
6461 unblock_input ();
6463 do_pending_window_change (false);
6466 /* Mouse warping. */
6468 void
6469 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
6471 UINT trail_num = 0;
6472 BOOL ret = false;
6473 RECT rect;
6474 POINT pt;
6476 block_input ();
6478 GetClientRect (FRAME_W32_WINDOW (f), &rect);
6479 pt.x = rect.left + pix_x;
6480 pt.y = rect.top + pix_y;
6481 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
6483 /* When "mouse trails" are in effect, moving the mouse cursor
6484 sometimes leaves behind an annoying "ghost" of the pointer.
6485 Avoid that by momentarily switching off mouse trails. */
6486 if (os_subtype == OS_NT
6487 && w32_major_version + w32_minor_version >= 6)
6488 ret = SystemParametersInfo (SPI_GETMOUSETRAILS, 0, &trail_num, 0);
6489 SetCursorPos (pt.x, pt.y);
6490 if (ret)
6491 SystemParametersInfo (SPI_SETMOUSETRAILS, trail_num, NULL, 0);
6493 unblock_input ();
6497 /* Focus shifting, raising and lowering. */
6499 /* The NOACTIVATE argument has no effect on Windows. According to the
6500 Windows API: An application cannot activate an inactive window
6501 without also bringing it to the top of the Z order. */
6503 void
6504 x_focus_frame (struct frame *f, bool noactivate)
6506 #if 0
6507 struct w32_display_info *dpyinfo = &one_w32_display_info;
6508 #endif
6510 /* Give input focus to frame. */
6511 block_input ();
6512 #if 0
6513 /* Try not to change its Z-order if possible. */
6514 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
6515 my_set_focus (f, FRAME_W32_WINDOW (f));
6516 else
6517 #endif
6518 my_set_foreground_window (FRAME_W32_WINDOW (f));
6519 unblock_input ();
6522 /* Raise frame F. */
6523 void
6524 x_raise_frame (struct frame *f)
6526 block_input ();
6528 /* Strictly speaking, raise-frame should only change the frame's Z
6529 order, leaving input focus unchanged. This is reasonable behavior
6530 on X where the usual policy is point-to-focus. However, this
6531 behavior would be very odd on Windows where the usual policy is
6532 click-to-focus.
6534 On X, if the mouse happens to be over the raised frame, it gets
6535 input focus anyway (so the window with focus will never be
6536 completely obscured) - if not, then just moving the mouse over it
6537 is sufficient to give it focus. On Windows, the user must actually
6538 click on the frame (preferably the title bar so as not to move
6539 point), which is more awkward. Also, no other Windows program
6540 raises a window to the top but leaves another window (possibly now
6541 completely obscured) with input focus.
6543 Because there is a system setting on Windows that allows the user
6544 to choose the point to focus policy, we make the strict semantics
6545 optional, but by default we grab focus when raising. */
6547 if (NILP (Vw32_grab_focus_on_raise))
6549 /* The obvious call to my_set_window_pos doesn't work if Emacs is
6550 not already the foreground application: the frame is raised
6551 above all other frames belonging to us, but not above the
6552 current top window. To achieve that, we have to resort to this
6553 more cumbersome method. */
6555 HDWP handle = BeginDeferWindowPos (2);
6556 if (handle)
6558 handle = DeferWindowPos (handle,
6559 FRAME_W32_WINDOW (f),
6560 HWND_TOP,
6561 0, 0, 0, 0,
6562 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
6563 if (handle)
6565 handle = DeferWindowPos (handle,
6566 GetForegroundWindow (),
6567 FRAME_W32_WINDOW (f),
6568 0, 0, 0, 0,
6569 SWP_NOSIZE | SWP_NOMOVE |
6570 SWP_NOACTIVATE);
6571 if (handle)
6572 EndDeferWindowPos (handle);
6576 else
6578 my_bring_window_to_top (FRAME_W32_WINDOW (f));
6581 unblock_input ();
6584 /* Lower frame F. */
6585 void
6586 x_lower_frame (struct frame *f)
6588 block_input ();
6589 my_set_window_pos (FRAME_W32_WINDOW (f),
6590 HWND_BOTTOM,
6591 0, 0, 0, 0,
6592 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
6593 unblock_input ();
6596 static void
6597 w32_frame_raise_lower (struct frame *f, bool raise_flag)
6599 if (! FRAME_W32_P (f))
6600 return;
6602 if (raise_flag)
6603 x_raise_frame (f);
6604 else
6605 x_lower_frame (f);
6608 /* Change of visibility. */
6610 /* This tries to wait until the frame is really visible.
6611 However, if the window manager asks the user where to position
6612 the frame, this will return before the user finishes doing that.
6613 The frame will not actually be visible at that time,
6614 but it will become visible later when the window manager
6615 finishes with it. */
6617 void
6618 x_make_frame_visible (struct frame *f)
6620 block_input ();
6622 x_set_bitmap_icon (f);
6624 if (! FRAME_VISIBLE_P (f))
6626 /* We test FRAME_GARBAGED_P here to make sure we don't
6627 call x_set_offset a second time
6628 if we get to x_make_frame_visible a second time
6629 before the window gets really visible. */
6630 if (! FRAME_ICONIFIED_P (f)
6631 && ! f->output_data.w32->asked_for_visible)
6633 if (!FRAME_PARENT_FRAME (f))
6635 RECT workarea_rect;
6636 RECT window_rect;
6638 /* Adjust vertical window position in order to avoid being
6639 covered by a taskbar placed at the bottom of the desktop. */
6640 SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
6641 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
6642 if (window_rect.bottom > workarea_rect.bottom
6643 && window_rect.top > workarea_rect.top)
6644 f->top_pos = max (window_rect.top
6645 - window_rect.bottom + workarea_rect.bottom,
6646 workarea_rect.top);
6649 x_set_offset (f, f->left_pos, f->top_pos, 0);
6652 f->output_data.w32->asked_for_visible = 1;
6654 /* According to a report in emacs-devel 2008-06-03, SW_SHOWNORMAL
6655 causes unexpected behavior when unminimizing frames that were
6656 previously maximized. But only SW_SHOWNORMAL works properly for
6657 frames that were truely hidden (using make-frame-invisible), so
6658 we need it to avoid Bug#5482. It seems that iconified is only
6659 set for minimized windows that are still visible, so use that to
6660 determine the appropriate flag to pass ShowWindow. */
6661 my_show_window (f, FRAME_W32_WINDOW (f),
6662 FRAME_ICONIFIED_P (f)
6663 ? SW_RESTORE
6664 : FRAME_NO_FOCUS_ON_MAP (f)
6665 ? SW_SHOWNOACTIVATE
6666 : SW_SHOWNORMAL);
6669 /* Synchronize to ensure Emacs knows the frame is visible
6670 before we do anything else. We do this loop with input not blocked
6671 so that incoming events are handled. */
6673 Lisp_Object frame;
6674 int count;
6676 /* This must come after we set COUNT. */
6677 unblock_input ();
6679 XSETFRAME (frame, f);
6681 /* Wait until the frame is visible. Process X events until a
6682 MapNotify event has been seen, or until we think we won't get a
6683 MapNotify at all.. */
6684 for (count = input_signal_count + 10;
6685 input_signal_count < count && !FRAME_VISIBLE_P (f);)
6687 /* Force processing of queued events. */
6688 /* TODO: x_sync equivalent? */
6690 /* Machines that do polling rather than SIGIO have been observed
6691 to go into a busy-wait here. So we'll fake an alarm signal
6692 to let the handler know that there's something to be read.
6693 We used to raise a real alarm, but it seems that the handler
6694 isn't always enabled here. This is probably a bug. */
6695 if (input_polling_used ())
6697 /* It could be confusing if a real alarm arrives while processing
6698 the fake one. Turn it off and let the handler reset it. */
6699 int old_poll_suppress_count = poll_suppress_count;
6700 poll_suppress_count = 1;
6701 poll_for_input_1 ();
6702 poll_suppress_count = old_poll_suppress_count;
6708 /* Change from mapped state to withdrawn state. */
6710 /* Make the frame visible (mapped and not iconified). */
6712 void
6713 x_make_frame_invisible (struct frame *f)
6715 /* Don't keep the highlight on an invisible frame. */
6716 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6717 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6719 block_input ();
6721 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
6723 /* We can't distinguish this from iconification
6724 just by the event that we get from the server.
6725 So we can't win using the usual strategy of letting
6726 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
6727 and synchronize with the server to make sure we agree. */
6728 SET_FRAME_VISIBLE (f, 0);
6729 SET_FRAME_ICONIFIED (f, false);
6731 unblock_input ();
6734 /* Change window state from mapped to iconified. */
6736 void
6737 x_iconify_frame (struct frame *f)
6739 /* Don't keep the highlight on an invisible frame. */
6740 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6741 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6743 if (FRAME_ICONIFIED_P (f))
6744 return;
6746 block_input ();
6748 x_set_bitmap_icon (f);
6750 /* Simulate the user minimizing the frame. */
6751 SendMessageTimeout (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0,
6752 0, 6000, NULL);
6754 SET_FRAME_VISIBLE (f, 0);
6755 SET_FRAME_ICONIFIED (f, true);
6757 unblock_input ();
6761 /* Free X resources of frame F. */
6763 void
6764 x_free_frame_resources (struct frame *f)
6766 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6767 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
6769 block_input ();
6771 /* We must free faces before destroying windows because some
6772 font-driver (e.g. xft) access a window while finishing a
6773 face. */
6774 free_frame_faces (f);
6776 if (FRAME_W32_WINDOW (f))
6777 my_destroy_window (f, FRAME_W32_WINDOW (f));
6779 free_frame_menubar (f);
6781 xfree (f->output_data.w32);
6782 f->output_data.w32 = NULL;
6784 if (f == dpyinfo->w32_focus_frame)
6785 dpyinfo->w32_focus_frame = 0;
6786 if (f == dpyinfo->w32_focus_event_frame)
6787 dpyinfo->w32_focus_event_frame = 0;
6788 if (f == dpyinfo->x_highlight_frame)
6789 dpyinfo->x_highlight_frame = 0;
6790 if (f == hlinfo->mouse_face_mouse_frame)
6791 reset_mouse_highlight (hlinfo);
6793 unblock_input ();
6797 /* Destroy the window of frame F. */
6798 static void
6799 x_destroy_window (struct frame *f)
6801 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6803 x_free_frame_resources (f);
6804 dpyinfo->reference_count--;
6808 /* Setting window manager hints. */
6810 /* Set the normal size hints for the window manager, for frame F.
6811 FLAGS is the flags word to use--or 0 meaning preserve the flags
6812 that the window now has.
6813 If USER_POSITION, set the USPosition
6814 flag (this is useful when FLAGS is 0). */
6815 void
6816 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
6818 Window window = FRAME_W32_WINDOW (f);
6820 enter_crit ();
6822 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
6823 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
6824 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
6825 SetWindowLong (window, WND_VSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_WIDTH (f));
6826 SetWindowLong (window, WND_HSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_HEIGHT (f));
6828 leave_crit ();
6831 /***********************************************************************
6832 Fonts
6833 ***********************************************************************/
6835 #ifdef GLYPH_DEBUG
6837 /* Check that FONT is valid on frame F. It is if it can be found in F's
6838 font table. */
6840 static void
6841 x_check_font (struct frame *f, struct font *font)
6843 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
6844 if (font->driver->check)
6845 eassert (font->driver->check (f, font) == 0);
6848 #endif /* GLYPH_DEBUG */
6850 /* Show hourglass cursor on frame F. */
6852 static void
6853 w32_show_hourglass (struct frame *f)
6855 if (!menubar_in_use && !current_popup_menu)
6857 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6859 w32->hourglass_p = 1;
6860 SetCursor (w32->hourglass_cursor);
6864 /* Hide hourglass cursor on frame F. */
6866 static void
6867 w32_hide_hourglass (struct frame *f)
6869 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6871 w32->hourglass_p = 0;
6872 if (f->pointer_invisible)
6873 SetCursor (NULL);
6874 else
6875 SetCursor (w32->current_cursor);
6878 /* FIXME: old code did that, but I don't know why. Anyway,
6879 this is used for non-GUI frames (see cancel_hourglass). */
6881 void
6882 w32_arrow_cursor (void)
6884 SetCursor (w32_load_cursor (IDC_ARROW));
6887 static void
6888 w32_toggle_invisible_pointer (struct frame *f, bool invisible)
6890 block_input ();
6892 if (f->pointer_invisible != invisible)
6894 f->pointer_invisible = invisible;
6895 w32_define_cursor (FRAME_W32_WINDOW (f),
6896 f->output_data.w32->current_cursor);
6899 unblock_input ();
6902 /***********************************************************************
6903 Initialization
6904 ***********************************************************************/
6906 static int w32_initialized = 0;
6908 void
6909 w32_initialize_display_info (Lisp_Object display_name)
6911 struct w32_display_info *dpyinfo = &one_w32_display_info;
6913 memset (dpyinfo, 0, sizeof (*dpyinfo));
6915 dpyinfo->name_list_element = Fcons (display_name, Qnil);
6916 dpyinfo->w32_id_name = xmalloc (SCHARS (Vinvocation_name)
6917 + SCHARS (Vsystem_name) + 2);
6918 sprintf (dpyinfo->w32_id_name, "%s@%s",
6919 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6921 /* Default Console mode values - overridden when running in GUI mode
6922 with values obtained from system metrics. */
6923 dpyinfo->resx = 1;
6924 dpyinfo->resy = 1;
6925 dpyinfo->n_planes = 1;
6926 dpyinfo->n_cbits = 4;
6927 dpyinfo->n_fonts = 0;
6928 dpyinfo->smallest_font_height = 1;
6929 dpyinfo->smallest_char_width = 1;
6930 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6931 dpyinfo->horizontal_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6932 /* TODO: dpyinfo->gray */
6934 reset_mouse_highlight (&dpyinfo->mouse_highlight);
6937 /* Create an xrdb-style database of resources to supersede registry settings.
6938 The database is just a concatenation of C strings, finished by an additional
6939 \0. The strings are submitted to some basic normalization, so
6941 [ *]option[ *]:[ *]value...
6943 becomes
6945 option:value...
6947 but any whitespace following value is not removed. */
6949 static char *
6950 w32_make_rdb (char *xrm_option)
6952 char *buffer = xmalloc (strlen (xrm_option) + 2);
6953 char *current = buffer;
6954 char ch;
6955 int in_option = 1;
6956 int before_value = 0;
6958 do {
6959 ch = *xrm_option++;
6961 if (ch == '\n')
6963 *current++ = '\0';
6964 in_option = 1;
6965 before_value = 0;
6967 else if (ch != ' ')
6969 *current++ = ch;
6970 if (in_option && (ch == ':'))
6972 in_option = 0;
6973 before_value = 1;
6975 else if (before_value)
6977 before_value = 0;
6980 else if (!(in_option || before_value))
6982 *current++ = ch;
6984 } while (ch);
6986 *current = '\0';
6988 return buffer;
6991 extern frame_parm_handler w32_frame_parm_handlers[];
6993 static struct redisplay_interface w32_redisplay_interface =
6995 w32_frame_parm_handlers,
6996 x_produce_glyphs,
6997 x_write_glyphs,
6998 x_insert_glyphs,
6999 x_clear_end_of_line,
7000 x_scroll_run,
7001 x_after_update_window_line,
7002 x_update_window_begin,
7003 x_update_window_end,
7004 0, /* flush_display */
7005 x_clear_window_mouse_face,
7006 x_get_glyph_overhangs,
7007 x_fix_overlapping_area,
7008 w32_draw_fringe_bitmap,
7009 w32_define_fringe_bitmap,
7010 w32_destroy_fringe_bitmap,
7011 w32_compute_glyph_string_overhangs,
7012 x_draw_glyph_string,
7013 w32_define_frame_cursor,
7014 w32_clear_frame_area,
7015 w32_draw_window_cursor,
7016 w32_draw_vertical_window_border,
7017 w32_draw_window_divider,
7018 w32_shift_glyphs_for_insert,
7019 w32_show_hourglass,
7020 w32_hide_hourglass
7023 static void x_delete_terminal (struct terminal *term);
7025 static struct terminal *
7026 w32_create_terminal (struct w32_display_info *dpyinfo)
7028 struct terminal *terminal;
7030 terminal = create_terminal (output_w32, &w32_redisplay_interface);
7032 terminal->display_info.w32 = dpyinfo;
7033 dpyinfo->terminal = terminal;
7035 /* MSVC does not type K&R functions with no arguments correctly, and
7036 so we must explicitly cast them. */
7037 terminal->clear_frame_hook = x_clear_frame;
7038 terminal->ins_del_lines_hook = x_ins_del_lines;
7039 terminal->delete_glyphs_hook = x_delete_glyphs;
7040 terminal->ring_bell_hook = w32_ring_bell;
7041 terminal->toggle_invisible_pointer_hook = w32_toggle_invisible_pointer;
7042 terminal->update_begin_hook = x_update_begin;
7043 terminal->update_end_hook = x_update_end;
7044 terminal->read_socket_hook = w32_read_socket;
7045 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
7046 terminal->mouse_position_hook = w32_mouse_position;
7047 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
7048 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
7049 terminal->fullscreen_hook = w32fullscreen_hook;
7050 terminal->menu_show_hook = w32_menu_show;
7051 terminal->popup_dialog_hook = w32_popup_dialog;
7052 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
7053 terminal->set_horizontal_scroll_bar_hook = w32_set_horizontal_scroll_bar;
7054 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
7055 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
7056 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
7057 terminal->delete_frame_hook = x_destroy_window;
7058 terminal->delete_terminal_hook = x_delete_terminal;
7059 /* Other hooks are NULL by default. */
7061 /* We don't yet support separate terminals on W32, so don't try to share
7062 keyboards between virtual terminals that are on the same physical
7063 terminal like X does. */
7064 terminal->kboard = allocate_kboard (Qw32);
7065 /* Don't let the initial kboard remain current longer than necessary.
7066 That would cause problems if a file loaded on startup tries to
7067 prompt in the mini-buffer. */
7068 if (current_kboard == initial_kboard)
7069 current_kboard = terminal->kboard;
7070 terminal->kboard->reference_count++;
7072 return terminal;
7075 static void
7076 x_delete_terminal (struct terminal *terminal)
7078 struct w32_display_info *dpyinfo = terminal->display_info.w32;
7080 /* Protect against recursive calls. delete_frame in
7081 delete_terminal calls us back when it deletes our last frame. */
7082 if (!terminal->name)
7083 return;
7085 block_input ();
7087 x_delete_display (dpyinfo);
7088 unblock_input ();
7091 struct w32_display_info *
7092 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
7094 struct w32_display_info *dpyinfo;
7095 struct terminal *terminal;
7096 HDC hdc;
7098 block_input ();
7100 if (!w32_initialized)
7102 w32_initialize ();
7103 w32_initialized = 1;
7106 w32_initialize_display_info (display_name);
7108 dpyinfo = &one_w32_display_info;
7109 terminal = w32_create_terminal (dpyinfo);
7111 /* Set the name of the terminal. */
7112 terminal->name = xlispstrdup (display_name);
7114 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
7116 /* Put this display on the chain. */
7117 dpyinfo->next = x_display_list;
7118 x_display_list = dpyinfo;
7120 hdc = GetDC (NULL);
7122 dpyinfo->root_window = GetDesktopWindow ();
7123 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
7124 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
7125 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
7126 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
7127 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
7128 ReleaseDC (NULL, hdc);
7130 /* initialize palette with white and black */
7132 XColor color;
7133 w32_defined_color (0, "white", &color, 1);
7134 w32_defined_color (0, "black", &color, 1);
7137 #ifdef WINDOWSNT
7138 /* Add the default keyboard. When !WINDOWSNT, we're using the
7139 standard Emacs console handling machinery and don't need an
7140 explicit FD here. */
7141 add_keyboard_wait_descriptor (0);
7142 #elif CYGWIN
7143 /* /dev/windows wakes us up when we have a thread message pending. */
7144 add_keyboard_wait_descriptor (w32_message_fd);
7145 #endif
7147 /* Create Fringe Bitmaps and store them for later use.
7149 On W32, bitmaps are all unsigned short, as Windows requires
7150 bitmap data to be Word aligned. For some reason they are
7151 horizontally reflected compared to how they appear on X, so we
7152 need to bitswap and convert to unsigned shorts before creating
7153 the bitmaps. */
7154 w32_init_fringe (terminal->rif);
7156 unblock_input ();
7158 return dpyinfo;
7161 /* Get rid of display DPYINFO, assuming all frames are already gone. */
7162 void
7163 x_delete_display (struct w32_display_info *dpyinfo)
7165 /* FIXME: the only display info apparently can't be deleted. */
7166 /* free palette table */
7168 struct w32_palette_entry * plist;
7170 plist = dpyinfo->color_list;
7171 while (plist)
7173 struct w32_palette_entry * pentry = plist;
7174 plist = plist->next;
7175 xfree (pentry);
7177 dpyinfo->color_list = NULL;
7178 if (dpyinfo->palette)
7179 DeleteObject (dpyinfo->palette);
7181 w32_reset_fringes ();
7185 /* Set up use of W32. */
7187 void
7188 w32_init_main_thread (void)
7190 dwMainThreadId = GetCurrentThreadId ();
7191 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
7192 GetCurrentProcess (), &hMainThread, 0, TRUE,
7193 DUPLICATE_SAME_ACCESS);
7198 DWORD WINAPI w32_msg_worker (void * arg);
7200 static void
7201 w32_initialize (void)
7203 HANDLE shell;
7204 HRESULT (WINAPI * set_user_model) (const wchar_t * id);
7206 baud_rate = 19200;
7208 w32_system_caret_hwnd = NULL;
7209 w32_system_caret_height = 0;
7210 w32_system_caret_x = 0;
7211 w32_system_caret_y = 0;
7213 /* On Windows 7 and later, we need to set the user model ID
7214 to associate emacsclient launched files with Emacs frames
7215 in the UI. */
7216 shell = GetModuleHandle ("shell32.dll");
7217 if (shell)
7219 set_user_model
7220 = (void *) GetProcAddress (shell,
7221 "SetCurrentProcessExplicitAppUserModelID");
7223 /* If the function is defined, then we are running on Windows 7
7224 or newer, and the UI uses this to group related windows
7225 together. Since emacs, runemacs, emacsclient are related, we
7226 want them grouped even though the executables are different,
7227 so we need to set a consistent ID between them. */
7228 if (set_user_model)
7229 set_user_model (L"GNU.Emacs");
7232 #ifdef CYGWIN
7233 if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
7234 fatal ("opening /dev/windows: %s", strerror (errno));
7235 #endif /* CYGWIN */
7237 /* Initialize w32_use_visible_system_caret based on whether a screen
7238 reader is in use. */
7239 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
7240 &w32_use_visible_system_caret, 0))
7241 w32_use_visible_system_caret = 0;
7243 any_help_event_p = 0;
7245 /* Initialize input mode: interrupt_input off, no flow control, allow
7246 8 bit character input, standard quit char. */
7247 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
7250 LCID input_locale_id = LOWORD (GetKeyboardLayout (0));
7251 w32_keyboard_codepage = codepage_for_locale (input_locale_id);
7254 /* Create the window thread - it will terminate itself when the app
7255 terminates */
7256 init_crit ();
7258 /* Wait for thread to start */
7260 MSG msg;
7262 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
7264 hWindowsThread = CreateThread (NULL, 0,
7265 w32_msg_worker,
7266 0, 0, &dwWindowsThreadId);
7268 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
7271 /* It is desirable that mainThread should have the same notion of
7272 focus window and active window as windowsThread. Unfortunately, the
7273 following call to AttachThreadInput, which should do precisely what
7274 we need, causes major problems when Emacs is linked as a console
7275 program. Unfortunately, we have good reasons for doing that, so
7276 instead we need to send messages to windowsThread to make some API
7277 calls for us (ones that affect, or depend on, the active/focus
7278 window state.) */
7279 #ifdef ATTACH_THREADS
7280 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
7281 #endif
7283 /* Dynamically link to optional system components. */
7285 HMODULE user_lib = GetModuleHandle ("user32.dll");
7287 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
7289 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
7291 #undef LOAD_PROC
7293 /* Ensure scrollbar handles are at least 5 pixels. */
7294 vertical_scroll_bar_min_handle = 5;
7295 horizontal_scroll_bar_min_handle = 5;
7297 /* For either kind of scroll bar, take account of the arrows; these
7298 effectively form the border of the main scroll bar range. */
7299 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
7300 = GetSystemMetrics (SM_CYVSCROLL);
7301 horizontal_scroll_bar_left_border = horizontal_scroll_bar_right_border
7302 = GetSystemMetrics (SM_CYHSCROLL);
7306 void
7307 syms_of_w32term (void)
7309 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
7311 DEFSYM (Qadded, "added");
7312 DEFSYM (Qremoved, "removed");
7313 DEFSYM (Qmodified, "modified");
7314 DEFSYM (Qrenamed_from, "renamed-from");
7315 DEFSYM (Qrenamed_to, "renamed-to");
7317 DEFVAR_INT ("w32-num-mouse-buttons",
7318 w32_num_mouse_buttons,
7319 doc: /* Number of physical mouse buttons. */);
7320 w32_num_mouse_buttons = 2;
7322 DEFVAR_LISP ("w32-swap-mouse-buttons",
7323 Vw32_swap_mouse_buttons,
7324 doc: /* Swap the mapping of middle and right mouse buttons.
7325 When nil, middle button is mouse-2 and right button is mouse-3. */);
7326 Vw32_swap_mouse_buttons = Qnil;
7328 DEFVAR_LISP ("w32-grab-focus-on-raise",
7329 Vw32_grab_focus_on_raise,
7330 doc: /* Raised frame grabs input focus.
7331 When t, `raise-frame' grabs input focus as well. This fits well
7332 with the normal Windows click-to-focus policy, but might not be
7333 desirable when using a point-to-focus policy. */);
7334 Vw32_grab_focus_on_raise = Qt;
7336 DEFVAR_LISP ("w32-capslock-is-shiftlock",
7337 Vw32_capslock_is_shiftlock,
7338 doc: /* Apply CapsLock state to non character input keys.
7339 When nil, CapsLock only affects normal character input keys. */);
7340 Vw32_capslock_is_shiftlock = Qnil;
7342 DEFVAR_LISP ("w32-recognize-altgr",
7343 Vw32_recognize_altgr,
7344 doc: /* Recognize right-alt and left-ctrl as AltGr.
7345 When nil, the right-alt and left-ctrl key combination is
7346 interpreted normally. */);
7347 Vw32_recognize_altgr = Qt;
7349 DEFVAR_BOOL ("w32-use-visible-system-caret",
7350 w32_use_visible_system_caret,
7351 doc: /* Flag to make the system caret visible.
7352 When this is non-nil, Emacs will indicate the position of point by
7353 using the system caret instead of drawing its own cursor. Some screen
7354 reader software does not track the system cursor properly when it is
7355 invisible, and gets confused by Emacs drawing its own cursor, so this
7356 variable is initialized to t when Emacs detects that screen reader
7357 software is running as it starts up.
7359 When this variable is set, other variables affecting the appearance of
7360 the cursor have no effect. */);
7362 w32_use_visible_system_caret = 0;
7364 /* We don't yet support this, but defining this here avoids whining
7365 from cus-start.el and other places, like "M-x set-variable". */
7366 DEFVAR_BOOL ("x-use-underline-position-properties",
7367 x_use_underline_position_properties,
7368 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
7369 A value of nil means ignore them. If you encounter fonts with bogus
7370 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7371 to 4.1, set this to nil. You can also use `underline-minimum-offset'
7372 to override the font's UNDERLINE_POSITION for small font display
7373 sizes. */);
7374 x_use_underline_position_properties = 0;
7376 DEFVAR_BOOL ("x-underline-at-descent-line",
7377 x_underline_at_descent_line,
7378 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7379 A value of nil means to draw the underline according to the value of the
7380 variable `x-use-underline-position-properties', which is usually at the
7381 baseline level. The default value is nil. */);
7382 x_underline_at_descent_line = 0;
7384 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7385 doc: /* Which toolkit scroll bars Emacs uses, if any.
7386 A value of nil means Emacs doesn't use toolkit scroll bars.
7387 With the X Window system, the value is a symbol describing the
7388 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
7389 With MS Windows or Nextstep, the value is t. */);
7390 Vx_toolkit_scroll_bars = Qt;
7392 DEFVAR_BOOL ("w32-unicode-filenames",
7393 w32_unicode_filenames,
7394 doc: /* Non-nil means use Unicode APIs when passing file names to the OS.
7395 A value of nil means file names passed to the OS APIs and returned
7396 from those APIs are encoded/decoded using the ANSI codepage
7397 specified by `file-name-coding-system'.
7399 This variable is set to non-nil by default when Emacs runs on Windows
7400 systems of the NT family, including W2K, XP, Vista, Windows 7 and
7401 Windows 8. It is set to nil on Windows 9X. */);
7402 if (os_subtype == OS_9X)
7403 w32_unicode_filenames = 0;
7404 else
7405 w32_unicode_filenames = 1;
7408 /* FIXME: The following variable will be (hopefully) removed
7409 before Emacs 25.1 gets released. */
7411 DEFVAR_BOOL ("w32-add-wrapped-menu-bar-lines",
7412 w32_add_wrapped_menu_bar_lines,
7413 doc: /* Non-nil means frame resizing accounts for wrapped menu bar lines.
7414 A value of nil means frame resizing does not add the height of wrapped
7415 menu bar lines when sending a frame resize request to the Windows API.
7416 This usually means that the resulting frame height is off by the number
7417 of wrapped menu bar lines. If this is non-nil, Emacs adds the height of
7418 wrapped menu bar lines when sending frame resize requests to the Windows
7419 API. */);
7420 w32_add_wrapped_menu_bar_lines = 1;
7422 /* Tell Emacs about this window system. */
7423 Fprovide (Qw32, Qnil);