Prefer HTTPS to HTTP for gnu.org
[emacs.git] / src / w32term.c
blobc15cbbfa847acb96b8d090ed46ae40f484156243
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 <https://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 #ifdef CYGWIN
167 int w32_message_fd = -1;
168 #endif /* CYGWIN */
170 static void w32_handle_tool_bar_click (struct frame *,
171 struct input_event *);
172 static void w32_define_cursor (Window, Cursor);
174 void x_lower_frame (struct frame *);
175 void x_scroll_bar_clear (struct frame *);
176 void x_raise_frame (struct frame *);
177 void x_wm_set_window_state (struct frame *, int);
178 void x_wm_set_icon_pixmap (struct frame *, int);
179 static void w32_initialize (void);
180 static void x_update_end (struct frame *);
181 static void w32_frame_up_to_date (struct frame *);
182 static void x_clear_frame (struct frame *);
183 static void frame_highlight (struct frame *);
184 static void frame_unhighlight (struct frame *);
185 static void x_new_focus_frame (struct w32_display_info *,
186 struct frame *);
187 static void x_focus_changed (int, int, struct w32_display_info *,
188 struct frame *, struct input_event *);
189 static void w32_detect_focus_change (struct w32_display_info *,
190 W32Msg *, struct input_event *);
191 static void w32_frame_rehighlight (struct frame *);
192 static void x_frame_rehighlight (struct w32_display_info *);
193 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
194 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
195 enum text_cursor_kinds);
196 static void w32_clip_to_row (struct window *, struct glyph_row *,
197 enum glyph_row_area, HDC);
198 static BOOL my_show_window (struct frame *, HWND, int);
199 static void my_set_window_pos (HWND, HWND, int, int, int, int, UINT);
200 #if 0
201 static void my_set_focus (struct frame *, HWND);
202 #endif
203 static void my_set_foreground_window (HWND);
204 static void my_destroy_window (struct frame *, HWND);
205 static void w32fullscreen_hook (struct frame *);
207 #ifdef GLYPH_DEBUG
208 static void x_check_font (struct frame *, struct font *);
209 #endif
212 /***********************************************************************
213 Debugging
214 ***********************************************************************/
216 #if 0
218 /* This is a function useful for recording debugging information about
219 the sequence of occurrences in this file. */
221 struct record
223 char *locus;
224 int type;
227 struct record event_record[100];
229 int event_record_index;
231 record_event (char *locus, int type)
233 if (event_record_index == sizeof (event_record) / sizeof (struct record))
234 event_record_index = 0;
236 event_record[event_record_index].locus = locus;
237 event_record[event_record_index].type = type;
238 event_record_index++;
241 #endif /* 0 */
244 static void
245 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
246 XGCValues *xgcv)
248 if (mask & GCForeground)
249 gc->foreground = xgcv->foreground;
250 if (mask & GCBackground)
251 gc->background = xgcv->background;
252 if (mask & GCFont)
253 gc->font = xgcv->font;
256 XGCValues *
257 XCreateGC (void *ignore, HWND wignore, unsigned long mask, XGCValues *xgcv)
259 XGCValues *gc = xzalloc (sizeof (XGCValues));
261 XChangeGC (ignore, gc, mask, xgcv);
263 return gc;
266 #if 0 /* unused for now, see x_draw_image_glyph_string below */
267 static void
268 XGetGCValues (void *ignore, XGCValues *gc,
269 unsigned long mask, XGCValues *xgcv)
271 XChangeGC (ignore, xgcv, mask, gc);
273 #endif
275 static void
276 w32_set_clip_rectangle (HDC hdc, RECT *rect)
278 if (rect)
280 HRGN clip_region = CreateRectRgnIndirect (rect);
281 SelectClipRgn (hdc, clip_region);
282 DeleteObject (clip_region);
284 else
285 SelectClipRgn (hdc, NULL);
288 /* Restore clipping rectangle in S */
289 static void
290 w32_restore_glyph_string_clip (struct glyph_string *s)
292 RECT *r = s->clip;
293 int n = s->num_clips;
295 if (n == 1)
296 w32_set_clip_rectangle (s->hdc, r);
297 else if (n > 1)
299 HRGN clip1 = CreateRectRgnIndirect (r);
300 HRGN clip2 = CreateRectRgnIndirect (r + 1);
301 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
302 SelectClipRgn (s->hdc, clip1);
303 DeleteObject (clip1);
304 DeleteObject (clip2);
308 static void
309 x_get_scale_factor(struct w32_display_info *dpyinfo, int *scale_x, int *scale_y)
311 const int base_res = 96;
313 *scale_x = *scale_y = 1;
315 if (dpyinfo)
317 if (dpyinfo->resx > base_res)
318 *scale_x = floor (dpyinfo->resx / base_res);
319 if (dpyinfo->resy > base_res)
320 *scale_y = floor (dpyinfo->resy / base_res);
325 Draw a wavy line under S. The wave fills wave_height pixels from y0.
327 x0 wave_length = 2
329 y0 * * * * *
330 |* * * * * * * * *
331 wave_height = 3 | * * * *
335 static void
336 w32_draw_underwave (struct glyph_string *s, COLORREF color)
338 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (s->f);
340 int scale_x, scale_y;
341 x_get_scale_factor (dpyinfo, &scale_x, &scale_y);
343 int wave_height = 3 * scale_y, wave_length = 2 * scale_x, thickness = scale_y;
344 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
345 XRectangle wave_clip, string_clip, final_clip;
346 RECT w32_final_clip, w32_string_clip;
347 HPEN hp, oldhp;
349 dx = wave_length;
350 dy = wave_height - 1;
351 x0 = s->x;
352 y0 = s->ybase + wave_height / 2 - scale_y;
353 width = s->width;
354 xmax = x0 + width;
356 /* Find and set clipping rectangle */
358 wave_clip.x = x0;
359 wave_clip.y = y0;
360 wave_clip.width = width;
361 wave_clip.height = wave_height;
363 get_glyph_string_clip_rect (s, &w32_string_clip);
364 CONVERT_TO_XRECT (string_clip, w32_string_clip);
366 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
367 return;
369 hp = CreatePen (PS_SOLID, thickness, color);
370 oldhp = SelectObject (s->hdc, hp);
371 CONVERT_FROM_XRECT (final_clip, w32_final_clip);
372 w32_set_clip_rectangle (s->hdc, &w32_final_clip);
374 /* Draw the waves */
376 x1 = x0 - (x0 % dx);
377 x2 = x1 + dx;
378 odd = (x1/dx) % 2;
379 y1 = y2 = y0;
381 if (odd)
382 y1 += dy;
383 else
384 y2 += dy;
386 MoveToEx (s->hdc, x1, y1, NULL);
388 while (x1 <= xmax)
390 LineTo (s->hdc, x2, y2);
391 x1 = x2, y1 = y2;
392 x2 += dx, y2 = y0 + odd*dy;
393 odd = !odd;
396 /* Restore previous pen and clipping rectangle(s) */
397 w32_restore_glyph_string_clip (s);
398 SelectObject (s->hdc, oldhp);
399 DeleteObject (hp);
402 /* Draw a hollow rectangle at the specified position. */
403 static void
404 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
405 int width, int height)
407 HBRUSH hb, oldhb;
408 HPEN hp, oldhp;
410 hb = CreateSolidBrush (gc->background);
411 hp = CreatePen (PS_SOLID, 0, gc->foreground);
412 oldhb = SelectObject (hdc, hb);
413 oldhp = SelectObject (hdc, hp);
415 /* We enlarge WIDTH and HEIGHT by 1 to be bug-compatible to the
416 brain-dead design of XDrawRectangle, which draws a rectangle that
417 is 1 pixel wider and higher than its arguments WIDTH and HEIGHT.
418 This allows us to keep the code that calls this function similar
419 to the corresponding code in xterm.c. For the details, see
420 http://lists.gnu.org/archives/html/emacs-devel/2014-10/msg00546.html. */
421 Rectangle (hdc, x, y, x + width + 1, y + height + 1);
423 SelectObject (hdc, oldhb);
424 SelectObject (hdc, oldhp);
425 DeleteObject (hb);
426 DeleteObject (hp);
429 /* Draw a filled rectangle at the specified position. */
430 void
431 w32_fill_rect (struct frame *f, HDC hdc, COLORREF pix, RECT *lprect)
433 HBRUSH hb;
435 hb = CreateSolidBrush (pix);
436 FillRect (hdc, lprect, hb);
437 DeleteObject (hb);
440 void
441 w32_clear_window (struct frame *f)
443 RECT rect;
444 HDC hdc = get_frame_dc (f);
446 /* Under certain conditions, this can be called at startup with
447 a console frame pointer before the GUI frame is created. An HDC
448 of 0 indicates this. */
449 if (hdc)
451 GetClientRect (FRAME_W32_WINDOW (f), &rect);
452 w32_clear_rect (f, hdc, &rect);
455 release_frame_dc (f, hdc);
458 #define OPAQUE_FRAME 255
460 void
461 x_set_frame_alpha (struct frame *f)
463 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
464 double alpha = 1.0;
465 double alpha_min = 1.0;
466 BYTE opac;
467 LONG ex_style;
468 HWND window = FRAME_W32_WINDOW (f);
470 /* Older versions of Windows do not support transparency. */
471 if (!pfnSetLayeredWindowAttributes)
472 return;
474 if (dpyinfo->w32_focus_frame == f)
475 alpha = f->alpha[0];
476 else
477 alpha = f->alpha[1];
479 if (FLOATP (Vframe_alpha_lower_limit))
480 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
481 else if (INTEGERP (Vframe_alpha_lower_limit))
482 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
484 if (alpha < 0.0)
485 return;
486 else if (alpha > 1.0)
487 alpha = 1.0;
488 else if (alpha < alpha_min && alpha_min <= 1.0)
489 alpha = alpha_min;
491 opac = alpha * OPAQUE_FRAME;
493 ex_style = GetWindowLong (window, GWL_EXSTYLE);
495 if (opac == OPAQUE_FRAME)
496 ex_style &= ~WS_EX_LAYERED;
497 else
498 ex_style |= WS_EX_LAYERED;
500 SetWindowLong (window, GWL_EXSTYLE, ex_style);
502 if (opac != OPAQUE_FRAME)
503 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
507 x_display_pixel_height (struct w32_display_info *dpyinfo)
509 int pixels = GetSystemMetrics (SM_CYVIRTUALSCREEN);
511 if (pixels == 0)
512 /* Fallback for Windows 95 or NT 4.0. */
513 pixels = GetSystemMetrics (SM_CYSCREEN);
515 return pixels;
519 x_display_pixel_width (struct w32_display_info *dpyinfo)
521 int pixels = GetSystemMetrics (SM_CXVIRTUALSCREEN);
523 if (pixels == 0)
524 /* Fallback for Windows 95 or NT 4.0. */
525 pixels = GetSystemMetrics (SM_CXSCREEN);
527 return pixels;
531 /***********************************************************************
532 Starting and ending an update
533 ***********************************************************************/
535 /* Start an update of frame F. This function is installed as a hook
536 for update_begin, i.e. it is called when update_begin is called.
537 This function is called prior to calls to x_update_window_begin for
538 each window being updated. */
540 static void
541 x_update_begin (struct frame *f)
543 struct w32_display_info *display_info = FRAME_DISPLAY_INFO (f);
545 if (! FRAME_W32_P (f))
546 return;
548 /* Regenerate display palette before drawing if list of requested
549 colors has changed. */
550 if (display_info->regen_palette)
552 w32_regenerate_palette (f);
553 display_info->regen_palette = FALSE;
558 /* Start update of window W. */
560 static void
561 x_update_window_begin (struct window *w)
563 struct frame *f = XFRAME (WINDOW_FRAME (w));
564 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
566 /* Hide the system caret during an update. */
567 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
569 SendMessageTimeout (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0,
570 0, 6000, NULL);
573 w->output_cursor = w->cursor;
575 block_input ();
577 if (f == hlinfo->mouse_face_mouse_frame)
579 /* Don't do highlighting for mouse motion during the update. */
580 hlinfo->mouse_face_defer = true;
582 /* If F needs to be redrawn, simply forget about any prior mouse
583 highlighting. */
584 if (FRAME_GARBAGED_P (f))
585 hlinfo->mouse_face_window = Qnil;
587 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
588 their mouse_face_p flag set, which means that they are always
589 unequal to rows in a desired matrix which never have that
590 flag set. So, rows containing mouse-face glyphs are never
591 scrolled, and we don't have to switch the mouse highlight off
592 here to prevent it from being scrolled. */
594 /* Can we tell that this update does not affect the window
595 where the mouse highlight is? If so, no need to turn off.
596 Likewise, don't do anything if the frame is garbaged;
597 in that case, the frame's current matrix that we would use
598 is all wrong, and we will redisplay that line anyway. */
599 if (!NILP (hlinfo->mouse_face_window)
600 && w == XWINDOW (hlinfo->mouse_face_window))
602 int i;
604 for (i = 0; i < w->desired_matrix->nrows; ++i)
605 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
606 break;
608 if (i < w->desired_matrix->nrows)
609 clear_mouse_face (hlinfo);
611 #endif /* 0 */
614 unblock_input ();
617 /* Draw a vertical window border from (x,y0) to (x,y1) */
619 static void
620 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
622 struct frame *f = XFRAME (WINDOW_FRAME (w));
623 RECT r;
624 HDC hdc;
625 struct face *face;
627 r.left = x;
628 r.right = x + 1;
629 r.top = y0;
630 r.bottom = y1;
632 hdc = get_frame_dc (f);
633 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
634 if (face)
635 w32_fill_rect (f, hdc, face->foreground, &r);
636 else
637 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
639 release_frame_dc (f, hdc);
643 /* Draw a window divider from (x0, y0) to (x1, y1) */
645 static void
646 w32_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
648 struct frame *f = XFRAME (WINDOW_FRAME (w));
649 HDC hdc = get_frame_dc (f);
650 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
651 struct face *face_first
652 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
653 struct face *face_last
654 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
655 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
656 unsigned long color_first = (face_first
657 ? face_first->foreground
658 : FRAME_FOREGROUND_PIXEL (f));
659 unsigned long color_last = (face_last
660 ? face_last->foreground
661 : FRAME_FOREGROUND_PIXEL (f));
663 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
664 /* Vertical. */
666 w32_fill_area_abs (f, hdc, color_first, x0, y0, x0 + 1, y1);
667 w32_fill_area_abs (f, hdc, color, x0 + 1, y0, x1 - 1, y1);
668 w32_fill_area_abs (f, hdc, color_last, x1 - 1, y0, x1, y1);
670 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
671 /* Horizontal. */
673 w32_fill_area_abs (f, hdc, color_first, x0, y0, x1, y0 + 1);
674 w32_fill_area_abs (f, hdc, color, x0, y0 + 1, x1, y1 - 1);
675 w32_fill_area_abs (f, hdc, color_last, x0, y1 - 1, x1, y1);
677 else
678 w32_fill_area_abs (f, hdc, color, x0, y0, x1, y1);
680 release_frame_dc (f, hdc);
683 /* End update of window W.
685 Draw vertical borders between horizontally adjacent windows, and
686 display W's cursor if CURSOR_ON_P is non-zero.
688 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
689 glyphs in mouse-face were overwritten. In that case we have to
690 make sure that the mouse-highlight is properly redrawn.
692 W may be a menu bar pseudo-window in case we don't have X toolkit
693 support. Such windows don't have a cursor, so don't display it
694 here. */
696 static void
697 x_update_window_end (struct window *w, bool cursor_on_p,
698 bool mouse_face_overwritten_p)
700 if (!w->pseudo_window_p)
702 block_input ();
704 if (cursor_on_p)
705 display_and_set_cursor (w, true,
706 w->output_cursor.hpos, w->output_cursor.vpos,
707 w->output_cursor.x, w->output_cursor.y);
709 if (draw_window_fringes (w, true))
711 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
712 x_draw_right_divider (w);
713 else
714 x_draw_vertical_border (w);
717 unblock_input ();
720 /* If a row with mouse-face was overwritten, arrange for
721 XTframe_up_to_date to redisplay the mouse highlight. */
722 if (mouse_face_overwritten_p)
724 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
726 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
727 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
728 hlinfo->mouse_face_window = Qnil;
731 /* Unhide the caret. This won't actually show the cursor, unless it
732 was visible before the corresponding call to HideCaret in
733 x_update_window_begin. */
734 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
736 SendMessageTimeout (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0,
737 0, 6000, NULL);
742 /* End update of frame F. This function is installed as a hook in
743 update_end. */
745 static void
746 x_update_end (struct frame *f)
748 if (! FRAME_W32_P (f))
749 return;
751 /* Mouse highlight may be displayed again. */
752 MOUSE_HL_INFO (f)->mouse_face_defer = false;
756 /* This function is called from various places in xdisp.c
757 whenever a complete update has been performed. */
759 static void
760 w32_frame_up_to_date (struct frame *f)
762 if (FRAME_W32_P (f))
763 FRAME_MOUSE_UPDATE (f);
767 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
768 arrow bitmaps, or clear the fringes if no bitmaps are required
769 before DESIRED_ROW is made current. This function is called from
770 update_window_line only if it is known that there are differences
771 between bitmaps to be drawn between current row and DESIRED_ROW. */
773 static void
774 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
776 struct frame *f;
777 int width, height;
779 eassert (w);
781 if (!desired_row->mode_line_p && !w->pseudo_window_p)
782 desired_row->redraw_fringe_bitmaps_p = true;
784 /* When a window has disappeared, make sure that no rest of
785 full-width rows stays visible in the internal border. Could
786 check here if updated window is the leftmost/rightmost window,
787 but I guess it's not worth doing since vertically split windows
788 are almost never used, internal border is rarely set, and the
789 overhead is very small. */
790 if (windows_or_buffers_changed
791 && desired_row->full_width_p
792 && (f = XFRAME (w->frame),
793 width = FRAME_INTERNAL_BORDER_WIDTH (f),
794 width != 0)
795 && (height = desired_row->visible_height,
796 height > 0))
798 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
800 block_input ();
802 HDC hdc = get_frame_dc (f);
803 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
805 if (face)
807 /* Fill border with internal border face. */
808 unsigned long color = face->background;
810 w32_fill_area (f, hdc, color, 0, y, width, height);
811 w32_fill_area (f, hdc, color, FRAME_PIXEL_WIDTH (f) - width,
812 y, width, height);
814 else
816 w32_clear_area (f, hdc, 0, y, width, height);
817 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
818 y, width, height);
820 release_frame_dc (f, hdc);
822 unblock_input ();
827 /* Draw the bitmap WHICH in one of the left or right fringes of
828 window W. ROW is the glyph row for which to display the bitmap; it
829 determines the vertical position at which the bitmap has to be
830 drawn. */
832 static void
833 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
834 struct draw_fringe_bitmap_params *p)
836 struct frame *f = XFRAME (WINDOW_FRAME (w));
837 HDC hdc;
838 struct face *face = p->face;
840 hdc = get_frame_dc (f);
842 /* Must clip because of partially visible lines. */
843 w32_clip_to_row (w, row, ANY_AREA, hdc);
845 if (p->bx >= 0 && !p->overlay_p)
846 w32_fill_area (f, hdc, face->background,
847 p->bx, p->by, p->nx, p->ny);
849 if (p->which && p->which < max_fringe_bmp)
851 HBITMAP pixmap = fringe_bmp[p->which];
852 HDC compat_hdc;
853 HANDLE horig_obj;
855 compat_hdc = CreateCompatibleDC (hdc);
857 SaveDC (hdc);
859 horig_obj = SelectObject (compat_hdc, pixmap);
861 /* Paint overlays transparently. */
862 if (p->overlay_p)
864 HBRUSH h_brush, h_orig_brush;
866 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
867 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
868 h_brush = CreateSolidBrush (face->foreground);
869 h_orig_brush = SelectObject (hdc, h_brush);
871 BitBlt (hdc, p->x, p->y, p->wd, p->h,
872 compat_hdc, 0, p->dh,
873 DSTINVERT);
874 BitBlt (hdc, p->x, p->y, p->wd, p->h,
875 compat_hdc, 0, p->dh,
876 0x2E064A);
877 BitBlt (hdc, p->x, p->y, p->wd, p->h,
878 compat_hdc, 0, p->dh,
879 DSTINVERT);
881 SelectObject (hdc, h_orig_brush);
882 DeleteObject (h_brush);
884 else
886 SetTextColor (hdc, face->background);
887 SetBkColor (hdc, (p->cursor_p
888 ? f->output_data.w32->cursor_pixel
889 : face->foreground));
891 BitBlt (hdc, p->x, p->y, p->wd, p->h,
892 compat_hdc, 0, p->dh,
893 SRCCOPY);
896 SelectObject (compat_hdc, horig_obj);
897 DeleteDC (compat_hdc);
898 RestoreDC (hdc, -1);
901 w32_set_clip_rectangle (hdc, NULL);
903 release_frame_dc (f, hdc);
906 static void
907 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
909 if (which >= max_fringe_bmp)
911 int i = max_fringe_bmp;
912 max_fringe_bmp = which + 20;
913 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
914 while (i < max_fringe_bmp)
915 fringe_bmp[i++] = 0;
918 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
921 static void
922 w32_destroy_fringe_bitmap (int which)
924 if (which >= max_fringe_bmp)
925 return;
927 if (fringe_bmp[which])
928 DeleteObject (fringe_bmp[which]);
929 fringe_bmp[which] = 0;
932 /***********************************************************************
933 Display Iterator
934 ***********************************************************************/
936 /* Function prototypes of this page. */
938 static void x_set_glyph_string_clipping (struct glyph_string *);
939 static void x_set_glyph_string_gc (struct glyph_string *);
940 static void x_draw_glyph_string_background (struct glyph_string *,
941 bool);
942 static void x_draw_glyph_string_foreground (struct glyph_string *);
943 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
944 static void x_draw_glyph_string_box (struct glyph_string *);
945 static void x_draw_glyph_string (struct glyph_string *);
946 static void x_set_cursor_gc (struct glyph_string *);
947 static void x_set_mode_line_face_gc (struct glyph_string *);
948 static void x_set_mouse_face_gc (struct glyph_string *);
949 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
950 static void w32_setup_relief_color (struct frame *, struct relief *,
951 double, int, COLORREF);
952 static void x_setup_relief_colors (struct glyph_string *);
953 static void x_draw_image_glyph_string (struct glyph_string *);
954 static void x_draw_image_relief (struct glyph_string *);
955 static void x_draw_image_foreground (struct glyph_string *);
956 static void w32_draw_image_foreground_1 (struct glyph_string *, HBITMAP);
957 static void x_clear_glyph_string_rect (struct glyph_string *, int,
958 int, int, int);
959 static void w32_draw_relief_rect (struct frame *, int, int, int, int,
960 int, int, int, int, int, int,
961 RECT *);
962 static void w32_draw_box_rect (struct glyph_string *, int, int, int, int,
963 int, bool, bool, RECT *);
966 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
967 face. */
969 static void
970 x_set_cursor_gc (struct glyph_string *s)
972 if (s->font == FRAME_FONT (s->f)
973 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
974 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
975 /* Sometimes we are not called for each change in the default
976 face's background color (e.g., bug#26851), so the additional
977 test in the next line gives us a chance to resync. */
978 && s->f->output_data.w32->cursor_gc->foreground == s->face->background
979 && !s->cmp)
980 s->gc = s->f->output_data.w32->cursor_gc;
981 else
983 /* Cursor on non-default face: must merge. */
984 XGCValues xgcv;
985 unsigned long mask;
987 xgcv.background = s->f->output_data.w32->cursor_pixel;
988 xgcv.foreground = s->face->background;
990 /* If the glyph would be invisible, try a different foreground. */
991 if (xgcv.foreground == xgcv.background)
992 xgcv.foreground = s->face->foreground;
993 if (xgcv.foreground == xgcv.background)
994 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
995 if (xgcv.foreground == xgcv.background)
996 xgcv.foreground = s->face->foreground;
998 /* Make sure the cursor is distinct from text in this face. */
999 if (xgcv.background == s->face->background
1000 && xgcv.foreground == s->face->foreground)
1002 xgcv.background = s->face->foreground;
1003 xgcv.foreground = s->face->background;
1006 IF_DEBUG (x_check_font (s->f, s->font));
1007 xgcv.font = s->font;
1008 mask = GCForeground | GCBackground | GCFont;
1010 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1011 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1012 mask, &xgcv);
1013 else
1014 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1015 = XCreateGC (NULL, FRAME_W32_WINDOW (s->f), mask, &xgcv);
1017 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1022 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1024 static void
1025 x_set_mouse_face_gc (struct glyph_string *s)
1027 int face_id;
1028 struct face *face;
1030 /* What face has to be used last for the mouse face? */
1031 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1032 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1033 if (face == NULL)
1034 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1036 if (s->first_glyph->type == CHAR_GLYPH)
1037 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1038 else
1039 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1040 s->face = FACE_FROM_ID (s->f, face_id);
1041 prepare_face_for_display (s->f, s->face);
1043 /* If font in this face is same as S->font, use it. */
1044 if (s->font == s->face->font)
1045 s->gc = s->face->gc;
1046 else
1048 /* Otherwise construct scratch_cursor_gc with values from FACE
1049 but font FONT. */
1050 XGCValues xgcv;
1051 unsigned long mask;
1053 xgcv.background = s->face->background;
1054 xgcv.foreground = s->face->foreground;
1055 IF_DEBUG (x_check_font (s->f, s->font));
1056 xgcv.font = s->font;
1057 mask = GCForeground | GCBackground | GCFont;
1059 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1060 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1061 mask, &xgcv);
1062 else
1063 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1064 = XCreateGC (NULL, FRAME_W32_WINDOW (s->f), mask, &xgcv);
1066 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1069 eassert (s->gc != 0);
1073 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1074 Faces to use in the mode line have already been computed when the
1075 matrix was built, so there isn't much to do, here. */
1077 static inline void
1078 x_set_mode_line_face_gc (struct glyph_string *s)
1080 s->gc = s->face->gc;
1084 /* Set S->gc of glyph string S for drawing that glyph string. Set
1085 S->stippled_p to a non-zero value if the face of S has a stipple
1086 pattern. */
1088 static inline void
1089 x_set_glyph_string_gc (struct glyph_string *s)
1091 prepare_face_for_display (s->f, s->face);
1093 if (s->hl == DRAW_NORMAL_TEXT)
1095 s->gc = s->face->gc;
1096 s->stippled_p = s->face->stipple != 0;
1098 else if (s->hl == DRAW_INVERSE_VIDEO)
1100 x_set_mode_line_face_gc (s);
1101 s->stippled_p = s->face->stipple != 0;
1103 else if (s->hl == DRAW_CURSOR)
1105 x_set_cursor_gc (s);
1106 s->stippled_p = false;
1108 else if (s->hl == DRAW_MOUSE_FACE)
1110 x_set_mouse_face_gc (s);
1111 s->stippled_p = s->face->stipple != 0;
1113 else if (s->hl == DRAW_IMAGE_RAISED
1114 || s->hl == DRAW_IMAGE_SUNKEN)
1116 s->gc = s->face->gc;
1117 s->stippled_p = s->face->stipple != 0;
1119 else
1120 emacs_abort ();
1122 /* GC must have been set. */
1123 eassert (s->gc != 0);
1127 /* Set clipping for output of glyph string S. S may be part of a mode
1128 line or menu if we don't have X toolkit support. */
1130 static inline void
1131 x_set_glyph_string_clipping (struct glyph_string *s)
1133 RECT *r = s->clip;
1134 int n = get_glyph_string_clip_rects (s, r, 2);
1136 if (n == 1)
1137 w32_set_clip_rectangle (s->hdc, r);
1138 else if (n > 1)
1140 HRGN clip1 = CreateRectRgnIndirect (r);
1141 HRGN clip2 = CreateRectRgnIndirect (r + 1);
1142 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
1143 SelectClipRgn (s->hdc, clip1);
1144 DeleteObject (clip1);
1145 DeleteObject (clip2);
1147 s->num_clips = n;
1150 /* Set SRC's clipping for output of glyph string DST. This is called
1151 when we are drawing DST's left_overhang or right_overhang only in
1152 the area of SRC. */
1154 static void
1155 x_set_glyph_string_clipping_exactly (struct glyph_string *src,
1156 struct glyph_string *dst)
1158 RECT r;
1160 r.left = src->x;
1161 r.right = r.left + src->width;
1162 r.top = src->y;
1163 r.bottom = r.top + src->height;
1164 dst->clip[0] = r;
1165 dst->num_clips = 1;
1166 w32_set_clip_rectangle (dst->hdc, &r);
1169 /* RIF:
1170 Compute left and right overhang of glyph string S. */
1172 static void
1173 w32_compute_glyph_string_overhangs (struct glyph_string *s)
1175 if (s->cmp == NULL
1176 && s->first_glyph->type == CHAR_GLYPH
1177 && !s->font_not_found_p)
1179 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1180 struct font *font = s->font;
1181 struct font_metrics metrics;
1182 int i;
1184 for (i = 0; i < s->nchars; i++)
1185 code[i] = s->char2b[i];
1186 font->driver->text_extents (font, code, s->nchars, &metrics);
1187 s->right_overhang = (metrics.rbearing > metrics.width
1188 ? metrics.rbearing - metrics.width : 0);
1189 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1191 else if (s->cmp)
1193 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1194 s->left_overhang = -s->cmp->lbearing;
1198 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1200 static inline void
1201 x_clear_glyph_string_rect (struct glyph_string *s,
1202 int x, int y, int w, int h)
1204 int real_x = x;
1205 int real_y = y;
1206 int real_w = w;
1207 int real_h = h;
1208 #if 0
1209 /* Take clipping into account. */
1210 if (s->gc->clip_mask == Rect)
1212 real_x = max (real_x, s->gc->clip_rectangle.left);
1213 real_y = max (real_y, s->gc->clip_rectangle.top);
1214 real_w = min (real_w, s->gc->clip_rectangle.right
1215 - s->gc->clip_rectangle.left);
1216 real_h = min (real_h, s->gc->clip_rectangle.bottom
1217 - s->gc->clip_rectangle.top);
1219 #endif
1220 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1221 real_w, real_h);
1225 /* Draw the background of glyph_string S. If S->background_filled_p
1226 is non-zero don't draw it. FORCE_P non-zero means draw the
1227 background even if it wouldn't be drawn normally. This is used
1228 when a string preceding S draws into the background of S, or S
1229 contains the first component of a composition. */
1231 static void
1232 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1234 /* Nothing to do if background has already been drawn or if it
1235 shouldn't be drawn in the first place. */
1236 if (!s->background_filled_p)
1238 int box_line_width = max (s->face->box_line_width, 0);
1240 #if 0 /* TODO: stipple */
1241 if (s->stippled_p)
1243 /* Fill background with a stipple pattern. */
1244 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1245 XFillRectangle (s->display, FRAME_W32_WINDOW (s->f), s->gc, s->x,
1246 s->y + box_line_width,
1247 s->background_width,
1248 s->height - 2 * box_line_width);
1249 XSetFillStyle (s->display, s->gc, FillSolid);
1250 s->background_filled_p = true;
1252 else
1253 #endif
1254 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1255 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1256 font dimensions, since the actual glyphs might be
1257 much smaller. So in that case we always clear the
1258 rectangle with background color. */
1259 || FONT_TOO_HIGH (s->font)
1260 || s->font_not_found_p
1261 || s->extends_to_end_of_line_p
1262 || force_p)
1264 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1265 s->background_width,
1266 s->height - 2 * box_line_width);
1267 s->background_filled_p = true;
1273 /* Draw the foreground of glyph string S. */
1275 static void
1276 x_draw_glyph_string_foreground (struct glyph_string *s)
1278 int i, x;
1280 /* If first glyph of S has a left box line, start drawing the text
1281 of S to the right of that box line. */
1282 if (s->face->box != FACE_NO_BOX
1283 && s->first_glyph->left_box_line_p)
1284 x = s->x + eabs (s->face->box_line_width);
1285 else
1286 x = s->x;
1288 SetTextColor (s->hdc, s->gc->foreground);
1289 SetBkColor (s->hdc, s->gc->background);
1290 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1292 /* Draw characters of S as rectangles if S's font could not be
1293 loaded. */
1294 if (s->font_not_found_p)
1296 for (i = 0; i < s->nchars; ++i)
1298 struct glyph *g = s->first_glyph + i;
1300 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1301 s->height - 1);
1302 x += g->pixel_width;
1305 else
1307 struct font *font = s->font;
1308 int boff = font->baseline_offset;
1309 int y;
1310 HFONT old_font;
1312 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1314 if (font->vertical_centering)
1315 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1317 y = s->ybase - boff;
1318 if (s->for_overlaps
1319 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1320 font->driver->draw (s, 0, s->nchars, x, y, false);
1321 else
1322 font->driver->draw (s, 0, s->nchars, x, y, true);
1323 if (s->face->overstrike)
1324 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1326 SelectObject (s->hdc, old_font);
1330 /* Draw the foreground of composite glyph string S. */
1332 static void
1333 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1335 int i, j, x;
1336 struct font *font = s->font;
1338 /* If first glyph of S has a left box line, start drawing the text
1339 of S to the right of that box line. */
1340 if (s->face && s->face->box != FACE_NO_BOX
1341 && s->first_glyph->left_box_line_p)
1342 x = s->x + eabs (s->face->box_line_width);
1343 else
1344 x = s->x;
1346 /* S is a glyph string for a composition. S->cmp_from is the index
1347 of the first character drawn for glyphs of this composition.
1348 S->cmp_from == 0 means we are drawing the very first character of
1349 this composition. */
1351 SetTextColor (s->hdc, s->gc->foreground);
1352 SetBkColor (s->hdc, s->gc->background);
1353 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1355 /* Draw a rectangle for the composition if the font for the very
1356 first character of the composition could not be loaded. */
1357 if (s->font_not_found_p)
1359 if (s->cmp_from == 0)
1360 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1361 s->height - 1);
1363 else if (! s->first_glyph->u.cmp.automatic)
1365 int y = s->ybase;
1366 HFONT old_font;
1368 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1370 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1371 /* TAB in a composition means display glyphs with padding
1372 space on the left or right. */
1373 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1375 int xx = x + s->cmp->offsets[j * 2];
1376 int yy = y - s->cmp->offsets[j * 2 + 1];
1378 font->driver->draw (s, j, j + 1, xx, yy, false);
1379 if (s->face->overstrike)
1380 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1382 SelectObject (s->hdc, old_font);
1384 else
1386 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1387 Lisp_Object glyph;
1388 int y = s->ybase;
1389 int width = 0;
1390 HFONT old_font;
1392 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1394 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1396 glyph = LGSTRING_GLYPH (gstring, i);
1397 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1398 width += LGLYPH_WIDTH (glyph);
1399 else
1401 int xoff, yoff, wadjust;
1403 if (j < i)
1405 font->driver->draw (s, j, i, x, y, false);
1406 x += width;
1408 xoff = LGLYPH_XOFF (glyph);
1409 yoff = LGLYPH_YOFF (glyph);
1410 wadjust = LGLYPH_WADJUST (glyph);
1411 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1412 x += wadjust;
1413 j = i + 1;
1414 width = 0;
1417 if (j < i)
1418 font->driver->draw (s, j, i, x, y, false);
1420 SelectObject (s->hdc, old_font);
1425 /* Draw the foreground of glyph string S for glyphless characters. */
1427 static void
1428 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1430 struct glyph *glyph = s->first_glyph;
1431 XChar2b char2b[8];
1432 int x, i, j;
1433 bool with_background;
1435 /* If first glyph of S has a left box line, start drawing the text
1436 of S to the right of that box line. */
1437 if (s->face->box != FACE_NO_BOX
1438 && s->first_glyph->left_box_line_p)
1439 x = s->x + eabs (s->face->box_line_width);
1440 else
1441 x = s->x;
1443 SetTextColor (s->hdc, s->gc->foreground);
1444 SetBkColor (s->hdc, s->gc->background);
1445 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1447 s->char2b = char2b;
1448 with_background = ((s->for_overlaps
1449 || (s->background_filled_p && s->hl != DRAW_CURSOR))) == 0;
1450 for (i = 0; i < s->nchars; i++, glyph++)
1452 char buf[7], *str = NULL;
1453 int len = glyph->u.glyphless.len;
1455 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1457 if (len > 0
1458 && CHAR_TABLE_P (Vglyphless_char_display)
1459 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1460 >= 1))
1462 Lisp_Object acronym
1463 = (! glyph->u.glyphless.for_no_font
1464 ? CHAR_TABLE_REF (Vglyphless_char_display,
1465 glyph->u.glyphless.ch)
1466 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1467 if (STRINGP (acronym))
1468 str = SSDATA (acronym);
1471 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1473 sprintf ((char *) buf, "%0*X",
1474 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1475 (unsigned int) glyph->u.glyphless.ch);
1476 str = buf;
1479 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1480 w32_draw_rectangle (s->hdc, s->gc,
1481 x, s->ybase - glyph->ascent,
1482 glyph->pixel_width - 1,
1483 glyph->ascent + glyph->descent - 1);
1484 if (str)
1486 struct font *font = s->font;
1487 int upper_len = (len + 1) / 2;
1488 unsigned code;
1489 HFONT old_font;
1491 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1492 /* It is certain that all LEN characters in STR are ASCII. */
1493 for (j = 0; j < len; j++)
1495 code = font->driver->encode_char (font, str[j]);
1496 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1498 font->driver->draw (s, 0, upper_len,
1499 x + glyph->slice.glyphless.upper_xoff,
1500 s->ybase + glyph->slice.glyphless.upper_yoff,
1501 with_background);
1502 font->driver->draw (s, upper_len, len,
1503 x + glyph->slice.glyphless.lower_xoff,
1504 s->ybase + glyph->slice.glyphless.lower_yoff,
1505 with_background);
1506 SelectObject (s->hdc, old_font);
1508 x += glyph->pixel_width;
1513 /* Brightness beyond which a color won't have its highlight brightness
1514 boosted.
1516 Nominally, highlight colors for `3d' faces are calculated by
1517 brightening an object's color by a constant scale factor, but this
1518 doesn't yield good results for dark colors, so for colors whose
1519 brightness is less than this value (on a scale of 0-255) have to
1520 use an additional additive factor.
1522 The value here is set so that the default menu-bar/mode-line color
1523 (grey75) will not have its highlights changed at all. */
1524 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1527 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1528 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1529 If this produces the same color as COLOR, try a color where all RGB
1530 values have DELTA added. Return the allocated color in *COLOR.
1531 DISPLAY is the X display, CMAP is the colormap to operate on.
1532 Value is non-zero if successful. */
1534 static int
1535 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1536 double factor, int delta)
1538 COLORREF new;
1539 long bright;
1541 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1542 delta /= 256;
1544 /* Change RGB values by specified FACTOR. Avoid overflow! */
1545 eassert (factor >= 0);
1546 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1547 min (0xff, factor * GetGValue (*color)),
1548 min (0xff, factor * GetBValue (*color)));
1550 /* Calculate brightness of COLOR. */
1551 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1552 + GetBValue (*color)) / 6;
1554 /* We only boost colors that are darker than
1555 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1556 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1557 /* Make an additive adjustment to NEW, because it's dark enough so
1558 that scaling by FACTOR alone isn't enough. */
1560 /* How far below the limit this color is (0 - 1, 1 being darker). */
1561 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1562 /* The additive adjustment. */
1563 int min_delta = delta * dimness * factor / 2;
1565 if (factor < 1)
1566 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1567 max (0, min (0xff, min_delta - GetGValue (*color))),
1568 max (0, min (0xff, min_delta - GetBValue (*color))));
1569 else
1570 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1571 max (0, min (0xff, min_delta + GetGValue (*color))),
1572 max (0, min (0xff, min_delta + GetBValue (*color))));
1575 if (new == *color)
1576 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1577 max (0, min (0xff, delta + GetGValue (*color))),
1578 max (0, min (0xff, delta + GetBValue (*color))));
1580 /* TODO: Map to palette and retry with delta if same? */
1581 /* TODO: Free colors (if using palette)? */
1583 if (new == *color)
1584 return 0;
1586 *color = new;
1588 return 1;
1591 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1592 colors in COLORS. On W32, we no longer try to map colors to
1593 a palette. */
1594 void
1595 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1597 int i;
1599 for (i = 0; i < ncolors; i++)
1601 DWORD pixel = colors[i].pixel;
1602 /* Convert to a 16 bit value in range 0 - 0xffff. */
1603 colors[i].red = GetRValue (pixel) * 257;
1604 colors[i].green = GetGValue (pixel) * 257;
1605 colors[i].blue = GetBValue (pixel) * 257;
1609 void
1610 x_query_color (struct frame *f, XColor *color)
1612 x_query_colors (f, color, 1);
1616 /* Set up the foreground color for drawing relief lines of glyph
1617 string S. RELIEF is a pointer to a struct relief containing the GC
1618 with which lines will be drawn. Use a color that is FACTOR or
1619 DELTA lighter or darker than the relief's background which is found
1620 in S->f->output_data.x->relief_background. If such a color cannot
1621 be allocated, use DEFAULT_PIXEL, instead. */
1623 static void
1624 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1625 int delta, COLORREF default_pixel)
1627 XGCValues xgcv;
1628 struct w32_output *di = f->output_data.w32;
1629 unsigned long mask = GCForeground;
1630 COLORREF pixel;
1631 COLORREF background = di->relief_background;
1632 #if 0
1633 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1634 #endif
1636 /* TODO: Free colors (if using palette)? */
1638 /* Allocate new color. */
1639 xgcv.foreground = default_pixel;
1640 pixel = background;
1641 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1642 xgcv.foreground = relief->pixel = pixel;
1644 xgcv.font = NULL; /* avoid compiler warnings */
1645 if (relief->gc == 0)
1647 #if 0 /* TODO: stipple */
1648 xgcv.stipple = dpyinfo->gray;
1649 mask |= GCStipple;
1650 #endif
1651 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1653 else
1654 XChangeGC (NULL, relief->gc, mask, &xgcv);
1658 /* Set up colors for the relief lines around glyph string S. */
1660 static void
1661 x_setup_relief_colors (struct glyph_string *s)
1663 struct w32_output *di = s->f->output_data.w32;
1664 COLORREF color;
1666 if (s->face->use_box_color_for_shadows_p)
1667 color = s->face->box_color;
1668 else if (s->first_glyph->type == IMAGE_GLYPH
1669 && s->img->pixmap
1670 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1671 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1672 else
1673 color = s->gc->background;
1675 if (di->white_relief.gc == 0
1676 || color != di->relief_background)
1678 di->relief_background = color;
1679 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1680 WHITE_PIX_DEFAULT (s->f));
1681 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1682 BLACK_PIX_DEFAULT (s->f));
1687 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1688 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1689 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1690 relief. LEFT_P non-zero means draw a relief on the left side of
1691 the rectangle. RIGHT_P non-zero means draw a relief on the right
1692 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1693 when drawing. */
1695 static void
1696 w32_draw_relief_rect (struct frame *f,
1697 int left_x, int top_y, int right_x, int bottom_y,
1698 int width, int raised_p,
1699 int top_p, int bot_p, int left_p, int right_p,
1700 RECT *clip_rect)
1702 int i;
1703 XGCValues gc;
1704 HDC hdc = get_frame_dc (f);
1706 if (raised_p)
1707 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1708 else
1709 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1711 w32_set_clip_rectangle (hdc, clip_rect);
1713 /* Top. */
1714 if (top_p)
1715 for (i = 0; i < width; ++i)
1716 w32_fill_area (f, hdc, gc.foreground,
1717 left_x + i * left_p, top_y + i,
1718 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1720 /* Left. */
1721 if (left_p)
1722 for (i = 0; i < width; ++i)
1723 w32_fill_area (f, hdc, gc.foreground,
1724 left_x + i, top_y + (i + 1) * top_p, 1,
1725 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1727 if (raised_p)
1728 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1729 else
1730 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1732 /* Bottom. */
1733 if (bot_p)
1734 for (i = 0; i < width; ++i)
1735 w32_fill_area (f, hdc, gc.foreground,
1736 left_x + i * left_p, bottom_y - i,
1737 right_x - left_x - i * (left_p + right_p) + 1, 1);
1739 /* Right. */
1740 if (right_p)
1741 for (i = 0; i < width; ++i)
1742 w32_fill_area (f, hdc, gc.foreground,
1743 right_x - i, top_y + (i + 1) * top_p, 1,
1744 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1746 w32_set_clip_rectangle (hdc, NULL);
1748 release_frame_dc (f, hdc);
1752 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1753 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1754 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1755 left side of the rectangle. RIGHT_P non-zero means draw a line
1756 on the right side of the rectangle. CLIP_RECT is the clipping
1757 rectangle to use when drawing. */
1759 static void
1760 w32_draw_box_rect (struct glyph_string *s,
1761 int left_x, int top_y, int right_x, int bottom_y, int width,
1762 bool left_p, bool right_p, RECT *clip_rect)
1764 w32_set_clip_rectangle (s->hdc, clip_rect);
1766 /* Top. */
1767 w32_fill_area (s->f, s->hdc, s->face->box_color,
1768 left_x, top_y, right_x - left_x + 1, width);
1770 /* Left. */
1771 if (left_p)
1773 w32_fill_area (s->f, s->hdc, s->face->box_color,
1774 left_x, top_y, width, bottom_y - top_y + 1);
1777 /* Bottom. */
1778 w32_fill_area (s->f, s->hdc, s->face->box_color,
1779 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1781 /* Right. */
1782 if (right_p)
1784 w32_fill_area (s->f, s->hdc, s->face->box_color,
1785 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1788 w32_set_clip_rectangle (s->hdc, NULL);
1792 /* Draw a box around glyph string S. */
1794 static void
1795 x_draw_glyph_string_box (struct glyph_string *s)
1797 int width, left_x, right_x, top_y, bottom_y, last_x;
1798 bool left_p, right_p, raised_p;
1799 struct glyph *last_glyph;
1800 RECT clip_rect;
1802 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1803 ? WINDOW_RIGHT_EDGE_X (s->w)
1804 : window_box_right (s->w, s->area));
1806 /* The glyph that may have a right box line. */
1807 last_glyph = (s->cmp || s->img
1808 ? s->first_glyph
1809 : s->first_glyph + s->nchars - 1);
1811 width = eabs (s->face->box_line_width);
1812 raised_p = s->face->box == FACE_RAISED_BOX;
1813 left_x = s->x;
1814 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1815 ? last_x - 1
1816 : min (last_x, s->x + s->background_width) - 1));
1817 top_y = s->y;
1818 bottom_y = top_y + s->height - 1;
1820 left_p = (s->first_glyph->left_box_line_p
1821 || (s->hl == DRAW_MOUSE_FACE
1822 && (s->prev == NULL
1823 || s->prev->hl != s->hl)));
1824 right_p = (last_glyph->right_box_line_p
1825 || (s->hl == DRAW_MOUSE_FACE
1826 && (s->next == NULL
1827 || s->next->hl != s->hl)));
1829 get_glyph_string_clip_rect (s, &clip_rect);
1831 if (s->face->box == FACE_SIMPLE_BOX)
1832 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1833 left_p, right_p, &clip_rect);
1834 else
1836 x_setup_relief_colors (s);
1837 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1838 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1843 /* Draw foreground of image glyph string S. */
1845 static void
1846 x_draw_image_foreground (struct glyph_string *s)
1848 int x = s->x;
1849 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1851 /* If first glyph of S has a left box line, start drawing it to the
1852 right of that line. */
1853 if (s->face->box != FACE_NO_BOX
1854 && s->first_glyph->left_box_line_p
1855 && s->slice.x == 0)
1856 x += eabs (s->face->box_line_width);
1858 /* If there is a margin around the image, adjust x- and y-position
1859 by that margin. */
1860 if (s->slice.x == 0)
1861 x += s->img->hmargin;
1862 if (s->slice.y == 0)
1863 y += s->img->vmargin;
1865 SaveDC (s->hdc);
1867 if (s->img->pixmap)
1869 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1870 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1871 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1872 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1873 SetBkColor (compat_hdc, RGB (255, 255, 255));
1874 SetTextColor (s->hdc, RGB (0, 0, 0));
1875 x_set_glyph_string_clipping (s);
1877 if (s->img->mask)
1879 HDC mask_dc = CreateCompatibleDC (s->hdc);
1880 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1882 SetTextColor (s->hdc, RGB (255, 255, 255));
1883 SetBkColor (s->hdc, RGB (0, 0, 0));
1885 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1886 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1887 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1888 mask_dc, s->slice.x, s->slice.y, SRCAND);
1889 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1890 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1892 SelectObject (mask_dc, mask_orig_obj);
1893 DeleteDC (mask_dc);
1895 else
1897 SetTextColor (s->hdc, s->gc->foreground);
1898 SetBkColor (s->hdc, s->gc->background);
1900 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1901 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1903 /* When the image has a mask, we can expect that at
1904 least part of a mouse highlight or a block cursor will
1905 be visible. If the image doesn't have a mask, make
1906 a block cursor visible by drawing a rectangle around
1907 the image. I believe it's looking better if we do
1908 nothing here for mouse-face. */
1909 if (s->hl == DRAW_CURSOR)
1911 int r = s->img->relief;
1912 if (r < 0) r = -r;
1913 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1914 s->slice.width + r*2 - 1,
1915 s->slice.height + r*2 - 1);
1919 w32_set_clip_rectangle (s->hdc, NULL);
1920 SelectObject (s->hdc, orig_brush);
1921 DeleteObject (fg_brush);
1922 SelectObject (compat_hdc, orig_obj);
1923 DeleteDC (compat_hdc);
1925 else
1926 w32_draw_rectangle (s->hdc, s->gc, x, y,
1927 s->slice.width - 1, s->slice.height - 1);
1929 RestoreDC (s->hdc ,-1);
1933 /* Draw a relief around the image glyph string S. */
1935 static void
1936 x_draw_image_relief (struct glyph_string *s)
1938 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
1939 int extra_x, extra_y;
1940 RECT r;
1941 int x = s->x;
1942 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1944 /* If first glyph of S has a left box line, start drawing it to the
1945 right of that line. */
1946 if (s->face->box != FACE_NO_BOX
1947 && s->first_glyph->left_box_line_p
1948 && s->slice.x == 0)
1949 x += eabs (s->face->box_line_width);
1951 /* If there is a margin around the image, adjust x- and y-position
1952 by that margin. */
1953 if (s->slice.x == 0)
1954 x += s->img->hmargin;
1955 if (s->slice.y == 0)
1956 y += s->img->vmargin;
1958 if (s->hl == DRAW_IMAGE_SUNKEN
1959 || s->hl == DRAW_IMAGE_RAISED)
1961 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief
1962 : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1963 raised_p = s->hl == DRAW_IMAGE_RAISED;
1965 else
1967 thick = eabs (s->img->relief);
1968 raised_p = s->img->relief > 0;
1971 x1 = x + s->slice.width - 1;
1972 y1 = y + s->slice.height - 1;
1974 extra_x = extra_y = 0;
1975 if (s->face->id == TOOL_BAR_FACE_ID)
1977 if (CONSP (Vtool_bar_button_margin)
1978 && INTEGERP (XCAR (Vtool_bar_button_margin))
1979 && INTEGERP (XCDR (Vtool_bar_button_margin)))
1981 extra_x = XINT (XCAR (Vtool_bar_button_margin));
1982 extra_y = XINT (XCDR (Vtool_bar_button_margin));
1984 else if (INTEGERP (Vtool_bar_button_margin))
1985 extra_x = extra_y = XINT (Vtool_bar_button_margin);
1988 top_p = bot_p = left_p = right_p = 0;
1990 if (s->slice.x == 0)
1991 x -= thick + extra_x, left_p = 1;
1992 if (s->slice.y == 0)
1993 y -= thick + extra_y, top_p = 1;
1994 if (s->slice.x + s->slice.width == s->img->width)
1995 x1 += thick + extra_x, right_p = 1;
1996 if (s->slice.y + s->slice.height == s->img->height)
1997 y1 += thick + extra_y, bot_p = 1;
1999 x_setup_relief_colors (s);
2000 get_glyph_string_clip_rect (s, &r);
2001 w32_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2002 top_p, bot_p, left_p, right_p, &r);
2006 /* Draw the foreground of image glyph string S to PIXMAP. */
2008 static void
2009 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
2011 HDC hdc = CreateCompatibleDC (s->hdc);
2012 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
2013 int x = 0;
2014 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2016 /* If first glyph of S has a left box line, start drawing it to the
2017 right of that line. */
2018 if (s->face->box != FACE_NO_BOX
2019 && s->first_glyph->left_box_line_p
2020 && s->slice.x == 0)
2021 x += eabs (s->face->box_line_width);
2023 /* If there is a margin around the image, adjust x- and y-position
2024 by that margin. */
2025 if (s->slice.x == 0)
2026 x += s->img->hmargin;
2027 if (s->slice.y == 0)
2028 y += s->img->vmargin;
2030 if (s->img->pixmap)
2032 HDC compat_hdc = CreateCompatibleDC (hdc);
2033 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2034 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
2035 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
2037 if (s->img->mask)
2039 HDC mask_dc = CreateCompatibleDC (hdc);
2040 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
2042 SetTextColor (hdc, RGB (0, 0, 0));
2043 SetBkColor (hdc, RGB (255, 255, 255));
2044 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2045 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2046 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2047 mask_dc, s->slice.x, s->slice.y, SRCAND);
2048 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2049 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2051 SelectObject (mask_dc, mask_orig_obj);
2052 DeleteDC (mask_dc);
2054 else
2056 SetTextColor (hdc, s->gc->foreground);
2057 SetBkColor (hdc, s->gc->background);
2059 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2060 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2062 /* When the image has a mask, we can expect that at
2063 least part of a mouse highlight or a block cursor will
2064 be visible. If the image doesn't have a mask, make
2065 a block cursor visible by drawing a rectangle around
2066 the image. I believe it's looking better if we do
2067 nothing here for mouse-face. */
2068 if (s->hl == DRAW_CURSOR)
2070 int r = s->img->relief;
2071 if (r < 0) r = -r;
2072 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2073 s->slice.width + r*2 - 1,
2074 s->slice.height + r*2 - 1);
2078 SelectObject (hdc, orig_brush);
2079 DeleteObject (fg_brush);
2080 SelectObject (compat_hdc, orig_obj);
2081 DeleteDC (compat_hdc);
2083 else
2084 w32_draw_rectangle (hdc, s->gc, x, y,
2085 s->slice.width - 1, s->slice.height - 1);
2087 SelectObject (hdc, orig_hdc_obj);
2088 DeleteDC (hdc);
2092 /* Draw part of the background of glyph string S. X, Y, W, and H
2093 give the rectangle to draw. */
2095 static void
2096 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2098 #if 0 /* TODO: stipple */
2099 if (s->stippled_p)
2101 /* Fill background with a stipple pattern. */
2102 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2103 XFillRectangle (s->display, FRAME_W32_WINDOW (s->f), s->gc, x, y, w, h);
2104 XSetFillStyle (s->display, s->gc, FillSolid);
2106 else
2107 #endif
2108 x_clear_glyph_string_rect (s, x, y, w, h);
2112 /* Draw image glyph string S.
2114 s->y
2115 s->x +-------------------------
2116 | s->face->box
2118 | +-------------------------
2119 | | s->img->vmargin
2121 | | +-------------------
2122 | | | the image
2126 static void
2127 x_draw_image_glyph_string (struct glyph_string *s)
2129 int x, y;
2130 int box_line_hwidth = eabs (s->face->box_line_width);
2131 int box_line_vwidth = max (s->face->box_line_width, 0);
2132 int height, width;
2133 HBITMAP pixmap = 0;
2135 height = s->height;
2136 if (s->slice.y == 0)
2137 height -= box_line_vwidth;
2138 if (s->slice.y + s->slice.height >= s->img->height)
2139 height -= box_line_vwidth;
2141 /* Fill background with face under the image. Do it only if row is
2142 taller than image or if image has a clip mask to reduce
2143 flickering. */
2144 s->stippled_p = s->face->stipple != 0;
2145 if (height > s->slice.height
2146 || s->img->hmargin
2147 || s->img->vmargin
2148 || s->img->mask
2149 || s->img->pixmap == 0
2150 || s->width != s->background_width)
2152 width = s->background_width;
2153 x = s->x;
2154 if (s->first_glyph->left_box_line_p
2155 && s->slice.x == 0)
2157 x += box_line_hwidth;
2158 width -= box_line_hwidth;
2161 y = s->y;
2162 if (s->slice.y == 0)
2163 y += box_line_vwidth;
2165 #if 0 /* TODO: figure out if we need to do this on Windows. */
2166 if (s->img->mask)
2168 /* Create a pixmap as large as the glyph string. Fill it
2169 with the background color. Copy the image to it, using
2170 its mask. Copy the temporary pixmap to the display. */
2171 Screen *screen = FRAME_X_SCREEN (s->f);
2172 int depth = DefaultDepthOfScreen (screen);
2174 /* Create a pixmap as large as the glyph string. */
2175 pixmap = XCreatePixmap (s->display, FRAME_W32_WINDOW (s->f),
2176 s->background_width,
2177 s->height, depth);
2179 /* Don't clip in the following because we're working on the
2180 pixmap. */
2181 XSetClipMask (s->display, s->gc, None);
2183 /* Fill the pixmap with the background color/stipple. */
2184 if (s->stippled_p)
2186 /* Fill background with a stipple pattern. */
2187 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2188 XFillRectangle (s->display, pixmap, s->gc,
2189 0, 0, s->background_width, s->height);
2190 XSetFillStyle (s->display, s->gc, FillSolid);
2192 else
2194 XGCValues xgcv;
2195 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2196 &xgcv);
2197 XSetForeground (s->display, s->gc, xgcv.background);
2198 XFillRectangle (s->display, pixmap, s->gc,
2199 0, 0, s->background_width, s->height);
2200 XSetForeground (s->display, s->gc, xgcv.foreground);
2203 else
2204 #endif
2205 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2207 s->background_filled_p = true;
2210 /* Draw the foreground. */
2211 if (pixmap != 0)
2213 w32_draw_image_foreground_1 (s, pixmap);
2214 x_set_glyph_string_clipping (s);
2216 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2217 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2218 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2219 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2221 SetTextColor (s->hdc, s->gc->foreground);
2222 SetBkColor (s->hdc, s->gc->background);
2223 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2224 compat_hdc, 0, 0, SRCCOPY);
2226 SelectObject (s->hdc, orig_brush);
2227 DeleteObject (fg_brush);
2228 SelectObject (compat_hdc, orig_obj);
2229 DeleteDC (compat_hdc);
2231 DeleteObject (pixmap);
2232 pixmap = 0;
2234 else
2235 x_draw_image_foreground (s);
2237 /* If we must draw a relief around the image, do it. */
2238 if (s->img->relief
2239 || s->hl == DRAW_IMAGE_RAISED
2240 || s->hl == DRAW_IMAGE_SUNKEN)
2241 x_draw_image_relief (s);
2245 /* Draw stretch glyph string S. */
2247 static void
2248 x_draw_stretch_glyph_string (struct glyph_string *s)
2250 eassert (s->first_glyph->type == STRETCH_GLYPH);
2252 if (s->hl == DRAW_CURSOR
2253 && !x_stretch_cursor_p)
2255 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2256 wide as the stretch glyph. */
2257 int width, background_width = s->background_width;
2258 int x = s->x;
2260 if (!s->row->reversed_p)
2262 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2264 if (x < left_x)
2266 background_width -= left_x - x;
2267 x = left_x;
2270 else
2272 /* In R2L rows, draw the cursor on the right edge of the
2273 stretch glyph. */
2274 int right_x = window_box_right (s->w, TEXT_AREA);
2276 if (x + background_width > right_x)
2277 background_width -= x - right_x;
2278 x += background_width;
2280 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2281 if (s->row->reversed_p)
2282 x -= width;
2284 /* Draw cursor. */
2285 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2287 /* Clear rest using the GC of the original non-cursor face. */
2288 if (width < background_width)
2290 XGCValues *gc = s->face->gc;
2291 int y = s->y;
2292 int w = background_width - width, h = s->height;
2293 RECT r;
2294 HDC hdc = s->hdc;
2296 if (!s->row->reversed_p)
2297 x += width;
2298 else
2299 x = s->x;
2300 if (s->row->mouse_face_p
2301 && cursor_in_mouse_face_p (s->w))
2303 x_set_mouse_face_gc (s);
2304 gc = s->gc;
2306 else
2307 gc = s->face->gc;
2309 get_glyph_string_clip_rect (s, &r);
2310 w32_set_clip_rectangle (hdc, &r);
2312 #if 0 /* TODO: stipple */
2313 if (s->face->stipple)
2315 /* Fill background with a stipple pattern. */
2316 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2317 XFillRectangle (s->display, FRAME_W32_WINDOW (s->f), gc, x, y, w, h);
2318 XSetFillStyle (s->display, gc, FillSolid);
2320 else
2321 #endif
2323 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2327 else if (!s->background_filled_p)
2329 int background_width = s->background_width;
2330 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2332 /* Don't draw into left margin, fringe or scrollbar area
2333 except for header line and mode line. */
2334 if (x < left_x && !s->row->mode_line_p)
2336 background_width -= left_x - x;
2337 x = left_x;
2339 if (background_width > 0)
2340 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2343 s->background_filled_p = true;
2347 /* Draw glyph string S. */
2349 static void
2350 x_draw_glyph_string (struct glyph_string *s)
2352 bool relief_drawn_p = 0;
2354 /* If S draws into the background of its successor, draw the
2355 background of the successor first so that S can draw into it.
2356 This makes S->next use XDrawString instead of XDrawImageString. */
2357 if (s->next && s->right_overhang && !s->for_overlaps)
2359 int width;
2360 struct glyph_string *next;
2361 for (width = 0, next = s->next;
2362 next && width < s->right_overhang;
2363 width += next->width, next = next->next)
2364 if (next->first_glyph->type != IMAGE_GLYPH)
2366 x_set_glyph_string_gc (next);
2367 x_set_glyph_string_clipping (next);
2368 if (next->first_glyph->type == STRETCH_GLYPH)
2369 x_draw_stretch_glyph_string (next);
2370 else
2371 x_draw_glyph_string_background (next, true);
2372 next->num_clips = 0;
2376 /* Set up S->gc, set clipping and draw S. */
2377 x_set_glyph_string_gc (s);
2379 /* Draw relief (if any) in advance for char/composition so that the
2380 glyph string can be drawn over it. */
2381 if (!s->for_overlaps
2382 && s->face->box != FACE_NO_BOX
2383 && (s->first_glyph->type == CHAR_GLYPH
2384 || s->first_glyph->type == COMPOSITE_GLYPH))
2387 x_set_glyph_string_clipping (s);
2388 x_draw_glyph_string_background (s, true);
2389 x_draw_glyph_string_box (s);
2390 x_set_glyph_string_clipping (s);
2391 relief_drawn_p = 1;
2393 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2394 && !s->clip_tail
2395 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2396 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2397 /* We must clip just this glyph. left_overhang part has already
2398 drawn when s->prev was drawn, and right_overhang part will be
2399 drawn later when s->next is drawn. */
2400 x_set_glyph_string_clipping_exactly (s, s);
2401 else
2402 x_set_glyph_string_clipping (s);
2404 switch (s->first_glyph->type)
2406 case IMAGE_GLYPH:
2407 x_draw_image_glyph_string (s);
2408 break;
2410 case STRETCH_GLYPH:
2411 x_draw_stretch_glyph_string (s);
2412 break;
2414 case CHAR_GLYPH:
2415 if (s->for_overlaps)
2416 s->background_filled_p = true;
2417 else
2418 x_draw_glyph_string_background (s, false);
2419 x_draw_glyph_string_foreground (s);
2420 break;
2422 case COMPOSITE_GLYPH:
2423 if (s->for_overlaps || (s->cmp_from > 0
2424 && ! s->first_glyph->u.cmp.automatic))
2425 s->background_filled_p = true;
2426 else
2427 x_draw_glyph_string_background (s, true);
2428 x_draw_composite_glyph_string_foreground (s);
2429 break;
2431 case GLYPHLESS_GLYPH:
2432 if (s->for_overlaps)
2433 s->background_filled_p = true;
2434 else
2435 x_draw_glyph_string_background (s, false);
2436 x_draw_glyphless_glyph_string_foreground (s);
2437 break;
2439 default:
2440 emacs_abort ();
2443 if (!s->for_overlaps)
2445 /* Draw underline. */
2446 if (s->face->underline_p)
2448 if (s->face->underline_type == FACE_UNDER_WAVE)
2450 COLORREF color;
2452 if (s->face->underline_defaulted_p)
2453 color = s->gc->foreground;
2454 else
2455 color = s->face->underline_color;
2457 w32_draw_underwave (s, color);
2459 else if (s->face->underline_type == FACE_UNDER_LINE)
2461 unsigned long thickness, position;
2462 int y;
2464 if (s->prev && s->prev->face->underline_p
2465 && s->prev->face->underline_type == FACE_UNDER_LINE)
2467 /* We use the same underline style as the previous one. */
2468 thickness = s->prev->underline_thickness;
2469 position = s->prev->underline_position;
2471 else
2473 struct font *font = font_for_underline_metrics (s);
2475 /* Get the underline thickness. Default is 1 pixel. */
2476 if (font && font->underline_thickness > 0)
2477 thickness = font->underline_thickness;
2478 else
2479 thickness = 1;
2480 if (x_underline_at_descent_line || !font)
2481 position = (s->height - thickness) - (s->ybase - s->y);
2482 else
2484 /* Get the underline position. This is the recommended
2485 vertical offset in pixels from the baseline to the top of
2486 the underline. This is a signed value according to the
2487 specs, and its default is
2489 ROUND ((maximum_descent) / 2), with
2490 ROUND (x) = floor (x + 0.5) */
2492 if (x_use_underline_position_properties
2493 && font->underline_position >= 0)
2494 position = font->underline_position;
2495 else
2496 position = (font->descent + 1) / 2;
2498 position = max (position, underline_minimum_offset);
2500 /* Check the sanity of thickness and position. We should
2501 avoid drawing underline out of the current line area. */
2502 if (s->y + s->height <= s->ybase + position)
2503 position = (s->height - 1) - (s->ybase - s->y);
2504 if (s->y + s->height < s->ybase + position + thickness)
2505 thickness = (s->y + s->height) - (s->ybase + position);
2506 s->underline_thickness = thickness;
2507 s->underline_position = position;
2508 y = s->ybase + position;
2509 if (s->face->underline_defaulted_p)
2511 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2512 y, s->width, 1);
2514 else
2516 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2517 y, s->width, 1);
2521 /* Draw overline. */
2522 if (s->face->overline_p)
2524 unsigned long dy = 0, h = 1;
2526 if (s->face->overline_color_defaulted_p)
2528 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2529 s->y + dy, s->width, h);
2531 else
2533 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2534 s->y + dy, s->width, h);
2538 /* Draw strike-through. */
2539 if (s->face->strike_through_p
2540 && !FONT_TEXTMETRIC (s->font).tmStruckOut)
2542 /* Y-coordinate and height of the glyph string's first
2543 glyph. We cannot use s->y and s->height because those
2544 could be larger if there are taller display elements
2545 (e.g., characters displayed with a larger font) in the
2546 same glyph row. */
2547 int glyph_y = s->ybase - s->first_glyph->ascent;
2548 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
2549 /* Strike-through width and offset from the glyph string's
2550 top edge. */
2551 unsigned long h = 1;
2552 unsigned long dy = (glyph_height - h) / 2;
2554 if (s->face->strike_through_color_defaulted_p)
2556 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2557 glyph_y + dy, s->width, h);
2559 else
2561 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2562 glyph_y + dy, s->width, h);
2566 /* Draw relief if not yet drawn. */
2567 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2568 x_draw_glyph_string_box (s);
2570 if (s->prev)
2572 struct glyph_string *prev;
2574 for (prev = s->prev; prev; prev = prev->prev)
2575 if (prev->hl != s->hl
2576 && prev->x + prev->width + prev->right_overhang > s->x)
2578 /* As prev was drawn while clipped to its own area, we
2579 must draw the right_overhang part using s->hl now. */
2580 enum draw_glyphs_face save = prev->hl;
2582 prev->hl = s->hl;
2583 x_set_glyph_string_gc (prev);
2584 x_set_glyph_string_clipping_exactly (s, prev);
2585 if (prev->first_glyph->type == CHAR_GLYPH)
2586 x_draw_glyph_string_foreground (prev);
2587 else
2588 x_draw_composite_glyph_string_foreground (prev);
2589 w32_set_clip_rectangle (prev->hdc, NULL);
2590 prev->hl = save;
2591 prev->num_clips = 0;
2595 if (s->next)
2597 struct glyph_string *next;
2599 for (next = s->next; next; next = next->next)
2600 if (next->hl != s->hl
2601 && next->x - next->left_overhang < s->x + s->width)
2603 /* As next will be drawn while clipped to its own area,
2604 we must draw the left_overhang part using s->hl now. */
2605 enum draw_glyphs_face save = next->hl;
2607 next->hl = s->hl;
2608 x_set_glyph_string_gc (next);
2609 x_set_glyph_string_clipping_exactly (s, next);
2610 if (next->first_glyph->type == CHAR_GLYPH)
2611 x_draw_glyph_string_foreground (next);
2612 else
2613 x_draw_composite_glyph_string_foreground (next);
2614 w32_set_clip_rectangle (next->hdc, NULL);
2615 next->hl = save;
2616 next->num_clips = 0;
2617 next->clip_head = s->next;
2622 /* Reset clipping. */
2623 w32_set_clip_rectangle (s->hdc, NULL);
2624 s->num_clips = 0;
2628 /* Shift display to make room for inserted glyphs. */
2630 static void
2631 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2632 int width, int height, int shift_by)
2634 HDC hdc;
2636 hdc = get_frame_dc (f);
2637 BitBlt (hdc, x + shift_by, y, width, height,
2638 hdc, x, y, SRCCOPY);
2640 release_frame_dc (f, hdc);
2644 /* Delete N glyphs at the nominal cursor position. Not implemented
2645 for X frames. */
2647 static void
2648 x_delete_glyphs (struct frame *f, register int n)
2650 if (! FRAME_W32_P (f))
2651 return;
2653 emacs_abort ();
2657 /* Clear entire frame. */
2659 static void
2660 x_clear_frame (struct frame *f)
2662 if (! FRAME_W32_P (f))
2663 return;
2665 /* Clearing the frame will erase any cursor, so mark them all as no
2666 longer visible. */
2667 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2669 block_input ();
2671 w32_clear_window (f);
2673 /* We have to clear the scroll bars, too. If we have changed
2674 colors or something like that, then they should be notified. */
2675 x_scroll_bar_clear (f);
2677 unblock_input ();
2681 /* Make audible bell. */
2683 static void
2684 w32_ring_bell (struct frame *f)
2686 block_input ();
2688 if (FRAME_W32_P (f) && visible_bell)
2690 int i;
2691 HWND hwnd = FRAME_W32_WINDOW (f);
2693 for (i = 0; i < 5; i++)
2695 FlashWindow (hwnd, TRUE);
2696 Sleep (10);
2698 FlashWindow (hwnd, FALSE);
2700 else
2701 w32_sys_ring_bell (f);
2703 unblock_input ();
2706 /***********************************************************************
2707 Line Dance
2708 ***********************************************************************/
2710 /* Perform an insert-lines or delete-lines operation, inserting N
2711 lines or deleting -N lines at vertical position VPOS. */
2713 static void
2714 x_ins_del_lines (struct frame *f, int vpos, int n)
2716 if (! FRAME_W32_P (f))
2717 return;
2719 emacs_abort ();
2723 /* Scroll part of the display as described by RUN. */
2725 static void
2726 x_scroll_run (struct window *w, struct run *run)
2728 struct frame *f = XFRAME (w->frame);
2729 int x, y, width, height, from_y, to_y, bottom_y;
2730 HWND hwnd = FRAME_W32_WINDOW (f);
2731 HRGN expect_dirty;
2733 /* Get frame-relative bounding box of the text display area of W,
2734 without mode lines. Include in this box the left and right
2735 fringes of W. */
2736 window_box (w, ANY_AREA, &x, &y, &width, &height);
2738 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2739 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2740 bottom_y = y + height;
2742 if (to_y < from_y)
2744 /* Scrolling up. Make sure we don't copy part of the mode
2745 line at the bottom. */
2746 if (from_y + run->height > bottom_y)
2747 height = bottom_y - from_y;
2748 else
2749 height = run->height;
2750 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2752 else
2754 /* Scrolling down. Make sure we don't copy over the mode line.
2755 at the bottom. */
2756 if (to_y + run->height > bottom_y)
2757 height = bottom_y - to_y;
2758 else
2759 height = run->height;
2760 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2763 block_input ();
2765 /* Cursor off. Will be switched on again in x_update_window_end. */
2766 x_clear_cursor (w);
2769 RECT from;
2770 RECT to;
2771 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2772 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2774 from.left = to.left = x;
2775 from.right = to.right = x + width;
2776 from.top = from_y;
2777 from.bottom = from_y + height;
2778 to.top = y;
2779 to.bottom = bottom_y;
2781 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2782 NULL, SW_INVALIDATE);
2784 /* Combine this with what we expect to be dirty. This covers the
2785 case where not all of the region we expect is actually dirty. */
2786 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2788 /* If the dirty region is not what we expected, redraw the entire frame. */
2789 if (!EqualRgn (combined, expect_dirty))
2790 SET_FRAME_GARBAGED (f);
2792 DeleteObject (dirty);
2793 DeleteObject (combined);
2796 unblock_input ();
2797 DeleteObject (expect_dirty);
2802 /***********************************************************************
2803 Exposure Events
2804 ***********************************************************************/
2806 static void
2807 frame_highlight (struct frame *f)
2809 x_update_cursor (f, 1);
2810 x_set_frame_alpha (f);
2813 static void
2814 frame_unhighlight (struct frame *f)
2816 x_update_cursor (f, 1);
2817 x_set_frame_alpha (f);
2820 /* The focus has changed. Update the frames as necessary to reflect
2821 the new situation. Note that we can't change the selected frame
2822 here, because the Lisp code we are interrupting might become confused.
2823 Each event gets marked with the frame in which it occurred, so the
2824 Lisp code can tell when the switch took place by examining the events. */
2826 static void
2827 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2829 struct frame *old_focus = dpyinfo->w32_focus_frame;
2831 if (frame != dpyinfo->w32_focus_frame)
2833 /* Set this before calling other routines, so that they see
2834 the correct value of w32_focus_frame. */
2835 dpyinfo->w32_focus_frame = frame;
2837 if (old_focus && old_focus->auto_lower)
2838 x_lower_frame (old_focus);
2840 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2841 dpyinfo->w32_pending_autoraise_frame = dpyinfo->w32_focus_frame;
2842 else
2843 dpyinfo->w32_pending_autoraise_frame = NULL;
2846 x_frame_rehighlight (dpyinfo);
2850 /* Handle FocusIn and FocusOut state changes for FRAME.
2851 If FRAME has focus and there exists more than one frame, puts
2852 a FOCUS_IN_EVENT into *BUFP. */
2854 static void
2855 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2856 struct frame *frame, struct input_event *bufp)
2858 if (type == WM_SETFOCUS)
2860 if (dpyinfo->w32_focus_event_frame != frame)
2862 x_new_focus_frame (dpyinfo, frame);
2863 dpyinfo->w32_focus_event_frame = frame;
2865 /* Don't stop displaying the initial startup message
2866 for a switch-frame event we don't need. */
2867 if (NILP (Vterminal_frame)
2868 && CONSP (Vframe_list)
2869 && !NILP (XCDR (Vframe_list)))
2871 bufp->arg = Qt;
2873 else
2875 bufp->arg = Qnil;
2878 bufp->kind = FOCUS_IN_EVENT;
2879 XSETFRAME (bufp->frame_or_window, frame);
2882 frame->output_data.x->focus_state |= state;
2884 /* TODO: IME focus? */
2886 else if (type == WM_KILLFOCUS)
2888 frame->output_data.x->focus_state &= ~state;
2890 if (dpyinfo->w32_focus_event_frame == frame)
2892 dpyinfo->w32_focus_event_frame = 0;
2893 x_new_focus_frame (dpyinfo, 0);
2895 bufp->kind = FOCUS_OUT_EVENT;
2896 XSETFRAME (bufp->frame_or_window, frame);
2899 /* TODO: IME focus? */
2904 /* The focus may have changed. Figure out if it is a real focus change,
2905 by checking both FocusIn/Out and Enter/LeaveNotify events.
2907 Returns FOCUS_IN_EVENT event in *BUFP. */
2909 static void
2910 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2911 struct input_event *bufp)
2913 struct frame *frame;
2915 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2916 if (! frame)
2917 return;
2919 /* On w32, this is only called from focus events, so no switch needed. */
2920 x_focus_changed (event->msg.message,
2921 (event->msg.message == WM_KILLFOCUS ?
2922 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2923 dpyinfo, frame, bufp);
2927 #if 0 /* unused */
2928 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2930 static void
2931 x_mouse_leave (struct w32_display_info *dpyinfo)
2933 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2935 #endif
2937 /* The focus has changed, or we have redirected a frame's focus to
2938 another frame (this happens when a frame uses a surrogate
2939 mini-buffer frame). Shift the highlight as appropriate.
2941 The FRAME argument doesn't necessarily have anything to do with which
2942 frame is being highlighted or un-highlighted; we only use it to find
2943 the appropriate X display info. */
2945 static void
2946 w32_frame_rehighlight (struct frame *frame)
2948 if (! FRAME_W32_P (frame))
2949 return;
2950 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
2953 static void
2954 x_frame_rehighlight (struct w32_display_info *dpyinfo)
2956 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2958 if (dpyinfo->w32_focus_frame)
2960 dpyinfo->x_highlight_frame
2961 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2962 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2963 : dpyinfo->w32_focus_frame);
2964 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2966 fset_focus_frame (dpyinfo->w32_focus_frame, Qnil);
2967 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2970 else
2971 dpyinfo->x_highlight_frame = 0;
2973 if (dpyinfo->x_highlight_frame != old_highlight)
2975 if (old_highlight)
2976 frame_unhighlight (old_highlight);
2977 if (dpyinfo->x_highlight_frame)
2978 frame_highlight (dpyinfo->x_highlight_frame);
2982 /* Keyboard processing - modifier keys, etc. */
2984 /* Convert a keysym to its name. */
2986 char *
2987 x_get_keysym_name (int keysym)
2989 /* Make static so we can always return it */
2990 static char value[100];
2992 block_input ();
2993 GetKeyNameText (keysym, value, 100);
2994 unblock_input ();
2996 return value;
2999 static int
3000 codepage_for_locale (LCID locale)
3002 char cp[20];
3004 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
3005 return atoi (cp);
3006 else
3007 return CP_ACP;
3011 /* Mouse clicks and mouse movement. Rah. */
3013 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
3014 the state in PUP. XBUTTON provides extra information for extended mouse
3015 button messages. Returns FALSE if unable to parse the message. */
3016 BOOL
3017 parse_button (int message, int xbutton, int * pbutton, int * pup)
3019 int button = 0;
3020 int up = 0;
3022 switch (message)
3024 case WM_LBUTTONDOWN:
3025 button = 0;
3026 up = 0;
3027 break;
3028 case WM_LBUTTONUP:
3029 button = 0;
3030 up = 1;
3031 break;
3032 case WM_MBUTTONDOWN:
3033 if (NILP (Vw32_swap_mouse_buttons))
3034 button = 1;
3035 else
3036 button = 2;
3037 up = 0;
3038 break;
3039 case WM_MBUTTONUP:
3040 if (NILP (Vw32_swap_mouse_buttons))
3041 button = 1;
3042 else
3043 button = 2;
3044 up = 1;
3045 break;
3046 case WM_RBUTTONDOWN:
3047 if (NILP (Vw32_swap_mouse_buttons))
3048 button = 2;
3049 else
3050 button = 1;
3051 up = 0;
3052 break;
3053 case WM_RBUTTONUP:
3054 if (NILP (Vw32_swap_mouse_buttons))
3055 button = 2;
3056 else
3057 button = 1;
3058 up = 1;
3059 break;
3060 case WM_XBUTTONDOWN:
3061 button = xbutton + 2;
3062 up = 0;
3063 break;
3064 case WM_XBUTTONUP:
3065 button = xbutton + 2;
3066 up = 1;
3067 break;
3068 default:
3069 return (FALSE);
3072 if (pup) *pup = up;
3073 if (pbutton) *pbutton = button;
3075 return (TRUE);
3079 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3081 If the event is a button press, then note that we have grabbed
3082 the mouse. */
3084 static Lisp_Object
3085 construct_mouse_click (struct input_event *result, W32Msg *msg, struct frame *f)
3087 int button = 0;
3088 int up = 0;
3090 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3091 &button, &up);
3093 /* Make the event type NO_EVENT; we'll change that when we decide
3094 otherwise. */
3095 result->kind = MOUSE_CLICK_EVENT;
3096 result->code = button;
3097 result->timestamp = msg->msg.time;
3098 result->modifiers = (msg->dwModifiers
3099 | (up
3100 ? up_modifier
3101 : down_modifier));
3103 XSETINT (result->x, LOWORD (msg->msg.lParam));
3104 XSETINT (result->y, HIWORD (msg->msg.lParam));
3105 XSETFRAME (result->frame_or_window, f);
3106 result->arg = Qnil;
3107 return Qnil;
3110 static Lisp_Object
3111 construct_mouse_wheel (struct input_event *result, W32Msg *msg, struct frame *f)
3113 POINT p;
3114 int delta;
3116 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3117 : WHEEL_EVENT;
3118 result->code = 0;
3119 result->timestamp = msg->msg.time;
3121 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3122 forward, away from the user (up); a negative value indicates that
3123 the wheel was rotated backward, toward the user (down). */
3124 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3126 /* The up and down modifiers indicate if the wheel was rotated up or
3127 down based on WHEEL_DELTA value. */
3128 result->modifiers = (msg->dwModifiers
3129 | ((delta < 0 ) ? down_modifier : up_modifier));
3131 /* With multiple monitors, we can legitimately get negative
3132 coordinates, so cast to short to interpret them correctly. */
3133 p.x = (short) LOWORD (msg->msg.lParam);
3134 p.y = (short) HIWORD (msg->msg.lParam);
3135 /* For the case that F's w32 window is not msg->msg.hwnd. */
3136 ScreenToClient (FRAME_W32_WINDOW (f), &p);
3137 XSETINT (result->x, p.x);
3138 XSETINT (result->y, p.y);
3139 XSETFRAME (result->frame_or_window, f);
3140 result->arg = Qnil;
3141 return Qnil;
3144 static Lisp_Object
3145 construct_drag_n_drop (struct input_event *result, W32Msg *msg, struct frame *f)
3147 Lisp_Object files;
3148 Lisp_Object frame;
3149 HDROP hdrop;
3150 POINT p;
3151 WORD num_files;
3152 wchar_t name_w[MAX_PATH];
3153 #ifdef NTGUI_UNICODE
3154 const int use_unicode = 1;
3155 #else
3156 int use_unicode = w32_unicode_filenames;
3157 char name_a[MAX_PATH];
3158 char file[MAX_UTF8_PATH];
3159 #endif
3160 int i;
3162 result->kind = DRAG_N_DROP_EVENT;
3163 result->code = 0;
3164 result->timestamp = msg->msg.time;
3165 result->modifiers = msg->dwModifiers;
3167 hdrop = (HDROP) msg->msg.wParam;
3168 DragQueryPoint (hdrop, &p);
3170 #if 0
3171 p.x = LOWORD (msg->msg.lParam);
3172 p.y = HIWORD (msg->msg.lParam);
3173 ScreenToClient (msg->msg.hwnd, &p);
3174 #endif
3176 XSETINT (result->x, p.x);
3177 XSETINT (result->y, p.y);
3179 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3180 files = Qnil;
3182 for (i = 0; i < num_files; i++)
3184 if (use_unicode)
3186 eassert (DragQueryFileW (hdrop, i, NULL, 0) < MAX_PATH);
3187 /* If DragQueryFile returns zero, it failed to fetch a file
3188 name. */
3189 if (DragQueryFileW (hdrop, i, name_w, MAX_PATH) == 0)
3190 continue;
3191 #ifdef NTGUI_UNICODE
3192 files = Fcons (from_unicode_buffer (name_w), files);
3193 #else
3194 filename_from_utf16 (name_w, file);
3195 files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
3196 #endif /* NTGUI_UNICODE */
3198 #ifndef NTGUI_UNICODE
3199 else
3201 eassert (DragQueryFileA (hdrop, i, NULL, 0) < MAX_PATH);
3202 if (DragQueryFileA (hdrop, i, name_a, MAX_PATH) == 0)
3203 continue;
3204 filename_from_ansi (name_a, file);
3205 files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
3207 #endif
3210 DragFinish (hdrop);
3212 XSETFRAME (frame, f);
3213 result->frame_or_window = frame;
3214 result->arg = files;
3215 return Qnil;
3219 #if HAVE_W32NOTIFY
3221 /* File event notifications (see w32notify.c). */
3223 Lisp_Object
3224 lispy_file_action (DWORD action)
3226 static char unknown_fmt[] = "unknown-action(%d)";
3227 Lisp_Object retval;
3229 switch (action)
3231 case FILE_ACTION_ADDED:
3232 retval = Qadded;
3233 break;
3234 case FILE_ACTION_REMOVED:
3235 retval = Qremoved;
3236 break;
3237 case FILE_ACTION_MODIFIED:
3238 retval = Qmodified;
3239 break;
3240 case FILE_ACTION_RENAMED_OLD_NAME:
3241 retval = Qrenamed_from;
3242 break;
3243 case FILE_ACTION_RENAMED_NEW_NAME:
3244 retval = Qrenamed_to;
3245 break;
3246 default:
3248 char buf[sizeof(unknown_fmt) - 1 + INT_STRLEN_BOUND (DWORD)];
3250 sprintf (buf, unknown_fmt, action);
3251 retval = intern (buf);
3253 break;
3256 return retval;
3259 #ifdef WINDOWSNT
3260 /* Put file notifications into the Emacs input event queue. This
3261 function runs when the WM_EMACS_FILENOTIFY message arrives from a
3262 watcher thread. */
3263 static void
3264 queue_notifications (struct input_event *event, W32Msg *msg, struct frame *f,
3265 int *evcount)
3267 struct notifications_set *ns = NULL;
3268 Lisp_Object frame;
3269 int done = 0;
3271 /* We cannot process notification before Emacs is fully initialized,
3272 since we need the UTF-16LE coding-system to be set up. */
3273 if (!initialized)
3274 return;
3276 XSETFRAME (frame, f);
3278 while (!done)
3280 ns = NULL;
3282 /* Find out if there is a record available in the linked list of
3283 notifications sets. If so, unlink the set from the linked
3284 list. Use critical section. */
3285 enter_crit ();
3286 if (notifications_set_head->next != notifications_set_head)
3288 ns = notifications_set_head->next;
3289 ns->prev->next = ns->next;
3290 ns->next->prev = ns->prev;
3292 else
3293 done = 1;
3294 leave_crit();
3296 if (ns)
3298 BYTE *p = ns->notifications;
3299 FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
3300 const DWORD min_size
3301 = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
3302 DWORD info_size = ns->size;
3303 Lisp_Object cs = Qutf_16le;
3304 Lisp_Object obj = w32_get_watch_object (ns->desc);
3306 /* notifications size could be zero when the buffer of
3307 notifications overflowed on the OS level, or when the
3308 directory being watched was itself deleted. Do nothing in
3309 that case. */
3310 if (info_size
3311 && !NILP (obj) && CONSP (obj))
3313 Lisp_Object callback = XCDR (obj);
3315 while (info_size >= min_size)
3317 Lisp_Object utf_16_fn
3318 = make_unibyte_string ((char *)fni->FileName,
3319 fni->FileNameLength);
3320 /* Note: mule-conf is preloaded, so utf-16le must
3321 already be defined at this point. */
3322 Lisp_Object fname
3323 = code_convert_string_norecord (utf_16_fn, cs, 0);
3324 Lisp_Object action = lispy_file_action (fni->Action);
3326 event->kind = FILE_NOTIFY_EVENT;
3327 event->timestamp = msg->msg.time;
3328 event->modifiers = 0;
3329 event->frame_or_window = callback;
3330 event->arg = list3 (make_pointer_integer (ns->desc),
3331 action, fname);
3332 kbd_buffer_store_event (event);
3333 (*evcount)++;
3334 if (!fni->NextEntryOffset)
3335 break;
3336 p += fni->NextEntryOffset;
3337 fni = (PFILE_NOTIFY_INFORMATION)p;
3338 info_size -= fni->NextEntryOffset;
3341 /* Free this notifications set. */
3342 xfree (ns->notifications);
3343 xfree (ns);
3346 /* We've stuffed all the events ourselves, so w32_read_socket shouldn't. */
3347 event->kind = NO_EVENT;
3349 #endif /* WINDOWSNT */
3350 #endif /* HAVE_W32NOTIFY */
3353 /* Function to report a mouse movement to the mainstream Emacs code.
3354 The input handler calls this.
3356 We have received a mouse movement event, which is given in *event.
3357 If the mouse is over a different glyph than it was last time, tell
3358 the mainstream emacs code by setting mouse_moved. If not, ask for
3359 another motion event, so we can check again the next time it moves. */
3361 static int
3362 note_mouse_movement (struct frame *frame, MSG *msg)
3364 struct w32_display_info *dpyinfo;
3365 int mouse_x = LOWORD (msg->lParam);
3366 int mouse_y = HIWORD (msg->lParam);
3367 RECT *r;
3369 if (!FRAME_X_OUTPUT (frame))
3370 return 0;
3372 dpyinfo = FRAME_DISPLAY_INFO (frame);
3373 dpyinfo->last_mouse_movement_time = msg->time;
3374 dpyinfo->last_mouse_motion_frame = frame;
3375 dpyinfo->last_mouse_motion_x = mouse_x;
3376 dpyinfo->last_mouse_motion_y = mouse_y;
3378 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3380 frame->mouse_moved = true;
3381 dpyinfo->last_mouse_scroll_bar = NULL;
3382 note_mouse_highlight (frame, -1, -1);
3383 dpyinfo->last_mouse_glyph_frame = NULL;
3384 return 1;
3387 /* Has the mouse moved off the glyph it was on at the last sighting? */
3388 r = &dpyinfo->last_mouse_glyph;
3389 if (frame != dpyinfo->last_mouse_glyph_frame
3390 || mouse_x < r->left || mouse_x >= r->right
3391 || mouse_y < r->top || mouse_y >= r->bottom)
3393 frame->mouse_moved = true;
3394 dpyinfo->last_mouse_scroll_bar = NULL;
3395 note_mouse_highlight (frame, mouse_x, mouse_y);
3396 /* Remember the mouse position here, as w32_mouse_position only
3397 gets called when mouse tracking is enabled but we also need
3398 to keep track of the mouse for help_echo and highlighting at
3399 other times. */
3400 remember_mouse_glyph (frame, mouse_x, mouse_y, r);
3401 dpyinfo->last_mouse_glyph_frame = frame;
3402 return 1;
3405 return 0;
3409 /************************************************************************
3410 Mouse Face
3411 ************************************************************************/
3413 static struct scroll_bar *x_window_to_scroll_bar (Window, int);
3414 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3415 enum scroll_bar_part *,
3416 Lisp_Object *, Lisp_Object *,
3417 Time *);
3418 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3419 enum scroll_bar_part *,
3420 Lisp_Object *, Lisp_Object *,
3421 Time *);
3422 static void
3423 w32_define_cursor (Window window, Cursor cursor)
3425 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3428 /* Return the current position of the mouse.
3429 *fp should be a frame which indicates which display to ask about.
3431 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3432 and *part to the frame, window, and scroll bar part that the mouse
3433 is over. Set *x and *y to the portion and whole of the mouse's
3434 position on the scroll bar.
3436 If the mouse movement started elsewhere, set *fp to the frame the
3437 mouse is on, *bar_window to nil, and *x and *y to the character cell
3438 the mouse is over.
3440 Set *time to the server time-stamp for the time at which the mouse
3441 was at this position.
3443 Don't store anything if we don't have a valid set of values to report.
3445 This clears the mouse_moved flag, so we can wait for the next mouse
3446 movement. */
3448 static void
3449 w32_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3450 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3451 Time *time)
3453 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3455 block_input ();
3457 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3459 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3461 if (bar->horizontal)
3462 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3463 else
3464 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3466 else
3468 POINT pt;
3469 Lisp_Object frame, tail;
3470 struct frame *f1 = NULL;
3472 /* Clear the mouse-moved flag for every frame on this display. */
3473 FOR_EACH_FRAME (tail, frame)
3474 XFRAME (frame)->mouse_moved = false;
3476 dpyinfo->last_mouse_scroll_bar = NULL;
3478 GetCursorPos (&pt);
3480 /* Now we have a position on the root; find the innermost window
3481 containing the pointer. */
3483 /* If mouse was grabbed on a frame, give coords for that
3484 frame even if the mouse is now outside it. Otherwise
3485 check for window under mouse on one of our frames. */
3486 if (x_mouse_grabbed (dpyinfo))
3487 f1 = dpyinfo->last_mouse_frame;
3488 else
3490 HWND wfp = WindowFromPoint (pt);
3492 if (wfp)
3494 f1 = x_any_window_to_frame (dpyinfo, wfp);
3495 if (f1)
3497 HWND cwfp = ChildWindowFromPoint (wfp, pt);
3499 if (cwfp)
3501 struct frame *f2 = x_any_window_to_frame (dpyinfo, cwfp);
3503 /* If a child window was found, make sure that its
3504 frame is a child frame (Bug#26615, maybe). */
3505 if (f2 && FRAME_PARENT_FRAME (f2))
3506 f1 = f2;
3512 /* If not, is it one of our scroll bars? */
3513 if (! f1)
3515 struct scroll_bar *bar
3516 = x_window_to_scroll_bar (WindowFromPoint (pt), 2);
3518 if (bar)
3519 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3522 if (f1 == 0 && insist > 0)
3523 f1 = SELECTED_FRAME ();
3525 if (f1)
3527 /* Ok, we found a frame. Store all the values.
3528 last_mouse_glyph is a rectangle used to reduce the
3529 generation of mouse events. To not miss any motion
3530 events, we must divide the frame into rectangles of the
3531 size of the smallest character that could be displayed
3532 on it, i.e. into the same rectangles that matrices on
3533 the frame are divided into. */
3535 dpyinfo = FRAME_DISPLAY_INFO (f1);
3536 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3537 remember_mouse_glyph (f1, pt.x, pt.y, &dpyinfo->last_mouse_glyph);
3538 dpyinfo->last_mouse_glyph_frame = f1;
3540 *bar_window = Qnil;
3541 *part = scroll_bar_above_handle;
3542 *fp = f1;
3543 XSETINT (*x, pt.x);
3544 XSETINT (*y, pt.y);
3545 *time = dpyinfo->last_mouse_movement_time;
3550 unblock_input ();
3554 /***********************************************************************
3555 Tool-bars
3556 ***********************************************************************/
3558 /* Handle mouse button event on the tool-bar of frame F, at
3559 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
3560 or ButtonRelease. */
3562 static void
3563 w32_handle_tool_bar_click (struct frame *f, struct input_event *button_event)
3565 int x = XFASTINT (button_event->x);
3566 int y = XFASTINT (button_event->y);
3568 if (button_event->modifiers & down_modifier)
3569 handle_tool_bar_click (f, x, y, 1, 0);
3570 else
3571 handle_tool_bar_click (f, x, y, 0,
3572 button_event->modifiers & ~up_modifier);
3577 /***********************************************************************
3578 Scroll bars
3579 ***********************************************************************/
3581 /* Scroll bar support. */
3583 /* Given a window ID, find the struct scroll_bar which manages it
3584 vertically. This can be called in GC, so we have to make sure to
3585 strip off mark bits. */
3587 static struct scroll_bar *
3588 x_window_to_scroll_bar (Window window_id, int type)
3590 Lisp_Object tail, frame;
3592 FOR_EACH_FRAME (tail, frame)
3594 Lisp_Object bar, condemned;
3596 /* Scan this frame's scroll bar list for a scroll bar with the
3597 right window ID. */
3598 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3599 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3600 /* This trick allows us to search both the ordinary and
3601 condemned scroll bar lists with one loop. */
3602 ! NILP (bar) || (bar = condemned,
3603 condemned = Qnil,
3604 ! NILP (bar));
3605 bar = XSCROLL_BAR (bar)->next)
3606 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id
3607 && (type = 2
3608 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
3609 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
3610 return XSCROLL_BAR (bar);
3613 return 0;
3618 /* Set the thumb size and position of vertical scroll bar BAR. We are currently
3619 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3621 static void
3622 w32_set_scroll_bar_thumb (struct scroll_bar *bar,
3623 int portion, int position, int whole)
3625 Window w = SCROLL_BAR_W32_WINDOW (bar);
3626 /* We use the whole scroll-bar height in the calculations below, to
3627 avoid strange effects like scrolling backwards when just clicking
3628 on the handle (without moving it). */
3629 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height)
3630 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3631 int sb_page, sb_pos;
3632 BOOL draggingp = bar->dragging ? TRUE : FALSE;
3633 SCROLLINFO si;
3635 /* We used to change the nPage setting while dragging the handle,
3636 but that had very strange effects (such as scrolling backwards
3637 while dragging downwards).
3639 Now, we don't change the nPage setting while dragging unless we
3640 get near to the end of the buffer, in which case we often have to
3641 resize the handle to "go all the way". */
3643 if (draggingp)
3645 int near_bottom_p;
3646 block_input ();
3647 si.cbSize = sizeof (si);
3648 si.fMask = SIF_POS | SIF_PAGE;
3649 GetScrollInfo (w, SB_CTL, &si);
3650 near_bottom_p = si.nPos + si.nPage >= range;
3651 unblock_input ();
3652 if (!near_bottom_p)
3653 return;
3656 if (whole)
3658 /* Position scroll bar at rock bottom if the bottom of the
3659 buffer is visible. This avoids shrinking the thumb away
3660 to nothing if it is held at the bottom of the buffer. */
3661 if (position + portion >= whole && !draggingp)
3663 sb_page = range * (whole - position) / whole;
3664 sb_pos = range;
3666 else
3668 sb_pos = position * range / whole;
3669 sb_page = (min (portion, (whole - position)) * range) / whole;
3672 else
3674 sb_page = range;
3675 sb_pos = 0;
3678 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3680 block_input ();
3682 si.cbSize = sizeof (si);
3683 si.fMask = SIF_PAGE | SIF_POS;
3684 si.nPage = sb_page;
3685 si.nPos = sb_pos;
3687 SetScrollInfo (w, SB_CTL, &si, TRUE);
3689 unblock_input ();
3692 /* Set the thumb size and position of horizontal scroll bar BAR. We are currently
3693 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3695 static void
3696 w32_set_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3697 int portion, int position, int whole)
3699 Window w = SCROLL_BAR_W32_WINDOW (bar);
3700 SCROLLINFO si;
3702 block_input ();
3704 si.cbSize = sizeof (si);
3705 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
3706 si.nMin = 0;
3707 si.nMax = whole;
3708 /* Allow nPage to be one larger than nPos so we don't allow the scrolling
3709 of an already fully visible buffer. */
3710 si.nPage = min (portion, si.nMax) + 1;
3711 si.nPos = min (position, si.nMax);
3712 SetScrollInfo (w, SB_CTL, &si, TRUE);
3714 unblock_input ();
3718 /************************************************************************
3719 Scroll bars, general
3720 ************************************************************************/
3722 static HWND
3723 my_create_vscrollbar (struct frame * f, struct scroll_bar * bar)
3725 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3726 WM_EMACS_CREATEVSCROLLBAR, (WPARAM) f,
3727 (LPARAM) bar);
3730 static HWND
3731 my_create_hscrollbar (struct frame * f, struct scroll_bar * bar)
3733 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3734 WM_EMACS_CREATEHSCROLLBAR, (WPARAM) f,
3735 (LPARAM) bar);
3738 /*#define ATTACH_THREADS*/
3740 static BOOL
3741 my_show_window (struct frame *f, HWND hwnd, int how)
3743 #ifndef ATTACH_THREADS
3744 return SendMessageTimeout (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3745 (WPARAM) hwnd, (LPARAM) how, 0, 6000, NULL);
3746 #else
3747 return ShowWindow (hwnd, how);
3748 #endif
3751 static void
3752 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3753 int x, int y, int cx, int cy, UINT flags)
3755 #ifndef ATTACH_THREADS
3756 WINDOWPOS pos;
3757 pos.hwndInsertAfter = hwndAfter;
3758 pos.x = x;
3759 pos.y = y;
3760 pos.cx = cx;
3761 pos.cy = cy;
3762 pos.flags = flags;
3763 SendMessageTimeout (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0,
3764 0, 6000, NULL);
3765 #else
3766 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3767 #endif
3770 #if 0
3771 static void
3772 my_set_focus (struct frame * f, HWND hwnd)
3774 SendMessageTimeout (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3775 (WPARAM) hwnd, 0, 0, 6000, NULL);
3777 #endif
3779 static void
3780 my_set_foreground_window (HWND hwnd)
3782 SendMessageTimeout (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0,
3783 0, 6000, NULL);
3787 static void
3788 my_destroy_window (struct frame * f, HWND hwnd)
3790 SendMessageTimeout (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3791 (WPARAM) hwnd, 0, 0, 6000, NULL);
3794 static void
3795 my_bring_window_to_top (HWND hwnd)
3797 SendMessageTimeout (hwnd, WM_EMACS_BRINGTOTOP, (WPARAM) hwnd, 0,
3798 0, 6000, NULL);
3801 /* Create a scroll bar and return the scroll bar vector for it. W is
3802 the Emacs window on which to create the scroll bar. TOP, LEFT,
3803 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
3804 scroll bar. */
3806 static struct scroll_bar *
3807 x_scroll_bar_create (struct window *w, int left, int top, int width, int height, bool horizontal)
3809 struct frame *f = XFRAME (WINDOW_FRAME (w));
3810 HWND hwnd;
3811 SCROLLINFO si;
3812 struct scroll_bar *bar
3813 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, top, PVEC_OTHER);
3814 Lisp_Object barobj;
3816 block_input ();
3818 XSETWINDOW (bar->window, w);
3819 bar->top = top;
3820 bar->left = left;
3821 bar->width = width;
3822 bar->height = height;
3823 bar->start = 0;
3824 bar->end = 0;
3825 bar->dragging = 0;
3826 bar->horizontal = horizontal;
3828 /* Requires geometry to be set before call to create the real window */
3830 if (horizontal)
3831 hwnd = my_create_hscrollbar (f, bar);
3832 else
3833 hwnd = my_create_vscrollbar (f, bar);
3835 si.cbSize = sizeof (si);
3836 si.fMask = SIF_ALL;
3837 si.nMin = 0;
3838 if (horizontal)
3839 si.nMax = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width)
3840 + HORIZONTAL_SCROLL_BAR_MIN_HANDLE;
3841 else
3842 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3843 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3844 si.nPage = si.nMax;
3845 si.nPos = 0;
3847 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3849 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3851 /* Add bar to its frame's list of scroll bars. */
3852 bar->next = FRAME_SCROLL_BARS (f);
3853 bar->prev = Qnil;
3854 XSETVECTOR (barobj, bar);
3855 fset_scroll_bars (f, barobj);
3856 if (! NILP (bar->next))
3857 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3859 unblock_input ();
3861 return bar;
3865 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3866 nil. */
3868 static void
3869 x_scroll_bar_remove (struct scroll_bar *bar)
3871 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3873 block_input ();
3875 /* Destroy the window. */
3876 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3878 /* Dissociate this scroll bar from its window. */
3879 if (bar->horizontal)
3880 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
3881 else
3882 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
3884 unblock_input ();
3887 /* Set the handle of the vertical scroll bar for WINDOW to indicate that
3888 we are displaying PORTION characters out of a total of WHOLE
3889 characters, starting at POSITION. If WINDOW has no vertical scroll
3890 bar, create one. */
3891 static void
3892 w32_set_vertical_scroll_bar (struct window *w,
3893 int portion, int whole, int position)
3895 struct frame *f = XFRAME (w->frame);
3896 Lisp_Object barobj;
3897 struct scroll_bar *bar;
3898 int top, height, left, width;
3899 int window_y, window_height;
3901 /* Get window dimensions. */
3902 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
3903 top = window_y;
3904 height = window_height;
3906 /* Compute the left edge and the width of the scroll bar area. */
3907 left = WINDOW_SCROLL_BAR_AREA_X (w);
3908 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
3910 /* Does the scroll bar exist yet? */
3911 if (NILP (w->vertical_scroll_bar))
3913 HDC hdc;
3914 block_input ();
3915 if (width > 0 && height > 0)
3917 hdc = get_frame_dc (f);
3918 w32_clear_area (f, hdc, left, top, width, height);
3919 release_frame_dc (f, hdc);
3921 unblock_input ();
3923 bar = x_scroll_bar_create (w, left, top, width, height, false);
3925 else
3927 /* It may just need to be moved and resized. */
3928 HWND hwnd;
3930 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3931 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3933 /* If already correctly positioned, do nothing. */
3934 if (bar->left == left
3935 && bar->top == top
3936 && bar->width == width
3937 && bar->height == height)
3939 /* Redraw after clear_frame. */
3940 if (!my_show_window (f, hwnd, SW_NORMAL))
3941 InvalidateRect (hwnd, NULL, FALSE);
3943 else
3945 HDC hdc;
3946 SCROLLINFO si;
3948 block_input ();
3949 if (width && height)
3951 hdc = get_frame_dc (f);
3952 /* Since Windows scroll bars are smaller than the space reserved
3953 for them on the frame, we have to clear "under" them. */
3954 w32_clear_area (f, hdc, left, top, width, height);
3955 release_frame_dc (f, hdc);
3956 x_clear_under_internal_border (f);
3958 /* Make sure scroll bar is "visible" before moving, to ensure the
3959 area of the parent window now exposed will be refreshed. */
3960 my_show_window (f, hwnd, SW_HIDE);
3961 /** MoveWindow (hwnd, left, top, width, max (height, 1), TRUE); **/
3962 /* Try to not draw over child frames. */
3963 SetWindowPos (hwnd, HWND_BOTTOM, left, top, width, max (height, 1),
3964 SWP_FRAMECHANGED);
3966 si.cbSize = sizeof (si);
3967 si.fMask = SIF_RANGE;
3968 si.nMin = 0;
3969 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3970 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3972 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3974 my_show_window (f, hwnd, SW_NORMAL);
3975 /* InvalidateRect (w, NULL, FALSE); */
3977 /* Remember new settings. */
3978 bar->left = left;
3979 bar->top = top;
3980 bar->width = width;
3981 bar->height = height;
3983 unblock_input ();
3986 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3987 XSETVECTOR (barobj, bar);
3988 wset_vertical_scroll_bar (w, barobj);
3991 /* Set the handle of the horizontal scroll bar for WINDOW to indicate
3992 that we are displaying PORTION characters out of a total of WHOLE
3993 characters, starting at POSITION. If WINDOW has no horizontal scroll
3994 bar, create one. */
3995 static void
3996 w32_set_horizontal_scroll_bar (struct window *w,
3997 int portion, int whole, int position)
3999 struct frame *f = XFRAME (w->frame);
4000 Lisp_Object barobj;
4001 struct scroll_bar *bar;
4002 int top, height, left, width;
4003 int window_x, window_width;
4004 int clear_left = WINDOW_LEFT_EDGE_X (w);
4005 int clear_width = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
4007 /* Get window dimensions. */
4008 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
4009 left = window_x;
4010 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
4011 width = window_width;
4012 top = WINDOW_SCROLL_BAR_AREA_Y (w);
4014 /* Does the scroll bar exist yet? */
4015 if (NILP (w->horizontal_scroll_bar))
4017 HDC hdc;
4018 block_input ();
4019 if (width > 0 && height > 0)
4021 hdc = get_frame_dc (f);
4022 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
4023 release_frame_dc (f, hdc);
4025 unblock_input ();
4027 bar = x_scroll_bar_create (w, left, top, width, height, true);
4029 else
4031 /* It may just need to be moved and resized. */
4032 HWND hwnd;
4034 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4035 hwnd = SCROLL_BAR_W32_WINDOW (bar);
4037 /* If already correctly positioned, do nothing. */
4038 if (bar->left == left && bar->top == top
4039 && bar->width == width && bar->height == height)
4041 /* Redraw after clear_frame. */
4042 if (!my_show_window (f, hwnd, SW_NORMAL))
4043 InvalidateRect (hwnd, NULL, FALSE);
4045 else
4047 HDC hdc;
4048 SCROLLINFO si;
4050 block_input ();
4051 if (width && height)
4053 hdc = get_frame_dc (f);
4054 /* Since Windows scroll bars are smaller than the space reserved
4055 for them on the frame, we have to clear "under" them. */
4056 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
4057 release_frame_dc (f, hdc);
4058 x_clear_under_internal_border (f);
4060 /* Make sure scroll bar is "visible" before moving, to ensure the
4061 area of the parent window now exposed will be refreshed. */
4062 my_show_window (f, hwnd, SW_HIDE);
4063 /** MoveWindow (hwnd, left, top, width, max (height, 1), TRUE); **/
4064 /* Try to not draw over child frames. */
4065 SetWindowPos (hwnd, HWND_BOTTOM, left, top, max (width, 1), height,
4066 SWP_FRAMECHANGED);
4068 /* +++ SetScrollInfo +++ */
4069 si.cbSize = sizeof (si);
4070 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
4071 si.nMin = 0;
4072 si.nMax = whole;
4073 si.nPage = min (portion, si.nMax) + 1;
4074 si.nPos = min (position, si.nMax);
4075 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
4077 my_show_window (f, hwnd, SW_NORMAL);
4078 /* InvalidateRect (w, NULL, FALSE); */
4080 /* Remember new settings. */
4081 bar->left = left;
4082 bar->top = top;
4083 bar->width = width;
4084 bar->height = height;
4086 unblock_input ();
4090 w32_set_horizontal_scroll_bar_thumb (bar, portion, position, whole);
4091 XSETVECTOR (barobj, bar);
4092 wset_horizontal_scroll_bar (w, barobj);
4096 /* The following three hooks are used when we're doing a thorough
4097 redisplay of the frame. We don't explicitly know which scroll bars
4098 are going to be deleted, because keeping track of when windows go
4099 away is a real pain - "Can you say set-window-configuration, boys
4100 and girls?" Instead, we just assert at the beginning of redisplay
4101 that *all* scroll bars are to be removed, and then save a scroll bar
4102 from the fiery pit when we actually redisplay its window. */
4104 /* Arrange for all scroll bars on FRAME to be removed at the next call
4105 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
4106 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
4108 static void
4109 w32_condemn_scroll_bars (struct frame *frame)
4111 if (!NILP (FRAME_SCROLL_BARS (frame)))
4113 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
4115 /* Prepend scrollbars to already condemned ones. */
4116 Lisp_Object last = FRAME_SCROLL_BARS (frame);
4118 while (!NILP (XSCROLL_BAR (last)->next))
4119 last = XSCROLL_BAR (last)->next;
4121 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
4122 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
4125 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
4126 fset_scroll_bars (frame, Qnil);
4131 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
4132 Note that WINDOW isn't necessarily condemned at all. */
4134 static void
4135 w32_redeem_scroll_bar (struct window *w)
4137 struct scroll_bar *bar;
4138 Lisp_Object barobj;
4139 struct frame *f;
4141 /* We can't redeem this window's scroll bar if it doesn't have one. */
4142 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
4143 emacs_abort ();
4145 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4147 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4148 /* Unlink it from the condemned list. */
4149 f = XFRAME (WINDOW_FRAME (w));
4150 if (NILP (bar->prev))
4152 /* If the prev pointer is nil, it must be the first in one of
4153 the lists. */
4154 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
4155 /* It's not condemned. Everything's fine. */
4156 goto horizontal;
4157 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4158 w->vertical_scroll_bar))
4159 fset_condemned_scroll_bars (f, bar->next);
4160 else
4161 /* If its prev pointer is nil, it must be at the front of
4162 one or the other! */
4163 emacs_abort ();
4165 else
4166 XSCROLL_BAR (bar->prev)->next = bar->next;
4168 if (! NILP (bar->next))
4169 XSCROLL_BAR (bar->next)->prev = bar->prev;
4171 bar->next = FRAME_SCROLL_BARS (f);
4172 bar->prev = Qnil;
4173 XSETVECTOR (barobj, bar);
4174 fset_scroll_bars (f, barobj);
4175 if (! NILP (bar->next))
4176 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4179 horizontal:
4180 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
4182 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4183 /* Unlink it from the condemned list. */
4184 f = XFRAME (WINDOW_FRAME (w));
4185 if (NILP (bar->prev))
4187 /* If the prev pointer is nil, it must be the first in one of
4188 the lists. */
4189 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
4190 /* It's not condemned. Everything's fine. */
4191 return;
4192 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4193 w->horizontal_scroll_bar))
4194 fset_condemned_scroll_bars (f, bar->next);
4195 else
4196 /* If its prev pointer is nil, it must be at the front of
4197 one or the other! */
4198 emacs_abort ();
4200 else
4201 XSCROLL_BAR (bar->prev)->next = bar->next;
4203 if (! NILP (bar->next))
4204 XSCROLL_BAR (bar->next)->prev = bar->prev;
4206 bar->next = FRAME_SCROLL_BARS (f);
4207 bar->prev = Qnil;
4208 XSETVECTOR (barobj, bar);
4209 fset_scroll_bars (f, barobj);
4210 if (! NILP (bar->next))
4211 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4215 /* Remove all scroll bars on FRAME that haven't been saved since the
4216 last call to `*condemn_scroll_bars_hook'. */
4218 static void
4219 w32_judge_scroll_bars (struct frame *f)
4221 Lisp_Object bar, next;
4223 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
4225 /* Clear out the condemned list now so we won't try to process any
4226 more events on the hapless scroll bars. */
4227 fset_condemned_scroll_bars (f, Qnil);
4229 for (; ! NILP (bar); bar = next)
4231 struct scroll_bar *b = XSCROLL_BAR (bar);
4233 x_scroll_bar_remove (b);
4235 next = b->next;
4236 b->next = b->prev = Qnil;
4239 /* Now there should be no references to the condemned scroll bars,
4240 and they should get garbage-collected. */
4243 /* Handle a mouse click on the vertical scroll bar BAR. If
4244 *EMACS_EVENT's kind is set to something other than NO_EVENT, it is
4245 enqueued.
4247 This may be called from a signal handler, so we have to ignore GC
4248 mark bits. */
4250 static int
4251 w32_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4252 struct input_event *emacs_event)
4254 if (! WINDOWP (bar->window))
4255 emacs_abort ();
4257 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
4258 emacs_event->code = 0;
4259 /* not really meaningful to distinguish up/down */
4260 emacs_event->modifiers = msg->dwModifiers;
4261 emacs_event->frame_or_window = bar->window;
4262 emacs_event->arg = Qnil;
4263 emacs_event->timestamp = msg->msg.time;
4266 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4267 int y;
4268 int dragging = bar->dragging;
4269 SCROLLINFO si;
4270 int sb_event = LOWORD (msg->msg.wParam);
4272 si.cbSize = sizeof (si);
4273 if (sb_event == SB_THUMBTRACK)
4274 si.fMask = SIF_TRACKPOS;
4275 else
4276 si.fMask = SIF_POS;
4278 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4279 if (sb_event == SB_THUMBTRACK)
4280 y = si.nTrackPos;
4281 else
4282 y = si.nPos;
4284 bar->dragging = 0;
4285 struct frame *f; /* Value is not used. */
4286 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4288 switch (sb_event)
4290 case SB_LINEDOWN:
4291 emacs_event->part = scroll_bar_down_arrow;
4292 break;
4293 case SB_LINEUP:
4294 emacs_event->part = scroll_bar_up_arrow;
4295 break;
4296 case SB_PAGEUP:
4297 emacs_event->part = scroll_bar_above_handle;
4298 break;
4299 case SB_PAGEDOWN:
4300 emacs_event->part = scroll_bar_below_handle;
4301 break;
4302 case SB_TOP:
4303 emacs_event->part = scroll_bar_handle;
4304 y = 0;
4305 break;
4306 case SB_BOTTOM:
4307 emacs_event->part = scroll_bar_handle;
4308 y = top_range;
4309 break;
4310 case SB_THUMBTRACK:
4311 case SB_THUMBPOSITION:
4312 bar->dragging = 1; /* ??????? */
4313 emacs_event->part = scroll_bar_handle;
4315 /* "Silently" update current position. */
4317 SCROLLINFO si;
4319 si.cbSize = sizeof (si);
4320 si.fMask = SIF_POS;
4321 si.nPos = y;
4322 /* Remember apparent position (we actually lag behind the real
4323 position, so don't set that directly). */
4324 last_scroll_bar_drag_pos = y;
4326 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4328 break;
4329 case SB_ENDSCROLL:
4330 /* If this is the end of a drag sequence, then reset the scroll
4331 handle size to normal and do a final redraw. Otherwise do
4332 nothing. */
4333 if (dragging)
4335 SCROLLINFO si;
4336 int start = bar->start;
4337 int end = bar->end;
4339 si.cbSize = sizeof (si);
4340 si.fMask = SIF_PAGE | SIF_POS;
4341 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
4342 si.nPos = last_scroll_bar_drag_pos;
4343 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4345 /* fall through */
4346 FALLTHROUGH;
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 FALLTHROUGH;
4462 default:
4463 emacs_event->kind = NO_EVENT;
4464 return FALSE;
4467 XSETINT (emacs_event->x, x);
4468 XSETINT (emacs_event->y, y);
4470 return TRUE;
4474 /* Return information to the user about the current position of the mouse
4475 on the vertical scroll bar. */
4476 static void
4477 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4478 enum scroll_bar_part *part,
4479 Lisp_Object *x, Lisp_Object *y,
4480 Time *time)
4482 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4483 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4484 Window w = SCROLL_BAR_W32_WINDOW (bar);
4485 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4486 int pos;
4487 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4488 SCROLLINFO si;
4489 int sb_event = LOWORD (dpyinfo->last_mouse_scroll_bar_pos);
4491 block_input ();
4493 *fp = f;
4494 *bar_window = bar->window;
4496 si.cbSize = sizeof (si);
4497 if (sb_event == SB_THUMBTRACK)
4498 si.fMask = SIF_TRACKPOS | SIF_PAGE | SIF_RANGE;
4499 else
4500 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4502 GetScrollInfo (w, SB_CTL, &si);
4503 if (sb_event == SB_THUMBTRACK)
4504 pos = si.nTrackPos;
4505 else
4506 pos = si.nPos;
4507 top_range = si.nMax - si.nPage + 1;
4509 *part = scroll_bar_handle;
4510 if (sb_event == SB_LINEDOWN)
4511 pos++;
4513 XSETINT (*x, pos);
4514 XSETINT (*y, top_range);
4516 f->mouse_moved = false;
4517 dpyinfo->last_mouse_scroll_bar = NULL;
4519 *time = dpyinfo->last_mouse_movement_time;
4521 unblock_input ();
4524 /* Return information to the user about the current position of the mouse
4525 on the horizontal scroll bar. */
4526 static void
4527 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4528 enum scroll_bar_part *part,
4529 Lisp_Object *x, Lisp_Object *y,
4530 Time *time)
4532 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4533 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4534 Window w = SCROLL_BAR_W32_WINDOW (bar);
4535 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4536 int pos;
4537 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4538 SCROLLINFO si;
4539 int sb_event = LOWORD (dpyinfo->last_mouse_scroll_bar_pos);
4541 block_input ();
4543 *fp = f;
4544 *bar_window = bar->window;
4546 si.cbSize = sizeof (si);
4547 if (sb_event == SB_THUMBTRACK)
4548 si.fMask = SIF_TRACKPOS | SIF_PAGE | SIF_RANGE;
4549 else
4550 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4552 GetScrollInfo (w, SB_CTL, &si);
4553 if (sb_event == SB_THUMBTRACK)
4554 pos = si.nTrackPos;
4555 else
4556 pos = si.nPos;
4557 left_range = si.nMax - si.nPage + 1;
4559 *part = scroll_bar_handle;
4560 if (sb_event == SB_LINERIGHT)
4561 pos++;
4564 XSETINT (*y, pos);
4565 XSETINT (*x, left_range);
4567 f->mouse_moved = false;
4568 dpyinfo->last_mouse_scroll_bar = NULL;
4570 *time = dpyinfo->last_mouse_movement_time;
4572 unblock_input ();
4576 /* The screen has been cleared so we may have changed foreground or
4577 background colors, and the scroll bars may need to be redrawn.
4578 Clear out the scroll bars, and ask for expose events, so we can
4579 redraw them. */
4581 void
4582 x_scroll_bar_clear (struct frame *f)
4584 Lisp_Object bar;
4586 /* We can have scroll bars even if this is 0,
4587 if we just turned off scroll bar mode.
4588 But in that case we should not clear them. */
4589 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4590 || FRAME_HAS_HORIZONTAL_SCROLL_BARS (f))
4591 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4592 bar = XSCROLL_BAR (bar)->next)
4594 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4595 HDC hdc = GetDC (window);
4596 RECT rect;
4598 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4599 arranges to refresh the scroll bar if hidden. */
4600 my_show_window (f, window, SW_HIDE);
4602 GetClientRect (window, &rect);
4603 select_palette (f, hdc);
4604 w32_clear_rect (f, hdc, &rect);
4605 x_clear_under_internal_border (f);
4606 deselect_palette (f, hdc);
4608 ReleaseDC (window, hdc);
4612 /* The main W32 event-reading loop - w32_read_socket. */
4614 /* Record the last 100 characters stored
4615 to help debug the loss-of-chars-during-GC problem. */
4617 static int temp_index;
4618 static short temp_buffer[100];
4620 /* Temporarily store lead byte of DBCS input sequences. */
4621 static char dbcs_lead = 0;
4623 /* Read events coming from the W32 shell.
4624 This routine is called by the SIGIO handler.
4625 We return as soon as there are no more events to be read.
4627 For an overview of how Emacs input works on MS-Windows, see the
4628 commentary before w32_msg_pump in w32fns.c.
4630 We return the number of characters stored into the buffer,
4631 thus pretending to be `read'.
4633 Some of these messages are reposted back to the message queue since the
4634 system calls the windows proc directly in a context where we cannot return
4635 the data nor can we guarantee the state we are in. So if we dispatch them
4636 we will get into an infinite loop. To prevent this from ever happening we
4637 will set a variable to indicate we are in the read_socket call and indicate
4638 which message we are processing since the windows proc gets called
4639 recursively with different messages by the system.
4642 extern void menubar_selection_callback (struct frame *, void *);
4644 static int
4645 w32_read_socket (struct terminal *terminal,
4646 struct input_event *hold_quit)
4648 int count = 0;
4649 int check_visibility = 0;
4650 W32Msg msg;
4651 struct frame *f;
4652 struct w32_display_info *dpyinfo = &one_w32_display_info;
4653 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
4655 block_input ();
4657 /* Process any incoming thread messages. */
4658 drain_message_queue ();
4660 /* TODO: ghostscript integration. */
4661 while (get_next_msg (&msg, FALSE))
4663 struct input_event inev;
4664 int do_help = 0;
4666 /* DebPrint (("w32_read_socket: %s time:%u\n", */
4667 /* w32_name_of_message (msg.msg.message), */
4668 /* msg.msg.time)); */
4670 EVENT_INIT (inev);
4671 inev.kind = NO_EVENT;
4672 inev.arg = Qnil;
4674 switch (msg.msg.message)
4676 case WM_EMACS_PAINT:
4677 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4679 if (f)
4681 if (msg.rect.right == msg.rect.left ||
4682 msg.rect.bottom == msg.rect.top)
4684 /* We may get paint messages even though the client
4685 area is clipped - these are not expose events. */
4686 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4687 SDATA (f->name)));
4689 else if (FRAME_VISIBLE_P (f) != 1)
4691 bool iconified = FRAME_ICONIFIED_P (f);
4693 /* Definitely not obscured, so mark as visible. */
4694 SET_FRAME_VISIBLE (f, 1);
4695 SET_FRAME_ICONIFIED (f, false);
4696 SET_FRAME_GARBAGED (f);
4697 if (!f->output_data.w32->asked_for_visible)
4698 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4699 SDATA (f->name)));
4701 /* WM_PAINT serves as MapNotify as well, so report
4702 visibility changes properly. */
4703 if (iconified)
4705 inev.kind = DEICONIFY_EVENT;
4706 XSETFRAME (inev.frame_or_window, f);
4708 else if (!NILP (Vframe_list) && !NILP (XCDR (Vframe_list)))
4709 /* Force a redisplay sooner or later to update the
4710 frame titles in case this is the second frame. */
4711 record_asynch_buffer_change ();
4713 else
4715 /* Erase background again for safety. But don't do
4716 that if the frame's 'garbaged' flag is set, since
4717 in that case expose_frame will do nothing, and if
4718 the various redisplay flags happen to be unset,
4719 we are left with a blank frame. */
4720 if (!FRAME_GARBAGED_P (f) || FRAME_PARENT_FRAME (f))
4722 HDC hdc = get_frame_dc (f);
4724 w32_clear_rect (f, hdc, &msg.rect);
4725 release_frame_dc (f, hdc);
4727 expose_frame (f,
4728 msg.rect.left,
4729 msg.rect.top,
4730 msg.rect.right - msg.rect.left,
4731 msg.rect.bottom - msg.rect.top);
4732 x_clear_under_internal_border (f);
4735 break;
4737 case WM_INPUTLANGCHANGE:
4738 /* Generate a language change event. */
4739 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4741 /* lParam contains the input language ID in its low 16 bits.
4742 Use it to update our record of the keyboard codepage. */
4743 w32_keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4744 & 0xffff));
4746 if (f)
4748 inev.kind = LANGUAGE_CHANGE_EVENT;
4749 XSETFRAME (inev.frame_or_window, f);
4750 inev.code = w32_keyboard_codepage;
4751 inev.modifiers = msg.msg.lParam & 0xffff;
4753 break;
4755 case WM_KEYDOWN:
4756 case WM_SYSKEYDOWN:
4757 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4759 if (f && !FRAME_ICONIFIED_P (f))
4761 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4762 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4764 clear_mouse_face (hlinfo);
4765 hlinfo->mouse_face_hidden = true;
4768 if (temp_index == sizeof temp_buffer / sizeof (short))
4769 temp_index = 0;
4770 temp_buffer[temp_index++] = msg.msg.wParam;
4771 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4772 inev.code = msg.msg.wParam;
4773 inev.modifiers = msg.dwModifiers;
4774 XSETFRAME (inev.frame_or_window, f);
4775 inev.timestamp = msg.msg.time;
4777 break;
4779 case WM_UNICHAR:
4780 case WM_SYSCHAR:
4781 case WM_CHAR:
4782 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4784 if (f && !FRAME_ICONIFIED_P (f))
4786 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4787 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4789 clear_mouse_face (hlinfo);
4790 hlinfo->mouse_face_hidden = true;
4793 if (temp_index == sizeof temp_buffer / sizeof (short))
4794 temp_index = 0;
4795 temp_buffer[temp_index++] = msg.msg.wParam;
4797 inev.modifiers = msg.dwModifiers;
4798 XSETFRAME (inev.frame_or_window, f);
4799 inev.timestamp = msg.msg.time;
4801 if (msg.msg.message == WM_UNICHAR)
4803 inev.code = msg.msg.wParam;
4805 else if (msg.msg.wParam < 256)
4807 wchar_t code;
4808 char dbcs[2];
4809 dbcs[0] = 0;
4810 dbcs[1] = (char) msg.msg.wParam;
4812 if (dbcs_lead)
4814 dbcs[0] = dbcs_lead;
4815 dbcs_lead = 0;
4816 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4817 dbcs, 2, &code, 1))
4819 /* Garbage */
4820 DebPrint (("Invalid DBCS sequence: %d %d\n",
4821 dbcs[0], dbcs[1]));
4822 inev.kind = NO_EVENT;
4823 break;
4826 else if (IsDBCSLeadByteEx (w32_keyboard_codepage,
4827 (BYTE) msg.msg.wParam))
4829 dbcs_lead = (char) msg.msg.wParam;
4830 inev.kind = NO_EVENT;
4831 break;
4833 else
4835 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4836 &dbcs[1], 1, &code, 1))
4838 /* What to do with garbage? */
4839 DebPrint (("Invalid character: %d\n", dbcs[1]));
4840 inev.kind = NO_EVENT;
4841 break;
4844 inev.code = code;
4846 else
4848 /* Windows shouldn't generate WM_CHAR events above 0xFF
4849 in non-Unicode message handlers. */
4850 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4851 inev.kind = NO_EVENT;
4852 break;
4854 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4855 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4857 break;
4859 case WM_APPCOMMAND:
4860 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4862 if (f && !FRAME_ICONIFIED_P (f))
4864 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4865 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4867 clear_mouse_face (hlinfo);
4868 hlinfo->mouse_face_hidden = true;
4871 if (temp_index == sizeof temp_buffer / sizeof (short))
4872 temp_index = 0;
4873 temp_buffer[temp_index++] = msg.msg.wParam;
4874 inev.kind = MULTIMEDIA_KEY_EVENT;
4875 inev.code = GET_APPCOMMAND_LPARAM (msg.msg.lParam);
4876 inev.modifiers = msg.dwModifiers;
4877 XSETFRAME (inev.frame_or_window, f);
4878 inev.timestamp = msg.msg.time;
4880 break;
4882 case WM_MOUSEMOVE:
4883 /* Ignore non-movement. */
4885 int x = LOWORD (msg.msg.lParam);
4886 int y = HIWORD (msg.msg.lParam);
4887 if (x == last_mousemove_x && y == last_mousemove_y)
4888 break;
4889 last_mousemove_x = x;
4890 last_mousemove_y = y;
4893 previous_help_echo_string = help_echo_string;
4894 help_echo_string = Qnil;
4896 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4897 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4899 if (hlinfo->mouse_face_hidden)
4901 hlinfo->mouse_face_hidden = false;
4902 clear_mouse_face (hlinfo);
4905 if (f)
4907 /* Maybe generate SELECT_WINDOW_EVENTs for
4908 `mouse-autoselect-window'. */
4909 if (!NILP (Vmouse_autoselect_window)
4910 && (f == XFRAME (selected_frame)
4911 /* Switch to f from another frame iff
4912 focus_follows_mouse is set and f accepts
4913 focus. */
4914 || (!NILP (focus_follows_mouse)
4915 && !FRAME_NO_ACCEPT_FOCUS (f))))
4917 static Lisp_Object last_mouse_window;
4918 Lisp_Object window = window_from_coordinates
4919 (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0);
4921 /* Window will be selected only when it is not
4922 selected now and last mouse movement event was
4923 not in it. Minibuffer window will be selected
4924 only when it is active. */
4925 if (WINDOWP (window)
4926 && !EQ (window, last_mouse_window)
4927 && !EQ (window, selected_window))
4929 inev.kind = SELECT_WINDOW_EVENT;
4930 inev.frame_or_window = window;
4933 /* Remember the last window where we saw the mouse. */
4934 last_mouse_window = window;
4937 if (!note_mouse_movement (f, &msg.msg))
4938 help_echo_string = previous_help_echo_string;
4940 else
4942 /* If we move outside the frame, then we're
4943 certainly no longer on any text in the frame. */
4944 clear_mouse_face (hlinfo);
4947 /* If the contents of the global variable help_echo_string
4948 has changed, generate a HELP_EVENT. */
4949 #if 0 /* The below is an invalid comparison when CHECK_LISP_OBJECT_TYPE.
4950 But it was originally changed to this to fix a bug, so I have
4951 not removed it completely in case the bug is still there. */
4952 if (help_echo_string != previous_help_echo_string ||
4953 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4954 #else /* This is what xterm.c does. */
4955 if (!NILP (help_echo_string)
4956 || !NILP (previous_help_echo_string))
4957 do_help = 1;
4958 #endif
4959 break;
4961 case WM_LBUTTONDOWN:
4962 case WM_LBUTTONUP:
4963 case WM_MBUTTONDOWN:
4964 case WM_MBUTTONUP:
4965 case WM_RBUTTONDOWN:
4966 case WM_RBUTTONUP:
4967 case WM_XBUTTONDOWN:
4968 case WM_XBUTTONUP:
4970 /* If we decide we want to generate an event to be seen
4971 by the rest of Emacs, we put it here. */
4972 bool tool_bar_p = 0;
4973 int button = 0;
4974 int up = 0;
4976 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4977 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4979 if (f)
4981 construct_mouse_click (&inev, &msg, f);
4983 /* Is this in the tool-bar? */
4984 if (WINDOWP (f->tool_bar_window)
4985 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4987 Lisp_Object window;
4988 int x = XFASTINT (inev.x);
4989 int y = XFASTINT (inev.y);
4991 window = window_from_coordinates (f, x, y, 0, 1);
4993 if (EQ (window, f->tool_bar_window))
4995 w32_handle_tool_bar_click (f, &inev);
4996 tool_bar_p = 1;
5000 if (tool_bar_p
5001 || (dpyinfo->w32_focus_frame
5002 && f != dpyinfo->w32_focus_frame
5003 /* This does not help when the click happens in
5004 a grand-parent frame. */
5005 && !frame_ancestor_p (f, dpyinfo->w32_focus_frame)))
5006 inev.kind = NO_EVENT;
5009 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
5010 &button, &up);
5012 if (up)
5014 dpyinfo->grabbed &= ~ (1 << button);
5016 else
5018 dpyinfo->grabbed |= (1 << button);
5019 dpyinfo->last_mouse_frame = f;
5020 /* Ignore any mouse motion that happened
5021 before this event; any subsequent mouse-movement
5022 Emacs events should reflect only motion after
5023 the ButtonPress. */
5024 if (f != 0)
5026 f->mouse_moved = false;
5027 if (!tool_bar_p)
5028 f->last_tool_bar_item = -1;
5031 break;
5034 case WM_MOUSEWHEEL:
5035 case WM_MOUSEHWHEEL:
5037 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
5038 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
5040 if (f)
5042 if (!dpyinfo->w32_focus_frame
5043 || f == dpyinfo->w32_focus_frame)
5044 /* Emit an Emacs wheel-up/down event. */
5046 construct_mouse_wheel (&inev, &msg, f);
5048 /* Ignore any mouse motion that happened before this
5049 event; any subsequent mouse-movement Emacs events
5050 should reflect only motion after the ButtonPress. */
5051 f->mouse_moved = false;
5052 f->last_tool_bar_item = -1;
5053 dpyinfo->last_mouse_frame = f;
5055 else if (FRAME_NO_ACCEPT_FOCUS (f)
5056 && !x_mouse_grabbed (dpyinfo))
5058 Lisp_Object frame1 = get_frame_param (f, Qmouse_wheel_frame);
5059 struct frame *f1 = FRAMEP (frame1) ? XFRAME (frame1) : NULL;
5061 if (f1 && FRAME_LIVE_P (f1) && FRAME_W32_P (f1))
5063 construct_mouse_wheel (&inev, &msg, f1);
5064 f1->mouse_moved = false;
5065 f1->last_tool_bar_item = -1;
5066 dpyinfo->last_mouse_frame = f1;
5068 else
5069 dpyinfo->last_mouse_frame = f;
5071 else
5072 dpyinfo->last_mouse_frame = f;
5074 else
5075 dpyinfo->last_mouse_frame = f;
5077 break;
5079 case WM_DROPFILES:
5080 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5082 if (f)
5083 construct_drag_n_drop (&inev, &msg, f);
5084 break;
5086 case WM_HSCROLL:
5088 struct scroll_bar *bar =
5089 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 1);
5091 if (bar)
5092 w32_horizontal_scroll_bar_handle_click (bar, &msg, &inev);
5093 break;
5096 case WM_VSCROLL:
5098 struct scroll_bar *bar =
5099 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 0);
5101 if (bar)
5102 w32_scroll_bar_handle_click (bar, &msg, &inev);
5103 break;
5106 case WM_WINDOWPOSCHANGED:
5107 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5109 if (f)
5111 RECT rect;
5112 int /* rows, columns, */ width, height, text_width, text_height;
5114 if (GetClientRect (msg.msg.hwnd, &rect)
5115 /* GetClientRect evidently returns (0, 0, 0, 0) if
5116 called on a minimized frame. Such "dimensions"
5117 aren't useful anyway. */
5118 && !(rect.bottom == 0
5119 && rect.top == 0
5120 && rect.left == 0
5121 && rect.right == 0))
5123 height = rect.bottom - rect.top;
5124 width = rect.right - rect.left;
5125 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
5126 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
5127 /* rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); */
5128 /* columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); */
5130 /* TODO: Clip size to the screen dimensions. */
5132 /* Even if the number of character rows and columns
5133 has not changed, the font size may have changed,
5134 so we need to check the pixel dimensions as well. */
5136 if (width != FRAME_PIXEL_WIDTH (f)
5137 || height != FRAME_PIXEL_HEIGHT (f)
5138 || text_width != FRAME_TEXT_WIDTH (f)
5139 || text_height != FRAME_TEXT_HEIGHT (f))
5141 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
5142 SET_FRAME_GARBAGED (f);
5143 cancel_mouse_face (f);
5144 f->win_gravity = NorthWestGravity;
5149 check_visibility = 1;
5150 break;
5152 case WM_ACTIVATE:
5153 case WM_ACTIVATEAPP:
5154 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5155 if (f)
5157 /* Run the full-screen hook function also when we are
5158 being activated, to actually install the required
5159 size in effect, if the WAIT flag is set. This is
5160 because when the hook is run from x_set_fullscreen,
5161 the frame might not yet be visible, if that call is a
5162 result of make-frame, and in that case the hook just
5163 sets the WAIT flag. */
5164 if ((msg.msg.message == WM_WINDOWPOSCHANGED || msg.msg.wParam)
5165 && (f->want_fullscreen & FULLSCREEN_WAIT))
5167 /* Must set visibility right here since otherwise
5168 w32fullscreen_hook returns immediately. */
5169 SET_FRAME_VISIBLE (f, 1);
5170 w32fullscreen_hook (f);
5174 check_visibility = 1;
5175 break;
5177 case WM_MOVE:
5178 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5180 if (f && FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P(f))
5182 x_real_positions (f, &f->left_pos, &f->top_pos);
5183 inev.kind = MOVE_FRAME_EVENT;
5184 XSETFRAME (inev.frame_or_window, f);
5187 check_visibility = 1;
5188 break;
5190 case WM_SHOWWINDOW:
5191 /* wParam non-zero means Window is about to be shown, 0 means
5192 about to be hidden. */
5193 /* Redo the mouse-highlight after the tooltip has gone. */
5194 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
5196 tip_window = NULL;
5197 x_redo_mouse_highlight (dpyinfo);
5200 /* If window has been obscured or exposed by another window
5201 being maximized or minimized/restored, then recheck
5202 visibility of all frames. Direct changes to our own
5203 windows get handled by WM_SIZE. */
5204 #if 0
5205 if (msg.msg.lParam != 0)
5206 check_visibility = 1;
5207 else
5209 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5210 f->async_visible = msg.msg.wParam;
5212 #endif
5214 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5215 if (f)
5216 x_clear_under_internal_border (f);
5218 check_visibility = 1;
5219 break;
5221 case WM_SIZE:
5222 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5224 /* Inform lisp of whether frame has been iconified etc. */
5225 if (f)
5227 switch (msg.msg.wParam)
5229 case SIZE_MINIMIZED:
5230 SET_FRAME_VISIBLE (f, 0);
5231 SET_FRAME_ICONIFIED (f, true);
5233 inev.kind = ICONIFY_EVENT;
5234 XSETFRAME (inev.frame_or_window, f);
5235 break;
5237 case SIZE_MAXIMIZED:
5239 bool iconified = FRAME_ICONIFIED_P (f);
5240 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
5242 SET_FRAME_VISIBLE (f, 1);
5243 SET_FRAME_ICONIFIED (f, false);
5245 /* wait_reading_process_output will notice this
5246 and update the frame's display structures. */
5247 SET_FRAME_GARBAGED (f);
5249 if (iconified)
5251 int x, y;
5253 /* Reset top and left positions of the Window
5254 here since Windows sends a WM_MOVE message
5255 BEFORE telling us the Window is minimized
5256 when the Window is iconified, with 3000,3000
5257 as the co-ords. */
5258 x_real_positions (f, &x, &y);
5259 f->left_pos = x;
5260 f->top_pos = y;
5262 inev.kind = DEICONIFY_EVENT;
5263 XSETFRAME (inev.frame_or_window, f);
5265 else if (! NILP (Vframe_list)
5266 && ! NILP (XCDR (Vframe_list)))
5267 /* Force a redisplay sooner or later
5268 to update the frame titles
5269 in case this is the second frame. */
5270 record_asynch_buffer_change ();
5272 /* Windows can send us a SIZE_MAXIMIZED message even
5273 when fullscreen is fullboth. The following is a
5274 simple hack to check that based on the fact that
5275 only a maximized fullscreen frame should have both
5276 top/left outside the screen. */
5277 if (EQ (fullscreen, Qfullwidth) || EQ (fullscreen, Qfullheight)
5278 || NILP (fullscreen))
5280 int x, y;
5282 x_real_positions (f, &x, &y);
5283 if (x < 0 && y < 0)
5284 store_frame_param (f, Qfullscreen, Qmaximized);
5288 break;
5290 case SIZE_RESTORED:
5292 bool iconified = FRAME_ICONIFIED_P (f);
5294 /* The following was made unconditional in a
5295 pathetic attempt to fix bug#16967 in revision
5296 116716 but, considered counterproductive was made
5297 conditional again in revision 116727. martin */
5298 if (iconified)
5299 SET_FRAME_VISIBLE (f, 1);
5300 SET_FRAME_ICONIFIED (f, false);
5302 /* wait_reading_process_output will notice this
5303 and update the frame's display structures. */
5304 SET_FRAME_GARBAGED (f);
5306 if (iconified)
5308 /* Reset top and left positions of the Window
5309 here since Windows sends a WM_MOVE message
5310 BEFORE telling us the Window is minimized
5311 when the Window is iconified, with 3000,3000
5312 as the co-ords. */
5313 x_real_positions (f, &f->left_pos, &f->top_pos);
5315 inev.kind = DEICONIFY_EVENT;
5316 XSETFRAME (inev.frame_or_window, f);
5318 else if (! NILP (Vframe_list)
5319 && ! NILP (XCDR (Vframe_list)))
5320 /* Force a redisplay sooner or later
5321 to update the frame titles
5322 in case this is the second frame. */
5323 record_asynch_buffer_change ();
5326 if (EQ (get_frame_param (f, Qfullscreen), Qmaximized))
5327 store_frame_param (f, Qfullscreen, Qnil);
5329 break;
5333 if (f && !FRAME_ICONIFIED_P (f) && msg.msg.wParam != SIZE_MINIMIZED)
5335 RECT rect;
5336 int /* rows, columns, */ width, height, text_width, text_height;
5338 if (GetClientRect (msg.msg.hwnd, &rect)
5339 /* GetClientRect evidently returns (0, 0, 0, 0) if
5340 called on a minimized frame. Such "dimensions"
5341 aren't useful anyway. */
5342 && !(rect.bottom == 0
5343 && rect.top == 0
5344 && rect.left == 0
5345 && rect.right == 0))
5347 height = rect.bottom - rect.top;
5348 width = rect.right - rect.left;
5349 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
5350 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
5351 /* rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); */
5352 /* columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); */
5354 /* TODO: Clip size to the screen dimensions. */
5356 /* Even if the number of character rows and columns
5357 has not changed, the font size may have changed,
5358 so we need to check the pixel dimensions as well. */
5360 if (width != FRAME_PIXEL_WIDTH (f)
5361 || height != FRAME_PIXEL_HEIGHT (f)
5362 || text_width != FRAME_TEXT_WIDTH (f)
5363 || text_height != FRAME_TEXT_HEIGHT (f))
5365 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
5366 SET_FRAME_GARBAGED (f);
5367 cancel_mouse_face (f);
5368 f->win_gravity = NorthWestGravity;
5373 check_visibility = 1;
5374 break;
5376 case WM_MOUSELEAVE:
5377 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
5378 if (f)
5380 if (f == hlinfo->mouse_face_mouse_frame)
5382 /* If we move outside the frame, then we're
5383 certainly no longer on any text in the frame. */
5384 clear_mouse_face (hlinfo);
5385 hlinfo->mouse_face_mouse_frame = 0;
5388 /* Generate a nil HELP_EVENT to cancel a help-echo.
5389 Do it only if there's something to cancel.
5390 Otherwise, the startup message is cleared when
5391 the mouse leaves the frame. */
5392 if (any_help_event_p)
5393 do_help = -1;
5395 break;
5397 case WM_SETFOCUS:
5398 w32_detect_focus_change (dpyinfo, &msg, &inev);
5400 dpyinfo->grabbed = 0;
5401 check_visibility = 1;
5402 break;
5404 case WM_KILLFOCUS:
5405 w32_detect_focus_change (dpyinfo, &msg, &inev);
5406 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
5408 if (f)
5410 if (f == hlinfo->mouse_face_mouse_frame)
5412 /* If we move outside the frame, then we're
5413 certainly no longer on any text in the frame. */
5414 clear_mouse_face (hlinfo);
5415 hlinfo->mouse_face_mouse_frame = 0;
5418 /* Generate a nil HELP_EVENT to cancel a help-echo.
5419 Do it only if there's something to cancel.
5420 Otherwise, the startup message is cleared when
5421 the mouse leaves the frame. */
5422 if (any_help_event_p)
5423 do_help = -1;
5426 dpyinfo->grabbed = 0;
5427 check_visibility = 1;
5428 break;
5430 case WM_CLOSE:
5431 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5433 if (f)
5435 inev.kind = DELETE_WINDOW_EVENT;
5436 XSETFRAME (inev.frame_or_window, f);
5438 break;
5440 case WM_ENDSESSION:
5441 inev.kind = END_SESSION_EVENT;
5442 break;
5444 case WM_INITMENU:
5445 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5447 if (f)
5449 inev.kind = MENU_BAR_ACTIVATE_EVENT;
5450 XSETFRAME (inev.frame_or_window, f);
5452 break;
5454 case WM_COMMAND:
5455 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5457 if (f)
5459 menubar_selection_callback (f, (void *)msg.msg.wParam);
5462 check_visibility = 1;
5463 break;
5465 case WM_DISPLAYCHANGE:
5466 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5468 if (f)
5470 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
5472 dpyinfo->n_cbits = msg.msg.wParam;
5473 /* The new display could have a different resolution, in
5474 which case we must reconsider what fullscreen means.
5475 The following code is untested yet. */
5476 if (!NILP (fullscreen))
5478 x_set_fullscreen (f, fullscreen, fullscreen);
5479 w32fullscreen_hook (f);
5482 DebPrint (("display change: %d %d\n",
5483 (short) LOWORD (msg.msg.lParam),
5484 (short) HIWORD (msg.msg.lParam)));
5487 check_visibility = 1;
5488 break;
5490 #if HAVE_W32NOTIFY
5491 case WM_EMACS_FILENOTIFY:
5492 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5493 if (f)
5494 queue_notifications (&inev, &msg, f, &count);
5495 break;
5496 #endif
5498 default:
5499 /* Check for messages registered at runtime. */
5500 if (msg.msg.message == msh_mousewheel)
5502 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
5503 msg.msg.message = WM_MOUSEWHEEL;
5504 prepend_msg (&msg);
5506 break;
5509 if (inev.kind != NO_EVENT)
5511 kbd_buffer_store_event_hold (&inev, hold_quit);
5512 count++;
5515 if (do_help
5516 && !(hold_quit && hold_quit->kind != NO_EVENT))
5518 Lisp_Object frame;
5520 if (f)
5521 XSETFRAME (frame, f);
5522 else
5523 frame = Qnil;
5525 if (do_help > 0)
5527 if (NILP (help_echo_string))
5529 help_echo_object = help_echo_window = Qnil;
5530 help_echo_pos = -1;
5533 any_help_event_p = 1;
5534 gen_help_event (help_echo_string, frame, help_echo_window,
5535 help_echo_object, help_echo_pos);
5537 else
5539 help_echo_string = Qnil;
5540 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
5542 count++;
5546 /* If the focus was just given to an autoraising frame,
5547 raise it now. FIXME: handle more than one such frame. */
5548 if (dpyinfo->w32_pending_autoraise_frame)
5550 x_raise_frame (dpyinfo->w32_pending_autoraise_frame);
5551 dpyinfo->w32_pending_autoraise_frame = NULL;
5554 /* Check which frames are still visible, if we have enqueued any user
5555 events or been notified of events that may affect visibility. We
5556 do this here because there doesn't seem to be any direct
5557 notification from Windows that the visibility of a window has
5558 changed (at least, not in all cases). */
5559 if (count > 0 || check_visibility)
5561 Lisp_Object tail, frame;
5563 FOR_EACH_FRAME (tail, frame)
5565 struct frame *f = XFRAME (frame);
5566 /* The tooltip has been drawn already. Avoid the
5567 SET_FRAME_GARBAGED below. */
5568 if (EQ (frame, tip_frame))
5569 continue;
5571 /* Check "visible" frames and mark each as obscured or not.
5572 Note that visible is nonzero for unobscured and obscured
5573 frames, but zero for hidden and iconified frames. */
5574 if (FRAME_W32_P (f) && FRAME_VISIBLE_P (f))
5576 RECT clipbox;
5577 HDC hdc;
5578 bool obscured;
5580 enter_crit ();
5581 /* Query clipping rectangle for the entire window area
5582 (GetWindowDC), not just the client portion (GetDC).
5583 Otherwise, the scrollbars and menubar aren't counted as
5584 part of the visible area of the frame, and we may think
5585 the frame is obscured when really a scrollbar is still
5586 visible and gets WM_PAINT messages above. */
5587 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
5588 GetClipBox (hdc, &clipbox);
5589 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
5590 leave_crit ();
5592 obscured = FRAME_OBSCURED_P (f);
5594 if (clipbox.right == clipbox.left || clipbox.bottom == clipbox.top)
5596 /* Frame has become completely obscured so mark as such (we
5597 do this by setting visible to 2 so that FRAME_VISIBLE_P
5598 is still true, but redisplay will skip it). */
5599 SET_FRAME_VISIBLE (f, 2);
5601 if (!obscured)
5602 DebPrint (("frame %p (%s) obscured\n", f, SDATA (f->name)));
5604 else
5606 /* Frame is not obscured, so mark it as such. */
5607 SET_FRAME_VISIBLE (f, 1);
5609 if (obscured)
5611 SET_FRAME_GARBAGED (f);
5612 DebPrint (("obscured frame %p (%s) found to be visible\n",
5613 f, SDATA (f->name)));
5615 /* Force a redisplay sooner or later. */
5616 record_asynch_buffer_change ();
5623 unblock_input ();
5624 return count;
5629 /***********************************************************************
5630 Text Cursor
5631 ***********************************************************************/
5633 /* Set clipping for output in glyph row ROW. W is the window in which
5634 we operate. GC is the graphics context to set clipping in.
5636 ROW may be a text row or, e.g., a mode line. Text rows must be
5637 clipped to the interior of the window dedicated to text display,
5638 mode lines must be clipped to the whole window. */
5640 static void
5641 w32_clip_to_row (struct window *w, struct glyph_row *row,
5642 enum glyph_row_area area, HDC hdc)
5644 RECT clip_rect;
5645 int window_x, window_y, window_width;
5647 window_box (w, area, &window_x, &window_y, &window_width, 0);
5649 clip_rect.left = window_x;
5650 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5651 clip_rect.top = max (clip_rect.top, window_y);
5652 clip_rect.right = clip_rect.left + window_width;
5653 clip_rect.bottom = clip_rect.top + row->visible_height;
5655 w32_set_clip_rectangle (hdc, &clip_rect);
5659 /* Draw a hollow box cursor on window W in glyph row ROW. */
5661 static void
5662 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
5664 struct frame *f = XFRAME (WINDOW_FRAME (w));
5665 HDC hdc;
5666 RECT rect;
5667 int left, top, h;
5668 struct glyph *cursor_glyph;
5669 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5671 /* Get the glyph the cursor is on. If we can't tell because
5672 the current matrix is invalid or such, give up. */
5673 cursor_glyph = get_phys_cursor_glyph (w);
5674 if (cursor_glyph == NULL)
5676 DeleteObject (hb);
5677 return;
5680 /* Compute frame-relative coordinates for phys cursor. */
5681 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
5682 rect.left = left;
5683 /* When on R2L character, show cursor at the right edge of the
5684 glyph, unless the cursor box is as wide as the glyph or wider
5685 (the latter happens when x-stretch-cursor is non-nil). */
5686 if ((cursor_glyph->resolved_level & 1) != 0
5687 && cursor_glyph->pixel_width > w->phys_cursor_width)
5688 rect.left += cursor_glyph->pixel_width - w->phys_cursor_width;
5689 rect.top = top;
5690 rect.bottom = rect.top + h;
5691 rect.right = rect.left + w->phys_cursor_width;
5693 hdc = get_frame_dc (f);
5694 /* Set clipping, draw the rectangle, and reset clipping again. */
5695 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5696 FrameRect (hdc, &rect, hb);
5697 DeleteObject (hb);
5698 w32_set_clip_rectangle (hdc, NULL);
5699 release_frame_dc (f, hdc);
5703 /* Draw a bar cursor on window W in glyph row ROW.
5705 Implementation note: One would like to draw a bar cursor with an
5706 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5707 Unfortunately, I didn't find a font yet that has this property set.
5708 --gerd. */
5710 static void
5711 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
5712 int width, enum text_cursor_kinds kind)
5714 struct frame *f = XFRAME (w->frame);
5715 struct glyph *cursor_glyph;
5717 /* If cursor is out of bounds, don't draw garbage. This can happen
5718 in mini-buffer windows when switching between echo area glyphs
5719 and mini-buffer. */
5720 cursor_glyph = get_phys_cursor_glyph (w);
5721 if (cursor_glyph == NULL)
5722 return;
5724 /* If on an image, draw like a normal cursor. That's usually better
5725 visible than drawing a bar, esp. if the image is large so that
5726 the bar might not be in the window. */
5727 if (cursor_glyph->type == IMAGE_GLYPH)
5729 struct glyph_row *row;
5730 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5731 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5733 else
5735 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5736 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5737 int x;
5738 HDC hdc;
5740 /* If the glyph's background equals the color we normally draw
5741 the bar cursor in, the bar cursor in its normal color is
5742 invisible. Use the glyph's foreground color instead in this
5743 case, on the assumption that the glyph's colors are chosen so
5744 that the glyph is legible. */
5745 if (face->background == cursor_color)
5746 cursor_color = face->foreground;
5748 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5750 hdc = get_frame_dc (f);
5751 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5753 if (kind == BAR_CURSOR)
5755 if (width < 0)
5756 width = FRAME_CURSOR_WIDTH (f);
5757 width = min (cursor_glyph->pixel_width, width);
5759 w->phys_cursor_width = width;
5761 /* If the character under cursor is R2L, draw the bar cursor
5762 on the right of its glyph, rather than on the left. */
5763 if ((cursor_glyph->resolved_level & 1) != 0)
5764 x += cursor_glyph->pixel_width - width;
5766 w32_fill_area (f, hdc, cursor_color, x,
5767 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5768 width, row->height);
5770 else /* HBAR_CURSOR */
5772 int dummy_x, dummy_y, dummy_h;
5774 if (width < 0)
5775 width = row->height;
5777 width = min (row->height, width);
5779 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5780 &dummy_y, &dummy_h);
5781 if ((cursor_glyph->resolved_level & 1) != 0
5782 && cursor_glyph->pixel_width > w->phys_cursor_width)
5783 x += cursor_glyph->pixel_width - w->phys_cursor_width;
5784 w32_fill_area (f, hdc, cursor_color, x,
5785 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5786 row->height - width),
5787 w->phys_cursor_width, width);
5790 w32_set_clip_rectangle (hdc, NULL);
5791 release_frame_dc (f, hdc);
5796 /* RIF: Define cursor CURSOR on frame F. */
5798 static void
5799 w32_define_frame_cursor (struct frame *f, Cursor cursor)
5801 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5805 /* RIF: Clear area on frame F. */
5807 static void
5808 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
5810 HDC hdc;
5812 hdc = get_frame_dc (f);
5813 w32_clear_area (f, hdc, x, y, width, height);
5814 release_frame_dc (f, hdc);
5817 /* RIF: Draw or clear cursor on window W. */
5819 static void
5820 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
5821 int x, int y, enum text_cursor_kinds cursor_type,
5822 int cursor_width, bool on_p, bool active_p)
5824 if (on_p)
5826 /* If the user wants to use the system caret, make sure our own
5827 cursor remains invisible. */
5828 if (w32_use_visible_system_caret)
5830 /* Call to erase_phys_cursor here seems to use the
5831 wrong values of w->phys_cursor, as they have been
5832 overwritten before this function was called. */
5833 if (w->phys_cursor_type != NO_CURSOR)
5834 erase_phys_cursor (w);
5836 cursor_type = w->phys_cursor_type = NO_CURSOR;
5837 w->phys_cursor_width = -1;
5839 else
5841 w->phys_cursor_type = cursor_type;
5844 w->phys_cursor_on_p = true;
5846 /* If this is the active cursor, we need to track it with the
5847 system caret, so third party software like screen magnifiers
5848 and speech synthesizers can follow the cursor. */
5849 if (active_p)
5851 struct frame *f = XFRAME (WINDOW_FRAME (w));
5852 HWND hwnd = FRAME_W32_WINDOW (f);
5854 w32_system_caret_x
5855 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5856 w32_system_caret_y
5857 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5858 + glyph_row->ascent - w->phys_cursor_ascent);
5859 w32_system_caret_window = w;
5860 w32_system_caret_hdr_height = WINDOW_HEADER_LINE_HEIGHT (w);
5861 w32_system_caret_mode_height = WINDOW_MODE_LINE_HEIGHT (w);
5863 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
5865 /* If the size of the active cursor changed, destroy the old
5866 system caret. */
5867 if (w32_system_caret_hwnd
5868 && (w32_system_caret_height != w->phys_cursor_height))
5869 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5871 w32_system_caret_height = w->phys_cursor_height;
5873 /* Move the system caret. */
5874 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5877 if (glyph_row->exact_window_width_line_p
5878 && (glyph_row->reversed_p
5879 ? (w->phys_cursor.hpos < 0)
5880 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5882 glyph_row->cursor_in_fringe_p = true;
5883 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5884 return;
5887 switch (cursor_type)
5889 case HOLLOW_BOX_CURSOR:
5890 x_draw_hollow_cursor (w, glyph_row);
5891 break;
5893 case FILLED_BOX_CURSOR:
5894 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5895 break;
5897 case BAR_CURSOR:
5898 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5899 break;
5901 case HBAR_CURSOR:
5902 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5903 break;
5905 case NO_CURSOR:
5906 w->phys_cursor_width = 0;
5907 break;
5909 default:
5910 emacs_abort ();
5917 /* Icons. */
5919 bool
5920 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5922 HANDLE main_icon;
5923 HANDLE small_icon = NULL;
5925 if (FRAME_W32_WINDOW (f) == 0)
5926 return 1;
5928 if (NILP (icon))
5929 main_icon = LoadIcon (hinst, EMACS_CLASS);
5930 else if (STRINGP (icon))
5932 /* Load the main icon from the named file. */
5933 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5934 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5935 /* Try to load a small icon to go with it. */
5936 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5937 GetSystemMetrics (SM_CXSMICON),
5938 GetSystemMetrics (SM_CYSMICON),
5939 LR_LOADFROMFILE);
5941 else if (SYMBOLP (icon))
5943 LPCTSTR name;
5945 if (EQ (icon, intern ("application")))
5946 name = (LPCTSTR) IDI_APPLICATION;
5947 else if (EQ (icon, intern ("hand")))
5948 name = (LPCTSTR) IDI_HAND;
5949 else if (EQ (icon, intern ("question")))
5950 name = (LPCTSTR) IDI_QUESTION;
5951 else if (EQ (icon, intern ("exclamation")))
5952 name = (LPCTSTR) IDI_EXCLAMATION;
5953 else if (EQ (icon, intern ("asterisk")))
5954 name = (LPCTSTR) IDI_ASTERISK;
5955 else if (EQ (icon, intern ("winlogo")))
5956 name = (LPCTSTR) IDI_WINLOGO;
5957 else
5958 return 1;
5960 main_icon = LoadIcon (NULL, name);
5962 else
5963 return 1;
5965 if (main_icon == NULL)
5966 return 1;
5968 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5969 (LPARAM) main_icon);
5971 /* If there is a small icon that goes with it, set that too. */
5972 if (small_icon)
5973 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5974 (LPARAM) small_icon);
5976 return 0;
5980 /************************************************************************
5981 Handling X errors
5982 ************************************************************************/
5984 /* Display Error Handling functions not used on W32. Listing them here
5985 helps diff stay in step when comparing w32term.c with xterm.c.
5987 x_error_catcher (display, error)
5988 x_catch_errors (dpy)
5989 x_catch_errors_unwind (old_val)
5990 x_check_errors (dpy, format)
5991 x_fully_uncatch_errors ()
5992 x_had_errors_p (dpy)
5993 x_clear_errors (dpy)
5994 x_uncatch_errors (dpy, count)
5995 x_trace_wire ()
5996 x_connection_signal (signalnum)
5997 x_connection_closed (dpy, error_message)
5998 x_error_quitter (display, error)
5999 x_error_handler (display, error)
6000 x_io_error_quitter (display)
6005 /* Changing the font of the frame. */
6007 Lisp_Object
6008 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
6010 struct font *font = XFONT_OBJECT (font_object);
6011 int unit, font_ascent, font_descent;
6013 if (fontset < 0)
6014 fontset = fontset_from_font (font_object);
6015 FRAME_FONTSET (f) = fontset;
6016 if (FRAME_FONT (f) == font)
6017 /* This font is already set in frame F. There's nothing more to
6018 do. */
6019 return font_object;
6021 FRAME_FONT (f) = font;
6022 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
6023 FRAME_COLUMN_WIDTH (f) = unit = font->average_width;
6024 get_font_ascent_descent (font, &font_ascent, &font_descent);
6025 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
6027 /* Compute number of scrollbar columns. */
6028 unit = FRAME_COLUMN_WIDTH (f);
6029 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
6030 FRAME_CONFIG_SCROLL_BAR_COLS (f)
6031 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
6032 else
6034 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
6035 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) =
6036 FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
6039 /* Now make the frame display the given font. */
6040 if (FRAME_X_WINDOW (f) != 0)
6042 /* Don't change the size of a tip frame; there's no point in
6043 doing it because it's done in Fx_show_tip, and it leads to
6044 problems because the tip frame has no widget. */
6045 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
6046 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
6047 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
6048 false, Qfont);
6051 /* X version sets font of input methods here also. */
6053 return font_object;
6057 /***********************************************************************
6058 TODO: W32 Input Methods
6059 ***********************************************************************/
6060 /* Listing missing functions from xterm.c helps diff stay in step.
6062 xim_destroy_callback (xim, client_data, call_data)
6063 xim_open_dpy (dpyinfo, resource_name)
6064 struct xim_inst_t
6065 xim_instantiate_callback (display, client_data, call_data)
6066 xim_initialize (dpyinfo, resource_name)
6067 xim_close_dpy (dpyinfo)
6072 /* Calculate the absolute position in frame F
6073 from its current recorded position values and gravity. */
6075 static void
6076 x_calc_absolute_position (struct frame *f)
6078 int flags = f->size_hint_flags;
6080 /* The sum of the widths of the frame's left and right borders, and
6081 the sum of the heights of the frame's top and bottom borders (in
6082 pixels) drawn by Windows. */
6083 unsigned int left_right_borders_width, top_bottom_borders_height;
6085 /* Try to get the actual values of these two variables. We compute
6086 the border width (height) by subtracting the width (height) of
6087 the frame's client area from the width (height) of the frame's
6088 entire window. */
6089 WINDOWPLACEMENT wp = { 0 };
6090 RECT client_rect = { 0 };
6092 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
6093 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
6095 left_right_borders_width =
6096 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
6097 (client_rect.right - client_rect.left);
6099 top_bottom_borders_height =
6100 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
6101 (client_rect.bottom - client_rect.top);
6103 else
6105 /* Use sensible default values. */
6106 left_right_borders_width = 8;
6107 top_bottom_borders_height = 32;
6110 /* With multiple monitors, we can legitimately get negative
6111 coordinates (for monitors above or to the left of the primary
6112 monitor). Find the display origin to ensure negative positions
6113 are computed correctly (Bug#21173). */
6114 int display_left = 0;
6115 int display_top = 0;
6116 struct frame *p = FRAME_PARENT_FRAME (f);
6118 if (!p && flags & (XNegative | YNegative))
6120 Lisp_Object list;
6122 list = Fw32_display_monitor_attributes_list (Qnil);
6123 while (CONSP (list))
6125 Lisp_Object attributes = CAR(list);
6126 Lisp_Object geometry;
6127 Lisp_Object monitor_left, monitor_top;
6129 list = CDR(list);
6131 geometry = Fassoc (Qgeometry, attributes, Qnil);
6132 if (!NILP (geometry))
6134 monitor_left = Fnth (make_number (1), geometry);
6135 monitor_top = Fnth (make_number (2), geometry);
6137 display_left = min (display_left, XINT (monitor_left));
6138 display_top = min (display_top, XINT (monitor_top));
6143 /* Treat negative positions as relative to the rightmost bottommost
6144 position that fits on the screen or parent frame.
6146 I see no need for subtracting 1 from the border widths - is there
6147 any on the remaining platforms? Here these subtractions did put
6148 the last pixel line/column of a frame off-display when, for
6149 example, a (set-frame-parameter nil 'left '(- 0)) specification was
6150 used - martin 20017-05-05. */
6151 if (flags & XNegative)
6153 if (p)
6154 f->left_pos = (FRAME_PIXEL_WIDTH (p)
6155 - FRAME_PIXEL_WIDTH (f)
6156 + f->left_pos
6157 - left_right_borders_width);
6158 else
6159 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
6160 + display_left
6161 - FRAME_PIXEL_WIDTH (f)
6162 + f->left_pos
6163 - left_right_borders_width);
6166 if (flags & YNegative)
6168 if (p)
6169 f->top_pos = (FRAME_PIXEL_HEIGHT (p)
6170 - FRAME_PIXEL_HEIGHT (f)
6171 + f->top_pos
6172 - top_bottom_borders_height);
6173 else
6174 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
6175 + display_top
6176 - FRAME_PIXEL_HEIGHT (f)
6177 + f->top_pos
6178 - top_bottom_borders_height);
6181 /* The left_pos and top_pos are now relative to the top and left
6182 screen edges, so the flags should correspond. */
6183 f->size_hint_flags &= ~ (XNegative | YNegative);
6186 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
6187 to really change the position, and 0 when calling from
6188 x_make_frame_visible (in that case, XOFF and YOFF are the current
6189 position values). It is -1 when calling from x_set_frame_parameters,
6190 which means, do adjust for borders but don't change the gravity. */
6192 void
6193 x_set_offset (struct frame *f, register int xoff, register int yoff,
6194 int change_gravity)
6196 int modified_top, modified_left;
6198 if (change_gravity > 0)
6200 f->top_pos = yoff;
6201 f->left_pos = xoff;
6202 f->size_hint_flags &= ~ (XNegative | YNegative);
6203 if (xoff < 0)
6204 f->size_hint_flags |= XNegative;
6205 if (yoff < 0)
6206 f->size_hint_flags |= YNegative;
6207 f->win_gravity = NorthWestGravity;
6209 x_calc_absolute_position (f);
6211 block_input ();
6212 x_wm_set_size_hint (f, (long) 0, false);
6214 modified_left = f->left_pos;
6215 modified_top = f->top_pos;
6217 if (!FRAME_PARENT_FRAME (f))
6218 my_set_window_pos (FRAME_W32_WINDOW (f), NULL,
6219 modified_left, modified_top,
6220 0, 0,
6221 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
6222 else
6223 my_set_window_pos (FRAME_W32_WINDOW (f), HWND_TOP,
6224 modified_left, modified_top,
6225 0, 0,
6226 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
6227 unblock_input ();
6230 static void
6231 w32fullscreen_hook (struct frame *f)
6233 if (FRAME_VISIBLE_P (f))
6235 HWND hwnd = FRAME_W32_WINDOW(f);
6236 DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
6237 RECT rect;
6238 enum fullscreen_type prev_fsmode = FRAME_PREV_FSMODE (f);
6240 block_input();
6241 f->want_fullscreen &= ~FULLSCREEN_WAIT;
6243 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
6244 GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6246 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
6248 if (!FRAME_UNDECORATED (f))
6249 SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
6250 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6252 else if (FRAME_PREV_FSMODE (f) == FULLSCREEN_HEIGHT
6253 || FRAME_PREV_FSMODE (f) == FULLSCREEN_WIDTH)
6254 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6256 FRAME_PREV_FSMODE (f) = f->want_fullscreen;
6258 if (f->want_fullscreen == FULLSCREEN_NONE)
6259 ShowWindow (hwnd, SW_SHOWNORMAL);
6260 else if (f->want_fullscreen == FULLSCREEN_MAXIMIZED)
6262 if (prev_fsmode == FULLSCREEN_BOTH || prev_fsmode == FULLSCREEN_WIDTH
6263 || prev_fsmode == FULLSCREEN_HEIGHT)
6264 /* Make window normal since otherwise the subsequent
6265 maximization might fail in some cases. */
6266 ShowWindow (hwnd, SW_SHOWNORMAL);
6267 ShowWindow (hwnd, SW_MAXIMIZE);
6269 else if (f->want_fullscreen == FULLSCREEN_BOTH)
6271 int menu_bar_height = GetSystemMetrics (SM_CYMENU);
6273 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6274 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6275 if (!FRAME_UNDECORATED (f))
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, depending on
6611 the value of Vx_visible_frame_timeout.
6612 However, if the window manager asks the user where to position
6613 the frame, this will return before the user finishes doing that.
6614 The frame will not actually be visible at that time,
6615 but it will become visible later when the window manager
6616 finishes with it. */
6618 void
6619 x_make_frame_visible (struct frame *f)
6621 block_input ();
6623 x_set_bitmap_icon (f);
6625 if (! FRAME_VISIBLE_P (f))
6627 /* We test FRAME_GARBAGED_P here to make sure we don't
6628 call x_set_offset a second time
6629 if we get to x_make_frame_visible a second time
6630 before the window gets really visible. */
6631 if (! FRAME_ICONIFIED_P (f)
6632 && ! f->output_data.w32->asked_for_visible)
6634 if (!FRAME_PARENT_FRAME (f))
6636 RECT workarea_rect;
6637 RECT window_rect;
6639 /* Adjust vertical window position in order to avoid being
6640 covered by a taskbar placed at the bottom of the desktop. */
6641 SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
6642 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
6643 if (window_rect.bottom > workarea_rect.bottom
6644 && window_rect.top > workarea_rect.top)
6645 f->top_pos = max (window_rect.top
6646 - window_rect.bottom + workarea_rect.bottom,
6647 workarea_rect.top);
6650 x_set_offset (f, f->left_pos, f->top_pos, 0);
6653 f->output_data.w32->asked_for_visible = 1;
6655 /* According to a report in emacs-devel 2008-06-03, SW_SHOWNORMAL
6656 causes unexpected behavior when unminimizing frames that were
6657 previously maximized. But only SW_SHOWNORMAL works properly for
6658 frames that were truely hidden (using make-frame-invisible), so
6659 we need it to avoid Bug#5482. It seems that iconified is only
6660 set for minimized windows that are still visible, so use that to
6661 determine the appropriate flag to pass ShowWindow. */
6662 my_show_window (f, FRAME_W32_WINDOW (f),
6663 FRAME_ICONIFIED_P (f)
6664 ? SW_RESTORE
6665 : FRAME_NO_FOCUS_ON_MAP (f)
6666 ? SW_SHOWNOACTIVATE
6667 : SW_SHOWNORMAL);
6670 if (!FLOATP (Vx_wait_for_event_timeout))
6671 return;
6673 /* Synchronize to ensure Emacs knows the frame is visible
6674 before we do anything else. We do this loop with input not blocked
6675 so that incoming events are handled. */
6677 Lisp_Object frame;
6678 double timeout = XFLOAT_DATA (Vx_wait_for_event_timeout);
6679 double start_time = XFLOAT_DATA (Ffloat_time (Qnil));
6681 /* This must come after we set COUNT. */
6682 unblock_input ();
6684 XSETFRAME (frame, f);
6686 /* Wait until the frame is visible. Process X events until a
6687 MapNotify event has been seen, or until we think we won't get a
6688 MapNotify at all.. */
6689 while (timeout > (XFLOAT_DATA (Ffloat_time (Qnil)) - start_time) &&
6690 !FRAME_VISIBLE_P (f))
6692 /* Force processing of queued events. */
6693 /* TODO: x_sync equivalent? */
6695 /* Machines that do polling rather than SIGIO have been observed
6696 to go into a busy-wait here. So we'll fake an alarm signal
6697 to let the handler know that there's something to be read.
6698 We used to raise a real alarm, but it seems that the handler
6699 isn't always enabled here. This is probably a bug. */
6700 if (input_polling_used ())
6702 /* It could be confusing if a real alarm arrives while processing
6703 the fake one. Turn it off and let the handler reset it. */
6704 int old_poll_suppress_count = poll_suppress_count;
6705 poll_suppress_count = 1;
6706 poll_for_input_1 ();
6707 poll_suppress_count = old_poll_suppress_count;
6713 /* Change from mapped state to withdrawn state. */
6715 /* Make the frame visible (mapped and not iconified). */
6717 void
6718 x_make_frame_invisible (struct frame *f)
6720 /* Don't keep the highlight on an invisible frame. */
6721 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6722 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6724 block_input ();
6726 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
6728 /* We can't distinguish this from iconification
6729 just by the event that we get from the server.
6730 So we can't win using the usual strategy of letting
6731 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
6732 and synchronize with the server to make sure we agree. */
6733 SET_FRAME_VISIBLE (f, 0);
6734 SET_FRAME_ICONIFIED (f, false);
6736 unblock_input ();
6739 /* Change window state from mapped to iconified. */
6741 void
6742 x_iconify_frame (struct frame *f)
6744 /* Don't keep the highlight on an invisible frame. */
6745 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6746 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6748 if (FRAME_ICONIFIED_P (f))
6749 return;
6751 block_input ();
6753 x_set_bitmap_icon (f);
6755 /* Simulate the user minimizing the frame. */
6756 SendMessageTimeout (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0,
6757 0, 6000, NULL);
6759 SET_FRAME_VISIBLE (f, 0);
6760 SET_FRAME_ICONIFIED (f, true);
6762 unblock_input ();
6766 /* Free X resources of frame F. */
6768 void
6769 x_free_frame_resources (struct frame *f)
6771 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6772 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
6774 block_input ();
6776 /* We must free faces before destroying windows because some
6777 font-driver (e.g. xft) access a window while finishing a
6778 face. */
6779 free_frame_faces (f);
6781 if (FRAME_W32_WINDOW (f))
6782 my_destroy_window (f, FRAME_W32_WINDOW (f));
6784 free_frame_menubar (f);
6786 xfree (f->output_data.w32);
6787 f->output_data.w32 = NULL;
6789 if (f == dpyinfo->w32_focus_frame)
6790 dpyinfo->w32_focus_frame = 0;
6791 if (f == dpyinfo->w32_focus_event_frame)
6792 dpyinfo->w32_focus_event_frame = 0;
6793 if (f == dpyinfo->x_highlight_frame)
6794 dpyinfo->x_highlight_frame = 0;
6795 if (f == hlinfo->mouse_face_mouse_frame)
6796 reset_mouse_highlight (hlinfo);
6798 unblock_input ();
6802 /* Destroy the window of frame F. */
6803 static void
6804 x_destroy_window (struct frame *f)
6806 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6808 x_free_frame_resources (f);
6809 dpyinfo->reference_count--;
6813 /* Setting window manager hints. */
6815 /* Set the normal size hints for the window manager, for frame F.
6816 FLAGS is the flags word to use--or 0 meaning preserve the flags
6817 that the window now has.
6818 If USER_POSITION, set the USPosition
6819 flag (this is useful when FLAGS is 0). */
6820 void
6821 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
6823 Window window = FRAME_W32_WINDOW (f);
6825 enter_crit ();
6827 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
6828 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
6829 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
6830 SetWindowLong (window, WND_VSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_WIDTH (f));
6831 SetWindowLong (window, WND_HSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_HEIGHT (f));
6833 leave_crit ();
6836 /***********************************************************************
6837 Fonts
6838 ***********************************************************************/
6840 #ifdef GLYPH_DEBUG
6842 /* Check that FONT is valid on frame F. It is if it can be found in F's
6843 font table. */
6845 static void
6846 x_check_font (struct frame *f, struct font *font)
6848 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
6849 if (font->driver->check)
6850 eassert (font->driver->check (f, font) == 0);
6853 #endif /* GLYPH_DEBUG */
6855 /* Show hourglass cursor on frame F. */
6857 static void
6858 w32_show_hourglass (struct frame *f)
6860 if (!menubar_in_use && !current_popup_menu)
6862 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6864 w32->hourglass_p = 1;
6865 SetCursor (w32->hourglass_cursor);
6869 /* Hide hourglass cursor on frame F. */
6871 static void
6872 w32_hide_hourglass (struct frame *f)
6874 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6876 w32->hourglass_p = 0;
6877 if (f->pointer_invisible)
6878 SetCursor (NULL);
6879 else
6880 SetCursor (w32->current_cursor);
6883 /* FIXME: old code did that, but I don't know why. Anyway,
6884 this is used for non-GUI frames (see cancel_hourglass). */
6886 void
6887 w32_arrow_cursor (void)
6889 SetCursor (w32_load_cursor (IDC_ARROW));
6892 static void
6893 w32_toggle_invisible_pointer (struct frame *f, bool invisible)
6895 block_input ();
6897 if (f->pointer_invisible != invisible)
6899 f->pointer_invisible = invisible;
6900 w32_define_cursor (FRAME_W32_WINDOW (f),
6901 f->output_data.w32->current_cursor);
6904 unblock_input ();
6907 /***********************************************************************
6908 Initialization
6909 ***********************************************************************/
6911 static int w32_initialized = 0;
6913 void
6914 w32_initialize_display_info (Lisp_Object display_name)
6916 struct w32_display_info *dpyinfo = &one_w32_display_info;
6918 memset (dpyinfo, 0, sizeof (*dpyinfo));
6920 dpyinfo->name_list_element = Fcons (display_name, Qnil);
6921 dpyinfo->w32_id_name = xmalloc (SCHARS (Vinvocation_name)
6922 + SCHARS (Vsystem_name) + 2);
6923 sprintf (dpyinfo->w32_id_name, "%s@%s",
6924 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6926 /* Default Console mode values - overridden when running in GUI mode
6927 with values obtained from system metrics. */
6928 dpyinfo->resx = 1;
6929 dpyinfo->resy = 1;
6930 dpyinfo->n_planes = 1;
6931 dpyinfo->n_cbits = 4;
6932 dpyinfo->n_fonts = 0;
6933 dpyinfo->smallest_font_height = 1;
6934 dpyinfo->smallest_char_width = 1;
6935 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6936 dpyinfo->horizontal_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6937 /* TODO: dpyinfo->gray */
6939 reset_mouse_highlight (&dpyinfo->mouse_highlight);
6942 /* Create an xrdb-style database of resources to supersede registry settings.
6943 The database is just a concatenation of C strings, finished by an additional
6944 \0. The strings are submitted to some basic normalization, so
6946 [ *]option[ *]:[ *]value...
6948 becomes
6950 option:value...
6952 but any whitespace following value is not removed. */
6954 static char *
6955 w32_make_rdb (char *xrm_option)
6957 char *buffer = xmalloc (strlen (xrm_option) + 2);
6958 char *current = buffer;
6959 char ch;
6960 int in_option = 1;
6961 int before_value = 0;
6963 do {
6964 ch = *xrm_option++;
6966 if (ch == '\n')
6968 *current++ = '\0';
6969 in_option = 1;
6970 before_value = 0;
6972 else if (ch != ' ')
6974 *current++ = ch;
6975 if (in_option && (ch == ':'))
6977 in_option = 0;
6978 before_value = 1;
6980 else if (before_value)
6982 before_value = 0;
6985 else if (!(in_option || before_value))
6987 *current++ = ch;
6989 } while (ch);
6991 *current = '\0';
6993 return buffer;
6996 extern frame_parm_handler w32_frame_parm_handlers[];
6998 static struct redisplay_interface w32_redisplay_interface =
7000 w32_frame_parm_handlers,
7001 x_produce_glyphs,
7002 x_write_glyphs,
7003 x_insert_glyphs,
7004 x_clear_end_of_line,
7005 x_scroll_run,
7006 x_after_update_window_line,
7007 x_update_window_begin,
7008 x_update_window_end,
7009 0, /* flush_display */
7010 x_clear_window_mouse_face,
7011 x_get_glyph_overhangs,
7012 x_fix_overlapping_area,
7013 w32_draw_fringe_bitmap,
7014 w32_define_fringe_bitmap,
7015 w32_destroy_fringe_bitmap,
7016 w32_compute_glyph_string_overhangs,
7017 x_draw_glyph_string,
7018 w32_define_frame_cursor,
7019 w32_clear_frame_area,
7020 w32_draw_window_cursor,
7021 w32_draw_vertical_window_border,
7022 w32_draw_window_divider,
7023 w32_shift_glyphs_for_insert,
7024 w32_show_hourglass,
7025 w32_hide_hourglass
7028 static void x_delete_terminal (struct terminal *term);
7030 static struct terminal *
7031 w32_create_terminal (struct w32_display_info *dpyinfo)
7033 struct terminal *terminal;
7035 terminal = create_terminal (output_w32, &w32_redisplay_interface);
7037 terminal->display_info.w32 = dpyinfo;
7038 dpyinfo->terminal = terminal;
7040 /* MSVC does not type K&R functions with no arguments correctly, and
7041 so we must explicitly cast them. */
7042 terminal->clear_frame_hook = x_clear_frame;
7043 terminal->ins_del_lines_hook = x_ins_del_lines;
7044 terminal->delete_glyphs_hook = x_delete_glyphs;
7045 terminal->ring_bell_hook = w32_ring_bell;
7046 terminal->toggle_invisible_pointer_hook = w32_toggle_invisible_pointer;
7047 terminal->update_begin_hook = x_update_begin;
7048 terminal->update_end_hook = x_update_end;
7049 terminal->read_socket_hook = w32_read_socket;
7050 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
7051 terminal->mouse_position_hook = w32_mouse_position;
7052 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
7053 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
7054 terminal->fullscreen_hook = w32fullscreen_hook;
7055 terminal->menu_show_hook = w32_menu_show;
7056 terminal->popup_dialog_hook = w32_popup_dialog;
7057 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
7058 terminal->set_horizontal_scroll_bar_hook = w32_set_horizontal_scroll_bar;
7059 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
7060 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
7061 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
7062 terminal->delete_frame_hook = x_destroy_window;
7063 terminal->delete_terminal_hook = x_delete_terminal;
7064 /* Other hooks are NULL by default. */
7066 /* We don't yet support separate terminals on W32, so don't try to share
7067 keyboards between virtual terminals that are on the same physical
7068 terminal like X does. */
7069 terminal->kboard = allocate_kboard (Qw32);
7070 /* Don't let the initial kboard remain current longer than necessary.
7071 That would cause problems if a file loaded on startup tries to
7072 prompt in the mini-buffer. */
7073 if (current_kboard == initial_kboard)
7074 current_kboard = terminal->kboard;
7075 terminal->kboard->reference_count++;
7077 return terminal;
7080 static void
7081 x_delete_terminal (struct terminal *terminal)
7083 struct w32_display_info *dpyinfo = terminal->display_info.w32;
7085 /* Protect against recursive calls. delete_frame in
7086 delete_terminal calls us back when it deletes our last frame. */
7087 if (!terminal->name)
7088 return;
7090 block_input ();
7092 x_delete_display (dpyinfo);
7093 unblock_input ();
7096 struct w32_display_info *
7097 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
7099 struct w32_display_info *dpyinfo;
7100 struct terminal *terminal;
7101 HDC hdc;
7103 block_input ();
7105 if (!w32_initialized)
7107 w32_initialize ();
7108 w32_initialized = 1;
7111 w32_initialize_display_info (display_name);
7113 dpyinfo = &one_w32_display_info;
7114 terminal = w32_create_terminal (dpyinfo);
7116 /* Set the name of the terminal. */
7117 terminal->name = xlispstrdup (display_name);
7119 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
7121 /* Put this display on the chain. */
7122 dpyinfo->next = x_display_list;
7123 x_display_list = dpyinfo;
7125 hdc = GetDC (NULL);
7127 dpyinfo->root_window = GetDesktopWindow ();
7128 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
7129 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
7130 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
7131 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
7132 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
7133 ReleaseDC (NULL, hdc);
7135 /* initialize palette with white and black */
7137 XColor color;
7138 w32_defined_color (0, "white", &color, 1);
7139 w32_defined_color (0, "black", &color, 1);
7142 #ifdef WINDOWSNT
7143 /* Add the default keyboard. When !WINDOWSNT, we're using the
7144 standard Emacs console handling machinery and don't need an
7145 explicit FD here. */
7146 add_keyboard_wait_descriptor (0);
7147 #elif CYGWIN
7148 /* /dev/windows wakes us up when we have a thread message pending. */
7149 add_keyboard_wait_descriptor (w32_message_fd);
7150 #endif
7152 /* Create Fringe Bitmaps and store them for later use.
7154 On W32, bitmaps are all unsigned short, as Windows requires
7155 bitmap data to be Word aligned. For some reason they are
7156 horizontally reflected compared to how they appear on X, so we
7157 need to bitswap and convert to unsigned shorts before creating
7158 the bitmaps. */
7159 w32_init_fringe (terminal->rif);
7161 unblock_input ();
7163 return dpyinfo;
7166 /* Get rid of display DPYINFO, assuming all frames are already gone. */
7167 void
7168 x_delete_display (struct w32_display_info *dpyinfo)
7170 /* FIXME: the only display info apparently can't be deleted. */
7171 /* free palette table */
7173 struct w32_palette_entry * plist;
7175 plist = dpyinfo->color_list;
7176 while (plist)
7178 struct w32_palette_entry * pentry = plist;
7179 plist = plist->next;
7180 xfree (pentry);
7182 dpyinfo->color_list = NULL;
7183 if (dpyinfo->palette)
7184 DeleteObject (dpyinfo->palette);
7186 w32_reset_fringes ();
7190 /* Set up use of W32. */
7192 void
7193 w32_init_main_thread (void)
7195 dwMainThreadId = GetCurrentThreadId ();
7196 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
7197 GetCurrentProcess (), &hMainThread, 0, TRUE,
7198 DUPLICATE_SAME_ACCESS);
7203 DWORD WINAPI w32_msg_worker (void * arg);
7205 static void
7206 w32_initialize (void)
7208 HANDLE shell;
7209 HRESULT (WINAPI * set_user_model) (const wchar_t * id);
7211 baud_rate = 19200;
7213 w32_system_caret_hwnd = NULL;
7214 w32_system_caret_height = 0;
7215 w32_system_caret_x = 0;
7216 w32_system_caret_y = 0;
7218 /* On Windows 7 and later, we need to set the user model ID
7219 to associate emacsclient launched files with Emacs frames
7220 in the UI. */
7221 shell = GetModuleHandle ("shell32.dll");
7222 if (shell)
7224 set_user_model
7225 = (void *) GetProcAddress (shell,
7226 "SetCurrentProcessExplicitAppUserModelID");
7228 /* If the function is defined, then we are running on Windows 7
7229 or newer, and the UI uses this to group related windows
7230 together. Since emacs, runemacs, emacsclient are related, we
7231 want them grouped even though the executables are different,
7232 so we need to set a consistent ID between them. */
7233 if (set_user_model)
7234 set_user_model (L"GNU.Emacs");
7237 #ifdef CYGWIN
7238 if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
7239 fatal ("opening /dev/windows: %s", strerror (errno));
7240 #endif /* CYGWIN */
7242 /* Initialize w32_use_visible_system_caret based on whether a screen
7243 reader is in use. */
7244 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
7245 &w32_use_visible_system_caret, 0))
7246 w32_use_visible_system_caret = 0;
7248 any_help_event_p = 0;
7250 /* Initialize input mode: interrupt_input off, no flow control, allow
7251 8 bit character input, standard quit char. */
7252 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
7255 LCID input_locale_id = LOWORD (GetKeyboardLayout (0));
7256 w32_keyboard_codepage = codepage_for_locale (input_locale_id);
7259 /* Create the window thread - it will terminate itself when the app
7260 terminates */
7261 init_crit ();
7263 /* Wait for thread to start */
7265 MSG msg;
7267 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
7269 hWindowsThread = CreateThread (NULL, 0,
7270 w32_msg_worker,
7271 0, 0, &dwWindowsThreadId);
7273 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
7276 /* It is desirable that mainThread should have the same notion of
7277 focus window and active window as windowsThread. Unfortunately, the
7278 following call to AttachThreadInput, which should do precisely what
7279 we need, causes major problems when Emacs is linked as a console
7280 program. Unfortunately, we have good reasons for doing that, so
7281 instead we need to send messages to windowsThread to make some API
7282 calls for us (ones that affect, or depend on, the active/focus
7283 window state.) */
7284 #ifdef ATTACH_THREADS
7285 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
7286 #endif
7288 /* Dynamically link to optional system components. */
7290 HMODULE user_lib = GetModuleHandle ("user32.dll");
7292 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
7294 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
7296 #undef LOAD_PROC
7298 /* Ensure scrollbar handles are at least 5 pixels. */
7299 vertical_scroll_bar_min_handle = 5;
7300 horizontal_scroll_bar_min_handle = 5;
7302 /* For either kind of scroll bar, take account of the arrows; these
7303 effectively form the border of the main scroll bar range. */
7304 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
7305 = GetSystemMetrics (SM_CYVSCROLL);
7306 horizontal_scroll_bar_left_border = horizontal_scroll_bar_right_border
7307 = GetSystemMetrics (SM_CYHSCROLL);
7311 void
7312 syms_of_w32term (void)
7314 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
7316 DEFSYM (Qadded, "added");
7317 DEFSYM (Qremoved, "removed");
7318 DEFSYM (Qmodified, "modified");
7319 DEFSYM (Qrenamed_from, "renamed-from");
7320 DEFSYM (Qrenamed_to, "renamed-to");
7322 DEFVAR_LISP ("x-wait-for-event-timeout", Vx_wait_for_event_timeout,
7323 doc: /* How long to wait for X events.
7325 Emacs will wait up to this many seconds to receive X events after
7326 making changes which affect the state of the graphical interface.
7327 Under some window managers this can take an indefinite amount of time,
7328 so it is important to limit the wait.
7330 If set to a non-float value, there will be no wait at all. */);
7331 Vx_wait_for_event_timeout = make_float (0.1);
7333 DEFVAR_INT ("w32-num-mouse-buttons",
7334 w32_num_mouse_buttons,
7335 doc: /* Number of physical mouse buttons. */);
7336 w32_num_mouse_buttons = 2;
7338 DEFVAR_LISP ("w32-swap-mouse-buttons",
7339 Vw32_swap_mouse_buttons,
7340 doc: /* Swap the mapping of middle and right mouse buttons.
7341 When nil, middle button is mouse-2 and right button is mouse-3. */);
7342 Vw32_swap_mouse_buttons = Qnil;
7344 DEFVAR_LISP ("w32-grab-focus-on-raise",
7345 Vw32_grab_focus_on_raise,
7346 doc: /* Raised frame grabs input focus.
7347 When t, `raise-frame' grabs input focus as well. This fits well
7348 with the normal Windows click-to-focus policy, but might not be
7349 desirable when using a point-to-focus policy. */);
7350 Vw32_grab_focus_on_raise = Qt;
7352 DEFVAR_LISP ("w32-capslock-is-shiftlock",
7353 Vw32_capslock_is_shiftlock,
7354 doc: /* Apply CapsLock state to non character input keys.
7355 When nil, CapsLock only affects normal character input keys. */);
7356 Vw32_capslock_is_shiftlock = Qnil;
7358 DEFVAR_LISP ("w32-recognize-altgr",
7359 Vw32_recognize_altgr,
7360 doc: /* Recognize right-alt and left-ctrl as AltGr.
7361 When nil, the right-alt and left-ctrl key combination is
7362 interpreted normally. */);
7363 Vw32_recognize_altgr = Qt;
7365 DEFVAR_BOOL ("w32-use-visible-system-caret",
7366 w32_use_visible_system_caret,
7367 doc: /* Flag to make the system caret visible.
7368 When this is non-nil, Emacs will indicate the position of point by
7369 using the system caret instead of drawing its own cursor. Some screen
7370 reader software does not track the system cursor properly when it is
7371 invisible, and gets confused by Emacs drawing its own cursor, so this
7372 variable is initialized to t when Emacs detects that screen reader
7373 software is running as it starts up.
7375 When this variable is set, other variables affecting the appearance of
7376 the cursor have no effect. */);
7378 w32_use_visible_system_caret = 0;
7380 /* We don't yet support this, but defining this here avoids whining
7381 from cus-start.el and other places, like "M-x set-variable". */
7382 DEFVAR_BOOL ("x-use-underline-position-properties",
7383 x_use_underline_position_properties,
7384 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
7385 A value of nil means ignore them. If you encounter fonts with bogus
7386 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7387 to 4.1, set this to nil. You can also use `underline-minimum-offset'
7388 to override the font's UNDERLINE_POSITION for small font display
7389 sizes. */);
7390 x_use_underline_position_properties = 0;
7392 DEFVAR_BOOL ("x-underline-at-descent-line",
7393 x_underline_at_descent_line,
7394 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7395 A value of nil means to draw the underline according to the value of the
7396 variable `x-use-underline-position-properties', which is usually at the
7397 baseline level. The default value is nil. */);
7398 x_underline_at_descent_line = 0;
7400 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7401 doc: /* Which toolkit scroll bars Emacs uses, if any.
7402 A value of nil means Emacs doesn't use toolkit scroll bars.
7403 With the X Window system, the value is a symbol describing the
7404 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
7405 With MS Windows or Nextstep, the value is t. */);
7406 Vx_toolkit_scroll_bars = Qt;
7408 DEFVAR_BOOL ("w32-unicode-filenames",
7409 w32_unicode_filenames,
7410 doc: /* Non-nil means use Unicode APIs when passing file names to the OS.
7411 A value of nil means file names passed to the OS APIs and returned
7412 from those APIs are encoded/decoded using the ANSI codepage
7413 specified by `file-name-coding-system'.
7415 This variable is set to non-nil by default when Emacs runs on Windows
7416 systems of the NT family, including W2K, XP, Vista, Windows 7 and
7417 Windows 8. It is set to nil on Windows 9X. */);
7418 if (os_subtype == OS_9X)
7419 w32_unicode_filenames = 0;
7420 else
7421 w32_unicode_filenames = 1;
7424 /* FIXME: The following variable will be (hopefully) removed
7425 before Emacs 25.1 gets released. */
7427 DEFVAR_BOOL ("w32-add-wrapped-menu-bar-lines",
7428 w32_add_wrapped_menu_bar_lines,
7429 doc: /* Non-nil means frame resizing accounts for wrapped menu bar lines.
7430 A value of nil means frame resizing does not add the height of wrapped
7431 menu bar lines when sending a frame resize request to the Windows API.
7432 This usually means that the resulting frame height is off by the number
7433 of wrapped menu bar lines. If this is non-nil, Emacs adds the height of
7434 wrapped menu bar lines when sending frame resize requests to the Windows
7435 API. */);
7436 w32_add_wrapped_menu_bar_lines = 1;
7438 /* Tell Emacs about this window system. */
7439 Fprovide (Qw32, Qnil);