Correct loop termination condition in c-syntactic-skip-backward.
[emacs.git] / src / w32term.c
blobdfda29fb903993fff5f4beacfa98ef0a167e8fbc
1 /* Implementation of GUI terminal on the Microsoft Windows API.
3 Copyright (C) 1989, 1993-2014 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"
53 #include "menu.h"
55 #ifdef WINDOWSNT
56 #include "w32.h" /* for filename_from_utf16, filename_from_ansi */
57 #include "w32heap.h"
58 #endif
60 #ifndef WINDOWSNT
61 #include <io.h> /* for get_osfhandle */
62 #endif
64 #include <shellapi.h>
66 #include "font.h"
67 #include "w32font.h"
69 /* Fringe bitmaps. */
71 static int max_fringe_bmp = 0;
72 static HBITMAP *fringe_bmp = 0;
74 /* Temporary variables for w32_read_socket. */
76 static int last_mousemove_x = 0;
77 static int last_mousemove_y = 0;
79 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
80 #ifndef GET_WHEEL_DELTA_WPARAM
81 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
82 #endif
84 /* Non-zero means that a HELP_EVENT has been generated since Emacs
85 start. */
87 static int any_help_event_p;
89 extern unsigned int msh_mousewheel;
91 extern void free_frame_menubar (struct frame *);
93 extern int w32_codepage_for_font (char *fontname);
94 extern Cursor w32_load_cursor (LPCTSTR name);
96 #define x_any_window_to_frame x_window_to_frame
97 #define x_top_window_to_frame x_window_to_frame
100 /* This is display since w32 does not support multiple ones. */
101 struct w32_display_info one_w32_display_info;
102 struct w32_display_info *x_display_list;
104 #if _WIN32_WINNT < 0x0500 && !defined(_W64)
105 /* Pre Windows 2000, this was not available, but define it here so
106 that Emacs compiled on such a platform will run on newer versions.
107 MinGW64 (_W64) defines these unconditionally, so avoid redefining. */
109 typedef struct tagWCRANGE
111 WCHAR wcLow;
112 USHORT cGlyphs;
113 } WCRANGE;
115 typedef struct tagGLYPHSET
117 DWORD cbThis;
118 DWORD flAccel;
119 DWORD cGlyphsSupported;
120 DWORD cRanges;
121 WCRANGE ranges[1];
122 } GLYPHSET;
124 #endif /* compiling for pre-Win2k */
126 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
127 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
129 #ifndef LWA_ALPHA
130 #define LWA_ALPHA 0x02
131 #endif
132 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
133 later targets by MSVC headers. */
134 #ifndef WS_EX_LAYERED
135 #define WS_EX_LAYERED 0x80000
136 #endif
138 /* SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN are not defined on 95 and
139 NT4. */
140 #ifndef SM_CXVIRTUALSCREEN
141 #define SM_CXVIRTUALSCREEN 78
142 #endif
143 #ifndef SM_CYVIRTUALSCREEN
144 #define SM_CYVIRTUALSCREEN 79
145 #endif
147 /* The handle of the frame that currently owns the system caret. */
148 HWND w32_system_caret_hwnd;
149 int w32_system_caret_height;
150 int w32_system_caret_x;
151 int w32_system_caret_y;
152 struct window *w32_system_caret_window;
153 int w32_system_caret_hdr_height;
154 int w32_system_caret_mode_height;
155 DWORD dwWindowsThreadId = 0;
156 HANDLE hWindowsThread = NULL;
157 DWORD dwMainThreadId = 0;
158 HANDLE hMainThread = NULL;
160 int vertical_scroll_bar_min_handle;
161 int horizontal_scroll_bar_min_handle;
162 int vertical_scroll_bar_top_border;
163 int vertical_scroll_bar_bottom_border;
164 int horizontal_scroll_bar_left_border;
165 int horizontal_scroll_bar_right_border;
167 int last_scroll_bar_drag_pos;
169 /* Keyboard code page - may be changed by language-change events. */
170 int w32_keyboard_codepage;
172 /* Incremented by w32_read_socket whenever it really tries to read
173 events. */
174 static int volatile input_signal_count;
176 #ifdef CYGWIN
177 int w32_message_fd = -1;
178 #endif /* CYGWIN */
180 static void w32_handle_tool_bar_click (struct frame *,
181 struct input_event *);
182 static void w32_define_cursor (Window, Cursor);
184 void x_lower_frame (struct frame *);
185 void x_scroll_bar_clear (struct frame *);
186 void x_wm_set_size_hint (struct frame *, long, bool);
187 void x_raise_frame (struct frame *);
188 void x_set_window_size (struct frame *, int, int, int, bool);
189 void x_wm_set_window_state (struct frame *, int);
190 void x_wm_set_icon_pixmap (struct frame *, int);
191 static void w32_initialize (void);
192 static void x_update_end (struct frame *);
193 static void w32_frame_up_to_date (struct frame *);
194 static void x_clear_frame (struct frame *);
195 static void frame_highlight (struct frame *);
196 static void frame_unhighlight (struct frame *);
197 static void x_new_focus_frame (struct w32_display_info *,
198 struct frame *);
199 static void x_focus_changed (int, int, struct w32_display_info *,
200 struct frame *, struct input_event *);
201 static void w32_detect_focus_change (struct w32_display_info *,
202 W32Msg *, struct input_event *);
203 static void w32_frame_rehighlight (struct frame *);
204 static void x_frame_rehighlight (struct w32_display_info *);
205 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
206 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
207 enum text_cursor_kinds);
208 static void w32_clip_to_row (struct window *, struct glyph_row *,
209 enum glyph_row_area, HDC);
210 static BOOL my_show_window (struct frame *, HWND, int);
211 static void my_set_window_pos (HWND, HWND, int, int, int, int, UINT);
212 #if 0
213 static void my_set_focus (struct frame *, HWND);
214 #endif
215 static void my_set_foreground_window (HWND);
216 static void my_destroy_window (struct frame *, HWND);
217 static void w32fullscreen_hook (struct frame *);
219 #ifdef GLYPH_DEBUG
220 static void x_check_font (struct frame *, struct font *);
221 #endif
223 static Lisp_Object Qvendor_specific_keysyms;
224 static Lisp_Object Qadded, Qremoved, Qmodified;
225 static Lisp_Object Qrenamed_from, Qrenamed_to;
228 /***********************************************************************
229 Debugging
230 ***********************************************************************/
232 #if 0
234 /* This is a function useful for recording debugging information about
235 the sequence of occurrences in this file. */
237 struct record
239 char *locus;
240 int type;
243 struct record event_record[100];
245 int event_record_index;
247 record_event (char *locus, int type)
249 if (event_record_index == sizeof (event_record) / sizeof (struct record))
250 event_record_index = 0;
252 event_record[event_record_index].locus = locus;
253 event_record[event_record_index].type = type;
254 event_record_index++;
257 #endif /* 0 */
260 void
261 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
262 XGCValues *xgcv)
264 if (mask & GCForeground)
265 gc->foreground = xgcv->foreground;
266 if (mask & GCBackground)
267 gc->background = xgcv->background;
268 if (mask & GCFont)
269 gc->font = xgcv->font;
272 XGCValues *
273 XCreateGC (void *ignore, Window window, unsigned long mask, XGCValues *xgcv)
275 XGCValues *gc = xzalloc (sizeof (XGCValues));
277 XChangeGC (ignore, gc, mask, xgcv);
279 return gc;
282 void
283 XGetGCValues (void *ignore, XGCValues *gc,
284 unsigned long mask, XGCValues *xgcv)
286 XChangeGC (ignore, xgcv, mask, gc);
289 static void
290 w32_set_clip_rectangle (HDC hdc, RECT *rect)
292 if (rect)
294 HRGN clip_region = CreateRectRgnIndirect (rect);
295 SelectClipRgn (hdc, clip_region);
296 DeleteObject (clip_region);
298 else
299 SelectClipRgn (hdc, NULL);
302 /* Restore clipping rectangle in S */
303 static void
304 w32_restore_glyph_string_clip (struct glyph_string *s)
306 RECT *r = s->clip;
307 int n = s->num_clips;
309 if (n == 1)
310 w32_set_clip_rectangle (s->hdc, r);
311 else if (n > 1)
313 HRGN clip1 = CreateRectRgnIndirect (r);
314 HRGN clip2 = CreateRectRgnIndirect (r + 1);
315 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
316 SelectClipRgn (s->hdc, clip1);
317 DeleteObject (clip1);
318 DeleteObject (clip2);
323 Draw a wavy line under S. The wave fills wave_height pixels from y0.
325 x0 wave_length = 2
327 y0 * * * * *
328 |* * * * * * * * *
329 wave_height = 3 | * * * *
333 void
334 w32_draw_underwave (struct glyph_string *s, COLORREF color)
336 int wave_height = 3, wave_length = 2;
337 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
338 XRectangle wave_clip, string_clip, final_clip;
339 RECT w32_final_clip, w32_string_clip;
340 HPEN hp, oldhp;
342 dx = wave_length;
343 dy = wave_height - 1;
344 x0 = s->x;
345 y0 = s->ybase - wave_height + 3;
346 width = s->width;
347 xmax = x0 + width;
349 /* Find and set clipping rectangle */
351 wave_clip.x = x0;
352 wave_clip.y = y0;
353 wave_clip.width = width;
354 wave_clip.height = wave_height;
356 get_glyph_string_clip_rect (s, &w32_string_clip);
357 CONVERT_TO_XRECT (string_clip, w32_string_clip);
359 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
360 return;
362 hp = CreatePen (PS_SOLID, 0, color);
363 oldhp = SelectObject (s->hdc, hp);
364 CONVERT_FROM_XRECT (final_clip, w32_final_clip);
365 w32_set_clip_rectangle (s->hdc, &w32_final_clip);
367 /* Draw the waves */
369 x1 = x0 - (x0 % dx);
370 x2 = x1 + dx;
371 odd = (x1/dx) % 2;
372 y1 = y2 = y0;
374 if (odd)
375 y1 += dy;
376 else
377 y2 += dy;
379 MoveToEx (s->hdc, x1, y1, NULL);
381 while (x1 <= xmax)
383 LineTo (s->hdc, x2, y2);
384 x1 = x2, y1 = y2;
385 x2 += dx, y2 = y0 + odd*dy;
386 odd = !odd;
389 /* Restore previous pen and clipping rectangle(s) */
390 w32_restore_glyph_string_clip (s);
391 SelectObject (s->hdc, oldhp);
392 DeleteObject (hp);
395 /* Draw a hollow rectangle at the specified position. */
396 void
397 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
398 int width, int height)
400 HBRUSH hb, oldhb;
401 HPEN hp, oldhp;
403 hb = CreateSolidBrush (gc->background);
404 hp = CreatePen (PS_SOLID, 0, gc->foreground);
405 oldhb = SelectObject (hdc, hb);
406 oldhp = SelectObject (hdc, hp);
408 Rectangle (hdc, x, y, x + width, y + height);
410 SelectObject (hdc, oldhb);
411 SelectObject (hdc, oldhp);
412 DeleteObject (hb);
413 DeleteObject (hp);
416 /* Draw a filled rectangle at the specified position. */
417 void
418 w32_fill_rect (struct frame *f, HDC hdc, COLORREF pix, RECT *lprect)
420 HBRUSH hb;
422 hb = CreateSolidBrush (pix);
423 FillRect (hdc, lprect, hb);
424 DeleteObject (hb);
427 void
428 w32_clear_window (struct frame *f)
430 RECT rect;
431 HDC hdc = get_frame_dc (f);
433 /* Under certain conditions, this can be called at startup with
434 a console frame pointer before the GUI frame is created. An HDC
435 of 0 indicates this. */
436 if (hdc)
438 GetClientRect (FRAME_W32_WINDOW (f), &rect);
439 w32_clear_rect (f, hdc, &rect);
442 release_frame_dc (f, hdc);
445 #define OPAQUE_FRAME 255
447 void
448 x_set_frame_alpha (struct frame *f)
450 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
451 double alpha = 1.0;
452 double alpha_min = 1.0;
453 BYTE opac;
454 LONG ex_style;
455 HWND window = FRAME_W32_WINDOW (f);
457 /* Older versions of Windows do not support transparency. */
458 if (!pfnSetLayeredWindowAttributes)
459 return;
461 if (dpyinfo->w32_focus_frame == f)
462 alpha = f->alpha[0];
463 else
464 alpha = f->alpha[1];
466 if (FLOATP (Vframe_alpha_lower_limit))
467 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
468 else if (INTEGERP (Vframe_alpha_lower_limit))
469 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
471 if (alpha < 0.0)
472 return;
473 else if (alpha > 1.0)
474 alpha = 1.0;
475 else if (alpha < alpha_min && alpha_min <= 1.0)
476 alpha = alpha_min;
478 opac = alpha * OPAQUE_FRAME;
480 ex_style = GetWindowLong (window, GWL_EXSTYLE);
482 if (opac == OPAQUE_FRAME)
483 ex_style &= ~WS_EX_LAYERED;
484 else
485 ex_style |= WS_EX_LAYERED;
487 SetWindowLong (window, GWL_EXSTYLE, ex_style);
489 if (opac != OPAQUE_FRAME)
490 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
494 x_display_pixel_height (struct w32_display_info *dpyinfo)
496 int pixels = GetSystemMetrics (SM_CYVIRTUALSCREEN);
498 if (pixels == 0)
499 /* Fallback for Windows 95 or NT 4.0. */
500 pixels = GetSystemMetrics (SM_CYSCREEN);
502 return pixels;
506 x_display_pixel_width (struct w32_display_info *dpyinfo)
508 int pixels = GetSystemMetrics (SM_CXVIRTUALSCREEN);
510 if (pixels == 0)
511 /* Fallback for Windows 95 or NT 4.0. */
512 pixels = GetSystemMetrics (SM_CXSCREEN);
514 return pixels;
518 /***********************************************************************
519 Starting and ending an update
520 ***********************************************************************/
522 /* Start an update of frame F. This function is installed as a hook
523 for update_begin, i.e. it is called when update_begin is called.
524 This function is called prior to calls to x_update_window_begin for
525 each window being updated. */
527 static void
528 x_update_begin (struct frame *f)
530 struct w32_display_info *display_info = FRAME_DISPLAY_INFO (f);
532 if (! FRAME_W32_P (f))
533 return;
535 /* Regenerate display palette before drawing if list of requested
536 colors has changed. */
537 if (display_info->regen_palette)
539 w32_regenerate_palette (f);
540 display_info->regen_palette = FALSE;
545 /* Start update of window W. */
547 static void
548 x_update_window_begin (struct window *w)
550 struct frame *f = XFRAME (WINDOW_FRAME (w));
551 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
553 /* Hide the system caret during an update. */
554 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
556 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
559 w->output_cursor = w->cursor;
561 block_input ();
563 if (f == hlinfo->mouse_face_mouse_frame)
565 /* Don't do highlighting for mouse motion during the update. */
566 hlinfo->mouse_face_defer = 1;
568 /* If F needs to be redrawn, simply forget about any prior mouse
569 highlighting. */
570 if (FRAME_GARBAGED_P (f))
571 hlinfo->mouse_face_window = Qnil;
573 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
574 their mouse_face_p flag set, which means that they are always
575 unequal to rows in a desired matrix which never have that
576 flag set. So, rows containing mouse-face glyphs are never
577 scrolled, and we don't have to switch the mouse highlight off
578 here to prevent it from being scrolled. */
580 /* Can we tell that this update does not affect the window
581 where the mouse highlight is? If so, no need to turn off.
582 Likewise, don't do anything if the frame is garbaged;
583 in that case, the frame's current matrix that we would use
584 is all wrong, and we will redisplay that line anyway. */
585 if (!NILP (hlinfo->mouse_face_window)
586 && w == XWINDOW (hlinfo->mouse_face_window))
588 int i;
590 for (i = 0; i < w->desired_matrix->nrows; ++i)
591 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
592 break;
594 if (i < w->desired_matrix->nrows)
595 clear_mouse_face (hlinfo);
597 #endif /* 0 */
600 unblock_input ();
603 /* Draw a vertical window border from (x,y0) to (x,y1) */
605 static void
606 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
608 struct frame *f = XFRAME (WINDOW_FRAME (w));
609 RECT r;
610 HDC hdc;
611 struct face *face;
613 r.left = x;
614 r.right = x + 1;
615 r.top = y0;
616 r.bottom = y1;
618 hdc = get_frame_dc (f);
619 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
620 if (face)
621 w32_fill_rect (f, hdc, face->foreground, &r);
622 else
623 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
625 release_frame_dc (f, hdc);
629 /* Draw a window divider from (x0, y0) to (x1, y1) */
631 static void
632 w32_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
634 struct frame *f = XFRAME (WINDOW_FRAME (w));
635 HDC hdc = get_frame_dc (f);
636 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
637 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
638 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
639 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
640 unsigned long color_first = (face_first
641 ? face_first->foreground
642 : FRAME_FOREGROUND_PIXEL (f));
643 unsigned long color_last = (face_last
644 ? face_last->foreground
645 : FRAME_FOREGROUND_PIXEL (f));
647 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
648 /* Vertical. */
650 w32_fill_area_abs (f, hdc, color_first, x0, y0, x0 + 1, y1);
651 w32_fill_area_abs (f, hdc, color, x0 + 1, y0, x1 - 1, y1);
652 w32_fill_area_abs (f, hdc, color_last, x1 - 1, y0, x1, y1);
654 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
655 /* Horizontal. */
657 w32_fill_area_abs (f, hdc, color_first, x0, y0, x1, y0 + 1);
658 w32_fill_area_abs (f, hdc, color, x0, y0 + 1, x1, y1 - 1);
659 w32_fill_area_abs (f, hdc, color_last, x0, y1 - 1, x1, y1);
661 else
662 w32_fill_area_abs (f, hdc, color, x0, y0, x1, y1);
664 release_frame_dc (f, hdc);
667 /* End update of window W.
669 Draw vertical borders between horizontally adjacent windows, and
670 display W's cursor if CURSOR_ON_P is non-zero.
672 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
673 glyphs in mouse-face were overwritten. In that case we have to
674 make sure that the mouse-highlight is properly redrawn.
676 W may be a menu bar pseudo-window in case we don't have X toolkit
677 support. Such windows don't have a cursor, so don't display it
678 here. */
680 static void
681 x_update_window_end (struct window *w, bool cursor_on_p,
682 bool mouse_face_overwritten_p)
684 if (!w->pseudo_window_p)
686 block_input ();
688 if (cursor_on_p)
689 display_and_set_cursor (w, 1,
690 w->output_cursor.hpos, w->output_cursor.vpos,
691 w->output_cursor.x, w->output_cursor.y);
693 if (draw_window_fringes (w, 1))
695 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
696 x_draw_right_divider (w);
697 else
698 x_draw_vertical_border (w);
701 unblock_input ();
704 /* If a row with mouse-face was overwritten, arrange for
705 XTframe_up_to_date to redisplay the mouse highlight. */
706 if (mouse_face_overwritten_p)
708 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
710 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
711 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
712 hlinfo->mouse_face_window = Qnil;
715 /* Unhide the caret. This won't actually show the cursor, unless it
716 was visible before the corresponding call to HideCaret in
717 x_update_window_begin. */
718 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
720 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
725 /* End update of frame F. This function is installed as a hook in
726 update_end. */
728 static void
729 x_update_end (struct frame *f)
731 if (! FRAME_W32_P (f))
732 return;
734 /* Mouse highlight may be displayed again. */
735 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
739 /* This function is called from various places in xdisp.c
740 whenever a complete update has been performed. */
742 static void
743 w32_frame_up_to_date (struct frame *f)
745 if (FRAME_W32_P (f))
746 FRAME_MOUSE_UPDATE (f);
750 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
751 arrow bitmaps, or clear the fringes if no bitmaps are required
752 before DESIRED_ROW is made current. This function is called from
753 update_window_line only if it is known that there are differences
754 between bitmaps to be drawn between current row and DESIRED_ROW. */
756 static void
757 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
759 struct frame *f;
760 int width, height;
762 eassert (w);
764 if (!desired_row->mode_line_p && !w->pseudo_window_p)
765 desired_row->redraw_fringe_bitmaps_p = 1;
767 /* When a window has disappeared, make sure that no rest of
768 full-width rows stays visible in the internal border. Could
769 check here if updated window is the leftmost/rightmost window,
770 but I guess it's not worth doing since vertically split windows
771 are almost never used, internal border is rarely set, and the
772 overhead is very small. */
773 if (windows_or_buffers_changed
774 && desired_row->full_width_p
775 && (f = XFRAME (w->frame),
776 width = FRAME_INTERNAL_BORDER_WIDTH (f),
777 width != 0)
778 && (height = desired_row->visible_height,
779 height > 0))
781 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
783 block_input ();
785 HDC hdc = get_frame_dc (f);
786 w32_clear_area (f, hdc, 0, y, width, height);
787 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
788 y, width, height);
789 release_frame_dc (f, hdc);
791 unblock_input ();
796 /* Draw the bitmap WHICH in one of the left or right fringes of
797 window W. ROW is the glyph row for which to display the bitmap; it
798 determines the vertical position at which the bitmap has to be
799 drawn. */
801 static void
802 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
803 struct draw_fringe_bitmap_params *p)
805 struct frame *f = XFRAME (WINDOW_FRAME (w));
806 HDC hdc;
807 struct face *face = p->face;
809 hdc = get_frame_dc (f);
811 /* Must clip because of partially visible lines. */
812 w32_clip_to_row (w, row, ANY_AREA, hdc);
814 if (p->bx >= 0 && !p->overlay_p)
815 w32_fill_area (f, hdc, face->background,
816 p->bx, p->by, p->nx, p->ny);
818 if (p->which && p->which < max_fringe_bmp)
820 HBITMAP pixmap = fringe_bmp[p->which];
821 HDC compat_hdc;
822 HANDLE horig_obj;
824 compat_hdc = CreateCompatibleDC (hdc);
826 SaveDC (hdc);
828 horig_obj = SelectObject (compat_hdc, pixmap);
830 /* Paint overlays transparently. */
831 if (p->overlay_p)
833 HBRUSH h_brush, h_orig_brush;
835 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
836 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
837 h_brush = CreateSolidBrush (face->foreground);
838 h_orig_brush = SelectObject (hdc, h_brush);
840 BitBlt (hdc, p->x, p->y, p->wd, p->h,
841 compat_hdc, 0, p->dh,
842 DSTINVERT);
843 BitBlt (hdc, p->x, p->y, p->wd, p->h,
844 compat_hdc, 0, p->dh,
845 0x2E064A);
846 BitBlt (hdc, p->x, p->y, p->wd, p->h,
847 compat_hdc, 0, p->dh,
848 DSTINVERT);
850 SelectObject (hdc, h_orig_brush);
851 DeleteObject (h_brush);
853 else
855 SetTextColor (hdc, face->background);
856 SetBkColor (hdc, (p->cursor_p
857 ? f->output_data.w32->cursor_pixel
858 : face->foreground));
860 BitBlt (hdc, p->x, p->y, p->wd, p->h,
861 compat_hdc, 0, p->dh,
862 SRCCOPY);
865 SelectObject (compat_hdc, horig_obj);
866 DeleteDC (compat_hdc);
867 RestoreDC (hdc, -1);
870 w32_set_clip_rectangle (hdc, NULL);
872 release_frame_dc (f, hdc);
875 static void
876 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
878 if (which >= max_fringe_bmp)
880 int i = max_fringe_bmp;
881 max_fringe_bmp = which + 20;
882 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
883 while (i < max_fringe_bmp)
884 fringe_bmp[i++] = 0;
887 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
890 static void
891 w32_destroy_fringe_bitmap (int which)
893 if (which >= max_fringe_bmp)
894 return;
896 if (fringe_bmp[which])
897 DeleteObject (fringe_bmp[which]);
898 fringe_bmp[which] = 0;
901 /***********************************************************************
902 Display Iterator
903 ***********************************************************************/
905 /* Function prototypes of this page. */
907 static void x_set_glyph_string_clipping (struct glyph_string *);
908 static void x_set_glyph_string_gc (struct glyph_string *);
909 static void x_draw_glyph_string_background (struct glyph_string *,
910 int);
911 static void x_draw_glyph_string_foreground (struct glyph_string *);
912 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
913 static void x_draw_glyph_string_box (struct glyph_string *);
914 static void x_draw_glyph_string (struct glyph_string *);
915 static void x_set_cursor_gc (struct glyph_string *);
916 static void x_set_mode_line_face_gc (struct glyph_string *);
917 static void x_set_mouse_face_gc (struct glyph_string *);
918 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
919 static void w32_setup_relief_color (struct frame *, struct relief *,
920 double, int, COLORREF);
921 static void x_setup_relief_colors (struct glyph_string *);
922 static void x_draw_image_glyph_string (struct glyph_string *);
923 static void x_draw_image_relief (struct glyph_string *);
924 static void x_draw_image_foreground (struct glyph_string *);
925 static void w32_draw_image_foreground_1 (struct glyph_string *, HBITMAP);
926 static void x_clear_glyph_string_rect (struct glyph_string *, int,
927 int, int, int);
928 static void w32_draw_relief_rect (struct frame *, int, int, int, int,
929 int, int, int, int, int, int,
930 RECT *);
931 static void w32_draw_box_rect (struct glyph_string *, int, int, int, int,
932 int, int, int, RECT *);
935 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
936 face. */
938 static void
939 x_set_cursor_gc (struct glyph_string *s)
941 if (s->font == FRAME_FONT (s->f)
942 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
943 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
944 && !s->cmp)
945 s->gc = s->f->output_data.w32->cursor_gc;
946 else
948 /* Cursor on non-default face: must merge. */
949 XGCValues xgcv;
950 unsigned long mask;
952 xgcv.background = s->f->output_data.w32->cursor_pixel;
953 xgcv.foreground = s->face->background;
955 /* If the glyph would be invisible, try a different foreground. */
956 if (xgcv.foreground == xgcv.background)
957 xgcv.foreground = s->face->foreground;
958 if (xgcv.foreground == xgcv.background)
959 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
960 if (xgcv.foreground == xgcv.background)
961 xgcv.foreground = s->face->foreground;
963 /* Make sure the cursor is distinct from text in this face. */
964 if (xgcv.background == s->face->background
965 && xgcv.foreground == s->face->foreground)
967 xgcv.background = s->face->foreground;
968 xgcv.foreground = s->face->background;
971 IF_DEBUG (x_check_font (s->f, s->font));
972 xgcv.font = s->font;
973 mask = GCForeground | GCBackground | GCFont;
975 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
976 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
977 mask, &xgcv);
978 else
979 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
980 = XCreateGC (NULL, s->window, mask, &xgcv);
982 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
987 /* Set up S->gc of glyph string S for drawing text in mouse face. */
989 static void
990 x_set_mouse_face_gc (struct glyph_string *s)
992 int face_id;
993 struct face *face;
995 /* What face has to be used last for the mouse face? */
996 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
997 face = FACE_FROM_ID (s->f, face_id);
998 if (face == NULL)
999 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1001 if (s->first_glyph->type == CHAR_GLYPH)
1002 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1003 else
1004 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1005 s->face = FACE_FROM_ID (s->f, face_id);
1006 prepare_face_for_display (s->f, s->face);
1008 /* If font in this face is same as S->font, use it. */
1009 if (s->font == s->face->font)
1010 s->gc = s->face->gc;
1011 else
1013 /* Otherwise construct scratch_cursor_gc with values from FACE
1014 but font FONT. */
1015 XGCValues xgcv;
1016 unsigned long mask;
1018 xgcv.background = s->face->background;
1019 xgcv.foreground = s->face->foreground;
1020 IF_DEBUG (x_check_font (s->f, s->font));
1021 xgcv.font = s->font;
1022 mask = GCForeground | GCBackground | GCFont;
1024 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1025 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1026 mask, &xgcv);
1027 else
1028 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1029 = XCreateGC (NULL, s->window, mask, &xgcv);
1031 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1034 eassert (s->gc != 0);
1038 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1039 Faces to use in the mode line have already been computed when the
1040 matrix was built, so there isn't much to do, here. */
1042 static inline void
1043 x_set_mode_line_face_gc (struct glyph_string *s)
1045 s->gc = s->face->gc;
1049 /* Set S->gc of glyph string S for drawing that glyph string. Set
1050 S->stippled_p to a non-zero value if the face of S has a stipple
1051 pattern. */
1053 static inline void
1054 x_set_glyph_string_gc (struct glyph_string *s)
1056 prepare_face_for_display (s->f, s->face);
1058 if (s->hl == DRAW_NORMAL_TEXT)
1060 s->gc = s->face->gc;
1061 s->stippled_p = s->face->stipple != 0;
1063 else if (s->hl == DRAW_INVERSE_VIDEO)
1065 x_set_mode_line_face_gc (s);
1066 s->stippled_p = s->face->stipple != 0;
1068 else if (s->hl == DRAW_CURSOR)
1070 x_set_cursor_gc (s);
1071 s->stippled_p = 0;
1073 else if (s->hl == DRAW_MOUSE_FACE)
1075 x_set_mouse_face_gc (s);
1076 s->stippled_p = s->face->stipple != 0;
1078 else if (s->hl == DRAW_IMAGE_RAISED
1079 || s->hl == DRAW_IMAGE_SUNKEN)
1081 s->gc = s->face->gc;
1082 s->stippled_p = s->face->stipple != 0;
1084 else
1085 emacs_abort ();
1087 /* GC must have been set. */
1088 eassert (s->gc != 0);
1092 /* Set clipping for output of glyph string S. S may be part of a mode
1093 line or menu if we don't have X toolkit support. */
1095 static inline void
1096 x_set_glyph_string_clipping (struct glyph_string *s)
1098 RECT *r = s->clip;
1099 int n = get_glyph_string_clip_rects (s, r, 2);
1101 if (n == 1)
1102 w32_set_clip_rectangle (s->hdc, r);
1103 else if (n > 1)
1105 HRGN clip1 = CreateRectRgnIndirect (r);
1106 HRGN clip2 = CreateRectRgnIndirect (r + 1);
1107 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
1108 SelectClipRgn (s->hdc, clip1);
1109 DeleteObject (clip1);
1110 DeleteObject (clip2);
1112 s->num_clips = n;
1115 /* Set SRC's clipping for output of glyph string DST. This is called
1116 when we are drawing DST's left_overhang or right_overhang only in
1117 the area of SRC. */
1119 static void
1120 x_set_glyph_string_clipping_exactly (struct glyph_string *src,
1121 struct glyph_string *dst)
1123 RECT r;
1125 r.left = src->x;
1126 r.right = r.left + src->width;
1127 r.top = src->y;
1128 r.bottom = r.top + src->height;
1129 dst->clip[0] = r;
1130 dst->num_clips = 1;
1131 w32_set_clip_rectangle (dst->hdc, &r);
1134 /* RIF:
1135 Compute left and right overhang of glyph string S. */
1137 static void
1138 w32_compute_glyph_string_overhangs (struct glyph_string *s)
1140 if (s->cmp == NULL
1141 && s->first_glyph->type == CHAR_GLYPH
1142 && !s->font_not_found_p)
1144 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1145 struct font *font = s->font;
1146 struct font_metrics metrics;
1147 int i;
1149 for (i = 0; i < s->nchars; i++)
1150 code[i] = s->char2b[i];
1151 font->driver->text_extents (font, code, s->nchars, &metrics);
1152 s->right_overhang = (metrics.rbearing > metrics.width
1153 ? metrics.rbearing - metrics.width : 0);
1154 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1156 else if (s->cmp)
1158 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1159 s->left_overhang = -s->cmp->lbearing;
1163 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1165 static inline void
1166 x_clear_glyph_string_rect (struct glyph_string *s,
1167 int x, int y, int w, int h)
1169 int real_x = x;
1170 int real_y = y;
1171 int real_w = w;
1172 int real_h = h;
1173 #if 0
1174 /* Take clipping into account. */
1175 if (s->gc->clip_mask == Rect)
1177 real_x = max (real_x, s->gc->clip_rectangle.left);
1178 real_y = max (real_y, s->gc->clip_rectangle.top);
1179 real_w = min (real_w, s->gc->clip_rectangle.right
1180 - s->gc->clip_rectangle.left);
1181 real_h = min (real_h, s->gc->clip_rectangle.bottom
1182 - s->gc->clip_rectangle.top);
1184 #endif
1185 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1186 real_w, real_h);
1190 /* Draw the background of glyph_string S. If S->background_filled_p
1191 is non-zero don't draw it. FORCE_P non-zero means draw the
1192 background even if it wouldn't be drawn normally. This is used
1193 when a string preceding S draws into the background of S, or S
1194 contains the first component of a composition. */
1196 static void
1197 x_draw_glyph_string_background (struct glyph_string *s, int force_p)
1199 /* Nothing to do if background has already been drawn or if it
1200 shouldn't be drawn in the first place. */
1201 if (!s->background_filled_p)
1203 int box_line_width = max (s->face->box_line_width, 0);
1205 #if 0 /* TODO: stipple */
1206 if (s->stippled_p)
1208 /* Fill background with a stipple pattern. */
1209 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1210 XFillRectangle (s->display, s->window, s->gc, s->x,
1211 s->y + box_line_width,
1212 s->background_width,
1213 s->height - 2 * box_line_width);
1214 XSetFillStyle (s->display, s->gc, FillSolid);
1215 s->background_filled_p = 1;
1217 else
1218 #endif
1219 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1220 || s->font_not_found_p
1221 || s->extends_to_end_of_line_p
1222 || force_p)
1224 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1225 s->background_width,
1226 s->height - 2 * box_line_width);
1227 s->background_filled_p = 1;
1233 /* Draw the foreground of glyph string S. */
1235 static void
1236 x_draw_glyph_string_foreground (struct glyph_string *s)
1238 int i, x;
1240 /* If first glyph of S has a left box line, start drawing the text
1241 of S to the right of that box line. */
1242 if (s->face->box != FACE_NO_BOX
1243 && s->first_glyph->left_box_line_p)
1244 x = s->x + eabs (s->face->box_line_width);
1245 else
1246 x = s->x;
1248 SetTextColor (s->hdc, s->gc->foreground);
1249 SetBkColor (s->hdc, s->gc->background);
1250 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1252 /* Draw characters of S as rectangles if S's font could not be
1253 loaded. */
1254 if (s->font_not_found_p)
1256 for (i = 0; i < s->nchars; ++i)
1258 struct glyph *g = s->first_glyph + i;
1260 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1261 s->height - 1);
1262 x += g->pixel_width;
1265 else
1267 struct font *font = s->font;
1268 int boff = font->baseline_offset;
1269 int y;
1270 HFONT old_font;
1272 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1274 if (font->vertical_centering)
1275 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1277 y = s->ybase - boff;
1278 if (s->for_overlaps
1279 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1280 font->driver->draw (s, 0, s->nchars, x, y, 0);
1281 else
1282 font->driver->draw (s, 0, s->nchars, x, y, 1);
1283 if (s->face->overstrike)
1284 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1286 SelectObject (s->hdc, old_font);
1290 /* Draw the foreground of composite glyph string S. */
1292 static void
1293 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1295 int i, j, x;
1296 struct font *font = s->font;
1298 /* If first glyph of S has a left box line, start drawing the text
1299 of S to the right of that box line. */
1300 if (s->face && s->face->box != FACE_NO_BOX
1301 && s->first_glyph->left_box_line_p)
1302 x = s->x + eabs (s->face->box_line_width);
1303 else
1304 x = s->x;
1306 /* S is a glyph string for a composition. S->cmp_from is the index
1307 of the first character drawn for glyphs of this composition.
1308 S->cmp_from == 0 means we are drawing the very first character of
1309 this composition. */
1311 SetTextColor (s->hdc, s->gc->foreground);
1312 SetBkColor (s->hdc, s->gc->background);
1313 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1315 /* Draw a rectangle for the composition if the font for the very
1316 first character of the composition could not be loaded. */
1317 if (s->font_not_found_p)
1319 if (s->cmp_from == 0)
1320 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1321 s->height - 1);
1323 else if (! s->first_glyph->u.cmp.automatic)
1325 int y = s->ybase;
1326 HFONT old_font;
1328 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1330 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1331 /* TAB in a composition means display glyphs with padding
1332 space on the left or right. */
1333 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1335 int xx = x + s->cmp->offsets[j * 2];
1336 int yy = y - s->cmp->offsets[j * 2 + 1];
1338 font->driver->draw (s, j, j + 1, xx, yy, 0);
1339 if (s->face->overstrike)
1340 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1342 SelectObject (s->hdc, old_font);
1344 else
1346 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1347 Lisp_Object glyph;
1348 int y = s->ybase;
1349 int width = 0;
1350 HFONT old_font;
1352 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1354 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1356 glyph = LGSTRING_GLYPH (gstring, i);
1357 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1358 width += LGLYPH_WIDTH (glyph);
1359 else
1361 int xoff, yoff, wadjust;
1363 if (j < i)
1365 font->driver->draw (s, j, i, x, y, 0);
1366 x += width;
1368 xoff = LGLYPH_XOFF (glyph);
1369 yoff = LGLYPH_YOFF (glyph);
1370 wadjust = LGLYPH_WADJUST (glyph);
1371 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1372 x += wadjust;
1373 j = i + 1;
1374 width = 0;
1377 if (j < i)
1378 font->driver->draw (s, j, i, x, y, 0);
1380 SelectObject (s->hdc, old_font);
1385 /* Draw the foreground of glyph string S for glyphless characters. */
1387 static void
1388 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1390 struct glyph *glyph = s->first_glyph;
1391 XChar2b char2b[8];
1392 int x, i, j;
1393 int with_background;
1395 /* If first glyph of S has a left box line, start drawing the text
1396 of S to the right of that box line. */
1397 if (s->face->box != FACE_NO_BOX
1398 && s->first_glyph->left_box_line_p)
1399 x = s->x + eabs (s->face->box_line_width);
1400 else
1401 x = s->x;
1403 SetTextColor (s->hdc, s->gc->foreground);
1404 SetBkColor (s->hdc, s->gc->background);
1405 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1407 s->char2b = char2b;
1408 with_background = ! (s->for_overlaps
1409 || (s->background_filled_p && s->hl != DRAW_CURSOR));
1410 for (i = 0; i < s->nchars; i++, glyph++)
1412 char buf[7], *str = NULL;
1413 int len = glyph->u.glyphless.len;
1415 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1417 if (len > 1
1418 && CHAR_TABLE_P (Vglyphless_char_display)
1419 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1420 >= 1))
1422 Lisp_Object acronym
1423 = (! glyph->u.glyphless.for_no_font
1424 ? CHAR_TABLE_REF (Vglyphless_char_display,
1425 glyph->u.glyphless.ch)
1426 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1427 if (STRINGP (acronym))
1428 str = SSDATA (acronym);
1431 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1433 sprintf ((char *) buf, "%0*X",
1434 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1435 glyph->u.glyphless.ch);
1436 str = buf;
1439 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1440 w32_draw_rectangle (s->hdc, s->gc,
1441 x, s->ybase - glyph->ascent,
1442 glyph->pixel_width - 1,
1443 glyph->ascent + glyph->descent - 1);
1444 if (str)
1446 struct font *font = s->font;
1447 int upper_len = (len + 1) / 2;
1448 unsigned code;
1449 HFONT old_font;
1451 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1452 /* It is certain that all LEN characters in STR are ASCII. */
1453 for (j = 0; j < len; j++)
1455 code = font->driver->encode_char (font, str[j]);
1456 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1458 font->driver->draw (s, 0, upper_len,
1459 x + glyph->slice.glyphless.upper_xoff,
1460 s->ybase + glyph->slice.glyphless.upper_yoff,
1461 with_background);
1462 font->driver->draw (s, upper_len, len,
1463 x + glyph->slice.glyphless.lower_xoff,
1464 s->ybase + glyph->slice.glyphless.lower_yoff,
1465 with_background);
1466 SelectObject (s->hdc, old_font);
1468 x += glyph->pixel_width;
1473 /* Brightness beyond which a color won't have its highlight brightness
1474 boosted.
1476 Nominally, highlight colors for `3d' faces are calculated by
1477 brightening an object's color by a constant scale factor, but this
1478 doesn't yield good results for dark colors, so for colors whose
1479 brightness is less than this value (on a scale of 0-255) have to
1480 use an additional additive factor.
1482 The value here is set so that the default menu-bar/mode-line color
1483 (grey75) will not have its highlights changed at all. */
1484 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1487 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1488 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1489 If this produces the same color as COLOR, try a color where all RGB
1490 values have DELTA added. Return the allocated color in *COLOR.
1491 DISPLAY is the X display, CMAP is the colormap to operate on.
1492 Value is non-zero if successful. */
1494 static int
1495 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1496 double factor, int delta)
1498 COLORREF new;
1499 long bright;
1501 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1502 delta /= 256;
1504 /* Change RGB values by specified FACTOR. Avoid overflow! */
1505 eassert (factor >= 0);
1506 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1507 min (0xff, factor * GetGValue (*color)),
1508 min (0xff, factor * GetBValue (*color)));
1510 /* Calculate brightness of COLOR. */
1511 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1512 + GetBValue (*color)) / 6;
1514 /* We only boost colors that are darker than
1515 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1516 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1517 /* Make an additive adjustment to NEW, because it's dark enough so
1518 that scaling by FACTOR alone isn't enough. */
1520 /* How far below the limit this color is (0 - 1, 1 being darker). */
1521 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1522 /* The additive adjustment. */
1523 int min_delta = delta * dimness * factor / 2;
1525 if (factor < 1)
1526 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1527 max (0, min (0xff, min_delta - GetGValue (*color))),
1528 max (0, min (0xff, min_delta - GetBValue (*color))));
1529 else
1530 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1531 max (0, min (0xff, min_delta + GetGValue (*color))),
1532 max (0, min (0xff, min_delta + GetBValue (*color))));
1535 if (new == *color)
1536 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1537 max (0, min (0xff, delta + GetGValue (*color))),
1538 max (0, min (0xff, delta + GetBValue (*color))));
1540 /* TODO: Map to palette and retry with delta if same? */
1541 /* TODO: Free colors (if using palette)? */
1543 if (new == *color)
1544 return 0;
1546 *color = new;
1548 return 1;
1551 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1552 colors in COLORS. On W32, we no longer try to map colors to
1553 a palette. */
1554 void
1555 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1557 int i;
1559 for (i = 0; i < ncolors; i++)
1561 DWORD pixel = colors[i].pixel;
1562 /* Convert to a 16 bit value in range 0 - 0xffff. */
1563 colors[i].red = GetRValue (pixel) * 257;
1564 colors[i].green = GetGValue (pixel) * 257;
1565 colors[i].blue = GetBValue (pixel) * 257;
1569 void
1570 x_query_color (struct frame *f, XColor *color)
1572 x_query_colors (f, color, 1);
1576 /* Set up the foreground color for drawing relief lines of glyph
1577 string S. RELIEF is a pointer to a struct relief containing the GC
1578 with which lines will be drawn. Use a color that is FACTOR or
1579 DELTA lighter or darker than the relief's background which is found
1580 in S->f->output_data.x->relief_background. If such a color cannot
1581 be allocated, use DEFAULT_PIXEL, instead. */
1583 static void
1584 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1585 int delta, COLORREF default_pixel)
1587 XGCValues xgcv;
1588 struct w32_output *di = f->output_data.w32;
1589 unsigned long mask = GCForeground;
1590 COLORREF pixel;
1591 COLORREF background = di->relief_background;
1592 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1594 /* TODO: Free colors (if using palette)? */
1596 /* Allocate new color. */
1597 xgcv.foreground = default_pixel;
1598 pixel = background;
1599 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1600 xgcv.foreground = relief->pixel = pixel;
1602 if (relief->gc == 0)
1604 #if 0 /* TODO: stipple */
1605 xgcv.stipple = dpyinfo->gray;
1606 mask |= GCStipple;
1607 #endif
1608 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1610 else
1611 XChangeGC (NULL, relief->gc, mask, &xgcv);
1615 /* Set up colors for the relief lines around glyph string S. */
1617 static void
1618 x_setup_relief_colors (struct glyph_string *s)
1620 struct w32_output *di = s->f->output_data.w32;
1621 COLORREF color;
1623 if (s->face->use_box_color_for_shadows_p)
1624 color = s->face->box_color;
1625 else if (s->first_glyph->type == IMAGE_GLYPH
1626 && s->img->pixmap
1627 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1628 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1629 else
1630 color = s->gc->background;
1632 if (di->white_relief.gc == 0
1633 || color != di->relief_background)
1635 di->relief_background = color;
1636 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1637 WHITE_PIX_DEFAULT (s->f));
1638 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1639 BLACK_PIX_DEFAULT (s->f));
1644 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1645 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1646 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1647 relief. LEFT_P non-zero means draw a relief on the left side of
1648 the rectangle. RIGHT_P non-zero means draw a relief on the right
1649 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1650 when drawing. */
1652 static void
1653 w32_draw_relief_rect (struct frame *f,
1654 int left_x, int top_y, int right_x, int bottom_y,
1655 int width, int raised_p,
1656 int top_p, int bot_p, int left_p, int right_p,
1657 RECT *clip_rect)
1659 int i;
1660 XGCValues gc;
1661 HDC hdc = get_frame_dc (f);
1663 if (raised_p)
1664 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1665 else
1666 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1668 w32_set_clip_rectangle (hdc, clip_rect);
1670 /* Top. */
1671 if (top_p)
1672 for (i = 0; i < width; ++i)
1673 w32_fill_area (f, hdc, gc.foreground,
1674 left_x + i * left_p, top_y + i,
1675 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1677 /* Left. */
1678 if (left_p)
1679 for (i = 0; i < width; ++i)
1680 w32_fill_area (f, hdc, gc.foreground,
1681 left_x + i, top_y + (i + 1) * top_p, 1,
1682 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1684 if (raised_p)
1685 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1686 else
1687 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1689 /* Bottom. */
1690 if (bot_p)
1691 for (i = 0; i < width; ++i)
1692 w32_fill_area (f, hdc, gc.foreground,
1693 left_x + i * left_p, bottom_y - i,
1694 right_x - left_x - i * (left_p + right_p) + 1, 1);
1696 /* Right. */
1697 if (right_p)
1698 for (i = 0; i < width; ++i)
1699 w32_fill_area (f, hdc, gc.foreground,
1700 right_x - i, top_y + (i + 1) * top_p, 1,
1701 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1703 w32_set_clip_rectangle (hdc, NULL);
1705 release_frame_dc (f, hdc);
1709 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1710 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1711 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1712 left side of the rectangle. RIGHT_P non-zero means draw a line
1713 on the right side of the rectangle. CLIP_RECT is the clipping
1714 rectangle to use when drawing. */
1716 static void
1717 w32_draw_box_rect (struct glyph_string *s,
1718 int left_x, int top_y, int right_x, int bottom_y, int width,
1719 int left_p, int right_p, RECT *clip_rect)
1721 w32_set_clip_rectangle (s->hdc, clip_rect);
1723 /* Top. */
1724 w32_fill_area (s->f, s->hdc, s->face->box_color,
1725 left_x, top_y, right_x - left_x + 1, width);
1727 /* Left. */
1728 if (left_p)
1730 w32_fill_area (s->f, s->hdc, s->face->box_color,
1731 left_x, top_y, width, bottom_y - top_y + 1);
1734 /* Bottom. */
1735 w32_fill_area (s->f, s->hdc, s->face->box_color,
1736 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1738 /* Right. */
1739 if (right_p)
1741 w32_fill_area (s->f, s->hdc, s->face->box_color,
1742 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1745 w32_set_clip_rectangle (s->hdc, NULL);
1749 /* Draw a box around glyph string S. */
1751 static void
1752 x_draw_glyph_string_box (struct glyph_string *s)
1754 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1755 int left_p, right_p;
1756 struct glyph *last_glyph;
1757 RECT clip_rect;
1759 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1760 ? WINDOW_RIGHT_EDGE_X (s->w)
1761 : window_box_right (s->w, s->area));
1763 /* The glyph that may have a right box line. */
1764 last_glyph = (s->cmp || s->img
1765 ? s->first_glyph
1766 : s->first_glyph + s->nchars - 1);
1768 width = eabs (s->face->box_line_width);
1769 raised_p = s->face->box == FACE_RAISED_BOX;
1770 left_x = s->x;
1771 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1772 ? last_x - 1
1773 : min (last_x, s->x + s->background_width) - 1));
1774 top_y = s->y;
1775 bottom_y = top_y + s->height - 1;
1777 left_p = (s->first_glyph->left_box_line_p
1778 || (s->hl == DRAW_MOUSE_FACE
1779 && (s->prev == NULL
1780 || s->prev->hl != s->hl)));
1781 right_p = (last_glyph->right_box_line_p
1782 || (s->hl == DRAW_MOUSE_FACE
1783 && (s->next == NULL
1784 || s->next->hl != s->hl)));
1786 get_glyph_string_clip_rect (s, &clip_rect);
1788 if (s->face->box == FACE_SIMPLE_BOX)
1789 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1790 left_p, right_p, &clip_rect);
1791 else
1793 x_setup_relief_colors (s);
1794 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1795 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1800 /* Draw foreground of image glyph string S. */
1802 static void
1803 x_draw_image_foreground (struct glyph_string *s)
1805 int x = s->x;
1806 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1808 /* If first glyph of S has a left box line, start drawing it to the
1809 right of that line. */
1810 if (s->face->box != FACE_NO_BOX
1811 && s->first_glyph->left_box_line_p
1812 && s->slice.x == 0)
1813 x += eabs (s->face->box_line_width);
1815 /* If there is a margin around the image, adjust x- and y-position
1816 by that margin. */
1817 if (s->slice.x == 0)
1818 x += s->img->hmargin;
1819 if (s->slice.y == 0)
1820 y += s->img->vmargin;
1822 SaveDC (s->hdc);
1824 if (s->img->pixmap)
1826 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1827 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1828 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1829 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1830 SetBkColor (compat_hdc, RGB (255, 255, 255));
1831 SetTextColor (s->hdc, RGB (0, 0, 0));
1832 x_set_glyph_string_clipping (s);
1834 if (s->img->mask)
1836 HDC mask_dc = CreateCompatibleDC (s->hdc);
1837 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1839 SetTextColor (s->hdc, RGB (255, 255, 255));
1840 SetBkColor (s->hdc, RGB (0, 0, 0));
1842 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1843 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1844 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1845 mask_dc, s->slice.x, s->slice.y, SRCAND);
1846 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1847 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1849 SelectObject (mask_dc, mask_orig_obj);
1850 DeleteDC (mask_dc);
1852 else
1854 SetTextColor (s->hdc, s->gc->foreground);
1855 SetBkColor (s->hdc, s->gc->background);
1857 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1858 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1860 /* When the image has a mask, we can expect that at
1861 least part of a mouse highlight or a block cursor will
1862 be visible. If the image doesn't have a mask, make
1863 a block cursor visible by drawing a rectangle around
1864 the image. I believe it's looking better if we do
1865 nothing here for mouse-face. */
1866 if (s->hl == DRAW_CURSOR)
1868 int r = s->img->relief;
1869 if (r < 0) r = -r;
1870 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1871 s->slice.width + r*2 - 1,
1872 s->slice.height + r*2 - 1);
1876 w32_set_clip_rectangle (s->hdc, NULL);
1877 SelectObject (s->hdc, orig_brush);
1878 DeleteObject (fg_brush);
1879 SelectObject (compat_hdc, orig_obj);
1880 DeleteDC (compat_hdc);
1882 else
1883 w32_draw_rectangle (s->hdc, s->gc, x, y,
1884 s->slice.width - 1, s->slice.height - 1);
1886 RestoreDC (s->hdc ,-1);
1890 /* Draw a relief around the image glyph string S. */
1892 static void
1893 x_draw_image_relief (struct glyph_string *s)
1895 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
1896 int extra_x, extra_y;
1897 RECT r;
1898 int x = s->x;
1899 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1901 /* If first glyph of S has a left box line, start drawing it to the
1902 right of that line. */
1903 if (s->face->box != FACE_NO_BOX
1904 && s->first_glyph->left_box_line_p
1905 && s->slice.x == 0)
1906 x += eabs (s->face->box_line_width);
1908 /* If there is a margin around the image, adjust x- and y-position
1909 by that margin. */
1910 if (s->slice.x == 0)
1911 x += s->img->hmargin;
1912 if (s->slice.y == 0)
1913 y += s->img->vmargin;
1915 if (s->hl == DRAW_IMAGE_SUNKEN
1916 || s->hl == DRAW_IMAGE_RAISED)
1918 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief
1919 : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1920 raised_p = s->hl == DRAW_IMAGE_RAISED;
1922 else
1924 thick = eabs (s->img->relief);
1925 raised_p = s->img->relief > 0;
1928 x1 = x + s->slice.width - 1;
1929 y1 = y + s->slice.height - 1;
1931 extra_x = extra_y = 0;
1932 if (s->face->id == TOOL_BAR_FACE_ID)
1934 if (CONSP (Vtool_bar_button_margin)
1935 && INTEGERP (XCAR (Vtool_bar_button_margin))
1936 && INTEGERP (XCDR (Vtool_bar_button_margin)))
1938 extra_x = XINT (XCAR (Vtool_bar_button_margin));
1939 extra_y = XINT (XCDR (Vtool_bar_button_margin));
1941 else if (INTEGERP (Vtool_bar_button_margin))
1942 extra_x = extra_y = XINT (Vtool_bar_button_margin);
1945 top_p = bot_p = left_p = right_p = 0;
1947 if (s->slice.x == 0)
1948 x -= thick + extra_x, left_p = 1;
1949 if (s->slice.y == 0)
1950 y -= thick + extra_y, top_p = 1;
1951 if (s->slice.x + s->slice.width == s->img->width)
1952 x1 += thick + extra_x, right_p = 1;
1953 if (s->slice.y + s->slice.height == s->img->height)
1954 y1 += thick + extra_y, bot_p = 1;
1956 x_setup_relief_colors (s);
1957 get_glyph_string_clip_rect (s, &r);
1958 w32_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
1959 top_p, bot_p, left_p, right_p, &r);
1963 /* Draw the foreground of image glyph string S to PIXMAP. */
1965 static void
1966 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
1968 HDC hdc = CreateCompatibleDC (s->hdc);
1969 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1970 int x = 0;
1971 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1973 /* If first glyph of S has a left box line, start drawing it to the
1974 right of that line. */
1975 if (s->face->box != FACE_NO_BOX
1976 && s->first_glyph->left_box_line_p
1977 && s->slice.x == 0)
1978 x += eabs (s->face->box_line_width);
1980 /* If there is a margin around the image, adjust x- and y-position
1981 by that margin. */
1982 if (s->slice.x == 0)
1983 x += s->img->hmargin;
1984 if (s->slice.y == 0)
1985 y += s->img->vmargin;
1987 if (s->img->pixmap)
1989 HDC compat_hdc = CreateCompatibleDC (hdc);
1990 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1991 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1992 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1994 if (s->img->mask)
1996 HDC mask_dc = CreateCompatibleDC (hdc);
1997 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1999 SetTextColor (hdc, RGB (0, 0, 0));
2000 SetBkColor (hdc, RGB (255, 255, 255));
2001 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2002 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2003 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2004 mask_dc, s->slice.x, s->slice.y, SRCAND);
2005 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2006 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2008 SelectObject (mask_dc, mask_orig_obj);
2009 DeleteDC (mask_dc);
2011 else
2013 SetTextColor (hdc, s->gc->foreground);
2014 SetBkColor (hdc, s->gc->background);
2016 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2017 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2019 /* When the image has a mask, we can expect that at
2020 least part of a mouse highlight or a block cursor will
2021 be visible. If the image doesn't have a mask, make
2022 a block cursor visible by drawing a rectangle around
2023 the image. I believe it's looking better if we do
2024 nothing here for mouse-face. */
2025 if (s->hl == DRAW_CURSOR)
2027 int r = s->img->relief;
2028 if (r < 0) r = -r;
2029 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2030 s->slice.width + r*2 - 1,
2031 s->slice.height + r*2 - 1);
2035 SelectObject (hdc, orig_brush);
2036 DeleteObject (fg_brush);
2037 SelectObject (compat_hdc, orig_obj);
2038 DeleteDC (compat_hdc);
2040 else
2041 w32_draw_rectangle (hdc, s->gc, x, y,
2042 s->slice.width - 1, s->slice.height - 1);
2044 SelectObject (hdc, orig_hdc_obj);
2045 DeleteDC (hdc);
2049 /* Draw part of the background of glyph string S. X, Y, W, and H
2050 give the rectangle to draw. */
2052 static void
2053 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2055 #if 0 /* TODO: stipple */
2056 if (s->stippled_p)
2058 /* Fill background with a stipple pattern. */
2059 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2060 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2061 XSetFillStyle (s->display, s->gc, FillSolid);
2063 else
2064 #endif
2065 x_clear_glyph_string_rect (s, x, y, w, h);
2069 /* Draw image glyph string S.
2071 s->y
2072 s->x +-------------------------
2073 | s->face->box
2075 | +-------------------------
2076 | | s->img->vmargin
2078 | | +-------------------
2079 | | | the image
2083 static void
2084 x_draw_image_glyph_string (struct glyph_string *s)
2086 int x, y;
2087 int box_line_hwidth = eabs (s->face->box_line_width);
2088 int box_line_vwidth = max (s->face->box_line_width, 0);
2089 int height, width;
2090 HBITMAP pixmap = 0;
2092 height = s->height;
2093 if (s->slice.y == 0)
2094 height -= box_line_vwidth;
2095 if (s->slice.y + s->slice.height >= s->img->height)
2096 height -= box_line_vwidth;
2098 /* Fill background with face under the image. Do it only if row is
2099 taller than image or if image has a clip mask to reduce
2100 flickering. */
2101 s->stippled_p = s->face->stipple != 0;
2102 if (height > s->slice.height
2103 || s->img->hmargin
2104 || s->img->vmargin
2105 || s->img->mask
2106 || s->img->pixmap == 0
2107 || s->width != s->background_width)
2109 width = s->background_width;
2110 x = s->x;
2111 if (s->first_glyph->left_box_line_p
2112 && s->slice.x == 0)
2114 x += box_line_hwidth;
2115 width -= box_line_hwidth;
2118 y = s->y;
2119 if (s->slice.y == 0)
2120 y += box_line_vwidth;
2122 #if 0 /* TODO: figure out if we need to do this on Windows. */
2123 if (s->img->mask)
2125 /* Create a pixmap as large as the glyph string. Fill it
2126 with the background color. Copy the image to it, using
2127 its mask. Copy the temporary pixmap to the display. */
2128 Screen *screen = FRAME_X_SCREEN (s->f);
2129 int depth = DefaultDepthOfScreen (screen);
2131 /* Create a pixmap as large as the glyph string. */
2132 pixmap = XCreatePixmap (s->display, s->window,
2133 s->background_width,
2134 s->height, depth);
2136 /* Don't clip in the following because we're working on the
2137 pixmap. */
2138 XSetClipMask (s->display, s->gc, None);
2140 /* Fill the pixmap with the background color/stipple. */
2141 if (s->stippled_p)
2143 /* Fill background with a stipple pattern. */
2144 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2145 XFillRectangle (s->display, pixmap, s->gc,
2146 0, 0, s->background_width, s->height);
2147 XSetFillStyle (s->display, s->gc, FillSolid);
2149 else
2151 XGCValues xgcv;
2152 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2153 &xgcv);
2154 XSetForeground (s->display, s->gc, xgcv.background);
2155 XFillRectangle (s->display, pixmap, s->gc,
2156 0, 0, s->background_width, s->height);
2157 XSetForeground (s->display, s->gc, xgcv.foreground);
2160 else
2161 #endif
2162 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2164 s->background_filled_p = 1;
2167 /* Draw the foreground. */
2168 if (pixmap != 0)
2170 w32_draw_image_foreground_1 (s, pixmap);
2171 x_set_glyph_string_clipping (s);
2173 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2174 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2175 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2176 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2178 SetTextColor (s->hdc, s->gc->foreground);
2179 SetBkColor (s->hdc, s->gc->background);
2180 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2181 compat_hdc, 0, 0, SRCCOPY);
2183 SelectObject (s->hdc, orig_brush);
2184 DeleteObject (fg_brush);
2185 SelectObject (compat_hdc, orig_obj);
2186 DeleteDC (compat_hdc);
2188 DeleteObject (pixmap);
2189 pixmap = 0;
2191 else
2192 x_draw_image_foreground (s);
2194 /* If we must draw a relief around the image, do it. */
2195 if (s->img->relief
2196 || s->hl == DRAW_IMAGE_RAISED
2197 || s->hl == DRAW_IMAGE_SUNKEN)
2198 x_draw_image_relief (s);
2202 /* Draw stretch glyph string S. */
2204 static void
2205 x_draw_stretch_glyph_string (struct glyph_string *s)
2207 eassert (s->first_glyph->type == STRETCH_GLYPH);
2209 if (s->hl == DRAW_CURSOR
2210 && !x_stretch_cursor_p)
2212 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2213 wide as the stretch glyph. */
2214 int width, background_width = s->background_width;
2215 int x = s->x;
2217 if (!s->row->reversed_p)
2219 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2221 if (x < left_x)
2223 background_width -= left_x - x;
2224 x = left_x;
2227 else
2229 /* In R2L rows, draw the cursor on the right edge of the
2230 stretch glyph. */
2231 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2233 if (x + background_width > right_x)
2234 background_width -= x - right_x;
2235 x += background_width;
2237 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2238 if (s->row->reversed_p)
2239 x -= width;
2241 /* Draw cursor. */
2242 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2244 /* Clear rest using the GC of the original non-cursor face. */
2245 if (width < background_width)
2247 XGCValues *gc = s->face->gc;
2248 int y = s->y;
2249 int w = background_width - width, h = s->height;
2250 RECT r;
2251 HDC hdc = s->hdc;
2253 if (!s->row->reversed_p)
2254 x += width;
2255 else
2256 x = s->x;
2257 if (s->row->mouse_face_p
2258 && cursor_in_mouse_face_p (s->w))
2260 x_set_mouse_face_gc (s);
2261 gc = s->gc;
2263 else
2264 gc = s->face->gc;
2266 get_glyph_string_clip_rect (s, &r);
2267 w32_set_clip_rectangle (hdc, &r);
2269 #if 0 /* TODO: stipple */
2270 if (s->face->stipple)
2272 /* Fill background with a stipple pattern. */
2273 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2274 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2275 XSetFillStyle (s->display, gc, FillSolid);
2277 else
2278 #endif
2280 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2284 else if (!s->background_filled_p)
2286 int background_width = s->background_width;
2287 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2289 /* Don't draw into left margin, fringe or scrollbar area
2290 except for header line and mode line. */
2291 if (x < left_x && !s->row->mode_line_p)
2293 background_width -= left_x - x;
2294 x = left_x;
2296 if (background_width > 0)
2297 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2300 s->background_filled_p = 1;
2304 /* Draw glyph string S. */
2306 static void
2307 x_draw_glyph_string (struct glyph_string *s)
2309 bool relief_drawn_p = 0;
2311 /* If S draws into the background of its successor, draw the
2312 background of the successor first so that S can draw into it.
2313 This makes S->next use XDrawString instead of XDrawImageString. */
2314 if (s->next && s->right_overhang && !s->for_overlaps)
2316 int width;
2317 struct glyph_string *next;
2318 for (width = 0, next = s->next;
2319 next && width < s->right_overhang;
2320 width += next->width, next = next->next)
2321 if (next->first_glyph->type != IMAGE_GLYPH)
2323 x_set_glyph_string_gc (next);
2324 x_set_glyph_string_clipping (next);
2325 if (next->first_glyph->type == STRETCH_GLYPH)
2326 x_draw_stretch_glyph_string (next);
2327 else
2328 x_draw_glyph_string_background (next, 1);
2329 next->num_clips = 0;
2333 /* Set up S->gc, set clipping and draw S. */
2334 x_set_glyph_string_gc (s);
2336 /* Draw relief (if any) in advance for char/composition so that the
2337 glyph string can be drawn over it. */
2338 if (!s->for_overlaps
2339 && s->face->box != FACE_NO_BOX
2340 && (s->first_glyph->type == CHAR_GLYPH
2341 || s->first_glyph->type == COMPOSITE_GLYPH))
2344 x_set_glyph_string_clipping (s);
2345 x_draw_glyph_string_background (s, 1);
2346 x_draw_glyph_string_box (s);
2347 x_set_glyph_string_clipping (s);
2348 relief_drawn_p = 1;
2350 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2351 && !s->clip_tail
2352 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2353 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2354 /* We must clip just this glyph. left_overhang part has already
2355 drawn when s->prev was drawn, and right_overhang part will be
2356 drawn later when s->next is drawn. */
2357 x_set_glyph_string_clipping_exactly (s, s);
2358 else
2359 x_set_glyph_string_clipping (s);
2361 switch (s->first_glyph->type)
2363 case IMAGE_GLYPH:
2364 x_draw_image_glyph_string (s);
2365 break;
2367 case STRETCH_GLYPH:
2368 x_draw_stretch_glyph_string (s);
2369 break;
2371 case CHAR_GLYPH:
2372 if (s->for_overlaps)
2373 s->background_filled_p = 1;
2374 else
2375 x_draw_glyph_string_background (s, 0);
2376 x_draw_glyph_string_foreground (s);
2377 break;
2379 case COMPOSITE_GLYPH:
2380 if (s->for_overlaps || (s->cmp_from > 0
2381 && ! s->first_glyph->u.cmp.automatic))
2382 s->background_filled_p = 1;
2383 else
2384 x_draw_glyph_string_background (s, 1);
2385 x_draw_composite_glyph_string_foreground (s);
2386 break;
2388 case GLYPHLESS_GLYPH:
2389 if (s->for_overlaps)
2390 s->background_filled_p = 1;
2391 else
2392 x_draw_glyph_string_background (s, 0);
2393 x_draw_glyphless_glyph_string_foreground (s);
2394 break;
2396 default:
2397 emacs_abort ();
2400 if (!s->for_overlaps)
2402 /* Draw underline. */
2403 if (s->face->underline_p)
2405 if (s->face->underline_type == FACE_UNDER_WAVE)
2407 COLORREF color;
2409 if (s->face->underline_defaulted_p)
2410 color = s->gc->foreground;
2411 else
2412 color = s->face->underline_color;
2414 w32_draw_underwave (s, color);
2416 else if (s->face->underline_type == FACE_UNDER_LINE)
2418 unsigned long thickness, position;
2419 int y;
2421 if (s->prev && s->prev->face->underline_p
2422 && s->prev->face->underline_type == FACE_UNDER_LINE)
2424 /* We use the same underline style as the previous one. */
2425 thickness = s->prev->underline_thickness;
2426 position = s->prev->underline_position;
2428 else
2430 /* Get the underline thickness. Default is 1 pixel. */
2431 if (s->font && s->font->underline_thickness > 0)
2432 thickness = s->font->underline_thickness;
2433 else
2434 thickness = 1;
2435 if (x_underline_at_descent_line)
2436 position = (s->height - thickness) - (s->ybase - s->y);
2437 else
2439 /* Get the underline position. This is the recommended
2440 vertical offset in pixels from the baseline to the top of
2441 the underline. This is a signed value according to the
2442 specs, and its default is
2444 ROUND ((maximum_descent) / 2), with
2445 ROUND (x) = floor (x + 0.5) */
2447 if (x_use_underline_position_properties
2448 && s->font && s->font->underline_position >= 0)
2449 position = s->font->underline_position;
2450 else if (s->font)
2451 position = (s->font->descent + 1) / 2;
2453 position = max (position, underline_minimum_offset);
2455 /* Check the sanity of thickness and position. We should
2456 avoid drawing underline out of the current line area. */
2457 if (s->y + s->height <= s->ybase + position)
2458 position = (s->height - 1) - (s->ybase - s->y);
2459 if (s->y + s->height < s->ybase + position + thickness)
2460 thickness = (s->y + s->height) - (s->ybase + position);
2461 s->underline_thickness = thickness;
2462 s->underline_position =position;
2463 y = s->ybase + position;
2464 if (s->face->underline_defaulted_p)
2466 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2467 y, s->width, 1);
2469 else
2471 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2472 y, s->width, 1);
2476 /* Draw overline. */
2477 if (s->face->overline_p)
2479 unsigned long dy = 0, h = 1;
2481 if (s->face->overline_color_defaulted_p)
2483 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2484 s->y + dy, s->width, h);
2486 else
2488 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2489 s->y + dy, s->width, h);
2493 /* Draw strike-through. */
2494 if (s->face->strike_through_p
2495 && !FONT_TEXTMETRIC (s->font).tmStruckOut)
2497 unsigned long h = 1;
2498 unsigned long dy = (s->height - h) / 2;
2500 if (s->face->strike_through_color_defaulted_p)
2502 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2503 s->width, h);
2505 else
2507 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2508 s->y + dy, s->width, h);
2512 /* Draw relief if not yet drawn. */
2513 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2514 x_draw_glyph_string_box (s);
2516 if (s->prev)
2518 struct glyph_string *prev;
2520 for (prev = s->prev; prev; prev = prev->prev)
2521 if (prev->hl != s->hl
2522 && prev->x + prev->width + prev->right_overhang > s->x)
2524 /* As prev was drawn while clipped to its own area, we
2525 must draw the right_overhang part using s->hl now. */
2526 enum draw_glyphs_face save = prev->hl;
2528 prev->hl = s->hl;
2529 x_set_glyph_string_gc (prev);
2530 x_set_glyph_string_clipping_exactly (s, prev);
2531 if (prev->first_glyph->type == CHAR_GLYPH)
2532 x_draw_glyph_string_foreground (prev);
2533 else
2534 x_draw_composite_glyph_string_foreground (prev);
2535 w32_set_clip_rectangle (prev->hdc, NULL);
2536 prev->hl = save;
2537 prev->num_clips = 0;
2541 if (s->next)
2543 struct glyph_string *next;
2545 for (next = s->next; next; next = next->next)
2546 if (next->hl != s->hl
2547 && next->x - next->left_overhang < s->x + s->width)
2549 /* As next will be drawn while clipped to its own area,
2550 we must draw the left_overhang part using s->hl now. */
2551 enum draw_glyphs_face save = next->hl;
2553 next->hl = s->hl;
2554 x_set_glyph_string_gc (next);
2555 x_set_glyph_string_clipping_exactly (s, next);
2556 if (next->first_glyph->type == CHAR_GLYPH)
2557 x_draw_glyph_string_foreground (next);
2558 else
2559 x_draw_composite_glyph_string_foreground (next);
2560 w32_set_clip_rectangle (next->hdc, NULL);
2561 next->hl = save;
2562 next->num_clips = 0;
2563 next->clip_head = s->next;
2568 /* Reset clipping. */
2569 w32_set_clip_rectangle (s->hdc, NULL);
2570 s->num_clips = 0;
2574 /* Shift display to make room for inserted glyphs. */
2576 void
2577 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2578 int width, int height, int shift_by)
2580 HDC hdc;
2582 hdc = get_frame_dc (f);
2583 BitBlt (hdc, x + shift_by, y, width, height,
2584 hdc, x, y, SRCCOPY);
2586 release_frame_dc (f, hdc);
2590 /* Delete N glyphs at the nominal cursor position. Not implemented
2591 for X frames. */
2593 static void
2594 x_delete_glyphs (struct frame *f, register int n)
2596 if (! FRAME_W32_P (f))
2597 return;
2599 emacs_abort ();
2603 /* Clear entire frame. */
2605 static void
2606 x_clear_frame (struct frame *f)
2608 if (! FRAME_W32_P (f))
2609 return;
2611 /* Clearing the frame will erase any cursor, so mark them all as no
2612 longer visible. */
2613 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2615 block_input ();
2617 w32_clear_window (f);
2619 /* We have to clear the scroll bars, too. If we have changed
2620 colors or something like that, then they should be notified. */
2621 x_scroll_bar_clear (f);
2623 unblock_input ();
2627 /* Make audible bell. */
2629 static void
2630 w32_ring_bell (struct frame *f)
2632 block_input ();
2634 if (FRAME_W32_P (f) && visible_bell)
2636 int i;
2637 HWND hwnd = FRAME_W32_WINDOW (f);
2639 for (i = 0; i < 5; i++)
2641 FlashWindow (hwnd, TRUE);
2642 Sleep (10);
2644 FlashWindow (hwnd, FALSE);
2646 else
2647 w32_sys_ring_bell (f);
2649 unblock_input ();
2652 /***********************************************************************
2653 Line Dance
2654 ***********************************************************************/
2656 /* Perform an insert-lines or delete-lines operation, inserting N
2657 lines or deleting -N lines at vertical position VPOS. */
2659 static void
2660 x_ins_del_lines (struct frame *f, int vpos, int n)
2662 if (! FRAME_W32_P (f))
2663 return;
2665 emacs_abort ();
2669 /* Scroll part of the display as described by RUN. */
2671 static void
2672 x_scroll_run (struct window *w, struct run *run)
2674 struct frame *f = XFRAME (w->frame);
2675 int x, y, width, height, from_y, to_y, bottom_y;
2676 HWND hwnd = FRAME_W32_WINDOW (f);
2677 HRGN expect_dirty;
2679 /* Get frame-relative bounding box of the text display area of W,
2680 without mode lines. Include in this box the left and right
2681 fringes of W. */
2682 window_box (w, ANY_AREA, &x, &y, &width, &height);
2684 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2685 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2686 bottom_y = y + height;
2688 if (to_y < from_y)
2690 /* Scrolling up. Make sure we don't copy part of the mode
2691 line at the bottom. */
2692 if (from_y + run->height > bottom_y)
2693 height = bottom_y - from_y;
2694 else
2695 height = run->height;
2696 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2698 else
2700 /* Scrolling down. Make sure we don't copy over the mode line.
2701 at the bottom. */
2702 if (to_y + run->height > bottom_y)
2703 height = bottom_y - to_y;
2704 else
2705 height = run->height;
2706 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2709 block_input ();
2711 /* Cursor off. Will be switched on again in x_update_window_end. */
2712 x_clear_cursor (w);
2715 RECT from;
2716 RECT to;
2717 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2718 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2720 from.left = to.left = x;
2721 from.right = to.right = x + width;
2722 from.top = from_y;
2723 from.bottom = from_y + height;
2724 to.top = y;
2725 to.bottom = bottom_y;
2727 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2728 NULL, SW_INVALIDATE);
2730 /* Combine this with what we expect to be dirty. This covers the
2731 case where not all of the region we expect is actually dirty. */
2732 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2734 /* If the dirty region is not what we expected, redraw the entire frame. */
2735 if (!EqualRgn (combined, expect_dirty))
2736 SET_FRAME_GARBAGED (f);
2738 DeleteObject (dirty);
2739 DeleteObject (combined);
2742 unblock_input ();
2743 DeleteObject (expect_dirty);
2748 /***********************************************************************
2749 Exposure Events
2750 ***********************************************************************/
2752 static void
2753 frame_highlight (struct frame *f)
2755 x_update_cursor (f, 1);
2756 x_set_frame_alpha (f);
2759 static void
2760 frame_unhighlight (struct frame *f)
2762 x_update_cursor (f, 1);
2763 x_set_frame_alpha (f);
2766 /* The focus has changed. Update the frames as necessary to reflect
2767 the new situation. Note that we can't change the selected frame
2768 here, because the Lisp code we are interrupting might become confused.
2769 Each event gets marked with the frame in which it occurred, so the
2770 Lisp code can tell when the switch took place by examining the events. */
2772 static void
2773 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2775 struct frame *old_focus = dpyinfo->w32_focus_frame;
2777 if (frame != dpyinfo->w32_focus_frame)
2779 /* Set this before calling other routines, so that they see
2780 the correct value of w32_focus_frame. */
2781 dpyinfo->w32_focus_frame = frame;
2783 if (old_focus && old_focus->auto_lower)
2784 x_lower_frame (old_focus);
2786 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2787 dpyinfo->w32_pending_autoraise_frame = dpyinfo->w32_focus_frame;
2788 else
2789 dpyinfo->w32_pending_autoraise_frame = NULL;
2792 x_frame_rehighlight (dpyinfo);
2796 /* Handle FocusIn and FocusOut state changes for FRAME.
2797 If FRAME has focus and there exists more than one frame, puts
2798 a FOCUS_IN_EVENT into *BUFP. */
2800 static void
2801 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2802 struct frame *frame, struct input_event *bufp)
2804 if (type == WM_SETFOCUS)
2806 if (dpyinfo->w32_focus_event_frame != frame)
2808 x_new_focus_frame (dpyinfo, frame);
2809 dpyinfo->w32_focus_event_frame = frame;
2811 /* Don't stop displaying the initial startup message
2812 for a switch-frame event we don't need. */
2813 if (NILP (Vterminal_frame)
2814 && CONSP (Vframe_list)
2815 && !NILP (XCDR (Vframe_list)))
2817 bufp->arg = Qt;
2819 else
2821 bufp->arg = Qnil;
2824 bufp->kind = FOCUS_IN_EVENT;
2825 XSETFRAME (bufp->frame_or_window, frame);
2828 frame->output_data.x->focus_state |= state;
2830 /* TODO: IME focus? */
2832 else if (type == WM_KILLFOCUS)
2834 frame->output_data.x->focus_state &= ~state;
2836 if (dpyinfo->w32_focus_event_frame == frame)
2838 dpyinfo->w32_focus_event_frame = 0;
2839 x_new_focus_frame (dpyinfo, 0);
2841 bufp->kind = FOCUS_OUT_EVENT;
2842 XSETFRAME (bufp->frame_or_window, frame);
2845 /* TODO: IME focus? */
2850 /* The focus may have changed. Figure out if it is a real focus change,
2851 by checking both FocusIn/Out and Enter/LeaveNotify events.
2853 Returns FOCUS_IN_EVENT event in *BUFP. */
2855 static void
2856 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2857 struct input_event *bufp)
2859 struct frame *frame;
2861 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2862 if (! frame)
2863 return;
2865 /* On w32, this is only called from focus events, so no switch needed. */
2866 x_focus_changed (event->msg.message,
2867 (event->msg.message == WM_KILLFOCUS ?
2868 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2869 dpyinfo, frame, bufp);
2873 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2875 void
2876 x_mouse_leave (struct w32_display_info *dpyinfo)
2878 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2881 /* The focus has changed, or we have redirected a frame's focus to
2882 another frame (this happens when a frame uses a surrogate
2883 mini-buffer frame). Shift the highlight as appropriate.
2885 The FRAME argument doesn't necessarily have anything to do with which
2886 frame is being highlighted or un-highlighted; we only use it to find
2887 the appropriate X display info. */
2889 static void
2890 w32_frame_rehighlight (struct frame *frame)
2892 if (! FRAME_W32_P (frame))
2893 return;
2894 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
2897 static void
2898 x_frame_rehighlight (struct w32_display_info *dpyinfo)
2900 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2902 if (dpyinfo->w32_focus_frame)
2904 dpyinfo->x_highlight_frame
2905 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2906 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2907 : dpyinfo->w32_focus_frame);
2908 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2910 fset_focus_frame (dpyinfo->w32_focus_frame, Qnil);
2911 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2914 else
2915 dpyinfo->x_highlight_frame = 0;
2917 if (dpyinfo->x_highlight_frame != old_highlight)
2919 if (old_highlight)
2920 frame_unhighlight (old_highlight);
2921 if (dpyinfo->x_highlight_frame)
2922 frame_highlight (dpyinfo->x_highlight_frame);
2926 /* Keyboard processing - modifier keys, etc. */
2928 /* Convert a keysym to its name. */
2930 char *
2931 x_get_keysym_name (int keysym)
2933 /* Make static so we can always return it */
2934 static char value[100];
2936 block_input ();
2937 GetKeyNameText (keysym, value, 100);
2938 unblock_input ();
2940 return value;
2943 static int
2944 codepage_for_locale (LCID locale)
2946 char cp[20];
2948 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
2949 return atoi (cp);
2950 else
2951 return CP_ACP;
2955 /* Mouse clicks and mouse movement. Rah. */
2957 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2958 the state in PUP. XBUTTON provides extra information for extended mouse
2959 button messages. Returns FALSE if unable to parse the message. */
2960 BOOL
2961 parse_button (int message, int xbutton, int * pbutton, int * pup)
2963 int button = 0;
2964 int up = 0;
2966 switch (message)
2968 case WM_LBUTTONDOWN:
2969 button = 0;
2970 up = 0;
2971 break;
2972 case WM_LBUTTONUP:
2973 button = 0;
2974 up = 1;
2975 break;
2976 case WM_MBUTTONDOWN:
2977 if (NILP (Vw32_swap_mouse_buttons))
2978 button = 1;
2979 else
2980 button = 2;
2981 up = 0;
2982 break;
2983 case WM_MBUTTONUP:
2984 if (NILP (Vw32_swap_mouse_buttons))
2985 button = 1;
2986 else
2987 button = 2;
2988 up = 1;
2989 break;
2990 case WM_RBUTTONDOWN:
2991 if (NILP (Vw32_swap_mouse_buttons))
2992 button = 2;
2993 else
2994 button = 1;
2995 up = 0;
2996 break;
2997 case WM_RBUTTONUP:
2998 if (NILP (Vw32_swap_mouse_buttons))
2999 button = 2;
3000 else
3001 button = 1;
3002 up = 1;
3003 break;
3004 case WM_XBUTTONDOWN:
3005 button = xbutton + 2;
3006 up = 0;
3007 break;
3008 case WM_XBUTTONUP:
3009 button = xbutton + 2;
3010 up = 1;
3011 break;
3012 default:
3013 return (FALSE);
3016 if (pup) *pup = up;
3017 if (pbutton) *pbutton = button;
3019 return (TRUE);
3023 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3025 If the event is a button press, then note that we have grabbed
3026 the mouse. */
3028 static Lisp_Object
3029 construct_mouse_click (struct input_event *result, W32Msg *msg, struct frame *f)
3031 int button;
3032 int up;
3034 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3035 &button, &up);
3037 /* Make the event type NO_EVENT; we'll change that when we decide
3038 otherwise. */
3039 result->kind = MOUSE_CLICK_EVENT;
3040 result->code = button;
3041 result->timestamp = msg->msg.time;
3042 result->modifiers = (msg->dwModifiers
3043 | (up
3044 ? up_modifier
3045 : down_modifier));
3047 XSETINT (result->x, LOWORD (msg->msg.lParam));
3048 XSETINT (result->y, HIWORD (msg->msg.lParam));
3049 XSETFRAME (result->frame_or_window, f);
3050 result->arg = Qnil;
3051 return Qnil;
3054 static Lisp_Object
3055 construct_mouse_wheel (struct input_event *result, W32Msg *msg, struct frame *f)
3057 POINT p;
3058 int delta;
3060 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3061 : WHEEL_EVENT;
3062 result->code = 0;
3063 result->timestamp = msg->msg.time;
3065 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3066 forward, away from the user (up); a negative value indicates that
3067 the wheel was rotated backward, toward the user (down). */
3068 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3070 /* The up and down modifiers indicate if the wheel was rotated up or
3071 down based on WHEEL_DELTA value. */
3072 result->modifiers = (msg->dwModifiers
3073 | ((delta < 0 ) ? down_modifier : up_modifier));
3075 /* With multiple monitors, we can legitimately get negative
3076 coordinates, so cast to short to interpret them correctly. */
3077 p.x = (short) LOWORD (msg->msg.lParam);
3078 p.y = (short) HIWORD (msg->msg.lParam);
3079 ScreenToClient (msg->msg.hwnd, &p);
3080 XSETINT (result->x, p.x);
3081 XSETINT (result->y, p.y);
3082 XSETFRAME (result->frame_or_window, f);
3083 result->arg = Qnil;
3084 return Qnil;
3087 static Lisp_Object
3088 construct_drag_n_drop (struct input_event *result, W32Msg *msg, struct frame *f)
3090 Lisp_Object files;
3091 Lisp_Object frame;
3092 HDROP hdrop;
3093 POINT p;
3094 WORD num_files;
3095 wchar_t name_w[MAX_PATH];
3096 #ifdef NTGUI_UNICODE
3097 const int use_unicode = 1;
3098 #else
3099 int use_unicode = w32_unicode_filenames;
3100 char name_a[MAX_PATH];
3101 char file[MAX_UTF8_PATH];
3102 #endif
3103 int i;
3105 result->kind = DRAG_N_DROP_EVENT;
3106 result->code = 0;
3107 result->timestamp = msg->msg.time;
3108 result->modifiers = msg->dwModifiers;
3110 hdrop = (HDROP) msg->msg.wParam;
3111 DragQueryPoint (hdrop, &p);
3113 #if 0
3114 p.x = LOWORD (msg->msg.lParam);
3115 p.y = HIWORD (msg->msg.lParam);
3116 ScreenToClient (msg->msg.hwnd, &p);
3117 #endif
3119 XSETINT (result->x, p.x);
3120 XSETINT (result->y, p.y);
3122 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3123 files = Qnil;
3125 for (i = 0; i < num_files; i++)
3127 if (use_unicode)
3129 eassert (DragQueryFileW (hdrop, i, NULL, 0) < MAX_PATH);
3130 /* If DragQueryFile returns zero, it failed to fetch a file
3131 name. */
3132 if (DragQueryFileW (hdrop, i, name_w, MAX_PATH) == 0)
3133 continue;
3134 #ifdef NTGUI_UNICODE
3135 files = Fcons (from_unicode_buffer (name_w), files);
3136 #else
3137 filename_from_utf16 (name_w, file);
3138 files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
3139 #endif /* NTGUI_UNICODE */
3141 #ifndef NTGUI_UNICODE
3142 else
3144 eassert (DragQueryFileA (hdrop, i, NULL, 0) < MAX_PATH);
3145 if (DragQueryFileA (hdrop, i, name_a, MAX_PATH) == 0)
3146 continue;
3147 filename_from_ansi (name_a, file);
3148 files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
3150 #endif
3153 DragFinish (hdrop);
3155 XSETFRAME (frame, f);
3156 result->frame_or_window = frame;
3157 result->arg = files;
3158 return Qnil;
3162 #if HAVE_W32NOTIFY
3164 /* File event notifications (see w32notify.c). */
3166 Lisp_Object
3167 lispy_file_action (DWORD action)
3169 static char unknown_fmt[] = "unknown-action(%d)";
3170 Lisp_Object retval;
3172 switch (action)
3174 case FILE_ACTION_ADDED:
3175 retval = Qadded;
3176 break;
3177 case FILE_ACTION_REMOVED:
3178 retval = Qremoved;
3179 break;
3180 case FILE_ACTION_MODIFIED:
3181 retval = Qmodified;
3182 break;
3183 case FILE_ACTION_RENAMED_OLD_NAME:
3184 retval = Qrenamed_from;
3185 break;
3186 case FILE_ACTION_RENAMED_NEW_NAME:
3187 retval = Qrenamed_to;
3188 break;
3189 default:
3191 char buf[sizeof(unknown_fmt) - 1 + INT_STRLEN_BOUND (DWORD)];
3193 sprintf (buf, unknown_fmt, action);
3194 retval = intern (buf);
3196 break;
3199 return retval;
3202 #ifdef WINDOWSNT
3203 /* Put file notifications into the Emacs input event queue. This
3204 function runs when the WM_EMACS_FILENOTIFY message arrives from a
3205 watcher thread. */
3206 static void
3207 queue_notifications (struct input_event *event, W32Msg *msg, struct frame *f,
3208 int *evcount)
3210 BYTE *p = file_notifications;
3211 FILE_NOTIFY_INFORMATION *fni = (PFILE_NOTIFY_INFORMATION)p;
3212 const DWORD min_size
3213 = offsetof (FILE_NOTIFY_INFORMATION, FileName) + sizeof(wchar_t);
3214 Lisp_Object frame;
3216 /* We cannot process notification before Emacs is fully initialized,
3217 since we need the UTF-16LE coding-system to be set up. */
3218 if (!initialized)
3220 notification_buffer_in_use = 0;
3221 return;
3224 XSETFRAME (frame, f);
3226 enter_crit ();
3227 if (notification_buffer_in_use)
3229 DWORD info_size = notifications_size;
3230 Lisp_Object cs = intern ("utf-16le");
3231 Lisp_Object obj = w32_get_watch_object (notifications_desc);
3233 /* notifications_size could be zero when the buffer of
3234 notifications overflowed on the OS level, or when the
3235 directory being watched was itself deleted. Do nothing in
3236 that case. */
3237 if (info_size
3238 && !NILP (obj) && CONSP (obj))
3240 Lisp_Object callback = XCDR (obj);
3242 while (info_size >= min_size)
3244 Lisp_Object utf_16_fn
3245 = make_unibyte_string ((char *)fni->FileName,
3246 fni->FileNameLength);
3247 /* Note: mule-conf is preloaded, so utf-16le must
3248 already be defined at this point. */
3249 Lisp_Object fname
3250 = code_convert_string_norecord (utf_16_fn, cs, 0);
3251 Lisp_Object action = lispy_file_action (fni->Action);
3253 event->kind = FILE_NOTIFY_EVENT;
3254 event->code
3255 = (ptrdiff_t)XINT (XIL ((EMACS_INT)notifications_desc));
3256 event->timestamp = msg->msg.time;
3257 event->modifiers = 0;
3258 event->frame_or_window = callback;
3259 event->arg = Fcons (action, fname);
3260 kbd_buffer_store_event (event);
3261 (*evcount)++;
3263 if (!fni->NextEntryOffset)
3264 break;
3265 p += fni->NextEntryOffset;
3266 fni = (PFILE_NOTIFY_INFORMATION)p;
3267 info_size -= fni->NextEntryOffset;
3270 notification_buffer_in_use = 0;
3272 else
3273 DebPrint (("We were promised notifications, but in-use flag is zero!\n"));
3274 leave_crit ();
3276 /* We've stuffed all the events ourselves, so w32_read_socket shouldn't. */
3277 event->kind = NO_EVENT;
3279 #endif /* WINDOWSNT */
3280 #endif /* HAVE_W32NOTIFY */
3283 /* Function to report a mouse movement to the mainstream Emacs code.
3284 The input handler calls this.
3286 We have received a mouse movement event, which is given in *event.
3287 If the mouse is over a different glyph than it was last time, tell
3288 the mainstream emacs code by setting mouse_moved. If not, ask for
3289 another motion event, so we can check again the next time it moves. */
3291 static int
3292 note_mouse_movement (struct frame *frame, MSG *msg)
3294 struct w32_display_info *dpyinfo;
3295 int mouse_x = LOWORD (msg->lParam);
3296 int mouse_y = HIWORD (msg->lParam);
3297 RECT *r;
3299 if (!FRAME_X_OUTPUT (frame))
3300 return 0;
3302 dpyinfo = FRAME_DISPLAY_INFO (frame);
3303 dpyinfo->last_mouse_movement_time = msg->time;
3304 dpyinfo->last_mouse_motion_frame = frame;
3305 dpyinfo->last_mouse_motion_x = mouse_x;
3306 dpyinfo->last_mouse_motion_y = mouse_y;
3308 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3310 frame->mouse_moved = 1;
3311 dpyinfo->last_mouse_scroll_bar = NULL;
3312 note_mouse_highlight (frame, -1, -1);
3313 dpyinfo->last_mouse_glyph_frame = NULL;
3314 return 1;
3317 /* Has the mouse moved off the glyph it was on at the last sighting? */
3318 r = &dpyinfo->last_mouse_glyph;
3319 if (frame != dpyinfo->last_mouse_glyph_frame
3320 || mouse_x < r->left || mouse_x >= r->right
3321 || mouse_y < r->top || mouse_y >= r->bottom)
3323 frame->mouse_moved = 1;
3324 dpyinfo->last_mouse_scroll_bar = NULL;
3325 note_mouse_highlight (frame, mouse_x, mouse_y);
3326 /* Remember the mouse position here, as w32_mouse_position only
3327 gets called when mouse tracking is enabled but we also need
3328 to keep track of the mouse for help_echo and highlighting at
3329 other times. */
3330 remember_mouse_glyph (frame, mouse_x, mouse_y, r);
3331 dpyinfo->last_mouse_glyph_frame = frame;
3332 return 1;
3335 return 0;
3339 /************************************************************************
3340 Mouse Face
3341 ************************************************************************/
3343 static struct scroll_bar *x_window_to_scroll_bar (Window, int);
3344 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3345 enum scroll_bar_part *,
3346 Lisp_Object *, Lisp_Object *,
3347 unsigned long *);
3348 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
3349 enum scroll_bar_part *,
3350 Lisp_Object *, Lisp_Object *,
3351 unsigned long *);
3352 static void x_check_fullscreen (struct frame *);
3354 static void
3355 w32_define_cursor (Window window, Cursor cursor)
3357 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3360 /* Return the current position of the mouse.
3361 *fp should be a frame which indicates which display to ask about.
3363 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3364 and *part to the frame, window, and scroll bar part that the mouse
3365 is over. Set *x and *y to the portion and whole of the mouse's
3366 position on the scroll bar.
3368 If the mouse movement started elsewhere, set *fp to the frame the
3369 mouse is on, *bar_window to nil, and *x and *y to the character cell
3370 the mouse is over.
3372 Set *time to the server time-stamp for the time at which the mouse
3373 was at this position.
3375 Don't store anything if we don't have a valid set of values to report.
3377 This clears the mouse_moved flag, so we can wait for the next mouse
3378 movement. */
3380 static void
3381 w32_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3382 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3383 unsigned long *time)
3385 struct frame *f1;
3386 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3388 block_input ();
3390 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3392 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3394 if (bar->horizontal)
3395 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3396 else
3397 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3399 else
3401 POINT pt;
3403 Lisp_Object frame, tail;
3405 /* Clear the mouse-moved flag for every frame on this display. */
3406 FOR_EACH_FRAME (tail, frame)
3407 XFRAME (frame)->mouse_moved = 0;
3409 dpyinfo->last_mouse_scroll_bar = NULL;
3411 GetCursorPos (&pt);
3413 /* Now we have a position on the root; find the innermost window
3414 containing the pointer. */
3416 /* If mouse was grabbed on a frame, give coords for that
3417 frame even if the mouse is now outside it. Otherwise
3418 check for window under mouse on one of our frames. */
3419 f1 = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
3420 : x_any_window_to_frame (dpyinfo, WindowFromPoint (pt)));
3422 /* If not, is it one of our scroll bars? */
3423 if (! f1)
3425 struct scroll_bar *bar
3426 = x_window_to_scroll_bar (WindowFromPoint (pt), 2);
3428 if (bar)
3429 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3432 if (f1 == 0 && insist > 0)
3433 f1 = SELECTED_FRAME ();
3435 if (f1)
3437 /* Ok, we found a frame. Store all the values.
3438 last_mouse_glyph is a rectangle used to reduce the
3439 generation of mouse events. To not miss any motion
3440 events, we must divide the frame into rectangles of the
3441 size of the smallest character that could be displayed
3442 on it, i.e. into the same rectangles that matrices on
3443 the frame are divided into. */
3445 dpyinfo = FRAME_DISPLAY_INFO (f1);
3446 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3447 remember_mouse_glyph (f1, pt.x, pt.y, &dpyinfo->last_mouse_glyph);
3448 dpyinfo->last_mouse_glyph_frame = f1;
3450 *bar_window = Qnil;
3451 *part = 0;
3452 *fp = f1;
3453 XSETINT (*x, pt.x);
3454 XSETINT (*y, pt.y);
3455 *time = dpyinfo->last_mouse_movement_time;
3460 unblock_input ();
3464 /***********************************************************************
3465 Tool-bars
3466 ***********************************************************************/
3468 /* Handle mouse button event on the tool-bar of frame F, at
3469 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
3470 or ButtonRelease. */
3472 static void
3473 w32_handle_tool_bar_click (struct frame *f, struct input_event *button_event)
3475 int x = XFASTINT (button_event->x);
3476 int y = XFASTINT (button_event->y);
3478 if (button_event->modifiers & down_modifier)
3479 handle_tool_bar_click (f, x, y, 1, 0);
3480 else
3481 handle_tool_bar_click (f, x, y, 0,
3482 button_event->modifiers & ~up_modifier);
3487 /***********************************************************************
3488 Scroll bars
3489 ***********************************************************************/
3491 /* Scroll bar support. */
3493 /* Given a window ID, find the struct scroll_bar which manages it
3494 vertically. This can be called in GC, so we have to make sure to
3495 strip off mark bits. */
3497 static struct scroll_bar *
3498 x_window_to_scroll_bar (Window window_id, int type)
3500 Lisp_Object tail, frame;
3502 FOR_EACH_FRAME (tail, frame)
3504 Lisp_Object bar, condemned;
3506 /* Scan this frame's scroll bar list for a scroll bar with the
3507 right window ID. */
3508 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3509 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3510 /* This trick allows us to search both the ordinary and
3511 condemned scroll bar lists with one loop. */
3512 ! NILP (bar) || (bar = condemned,
3513 condemned = Qnil,
3514 ! NILP (bar));
3515 bar = XSCROLL_BAR (bar)->next)
3516 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id
3517 && (type = 2
3518 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
3519 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
3520 return XSCROLL_BAR (bar);
3523 return 0;
3528 /* Set the thumb size and position of vertical scroll bar BAR. We are currently
3529 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3531 static void
3532 w32_set_scroll_bar_thumb (struct scroll_bar *bar,
3533 int portion, int position, int whole)
3535 Window w = SCROLL_BAR_W32_WINDOW (bar);
3536 /* We use the whole scroll-bar height in the calculations below, to
3537 avoid strange effects like scrolling backwards when just clicking
3538 on the handle (without moving it). */
3539 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height)
3540 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3541 int sb_page, sb_pos;
3542 BOOL draggingp = bar->dragging ? TRUE : FALSE;
3543 SCROLLINFO si;
3545 /* We used to change the nPage setting while dragging the handle,
3546 but that had very strange effects (such as scrolling backwards
3547 while dragging downwards).
3549 Now, we don't change the nPage setting while dragging unless we
3550 get near to the end of the buffer, in which case we often have to
3551 resize the handle to "go all the way". */
3553 if (draggingp)
3555 int near_bottom_p;
3556 block_input ();
3557 si.cbSize = sizeof (si);
3558 si.fMask = SIF_POS | SIF_PAGE;
3559 GetScrollInfo (w, SB_CTL, &si);
3560 near_bottom_p = si.nPos + si.nPage >= range;
3561 unblock_input ();
3562 if (!near_bottom_p)
3563 return;
3566 if (whole)
3568 /* Position scroll bar at rock bottom if the bottom of the
3569 buffer is visible. This avoids shrinking the thumb away
3570 to nothing if it is held at the bottom of the buffer. */
3571 if (position + portion >= whole && !draggingp)
3573 sb_page = range * (whole - position) / whole;
3574 sb_pos = range;
3576 else
3578 sb_pos = position * range / whole;
3579 sb_page = (min (portion, (whole - position)) * range) / whole;
3582 else
3584 sb_page = range;
3585 sb_pos = 0;
3588 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3590 block_input ();
3592 si.cbSize = sizeof (si);
3593 si.fMask = SIF_PAGE | SIF_POS;
3594 si.nPage = sb_page;
3595 si.nPos = sb_pos;
3597 SetScrollInfo (w, SB_CTL, &si, TRUE);
3599 unblock_input ();
3602 /* Set the thumb size and position of horizontal scroll bar BAR. We are currently
3603 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3605 static void
3606 w32_set_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3607 int portion, int position, int whole)
3609 Window w = SCROLL_BAR_W32_WINDOW (bar);
3610 SCROLLINFO si;
3612 block_input ();
3614 si.cbSize = sizeof (si);
3615 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
3616 si.nMin = 0;
3617 si.nMax = whole;
3618 /* Allow nPage to be one larger than nPos so we don't allow to scroll
3619 an already fully visible buffer. */
3620 si.nPage = min (portion, si.nMax) + 1;
3621 si.nPos = min (position, si.nMax);
3622 SetScrollInfo (w, SB_CTL, &si, TRUE);
3624 unblock_input ();
3628 /************************************************************************
3629 Scroll bars, general
3630 ************************************************************************/
3632 static HWND
3633 my_create_vscrollbar (struct frame * f, struct scroll_bar * bar)
3635 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3636 WM_EMACS_CREATEVSCROLLBAR, (WPARAM) f,
3637 (LPARAM) bar);
3640 static HWND
3641 my_create_hscrollbar (struct frame * f, struct scroll_bar * bar)
3643 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3644 WM_EMACS_CREATEHSCROLLBAR, (WPARAM) f,
3645 (LPARAM) bar);
3648 /*#define ATTACH_THREADS*/
3650 static BOOL
3651 my_show_window (struct frame *f, HWND hwnd, int how)
3653 #ifndef ATTACH_THREADS
3654 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3655 (WPARAM) hwnd, (LPARAM) how);
3656 #else
3657 return ShowWindow (hwnd, how);
3658 #endif
3661 static void
3662 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3663 int x, int y, int cx, int cy, UINT flags)
3665 #ifndef ATTACH_THREADS
3666 WINDOWPOS pos;
3667 pos.hwndInsertAfter = hwndAfter;
3668 pos.x = x;
3669 pos.y = y;
3670 pos.cx = cx;
3671 pos.cy = cy;
3672 pos.flags = flags;
3673 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3674 #else
3675 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3676 #endif
3679 #if 0
3680 static void
3681 my_set_focus (struct frame * f, HWND hwnd)
3683 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3684 (WPARAM) hwnd, 0);
3686 #endif
3688 static void
3689 my_set_foreground_window (HWND hwnd)
3691 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3695 static void
3696 my_destroy_window (struct frame * f, HWND hwnd)
3698 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3699 (WPARAM) hwnd, 0);
3702 static void
3703 my_bring_window_to_top (HWND hwnd)
3705 SendMessage (hwnd, WM_EMACS_BRINGTOTOP, (WPARAM) hwnd, 0);
3708 /* Create a scroll bar and return the scroll bar vector for it. W is
3709 the Emacs window on which to create the scroll bar. TOP, LEFT,
3710 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
3711 scroll bar. */
3713 static struct scroll_bar *
3714 x_scroll_bar_create (struct window *w, int left, int top, int width, int height, bool horizontal)
3716 struct frame *f = XFRAME (WINDOW_FRAME (w));
3717 HWND hwnd;
3718 SCROLLINFO si;
3719 struct scroll_bar *bar
3720 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, top, PVEC_OTHER);
3721 Lisp_Object barobj;
3723 block_input ();
3725 XSETWINDOW (bar->window, w);
3726 bar->top = top;
3727 bar->left = left;
3728 bar->width = width;
3729 bar->height = height;
3730 bar->start = 0;
3731 bar->end = 0;
3732 bar->dragging = 0;
3733 bar->horizontal = horizontal;
3735 /* Requires geometry to be set before call to create the real window */
3737 if (horizontal)
3738 hwnd = my_create_hscrollbar (f, bar);
3739 else
3740 hwnd = my_create_vscrollbar (f, bar);
3742 si.cbSize = sizeof (si);
3743 si.fMask = SIF_ALL;
3744 si.nMin = 0;
3745 if (horizontal)
3746 si.nMax = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width)
3747 + HORIZONTAL_SCROLL_BAR_MIN_HANDLE;
3748 else
3749 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3750 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3751 si.nPage = si.nMax;
3752 si.nPos = 0;
3754 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3756 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3758 /* Add bar to its frame's list of scroll bars. */
3759 bar->next = FRAME_SCROLL_BARS (f);
3760 bar->prev = Qnil;
3761 XSETVECTOR (barobj, bar);
3762 fset_scroll_bars (f, barobj);
3763 if (! NILP (bar->next))
3764 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3766 unblock_input ();
3768 return bar;
3772 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3773 nil. */
3775 static void
3776 x_scroll_bar_remove (struct scroll_bar *bar)
3778 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3780 block_input ();
3782 /* Destroy the window. */
3783 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3785 /* Dissociate this scroll bar from its window. */
3786 if (bar->horizontal)
3787 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
3788 else
3789 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
3791 unblock_input ();
3794 /* Set the handle of the vertical scroll bar for WINDOW to indicate that
3795 we are displaying PORTION characters out of a total of WHOLE
3796 characters, starting at POSITION. If WINDOW has no vertical scroll
3797 bar, create one. */
3798 static void
3799 w32_set_vertical_scroll_bar (struct window *w,
3800 int portion, int whole, int position)
3802 struct frame *f = XFRAME (w->frame);
3803 Lisp_Object barobj;
3804 struct scroll_bar *bar;
3805 int top, height, left, width;
3806 int window_y, window_height;
3808 /* Get window dimensions. */
3809 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
3810 top = window_y;
3811 height = window_height;
3813 /* Compute the left edge and the width of the scroll bar area. */
3814 left = WINDOW_SCROLL_BAR_AREA_X (w);
3815 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
3817 /* Does the scroll bar exist yet? */
3818 if (NILP (w->vertical_scroll_bar))
3820 HDC hdc;
3821 block_input ();
3822 if (width > 0 && height > 0)
3824 hdc = get_frame_dc (f);
3825 w32_clear_area (f, hdc, left, top, width, height);
3826 release_frame_dc (f, hdc);
3828 unblock_input ();
3830 bar = x_scroll_bar_create (w, left, top, width, height, 0);
3832 else
3834 /* It may just need to be moved and resized. */
3835 HWND hwnd;
3837 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3838 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3840 /* If already correctly positioned, do nothing. */
3841 if (bar->left == left
3842 && bar->top == top
3843 && bar->width == width
3844 && bar->height == height)
3846 /* Redraw after clear_frame. */
3847 if (!my_show_window (f, hwnd, SW_NORMAL))
3848 InvalidateRect (hwnd, NULL, FALSE);
3850 else
3852 HDC hdc;
3853 SCROLLINFO si;
3855 block_input ();
3856 if (width && height)
3858 hdc = get_frame_dc (f);
3859 /* Since Windows scroll bars are smaller than the space reserved
3860 for them on the frame, we have to clear "under" them. */
3861 w32_clear_area (f, hdc, left, top, width, height);
3862 release_frame_dc (f, hdc);
3864 /* Make sure scroll bar is "visible" before moving, to ensure the
3865 area of the parent window now exposed will be refreshed. */
3866 my_show_window (f, hwnd, SW_HIDE);
3867 MoveWindow (hwnd, left, top, width, max (height, 1), TRUE);
3869 si.cbSize = sizeof (si);
3870 si.fMask = SIF_RANGE;
3871 si.nMin = 0;
3872 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3873 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3875 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3877 my_show_window (f, hwnd, SW_NORMAL);
3878 /* InvalidateRect (w, NULL, FALSE); */
3880 /* Remember new settings. */
3881 bar->left = left;
3882 bar->top = top;
3883 bar->width = width;
3884 bar->height = height;
3886 unblock_input ();
3889 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3890 XSETVECTOR (barobj, bar);
3891 wset_vertical_scroll_bar (w, barobj);
3894 /* Set the handle of the horizontal scroll bar for WINDOW to indicate
3895 that we are displaying PORTION characters out of a total of WHOLE
3896 characters, starting at POSITION. If WINDOW has no horizontal scroll
3897 bar, create one. */
3898 static void
3899 w32_set_horizontal_scroll_bar (struct window *w,
3900 int portion, int whole, int position)
3902 struct frame *f = XFRAME (w->frame);
3903 Lisp_Object barobj;
3904 struct scroll_bar *bar;
3905 int top, height, left, width;
3906 int window_x, window_width;
3907 int clear_left = WINDOW_LEFT_EDGE_X (w);
3908 int clear_width = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
3910 /* Get window dimensions. */
3911 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
3912 left = window_x;
3913 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
3914 width = window_width;
3915 top = WINDOW_SCROLL_BAR_AREA_Y (w);
3917 /* Does the scroll bar exist yet? */
3918 if (NILP (w->horizontal_scroll_bar))
3920 HDC hdc;
3921 block_input ();
3922 if (width > 0 && height > 0)
3924 hdc = get_frame_dc (f);
3925 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
3926 release_frame_dc (f, hdc);
3928 unblock_input ();
3930 bar = x_scroll_bar_create (w, left, top, width, height, 1);
3932 else
3934 /* It may just need to be moved and resized. */
3935 HWND hwnd;
3937 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
3938 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3940 /* If already correctly positioned, do nothing. */
3941 if (bar->left == left && bar->top == top
3942 && bar->width == width && bar->height == height)
3944 /* Redraw after clear_frame. */
3945 if (!my_show_window (f, hwnd, SW_NORMAL))
3946 InvalidateRect (hwnd, NULL, FALSE);
3948 else
3950 HDC hdc;
3951 SCROLLINFO si;
3953 block_input ();
3954 if (width && height)
3956 hdc = get_frame_dc (f);
3957 /* Since Windows scroll bars are smaller than the space reserved
3958 for them on the frame, we have to clear "under" them. */
3959 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
3960 release_frame_dc (f, hdc);
3962 /* Make sure scroll bar is "visible" before moving, to ensure the
3963 area of the parent window now exposed will be refreshed. */
3964 my_show_window (f, hwnd, SW_HIDE);
3965 MoveWindow (hwnd, left, top, width, max (height, 1), TRUE);
3967 /* +++ SetScrollInfo +++ */
3968 si.cbSize = sizeof (si);
3969 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
3970 si.nMin = 0;
3971 si.nMax = whole;
3972 si.nPage = min (portion, si.nMax) + 1;
3973 si.nPos = min (position, si.nMax);
3974 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3976 my_show_window (f, hwnd, SW_NORMAL);
3977 /* InvalidateRect (w, NULL, FALSE); */
3979 /* Remember new settings. */
3980 bar->left = left;
3981 bar->top = top;
3982 bar->width = width;
3983 bar->height = height;
3985 unblock_input ();
3989 w32_set_horizontal_scroll_bar_thumb (bar, portion, position, whole);
3990 XSETVECTOR (barobj, bar);
3991 wset_horizontal_scroll_bar (w, barobj);
3995 /* The following three hooks are used when we're doing a thorough
3996 redisplay of the frame. We don't explicitly know which scroll bars
3997 are going to be deleted, because keeping track of when windows go
3998 away is a real pain - "Can you say set-window-configuration, boys
3999 and girls?" Instead, we just assert at the beginning of redisplay
4000 that *all* scroll bars are to be removed, and then save a scroll bar
4001 from the fiery pit when we actually redisplay its window. */
4003 /* Arrange for all scroll bars on FRAME to be removed at the next call
4004 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
4005 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
4007 static void
4008 w32_condemn_scroll_bars (struct frame *frame)
4010 if (!NILP (FRAME_SCROLL_BARS (frame)))
4012 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
4014 /* Prepend scrollbars to already condemned ones. */
4015 Lisp_Object last = FRAME_SCROLL_BARS (frame);
4017 while (!NILP (XSCROLL_BAR (last)->next))
4018 last = XSCROLL_BAR (last)->next;
4020 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
4021 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
4024 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
4025 fset_scroll_bars (frame, Qnil);
4030 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
4031 Note that WINDOW isn't necessarily condemned at all. */
4033 static void
4034 w32_redeem_scroll_bar (struct window *w)
4036 struct scroll_bar *bar;
4037 Lisp_Object barobj;
4038 struct frame *f;
4040 /* We can't redeem this window's scroll bar if it doesn't have one. */
4041 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
4042 emacs_abort ();
4044 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4046 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4047 /* Unlink it from the condemned list. */
4048 f = XFRAME (WINDOW_FRAME (w));
4049 if (NILP (bar->prev))
4051 /* If the prev pointer is nil, it must be the first in one of
4052 the lists. */
4053 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
4054 /* It's not condemned. Everything's fine. */
4055 goto horizontal;
4056 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4057 w->vertical_scroll_bar))
4058 fset_condemned_scroll_bars (f, bar->next);
4059 else
4060 /* If its prev pointer is nil, it must be at the front of
4061 one or the other! */
4062 emacs_abort ();
4064 else
4065 XSCROLL_BAR (bar->prev)->next = bar->next;
4067 if (! NILP (bar->next))
4068 XSCROLL_BAR (bar->next)->prev = bar->prev;
4070 bar->next = FRAME_SCROLL_BARS (f);
4071 bar->prev = Qnil;
4072 XSETVECTOR (barobj, bar);
4073 fset_scroll_bars (f, barobj);
4074 if (! NILP (bar->next))
4075 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4078 horizontal:
4079 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
4081 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4082 /* Unlink it from the condemned list. */
4083 f = XFRAME (WINDOW_FRAME (w));
4084 if (NILP (bar->prev))
4086 /* If the prev pointer is nil, it must be the first in one of
4087 the lists. */
4088 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
4089 /* It's not condemned. Everything's fine. */
4090 return;
4091 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
4092 w->horizontal_scroll_bar))
4093 fset_condemned_scroll_bars (f, bar->next);
4094 else
4095 /* If its prev pointer is nil, it must be at the front of
4096 one or the other! */
4097 emacs_abort ();
4099 else
4100 XSCROLL_BAR (bar->prev)->next = bar->next;
4102 if (! NILP (bar->next))
4103 XSCROLL_BAR (bar->next)->prev = bar->prev;
4105 bar->next = FRAME_SCROLL_BARS (f);
4106 bar->prev = Qnil;
4107 XSETVECTOR (barobj, bar);
4108 fset_scroll_bars (f, barobj);
4109 if (! NILP (bar->next))
4110 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4114 /* Remove all scroll bars on FRAME that haven't been saved since the
4115 last call to `*condemn_scroll_bars_hook'. */
4117 static void
4118 w32_judge_scroll_bars (struct frame *f)
4120 Lisp_Object bar, next;
4122 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
4124 /* Clear out the condemned list now so we won't try to process any
4125 more events on the hapless scroll bars. */
4126 fset_condemned_scroll_bars (f, Qnil);
4128 for (; ! NILP (bar); bar = next)
4130 struct scroll_bar *b = XSCROLL_BAR (bar);
4132 x_scroll_bar_remove (b);
4134 next = b->next;
4135 b->next = b->prev = Qnil;
4138 /* Now there should be no references to the condemned scroll bars,
4139 and they should get garbage-collected. */
4142 /* Handle a mouse click on the vertical scroll bar BAR. If
4143 *EMACS_EVENT's kind is set to something other than NO_EVENT, it is
4144 enqueued.
4146 This may be called from a signal handler, so we have to ignore GC
4147 mark bits. */
4149 static int
4150 w32_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4151 struct input_event *emacs_event)
4153 if (! WINDOWP (bar->window))
4154 emacs_abort ();
4156 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
4157 emacs_event->code = 0;
4158 /* not really meaningful to distinguish up/down */
4159 emacs_event->modifiers = msg->dwModifiers;
4160 emacs_event->frame_or_window = bar->window;
4161 emacs_event->arg = Qnil;
4162 emacs_event->timestamp = msg->msg.time;
4165 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4166 int y;
4167 int dragging = bar->dragging;
4168 SCROLLINFO si;
4170 si.cbSize = sizeof (si);
4171 si.fMask = SIF_POS;
4173 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4174 y = si.nPos;
4176 bar->dragging = 0;
4177 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4179 switch (LOWORD (msg->msg.wParam))
4181 case SB_LINEDOWN:
4182 emacs_event->part = scroll_bar_down_arrow;
4183 break;
4184 case SB_LINEUP:
4185 emacs_event->part = scroll_bar_up_arrow;
4186 break;
4187 case SB_PAGEUP:
4188 emacs_event->part = scroll_bar_above_handle;
4189 break;
4190 case SB_PAGEDOWN:
4191 emacs_event->part = scroll_bar_below_handle;
4192 break;
4193 case SB_TOP:
4194 emacs_event->part = scroll_bar_handle;
4195 y = 0;
4196 break;
4197 case SB_BOTTOM:
4198 emacs_event->part = scroll_bar_handle;
4199 y = top_range;
4200 break;
4201 case SB_THUMBTRACK:
4202 case SB_THUMBPOSITION:
4203 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height) <= 0xffff)
4204 y = HIWORD (msg->msg.wParam);
4205 bar->dragging = 1; /* ??????? */
4206 emacs_event->part = scroll_bar_handle;
4208 /* "Silently" update current position. */
4210 SCROLLINFO si;
4212 si.cbSize = sizeof (si);
4213 si.fMask = SIF_POS;
4214 si.nPos = y;
4215 /* Remember apparent position (we actually lag behind the real
4216 position, so don't set that directly). */
4217 last_scroll_bar_drag_pos = y;
4219 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4221 break;
4222 case SB_ENDSCROLL:
4223 /* If this is the end of a drag sequence, then reset the scroll
4224 handle size to normal and do a final redraw. Otherwise do
4225 nothing. */
4226 if (dragging)
4228 SCROLLINFO si;
4229 int start = bar->start;
4230 int end = bar->end;
4232 si.cbSize = sizeof (si);
4233 si.fMask = SIF_PAGE | SIF_POS;
4234 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
4235 si.nPos = last_scroll_bar_drag_pos;
4236 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4238 /* fall through */
4239 default:
4240 emacs_event->kind = NO_EVENT;
4241 return FALSE;
4244 XSETINT (emacs_event->x, y);
4245 XSETINT (emacs_event->y, top_range);
4247 return TRUE;
4251 /* Handle a mouse click on the horizontal scroll bar BAR. If
4252 *EMACS_EVENT's kind is set to something other than NO_EVENT, it is
4253 enqueued.
4255 This may be called from a signal handler, so we have to ignore GC
4256 mark bits. */
4258 static int
4259 w32_horizontal_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4260 struct input_event *emacs_event)
4262 if (! WINDOWP (bar->window))
4263 emacs_abort ();
4265 emacs_event->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4266 emacs_event->code = 0;
4267 /* not really meaningful to distinguish left/right */
4268 emacs_event->modifiers = msg->dwModifiers;
4269 emacs_event->frame_or_window = bar->window;
4270 emacs_event->arg = Qnil;
4271 emacs_event->timestamp = msg->msg.time;
4274 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4275 int x;
4276 int dragging = bar->dragging;
4277 SCROLLINFO si;
4279 si.cbSize = sizeof (si);
4280 si.fMask = SIF_POS;
4282 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4283 x = si.nPos;
4285 bar->dragging = 0;
4286 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4288 switch (LOWORD (msg->msg.wParam))
4290 case SB_LINELEFT:
4291 emacs_event->part = scroll_bar_left_arrow;
4292 break;
4293 case SB_LINERIGHT:
4294 emacs_event->part = scroll_bar_right_arrow;
4295 break;
4296 case SB_PAGELEFT:
4297 emacs_event->part = scroll_bar_before_handle;
4298 break;
4299 case SB_PAGERIGHT:
4300 emacs_event->part = scroll_bar_after_handle;
4301 break;
4302 case SB_LEFT:
4303 emacs_event->part = scroll_bar_horizontal_handle;
4304 x = 0;
4305 break;
4306 case SB_RIGHT:
4307 emacs_event->part = scroll_bar_horizontal_handle;
4308 x = left_range;
4309 break;
4310 case SB_THUMBTRACK:
4311 case SB_THUMBPOSITION:
4312 if (HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width) <= 0xffff)
4313 x = HIWORD (msg->msg.wParam);
4314 bar->dragging = 1;
4315 emacs_event->part = scroll_bar_horizontal_handle;
4317 /* "Silently" update current position. */
4319 SCROLLINFO si;
4321 si.cbSize = sizeof (si);
4322 si.fMask = SIF_POS;
4323 si.nPos = min (x, XWINDOW (bar->window)->hscroll_whole - 1);
4324 /* Remember apparent position (we actually lag behind the real
4325 position, so don't set that directly). */
4326 last_scroll_bar_drag_pos = x;
4328 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4330 break;
4331 case SB_ENDSCROLL:
4332 /* If this is the end of a drag sequence, then reset the scroll
4333 handle size to normal and do a final redraw. Otherwise do
4334 nothing. */
4335 if (dragging)
4337 SCROLLINFO si;
4338 int start = bar->start;
4339 int end = bar->end;
4341 si.cbSize = sizeof (si);
4342 /** si.fMask = SIF_PAGE | SIF_POS; **/
4343 si.fMask = SIF_POS;
4344 /** si.nPage = end - start + HORIZONTAL_SCROLL_BAR_MIN_HANDLE; **/
4345 si.nPos = min (last_scroll_bar_drag_pos,
4346 XWINDOW (bar->window)->hscroll_whole - 1);
4347 /** si.nPos = last_scroll_bar_drag_pos; **/
4348 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4350 /* fall through */
4351 default:
4352 emacs_event->kind = NO_EVENT;
4353 return FALSE;
4356 XSETINT (emacs_event->x, x);
4357 XSETINT (emacs_event->y, left_range);
4359 return TRUE;
4363 /* Return information to the user about the current position of the mouse
4364 on the vertical scroll bar. */
4365 static void
4366 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4367 enum scroll_bar_part *part,
4368 Lisp_Object *x, Lisp_Object *y,
4369 unsigned long *time)
4371 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4372 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4373 Window w = SCROLL_BAR_W32_WINDOW (bar);
4374 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4375 int pos;
4376 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4377 SCROLLINFO si;
4379 block_input ();
4381 *fp = f;
4382 *bar_window = bar->window;
4384 si.cbSize = sizeof (si);
4385 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4387 GetScrollInfo (w, SB_CTL, &si);
4388 pos = si.nPos;
4389 top_range = si.nMax - si.nPage + 1;
4391 switch (LOWORD (dpyinfo->last_mouse_scroll_bar_pos))
4393 case SB_THUMBPOSITION:
4394 case SB_THUMBTRACK:
4395 *part = scroll_bar_handle;
4396 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height) <= 0xffff)
4397 pos = HIWORD (dpyinfo->last_mouse_scroll_bar_pos);
4398 break;
4399 case SB_LINEDOWN:
4400 *part = scroll_bar_handle;
4401 pos++;
4402 break;
4403 default:
4404 *part = scroll_bar_handle;
4405 break;
4408 XSETINT (*x, pos);
4409 XSETINT (*y, top_range);
4411 f->mouse_moved = 0;
4412 dpyinfo->last_mouse_scroll_bar = NULL;
4414 *time = dpyinfo->last_mouse_movement_time;
4416 unblock_input ();
4419 /* Return information to the user about the current position of the mouse
4420 on the horizontal scroll bar. */
4421 static void
4422 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4423 enum scroll_bar_part *part,
4424 Lisp_Object *x, Lisp_Object *y,
4425 unsigned long *time)
4427 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4428 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4429 Window w = SCROLL_BAR_W32_WINDOW (bar);
4430 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4431 int pos;
4432 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4433 SCROLLINFO si;
4435 block_input ();
4437 *fp = f;
4438 *bar_window = bar->window;
4440 si.cbSize = sizeof (si);
4441 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4443 GetScrollInfo (w, SB_CTL, &si);
4444 pos = si.nPos;
4445 left_range = si.nMax - si.nPage + 1;
4447 switch (LOWORD (dpyinfo->last_mouse_scroll_bar_pos))
4449 case SB_THUMBPOSITION:
4450 case SB_THUMBTRACK:
4451 *part = scroll_bar_handle;
4452 if (HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width) <= 0xffff)
4453 pos = HIWORD (dpyinfo->last_mouse_scroll_bar_pos);
4454 break;
4455 case SB_LINERIGHT:
4456 *part = scroll_bar_handle;
4457 pos++;
4458 break;
4459 default:
4460 *part = scroll_bar_handle;
4461 break;
4464 XSETINT (*y, pos);
4465 XSETINT (*x, left_range);
4467 f->mouse_moved = 0;
4468 dpyinfo->last_mouse_scroll_bar = NULL;
4470 *time = dpyinfo->last_mouse_movement_time;
4472 unblock_input ();
4476 /* The screen has been cleared so we may have changed foreground or
4477 background colors, and the scroll bars may need to be redrawn.
4478 Clear out the scroll bars, and ask for expose events, so we can
4479 redraw them. */
4481 void
4482 x_scroll_bar_clear (struct frame *f)
4484 Lisp_Object bar;
4486 /* We can have scroll bars even if this is 0,
4487 if we just turned off scroll bar mode.
4488 But in that case we should not clear them. */
4489 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4490 || FRAME_HAS_HORIZONTAL_SCROLL_BARS (f))
4491 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4492 bar = XSCROLL_BAR (bar)->next)
4494 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4495 HDC hdc = GetDC (window);
4496 RECT rect;
4498 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4499 arranges to refresh the scroll bar if hidden. */
4500 my_show_window (f, window, SW_HIDE);
4502 GetClientRect (window, &rect);
4503 select_palette (f, hdc);
4504 w32_clear_rect (f, hdc, &rect);
4505 deselect_palette (f, hdc);
4507 ReleaseDC (window, hdc);
4511 static void
4512 set_frame_param (struct frame *f, Lisp_Object prop, Lisp_Object val)
4514 register Lisp_Object old_alist_elt;
4516 old_alist_elt = Fassq (prop, f->param_alist);
4517 if (EQ (old_alist_elt, Qnil))
4518 fset_param_alist (f, Fcons (Fcons (prop, val), f->param_alist));
4519 else
4520 Fsetcdr (old_alist_elt, val);
4523 /* The main W32 event-reading loop - w32_read_socket. */
4525 /* Record the last 100 characters stored
4526 to help debug the loss-of-chars-during-GC problem. */
4528 static int temp_index;
4529 static short temp_buffer[100];
4531 /* Temporarily store lead byte of DBCS input sequences. */
4532 static char dbcs_lead = 0;
4534 /* Read events coming from the W32 shell.
4535 This routine is called by the SIGIO handler.
4536 We return as soon as there are no more events to be read.
4538 For an overview of how Emacs input works on MS-Windows, see the
4539 commentary before w32_msg_pump in w32fns.c.
4541 We return the number of characters stored into the buffer,
4542 thus pretending to be `read'.
4544 Some of these messages are reposted back to the message queue since the
4545 system calls the windows proc directly in a context where we cannot return
4546 the data nor can we guarantee the state we are in. So if we dispatch them
4547 we will get into an infinite loop. To prevent this from ever happening we
4548 will set a variable to indicate we are in the read_socket call and indicate
4549 which message we are processing since the windows proc gets called
4550 recursively with different messages by the system.
4553 static int
4554 w32_read_socket (struct terminal *terminal,
4555 struct input_event *hold_quit)
4557 int count = 0;
4558 int check_visibility = 0;
4559 W32Msg msg;
4560 struct frame *f;
4561 struct w32_display_info *dpyinfo = &one_w32_display_info;
4562 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
4564 block_input ();
4566 /* So people can tell when we have read the available input. */
4567 input_signal_count++;
4569 /* Process any incoming thread messages. */
4570 drain_message_queue ();
4572 /* TODO: ghostscript integration. */
4573 while (get_next_msg (&msg, FALSE))
4575 struct input_event inev;
4576 int do_help = 0;
4578 /* DebPrint (("w32_read_socket: %s time:%u\n", */
4579 /* w32_name_of_message (msg.msg.message), */
4580 /* msg.msg.time)); */
4582 EVENT_INIT (inev);
4583 inev.kind = NO_EVENT;
4584 inev.arg = Qnil;
4586 switch (msg.msg.message)
4588 case WM_EMACS_PAINT:
4589 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4591 if (f)
4593 if (msg.rect.right == msg.rect.left ||
4594 msg.rect.bottom == msg.rect.top)
4596 /* We may get paint messages even though the client
4597 area is clipped - these are not expose events. */
4598 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4599 SDATA (f->name)));
4601 else if (FRAME_VISIBLE_P (f) != 1)
4603 bool iconified = FRAME_ICONIFIED_P (f);
4605 /* Definitely not obscured, so mark as visible. */
4606 SET_FRAME_VISIBLE (f, 1);
4607 SET_FRAME_ICONIFIED (f, 0);
4608 SET_FRAME_GARBAGED (f);
4609 if (!f->output_data.w32->asked_for_visible)
4610 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4611 SDATA (f->name)));
4613 /* WM_PAINT serves as MapNotify as well, so report
4614 visibility changes properly. */
4615 if (iconified)
4617 inev.kind = DEICONIFY_EVENT;
4618 XSETFRAME (inev.frame_or_window, f);
4620 else if (!NILP (Vframe_list) && !NILP (XCDR (Vframe_list)))
4621 /* Force a redisplay sooner or later to update the
4622 frame titles in case this is the second frame. */
4623 record_asynch_buffer_change ();
4625 else
4627 HDC hdc = get_frame_dc (f);
4629 /* Erase background again for safety. */
4630 w32_clear_rect (f, hdc, &msg.rect);
4631 release_frame_dc (f, hdc);
4632 expose_frame (f,
4633 msg.rect.left,
4634 msg.rect.top,
4635 msg.rect.right - msg.rect.left,
4636 msg.rect.bottom - msg.rect.top);
4639 break;
4641 case WM_INPUTLANGCHANGE:
4642 /* Generate a language change event. */
4643 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4645 /* lParam contains the input language ID in its low 16 bits.
4646 Use it to update our record of the keyboard codepage. */
4647 w32_keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4648 & 0xffff));
4650 if (f)
4652 inev.kind = LANGUAGE_CHANGE_EVENT;
4653 XSETFRAME (inev.frame_or_window, f);
4654 inev.code = w32_keyboard_codepage;
4655 inev.modifiers = msg.msg.lParam & 0xffff;
4657 break;
4659 case WM_KEYDOWN:
4660 case WM_SYSKEYDOWN:
4661 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4663 if (f && !FRAME_ICONIFIED_P (f))
4665 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4666 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4668 clear_mouse_face (hlinfo);
4669 hlinfo->mouse_face_hidden = 1;
4672 if (temp_index == sizeof temp_buffer / sizeof (short))
4673 temp_index = 0;
4674 temp_buffer[temp_index++] = msg.msg.wParam;
4675 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4676 inev.code = msg.msg.wParam;
4677 inev.modifiers = msg.dwModifiers;
4678 XSETFRAME (inev.frame_or_window, f);
4679 inev.timestamp = msg.msg.time;
4681 break;
4683 case WM_UNICHAR:
4684 case WM_SYSCHAR:
4685 case WM_CHAR:
4686 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4688 if (f && !FRAME_ICONIFIED_P (f))
4690 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4691 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4693 clear_mouse_face (hlinfo);
4694 hlinfo->mouse_face_hidden = 1;
4697 if (temp_index == sizeof temp_buffer / sizeof (short))
4698 temp_index = 0;
4699 temp_buffer[temp_index++] = msg.msg.wParam;
4701 inev.modifiers = msg.dwModifiers;
4702 XSETFRAME (inev.frame_or_window, f);
4703 inev.timestamp = msg.msg.time;
4705 if (msg.msg.message == WM_UNICHAR)
4707 inev.code = msg.msg.wParam;
4709 else if (msg.msg.wParam < 256)
4711 wchar_t code;
4712 char dbcs[2];
4713 dbcs[0] = 0;
4714 dbcs[1] = (char) msg.msg.wParam;
4716 if (dbcs_lead)
4718 dbcs[0] = dbcs_lead;
4719 dbcs_lead = 0;
4720 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4721 dbcs, 2, &code, 1))
4723 /* Garbage */
4724 DebPrint (("Invalid DBCS sequence: %d %d\n",
4725 dbcs[0], dbcs[1]));
4726 inev.kind = NO_EVENT;
4727 break;
4730 else if (IsDBCSLeadByteEx (w32_keyboard_codepage,
4731 (BYTE) msg.msg.wParam))
4733 dbcs_lead = (char) msg.msg.wParam;
4734 inev.kind = NO_EVENT;
4735 break;
4737 else
4739 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4740 &dbcs[1], 1, &code, 1))
4742 /* What to do with garbage? */
4743 DebPrint (("Invalid character: %d\n", dbcs[1]));
4744 inev.kind = NO_EVENT;
4745 break;
4748 inev.code = code;
4750 else
4752 /* Windows shouldn't generate WM_CHAR events above 0xFF
4753 in non-Unicode message handlers. */
4754 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4755 inev.kind = NO_EVENT;
4756 break;
4758 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4759 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4761 break;
4763 case WM_APPCOMMAND:
4764 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4766 if (f && !FRAME_ICONIFIED_P (f))
4768 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4769 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4771 clear_mouse_face (hlinfo);
4772 hlinfo->mouse_face_hidden = 1;
4775 if (temp_index == sizeof temp_buffer / sizeof (short))
4776 temp_index = 0;
4777 temp_buffer[temp_index++] = msg.msg.wParam;
4778 inev.kind = MULTIMEDIA_KEY_EVENT;
4779 inev.code = GET_APPCOMMAND_LPARAM (msg.msg.lParam);
4780 inev.modifiers = msg.dwModifiers;
4781 XSETFRAME (inev.frame_or_window, f);
4782 inev.timestamp = msg.msg.time;
4784 break;
4786 case WM_MOUSEMOVE:
4787 /* Ignore non-movement. */
4789 int x = LOWORD (msg.msg.lParam);
4790 int y = HIWORD (msg.msg.lParam);
4791 if (x == last_mousemove_x && y == last_mousemove_y)
4792 break;
4793 last_mousemove_x = x;
4794 last_mousemove_y = y;
4797 previous_help_echo_string = help_echo_string;
4798 help_echo_string = Qnil;
4800 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4801 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4803 if (hlinfo->mouse_face_hidden)
4805 hlinfo->mouse_face_hidden = 0;
4806 clear_mouse_face (hlinfo);
4809 if (f)
4811 /* Generate SELECT_WINDOW_EVENTs when needed. */
4812 if (!NILP (Vmouse_autoselect_window))
4814 static Lisp_Object last_mouse_window;
4815 Lisp_Object window = window_from_coordinates
4816 (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0);
4818 /* Window will be selected only when it is not
4819 selected now and last mouse movement event was
4820 not in it. Minibuffer window will be selected
4821 only when it is active. */
4822 if (WINDOWP (window)
4823 && !EQ (window, last_mouse_window)
4824 && !EQ (window, selected_window)
4825 /* For click-to-focus window managers
4826 create event iff we don't leave the
4827 selected frame. */
4828 && (focus_follows_mouse
4829 || (EQ (XWINDOW (window)->frame,
4830 XWINDOW (selected_window)->frame))))
4832 inev.kind = SELECT_WINDOW_EVENT;
4833 inev.frame_or_window = window;
4835 /* Remember the last window where we saw the mouse. */
4836 last_mouse_window = window;
4838 if (!note_mouse_movement (f, &msg.msg))
4839 help_echo_string = previous_help_echo_string;
4841 else
4843 /* If we move outside the frame, then we're
4844 certainly no longer on any text in the frame. */
4845 clear_mouse_face (hlinfo);
4848 /* If the contents of the global variable help_echo_string
4849 has changed, generate a HELP_EVENT. */
4850 #if 0 /* The below is an invalid comparison when CHECK_LISP_OBJECT_TYPE.
4851 But it was originally changed to this to fix a bug, so I have
4852 not removed it completely in case the bug is still there. */
4853 if (help_echo_string != previous_help_echo_string ||
4854 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4855 #else /* This is what xterm.c does. */
4856 if (!NILP (help_echo_string)
4857 || !NILP (previous_help_echo_string))
4858 do_help = 1;
4859 #endif
4860 break;
4862 case WM_LBUTTONDOWN:
4863 case WM_LBUTTONUP:
4864 case WM_MBUTTONDOWN:
4865 case WM_MBUTTONUP:
4866 case WM_RBUTTONDOWN:
4867 case WM_RBUTTONUP:
4868 case WM_XBUTTONDOWN:
4869 case WM_XBUTTONUP:
4871 /* If we decide we want to generate an event to be seen
4872 by the rest of Emacs, we put it here. */
4873 bool tool_bar_p = 0;
4874 int button;
4875 int up;
4877 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4878 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4880 if (f)
4882 construct_mouse_click (&inev, &msg, f);
4884 /* Is this in the tool-bar? */
4885 if (WINDOWP (f->tool_bar_window)
4886 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4888 Lisp_Object window;
4889 int x = XFASTINT (inev.x);
4890 int y = XFASTINT (inev.y);
4892 window = window_from_coordinates (f, x, y, 0, 1);
4894 if (EQ (window, f->tool_bar_window))
4896 w32_handle_tool_bar_click (f, &inev);
4897 tool_bar_p = 1;
4901 if (tool_bar_p
4902 || (dpyinfo->w32_focus_frame
4903 && f != dpyinfo->w32_focus_frame))
4904 inev.kind = NO_EVENT;
4907 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4908 &button, &up);
4910 if (up)
4912 dpyinfo->grabbed &= ~ (1 << button);
4914 else
4916 dpyinfo->grabbed |= (1 << button);
4917 dpyinfo->last_mouse_frame = f;
4918 /* Ignore any mouse motion that happened
4919 before this event; any subsequent mouse-movement
4920 Emacs events should reflect only motion after
4921 the ButtonPress. */
4922 if (f != 0)
4924 f->mouse_moved = 0;
4925 if (!tool_bar_p)
4926 f->last_tool_bar_item = -1;
4929 break;
4932 case WM_MOUSEWHEEL:
4933 case WM_MOUSEHWHEEL:
4935 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4936 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4938 if (f)
4941 if (!dpyinfo->w32_focus_frame
4942 || f == dpyinfo->w32_focus_frame)
4944 /* Emit an Emacs wheel-up/down event. */
4945 construct_mouse_wheel (&inev, &msg, f);
4947 /* Ignore any mouse motion that happened before this
4948 event; any subsequent mouse-movement Emacs events
4949 should reflect only motion after the
4950 ButtonPress. */
4951 f->mouse_moved = 0;
4952 f->last_tool_bar_item = -1;
4954 dpyinfo->last_mouse_frame = f;
4956 break;
4958 case WM_DROPFILES:
4959 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4961 if (f)
4962 construct_drag_n_drop (&inev, &msg, f);
4963 break;
4965 case WM_HSCROLL:
4967 struct scroll_bar *bar =
4968 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 1);
4970 if (bar)
4971 w32_horizontal_scroll_bar_handle_click (bar, &msg, &inev);
4972 break;
4975 case WM_VSCROLL:
4977 struct scroll_bar *bar =
4978 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 0);
4980 if (bar)
4981 w32_scroll_bar_handle_click (bar, &msg, &inev);
4982 break;
4985 case WM_WINDOWPOSCHANGED:
4986 case WM_ACTIVATE:
4987 case WM_ACTIVATEAPP:
4988 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4989 if (f)
4991 /* Run the full-screen hook function also when we are
4992 being activated, to actually install the required
4993 size in effect, if the WAIT flag is set. This is
4994 because when the hook is run from x_set_fullscreen,
4995 the frame might not yet be visible, if that call is a
4996 result of make-frame, and in that case the hook just
4997 sets the WAIT flag. */
4998 if ((msg.msg.message == WM_WINDOWPOSCHANGED || msg.msg.wParam)
4999 && (f->want_fullscreen & FULLSCREEN_WAIT))
5000 w32fullscreen_hook (f);
5001 x_check_fullscreen (f);
5003 check_visibility = 1;
5004 break;
5006 case WM_MOVE:
5007 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5009 if (f && !FRAME_ICONIFIED_P (f))
5010 x_real_positions (f, &f->left_pos, &f->top_pos);
5012 check_visibility = 1;
5013 break;
5015 case WM_SHOWWINDOW:
5016 /* wParam non-zero means Window is about to be shown, 0 means
5017 about to be hidden. */
5018 /* Redo the mouse-highlight after the tooltip has gone. */
5019 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
5021 tip_window = NULL;
5022 x_redo_mouse_highlight (dpyinfo);
5025 /* If window has been obscured or exposed by another window
5026 being maximized or minimized/restored, then recheck
5027 visibility of all frames. Direct changes to our own
5028 windows get handled by WM_SIZE. */
5029 #if 0
5030 if (msg.msg.lParam != 0)
5031 check_visibility = 1;
5032 else
5034 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5035 f->async_visible = msg.msg.wParam;
5037 #endif
5039 check_visibility = 1;
5040 break;
5042 case WM_SIZE:
5043 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5045 /* Inform lisp of whether frame has been iconified etc. */
5046 if (f)
5048 switch (msg.msg.wParam)
5050 case SIZE_MINIMIZED:
5051 SET_FRAME_VISIBLE (f, 0);
5052 SET_FRAME_ICONIFIED (f, 1);
5054 inev.kind = ICONIFY_EVENT;
5055 XSETFRAME (inev.frame_or_window, f);
5056 break;
5058 case SIZE_MAXIMIZED:
5060 bool iconified = FRAME_ICONIFIED_P (f);
5062 SET_FRAME_VISIBLE (f, 1);
5063 SET_FRAME_ICONIFIED (f, 0);
5065 /* wait_reading_process_output will notice this
5066 and update the frame's display structures. */
5067 SET_FRAME_GARBAGED (f);
5069 if (iconified)
5071 int x, y;
5073 /* Reset top and left positions of the Window
5074 here since Windows sends a WM_MOVE message
5075 BEFORE telling us the Window is minimized
5076 when the Window is iconified, with 3000,3000
5077 as the co-ords. */
5078 x_real_positions (f, &x, &y);
5079 f->left_pos = x;
5080 f->top_pos = y;
5082 inev.kind = DEICONIFY_EVENT;
5083 XSETFRAME (inev.frame_or_window, f);
5085 else if (! NILP (Vframe_list)
5086 && ! NILP (XCDR (Vframe_list)))
5087 /* Force a redisplay sooner or later
5088 to update the frame titles
5089 in case this is the second frame. */
5090 record_asynch_buffer_change ();
5093 if (EQ (get_frame_param (f, Qfullscreen), Qnil))
5094 set_frame_param (f, Qfullscreen, Qmaximized);
5095 else if (! EQ (get_frame_param (f, Qfullscreen), Qmaximized))
5096 set_frame_param (f, Qmaximized, Qmaximized);
5098 break;
5100 case SIZE_RESTORED:
5102 bool iconified = FRAME_ICONIFIED_P (f);
5104 /* The following was made unconditional in a
5105 pathetic attempt to fix bug#16967 in revision
5106 116716 but, considered counterproductive was made
5107 conditional again in revision 116727. martin */
5108 if (iconified)
5109 SET_FRAME_VISIBLE (f, 1);
5110 SET_FRAME_ICONIFIED (f, 0);
5112 /* wait_reading_process_output will notice this
5113 and update the frame's display structures. */
5114 SET_FRAME_GARBAGED (f);
5116 if (iconified)
5118 /* Reset top and left positions of the Window
5119 here since Windows sends a WM_MOVE message
5120 BEFORE telling us the Window is minimized
5121 when the Window is iconified, with 3000,3000
5122 as the co-ords. */
5123 x_real_positions (f, &f->left_pos, &f->top_pos);
5125 inev.kind = DEICONIFY_EVENT;
5126 XSETFRAME (inev.frame_or_window, f);
5128 else if (! NILP (Vframe_list)
5129 && ! NILP (XCDR (Vframe_list)))
5130 /* Force a redisplay sooner or later
5131 to update the frame titles
5132 in case this is the second frame. */
5133 record_asynch_buffer_change ();
5136 if (EQ (get_frame_param (f, Qfullscreen), Qmaximized))
5137 set_frame_param (f, Qfullscreen, Qnil);
5138 else if (! EQ (get_frame_param (f, Qmaximized), Qnil))
5139 set_frame_param (f, Qmaximized, Qnil);
5141 break;
5145 if (f && !FRAME_ICONIFIED_P (f) && msg.msg.wParam != SIZE_MINIMIZED)
5147 RECT rect;
5148 int rows, columns, width, height, text_width, text_height;
5150 GetClientRect (msg.msg.hwnd, &rect);
5152 height = rect.bottom - rect.top;
5153 width = rect.right - rect.left;
5154 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
5155 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
5156 /* rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); */
5157 /* columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); */
5159 /* TODO: Clip size to the screen dimensions. */
5161 /* Even if the number of character rows and columns has
5162 not changed, the font size may have changed, so we need
5163 to check the pixel dimensions as well. */
5165 if (width != FRAME_PIXEL_WIDTH (f)
5166 || height != FRAME_PIXEL_HEIGHT (f)
5167 || text_width != FRAME_TEXT_WIDTH (f)
5168 || text_height != FRAME_TEXT_HEIGHT (f))
5170 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
5171 SET_FRAME_GARBAGED (f);
5172 cancel_mouse_face (f);
5173 f->win_gravity = NorthWestGravity;
5177 check_visibility = 1;
5178 break;
5180 case WM_MOUSELEAVE:
5181 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
5182 if (f)
5184 if (f == hlinfo->mouse_face_mouse_frame)
5186 /* If we move outside the frame, then we're
5187 certainly no longer on any text in the frame. */
5188 clear_mouse_face (hlinfo);
5189 hlinfo->mouse_face_mouse_frame = 0;
5192 /* Generate a nil HELP_EVENT to cancel a help-echo.
5193 Do it only if there's something to cancel.
5194 Otherwise, the startup message is cleared when
5195 the mouse leaves the frame. */
5196 if (any_help_event_p)
5197 do_help = -1;
5199 break;
5201 case WM_SETFOCUS:
5202 w32_detect_focus_change (dpyinfo, &msg, &inev);
5204 dpyinfo->grabbed = 0;
5205 check_visibility = 1;
5206 break;
5208 case WM_KILLFOCUS:
5209 w32_detect_focus_change (dpyinfo, &msg, &inev);
5210 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
5212 if (f)
5214 if (f == hlinfo->mouse_face_mouse_frame)
5216 /* If we move outside the frame, then we're
5217 certainly no longer on any text in the frame. */
5218 clear_mouse_face (hlinfo);
5219 hlinfo->mouse_face_mouse_frame = 0;
5222 /* Generate a nil HELP_EVENT to cancel a help-echo.
5223 Do it only if there's something to cancel.
5224 Otherwise, the startup message is cleared when
5225 the mouse leaves the frame. */
5226 if (any_help_event_p)
5227 do_help = -1;
5230 dpyinfo->grabbed = 0;
5231 check_visibility = 1;
5232 break;
5234 case WM_CLOSE:
5235 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5237 if (f)
5239 inev.kind = DELETE_WINDOW_EVENT;
5240 XSETFRAME (inev.frame_or_window, f);
5242 break;
5244 case WM_INITMENU:
5245 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5247 if (f)
5249 inev.kind = MENU_BAR_ACTIVATE_EVENT;
5250 XSETFRAME (inev.frame_or_window, f);
5252 break;
5254 case WM_COMMAND:
5255 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5257 if (f)
5259 extern void menubar_selection_callback
5260 (struct frame *f, void * client_data);
5261 menubar_selection_callback (f, (void *)msg.msg.wParam);
5264 check_visibility = 1;
5265 break;
5267 case WM_DISPLAYCHANGE:
5268 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5270 if (f)
5272 dpyinfo->n_cbits = msg.msg.wParam;
5273 /* The new display could have a different resolution, in
5274 which case we must reconsider what fullscreen
5275 means. */
5276 x_check_fullscreen (f);
5277 DebPrint (("display change: %d %d\n",
5278 (short) LOWORD (msg.msg.lParam),
5279 (short) HIWORD (msg.msg.lParam)));
5282 check_visibility = 1;
5283 break;
5285 #if HAVE_W32NOTIFY
5286 case WM_EMACS_FILENOTIFY:
5287 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5288 if (f)
5289 queue_notifications (&inev, &msg, f, &count);
5290 break;
5291 #endif
5293 default:
5294 /* Check for messages registered at runtime. */
5295 if (msg.msg.message == msh_mousewheel)
5297 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
5298 msg.msg.message = WM_MOUSEWHEEL;
5299 prepend_msg (&msg);
5301 break;
5304 if (inev.kind != NO_EVENT)
5306 kbd_buffer_store_event_hold (&inev, hold_quit);
5307 count++;
5310 if (do_help
5311 && !(hold_quit && hold_quit->kind != NO_EVENT))
5313 Lisp_Object frame;
5315 if (f)
5316 XSETFRAME (frame, f);
5317 else
5318 frame = Qnil;
5320 if (do_help > 0)
5322 if (NILP (help_echo_string))
5324 help_echo_object = help_echo_window = Qnil;
5325 help_echo_pos = -1;
5328 any_help_event_p = 1;
5329 gen_help_event (help_echo_string, frame, help_echo_window,
5330 help_echo_object, help_echo_pos);
5332 else
5334 help_echo_string = Qnil;
5335 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
5337 count++;
5341 /* If the focus was just given to an autoraising frame,
5342 raise it now. FIXME: handle more than one such frame. */
5343 if (dpyinfo->w32_pending_autoraise_frame)
5345 x_raise_frame (dpyinfo->w32_pending_autoraise_frame);
5346 dpyinfo->w32_pending_autoraise_frame = NULL;
5349 /* Check which frames are still visible, if we have enqueued any user
5350 events or been notified of events that may affect visibility. We
5351 do this here because there doesn't seem to be any direct
5352 notification from Windows that the visibility of a window has
5353 changed (at least, not in all cases). */
5354 if (count > 0 || check_visibility)
5356 Lisp_Object tail, frame;
5358 FOR_EACH_FRAME (tail, frame)
5360 struct frame *f = XFRAME (frame);
5361 /* The tooltip has been drawn already. Avoid the
5362 SET_FRAME_GARBAGED below. */
5363 if (EQ (frame, tip_frame))
5364 continue;
5366 /* Check "visible" frames and mark each as obscured or not.
5367 Note that visible is nonzero for unobscured and obscured
5368 frames, but zero for hidden and iconified frames. */
5369 if (FRAME_W32_P (f) && FRAME_VISIBLE_P (f))
5371 RECT clipbox;
5372 HDC hdc;
5373 bool obscured;
5375 enter_crit ();
5376 /* Query clipping rectangle for the entire window area
5377 (GetWindowDC), not just the client portion (GetDC).
5378 Otherwise, the scrollbars and menubar aren't counted as
5379 part of the visible area of the frame, and we may think
5380 the frame is obscured when really a scrollbar is still
5381 visible and gets WM_PAINT messages above. */
5382 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
5383 GetClipBox (hdc, &clipbox);
5384 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
5385 leave_crit ();
5387 obscured = FRAME_OBSCURED_P (f);
5389 if (clipbox.right == clipbox.left || clipbox.bottom == clipbox.top)
5391 /* Frame has become completely obscured so mark as such (we
5392 do this by setting visible to 2 so that FRAME_VISIBLE_P
5393 is still true, but redisplay will skip it). */
5394 SET_FRAME_VISIBLE (f, 2);
5396 if (!obscured)
5397 DebPrint (("frame %p (%s) obscured\n", f, SDATA (f->name)));
5399 else
5401 /* Frame is not obscured, so mark it as such. */
5402 SET_FRAME_VISIBLE (f, 1);
5404 if (obscured)
5406 SET_FRAME_GARBAGED (f);
5407 DebPrint (("obscured frame %p (%s) found to be visible\n",
5408 f, SDATA (f->name)));
5410 /* Force a redisplay sooner or later. */
5411 record_asynch_buffer_change ();
5418 unblock_input ();
5419 return count;
5424 /***********************************************************************
5425 Text Cursor
5426 ***********************************************************************/
5428 /* Set clipping for output in glyph row ROW. W is the window in which
5429 we operate. GC is the graphics context to set clipping in.
5431 ROW may be a text row or, e.g., a mode line. Text rows must be
5432 clipped to the interior of the window dedicated to text display,
5433 mode lines must be clipped to the whole window. */
5435 static void
5436 w32_clip_to_row (struct window *w, struct glyph_row *row,
5437 enum glyph_row_area area, HDC hdc)
5439 RECT clip_rect;
5440 int window_x, window_y, window_width;
5442 window_box (w, area, &window_x, &window_y, &window_width, 0);
5444 clip_rect.left = window_x;
5445 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5446 clip_rect.top = max (clip_rect.top, window_y);
5447 clip_rect.right = clip_rect.left + window_width;
5448 clip_rect.bottom = clip_rect.top + row->visible_height;
5450 w32_set_clip_rectangle (hdc, &clip_rect);
5454 /* Draw a hollow box cursor on window W in glyph row ROW. */
5456 static void
5457 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
5459 struct frame *f = XFRAME (WINDOW_FRAME (w));
5460 HDC hdc;
5461 RECT rect;
5462 int left, top, h;
5463 struct glyph *cursor_glyph;
5464 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5466 /* Get the glyph the cursor is on. If we can't tell because
5467 the current matrix is invalid or such, give up. */
5468 cursor_glyph = get_phys_cursor_glyph (w);
5469 if (cursor_glyph == NULL)
5471 DeleteObject (hb);
5472 return;
5475 /* Compute frame-relative coordinates for phys cursor. */
5476 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
5477 rect.left = left;
5478 rect.top = top;
5479 rect.bottom = rect.top + h;
5480 rect.right = rect.left + w->phys_cursor_width;
5482 hdc = get_frame_dc (f);
5483 /* Set clipping, draw the rectangle, and reset clipping again. */
5484 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5485 FrameRect (hdc, &rect, hb);
5486 DeleteObject (hb);
5487 w32_set_clip_rectangle (hdc, NULL);
5488 release_frame_dc (f, hdc);
5492 /* Draw a bar cursor on window W in glyph row ROW.
5494 Implementation note: One would like to draw a bar cursor with an
5495 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5496 Unfortunately, I didn't find a font yet that has this property set.
5497 --gerd. */
5499 static void
5500 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
5501 int width, enum text_cursor_kinds kind)
5503 struct frame *f = XFRAME (w->frame);
5504 struct glyph *cursor_glyph;
5506 /* If cursor is out of bounds, don't draw garbage. This can happen
5507 in mini-buffer windows when switching between echo area glyphs
5508 and mini-buffer. */
5509 cursor_glyph = get_phys_cursor_glyph (w);
5510 if (cursor_glyph == NULL)
5511 return;
5513 /* If on an image, draw like a normal cursor. That's usually better
5514 visible than drawing a bar, esp. if the image is large so that
5515 the bar might not be in the window. */
5516 if (cursor_glyph->type == IMAGE_GLYPH)
5518 struct glyph_row *row;
5519 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5520 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5522 else
5524 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5525 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5526 int x;
5527 HDC hdc;
5529 /* If the glyph's background equals the color we normally draw
5530 the bar cursor in, the bar cursor in its normal color is
5531 invisible. Use the glyph's foreground color instead in this
5532 case, on the assumption that the glyph's colors are chosen so
5533 that the glyph is legible. */
5534 if (face->background == cursor_color)
5535 cursor_color = face->foreground;
5537 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5539 hdc = get_frame_dc (f);
5540 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5542 if (kind == BAR_CURSOR)
5544 if (width < 0)
5545 width = FRAME_CURSOR_WIDTH (f);
5546 width = min (cursor_glyph->pixel_width, width);
5548 w->phys_cursor_width = width;
5550 /* If the character under cursor is R2L, draw the bar cursor
5551 on the right of its glyph, rather than on the left. */
5552 if ((cursor_glyph->resolved_level & 1) != 0)
5553 x += cursor_glyph->pixel_width - width;
5555 w32_fill_area (f, hdc, cursor_color, x,
5556 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5557 width, row->height);
5559 else
5561 int dummy_x, dummy_y, dummy_h;
5563 if (width < 0)
5564 width = row->height;
5566 width = min (row->height, width);
5568 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5569 &dummy_y, &dummy_h);
5570 w32_fill_area (f, hdc, cursor_color, x,
5571 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5572 row->height - width),
5573 w->phys_cursor_width, width);
5576 w32_set_clip_rectangle (hdc, NULL);
5577 release_frame_dc (f, hdc);
5582 /* RIF: Define cursor CURSOR on frame F. */
5584 static void
5585 w32_define_frame_cursor (struct frame *f, Cursor cursor)
5587 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5591 /* RIF: Clear area on frame F. */
5593 static void
5594 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
5596 HDC hdc;
5598 hdc = get_frame_dc (f);
5599 w32_clear_area (f, hdc, x, y, width, height);
5600 release_frame_dc (f, hdc);
5603 /* RIF: Draw or clear cursor on window W. */
5605 static void
5606 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
5607 int x, int y, enum text_cursor_kinds cursor_type,
5608 int cursor_width, bool on_p, bool active_p)
5610 if (on_p)
5612 /* If the user wants to use the system caret, make sure our own
5613 cursor remains invisible. */
5614 if (w32_use_visible_system_caret)
5616 /* Call to erase_phys_cursor here seems to use the
5617 wrong values of w->phys_cursor, as they have been
5618 overwritten before this function was called. */
5619 if (w->phys_cursor_type != NO_CURSOR)
5620 erase_phys_cursor (w);
5622 cursor_type = w->phys_cursor_type = NO_CURSOR;
5623 w->phys_cursor_width = -1;
5625 else
5627 w->phys_cursor_type = cursor_type;
5630 w->phys_cursor_on_p = 1;
5632 /* If this is the active cursor, we need to track it with the
5633 system caret, so third party software like screen magnifiers
5634 and speech synthesizers can follow the cursor. */
5635 if (active_p)
5637 struct frame *f = XFRAME (WINDOW_FRAME (w));
5638 HWND hwnd = FRAME_W32_WINDOW (f);
5640 w32_system_caret_x
5641 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5642 w32_system_caret_y
5643 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5644 + glyph_row->ascent - w->phys_cursor_ascent);
5645 w32_system_caret_window = w;
5646 w32_system_caret_hdr_height = WINDOW_HEADER_LINE_HEIGHT (w);
5647 w32_system_caret_mode_height = WINDOW_MODE_LINE_HEIGHT (w);
5649 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
5651 /* If the size of the active cursor changed, destroy the old
5652 system caret. */
5653 if (w32_system_caret_hwnd
5654 && (w32_system_caret_height != w->phys_cursor_height))
5655 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5657 w32_system_caret_height = w->phys_cursor_height;
5659 /* Move the system caret. */
5660 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5663 if (glyph_row->exact_window_width_line_p
5664 && (glyph_row->reversed_p
5665 ? (w->phys_cursor.hpos < 0)
5666 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5668 glyph_row->cursor_in_fringe_p = 1;
5669 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5670 return;
5673 switch (cursor_type)
5675 case HOLLOW_BOX_CURSOR:
5676 x_draw_hollow_cursor (w, glyph_row);
5677 break;
5679 case FILLED_BOX_CURSOR:
5680 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5681 break;
5683 case BAR_CURSOR:
5684 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5685 break;
5687 case HBAR_CURSOR:
5688 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5689 break;
5691 case NO_CURSOR:
5692 w->phys_cursor_width = 0;
5693 break;
5695 default:
5696 emacs_abort ();
5703 /* Icons. */
5706 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5708 HANDLE main_icon;
5709 HANDLE small_icon = NULL;
5711 if (FRAME_W32_WINDOW (f) == 0)
5712 return 1;
5714 if (NILP (icon))
5715 main_icon = LoadIcon (hinst, EMACS_CLASS);
5716 else if (STRINGP (icon))
5718 /* Load the main icon from the named file. */
5719 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5720 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5721 /* Try to load a small icon to go with it. */
5722 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5723 GetSystemMetrics (SM_CXSMICON),
5724 GetSystemMetrics (SM_CYSMICON),
5725 LR_LOADFROMFILE);
5727 else if (SYMBOLP (icon))
5729 LPCTSTR name;
5731 if (EQ (icon, intern ("application")))
5732 name = (LPCTSTR) IDI_APPLICATION;
5733 else if (EQ (icon, intern ("hand")))
5734 name = (LPCTSTR) IDI_HAND;
5735 else if (EQ (icon, intern ("question")))
5736 name = (LPCTSTR) IDI_QUESTION;
5737 else if (EQ (icon, intern ("exclamation")))
5738 name = (LPCTSTR) IDI_EXCLAMATION;
5739 else if (EQ (icon, intern ("asterisk")))
5740 name = (LPCTSTR) IDI_ASTERISK;
5741 else if (EQ (icon, intern ("winlogo")))
5742 name = (LPCTSTR) IDI_WINLOGO;
5743 else
5744 return 1;
5746 main_icon = LoadIcon (NULL, name);
5748 else
5749 return 1;
5751 if (main_icon == NULL)
5752 return 1;
5754 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5755 (LPARAM) main_icon);
5757 /* If there is a small icon that goes with it, set that too. */
5758 if (small_icon)
5759 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5760 (LPARAM) small_icon);
5762 return 0;
5766 /************************************************************************
5767 Handling X errors
5768 ************************************************************************/
5770 /* Display Error Handling functions not used on W32. Listing them here
5771 helps diff stay in step when comparing w32term.c with xterm.c.
5773 x_error_catcher (display, error)
5774 x_catch_errors (dpy)
5775 x_catch_errors_unwind (old_val)
5776 x_check_errors (dpy, format)
5777 x_fully_uncatch_errors ()
5778 x_had_errors_p (dpy)
5779 x_clear_errors (dpy)
5780 x_uncatch_errors (dpy, count)
5781 x_trace_wire ()
5782 x_connection_signal (signalnum)
5783 x_connection_closed (dpy, error_message)
5784 x_error_quitter (display, error)
5785 x_error_handler (display, error)
5786 x_io_error_quitter (display)
5791 /* Changing the font of the frame. */
5793 Lisp_Object
5794 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
5796 struct font *font = XFONT_OBJECT (font_object);
5797 int unit;
5799 if (fontset < 0)
5800 fontset = fontset_from_font (font_object);
5801 FRAME_FONTSET (f) = fontset;
5802 if (FRAME_FONT (f) == font)
5803 /* This font is already set in frame F. There's nothing more to
5804 do. */
5805 return font_object;
5807 FRAME_FONT (f) = font;
5808 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5809 FRAME_COLUMN_WIDTH (f) = unit = font->average_width;
5810 FRAME_LINE_HEIGHT (f) = font->height;
5812 /* Compute number of scrollbar columns. */
5813 unit = FRAME_COLUMN_WIDTH (f);
5814 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5815 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5816 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
5817 else
5819 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
5820 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) =
5821 FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
5824 /* Now make the frame display the given font. */
5825 if (FRAME_X_WINDOW (f) != 0)
5827 /* Don't change the size of a tip frame; there's no point in
5828 doing it because it's done in Fx_show_tip, and it leads to
5829 problems because the tip frame has no widget. */
5830 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5831 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
5832 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3, 0);
5835 /* X version sets font of input methods here also. */
5837 return font_object;
5841 /***********************************************************************
5842 TODO: W32 Input Methods
5843 ***********************************************************************/
5844 /* Listing missing functions from xterm.c helps diff stay in step.
5846 xim_destroy_callback (xim, client_data, call_data)
5847 xim_open_dpy (dpyinfo, resource_name)
5848 struct xim_inst_t
5849 xim_instantiate_callback (display, client_data, call_data)
5850 xim_initialize (dpyinfo, resource_name)
5851 xim_close_dpy (dpyinfo)
5856 /* Calculate the absolute position in frame F
5857 from its current recorded position values and gravity. */
5859 void
5860 x_calc_absolute_position (struct frame *f)
5862 int flags = f->size_hint_flags;
5864 /* The sum of the widths of the frame's left and right borders, and
5865 the sum of the heights of the frame's top and bottom borders (in
5866 pixels) drawn by Windows. */
5867 unsigned int left_right_borders_width, top_bottom_borders_height;
5869 /* Try to get the actual values of these two variables. We compute
5870 the border width (height) by subtracting the width (height) of
5871 the frame's client area from the width (height) of the frame's
5872 entire window. */
5873 WINDOWPLACEMENT wp = { 0 };
5874 RECT client_rect = { 0 };
5876 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5877 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5879 left_right_borders_width =
5880 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5881 (client_rect.right - client_rect.left);
5883 top_bottom_borders_height =
5884 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5885 (client_rect.bottom - client_rect.top);
5887 else
5889 /* Use sensible default values. */
5890 left_right_borders_width = 8;
5891 top_bottom_borders_height = 32;
5894 /* Treat negative positions as relative to the rightmost bottommost
5895 position that fits on the screen. */
5896 if (flags & XNegative)
5897 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
5898 - FRAME_PIXEL_WIDTH (f)
5899 + f->left_pos
5900 - (left_right_borders_width - 1));
5902 if (flags & YNegative)
5903 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
5904 - FRAME_PIXEL_HEIGHT (f)
5905 + f->top_pos
5906 - (top_bottom_borders_height - 1));
5908 /* The left_pos and top_pos are now relative to the top and left
5909 screen edges, so the flags should correspond. */
5910 f->size_hint_flags &= ~ (XNegative | YNegative);
5913 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5914 to really change the position, and 0 when calling from
5915 x_make_frame_visible (in that case, XOFF and YOFF are the current
5916 position values). It is -1 when calling from x_set_frame_parameters,
5917 which means, do adjust for borders but don't change the gravity. */
5919 void
5920 x_set_offset (struct frame *f, register int xoff, register int yoff,
5921 int change_gravity)
5923 int modified_top, modified_left;
5925 if (change_gravity > 0)
5927 f->top_pos = yoff;
5928 f->left_pos = xoff;
5929 f->size_hint_flags &= ~ (XNegative | YNegative);
5930 if (xoff < 0)
5931 f->size_hint_flags |= XNegative;
5932 if (yoff < 0)
5933 f->size_hint_flags |= YNegative;
5934 f->win_gravity = NorthWestGravity;
5936 x_calc_absolute_position (f);
5938 block_input ();
5939 x_wm_set_size_hint (f, (long) 0, 0);
5941 modified_left = f->left_pos;
5942 modified_top = f->top_pos;
5944 my_set_window_pos (FRAME_W32_WINDOW (f),
5945 NULL,
5946 modified_left, modified_top,
5947 0, 0,
5948 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5949 unblock_input ();
5952 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
5953 wanted positions of the WM window (not Emacs window).
5954 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
5955 window (FRAME_X_WINDOW).
5958 static void
5959 x_fullscreen_adjust (struct frame *f, int *width, int *height, int *top_pos, int *left_pos)
5961 int newwidth = FRAME_COLS (f);
5962 int newheight = FRAME_LINES (f);
5963 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
5965 *top_pos = f->top_pos;
5966 *left_pos = f->left_pos;
5968 if (f->want_fullscreen & FULLSCREEN_HEIGHT)
5970 int ph;
5972 ph = x_display_pixel_height (dpyinfo);
5973 newheight = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, ph);
5974 ph = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, newheight) - f->y_pixels_diff;
5975 newheight = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, ph);
5976 *top_pos = 0;
5979 if (f->want_fullscreen & FULLSCREEN_WIDTH)
5981 int pw;
5983 pw = x_display_pixel_width (dpyinfo);
5984 newwidth = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pw);
5985 pw = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, newwidth) - f->x_pixels_diff;
5986 newwidth = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pw);
5987 *left_pos = 0;
5990 *width = newwidth;
5991 *height = newheight;
5994 /* Check if we need to resize the frame due to a fullscreen request.
5995 If so needed, resize the frame. */
5996 static void
5997 x_check_fullscreen (struct frame *f)
5999 if (f->want_fullscreen & FULLSCREEN_BOTH)
6001 int width, height, ign;
6003 x_real_positions (f, &f->left_pos, &f->top_pos);
6005 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
6007 /* We do not need to move the window, it shall be taken care of
6008 when setting WM manager hints. */
6009 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
6011 change_frame_size (f, width, height, 0, 1, 0, 0);
6012 SET_FRAME_GARBAGED (f);
6013 cancel_mouse_face (f);
6015 /* Wait for the change of frame size to occur. */
6016 f->want_fullscreen |= FULLSCREEN_WAIT;
6021 static void
6022 w32fullscreen_hook (struct frame *f)
6024 if (FRAME_VISIBLE_P (f))
6026 HWND hwnd = FRAME_W32_WINDOW(f);
6027 DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
6028 RECT rect;
6029 enum fullscreen_type prev_fsmode = FRAME_PREV_FSMODE (f);
6031 block_input();
6032 f->want_fullscreen &= ~FULLSCREEN_WAIT;
6034 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
6035 GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6037 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
6039 SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
6040 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6042 else if (FRAME_PREV_FSMODE (f) == FULLSCREEN_HEIGHT
6043 || FRAME_PREV_FSMODE (f) == FULLSCREEN_WIDTH)
6044 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6046 FRAME_PREV_FSMODE (f) = f->want_fullscreen;
6048 if (f->want_fullscreen == FULLSCREEN_NONE)
6049 ShowWindow (hwnd, SW_SHOWNORMAL);
6050 else if (f->want_fullscreen == FULLSCREEN_MAXIMIZED)
6052 if (prev_fsmode == FULLSCREEN_BOTH || prev_fsmode == FULLSCREEN_WIDTH
6053 || prev_fsmode == FULLSCREEN_HEIGHT)
6054 /* Make window normal since otherwise the subsequent
6055 maximization might fail in some cases. */
6056 ShowWindow (hwnd, SW_SHOWNORMAL);
6057 ShowWindow (hwnd, SW_MAXIMIZE);
6059 else if (f->want_fullscreen == FULLSCREEN_BOTH)
6061 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6062 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6063 SetWindowLong (hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
6064 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
6065 rect.right - rect.left, rect.bottom - rect.top,
6066 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
6068 else
6070 ShowWindow (hwnd, SW_SHOWNORMAL);
6071 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6072 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6073 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
6074 rect.right - rect.left, rect.bottom - rect.top, 0);
6077 f->want_fullscreen = FULLSCREEN_NONE;
6078 unblock_input ();
6080 else
6081 f->want_fullscreen |= FULLSCREEN_WAIT;
6084 /* Call this to change the size of frame F's x-window.
6085 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
6086 for this size change and subsequent size changes.
6087 Otherwise we leave the window gravity unchanged. */
6089 void
6090 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
6092 int pixelwidth, pixelheight;
6093 RECT rect;
6095 block_input ();
6097 if (pixelwise)
6099 pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
6100 pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
6102 else
6104 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width);
6105 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height);
6108 f->win_gravity = NorthWestGravity;
6109 x_wm_set_size_hint (f, (long) 0, 0);
6111 f->want_fullscreen = FULLSCREEN_NONE;
6112 w32fullscreen_hook (f);
6114 rect.left = rect.top = 0;
6115 rect.right = pixelwidth;
6116 rect.bottom = pixelheight;
6118 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
6119 FRAME_EXTERNAL_MENU_BAR (f));
6121 my_set_window_pos (FRAME_W32_WINDOW (f),
6122 NULL,
6123 0, 0,
6124 rect.right - rect.left,
6125 rect.bottom - rect.top,
6126 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
6128 /* If w32_enable_frame_resize_hack is non-nil, immediately apply the
6129 new pixel sizes to the frame and its subwindows.
6131 Jason Rumney earlier refused to call change_frame_size right here
6132 with the following argument:
6134 The following mirrors what is done in xterm.c. It appears to be for
6135 informing lisp of the new size immediately, while the actual resize
6136 will happen asynchronously. But on Windows, the menu bar
6137 automatically wraps when the frame is too narrow to contain it, and
6138 that causes any calculations made here to come out wrong. The end
6139 is some nasty buggy behavior, including the potential loss of the
6140 minibuffer.
6142 Disabling this code is either not sufficient to fix the problems
6143 completely, or it causes fresh problems, but at least it removes
6144 the most problematic symptom of the minibuffer becoming unusable.
6146 However, as the discussion about how to handle frame size
6147 parameters on Windows (Bug#1348, Bug#16028) shows, that cure seems
6148 worse than the disease. In particular, menu bar wrapping looks
6149 like a non-issue - maybe so because Windows eventually gets back to
6150 us with the correct client rectangle anyway. But we have to avoid
6151 calling change_frame_size with a delta of less than one canoncial
6152 character size when frame_resize_pixelwise is nil, as explained in
6153 the comment above. */
6155 if (w32_enable_frame_resize_hack)
6158 change_frame_size (f, FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth),
6159 FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight),
6160 0, 1, 0, 1);
6161 SET_FRAME_GARBAGED (f);
6163 /* If cursor was outside the new size, mark it as off. */
6164 mark_window_cursors_off (XWINDOW (f->root_window));
6166 /* Clear out any recollection of where the mouse highlighting was,
6167 since it might be in a place that's outside the new frame size.
6168 Actually checking whether it is outside is a pain in the neck,
6169 so don't try--just let the highlighting be done afresh with new
6170 size. */
6171 cancel_mouse_face (f);
6174 unblock_input ();
6176 do_pending_window_change (0);
6179 /* Mouse warping. */
6181 void
6182 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
6184 RECT rect;
6185 POINT pt;
6187 block_input ();
6189 GetClientRect (FRAME_W32_WINDOW (f), &rect);
6190 pt.x = rect.left + pix_x;
6191 pt.y = rect.top + pix_y;
6192 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
6194 SetCursorPos (pt.x, pt.y);
6196 unblock_input ();
6200 /* focus shifting, raising and lowering. */
6202 void
6203 x_focus_frame (struct frame *f)
6205 #if 0
6206 struct w32_display_info *dpyinfo = &one_w32_display_info;
6207 #endif
6209 /* Give input focus to frame. */
6210 block_input ();
6211 #if 0
6212 /* Try not to change its Z-order if possible. */
6213 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
6214 my_set_focus (f, FRAME_W32_WINDOW (f));
6215 else
6216 #endif
6217 my_set_foreground_window (FRAME_W32_WINDOW (f));
6218 unblock_input ();
6221 /* Raise frame F. */
6222 void
6223 x_raise_frame (struct frame *f)
6225 block_input ();
6227 /* Strictly speaking, raise-frame should only change the frame's Z
6228 order, leaving input focus unchanged. This is reasonable behavior
6229 on X where the usual policy is point-to-focus. However, this
6230 behavior would be very odd on Windows where the usual policy is
6231 click-to-focus.
6233 On X, if the mouse happens to be over the raised frame, it gets
6234 input focus anyway (so the window with focus will never be
6235 completely obscured) - if not, then just moving the mouse over it
6236 is sufficient to give it focus. On Windows, the user must actually
6237 click on the frame (preferably the title bar so as not to move
6238 point), which is more awkward. Also, no other Windows program
6239 raises a window to the top but leaves another window (possibly now
6240 completely obscured) with input focus.
6242 Because there is a system setting on Windows that allows the user
6243 to choose the point to focus policy, we make the strict semantics
6244 optional, but by default we grab focus when raising. */
6246 if (NILP (Vw32_grab_focus_on_raise))
6248 /* The obvious call to my_set_window_pos doesn't work if Emacs is
6249 not already the foreground application: the frame is raised
6250 above all other frames belonging to us, but not above the
6251 current top window. To achieve that, we have to resort to this
6252 more cumbersome method. */
6254 HDWP handle = BeginDeferWindowPos (2);
6255 if (handle)
6257 handle = DeferWindowPos (handle,
6258 FRAME_W32_WINDOW (f),
6259 HWND_TOP,
6260 0, 0, 0, 0,
6261 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
6262 if (handle)
6264 handle = DeferWindowPos (handle,
6265 GetForegroundWindow (),
6266 FRAME_W32_WINDOW (f),
6267 0, 0, 0, 0,
6268 SWP_NOSIZE | SWP_NOMOVE |
6269 SWP_NOACTIVATE);
6270 if (handle)
6271 EndDeferWindowPos (handle);
6275 else
6277 my_bring_window_to_top (FRAME_W32_WINDOW (f));
6280 unblock_input ();
6283 /* Lower frame F. */
6284 void
6285 x_lower_frame (struct frame *f)
6287 block_input ();
6288 my_set_window_pos (FRAME_W32_WINDOW (f),
6289 HWND_BOTTOM,
6290 0, 0, 0, 0,
6291 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
6292 unblock_input ();
6295 static void
6296 w32_frame_raise_lower (struct frame *f, int raise_flag)
6298 if (! FRAME_W32_P (f))
6299 return;
6301 if (raise_flag)
6302 x_raise_frame (f);
6303 else
6304 x_lower_frame (f);
6307 /* Change of visibility. */
6309 /* This tries to wait until the frame is really visible.
6310 However, if the window manager asks the user where to position
6311 the frame, this will return before the user finishes doing that.
6312 The frame will not actually be visible at that time,
6313 but it will become visible later when the window manager
6314 finishes with it. */
6316 void
6317 x_make_frame_visible (struct frame *f)
6319 block_input ();
6321 x_set_bitmap_icon (f);
6323 if (! FRAME_VISIBLE_P (f))
6325 /* We test FRAME_GARBAGED_P here to make sure we don't
6326 call x_set_offset a second time
6327 if we get to x_make_frame_visible a second time
6328 before the window gets really visible. */
6329 if (! FRAME_ICONIFIED_P (f)
6330 && ! f->output_data.w32->asked_for_visible)
6332 RECT workarea_rect;
6333 RECT window_rect;
6335 /* Adjust vertical window position in order to avoid being
6336 covered by a task bar placed at the bottom of the desktop. */
6337 SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
6338 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
6339 if (window_rect.bottom > workarea_rect.bottom
6340 && window_rect.top > workarea_rect.top)
6341 f->top_pos = max (window_rect.top
6342 - window_rect.bottom + workarea_rect.bottom,
6343 workarea_rect.top);
6345 x_set_offset (f, f->left_pos, f->top_pos, 0);
6348 f->output_data.w32->asked_for_visible = 1;
6350 /* According to a report in emacs-devel 2008-06-03, SW_SHOWNORMAL
6351 causes unexpected behavior when unminimizing frames that were
6352 previously maximized. But only SW_SHOWNORMAL works properly for
6353 frames that were truely hidden (using make-frame-invisible), so
6354 we need it to avoid Bug#5482. It seems that iconified is only
6355 set for minimized windows that are still visible, so use that to
6356 determine the appropriate flag to pass ShowWindow. */
6357 my_show_window (f, FRAME_W32_WINDOW (f),
6358 FRAME_ICONIFIED_P (f) ? SW_RESTORE : SW_SHOWNORMAL);
6361 /* Synchronize to ensure Emacs knows the frame is visible
6362 before we do anything else. We do this loop with input not blocked
6363 so that incoming events are handled. */
6365 Lisp_Object frame;
6366 int count;
6368 /* This must come after we set COUNT. */
6369 unblock_input ();
6371 XSETFRAME (frame, f);
6373 /* Wait until the frame is visible. Process X events until a
6374 MapNotify event has been seen, or until we think we won't get a
6375 MapNotify at all.. */
6376 for (count = input_signal_count + 10;
6377 input_signal_count < count && !FRAME_VISIBLE_P (f);)
6379 /* Force processing of queued events. */
6380 /* TODO: x_sync equivalent? */
6382 /* Machines that do polling rather than SIGIO have been observed
6383 to go into a busy-wait here. So we'll fake an alarm signal
6384 to let the handler know that there's something to be read.
6385 We used to raise a real alarm, but it seems that the handler
6386 isn't always enabled here. This is probably a bug. */
6387 if (input_polling_used ())
6389 /* It could be confusing if a real alarm arrives while processing
6390 the fake one. Turn it off and let the handler reset it. */
6391 int old_poll_suppress_count = poll_suppress_count;
6392 poll_suppress_count = 1;
6393 poll_for_input_1 ();
6394 poll_suppress_count = old_poll_suppress_count;
6400 /* Change from mapped state to withdrawn state. */
6402 /* Make the frame visible (mapped and not iconified). */
6404 void
6405 x_make_frame_invisible (struct frame *f)
6407 /* Don't keep the highlight on an invisible frame. */
6408 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6409 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6411 block_input ();
6413 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
6415 /* We can't distinguish this from iconification
6416 just by the event that we get from the server.
6417 So we can't win using the usual strategy of letting
6418 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
6419 and synchronize with the server to make sure we agree. */
6420 SET_FRAME_VISIBLE (f, 0);
6421 SET_FRAME_ICONIFIED (f, 0);
6423 unblock_input ();
6426 /* Change window state from mapped to iconified. */
6428 void
6429 x_iconify_frame (struct frame *f)
6431 /* Don't keep the highlight on an invisible frame. */
6432 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6433 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6435 if (FRAME_ICONIFIED_P (f))
6436 return;
6438 block_input ();
6440 x_set_bitmap_icon (f);
6442 /* Simulate the user minimizing the frame. */
6443 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
6445 SET_FRAME_VISIBLE (f, 0);
6446 SET_FRAME_ICONIFIED (f, 1);
6448 unblock_input ();
6452 /* Free X resources of frame F. */
6454 void
6455 x_free_frame_resources (struct frame *f)
6457 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6458 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
6460 block_input ();
6462 /* We must free faces before destroying windows because some
6463 font-driver (e.g. xft) access a window while finishing a
6464 face. */
6465 free_frame_faces (f);
6467 if (FRAME_W32_WINDOW (f))
6468 my_destroy_window (f, FRAME_W32_WINDOW (f));
6470 free_frame_menubar (f);
6472 xfree (f->output_data.w32);
6473 f->output_data.w32 = NULL;
6475 if (f == dpyinfo->w32_focus_frame)
6476 dpyinfo->w32_focus_frame = 0;
6477 if (f == dpyinfo->w32_focus_event_frame)
6478 dpyinfo->w32_focus_event_frame = 0;
6479 if (f == dpyinfo->x_highlight_frame)
6480 dpyinfo->x_highlight_frame = 0;
6481 if (f == hlinfo->mouse_face_mouse_frame)
6482 reset_mouse_highlight (hlinfo);
6484 unblock_input ();
6488 /* Destroy the window of frame F. */
6489 void
6490 x_destroy_window (struct frame *f)
6492 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6494 x_free_frame_resources (f);
6495 dpyinfo->reference_count--;
6499 /* Setting window manager hints. */
6501 /* Set the normal size hints for the window manager, for frame F.
6502 FLAGS is the flags word to use--or 0 meaning preserve the flags
6503 that the window now has.
6504 If USER_POSITION, set the USPosition
6505 flag (this is useful when FLAGS is 0). */
6506 void
6507 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
6509 Window window = FRAME_W32_WINDOW (f);
6511 enter_crit ();
6513 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
6514 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
6515 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
6516 SetWindowLong (window, WND_VSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_WIDTH (f));
6517 SetWindowLong (window, WND_HSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_HEIGHT (f));
6519 leave_crit ();
6522 /***********************************************************************
6523 Fonts
6524 ***********************************************************************/
6526 #ifdef GLYPH_DEBUG
6528 /* Check that FONT is valid on frame F. It is if it can be found in F's
6529 font table. */
6531 static void
6532 x_check_font (struct frame *f, struct font *font)
6534 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
6535 if (font->driver->check)
6536 eassert (font->driver->check (f, font) == 0);
6539 #endif /* GLYPH_DEBUG */
6541 /* Show hourglass cursor on frame F. */
6543 static void
6544 w32_show_hourglass (struct frame *f)
6546 if (!menubar_in_use && !current_popup_menu)
6548 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6550 w32->hourglass_p = 1;
6551 SetCursor (w32->hourglass_cursor);
6555 /* Hide hourglass cursor on frame F. */
6557 static void
6558 w32_hide_hourglass (struct frame *f)
6560 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6562 w32->hourglass_p = 0;
6563 SetCursor (w32->current_cursor);
6566 /* FIXME: old code did that, but I don't know why. Anyway,
6567 this is used for non-GUI frames (see cancel_hourglass). */
6569 void
6570 w32_arrow_cursor (void)
6572 SetCursor (w32_load_cursor (IDC_ARROW));
6575 /***********************************************************************
6576 Initialization
6577 ***********************************************************************/
6579 static int w32_initialized = 0;
6581 void
6582 w32_initialize_display_info (Lisp_Object display_name)
6584 struct w32_display_info *dpyinfo = &one_w32_display_info;
6586 memset (dpyinfo, 0, sizeof (*dpyinfo));
6588 dpyinfo->name_list_element = Fcons (display_name, Qnil);
6589 dpyinfo->w32_id_name = xmalloc (SCHARS (Vinvocation_name)
6590 + SCHARS (Vsystem_name) + 2);
6591 sprintf (dpyinfo->w32_id_name, "%s@%s",
6592 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6594 /* Default Console mode values - overridden when running in GUI mode
6595 with values obtained from system metrics. */
6596 dpyinfo->resx = 1;
6597 dpyinfo->resy = 1;
6598 dpyinfo->n_planes = 1;
6599 dpyinfo->n_cbits = 4;
6600 dpyinfo->n_fonts = 0;
6601 dpyinfo->smallest_font_height = 1;
6602 dpyinfo->smallest_char_width = 1;
6603 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6604 dpyinfo->horizontal_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6605 /* TODO: dpyinfo->gray */
6607 reset_mouse_highlight (&dpyinfo->mouse_highlight);
6610 /* Create an xrdb-style database of resources to supersede registry settings.
6611 The database is just a concatenation of C strings, finished by an additional
6612 \0. The strings are submitted to some basic normalization, so
6614 [ *]option[ *]:[ *]value...
6616 becomes
6618 option:value...
6620 but any whitespace following value is not removed. */
6622 static char *
6623 w32_make_rdb (char *xrm_option)
6625 char *buffer = xmalloc (strlen (xrm_option) + 2);
6626 char *current = buffer;
6627 char ch;
6628 int in_option = 1;
6629 int before_value = 0;
6631 do {
6632 ch = *xrm_option++;
6634 if (ch == '\n')
6636 *current++ = '\0';
6637 in_option = 1;
6638 before_value = 0;
6640 else if (ch != ' ')
6642 *current++ = ch;
6643 if (in_option && (ch == ':'))
6645 in_option = 0;
6646 before_value = 1;
6648 else if (before_value)
6650 before_value = 0;
6653 else if (!(in_option || before_value))
6655 *current++ = ch;
6657 } while (ch);
6659 *current = '\0';
6661 return buffer;
6664 extern frame_parm_handler w32_frame_parm_handlers[];
6666 static struct redisplay_interface w32_redisplay_interface =
6668 w32_frame_parm_handlers,
6669 x_produce_glyphs,
6670 x_write_glyphs,
6671 x_insert_glyphs,
6672 x_clear_end_of_line,
6673 x_scroll_run,
6674 x_after_update_window_line,
6675 x_update_window_begin,
6676 x_update_window_end,
6677 0, /* flush_display */
6678 x_clear_window_mouse_face,
6679 x_get_glyph_overhangs,
6680 x_fix_overlapping_area,
6681 w32_draw_fringe_bitmap,
6682 w32_define_fringe_bitmap,
6683 w32_destroy_fringe_bitmap,
6684 w32_compute_glyph_string_overhangs,
6685 x_draw_glyph_string,
6686 w32_define_frame_cursor,
6687 w32_clear_frame_area,
6688 w32_draw_window_cursor,
6689 w32_draw_vertical_window_border,
6690 w32_draw_window_divider,
6691 w32_shift_glyphs_for_insert,
6692 w32_show_hourglass,
6693 w32_hide_hourglass
6696 static void x_delete_terminal (struct terminal *term);
6698 static struct terminal *
6699 w32_create_terminal (struct w32_display_info *dpyinfo)
6701 struct terminal *terminal;
6703 terminal = create_terminal (output_w32, &w32_redisplay_interface);
6705 terminal->display_info.w32 = dpyinfo;
6706 dpyinfo->terminal = terminal;
6708 /* MSVC does not type K&R functions with no arguments correctly, and
6709 so we must explicitly cast them. */
6710 terminal->clear_frame_hook = x_clear_frame;
6711 terminal->ins_del_lines_hook = x_ins_del_lines;
6712 terminal->delete_glyphs_hook = x_delete_glyphs;
6713 terminal->ring_bell_hook = w32_ring_bell;
6714 terminal->update_begin_hook = x_update_begin;
6715 terminal->update_end_hook = x_update_end;
6716 terminal->read_socket_hook = w32_read_socket;
6717 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6718 terminal->mouse_position_hook = w32_mouse_position;
6719 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6720 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6721 terminal->fullscreen_hook = w32fullscreen_hook;
6722 terminal->menu_show_hook = w32_menu_show;
6723 terminal->popup_dialog_hook = w32_popup_dialog;
6724 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6725 terminal->set_horizontal_scroll_bar_hook = w32_set_horizontal_scroll_bar;
6726 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6727 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6728 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6729 terminal->delete_frame_hook = x_destroy_window;
6730 terminal->delete_terminal_hook = x_delete_terminal;
6731 /* Other hooks are NULL by default. */
6733 /* We don't yet support separate terminals on W32, so don't try to share
6734 keyboards between virtual terminals that are on the same physical
6735 terminal like X does. */
6736 terminal->kboard = allocate_kboard (Qw32);
6737 /* Don't let the initial kboard remain current longer than necessary.
6738 That would cause problems if a file loaded on startup tries to
6739 prompt in the mini-buffer. */
6740 if (current_kboard == initial_kboard)
6741 current_kboard = terminal->kboard;
6742 terminal->kboard->reference_count++;
6744 return terminal;
6747 static void
6748 x_delete_terminal (struct terminal *terminal)
6750 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6752 /* Protect against recursive calls. delete_frame in
6753 delete_terminal calls us back when it deletes our last frame. */
6754 if (!terminal->name)
6755 return;
6757 block_input ();
6759 x_delete_display (dpyinfo);
6760 unblock_input ();
6763 struct w32_display_info *
6764 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
6766 struct w32_display_info *dpyinfo;
6767 struct terminal *terminal;
6768 HDC hdc;
6770 block_input ();
6772 if (!w32_initialized)
6774 w32_initialize ();
6775 w32_initialized = 1;
6778 w32_initialize_display_info (display_name);
6780 dpyinfo = &one_w32_display_info;
6781 terminal = w32_create_terminal (dpyinfo);
6783 /* Set the name of the terminal. */
6784 terminal->name = xlispstrdup (display_name);
6786 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6788 /* Put this display on the chain. */
6789 dpyinfo->next = x_display_list;
6790 x_display_list = dpyinfo;
6792 hdc = GetDC (NULL);
6794 dpyinfo->root_window = GetDesktopWindow ();
6795 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6796 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6797 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6798 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6799 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6800 ReleaseDC (NULL, hdc);
6802 /* initialize palette with white and black */
6804 XColor color;
6805 w32_defined_color (0, "white", &color, 1);
6806 w32_defined_color (0, "black", &color, 1);
6809 #ifdef WINDOWSNT
6810 /* Add the default keyboard. When !WINDOWSNT, we're using the
6811 standard Emacs console handling machinery and don't need an
6812 explicit FD here. */
6813 add_keyboard_wait_descriptor (0);
6814 #elif CYGWIN
6815 /* /dev/windows wakes us up when we have a thread message pending. */
6816 add_keyboard_wait_descriptor (w32_message_fd);
6817 #endif
6819 /* Create Fringe Bitmaps and store them for later use.
6821 On W32, bitmaps are all unsigned short, as Windows requires
6822 bitmap data to be Word aligned. For some reason they are
6823 horizontally reflected compared to how they appear on X, so we
6824 need to bitswap and convert to unsigned shorts before creating
6825 the bitmaps. */
6826 w32_init_fringe (terminal->rif);
6828 unblock_input ();
6830 return dpyinfo;
6833 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6834 void
6835 x_delete_display (struct w32_display_info *dpyinfo)
6837 /* FIXME: the only display info apparently can't be deleted. */
6838 /* free palette table */
6840 struct w32_palette_entry * plist;
6842 plist = dpyinfo->color_list;
6843 while (plist)
6845 struct w32_palette_entry * pentry = plist;
6846 plist = plist->next;
6847 xfree (pentry);
6849 dpyinfo->color_list = NULL;
6850 if (dpyinfo->palette)
6851 DeleteObject (dpyinfo->palette);
6853 w32_reset_fringes ();
6857 /* Set up use of W32. */
6859 DWORD WINAPI w32_msg_worker (void * arg);
6861 static void
6862 w32_initialize (void)
6864 HANDLE shell;
6865 HRESULT (WINAPI * set_user_model) (const wchar_t * id);
6867 baud_rate = 19200;
6869 w32_system_caret_hwnd = NULL;
6870 w32_system_caret_height = 0;
6871 w32_system_caret_x = 0;
6872 w32_system_caret_y = 0;
6874 /* On Windows 7 and later, we need to set the user model ID
6875 to associate emacsclient launched files with Emacs frames
6876 in the UI. */
6877 shell = GetModuleHandle ("shell32.dll");
6878 if (shell)
6880 set_user_model
6881 = (void *) GetProcAddress (shell,
6882 "SetCurrentProcessExplicitAppUserModelID");
6884 /* If the function is defined, then we are running on Windows 7
6885 or newer, and the UI uses this to group related windows
6886 together. Since emacs, runemacs, emacsclient are related, we
6887 want them grouped even though the executables are different,
6888 so we need to set a consistent ID between them. */
6889 if (set_user_model)
6890 set_user_model (L"GNU.Emacs");
6893 #ifdef CYGWIN
6894 if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
6895 fatal ("opening /dev/windows: %s", strerror (errno));
6896 #endif /* CYGWIN */
6898 /* Initialize w32_use_visible_system_caret based on whether a screen
6899 reader is in use. */
6900 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6901 &w32_use_visible_system_caret, 0))
6902 w32_use_visible_system_caret = 0;
6904 any_help_event_p = 0;
6906 /* Initialize input mode: interrupt_input off, no flow control, allow
6907 8 bit character input, standard quit char. */
6908 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6911 LCID input_locale_id = LOWORD (GetKeyboardLayout (0));
6912 w32_keyboard_codepage = codepage_for_locale (input_locale_id);
6915 /* Create the window thread - it will terminate itself when the app
6916 terminates */
6917 init_crit ();
6919 dwMainThreadId = GetCurrentThreadId ();
6920 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6921 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6923 /* Wait for thread to start */
6925 MSG msg;
6927 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6929 hWindowsThread = CreateThread (NULL, 0,
6930 w32_msg_worker,
6931 0, 0, &dwWindowsThreadId);
6933 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6936 /* It is desirable that mainThread should have the same notion of
6937 focus window and active window as windowsThread. Unfortunately, the
6938 following call to AttachThreadInput, which should do precisely what
6939 we need, causes major problems when Emacs is linked as a console
6940 program. Unfortunately, we have good reasons for doing that, so
6941 instead we need to send messages to windowsThread to make some API
6942 calls for us (ones that affect, or depend on, the active/focus
6943 window state.) */
6944 #ifdef ATTACH_THREADS
6945 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6946 #endif
6948 /* Dynamically link to optional system components. */
6950 HMODULE user_lib = GetModuleHandle ("user32.dll");
6952 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6954 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6956 #undef LOAD_PROC
6958 /* Ensure scrollbar handles are at least 5 pixels. */
6959 vertical_scroll_bar_min_handle = 5;
6960 horizontal_scroll_bar_min_handle = 5;
6962 /* For either kind of scroll bar, take account of the arrows; these
6963 effectively form the border of the main scroll bar range. */
6964 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6965 = GetSystemMetrics (SM_CYVSCROLL);
6966 horizontal_scroll_bar_left_border = horizontal_scroll_bar_right_border
6967 = GetSystemMetrics (SM_CYHSCROLL);
6971 void
6972 syms_of_w32term (void)
6974 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6976 DEFSYM (Qadded, "added");
6977 DEFSYM (Qremoved, "removed");
6978 DEFSYM (Qmodified, "modified");
6979 DEFSYM (Qrenamed_from, "renamed-from");
6980 DEFSYM (Qrenamed_to, "renamed-to");
6982 DEFVAR_INT ("w32-num-mouse-buttons",
6983 w32_num_mouse_buttons,
6984 doc: /* Number of physical mouse buttons. */);
6985 w32_num_mouse_buttons = 2;
6987 DEFVAR_LISP ("w32-swap-mouse-buttons",
6988 Vw32_swap_mouse_buttons,
6989 doc: /* Swap the mapping of middle and right mouse buttons.
6990 When nil, middle button is mouse-2 and right button is mouse-3. */);
6991 Vw32_swap_mouse_buttons = Qnil;
6993 DEFVAR_LISP ("w32-grab-focus-on-raise",
6994 Vw32_grab_focus_on_raise,
6995 doc: /* Raised frame grabs input focus.
6996 When t, `raise-frame' grabs input focus as well. This fits well
6997 with the normal Windows click-to-focus policy, but might not be
6998 desirable when using a point-to-focus policy. */);
6999 Vw32_grab_focus_on_raise = Qt;
7001 DEFVAR_LISP ("w32-capslock-is-shiftlock",
7002 Vw32_capslock_is_shiftlock,
7003 doc: /* Apply CapsLock state to non character input keys.
7004 When nil, CapsLock only affects normal character input keys. */);
7005 Vw32_capslock_is_shiftlock = Qnil;
7007 DEFVAR_LISP ("w32-recognize-altgr",
7008 Vw32_recognize_altgr,
7009 doc: /* Recognize right-alt and left-ctrl as AltGr.
7010 When nil, the right-alt and left-ctrl key combination is
7011 interpreted normally. */);
7012 Vw32_recognize_altgr = Qt;
7014 DEFVAR_BOOL ("w32-use-visible-system-caret",
7015 w32_use_visible_system_caret,
7016 doc: /* Flag to make the system caret visible.
7017 When this is non-nil, Emacs will indicate the position of point by
7018 using the system caret instead of drawing its own cursor. Some screen
7019 reader software does not track the system cursor properly when it is
7020 invisible, and gets confused by Emacs drawing its own cursor, so this
7021 variable is initialized to t when Emacs detects that screen reader
7022 software is running as it starts up.
7024 When this variable is set, other variables affecting the appearance of
7025 the cursor have no effect. */);
7027 w32_use_visible_system_caret = 0;
7029 /* We don't yet support this, but defining this here avoids whining
7030 from cus-start.el and other places, like "M-x set-variable". */
7031 DEFVAR_BOOL ("x-use-underline-position-properties",
7032 x_use_underline_position_properties,
7033 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
7034 A value of nil means ignore them. If you encounter fonts with bogus
7035 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7036 to 4.1, set this to nil. You can also use `underline-minimum-offset'
7037 to override the font's UNDERLINE_POSITION for small font display
7038 sizes. */);
7039 x_use_underline_position_properties = 0;
7041 DEFVAR_BOOL ("x-underline-at-descent-line",
7042 x_underline_at_descent_line,
7043 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7044 A value of nil means to draw the underline according to the value of the
7045 variable `x-use-underline-position-properties', which is usually at the
7046 baseline level. The default value is nil. */);
7047 x_underline_at_descent_line = 0;
7049 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7050 doc: /* Which toolkit scroll bars Emacs uses, if any.
7051 A value of nil means Emacs doesn't use toolkit scroll bars.
7052 With the X Window system, the value is a symbol describing the
7053 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
7054 With MS Windows or Nextstep, the value is t. */);
7055 Vx_toolkit_scroll_bars = Qt;
7057 DEFVAR_BOOL ("w32-unicode-filenames",
7058 w32_unicode_filenames,
7059 doc: /* Non-nil means use Unicode APIs when passing file names to the OS.
7060 A value of nil means file names passed to the OS APIs and returned
7061 from those APIs are encoded/decoded using the ANSI codepage
7062 specified by `file-name-coding-system'.
7064 This variable is set to non-nil by default when Emacs runs on Windows
7065 systems of the NT family, including W2K, XP, Vista, Windows 7 and
7066 Windows 8. It is set to nil on Windows 9X. */);
7067 w32_unicode_filenames = 0;
7069 DEFVAR_BOOL ("w32-enable-frame-resize-hack",
7070 w32_enable_frame_resize_hack,
7071 doc: /* Non-nil means enable hack for frame resizing on Windows.
7072 A value of nil means to resize frames by sending a corresponding request
7073 to the Windows API and changing the pixel sizes of the frame and its
7074 windows after the latter calls back. If this is non-nil, Emacs changes
7075 the pixel sizes of the frame and its windows at the time it sends the
7076 resize request to the API. */);
7077 w32_enable_frame_resize_hack = 1;
7079 /* Tell Emacs about this window system. */
7080 Fprovide (Qw32, Qnil);