]> code.delx.au - gnu-emacs/blob - src/w32term.c
Fix horizontal scroll bar dragging in R2L paragraphs.
[gnu-emacs] / src / w32term.c
1 /* Implementation of GUI terminal on the Microsoft Windows API.
2
3 Copyright (C) 1989, 1993-2014 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include "lisp.h"
24 #include "blockinput.h"
25 #include "w32term.h"
26
27 #include "systty.h"
28 #include "systime.h"
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <imm.h>
34
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"
54
55 #ifdef WINDOWSNT
56 #include "w32.h" /* for filename_from_utf16, filename_from_ansi */
57 #include "w32heap.h"
58 #endif
59
60 #ifndef WINDOWSNT
61 #include <io.h> /* for get_osfhandle */
62 #endif
63
64 #include <shellapi.h>
65
66 #include "font.h"
67 #include "w32font.h"
68 \f
69 /* Fringe bitmaps. */
70
71 static int max_fringe_bmp = 0;
72 static HBITMAP *fringe_bmp = 0;
73
74 /* Temporary variables for w32_read_socket. */
75
76 static int last_mousemove_x = 0;
77 static int last_mousemove_y = 0;
78
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
83
84 /* Non-zero means that a HELP_EVENT has been generated since Emacs
85 start. */
86
87 static int any_help_event_p;
88
89 extern unsigned int msh_mousewheel;
90
91 extern void free_frame_menubar (struct frame *);
92
93 extern int w32_codepage_for_font (char *fontname);
94 extern Cursor w32_load_cursor (LPCTSTR name);
95
96 #define x_any_window_to_frame x_window_to_frame
97 #define x_top_window_to_frame x_window_to_frame
98
99 \f
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;
103
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. */
108
109 typedef struct tagWCRANGE
110 {
111 WCHAR wcLow;
112 USHORT cGlyphs;
113 } WCRANGE;
114
115 typedef struct tagGLYPHSET
116 {
117 DWORD cbThis;
118 DWORD flAccel;
119 DWORD cGlyphsSupported;
120 DWORD cRanges;
121 WCRANGE ranges[1];
122 } GLYPHSET;
123
124 #endif /* compiling for pre-Win2k */
125
126 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
127 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
128
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
137
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
146
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;
159
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;
166
167 int last_scroll_bar_drag_pos;
168
169 /* Keyboard code page - may be changed by language-change events. */
170 int w32_keyboard_codepage;
171
172 /* Incremented by w32_read_socket whenever it really tries to read
173 events. */
174 static int volatile input_signal_count;
175
176 #ifdef CYGWIN
177 int w32_message_fd = -1;
178 #endif /* CYGWIN */
179
180 static void w32_handle_tool_bar_click (struct frame *,
181 struct input_event *);
182 static void w32_define_cursor (Window, Cursor);
183
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 *);
218
219 #ifdef GLYPH_DEBUG
220 static void x_check_font (struct frame *, struct font *);
221 #endif
222
223 static Lisp_Object Qvendor_specific_keysyms;
224 static Lisp_Object Qadded, Qremoved, Qmodified;
225 static Lisp_Object Qrenamed_from, Qrenamed_to;
226
227 \f
228 /***********************************************************************
229 Debugging
230 ***********************************************************************/
231
232 #if 0
233
234 /* This is a function useful for recording debugging information about
235 the sequence of occurrences in this file. */
236
237 struct record
238 {
239 char *locus;
240 int type;
241 };
242
243 struct record event_record[100];
244
245 int event_record_index;
246
247 record_event (char *locus, int type)
248 {
249 if (event_record_index == sizeof (event_record) / sizeof (struct record))
250 event_record_index = 0;
251
252 event_record[event_record_index].locus = locus;
253 event_record[event_record_index].type = type;
254 event_record_index++;
255 }
256
257 #endif /* 0 */
258 \f
259
260 void
261 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
262 XGCValues *xgcv)
263 {
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;
270 }
271
272 XGCValues *
273 XCreateGC (void *ignore, Window window, unsigned long mask, XGCValues *xgcv)
274 {
275 XGCValues *gc = xzalloc (sizeof (XGCValues));
276
277 XChangeGC (ignore, gc, mask, xgcv);
278
279 return gc;
280 }
281
282 void
283 XGetGCValues (void *ignore, XGCValues *gc,
284 unsigned long mask, XGCValues *xgcv)
285 {
286 XChangeGC (ignore, xgcv, mask, gc);
287 }
288
289 static void
290 w32_set_clip_rectangle (HDC hdc, RECT *rect)
291 {
292 if (rect)
293 {
294 HRGN clip_region = CreateRectRgnIndirect (rect);
295 SelectClipRgn (hdc, clip_region);
296 DeleteObject (clip_region);
297 }
298 else
299 SelectClipRgn (hdc, NULL);
300 }
301
302 /* Restore clipping rectangle in S */
303 static void
304 w32_restore_glyph_string_clip (struct glyph_string *s)
305 {
306 RECT *r = s->clip;
307 int n = s->num_clips;
308
309 if (n == 1)
310 w32_set_clip_rectangle (s->hdc, r);
311 else if (n > 1)
312 {
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);
319 }
320 }
321
322 /*
323 Draw a wavy line under S. The wave fills wave_height pixels from y0.
324
325 x0 wave_length = 2
326 --
327 y0 * * * * *
328 |* * * * * * * * *
329 wave_height = 3 | * * * *
330
331 */
332
333 void
334 w32_draw_underwave (struct glyph_string *s, COLORREF color)
335 {
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;
341
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;
348
349 /* Find and set clipping rectangle */
350
351 wave_clip.x = x0;
352 wave_clip.y = y0;
353 wave_clip.width = width;
354 wave_clip.height = wave_height;
355
356 get_glyph_string_clip_rect (s, &w32_string_clip);
357 CONVERT_TO_XRECT (string_clip, w32_string_clip);
358
359 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
360 return;
361
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);
366
367 /* Draw the waves */
368
369 x1 = x0 - (x0 % dx);
370 x2 = x1 + dx;
371 odd = (x1/dx) % 2;
372 y1 = y2 = y0;
373
374 if (odd)
375 y1 += dy;
376 else
377 y2 += dy;
378
379 MoveToEx (s->hdc, x1, y1, NULL);
380
381 while (x1 <= xmax)
382 {
383 LineTo (s->hdc, x2, y2);
384 x1 = x2, y1 = y2;
385 x2 += dx, y2 = y0 + odd*dy;
386 odd = !odd;
387 }
388
389 /* Restore previous pen and clipping rectangle(s) */
390 w32_restore_glyph_string_clip (s);
391 SelectObject (s->hdc, oldhp);
392 DeleteObject (hp);
393 }
394
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)
399 {
400 HBRUSH hb, oldhb;
401 HPEN hp, oldhp;
402
403 hb = CreateSolidBrush (gc->background);
404 hp = CreatePen (PS_SOLID, 0, gc->foreground);
405 oldhb = SelectObject (hdc, hb);
406 oldhp = SelectObject (hdc, hp);
407
408 Rectangle (hdc, x, y, x + width, y + height);
409
410 SelectObject (hdc, oldhb);
411 SelectObject (hdc, oldhp);
412 DeleteObject (hb);
413 DeleteObject (hp);
414 }
415
416 /* Draw a filled rectangle at the specified position. */
417 void
418 w32_fill_rect (struct frame *f, HDC hdc, COLORREF pix, RECT *lprect)
419 {
420 HBRUSH hb;
421
422 hb = CreateSolidBrush (pix);
423 FillRect (hdc, lprect, hb);
424 DeleteObject (hb);
425 }
426
427 void
428 w32_clear_window (struct frame *f)
429 {
430 RECT rect;
431 HDC hdc = get_frame_dc (f);
432
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)
437 {
438 GetClientRect (FRAME_W32_WINDOW (f), &rect);
439 w32_clear_rect (f, hdc, &rect);
440 }
441
442 release_frame_dc (f, hdc);
443 }
444
445 #define OPAQUE_FRAME 255
446
447 void
448 x_set_frame_alpha (struct frame *f)
449 {
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);
456
457 /* Older versions of Windows do not support transparency. */
458 if (!pfnSetLayeredWindowAttributes)
459 return;
460
461 if (dpyinfo->w32_focus_frame == f)
462 alpha = f->alpha[0];
463 else
464 alpha = f->alpha[1];
465
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;
470
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;
477
478 opac = alpha * OPAQUE_FRAME;
479
480 ex_style = GetWindowLong (window, GWL_EXSTYLE);
481
482 if (opac == OPAQUE_FRAME)
483 ex_style &= ~WS_EX_LAYERED;
484 else
485 ex_style |= WS_EX_LAYERED;
486
487 SetWindowLong (window, GWL_EXSTYLE, ex_style);
488
489 if (opac != OPAQUE_FRAME)
490 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
491 }
492
493 int
494 x_display_pixel_height (struct w32_display_info *dpyinfo)
495 {
496 int pixels = GetSystemMetrics (SM_CYVIRTUALSCREEN);
497
498 if (pixels == 0)
499 /* Fallback for Windows 95 or NT 4.0. */
500 pixels = GetSystemMetrics (SM_CYSCREEN);
501
502 return pixels;
503 }
504
505 int
506 x_display_pixel_width (struct w32_display_info *dpyinfo)
507 {
508 int pixels = GetSystemMetrics (SM_CXVIRTUALSCREEN);
509
510 if (pixels == 0)
511 /* Fallback for Windows 95 or NT 4.0. */
512 pixels = GetSystemMetrics (SM_CXSCREEN);
513
514 return pixels;
515 }
516
517 \f
518 /***********************************************************************
519 Starting and ending an update
520 ***********************************************************************/
521
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. */
526
527 static void
528 x_update_begin (struct frame *f)
529 {
530 struct w32_display_info *display_info = FRAME_DISPLAY_INFO (f);
531
532 if (! FRAME_W32_P (f))
533 return;
534
535 /* Regenerate display palette before drawing if list of requested
536 colors has changed. */
537 if (display_info->regen_palette)
538 {
539 w32_regenerate_palette (f);
540 display_info->regen_palette = FALSE;
541 }
542 }
543
544
545 /* Start update of window W. */
546
547 static void
548 x_update_window_begin (struct window *w)
549 {
550 struct frame *f = XFRAME (WINDOW_FRAME (w));
551 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
552
553 /* Hide the system caret during an update. */
554 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
555 {
556 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
557 }
558
559 w->output_cursor = w->cursor;
560
561 block_input ();
562
563 if (f == hlinfo->mouse_face_mouse_frame)
564 {
565 /* Don't do highlighting for mouse motion during the update. */
566 hlinfo->mouse_face_defer = 1;
567
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;
572
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. */
579
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))
587 {
588 int i;
589
590 for (i = 0; i < w->desired_matrix->nrows; ++i)
591 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
592 break;
593
594 if (i < w->desired_matrix->nrows)
595 clear_mouse_face (hlinfo);
596 }
597 #endif /* 0 */
598 }
599
600 unblock_input ();
601 }
602
603 /* Draw a vertical window border from (x,y0) to (x,y1) */
604
605 static void
606 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
607 {
608 struct frame *f = XFRAME (WINDOW_FRAME (w));
609 RECT r;
610 HDC hdc;
611 struct face *face;
612
613 r.left = x;
614 r.right = x + 1;
615 r.top = y0;
616 r.bottom = y1;
617
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);
624
625 release_frame_dc (f, hdc);
626 }
627
628
629 /* Draw a window divider from (x0, y0) to (x1, y1) */
630
631 static void
632 w32_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
633 {
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));
646
647 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
648 /* Vertical. */
649 {
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);
653 }
654 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
655 /* Horizontal. */
656 {
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);
660 }
661 else
662 w32_fill_area_abs (f, hdc, color, x0, y0, x1, y1);
663
664 release_frame_dc (f, hdc);
665 }
666
667 /* End update of window W.
668
669 Draw vertical borders between horizontally adjacent windows, and
670 display W's cursor if CURSOR_ON_P is non-zero.
671
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.
675
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. */
679
680 static void
681 x_update_window_end (struct window *w, bool cursor_on_p,
682 bool mouse_face_overwritten_p)
683 {
684 if (!w->pseudo_window_p)
685 {
686 block_input ();
687
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);
692
693 if (draw_window_fringes (w, 1))
694 {
695 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
696 x_draw_right_divider (w);
697 else
698 x_draw_vertical_border (w);
699 }
700
701 unblock_input ();
702 }
703
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)
707 {
708 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
709
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;
713 }
714
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)
719 {
720 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
721 }
722 }
723
724
725 /* End update of frame F. This function is installed as a hook in
726 update_end. */
727
728 static void
729 x_update_end (struct frame *f)
730 {
731 if (! FRAME_W32_P (f))
732 return;
733
734 /* Mouse highlight may be displayed again. */
735 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
736 }
737
738
739 /* This function is called from various places in xdisp.c
740 whenever a complete update has been performed. */
741
742 static void
743 w32_frame_up_to_date (struct frame *f)
744 {
745 if (FRAME_W32_P (f))
746 FRAME_MOUSE_UPDATE (f);
747 }
748
749
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. */
755
756 static void
757 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
758 {
759 struct frame *f;
760 int width, height;
761
762 eassert (w);
763
764 if (!desired_row->mode_line_p && !w->pseudo_window_p)
765 desired_row->redraw_fringe_bitmaps_p = 1;
766
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))
780 {
781 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
782
783 block_input ();
784 {
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);
790 }
791 unblock_input ();
792 }
793 }
794
795
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. */
800
801 static void
802 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
803 struct draw_fringe_bitmap_params *p)
804 {
805 struct frame *f = XFRAME (WINDOW_FRAME (w));
806 HDC hdc;
807 struct face *face = p->face;
808
809 hdc = get_frame_dc (f);
810
811 /* Must clip because of partially visible lines. */
812 w32_clip_to_row (w, row, ANY_AREA, hdc);
813
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);
817
818 if (p->which && p->which < max_fringe_bmp)
819 {
820 HBITMAP pixmap = fringe_bmp[p->which];
821 HDC compat_hdc;
822 HANDLE horig_obj;
823
824 compat_hdc = CreateCompatibleDC (hdc);
825
826 SaveDC (hdc);
827
828 horig_obj = SelectObject (compat_hdc, pixmap);
829
830 /* Paint overlays transparently. */
831 if (p->overlay_p)
832 {
833 HBRUSH h_brush, h_orig_brush;
834
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);
839
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);
849
850 SelectObject (hdc, h_orig_brush);
851 DeleteObject (h_brush);
852 }
853 else
854 {
855 SetTextColor (hdc, face->background);
856 SetBkColor (hdc, (p->cursor_p
857 ? f->output_data.w32->cursor_pixel
858 : face->foreground));
859
860 BitBlt (hdc, p->x, p->y, p->wd, p->h,
861 compat_hdc, 0, p->dh,
862 SRCCOPY);
863 }
864
865 SelectObject (compat_hdc, horig_obj);
866 DeleteDC (compat_hdc);
867 RestoreDC (hdc, -1);
868 }
869
870 w32_set_clip_rectangle (hdc, NULL);
871
872 release_frame_dc (f, hdc);
873 }
874
875 static void
876 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
877 {
878 if (which >= max_fringe_bmp)
879 {
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;
885 }
886
887 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
888 }
889
890 static void
891 w32_destroy_fringe_bitmap (int which)
892 {
893 if (which >= max_fringe_bmp)
894 return;
895
896 if (fringe_bmp[which])
897 DeleteObject (fringe_bmp[which]);
898 fringe_bmp[which] = 0;
899 }
900
901 /***********************************************************************
902 Display Iterator
903 ***********************************************************************/
904
905 /* Function prototypes of this page. */
906
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 *);
933
934
935 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
936 face. */
937
938 static void
939 x_set_cursor_gc (struct glyph_string *s)
940 {
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
947 {
948 /* Cursor on non-default face: must merge. */
949 XGCValues xgcv;
950 unsigned long mask;
951
952 xgcv.background = s->f->output_data.w32->cursor_pixel;
953 xgcv.foreground = s->face->background;
954
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;
962
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)
966 {
967 xgcv.background = s->face->foreground;
968 xgcv.foreground = s->face->background;
969 }
970
971 IF_DEBUG (x_check_font (s->f, s->font));
972 xgcv.font = s->font;
973 mask = GCForeground | GCBackground | GCFont;
974
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);
981
982 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
983 }
984 }
985
986
987 /* Set up S->gc of glyph string S for drawing text in mouse face. */
988
989 static void
990 x_set_mouse_face_gc (struct glyph_string *s)
991 {
992 int face_id;
993 struct face *face;
994
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);
1000
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);
1007
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
1012 {
1013 /* Otherwise construct scratch_cursor_gc with values from FACE
1014 but font FONT. */
1015 XGCValues xgcv;
1016 unsigned long mask;
1017
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;
1023
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);
1030
1031 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1032 }
1033
1034 eassert (s->gc != 0);
1035 }
1036
1037
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. */
1041
1042 static inline void
1043 x_set_mode_line_face_gc (struct glyph_string *s)
1044 {
1045 s->gc = s->face->gc;
1046 }
1047
1048
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. */
1052
1053 static inline void
1054 x_set_glyph_string_gc (struct glyph_string *s)
1055 {
1056 prepare_face_for_display (s->f, s->face);
1057
1058 if (s->hl == DRAW_NORMAL_TEXT)
1059 {
1060 s->gc = s->face->gc;
1061 s->stippled_p = s->face->stipple != 0;
1062 }
1063 else if (s->hl == DRAW_INVERSE_VIDEO)
1064 {
1065 x_set_mode_line_face_gc (s);
1066 s->stippled_p = s->face->stipple != 0;
1067 }
1068 else if (s->hl == DRAW_CURSOR)
1069 {
1070 x_set_cursor_gc (s);
1071 s->stippled_p = 0;
1072 }
1073 else if (s->hl == DRAW_MOUSE_FACE)
1074 {
1075 x_set_mouse_face_gc (s);
1076 s->stippled_p = s->face->stipple != 0;
1077 }
1078 else if (s->hl == DRAW_IMAGE_RAISED
1079 || s->hl == DRAW_IMAGE_SUNKEN)
1080 {
1081 s->gc = s->face->gc;
1082 s->stippled_p = s->face->stipple != 0;
1083 }
1084 else
1085 emacs_abort ();
1086
1087 /* GC must have been set. */
1088 eassert (s->gc != 0);
1089 }
1090
1091
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. */
1094
1095 static inline void
1096 x_set_glyph_string_clipping (struct glyph_string *s)
1097 {
1098 RECT *r = s->clip;
1099 int n = get_glyph_string_clip_rects (s, r, 2);
1100
1101 if (n == 1)
1102 w32_set_clip_rectangle (s->hdc, r);
1103 else if (n > 1)
1104 {
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);
1111 }
1112 s->num_clips = n;
1113 }
1114
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. */
1118
1119 static void
1120 x_set_glyph_string_clipping_exactly (struct glyph_string *src,
1121 struct glyph_string *dst)
1122 {
1123 RECT r;
1124
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);
1132 }
1133
1134 /* RIF:
1135 Compute left and right overhang of glyph string S. */
1136
1137 static void
1138 w32_compute_glyph_string_overhangs (struct glyph_string *s)
1139 {
1140 if (s->cmp == NULL
1141 && s->first_glyph->type == CHAR_GLYPH
1142 && !s->font_not_found_p)
1143 {
1144 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1145 struct font *font = s->font;
1146 struct font_metrics metrics;
1147 int i;
1148
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;
1155 }
1156 else if (s->cmp)
1157 {
1158 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1159 s->left_overhang = -s->cmp->lbearing;
1160 }
1161 }
1162
1163 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1164
1165 static inline void
1166 x_clear_glyph_string_rect (struct glyph_string *s,
1167 int x, int y, int w, int h)
1168 {
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)
1176 {
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);
1183 }
1184 #endif
1185 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1186 real_w, real_h);
1187 }
1188
1189
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. */
1195
1196 static void
1197 x_draw_glyph_string_background (struct glyph_string *s, int force_p)
1198 {
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)
1202 {
1203 int box_line_width = max (s->face->box_line_width, 0);
1204
1205 #if 0 /* TODO: stipple */
1206 if (s->stippled_p)
1207 {
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;
1216 }
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)
1223 {
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;
1228 }
1229 }
1230 }
1231
1232
1233 /* Draw the foreground of glyph string S. */
1234
1235 static void
1236 x_draw_glyph_string_foreground (struct glyph_string *s)
1237 {
1238 int i, x;
1239
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;
1247
1248 SetTextColor (s->hdc, s->gc->foreground);
1249 SetBkColor (s->hdc, s->gc->background);
1250 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1251
1252 /* Draw characters of S as rectangles if S's font could not be
1253 loaded. */
1254 if (s->font_not_found_p)
1255 {
1256 for (i = 0; i < s->nchars; ++i)
1257 {
1258 struct glyph *g = s->first_glyph + i;
1259
1260 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1261 s->height - 1);
1262 x += g->pixel_width;
1263 }
1264 }
1265 else
1266 {
1267 struct font *font = s->font;
1268 int boff = font->baseline_offset;
1269 int y;
1270 HFONT old_font;
1271
1272 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1273
1274 if (font->vertical_centering)
1275 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1276
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);
1285
1286 SelectObject (s->hdc, old_font);
1287 }
1288 }
1289
1290 /* Draw the foreground of composite glyph string S. */
1291
1292 static void
1293 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1294 {
1295 int i, j, x;
1296 struct font *font = s->font;
1297
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;
1305
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. */
1310
1311 SetTextColor (s->hdc, s->gc->foreground);
1312 SetBkColor (s->hdc, s->gc->background);
1313 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1314
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)
1318 {
1319 if (s->cmp_from == 0)
1320 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1321 s->height - 1);
1322 }
1323 else if (! s->first_glyph->u.cmp.automatic)
1324 {
1325 int y = s->ybase;
1326 HFONT old_font;
1327
1328 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1329
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')
1334 {
1335 int xx = x + s->cmp->offsets[j * 2];
1336 int yy = y - s->cmp->offsets[j * 2 + 1];
1337
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);
1341 }
1342 SelectObject (s->hdc, old_font);
1343 }
1344 else
1345 {
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;
1351
1352 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1353
1354 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1355 {
1356 glyph = LGSTRING_GLYPH (gstring, i);
1357 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1358 width += LGLYPH_WIDTH (glyph);
1359 else
1360 {
1361 int xoff, yoff, wadjust;
1362
1363 if (j < i)
1364 {
1365 font->driver->draw (s, j, i, x, y, 0);
1366 x += width;
1367 }
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;
1375 }
1376 }
1377 if (j < i)
1378 font->driver->draw (s, j, i, x, y, 0);
1379
1380 SelectObject (s->hdc, old_font);
1381 }
1382 }
1383
1384
1385 /* Draw the foreground of glyph string S for glyphless characters. */
1386
1387 static void
1388 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1389 {
1390 struct glyph *glyph = s->first_glyph;
1391 XChar2b char2b[8];
1392 int x, i, j;
1393 int with_background;
1394
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;
1402
1403 SetTextColor (s->hdc, s->gc->foreground);
1404 SetBkColor (s->hdc, s->gc->background);
1405 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1406
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++)
1411 {
1412 char buf[7], *str = NULL;
1413 int len = glyph->u.glyphless.len;
1414
1415 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1416 {
1417 if (len > 1
1418 && CHAR_TABLE_P (Vglyphless_char_display)
1419 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1420 >= 1))
1421 {
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);
1429 }
1430 }
1431 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1432 {
1433 sprintf ((char *) buf, "%0*X",
1434 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1435 glyph->u.glyphless.ch);
1436 str = buf;
1437 }
1438
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)
1445 {
1446 struct font *font = s->font;
1447 int upper_len = (len + 1) / 2;
1448 unsigned code;
1449 HFONT old_font;
1450
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++)
1454 {
1455 code = font->driver->encode_char (font, str[j]);
1456 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1457 }
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);
1467 }
1468 x += glyph->pixel_width;
1469 }
1470 }
1471
1472
1473 /* Brightness beyond which a color won't have its highlight brightness
1474 boosted.
1475
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.
1481
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
1485
1486
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. */
1493
1494 static int
1495 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1496 double factor, int delta)
1497 {
1498 COLORREF new;
1499 long bright;
1500
1501 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1502 delta /= 256;
1503
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)));
1509
1510 /* Calculate brightness of COLOR. */
1511 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1512 + GetBValue (*color)) / 6;
1513
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. */
1519 {
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;
1524
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))));
1533 }
1534
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))));
1539
1540 /* TODO: Map to palette and retry with delta if same? */
1541 /* TODO: Free colors (if using palette)? */
1542
1543 if (new == *color)
1544 return 0;
1545
1546 *color = new;
1547
1548 return 1;
1549 }
1550
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)
1556 {
1557 int i;
1558
1559 for (i = 0; i < ncolors; i++)
1560 {
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;
1566 }
1567 }
1568
1569 void
1570 x_query_color (struct frame *f, XColor *color)
1571 {
1572 x_query_colors (f, color, 1);
1573 }
1574
1575
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. */
1582
1583 static void
1584 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1585 int delta, COLORREF default_pixel)
1586 {
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);
1593
1594 /* TODO: Free colors (if using palette)? */
1595
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;
1601
1602 if (relief->gc == 0)
1603 {
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);
1609 }
1610 else
1611 XChangeGC (NULL, relief->gc, mask, &xgcv);
1612 }
1613
1614
1615 /* Set up colors for the relief lines around glyph string S. */
1616
1617 static void
1618 x_setup_relief_colors (struct glyph_string *s)
1619 {
1620 struct w32_output *di = s->f->output_data.w32;
1621 COLORREF color;
1622
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;
1631
1632 if (di->white_relief.gc == 0
1633 || color != di->relief_background)
1634 {
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));
1640 }
1641 }
1642
1643
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. */
1651
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)
1658 {
1659 int i;
1660 XGCValues gc;
1661 HDC hdc = get_frame_dc (f);
1662
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;
1667
1668 w32_set_clip_rectangle (hdc, clip_rect);
1669
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);
1676
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);
1683
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;
1688
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);
1695
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);
1702
1703 w32_set_clip_rectangle (hdc, NULL);
1704
1705 release_frame_dc (f, hdc);
1706 }
1707
1708
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. */
1715
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)
1720 {
1721 w32_set_clip_rectangle (s->hdc, clip_rect);
1722
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);
1726
1727 /* Left. */
1728 if (left_p)
1729 {
1730 w32_fill_area (s->f, s->hdc, s->face->box_color,
1731 left_x, top_y, width, bottom_y - top_y + 1);
1732 }
1733
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);
1737
1738 /* Right. */
1739 if (right_p)
1740 {
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);
1743 }
1744
1745 w32_set_clip_rectangle (s->hdc, NULL);
1746 }
1747
1748
1749 /* Draw a box around glyph string S. */
1750
1751 static void
1752 x_draw_glyph_string_box (struct glyph_string *s)
1753 {
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;
1758
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));
1762
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);
1767
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;
1776
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)));
1785
1786 get_glyph_string_clip_rect (s, &clip_rect);
1787
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
1792 {
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);
1796 }
1797 }
1798
1799
1800 /* Draw foreground of image glyph string S. */
1801
1802 static void
1803 x_draw_image_foreground (struct glyph_string *s)
1804 {
1805 int x = s->x;
1806 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1807
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);
1814
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;
1821
1822 SaveDC (s->hdc);
1823
1824 if (s->img->pixmap)
1825 {
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);
1833
1834 if (s->img->mask)
1835 {
1836 HDC mask_dc = CreateCompatibleDC (s->hdc);
1837 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1838
1839 SetTextColor (s->hdc, RGB (255, 255, 255));
1840 SetBkColor (s->hdc, RGB (0, 0, 0));
1841
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);
1848
1849 SelectObject (mask_dc, mask_orig_obj);
1850 DeleteDC (mask_dc);
1851 }
1852 else
1853 {
1854 SetTextColor (s->hdc, s->gc->foreground);
1855 SetBkColor (s->hdc, s->gc->background);
1856
1857 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1858 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1859
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)
1867 {
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);
1873 }
1874 }
1875
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);
1881 }
1882 else
1883 w32_draw_rectangle (s->hdc, s->gc, x, y,
1884 s->slice.width - 1, s->slice.height - 1);
1885
1886 RestoreDC (s->hdc ,-1);
1887 }
1888
1889
1890 /* Draw a relief around the image glyph string S. */
1891
1892 static void
1893 x_draw_image_relief (struct glyph_string *s)
1894 {
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);
1900
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);
1907
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;
1914
1915 if (s->hl == DRAW_IMAGE_SUNKEN
1916 || s->hl == DRAW_IMAGE_RAISED)
1917 {
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;
1921 }
1922 else
1923 {
1924 thick = eabs (s->img->relief);
1925 raised_p = s->img->relief > 0;
1926 }
1927
1928 x1 = x + s->slice.width - 1;
1929 y1 = y + s->slice.height - 1;
1930
1931 extra_x = extra_y = 0;
1932 if (s->face->id == TOOL_BAR_FACE_ID)
1933 {
1934 if (CONSP (Vtool_bar_button_margin)
1935 && INTEGERP (XCAR (Vtool_bar_button_margin))
1936 && INTEGERP (XCDR (Vtool_bar_button_margin)))
1937 {
1938 extra_x = XINT (XCAR (Vtool_bar_button_margin));
1939 extra_y = XINT (XCDR (Vtool_bar_button_margin));
1940 }
1941 else if (INTEGERP (Vtool_bar_button_margin))
1942 extra_x = extra_y = XINT (Vtool_bar_button_margin);
1943 }
1944
1945 top_p = bot_p = left_p = right_p = 0;
1946
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;
1955
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);
1960 }
1961
1962
1963 /* Draw the foreground of image glyph string S to PIXMAP. */
1964
1965 static void
1966 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
1967 {
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);
1972
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);
1979
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;
1986
1987 if (s->img->pixmap)
1988 {
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);
1993
1994 if (s->img->mask)
1995 {
1996 HDC mask_dc = CreateCompatibleDC (hdc);
1997 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1998
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);
2007
2008 SelectObject (mask_dc, mask_orig_obj);
2009 DeleteDC (mask_dc);
2010 }
2011 else
2012 {
2013 SetTextColor (hdc, s->gc->foreground);
2014 SetBkColor (hdc, s->gc->background);
2015
2016 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2017 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2018
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)
2026 {
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);
2032 }
2033 }
2034
2035 SelectObject (hdc, orig_brush);
2036 DeleteObject (fg_brush);
2037 SelectObject (compat_hdc, orig_obj);
2038 DeleteDC (compat_hdc);
2039 }
2040 else
2041 w32_draw_rectangle (hdc, s->gc, x, y,
2042 s->slice.width - 1, s->slice.height - 1);
2043
2044 SelectObject (hdc, orig_hdc_obj);
2045 DeleteDC (hdc);
2046 }
2047
2048
2049 /* Draw part of the background of glyph string S. X, Y, W, and H
2050 give the rectangle to draw. */
2051
2052 static void
2053 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2054 {
2055 #if 0 /* TODO: stipple */
2056 if (s->stippled_p)
2057 {
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);
2062 }
2063 else
2064 #endif
2065 x_clear_glyph_string_rect (s, x, y, w, h);
2066 }
2067
2068
2069 /* Draw image glyph string S.
2070
2071 s->y
2072 s->x +-------------------------
2073 | s->face->box
2074 |
2075 | +-------------------------
2076 | | s->img->vmargin
2077 | |
2078 | | +-------------------
2079 | | | the image
2080
2081 */
2082
2083 static void
2084 x_draw_image_glyph_string (struct glyph_string *s)
2085 {
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;
2091
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;
2097
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)
2108 {
2109 width = s->background_width;
2110 x = s->x;
2111 if (s->first_glyph->left_box_line_p
2112 && s->slice.x == 0)
2113 {
2114 x += box_line_hwidth;
2115 width -= box_line_hwidth;
2116 }
2117
2118 y = s->y;
2119 if (s->slice.y == 0)
2120 y += box_line_vwidth;
2121
2122 #if 0 /* TODO: figure out if we need to do this on Windows. */
2123 if (s->img->mask)
2124 {
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);
2130
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);
2135
2136 /* Don't clip in the following because we're working on the
2137 pixmap. */
2138 XSetClipMask (s->display, s->gc, None);
2139
2140 /* Fill the pixmap with the background color/stipple. */
2141 if (s->stippled_p)
2142 {
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);
2148 }
2149 else
2150 {
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);
2158 }
2159 }
2160 else
2161 #endif
2162 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2163
2164 s->background_filled_p = 1;
2165 }
2166
2167 /* Draw the foreground. */
2168 if (pixmap != 0)
2169 {
2170 w32_draw_image_foreground_1 (s, pixmap);
2171 x_set_glyph_string_clipping (s);
2172 {
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);
2177
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);
2182
2183 SelectObject (s->hdc, orig_brush);
2184 DeleteObject (fg_brush);
2185 SelectObject (compat_hdc, orig_obj);
2186 DeleteDC (compat_hdc);
2187 }
2188 DeleteObject (pixmap);
2189 pixmap = 0;
2190 }
2191 else
2192 x_draw_image_foreground (s);
2193
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);
2199 }
2200
2201
2202 /* Draw stretch glyph string S. */
2203
2204 static void
2205 x_draw_stretch_glyph_string (struct glyph_string *s)
2206 {
2207 eassert (s->first_glyph->type == STRETCH_GLYPH);
2208
2209 if (s->hl == DRAW_CURSOR
2210 && !x_stretch_cursor_p)
2211 {
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;
2216
2217 if (!s->row->reversed_p)
2218 {
2219 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2220
2221 if (x < left_x)
2222 {
2223 background_width -= left_x - x;
2224 x = left_x;
2225 }
2226 }
2227 else
2228 {
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);
2232
2233 if (x + background_width > right_x)
2234 background_width -= x - right_x;
2235 x += background_width;
2236 }
2237 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2238 if (s->row->reversed_p)
2239 x -= width;
2240
2241 /* Draw cursor. */
2242 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2243
2244 /* Clear rest using the GC of the original non-cursor face. */
2245 if (width < background_width)
2246 {
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;
2252
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))
2259 {
2260 x_set_mouse_face_gc (s);
2261 gc = s->gc;
2262 }
2263 else
2264 gc = s->face->gc;
2265
2266 get_glyph_string_clip_rect (s, &r);
2267 w32_set_clip_rectangle (hdc, &r);
2268
2269 #if 0 /* TODO: stipple */
2270 if (s->face->stipple)
2271 {
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);
2276 }
2277 else
2278 #endif
2279 {
2280 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2281 }
2282 }
2283 }
2284 else if (!s->background_filled_p)
2285 {
2286 int background_width = s->background_width;
2287 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2288
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)
2292 {
2293 background_width -= left_x - x;
2294 x = left_x;
2295 }
2296 if (background_width > 0)
2297 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2298 }
2299
2300 s->background_filled_p = 1;
2301 }
2302
2303
2304 /* Draw glyph string S. */
2305
2306 static void
2307 x_draw_glyph_string (struct glyph_string *s)
2308 {
2309 bool relief_drawn_p = 0;
2310
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)
2315 {
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)
2322 {
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;
2330 }
2331 }
2332
2333 /* Set up S->gc, set clipping and draw S. */
2334 x_set_glyph_string_gc (s);
2335
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))
2342
2343 {
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;
2349 }
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);
2360
2361 switch (s->first_glyph->type)
2362 {
2363 case IMAGE_GLYPH:
2364 x_draw_image_glyph_string (s);
2365 break;
2366
2367 case STRETCH_GLYPH:
2368 x_draw_stretch_glyph_string (s);
2369 break;
2370
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;
2378
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;
2387
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;
2395
2396 default:
2397 emacs_abort ();
2398 }
2399
2400 if (!s->for_overlaps)
2401 {
2402 /* Draw underline. */
2403 if (s->face->underline_p)
2404 {
2405 if (s->face->underline_type == FACE_UNDER_WAVE)
2406 {
2407 COLORREF color;
2408
2409 if (s->face->underline_defaulted_p)
2410 color = s->gc->foreground;
2411 else
2412 color = s->face->underline_color;
2413
2414 w32_draw_underwave (s, color);
2415 }
2416 else if (s->face->underline_type == FACE_UNDER_LINE)
2417 {
2418 unsigned long thickness, position;
2419 int y;
2420
2421 if (s->prev && s->prev->face->underline_p
2422 && s->prev->face->underline_type == FACE_UNDER_LINE)
2423 {
2424 /* We use the same underline style as the previous one. */
2425 thickness = s->prev->underline_thickness;
2426 position = s->prev->underline_position;
2427 }
2428 else
2429 {
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
2438 {
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
2443
2444 ROUND ((maximum_descent) / 2), with
2445 ROUND (x) = floor (x + 0.5) */
2446
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;
2452 }
2453 position = max (position, underline_minimum_offset);
2454 }
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)
2465 {
2466 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2467 y, s->width, 1);
2468 }
2469 else
2470 {
2471 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2472 y, s->width, 1);
2473 }
2474 }
2475 }
2476 /* Draw overline. */
2477 if (s->face->overline_p)
2478 {
2479 unsigned long dy = 0, h = 1;
2480
2481 if (s->face->overline_color_defaulted_p)
2482 {
2483 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2484 s->y + dy, s->width, h);
2485 }
2486 else
2487 {
2488 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2489 s->y + dy, s->width, h);
2490 }
2491 }
2492
2493 /* Draw strike-through. */
2494 if (s->face->strike_through_p
2495 && !FONT_TEXTMETRIC (s->font).tmStruckOut)
2496 {
2497 unsigned long h = 1;
2498 unsigned long dy = (s->height - h) / 2;
2499
2500 if (s->face->strike_through_color_defaulted_p)
2501 {
2502 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2503 s->width, h);
2504 }
2505 else
2506 {
2507 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2508 s->y + dy, s->width, h);
2509 }
2510 }
2511
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);
2515
2516 if (s->prev)
2517 {
2518 struct glyph_string *prev;
2519
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)
2523 {
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;
2527
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;
2538 }
2539 }
2540
2541 if (s->next)
2542 {
2543 struct glyph_string *next;
2544
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)
2548 {
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;
2552
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;
2564 }
2565 }
2566 }
2567
2568 /* Reset clipping. */
2569 w32_set_clip_rectangle (s->hdc, NULL);
2570 s->num_clips = 0;
2571 }
2572
2573
2574 /* Shift display to make room for inserted glyphs. */
2575
2576 void
2577 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2578 int width, int height, int shift_by)
2579 {
2580 HDC hdc;
2581
2582 hdc = get_frame_dc (f);
2583 BitBlt (hdc, x + shift_by, y, width, height,
2584 hdc, x, y, SRCCOPY);
2585
2586 release_frame_dc (f, hdc);
2587 }
2588
2589
2590 /* Delete N glyphs at the nominal cursor position. Not implemented
2591 for X frames. */
2592
2593 static void
2594 x_delete_glyphs (struct frame *f, register int n)
2595 {
2596 if (! FRAME_W32_P (f))
2597 return;
2598
2599 emacs_abort ();
2600 }
2601
2602
2603 /* Clear entire frame. */
2604
2605 static void
2606 x_clear_frame (struct frame *f)
2607 {
2608 if (! FRAME_W32_P (f))
2609 return;
2610
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)));
2614
2615 block_input ();
2616
2617 w32_clear_window (f);
2618
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);
2622
2623 unblock_input ();
2624 }
2625
2626 \f
2627 /* Make audible bell. */
2628
2629 static void
2630 w32_ring_bell (struct frame *f)
2631 {
2632 block_input ();
2633
2634 if (FRAME_W32_P (f) && visible_bell)
2635 {
2636 int i;
2637 HWND hwnd = FRAME_W32_WINDOW (f);
2638
2639 for (i = 0; i < 5; i++)
2640 {
2641 FlashWindow (hwnd, TRUE);
2642 Sleep (10);
2643 }
2644 FlashWindow (hwnd, FALSE);
2645 }
2646 else
2647 w32_sys_ring_bell (f);
2648
2649 unblock_input ();
2650 }
2651
2652 /***********************************************************************
2653 Line Dance
2654 ***********************************************************************/
2655
2656 /* Perform an insert-lines or delete-lines operation, inserting N
2657 lines or deleting -N lines at vertical position VPOS. */
2658
2659 static void
2660 x_ins_del_lines (struct frame *f, int vpos, int n)
2661 {
2662 if (! FRAME_W32_P (f))
2663 return;
2664
2665 emacs_abort ();
2666 }
2667
2668
2669 /* Scroll part of the display as described by RUN. */
2670
2671 static void
2672 x_scroll_run (struct window *w, struct run *run)
2673 {
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;
2678
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);
2683
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;
2687
2688 if (to_y < from_y)
2689 {
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);
2697 }
2698 else
2699 {
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);
2707 }
2708
2709 block_input ();
2710
2711 /* Cursor off. Will be switched on again in x_update_window_end. */
2712 x_clear_cursor (w);
2713
2714 {
2715 RECT from;
2716 RECT to;
2717 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2718 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2719
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;
2726
2727 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2728 NULL, SW_INVALIDATE);
2729
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);
2733
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);
2737
2738 DeleteObject (dirty);
2739 DeleteObject (combined);
2740 }
2741
2742 unblock_input ();
2743 DeleteObject (expect_dirty);
2744 }
2745
2746
2747 \f
2748 /***********************************************************************
2749 Exposure Events
2750 ***********************************************************************/
2751
2752 static void
2753 frame_highlight (struct frame *f)
2754 {
2755 x_update_cursor (f, 1);
2756 x_set_frame_alpha (f);
2757 }
2758
2759 static void
2760 frame_unhighlight (struct frame *f)
2761 {
2762 x_update_cursor (f, 1);
2763 x_set_frame_alpha (f);
2764 }
2765
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. */
2771
2772 static void
2773 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2774 {
2775 struct frame *old_focus = dpyinfo->w32_focus_frame;
2776
2777 if (frame != dpyinfo->w32_focus_frame)
2778 {
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;
2782
2783 if (old_focus && old_focus->auto_lower)
2784 x_lower_frame (old_focus);
2785
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;
2790 }
2791
2792 x_frame_rehighlight (dpyinfo);
2793 }
2794
2795
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. */
2799
2800 static void
2801 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2802 struct frame *frame, struct input_event *bufp)
2803 {
2804 if (type == WM_SETFOCUS)
2805 {
2806 if (dpyinfo->w32_focus_event_frame != frame)
2807 {
2808 x_new_focus_frame (dpyinfo, frame);
2809 dpyinfo->w32_focus_event_frame = frame;
2810
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)))
2816 {
2817 bufp->arg = Qt;
2818 }
2819 else
2820 {
2821 bufp->arg = Qnil;
2822 }
2823
2824 bufp->kind = FOCUS_IN_EVENT;
2825 XSETFRAME (bufp->frame_or_window, frame);
2826 }
2827
2828 frame->output_data.x->focus_state |= state;
2829
2830 /* TODO: IME focus? */
2831 }
2832 else if (type == WM_KILLFOCUS)
2833 {
2834 frame->output_data.x->focus_state &= ~state;
2835
2836 if (dpyinfo->w32_focus_event_frame == frame)
2837 {
2838 dpyinfo->w32_focus_event_frame = 0;
2839 x_new_focus_frame (dpyinfo, 0);
2840
2841 bufp->kind = FOCUS_OUT_EVENT;
2842 XSETFRAME (bufp->frame_or_window, frame);
2843 }
2844
2845 /* TODO: IME focus? */
2846 }
2847 }
2848
2849
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.
2852
2853 Returns FOCUS_IN_EVENT event in *BUFP. */
2854
2855 static void
2856 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2857 struct input_event *bufp)
2858 {
2859 struct frame *frame;
2860
2861 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2862 if (! frame)
2863 return;
2864
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);
2870 }
2871
2872
2873 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2874
2875 void
2876 x_mouse_leave (struct w32_display_info *dpyinfo)
2877 {
2878 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2879 }
2880
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.
2884
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. */
2888
2889 static void
2890 w32_frame_rehighlight (struct frame *frame)
2891 {
2892 if (! FRAME_W32_P (frame))
2893 return;
2894 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
2895 }
2896
2897 static void
2898 x_frame_rehighlight (struct w32_display_info *dpyinfo)
2899 {
2900 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2901
2902 if (dpyinfo->w32_focus_frame)
2903 {
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))
2909 {
2910 fset_focus_frame (dpyinfo->w32_focus_frame, Qnil);
2911 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2912 }
2913 }
2914 else
2915 dpyinfo->x_highlight_frame = 0;
2916
2917 if (dpyinfo->x_highlight_frame != old_highlight)
2918 {
2919 if (old_highlight)
2920 frame_unhighlight (old_highlight);
2921 if (dpyinfo->x_highlight_frame)
2922 frame_highlight (dpyinfo->x_highlight_frame);
2923 }
2924 }
2925 \f
2926 /* Keyboard processing - modifier keys, etc. */
2927
2928 /* Convert a keysym to its name. */
2929
2930 char *
2931 x_get_keysym_name (int keysym)
2932 {
2933 /* Make static so we can always return it */
2934 static char value[100];
2935
2936 block_input ();
2937 GetKeyNameText (keysym, value, 100);
2938 unblock_input ();
2939
2940 return value;
2941 }
2942
2943 static int
2944 codepage_for_locale (LCID locale)
2945 {
2946 char cp[20];
2947
2948 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
2949 return atoi (cp);
2950 else
2951 return CP_ACP;
2952 }
2953
2954 \f
2955 /* Mouse clicks and mouse movement. Rah. */
2956
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)
2962 {
2963 int button = 0;
2964 int up = 0;
2965
2966 switch (message)
2967 {
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);
3014 }
3015
3016 if (pup) *pup = up;
3017 if (pbutton) *pbutton = button;
3018
3019 return (TRUE);
3020 }
3021
3022
3023 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3024
3025 If the event is a button press, then note that we have grabbed
3026 the mouse. */
3027
3028 static Lisp_Object
3029 construct_mouse_click (struct input_event *result, W32Msg *msg, struct frame *f)
3030 {
3031 int button;
3032 int up;
3033
3034 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3035 &button, &up);
3036
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));
3046
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;
3052 }
3053
3054 static Lisp_Object
3055 construct_mouse_wheel (struct input_event *result, W32Msg *msg, struct frame *f)
3056 {
3057 POINT p;
3058 int delta;
3059
3060 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3061 : WHEEL_EVENT;
3062 result->code = 0;
3063 result->timestamp = msg->msg.time;
3064
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);
3069
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));
3074
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;
3085 }
3086
3087 static Lisp_Object
3088 construct_drag_n_drop (struct input_event *result, W32Msg *msg, struct frame *f)
3089 {
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;
3104
3105 result->kind = DRAG_N_DROP_EVENT;
3106 result->code = 0;
3107 result->timestamp = msg->msg.time;
3108 result->modifiers = msg->dwModifiers;
3109
3110 hdrop = (HDROP) msg->msg.wParam;
3111 DragQueryPoint (hdrop, &p);
3112
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
3118
3119 XSETINT (result->x, p.x);
3120 XSETINT (result->y, p.y);
3121
3122 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3123 files = Qnil;
3124
3125 for (i = 0; i < num_files; i++)
3126 {
3127 if (use_unicode)
3128 {
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 */
3140 }
3141 #ifndef NTGUI_UNICODE
3142 else
3143 {
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);
3149 }
3150 #endif
3151 }
3152
3153 DragFinish (hdrop);
3154
3155 XSETFRAME (frame, f);
3156 result->frame_or_window = frame;
3157 result->arg = files;
3158 return Qnil;
3159 }
3160
3161 \f
3162 #if HAVE_W32NOTIFY
3163
3164 /* File event notifications (see w32notify.c). */
3165
3166 Lisp_Object
3167 lispy_file_action (DWORD action)
3168 {
3169 static char unknown_fmt[] = "unknown-action(%d)";
3170 Lisp_Object retval;
3171
3172 switch (action)
3173 {
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:
3190 {
3191 char buf[sizeof(unknown_fmt) - 1 + INT_STRLEN_BOUND (DWORD)];
3192
3193 sprintf (buf, unknown_fmt, action);
3194 retval = intern (buf);
3195 }
3196 break;
3197 }
3198
3199 return retval;
3200 }
3201
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)
3209 {
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;
3215
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)
3219 {
3220 notification_buffer_in_use = 0;
3221 return;
3222 }
3223
3224 XSETFRAME (frame, f);
3225
3226 enter_crit ();
3227 if (notification_buffer_in_use)
3228 {
3229 DWORD info_size = notifications_size;
3230 Lisp_Object cs = intern ("utf-16le");
3231 Lisp_Object obj = w32_get_watch_object (notifications_desc);
3232
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))
3239 {
3240 Lisp_Object callback = XCDR (obj);
3241
3242 while (info_size >= min_size)
3243 {
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);
3252
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)++;
3262
3263 if (!fni->NextEntryOffset)
3264 break;
3265 p += fni->NextEntryOffset;
3266 fni = (PFILE_NOTIFY_INFORMATION)p;
3267 info_size -= fni->NextEntryOffset;
3268 }
3269 }
3270 notification_buffer_in_use = 0;
3271 }
3272 else
3273 DebPrint (("We were promised notifications, but in-use flag is zero!\n"));
3274 leave_crit ();
3275
3276 /* We've stuffed all the events ourselves, so w32_read_socket shouldn't. */
3277 event->kind = NO_EVENT;
3278 }
3279 #endif /* WINDOWSNT */
3280 #endif /* HAVE_W32NOTIFY */
3281
3282 \f
3283 /* Function to report a mouse movement to the mainstream Emacs code.
3284 The input handler calls this.
3285
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. */
3290
3291 static int
3292 note_mouse_movement (struct frame *frame, MSG *msg)
3293 {
3294 struct w32_display_info *dpyinfo;
3295 int mouse_x = LOWORD (msg->lParam);
3296 int mouse_y = HIWORD (msg->lParam);
3297 RECT *r;
3298
3299 if (!FRAME_X_OUTPUT (frame))
3300 return 0;
3301
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;
3307
3308 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3309 {
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;
3315 }
3316
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)
3322 {
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;
3333 }
3334
3335 return 0;
3336 }
3337
3338 \f
3339 /************************************************************************
3340 Mouse Face
3341 ************************************************************************/
3342
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 *);
3353
3354 static void
3355 w32_define_cursor (Window window, Cursor cursor)
3356 {
3357 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3358 }
3359
3360 /* Return the current position of the mouse.
3361 *fp should be a frame which indicates which display to ask about.
3362
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.
3367
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.
3371
3372 Set *time to the server time-stamp for the time at which the mouse
3373 was at this position.
3374
3375 Don't store anything if we don't have a valid set of values to report.
3376
3377 This clears the mouse_moved flag, so we can wait for the next mouse
3378 movement. */
3379
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)
3384 {
3385 struct frame *f1;
3386 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3387
3388 block_input ();
3389
3390 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3391 {
3392 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3393
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);
3398 }
3399 else
3400 {
3401 POINT pt;
3402
3403 Lisp_Object frame, tail;
3404
3405 /* Clear the mouse-moved flag for every frame on this display. */
3406 FOR_EACH_FRAME (tail, frame)
3407 XFRAME (frame)->mouse_moved = 0;
3408
3409 dpyinfo->last_mouse_scroll_bar = NULL;
3410
3411 GetCursorPos (&pt);
3412
3413 /* Now we have a position on the root; find the innermost window
3414 containing the pointer. */
3415 {
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)));
3421
3422 /* If not, is it one of our scroll bars? */
3423 if (! f1)
3424 {
3425 struct scroll_bar *bar
3426 = x_window_to_scroll_bar (WindowFromPoint (pt), 2);
3427
3428 if (bar)
3429 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3430 }
3431
3432 if (f1 == 0 && insist > 0)
3433 f1 = SELECTED_FRAME ();
3434
3435 if (f1)
3436 {
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. */
3444
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;
3449
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;
3456 }
3457 }
3458 }
3459
3460 unblock_input ();
3461 }
3462
3463 \f
3464 /***********************************************************************
3465 Tool-bars
3466 ***********************************************************************/
3467
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. */
3471
3472 static void
3473 w32_handle_tool_bar_click (struct frame *f, struct input_event *button_event)
3474 {
3475 int x = XFASTINT (button_event->x);
3476 int y = XFASTINT (button_event->y);
3477
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);
3483 }
3484
3485
3486 \f
3487 /***********************************************************************
3488 Scroll bars
3489 ***********************************************************************/
3490
3491 /* Scroll bar support. */
3492
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. */
3496
3497 static struct scroll_bar *
3498 x_window_to_scroll_bar (Window window_id, int type)
3499 {
3500 Lisp_Object tail, frame;
3501
3502 FOR_EACH_FRAME (tail, frame)
3503 {
3504 Lisp_Object bar, condemned;
3505
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);
3521 }
3522
3523 return 0;
3524 }
3525
3526
3527 \f
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. */
3530
3531 static void
3532 w32_set_scroll_bar_thumb (struct scroll_bar *bar,
3533 int portion, int position, int whole)
3534 {
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;
3544
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).
3548
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". */
3552
3553 if (draggingp)
3554 {
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;
3564 }
3565
3566 if (whole)
3567 {
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)
3572 {
3573 sb_page = range * (whole - position) / whole;
3574 sb_pos = range;
3575 }
3576 else
3577 {
3578 sb_pos = position * range / whole;
3579 sb_page = (min (portion, (whole - position)) * range) / whole;
3580 }
3581 }
3582 else
3583 {
3584 sb_page = range;
3585 sb_pos = 0;
3586 }
3587
3588 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3589
3590 block_input ();
3591
3592 si.cbSize = sizeof (si);
3593 si.fMask = SIF_PAGE | SIF_POS;
3594 si.nPage = sb_page;
3595 si.nPos = sb_pos;
3596
3597 SetScrollInfo (w, SB_CTL, &si, TRUE);
3598
3599 unblock_input ();
3600 }
3601
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. */
3604
3605 static void
3606 w32_set_horizontal_scroll_bar_thumb (struct scroll_bar *bar,
3607 int portion, int position, int whole)
3608 {
3609 Window w = SCROLL_BAR_W32_WINDOW (bar);
3610 SCROLLINFO si;
3611
3612 block_input ();
3613
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);
3623
3624 unblock_input ();
3625 }
3626
3627 \f
3628 /************************************************************************
3629 Scroll bars, general
3630 ************************************************************************/
3631
3632 static HWND
3633 my_create_vscrollbar (struct frame * f, struct scroll_bar * bar)
3634 {
3635 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3636 WM_EMACS_CREATEVSCROLLBAR, (WPARAM) f,
3637 (LPARAM) bar);
3638 }
3639
3640 static HWND
3641 my_create_hscrollbar (struct frame * f, struct scroll_bar * bar)
3642 {
3643 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3644 WM_EMACS_CREATEHSCROLLBAR, (WPARAM) f,
3645 (LPARAM) bar);
3646 }
3647
3648 /*#define ATTACH_THREADS*/
3649
3650 static BOOL
3651 my_show_window (struct frame *f, HWND hwnd, int how)
3652 {
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
3659 }
3660
3661 static void
3662 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3663 int x, int y, int cx, int cy, UINT flags)
3664 {
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
3677 }
3678
3679 #if 0
3680 static void
3681 my_set_focus (struct frame * f, HWND hwnd)
3682 {
3683 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3684 (WPARAM) hwnd, 0);
3685 }
3686 #endif
3687
3688 static void
3689 my_set_foreground_window (HWND hwnd)
3690 {
3691 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3692 }
3693
3694
3695 static void
3696 my_destroy_window (struct frame * f, HWND hwnd)
3697 {
3698 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3699 (WPARAM) hwnd, 0);
3700 }
3701
3702 static void
3703 my_bring_window_to_top (HWND hwnd)
3704 {
3705 SendMessage (hwnd, WM_EMACS_BRINGTOTOP, (WPARAM) hwnd, 0);
3706 }
3707
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. */
3712
3713 static struct scroll_bar *
3714 x_scroll_bar_create (struct window *w, int left, int top, int width, int height, bool horizontal)
3715 {
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;
3722
3723 block_input ();
3724
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;
3734
3735 /* Requires geometry to be set before call to create the real window */
3736
3737 if (horizontal)
3738 hwnd = my_create_hscrollbar (f, bar);
3739 else
3740 hwnd = my_create_vscrollbar (f, bar);
3741
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;
3753
3754 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3755
3756 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3757
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);
3765
3766 unblock_input ();
3767
3768 return bar;
3769 }
3770
3771
3772 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3773 nil. */
3774
3775 static void
3776 x_scroll_bar_remove (struct scroll_bar *bar)
3777 {
3778 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3779
3780 block_input ();
3781
3782 /* Destroy the window. */
3783 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3784
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);
3790
3791 unblock_input ();
3792 }
3793
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)
3801 {
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;
3807
3808 /* Get window dimensions. */
3809 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
3810 top = window_y;
3811 height = window_height;
3812
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);
3816
3817 /* Does the scroll bar exist yet? */
3818 if (NILP (w->vertical_scroll_bar))
3819 {
3820 HDC hdc;
3821 block_input ();
3822 if (width > 0 && height > 0)
3823 {
3824 hdc = get_frame_dc (f);
3825 w32_clear_area (f, hdc, left, top, width, height);
3826 release_frame_dc (f, hdc);
3827 }
3828 unblock_input ();
3829
3830 bar = x_scroll_bar_create (w, left, top, width, height, 0);
3831 }
3832 else
3833 {
3834 /* It may just need to be moved and resized. */
3835 HWND hwnd;
3836
3837 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3838 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3839
3840 /* If already correctly positioned, do nothing. */
3841 if (bar->left == left
3842 && bar->top == top
3843 && bar->width == width
3844 && bar->height == height)
3845 {
3846 /* Redraw after clear_frame. */
3847 if (!my_show_window (f, hwnd, SW_NORMAL))
3848 InvalidateRect (hwnd, NULL, FALSE);
3849 }
3850 else
3851 {
3852 HDC hdc;
3853 SCROLLINFO si;
3854
3855 block_input ();
3856 if (width && height)
3857 {
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);
3863 }
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);
3868
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;
3874
3875 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3876
3877 my_show_window (f, hwnd, SW_NORMAL);
3878 /* InvalidateRect (w, NULL, FALSE); */
3879
3880 /* Remember new settings. */
3881 bar->left = left;
3882 bar->top = top;
3883 bar->width = width;
3884 bar->height = height;
3885
3886 unblock_input ();
3887 }
3888 }
3889 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3890 XSETVECTOR (barobj, bar);
3891 wset_vertical_scroll_bar (w, barobj);
3892 }
3893
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)
3901 {
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);
3909
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);
3916
3917 /* Does the scroll bar exist yet? */
3918 if (NILP (w->horizontal_scroll_bar))
3919 {
3920 HDC hdc;
3921 block_input ();
3922 if (width > 0 && height > 0)
3923 {
3924 hdc = get_frame_dc (f);
3925 w32_clear_area (f, hdc, clear_left, top, clear_width, height);
3926 release_frame_dc (f, hdc);
3927 }
3928 unblock_input ();
3929
3930 bar = x_scroll_bar_create (w, left, top, width, height, 1);
3931 }
3932 else
3933 {
3934 /* It may just need to be moved and resized. */
3935 HWND hwnd;
3936
3937 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
3938 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3939
3940 /* If already correctly positioned, do nothing. */
3941 if (bar->left == left && bar->top == top
3942 && bar->width == width && bar->height == height)
3943 {
3944 /* Redraw after clear_frame. */
3945 if (!my_show_window (f, hwnd, SW_NORMAL))
3946 InvalidateRect (hwnd, NULL, FALSE);
3947 }
3948 else
3949 {
3950 HDC hdc;
3951 SCROLLINFO si;
3952
3953 block_input ();
3954 if (width && height)
3955 {
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);
3961 }
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);
3966
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);
3975
3976 my_show_window (f, hwnd, SW_NORMAL);
3977 /* InvalidateRect (w, NULL, FALSE); */
3978
3979 /* Remember new settings. */
3980 bar->left = left;
3981 bar->top = top;
3982 bar->width = width;
3983 bar->height = height;
3984
3985 unblock_input ();
3986 }
3987 }
3988
3989 w32_set_horizontal_scroll_bar_thumb (bar, portion, position, whole);
3990 XSETVECTOR (barobj, bar);
3991 wset_horizontal_scroll_bar (w, barobj);
3992 }
3993
3994
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. */
4002
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. */
4006
4007 static void
4008 w32_condemn_scroll_bars (struct frame *frame)
4009 {
4010 if (!NILP (FRAME_SCROLL_BARS (frame)))
4011 {
4012 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
4013 {
4014 /* Prepend scrollbars to already condemned ones. */
4015 Lisp_Object last = FRAME_SCROLL_BARS (frame);
4016
4017 while (!NILP (XSCROLL_BAR (last)->next))
4018 last = XSCROLL_BAR (last)->next;
4019
4020 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
4021 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
4022 }
4023
4024 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
4025 fset_scroll_bars (frame, Qnil);
4026 }
4027 }
4028
4029
4030 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
4031 Note that WINDOW isn't necessarily condemned at all. */
4032
4033 static void
4034 w32_redeem_scroll_bar (struct window *w)
4035 {
4036 struct scroll_bar *bar;
4037 Lisp_Object barobj;
4038 struct frame *f;
4039
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 ();
4043
4044 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4045 {
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))
4050 {
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 ();
4063 }
4064 else
4065 XSCROLL_BAR (bar->prev)->next = bar->next;
4066
4067 if (! NILP (bar->next))
4068 XSCROLL_BAR (bar->next)->prev = bar->prev;
4069
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);
4076 }
4077
4078 horizontal:
4079 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
4080 {
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))
4085 {
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 ();
4098 }
4099 else
4100 XSCROLL_BAR (bar->prev)->next = bar->next;
4101
4102 if (! NILP (bar->next))
4103 XSCROLL_BAR (bar->next)->prev = bar->prev;
4104
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);
4111 }
4112 }
4113
4114 /* Remove all scroll bars on FRAME that haven't been saved since the
4115 last call to `*condemn_scroll_bars_hook'. */
4116
4117 static void
4118 w32_judge_scroll_bars (struct frame *f)
4119 {
4120 Lisp_Object bar, next;
4121
4122 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
4123
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);
4127
4128 for (; ! NILP (bar); bar = next)
4129 {
4130 struct scroll_bar *b = XSCROLL_BAR (bar);
4131
4132 x_scroll_bar_remove (b);
4133
4134 next = b->next;
4135 b->next = b->prev = Qnil;
4136 }
4137
4138 /* Now there should be no references to the condemned scroll bars,
4139 and they should get garbage-collected. */
4140 }
4141
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.
4145
4146 This may be called from a signal handler, so we have to ignore GC
4147 mark bits. */
4148
4149 static int
4150 w32_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4151 struct input_event *emacs_event)
4152 {
4153 if (! WINDOWP (bar->window))
4154 emacs_abort ();
4155
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;
4163
4164 {
4165 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4166 int y;
4167 int dragging = bar->dragging;
4168 SCROLLINFO si;
4169
4170 si.cbSize = sizeof (si);
4171 si.fMask = SIF_POS;
4172
4173 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4174 y = si.nPos;
4175
4176 bar->dragging = 0;
4177 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4178
4179 switch (LOWORD (msg->msg.wParam))
4180 {
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;
4207
4208 /* "Silently" update current position. */
4209 {
4210 SCROLLINFO si;
4211
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;
4218
4219 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4220 }
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)
4227 {
4228 SCROLLINFO si;
4229 int start = bar->start;
4230 int end = bar->end;
4231
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);
4237 }
4238 /* fall through */
4239 default:
4240 emacs_event->kind = NO_EVENT;
4241 return FALSE;
4242 }
4243
4244 XSETINT (emacs_event->x, y);
4245 XSETINT (emacs_event->y, top_range);
4246
4247 return TRUE;
4248 }
4249 }
4250
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.
4254
4255 This may be called from a signal handler, so we have to ignore GC
4256 mark bits. */
4257
4258 static int
4259 w32_horizontal_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
4260 struct input_event *emacs_event)
4261 {
4262 if (! WINDOWP (bar->window))
4263 emacs_abort ();
4264
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;
4272
4273 {
4274 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4275 int x, y;
4276 int dragging = bar->dragging;
4277 SCROLLINFO si;
4278
4279 si.cbSize = sizeof (si);
4280 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4281 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4282 x = si.nPos;
4283 y = si.nMax - si.nPos - si.nPage;
4284
4285 bar->dragging = 0;
4286 FRAME_DISPLAY_INFO (f)->last_mouse_scroll_bar_pos = msg->msg.wParam;
4287
4288 switch (LOWORD (msg->msg.wParam))
4289 {
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 {
4314 x = HIWORD (msg->msg.wParam);
4315 y = si.nMax - x - si.nPage;
4316 }
4317 bar->dragging = 1;
4318 emacs_event->part = scroll_bar_horizontal_handle;
4319
4320 /* "Silently" update current position. */
4321 {
4322 SCROLLINFO si;
4323
4324 si.cbSize = sizeof (si);
4325 si.fMask = SIF_POS;
4326 si.nPos = min (x, XWINDOW (bar->window)->hscroll_whole - 1);
4327 /* Remember apparent position (we actually lag behind the real
4328 position, so don't set that directly). */
4329 last_scroll_bar_drag_pos = x;
4330
4331 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4332 }
4333 break;
4334 case SB_ENDSCROLL:
4335 /* If this is the end of a drag sequence, then reset the scroll
4336 handle size to normal and do a final redraw. Otherwise do
4337 nothing. */
4338 if (dragging)
4339 {
4340 SCROLLINFO si;
4341 int start = bar->start;
4342 int end = bar->end;
4343
4344 si.cbSize = sizeof (si);
4345 /** si.fMask = SIF_PAGE | SIF_POS; **/
4346 si.fMask = SIF_POS;
4347 /** si.nPage = end - start + HORIZONTAL_SCROLL_BAR_MIN_HANDLE; **/
4348 si.nPos = min (last_scroll_bar_drag_pos,
4349 XWINDOW (bar->window)->hscroll_whole - 1);
4350 /** si.nPos = last_scroll_bar_drag_pos; **/
4351 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4352 }
4353 /* fall through */
4354 default:
4355 emacs_event->kind = NO_EVENT;
4356 return FALSE;
4357 }
4358
4359 XSETINT (emacs_event->x, x);
4360 XSETINT (emacs_event->y, y);
4361
4362 return TRUE;
4363 }
4364 }
4365
4366 /* Return information to the user about the current position of the mouse
4367 on the vertical scroll bar. */
4368 static void
4369 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4370 enum scroll_bar_part *part,
4371 Lisp_Object *x, Lisp_Object *y,
4372 unsigned long *time)
4373 {
4374 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4375 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4376 Window w = SCROLL_BAR_W32_WINDOW (bar);
4377 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4378 int pos;
4379 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4380 SCROLLINFO si;
4381
4382 block_input ();
4383
4384 *fp = f;
4385 *bar_window = bar->window;
4386
4387 si.cbSize = sizeof (si);
4388 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4389
4390 GetScrollInfo (w, SB_CTL, &si);
4391 pos = si.nPos;
4392 top_range = si.nMax - si.nPage + 1;
4393
4394 switch (LOWORD (dpyinfo->last_mouse_scroll_bar_pos))
4395 {
4396 case SB_THUMBPOSITION:
4397 case SB_THUMBTRACK:
4398 *part = scroll_bar_handle;
4399 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height) <= 0xffff)
4400 pos = HIWORD (dpyinfo->last_mouse_scroll_bar_pos);
4401 break;
4402 case SB_LINEDOWN:
4403 *part = scroll_bar_handle;
4404 pos++;
4405 break;
4406 default:
4407 *part = scroll_bar_handle;
4408 break;
4409 }
4410
4411 XSETINT (*x, pos);
4412 XSETINT (*y, top_range);
4413
4414 f->mouse_moved = 0;
4415 dpyinfo->last_mouse_scroll_bar = NULL;
4416
4417 *time = dpyinfo->last_mouse_movement_time;
4418
4419 unblock_input ();
4420 }
4421
4422 /* Return information to the user about the current position of the mouse
4423 on the horizontal scroll bar. */
4424 static void
4425 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
4426 enum scroll_bar_part *part,
4427 Lisp_Object *x, Lisp_Object *y,
4428 unsigned long *time)
4429 {
4430 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4431 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4432 Window w = SCROLL_BAR_W32_WINDOW (bar);
4433 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4434 int pos;
4435 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
4436 SCROLLINFO si;
4437
4438 block_input ();
4439
4440 *fp = f;
4441 *bar_window = bar->window;
4442
4443 si.cbSize = sizeof (si);
4444 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4445
4446 GetScrollInfo (w, SB_CTL, &si);
4447 pos = si.nPos;
4448 left_range = si.nMax - si.nPage + 1;
4449
4450 switch (LOWORD (dpyinfo->last_mouse_scroll_bar_pos))
4451 {
4452 case SB_THUMBPOSITION:
4453 case SB_THUMBTRACK:
4454 *part = scroll_bar_handle;
4455 if (HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width) <= 0xffff)
4456 pos = HIWORD (dpyinfo->last_mouse_scroll_bar_pos);
4457 break;
4458 case SB_LINERIGHT:
4459 *part = scroll_bar_handle;
4460 pos++;
4461 break;
4462 default:
4463 *part = scroll_bar_handle;
4464 break;
4465 }
4466
4467 XSETINT (*y, pos);
4468 XSETINT (*x, left_range);
4469
4470 f->mouse_moved = 0;
4471 dpyinfo->last_mouse_scroll_bar = NULL;
4472
4473 *time = dpyinfo->last_mouse_movement_time;
4474
4475 unblock_input ();
4476 }
4477
4478
4479 /* The screen has been cleared so we may have changed foreground or
4480 background colors, and the scroll bars may need to be redrawn.
4481 Clear out the scroll bars, and ask for expose events, so we can
4482 redraw them. */
4483
4484 void
4485 x_scroll_bar_clear (struct frame *f)
4486 {
4487 Lisp_Object bar;
4488
4489 /* We can have scroll bars even if this is 0,
4490 if we just turned off scroll bar mode.
4491 But in that case we should not clear them. */
4492 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4493 || FRAME_HAS_HORIZONTAL_SCROLL_BARS (f))
4494 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4495 bar = XSCROLL_BAR (bar)->next)
4496 {
4497 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4498 HDC hdc = GetDC (window);
4499 RECT rect;
4500
4501 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4502 arranges to refresh the scroll bar if hidden. */
4503 my_show_window (f, window, SW_HIDE);
4504
4505 GetClientRect (window, &rect);
4506 select_palette (f, hdc);
4507 w32_clear_rect (f, hdc, &rect);
4508 deselect_palette (f, hdc);
4509
4510 ReleaseDC (window, hdc);
4511 }
4512 }
4513
4514 static void
4515 set_frame_param (struct frame *f, Lisp_Object prop, Lisp_Object val)
4516 {
4517 register Lisp_Object old_alist_elt;
4518
4519 old_alist_elt = Fassq (prop, f->param_alist);
4520 if (EQ (old_alist_elt, Qnil))
4521 fset_param_alist (f, Fcons (Fcons (prop, val), f->param_alist));
4522 else
4523 Fsetcdr (old_alist_elt, val);
4524 }
4525
4526 /* The main W32 event-reading loop - w32_read_socket. */
4527
4528 /* Record the last 100 characters stored
4529 to help debug the loss-of-chars-during-GC problem. */
4530
4531 static int temp_index;
4532 static short temp_buffer[100];
4533
4534 /* Temporarily store lead byte of DBCS input sequences. */
4535 static char dbcs_lead = 0;
4536
4537 /* Read events coming from the W32 shell.
4538 This routine is called by the SIGIO handler.
4539 We return as soon as there are no more events to be read.
4540
4541 For an overview of how Emacs input works on MS-Windows, see the
4542 commentary before w32_msg_pump in w32fns.c.
4543
4544 We return the number of characters stored into the buffer,
4545 thus pretending to be `read'.
4546
4547 Some of these messages are reposted back to the message queue since the
4548 system calls the windows proc directly in a context where we cannot return
4549 the data nor can we guarantee the state we are in. So if we dispatch them
4550 we will get into an infinite loop. To prevent this from ever happening we
4551 will set a variable to indicate we are in the read_socket call and indicate
4552 which message we are processing since the windows proc gets called
4553 recursively with different messages by the system.
4554 */
4555
4556 static int
4557 w32_read_socket (struct terminal *terminal,
4558 struct input_event *hold_quit)
4559 {
4560 int count = 0;
4561 int check_visibility = 0;
4562 W32Msg msg;
4563 struct frame *f;
4564 struct w32_display_info *dpyinfo = &one_w32_display_info;
4565 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
4566
4567 block_input ();
4568
4569 /* So people can tell when we have read the available input. */
4570 input_signal_count++;
4571
4572 /* Process any incoming thread messages. */
4573 drain_message_queue ();
4574
4575 /* TODO: ghostscript integration. */
4576 while (get_next_msg (&msg, FALSE))
4577 {
4578 struct input_event inev;
4579 int do_help = 0;
4580
4581 /* DebPrint (("w32_read_socket: %s time:%u\n", */
4582 /* w32_name_of_message (msg.msg.message), */
4583 /* msg.msg.time)); */
4584
4585 EVENT_INIT (inev);
4586 inev.kind = NO_EVENT;
4587 inev.arg = Qnil;
4588
4589 switch (msg.msg.message)
4590 {
4591 case WM_EMACS_PAINT:
4592 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4593
4594 if (f)
4595 {
4596 if (msg.rect.right == msg.rect.left ||
4597 msg.rect.bottom == msg.rect.top)
4598 {
4599 /* We may get paint messages even though the client
4600 area is clipped - these are not expose events. */
4601 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4602 SDATA (f->name)));
4603 }
4604 else if (FRAME_VISIBLE_P (f) != 1)
4605 {
4606 bool iconified = FRAME_ICONIFIED_P (f);
4607
4608 /* Definitely not obscured, so mark as visible. */
4609 SET_FRAME_VISIBLE (f, 1);
4610 SET_FRAME_ICONIFIED (f, 0);
4611 SET_FRAME_GARBAGED (f);
4612 if (!f->output_data.w32->asked_for_visible)
4613 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4614 SDATA (f->name)));
4615
4616 /* WM_PAINT serves as MapNotify as well, so report
4617 visibility changes properly. */
4618 if (iconified)
4619 {
4620 inev.kind = DEICONIFY_EVENT;
4621 XSETFRAME (inev.frame_or_window, f);
4622 }
4623 else if (!NILP (Vframe_list) && !NILP (XCDR (Vframe_list)))
4624 /* Force a redisplay sooner or later to update the
4625 frame titles in case this is the second frame. */
4626 record_asynch_buffer_change ();
4627 }
4628 else
4629 {
4630 HDC hdc = get_frame_dc (f);
4631
4632 /* Erase background again for safety. */
4633 w32_clear_rect (f, hdc, &msg.rect);
4634 release_frame_dc (f, hdc);
4635 expose_frame (f,
4636 msg.rect.left,
4637 msg.rect.top,
4638 msg.rect.right - msg.rect.left,
4639 msg.rect.bottom - msg.rect.top);
4640 }
4641 }
4642 break;
4643
4644 case WM_INPUTLANGCHANGE:
4645 /* Generate a language change event. */
4646 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4647
4648 /* lParam contains the input language ID in its low 16 bits.
4649 Use it to update our record of the keyboard codepage. */
4650 w32_keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4651 & 0xffff));
4652
4653 if (f)
4654 {
4655 inev.kind = LANGUAGE_CHANGE_EVENT;
4656 XSETFRAME (inev.frame_or_window, f);
4657 inev.code = w32_keyboard_codepage;
4658 inev.modifiers = msg.msg.lParam & 0xffff;
4659 }
4660 break;
4661
4662 case WM_KEYDOWN:
4663 case WM_SYSKEYDOWN:
4664 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4665
4666 if (f && !FRAME_ICONIFIED_P (f))
4667 {
4668 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4669 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4670 {
4671 clear_mouse_face (hlinfo);
4672 hlinfo->mouse_face_hidden = 1;
4673 }
4674
4675 if (temp_index == sizeof temp_buffer / sizeof (short))
4676 temp_index = 0;
4677 temp_buffer[temp_index++] = msg.msg.wParam;
4678 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4679 inev.code = msg.msg.wParam;
4680 inev.modifiers = msg.dwModifiers;
4681 XSETFRAME (inev.frame_or_window, f);
4682 inev.timestamp = msg.msg.time;
4683 }
4684 break;
4685
4686 case WM_UNICHAR:
4687 case WM_SYSCHAR:
4688 case WM_CHAR:
4689 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4690
4691 if (f && !FRAME_ICONIFIED_P (f))
4692 {
4693 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4694 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4695 {
4696 clear_mouse_face (hlinfo);
4697 hlinfo->mouse_face_hidden = 1;
4698 }
4699
4700 if (temp_index == sizeof temp_buffer / sizeof (short))
4701 temp_index = 0;
4702 temp_buffer[temp_index++] = msg.msg.wParam;
4703
4704 inev.modifiers = msg.dwModifiers;
4705 XSETFRAME (inev.frame_or_window, f);
4706 inev.timestamp = msg.msg.time;
4707
4708 if (msg.msg.message == WM_UNICHAR)
4709 {
4710 inev.code = msg.msg.wParam;
4711 }
4712 else if (msg.msg.wParam < 256)
4713 {
4714 wchar_t code;
4715 char dbcs[2];
4716 dbcs[0] = 0;
4717 dbcs[1] = (char) msg.msg.wParam;
4718
4719 if (dbcs_lead)
4720 {
4721 dbcs[0] = dbcs_lead;
4722 dbcs_lead = 0;
4723 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4724 dbcs, 2, &code, 1))
4725 {
4726 /* Garbage */
4727 DebPrint (("Invalid DBCS sequence: %d %d\n",
4728 dbcs[0], dbcs[1]));
4729 inev.kind = NO_EVENT;
4730 break;
4731 }
4732 }
4733 else if (IsDBCSLeadByteEx (w32_keyboard_codepage,
4734 (BYTE) msg.msg.wParam))
4735 {
4736 dbcs_lead = (char) msg.msg.wParam;
4737 inev.kind = NO_EVENT;
4738 break;
4739 }
4740 else
4741 {
4742 if (!MultiByteToWideChar (w32_keyboard_codepage, 0,
4743 &dbcs[1], 1, &code, 1))
4744 {
4745 /* What to do with garbage? */
4746 DebPrint (("Invalid character: %d\n", dbcs[1]));
4747 inev.kind = NO_EVENT;
4748 break;
4749 }
4750 }
4751 inev.code = code;
4752 }
4753 else
4754 {
4755 /* Windows shouldn't generate WM_CHAR events above 0xFF
4756 in non-Unicode message handlers. */
4757 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4758 inev.kind = NO_EVENT;
4759 break;
4760 }
4761 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4762 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4763 }
4764 break;
4765
4766 case WM_APPCOMMAND:
4767 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4768
4769 if (f && !FRAME_ICONIFIED_P (f))
4770 {
4771 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4772 && !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
4773 {
4774 clear_mouse_face (hlinfo);
4775 hlinfo->mouse_face_hidden = 1;
4776 }
4777
4778 if (temp_index == sizeof temp_buffer / sizeof (short))
4779 temp_index = 0;
4780 temp_buffer[temp_index++] = msg.msg.wParam;
4781 inev.kind = MULTIMEDIA_KEY_EVENT;
4782 inev.code = GET_APPCOMMAND_LPARAM (msg.msg.lParam);
4783 inev.modifiers = msg.dwModifiers;
4784 XSETFRAME (inev.frame_or_window, f);
4785 inev.timestamp = msg.msg.time;
4786 }
4787 break;
4788
4789 case WM_MOUSEMOVE:
4790 /* Ignore non-movement. */
4791 {
4792 int x = LOWORD (msg.msg.lParam);
4793 int y = HIWORD (msg.msg.lParam);
4794 if (x == last_mousemove_x && y == last_mousemove_y)
4795 break;
4796 last_mousemove_x = x;
4797 last_mousemove_y = y;
4798 }
4799
4800 previous_help_echo_string = help_echo_string;
4801 help_echo_string = Qnil;
4802
4803 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4804 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4805
4806 if (hlinfo->mouse_face_hidden)
4807 {
4808 hlinfo->mouse_face_hidden = 0;
4809 clear_mouse_face (hlinfo);
4810 }
4811
4812 if (f)
4813 {
4814 /* Generate SELECT_WINDOW_EVENTs when needed. */
4815 if (!NILP (Vmouse_autoselect_window))
4816 {
4817 static Lisp_Object last_mouse_window;
4818 Lisp_Object window = window_from_coordinates
4819 (f, LOWORD (msg.msg.lParam), HIWORD (msg.msg.lParam), 0, 0);
4820
4821 /* Window will be selected only when it is not
4822 selected now and last mouse movement event was
4823 not in it. Minibuffer window will be selected
4824 only when it is active. */
4825 if (WINDOWP (window)
4826 && !EQ (window, last_mouse_window)
4827 && !EQ (window, selected_window)
4828 /* For click-to-focus window managers
4829 create event iff we don't leave the
4830 selected frame. */
4831 && (focus_follows_mouse
4832 || (EQ (XWINDOW (window)->frame,
4833 XWINDOW (selected_window)->frame))))
4834 {
4835 inev.kind = SELECT_WINDOW_EVENT;
4836 inev.frame_or_window = window;
4837 }
4838 /* Remember the last window where we saw the mouse. */
4839 last_mouse_window = window;
4840 }
4841 if (!note_mouse_movement (f, &msg.msg))
4842 help_echo_string = previous_help_echo_string;
4843 }
4844 else
4845 {
4846 /* If we move outside the frame, then we're
4847 certainly no longer on any text in the frame. */
4848 clear_mouse_face (hlinfo);
4849 }
4850
4851 /* If the contents of the global variable help_echo_string
4852 has changed, generate a HELP_EVENT. */
4853 #if 0 /* The below is an invalid comparison when CHECK_LISP_OBJECT_TYPE.
4854 But it was originally changed to this to fix a bug, so I have
4855 not removed it completely in case the bug is still there. */
4856 if (help_echo_string != previous_help_echo_string ||
4857 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4858 #else /* This is what xterm.c does. */
4859 if (!NILP (help_echo_string)
4860 || !NILP (previous_help_echo_string))
4861 do_help = 1;
4862 #endif
4863 break;
4864
4865 case WM_LBUTTONDOWN:
4866 case WM_LBUTTONUP:
4867 case WM_MBUTTONDOWN:
4868 case WM_MBUTTONUP:
4869 case WM_RBUTTONDOWN:
4870 case WM_RBUTTONUP:
4871 case WM_XBUTTONDOWN:
4872 case WM_XBUTTONUP:
4873 {
4874 /* If we decide we want to generate an event to be seen
4875 by the rest of Emacs, we put it here. */
4876 bool tool_bar_p = 0;
4877 int button;
4878 int up;
4879
4880 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4881 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4882
4883 if (f)
4884 {
4885 construct_mouse_click (&inev, &msg, f);
4886
4887 /* Is this in the tool-bar? */
4888 if (WINDOWP (f->tool_bar_window)
4889 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4890 {
4891 Lisp_Object window;
4892 int x = XFASTINT (inev.x);
4893 int y = XFASTINT (inev.y);
4894
4895 window = window_from_coordinates (f, x, y, 0, 1);
4896
4897 if (EQ (window, f->tool_bar_window))
4898 {
4899 w32_handle_tool_bar_click (f, &inev);
4900 tool_bar_p = 1;
4901 }
4902 }
4903
4904 if (tool_bar_p
4905 || (dpyinfo->w32_focus_frame
4906 && f != dpyinfo->w32_focus_frame))
4907 inev.kind = NO_EVENT;
4908 }
4909
4910 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4911 &button, &up);
4912
4913 if (up)
4914 {
4915 dpyinfo->grabbed &= ~ (1 << button);
4916 }
4917 else
4918 {
4919 dpyinfo->grabbed |= (1 << button);
4920 dpyinfo->last_mouse_frame = f;
4921 /* Ignore any mouse motion that happened
4922 before this event; any subsequent mouse-movement
4923 Emacs events should reflect only motion after
4924 the ButtonPress. */
4925 if (f != 0)
4926 {
4927 f->mouse_moved = 0;
4928 if (!tool_bar_p)
4929 f->last_tool_bar_item = -1;
4930 }
4931 }
4932 break;
4933 }
4934
4935 case WM_MOUSEWHEEL:
4936 case WM_MOUSEHWHEEL:
4937 {
4938 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
4939 : x_window_to_frame (dpyinfo, msg.msg.hwnd));
4940
4941 if (f)
4942 {
4943
4944 if (!dpyinfo->w32_focus_frame
4945 || f == dpyinfo->w32_focus_frame)
4946 {
4947 /* Emit an Emacs wheel-up/down event. */
4948 construct_mouse_wheel (&inev, &msg, f);
4949 }
4950 /* Ignore any mouse motion that happened before this
4951 event; any subsequent mouse-movement Emacs events
4952 should reflect only motion after the
4953 ButtonPress. */
4954 f->mouse_moved = 0;
4955 f->last_tool_bar_item = -1;
4956 }
4957 dpyinfo->last_mouse_frame = f;
4958 }
4959 break;
4960
4961 case WM_DROPFILES:
4962 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4963
4964 if (f)
4965 construct_drag_n_drop (&inev, &msg, f);
4966 break;
4967
4968 case WM_HSCROLL:
4969 {
4970 struct scroll_bar *bar =
4971 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 1);
4972
4973 if (bar)
4974 w32_horizontal_scroll_bar_handle_click (bar, &msg, &inev);
4975 break;
4976 }
4977
4978 case WM_VSCROLL:
4979 {
4980 struct scroll_bar *bar =
4981 x_window_to_scroll_bar ((HWND)msg.msg.lParam, 0);
4982
4983 if (bar)
4984 w32_scroll_bar_handle_click (bar, &msg, &inev);
4985 break;
4986 }
4987
4988 case WM_WINDOWPOSCHANGED:
4989 case WM_ACTIVATE:
4990 case WM_ACTIVATEAPP:
4991 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4992 if (f)
4993 {
4994 /* Run the full-screen hook function also when we are
4995 being activated, to actually install the required
4996 size in effect, if the WAIT flag is set. This is
4997 because when the hook is run from x_set_fullscreen,
4998 the frame might not yet be visible, if that call is a
4999 result of make-frame, and in that case the hook just
5000 sets the WAIT flag. */
5001 if ((msg.msg.message == WM_WINDOWPOSCHANGED || msg.msg.wParam)
5002 && (f->want_fullscreen & FULLSCREEN_WAIT))
5003 w32fullscreen_hook (f);
5004 x_check_fullscreen (f);
5005 }
5006 check_visibility = 1;
5007 break;
5008
5009 case WM_MOVE:
5010 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5011
5012 if (f && !FRAME_ICONIFIED_P (f))
5013 x_real_positions (f, &f->left_pos, &f->top_pos);
5014
5015 check_visibility = 1;
5016 break;
5017
5018 case WM_SHOWWINDOW:
5019 /* wParam non-zero means Window is about to be shown, 0 means
5020 about to be hidden. */
5021 /* Redo the mouse-highlight after the tooltip has gone. */
5022 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
5023 {
5024 tip_window = NULL;
5025 x_redo_mouse_highlight (dpyinfo);
5026 }
5027
5028 /* If window has been obscured or exposed by another window
5029 being maximized or minimized/restored, then recheck
5030 visibility of all frames. Direct changes to our own
5031 windows get handled by WM_SIZE. */
5032 #if 0
5033 if (msg.msg.lParam != 0)
5034 check_visibility = 1;
5035 else
5036 {
5037 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5038 f->async_visible = msg.msg.wParam;
5039 }
5040 #endif
5041
5042 check_visibility = 1;
5043 break;
5044
5045 case WM_SIZE:
5046 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5047
5048 /* Inform lisp of whether frame has been iconified etc. */
5049 if (f)
5050 {
5051 switch (msg.msg.wParam)
5052 {
5053 case SIZE_MINIMIZED:
5054 SET_FRAME_VISIBLE (f, 0);
5055 SET_FRAME_ICONIFIED (f, 1);
5056
5057 inev.kind = ICONIFY_EVENT;
5058 XSETFRAME (inev.frame_or_window, f);
5059 break;
5060
5061 case SIZE_MAXIMIZED:
5062 {
5063 bool iconified = FRAME_ICONIFIED_P (f);
5064
5065 SET_FRAME_VISIBLE (f, 1);
5066 SET_FRAME_ICONIFIED (f, 0);
5067
5068 /* wait_reading_process_output will notice this
5069 and update the frame's display structures. */
5070 SET_FRAME_GARBAGED (f);
5071
5072 if (iconified)
5073 {
5074 int x, y;
5075
5076 /* Reset top and left positions of the Window
5077 here since Windows sends a WM_MOVE message
5078 BEFORE telling us the Window is minimized
5079 when the Window is iconified, with 3000,3000
5080 as the co-ords. */
5081 x_real_positions (f, &x, &y);
5082 f->left_pos = x;
5083 f->top_pos = y;
5084
5085 inev.kind = DEICONIFY_EVENT;
5086 XSETFRAME (inev.frame_or_window, f);
5087 }
5088 else if (! NILP (Vframe_list)
5089 && ! NILP (XCDR (Vframe_list)))
5090 /* Force a redisplay sooner or later
5091 to update the frame titles
5092 in case this is the second frame. */
5093 record_asynch_buffer_change ();
5094 }
5095
5096 if (EQ (get_frame_param (f, Qfullscreen), Qnil))
5097 set_frame_param (f, Qfullscreen, Qmaximized);
5098 else if (! EQ (get_frame_param (f, Qfullscreen), Qmaximized))
5099 set_frame_param (f, Qmaximized, Qmaximized);
5100
5101 break;
5102
5103 case SIZE_RESTORED:
5104 {
5105 bool iconified = FRAME_ICONIFIED_P (f);
5106
5107 /* The following was made unconditional in a
5108 pathetic attempt to fix bug#16967 in revision
5109 116716 but, considered counterproductive was made
5110 conditional again in revision 116727. martin */
5111 if (iconified)
5112 SET_FRAME_VISIBLE (f, 1);
5113 SET_FRAME_ICONIFIED (f, 0);
5114
5115 /* wait_reading_process_output will notice this
5116 and update the frame's display structures. */
5117 SET_FRAME_GARBAGED (f);
5118
5119 if (iconified)
5120 {
5121 /* Reset top and left positions of the Window
5122 here since Windows sends a WM_MOVE message
5123 BEFORE telling us the Window is minimized
5124 when the Window is iconified, with 3000,3000
5125 as the co-ords. */
5126 x_real_positions (f, &f->left_pos, &f->top_pos);
5127
5128 inev.kind = DEICONIFY_EVENT;
5129 XSETFRAME (inev.frame_or_window, f);
5130 }
5131 else if (! NILP (Vframe_list)
5132 && ! NILP (XCDR (Vframe_list)))
5133 /* Force a redisplay sooner or later
5134 to update the frame titles
5135 in case this is the second frame. */
5136 record_asynch_buffer_change ();
5137 }
5138
5139 if (EQ (get_frame_param (f, Qfullscreen), Qmaximized))
5140 set_frame_param (f, Qfullscreen, Qnil);
5141 else if (! EQ (get_frame_param (f, Qmaximized), Qnil))
5142 set_frame_param (f, Qmaximized, Qnil);
5143
5144 break;
5145 }
5146 }
5147
5148 if (f && !FRAME_ICONIFIED_P (f) && msg.msg.wParam != SIZE_MINIMIZED)
5149 {
5150 RECT rect;
5151 int rows, columns, width, height, text_width, text_height;
5152
5153 GetClientRect (msg.msg.hwnd, &rect);
5154
5155 height = rect.bottom - rect.top;
5156 width = rect.right - rect.left;
5157 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, width);
5158 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, height);
5159 /* rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); */
5160 /* columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); */
5161
5162 /* TODO: Clip size to the screen dimensions. */
5163
5164 /* Even if the number of character rows and columns has
5165 not changed, the font size may have changed, so we need
5166 to check the pixel dimensions as well. */
5167
5168 if (width != FRAME_PIXEL_WIDTH (f)
5169 || height != FRAME_PIXEL_HEIGHT (f)
5170 || text_width != FRAME_TEXT_WIDTH (f)
5171 || text_height != FRAME_TEXT_HEIGHT (f))
5172 {
5173 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
5174 SET_FRAME_GARBAGED (f);
5175 cancel_mouse_face (f);
5176 f->win_gravity = NorthWestGravity;
5177 }
5178 }
5179
5180 check_visibility = 1;
5181 break;
5182
5183 case WM_MOUSELEAVE:
5184 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
5185 if (f)
5186 {
5187 if (f == hlinfo->mouse_face_mouse_frame)
5188 {
5189 /* If we move outside the frame, then we're
5190 certainly no longer on any text in the frame. */
5191 clear_mouse_face (hlinfo);
5192 hlinfo->mouse_face_mouse_frame = 0;
5193 }
5194
5195 /* Generate a nil HELP_EVENT to cancel a help-echo.
5196 Do it only if there's something to cancel.
5197 Otherwise, the startup message is cleared when
5198 the mouse leaves the frame. */
5199 if (any_help_event_p)
5200 do_help = -1;
5201 }
5202 break;
5203
5204 case WM_SETFOCUS:
5205 w32_detect_focus_change (dpyinfo, &msg, &inev);
5206
5207 dpyinfo->grabbed = 0;
5208 check_visibility = 1;
5209 break;
5210
5211 case WM_KILLFOCUS:
5212 w32_detect_focus_change (dpyinfo, &msg, &inev);
5213 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
5214
5215 if (f)
5216 {
5217 if (f == hlinfo->mouse_face_mouse_frame)
5218 {
5219 /* If we move outside the frame, then we're
5220 certainly no longer on any text in the frame. */
5221 clear_mouse_face (hlinfo);
5222 hlinfo->mouse_face_mouse_frame = 0;
5223 }
5224
5225 /* Generate a nil HELP_EVENT to cancel a help-echo.
5226 Do it only if there's something to cancel.
5227 Otherwise, the startup message is cleared when
5228 the mouse leaves the frame. */
5229 if (any_help_event_p)
5230 do_help = -1;
5231 }
5232
5233 dpyinfo->grabbed = 0;
5234 check_visibility = 1;
5235 break;
5236
5237 case WM_CLOSE:
5238 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5239
5240 if (f)
5241 {
5242 inev.kind = DELETE_WINDOW_EVENT;
5243 XSETFRAME (inev.frame_or_window, f);
5244 }
5245 break;
5246
5247 case WM_INITMENU:
5248 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5249
5250 if (f)
5251 {
5252 inev.kind = MENU_BAR_ACTIVATE_EVENT;
5253 XSETFRAME (inev.frame_or_window, f);
5254 }
5255 break;
5256
5257 case WM_COMMAND:
5258 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5259
5260 if (f)
5261 {
5262 extern void menubar_selection_callback
5263 (struct frame *f, void * client_data);
5264 menubar_selection_callback (f, (void *)msg.msg.wParam);
5265 }
5266
5267 check_visibility = 1;
5268 break;
5269
5270 case WM_DISPLAYCHANGE:
5271 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5272
5273 if (f)
5274 {
5275 dpyinfo->n_cbits = msg.msg.wParam;
5276 /* The new display could have a different resolution, in
5277 which case we must reconsider what fullscreen
5278 means. */
5279 x_check_fullscreen (f);
5280 DebPrint (("display change: %d %d\n",
5281 (short) LOWORD (msg.msg.lParam),
5282 (short) HIWORD (msg.msg.lParam)));
5283 }
5284
5285 check_visibility = 1;
5286 break;
5287
5288 #if HAVE_W32NOTIFY
5289 case WM_EMACS_FILENOTIFY:
5290 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
5291 if (f)
5292 queue_notifications (&inev, &msg, f, &count);
5293 break;
5294 #endif
5295
5296 default:
5297 /* Check for messages registered at runtime. */
5298 if (msg.msg.message == msh_mousewheel)
5299 {
5300 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
5301 msg.msg.message = WM_MOUSEWHEEL;
5302 prepend_msg (&msg);
5303 }
5304 break;
5305 }
5306
5307 if (inev.kind != NO_EVENT)
5308 {
5309 kbd_buffer_store_event_hold (&inev, hold_quit);
5310 count++;
5311 }
5312
5313 if (do_help
5314 && !(hold_quit && hold_quit->kind != NO_EVENT))
5315 {
5316 Lisp_Object frame;
5317
5318 if (f)
5319 XSETFRAME (frame, f);
5320 else
5321 frame = Qnil;
5322
5323 if (do_help > 0)
5324 {
5325 if (NILP (help_echo_string))
5326 {
5327 help_echo_object = help_echo_window = Qnil;
5328 help_echo_pos = -1;
5329 }
5330
5331 any_help_event_p = 1;
5332 gen_help_event (help_echo_string, frame, help_echo_window,
5333 help_echo_object, help_echo_pos);
5334 }
5335 else
5336 {
5337 help_echo_string = Qnil;
5338 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
5339 }
5340 count++;
5341 }
5342 }
5343
5344 /* If the focus was just given to an autoraising frame,
5345 raise it now. FIXME: handle more than one such frame. */
5346 if (dpyinfo->w32_pending_autoraise_frame)
5347 {
5348 x_raise_frame (dpyinfo->w32_pending_autoraise_frame);
5349 dpyinfo->w32_pending_autoraise_frame = NULL;
5350 }
5351
5352 /* Check which frames are still visible, if we have enqueued any user
5353 events or been notified of events that may affect visibility. We
5354 do this here because there doesn't seem to be any direct
5355 notification from Windows that the visibility of a window has
5356 changed (at least, not in all cases). */
5357 if (count > 0 || check_visibility)
5358 {
5359 Lisp_Object tail, frame;
5360
5361 FOR_EACH_FRAME (tail, frame)
5362 {
5363 struct frame *f = XFRAME (frame);
5364 /* The tooltip has been drawn already. Avoid the
5365 SET_FRAME_GARBAGED below. */
5366 if (EQ (frame, tip_frame))
5367 continue;
5368
5369 /* Check "visible" frames and mark each as obscured or not.
5370 Note that visible is nonzero for unobscured and obscured
5371 frames, but zero for hidden and iconified frames. */
5372 if (FRAME_W32_P (f) && FRAME_VISIBLE_P (f))
5373 {
5374 RECT clipbox;
5375 HDC hdc;
5376 bool obscured;
5377
5378 enter_crit ();
5379 /* Query clipping rectangle for the entire window area
5380 (GetWindowDC), not just the client portion (GetDC).
5381 Otherwise, the scrollbars and menubar aren't counted as
5382 part of the visible area of the frame, and we may think
5383 the frame is obscured when really a scrollbar is still
5384 visible and gets WM_PAINT messages above. */
5385 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
5386 GetClipBox (hdc, &clipbox);
5387 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
5388 leave_crit ();
5389
5390 obscured = FRAME_OBSCURED_P (f);
5391
5392 if (clipbox.right == clipbox.left || clipbox.bottom == clipbox.top)
5393 {
5394 /* Frame has become completely obscured so mark as such (we
5395 do this by setting visible to 2 so that FRAME_VISIBLE_P
5396 is still true, but redisplay will skip it). */
5397 SET_FRAME_VISIBLE (f, 2);
5398
5399 if (!obscured)
5400 DebPrint (("frame %p (%s) obscured\n", f, SDATA (f->name)));
5401 }
5402 else
5403 {
5404 /* Frame is not obscured, so mark it as such. */
5405 SET_FRAME_VISIBLE (f, 1);
5406
5407 if (obscured)
5408 {
5409 SET_FRAME_GARBAGED (f);
5410 DebPrint (("obscured frame %p (%s) found to be visible\n",
5411 f, SDATA (f->name)));
5412
5413 /* Force a redisplay sooner or later. */
5414 record_asynch_buffer_change ();
5415 }
5416 }
5417 }
5418 }
5419 }
5420
5421 unblock_input ();
5422 return count;
5423 }
5424
5425
5426 \f
5427 /***********************************************************************
5428 Text Cursor
5429 ***********************************************************************/
5430
5431 /* Set clipping for output in glyph row ROW. W is the window in which
5432 we operate. GC is the graphics context to set clipping in.
5433
5434 ROW may be a text row or, e.g., a mode line. Text rows must be
5435 clipped to the interior of the window dedicated to text display,
5436 mode lines must be clipped to the whole window. */
5437
5438 static void
5439 w32_clip_to_row (struct window *w, struct glyph_row *row,
5440 enum glyph_row_area area, HDC hdc)
5441 {
5442 RECT clip_rect;
5443 int window_x, window_y, window_width;
5444
5445 window_box (w, area, &window_x, &window_y, &window_width, 0);
5446
5447 clip_rect.left = window_x;
5448 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
5449 clip_rect.top = max (clip_rect.top, window_y);
5450 clip_rect.right = clip_rect.left + window_width;
5451 clip_rect.bottom = clip_rect.top + row->visible_height;
5452
5453 w32_set_clip_rectangle (hdc, &clip_rect);
5454 }
5455
5456
5457 /* Draw a hollow box cursor on window W in glyph row ROW. */
5458
5459 static void
5460 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
5461 {
5462 struct frame *f = XFRAME (WINDOW_FRAME (w));
5463 HDC hdc;
5464 RECT rect;
5465 int left, top, h;
5466 struct glyph *cursor_glyph;
5467 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5468
5469 /* Get the glyph the cursor is on. If we can't tell because
5470 the current matrix is invalid or such, give up. */
5471 cursor_glyph = get_phys_cursor_glyph (w);
5472 if (cursor_glyph == NULL)
5473 {
5474 DeleteObject (hb);
5475 return;
5476 }
5477
5478 /* Compute frame-relative coordinates for phys cursor. */
5479 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
5480 rect.left = left;
5481 rect.top = top;
5482 rect.bottom = rect.top + h;
5483 rect.right = rect.left + w->phys_cursor_width;
5484
5485 hdc = get_frame_dc (f);
5486 /* Set clipping, draw the rectangle, and reset clipping again. */
5487 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5488 FrameRect (hdc, &rect, hb);
5489 DeleteObject (hb);
5490 w32_set_clip_rectangle (hdc, NULL);
5491 release_frame_dc (f, hdc);
5492 }
5493
5494
5495 /* Draw a bar cursor on window W in glyph row ROW.
5496
5497 Implementation note: One would like to draw a bar cursor with an
5498 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5499 Unfortunately, I didn't find a font yet that has this property set.
5500 --gerd. */
5501
5502 static void
5503 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
5504 int width, enum text_cursor_kinds kind)
5505 {
5506 struct frame *f = XFRAME (w->frame);
5507 struct glyph *cursor_glyph;
5508
5509 /* If cursor is out of bounds, don't draw garbage. This can happen
5510 in mini-buffer windows when switching between echo area glyphs
5511 and mini-buffer. */
5512 cursor_glyph = get_phys_cursor_glyph (w);
5513 if (cursor_glyph == NULL)
5514 return;
5515
5516 /* If on an image, draw like a normal cursor. That's usually better
5517 visible than drawing a bar, esp. if the image is large so that
5518 the bar might not be in the window. */
5519 if (cursor_glyph->type == IMAGE_GLYPH)
5520 {
5521 struct glyph_row *row;
5522 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5523 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5524 }
5525 else
5526 {
5527 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5528 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5529 int x;
5530 HDC hdc;
5531
5532 /* If the glyph's background equals the color we normally draw
5533 the bar cursor in, the bar cursor in its normal color is
5534 invisible. Use the glyph's foreground color instead in this
5535 case, on the assumption that the glyph's colors are chosen so
5536 that the glyph is legible. */
5537 if (face->background == cursor_color)
5538 cursor_color = face->foreground;
5539
5540 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5541
5542 hdc = get_frame_dc (f);
5543 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5544
5545 if (kind == BAR_CURSOR)
5546 {
5547 if (width < 0)
5548 width = FRAME_CURSOR_WIDTH (f);
5549 width = min (cursor_glyph->pixel_width, width);
5550
5551 w->phys_cursor_width = width;
5552
5553 /* If the character under cursor is R2L, draw the bar cursor
5554 on the right of its glyph, rather than on the left. */
5555 if ((cursor_glyph->resolved_level & 1) != 0)
5556 x += cursor_glyph->pixel_width - width;
5557
5558 w32_fill_area (f, hdc, cursor_color, x,
5559 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5560 width, row->height);
5561 }
5562 else
5563 {
5564 int dummy_x, dummy_y, dummy_h;
5565
5566 if (width < 0)
5567 width = row->height;
5568
5569 width = min (row->height, width);
5570
5571 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5572 &dummy_y, &dummy_h);
5573 w32_fill_area (f, hdc, cursor_color, x,
5574 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5575 row->height - width),
5576 w->phys_cursor_width, width);
5577 }
5578
5579 w32_set_clip_rectangle (hdc, NULL);
5580 release_frame_dc (f, hdc);
5581 }
5582 }
5583
5584
5585 /* RIF: Define cursor CURSOR on frame F. */
5586
5587 static void
5588 w32_define_frame_cursor (struct frame *f, Cursor cursor)
5589 {
5590 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5591 }
5592
5593
5594 /* RIF: Clear area on frame F. */
5595
5596 static void
5597 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
5598 {
5599 HDC hdc;
5600
5601 hdc = get_frame_dc (f);
5602 w32_clear_area (f, hdc, x, y, width, height);
5603 release_frame_dc (f, hdc);
5604 }
5605
5606 /* RIF: Draw or clear cursor on window W. */
5607
5608 static void
5609 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
5610 int x, int y, enum text_cursor_kinds cursor_type,
5611 int cursor_width, bool on_p, bool active_p)
5612 {
5613 if (on_p)
5614 {
5615 /* If the user wants to use the system caret, make sure our own
5616 cursor remains invisible. */
5617 if (w32_use_visible_system_caret)
5618 {
5619 /* Call to erase_phys_cursor here seems to use the
5620 wrong values of w->phys_cursor, as they have been
5621 overwritten before this function was called. */
5622 if (w->phys_cursor_type != NO_CURSOR)
5623 erase_phys_cursor (w);
5624
5625 cursor_type = w->phys_cursor_type = NO_CURSOR;
5626 w->phys_cursor_width = -1;
5627 }
5628 else
5629 {
5630 w->phys_cursor_type = cursor_type;
5631 }
5632
5633 w->phys_cursor_on_p = 1;
5634
5635 /* If this is the active cursor, we need to track it with the
5636 system caret, so third party software like screen magnifiers
5637 and speech synthesizers can follow the cursor. */
5638 if (active_p)
5639 {
5640 struct frame *f = XFRAME (WINDOW_FRAME (w));
5641 HWND hwnd = FRAME_W32_WINDOW (f);
5642
5643 w32_system_caret_x
5644 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5645 w32_system_caret_y
5646 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5647 + glyph_row->ascent - w->phys_cursor_ascent);
5648 w32_system_caret_window = w;
5649 w32_system_caret_hdr_height = WINDOW_HEADER_LINE_HEIGHT (w);
5650 w32_system_caret_mode_height = WINDOW_MODE_LINE_HEIGHT (w);
5651
5652 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
5653
5654 /* If the size of the active cursor changed, destroy the old
5655 system caret. */
5656 if (w32_system_caret_hwnd
5657 && (w32_system_caret_height != w->phys_cursor_height))
5658 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5659
5660 w32_system_caret_height = w->phys_cursor_height;
5661
5662 /* Move the system caret. */
5663 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5664 }
5665
5666 if (glyph_row->exact_window_width_line_p
5667 && (glyph_row->reversed_p
5668 ? (w->phys_cursor.hpos < 0)
5669 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5670 {
5671 glyph_row->cursor_in_fringe_p = 1;
5672 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5673 return;
5674 }
5675
5676 switch (cursor_type)
5677 {
5678 case HOLLOW_BOX_CURSOR:
5679 x_draw_hollow_cursor (w, glyph_row);
5680 break;
5681
5682 case FILLED_BOX_CURSOR:
5683 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5684 break;
5685
5686 case BAR_CURSOR:
5687 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5688 break;
5689
5690 case HBAR_CURSOR:
5691 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5692 break;
5693
5694 case NO_CURSOR:
5695 w->phys_cursor_width = 0;
5696 break;
5697
5698 default:
5699 emacs_abort ();
5700 }
5701 }
5702 }
5703
5704
5705 \f
5706 /* Icons. */
5707
5708 int
5709 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5710 {
5711 HANDLE main_icon;
5712 HANDLE small_icon = NULL;
5713
5714 if (FRAME_W32_WINDOW (f) == 0)
5715 return 1;
5716
5717 if (NILP (icon))
5718 main_icon = LoadIcon (hinst, EMACS_CLASS);
5719 else if (STRINGP (icon))
5720 {
5721 /* Load the main icon from the named file. */
5722 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5723 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5724 /* Try to load a small icon to go with it. */
5725 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5726 GetSystemMetrics (SM_CXSMICON),
5727 GetSystemMetrics (SM_CYSMICON),
5728 LR_LOADFROMFILE);
5729 }
5730 else if (SYMBOLP (icon))
5731 {
5732 LPCTSTR name;
5733
5734 if (EQ (icon, intern ("application")))
5735 name = (LPCTSTR) IDI_APPLICATION;
5736 else if (EQ (icon, intern ("hand")))
5737 name = (LPCTSTR) IDI_HAND;
5738 else if (EQ (icon, intern ("question")))
5739 name = (LPCTSTR) IDI_QUESTION;
5740 else if (EQ (icon, intern ("exclamation")))
5741 name = (LPCTSTR) IDI_EXCLAMATION;
5742 else if (EQ (icon, intern ("asterisk")))
5743 name = (LPCTSTR) IDI_ASTERISK;
5744 else if (EQ (icon, intern ("winlogo")))
5745 name = (LPCTSTR) IDI_WINLOGO;
5746 else
5747 return 1;
5748
5749 main_icon = LoadIcon (NULL, name);
5750 }
5751 else
5752 return 1;
5753
5754 if (main_icon == NULL)
5755 return 1;
5756
5757 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5758 (LPARAM) main_icon);
5759
5760 /* If there is a small icon that goes with it, set that too. */
5761 if (small_icon)
5762 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5763 (LPARAM) small_icon);
5764
5765 return 0;
5766 }
5767
5768 \f
5769 /************************************************************************
5770 Handling X errors
5771 ************************************************************************/
5772
5773 /* Display Error Handling functions not used on W32. Listing them here
5774 helps diff stay in step when comparing w32term.c with xterm.c.
5775
5776 x_error_catcher (display, error)
5777 x_catch_errors (dpy)
5778 x_catch_errors_unwind (old_val)
5779 x_check_errors (dpy, format)
5780 x_fully_uncatch_errors ()
5781 x_had_errors_p (dpy)
5782 x_clear_errors (dpy)
5783 x_uncatch_errors (dpy, count)
5784 x_trace_wire ()
5785 x_connection_signal (signalnum)
5786 x_connection_closed (dpy, error_message)
5787 x_error_quitter (display, error)
5788 x_error_handler (display, error)
5789 x_io_error_quitter (display)
5790
5791 */
5792
5793 \f
5794 /* Changing the font of the frame. */
5795
5796 Lisp_Object
5797 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
5798 {
5799 struct font *font = XFONT_OBJECT (font_object);
5800 int unit;
5801
5802 if (fontset < 0)
5803 fontset = fontset_from_font (font_object);
5804 FRAME_FONTSET (f) = fontset;
5805 if (FRAME_FONT (f) == font)
5806 /* This font is already set in frame F. There's nothing more to
5807 do. */
5808 return font_object;
5809
5810 FRAME_FONT (f) = font;
5811 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5812 FRAME_COLUMN_WIDTH (f) = unit = font->average_width;
5813 FRAME_LINE_HEIGHT (f) = font->height;
5814
5815 /* Compute number of scrollbar columns. */
5816 unit = FRAME_COLUMN_WIDTH (f);
5817 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5818 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5819 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
5820 else
5821 {
5822 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
5823 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) =
5824 FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
5825 }
5826
5827 /* Now make the frame display the given font. */
5828 if (FRAME_X_WINDOW (f) != 0)
5829 {
5830 /* Don't change the size of a tip frame; there's no point in
5831 doing it because it's done in Fx_show_tip, and it leads to
5832 problems because the tip frame has no widget. */
5833 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5834 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
5835 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3, 0);
5836 }
5837
5838 /* X version sets font of input methods here also. */
5839
5840 return font_object;
5841 }
5842
5843 \f
5844 /***********************************************************************
5845 TODO: W32 Input Methods
5846 ***********************************************************************/
5847 /* Listing missing functions from xterm.c helps diff stay in step.
5848
5849 xim_destroy_callback (xim, client_data, call_data)
5850 xim_open_dpy (dpyinfo, resource_name)
5851 struct xim_inst_t
5852 xim_instantiate_callback (display, client_data, call_data)
5853 xim_initialize (dpyinfo, resource_name)
5854 xim_close_dpy (dpyinfo)
5855
5856 */
5857
5858 \f
5859 /* Calculate the absolute position in frame F
5860 from its current recorded position values and gravity. */
5861
5862 void
5863 x_calc_absolute_position (struct frame *f)
5864 {
5865 int flags = f->size_hint_flags;
5866
5867 /* The sum of the widths of the frame's left and right borders, and
5868 the sum of the heights of the frame's top and bottom borders (in
5869 pixels) drawn by Windows. */
5870 unsigned int left_right_borders_width, top_bottom_borders_height;
5871
5872 /* Try to get the actual values of these two variables. We compute
5873 the border width (height) by subtracting the width (height) of
5874 the frame's client area from the width (height) of the frame's
5875 entire window. */
5876 WINDOWPLACEMENT wp = { 0 };
5877 RECT client_rect = { 0 };
5878
5879 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5880 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5881 {
5882 left_right_borders_width =
5883 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5884 (client_rect.right - client_rect.left);
5885
5886 top_bottom_borders_height =
5887 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5888 (client_rect.bottom - client_rect.top);
5889 }
5890 else
5891 {
5892 /* Use sensible default values. */
5893 left_right_borders_width = 8;
5894 top_bottom_borders_height = 32;
5895 }
5896
5897 /* Treat negative positions as relative to the rightmost bottommost
5898 position that fits on the screen. */
5899 if (flags & XNegative)
5900 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
5901 - FRAME_PIXEL_WIDTH (f)
5902 + f->left_pos
5903 - (left_right_borders_width - 1));
5904
5905 if (flags & YNegative)
5906 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
5907 - FRAME_PIXEL_HEIGHT (f)
5908 + f->top_pos
5909 - (top_bottom_borders_height - 1));
5910
5911 /* The left_pos and top_pos are now relative to the top and left
5912 screen edges, so the flags should correspond. */
5913 f->size_hint_flags &= ~ (XNegative | YNegative);
5914 }
5915
5916 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5917 to really change the position, and 0 when calling from
5918 x_make_frame_visible (in that case, XOFF and YOFF are the current
5919 position values). It is -1 when calling from x_set_frame_parameters,
5920 which means, do adjust for borders but don't change the gravity. */
5921
5922 void
5923 x_set_offset (struct frame *f, register int xoff, register int yoff,
5924 int change_gravity)
5925 {
5926 int modified_top, modified_left;
5927
5928 if (change_gravity > 0)
5929 {
5930 f->top_pos = yoff;
5931 f->left_pos = xoff;
5932 f->size_hint_flags &= ~ (XNegative | YNegative);
5933 if (xoff < 0)
5934 f->size_hint_flags |= XNegative;
5935 if (yoff < 0)
5936 f->size_hint_flags |= YNegative;
5937 f->win_gravity = NorthWestGravity;
5938 }
5939 x_calc_absolute_position (f);
5940
5941 block_input ();
5942 x_wm_set_size_hint (f, (long) 0, 0);
5943
5944 modified_left = f->left_pos;
5945 modified_top = f->top_pos;
5946
5947 my_set_window_pos (FRAME_W32_WINDOW (f),
5948 NULL,
5949 modified_left, modified_top,
5950 0, 0,
5951 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5952 unblock_input ();
5953 }
5954
5955 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
5956 wanted positions of the WM window (not Emacs window).
5957 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
5958 window (FRAME_X_WINDOW).
5959 */
5960
5961 static void
5962 x_fullscreen_adjust (struct frame *f, int *width, int *height, int *top_pos, int *left_pos)
5963 {
5964 int newwidth = FRAME_COLS (f);
5965 int newheight = FRAME_LINES (f);
5966 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
5967
5968 *top_pos = f->top_pos;
5969 *left_pos = f->left_pos;
5970
5971 if (f->want_fullscreen & FULLSCREEN_HEIGHT)
5972 {
5973 int ph;
5974
5975 ph = x_display_pixel_height (dpyinfo);
5976 newheight = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, ph);
5977 ph = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, newheight) - f->y_pixels_diff;
5978 newheight = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, ph);
5979 *top_pos = 0;
5980 }
5981
5982 if (f->want_fullscreen & FULLSCREEN_WIDTH)
5983 {
5984 int pw;
5985
5986 pw = x_display_pixel_width (dpyinfo);
5987 newwidth = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pw);
5988 pw = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, newwidth) - f->x_pixels_diff;
5989 newwidth = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pw);
5990 *left_pos = 0;
5991 }
5992
5993 *width = newwidth;
5994 *height = newheight;
5995 }
5996
5997 /* Check if we need to resize the frame due to a fullscreen request.
5998 If so needed, resize the frame. */
5999 static void
6000 x_check_fullscreen (struct frame *f)
6001 {
6002 if (f->want_fullscreen & FULLSCREEN_BOTH)
6003 {
6004 int width, height, ign;
6005
6006 x_real_positions (f, &f->left_pos, &f->top_pos);
6007
6008 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
6009
6010 /* We do not need to move the window, it shall be taken care of
6011 when setting WM manager hints. */
6012 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
6013 {
6014 change_frame_size (f, width, height, 0, 1, 0, 0);
6015 SET_FRAME_GARBAGED (f);
6016 cancel_mouse_face (f);
6017
6018 /* Wait for the change of frame size to occur. */
6019 f->want_fullscreen |= FULLSCREEN_WAIT;
6020 }
6021 }
6022 }
6023
6024 static void
6025 w32fullscreen_hook (struct frame *f)
6026 {
6027 if (FRAME_VISIBLE_P (f))
6028 {
6029 HWND hwnd = FRAME_W32_WINDOW(f);
6030 DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
6031 RECT rect;
6032 enum fullscreen_type prev_fsmode = FRAME_PREV_FSMODE (f);
6033
6034 block_input();
6035 f->want_fullscreen &= ~FULLSCREEN_WAIT;
6036
6037 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
6038 GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6039
6040 if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
6041 {
6042 SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
6043 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6044 }
6045 else if (FRAME_PREV_FSMODE (f) == FULLSCREEN_HEIGHT
6046 || FRAME_PREV_FSMODE (f) == FULLSCREEN_WIDTH)
6047 SetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
6048
6049 FRAME_PREV_FSMODE (f) = f->want_fullscreen;
6050
6051 if (f->want_fullscreen == FULLSCREEN_NONE)
6052 ShowWindow (hwnd, SW_SHOWNORMAL);
6053 else if (f->want_fullscreen == FULLSCREEN_MAXIMIZED)
6054 {
6055 if (prev_fsmode == FULLSCREEN_BOTH || prev_fsmode == FULLSCREEN_WIDTH
6056 || prev_fsmode == FULLSCREEN_HEIGHT)
6057 /* Make window normal since otherwise the subsequent
6058 maximization might fail in some cases. */
6059 ShowWindow (hwnd, SW_SHOWNORMAL);
6060 ShowWindow (hwnd, SW_MAXIMIZE);
6061 }
6062 else if (f->want_fullscreen == FULLSCREEN_BOTH)
6063 {
6064 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6065 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6066 SetWindowLong (hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
6067 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
6068 rect.right - rect.left, rect.bottom - rect.top,
6069 SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
6070 }
6071 else
6072 {
6073 ShowWindow (hwnd, SW_SHOWNORMAL);
6074 w32_fullscreen_rect (hwnd, f->want_fullscreen,
6075 FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
6076 SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
6077 rect.right - rect.left, rect.bottom - rect.top, 0);
6078 }
6079
6080 f->want_fullscreen = FULLSCREEN_NONE;
6081 unblock_input ();
6082 }
6083 else
6084 f->want_fullscreen |= FULLSCREEN_WAIT;
6085 }
6086
6087 /* Call this to change the size of frame F's x-window.
6088 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
6089 for this size change and subsequent size changes.
6090 Otherwise we leave the window gravity unchanged. */
6091
6092 void
6093 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
6094 {
6095 int pixelwidth, pixelheight;
6096 RECT rect;
6097
6098 block_input ();
6099
6100 if (pixelwise)
6101 {
6102 pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
6103 pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
6104 }
6105 else
6106 {
6107 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width);
6108 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height);
6109 }
6110
6111 f->win_gravity = NorthWestGravity;
6112 x_wm_set_size_hint (f, (long) 0, 0);
6113
6114 f->want_fullscreen = FULLSCREEN_NONE;
6115 w32fullscreen_hook (f);
6116
6117 rect.left = rect.top = 0;
6118 rect.right = pixelwidth;
6119 rect.bottom = pixelheight;
6120
6121 AdjustWindowRect (&rect, f->output_data.w32->dwStyle,
6122 FRAME_EXTERNAL_MENU_BAR (f));
6123
6124 my_set_window_pos (FRAME_W32_WINDOW (f),
6125 NULL,
6126 0, 0,
6127 rect.right - rect.left,
6128 rect.bottom - rect.top,
6129 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
6130
6131 /* If w32_enable_frame_resize_hack is non-nil, immediately apply the
6132 new pixel sizes to the frame and its subwindows.
6133
6134 Jason Rumney earlier refused to call change_frame_size right here
6135 with the following argument:
6136
6137 The following mirrors what is done in xterm.c. It appears to be for
6138 informing lisp of the new size immediately, while the actual resize
6139 will happen asynchronously. But on Windows, the menu bar
6140 automatically wraps when the frame is too narrow to contain it, and
6141 that causes any calculations made here to come out wrong. The end
6142 is some nasty buggy behavior, including the potential loss of the
6143 minibuffer.
6144
6145 Disabling this code is either not sufficient to fix the problems
6146 completely, or it causes fresh problems, but at least it removes
6147 the most problematic symptom of the minibuffer becoming unusable.
6148
6149 However, as the discussion about how to handle frame size
6150 parameters on Windows (Bug#1348, Bug#16028) shows, that cure seems
6151 worse than the disease. In particular, menu bar wrapping looks
6152 like a non-issue - maybe so because Windows eventually gets back to
6153 us with the correct client rectangle anyway. But we have to avoid
6154 calling change_frame_size with a delta of less than one canoncial
6155 character size when frame_resize_pixelwise is nil, as explained in
6156 the comment above. */
6157
6158 if (w32_enable_frame_resize_hack)
6159
6160 {
6161 change_frame_size (f, FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth),
6162 FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight),
6163 0, 1, 0, 1);
6164 SET_FRAME_GARBAGED (f);
6165
6166 /* If cursor was outside the new size, mark it as off. */
6167 mark_window_cursors_off (XWINDOW (f->root_window));
6168
6169 /* Clear out any recollection of where the mouse highlighting was,
6170 since it might be in a place that's outside the new frame size.
6171 Actually checking whether it is outside is a pain in the neck,
6172 so don't try--just let the highlighting be done afresh with new
6173 size. */
6174 cancel_mouse_face (f);
6175 }
6176
6177 unblock_input ();
6178
6179 do_pending_window_change (0);
6180 }
6181 \f
6182 /* Mouse warping. */
6183
6184 void
6185 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
6186 {
6187 RECT rect;
6188 POINT pt;
6189
6190 block_input ();
6191
6192 GetClientRect (FRAME_W32_WINDOW (f), &rect);
6193 pt.x = rect.left + pix_x;
6194 pt.y = rect.top + pix_y;
6195 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
6196
6197 SetCursorPos (pt.x, pt.y);
6198
6199 unblock_input ();
6200 }
6201
6202 \f
6203 /* focus shifting, raising and lowering. */
6204
6205 void
6206 x_focus_frame (struct frame *f)
6207 {
6208 #if 0
6209 struct w32_display_info *dpyinfo = &one_w32_display_info;
6210 #endif
6211
6212 /* Give input focus to frame. */
6213 block_input ();
6214 #if 0
6215 /* Try not to change its Z-order if possible. */
6216 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
6217 my_set_focus (f, FRAME_W32_WINDOW (f));
6218 else
6219 #endif
6220 my_set_foreground_window (FRAME_W32_WINDOW (f));
6221 unblock_input ();
6222 }
6223
6224 /* Raise frame F. */
6225 void
6226 x_raise_frame (struct frame *f)
6227 {
6228 block_input ();
6229
6230 /* Strictly speaking, raise-frame should only change the frame's Z
6231 order, leaving input focus unchanged. This is reasonable behavior
6232 on X where the usual policy is point-to-focus. However, this
6233 behavior would be very odd on Windows where the usual policy is
6234 click-to-focus.
6235
6236 On X, if the mouse happens to be over the raised frame, it gets
6237 input focus anyway (so the window with focus will never be
6238 completely obscured) - if not, then just moving the mouse over it
6239 is sufficient to give it focus. On Windows, the user must actually
6240 click on the frame (preferably the title bar so as not to move
6241 point), which is more awkward. Also, no other Windows program
6242 raises a window to the top but leaves another window (possibly now
6243 completely obscured) with input focus.
6244
6245 Because there is a system setting on Windows that allows the user
6246 to choose the point to focus policy, we make the strict semantics
6247 optional, but by default we grab focus when raising. */
6248
6249 if (NILP (Vw32_grab_focus_on_raise))
6250 {
6251 /* The obvious call to my_set_window_pos doesn't work if Emacs is
6252 not already the foreground application: the frame is raised
6253 above all other frames belonging to us, but not above the
6254 current top window. To achieve that, we have to resort to this
6255 more cumbersome method. */
6256
6257 HDWP handle = BeginDeferWindowPos (2);
6258 if (handle)
6259 {
6260 handle = DeferWindowPos (handle,
6261 FRAME_W32_WINDOW (f),
6262 HWND_TOP,
6263 0, 0, 0, 0,
6264 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
6265 if (handle)
6266 {
6267 handle = DeferWindowPos (handle,
6268 GetForegroundWindow (),
6269 FRAME_W32_WINDOW (f),
6270 0, 0, 0, 0,
6271 SWP_NOSIZE | SWP_NOMOVE |
6272 SWP_NOACTIVATE);
6273 if (handle)
6274 EndDeferWindowPos (handle);
6275 }
6276 }
6277 }
6278 else
6279 {
6280 my_bring_window_to_top (FRAME_W32_WINDOW (f));
6281 }
6282
6283 unblock_input ();
6284 }
6285
6286 /* Lower frame F. */
6287 void
6288 x_lower_frame (struct frame *f)
6289 {
6290 block_input ();
6291 my_set_window_pos (FRAME_W32_WINDOW (f),
6292 HWND_BOTTOM,
6293 0, 0, 0, 0,
6294 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
6295 unblock_input ();
6296 }
6297
6298 static void
6299 w32_frame_raise_lower (struct frame *f, int raise_flag)
6300 {
6301 if (! FRAME_W32_P (f))
6302 return;
6303
6304 if (raise_flag)
6305 x_raise_frame (f);
6306 else
6307 x_lower_frame (f);
6308 }
6309 \f
6310 /* Change of visibility. */
6311
6312 /* This tries to wait until the frame is really visible.
6313 However, if the window manager asks the user where to position
6314 the frame, this will return before the user finishes doing that.
6315 The frame will not actually be visible at that time,
6316 but it will become visible later when the window manager
6317 finishes with it. */
6318
6319 void
6320 x_make_frame_visible (struct frame *f)
6321 {
6322 block_input ();
6323
6324 x_set_bitmap_icon (f);
6325
6326 if (! FRAME_VISIBLE_P (f))
6327 {
6328 /* We test FRAME_GARBAGED_P here to make sure we don't
6329 call x_set_offset a second time
6330 if we get to x_make_frame_visible a second time
6331 before the window gets really visible. */
6332 if (! FRAME_ICONIFIED_P (f)
6333 && ! f->output_data.w32->asked_for_visible)
6334 {
6335 RECT workarea_rect;
6336 RECT window_rect;
6337
6338 /* Adjust vertical window position in order to avoid being
6339 covered by a task bar placed at the bottom of the desktop. */
6340 SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
6341 GetWindowRect (FRAME_W32_WINDOW (f), &window_rect);
6342 if (window_rect.bottom > workarea_rect.bottom
6343 && window_rect.top > workarea_rect.top)
6344 f->top_pos = max (window_rect.top
6345 - window_rect.bottom + workarea_rect.bottom,
6346 workarea_rect.top);
6347
6348 x_set_offset (f, f->left_pos, f->top_pos, 0);
6349 }
6350
6351 f->output_data.w32->asked_for_visible = 1;
6352
6353 /* According to a report in emacs-devel 2008-06-03, SW_SHOWNORMAL
6354 causes unexpected behavior when unminimizing frames that were
6355 previously maximized. But only SW_SHOWNORMAL works properly for
6356 frames that were truely hidden (using make-frame-invisible), so
6357 we need it to avoid Bug#5482. It seems that iconified is only
6358 set for minimized windows that are still visible, so use that to
6359 determine the appropriate flag to pass ShowWindow. */
6360 my_show_window (f, FRAME_W32_WINDOW (f),
6361 FRAME_ICONIFIED_P (f) ? SW_RESTORE : SW_SHOWNORMAL);
6362 }
6363
6364 /* Synchronize to ensure Emacs knows the frame is visible
6365 before we do anything else. We do this loop with input not blocked
6366 so that incoming events are handled. */
6367 {
6368 Lisp_Object frame;
6369 int count;
6370
6371 /* This must come after we set COUNT. */
6372 unblock_input ();
6373
6374 XSETFRAME (frame, f);
6375
6376 /* Wait until the frame is visible. Process X events until a
6377 MapNotify event has been seen, or until we think we won't get a
6378 MapNotify at all.. */
6379 for (count = input_signal_count + 10;
6380 input_signal_count < count && !FRAME_VISIBLE_P (f);)
6381 {
6382 /* Force processing of queued events. */
6383 /* TODO: x_sync equivalent? */
6384
6385 /* Machines that do polling rather than SIGIO have been observed
6386 to go into a busy-wait here. So we'll fake an alarm signal
6387 to let the handler know that there's something to be read.
6388 We used to raise a real alarm, but it seems that the handler
6389 isn't always enabled here. This is probably a bug. */
6390 if (input_polling_used ())
6391 {
6392 /* It could be confusing if a real alarm arrives while processing
6393 the fake one. Turn it off and let the handler reset it. */
6394 int old_poll_suppress_count = poll_suppress_count;
6395 poll_suppress_count = 1;
6396 poll_for_input_1 ();
6397 poll_suppress_count = old_poll_suppress_count;
6398 }
6399 }
6400 }
6401 }
6402
6403 /* Change from mapped state to withdrawn state. */
6404
6405 /* Make the frame visible (mapped and not iconified). */
6406
6407 void
6408 x_make_frame_invisible (struct frame *f)
6409 {
6410 /* Don't keep the highlight on an invisible frame. */
6411 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6412 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6413
6414 block_input ();
6415
6416 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
6417
6418 /* We can't distinguish this from iconification
6419 just by the event that we get from the server.
6420 So we can't win using the usual strategy of letting
6421 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
6422 and synchronize with the server to make sure we agree. */
6423 SET_FRAME_VISIBLE (f, 0);
6424 SET_FRAME_ICONIFIED (f, 0);
6425
6426 unblock_input ();
6427 }
6428
6429 /* Change window state from mapped to iconified. */
6430
6431 void
6432 x_iconify_frame (struct frame *f)
6433 {
6434 /* Don't keep the highlight on an invisible frame. */
6435 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
6436 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
6437
6438 if (FRAME_ICONIFIED_P (f))
6439 return;
6440
6441 block_input ();
6442
6443 x_set_bitmap_icon (f);
6444
6445 /* Simulate the user minimizing the frame. */
6446 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
6447
6448 SET_FRAME_VISIBLE (f, 0);
6449 SET_FRAME_ICONIFIED (f, 1);
6450
6451 unblock_input ();
6452 }
6453
6454 \f
6455 /* Free X resources of frame F. */
6456
6457 void
6458 x_free_frame_resources (struct frame *f)
6459 {
6460 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6461 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
6462
6463 block_input ();
6464
6465 /* We must free faces before destroying windows because some
6466 font-driver (e.g. xft) access a window while finishing a
6467 face. */
6468 free_frame_faces (f);
6469
6470 if (FRAME_W32_WINDOW (f))
6471 my_destroy_window (f, FRAME_W32_WINDOW (f));
6472
6473 free_frame_menubar (f);
6474
6475 xfree (f->output_data.w32);
6476 f->output_data.w32 = NULL;
6477
6478 if (f == dpyinfo->w32_focus_frame)
6479 dpyinfo->w32_focus_frame = 0;
6480 if (f == dpyinfo->w32_focus_event_frame)
6481 dpyinfo->w32_focus_event_frame = 0;
6482 if (f == dpyinfo->x_highlight_frame)
6483 dpyinfo->x_highlight_frame = 0;
6484 if (f == hlinfo->mouse_face_mouse_frame)
6485 reset_mouse_highlight (hlinfo);
6486
6487 unblock_input ();
6488 }
6489
6490
6491 /* Destroy the window of frame F. */
6492 void
6493 x_destroy_window (struct frame *f)
6494 {
6495 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6496
6497 x_free_frame_resources (f);
6498 dpyinfo->reference_count--;
6499 }
6500
6501 \f
6502 /* Setting window manager hints. */
6503
6504 /* Set the normal size hints for the window manager, for frame F.
6505 FLAGS is the flags word to use--or 0 meaning preserve the flags
6506 that the window now has.
6507 If USER_POSITION, set the USPosition
6508 flag (this is useful when FLAGS is 0). */
6509 void
6510 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
6511 {
6512 Window window = FRAME_W32_WINDOW (f);
6513
6514 enter_crit ();
6515
6516 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
6517 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
6518 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
6519 SetWindowLong (window, WND_VSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_WIDTH (f));
6520 SetWindowLong (window, WND_HSCROLLBAR_INDEX, FRAME_SCROLL_BAR_AREA_HEIGHT (f));
6521
6522 leave_crit ();
6523 }
6524
6525 /***********************************************************************
6526 Fonts
6527 ***********************************************************************/
6528
6529 #ifdef GLYPH_DEBUG
6530
6531 /* Check that FONT is valid on frame F. It is if it can be found in F's
6532 font table. */
6533
6534 static void
6535 x_check_font (struct frame *f, struct font *font)
6536 {
6537 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
6538 if (font->driver->check)
6539 eassert (font->driver->check (f, font) == 0);
6540 }
6541
6542 #endif /* GLYPH_DEBUG */
6543
6544 /* Show hourglass cursor on frame F. */
6545
6546 static void
6547 w32_show_hourglass (struct frame *f)
6548 {
6549 if (!menubar_in_use && !current_popup_menu)
6550 {
6551 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6552
6553 w32->hourglass_p = 1;
6554 SetCursor (w32->hourglass_cursor);
6555 }
6556 }
6557
6558 /* Hide hourglass cursor on frame F. */
6559
6560 static void
6561 w32_hide_hourglass (struct frame *f)
6562 {
6563 struct w32_output *w32 = FRAME_X_OUTPUT (f);
6564
6565 w32->hourglass_p = 0;
6566 SetCursor (w32->current_cursor);
6567 }
6568
6569 /* FIXME: old code did that, but I don't know why. Anyway,
6570 this is used for non-GUI frames (see cancel_hourglass). */
6571
6572 void
6573 w32_arrow_cursor (void)
6574 {
6575 SetCursor (w32_load_cursor (IDC_ARROW));
6576 }
6577
6578 /***********************************************************************
6579 Initialization
6580 ***********************************************************************/
6581
6582 static int w32_initialized = 0;
6583
6584 void
6585 w32_initialize_display_info (Lisp_Object display_name)
6586 {
6587 struct w32_display_info *dpyinfo = &one_w32_display_info;
6588
6589 memset (dpyinfo, 0, sizeof (*dpyinfo));
6590
6591 dpyinfo->name_list_element = Fcons (display_name, Qnil);
6592 dpyinfo->w32_id_name = xmalloc (SCHARS (Vinvocation_name)
6593 + SCHARS (Vsystem_name) + 2);
6594 sprintf (dpyinfo->w32_id_name, "%s@%s",
6595 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6596
6597 /* Default Console mode values - overridden when running in GUI mode
6598 with values obtained from system metrics. */
6599 dpyinfo->resx = 1;
6600 dpyinfo->resy = 1;
6601 dpyinfo->n_planes = 1;
6602 dpyinfo->n_cbits = 4;
6603 dpyinfo->n_fonts = 0;
6604 dpyinfo->smallest_font_height = 1;
6605 dpyinfo->smallest_char_width = 1;
6606 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6607 dpyinfo->horizontal_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6608 /* TODO: dpyinfo->gray */
6609
6610 reset_mouse_highlight (&dpyinfo->mouse_highlight);
6611 }
6612
6613 /* Create an xrdb-style database of resources to supersede registry settings.
6614 The database is just a concatenation of C strings, finished by an additional
6615 \0. The strings are submitted to some basic normalization, so
6616
6617 [ *]option[ *]:[ *]value...
6618
6619 becomes
6620
6621 option:value...
6622
6623 but any whitespace following value is not removed. */
6624
6625 static char *
6626 w32_make_rdb (char *xrm_option)
6627 {
6628 char *buffer = xmalloc (strlen (xrm_option) + 2);
6629 char *current = buffer;
6630 char ch;
6631 int in_option = 1;
6632 int before_value = 0;
6633
6634 do {
6635 ch = *xrm_option++;
6636
6637 if (ch == '\n')
6638 {
6639 *current++ = '\0';
6640 in_option = 1;
6641 before_value = 0;
6642 }
6643 else if (ch != ' ')
6644 {
6645 *current++ = ch;
6646 if (in_option && (ch == ':'))
6647 {
6648 in_option = 0;
6649 before_value = 1;
6650 }
6651 else if (before_value)
6652 {
6653 before_value = 0;
6654 }
6655 }
6656 else if (!(in_option || before_value))
6657 {
6658 *current++ = ch;
6659 }
6660 } while (ch);
6661
6662 *current = '\0';
6663
6664 return buffer;
6665 }
6666
6667 extern frame_parm_handler w32_frame_parm_handlers[];
6668
6669 static struct redisplay_interface w32_redisplay_interface =
6670 {
6671 w32_frame_parm_handlers,
6672 x_produce_glyphs,
6673 x_write_glyphs,
6674 x_insert_glyphs,
6675 x_clear_end_of_line,
6676 x_scroll_run,
6677 x_after_update_window_line,
6678 x_update_window_begin,
6679 x_update_window_end,
6680 0, /* flush_display */
6681 x_clear_window_mouse_face,
6682 x_get_glyph_overhangs,
6683 x_fix_overlapping_area,
6684 w32_draw_fringe_bitmap,
6685 w32_define_fringe_bitmap,
6686 w32_destroy_fringe_bitmap,
6687 w32_compute_glyph_string_overhangs,
6688 x_draw_glyph_string,
6689 w32_define_frame_cursor,
6690 w32_clear_frame_area,
6691 w32_draw_window_cursor,
6692 w32_draw_vertical_window_border,
6693 w32_draw_window_divider,
6694 w32_shift_glyphs_for_insert,
6695 w32_show_hourglass,
6696 w32_hide_hourglass
6697 };
6698
6699 static void x_delete_terminal (struct terminal *term);
6700
6701 static struct terminal *
6702 w32_create_terminal (struct w32_display_info *dpyinfo)
6703 {
6704 struct terminal *terminal;
6705
6706 terminal = create_terminal (output_w32, &w32_redisplay_interface);
6707
6708 terminal->display_info.w32 = dpyinfo;
6709 dpyinfo->terminal = terminal;
6710
6711 /* MSVC does not type K&R functions with no arguments correctly, and
6712 so we must explicitly cast them. */
6713 terminal->clear_frame_hook = x_clear_frame;
6714 terminal->ins_del_lines_hook = x_ins_del_lines;
6715 terminal->delete_glyphs_hook = x_delete_glyphs;
6716 terminal->ring_bell_hook = w32_ring_bell;
6717 terminal->update_begin_hook = x_update_begin;
6718 terminal->update_end_hook = x_update_end;
6719 terminal->read_socket_hook = w32_read_socket;
6720 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6721 terminal->mouse_position_hook = w32_mouse_position;
6722 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6723 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6724 terminal->fullscreen_hook = w32fullscreen_hook;
6725 terminal->menu_show_hook = w32_menu_show;
6726 terminal->popup_dialog_hook = w32_popup_dialog;
6727 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6728 terminal->set_horizontal_scroll_bar_hook = w32_set_horizontal_scroll_bar;
6729 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6730 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6731 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6732 terminal->delete_frame_hook = x_destroy_window;
6733 terminal->delete_terminal_hook = x_delete_terminal;
6734 /* Other hooks are NULL by default. */
6735
6736 /* We don't yet support separate terminals on W32, so don't try to share
6737 keyboards between virtual terminals that are on the same physical
6738 terminal like X does. */
6739 terminal->kboard = allocate_kboard (Qw32);
6740 /* Don't let the initial kboard remain current longer than necessary.
6741 That would cause problems if a file loaded on startup tries to
6742 prompt in the mini-buffer. */
6743 if (current_kboard == initial_kboard)
6744 current_kboard = terminal->kboard;
6745 terminal->kboard->reference_count++;
6746
6747 return terminal;
6748 }
6749
6750 static void
6751 x_delete_terminal (struct terminal *terminal)
6752 {
6753 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6754
6755 /* Protect against recursive calls. delete_frame in
6756 delete_terminal calls us back when it deletes our last frame. */
6757 if (!terminal->name)
6758 return;
6759
6760 block_input ();
6761
6762 x_delete_display (dpyinfo);
6763 unblock_input ();
6764 }
6765
6766 struct w32_display_info *
6767 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
6768 {
6769 struct w32_display_info *dpyinfo;
6770 struct terminal *terminal;
6771 HDC hdc;
6772
6773 block_input ();
6774
6775 if (!w32_initialized)
6776 {
6777 w32_initialize ();
6778 w32_initialized = 1;
6779 }
6780
6781 w32_initialize_display_info (display_name);
6782
6783 dpyinfo = &one_w32_display_info;
6784 terminal = w32_create_terminal (dpyinfo);
6785
6786 /* Set the name of the terminal. */
6787 terminal->name = xlispstrdup (display_name);
6788
6789 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6790
6791 /* Put this display on the chain. */
6792 dpyinfo->next = x_display_list;
6793 x_display_list = dpyinfo;
6794
6795 hdc = GetDC (NULL);
6796
6797 dpyinfo->root_window = GetDesktopWindow ();
6798 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6799 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6800 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6801 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6802 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6803 ReleaseDC (NULL, hdc);
6804
6805 /* initialize palette with white and black */
6806 {
6807 XColor color;
6808 w32_defined_color (0, "white", &color, 1);
6809 w32_defined_color (0, "black", &color, 1);
6810 }
6811
6812 #ifdef WINDOWSNT
6813 /* Add the default keyboard. When !WINDOWSNT, we're using the
6814 standard Emacs console handling machinery and don't need an
6815 explicit FD here. */
6816 add_keyboard_wait_descriptor (0);
6817 #elif CYGWIN
6818 /* /dev/windows wakes us up when we have a thread message pending. */
6819 add_keyboard_wait_descriptor (w32_message_fd);
6820 #endif
6821
6822 /* Create Fringe Bitmaps and store them for later use.
6823
6824 On W32, bitmaps are all unsigned short, as Windows requires
6825 bitmap data to be Word aligned. For some reason they are
6826 horizontally reflected compared to how they appear on X, so we
6827 need to bitswap and convert to unsigned shorts before creating
6828 the bitmaps. */
6829 w32_init_fringe (terminal->rif);
6830
6831 unblock_input ();
6832
6833 return dpyinfo;
6834 }
6835 \f
6836 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6837 void
6838 x_delete_display (struct w32_display_info *dpyinfo)
6839 {
6840 /* FIXME: the only display info apparently can't be deleted. */
6841 /* free palette table */
6842 {
6843 struct w32_palette_entry * plist;
6844
6845 plist = dpyinfo->color_list;
6846 while (plist)
6847 {
6848 struct w32_palette_entry * pentry = plist;
6849 plist = plist->next;
6850 xfree (pentry);
6851 }
6852 dpyinfo->color_list = NULL;
6853 if (dpyinfo->palette)
6854 DeleteObject (dpyinfo->palette);
6855 }
6856 w32_reset_fringes ();
6857 }
6858
6859 \f
6860 /* Set up use of W32. */
6861
6862 DWORD WINAPI w32_msg_worker (void * arg);
6863
6864 static void
6865 w32_initialize (void)
6866 {
6867 HANDLE shell;
6868 HRESULT (WINAPI * set_user_model) (const wchar_t * id);
6869
6870 baud_rate = 19200;
6871
6872 w32_system_caret_hwnd = NULL;
6873 w32_system_caret_height = 0;
6874 w32_system_caret_x = 0;
6875 w32_system_caret_y = 0;
6876
6877 /* On Windows 7 and later, we need to set the user model ID
6878 to associate emacsclient launched files with Emacs frames
6879 in the UI. */
6880 shell = GetModuleHandle ("shell32.dll");
6881 if (shell)
6882 {
6883 set_user_model
6884 = (void *) GetProcAddress (shell,
6885 "SetCurrentProcessExplicitAppUserModelID");
6886
6887 /* If the function is defined, then we are running on Windows 7
6888 or newer, and the UI uses this to group related windows
6889 together. Since emacs, runemacs, emacsclient are related, we
6890 want them grouped even though the executables are different,
6891 so we need to set a consistent ID between them. */
6892 if (set_user_model)
6893 set_user_model (L"GNU.Emacs");
6894 }
6895
6896 #ifdef CYGWIN
6897 if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
6898 fatal ("opening /dev/windows: %s", strerror (errno));
6899 #endif /* CYGWIN */
6900
6901 /* Initialize w32_use_visible_system_caret based on whether a screen
6902 reader is in use. */
6903 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6904 &w32_use_visible_system_caret, 0))
6905 w32_use_visible_system_caret = 0;
6906
6907 any_help_event_p = 0;
6908
6909 /* Initialize input mode: interrupt_input off, no flow control, allow
6910 8 bit character input, standard quit char. */
6911 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6912
6913 {
6914 LCID input_locale_id = LOWORD (GetKeyboardLayout (0));
6915 w32_keyboard_codepage = codepage_for_locale (input_locale_id);
6916 }
6917
6918 /* Create the window thread - it will terminate itself when the app
6919 terminates */
6920 init_crit ();
6921
6922 dwMainThreadId = GetCurrentThreadId ();
6923 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6924 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6925
6926 /* Wait for thread to start */
6927 {
6928 MSG msg;
6929
6930 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6931
6932 hWindowsThread = CreateThread (NULL, 0,
6933 w32_msg_worker,
6934 0, 0, &dwWindowsThreadId);
6935
6936 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6937 }
6938
6939 /* It is desirable that mainThread should have the same notion of
6940 focus window and active window as windowsThread. Unfortunately, the
6941 following call to AttachThreadInput, which should do precisely what
6942 we need, causes major problems when Emacs is linked as a console
6943 program. Unfortunately, we have good reasons for doing that, so
6944 instead we need to send messages to windowsThread to make some API
6945 calls for us (ones that affect, or depend on, the active/focus
6946 window state.) */
6947 #ifdef ATTACH_THREADS
6948 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6949 #endif
6950
6951 /* Dynamically link to optional system components. */
6952 {
6953 HMODULE user_lib = GetModuleHandle ("user32.dll");
6954
6955 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6956
6957 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6958
6959 #undef LOAD_PROC
6960
6961 /* Ensure scrollbar handles are at least 5 pixels. */
6962 vertical_scroll_bar_min_handle = 5;
6963 horizontal_scroll_bar_min_handle = 5;
6964
6965 /* For either kind of scroll bar, take account of the arrows; these
6966 effectively form the border of the main scroll bar range. */
6967 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6968 = GetSystemMetrics (SM_CYVSCROLL);
6969 horizontal_scroll_bar_left_border = horizontal_scroll_bar_right_border
6970 = GetSystemMetrics (SM_CYHSCROLL);
6971 }
6972 }
6973
6974 void
6975 syms_of_w32term (void)
6976 {
6977 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6978
6979 DEFSYM (Qadded, "added");
6980 DEFSYM (Qremoved, "removed");
6981 DEFSYM (Qmodified, "modified");
6982 DEFSYM (Qrenamed_from, "renamed-from");
6983 DEFSYM (Qrenamed_to, "renamed-to");
6984
6985 DEFVAR_INT ("w32-num-mouse-buttons",
6986 w32_num_mouse_buttons,
6987 doc: /* Number of physical mouse buttons. */);
6988 w32_num_mouse_buttons = 2;
6989
6990 DEFVAR_LISP ("w32-swap-mouse-buttons",
6991 Vw32_swap_mouse_buttons,
6992 doc: /* Swap the mapping of middle and right mouse buttons.
6993 When nil, middle button is mouse-2 and right button is mouse-3. */);
6994 Vw32_swap_mouse_buttons = Qnil;
6995
6996 DEFVAR_LISP ("w32-grab-focus-on-raise",
6997 Vw32_grab_focus_on_raise,
6998 doc: /* Raised frame grabs input focus.
6999 When t, `raise-frame' grabs input focus as well. This fits well
7000 with the normal Windows click-to-focus policy, but might not be
7001 desirable when using a point-to-focus policy. */);
7002 Vw32_grab_focus_on_raise = Qt;
7003
7004 DEFVAR_LISP ("w32-capslock-is-shiftlock",
7005 Vw32_capslock_is_shiftlock,
7006 doc: /* Apply CapsLock state to non character input keys.
7007 When nil, CapsLock only affects normal character input keys. */);
7008 Vw32_capslock_is_shiftlock = Qnil;
7009
7010 DEFVAR_LISP ("w32-recognize-altgr",
7011 Vw32_recognize_altgr,
7012 doc: /* Recognize right-alt and left-ctrl as AltGr.
7013 When nil, the right-alt and left-ctrl key combination is
7014 interpreted normally. */);
7015 Vw32_recognize_altgr = Qt;
7016
7017 DEFVAR_BOOL ("w32-use-visible-system-caret",
7018 w32_use_visible_system_caret,
7019 doc: /* Flag to make the system caret visible.
7020 When this is non-nil, Emacs will indicate the position of point by
7021 using the system caret instead of drawing its own cursor. Some screen
7022 reader software does not track the system cursor properly when it is
7023 invisible, and gets confused by Emacs drawing its own cursor, so this
7024 variable is initialized to t when Emacs detects that screen reader
7025 software is running as it starts up.
7026
7027 When this variable is set, other variables affecting the appearance of
7028 the cursor have no effect. */);
7029
7030 w32_use_visible_system_caret = 0;
7031
7032 /* We don't yet support this, but defining this here avoids whining
7033 from cus-start.el and other places, like "M-x set-variable". */
7034 DEFVAR_BOOL ("x-use-underline-position-properties",
7035 x_use_underline_position_properties,
7036 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
7037 A value of nil means ignore them. If you encounter fonts with bogus
7038 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7039 to 4.1, set this to nil. You can also use `underline-minimum-offset'
7040 to override the font's UNDERLINE_POSITION for small font display
7041 sizes. */);
7042 x_use_underline_position_properties = 0;
7043
7044 DEFVAR_BOOL ("x-underline-at-descent-line",
7045 x_underline_at_descent_line,
7046 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7047 A value of nil means to draw the underline according to the value of the
7048 variable `x-use-underline-position-properties', which is usually at the
7049 baseline level. The default value is nil. */);
7050 x_underline_at_descent_line = 0;
7051
7052 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7053 doc: /* Which toolkit scroll bars Emacs uses, if any.
7054 A value of nil means Emacs doesn't use toolkit scroll bars.
7055 With the X Window system, the value is a symbol describing the
7056 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
7057 With MS Windows or Nextstep, the value is t. */);
7058 Vx_toolkit_scroll_bars = Qt;
7059
7060 DEFVAR_BOOL ("w32-unicode-filenames",
7061 w32_unicode_filenames,
7062 doc: /* Non-nil means use Unicode APIs when passing file names to the OS.
7063 A value of nil means file names passed to the OS APIs and returned
7064 from those APIs are encoded/decoded using the ANSI codepage
7065 specified by `file-name-coding-system'.
7066
7067 This variable is set to non-nil by default when Emacs runs on Windows
7068 systems of the NT family, including W2K, XP, Vista, Windows 7 and
7069 Windows 8. It is set to nil on Windows 9X. */);
7070 w32_unicode_filenames = 0;
7071
7072 DEFVAR_BOOL ("w32-enable-frame-resize-hack",
7073 w32_enable_frame_resize_hack,
7074 doc: /* Non-nil means enable hack for frame resizing on Windows.
7075 A value of nil means to resize frames by sending a corresponding request
7076 to the Windows API and changing the pixel sizes of the frame and its
7077 windows after the latter calls back. If this is non-nil, Emacs changes
7078 the pixel sizes of the frame and its windows at the time it sends the
7079 resize request to the API. */);
7080 w32_enable_frame_resize_hack = 1;
7081
7082 /* Tell Emacs about this window system. */
7083 Fprovide (Qw32, Qnil);
7084 }