Install the actual changes missing in previous revision
[emacs.git] / src / w32term.c
blob70625452d09461f82dabe9a222b61e8a47858385
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;
4228 block_input ();
4230 /* So people can tell when we have read the available input. */
4231 input_signal_count++;
4233 /* Process any incoming thread messages. */
4234 drain_message_queue ();
4236 /* TODO: ghostscript integration. */
4237 while (get_next_msg (&msg, FALSE))
4239 struct input_event inev;
4240 int do_help = 0;
4242 /* DebPrint (("w32_read_socket: %s time:%u\n", */
4243 /* w32_name_of_message (msg.msg.message), */
4244 /* msg.msg.time)); */
4246 EVENT_INIT (inev);
4247 inev.kind = NO_EVENT;
4248 inev.arg = Qnil;
4250 switch (msg.msg.message)
4252 case WM_EMACS_PAINT:
4253 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4255 if (f)
4257 if (msg.rect.right == msg.rect.left ||
4258 msg.rect.bottom == msg.rect.top)
4260 /* We may get paint messages even though the client
4261 area is clipped - these are not expose events. */
4262 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4263 SDATA (f->name)));
4265 else if (FRAME_VISIBLE_P (f) != 1)
4267 bool iconified = FRAME_ICONIFIED_P (f);
4269 /* Definitely not obscured, so mark as visible. */
4270 SET_FRAME_VISIBLE (f, 1);
4271 SET_FRAME_ICONIFIED (f, 0);
4272 SET_FRAME_GARBAGED (f);
4273 if (!f->output_data.w32->asked_for_visible)
4274 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4275 SDATA (f->name)));
4277 /* WM_PAINT serves as MapNotify as well, so report
4278 visibility changes properly. */
4279 if (iconified)
4281 inev.kind = DEICONIFY_EVENT;
4282 XSETFRAME (inev.frame_or_window, f);
4284 else if (!NILP (Vframe_list) && !NILP (XCDR (Vframe_list)))
4285 /* Force a redisplay sooner or later to update the
4286 frame titles in case this is the second frame. */
4287 record_asynch_buffer_change ();
4289 else
4291 HDC hdc = get_frame_dc (f);
4293 /* Erase background again for safety. */
4294 w32_clear_rect (f, hdc, &msg.rect);
4295 release_frame_dc (f, hdc);
4296 expose_frame (f,
4297 msg.rect.left,
4298 msg.rect.top,
4299 msg.rect.right - msg.rect.left,
4300 msg.rect.bottom - msg.rect.top);
4303 break;
4305 case WM_INPUTLANGCHANGE:
4306 /* Generate a language change event. */
4307 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4309 /* lParam contains the input language ID in its low 16 bits.
4310 Use it to update our record of the keyboard codepage. */
4311 w32_keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4312 & 0xffff));
4314 if (f)
4316 inev.kind = LANGUAGE_CHANGE_EVENT;
4317 XSETFRAME (inev.frame_or_window, f);
4318 inev.code = w32_keyboard_codepage;
4319 inev.modifiers = msg.msg.lParam & 0xffff;
4321 break;
4323 case WM_KEYDOWN:
4324 case WM_SYSKEYDOWN:
4325 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4327 if (f && !FRAME_ICONIFIED_P (f))
4329 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4330 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4332 clear_mouse_face (hlinfo);
4333 hlinfo->mouse_face_hidden = 1;
4336 if (temp_index == sizeof temp_buffer / sizeof (short))
4337 temp_index = 0;
4338 temp_buffer[temp_index++] = msg.msg.wParam;
4339 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4340 inev.code = msg.msg.wParam;
4341 inev.modifiers = msg.dwModifiers;
4342 XSETFRAME (inev.frame_or_window, f);
4343 inev.timestamp = msg.msg.time;
4345 break;
4347 case WM_UNICHAR:
4348 case WM_SYSCHAR:
4349 case WM_CHAR:
4350 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4352 if (f && !FRAME_ICONIFIED_P (f))
4354 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4355 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4357 clear_mouse_face (hlinfo);
4358 hlinfo->mouse_face_hidden = 1;
4361 if (temp_index == sizeof temp_buffer / sizeof (short))
4362 temp_index = 0;
4363 temp_buffer[temp_index++] = msg.msg.wParam;
4365 inev.modifiers = msg.dwModifiers;
4366 XSETFRAME (inev.frame_or_window, f);
4367 inev.timestamp = msg.msg.time;
4369 if (msg.msg.message == WM_UNICHAR)
4371 inev.code = msg.msg.wParam;
4373 else if (msg.msg.wParam < 256)
4375 wchar_t code;
4376 char dbcs[2];
4377 dbcs[0] = 0;
4378 dbcs[1] = (char) msg.msg.wParam;
4380 if (dbcs_lead)
4382 dbcs[0] = dbcs_lead;
4383 dbcs_lead = 0;
4384 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4385 dbcs, 2, &code, 1))
4387 /* Garbage */
4388 DebPrint (("Invalid DBCS sequence: %d %d\n",
4389 dbcs[0], dbcs[1]));
4390 inev.kind = NO_EVENT;
4391 break;
4394 else if (IsDBCSLeadByteEx (w32_keyboard_codepage,
4395 (BYTE) msg.msg.wParam))
4397 dbcs_lead = (char) msg.msg.wParam;
4398 inev.kind = NO_EVENT;
4399 break;
4401 else
4403 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4404 &dbcs[1], 1, &code, 1))
4406 /* What to do with garbage? */
4407 DebPrint (("Invalid character: %d\n", dbcs[1]));
4408 inev.kind = NO_EVENT;
4409 break;
4412 inev.code = code;
4414 else
4416 /* Windows shouldn't generate WM_CHAR events above 0xFF
4417 in non-Unicode message handlers. */
4418 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4419 inev.kind = NO_EVENT;
4420 break;
4422 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4423 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4425 break;
4427 case WM_APPCOMMAND:
4428 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4430 if (f && !FRAME_ICONIFIED_P (f))
4432 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4433 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4435 clear_mouse_face (hlinfo);
4436 hlinfo->mouse_face_hidden = 1;
4439 if (temp_index == sizeof temp_buffer / sizeof (short))
4440 temp_index = 0;
4441 temp_buffer[temp_index++] = msg.msg.wParam;
4442 inev.kind = MULTIMEDIA_KEY_EVENT;
4443 inev.code = GET_APPCOMMAND_LPARAM (msg.msg.lParam);
4444 inev.modifiers = msg.dwModifiers;
4445 XSETFRAME (inev.frame_or_window, f);
4446 inev.timestamp = msg.msg.time;
4448 break;
4450 case WM_MOUSEMOVE:
4451 /* Ignore non-movement. */
4453 int x = LOWORD (msg.msg.lParam);
4454 int y = HIWORD (msg.msg.lParam);
4455 if (x == last_mousemove_x && y == last_mousemove_y)
4456 break;
4457 last_mousemove_x = x;
4458 last_mousemove_y = y;
4461 previous_help_echo_string = help_echo_string;
4462 help_echo_string = Qnil;
4464 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4465 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4467 if (hlinfo->mouse_face_hidden)
4469 hlinfo->mouse_face_hidden = 0;
4470 clear_mouse_face (hlinfo);
4473 if (f)
4475 /* Generate SELECT_WINDOW_EVENTs when needed. */
4476 if (!NILP (Vmouse_autoselect_window))
4478 static Lisp_Object last_mouse_window;
4479 Lisp_Object window = window_from_coordinates
4480 (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0);
4482 /* Window will be selected only when it is not
4483 selected now and last mouse movement event was
4484 not in it. Minibuffer window will be selected
4485 only when it is active. */
4486 if (WINDOWP (window)
4487 && !EQ (window, last_mouse_window)
4488 && !EQ (window, selected_window)
4489 /* For click-to-focus window managers
4490 create event iff we don't leave the
4491 selected frame. */
4492 && (focus_follows_mouse
4493 || (EQ (XWINDOW (window)->frame,
4494 XWINDOW (selected_window)->frame))))
4496 inev.kind = SELECT_WINDOW_EVENT;
4497 inev.frame_or_window = window;
4499 /* Remember the last window where we saw the mouse. */
4500 last_mouse_window = window;
4502 if (!note_mouse_movement (f, &msg.msg))
4503 help_echo_string = previous_help_echo_string;
4505 else
4507 /* If we move outside the frame, then we're
4508 certainly no longer on any text in the frame. */
4509 clear_mouse_face (hlinfo);
4512 /* If the contents of the global variable help_echo_string
4513 has changed, generate a HELP_EVENT. */
4514 #if 0 /* The below is an invalid comparison when CHECK_LISP_OBJECT_TYPE.
4515 But it was originally changed to this to fix a bug, so I have
4516 not removed it completely in case the bug is still there. */
4517 if (help_echo_string != previous_help_echo_string ||
4518 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4519 #else /* This is what xterm.c does. */
4520 if (!NILP (help_echo_string)
4521 || !NILP (previous_help_echo_string))
4522 do_help = 1;
4523 #endif
4524 break;
4526 case WM_LBUTTONDOWN:
4527 case WM_LBUTTONUP:
4528 case WM_MBUTTONDOWN:
4529 case WM_MBUTTONUP:
4530 case WM_RBUTTONDOWN:
4531 case WM_RBUTTONUP:
4532 case WM_XBUTTONDOWN:
4533 case WM_XBUTTONUP:
4535 /* If we decide we want to generate an event to be seen
4536 by the rest of Emacs, we put it here. */
4537 bool tool_bar_p = 0;
4538 int button;
4539 int up;
4541 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4542 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4544 if (f)
4546 construct_mouse_click (&inev, &msg, f);
4548 /* Is this in the tool-bar? */
4549 if (WINDOWP (f->tool_bar_window)
4550 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4552 Lisp_Object window;
4553 int x = XFASTINT (inev.x);
4554 int y = XFASTINT (inev.y);
4556 window = window_from_coordinates (f, x, y, 0, 1);
4558 if (EQ (window, f->tool_bar_window))
4560 w32_handle_tool_bar_click (f, &inev);
4561 tool_bar_p = 1;
4565 if (tool_bar_p
4566 || (dpyinfo->w32_focus_frame
4567 && f != dpyinfo->w32_focus_frame))
4568 inev.kind = NO_EVENT;
4571 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4572 &button, &up);
4574 if (up)
4576 dpyinfo->grabbed &= ~ (1 << button);
4578 else
4580 dpyinfo->grabbed |= (1 << button);
4581 dpyinfo->last_mouse_frame = f;
4582 /* Ignore any mouse motion that happened
4583 before this event; any subsequent mouse-movement
4584 Emacs events should reflect only motion after
4585 the ButtonPress. */
4586 if (f != 0)
4587 f->mouse_moved = 0;
4589 if (!tool_bar_p)
4590 last_tool_bar_item = -1;
4592 break;
4595 case WM_MOUSEWHEEL:
4596 case WM_MOUSEHWHEEL:
4598 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4599 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4601 if (f)
4604 if (!dpyinfo->w32_focus_frame
4605 || f == dpyinfo->w32_focus_frame)
4607 /* Emit an Emacs wheel-up/down event. */
4608 construct_mouse_wheel (&inev, &msg, f);
4610 /* Ignore any mouse motion that happened before this
4611 event; any subsequent mouse-movement Emacs events
4612 should reflect only motion after the
4613 ButtonPress. */
4614 f->mouse_moved = 0;
4616 dpyinfo->last_mouse_frame = f;
4617 last_tool_bar_item = -1;
4619 break;
4621 case WM_DROPFILES:
4622 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4624 if (f)
4625 construct_drag_n_drop (&inev, &msg, f);
4626 break;
4628 case WM_VSCROLL:
4630 struct scroll_bar *bar =
4631 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4633 if (bar)
4634 w32_scroll_bar_handle_click (bar, &msg, &inev);
4635 break;
4638 case WM_WINDOWPOSCHANGED:
4639 case WM_ACTIVATE:
4640 case WM_ACTIVATEAPP:
4641 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4642 if (f)
4644 /* Run the full-screen hook function also when we are
4645 being activated, to actually install the required
4646 size in effect, if the WAIT flag is set. This is
4647 because when the hook is run from x_set_fullscreen,
4648 the frame might not yet be visible, if that call is a
4649 result of make-frame, and in that case the hook just
4650 sets the WAIT flag. */
4651 if ((msg.msg.message == WM_WINDOWPOSCHANGED || msg.msg.wParam)
4652 && (f->want_fullscreen & FULLSCREEN_WAIT))
4653 w32fullscreen_hook (f);
4654 x_check_fullscreen (f);
4656 check_visibility = 1;
4657 break;
4659 case WM_MOVE:
4660 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4662 if (f && !FRAME_ICONIFIED_P (f))
4663 x_real_positions (f, &f->left_pos, &f->top_pos);
4665 check_visibility = 1;
4666 break;
4668 case WM_SHOWWINDOW:
4669 /* wParam non-zero means Window is about to be shown, 0 means
4670 about to be hidden. */
4671 /* Redo the mouse-highlight after the tooltip has gone. */
4672 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4674 tip_window = NULL;
4675 x_redo_mouse_highlight (dpyinfo);
4678 /* If window has been obscured or exposed by another window
4679 being maximized or minimized/restored, then recheck
4680 visibility of all frames. Direct changes to our own
4681 windows get handled by WM_SIZE. */
4682 #if 0
4683 if (msg.msg.lParam != 0)
4684 check_visibility = 1;
4685 else
4687 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4688 f->async_visible = msg.msg.wParam;
4690 #endif
4692 check_visibility = 1;
4693 break;
4695 case WM_SIZE:
4696 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4698 /* Inform lisp of whether frame has been iconified etc. */
4699 if (f)
4701 switch (msg.msg.wParam)
4703 case SIZE_MINIMIZED:
4704 SET_FRAME_VISIBLE (f, 0);
4705 SET_FRAME_ICONIFIED (f, 1);
4707 inev.kind = ICONIFY_EVENT;
4708 XSETFRAME (inev.frame_or_window, f);
4709 break;
4711 case SIZE_MAXIMIZED:
4713 bool iconified = FRAME_ICONIFIED_P (f);
4715 SET_FRAME_VISIBLE (f, 1);
4716 SET_FRAME_ICONIFIED (f, 0);
4718 /* wait_reading_process_output will notice this
4719 and update the frame's display structures. */
4720 SET_FRAME_GARBAGED (f);
4722 if (iconified)
4724 int x, y;
4726 /* Reset top and left positions of the Window
4727 here since Windows sends a WM_MOVE message
4728 BEFORE telling us the Window is minimized
4729 when the Window is iconified, with 3000,3000
4730 as the co-ords. */
4731 x_real_positions (f, &x, &y);
4732 f->left_pos = x;
4733 f->top_pos = y;
4735 inev.kind = DEICONIFY_EVENT;
4736 XSETFRAME (inev.frame_or_window, f);
4738 else if (! NILP (Vframe_list)
4739 && ! NILP (XCDR (Vframe_list)))
4740 /* Force a redisplay sooner or later
4741 to update the frame titles
4742 in case this is the second frame. */
4743 record_asynch_buffer_change ();
4746 if (get_frame_param (f, Qfullscreen) == Qnil)
4747 set_frame_param (f, Qfullscreen, Qmaximized);
4748 else if (get_frame_param (f, Qfullscreen) != Qmaximized)
4749 set_frame_param (f, Qmaximized, Qmaximized);
4751 break;
4753 case SIZE_RESTORED:
4755 bool iconified = FRAME_ICONIFIED_P (f);
4757 if (iconified)
4758 SET_FRAME_VISIBLE (f, 1);
4759 SET_FRAME_ICONIFIED (f, 0);
4761 /* wait_reading_process_output will notice this
4762 and update the frame's display structures. */
4763 SET_FRAME_GARBAGED (f);
4765 if (iconified)
4767 /* Reset top and left positions of the Window
4768 here since Windows sends a WM_MOVE message
4769 BEFORE telling us the Window is minimized
4770 when the Window is iconified, with 3000,3000
4771 as the co-ords. */
4772 x_real_positions (f, &f->left_pos, &f->top_pos);
4774 inev.kind = DEICONIFY_EVENT;
4775 XSETFRAME (inev.frame_or_window, f);
4777 else if (! NILP (Vframe_list)
4778 && ! NILP (XCDR (Vframe_list)))
4779 /* Force a redisplay sooner or later
4780 to update the frame titles
4781 in case this is the second frame. */
4782 record_asynch_buffer_change ();
4785 if (get_frame_param (f, Qfullscreen) == Qmaximized)
4786 set_frame_param (f, Qfullscreen, Qnil);
4787 else if (get_frame_param (f, Qmaximized) != Qnil)
4788 set_frame_param (f, Qmaximized, Qnil);
4790 break;
4794 if (f && !FRAME_ICONIFIED_P (f) && msg.msg.wParam != SIZE_MINIMIZED)
4796 RECT rect;
4797 int rows, columns, width, height, text_width, text_height;
4799 GetClientRect (msg.msg.hwnd, &rect);
4801 height = rect.bottom - rect.top;
4802 width = rect.right - rect.left;
4803 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
4804 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
4805 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4806 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4808 /* TODO: Clip size to the screen dimensions. */
4810 /* Even if the number of character rows and columns has
4811 not changed, the font size may have changed, so we need
4812 to check the pixel dimensions as well. */
4814 if (width != FRAME_PIXEL_WIDTH (f)
4815 || height != FRAME_PIXEL_HEIGHT (f)
4816 || text_width != FRAME_TEXT_WIDTH (f)
4817 || text_height != FRAME_TEXT_HEIGHT (f))
4819 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
4820 SET_FRAME_GARBAGED (f);
4821 cancel_mouse_face (f);
4822 /* Do we want to set these here ???? */
4823 /** FRAME_PIXEL_WIDTH (f) = width; **/
4824 /** FRAME_TEXT_WIDTH (f) = text_width; **/
4825 /** FRAME_PIXEL_HEIGHT (f) = height; **/
4826 f->win_gravity = NorthWestGravity;
4830 check_visibility = 1;
4831 break;
4833 case WM_MOUSELEAVE:
4834 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4835 if (f)
4837 if (f == hlinfo->mouse_face_mouse_frame)
4839 /* If we move outside the frame, then we're
4840 certainly no longer on any text in the frame. */
4841 clear_mouse_face (hlinfo);
4842 hlinfo->mouse_face_mouse_frame = 0;
4845 /* Generate a nil HELP_EVENT to cancel a help-echo.
4846 Do it only if there's something to cancel.
4847 Otherwise, the startup message is cleared when
4848 the mouse leaves the frame. */
4849 if (any_help_event_p)
4850 do_help = -1;
4852 break;
4854 case WM_SETFOCUS:
4855 w32_detect_focus_change (dpyinfo, &msg, &inev);
4857 dpyinfo->grabbed = 0;
4858 check_visibility = 1;
4859 break;
4861 case WM_KILLFOCUS:
4862 w32_detect_focus_change (dpyinfo, &msg, &inev);
4863 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4865 if (f)
4867 if (f == hlinfo->mouse_face_mouse_frame)
4869 /* If we move outside the frame, then we're
4870 certainly no longer on any text in the frame. */
4871 clear_mouse_face (hlinfo);
4872 hlinfo->mouse_face_mouse_frame = 0;
4875 /* Generate a nil HELP_EVENT to cancel a help-echo.
4876 Do it only if there's something to cancel.
4877 Otherwise, the startup message is cleared when
4878 the mouse leaves the frame. */
4879 if (any_help_event_p)
4880 do_help = -1;
4883 dpyinfo->grabbed = 0;
4884 check_visibility = 1;
4885 break;
4887 case WM_CLOSE:
4888 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4890 if (f)
4892 inev.kind = DELETE_WINDOW_EVENT;
4893 XSETFRAME (inev.frame_or_window, f);
4895 break;
4897 case WM_INITMENU:
4898 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4900 if (f)
4902 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4903 XSETFRAME (inev.frame_or_window, f);
4905 break;
4907 case WM_COMMAND:
4908 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4910 if (f)
4912 extern void menubar_selection_callback
4913 (struct frame *f, void * client_data);
4914 menubar_selection_callback (f, (void *)msg.msg.wParam);
4917 check_visibility = 1;
4918 break;
4920 case WM_DISPLAYCHANGE:
4921 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4923 if (f)
4925 dpyinfo->n_cbits = msg.msg.wParam;
4926 DebPrint (("display change: %d %d\n",
4927 (short) LOWORD (msg.msg.lParam),
4928 (short) HIWORD (msg.msg.lParam)));
4931 check_visibility = 1;
4932 break;
4934 #if HAVE_W32NOTIFY
4935 case WM_EMACS_FILENOTIFY:
4936 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4937 if (f)
4938 queue_notifications (&inev, &msg, f, &count);
4939 break;
4940 #endif
4942 default:
4943 /* Check for messages registered at runtime. */
4944 if (msg.msg.message == msh_mousewheel)
4946 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4947 msg.msg.message = WM_MOUSEWHEEL;
4948 prepend_msg (&msg);
4950 break;
4953 if (inev.kind != NO_EVENT)
4955 kbd_buffer_store_event_hold (&inev, hold_quit);
4956 count++;
4959 if (do_help
4960 && !(hold_quit && hold_quit->kind != NO_EVENT))
4962 Lisp_Object frame;
4964 if (f)
4965 XSETFRAME (frame, f);
4966 else
4967 frame = Qnil;
4969 if (do_help > 0)
4971 if (NILP (help_echo_string))
4973 help_echo_object = help_echo_window = Qnil;
4974 help_echo_pos = -1;
4977 any_help_event_p = 1;
4978 gen_help_event (help_echo_string, frame, help_echo_window,
4979 help_echo_object, help_echo_pos);
4981 else
4983 help_echo_string = Qnil;
4984 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4986 count++;
4990 /* If the focus was just given to an autoraising frame,
4991 raise it now. FIXME: handle more than one such frame. */
4992 if (dpyinfo->w32_pending_autoraise_frame)
4994 x_raise_frame (dpyinfo->w32_pending_autoraise_frame);
4995 dpyinfo->w32_pending_autoraise_frame = NULL;
4998 /* Check which frames are still visible, if we have enqueued any user
4999 events or been notified of events that may affect visibility. We
5000 do this here because there doesn't seem to be any direct
5001 notification from Windows that the visibility of a window has
5002 changed (at least, not in all cases). */
5003 if (count > 0 || check_visibility)
5005 Lisp_Object tail, frame;
5007 FOR_EACH_FRAME (tail, frame)
5009 struct frame *f = XFRAME (frame);
5010 /* The tooltip has been drawn already. Avoid the
5011 SET_FRAME_GARBAGED below. */
5012 if (EQ (frame, tip_frame))
5013 continue;
5015 /* Check "visible" frames and mark each as obscured or not.
5016 Note that visible is nonzero for unobscured and obscured
5017 frames, but zero for hidden and iconified frames. */
5018 if (FRAME_W32_P (f) && FRAME_VISIBLE_P (f))
5020 RECT clipbox;
5021 HDC hdc;
5022 bool obscured;
5024 enter_crit ();
5025 /* Query clipping rectangle for the entire window area
5026 (GetWindowDC), not just the client portion (GetDC).
5027 Otherwise, the scrollbars and menubar aren't counted as
5028 part of the visible area of the frame, and we may think
5029 the frame is obscured when really a scrollbar is still
5030 visible and gets WM_PAINT messages above. */
5031 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
5032 GetClipBox (hdc, &clipbox);
5033 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
5034 leave_crit ();
5036 obscured = FRAME_OBSCURED_P (f);
5038 if (clipbox.right == clipbox.left || clipbox.bottom == clipbox.top)
5040 /* Frame has become completely obscured so mark as such (we
5041 do this by setting visible to 2 so that FRAME_VISIBLE_P
5042 is still true, but redisplay will skip it). */
5043 SET_FRAME_VISIBLE (f, 2);
5045 if (!obscured)
5046 DebPrint (("frame %p (%s) obscured\n", f, SDATA (f->name)));
5048 else
5050 /* Frame is not obscured, so mark it as such. */
5051 SET_FRAME_VISIBLE (f, 1);
5053 if (obscured)
5055 SET_FRAME_GARBAGED (f);
5056 DebPrint (("obscured frame %p (%s) found to be visible\n",
5057 f, SDATA (f->name)));
5059 /* Force a redisplay sooner or later. */
5060 record_asynch_buffer_change ();
5067 unblock_input ();
5068 return count;
5073 /***********************************************************************
5074 Text Cursor
5075 ***********************************************************************/
5077 /* Set clipping for output in glyph row ROW. W is the window in which
5078 we operate. GC is the graphics context to set clipping in.
5080 ROW may be a text row or, e.g., a mode line. Text rows must be
5081 clipped to the interior of the window dedicated to text display,
5082 mode lines must be clipped to the whole window. */
5084 static void
5085 w32_clip_to_row (struct window *w, struct glyph_row *row,
5086 enum glyph_row_area area, HDC hdc)
5088 RECT clip_rect;
5089 int window_x, window_y, window_width;
5091 window_box (w, area, &window_x, &window_y, &window_width, 0);
5093 clip_rect.left = window_x;
5094 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5095 clip_rect.top = max (clip_rect.top, window_y);
5096 clip_rect.right = clip_rect.left + window_width;
5097 clip_rect.bottom = clip_rect.top + row->visible_height;
5099 w32_set_clip_rectangle (hdc, &clip_rect);
5103 /* Draw a hollow box cursor on window W in glyph row ROW. */
5105 static void
5106 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
5108 struct frame *f = XFRAME (WINDOW_FRAME (w));
5109 HDC hdc;
5110 RECT rect;
5111 int left, top, h;
5112 struct glyph *cursor_glyph;
5113 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5115 /* Get the glyph the cursor is on. If we can't tell because
5116 the current matrix is invalid or such, give up. */
5117 cursor_glyph = get_phys_cursor_glyph (w);
5118 if (cursor_glyph == NULL)
5120 DeleteObject (hb);
5121 return;
5124 /* Compute frame-relative coordinates for phys cursor. */
5125 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
5126 rect.left = left;
5127 rect.top = top;
5128 rect.bottom = rect.top + h;
5129 rect.right = rect.left + w->phys_cursor_width;
5131 hdc = get_frame_dc (f);
5132 /* Set clipping, draw the rectangle, and reset clipping again. */
5133 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5134 FrameRect (hdc, &rect, hb);
5135 DeleteObject (hb);
5136 w32_set_clip_rectangle (hdc, NULL);
5137 release_frame_dc (f, hdc);
5141 /* Draw a bar cursor on window W in glyph row ROW.
5143 Implementation note: One would like to draw a bar cursor with an
5144 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5145 Unfortunately, I didn't find a font yet that has this property set.
5146 --gerd. */
5148 static void
5149 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
5150 int width, enum text_cursor_kinds kind)
5152 struct frame *f = XFRAME (w->frame);
5153 struct glyph *cursor_glyph;
5155 /* If cursor is out of bounds, don't draw garbage. This can happen
5156 in mini-buffer windows when switching between echo area glyphs
5157 and mini-buffer. */
5158 cursor_glyph = get_phys_cursor_glyph (w);
5159 if (cursor_glyph == NULL)
5160 return;
5162 /* If on an image, draw like a normal cursor. That's usually better
5163 visible than drawing a bar, esp. if the image is large so that
5164 the bar might not be in the window. */
5165 if (cursor_glyph->type == IMAGE_GLYPH)
5167 struct glyph_row *row;
5168 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5169 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5171 else
5173 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5174 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5175 int x;
5176 HDC hdc;
5178 /* If the glyph's background equals the color we normally draw
5179 the bar cursor in, the bar cursor in its normal color is
5180 invisible. Use the glyph's foreground color instead in this
5181 case, on the assumption that the glyph's colors are chosen so
5182 that the glyph is legible. */
5183 if (face->background == cursor_color)
5184 cursor_color = face->foreground;
5186 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5188 hdc = get_frame_dc (f);
5189 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5191 if (kind == BAR_CURSOR)
5193 if (width < 0)
5194 width = FRAME_CURSOR_WIDTH (f);
5195 width = min (cursor_glyph->pixel_width, width);
5197 w->phys_cursor_width = width;
5199 /* If the character under cursor is R2L, draw the bar cursor
5200 on the right of its glyph, rather than on the left. */
5201 if ((cursor_glyph->resolved_level & 1) != 0)
5202 x += cursor_glyph->pixel_width - width;
5204 w32_fill_area (f, hdc, cursor_color, x,
5205 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5206 width, row->height);
5208 else
5210 int dummy_x, dummy_y, dummy_h;
5212 if (width < 0)
5213 width = row->height;
5215 width = min (row->height, width);
5217 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5218 &dummy_y, &dummy_h);
5219 w32_fill_area (f, hdc, cursor_color, x,
5220 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5221 row->height - width),
5222 w->phys_cursor_width, width);
5225 w32_set_clip_rectangle (hdc, NULL);
5226 release_frame_dc (f, hdc);
5231 /* RIF: Define cursor CURSOR on frame F. */
5233 static void
5234 w32_define_frame_cursor (struct frame *f, Cursor cursor)
5236 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5240 /* RIF: Clear area on frame F. */
5242 static void
5243 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
5245 HDC hdc;
5247 hdc = get_frame_dc (f);
5248 w32_clear_area (f, hdc, x, y, width, height);
5249 release_frame_dc (f, hdc);
5252 /* RIF: Draw or clear cursor on window W. */
5254 static void
5255 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
5256 int x, int y, enum text_cursor_kinds cursor_type,
5257 int cursor_width, bool on_p, bool active_p)
5259 if (on_p)
5261 /* If the user wants to use the system caret, make sure our own
5262 cursor remains invisible. */
5263 if (w32_use_visible_system_caret)
5265 /* Call to erase_phys_cursor here seems to use the
5266 wrong values of w->phys_cursor, as they have been
5267 overwritten before this function was called. */
5268 if (w->phys_cursor_type != NO_CURSOR)
5269 erase_phys_cursor (w);
5271 cursor_type = w->phys_cursor_type = NO_CURSOR;
5272 w->phys_cursor_width = -1;
5274 else
5276 w->phys_cursor_type = cursor_type;
5279 w->phys_cursor_on_p = 1;
5281 /* If this is the active cursor, we need to track it with the
5282 system caret, so third party software like screen magnifiers
5283 and speech synthesizers can follow the cursor. */
5284 if (active_p)
5286 struct frame *f = XFRAME (WINDOW_FRAME (w));
5287 HWND hwnd = FRAME_W32_WINDOW (f);
5289 w32_system_caret_x
5290 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5291 w32_system_caret_y
5292 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5293 + glyph_row->ascent - w->phys_cursor_ascent);
5294 w32_system_caret_window = w;
5295 w32_system_caret_hdr_height = WINDOW_HEADER_LINE_HEIGHT (w);
5296 w32_system_caret_mode_height = WINDOW_MODE_LINE_HEIGHT (w);
5298 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
5300 /* If the size of the active cursor changed, destroy the old
5301 system caret. */
5302 if (w32_system_caret_hwnd
5303 && (w32_system_caret_height != w->phys_cursor_height))
5304 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5306 w32_system_caret_height = w->phys_cursor_height;
5308 /* Move the system caret. */
5309 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5312 if (glyph_row->exact_window_width_line_p
5313 && (glyph_row->reversed_p
5314 ? (w->phys_cursor.hpos < 0)
5315 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5317 glyph_row->cursor_in_fringe_p = 1;
5318 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5319 return;
5322 switch (cursor_type)
5324 case HOLLOW_BOX_CURSOR:
5325 x_draw_hollow_cursor (w, glyph_row);
5326 break;
5328 case FILLED_BOX_CURSOR:
5329 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5330 break;
5332 case BAR_CURSOR:
5333 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5334 break;
5336 case HBAR_CURSOR:
5337 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5338 break;
5340 case NO_CURSOR:
5341 w->phys_cursor_width = 0;
5342 break;
5344 default:
5345 emacs_abort ();
5352 /* Icons. */
5355 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5357 HANDLE main_icon;
5358 HANDLE small_icon = NULL;
5360 if (FRAME_W32_WINDOW (f) == 0)
5361 return 1;
5363 if (NILP (icon))
5364 main_icon = LoadIcon (hinst, EMACS_CLASS);
5365 else if (STRINGP (icon))
5367 /* Load the main icon from the named file. */
5368 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5369 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5370 /* Try to load a small icon to go with it. */
5371 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5372 GetSystemMetrics (SM_CXSMICON),
5373 GetSystemMetrics (SM_CYSMICON),
5374 LR_LOADFROMFILE);
5376 else if (SYMBOLP (icon))
5378 LPCTSTR name;
5380 if (EQ (icon, intern ("application")))
5381 name = (LPCTSTR) IDI_APPLICATION;
5382 else if (EQ (icon, intern ("hand")))
5383 name = (LPCTSTR) IDI_HAND;
5384 else if (EQ (icon, intern ("question")))
5385 name = (LPCTSTR) IDI_QUESTION;
5386 else if (EQ (icon, intern ("exclamation")))
5387 name = (LPCTSTR) IDI_EXCLAMATION;
5388 else if (EQ (icon, intern ("asterisk")))
5389 name = (LPCTSTR) IDI_ASTERISK;
5390 else if (EQ (icon, intern ("winlogo")))
5391 name = (LPCTSTR) IDI_WINLOGO;
5392 else
5393 return 1;
5395 main_icon = LoadIcon (NULL, name);
5397 else
5398 return 1;
5400 if (main_icon == NULL)
5401 return 1;
5403 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5404 (LPARAM) main_icon);
5406 /* If there is a small icon that goes with it, set that too. */
5407 if (small_icon)
5408 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5409 (LPARAM) small_icon);
5411 return 0;
5415 /************************************************************************
5416 Handling X errors
5417 ************************************************************************/
5419 /* Display Error Handling functions not used on W32. Listing them here
5420 helps diff stay in step when comparing w32term.c with xterm.c.
5422 x_error_catcher (display, error)
5423 x_catch_errors (dpy)
5424 x_catch_errors_unwind (old_val)
5425 x_check_errors (dpy, format)
5426 x_fully_uncatch_errors ()
5427 x_had_errors_p (dpy)
5428 x_clear_errors (dpy)
5429 x_uncatch_errors (dpy, count)
5430 x_trace_wire ()
5431 x_connection_signal (signalnum)
5432 x_connection_closed (dpy, error_message)
5433 x_error_quitter (display, error)
5434 x_error_handler (display, error)
5435 x_io_error_quitter (display)
5440 /* Changing the font of the frame. */
5442 Lisp_Object
5443 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
5445 struct font *font = XFONT_OBJECT (font_object);
5447 if (fontset < 0)
5448 fontset = fontset_from_font (font_object);
5449 FRAME_FONTSET (f) = fontset;
5450 if (FRAME_FONT (f) == font)
5451 /* This font is already set in frame F. There's nothing more to
5452 do. */
5453 return font_object;
5455 FRAME_FONT (f) = font;
5456 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5457 FRAME_COLUMN_WIDTH (f) = font->average_width;
5458 FRAME_LINE_HEIGHT (f) = font->height;
5460 compute_fringe_widths (f, 1);
5462 /* Compute the scroll bar width in character columns. */
5463 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5465 int wid = FRAME_COLUMN_WIDTH (f);
5466 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5467 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5469 else
5471 int wid = FRAME_COLUMN_WIDTH (f);
5472 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5475 /* Now make the frame display the given font. */
5476 if (FRAME_X_WINDOW (f) != 0)
5478 /* Don't change the size of a tip frame; there's no point in
5479 doing it because it's done in Fx_show_tip, and it leads to
5480 problems because the tip frame has no widget. */
5481 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5482 x_set_window_size (f, 0, FRAME_TEXT_WIDTH (f),
5483 FRAME_TEXT_HEIGHT (f), 1);
5486 /* X version sets font of input methods here also. */
5488 return font_object;
5492 /***********************************************************************
5493 TODO: W32 Input Methods
5494 ***********************************************************************/
5495 /* Listing missing functions from xterm.c helps diff stay in step.
5497 xim_destroy_callback (xim, client_data, call_data)
5498 xim_open_dpy (dpyinfo, resource_name)
5499 struct xim_inst_t
5500 xim_instantiate_callback (display, client_data, call_data)
5501 xim_initialize (dpyinfo, resource_name)
5502 xim_close_dpy (dpyinfo)
5507 /* Calculate the absolute position in frame F
5508 from its current recorded position values and gravity. */
5510 void
5511 x_calc_absolute_position (struct frame *f)
5513 int flags = f->size_hint_flags;
5515 /* The sum of the widths of the frame's left and right borders, and
5516 the sum of the heights of the frame's top and bottom borders (in
5517 pixels) drawn by Windows. */
5518 unsigned int left_right_borders_width, top_bottom_borders_height;
5520 /* Try to get the actual values of these two variables. We compute
5521 the border width (height) by subtracting the width (height) of
5522 the frame's client area from the width (height) of the frame's
5523 entire window. */
5524 WINDOWPLACEMENT wp = { 0 };
5525 RECT client_rect = { 0 };
5527 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5528 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5530 left_right_borders_width =
5531 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5532 (client_rect.right - client_rect.left);
5534 top_bottom_borders_height =
5535 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5536 (client_rect.bottom - client_rect.top);
5538 else
5540 /* Use sensible default values. */
5541 left_right_borders_width = 8;
5542 top_bottom_borders_height = 32;
5545 /* Treat negative positions as relative to the rightmost bottommost
5546 position that fits on the screen. */
5547 if (flags & XNegative)
5548 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
5549 - FRAME_PIXEL_WIDTH (f)
5550 + f->left_pos
5551 - (left_right_borders_width - 1));
5553 if (flags & YNegative)
5554 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
5555 - FRAME_PIXEL_HEIGHT (f)
5556 + f->top_pos
5557 - (top_bottom_borders_height - 1));
5559 /* The left_pos and top_pos are now relative to the top and left
5560 screen edges, so the flags should correspond. */
5561 f->size_hint_flags &= ~ (XNegative | YNegative);
5564 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5565 to really change the position, and 0 when calling from
5566 x_make_frame_visible (in that case, XOFF and YOFF are the current
5567 position values). It is -1 when calling from x_set_frame_parameters,
5568 which means, do adjust for borders but don't change the gravity. */
5570 void
5571 x_set_offset (struct frame *f, register int xoff, register int yoff,
5572 int change_gravity)
5574 int modified_top, modified_left;
5576 if (change_gravity > 0)
5578 f->top_pos = yoff;
5579 f->left_pos = xoff;
5580 f->size_hint_flags &= ~ (XNegative | YNegative);
5581 if (xoff < 0)
5582 f->size_hint_flags |= XNegative;
5583 if (yoff < 0)
5584 f->size_hint_flags |= YNegative;
5585 f->win_gravity = NorthWestGravity;
5587 x_calc_absolute_position (f);
5589 block_input ();
5590 x_wm_set_size_hint (f, (long) 0, 0);
5592 modified_left = f->left_pos;
5593 modified_top = f->top_pos;
5595 my_set_window_pos (FRAME_W32_WINDOW (f),
5596 NULL,
5597 modified_left, modified_top,
5598 0, 0,
5599 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5600 unblock_input ();
5604 /* Check if we need to resize the frame due to a fullscreen request.
5605 If so needed, resize the frame. */
5606 static void
5607 x_check_fullscreen (struct frame *f)
5609 if (f->want_fullscreen & FULLSCREEN_BOTH)
5611 int width, height, ign;
5613 x_real_positions (f, &f->left_pos, &f->top_pos);
5615 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5617 /* We do not need to move the window, it shall be taken care of
5618 when setting WM manager hints. */
5619 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5621 change_frame_size (f, width, height, 0, 1, 0, 0);
5622 SET_FRAME_GARBAGED (f);
5623 cancel_mouse_face (f);
5625 /* Wait for the change of frame size to occur. */
5626 f->want_fullscreen |= FULLSCREEN_WAIT;
5631 static void
5632 w32fullscreen_hook (struct frame *f)
5634 if (FRAME_VISIBLE_P (f))
5636 HWND hwnd = FRAME_W32_WINDOW(f);
5637 DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
5638 RECT rect;
5640 block_input();
5641 f->want_fullscreen &= ~FULLSCREEN_WAIT;
5643 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
5644 GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
5646 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
5648 SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
5649 SetWindowPos (hwnd, NULL, 0, 0, 0, 0,
5650 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
5651 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
5654 w32_fullscreen_rect (hwnd, f->want_fullscreen,
5655 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
5656 FRAME_PREV_FSMODE (f) = f->want_fullscreen;
5657 if (f->want_fullscreen == FULLSCREEN_MAXIMIZED)
5658 PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, 0xf030, 0);
5659 else if (f->want_fullscreen == FULLSCREEN_BOTH)
5661 SetWindowLong (hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
5662 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
5663 rect.right - rect.left, rect.bottom - rect.top,
5664 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
5666 else
5668 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
5669 rect.right - rect.left, rect.bottom - rect.top, 0);
5672 f->want_fullscreen = FULLSCREEN_NONE;
5673 unblock_input ();
5675 else
5676 f->want_fullscreen |= FULLSCREEN_WAIT;
5679 /* Call this to change the size of frame F's x-window.
5680 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5681 for this size change and subsequent size changes.
5682 Otherwise we leave the window gravity unchanged. */
5684 void
5685 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
5687 int pixelwidth, pixelheight;
5689 block_input ();
5691 check_frame_size (f, &width, &height, pixelwise);
5692 f->scroll_bar_actual_width
5693 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5695 compute_fringe_widths (f, 0);
5697 if (pixelwise)
5699 pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
5700 pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
5702 else
5704 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width);
5705 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height);
5708 f->win_gravity = NorthWestGravity;
5709 x_wm_set_size_hint (f, (long) 0, 0);
5712 RECT rect;
5714 rect.left = rect.top = 0;
5715 rect.right = pixelwidth;
5716 rect.bottom = pixelheight;
5718 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
5719 FRAME_EXTERNAL_MENU_BAR (f));
5721 my_set_window_pos (FRAME_W32_WINDOW (f),
5722 NULL,
5723 0, 0,
5724 rect.right - rect.left,
5725 rect.bottom - rect.top,
5726 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5729 #if 0
5730 /* The following mirrors what is done in xterm.c. It appears to be
5731 for informing lisp of the new size immediately, while the actual
5732 resize will happen asynchronously. But on Windows, the menu bar
5733 automatically wraps when the frame is too narrow to contain it,
5734 and that causes any calculations made here to come out wrong. The
5735 end is some nasty buggy behavior, including the potential loss
5736 of the minibuffer.
5738 Disabling this code is either not sufficient to fix the problems
5739 completely, or it causes fresh problems, but at least it removes
5740 the most problematic symptom of the minibuffer becoming unusable.
5742 -----------------------------------------------------------------
5744 Now, strictly speaking, we can't be sure that this is accurate,
5745 but the window manager will get around to dealing with the size
5746 change request eventually, and we'll hear how it went when the
5747 ConfigureNotify event gets here.
5749 We could just not bother storing any of this information here,
5750 and let the ConfigureNotify event set everything up, but that
5751 might be kind of confusing to the Lisp code, since size changes
5752 wouldn't be reported in the frame parameters until some random
5753 point in the future when the ConfigureNotify event arrives.
5755 We pass 1 for DELAY since we can't run Lisp code inside of
5756 a BLOCK_INPUT. */
5757 change_frame_size (f, width, height, 0, 1, 0, 0);
5758 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5759 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5761 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5762 receive in the ConfigureNotify event; if we get what we asked
5763 for, then the event won't cause the screen to become garbaged, so
5764 we have to make sure to do it here. */
5765 SET_FRAME_GARBAGED (f);
5767 /* If cursor was outside the new size, mark it as off. */
5768 mark_window_cursors_off (XWINDOW (f->root_window));
5770 /* Clear out any recollection of where the mouse highlighting was,
5771 since it might be in a place that's outside the new frame size.
5772 Actually checking whether it is outside is a pain in the neck,
5773 so don't try--just let the highlighting be done afresh with new size. */
5774 cancel_mouse_face (f);
5775 #endif
5777 unblock_input ();
5780 /* Mouse warping. */
5782 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5784 void
5785 x_set_mouse_position (struct frame *f, int x, int y)
5787 int pix_x, pix_y;
5789 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5790 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5792 if (pix_x < 0) pix_x = 0;
5793 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5795 if (pix_y < 0) pix_y = 0;
5796 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5798 x_set_mouse_pixel_position (f, pix_x, pix_y);
5801 void
5802 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
5804 RECT rect;
5805 POINT pt;
5807 block_input ();
5809 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5810 pt.x = rect.left + pix_x;
5811 pt.y = rect.top + pix_y;
5812 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5814 SetCursorPos (pt.x, pt.y);
5816 unblock_input ();
5820 /* focus shifting, raising and lowering. */
5822 void
5823 x_focus_frame (struct frame *f)
5825 struct w32_display_info *dpyinfo = &one_w32_display_info;
5827 /* Give input focus to frame. */
5828 block_input ();
5829 #if 0
5830 /* Try not to change its Z-order if possible. */
5831 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5832 my_set_focus (f, FRAME_W32_WINDOW (f));
5833 else
5834 #endif
5835 my_set_foreground_window (FRAME_W32_WINDOW (f));
5836 unblock_input ();
5839 /* Raise frame F. */
5840 void
5841 x_raise_frame (struct frame *f)
5843 block_input ();
5845 /* Strictly speaking, raise-frame should only change the frame's Z
5846 order, leaving input focus unchanged. This is reasonable behavior
5847 on X where the usual policy is point-to-focus. However, this
5848 behavior would be very odd on Windows where the usual policy is
5849 click-to-focus.
5851 On X, if the mouse happens to be over the raised frame, it gets
5852 input focus anyway (so the window with focus will never be
5853 completely obscured) - if not, then just moving the mouse over it
5854 is sufficient to give it focus. On Windows, the user must actually
5855 click on the frame (preferably the title bar so as not to move
5856 point), which is more awkward. Also, no other Windows program
5857 raises a window to the top but leaves another window (possibly now
5858 completely obscured) with input focus.
5860 Because there is a system setting on Windows that allows the user
5861 to choose the point to focus policy, we make the strict semantics
5862 optional, but by default we grab focus when raising. */
5864 if (NILP (Vw32_grab_focus_on_raise))
5866 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5867 not already the foreground application: the frame is raised
5868 above all other frames belonging to us, but not above the
5869 current top window. To achieve that, we have to resort to this
5870 more cumbersome method. */
5872 HDWP handle = BeginDeferWindowPos (2);
5873 if (handle)
5875 handle = DeferWindowPos (handle,
5876 FRAME_W32_WINDOW (f),
5877 HWND_TOP,
5878 0, 0, 0, 0,
5879 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5880 if (handle)
5882 handle = DeferWindowPos (handle,
5883 GetForegroundWindow (),
5884 FRAME_W32_WINDOW (f),
5885 0, 0, 0, 0,
5886 SWP_NOSIZE | SWP_NOMOVE |
5887 SWP_NOACTIVATE);
5888 if (handle)
5889 EndDeferWindowPos (handle);
5893 else
5895 my_bring_window_to_top (FRAME_W32_WINDOW (f));
5898 unblock_input ();
5901 /* Lower frame F. */
5902 void
5903 x_lower_frame (struct frame *f)
5905 block_input ();
5906 my_set_window_pos (FRAME_W32_WINDOW (f),
5907 HWND_BOTTOM,
5908 0, 0, 0, 0,
5909 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5910 unblock_input ();
5913 static void
5914 w32_frame_raise_lower (struct frame *f, int raise_flag)
5916 if (! FRAME_W32_P (f))
5917 return;
5919 if (raise_flag)
5920 x_raise_frame (f);
5921 else
5922 x_lower_frame (f);
5925 /* Change of visibility. */
5927 /* This tries to wait until the frame is really visible.
5928 However, if the window manager asks the user where to position
5929 the frame, this will return before the user finishes doing that.
5930 The frame will not actually be visible at that time,
5931 but it will become visible later when the window manager
5932 finishes with it. */
5934 void
5935 x_make_frame_visible (struct frame *f)
5937 block_input ();
5939 x_set_bitmap_icon (f);
5941 if (! FRAME_VISIBLE_P (f))
5943 /* We test FRAME_GARBAGED_P here to make sure we don't
5944 call x_set_offset a second time
5945 if we get to x_make_frame_visible a second time
5946 before the window gets really visible. */
5947 if (! FRAME_ICONIFIED_P (f)
5948 && ! f->output_data.w32->asked_for_visible)
5950 RECT workarea_rect;
5951 RECT window_rect;
5953 /* Adjust vertical window position in order to avoid being
5954 covered by a task bar placed at the bottom of the desktop. */
5955 SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
5956 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
5957 if (window_rect.bottom > workarea_rect.bottom
5958 && window_rect.top > workarea_rect.top)
5959 f->top_pos = max (window_rect.top
5960 - window_rect.bottom + workarea_rect.bottom,
5961 workarea_rect.top);
5963 x_set_offset (f, f->left_pos, f->top_pos, 0);
5966 f->output_data.w32->asked_for_visible = 1;
5968 /* According to a report in emacs-devel 2008-06-03, SW_SHOWNORMAL
5969 causes unexpected behavior when unminimizing frames that were
5970 previously maximized. But only SW_SHOWNORMAL works properly for
5971 frames that were truely hidden (using make-frame-invisible), so
5972 we need it to avoid Bug#5482. It seems that iconified is only
5973 set for minimized windows that are still visible, so use that to
5974 determine the appropriate flag to pass ShowWindow. */
5975 my_show_window (f, FRAME_W32_WINDOW (f),
5976 FRAME_ICONIFIED_P (f) ? SW_RESTORE : SW_SHOWNORMAL);
5979 /* Synchronize to ensure Emacs knows the frame is visible
5980 before we do anything else. We do this loop with input not blocked
5981 so that incoming events are handled. */
5983 Lisp_Object frame;
5984 int count;
5986 /* This must come after we set COUNT. */
5987 unblock_input ();
5989 XSETFRAME (frame, f);
5991 /* Wait until the frame is visible. Process X events until a
5992 MapNotify event has been seen, or until we think we won't get a
5993 MapNotify at all.. */
5994 for (count = input_signal_count + 10;
5995 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5997 /* Force processing of queued events. */
5998 /* TODO: x_sync equivalent? */
6000 /* Machines that do polling rather than SIGIO have been observed
6001 to go into a busy-wait here. So we'll fake an alarm signal
6002 to let the handler know that there's something to be read.
6003 We used to raise a real alarm, but it seems that the handler
6004 isn't always enabled here. This is probably a bug. */
6005 if (input_polling_used ())
6007 /* It could be confusing if a real alarm arrives while processing
6008 the fake one. Turn it off and let the handler reset it. */
6009 int old_poll_suppress_count = poll_suppress_count;
6010 poll_suppress_count = 1;
6011 poll_for_input_1 ();
6012 poll_suppress_count = old_poll_suppress_count;
6018 /* Change from mapped state to withdrawn state. */
6020 /* Make the frame visible (mapped and not iconified). */
6022 void
6023 x_make_frame_invisible (struct frame *f)
6025 /* Don't keep the highlight on an invisible frame. */
6026 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6027 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6029 block_input ();
6031 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
6033 /* We can't distinguish this from iconification
6034 just by the event that we get from the server.
6035 So we can't win using the usual strategy of letting
6036 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
6037 and synchronize with the server to make sure we agree. */
6038 SET_FRAME_VISIBLE (f, 0);
6039 SET_FRAME_ICONIFIED (f, 0);
6041 unblock_input ();
6044 /* Change window state from mapped to iconified. */
6046 void
6047 x_iconify_frame (struct frame *f)
6049 /* Don't keep the highlight on an invisible frame. */
6050 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6051 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6053 if (FRAME_ICONIFIED_P (f))
6054 return;
6056 block_input ();
6058 x_set_bitmap_icon (f);
6060 /* Simulate the user minimizing the frame. */
6061 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
6063 SET_FRAME_VISIBLE (f, 0);
6064 SET_FRAME_ICONIFIED (f, 1);
6066 unblock_input ();
6070 /* Free X resources of frame F. */
6072 void
6073 x_free_frame_resources (struct frame *f)
6075 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6076 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
6078 block_input ();
6080 /* We must free faces before destroying windows because some
6081 font-driver (e.g. xft) access a window while finishing a
6082 face. */
6083 if (FRAME_FACE_CACHE (f))
6084 free_frame_faces (f);
6086 if (FRAME_W32_WINDOW (f))
6087 my_destroy_window (f, FRAME_W32_WINDOW (f));
6089 free_frame_menubar (f);
6091 if (FRAME_FACE_CACHE (f))
6092 free_frame_faces (f);
6094 xfree (f->output_data.w32);
6095 f->output_data.w32 = NULL;
6097 if (f == dpyinfo->w32_focus_frame)
6098 dpyinfo->w32_focus_frame = 0;
6099 if (f == dpyinfo->w32_focus_event_frame)
6100 dpyinfo->w32_focus_event_frame = 0;
6101 if (f == dpyinfo->x_highlight_frame)
6102 dpyinfo->x_highlight_frame = 0;
6103 if (f == hlinfo->mouse_face_mouse_frame)
6104 reset_mouse_highlight (hlinfo);
6106 unblock_input ();
6110 /* Destroy the window of frame F. */
6111 void
6112 x_destroy_window (struct frame *f)
6114 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6116 x_free_frame_resources (f);
6117 dpyinfo->reference_count--;
6121 /* Setting window manager hints. */
6123 /* Set the normal size hints for the window manager, for frame F.
6124 FLAGS is the flags word to use--or 0 meaning preserve the flags
6125 that the window now has.
6126 If USER_POSITION, set the USPosition
6127 flag (this is useful when FLAGS is 0). */
6128 void
6129 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
6131 Window window = FRAME_W32_WINDOW (f);
6133 enter_crit ();
6135 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
6136 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
6137 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
6138 SetWindowLong (window, WND_SCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_WIDTH (f));
6140 leave_crit ();
6143 /***********************************************************************
6144 Fonts
6145 ***********************************************************************/
6147 #ifdef GLYPH_DEBUG
6149 /* Check that FONT is valid on frame F. It is if it can be found in F's
6150 font table. */
6152 static void
6153 x_check_font (struct frame *f, struct font *font)
6155 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
6156 if (font->driver->check)
6157 eassert (font->driver->check (f, font) == 0);
6160 #endif /* GLYPH_DEBUG */
6164 /***********************************************************************
6165 Initialization
6166 ***********************************************************************/
6168 static int w32_initialized = 0;
6170 void
6171 w32_initialize_display_info (Lisp_Object display_name)
6173 struct w32_display_info *dpyinfo = &one_w32_display_info;
6175 memset (dpyinfo, 0, sizeof (*dpyinfo));
6177 dpyinfo->name_list_element = Fcons (display_name, Qnil);
6178 dpyinfo->w32_id_name = xmalloc (SCHARS (Vinvocation_name)
6179 + SCHARS (Vsystem_name) + 2);
6180 sprintf (dpyinfo->w32_id_name, "%s@%s",
6181 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6183 /* Default Console mode values - overridden when running in GUI mode
6184 with values obtained from system metrics. */
6185 dpyinfo->resx = 1;
6186 dpyinfo->resy = 1;
6187 dpyinfo->n_planes = 1;
6188 dpyinfo->n_cbits = 4;
6189 dpyinfo->n_fonts = 0;
6190 dpyinfo->smallest_font_height = 1;
6191 dpyinfo->smallest_char_width = 1;
6192 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6193 /* TODO: dpyinfo->gray */
6195 reset_mouse_highlight (&dpyinfo->mouse_highlight);
6198 /* Create an xrdb-style database of resources to supersede registry settings.
6199 The database is just a concatenation of C strings, finished by an additional
6200 \0. The strings are submitted to some basic normalization, so
6202 [ *]option[ *]:[ *]value...
6204 becomes
6206 option:value...
6208 but any whitespace following value is not removed. */
6210 static char *
6211 w32_make_rdb (char *xrm_option)
6213 char *buffer = xmalloc (strlen (xrm_option) + 2);
6214 char *current = buffer;
6215 char ch;
6216 int in_option = 1;
6217 int before_value = 0;
6219 do {
6220 ch = *xrm_option++;
6222 if (ch == '\n')
6224 *current++ = '\0';
6225 in_option = 1;
6226 before_value = 0;
6228 else if (ch != ' ')
6230 *current++ = ch;
6231 if (in_option && (ch == ':'))
6233 in_option = 0;
6234 before_value = 1;
6236 else if (before_value)
6238 before_value = 0;
6241 else if (!(in_option || before_value))
6243 *current++ = ch;
6245 } while (ch);
6247 *current = '\0';
6249 return buffer;
6252 extern frame_parm_handler w32_frame_parm_handlers[];
6254 static struct redisplay_interface w32_redisplay_interface =
6256 w32_frame_parm_handlers,
6257 x_produce_glyphs,
6258 x_write_glyphs,
6259 x_insert_glyphs,
6260 x_clear_end_of_line,
6261 x_scroll_run,
6262 x_after_update_window_line,
6263 x_update_window_begin,
6264 x_update_window_end,
6265 0, /* flush_display */
6266 x_clear_window_mouse_face,
6267 x_get_glyph_overhangs,
6268 x_fix_overlapping_area,
6269 w32_draw_fringe_bitmap,
6270 w32_define_fringe_bitmap,
6271 w32_destroy_fringe_bitmap,
6272 w32_compute_glyph_string_overhangs,
6273 x_draw_glyph_string,
6274 w32_define_frame_cursor,
6275 w32_clear_frame_area,
6276 w32_draw_window_cursor,
6277 w32_draw_vertical_window_border,
6278 w32_draw_window_divider,
6279 w32_shift_glyphs_for_insert
6282 static void x_delete_terminal (struct terminal *term);
6284 static struct terminal *
6285 w32_create_terminal (struct w32_display_info *dpyinfo)
6287 struct terminal *terminal;
6289 terminal = create_terminal ();
6291 terminal->type = output_w32;
6292 terminal->display_info.w32 = dpyinfo;
6293 dpyinfo->terminal = terminal;
6295 /* MSVC does not type K&R functions with no arguments correctly, and
6296 so we must explicitly cast them. */
6297 terminal->clear_frame_hook = x_clear_frame;
6298 terminal->ins_del_lines_hook = x_ins_del_lines;
6299 terminal->delete_glyphs_hook = x_delete_glyphs;
6300 terminal->ring_bell_hook = w32_ring_bell;
6301 terminal->reset_terminal_modes_hook = NULL;
6302 terminal->set_terminal_modes_hook = NULL;
6303 terminal->update_begin_hook = x_update_begin;
6304 terminal->update_end_hook = x_update_end;
6305 terminal->set_terminal_window_hook = NULL;
6306 terminal->read_socket_hook = w32_read_socket;
6307 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6308 terminal->mouse_position_hook = w32_mouse_position;
6309 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6310 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6311 terminal->fullscreen_hook = w32fullscreen_hook;
6312 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6313 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6314 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6315 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6317 terminal->delete_frame_hook = x_destroy_window;
6318 terminal->delete_terminal_hook = x_delete_terminal;
6320 terminal->rif = &w32_redisplay_interface;
6322 /* We don't yet support separate terminals on W32, so don't try to share
6323 keyboards between virtual terminals that are on the same physical
6324 terminal like X does. */
6325 terminal->kboard = allocate_kboard (Qw32);
6326 /* Don't let the initial kboard remain current longer than necessary.
6327 That would cause problems if a file loaded on startup tries to
6328 prompt in the mini-buffer. */
6329 if (current_kboard == initial_kboard)
6330 current_kboard = terminal->kboard;
6331 terminal->kboard->reference_count++;
6333 return terminal;
6336 static void
6337 x_delete_terminal (struct terminal *terminal)
6339 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6341 /* Protect against recursive calls. delete_frame in
6342 delete_terminal calls us back when it deletes our last frame. */
6343 if (!terminal->name)
6344 return;
6346 block_input ();
6348 x_delete_display (dpyinfo);
6349 unblock_input ();
6352 struct w32_display_info *
6353 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
6355 struct w32_display_info *dpyinfo;
6356 struct terminal *terminal;
6357 HDC hdc;
6359 block_input ();
6361 if (!w32_initialized)
6363 w32_initialize ();
6364 w32_initialized = 1;
6367 w32_initialize_display_info (display_name);
6369 dpyinfo = &one_w32_display_info;
6370 terminal = w32_create_terminal (dpyinfo);
6372 /* Set the name of the terminal. */
6373 terminal->name = xlispstrdup (display_name);
6375 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6377 /* Put this display on the chain. */
6378 dpyinfo->next = x_display_list;
6379 x_display_list = dpyinfo;
6381 hdc = GetDC (NULL);
6383 dpyinfo->root_window = GetDesktopWindow ();
6384 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6385 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6386 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6387 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6388 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6389 ReleaseDC (NULL, hdc);
6391 /* initialize palette with white and black */
6393 XColor color;
6394 w32_defined_color (0, "white", &color, 1);
6395 w32_defined_color (0, "black", &color, 1);
6398 #ifdef WINDOWSNT
6399 /* Add the default keyboard. When !WINDOWSNT, we're using the
6400 standard Emacs console handling machinery and don't need an
6401 explicit FD here. */
6402 add_keyboard_wait_descriptor (0);
6403 #elif CYGWIN
6404 /* /dev/windows wakes us up when we have a thread message pending. */
6405 add_keyboard_wait_descriptor (w32_message_fd);
6406 #endif
6408 /* Create Fringe Bitmaps and store them for later use.
6410 On W32, bitmaps are all unsigned short, as Windows requires
6411 bitmap data to be Word aligned. For some reason they are
6412 horizontally reflected compared to how they appear on X, so we
6413 need to bitswap and convert to unsigned shorts before creating
6414 the bitmaps. */
6415 w32_init_fringe (terminal->rif);
6417 unblock_input ();
6419 return dpyinfo;
6422 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6423 void
6424 x_delete_display (struct w32_display_info *dpyinfo)
6426 /* FIXME: the only display info apparently can't be deleted. */
6427 /* free palette table */
6429 struct w32_palette_entry * plist;
6431 plist = dpyinfo->color_list;
6432 while (plist)
6434 struct w32_palette_entry * pentry = plist;
6435 plist = plist->next;
6436 xfree (pentry);
6438 dpyinfo->color_list = NULL;
6439 if (dpyinfo->palette)
6440 DeleteObject (dpyinfo->palette);
6442 xfree (dpyinfo->w32_id_name);
6444 w32_reset_fringes ();
6448 /* Set up use of W32. */
6450 DWORD WINAPI w32_msg_worker (void * arg);
6452 static void
6453 w32_initialize (void)
6455 HANDLE shell;
6456 HRESULT (WINAPI * set_user_model) (wchar_t * id);
6458 baud_rate = 19200;
6460 w32_system_caret_hwnd = NULL;
6461 w32_system_caret_height = 0;
6462 w32_system_caret_x = 0;
6463 w32_system_caret_y = 0;
6465 /* On Windows 7 and later, we need to set the user model ID
6466 to associate emacsclient launched files with Emacs frames
6467 in the UI. */
6468 shell = GetModuleHandle ("shell32.dll");
6469 if (shell)
6471 set_user_model
6472 = (void *) GetProcAddress (shell,
6473 "SetCurrentProcessExplicitAppUserModelID");
6475 /* If the function is defined, then we are running on Windows 7
6476 or newer, and the UI uses this to group related windows
6477 together. Since emacs, runemacs, emacsclient are related, we
6478 want them grouped even though the executables are different,
6479 so we need to set a consistent ID between them. */
6480 if (set_user_model)
6481 set_user_model (L"GNU.Emacs");
6484 #ifdef CYGWIN
6485 if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
6486 fatal ("opening /dev/windows: %s", strerror (errno));
6487 #endif /* CYGWIN */
6489 /* Initialize w32_use_visible_system_caret based on whether a screen
6490 reader is in use. */
6491 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6492 &w32_use_visible_system_caret, 0))
6493 w32_use_visible_system_caret = 0;
6495 last_tool_bar_item = -1;
6496 any_help_event_p = 0;
6498 /* Initialize input mode: interrupt_input off, no flow control, allow
6499 8 bit character input, standard quit char. */
6500 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6503 DWORD input_locale_id = ((DWORD_PTR) GetKeyboardLayout (0) & 0xffffffff);
6504 w32_keyboard_codepage =
6505 codepage_for_locale ((LCID) (input_locale_id & 0xffff));
6508 /* Create the window thread - it will terminate itself when the app
6509 terminates */
6510 init_crit ();
6512 dwMainThreadId = GetCurrentThreadId ();
6513 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6514 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6516 /* Wait for thread to start */
6518 MSG msg;
6520 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6522 hWindowsThread = CreateThread (NULL, 0,
6523 w32_msg_worker,
6524 0, 0, &dwWindowsThreadId);
6526 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6529 /* It is desirable that mainThread should have the same notion of
6530 focus window and active window as windowsThread. Unfortunately, the
6531 following call to AttachThreadInput, which should do precisely what
6532 we need, causes major problems when Emacs is linked as a console
6533 program. Unfortunately, we have good reasons for doing that, so
6534 instead we need to send messages to windowsThread to make some API
6535 calls for us (ones that affect, or depend on, the active/focus
6536 window state.) */
6537 #ifdef ATTACH_THREADS
6538 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6539 #endif
6541 /* Dynamically link to optional system components. */
6543 HMODULE user_lib = GetModuleHandle ("user32.dll");
6545 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6547 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6549 #undef LOAD_PROC
6551 /* Ensure scrollbar handle is at least 5 pixels. */
6552 vertical_scroll_bar_min_handle = 5;
6554 /* For either kind of scroll bar, take account of the arrows; these
6555 effectively form the border of the main scroll bar range. */
6556 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6557 = GetSystemMetrics (SM_CYVSCROLL);
6561 void
6562 syms_of_w32term (void)
6564 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6566 DEFSYM (Qadded, "added");
6567 DEFSYM (Qremoved, "removed");
6568 DEFSYM (Qmodified, "modified");
6569 DEFSYM (Qrenamed_from, "renamed-from");
6570 DEFSYM (Qrenamed_to, "renamed-to");
6572 DEFVAR_INT ("w32-num-mouse-buttons",
6573 w32_num_mouse_buttons,
6574 doc: /* Number of physical mouse buttons. */);
6575 w32_num_mouse_buttons = 2;
6577 DEFVAR_LISP ("w32-swap-mouse-buttons",
6578 Vw32_swap_mouse_buttons,
6579 doc: /* Swap the mapping of middle and right mouse buttons.
6580 When nil, middle button is mouse-2 and right button is mouse-3. */);
6581 Vw32_swap_mouse_buttons = Qnil;
6583 DEFVAR_LISP ("w32-grab-focus-on-raise",
6584 Vw32_grab_focus_on_raise,
6585 doc: /* Raised frame grabs input focus.
6586 When t, `raise-frame' grabs input focus as well. This fits well
6587 with the normal Windows click-to-focus policy, but might not be
6588 desirable when using a point-to-focus policy. */);
6589 Vw32_grab_focus_on_raise = Qt;
6591 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6592 Vw32_capslock_is_shiftlock,
6593 doc: /* Apply CapsLock state to non character input keys.
6594 When nil, CapsLock only affects normal character input keys. */);
6595 Vw32_capslock_is_shiftlock = Qnil;
6597 DEFVAR_LISP ("w32-recognize-altgr",
6598 Vw32_recognize_altgr,
6599 doc: /* Recognize right-alt and left-ctrl as AltGr.
6600 When nil, the right-alt and left-ctrl key combination is
6601 interpreted normally. */);
6602 Vw32_recognize_altgr = Qt;
6604 DEFVAR_BOOL ("w32-use-visible-system-caret",
6605 w32_use_visible_system_caret,
6606 doc: /* Flag to make the system caret visible.
6607 When this is non-nil, Emacs will indicate the position of point by
6608 using the system caret instead of drawing its own cursor. Some screen
6609 reader software does not track the system cursor properly when it is
6610 invisible, and gets confused by Emacs drawing its own cursor, so this
6611 variable is initialized to t when Emacs detects that screen reader
6612 software is running as it starts up.
6614 When this variable is set, other variables affecting the appearance of
6615 the cursor have no effect. */);
6617 w32_use_visible_system_caret = 0;
6619 /* We don't yet support this, but defining this here avoids whining
6620 from cus-start.el and other places, like "M-x set-variable". */
6621 DEFVAR_BOOL ("x-use-underline-position-properties",
6622 x_use_underline_position_properties,
6623 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
6624 A value of nil means ignore them. If you encounter fonts with bogus
6625 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6626 to 4.1, set this to nil. You can also use `underline-minimum-offset'
6627 to override the font's UNDERLINE_POSITION for small font display
6628 sizes. */);
6629 x_use_underline_position_properties = 0;
6631 DEFVAR_BOOL ("x-underline-at-descent-line",
6632 x_underline_at_descent_line,
6633 doc: /* Non-nil means to draw the underline at the same place as the descent line.
6634 A value of nil means to draw the underline according to the value of the
6635 variable `x-use-underline-position-properties', which is usually at the
6636 baseline level. The default value is nil. */);
6637 x_underline_at_descent_line = 0;
6639 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
6640 doc: /* Which toolkit scroll bars Emacs uses, if any.
6641 A value of nil means Emacs doesn't use toolkit scroll bars.
6642 With the X Window system, the value is a symbol describing the
6643 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
6644 With MS Windows or Nextstep, the value is t. */);
6645 Vx_toolkit_scroll_bars = Qt;
6647 /* Tell Emacs about this window system. */
6648 Fprovide (Qw32, Qnil);