Support resizing frames and windows pixelwise.
[emacs.git] / src / w32term.c
blob38b297810b1035791fdc3ac33d983977d31f3bd4
1 /* Implementation of GUI terminal on the Microsoft Windows API.
3 Copyright (C) 1989, 1993-2013 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
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include "lisp.h"
24 #include "blockinput.h"
25 #include "w32term.h"
27 #include "systty.h"
28 #include "systime.h"
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <imm.h>
35 #include "charset.h"
36 #include "character.h"
37 #include "coding.h"
38 #include "ccl.h"
39 #include "frame.h"
40 #include "dispextern.h"
41 #include "fontset.h"
42 #include "termhooks.h"
43 #include "termopts.h"
44 #include "termchar.h"
45 #include "disptab.h"
46 #include "buffer.h"
47 #include "window.h"
48 #include "keyboard.h"
49 #include "intervals.h"
50 #include "process.h"
51 #include "atimer.h"
52 #include "keymap.h"
54 #ifdef WINDOWSNT
55 #include "w32heap.h"
56 #endif
58 #ifndef WINDOWSNT
59 #include <io.h> /* for get_osfhandle */
60 #endif
62 #include <shellapi.h>
64 #include "font.h"
65 #include "w32font.h"
67 /* Fringe bitmaps. */
69 static int max_fringe_bmp = 0;
70 static HBITMAP *fringe_bmp = 0;
72 /* Temporary variables for w32_read_socket. */
74 static int last_mousemove_x = 0;
75 static int last_mousemove_y = 0;
77 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
78 #ifndef GET_WHEEL_DELTA_WPARAM
79 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
80 #endif
82 /* Non-zero means that a HELP_EVENT has been generated since Emacs
83 start. */
85 static int any_help_event_p;
87 extern unsigned int msh_mousewheel;
89 extern void free_frame_menubar (struct frame *);
91 extern int w32_codepage_for_font (char *fontname);
92 extern Cursor w32_load_cursor (LPCTSTR name);
94 #define x_any_window_to_frame x_window_to_frame
95 #define x_top_window_to_frame x_window_to_frame
98 /* This is display since w32 does not support multiple ones. */
99 struct w32_display_info one_w32_display_info;
100 struct w32_display_info *x_display_list;
102 #if _WIN32_WINNT < 0x0500 && !defined(_W64)
103 /* Pre Windows 2000, this was not available, but define it here so
104 that Emacs compiled on such a platform will run on newer versions.
105 MinGW64 (_W64) defines these unconditionally, so avoid redefining. */
107 typedef struct tagWCRANGE
109 WCHAR wcLow;
110 USHORT cGlyphs;
111 } WCRANGE;
113 typedef struct tagGLYPHSET
115 DWORD cbThis;
116 DWORD flAccel;
117 DWORD cGlyphsSupported;
118 DWORD cRanges;
119 WCRANGE ranges[1];
120 } GLYPHSET;
122 #endif /* compiling for pre-Win2k */
124 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
125 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
127 #ifndef LWA_ALPHA
128 #define LWA_ALPHA 0x02
129 #endif
130 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
131 later targets by MSVC headers. */
132 #ifndef WS_EX_LAYERED
133 #define WS_EX_LAYERED 0x80000
134 #endif
136 /* SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN are not defined on 95 and
137 NT4. */
138 #ifndef SM_CXVIRTUALSCREEN
139 #define SM_CXVIRTUALSCREEN 78
140 #endif
141 #ifndef SM_CYVIRTUALSCREEN
142 #define SM_CYVIRTUALSCREEN 79
143 #endif
145 /* The handle of the frame that currently owns the system caret. */
146 HWND w32_system_caret_hwnd;
147 int w32_system_caret_height;
148 int w32_system_caret_x;
149 int w32_system_caret_y;
150 struct window *w32_system_caret_window;
151 int w32_system_caret_hdr_height;
152 int w32_system_caret_mode_height;
153 DWORD dwWindowsThreadId = 0;
154 HANDLE hWindowsThread = NULL;
155 DWORD dwMainThreadId = 0;
156 HANDLE hMainThread = NULL;
158 int vertical_scroll_bar_min_handle;
159 int vertical_scroll_bar_top_border;
160 int vertical_scroll_bar_bottom_border;
162 int last_scroll_bar_drag_pos;
164 /* Keyboard code page - may be changed by language-change events. */
165 int w32_keyboard_codepage;
167 /* Incremented by w32_read_socket whenever it really tries to read
168 events. */
169 static int volatile input_signal_count;
171 #ifdef CYGWIN
172 int w32_message_fd = -1;
173 #endif /* CYGWIN */
175 static void w32_handle_tool_bar_click (struct frame *,
176 struct input_event *);
177 static void w32_define_cursor (Window, Cursor);
179 void x_lower_frame (struct frame *);
180 void x_scroll_bar_clear (struct frame *);
181 void x_wm_set_size_hint (struct frame *, long, bool);
182 void x_raise_frame (struct frame *);
183 void x_set_window_size (struct frame *, int, int, int, bool);
184 void x_wm_set_window_state (struct frame *, int);
185 void x_wm_set_icon_pixmap (struct frame *, int);
186 static void w32_initialize (void);
187 static void x_update_end (struct frame *);
188 static void w32_frame_up_to_date (struct frame *);
189 static void x_clear_frame (struct frame *);
190 static void frame_highlight (struct frame *);
191 static void frame_unhighlight (struct frame *);
192 static void x_new_focus_frame (struct w32_display_info *,
193 struct frame *);
194 static void x_focus_changed (int, int, struct w32_display_info *,
195 struct frame *, struct input_event *);
196 static void w32_detect_focus_change (struct w32_display_info *,
197 W32Msg *, struct input_event *);
198 static void w32_frame_rehighlight (struct frame *);
199 static void x_frame_rehighlight (struct w32_display_info *);
200 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
201 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
202 enum text_cursor_kinds);
203 static void w32_clip_to_row (struct window *, struct glyph_row *,
204 enum glyph_row_area, HDC);
205 static BOOL my_show_window (struct frame *, HWND, int);
206 static void my_set_window_pos (HWND, HWND, int, int, int, int, UINT);
207 #if 0
208 static void my_set_focus (struct frame *, HWND);
209 #endif
210 static void my_set_foreground_window (HWND);
211 static void my_destroy_window (struct frame *, HWND);
212 static void w32fullscreen_hook (struct frame *);
214 #ifdef GLYPH_DEBUG
215 static void x_check_font (struct frame *, struct font *);
216 #endif
218 static Lisp_Object Qvendor_specific_keysyms;
219 static Lisp_Object Qadded, Qremoved, Qmodified;
220 static Lisp_Object Qrenamed_from, Qrenamed_to;
223 /***********************************************************************
224 Debugging
225 ***********************************************************************/
227 #if 0
229 /* This is a function useful for recording debugging information about
230 the sequence of occurrences in this file. */
232 struct record
234 char *locus;
235 int type;
238 struct record event_record[100];
240 int event_record_index;
242 record_event (char *locus, int type)
244 if (event_record_index == sizeof (event_record) / sizeof (struct record))
245 event_record_index = 0;
247 event_record[event_record_index].locus = locus;
248 event_record[event_record_index].type = type;
249 event_record_index++;
252 #endif /* 0 */
255 void
256 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
257 XGCValues *xgcv)
259 if (mask & GCForeground)
260 gc->foreground = xgcv->foreground;
261 if (mask & GCBackground)
262 gc->background = xgcv->background;
263 if (mask & GCFont)
264 gc->font = xgcv->font;
267 XGCValues *
268 XCreateGC (void *ignore, Window window, unsigned long mask, XGCValues *xgcv)
270 XGCValues *gc = xzalloc (sizeof (XGCValues));
272 XChangeGC (ignore, gc, mask, xgcv);
274 return gc;
277 void
278 XGetGCValues (void *ignore, XGCValues *gc,
279 unsigned long mask, XGCValues *xgcv)
281 XChangeGC (ignore, xgcv, mask, gc);
284 static void
285 w32_set_clip_rectangle (HDC hdc, RECT *rect)
287 if (rect)
289 HRGN clip_region = CreateRectRgnIndirect (rect);
290 SelectClipRgn (hdc, clip_region);
291 DeleteObject (clip_region);
293 else
294 SelectClipRgn (hdc, NULL);
297 /* Restore clipping rectangle in S */
298 static void
299 w32_restore_glyph_string_clip (struct glyph_string *s)
301 RECT *r = s->clip;
302 int n = s->num_clips;
304 if (n == 1)
305 w32_set_clip_rectangle (s->hdc, r);
306 else if (n > 1)
308 HRGN clip1 = CreateRectRgnIndirect (r);
309 HRGN clip2 = CreateRectRgnIndirect (r + 1);
310 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
311 SelectClipRgn (s->hdc, clip1);
312 DeleteObject (clip1);
313 DeleteObject (clip2);
318 Draw a wavy line under S. The wave fills wave_height pixels from y0.
320 x0 wave_length = 2
322 y0 * * * * *
323 |* * * * * * * * *
324 wave_height = 3 | * * * *
328 void
329 w32_draw_underwave (struct glyph_string *s, COLORREF color)
331 int wave_height = 3, wave_length = 2;
332 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
333 XRectangle wave_clip, string_clip, final_clip;
334 RECT w32_final_clip, w32_string_clip;
335 HPEN hp, oldhp;
337 dx = wave_length;
338 dy = wave_height - 1;
339 x0 = s->x;
340 y0 = s->ybase - wave_height + 3;
341 width = s->width;
342 xmax = x0 + width;
344 /* Find and set clipping rectangle */
346 wave_clip.x = x0;
347 wave_clip.y = y0;
348 wave_clip.width = width;
349 wave_clip.height = wave_height;
351 get_glyph_string_clip_rect (s, &w32_string_clip);
352 CONVERT_TO_XRECT (string_clip, w32_string_clip);
354 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
355 return;
357 hp = CreatePen (PS_SOLID, 0, color);
358 oldhp = SelectObject (s->hdc, hp);
359 CONVERT_FROM_XRECT (final_clip, w32_final_clip);
360 w32_set_clip_rectangle (s->hdc, &w32_final_clip);
362 /* Draw the waves */
364 x1 = x0 - (x0 % dx);
365 x2 = x1 + dx;
366 odd = (x1/dx) % 2;
367 y1 = y2 = y0;
369 if (odd)
370 y1 += dy;
371 else
372 y2 += dy;
374 MoveToEx (s->hdc, x1, y1, NULL);
376 while (x1 <= xmax)
378 LineTo (s->hdc, x2, y2);
379 x1 = x2, y1 = y2;
380 x2 += dx, y2 = y0 + odd*dy;
381 odd = !odd;
384 /* Restore previous pen and clipping rectangle(s) */
385 w32_restore_glyph_string_clip (s);
386 SelectObject (s->hdc, oldhp);
387 DeleteObject (hp);
390 /* Draw a hollow rectangle at the specified position. */
391 void
392 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
393 int width, int height)
395 HBRUSH hb, oldhb;
396 HPEN hp, oldhp;
398 hb = CreateSolidBrush (gc->background);
399 hp = CreatePen (PS_SOLID, 0, gc->foreground);
400 oldhb = SelectObject (hdc, hb);
401 oldhp = SelectObject (hdc, hp);
403 Rectangle (hdc, x, y, x + width, y + height);
405 SelectObject (hdc, oldhb);
406 SelectObject (hdc, oldhp);
407 DeleteObject (hb);
408 DeleteObject (hp);
411 /* Draw a filled rectangle at the specified position. */
412 void
413 w32_fill_rect (struct frame *f, HDC hdc, COLORREF pix, RECT *lprect)
415 HBRUSH hb;
417 hb = CreateSolidBrush (pix);
418 FillRect (hdc, lprect, hb);
419 DeleteObject (hb);
422 void
423 w32_clear_window (struct frame *f)
425 RECT rect;
426 HDC hdc = get_frame_dc (f);
428 /* Under certain conditions, this can be called at startup with
429 a console frame pointer before the GUI frame is created. An HDC
430 of 0 indicates this. */
431 if (hdc)
433 GetClientRect (FRAME_W32_WINDOW (f), &rect);
434 w32_clear_rect (f, hdc, &rect);
437 release_frame_dc (f, hdc);
440 #define OPAQUE_FRAME 255
442 void
443 x_set_frame_alpha (struct frame *f)
445 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
446 double alpha = 1.0;
447 double alpha_min = 1.0;
448 BYTE opac;
449 LONG ex_style;
450 HWND window = FRAME_W32_WINDOW (f);
452 /* Older versions of Windows do not support transparency. */
453 if (!pfnSetLayeredWindowAttributes)
454 return;
456 if (dpyinfo->x_highlight_frame == f)
457 alpha = f->alpha[0];
458 else
459 alpha = f->alpha[1];
461 if (FLOATP (Vframe_alpha_lower_limit))
462 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
463 else if (INTEGERP (Vframe_alpha_lower_limit))
464 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
466 if (alpha < 0.0)
467 return;
468 else if (alpha > 1.0)
469 alpha = 1.0;
470 else if (alpha < alpha_min && alpha_min <= 1.0)
471 alpha = alpha_min;
473 opac = alpha * OPAQUE_FRAME;
475 ex_style = GetWindowLong (window, GWL_EXSTYLE);
477 if (opac == OPAQUE_FRAME)
478 ex_style &= ~WS_EX_LAYERED;
479 else
480 ex_style |= WS_EX_LAYERED;
482 SetWindowLong (window, GWL_EXSTYLE, ex_style);
484 if (opac != OPAQUE_FRAME)
485 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
489 x_display_pixel_height (struct w32_display_info *dpyinfo)
491 int pixels = GetSystemMetrics (SM_CYVIRTUALSCREEN);
493 if (pixels == 0)
494 /* Fallback for Windows 95 or NT 4.0. */
495 pixels = GetSystemMetrics (SM_CYSCREEN);
497 return pixels;
501 x_display_pixel_width (struct w32_display_info *dpyinfo)
503 int pixels = GetSystemMetrics (SM_CXVIRTUALSCREEN);
505 if (pixels == 0)
506 /* Fallback for Windows 95 or NT 4.0. */
507 pixels = GetSystemMetrics (SM_CXSCREEN);
509 return pixels;
513 /***********************************************************************
514 Starting and ending an update
515 ***********************************************************************/
517 /* Start an update of frame F. This function is installed as a hook
518 for update_begin, i.e. it is called when update_begin is called.
519 This function is called prior to calls to x_update_window_begin for
520 each window being updated. */
522 static void
523 x_update_begin (struct frame *f)
525 struct w32_display_info *display_info = FRAME_DISPLAY_INFO (f);
527 if (! FRAME_W32_P (f))
528 return;
530 /* Regenerate display palette before drawing if list of requested
531 colors has changed. */
532 if (display_info->regen_palette)
534 w32_regenerate_palette (f);
535 display_info->regen_palette = FALSE;
540 /* Start update of window W. */
542 static void
543 x_update_window_begin (struct window *w)
545 struct frame *f = XFRAME (WINDOW_FRAME (w));
546 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
548 /* Hide the system caret during an update. */
549 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
551 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
554 w->output_cursor = w->cursor;
556 block_input ();
558 if (f == hlinfo->mouse_face_mouse_frame)
560 /* Don't do highlighting for mouse motion during the update. */
561 hlinfo->mouse_face_defer = 1;
563 /* If F needs to be redrawn, simply forget about any prior mouse
564 highlighting. */
565 if (FRAME_GARBAGED_P (f))
566 hlinfo->mouse_face_window = Qnil;
568 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
569 their mouse_face_p flag set, which means that they are always
570 unequal to rows in a desired matrix which never have that
571 flag set. So, rows containing mouse-face glyphs are never
572 scrolled, and we don't have to switch the mouse highlight off
573 here to prevent it from being scrolled. */
575 /* Can we tell that this update does not affect the window
576 where the mouse highlight is? If so, no need to turn off.
577 Likewise, don't do anything if the frame is garbaged;
578 in that case, the frame's current matrix that we would use
579 is all wrong, and we will redisplay that line anyway. */
580 if (!NILP (hlinfo->mouse_face_window)
581 && w == XWINDOW (hlinfo->mouse_face_window))
583 int i;
585 for (i = 0; i < w->desired_matrix->nrows; ++i)
586 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
587 break;
589 if (i < w->desired_matrix->nrows)
590 clear_mouse_face (hlinfo);
592 #endif /* 0 */
595 unblock_input ();
598 /* Draw a vertical window border from (x,y0) to (x,y1) */
600 static void
601 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
603 struct frame *f = XFRAME (WINDOW_FRAME (w));
604 RECT r;
605 HDC hdc;
606 struct face *face;
608 r.left = x;
609 r.right = x + 1;
610 r.top = y0;
611 r.bottom = y1;
613 hdc = get_frame_dc (f);
614 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
615 if (face)
616 w32_fill_rect (f, hdc, face->foreground, &r);
617 else
618 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
620 release_frame_dc (f, hdc);
624 /* Draw a window divider from (x0, y0) to (x1, y1) */
626 static void
627 w32_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
629 struct frame *f = XFRAME (WINDOW_FRAME (w));
630 RECT r;
631 HDC hdc;
632 struct face *face;
634 r.left = x0;
635 r.right = x1;
636 r.top = y0;
637 r.bottom = y1;
639 hdc = get_frame_dc (f);
640 face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
641 if (face)
642 w32_fill_rect (f, hdc, face->foreground, &r);
643 else
644 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
646 release_frame_dc (f, hdc);
650 /* End update of window W.
652 Draw vertical borders between horizontally adjacent windows, and
653 display W's cursor if CURSOR_ON_P is non-zero.
655 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
656 glyphs in mouse-face were overwritten. In that case we have to
657 make sure that the mouse-highlight is properly redrawn.
659 W may be a menu bar pseudo-window in case we don't have X toolkit
660 support. Such windows don't have a cursor, so don't display it
661 here. */
663 static void
664 x_update_window_end (struct window *w, bool cursor_on_p,
665 bool mouse_face_overwritten_p)
667 if (!w->pseudo_window_p)
669 block_input ();
671 if (cursor_on_p)
672 display_and_set_cursor (w, 1,
673 w->output_cursor.hpos, w->output_cursor.vpos,
674 w->output_cursor.x, w->output_cursor.y);
676 if (draw_window_fringes (w, 1))
678 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
679 x_draw_right_divider (w);
680 else
681 x_draw_vertical_border (w);
684 unblock_input ();
687 /* If a row with mouse-face was overwritten, arrange for
688 XTframe_up_to_date to redisplay the mouse highlight. */
689 if (mouse_face_overwritten_p)
691 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
693 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
694 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
695 hlinfo->mouse_face_window = Qnil;
698 /* Unhide the caret. This won't actually show the cursor, unless it
699 was visible before the corresponding call to HideCaret in
700 x_update_window_begin. */
701 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
703 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
708 /* End update of frame F. This function is installed as a hook in
709 update_end. */
711 static void
712 x_update_end (struct frame *f)
714 if (! FRAME_W32_P (f))
715 return;
717 /* Mouse highlight may be displayed again. */
718 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
722 /* This function is called from various places in xdisp.c
723 whenever a complete update has been performed. */
725 static void
726 w32_frame_up_to_date (struct frame *f)
728 if (FRAME_W32_P (f))
729 FRAME_MOUSE_UPDATE (f);
733 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
734 arrow bitmaps, or clear the fringes if no bitmaps are required
735 before DESIRED_ROW is made current. This function is called from
736 update_window_line only if it is known that there are differences
737 between bitmaps to be drawn between current row and DESIRED_ROW. */
739 static void
740 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
742 struct frame *f;
743 int width, height;
745 eassert (w);
747 if (!desired_row->mode_line_p && !w->pseudo_window_p)
748 desired_row->redraw_fringe_bitmaps_p = 1;
750 /* When a window has disappeared, make sure that no rest of
751 full-width rows stays visible in the internal border. Could
752 check here if updated window is the leftmost/rightmost window,
753 but I guess it's not worth doing since vertically split windows
754 are almost never used, internal border is rarely set, and the
755 overhead is very small. */
756 if (windows_or_buffers_changed
757 && desired_row->full_width_p
758 && (f = XFRAME (w->frame),
759 width = FRAME_INTERNAL_BORDER_WIDTH (f),
760 width != 0)
761 && (height = desired_row->visible_height,
762 height > 0))
764 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
766 block_input ();
768 HDC hdc = get_frame_dc (f);
769 w32_clear_area (f, hdc, 0, y, width, height);
770 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
771 y, width, height);
772 release_frame_dc (f, hdc);
774 unblock_input ();
779 /* Draw the bitmap WHICH in one of the left or right fringes of
780 window W. ROW is the glyph row for which to display the bitmap; it
781 determines the vertical position at which the bitmap has to be
782 drawn. */
784 static void
785 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
786 struct draw_fringe_bitmap_params *p)
788 struct frame *f = XFRAME (WINDOW_FRAME (w));
789 HDC hdc;
790 struct face *face = p->face;
792 hdc = get_frame_dc (f);
794 if (!p->overlay_p)
796 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
798 /* If the fringe is adjacent to the left (right) scroll bar of a
799 leftmost (rightmost, respectively) window, then extend its
800 background to the gap between the fringe and the bar. */
801 if ((WINDOW_LEFTMOST_P (w)
802 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
803 || (WINDOW_RIGHTMOST_P (w)
804 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
806 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
808 if (sb_width > 0)
810 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
811 int bar_area_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
813 if (bx < 0)
815 /* Bitmap fills the fringe. */
816 if (bar_area_x + bar_area_width == p->x)
817 bx = bar_area_x + sb_width;
818 else if (p->x + p->wd == bar_area_x)
819 bx = bar_area_x;
820 if (bx >= 0)
822 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
824 nx = bar_area_width - sb_width;
825 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
826 row->y));
827 ny = row->visible_height;
830 else
832 if (bar_area_x + bar_area_width == bx)
834 bx = bar_area_x + sb_width;
835 nx += bar_area_width - sb_width;
837 else if (bx + nx == bar_area_x)
838 nx += bar_area_width - sb_width;
843 if (bx >= 0 && nx > 0)
844 w32_fill_area (f, hdc, face->background, bx, by, nx, ny);
847 /* Must clip because of partially visible lines. */
848 w32_clip_to_row (w, row, ANY_AREA, hdc);
850 if (p->which && p->which < max_fringe_bmp)
852 HBITMAP pixmap = fringe_bmp[p->which];
853 HDC compat_hdc;
854 HANDLE horig_obj;
856 compat_hdc = CreateCompatibleDC (hdc);
858 SaveDC (hdc);
860 horig_obj = SelectObject (compat_hdc, pixmap);
862 /* Paint overlays transparently. */
863 if (p->overlay_p)
865 HBRUSH h_brush, h_orig_brush;
867 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
868 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
869 h_brush = CreateSolidBrush (face->foreground);
870 h_orig_brush = SelectObject (hdc, h_brush);
872 BitBlt (hdc, p->x, p->y, p->wd, p->h,
873 compat_hdc, 0, p->dh,
874 DSTINVERT);
875 BitBlt (hdc, p->x, p->y, p->wd, p->h,
876 compat_hdc, 0, p->dh,
877 0x2E064A);
878 BitBlt (hdc, p->x, p->y, p->wd, p->h,
879 compat_hdc, 0, p->dh,
880 DSTINVERT);
882 SelectObject (hdc, h_orig_brush);
883 DeleteObject (h_brush);
885 else
887 SetTextColor (hdc, face->background);
888 SetBkColor (hdc, (p->cursor_p
889 ? f->output_data.w32->cursor_pixel
890 : face->foreground));
892 BitBlt (hdc, p->x, p->y, p->wd, p->h,
893 compat_hdc, 0, p->dh,
894 SRCCOPY);
897 SelectObject (compat_hdc, horig_obj);
898 DeleteDC (compat_hdc);
899 RestoreDC (hdc, -1);
902 w32_set_clip_rectangle (hdc, NULL);
904 release_frame_dc (f, hdc);
907 static void
908 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
910 if (which >= max_fringe_bmp)
912 int i = max_fringe_bmp;
913 max_fringe_bmp = which + 20;
914 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
915 while (i < max_fringe_bmp)
916 fringe_bmp[i++] = 0;
919 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
922 static void
923 w32_destroy_fringe_bitmap (int which)
925 if (which >= max_fringe_bmp)
926 return;
928 if (fringe_bmp[which])
929 DeleteObject (fringe_bmp[which]);
930 fringe_bmp[which] = 0;
933 /***********************************************************************
934 Display Iterator
935 ***********************************************************************/
937 /* Function prototypes of this page. */
939 static void x_set_glyph_string_clipping (struct glyph_string *);
940 static void x_set_glyph_string_gc (struct glyph_string *);
941 static void x_draw_glyph_string_background (struct glyph_string *,
942 int);
943 static void x_draw_glyph_string_foreground (struct glyph_string *);
944 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
945 static void x_draw_glyph_string_box (struct glyph_string *);
946 static void x_draw_glyph_string (struct glyph_string *);
947 static void x_set_cursor_gc (struct glyph_string *);
948 static void x_set_mode_line_face_gc (struct glyph_string *);
949 static void x_set_mouse_face_gc (struct glyph_string *);
950 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
951 static void w32_setup_relief_color (struct frame *, struct relief *,
952 double, int, COLORREF);
953 static void x_setup_relief_colors (struct glyph_string *);
954 static void x_draw_image_glyph_string (struct glyph_string *);
955 static void x_draw_image_relief (struct glyph_string *);
956 static void x_draw_image_foreground (struct glyph_string *);
957 static void w32_draw_image_foreground_1 (struct glyph_string *, HBITMAP);
958 static void x_clear_glyph_string_rect (struct glyph_string *, int,
959 int, int, int);
960 static void w32_draw_relief_rect (struct frame *, int, int, int, int,
961 int, int, int, int, int, int,
962 RECT *);
963 static void w32_draw_box_rect (struct glyph_string *, int, int, int, int,
964 int, int, int, RECT *);
967 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
968 face. */
970 static void
971 x_set_cursor_gc (struct glyph_string *s)
973 if (s->font == FRAME_FONT (s->f)
974 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
975 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
976 && !s->cmp)
977 s->gc = s->f->output_data.w32->cursor_gc;
978 else
980 /* Cursor on non-default face: must merge. */
981 XGCValues xgcv;
982 unsigned long mask;
984 xgcv.background = s->f->output_data.w32->cursor_pixel;
985 xgcv.foreground = s->face->background;
987 /* If the glyph would be invisible, try a different foreground. */
988 if (xgcv.foreground == xgcv.background)
989 xgcv.foreground = s->face->foreground;
990 if (xgcv.foreground == xgcv.background)
991 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
992 if (xgcv.foreground == xgcv.background)
993 xgcv.foreground = s->face->foreground;
995 /* Make sure the cursor is distinct from text in this face. */
996 if (xgcv.background == s->face->background
997 && xgcv.foreground == s->face->foreground)
999 xgcv.background = s->face->foreground;
1000 xgcv.foreground = s->face->background;
1003 IF_DEBUG (x_check_font (s->f, s->font));
1004 xgcv.font = s->font;
1005 mask = GCForeground | GCBackground | GCFont;
1007 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1008 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1009 mask, &xgcv);
1010 else
1011 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1012 = XCreateGC (NULL, s->window, mask, &xgcv);
1014 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1019 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1021 static void
1022 x_set_mouse_face_gc (struct glyph_string *s)
1024 int face_id;
1025 struct face *face;
1027 /* What face has to be used last for the mouse face? */
1028 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1029 face = FACE_FROM_ID (s->f, face_id);
1030 if (face == NULL)
1031 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1033 if (s->first_glyph->type == CHAR_GLYPH)
1034 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1035 else
1036 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1037 s->face = FACE_FROM_ID (s->f, face_id);
1038 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1040 /* If font in this face is same as S->font, use it. */
1041 if (s->font == s->face->font)
1042 s->gc = s->face->gc;
1043 else
1045 /* Otherwise construct scratch_cursor_gc with values from FACE
1046 but font FONT. */
1047 XGCValues xgcv;
1048 unsigned long mask;
1050 xgcv.background = s->face->background;
1051 xgcv.foreground = s->face->foreground;
1052 IF_DEBUG (x_check_font (s->f, s->font));
1053 xgcv.font = s->font;
1054 mask = GCForeground | GCBackground | GCFont;
1056 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1057 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1058 mask, &xgcv);
1059 else
1060 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1061 = XCreateGC (NULL, s->window, mask, &xgcv);
1063 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1066 eassert (s->gc != 0);
1070 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1071 Faces to use in the mode line have already been computed when the
1072 matrix was built, so there isn't much to do, here. */
1074 static inline void
1075 x_set_mode_line_face_gc (struct glyph_string *s)
1077 s->gc = s->face->gc;
1081 /* Set S->gc of glyph string S for drawing that glyph string. Set
1082 S->stippled_p to a non-zero value if the face of S has a stipple
1083 pattern. */
1085 static inline void
1086 x_set_glyph_string_gc (struct glyph_string *s)
1088 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1090 if (s->hl == DRAW_NORMAL_TEXT)
1092 s->gc = s->face->gc;
1093 s->stippled_p = s->face->stipple != 0;
1095 else if (s->hl == DRAW_INVERSE_VIDEO)
1097 x_set_mode_line_face_gc (s);
1098 s->stippled_p = s->face->stipple != 0;
1100 else if (s->hl == DRAW_CURSOR)
1102 x_set_cursor_gc (s);
1103 s->stippled_p = 0;
1105 else if (s->hl == DRAW_MOUSE_FACE)
1107 x_set_mouse_face_gc (s);
1108 s->stippled_p = s->face->stipple != 0;
1110 else if (s->hl == DRAW_IMAGE_RAISED
1111 || s->hl == DRAW_IMAGE_SUNKEN)
1113 s->gc = s->face->gc;
1114 s->stippled_p = s->face->stipple != 0;
1116 else
1118 s->gc = s->face->gc;
1119 s->stippled_p = s->face->stipple != 0;
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, int 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, s->window, 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 = 1;
1252 else
1253 #endif
1254 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1255 || s->font_not_found_p
1256 || s->extends_to_end_of_line_p
1257 || force_p)
1259 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1260 s->background_width,
1261 s->height - 2 * box_line_width);
1262 s->background_filled_p = 1;
1268 /* Draw the foreground of glyph string S. */
1270 static void
1271 x_draw_glyph_string_foreground (struct glyph_string *s)
1273 int i, x;
1275 /* If first glyph of S has a left box line, start drawing the text
1276 of S to the right of that box line. */
1277 if (s->face->box != FACE_NO_BOX
1278 && s->first_glyph->left_box_line_p)
1279 x = s->x + eabs (s->face->box_line_width);
1280 else
1281 x = s->x;
1283 SetTextColor (s->hdc, s->gc->foreground);
1284 SetBkColor (s->hdc, s->gc->background);
1285 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1287 /* Draw characters of S as rectangles if S's font could not be
1288 loaded. */
1289 if (s->font_not_found_p)
1291 for (i = 0; i < s->nchars; ++i)
1293 struct glyph *g = s->first_glyph + i;
1295 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1296 s->height - 1);
1297 x += g->pixel_width;
1300 else
1302 struct font *font = s->font;
1303 int boff = font->baseline_offset;
1304 int y;
1305 HFONT old_font;
1307 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1309 if (font->vertical_centering)
1310 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1312 y = s->ybase - boff;
1313 if (s->for_overlaps
1314 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1315 font->driver->draw (s, 0, s->nchars, x, y, 0);
1316 else
1317 font->driver->draw (s, 0, s->nchars, x, y, 1);
1318 if (s->face->overstrike)
1319 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1321 SelectObject (s->hdc, old_font);
1325 /* Draw the foreground of composite glyph string S. */
1327 static void
1328 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1330 int i, j, x;
1331 struct font *font = s->font;
1333 /* If first glyph of S has a left box line, start drawing the text
1334 of S to the right of that box line. */
1335 if (s->face && s->face->box != FACE_NO_BOX
1336 && s->first_glyph->left_box_line_p)
1337 x = s->x + eabs (s->face->box_line_width);
1338 else
1339 x = s->x;
1341 /* S is a glyph string for a composition. S->cmp_from is the index
1342 of the first character drawn for glyphs of this composition.
1343 S->cmp_from == 0 means we are drawing the very first character of
1344 this composition. */
1346 SetTextColor (s->hdc, s->gc->foreground);
1347 SetBkColor (s->hdc, s->gc->background);
1348 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1350 /* Draw a rectangle for the composition if the font for the very
1351 first character of the composition could not be loaded. */
1352 if (s->font_not_found_p)
1354 if (s->cmp_from == 0)
1355 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1356 s->height - 1);
1358 else if (! s->first_glyph->u.cmp.automatic)
1360 int y = s->ybase;
1361 HFONT old_font;
1363 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1365 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1366 /* TAB in a composition means display glyphs with padding
1367 space on the left or right. */
1368 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1370 int xx = x + s->cmp->offsets[j * 2];
1371 int yy = y - s->cmp->offsets[j * 2 + 1];
1373 font->driver->draw (s, j, j + 1, xx, yy, 0);
1374 if (s->face->overstrike)
1375 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1377 SelectObject (s->hdc, old_font);
1379 else
1381 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1382 Lisp_Object glyph;
1383 int y = s->ybase;
1384 int width = 0;
1385 HFONT old_font;
1387 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1389 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1391 glyph = LGSTRING_GLYPH (gstring, i);
1392 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1393 width += LGLYPH_WIDTH (glyph);
1394 else
1396 int xoff, yoff, wadjust;
1398 if (j < i)
1400 font->driver->draw (s, j, i, x, y, 0);
1401 x += width;
1403 xoff = LGLYPH_XOFF (glyph);
1404 yoff = LGLYPH_YOFF (glyph);
1405 wadjust = LGLYPH_WADJUST (glyph);
1406 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1407 x += wadjust;
1408 j = i + 1;
1409 width = 0;
1412 if (j < i)
1413 font->driver->draw (s, j, i, x, y, 0);
1415 SelectObject (s->hdc, old_font);
1420 /* Draw the foreground of glyph string S for glyphless characters. */
1422 static void
1423 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1425 struct glyph *glyph = s->first_glyph;
1426 XChar2b char2b[8];
1427 int x, i, j;
1428 int with_background;
1430 /* If first glyph of S has a left box line, start drawing the text
1431 of S to the right of that box line. */
1432 if (s->face->box != FACE_NO_BOX
1433 && s->first_glyph->left_box_line_p)
1434 x = s->x + eabs (s->face->box_line_width);
1435 else
1436 x = s->x;
1438 SetTextColor (s->hdc, s->gc->foreground);
1439 SetBkColor (s->hdc, s->gc->background);
1440 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1442 s->char2b = char2b;
1443 with_background = ! (s->for_overlaps
1444 || (s->background_filled_p && s->hl != DRAW_CURSOR));
1445 for (i = 0; i < s->nchars; i++, glyph++)
1447 char buf[7], *str = NULL;
1448 int len = glyph->u.glyphless.len;
1450 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1452 if (len > 1
1453 && CHAR_TABLE_P (Vglyphless_char_display)
1454 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1455 >= 1))
1457 Lisp_Object acronym
1458 = (! glyph->u.glyphless.for_no_font
1459 ? CHAR_TABLE_REF (Vglyphless_char_display,
1460 glyph->u.glyphless.ch)
1461 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1462 if (STRINGP (acronym))
1463 str = SSDATA (acronym);
1466 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1468 sprintf ((char *) buf, "%0*X",
1469 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1470 glyph->u.glyphless.ch);
1471 str = buf;
1474 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1475 w32_draw_rectangle (s->hdc, s->gc,
1476 x, s->ybase - glyph->ascent,
1477 glyph->pixel_width - 1,
1478 glyph->ascent + glyph->descent - 1);
1479 if (str)
1481 struct font *font = s->font;
1482 int upper_len = (len + 1) / 2;
1483 unsigned code;
1484 HFONT old_font;
1486 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1487 /* It is certain that all LEN characters in STR are ASCII. */
1488 for (j = 0; j < len; j++)
1490 code = font->driver->encode_char (font, str[j]);
1491 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1493 font->driver->draw (s, 0, upper_len,
1494 x + glyph->slice.glyphless.upper_xoff,
1495 s->ybase + glyph->slice.glyphless.upper_yoff,
1496 with_background);
1497 font->driver->draw (s, upper_len, len,
1498 x + glyph->slice.glyphless.lower_xoff,
1499 s->ybase + glyph->slice.glyphless.lower_yoff,
1500 with_background);
1501 SelectObject (s->hdc, old_font);
1503 x += glyph->pixel_width;
1508 /* Brightness beyond which a color won't have its highlight brightness
1509 boosted.
1511 Nominally, highlight colors for `3d' faces are calculated by
1512 brightening an object's color by a constant scale factor, but this
1513 doesn't yield good results for dark colors, so for colors whose
1514 brightness is less than this value (on a scale of 0-255) have to
1515 use an additional additive factor.
1517 The value here is set so that the default menu-bar/mode-line color
1518 (grey75) will not have its highlights changed at all. */
1519 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1522 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1523 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1524 If this produces the same color as COLOR, try a color where all RGB
1525 values have DELTA added. Return the allocated color in *COLOR.
1526 DISPLAY is the X display, CMAP is the colormap to operate on.
1527 Value is non-zero if successful. */
1529 static int
1530 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1531 double factor, int delta)
1533 COLORREF new;
1534 long bright;
1536 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1537 delta /= 256;
1539 /* Change RGB values by specified FACTOR. Avoid overflow! */
1540 eassert (factor >= 0);
1541 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1542 min (0xff, factor * GetGValue (*color)),
1543 min (0xff, factor * GetBValue (*color)));
1545 /* Calculate brightness of COLOR. */
1546 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1547 + GetBValue (*color)) / 6;
1549 /* We only boost colors that are darker than
1550 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1551 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1552 /* Make an additive adjustment to NEW, because it's dark enough so
1553 that scaling by FACTOR alone isn't enough. */
1555 /* How far below the limit this color is (0 - 1, 1 being darker). */
1556 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1557 /* The additive adjustment. */
1558 int min_delta = delta * dimness * factor / 2;
1560 if (factor < 1)
1561 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1562 max (0, min (0xff, min_delta - GetGValue (*color))),
1563 max (0, min (0xff, min_delta - GetBValue (*color))));
1564 else
1565 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1566 max (0, min (0xff, min_delta + GetGValue (*color))),
1567 max (0, min (0xff, min_delta + GetBValue (*color))));
1570 if (new == *color)
1571 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1572 max (0, min (0xff, delta + GetGValue (*color))),
1573 max (0, min (0xff, delta + GetBValue (*color))));
1575 /* TODO: Map to palette and retry with delta if same? */
1576 /* TODO: Free colors (if using palette)? */
1578 if (new == *color)
1579 return 0;
1581 *color = new;
1583 return 1;
1586 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1587 colors in COLORS. On W32, we no longer try to map colors to
1588 a palette. */
1589 void
1590 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1592 int i;
1594 for (i = 0; i < ncolors; i++)
1596 DWORD pixel = colors[i].pixel;
1597 /* Convert to a 16 bit value in range 0 - 0xffff. */
1598 colors[i].red = GetRValue (pixel) * 257;
1599 colors[i].green = GetGValue (pixel) * 257;
1600 colors[i].blue = GetBValue (pixel) * 257;
1604 void
1605 x_query_color (struct frame *f, XColor *color)
1607 x_query_colors (f, color, 1);
1611 /* Set up the foreground color for drawing relief lines of glyph
1612 string S. RELIEF is a pointer to a struct relief containing the GC
1613 with which lines will be drawn. Use a color that is FACTOR or
1614 DELTA lighter or darker than the relief's background which is found
1615 in S->f->output_data.x->relief_background. If such a color cannot
1616 be allocated, use DEFAULT_PIXEL, instead. */
1618 static void
1619 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1620 int delta, COLORREF default_pixel)
1622 XGCValues xgcv;
1623 struct w32_output *di = f->output_data.w32;
1624 unsigned long mask = GCForeground;
1625 COLORREF pixel;
1626 COLORREF background = di->relief_background;
1627 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1629 /* TODO: Free colors (if using palette)? */
1631 /* Allocate new color. */
1632 xgcv.foreground = default_pixel;
1633 pixel = background;
1634 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1635 xgcv.foreground = relief->pixel = pixel;
1637 if (relief->gc == 0)
1639 #if 0 /* TODO: stipple */
1640 xgcv.stipple = dpyinfo->gray;
1641 mask |= GCStipple;
1642 #endif
1643 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1645 else
1646 XChangeGC (NULL, relief->gc, mask, &xgcv);
1650 /* Set up colors for the relief lines around glyph string S. */
1652 static void
1653 x_setup_relief_colors (struct glyph_string *s)
1655 struct w32_output *di = s->f->output_data.w32;
1656 COLORREF color;
1658 if (s->face->use_box_color_for_shadows_p)
1659 color = s->face->box_color;
1660 else if (s->first_glyph->type == IMAGE_GLYPH
1661 && s->img->pixmap
1662 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1663 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1664 else
1665 color = s->gc->background;
1667 if (di->white_relief.gc == 0
1668 || color != di->relief_background)
1670 di->relief_background = color;
1671 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1672 WHITE_PIX_DEFAULT (s->f));
1673 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1674 BLACK_PIX_DEFAULT (s->f));
1679 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1680 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1681 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1682 relief. LEFT_P non-zero means draw a relief on the left side of
1683 the rectangle. RIGHT_P non-zero means draw a relief on the right
1684 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1685 when drawing. */
1687 static void
1688 w32_draw_relief_rect (struct frame *f,
1689 int left_x, int top_y, int right_x, int bottom_y,
1690 int width, int raised_p,
1691 int top_p, int bot_p, int left_p, int right_p,
1692 RECT *clip_rect)
1694 int i;
1695 XGCValues gc;
1696 HDC hdc = get_frame_dc (f);
1698 if (raised_p)
1699 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1700 else
1701 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1703 w32_set_clip_rectangle (hdc, clip_rect);
1705 /* Top. */
1706 if (top_p)
1707 for (i = 0; i < width; ++i)
1708 w32_fill_area (f, hdc, gc.foreground,
1709 left_x + i * left_p, top_y + i,
1710 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1712 /* Left. */
1713 if (left_p)
1714 for (i = 0; i < width; ++i)
1715 w32_fill_area (f, hdc, gc.foreground,
1716 left_x + i, top_y + (i + 1) * top_p, 1,
1717 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1719 if (raised_p)
1720 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1721 else
1722 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1724 /* Bottom. */
1725 if (bot_p)
1726 for (i = 0; i < width; ++i)
1727 w32_fill_area (f, hdc, gc.foreground,
1728 left_x + i * left_p, bottom_y - i,
1729 right_x - left_x - i * (left_p + right_p) + 1, 1);
1731 /* Right. */
1732 if (right_p)
1733 for (i = 0; i < width; ++i)
1734 w32_fill_area (f, hdc, gc.foreground,
1735 right_x - i, top_y + (i + 1) * top_p, 1,
1736 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1738 w32_set_clip_rectangle (hdc, NULL);
1740 release_frame_dc (f, hdc);
1744 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1745 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1746 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1747 left side of the rectangle. RIGHT_P non-zero means draw a line
1748 on the right side of the rectangle. CLIP_RECT is the clipping
1749 rectangle to use when drawing. */
1751 static void
1752 w32_draw_box_rect (struct glyph_string *s,
1753 int left_x, int top_y, int right_x, int bottom_y, int width,
1754 int left_p, int right_p, RECT *clip_rect)
1756 w32_set_clip_rectangle (s->hdc, clip_rect);
1758 /* Top. */
1759 w32_fill_area (s->f, s->hdc, s->face->box_color,
1760 left_x, top_y, right_x - left_x + 1, width);
1762 /* Left. */
1763 if (left_p)
1765 w32_fill_area (s->f, s->hdc, s->face->box_color,
1766 left_x, top_y, width, bottom_y - top_y + 1);
1769 /* Bottom. */
1770 w32_fill_area (s->f, s->hdc, s->face->box_color,
1771 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1773 /* Right. */
1774 if (right_p)
1776 w32_fill_area (s->f, s->hdc, s->face->box_color,
1777 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1780 w32_set_clip_rectangle (s->hdc, NULL);
1784 /* Draw a box around glyph string S. */
1786 static void
1787 x_draw_glyph_string_box (struct glyph_string *s)
1789 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1790 int left_p, right_p;
1791 struct glyph *last_glyph;
1792 RECT clip_rect;
1794 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1795 ? WINDOW_RIGHT_EDGE_X (s->w)
1796 : window_box_right (s->w, s->area));
1798 /* The glyph that may have a right box line. */
1799 last_glyph = (s->cmp || s->img
1800 ? s->first_glyph
1801 : s->first_glyph + s->nchars - 1);
1803 width = eabs (s->face->box_line_width);
1804 raised_p = s->face->box == FACE_RAISED_BOX;
1805 left_x = s->x;
1806 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1807 ? last_x - 1
1808 : min (last_x, s->x + s->background_width) - 1));
1809 top_y = s->y;
1810 bottom_y = top_y + s->height - 1;
1812 left_p = (s->first_glyph->left_box_line_p
1813 || (s->hl == DRAW_MOUSE_FACE
1814 && (s->prev == NULL
1815 || s->prev->hl != s->hl)));
1816 right_p = (last_glyph->right_box_line_p
1817 || (s->hl == DRAW_MOUSE_FACE
1818 && (s->next == NULL
1819 || s->next->hl != s->hl)));
1821 get_glyph_string_clip_rect (s, &clip_rect);
1823 if (s->face->box == FACE_SIMPLE_BOX)
1824 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1825 left_p, right_p, &clip_rect);
1826 else
1828 x_setup_relief_colors (s);
1829 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1830 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1835 /* Draw foreground of image glyph string S. */
1837 static void
1838 x_draw_image_foreground (struct glyph_string *s)
1840 int x = s->x;
1841 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1843 /* If first glyph of S has a left box line, start drawing it to the
1844 right of that line. */
1845 if (s->face->box != FACE_NO_BOX
1846 && s->first_glyph->left_box_line_p
1847 && s->slice.x == 0)
1848 x += eabs (s->face->box_line_width);
1850 /* If there is a margin around the image, adjust x- and y-position
1851 by that margin. */
1852 if (s->slice.x == 0)
1853 x += s->img->hmargin;
1854 if (s->slice.y == 0)
1855 y += s->img->vmargin;
1857 SaveDC (s->hdc);
1859 if (s->img->pixmap)
1861 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1862 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1863 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1864 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1865 SetBkColor (compat_hdc, RGB (255, 255, 255));
1866 SetTextColor (s->hdc, RGB (0, 0, 0));
1867 x_set_glyph_string_clipping (s);
1869 if (s->img->mask)
1871 HDC mask_dc = CreateCompatibleDC (s->hdc);
1872 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1874 SetTextColor (s->hdc, RGB (255, 255, 255));
1875 SetBkColor (s->hdc, RGB (0, 0, 0));
1877 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1878 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1879 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1880 mask_dc, s->slice.x, s->slice.y, SRCAND);
1881 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1882 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1884 SelectObject (mask_dc, mask_orig_obj);
1885 DeleteDC (mask_dc);
1887 else
1889 SetTextColor (s->hdc, s->gc->foreground);
1890 SetBkColor (s->hdc, s->gc->background);
1892 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1893 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1895 /* When the image has a mask, we can expect that at
1896 least part of a mouse highlight or a block cursor will
1897 be visible. If the image doesn't have a mask, make
1898 a block cursor visible by drawing a rectangle around
1899 the image. I believe it's looking better if we do
1900 nothing here for mouse-face. */
1901 if (s->hl == DRAW_CURSOR)
1903 int r = s->img->relief;
1904 if (r < 0) r = -r;
1905 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1906 s->slice.width + r*2 - 1,
1907 s->slice.height + r*2 - 1);
1911 w32_set_clip_rectangle (s->hdc, NULL);
1912 SelectObject (s->hdc, orig_brush);
1913 DeleteObject (fg_brush);
1914 SelectObject (compat_hdc, orig_obj);
1915 DeleteDC (compat_hdc);
1917 else
1918 w32_draw_rectangle (s->hdc, s->gc, x, y,
1919 s->slice.width - 1, s->slice.height - 1);
1921 RestoreDC (s->hdc ,-1);
1925 /* Draw a relief around the image glyph string S. */
1927 static void
1928 x_draw_image_relief (struct glyph_string *s)
1930 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
1931 RECT r;
1932 int x = s->x;
1933 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1935 /* If first glyph of S has a left box line, start drawing it to the
1936 right of that line. */
1937 if (s->face->box != FACE_NO_BOX
1938 && s->first_glyph->left_box_line_p
1939 && s->slice.x == 0)
1940 x += eabs (s->face->box_line_width);
1942 /* If there is a margin around the image, adjust x- and y-position
1943 by that margin. */
1944 if (s->slice.x == 0)
1945 x += s->img->hmargin;
1946 if (s->slice.y == 0)
1947 y += s->img->vmargin;
1949 if (s->hl == DRAW_IMAGE_SUNKEN
1950 || s->hl == DRAW_IMAGE_RAISED)
1952 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief
1953 : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1954 raised_p = s->hl == DRAW_IMAGE_RAISED;
1956 else
1958 thick = eabs (s->img->relief);
1959 raised_p = s->img->relief > 0;
1962 x1 = x + s->slice.width - 1;
1963 y1 = y + s->slice.height - 1;
1964 top_p = bot_p = left_p = right_p = 0;
1966 if (s->slice.x == 0)
1967 x -= thick, left_p = 1;
1968 if (s->slice.y == 0)
1969 y -= thick, top_p = 1;
1970 if (s->slice.x + s->slice.width == s->img->width)
1971 x1 += thick, right_p = 1;
1972 if (s->slice.y + s->slice.height == s->img->height)
1973 y1 += thick, bot_p = 1;
1975 x_setup_relief_colors (s);
1976 get_glyph_string_clip_rect (s, &r);
1977 w32_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
1978 top_p, bot_p, left_p, right_p, &r);
1982 /* Draw the foreground of image glyph string S to PIXMAP. */
1984 static void
1985 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
1987 HDC hdc = CreateCompatibleDC (s->hdc);
1988 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1989 int x = 0;
1990 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1992 /* If first glyph of S has a left box line, start drawing it to the
1993 right of that line. */
1994 if (s->face->box != FACE_NO_BOX
1995 && s->first_glyph->left_box_line_p
1996 && s->slice.x == 0)
1997 x += eabs (s->face->box_line_width);
1999 /* If there is a margin around the image, adjust x- and y-position
2000 by that margin. */
2001 if (s->slice.x == 0)
2002 x += s->img->hmargin;
2003 if (s->slice.y == 0)
2004 y += s->img->vmargin;
2006 if (s->img->pixmap)
2008 HDC compat_hdc = CreateCompatibleDC (hdc);
2009 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2010 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
2011 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
2013 if (s->img->mask)
2015 HDC mask_dc = CreateCompatibleDC (hdc);
2016 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
2018 SetTextColor (hdc, RGB (0, 0, 0));
2019 SetBkColor (hdc, RGB (255, 255, 255));
2020 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2021 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2022 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2023 mask_dc, s->slice.x, s->slice.y, SRCAND);
2024 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2025 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2027 SelectObject (mask_dc, mask_orig_obj);
2028 DeleteDC (mask_dc);
2030 else
2032 SetTextColor (hdc, s->gc->foreground);
2033 SetBkColor (hdc, s->gc->background);
2035 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2036 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2038 /* When the image has a mask, we can expect that at
2039 least part of a mouse highlight or a block cursor will
2040 be visible. If the image doesn't have a mask, make
2041 a block cursor visible by drawing a rectangle around
2042 the image. I believe it's looking better if we do
2043 nothing here for mouse-face. */
2044 if (s->hl == DRAW_CURSOR)
2046 int r = s->img->relief;
2047 if (r < 0) r = -r;
2048 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2049 s->slice.width + r*2 - 1,
2050 s->slice.height + r*2 - 1);
2054 SelectObject (hdc, orig_brush);
2055 DeleteObject (fg_brush);
2056 SelectObject (compat_hdc, orig_obj);
2057 DeleteDC (compat_hdc);
2059 else
2060 w32_draw_rectangle (hdc, s->gc, x, y,
2061 s->slice.width - 1, s->slice.height - 1);
2063 SelectObject (hdc, orig_hdc_obj);
2064 DeleteDC (hdc);
2068 /* Draw part of the background of glyph string S. X, Y, W, and H
2069 give the rectangle to draw. */
2071 static void
2072 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2074 #if 0 /* TODO: stipple */
2075 if (s->stippled_p)
2077 /* Fill background with a stipple pattern. */
2078 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2079 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2080 XSetFillStyle (s->display, s->gc, FillSolid);
2082 else
2083 #endif
2084 x_clear_glyph_string_rect (s, x, y, w, h);
2088 /* Draw image glyph string S.
2090 s->y
2091 s->x +-------------------------
2092 | s->face->box
2094 | +-------------------------
2095 | | s->img->vmargin
2097 | | +-------------------
2098 | | | the image
2102 static void
2103 x_draw_image_glyph_string (struct glyph_string *s)
2105 int x, y;
2106 int box_line_hwidth = eabs (s->face->box_line_width);
2107 int box_line_vwidth = max (s->face->box_line_width, 0);
2108 int height;
2109 HBITMAP pixmap = 0;
2111 height = s->height - 2 * box_line_vwidth;
2113 /* Fill background with face under the image. Do it only if row is
2114 taller than image or if image has a clip mask to reduce
2115 flickering. */
2116 s->stippled_p = s->face->stipple != 0;
2117 if (height > s->slice.height
2118 || s->img->hmargin
2119 || s->img->vmargin
2120 || s->img->mask
2121 || s->img->pixmap == 0
2122 || s->width != s->background_width)
2124 x = s->x;
2125 if (s->first_glyph->left_box_line_p
2126 && s->slice.x == 0)
2127 x += box_line_hwidth;
2129 y = s->y;
2130 if (s->slice.y == 0)
2131 y += box_line_vwidth;
2133 #if 0 /* TODO: figure out if we need to do this on Windows. */
2134 if (s->img->mask)
2136 /* Create a pixmap as large as the glyph string. Fill it
2137 with the background color. Copy the image to it, using
2138 its mask. Copy the temporary pixmap to the display. */
2139 Screen *screen = FRAME_X_SCREEN (s->f);
2140 int depth = DefaultDepthOfScreen (screen);
2142 /* Create a pixmap as large as the glyph string. */
2143 pixmap = XCreatePixmap (s->display, s->window,
2144 s->background_width,
2145 s->height, depth);
2147 /* Don't clip in the following because we're working on the
2148 pixmap. */
2149 XSetClipMask (s->display, s->gc, None);
2151 /* Fill the pixmap with the background color/stipple. */
2152 if (s->stippled_p)
2154 /* Fill background with a stipple pattern. */
2155 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2156 XFillRectangle (s->display, pixmap, s->gc,
2157 0, 0, s->background_width, s->height);
2158 XSetFillStyle (s->display, s->gc, FillSolid);
2160 else
2162 XGCValues xgcv;
2163 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2164 &xgcv);
2165 XSetForeground (s->display, s->gc, xgcv.background);
2166 XFillRectangle (s->display, pixmap, s->gc,
2167 0, 0, s->background_width, s->height);
2168 XSetForeground (s->display, s->gc, xgcv.foreground);
2171 else
2172 #endif
2173 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2175 s->background_filled_p = 1;
2178 /* Draw the foreground. */
2179 if (pixmap != 0)
2181 w32_draw_image_foreground_1 (s, pixmap);
2182 x_set_glyph_string_clipping (s);
2184 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2185 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2186 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2187 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2189 SetTextColor (s->hdc, s->gc->foreground);
2190 SetBkColor (s->hdc, s->gc->background);
2191 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2192 compat_hdc, 0, 0, SRCCOPY);
2194 SelectObject (s->hdc, orig_brush);
2195 DeleteObject (fg_brush);
2196 SelectObject (compat_hdc, orig_obj);
2197 DeleteDC (compat_hdc);
2199 DeleteObject (pixmap);
2200 pixmap = 0;
2202 else
2203 x_draw_image_foreground (s);
2205 /* If we must draw a relief around the image, do it. */
2206 if (s->img->relief
2207 || s->hl == DRAW_IMAGE_RAISED
2208 || s->hl == DRAW_IMAGE_SUNKEN)
2209 x_draw_image_relief (s);
2213 /* Draw stretch glyph string S. */
2215 static void
2216 x_draw_stretch_glyph_string (struct glyph_string *s)
2218 eassert (s->first_glyph->type == STRETCH_GLYPH);
2220 if (s->hl == DRAW_CURSOR
2221 && !x_stretch_cursor_p)
2223 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2224 wide as the stretch glyph. */
2225 int width, background_width = s->background_width;
2226 int x = s->x;
2228 if (!s->row->reversed_p)
2230 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2232 if (x < left_x)
2234 background_width -= left_x - x;
2235 x = left_x;
2238 else
2240 /* In R2L rows, draw the cursor on the right edge of the
2241 stretch glyph. */
2242 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2244 if (x + background_width > right_x)
2245 background_width -= x - right_x;
2246 x += background_width;
2248 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2249 if (s->row->reversed_p)
2250 x -= width;
2252 /* Draw cursor. */
2253 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2255 /* Clear rest using the GC of the original non-cursor face. */
2256 if (width < background_width)
2258 XGCValues *gc = s->face->gc;
2259 int y = s->y;
2260 int w = background_width - width, h = s->height;
2261 RECT r;
2262 HDC hdc = s->hdc;
2264 if (!s->row->reversed_p)
2265 x += width;
2266 else
2267 x = s->x;
2268 if (s->row->mouse_face_p
2269 && cursor_in_mouse_face_p (s->w))
2271 x_set_mouse_face_gc (s);
2272 gc = s->gc;
2274 else
2275 gc = s->face->gc;
2277 get_glyph_string_clip_rect (s, &r);
2278 w32_set_clip_rectangle (hdc, &r);
2280 #if 0 /* TODO: stipple */
2281 if (s->face->stipple)
2283 /* Fill background with a stipple pattern. */
2284 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2285 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2286 XSetFillStyle (s->display, gc, FillSolid);
2288 else
2289 #endif
2291 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2295 else if (!s->background_filled_p)
2297 int background_width = s->background_width;
2298 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2300 /* Don't draw into left margin, fringe or scrollbar area
2301 except for header line and mode line. */
2302 if (x < left_x && !s->row->mode_line_p)
2304 background_width -= left_x - x;
2305 x = left_x;
2307 if (background_width > 0)
2308 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2311 s->background_filled_p = 1;
2315 /* Draw glyph string S. */
2317 static void
2318 x_draw_glyph_string (struct glyph_string *s)
2320 bool relief_drawn_p = 0;
2322 /* If S draws into the background of its successor, draw the
2323 background of the successor first so that S can draw into it.
2324 This makes S->next use XDrawString instead of XDrawImageString. */
2325 if (s->next && s->right_overhang && !s->for_overlaps)
2327 int width;
2328 struct glyph_string *next;
2329 for (width = 0, next = s->next;
2330 next && width < s->right_overhang;
2331 width += next->width, next = next->next)
2332 if (next->first_glyph->type != IMAGE_GLYPH)
2334 x_set_glyph_string_gc (next);
2335 x_set_glyph_string_clipping (next);
2336 if (next->first_glyph->type == STRETCH_GLYPH)
2337 x_draw_stretch_glyph_string (next);
2338 else
2339 x_draw_glyph_string_background (next, 1);
2340 next->num_clips = 0;
2344 /* Set up S->gc, set clipping and draw S. */
2345 x_set_glyph_string_gc (s);
2347 /* Draw relief (if any) in advance for char/composition so that the
2348 glyph string can be drawn over it. */
2349 if (!s->for_overlaps
2350 && s->face->box != FACE_NO_BOX
2351 && (s->first_glyph->type == CHAR_GLYPH
2352 || s->first_glyph->type == COMPOSITE_GLYPH))
2355 x_set_glyph_string_clipping (s);
2356 x_draw_glyph_string_background (s, 1);
2357 x_draw_glyph_string_box (s);
2358 x_set_glyph_string_clipping (s);
2359 relief_drawn_p = 1;
2361 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2362 && !s->clip_tail
2363 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2364 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2365 /* We must clip just this glyph. left_overhang part has already
2366 drawn when s->prev was drawn, and right_overhang part will be
2367 drawn later when s->next is drawn. */
2368 x_set_glyph_string_clipping_exactly (s, s);
2369 else
2370 x_set_glyph_string_clipping (s);
2372 switch (s->first_glyph->type)
2374 case IMAGE_GLYPH:
2375 x_draw_image_glyph_string (s);
2376 break;
2378 case STRETCH_GLYPH:
2379 x_draw_stretch_glyph_string (s);
2380 break;
2382 case CHAR_GLYPH:
2383 if (s->for_overlaps)
2384 s->background_filled_p = 1;
2385 else
2386 x_draw_glyph_string_background (s, 0);
2387 x_draw_glyph_string_foreground (s);
2388 break;
2390 case COMPOSITE_GLYPH:
2391 if (s->for_overlaps || (s->cmp_from > 0
2392 && ! s->first_glyph->u.cmp.automatic))
2393 s->background_filled_p = 1;
2394 else
2395 x_draw_glyph_string_background (s, 1);
2396 x_draw_composite_glyph_string_foreground (s);
2397 break;
2399 case GLYPHLESS_GLYPH:
2400 if (s->for_overlaps)
2401 s->background_filled_p = 1;
2402 else
2403 x_draw_glyph_string_background (s, 0);
2404 x_draw_glyphless_glyph_string_foreground (s);
2405 break;
2407 default:
2408 emacs_abort ();
2411 if (!s->for_overlaps)
2413 /* Draw underline. */
2414 if (s->face->underline_p)
2416 if (s->face->underline_type == FACE_UNDER_WAVE)
2418 COLORREF color;
2420 if (s->face->underline_defaulted_p)
2421 color = s->gc->foreground;
2422 else
2423 color = s->face->underline_color;
2425 w32_draw_underwave (s, color);
2427 else if (s->face->underline_type == FACE_UNDER_LINE)
2429 unsigned long thickness, position;
2430 int y;
2432 if (s->prev && s->prev->face->underline_p
2433 && s->prev->face->underline_type == FACE_UNDER_LINE)
2435 /* We use the same underline style as the previous one. */
2436 thickness = s->prev->underline_thickness;
2437 position = s->prev->underline_position;
2439 else
2441 /* Get the underline thickness. Default is 1 pixel. */
2442 if (s->font && s->font->underline_thickness > 0)
2443 thickness = s->font->underline_thickness;
2444 else
2445 thickness = 1;
2446 if (x_underline_at_descent_line)
2447 position = (s->height - thickness) - (s->ybase - s->y);
2448 else
2450 /* Get the underline position. This is the recommended
2451 vertical offset in pixels from the baseline to the top of
2452 the underline. This is a signed value according to the
2453 specs, and its default is
2455 ROUND ((maximum_descent) / 2), with
2456 ROUND (x) = floor (x + 0.5) */
2458 if (x_use_underline_position_properties
2459 && s->font && s->font->underline_position >= 0)
2460 position = s->font->underline_position;
2461 else if (s->font)
2462 position = (s->font->descent + 1) / 2;
2464 position = max (position, underline_minimum_offset);
2466 /* Check the sanity of thickness and position. We should
2467 avoid drawing underline out of the current line area. */
2468 if (s->y + s->height <= s->ybase + position)
2469 position = (s->height - 1) - (s->ybase - s->y);
2470 if (s->y + s->height < s->ybase + position + thickness)
2471 thickness = (s->y + s->height) - (s->ybase + position);
2472 s->underline_thickness = thickness;
2473 s->underline_position =position;
2474 y = s->ybase + position;
2475 if (s->face->underline_defaulted_p)
2477 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2478 y, s->width, 1);
2480 else
2482 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2483 y, s->width, 1);
2487 /* Draw overline. */
2488 if (s->face->overline_p)
2490 unsigned long dy = 0, h = 1;
2492 if (s->face->overline_color_defaulted_p)
2494 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2495 s->y + dy, s->width, h);
2497 else
2499 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2500 s->y + dy, s->width, h);
2504 /* Draw strike-through. */
2505 if (s->face->strike_through_p
2506 && !FONT_TEXTMETRIC (s->font).tmStruckOut)
2508 unsigned long h = 1;
2509 unsigned long dy = (s->height - h) / 2;
2511 if (s->face->strike_through_color_defaulted_p)
2513 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2514 s->width, h);
2516 else
2518 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2519 s->y + dy, s->width, h);
2523 /* Draw relief if not yet drawn. */
2524 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2525 x_draw_glyph_string_box (s);
2527 if (s->prev)
2529 struct glyph_string *prev;
2531 for (prev = s->prev; prev; prev = prev->prev)
2532 if (prev->hl != s->hl
2533 && prev->x + prev->width + prev->right_overhang > s->x)
2535 /* As prev was drawn while clipped to its own area, we
2536 must draw the right_overhang part using s->hl now. */
2537 enum draw_glyphs_face save = prev->hl;
2539 prev->hl = s->hl;
2540 x_set_glyph_string_gc (prev);
2541 x_set_glyph_string_clipping_exactly (s, prev);
2542 if (prev->first_glyph->type == CHAR_GLYPH)
2543 x_draw_glyph_string_foreground (prev);
2544 else
2545 x_draw_composite_glyph_string_foreground (prev);
2546 w32_set_clip_rectangle (prev->hdc, NULL);
2547 prev->hl = save;
2548 prev->num_clips = 0;
2552 if (s->next)
2554 struct glyph_string *next;
2556 for (next = s->next; next; next = next->next)
2557 if (next->hl != s->hl
2558 && next->x - next->left_overhang < s->x + s->width)
2560 /* As next will be drawn while clipped to its own area,
2561 we must draw the left_overhang part using s->hl now. */
2562 enum draw_glyphs_face save = next->hl;
2564 next->hl = s->hl;
2565 x_set_glyph_string_gc (next);
2566 x_set_glyph_string_clipping_exactly (s, next);
2567 if (next->first_glyph->type == CHAR_GLYPH)
2568 x_draw_glyph_string_foreground (next);
2569 else
2570 x_draw_composite_glyph_string_foreground (next);
2571 w32_set_clip_rectangle (next->hdc, NULL);
2572 next->hl = save;
2573 next->num_clips = 0;
2574 next->clip_head = s->next;
2579 /* Reset clipping. */
2580 w32_set_clip_rectangle (s->hdc, NULL);
2581 s->num_clips = 0;
2585 /* Shift display to make room for inserted glyphs. */
2587 void
2588 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2589 int width, int height, int shift_by)
2591 HDC hdc;
2593 hdc = get_frame_dc (f);
2594 BitBlt (hdc, x + shift_by, y, width, height,
2595 hdc, x, y, SRCCOPY);
2597 release_frame_dc (f, hdc);
2601 /* Delete N glyphs at the nominal cursor position. Not implemented
2602 for X frames. */
2604 static void
2605 x_delete_glyphs (struct frame *f, register int n)
2607 if (! FRAME_W32_P (f))
2608 return;
2610 emacs_abort ();
2614 /* Clear entire frame. */
2616 static void
2617 x_clear_frame (struct frame *f)
2619 if (! FRAME_W32_P (f))
2620 return;
2622 /* Clearing the frame will erase any cursor, so mark them all as no
2623 longer visible. */
2624 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2626 block_input ();
2628 w32_clear_window (f);
2630 /* We have to clear the scroll bars, too. If we have changed
2631 colors or something like that, then they should be notified. */
2632 x_scroll_bar_clear (f);
2634 unblock_input ();
2638 /* Make audible bell. */
2640 static void
2641 w32_ring_bell (struct frame *f)
2643 block_input ();
2645 if (FRAME_W32_P (f) && visible_bell)
2647 int i;
2648 HWND hwnd = FRAME_W32_WINDOW (f);
2650 for (i = 0; i < 5; i++)
2652 FlashWindow (hwnd, TRUE);
2653 Sleep (10);
2655 FlashWindow (hwnd, FALSE);
2657 else
2658 w32_sys_ring_bell (f);
2660 unblock_input ();
2663 /***********************************************************************
2664 Line Dance
2665 ***********************************************************************/
2667 /* Perform an insert-lines or delete-lines operation, inserting N
2668 lines or deleting -N lines at vertical position VPOS. */
2670 static void
2671 x_ins_del_lines (struct frame *f, int vpos, int n)
2673 if (! FRAME_W32_P (f))
2674 return;
2676 emacs_abort ();
2680 /* Scroll part of the display as described by RUN. */
2682 static void
2683 x_scroll_run (struct window *w, struct run *run)
2685 struct frame *f = XFRAME (w->frame);
2686 int x, y, width, height, from_y, to_y, bottom_y;
2687 HWND hwnd = FRAME_W32_WINDOW (f);
2688 HRGN expect_dirty;
2690 /* Get frame-relative bounding box of the text display area of W,
2691 without mode lines. Include in this box the left and right
2692 fringes of W. */
2693 window_box (w, ANY_AREA, &x, &y, &width, &height);
2695 /* If the fringe is adjacent to the left (right) scroll bar of a
2696 leftmost (rightmost, respectively) window, then extend its
2697 background to the gap between the fringe and the bar. */
2698 if ((WINDOW_LEFTMOST_P (w)
2699 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
2700 || (WINDOW_RIGHTMOST_P (w)
2701 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
2703 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
2705 if (sb_width > 0)
2707 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
2708 int bar_area_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
2710 if (bar_area_x + bar_area_width == x)
2712 x = bar_area_x + sb_width;
2713 width += bar_area_width - sb_width;
2715 else if (x + width == bar_area_x)
2716 width += bar_area_width - sb_width;
2720 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2721 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2722 bottom_y = y + height;
2724 if (to_y < from_y)
2726 /* Scrolling up. Make sure we don't copy part of the mode
2727 line at the bottom. */
2728 if (from_y + run->height > bottom_y)
2729 height = bottom_y - from_y;
2730 else
2731 height = run->height;
2732 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2734 else
2736 /* Scrolling down. Make sure we don't copy over the mode line.
2737 at the bottom. */
2738 if (to_y + run->height > bottom_y)
2739 height = bottom_y - to_y;
2740 else
2741 height = run->height;
2742 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2745 block_input ();
2747 /* Cursor off. Will be switched on again in x_update_window_end. */
2748 x_clear_cursor (w);
2751 RECT from;
2752 RECT to;
2753 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2754 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2756 from.left = to.left = x;
2757 from.right = to.right = x + width;
2758 from.top = from_y;
2759 from.bottom = from_y + height;
2760 to.top = y;
2761 to.bottom = bottom_y;
2763 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2764 NULL, SW_INVALIDATE);
2766 /* Combine this with what we expect to be dirty. This covers the
2767 case where not all of the region we expect is actually dirty. */
2768 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2770 /* If the dirty region is not what we expected, redraw the entire frame. */
2771 if (!EqualRgn (combined, expect_dirty))
2772 SET_FRAME_GARBAGED (f);
2774 DeleteObject (dirty);
2775 DeleteObject (combined);
2778 unblock_input ();
2779 DeleteObject (expect_dirty);
2784 /***********************************************************************
2785 Exposure Events
2786 ***********************************************************************/
2788 static void
2789 frame_highlight (struct frame *f)
2791 x_update_cursor (f, 1);
2792 x_set_frame_alpha (f);
2795 static void
2796 frame_unhighlight (struct frame *f)
2798 x_update_cursor (f, 1);
2799 x_set_frame_alpha (f);
2802 /* The focus has changed. Update the frames as necessary to reflect
2803 the new situation. Note that we can't change the selected frame
2804 here, because the Lisp code we are interrupting might become confused.
2805 Each event gets marked with the frame in which it occurred, so the
2806 Lisp code can tell when the switch took place by examining the events. */
2808 static void
2809 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2811 struct frame *old_focus = dpyinfo->w32_focus_frame;
2813 if (frame != dpyinfo->w32_focus_frame)
2815 /* Set this before calling other routines, so that they see
2816 the correct value of w32_focus_frame. */
2817 dpyinfo->w32_focus_frame = frame;
2819 if (old_focus && old_focus->auto_lower)
2820 x_lower_frame (old_focus);
2822 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2823 dpyinfo->w32_pending_autoraise_frame = dpyinfo->w32_focus_frame;
2824 else
2825 dpyinfo->w32_pending_autoraise_frame = NULL;
2828 x_frame_rehighlight (dpyinfo);
2832 /* Handle FocusIn and FocusOut state changes for FRAME.
2833 If FRAME has focus and there exists more than one frame, puts
2834 a FOCUS_IN_EVENT into *BUFP. */
2836 static void
2837 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2838 struct frame *frame, struct input_event *bufp)
2840 if (type == WM_SETFOCUS)
2842 if (dpyinfo->w32_focus_event_frame != frame)
2844 x_new_focus_frame (dpyinfo, frame);
2845 dpyinfo->w32_focus_event_frame = frame;
2847 /* Don't stop displaying the initial startup message
2848 for a switch-frame event we don't need. */
2849 if (NILP (Vterminal_frame)
2850 && CONSP (Vframe_list)
2851 && !NILP (XCDR (Vframe_list)))
2853 bufp->arg = Qt;
2855 else
2857 bufp->arg = Qnil;
2860 bufp->kind = FOCUS_IN_EVENT;
2861 XSETFRAME (bufp->frame_or_window, frame);
2864 frame->output_data.x->focus_state |= state;
2866 /* TODO: IME focus? */
2868 else if (type == WM_KILLFOCUS)
2870 frame->output_data.x->focus_state &= ~state;
2872 if (dpyinfo->w32_focus_event_frame == frame)
2874 dpyinfo->w32_focus_event_frame = 0;
2875 x_new_focus_frame (dpyinfo, 0);
2877 bufp->kind = FOCUS_OUT_EVENT;
2878 XSETFRAME (bufp->frame_or_window, frame);
2881 /* TODO: IME focus? */
2886 /* The focus may have changed. Figure out if it is a real focus change,
2887 by checking both FocusIn/Out and Enter/LeaveNotify events.
2889 Returns FOCUS_IN_EVENT event in *BUFP. */
2891 static void
2892 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2893 struct input_event *bufp)
2895 struct frame *frame;
2897 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2898 if (! frame)
2899 return;
2901 /* On w32, this is only called from focus events, so no switch needed. */
2902 x_focus_changed (event->msg.message,
2903 (event->msg.message == WM_KILLFOCUS ?
2904 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2905 dpyinfo, frame, bufp);
2909 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2911 void
2912 x_mouse_leave (struct w32_display_info *dpyinfo)
2914 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2917 /* The focus has changed, or we have redirected a frame's focus to
2918 another frame (this happens when a frame uses a surrogate
2919 mini-buffer frame). Shift the highlight as appropriate.
2921 The FRAME argument doesn't necessarily have anything to do with which
2922 frame is being highlighted or un-highlighted; we only use it to find
2923 the appropriate X display info. */
2925 static void
2926 w32_frame_rehighlight (struct frame *frame)
2928 if (! FRAME_W32_P (frame))
2929 return;
2930 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
2933 static void
2934 x_frame_rehighlight (struct w32_display_info *dpyinfo)
2936 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2938 if (dpyinfo->w32_focus_frame)
2940 dpyinfo->x_highlight_frame
2941 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2942 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2943 : dpyinfo->w32_focus_frame);
2944 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2946 fset_focus_frame (dpyinfo->w32_focus_frame, Qnil);
2947 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2950 else
2951 dpyinfo->x_highlight_frame = 0;
2953 if (dpyinfo->x_highlight_frame != old_highlight)
2955 if (old_highlight)
2956 frame_unhighlight (old_highlight);
2957 if (dpyinfo->x_highlight_frame)
2958 frame_highlight (dpyinfo->x_highlight_frame);
2962 /* Keyboard processing - modifier keys, etc. */
2964 /* Convert a keysym to its name. */
2966 char *
2967 x_get_keysym_name (int keysym)
2969 /* Make static so we can always return it */
2970 static char value[100];
2972 block_input ();
2973 GetKeyNameText (keysym, value, 100);
2974 unblock_input ();
2976 return value;
2979 static int
2980 codepage_for_locale (LCID locale)
2982 char cp[20];
2984 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
2985 return atoi (cp);
2986 else
2987 return CP_ACP;
2991 /* Mouse clicks and mouse movement. Rah. */
2993 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2994 the state in PUP. XBUTTON provides extra information for extended mouse
2995 button messages. Returns FALSE if unable to parse the message. */
2996 BOOL
2997 parse_button (int message, int xbutton, int * pbutton, int * pup)
2999 int button = 0;
3000 int up = 0;
3002 switch (message)
3004 case WM_LBUTTONDOWN:
3005 button = 0;
3006 up = 0;
3007 break;
3008 case WM_LBUTTONUP:
3009 button = 0;
3010 up = 1;
3011 break;
3012 case WM_MBUTTONDOWN:
3013 if (NILP (Vw32_swap_mouse_buttons))
3014 button = 1;
3015 else
3016 button = 2;
3017 up = 0;
3018 break;
3019 case WM_MBUTTONUP:
3020 if (NILP (Vw32_swap_mouse_buttons))
3021 button = 1;
3022 else
3023 button = 2;
3024 up = 1;
3025 break;
3026 case WM_RBUTTONDOWN:
3027 if (NILP (Vw32_swap_mouse_buttons))
3028 button = 2;
3029 else
3030 button = 1;
3031 up = 0;
3032 break;
3033 case WM_RBUTTONUP:
3034 if (NILP (Vw32_swap_mouse_buttons))
3035 button = 2;
3036 else
3037 button = 1;
3038 up = 1;
3039 break;
3040 case WM_XBUTTONDOWN:
3041 button = xbutton + 2;
3042 up = 0;
3043 break;
3044 case WM_XBUTTONUP:
3045 button = xbutton + 2;
3046 up = 1;
3047 break;
3048 default:
3049 return (FALSE);
3052 if (pup) *pup = up;
3053 if (pbutton) *pbutton = button;
3055 return (TRUE);
3059 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3061 If the event is a button press, then note that we have grabbed
3062 the mouse. */
3064 static Lisp_Object
3065 construct_mouse_click (struct input_event *result, W32Msg *msg, struct frame *f)
3067 int button;
3068 int up;
3070 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3071 &button, &up);
3073 /* Make the event type NO_EVENT; we'll change that when we decide
3074 otherwise. */
3075 result->kind = MOUSE_CLICK_EVENT;
3076 result->code = button;
3077 result->timestamp = msg->msg.time;
3078 result->modifiers = (msg->dwModifiers
3079 | (up
3080 ? up_modifier
3081 : down_modifier));
3083 XSETINT (result->x, LOWORD (msg->msg.lParam));
3084 XSETINT (result->y, HIWORD (msg->msg.lParam));
3085 XSETFRAME (result->frame_or_window, f);
3086 result->arg = Qnil;
3087 return Qnil;
3090 static Lisp_Object
3091 construct_mouse_wheel (struct input_event *result, W32Msg *msg, struct frame *f)
3093 POINT p;
3094 int delta;
3096 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3097 : WHEEL_EVENT;
3098 result->code = 0;
3099 result->timestamp = msg->msg.time;
3101 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3102 forward, away from the user (up); a negative value indicates that
3103 the wheel was rotated backward, toward the user (down). */
3104 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3106 /* The up and down modifiers indicate if the wheel was rotated up or
3107 down based on WHEEL_DELTA value. */
3108 result->modifiers = (msg->dwModifiers
3109 | ((delta < 0 ) ? down_modifier : up_modifier));
3111 /* With multiple monitors, we can legitimately get negative
3112 coordinates, so cast to short to interpret them correctly. */
3113 p.x = (short) LOWORD (msg->msg.lParam);
3114 p.y = (short) HIWORD (msg->msg.lParam);
3115 ScreenToClient (msg->msg.hwnd, &p);
3116 XSETINT (result->x, p.x);
3117 XSETINT (result->y, p.y);
3118 XSETFRAME (result->frame_or_window, f);
3119 result->arg = Qnil;
3120 return Qnil;
3123 static Lisp_Object
3124 construct_drag_n_drop (struct input_event *result, W32Msg *msg, struct frame *f)
3126 Lisp_Object files;
3127 Lisp_Object frame;
3128 HDROP hdrop;
3129 POINT p;
3130 WORD num_files;
3131 guichar_t *name;
3132 int i, len;
3134 result->kind = DRAG_N_DROP_EVENT;
3135 result->code = 0;
3136 result->timestamp = msg->msg.time;
3137 result->modifiers = msg->dwModifiers;
3139 hdrop = (HDROP) msg->msg.wParam;
3140 DragQueryPoint (hdrop, &p);
3142 #if 0
3143 p.x = LOWORD (msg->msg.lParam);
3144 p.y = HIWORD (msg->msg.lParam);
3145 ScreenToClient (msg->msg.hwnd, &p);
3146 #endif
3148 XSETINT (result->x, p.x);
3149 XSETINT (result->y, p.y);
3151 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3152 files = Qnil;
3154 for (i = 0; i < num_files; i++)
3156 len = GUI_FN (DragQueryFile) (hdrop, i, NULL, 0);
3157 if (len <= 0)
3158 continue;
3160 name = alloca ((len + 1) * sizeof (*name));
3161 GUI_FN (DragQueryFile) (hdrop, i, name, len + 1);
3162 #ifdef NTGUI_UNICODE
3163 files = Fcons (from_unicode_buffer (name), files);
3164 #else
3165 files = Fcons (DECODE_FILE (build_string (name)), files);
3166 #endif /* NTGUI_UNICODE */
3169 DragFinish (hdrop);
3171 XSETFRAME (frame, f);
3172 result->frame_or_window = frame;
3173 result->arg = files;
3174 return Qnil;
3178 #if HAVE_W32NOTIFY
3180 /* File event notifications (see w32notify.c). */
3182 Lisp_Object
3183 lispy_file_action (DWORD action)
3185 static char unknown_fmt[] = "unknown-action(%d)";
3186 Lisp_Object retval;
3188 switch (action)
3190 case FILE_ACTION_ADDED:
3191 retval = Qadded;
3192 break;
3193 case FILE_ACTION_REMOVED:
3194 retval = Qremoved;
3195 break;
3196 case FILE_ACTION_MODIFIED:
3197 retval = Qmodified;
3198 break;
3199 case FILE_ACTION_RENAMED_OLD_NAME:
3200 retval = Qrenamed_from;
3201 break;
3202 case FILE_ACTION_RENAMED_NEW_NAME:
3203 retval = Qrenamed_to;
3204 break;
3205 default:
3207 char buf[sizeof(unknown_fmt) - 1 + INT_STRLEN_BOUND (DWORD)];
3209 sprintf (buf, unknown_fmt, action);
3210 retval = intern (buf);
3212 break;
3215 return retval;
3218 #ifdef WINDOWSNT
3219 /* Put file notifications into the Emacs input event queue. This
3220 function runs when the WM_EMACS_FILENOTIFY message arrives from a
3221 watcher thread. */
3222 static void
3223 queue_notifications (struct input_event *event, W32Msg *msg, struct frame *f,
3224 int *evcount)
3226 BYTE *p = file_notifications;
3227 FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
3228 const DWORD min_size
3229 = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
3230 Lisp_Object frame;
3232 /* We cannot process notification before Emacs is fully initialized,
3233 since we need the UTF-16LE coding-system to be set up. */
3234 if (!initialized)
3236 notification_buffer_in_use = 0;
3237 return;
3240 XSETFRAME (frame, f);
3242 enter_crit ();
3243 if (notification_buffer_in_use)
3245 DWORD info_size = notifications_size;
3246 Lisp_Object cs = intern ("utf-16le");
3247 Lisp_Object obj = w32_get_watch_object (notifications_desc);
3249 /* notifications_size could be zero when the buffer of
3250 notifications overflowed on the OS level, or when the
3251 directory being watched was itself deleted. Do nothing in
3252 that case. */
3253 if (info_size
3254 && !NILP (obj) && CONSP (obj))
3256 Lisp_Object callback = XCDR (obj);
3258 while (info_size >= min_size)
3260 Lisp_Object utf_16_fn
3261 = make_unibyte_string ((char *)fni->FileName,
3262 fni->FileNameLength);
3263 /* Note: mule-conf is preloaded, so utf-16le must
3264 already be defined at this point. */
3265 Lisp_Object fname
3266 = code_convert_string_norecord (utf_16_fn, cs, 0);
3267 Lisp_Object action = lispy_file_action (fni->Action);
3269 event->kind = FILE_NOTIFY_EVENT;
3270 event->code
3271 = (ptrdiff_t)XINT (XIL ((EMACS_INT)notifications_desc));
3272 event->timestamp = msg->msg.time;
3273 event->modifiers = 0;
3274 event->frame_or_window = callback;
3275 event->arg = Fcons (action, fname);
3276 kbd_buffer_store_event (event);
3277 (*evcount)++;
3279 if (!fni->NextEntryOffset)
3280 break;
3281 p += fni->NextEntryOffset;
3282 fni = (PFILE_NOTIFY_INFORMATION)p;
3283 info_size -= fni->NextEntryOffset;
3286 notification_buffer_in_use = 0;
3288 else
3289 DebPrint (("We were promised notifications, but in-use flag is zero!\n"));
3290 leave_crit ();
3292 /* We've stuffed all the events ourselves, so w32_read_socket shouldn't. */
3293 event->kind = NO_EVENT;
3295 #endif /* WINDOWSNT */
3296 #endif /* HAVE_W32NOTIFY */
3299 /* Function to report a mouse movement to the mainstream Emacs code.
3300 The input handler calls this.
3302 We have received a mouse movement event, which is given in *event.
3303 If the mouse is over a different glyph than it was last time, tell
3304 the mainstream emacs code by setting mouse_moved. If not, ask for
3305 another motion event, so we can check again the next time it moves. */
3307 static int
3308 note_mouse_movement (struct frame *frame, MSG *msg)
3310 struct w32_display_info *dpyinfo;
3311 int mouse_x = LOWORD (msg->lParam);
3312 int mouse_y = HIWORD (msg->lParam);
3313 RECT *r;
3315 if (!FRAME_X_OUTPUT (frame))
3316 return 0;
3318 dpyinfo = FRAME_DISPLAY_INFO (frame);
3319 dpyinfo->last_mouse_movement_time = msg->time;
3320 dpyinfo->last_mouse_motion_frame = frame;
3321 dpyinfo->last_mouse_motion_x = mouse_x;
3322 dpyinfo->last_mouse_motion_y = mouse_y;
3324 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3326 frame->mouse_moved = 1;
3327 dpyinfo->last_mouse_scroll_bar = NULL;
3328 note_mouse_highlight (frame, -1, -1);
3329 dpyinfo->last_mouse_glyph_frame = NULL;
3330 return 1;
3333 /* Has the mouse moved off the glyph it was on at the last sighting? */
3334 r = &dpyinfo->last_mouse_glyph;
3335 if (frame != dpyinfo->last_mouse_glyph_frame
3336 || mouse_x < r->left || mouse_x >= r->right
3337 || mouse_y < r->top || mouse_y >= r->bottom)
3339 frame->mouse_moved = 1;
3340 dpyinfo->last_mouse_scroll_bar = NULL;
3341 note_mouse_highlight (frame, mouse_x, mouse_y);
3342 /* Remember the mouse position here, as w32_mouse_position only
3343 gets called when mouse tracking is enabled but we also need
3344 to keep track of the mouse for help_echo and highlighting at
3345 other times. */
3346 remember_mouse_glyph (frame, mouse_x, mouse_y, r);
3347 dpyinfo->last_mouse_glyph_frame = frame;
3348 return 1;
3351 return 0;
3355 /************************************************************************
3356 Mouse Face
3357 ************************************************************************/
3359 static struct scroll_bar *x_window_to_scroll_bar (Window);
3360 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3361 enum scroll_bar_part *,
3362 Lisp_Object *, Lisp_Object *,
3363 unsigned long *);
3364 static void x_check_fullscreen (struct frame *);
3366 static void
3367 w32_define_cursor (Window window, Cursor cursor)
3369 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3371 /* Return the current position of the mouse.
3372 *fp should be a frame which indicates which display to ask about.
3374 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3375 and *part to the frame, window, and scroll bar part that the mouse
3376 is over. Set *x and *y to the portion and whole of the mouse's
3377 position on the scroll bar.
3379 If the mouse movement started elsewhere, set *fp to the frame the
3380 mouse is on, *bar_window to nil, and *x and *y to the character cell
3381 the mouse is over.
3383 Set *time to the server time-stamp for the time at which the mouse
3384 was at this position.
3386 Don't store anything if we don't have a valid set of values to report.
3388 This clears the mouse_moved flag, so we can wait for the next mouse
3389 movement. */
3391 static void
3392 w32_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3393 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3394 unsigned long *time)
3396 struct frame *f1;
3397 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3399 block_input ();
3401 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3402 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3403 else
3405 POINT pt;
3407 Lisp_Object frame, tail;
3409 /* Clear the mouse-moved flag for every frame on this display. */
3410 FOR_EACH_FRAME (tail, frame)
3411 XFRAME (frame)->mouse_moved = 0;
3413 dpyinfo->last_mouse_scroll_bar = NULL;
3415 GetCursorPos (&pt);
3417 /* Now we have a position on the root; find the innermost window
3418 containing the pointer. */
3420 /* If mouse was grabbed on a frame, give coords for that
3421 frame even if the mouse is now outside it. Otherwise
3422 check for window under mouse on one of our frames. */
3423 f1 = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
3424 : x_any_window_to_frame (dpyinfo, WindowFromPoint (pt)));
3426 /* If not, is it one of our scroll bars? */
3427 if (! f1)
3429 struct scroll_bar *bar
3430 = x_window_to_scroll_bar (WindowFromPoint (pt));
3432 if (bar)
3434 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3438 if (f1 == 0 && insist > 0)
3439 f1 = SELECTED_FRAME ();
3441 if (f1)
3443 /* Ok, we found a frame. Store all the values.
3444 last_mouse_glyph is a rectangle used to reduce the
3445 generation of mouse events. To not miss any motion
3446 events, we must divide the frame into rectangles of the
3447 size of the smallest character that could be displayed
3448 on it, i.e. into the same rectangles that matrices on
3449 the frame are divided into. */
3451 dpyinfo = FRAME_DISPLAY_INFO (f1);
3452 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3453 remember_mouse_glyph (f1, pt.x, pt.y, &dpyinfo->last_mouse_glyph);
3454 dpyinfo->last_mouse_glyph_frame = f1;
3456 *bar_window = Qnil;
3457 *part = 0;
3458 *fp = f1;
3459 XSETINT (*x, pt.x);
3460 XSETINT (*y, pt.y);
3461 *time = dpyinfo->last_mouse_movement_time;
3466 unblock_input ();
3470 /***********************************************************************
3471 Tool-bars
3472 ***********************************************************************/
3474 /* Handle mouse button event on the tool-bar of frame F, at
3475 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
3476 or ButtonRelease. */
3478 static void
3479 w32_handle_tool_bar_click (struct frame *f, struct input_event *button_event)
3481 int x = XFASTINT (button_event->x);
3482 int y = XFASTINT (button_event->y);
3484 if (button_event->modifiers & down_modifier)
3485 handle_tool_bar_click (f, x, y, 1, 0);
3486 else
3487 handle_tool_bar_click (f, x, y, 0,
3488 button_event->modifiers & ~up_modifier);
3493 /***********************************************************************
3494 Scroll bars
3495 ***********************************************************************/
3497 /* Scroll bar support. */
3499 /* Given a window ID, find the struct scroll_bar which manages it.
3500 This can be called in GC, so we have to make sure to strip off mark
3501 bits. */
3503 static struct scroll_bar *
3504 x_window_to_scroll_bar (Window window_id)
3506 Lisp_Object tail, frame;
3508 FOR_EACH_FRAME (tail, frame)
3510 Lisp_Object bar, condemned;
3512 /* Scan this frame's scroll bar list for a scroll bar with the
3513 right window ID. */
3514 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3515 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3516 /* This trick allows us to search both the ordinary and
3517 condemned scroll bar lists with one loop. */
3518 ! NILP (bar) || (bar = condemned,
3519 condemned = Qnil,
3520 ! NILP (bar));
3521 bar = XSCROLL_BAR (bar)->next)
3522 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3523 return XSCROLL_BAR (bar);
3526 return 0;
3531 /* Set the thumb size and position of scroll bar BAR. We are currently
3532 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3534 static void
3535 w32_set_scroll_bar_thumb (struct scroll_bar *bar,
3536 int portion, int position, int whole)
3538 Window w = SCROLL_BAR_W32_WINDOW (bar);
3539 /* We use the whole scroll-bar height in the calculations below, to
3540 avoid strange effects like scrolling backwards when just clicking
3541 on the handle (without moving it). */
3542 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height))
3543 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3544 int sb_page, sb_pos;
3545 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3546 SCROLLINFO si;
3548 /* We used to change the nPage setting while dragging the handle,
3549 but that had very strange effects (such as scrolling backwards
3550 while dragging downwards).
3552 Now, we don't change the nPage setting while dragging unless we
3553 get near to the end of the buffer, in which case we often have to
3554 resize the handle to "go all the way". */
3556 if (draggingp)
3558 int near_bottom_p;
3559 block_input ();
3560 si.cbSize = sizeof (si);
3561 si.fMask = SIF_POS | SIF_PAGE;
3562 GetScrollInfo (w, SB_CTL, &si);
3563 near_bottom_p = si.nPos + si.nPage >= range;
3564 unblock_input ();
3565 if (!near_bottom_p)
3566 return;
3569 if (whole)
3571 /* Position scroll bar at rock bottom if the bottom of the
3572 buffer is visible. This avoids shrinking the thumb away
3573 to nothing if it is held at the bottom of the buffer. */
3574 if (position + portion >= whole && !draggingp)
3576 sb_page = range * (whole - position) / whole;
3577 sb_pos = range;
3579 else
3581 sb_pos = position * range / whole;
3582 sb_page = (min (portion, (whole - position)) * range) / whole;
3585 else
3587 sb_page = range;
3588 sb_pos = 0;
3591 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3593 block_input ();
3595 si.cbSize = sizeof (si);
3596 si.fMask = SIF_PAGE | SIF_POS;
3597 si.nPage = sb_page;
3598 si.nPos = sb_pos;
3600 SetScrollInfo (w, SB_CTL, &si, TRUE);
3602 unblock_input ();
3606 /************************************************************************
3607 Scroll bars, general
3608 ************************************************************************/
3610 static HWND
3611 my_create_scrollbar (struct frame * f, struct scroll_bar * bar)
3613 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3614 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3615 (LPARAM) bar);
3618 /*#define ATTACH_THREADS*/
3620 static BOOL
3621 my_show_window (struct frame *f, HWND hwnd, int how)
3623 #ifndef ATTACH_THREADS
3624 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3625 (WPARAM) hwnd, (LPARAM) how);
3626 #else
3627 return ShowWindow (hwnd, how);
3628 #endif
3631 static void
3632 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3633 int x, int y, int cx, int cy, UINT flags)
3635 #ifndef ATTACH_THREADS
3636 WINDOWPOS pos;
3637 pos.hwndInsertAfter = hwndAfter;
3638 pos.x = x;
3639 pos.y = y;
3640 pos.cx = cx;
3641 pos.cy = cy;
3642 pos.flags = flags;
3643 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3644 #else
3645 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3646 #endif
3649 #if 0
3650 static void
3651 my_set_focus (struct frame * f, HWND hwnd)
3653 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3654 (WPARAM) hwnd, 0);
3656 #endif
3658 static void
3659 my_set_foreground_window (HWND hwnd)
3661 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3665 static void
3666 my_destroy_window (struct frame * f, HWND hwnd)
3668 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3669 (WPARAM) hwnd, 0);
3672 static void
3673 my_bring_window_to_top (HWND hwnd)
3675 SendMessage (hwnd, WM_EMACS_BRINGTOTOP, (WPARAM) hwnd, 0);
3678 /* Create a scroll bar and return the scroll bar vector for it. W is
3679 the Emacs window on which to create the scroll bar. TOP, LEFT,
3680 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
3681 scroll bar. */
3683 static struct scroll_bar *
3684 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
3686 struct frame *f = XFRAME (WINDOW_FRAME (w));
3687 HWND hwnd;
3688 SCROLLINFO si;
3689 struct scroll_bar *bar
3690 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, fringe_extended_p, PVEC_OTHER);
3691 Lisp_Object barobj;
3693 block_input ();
3695 XSETWINDOW (bar->window, w);
3696 XSETINT (bar->top, top);
3697 XSETINT (bar->left, left);
3698 XSETINT (bar->width, width);
3699 XSETINT (bar->height, height);
3700 XSETINT (bar->start, 0);
3701 XSETINT (bar->end, 0);
3702 bar->dragging = Qnil;
3703 bar->fringe_extended_p = 0;
3705 /* Requires geometry to be set before call to create the real window */
3707 hwnd = my_create_scrollbar (f, bar);
3709 si.cbSize = sizeof (si);
3710 si.fMask = SIF_ALL;
3711 si.nMin = 0;
3712 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3713 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3714 si.nPage = si.nMax;
3715 si.nPos = 0;
3717 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3719 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3721 /* Add bar to its frame's list of scroll bars. */
3722 bar->next = FRAME_SCROLL_BARS (f);
3723 bar->prev = Qnil;
3724 XSETVECTOR (barobj, bar);
3725 fset_scroll_bars (f, barobj);
3726 if (! NILP (bar->next))
3727 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3729 unblock_input ();
3731 return bar;
3735 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3736 nil. */
3738 static void
3739 x_scroll_bar_remove (struct scroll_bar *bar)
3741 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3743 block_input ();
3745 /* Destroy the window. */
3746 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3748 /* Dissociate this scroll bar from its window. */
3749 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
3751 unblock_input ();
3754 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3755 that we are displaying PORTION characters out of a total of WHOLE
3756 characters, starting at POSITION. If WINDOW has no scroll bar,
3757 create one. */
3758 static void
3759 w32_set_vertical_scroll_bar (struct window *w,
3760 int portion, int whole, int position)
3762 struct frame *f = XFRAME (w->frame);
3763 Lisp_Object barobj;
3764 struct scroll_bar *bar;
3765 int top, height, left, sb_left, width, sb_width;
3766 int window_y, window_height;
3767 bool fringe_extended_p;
3769 /* Get window dimensions. */
3770 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
3771 top = window_y;
3772 width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3773 height = window_height;
3775 /* Compute the left edge of the scroll bar area. */
3776 left = WINDOW_SCROLL_BAR_AREA_X (w);
3778 /* Compute the width of the scroll bar which might be less than
3779 the width of the area reserved for the scroll bar. */
3780 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3781 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3782 else
3783 sb_width = width;
3785 /* Compute the left edge of the scroll bar. */
3786 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3787 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3788 else
3789 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3791 fringe_extended_p = WINDOW_FRINGE_EXTENDED_P (w);
3793 /* Does the scroll bar exist yet? */
3794 if (NILP (w->vertical_scroll_bar))
3796 HDC hdc;
3797 block_input ();
3798 if (width > 0 && height > 0)
3800 hdc = get_frame_dc (f);
3801 if (fringe_extended_p)
3802 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3803 else
3804 w32_clear_area (f, hdc, left, top, width, height);
3805 release_frame_dc (f, hdc);
3807 unblock_input ();
3809 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3811 else
3813 /* It may just need to be moved and resized. */
3814 HWND hwnd;
3816 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3817 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3819 /* If already correctly positioned, do nothing. */
3820 if (XINT (bar->left) == sb_left
3821 && XINT (bar->top) == top
3822 && XINT (bar->width) == sb_width
3823 && XINT (bar->height) == height
3824 && bar->fringe_extended_p == fringe_extended_p)
3826 /* Redraw after clear_frame. */
3827 if (!my_show_window (f, hwnd, SW_NORMAL))
3828 InvalidateRect (hwnd, NULL, FALSE);
3830 else
3832 HDC hdc;
3833 SCROLLINFO si;
3835 block_input ();
3836 if (width && height)
3838 hdc = get_frame_dc (f);
3839 /* Since Windows scroll bars are smaller than the space reserved
3840 for them on the frame, we have to clear "under" them. */
3841 if (fringe_extended_p)
3842 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3843 else
3844 w32_clear_area (f, hdc, left, top, width, height);
3845 release_frame_dc (f, hdc);
3847 /* Make sure scroll bar is "visible" before moving, to ensure the
3848 area of the parent window now exposed will be refreshed. */
3849 my_show_window (f, hwnd, SW_HIDE);
3850 MoveWindow (hwnd, sb_left, top, sb_width,
3851 max (height, 1), TRUE);
3853 si.cbSize = sizeof (si);
3854 si.fMask = SIF_RANGE;
3855 si.nMin = 0;
3856 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3857 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3859 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3861 my_show_window (f, hwnd, SW_NORMAL);
3862 /* InvalidateRect (w, NULL, FALSE); */
3864 /* Remember new settings. */
3865 XSETINT (bar->left, sb_left);
3866 XSETINT (bar->top, top);
3867 XSETINT (bar->width, sb_width);
3868 XSETINT (bar->height, height);
3870 unblock_input ();
3873 bar->fringe_extended_p = fringe_extended_p;
3875 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3876 XSETVECTOR (barobj, bar);
3877 wset_vertical_scroll_bar (w, barobj);
3881 /* The following three hooks are used when we're doing a thorough
3882 redisplay of the frame. We don't explicitly know which scroll bars
3883 are going to be deleted, because keeping track of when windows go
3884 away is a real pain - "Can you say set-window-configuration, boys
3885 and girls?" Instead, we just assert at the beginning of redisplay
3886 that *all* scroll bars are to be removed, and then save a scroll bar
3887 from the fiery pit when we actually redisplay its window. */
3889 /* Arrange for all scroll bars on FRAME to be removed at the next call
3890 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3891 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3893 static void
3894 w32_condemn_scroll_bars (struct frame *frame)
3896 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3897 while (! NILP (FRAME_SCROLL_BARS (frame)))
3899 Lisp_Object bar;
3900 bar = FRAME_SCROLL_BARS (frame);
3901 fset_scroll_bars (frame, XSCROLL_BAR (bar)->next);
3902 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3903 XSCROLL_BAR (bar)->prev = Qnil;
3904 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3905 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3906 fset_condemned_scroll_bars (frame, bar);
3911 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3912 Note that WINDOW isn't necessarily condemned at all. */
3914 static void
3915 w32_redeem_scroll_bar (struct window *window)
3917 struct scroll_bar *bar;
3918 Lisp_Object barobj;
3919 struct frame *f;
3921 /* We can't redeem this window's scroll bar if it doesn't have one. */
3922 if (NILP (window->vertical_scroll_bar))
3923 emacs_abort ();
3925 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3927 /* Unlink it from the condemned list. */
3928 f = XFRAME (WINDOW_FRAME (window));
3929 if (NILP (bar->prev))
3931 /* If the prev pointer is nil, it must be the first in one of
3932 the lists. */
3933 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3934 /* It's not condemned. Everything's fine. */
3935 return;
3936 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3937 window->vertical_scroll_bar))
3938 fset_condemned_scroll_bars (f, bar->next);
3939 else
3940 /* If its prev pointer is nil, it must be at the front of
3941 one or the other! */
3942 emacs_abort ();
3944 else
3945 XSCROLL_BAR (bar->prev)->next = bar->next;
3947 if (! NILP (bar->next))
3948 XSCROLL_BAR (bar->next)->prev = bar->prev;
3950 bar->next = FRAME_SCROLL_BARS (f);
3951 bar->prev = Qnil;
3952 XSETVECTOR (barobj, bar);
3953 fset_scroll_bars (f, barobj);
3954 if (! NILP (bar->next))
3955 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3958 /* Remove all scroll bars on FRAME that haven't been saved since the
3959 last call to `*condemn_scroll_bars_hook'. */
3961 static void
3962 w32_judge_scroll_bars (struct frame *f)
3964 Lisp_Object bar, next;
3966 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3968 /* Clear out the condemned list now so we won't try to process any
3969 more events on the hapless scroll bars. */
3970 fset_condemned_scroll_bars (f, Qnil);
3972 for (; ! NILP (bar); bar = next)
3974 struct scroll_bar *b = XSCROLL_BAR (bar);
3976 x_scroll_bar_remove (b);
3978 next = b->next;
3979 b->next = b->prev = Qnil;
3982 /* Now there should be no references to the condemned scroll bars,
3983 and they should get garbage-collected. */
3986 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3987 is set to something other than NO_EVENT, it is enqueued.
3989 This may be called from a signal handler, so we have to ignore GC
3990 mark bits. */
3992 static int
3993 w32_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
3994 struct input_event *emacs_event)
3996 if (! WINDOWP (bar->window))
3997 emacs_abort ();
3999 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
4000 emacs_event->code = 0;
4001 /* not really meaningful to distinguish up/down */
4002 emacs_event->modifiers = msg->dwModifiers;
4003 emacs_event->frame_or_window = bar->window;
4004 emacs_event->arg = Qnil;
4005 emacs_event->timestamp = msg->msg.time;
4008 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4009 int y;
4010 int dragging = !NILP (bar->dragging);
4011 SCROLLINFO si;
4013 si.cbSize = sizeof (si);
4014 si.fMask = SIF_POS;
4016 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4017 y = si.nPos;
4019 bar->dragging = Qnil;
4020 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4022 switch (LOWORD (msg->msg.wParam))
4024 case SB_LINEDOWN:
4025 emacs_event->part = scroll_bar_down_arrow;
4026 break;
4027 case SB_LINEUP:
4028 emacs_event->part = scroll_bar_up_arrow;
4029 break;
4030 case SB_PAGEUP:
4031 emacs_event->part = scroll_bar_above_handle;
4032 break;
4033 case SB_PAGEDOWN:
4034 emacs_event->part = scroll_bar_below_handle;
4035 break;
4036 case SB_TOP:
4037 emacs_event->part = scroll_bar_handle;
4038 y = 0;
4039 break;
4040 case SB_BOTTOM:
4041 emacs_event->part = scroll_bar_handle;
4042 y = top_range;
4043 break;
4044 case SB_THUMBTRACK:
4045 case SB_THUMBPOSITION:
4046 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
4047 y = HIWORD (msg->msg.wParam);
4048 bar->dragging = Qt;
4049 emacs_event->part = scroll_bar_handle;
4051 /* "Silently" update current position. */
4053 SCROLLINFO si;
4055 si.cbSize = sizeof (si);
4056 si.fMask = SIF_POS;
4057 si.nPos = y;
4058 /* Remember apparent position (we actually lag behind the real
4059 position, so don't set that directly). */
4060 last_scroll_bar_drag_pos = y;
4062 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4064 break;
4065 case SB_ENDSCROLL:
4066 /* If this is the end of a drag sequence, then reset the scroll
4067 handle size to normal and do a final redraw. Otherwise do
4068 nothing. */
4069 if (dragging)
4071 SCROLLINFO si;
4072 int start = XINT (bar->start);
4073 int end = XINT (bar->end);
4075 si.cbSize = sizeof (si);
4076 si.fMask = SIF_PAGE | SIF_POS;
4077 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
4078 si.nPos = last_scroll_bar_drag_pos;
4079 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4081 /* fall through */
4082 default:
4083 emacs_event->kind = NO_EVENT;
4084 return FALSE;
4087 XSETINT (emacs_event->x, y);
4088 XSETINT (emacs_event->y, top_range);
4090 return TRUE;
4094 /* Return information to the user about the current position of the mouse
4095 on the scroll bar. */
4097 static void
4098 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4099 enum scroll_bar_part *part,
4100 Lisp_Object *x, Lisp_Object *y,
4101 unsigned long *time)
4103 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4104 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4105 Window w = SCROLL_BAR_W32_WINDOW (bar);
4106 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4107 int pos;
4108 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4109 SCROLLINFO si;
4111 block_input ();
4113 *fp = f;
4114 *bar_window = bar->window;
4116 si.cbSize = sizeof (si);
4117 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4119 GetScrollInfo (w, SB_CTL, &si);
4120 pos = si.nPos;
4121 top_range = si.nMax - si.nPage + 1;
4123 switch (LOWORD (dpyinfo->last_mouse_scroll_bar_pos))
4125 case SB_THUMBPOSITION:
4126 case SB_THUMBTRACK:
4127 *part = scroll_bar_handle;
4128 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
4129 pos = HIWORD (dpyinfo->last_mouse_scroll_bar_pos);
4130 break;
4131 case SB_LINEDOWN:
4132 *part = scroll_bar_handle;
4133 pos++;
4134 break;
4135 default:
4136 *part = scroll_bar_handle;
4137 break;
4140 XSETINT (*x, pos);
4141 XSETINT (*y, top_range);
4143 f->mouse_moved = 0;
4144 dpyinfo->last_mouse_scroll_bar = NULL;
4146 *time = dpyinfo->last_mouse_movement_time;
4148 unblock_input ();
4152 /* The screen has been cleared so we may have changed foreground or
4153 background colors, and the scroll bars may need to be redrawn.
4154 Clear out the scroll bars, and ask for expose events, so we can
4155 redraw them. */
4157 void
4158 x_scroll_bar_clear (struct frame *f)
4160 Lisp_Object bar;
4162 /* We can have scroll bars even if this is 0,
4163 if we just turned off scroll bar mode.
4164 But in that case we should not clear them. */
4165 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4166 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4167 bar = XSCROLL_BAR (bar)->next)
4169 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4170 HDC hdc = GetDC (window);
4171 RECT rect;
4173 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4174 arranges to refresh the scroll bar if hidden. */
4175 my_show_window (f, window, SW_HIDE);
4177 GetClientRect (window, &rect);
4178 select_palette (f, hdc);
4179 w32_clear_rect (f, hdc, &rect);
4180 deselect_palette (f, hdc);
4182 ReleaseDC (window, hdc);
4187 /* The main W32 event-reading loop - w32_read_socket. */
4189 /* Record the last 100 characters stored
4190 to help debug the loss-of-chars-during-GC problem. */
4192 static int temp_index;
4193 static short temp_buffer[100];
4195 /* Temporarily store lead byte of DBCS input sequences. */
4196 static char dbcs_lead = 0;
4198 /* Read events coming from the W32 shell.
4199 This routine is called by the SIGIO handler.
4200 We return as soon as there are no more events to be read.
4202 For an overview of how Emacs input works on MS-Windows, see the
4203 commentary before w32_msg_pump in w32fns.c.
4205 We return the number of characters stored into the buffer,
4206 thus pretending to be `read'.
4208 Some of these messages are reposted back to the message queue since the
4209 system calls the windows proc directly in a context where we cannot return
4210 the data nor can we guarantee the state we are in. So if we dispatch them
4211 we will get into an infinite loop. To prevent this from ever happening we
4212 will set a variable to indicate we are in the read_socket call and indicate
4213 which message we are processing since the windows proc gets called
4214 recursively with different messages by the system.
4217 static int
4218 w32_read_socket (struct terminal *terminal,
4219 struct input_event *hold_quit)
4221 int count = 0;
4222 int check_visibility = 0;
4223 W32Msg msg;
4224 struct frame *f;
4225 struct w32_display_info *dpyinfo = &one_w32_display_info;
4226 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
4227 static char buf[1];
4229 block_input ();
4231 /* So people can tell when we have read the available input. */
4232 input_signal_count++;
4234 /* Process any incoming thread messages. */
4235 drain_message_queue ();
4237 /* TODO: ghostscript integration. */
4238 while (get_next_msg (&msg, FALSE))
4240 struct input_event inev;
4241 int do_help = 0;
4243 /* DebPrint (("w32_read_socket: %s time:%u\n", */
4244 /* w32_name_of_message (msg.msg.message), */
4245 /* msg.msg.time)); */
4247 EVENT_INIT (inev);
4248 inev.kind = NO_EVENT;
4249 inev.arg = Qnil;
4251 switch (msg.msg.message)
4253 case WM_EMACS_PAINT:
4254 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4256 if (f)
4258 if (msg.rect.right == msg.rect.left ||
4259 msg.rect.bottom == msg.rect.top)
4261 /* We may get paint messages even though the client
4262 area is clipped - these are not expose events. */
4263 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4264 SDATA (f->name)));
4266 else if (FRAME_VISIBLE_P (f) != 1)
4268 bool iconified = FRAME_ICONIFIED_P (f);
4270 /* Definitely not obscured, so mark as visible. */
4271 SET_FRAME_VISIBLE (f, 1);
4272 SET_FRAME_ICONIFIED (f, 0);
4273 SET_FRAME_GARBAGED (f);
4274 if (!f->output_data.w32->asked_for_visible)
4275 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4276 SDATA (f->name)));
4278 /* WM_PAINT serves as MapNotify as well, so report
4279 visibility changes properly. */
4280 if (iconified)
4282 inev.kind = DEICONIFY_EVENT;
4283 XSETFRAME (inev.frame_or_window, f);
4285 else if (!NILP (Vframe_list) && !NILP (XCDR (Vframe_list)))
4286 /* Force a redisplay sooner or later to update the
4287 frame titles in case this is the second frame. */
4288 record_asynch_buffer_change ();
4290 else
4292 HDC hdc = get_frame_dc (f);
4294 /* Erase background again for safety. */
4295 w32_clear_rect (f, hdc, &msg.rect);
4296 release_frame_dc (f, hdc);
4297 expose_frame (f,
4298 msg.rect.left,
4299 msg.rect.top,
4300 msg.rect.right - msg.rect.left,
4301 msg.rect.bottom - msg.rect.top);
4304 break;
4306 case WM_INPUTLANGCHANGE:
4307 /* Generate a language change event. */
4308 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4310 /* lParam contains the input language ID in its low 16 bits.
4311 Use it to update our record of the keyboard codepage. */
4312 w32_keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4313 & 0xffff));
4315 if (f)
4317 inev.kind = LANGUAGE_CHANGE_EVENT;
4318 XSETFRAME (inev.frame_or_window, f);
4319 inev.code = w32_keyboard_codepage;
4320 inev.modifiers = msg.msg.lParam & 0xffff;
4322 break;
4324 case WM_KEYDOWN:
4325 case WM_SYSKEYDOWN:
4326 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4328 if (f && !FRAME_ICONIFIED_P (f))
4330 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4331 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4333 clear_mouse_face (hlinfo);
4334 hlinfo->mouse_face_hidden = 1;
4337 if (temp_index == sizeof temp_buffer / sizeof (short))
4338 temp_index = 0;
4339 temp_buffer[temp_index++] = msg.msg.wParam;
4340 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4341 inev.code = msg.msg.wParam;
4342 inev.modifiers = msg.dwModifiers;
4343 XSETFRAME (inev.frame_or_window, f);
4344 inev.timestamp = msg.msg.time;
4346 break;
4348 case WM_UNICHAR:
4349 case WM_SYSCHAR:
4350 case WM_CHAR:
4351 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4353 if (f && !FRAME_ICONIFIED_P (f))
4355 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4356 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4358 clear_mouse_face (hlinfo);
4359 hlinfo->mouse_face_hidden = 1;
4362 if (temp_index == sizeof temp_buffer / sizeof (short))
4363 temp_index = 0;
4364 temp_buffer[temp_index++] = msg.msg.wParam;
4366 inev.modifiers = msg.dwModifiers;
4367 XSETFRAME (inev.frame_or_window, f);
4368 inev.timestamp = msg.msg.time;
4370 if (msg.msg.message == WM_UNICHAR)
4372 inev.code = msg.msg.wParam;
4374 else if (msg.msg.wParam < 256)
4376 wchar_t code;
4377 char dbcs[2];
4378 dbcs[0] = 0;
4379 dbcs[1] = (char) msg.msg.wParam;
4381 if (dbcs_lead)
4383 dbcs[0] = dbcs_lead;
4384 dbcs_lead = 0;
4385 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4386 dbcs, 2, &code, 1))
4388 /* Garbage */
4389 DebPrint (("Invalid DBCS sequence: %d %d\n",
4390 dbcs[0], dbcs[1]));
4391 inev.kind = NO_EVENT;
4392 break;
4395 else if (IsDBCSLeadByteEx (w32_keyboard_codepage,
4396 (BYTE) msg.msg.wParam))
4398 dbcs_lead = (char) msg.msg.wParam;
4399 inev.kind = NO_EVENT;
4400 break;
4402 else
4404 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4405 &dbcs[1], 1, &code, 1))
4407 /* What to do with garbage? */
4408 DebPrint (("Invalid character: %d\n", dbcs[1]));
4409 inev.kind = NO_EVENT;
4410 break;
4413 inev.code = code;
4415 else
4417 /* Windows shouldn't generate WM_CHAR events above 0xFF
4418 in non-Unicode message handlers. */
4419 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4420 inev.kind = NO_EVENT;
4421 break;
4423 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4424 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4426 break;
4428 case WM_APPCOMMAND:
4429 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4431 if (f && !FRAME_ICONIFIED_P (f))
4433 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4434 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4436 clear_mouse_face (hlinfo);
4437 hlinfo->mouse_face_hidden = 1;
4440 if (temp_index == sizeof temp_buffer / sizeof (short))
4441 temp_index = 0;
4442 temp_buffer[temp_index++] = msg.msg.wParam;
4443 inev.kind = MULTIMEDIA_KEY_EVENT;
4444 inev.code = GET_APPCOMMAND_LPARAM (msg.msg.lParam);
4445 inev.modifiers = msg.dwModifiers;
4446 XSETFRAME (inev.frame_or_window, f);
4447 inev.timestamp = msg.msg.time;
4449 break;
4451 case WM_MOUSEMOVE:
4452 /* Ignore non-movement. */
4454 int x = LOWORD (msg.msg.lParam);
4455 int y = HIWORD (msg.msg.lParam);
4456 if (x == last_mousemove_x && y == last_mousemove_y)
4457 break;
4458 last_mousemove_x = x;
4459 last_mousemove_y = y;
4462 previous_help_echo_string = help_echo_string;
4463 help_echo_string = Qnil;
4465 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4466 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4468 if (hlinfo->mouse_face_hidden)
4470 hlinfo->mouse_face_hidden = 0;
4471 clear_mouse_face (hlinfo);
4474 if (f)
4476 /* Generate SELECT_WINDOW_EVENTs when needed. */
4477 if (!NILP (Vmouse_autoselect_window))
4479 static Lisp_Object last_mouse_window;
4480 Lisp_Object window = window_from_coordinates
4481 (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0);
4483 /* Window will be selected only when it is not
4484 selected now and last mouse movement event was
4485 not in it. Minibuffer window will be selected
4486 only when it is active. */
4487 if (WINDOWP (window)
4488 && !EQ (window, last_mouse_window)
4489 && !EQ (window, selected_window)
4490 /* For click-to-focus window managers
4491 create event iff we don't leave the
4492 selected frame. */
4493 && (focus_follows_mouse
4494 || (EQ (XWINDOW (window)->frame,
4495 XWINDOW (selected_window)->frame))))
4497 inev.kind = SELECT_WINDOW_EVENT;
4498 inev.frame_or_window = window;
4500 /* Remember the last window where we saw the mouse. */
4501 last_mouse_window = window;
4503 if (!note_mouse_movement (f, &msg.msg))
4504 help_echo_string = previous_help_echo_string;
4506 else
4508 /* If we move outside the frame, then we're
4509 certainly no longer on any text in the frame. */
4510 clear_mouse_face (hlinfo);
4513 /* If the contents of the global variable help_echo_string
4514 has changed, generate a HELP_EVENT. */
4515 #if 0 /* The below is an invalid comparison when CHECK_LISP_OBJECT_TYPE.
4516 But it was originally changed to this to fix a bug, so I have
4517 not removed it completely in case the bug is still there. */
4518 if (help_echo_string != previous_help_echo_string ||
4519 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4520 #else /* This is what xterm.c does. */
4521 if (!NILP (help_echo_string)
4522 || !NILP (previous_help_echo_string))
4523 do_help = 1;
4524 #endif
4525 break;
4527 case WM_LBUTTONDOWN:
4528 case WM_LBUTTONUP:
4529 case WM_MBUTTONDOWN:
4530 case WM_MBUTTONUP:
4531 case WM_RBUTTONDOWN:
4532 case WM_RBUTTONUP:
4533 case WM_XBUTTONDOWN:
4534 case WM_XBUTTONUP:
4536 /* If we decide we want to generate an event to be seen
4537 by the rest of Emacs, we put it here. */
4538 bool tool_bar_p = 0;
4539 int button;
4540 int up;
4542 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4543 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4545 if (f)
4547 construct_mouse_click (&inev, &msg, f);
4549 /* Is this in the tool-bar? */
4550 if (WINDOWP (f->tool_bar_window)
4551 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4553 Lisp_Object window;
4554 int x = XFASTINT (inev.x);
4555 int y = XFASTINT (inev.y);
4557 window = window_from_coordinates (f, x, y, 0, 1);
4559 if (EQ (window, f->tool_bar_window))
4561 w32_handle_tool_bar_click (f, &inev);
4562 tool_bar_p = 1;
4566 if (tool_bar_p
4567 || (dpyinfo->w32_focus_frame
4568 && f != dpyinfo->w32_focus_frame))
4569 inev.kind = NO_EVENT;
4572 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4573 &button, &up);
4575 if (up)
4577 dpyinfo->grabbed &= ~ (1 << button);
4579 else
4581 dpyinfo->grabbed |= (1 << button);
4582 dpyinfo->last_mouse_frame = f;
4583 /* Ignore any mouse motion that happened
4584 before this event; any subsequent mouse-movement
4585 Emacs events should reflect only motion after
4586 the ButtonPress. */
4587 if (f != 0)
4588 f->mouse_moved = 0;
4590 if (!tool_bar_p)
4591 last_tool_bar_item = -1;
4593 break;
4596 case WM_MOUSEWHEEL:
4597 case WM_MOUSEHWHEEL:
4599 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4600 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4602 if (f)
4605 if (!dpyinfo->w32_focus_frame
4606 || f == dpyinfo->w32_focus_frame)
4608 /* Emit an Emacs wheel-up/down event. */
4609 construct_mouse_wheel (&inev, &msg, f);
4611 /* Ignore any mouse motion that happened before this
4612 event; any subsequent mouse-movement Emacs events
4613 should reflect only motion after the
4614 ButtonPress. */
4615 f->mouse_moved = 0;
4617 dpyinfo->last_mouse_frame = f;
4618 last_tool_bar_item = -1;
4620 break;
4622 case WM_DROPFILES:
4623 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4625 if (f)
4626 construct_drag_n_drop (&inev, &msg, f);
4627 break;
4629 case WM_VSCROLL:
4631 struct scroll_bar *bar =
4632 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4634 if (bar)
4635 w32_scroll_bar_handle_click (bar, &msg, &inev);
4636 break;
4639 case WM_WINDOWPOSCHANGED:
4640 case WM_ACTIVATE:
4641 case WM_ACTIVATEAPP:
4642 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4643 if (f)
4645 /* Run the full-screen hook function also when we are
4646 being activated, to actually install the required
4647 size in effect, if the WAIT flag is set. This is
4648 because when the hook is run from x_set_fullscreen,
4649 the frame might not yet be visible, if that call is a
4650 result of make-frame, and in that case the hook just
4651 sets the WAIT flag. */
4652 if ((msg.msg.message == WM_WINDOWPOSCHANGED || msg.msg.wParam)
4653 && (f->want_fullscreen & FULLSCREEN_WAIT))
4654 w32fullscreen_hook (f);
4655 x_check_fullscreen (f);
4657 check_visibility = 1;
4658 break;
4660 case WM_MOVE:
4661 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4663 if (f && !FRAME_ICONIFIED_P (f))
4664 x_real_positions (f, &f->left_pos, &f->top_pos);
4666 check_visibility = 1;
4667 break;
4669 case WM_SHOWWINDOW:
4670 /* wParam non-zero means Window is about to be shown, 0 means
4671 about to be hidden. */
4672 /* Redo the mouse-highlight after the tooltip has gone. */
4673 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4675 tip_window = NULL;
4676 x_redo_mouse_highlight (dpyinfo);
4679 /* If window has been obscured or exposed by another window
4680 being maximized or minimized/restored, then recheck
4681 visibility of all frames. Direct changes to our own
4682 windows get handled by WM_SIZE. */
4683 #if 0
4684 if (msg.msg.lParam != 0)
4685 check_visibility = 1;
4686 else
4688 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4689 f->async_visible = msg.msg.wParam;
4691 #endif
4693 check_visibility = 1;
4694 break;
4696 case WM_SIZE:
4697 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4699 /* Inform lisp of whether frame has been iconified etc. */
4700 if (f)
4702 switch (msg.msg.wParam)
4704 case SIZE_MINIMIZED:
4705 SET_FRAME_VISIBLE (f, 0);
4706 SET_FRAME_ICONIFIED (f, 1);
4708 inev.kind = ICONIFY_EVENT;
4709 XSETFRAME (inev.frame_or_window, f);
4710 break;
4712 case SIZE_MAXIMIZED:
4714 bool iconified = FRAME_ICONIFIED_P (f);
4716 SET_FRAME_VISIBLE (f, 1);
4717 SET_FRAME_ICONIFIED (f, 0);
4719 /* wait_reading_process_output will notice this
4720 and update the frame's display structures. */
4721 SET_FRAME_GARBAGED (f);
4723 if (iconified)
4725 int x, y;
4727 /* Reset top and left positions of the Window
4728 here since Windows sends a WM_MOVE message
4729 BEFORE telling us the Window is minimized
4730 when the Window is iconified, with 3000,3000
4731 as the co-ords. */
4732 x_real_positions (f, &x, &y);
4733 f->left_pos = x;
4734 f->top_pos = y;
4736 inev.kind = DEICONIFY_EVENT;
4737 XSETFRAME (inev.frame_or_window, f);
4739 else if (! NILP (Vframe_list)
4740 && ! NILP (XCDR (Vframe_list)))
4741 /* Force a redisplay sooner or later
4742 to update the frame titles
4743 in case this is the second frame. */
4744 record_asynch_buffer_change ();
4747 if (get_frame_param (f, Qfullscreen) == Qnil)
4748 set_frame_param (f, Qfullscreen, Qmaximized);
4749 else if (get_frame_param (f, Qfullscreen) != Qmaximized)
4750 set_frame_param (f, Qmaximized, Qmaximized);
4752 break;
4754 case SIZE_RESTORED:
4756 bool iconified = FRAME_ICONIFIED_P (f);
4758 if (iconified)
4759 SET_FRAME_VISIBLE (f, 1);
4760 SET_FRAME_ICONIFIED (f, 0);
4762 /* wait_reading_process_output will notice this
4763 and update the frame's display structures. */
4764 SET_FRAME_GARBAGED (f);
4766 if (iconified)
4768 /* Reset top and left positions of the Window
4769 here since Windows sends a WM_MOVE message
4770 BEFORE telling us the Window is minimized
4771 when the Window is iconified, with 3000,3000
4772 as the co-ords. */
4773 x_real_positions (f, &f->left_pos, &f->top_pos);
4775 inev.kind = DEICONIFY_EVENT;
4776 XSETFRAME (inev.frame_or_window, f);
4778 else if (! NILP (Vframe_list)
4779 && ! NILP (XCDR (Vframe_list)))
4780 /* Force a redisplay sooner or later
4781 to update the frame titles
4782 in case this is the second frame. */
4783 record_asynch_buffer_change ();
4786 if (get_frame_param (f, Qfullscreen) == Qmaximized)
4787 set_frame_param (f, Qfullscreen, Qnil);
4788 else if (get_frame_param (f, Qmaximized) != Qnil)
4789 set_frame_param (f, Qmaximized, Qnil);
4791 break;
4795 if (f && !FRAME_ICONIFIED_P (f) && msg.msg.wParam != SIZE_MINIMIZED)
4797 RECT rect;
4798 int rows, columns, width, height, text_width, text_height;
4800 GetClientRect (msg.msg.hwnd, &rect);
4802 height = rect.bottom - rect.top;
4803 width = rect.right - rect.left;
4804 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
4805 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
4806 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4807 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4809 /* TODO: Clip size to the screen dimensions. */
4811 /* Even if the number of character rows and columns has
4812 not changed, the font size may have changed, so we need
4813 to check the pixel dimensions as well. */
4815 if (width != FRAME_PIXEL_WIDTH (f)
4816 || height != FRAME_PIXEL_HEIGHT (f)
4817 || text_width != FRAME_TEXT_WIDTH (f)
4818 || text_height != FRAME_TEXT_HEIGHT (f))
4820 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
4821 SET_FRAME_GARBAGED (f);
4822 cancel_mouse_face (f);
4823 /* Do we want to set these here ???? */
4824 /** FRAME_PIXEL_WIDTH (f) = width; **/
4825 /** FRAME_TEXT_WIDTH (f) = text_width; **/
4826 /** FRAME_PIXEL_HEIGHT (f) = height; **/
4827 f->win_gravity = NorthWestGravity;
4831 check_visibility = 1;
4832 break;
4834 case WM_MOUSELEAVE:
4835 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4836 if (f)
4838 if (f == hlinfo->mouse_face_mouse_frame)
4840 /* If we move outside the frame, then we're
4841 certainly no longer on any text in the frame. */
4842 clear_mouse_face (hlinfo);
4843 hlinfo->mouse_face_mouse_frame = 0;
4846 /* Generate a nil HELP_EVENT to cancel a help-echo.
4847 Do it only if there's something to cancel.
4848 Otherwise, the startup message is cleared when
4849 the mouse leaves the frame. */
4850 if (any_help_event_p)
4851 do_help = -1;
4853 break;
4855 case WM_SETFOCUS:
4856 w32_detect_focus_change (dpyinfo, &msg, &inev);
4858 dpyinfo->grabbed = 0;
4859 check_visibility = 1;
4860 break;
4862 case WM_KILLFOCUS:
4863 w32_detect_focus_change (dpyinfo, &msg, &inev);
4864 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4866 if (f)
4868 if (f == hlinfo->mouse_face_mouse_frame)
4870 /* If we move outside the frame, then we're
4871 certainly no longer on any text in the frame. */
4872 clear_mouse_face (hlinfo);
4873 hlinfo->mouse_face_mouse_frame = 0;
4876 /* Generate a nil HELP_EVENT to cancel a help-echo.
4877 Do it only if there's something to cancel.
4878 Otherwise, the startup message is cleared when
4879 the mouse leaves the frame. */
4880 if (any_help_event_p)
4881 do_help = -1;
4884 dpyinfo->grabbed = 0;
4885 check_visibility = 1;
4886 break;
4888 case WM_CLOSE:
4889 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4891 if (f)
4893 inev.kind = DELETE_WINDOW_EVENT;
4894 XSETFRAME (inev.frame_or_window, f);
4896 break;
4898 case WM_INITMENU:
4899 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4901 if (f)
4903 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4904 XSETFRAME (inev.frame_or_window, f);
4906 break;
4908 case WM_COMMAND:
4909 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4911 if (f)
4913 extern void menubar_selection_callback
4914 (struct frame *f, void * client_data);
4915 menubar_selection_callback (f, (void *)msg.msg.wParam);
4918 check_visibility = 1;
4919 break;
4921 case WM_DISPLAYCHANGE:
4922 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4924 if (f)
4926 dpyinfo->n_cbits = msg.msg.wParam;
4927 DebPrint (("display change: %d %d\n",
4928 (short) LOWORD (msg.msg.lParam),
4929 (short) HIWORD (msg.msg.lParam)));
4932 check_visibility = 1;
4933 break;
4935 #if HAVE_W32NOTIFY
4936 case WM_EMACS_FILENOTIFY:
4937 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4938 if (f)
4939 queue_notifications (&inev, &msg, f, &count);
4940 break;
4941 #endif
4943 default:
4944 /* Check for messages registered at runtime. */
4945 if (msg.msg.message == msh_mousewheel)
4947 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4948 msg.msg.message = WM_MOUSEWHEEL;
4949 prepend_msg (&msg);
4951 break;
4954 if (inev.kind != NO_EVENT)
4956 kbd_buffer_store_event_hold (&inev, hold_quit);
4957 count++;
4960 if (do_help
4961 && !(hold_quit && hold_quit->kind != NO_EVENT))
4963 Lisp_Object frame;
4965 if (f)
4966 XSETFRAME (frame, f);
4967 else
4968 frame = Qnil;
4970 if (do_help > 0)
4972 if (NILP (help_echo_string))
4974 help_echo_object = help_echo_window = Qnil;
4975 help_echo_pos = -1;
4978 any_help_event_p = 1;
4979 gen_help_event (help_echo_string, frame, help_echo_window,
4980 help_echo_object, help_echo_pos);
4982 else
4984 help_echo_string = Qnil;
4985 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4987 count++;
4991 /* If the focus was just given to an autoraising frame,
4992 raise it now. FIXME: handle more than one such frame. */
4993 if (dpyinfo->w32_pending_autoraise_frame)
4995 x_raise_frame (dpyinfo->w32_pending_autoraise_frame);
4996 dpyinfo->w32_pending_autoraise_frame = NULL;
4999 /* Check which frames are still visible, if we have enqueued any user
5000 events or been notified of events that may affect visibility. We
5001 do this here because there doesn't seem to be any direct
5002 notification from Windows that the visibility of a window has
5003 changed (at least, not in all cases). */
5004 if (count > 0 || check_visibility)
5006 Lisp_Object tail, frame;
5008 FOR_EACH_FRAME (tail, frame)
5010 struct frame *f = XFRAME (frame);
5011 /* The tooltip has been drawn already. Avoid the
5012 SET_FRAME_GARBAGED below. */
5013 if (EQ (frame, tip_frame))
5014 continue;
5016 /* Check "visible" frames and mark each as obscured or not.
5017 Note that visible is nonzero for unobscured and obscured
5018 frames, but zero for hidden and iconified frames. */
5019 if (FRAME_W32_P (f) && FRAME_VISIBLE_P (f))
5021 RECT clipbox;
5022 HDC hdc;
5023 bool obscured;
5025 enter_crit ();
5026 /* Query clipping rectangle for the entire window area
5027 (GetWindowDC), not just the client portion (GetDC).
5028 Otherwise, the scrollbars and menubar aren't counted as
5029 part of the visible area of the frame, and we may think
5030 the frame is obscured when really a scrollbar is still
5031 visible and gets WM_PAINT messages above. */
5032 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
5033 GetClipBox (hdc, &clipbox);
5034 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
5035 leave_crit ();
5037 obscured = FRAME_OBSCURED_P (f);
5039 if (clipbox.right == clipbox.left || clipbox.bottom == clipbox.top)
5041 /* Frame has become completely obscured so mark as such (we
5042 do this by setting visible to 2 so that FRAME_VISIBLE_P
5043 is still true, but redisplay will skip it). */
5044 SET_FRAME_VISIBLE (f, 2);
5046 if (!obscured)
5047 DebPrint (("frame %p (%s) obscured\n", f, SDATA (f->name)));
5049 else
5051 /* Frame is not obscured, so mark it as such. */
5052 SET_FRAME_VISIBLE (f, 1);
5054 if (obscured)
5056 SET_FRAME_GARBAGED (f);
5057 DebPrint (("obscured frame %p (%s) found to be visible\n",
5058 f, SDATA (f->name)));
5060 /* Force a redisplay sooner or later. */
5061 record_asynch_buffer_change ();
5068 unblock_input ();
5069 return count;
5074 /***********************************************************************
5075 Text Cursor
5076 ***********************************************************************/
5078 /* Set clipping for output in glyph row ROW. W is the window in which
5079 we operate. GC is the graphics context to set clipping in.
5081 ROW may be a text row or, e.g., a mode line. Text rows must be
5082 clipped to the interior of the window dedicated to text display,
5083 mode lines must be clipped to the whole window. */
5085 static void
5086 w32_clip_to_row (struct window *w, struct glyph_row *row,
5087 enum glyph_row_area area, HDC hdc)
5089 RECT clip_rect;
5090 int window_x, window_y, window_width;
5092 window_box (w, area, &window_x, &window_y, &window_width, 0);
5094 clip_rect.left = window_x;
5095 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5096 clip_rect.top = max (clip_rect.top, window_y);
5097 clip_rect.right = clip_rect.left + window_width;
5098 clip_rect.bottom = clip_rect.top + row->visible_height;
5100 w32_set_clip_rectangle (hdc, &clip_rect);
5104 /* Draw a hollow box cursor on window W in glyph row ROW. */
5106 static void
5107 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
5109 struct frame *f = XFRAME (WINDOW_FRAME (w));
5110 HDC hdc;
5111 RECT rect;
5112 int left, top, h;
5113 struct glyph *cursor_glyph;
5114 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5116 /* Get the glyph the cursor is on. If we can't tell because
5117 the current matrix is invalid or such, give up. */
5118 cursor_glyph = get_phys_cursor_glyph (w);
5119 if (cursor_glyph == NULL)
5121 DeleteObject (hb);
5122 return;
5125 /* Compute frame-relative coordinates for phys cursor. */
5126 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
5127 rect.left = left;
5128 rect.top = top;
5129 rect.bottom = rect.top + h;
5130 rect.right = rect.left + w->phys_cursor_width;
5132 hdc = get_frame_dc (f);
5133 /* Set clipping, draw the rectangle, and reset clipping again. */
5134 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5135 FrameRect (hdc, &rect, hb);
5136 DeleteObject (hb);
5137 w32_set_clip_rectangle (hdc, NULL);
5138 release_frame_dc (f, hdc);
5142 /* Draw a bar cursor on window W in glyph row ROW.
5144 Implementation note: One would like to draw a bar cursor with an
5145 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5146 Unfortunately, I didn't find a font yet that has this property set.
5147 --gerd. */
5149 static void
5150 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
5151 int width, enum text_cursor_kinds kind)
5153 struct frame *f = XFRAME (w->frame);
5154 struct glyph *cursor_glyph;
5156 /* If cursor is out of bounds, don't draw garbage. This can happen
5157 in mini-buffer windows when switching between echo area glyphs
5158 and mini-buffer. */
5159 cursor_glyph = get_phys_cursor_glyph (w);
5160 if (cursor_glyph == NULL)
5161 return;
5163 /* If on an image, draw like a normal cursor. That's usually better
5164 visible than drawing a bar, esp. if the image is large so that
5165 the bar might not be in the window. */
5166 if (cursor_glyph->type == IMAGE_GLYPH)
5168 struct glyph_row *row;
5169 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5170 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5172 else
5174 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5175 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5176 int x;
5177 HDC hdc;
5179 /* If the glyph's background equals the color we normally draw
5180 the bar cursor in, the bar cursor in its normal color is
5181 invisible. Use the glyph's foreground color instead in this
5182 case, on the assumption that the glyph's colors are chosen so
5183 that the glyph is legible. */
5184 if (face->background == cursor_color)
5185 cursor_color = face->foreground;
5187 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5189 hdc = get_frame_dc (f);
5190 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5192 if (kind == BAR_CURSOR)
5194 if (width < 0)
5195 width = FRAME_CURSOR_WIDTH (f);
5196 width = min (cursor_glyph->pixel_width, width);
5198 w->phys_cursor_width = width;
5200 /* If the character under cursor is R2L, draw the bar cursor
5201 on the right of its glyph, rather than on the left. */
5202 if ((cursor_glyph->resolved_level & 1) != 0)
5203 x += cursor_glyph->pixel_width - width;
5205 w32_fill_area (f, hdc, cursor_color, x,
5206 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5207 width, row->height);
5209 else
5211 int dummy_x, dummy_y, dummy_h;
5213 if (width < 0)
5214 width = row->height;
5216 width = min (row->height, width);
5218 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5219 &dummy_y, &dummy_h);
5220 w32_fill_area (f, hdc, cursor_color, x,
5221 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5222 row->height - width),
5223 w->phys_cursor_width, width);
5226 w32_set_clip_rectangle (hdc, NULL);
5227 release_frame_dc (f, hdc);
5232 /* RIF: Define cursor CURSOR on frame F. */
5234 static void
5235 w32_define_frame_cursor (struct frame *f, Cursor cursor)
5237 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5241 /* RIF: Clear area on frame F. */
5243 static void
5244 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
5246 HDC hdc;
5248 hdc = get_frame_dc (f);
5249 w32_clear_area (f, hdc, x, y, width, height);
5250 release_frame_dc (f, hdc);
5253 /* RIF: Draw or clear cursor on window W. */
5255 static void
5256 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
5257 int x, int y, enum text_cursor_kinds cursor_type,
5258 int cursor_width, bool on_p, bool active_p)
5260 if (on_p)
5262 /* If the user wants to use the system caret, make sure our own
5263 cursor remains invisible. */
5264 if (w32_use_visible_system_caret)
5266 /* Call to erase_phys_cursor here seems to use the
5267 wrong values of w->phys_cursor, as they have been
5268 overwritten before this function was called. */
5269 if (w->phys_cursor_type != NO_CURSOR)
5270 erase_phys_cursor (w);
5272 cursor_type = w->phys_cursor_type = NO_CURSOR;
5273 w->phys_cursor_width = -1;
5275 else
5277 w->phys_cursor_type = cursor_type;
5280 w->phys_cursor_on_p = 1;
5282 /* If this is the active cursor, we need to track it with the
5283 system caret, so third party software like screen magnifiers
5284 and speech synthesizers can follow the cursor. */
5285 if (active_p)
5287 struct frame *f = XFRAME (WINDOW_FRAME (w));
5288 HWND hwnd = FRAME_W32_WINDOW (f);
5290 w32_system_caret_x
5291 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5292 w32_system_caret_y
5293 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5294 + glyph_row->ascent - w->phys_cursor_ascent);
5295 w32_system_caret_window = w;
5296 w32_system_caret_hdr_height = WINDOW_HEADER_LINE_HEIGHT (w);
5297 w32_system_caret_mode_height = WINDOW_MODE_LINE_HEIGHT (w);
5299 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
5301 /* If the size of the active cursor changed, destroy the old
5302 system caret. */
5303 if (w32_system_caret_hwnd
5304 && (w32_system_caret_height != w->phys_cursor_height))
5305 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5307 w32_system_caret_height = w->phys_cursor_height;
5309 /* Move the system caret. */
5310 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5313 if (glyph_row->exact_window_width_line_p
5314 && (glyph_row->reversed_p
5315 ? (w->phys_cursor.hpos < 0)
5316 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5318 glyph_row->cursor_in_fringe_p = 1;
5319 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5320 return;
5323 switch (cursor_type)
5325 case HOLLOW_BOX_CURSOR:
5326 x_draw_hollow_cursor (w, glyph_row);
5327 break;
5329 case FILLED_BOX_CURSOR:
5330 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5331 break;
5333 case BAR_CURSOR:
5334 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5335 break;
5337 case HBAR_CURSOR:
5338 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5339 break;
5341 case NO_CURSOR:
5342 w->phys_cursor_width = 0;
5343 break;
5345 default:
5346 emacs_abort ();
5353 /* Icons. */
5356 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5358 HANDLE main_icon;
5359 HANDLE small_icon = NULL;
5361 if (FRAME_W32_WINDOW (f) == 0)
5362 return 1;
5364 if (NILP (icon))
5365 main_icon = LoadIcon (hinst, EMACS_CLASS);
5366 else if (STRINGP (icon))
5368 /* Load the main icon from the named file. */
5369 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5370 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5371 /* Try to load a small icon to go with it. */
5372 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5373 GetSystemMetrics (SM_CXSMICON),
5374 GetSystemMetrics (SM_CYSMICON),
5375 LR_LOADFROMFILE);
5377 else if (SYMBOLP (icon))
5379 LPCTSTR name;
5381 if (EQ (icon, intern ("application")))
5382 name = (LPCTSTR) IDI_APPLICATION;
5383 else if (EQ (icon, intern ("hand")))
5384 name = (LPCTSTR) IDI_HAND;
5385 else if (EQ (icon, intern ("question")))
5386 name = (LPCTSTR) IDI_QUESTION;
5387 else if (EQ (icon, intern ("exclamation")))
5388 name = (LPCTSTR) IDI_EXCLAMATION;
5389 else if (EQ (icon, intern ("asterisk")))
5390 name = (LPCTSTR) IDI_ASTERISK;
5391 else if (EQ (icon, intern ("winlogo")))
5392 name = (LPCTSTR) IDI_WINLOGO;
5393 else
5394 return 1;
5396 main_icon = LoadIcon (NULL, name);
5398 else
5399 return 1;
5401 if (main_icon == NULL)
5402 return 1;
5404 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5405 (LPARAM) main_icon);
5407 /* If there is a small icon that goes with it, set that too. */
5408 if (small_icon)
5409 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5410 (LPARAM) small_icon);
5412 return 0;
5416 /************************************************************************
5417 Handling X errors
5418 ************************************************************************/
5420 /* Display Error Handling functions not used on W32. Listing them here
5421 helps diff stay in step when comparing w32term.c with xterm.c.
5423 x_error_catcher (display, error)
5424 x_catch_errors (dpy)
5425 x_catch_errors_unwind (old_val)
5426 x_check_errors (dpy, format)
5427 x_fully_uncatch_errors ()
5428 x_had_errors_p (dpy)
5429 x_clear_errors (dpy)
5430 x_uncatch_errors (dpy, count)
5431 x_trace_wire ()
5432 x_connection_signal (signalnum)
5433 x_connection_closed (dpy, error_message)
5434 x_error_quitter (display, error)
5435 x_error_handler (display, error)
5436 x_io_error_quitter (display)
5441 /* Changing the font of the frame. */
5443 Lisp_Object
5444 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
5446 struct font *font = XFONT_OBJECT (font_object);
5448 if (fontset < 0)
5449 fontset = fontset_from_font (font_object);
5450 FRAME_FONTSET (f) = fontset;
5451 if (FRAME_FONT (f) == font)
5452 /* This font is already set in frame F. There's nothing more to
5453 do. */
5454 return font_object;
5456 FRAME_FONT (f) = font;
5457 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5458 FRAME_COLUMN_WIDTH (f) = font->average_width;
5459 FRAME_LINE_HEIGHT (f) = font->height;
5461 compute_fringe_widths (f, 1);
5463 /* Compute the scroll bar width in character columns. */
5464 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5466 int wid = FRAME_COLUMN_WIDTH (f);
5467 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5468 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5470 else
5472 int wid = FRAME_COLUMN_WIDTH (f);
5473 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5476 /* Now make the frame display the given font. */
5477 if (FRAME_X_WINDOW (f) != 0)
5479 /* Don't change the size of a tip frame; there's no point in
5480 doing it because it's done in Fx_show_tip, and it leads to
5481 problems because the tip frame has no widget. */
5482 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5483 x_set_window_size (f, 0, FRAME_TEXT_WIDTH (f),
5484 FRAME_TEXT_HEIGHT (f), 1);
5487 /* X version sets font of input methods here also. */
5489 return font_object;
5493 /***********************************************************************
5494 TODO: W32 Input Methods
5495 ***********************************************************************/
5496 /* Listing missing functions from xterm.c helps diff stay in step.
5498 xim_destroy_callback (xim, client_data, call_data)
5499 xim_open_dpy (dpyinfo, resource_name)
5500 struct xim_inst_t
5501 xim_instantiate_callback (display, client_data, call_data)
5502 xim_initialize (dpyinfo, resource_name)
5503 xim_close_dpy (dpyinfo)
5508 /* Calculate the absolute position in frame F
5509 from its current recorded position values and gravity. */
5511 void
5512 x_calc_absolute_position (struct frame *f)
5514 int flags = f->size_hint_flags;
5516 /* The sum of the widths of the frame's left and right borders, and
5517 the sum of the heights of the frame's top and bottom borders (in
5518 pixels) drawn by Windows. */
5519 unsigned int left_right_borders_width, top_bottom_borders_height;
5521 /* Try to get the actual values of these two variables. We compute
5522 the border width (height) by subtracting the width (height) of
5523 the frame's client area from the width (height) of the frame's
5524 entire window. */
5525 WINDOWPLACEMENT wp = { 0 };
5526 RECT client_rect = { 0 };
5528 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5529 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5531 left_right_borders_width =
5532 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5533 (client_rect.right - client_rect.left);
5535 top_bottom_borders_height =
5536 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5537 (client_rect.bottom - client_rect.top);
5539 else
5541 /* Use sensible default values. */
5542 left_right_borders_width = 8;
5543 top_bottom_borders_height = 32;
5546 /* Treat negative positions as relative to the rightmost bottommost
5547 position that fits on the screen. */
5548 if (flags & XNegative)
5549 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
5550 - FRAME_PIXEL_WIDTH (f)
5551 + f->left_pos
5552 - (left_right_borders_width - 1));
5554 if (flags & YNegative)
5555 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
5556 - FRAME_PIXEL_HEIGHT (f)
5557 + f->top_pos
5558 - (top_bottom_borders_height - 1));
5560 /* The left_pos and top_pos are now relative to the top and left
5561 screen edges, so the flags should correspond. */
5562 f->size_hint_flags &= ~ (XNegative | YNegative);
5565 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5566 to really change the position, and 0 when calling from
5567 x_make_frame_visible (in that case, XOFF and YOFF are the current
5568 position values). It is -1 when calling from x_set_frame_parameters,
5569 which means, do adjust for borders but don't change the gravity. */
5571 void
5572 x_set_offset (struct frame *f, register int xoff, register int yoff,
5573 int change_gravity)
5575 int modified_top, modified_left;
5577 if (change_gravity > 0)
5579 f->top_pos = yoff;
5580 f->left_pos = xoff;
5581 f->size_hint_flags &= ~ (XNegative | YNegative);
5582 if (xoff < 0)
5583 f->size_hint_flags |= XNegative;
5584 if (yoff < 0)
5585 f->size_hint_flags |= YNegative;
5586 f->win_gravity = NorthWestGravity;
5588 x_calc_absolute_position (f);
5590 block_input ();
5591 x_wm_set_size_hint (f, (long) 0, 0);
5593 modified_left = f->left_pos;
5594 modified_top = f->top_pos;
5596 my_set_window_pos (FRAME_W32_WINDOW (f),
5597 NULL,
5598 modified_left, modified_top,
5599 0, 0,
5600 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5601 unblock_input ();
5605 /* Check if we need to resize the frame due to a fullscreen request.
5606 If so needed, resize the frame. */
5607 static void
5608 x_check_fullscreen (struct frame *f)
5610 if (f->want_fullscreen & FULLSCREEN_BOTH)
5612 int width, height, ign;
5614 x_real_positions (f, &f->left_pos, &f->top_pos);
5616 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5618 /* We do not need to move the window, it shall be taken care of
5619 when setting WM manager hints. */
5620 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5622 change_frame_size (f, width, height, 0, 1, 0, 0);
5623 SET_FRAME_GARBAGED (f);
5624 cancel_mouse_face (f);
5626 /* Wait for the change of frame size to occur. */
5627 f->want_fullscreen |= FULLSCREEN_WAIT;
5632 static void
5633 w32fullscreen_hook (struct frame *f)
5635 if (FRAME_VISIBLE_P (f))
5637 HWND hwnd = FRAME_W32_WINDOW(f);
5638 DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
5639 RECT rect;
5641 block_input();
5642 f->want_fullscreen &= ~FULLSCREEN_WAIT;
5644 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
5645 GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
5647 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
5649 SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
5650 SetWindowPos (hwnd, NULL, 0, 0, 0, 0,
5651 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
5652 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
5655 w32_fullscreen_rect (hwnd, f->want_fullscreen,
5656 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
5657 FRAME_PREV_FSMODE (f) = f->want_fullscreen;
5658 if (f->want_fullscreen == FULLSCREEN_MAXIMIZED)
5659 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, 0xf030, 0);
5660 else if (f->want_fullscreen == FULLSCREEN_BOTH)
5662 SetWindowLong (hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
5663 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
5664 rect.right - rect.left, rect.bottom - rect.top,
5665 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
5667 else
5669 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
5670 rect.right - rect.left, rect.bottom - rect.top, 0);
5673 f->want_fullscreen = FULLSCREEN_NONE;
5674 unblock_input ();
5676 else
5677 f->want_fullscreen |= FULLSCREEN_WAIT;
5680 /* Call this to change the size of frame F's x-window.
5681 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5682 for this size change and subsequent size changes.
5683 Otherwise we leave the window gravity unchanged. */
5685 void
5686 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
5688 int pixelwidth, pixelheight;
5690 block_input ();
5692 check_frame_size (f, &width, &height, pixelwise);
5693 f->scroll_bar_actual_width
5694 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5696 compute_fringe_widths (f, 0);
5698 if (pixelwise)
5700 pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
5701 pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
5703 else
5705 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width);
5706 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height);
5709 f->win_gravity = NorthWestGravity;
5710 x_wm_set_size_hint (f, (long) 0, 0);
5713 RECT rect;
5715 rect.left = rect.top = 0;
5716 rect.right = pixelwidth;
5717 rect.bottom = pixelheight;
5719 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5720 FRAME_EXTERNAL_MENU_BAR (f));
5722 my_set_window_pos (FRAME_W32_WINDOW (f),
5723 NULL,
5724 0, 0,
5725 rect.right - rect.left,
5726 rect.bottom - rect.top,
5727 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5730 #if 0
5731 /* The following mirrors what is done in xterm.c. It appears to be
5732 for informing lisp of the new size immediately, while the actual
5733 resize will happen asynchronously. But on Windows, the menu bar
5734 automatically wraps when the frame is too narrow to contain it,
5735 and that causes any calculations made here to come out wrong. The
5736 end is some nasty buggy behavior, including the potential loss
5737 of the minibuffer.
5739 Disabling this code is either not sufficient to fix the problems
5740 completely, or it causes fresh problems, but at least it removes
5741 the most problematic symptom of the minibuffer becoming unusable.
5743 -----------------------------------------------------------------
5745 Now, strictly speaking, we can't be sure that this is accurate,
5746 but the window manager will get around to dealing with the size
5747 change request eventually, and we'll hear how it went when the
5748 ConfigureNotify event gets here.
5750 We could just not bother storing any of this information here,
5751 and let the ConfigureNotify event set everything up, but that
5752 might be kind of confusing to the Lisp code, since size changes
5753 wouldn't be reported in the frame parameters until some random
5754 point in the future when the ConfigureNotify event arrives.
5756 We pass 1 for DELAY since we can't run Lisp code inside of
5757 a BLOCK_INPUT. */
5758 change_frame_size (f, width, height, 0, 1, 0, 0);
5759 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5760 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5762 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5763 receive in the ConfigureNotify event; if we get what we asked
5764 for, then the event won't cause the screen to become garbaged, so
5765 we have to make sure to do it here. */
5766 SET_FRAME_GARBAGED (f);
5768 /* If cursor was outside the new size, mark it as off. */
5769 mark_window_cursors_off (XWINDOW (f->root_window));
5771 /* Clear out any recollection of where the mouse highlighting was,
5772 since it might be in a place that's outside the new frame size.
5773 Actually checking whether it is outside is a pain in the neck,
5774 so don't try--just let the highlighting be done afresh with new size. */
5775 cancel_mouse_face (f);
5776 #endif
5778 unblock_input ();
5781 /* Mouse warping. */
5783 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5785 void
5786 x_set_mouse_position (struct frame *f, int x, int y)
5788 int pix_x, pix_y;
5790 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5791 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5793 if (pix_x < 0) pix_x = 0;
5794 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5796 if (pix_y < 0) pix_y = 0;
5797 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5799 x_set_mouse_pixel_position (f, pix_x, pix_y);
5802 void
5803 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
5805 RECT rect;
5806 POINT pt;
5808 block_input ();
5810 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5811 pt.x = rect.left + pix_x;
5812 pt.y = rect.top + pix_y;
5813 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5815 SetCursorPos (pt.x, pt.y);
5817 unblock_input ();
5821 /* focus shifting, raising and lowering. */
5823 void
5824 x_focus_frame (struct frame *f)
5826 struct w32_display_info *dpyinfo = &one_w32_display_info;
5828 /* Give input focus to frame. */
5829 block_input ();
5830 #if 0
5831 /* Try not to change its Z-order if possible. */
5832 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5833 my_set_focus (f, FRAME_W32_WINDOW (f));
5834 else
5835 #endif
5836 my_set_foreground_window (FRAME_W32_WINDOW (f));
5837 unblock_input ();
5840 /* Raise frame F. */
5841 void
5842 x_raise_frame (struct frame *f)
5844 block_input ();
5846 /* Strictly speaking, raise-frame should only change the frame's Z
5847 order, leaving input focus unchanged. This is reasonable behavior
5848 on X where the usual policy is point-to-focus. However, this
5849 behavior would be very odd on Windows where the usual policy is
5850 click-to-focus.
5852 On X, if the mouse happens to be over the raised frame, it gets
5853 input focus anyway (so the window with focus will never be
5854 completely obscured) - if not, then just moving the mouse over it
5855 is sufficient to give it focus. On Windows, the user must actually
5856 click on the frame (preferably the title bar so as not to move
5857 point), which is more awkward. Also, no other Windows program
5858 raises a window to the top but leaves another window (possibly now
5859 completely obscured) with input focus.
5861 Because there is a system setting on Windows that allows the user
5862 to choose the point to focus policy, we make the strict semantics
5863 optional, but by default we grab focus when raising. */
5865 if (NILP (Vw32_grab_focus_on_raise))
5867 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5868 not already the foreground application: the frame is raised
5869 above all other frames belonging to us, but not above the
5870 current top window. To achieve that, we have to resort to this
5871 more cumbersome method. */
5873 HDWP handle = BeginDeferWindowPos (2);
5874 if (handle)
5876 handle = DeferWindowPos (handle,
5877 FRAME_W32_WINDOW (f),
5878 HWND_TOP,
5879 0, 0, 0, 0,
5880 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5881 if (handle)
5883 handle = DeferWindowPos (handle,
5884 GetForegroundWindow (),
5885 FRAME_W32_WINDOW (f),
5886 0, 0, 0, 0,
5887 SWP_NOSIZE | SWP_NOMOVE |
5888 SWP_NOACTIVATE);
5889 if (handle)
5890 EndDeferWindowPos (handle);
5894 else
5896 my_bring_window_to_top (FRAME_W32_WINDOW (f));
5899 unblock_input ();
5902 /* Lower frame F. */
5903 void
5904 x_lower_frame (struct frame *f)
5906 block_input ();
5907 my_set_window_pos (FRAME_W32_WINDOW (f),
5908 HWND_BOTTOM,
5909 0, 0, 0, 0,
5910 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5911 unblock_input ();
5914 static void
5915 w32_frame_raise_lower (struct frame *f, int raise_flag)
5917 if (! FRAME_W32_P (f))
5918 return;
5920 if (raise_flag)
5921 x_raise_frame (f);
5922 else
5923 x_lower_frame (f);
5926 /* Change of visibility. */
5928 /* This tries to wait until the frame is really visible.
5929 However, if the window manager asks the user where to position
5930 the frame, this will return before the user finishes doing that.
5931 The frame will not actually be visible at that time,
5932 but it will become visible later when the window manager
5933 finishes with it. */
5935 void
5936 x_make_frame_visible (struct frame *f)
5938 block_input ();
5940 x_set_bitmap_icon (f);
5942 if (! FRAME_VISIBLE_P (f))
5944 /* We test FRAME_GARBAGED_P here to make sure we don't
5945 call x_set_offset a second time
5946 if we get to x_make_frame_visible a second time
5947 before the window gets really visible. */
5948 if (! FRAME_ICONIFIED_P (f)
5949 && ! f->output_data.w32->asked_for_visible)
5951 RECT workarea_rect;
5952 RECT window_rect;
5954 /* Adjust vertical window position in order to avoid being
5955 covered by a task bar placed at the bottom of the desktop. */
5956 SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
5957 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
5958 if (window_rect.bottom > workarea_rect.bottom
5959 && window_rect.top > workarea_rect.top)
5960 f->top_pos = max (window_rect.top
5961 - window_rect.bottom + workarea_rect.bottom,
5962 workarea_rect.top);
5964 x_set_offset (f, f->left_pos, f->top_pos, 0);
5967 f->output_data.w32->asked_for_visible = 1;
5969 /* According to a report in emacs-devel 2008-06-03, SW_SHOWNORMAL
5970 causes unexpected behavior when unminimizing frames that were
5971 previously maximized. But only SW_SHOWNORMAL works properly for
5972 frames that were truely hidden (using make-frame-invisible), so
5973 we need it to avoid Bug#5482. It seems that iconified is only
5974 set for minimized windows that are still visible, so use that to
5975 determine the appropriate flag to pass ShowWindow. */
5976 my_show_window (f, FRAME_W32_WINDOW (f),
5977 FRAME_ICONIFIED_P (f) ? SW_RESTORE : SW_SHOWNORMAL);
5980 /* Synchronize to ensure Emacs knows the frame is visible
5981 before we do anything else. We do this loop with input not blocked
5982 so that incoming events are handled. */
5984 Lisp_Object frame;
5985 int count;
5987 /* This must come after we set COUNT. */
5988 unblock_input ();
5990 XSETFRAME (frame, f);
5992 /* Wait until the frame is visible. Process X events until a
5993 MapNotify event has been seen, or until we think we won't get a
5994 MapNotify at all.. */
5995 for (count = input_signal_count + 10;
5996 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5998 /* Force processing of queued events. */
5999 /* TODO: x_sync equivalent? */
6001 /* Machines that do polling rather than SIGIO have been observed
6002 to go into a busy-wait here. So we'll fake an alarm signal
6003 to let the handler know that there's something to be read.
6004 We used to raise a real alarm, but it seems that the handler
6005 isn't always enabled here. This is probably a bug. */
6006 if (input_polling_used ())
6008 /* It could be confusing if a real alarm arrives while processing
6009 the fake one. Turn it off and let the handler reset it. */
6010 int old_poll_suppress_count = poll_suppress_count;
6011 poll_suppress_count = 1;
6012 poll_for_input_1 ();
6013 poll_suppress_count = old_poll_suppress_count;
6019 /* Change from mapped state to withdrawn state. */
6021 /* Make the frame visible (mapped and not iconified). */
6023 void
6024 x_make_frame_invisible (struct frame *f)
6026 /* Don't keep the highlight on an invisible frame. */
6027 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6028 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6030 block_input ();
6032 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
6034 /* We can't distinguish this from iconification
6035 just by the event that we get from the server.
6036 So we can't win using the usual strategy of letting
6037 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
6038 and synchronize with the server to make sure we agree. */
6039 SET_FRAME_VISIBLE (f, 0);
6040 SET_FRAME_ICONIFIED (f, 0);
6042 unblock_input ();
6045 /* Change window state from mapped to iconified. */
6047 void
6048 x_iconify_frame (struct frame *f)
6050 /* Don't keep the highlight on an invisible frame. */
6051 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6052 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6054 if (FRAME_ICONIFIED_P (f))
6055 return;
6057 block_input ();
6059 x_set_bitmap_icon (f);
6061 /* Simulate the user minimizing the frame. */
6062 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
6064 SET_FRAME_VISIBLE (f, 0);
6065 SET_FRAME_ICONIFIED (f, 1);
6067 unblock_input ();
6071 /* Free X resources of frame F. */
6073 void
6074 x_free_frame_resources (struct frame *f)
6076 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6077 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
6079 block_input ();
6081 /* We must free faces before destroying windows because some
6082 font-driver (e.g. xft) access a window while finishing a
6083 face. */
6084 if (FRAME_FACE_CACHE (f))
6085 free_frame_faces (f);
6087 if (FRAME_W32_WINDOW (f))
6088 my_destroy_window (f, FRAME_W32_WINDOW (f));
6090 free_frame_menubar (f);
6092 if (FRAME_FACE_CACHE (f))
6093 free_frame_faces (f);
6095 xfree (f->output_data.w32);
6096 f->output_data.w32 = NULL;
6098 if (f == dpyinfo->w32_focus_frame)
6099 dpyinfo->w32_focus_frame = 0;
6100 if (f == dpyinfo->w32_focus_event_frame)
6101 dpyinfo->w32_focus_event_frame = 0;
6102 if (f == dpyinfo->x_highlight_frame)
6103 dpyinfo->x_highlight_frame = 0;
6104 if (f == hlinfo->mouse_face_mouse_frame)
6105 reset_mouse_highlight (hlinfo);
6107 unblock_input ();
6111 /* Destroy the window of frame F. */
6112 void
6113 x_destroy_window (struct frame *f)
6115 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6117 x_free_frame_resources (f);
6118 dpyinfo->reference_count--;
6122 /* Setting window manager hints. */
6124 /* Set the normal size hints for the window manager, for frame F.
6125 FLAGS is the flags word to use--or 0 meaning preserve the flags
6126 that the window now has.
6127 If USER_POSITION, set the USPosition
6128 flag (this is useful when FLAGS is 0). */
6129 void
6130 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
6132 Window window = FRAME_W32_WINDOW (f);
6134 enter_crit ();
6136 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
6137 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
6138 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
6139 SetWindowLong (window, WND_SCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_WIDTH (f));
6141 leave_crit ();
6144 /***********************************************************************
6145 Fonts
6146 ***********************************************************************/
6148 #ifdef GLYPH_DEBUG
6150 /* Check that FONT is valid on frame F. It is if it can be found in F's
6151 font table. */
6153 static void
6154 x_check_font (struct frame *f, struct font *font)
6156 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
6157 if (font->driver->check)
6158 eassert (font->driver->check (f, font) == 0);
6161 #endif /* GLYPH_DEBUG */
6165 /***********************************************************************
6166 Initialization
6167 ***********************************************************************/
6169 static int w32_initialized = 0;
6171 void
6172 w32_initialize_display_info (Lisp_Object display_name)
6174 struct w32_display_info *dpyinfo = &one_w32_display_info;
6176 memset (dpyinfo, 0, sizeof (*dpyinfo));
6178 dpyinfo->name_list_element = Fcons (display_name, Qnil);
6179 dpyinfo->w32_id_name = xmalloc (SCHARS (Vinvocation_name)
6180 + SCHARS (Vsystem_name) + 2);
6181 sprintf (dpyinfo->w32_id_name, "%s@%s",
6182 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6184 /* Default Console mode values - overridden when running in GUI mode
6185 with values obtained from system metrics. */
6186 dpyinfo->resx = 1;
6187 dpyinfo->resy = 1;
6188 dpyinfo->n_planes = 1;
6189 dpyinfo->n_cbits = 4;
6190 dpyinfo->n_fonts = 0;
6191 dpyinfo->smallest_font_height = 1;
6192 dpyinfo->smallest_char_width = 1;
6193 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6194 /* TODO: dpyinfo->gray */
6196 reset_mouse_highlight (&dpyinfo->mouse_highlight);
6199 /* Create an xrdb-style database of resources to supersede registry settings.
6200 The database is just a concatenation of C strings, finished by an additional
6201 \0. The strings are submitted to some basic normalization, so
6203 [ *]option[ *]:[ *]value...
6205 becomes
6207 option:value...
6209 but any whitespace following value is not removed. */
6211 static char *
6212 w32_make_rdb (char *xrm_option)
6214 char *buffer = xmalloc (strlen (xrm_option) + 2);
6215 char *current = buffer;
6216 char ch;
6217 int in_option = 1;
6218 int before_value = 0;
6220 do {
6221 ch = *xrm_option++;
6223 if (ch == '\n')
6225 *current++ = '\0';
6226 in_option = 1;
6227 before_value = 0;
6229 else if (ch != ' ')
6231 *current++ = ch;
6232 if (in_option && (ch == ':'))
6234 in_option = 0;
6235 before_value = 1;
6237 else if (before_value)
6239 before_value = 0;
6242 else if (!(in_option || before_value))
6244 *current++ = ch;
6246 } while (ch);
6248 *current = '\0';
6250 return buffer;
6253 extern frame_parm_handler w32_frame_parm_handlers[];
6255 static struct redisplay_interface w32_redisplay_interface =
6257 w32_frame_parm_handlers,
6258 x_produce_glyphs,
6259 x_write_glyphs,
6260 x_insert_glyphs,
6261 x_clear_end_of_line,
6262 x_scroll_run,
6263 x_after_update_window_line,
6264 x_update_window_begin,
6265 x_update_window_end,
6266 0, /* flush_display */
6267 x_clear_window_mouse_face,
6268 x_get_glyph_overhangs,
6269 x_fix_overlapping_area,
6270 w32_draw_fringe_bitmap,
6271 w32_define_fringe_bitmap,
6272 w32_destroy_fringe_bitmap,
6273 w32_compute_glyph_string_overhangs,
6274 x_draw_glyph_string,
6275 w32_define_frame_cursor,
6276 w32_clear_frame_area,
6277 w32_draw_window_cursor,
6278 w32_draw_vertical_window_border,
6279 w32_draw_window_divider,
6280 w32_shift_glyphs_for_insert
6283 static void x_delete_terminal (struct terminal *term);
6285 static struct terminal *
6286 w32_create_terminal (struct w32_display_info *dpyinfo)
6288 struct terminal *terminal;
6290 terminal = create_terminal ();
6292 terminal->type = output_w32;
6293 terminal->display_info.w32 = dpyinfo;
6294 dpyinfo->terminal = terminal;
6296 /* MSVC does not type K&R functions with no arguments correctly, and
6297 so we must explicitly cast them. */
6298 terminal->clear_frame_hook = x_clear_frame;
6299 terminal->ins_del_lines_hook = x_ins_del_lines;
6300 terminal->delete_glyphs_hook = x_delete_glyphs;
6301 terminal->ring_bell_hook = w32_ring_bell;
6302 terminal->reset_terminal_modes_hook = NULL;
6303 terminal->set_terminal_modes_hook = NULL;
6304 terminal->update_begin_hook = x_update_begin;
6305 terminal->update_end_hook = x_update_end;
6306 terminal->set_terminal_window_hook = NULL;
6307 terminal->read_socket_hook = w32_read_socket;
6308 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6309 terminal->mouse_position_hook = w32_mouse_position;
6310 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6311 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6312 terminal->fullscreen_hook = w32fullscreen_hook;
6313 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6314 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6315 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6316 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6318 terminal->delete_frame_hook = x_destroy_window;
6319 terminal->delete_terminal_hook = x_delete_terminal;
6321 terminal->rif = &w32_redisplay_interface;
6323 /* We don't yet support separate terminals on W32, so don't try to share
6324 keyboards between virtual terminals that are on the same physical
6325 terminal like X does. */
6326 terminal->kboard = allocate_kboard (Qw32);
6327 /* Don't let the initial kboard remain current longer than necessary.
6328 That would cause problems if a file loaded on startup tries to
6329 prompt in the mini-buffer. */
6330 if (current_kboard == initial_kboard)
6331 current_kboard = terminal->kboard;
6332 terminal->kboard->reference_count++;
6334 return terminal;
6337 static void
6338 x_delete_terminal (struct terminal *terminal)
6340 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6342 /* Protect against recursive calls. delete_frame in
6343 delete_terminal calls us back when it deletes our last frame. */
6344 if (!terminal->name)
6345 return;
6347 block_input ();
6349 x_delete_display (dpyinfo);
6350 unblock_input ();
6353 struct w32_display_info *
6354 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
6356 struct w32_display_info *dpyinfo;
6357 struct terminal *terminal;
6358 HDC hdc;
6360 block_input ();
6362 if (!w32_initialized)
6364 w32_initialize ();
6365 w32_initialized = 1;
6368 w32_initialize_display_info (display_name);
6370 dpyinfo = &one_w32_display_info;
6371 terminal = w32_create_terminal (dpyinfo);
6373 /* Set the name of the terminal. */
6374 terminal->name = xlispstrdup (display_name);
6376 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6378 /* Put this display on the chain. */
6379 dpyinfo->next = x_display_list;
6380 x_display_list = dpyinfo;
6382 hdc = GetDC (NULL);
6384 dpyinfo->root_window = GetDesktopWindow ();
6385 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6386 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6387 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6388 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6389 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6390 ReleaseDC (NULL, hdc);
6392 /* initialize palette with white and black */
6394 XColor color;
6395 w32_defined_color (0, "white", &color, 1);
6396 w32_defined_color (0, "black", &color, 1);
6399 #ifdef WINDOWSNT
6400 /* Add the default keyboard. When !WINDOWSNT, we're using the
6401 standard Emacs console handling machinery and don't need an
6402 explicit FD here. */
6403 add_keyboard_wait_descriptor (0);
6404 #elif CYGWIN
6405 /* /dev/windows wakes us up when we have a thread message pending. */
6406 add_keyboard_wait_descriptor (w32_message_fd);
6407 #endif
6409 /* Create Fringe Bitmaps and store them for later use.
6411 On W32, bitmaps are all unsigned short, as Windows requires
6412 bitmap data to be Word aligned. For some reason they are
6413 horizontally reflected compared to how they appear on X, so we
6414 need to bitswap and convert to unsigned shorts before creating
6415 the bitmaps. */
6416 w32_init_fringe (terminal->rif);
6418 unblock_input ();
6420 return dpyinfo;
6423 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6424 void
6425 x_delete_display (struct w32_display_info *dpyinfo)
6427 /* FIXME: the only display info apparently can't be deleted. */
6428 /* free palette table */
6430 struct w32_palette_entry * plist;
6432 plist = dpyinfo->color_list;
6433 while (plist)
6435 struct w32_palette_entry * pentry = plist;
6436 plist = plist->next;
6437 xfree (pentry);
6439 dpyinfo->color_list = NULL;
6440 if (dpyinfo->palette)
6441 DeleteObject (dpyinfo->palette);
6443 xfree (dpyinfo->w32_id_name);
6445 w32_reset_fringes ();
6449 /* Set up use of W32. */
6451 DWORD WINAPI w32_msg_worker (void * arg);
6453 static void
6454 w32_initialize (void)
6456 HANDLE shell;
6457 HRESULT (WINAPI * set_user_model) (wchar_t * id);
6459 baud_rate = 19200;
6461 w32_system_caret_hwnd = NULL;
6462 w32_system_caret_height = 0;
6463 w32_system_caret_x = 0;
6464 w32_system_caret_y = 0;
6466 /* On Windows 7 and later, we need to set the user model ID
6467 to associate emacsclient launched files with Emacs frames
6468 in the UI. */
6469 shell = GetModuleHandle ("shell32.dll");
6470 if (shell)
6472 set_user_model
6473 = (void *) GetProcAddress (shell,
6474 "SetCurrentProcessExplicitAppUserModelID");
6476 /* If the function is defined, then we are running on Windows 7
6477 or newer, and the UI uses this to group related windows
6478 together. Since emacs, runemacs, emacsclient are related, we
6479 want them grouped even though the executables are different,
6480 so we need to set a consistent ID between them. */
6481 if (set_user_model)
6482 set_user_model (L"GNU.Emacs");
6485 #ifdef CYGWIN
6486 if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
6487 fatal ("opening /dev/windows: %s", strerror (errno));
6488 #endif /* CYGWIN */
6490 /* Initialize w32_use_visible_system_caret based on whether a screen
6491 reader is in use. */
6492 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6493 &w32_use_visible_system_caret, 0))
6494 w32_use_visible_system_caret = 0;
6496 last_tool_bar_item = -1;
6497 any_help_event_p = 0;
6499 /* Initialize input mode: interrupt_input off, no flow control, allow
6500 8 bit character input, standard quit char. */
6501 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6504 DWORD input_locale_id = ((DWORD_PTR) GetKeyboardLayout (0) & 0xffffffff);
6505 w32_keyboard_codepage =
6506 codepage_for_locale ((LCID) (input_locale_id & 0xffff));
6509 /* Create the window thread - it will terminate itself when the app
6510 terminates */
6511 init_crit ();
6513 dwMainThreadId = GetCurrentThreadId ();
6514 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6515 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6517 /* Wait for thread to start */
6519 MSG msg;
6521 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6523 hWindowsThread = CreateThread (NULL, 0,
6524 w32_msg_worker,
6525 0, 0, &dwWindowsThreadId);
6527 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6530 /* It is desirable that mainThread should have the same notion of
6531 focus window and active window as windowsThread. Unfortunately, the
6532 following call to AttachThreadInput, which should do precisely what
6533 we need, causes major problems when Emacs is linked as a console
6534 program. Unfortunately, we have good reasons for doing that, so
6535 instead we need to send messages to windowsThread to make some API
6536 calls for us (ones that affect, or depend on, the active/focus
6537 window state.) */
6538 #ifdef ATTACH_THREADS
6539 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6540 #endif
6542 /* Dynamically link to optional system components. */
6544 HMODULE user_lib = GetModuleHandle ("user32.dll");
6546 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6548 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6550 #undef LOAD_PROC
6552 /* Ensure scrollbar handle is at least 5 pixels. */
6553 vertical_scroll_bar_min_handle = 5;
6555 /* For either kind of scroll bar, take account of the arrows; these
6556 effectively form the border of the main scroll bar range. */
6557 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6558 = GetSystemMetrics (SM_CYVSCROLL);
6562 void
6563 syms_of_w32term (void)
6565 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6567 DEFSYM (Qadded, "added");
6568 DEFSYM (Qremoved, "removed");
6569 DEFSYM (Qmodified, "modified");
6570 DEFSYM (Qrenamed_from, "renamed-from");
6571 DEFSYM (Qrenamed_to, "renamed-to");
6573 DEFVAR_INT ("w32-num-mouse-buttons",
6574 w32_num_mouse_buttons,
6575 doc: /* Number of physical mouse buttons. */);
6576 w32_num_mouse_buttons = 2;
6578 DEFVAR_LISP ("w32-swap-mouse-buttons",
6579 Vw32_swap_mouse_buttons,
6580 doc: /* Swap the mapping of middle and right mouse buttons.
6581 When nil, middle button is mouse-2 and right button is mouse-3. */);
6582 Vw32_swap_mouse_buttons = Qnil;
6584 DEFVAR_LISP ("w32-grab-focus-on-raise",
6585 Vw32_grab_focus_on_raise,
6586 doc: /* Raised frame grabs input focus.
6587 When t, `raise-frame' grabs input focus as well. This fits well
6588 with the normal Windows click-to-focus policy, but might not be
6589 desirable when using a point-to-focus policy. */);
6590 Vw32_grab_focus_on_raise = Qt;
6592 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6593 Vw32_capslock_is_shiftlock,
6594 doc: /* Apply CapsLock state to non character input keys.
6595 When nil, CapsLock only affects normal character input keys. */);
6596 Vw32_capslock_is_shiftlock = Qnil;
6598 DEFVAR_LISP ("w32-recognize-altgr",
6599 Vw32_recognize_altgr,
6600 doc: /* Recognize right-alt and left-ctrl as AltGr.
6601 When nil, the right-alt and left-ctrl key combination is
6602 interpreted normally. */);
6603 Vw32_recognize_altgr = Qt;
6605 DEFVAR_BOOL ("w32-use-visible-system-caret",
6606 w32_use_visible_system_caret,
6607 doc: /* Flag to make the system caret visible.
6608 When this is non-nil, Emacs will indicate the position of point by
6609 using the system caret instead of drawing its own cursor. Some screen
6610 reader software does not track the system cursor properly when it is
6611 invisible, and gets confused by Emacs drawing its own cursor, so this
6612 variable is initialized to t when Emacs detects that screen reader
6613 software is running as it starts up.
6615 When this variable is set, other variables affecting the appearance of
6616 the cursor have no effect. */);
6618 w32_use_visible_system_caret = 0;
6620 /* We don't yet support this, but defining this here avoids whining
6621 from cus-start.el and other places, like "M-x set-variable". */
6622 DEFVAR_BOOL ("x-use-underline-position-properties",
6623 x_use_underline_position_properties,
6624 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
6625 A value of nil means ignore them. If you encounter fonts with bogus
6626 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6627 to 4.1, set this to nil. You can also use `underline-minimum-offset'
6628 to override the font's UNDERLINE_POSITION for small font display
6629 sizes. */);
6630 x_use_underline_position_properties = 0;
6632 DEFVAR_BOOL ("x-underline-at-descent-line",
6633 x_underline_at_descent_line,
6634 doc: /* Non-nil means to draw the underline at the same place as the descent line.
6635 A value of nil means to draw the underline according to the value of the
6636 variable `x-use-underline-position-properties', which is usually at the
6637 baseline level. The default value is nil. */);
6638 x_underline_at_descent_line = 0;
6640 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
6641 doc: /* Which toolkit scroll bars Emacs uses, if any.
6642 A value of nil means Emacs doesn't use toolkit scroll bars.
6643 With the X Window system, the value is a symbol describing the
6644 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
6645 With MS Windows or Nextstep, the value is t. */);
6646 Vx_toolkit_scroll_bars = Qt;
6648 /* Tell Emacs about this window system. */
6649 Fprovide (Qw32, Qnil);