bug#1849 - Windows 7 Taskbar Support
[bpt/emacs.git] / src / w32term.c
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "w32term.h"
28
29 #include "systty.h"
30 #include "systime.h"
31
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35
36 #include "charset.h"
37 #include "character.h"
38 #include "coding.h"
39 #include "ccl.h"
40 #include "frame.h"
41 #include "dispextern.h"
42 #include "fontset.h"
43 #include "termhooks.h"
44 #include "termopts.h"
45 #include "termchar.h"
46 #include "disptab.h"
47 #include "buffer.h"
48 #include "window.h"
49 #include "keyboard.h"
50 #include "intervals.h"
51 #include "process.h"
52 #include "atimer.h"
53 #include "keymap.h"
54
55 #include "w32heap.h"
56 #include <shellapi.h>
57
58 #include "font.h"
59 #include "w32font.h"
60 \f
61 /* Fringe bitmaps. */
62
63 static int max_fringe_bmp = 0;
64 static HBITMAP *fringe_bmp = 0;
65
66 /* Non-nil means Emacs uses toolkit scroll bars. */
67
68 Lisp_Object Vx_toolkit_scroll_bars;
69
70 /* Temporary variables for w32_read_socket. */
71
72 static int last_mousemove_x = 0;
73 static int last_mousemove_y = 0;
74
75 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
76 #ifndef GET_WHEEL_DELTA_WPARAM
77 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
78 #endif
79
80 /* Non-zero means that a HELP_EVENT has been generated since Emacs
81 start. */
82
83 static int any_help_event_p;
84
85 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
86 static Lisp_Object last_window;
87
88 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
89 int x_use_underline_position_properties;
90
91 /* Non-zero means to draw the underline at the same place as the descent line. */
92
93 int x_underline_at_descent_line;
94
95 extern unsigned int msh_mousewheel;
96
97 extern void free_frame_menubar ();
98
99 extern int w32_codepage_for_font (char *fontname);
100 extern Cursor w32_load_cursor (LPCTSTR name);
101
102 extern Lisp_Object Vwindow_system;
103
104 #define x_any_window_to_frame x_window_to_frame
105 #define x_top_window_to_frame x_window_to_frame
106
107 \f
108 /* This is display since w32 does not support multiple ones. */
109 struct w32_display_info one_w32_display_info;
110 struct w32_display_info *x_display_list;
111
112 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
113 one for each element of w32_display_list and in the same order.
114 NAME is the name of the frame.
115 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
116 Lisp_Object w32_display_name_list;
117
118
119 #ifndef GLYPHSET
120 /* Pre Windows 2000, this was not available, but define it here so
121 that Emacs compiled on such a platform will run on newer versions. */
122
123 typedef struct tagWCRANGE
124 {
125 WCHAR wcLow;
126 USHORT cGlyphs;
127 } WCRANGE;
128
129 typedef struct tagGLYPHSET
130 {
131 DWORD cbThis;
132 DWORD flAccel;
133 DWORD cGlyphsSupported;
134 DWORD cRanges;
135 WCRANGE ranges[1];
136 } GLYPHSET;
137
138 #endif
139
140 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
141 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
142
143 #ifndef LWA_ALPHA
144 #define LWA_ALPHA 0x02
145 #endif
146 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
147 later targets by MSVC headers. */
148 #ifndef WS_EX_LAYERED
149 #define WS_EX_LAYERED 0x80000
150 #endif
151
152 /* Frame being updated by update_frame. This is declared in term.c.
153 This is set by update_begin and looked at by all the
154 w32 functions. It is zero while not inside an update.
155 In that case, the w32 functions assume that `SELECTED_FRAME ()'
156 is the frame to apply to. */
157 extern struct frame *updating_frame;
158
159 /* This is a frame waiting to be autoraised, within w32_read_socket. */
160 struct frame *pending_autoraise_frame;
161
162 /* The handle of the frame that currently owns the system caret. */
163 HWND w32_system_caret_hwnd;
164 int w32_system_caret_height;
165 int w32_system_caret_x;
166 int w32_system_caret_y;
167 int w32_use_visible_system_caret;
168
169 DWORD dwWindowsThreadId = 0;
170 HANDLE hWindowsThread = NULL;
171 DWORD dwMainThreadId = 0;
172 HANDLE hMainThread = NULL;
173
174 int vertical_scroll_bar_min_handle;
175 int vertical_scroll_bar_top_border;
176 int vertical_scroll_bar_bottom_border;
177
178 int last_scroll_bar_drag_pos;
179
180 /* Mouse movement. */
181
182 /* Where the mouse was last time we reported a mouse event. */
183 static RECT last_mouse_glyph;
184 static FRAME_PTR last_mouse_glyph_frame;
185 static Lisp_Object last_mouse_press_frame;
186
187 int w32_num_mouse_buttons;
188
189 Lisp_Object Vw32_swap_mouse_buttons;
190
191 /* Control whether x_raise_frame also sets input focus. */
192 Lisp_Object Vw32_grab_focus_on_raise;
193
194 /* Control whether Caps Lock affects non-ascii characters. */
195 Lisp_Object Vw32_capslock_is_shiftlock;
196
197 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
198 Lisp_Object Vw32_recognize_altgr;
199
200 /* The scroll bar in which the last motion event occurred.
201
202 If the last motion event occurred in a scroll bar, we set this
203 so w32_mouse_position can know whether to report a scroll bar motion or
204 an ordinary motion.
205
206 If the last motion event didn't occur in a scroll bar, we set this
207 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
208 static Lisp_Object last_mouse_scroll_bar;
209 static int last_mouse_scroll_bar_pos;
210
211 /* This is a hack. We would really prefer that w32_mouse_position would
212 return the time associated with the position it returns, but there
213 doesn't seem to be any way to wrest the time-stamp from the server
214 along with the position query. So, we just keep track of the time
215 of the last movement we received, and return that in hopes that
216 it's somewhat accurate. */
217 static Time last_mouse_movement_time;
218
219 /* Incremented by w32_read_socket whenever it really tries to read
220 events. */
221 #ifdef __STDC__
222 static int volatile input_signal_count;
223 #else
224 static int input_signal_count;
225 #endif
226
227 extern Lisp_Object Vcommand_line_args, Vsystem_name;
228
229 #ifndef USE_CRT_DLL
230 extern int errno;
231 #endif
232
233 /* A mask of extra modifier bits to put into every keyboard char. */
234 extern EMACS_INT extra_keyboard_modifiers;
235
236 /* Keyboard code page - may be changed by language-change events. */
237 static int keyboard_codepage;
238
239 static void x_update_window_end P_ ((struct window *, int, int));
240 static void w32_handle_tool_bar_click P_ ((struct frame *,
241 struct input_event *));
242 static void w32_define_cursor P_ ((Window, Cursor));
243
244 void x_lower_frame P_ ((struct frame *));
245 void x_scroll_bar_clear P_ ((struct frame *));
246 void x_wm_set_size_hint P_ ((struct frame *, long, int));
247 void x_raise_frame P_ ((struct frame *));
248 void x_set_window_size P_ ((struct frame *, int, int, int));
249 void x_wm_set_window_state P_ ((struct frame *, int));
250 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
251 static void w32_initialize P_ ((void));
252 static void x_update_end P_ ((struct frame *));
253 static void w32_frame_up_to_date P_ ((struct frame *));
254 static void w32_set_terminal_modes P_ ((struct terminal *));
255 static void w32_reset_terminal_modes P_ ((struct terminal *));
256 static void x_clear_frame P_ ((struct frame *));
257 static void frame_highlight P_ ((struct frame *));
258 static void frame_unhighlight P_ ((struct frame *));
259 static void x_new_focus_frame P_ ((struct w32_display_info *,
260 struct frame *));
261 static void x_focus_changed P_ ((int, int, struct w32_display_info *,
262 struct frame *, struct input_event *));
263 static void w32_detect_focus_change P_ ((struct w32_display_info *,
264 W32Msg *, struct input_event *));
265 static void w32_frame_rehighlight P_ ((struct frame *));
266 static void x_frame_rehighlight P_ ((struct w32_display_info *));
267 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
268 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
269 enum text_cursor_kinds));
270 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *, int, HDC));
271 static BOOL my_show_window P_ ((struct frame *, HWND, int));
272 static void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
273 static void my_set_focus P_ ((struct frame *, HWND));
274 static void my_set_foreground_window P_ ((HWND));
275 static void my_destroy_window P_ ((struct frame *, HWND));
276
277 static Lisp_Object Qvendor_specific_keysyms;
278
279 \f
280 /***********************************************************************
281 Debugging
282 ***********************************************************************/
283
284 #if 0
285
286 /* This is a function useful for recording debugging information about
287 the sequence of occurrences in this file. */
288
289 struct record
290 {
291 char *locus;
292 int type;
293 };
294
295 struct record event_record[100];
296
297 int event_record_index;
298
299 record_event (locus, type)
300 char *locus;
301 int type;
302 {
303 if (event_record_index == sizeof (event_record) / sizeof (struct record))
304 event_record_index = 0;
305
306 event_record[event_record_index].locus = locus;
307 event_record[event_record_index].type = type;
308 event_record_index++;
309 }
310
311 #endif /* 0 */
312 \f
313
314 void
315 XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
316 XGCValues *xgcv)
317 {
318 if (mask & GCForeground)
319 gc->foreground = xgcv->foreground;
320 if (mask & GCBackground)
321 gc->background = xgcv->background;
322 if (mask & GCFont)
323 gc->font = xgcv->font;
324 }
325
326 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
327 XGCValues *xgcv)
328 {
329 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
330 bzero (gc, sizeof (XGCValues));
331
332 XChangeGC (ignore, gc, mask, xgcv);
333
334 return gc;
335 }
336
337 void
338 XGetGCValues (void* ignore, XGCValues *gc,
339 unsigned long mask, XGCValues *xgcv)
340 {
341 XChangeGC (ignore, xgcv, mask, gc);
342 }
343
344 static void
345 w32_set_clip_rectangle (HDC hdc, RECT *rect)
346 {
347 if (rect)
348 {
349 HRGN clip_region = CreateRectRgnIndirect (rect);
350 SelectClipRgn (hdc, clip_region);
351 DeleteObject (clip_region);
352 }
353 else
354 SelectClipRgn (hdc, NULL);
355 }
356
357
358 /* Draw a hollow rectangle at the specified position. */
359 void
360 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
361 int width, int height)
362 {
363 HBRUSH hb, oldhb;
364 HPEN hp, oldhp;
365
366 hb = CreateSolidBrush (gc->background);
367 hp = CreatePen (PS_SOLID, 0, gc->foreground);
368 oldhb = SelectObject (hdc, hb);
369 oldhp = SelectObject (hdc, hp);
370
371 Rectangle (hdc, x, y, x + width, y + height);
372
373 SelectObject (hdc, oldhb);
374 SelectObject (hdc, oldhp);
375 DeleteObject (hb);
376 DeleteObject (hp);
377 }
378
379 /* Draw a filled rectangle at the specified position. */
380 void
381 w32_fill_rect (f, hdc, pix, lprect)
382 FRAME_PTR f;
383 HDC hdc;
384 COLORREF pix;
385 RECT * lprect;
386 {
387 HBRUSH hb;
388
389 hb = CreateSolidBrush (pix);
390 FillRect (hdc, lprect, hb);
391 DeleteObject (hb);
392 }
393
394 void
395 w32_clear_window (f)
396 FRAME_PTR f;
397 {
398 RECT rect;
399 HDC hdc = get_frame_dc (f);
400
401 /* Under certain conditions, this can be called at startup with
402 a console frame pointer before the GUI frame is created. An HDC
403 of 0 indicates this. */
404 if (hdc)
405 {
406 GetClientRect (FRAME_W32_WINDOW (f), &rect);
407 w32_clear_rect (f, hdc, &rect);
408 }
409
410 release_frame_dc (f, hdc);
411 }
412
413 #define OPAQUE_FRAME 255
414
415 void
416 x_set_frame_alpha (f)
417 struct frame *f;
418 {
419 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
420 double alpha = 1.0;
421 double alpha_min = 1.0;
422 BYTE opac;
423 LONG ex_style;
424 HWND window = FRAME_W32_WINDOW (f);
425
426 /* Older versions of Windows do not support transparency. */
427 if (!pfnSetLayeredWindowAttributes)
428 return;
429
430 if (dpyinfo->x_highlight_frame == f)
431 alpha = f->alpha[0];
432 else
433 alpha = f->alpha[1];
434
435 if (FLOATP (Vframe_alpha_lower_limit))
436 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
437 else if (INTEGERP (Vframe_alpha_lower_limit))
438 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
439
440 if (alpha < 0.0)
441 return;
442 else if (alpha > 1.0)
443 alpha = 1.0;
444 else if (alpha < alpha_min && alpha_min <= 1.0)
445 alpha = alpha_min;
446
447 opac = alpha * OPAQUE_FRAME;
448
449 ex_style = GetWindowLong (window, GWL_EXSTYLE);
450
451 if (opac == OPAQUE_FRAME)
452 ex_style &= ~WS_EX_LAYERED;
453 else
454 ex_style |= WS_EX_LAYERED;
455
456 SetWindowLong (window, GWL_EXSTYLE, ex_style);
457
458 if (opac != OPAQUE_FRAME)
459 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
460 }
461
462 int
463 x_display_pixel_height (dpyinfo)
464 struct w32_display_info *dpyinfo;
465 {
466 HDC dc = GetDC (NULL);
467 int pixels = GetDeviceCaps (dc, VERTRES);
468 ReleaseDC (NULL, dc);
469 return pixels;
470 }
471
472 int
473 x_display_pixel_width (dpyinfo)
474 struct w32_display_info *dpyinfo;
475 {
476 HDC dc = GetDC (NULL);
477 int pixels = GetDeviceCaps (dc, HORZRES);
478 ReleaseDC (NULL, dc);
479 return pixels;
480 }
481
482 \f
483 /***********************************************************************
484 Starting and ending an update
485 ***********************************************************************/
486
487 /* Start an update of frame F. This function is installed as a hook
488 for update_begin, i.e. it is called when update_begin is called.
489 This function is called prior to calls to x_update_window_begin for
490 each window being updated. */
491
492 static void
493 x_update_begin (f)
494 struct frame *f;
495 {
496 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
497
498 if (! FRAME_W32_P (f))
499 return;
500
501 /* Regenerate display palette before drawing if list of requested
502 colors has changed. */
503 if (display_info->regen_palette)
504 {
505 w32_regenerate_palette (f);
506 display_info->regen_palette = FALSE;
507 }
508 }
509
510
511 /* Start update of window W. Set the global variable updated_window
512 to the window being updated and set output_cursor to the cursor
513 position of W. */
514
515 static void
516 x_update_window_begin (w)
517 struct window *w;
518 {
519 struct frame *f = XFRAME (WINDOW_FRAME (w));
520 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
521
522 /* Hide the system caret during an update. */
523 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
524 {
525 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
526 }
527
528 updated_window = w;
529 set_output_cursor (&w->cursor);
530
531 BLOCK_INPUT;
532
533 if (f == display_info->mouse_face_mouse_frame)
534 {
535 /* Don't do highlighting for mouse motion during the update. */
536 display_info->mouse_face_defer = 1;
537
538 /* If F needs to be redrawn, simply forget about any prior mouse
539 highlighting. */
540 if (FRAME_GARBAGED_P (f))
541 display_info->mouse_face_window = Qnil;
542
543 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
544 their mouse_face_p flag set, which means that they are always
545 unequal to rows in a desired matrix which never have that
546 flag set. So, rows containing mouse-face glyphs are never
547 scrolled, and we don't have to switch the mouse highlight off
548 here to prevent it from being scrolled. */
549
550 /* Can we tell that this update does not affect the window
551 where the mouse highlight is? If so, no need to turn off.
552 Likewise, don't do anything if the frame is garbaged;
553 in that case, the frame's current matrix that we would use
554 is all wrong, and we will redisplay that line anyway. */
555 if (!NILP (display_info->mouse_face_window)
556 && w == XWINDOW (display_info->mouse_face_window))
557 {
558 int i;
559
560 for (i = 0; i < w->desired_matrix->nrows; ++i)
561 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
562 break;
563
564 if (i < w->desired_matrix->nrows)
565 clear_mouse_face (display_info);
566 }
567 #endif /* 0 */
568 }
569
570 UNBLOCK_INPUT;
571 }
572
573 /* Draw a vertical window border from (x,y0) to (x,y1) */
574
575 static void
576 w32_draw_vertical_window_border (w, x, y0, y1)
577 struct window *w;
578 int x, y0, y1;
579 {
580 struct frame *f = XFRAME (WINDOW_FRAME (w));
581 RECT r;
582 HDC hdc;
583 struct face *face;
584
585 r.left = x;
586 r.right = x + 1;
587 r.top = y0;
588 r.bottom = y1;
589
590 hdc = get_frame_dc (f);
591 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
592 if (face)
593 w32_fill_rect (f, hdc, face->foreground, &r);
594 else
595 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
596
597 release_frame_dc (f, hdc);
598 }
599
600
601 /* End update of window W (which is equal to updated_window).
602
603 Draw vertical borders between horizontally adjacent windows, and
604 display W's cursor if CURSOR_ON_P is non-zero.
605
606 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
607 glyphs in mouse-face were overwritten. In that case we have to
608 make sure that the mouse-highlight is properly redrawn.
609
610 W may be a menu bar pseudo-window in case we don't have X toolkit
611 support. Such windows don't have a cursor, so don't display it
612 here. */
613
614 static void
615 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
616 struct window *w;
617 int cursor_on_p, mouse_face_overwritten_p;
618 {
619 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
620
621 if (!w->pseudo_window_p)
622 {
623 BLOCK_INPUT;
624
625 if (cursor_on_p)
626 display_and_set_cursor (w, 1, output_cursor.hpos,
627 output_cursor.vpos,
628 output_cursor.x, output_cursor.y);
629
630 if (draw_window_fringes (w, 1))
631 x_draw_vertical_border (w);
632
633 UNBLOCK_INPUT;
634 }
635
636 /* If a row with mouse-face was overwritten, arrange for
637 XTframe_up_to_date to redisplay the mouse highlight. */
638 if (mouse_face_overwritten_p)
639 {
640 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
641 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
642 dpyinfo->mouse_face_window = Qnil;
643 }
644
645 /* Unhide the caret. This won't actually show the cursor, unless it
646 was visible before the corresponding call to HideCaret in
647 x_update_window_begin. */
648 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
649 {
650 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
651 }
652
653 updated_window = NULL;
654 }
655
656
657 /* End update of frame F. This function is installed as a hook in
658 update_end. */
659
660 static void
661 x_update_end (f)
662 struct frame *f;
663 {
664 if (! FRAME_W32_P (f))
665 return;
666
667 /* Mouse highlight may be displayed again. */
668 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
669 }
670
671
672 /* This function is called from various places in xdisp.c whenever a
673 complete update has been performed. The global variable
674 updated_window is not available here. */
675
676 static void
677 w32_frame_up_to_date (f)
678 struct frame *f;
679 {
680 if (FRAME_W32_P (f))
681 {
682 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
683
684 if (dpyinfo->mouse_face_deferred_gc
685 || f == dpyinfo->mouse_face_mouse_frame)
686 {
687 BLOCK_INPUT;
688 if (dpyinfo->mouse_face_mouse_frame)
689 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
690 dpyinfo->mouse_face_mouse_x,
691 dpyinfo->mouse_face_mouse_y);
692 dpyinfo->mouse_face_deferred_gc = 0;
693 UNBLOCK_INPUT;
694 }
695 }
696 }
697
698
699 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
700 arrow bitmaps, or clear the fringes if no bitmaps are required
701 before DESIRED_ROW is made current. The window being updated is
702 found in updated_window. This function is called from
703 update_window_line only if it is known that there are differences
704 between bitmaps to be drawn between current row and DESIRED_ROW. */
705
706 static void
707 x_after_update_window_line (desired_row)
708 struct glyph_row *desired_row;
709 {
710 struct window *w = updated_window;
711 struct frame *f;
712 int width, height;
713
714 xassert (w);
715
716 if (!desired_row->mode_line_p && !w->pseudo_window_p)
717 desired_row->redraw_fringe_bitmaps_p = 1;
718
719 /* When a window has disappeared, make sure that no rest of
720 full-width rows stays visible in the internal border. Could
721 check here if updated_window is the leftmost/rightmost window,
722 but I guess it's not worth doing since vertically split windows
723 are almost never used, internal border is rarely set, and the
724 overhead is very small. */
725 if (windows_or_buffers_changed
726 && desired_row->full_width_p
727 && (f = XFRAME (w->frame),
728 width = FRAME_INTERNAL_BORDER_WIDTH (f),
729 width != 0)
730 && (height = desired_row->visible_height,
731 height > 0))
732 {
733 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
734
735 /* Internal border is drawn below the tool bar. */
736 if (WINDOWP (f->tool_bar_window)
737 && w == XWINDOW (f->tool_bar_window))
738 y -= width;
739
740 BLOCK_INPUT;
741 {
742 HDC hdc = get_frame_dc (f);
743 w32_clear_area (f, hdc, 0, y, width, height);
744 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
745 y, width, height);
746 release_frame_dc (f, hdc);
747 }
748 UNBLOCK_INPUT;
749 }
750 }
751
752
753 /* Draw the bitmap WHICH in one of the left or right fringes of
754 window W. ROW is the glyph row for which to display the bitmap; it
755 determines the vertical position at which the bitmap has to be
756 drawn. */
757
758 static void
759 w32_draw_fringe_bitmap (w, row, p)
760 struct window *w;
761 struct glyph_row *row;
762 struct draw_fringe_bitmap_params *p;
763 {
764 struct frame *f = XFRAME (WINDOW_FRAME (w));
765 HDC hdc;
766 struct face *face = p->face;
767 int rowY;
768
769 hdc = get_frame_dc (f);
770
771 if (!p->overlay_p)
772 {
773 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
774
775 /* If the fringe is adjacent to the left (right) scroll bar of a
776 leftmost (rightmost, respectively) window, then extend its
777 background to the gap between the fringe and the bar. */
778 if ((WINDOW_LEFTMOST_P (w)
779 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
780 || (WINDOW_RIGHTMOST_P (w)
781 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
782 {
783 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
784
785 if (sb_width > 0)
786 {
787 int left = WINDOW_SCROLL_BAR_AREA_X (w);
788 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
789 * FRAME_COLUMN_WIDTH (f));
790
791 if (bx < 0)
792 {
793 /* Bitmap fills the fringe. */
794 if (left + width == p->x)
795 bx = left + sb_width;
796 else if (p->x + p->wd == left)
797 bx = left;
798 if (bx >= 0)
799 {
800 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
801
802 nx = width - sb_width;
803 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
804 row->y));
805 ny = row->visible_height;
806 }
807 }
808 else
809 {
810 if (left + width == bx)
811 {
812 bx = left + sb_width;
813 nx += width - sb_width;
814 }
815 else if (bx + nx == left)
816 nx += width - sb_width;
817 }
818 }
819 }
820
821 if (bx >= 0 && nx > 0)
822 w32_fill_area (f, hdc, face->background, bx, by, nx, ny);
823 }
824
825 /* Must clip because of partially visible lines. */
826 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
827 if (p->y < rowY)
828 {
829 /* Adjust position of "bottom aligned" bitmap on partially
830 visible last row. */
831 int oldY = row->y;
832 int oldVH = row->visible_height;
833 row->visible_height = p->h;
834 row->y -= rowY - p->y;
835 w32_clip_to_row (w, row, -1, hdc);
836 row->y = oldY;
837 row->visible_height = oldVH;
838 }
839 else
840 w32_clip_to_row (w, row, -1, hdc);
841
842 if (p->which && p->which < max_fringe_bmp)
843 {
844 HBITMAP pixmap = fringe_bmp[p->which];
845 HDC compat_hdc;
846 HANDLE horig_obj;
847
848 compat_hdc = CreateCompatibleDC (hdc);
849
850 SaveDC (hdc);
851
852 horig_obj = SelectObject (compat_hdc, pixmap);
853
854 /* Paint overlays transparently. */
855 if (p->overlay_p)
856 {
857 HBRUSH h_brush, h_orig_brush;
858
859 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
860 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
861 h_brush = CreateSolidBrush (face->foreground);
862 h_orig_brush = SelectObject (hdc, h_brush);
863
864 BitBlt (hdc, p->x, p->y, p->wd, p->h,
865 compat_hdc, 0, p->dh,
866 DSTINVERT);
867 BitBlt (hdc, p->x, p->y, p->wd, p->h,
868 compat_hdc, 0, p->dh,
869 0x2E064A);
870 BitBlt (hdc, p->x, p->y, p->wd, p->h,
871 compat_hdc, 0, p->dh,
872 DSTINVERT);
873
874 SelectObject (hdc, h_orig_brush);
875 DeleteObject (h_brush);
876 }
877 else
878 {
879 SetTextColor (hdc, face->background);
880 SetBkColor (hdc, (p->cursor_p
881 ? f->output_data.w32->cursor_pixel
882 : face->foreground));
883
884 BitBlt (hdc, p->x, p->y, p->wd, p->h,
885 compat_hdc, 0, p->dh,
886 SRCCOPY);
887 }
888
889 SelectObject (compat_hdc, horig_obj);
890 DeleteDC (compat_hdc);
891 RestoreDC (hdc, -1);
892 }
893
894 w32_set_clip_rectangle (hdc, NULL);
895
896 release_frame_dc (f, hdc);
897 }
898
899 static void
900 w32_define_fringe_bitmap (which, bits, h, wd)
901 int which;
902 unsigned short *bits;
903 int h, wd;
904 {
905 if (which >= max_fringe_bmp)
906 {
907 int i = max_fringe_bmp;
908 max_fringe_bmp = which + 20;
909 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
910 while (i < max_fringe_bmp)
911 fringe_bmp[i++] = 0;
912 }
913
914 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
915 }
916
917 static void
918 w32_destroy_fringe_bitmap (which)
919 int which;
920 {
921 if (which >= max_fringe_bmp)
922 return;
923
924 if (fringe_bmp[which])
925 DeleteObject (fringe_bmp[which]);
926 fringe_bmp[which] = 0;
927 }
928
929
930 \f
931 /* This is called when starting Emacs and when restarting after
932 suspend. When starting Emacs, no window is mapped. And nothing
933 must be done to Emacs's own window if it is suspended (though that
934 rarely happens). */
935
936 static void
937 w32_set_terminal_modes (struct terminal *term)
938 {
939 }
940
941 /* This is called when exiting or suspending Emacs. Exiting will make
942 the W32 windows go away, and suspending requires no action. */
943
944 static void
945 w32_reset_terminal_modes (struct terminal *term)
946 {
947 }
948
949
950 \f
951 /***********************************************************************
952 Display Iterator
953 ***********************************************************************/
954
955 /* Function prototypes of this page. */
956
957 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
958 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
959 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
960 int));
961 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
962 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
963 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
964 static void x_draw_glyph_string P_ ((struct glyph_string *));
965 static void x_set_cursor_gc P_ ((struct glyph_string *));
966 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
967 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
968 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
969 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
970 double, int, COLORREF));
971 static void x_setup_relief_colors P_ ((struct glyph_string *));
972 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
973 static void x_draw_image_relief P_ ((struct glyph_string *));
974 static void x_draw_image_foreground P_ ((struct glyph_string *));
975 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
976 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
977 int, int, int));
978 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
979 int, int, int, int, int, int,
980 RECT *));
981 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
982 int, int, int, RECT *));
983
984
985 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
986 face. */
987
988 static void
989 x_set_cursor_gc (s)
990 struct glyph_string *s;
991 {
992 if (s->font == FRAME_FONT (s->f)
993 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
994 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
995 && !s->cmp)
996 s->gc = s->f->output_data.w32->cursor_gc;
997 else
998 {
999 /* Cursor on non-default face: must merge. */
1000 XGCValues xgcv;
1001 unsigned long mask;
1002
1003 xgcv.background = s->f->output_data.w32->cursor_pixel;
1004 xgcv.foreground = s->face->background;
1005
1006 /* If the glyph would be invisible, try a different foreground. */
1007 if (xgcv.foreground == xgcv.background)
1008 xgcv.foreground = s->face->foreground;
1009 if (xgcv.foreground == xgcv.background)
1010 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
1011 if (xgcv.foreground == xgcv.background)
1012 xgcv.foreground = s->face->foreground;
1013
1014 /* Make sure the cursor is distinct from text in this face. */
1015 if (xgcv.background == s->face->background
1016 && xgcv.foreground == s->face->foreground)
1017 {
1018 xgcv.background = s->face->foreground;
1019 xgcv.foreground = s->face->background;
1020 }
1021
1022 IF_DEBUG (x_check_font (s->f, s->font));
1023 xgcv.font = s->font;
1024 mask = GCForeground | GCBackground | GCFont;
1025
1026 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1027 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1028 mask, &xgcv);
1029 else
1030 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1031 = XCreateGC (NULL, s->window, mask, &xgcv);
1032
1033 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1034 }
1035 }
1036
1037
1038 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1039
1040 static void
1041 x_set_mouse_face_gc (s)
1042 struct glyph_string *s;
1043 {
1044 int face_id;
1045 struct face *face;
1046
1047 /* What face has to be used last for the mouse face? */
1048 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
1049 face = FACE_FROM_ID (s->f, face_id);
1050 if (face == NULL)
1051 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1052
1053 if (s->first_glyph->type == CHAR_GLYPH)
1054 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1055 else
1056 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1057 s->face = FACE_FROM_ID (s->f, face_id);
1058 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1059
1060 /* If font in this face is same as S->font, use it. */
1061 if (s->font == s->face->font)
1062 s->gc = s->face->gc;
1063 else
1064 {
1065 /* Otherwise construct scratch_cursor_gc with values from FACE
1066 but font FONT. */
1067 XGCValues xgcv;
1068 unsigned long mask;
1069
1070 xgcv.background = s->face->background;
1071 xgcv.foreground = s->face->foreground;
1072 IF_DEBUG (x_check_font (s->f, s->font));
1073 xgcv.font = s->font;
1074 mask = GCForeground | GCBackground | GCFont;
1075
1076 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1077 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1078 mask, &xgcv);
1079 else
1080 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1081 = XCreateGC (NULL, s->window, mask, &xgcv);
1082
1083 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1084 }
1085
1086 xassert (s->gc != 0);
1087 }
1088
1089
1090 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1091 Faces to use in the mode line have already been computed when the
1092 matrix was built, so there isn't much to do, here. */
1093
1094 static INLINE void
1095 x_set_mode_line_face_gc (s)
1096 struct glyph_string *s;
1097 {
1098 s->gc = s->face->gc;
1099 }
1100
1101
1102 /* Set S->gc of glyph string S for drawing that glyph string. Set
1103 S->stippled_p to a non-zero value if the face of S has a stipple
1104 pattern. */
1105
1106 static INLINE void
1107 x_set_glyph_string_gc (s)
1108 struct glyph_string *s;
1109 {
1110 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1111
1112 if (s->hl == DRAW_NORMAL_TEXT)
1113 {
1114 s->gc = s->face->gc;
1115 s->stippled_p = s->face->stipple != 0;
1116 }
1117 else if (s->hl == DRAW_INVERSE_VIDEO)
1118 {
1119 x_set_mode_line_face_gc (s);
1120 s->stippled_p = s->face->stipple != 0;
1121 }
1122 else if (s->hl == DRAW_CURSOR)
1123 {
1124 x_set_cursor_gc (s);
1125 s->stippled_p = 0;
1126 }
1127 else if (s->hl == DRAW_MOUSE_FACE)
1128 {
1129 x_set_mouse_face_gc (s);
1130 s->stippled_p = s->face->stipple != 0;
1131 }
1132 else if (s->hl == DRAW_IMAGE_RAISED
1133 || s->hl == DRAW_IMAGE_SUNKEN)
1134 {
1135 s->gc = s->face->gc;
1136 s->stippled_p = s->face->stipple != 0;
1137 }
1138 else
1139 {
1140 s->gc = s->face->gc;
1141 s->stippled_p = s->face->stipple != 0;
1142 }
1143
1144 /* GC must have been set. */
1145 xassert (s->gc != 0);
1146 }
1147
1148
1149 /* Set clipping for output of glyph string S. S may be part of a mode
1150 line or menu if we don't have X toolkit support. */
1151
1152 static INLINE void
1153 x_set_glyph_string_clipping (s)
1154 struct glyph_string *s;
1155 {
1156 RECT *r = s->clip;
1157 int n = get_glyph_string_clip_rects (s, r, 2);
1158
1159 if (n == 1)
1160 w32_set_clip_rectangle (s->hdc, r);
1161 else if (n > 1)
1162 {
1163 HRGN full_clip, clip1, clip2;
1164 clip1 = CreateRectRgnIndirect (r);
1165 clip2 = CreateRectRgnIndirect (r + 1);
1166 if (CombineRgn (full_clip, clip1, clip2, RGN_OR) != ERROR)
1167 {
1168 SelectClipRgn (s->hdc, full_clip);
1169 }
1170 DeleteObject (clip1);
1171 DeleteObject (clip2);
1172 DeleteObject (full_clip);
1173 }
1174 s->num_clips = n;
1175 }
1176
1177 /* Set SRC's clipping for output of glyph string DST. This is called
1178 when we are drawing DST's left_overhang or right_overhang only in
1179 the area of SRC. */
1180
1181 static void
1182 x_set_glyph_string_clipping_exactly (src, dst)
1183 struct glyph_string *src, *dst;
1184 {
1185 RECT r;
1186
1187 r.left = src->x;
1188 r.right = r.left + src->width;
1189 r.top = src->y;
1190 r.bottom = r.top + src->height;
1191 dst->clip[0] = r;
1192 dst->num_clips = 1;
1193 w32_set_clip_rectangle (dst->hdc, &r);
1194 }
1195
1196 /* RIF:
1197 Compute left and right overhang of glyph string S. */
1198
1199 static void
1200 w32_compute_glyph_string_overhangs (s)
1201 struct glyph_string *s;
1202 {
1203 if (s->cmp == NULL
1204 && s->first_glyph->type == CHAR_GLYPH
1205 && !s->font_not_found_p)
1206 {
1207 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1208 struct font *font = s->font;
1209 struct font_metrics metrics;
1210 int i;
1211
1212 for (i = 0; i < s->nchars; i++)
1213 code[i] = s->char2b[i];
1214 font->driver->text_extents (font, code, s->nchars, &metrics);
1215 s->right_overhang = (metrics.rbearing > metrics.width
1216 ? metrics.rbearing - metrics.width : 0);
1217 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1218 }
1219 else if (s->cmp)
1220 {
1221 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1222 s->left_overhang = -s->cmp->lbearing;
1223 }
1224 }
1225
1226 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1227
1228 static INLINE void
1229 x_clear_glyph_string_rect (s, x, y, w, h)
1230 struct glyph_string *s;
1231 int x, y, w, h;
1232 {
1233 int real_x = x;
1234 int real_y = y;
1235 int real_w = w;
1236 int real_h = h;
1237 #if 0
1238 /* Take clipping into account. */
1239 if (s->gc->clip_mask == Rect)
1240 {
1241 real_x = max (real_x, s->gc->clip_rectangle.left);
1242 real_y = max (real_y, s->gc->clip_rectangle.top);
1243 real_w = min (real_w, s->gc->clip_rectangle.right
1244 - s->gc->clip_rectangle.left);
1245 real_h = min (real_h, s->gc->clip_rectangle.bottom
1246 - s->gc->clip_rectangle.top);
1247 }
1248 #endif
1249 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1250 real_w, real_h);
1251 }
1252
1253
1254 /* Draw the background of glyph_string S. If S->background_filled_p
1255 is non-zero don't draw it. FORCE_P non-zero means draw the
1256 background even if it wouldn't be drawn normally. This is used
1257 when a string preceding S draws into the background of S, or S
1258 contains the first component of a composition. */
1259
1260 static void
1261 x_draw_glyph_string_background (s, force_p)
1262 struct glyph_string *s;
1263 int force_p;
1264 {
1265 /* Nothing to do if background has already been drawn or if it
1266 shouldn't be drawn in the first place. */
1267 if (!s->background_filled_p)
1268 {
1269 int box_line_width = max (s->face->box_line_width, 0);
1270
1271 #if 0 /* TODO: stipple */
1272 if (s->stippled_p)
1273 {
1274 /* Fill background with a stipple pattern. */
1275 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1276 XFillRectangle (s->display, s->window, s->gc, s->x,
1277 s->y + box_line_width,
1278 s->background_width,
1279 s->height - 2 * box_line_width);
1280 XSetFillStyle (s->display, s->gc, FillSolid);
1281 s->background_filled_p = 1;
1282 }
1283 else
1284 #endif
1285 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1286 || s->font_not_found_p
1287 || s->extends_to_end_of_line_p
1288 || force_p)
1289 {
1290 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1291 s->background_width,
1292 s->height - 2 * box_line_width);
1293 s->background_filled_p = 1;
1294 }
1295 }
1296 }
1297
1298
1299 /* Draw the foreground of glyph string S. */
1300
1301 static void
1302 x_draw_glyph_string_foreground (s)
1303 struct glyph_string *s;
1304 {
1305 int i, x;
1306
1307 /* If first glyph of S has a left box line, start drawing the text
1308 of S to the right of that box line. */
1309 if (s->face->box != FACE_NO_BOX
1310 && s->first_glyph->left_box_line_p)
1311 x = s->x + eabs (s->face->box_line_width);
1312 else
1313 x = s->x;
1314
1315 SetTextColor (s->hdc, s->gc->foreground);
1316 SetBkColor (s->hdc, s->gc->background);
1317 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1318
1319 /* Draw characters of S as rectangles if S's font could not be
1320 loaded. */
1321 if (s->font_not_found_p)
1322 {
1323 for (i = 0; i < s->nchars; ++i)
1324 {
1325 struct glyph *g = s->first_glyph + i;
1326
1327 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1328 s->height - 1);
1329 x += g->pixel_width;
1330 }
1331 }
1332 else
1333 {
1334 struct font *font = s->font;
1335 int boff = font->baseline_offset;
1336 int y;
1337 HFONT old_font;
1338
1339 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1340
1341 if (font->vertical_centering)
1342 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1343
1344 y = s->ybase - boff;
1345 if (s->for_overlaps
1346 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1347 font->driver->draw (s, 0, s->nchars, x, y, 0);
1348 else
1349 font->driver->draw (s, 0, s->nchars, x, y, 1);
1350 if (s->face->overstrike)
1351 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1352
1353 SelectObject (s->hdc, old_font);
1354 }
1355 }
1356
1357 /* Draw the foreground of composite glyph string S. */
1358
1359 static void
1360 x_draw_composite_glyph_string_foreground (s)
1361 struct glyph_string *s;
1362 {
1363 int i, j, x;
1364 struct font *font = s->font;
1365
1366 /* If first glyph of S has a left box line, start drawing the text
1367 of S to the right of that box line. */
1368 if (s->face && s->face->box != FACE_NO_BOX
1369 && s->first_glyph->left_box_line_p)
1370 x = s->x + eabs (s->face->box_line_width);
1371 else
1372 x = s->x;
1373
1374 /* S is a glyph string for a composition. S->cmp_from is the index
1375 of the first character drawn for glyphs of this composition.
1376 S->cmp_from == 0 means we are drawing the very first character of
1377 this composition. */
1378
1379 SetTextColor (s->hdc, s->gc->foreground);
1380 SetBkColor (s->hdc, s->gc->background);
1381 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1382
1383 /* Draw a rectangle for the composition if the font for the very
1384 first character of the composition could not be loaded. */
1385 if (s->font_not_found_p)
1386 {
1387 if (s->cmp_from == 0)
1388 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1389 s->height - 1);
1390 }
1391 else if (! s->first_glyph->u.cmp.automatic)
1392 {
1393 int y = s->ybase;
1394 int width = 0;
1395 HFONT old_font;
1396
1397 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1398
1399 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1400 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1401 {
1402 int xx = x + s->cmp->offsets[j * 2];
1403 int yy = y - s->cmp->offsets[j * 2 + 1];
1404
1405 font->driver->draw (s, j, j + 1, xx, yy, 0);
1406 if (s->face->overstrike)
1407 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1408 }
1409 SelectObject (s->hdc, old_font);
1410 }
1411 else
1412 {
1413 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1414 Lisp_Object glyph;
1415 int y = s->ybase;
1416 int width = 0;
1417 HFONT old_font;
1418
1419 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1420
1421 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1422 {
1423 glyph = LGSTRING_GLYPH (gstring, i);
1424 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1425 width += LGLYPH_WIDTH (glyph);
1426 else
1427 {
1428 int xoff, yoff, wadjust;
1429
1430 if (j < i)
1431 {
1432 font->driver->draw (s, j, i, x, y, 0);
1433 x += width;
1434 }
1435 xoff = LGLYPH_XOFF (glyph);
1436 yoff = LGLYPH_YOFF (glyph);
1437 wadjust = LGLYPH_WADJUST (glyph);
1438 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1439 x += wadjust;
1440 j = i + 1;
1441 width = 0;
1442 }
1443 }
1444 if (j < i)
1445 font->driver->draw (s, j, i, x, y, 0);
1446
1447 SelectObject (s->hdc, old_font);
1448 }
1449 }
1450
1451
1452 /* Brightness beyond which a color won't have its highlight brightness
1453 boosted.
1454
1455 Nominally, highlight colors for `3d' faces are calculated by
1456 brightening an object's color by a constant scale factor, but this
1457 doesn't yield good results for dark colors, so for colors who's
1458 brightness is less than this value (on a scale of 0-255) have to
1459 use an additional additive factor.
1460
1461 The value here is set so that the default menu-bar/mode-line color
1462 (grey75) will not have its highlights changed at all. */
1463 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1464
1465
1466 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1467 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1468 If this produces the same color as COLOR, try a color where all RGB
1469 values have DELTA added. Return the allocated color in *COLOR.
1470 DISPLAY is the X display, CMAP is the colormap to operate on.
1471 Value is non-zero if successful. */
1472
1473 static int
1474 w32_alloc_lighter_color (f, color, factor, delta)
1475 struct frame *f;
1476 COLORREF *color;
1477 double factor;
1478 int delta;
1479 {
1480 COLORREF new;
1481 long bright;
1482
1483 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1484 delta /= 256;
1485
1486 /* Change RGB values by specified FACTOR. Avoid overflow! */
1487 xassert (factor >= 0);
1488 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1489 min (0xff, factor * GetGValue (*color)),
1490 min (0xff, factor * GetBValue (*color)));
1491
1492 /* Calculate brightness of COLOR. */
1493 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1494 + GetBValue (*color)) / 6;
1495
1496 /* We only boost colors that are darker than
1497 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1498 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1499 /* Make an additive adjustment to NEW, because it's dark enough so
1500 that scaling by FACTOR alone isn't enough. */
1501 {
1502 /* How far below the limit this color is (0 - 1, 1 being darker). */
1503 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1504 /* The additive adjustment. */
1505 int min_delta = delta * dimness * factor / 2;
1506
1507 if (factor < 1)
1508 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1509 max (0, min (0xff, min_delta - GetGValue (*color))),
1510 max (0, min (0xff, min_delta - GetBValue (*color))));
1511 else
1512 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1513 max (0, min (0xff, min_delta + GetGValue (*color))),
1514 max (0, min (0xff, min_delta + GetBValue (*color))));
1515 }
1516
1517 if (new == *color)
1518 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1519 max (0, min (0xff, delta + GetGValue (*color))),
1520 max (0, min (0xff, delta + GetBValue (*color))));
1521
1522 /* TODO: Map to palette and retry with delta if same? */
1523 /* TODO: Free colors (if using palette)? */
1524
1525 if (new == *color)
1526 return 0;
1527
1528 *color = new;
1529
1530 return 1;
1531 }
1532
1533 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1534 colors in COLORS. On W32, we no longer try to map colors to
1535 a palette. */
1536 void
1537 x_query_colors (f, colors, ncolors)
1538 struct frame *f;
1539 XColor *colors;
1540 int ncolors;
1541 {
1542 int i;
1543
1544 for (i = 0; i < ncolors; i++)
1545 {
1546 DWORD pixel = colors[i].pixel;
1547 /* Convert to a 16 bit value in range 0 - 0xffff. */
1548 colors[i].red = GetRValue (pixel) * 257;
1549 colors[i].green = GetGValue (pixel) * 257;
1550 colors[i].blue = GetBValue (pixel) * 257;
1551 }
1552 }
1553
1554 void
1555 x_query_color (f, color)
1556 struct frame *f;
1557 XColor *color;
1558 {
1559 x_query_colors (f, color, 1);
1560 }
1561
1562
1563 /* Set up the foreground color for drawing relief lines of glyph
1564 string S. RELIEF is a pointer to a struct relief containing the GC
1565 with which lines will be drawn. Use a color that is FACTOR or
1566 DELTA lighter or darker than the relief's background which is found
1567 in S->f->output_data.x->relief_background. If such a color cannot
1568 be allocated, use DEFAULT_PIXEL, instead. */
1569
1570 static void
1571 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
1572 struct frame *f;
1573 struct relief *relief;
1574 double factor;
1575 int delta;
1576 COLORREF default_pixel;
1577 {
1578 XGCValues xgcv;
1579 struct w32_output *di = f->output_data.w32;
1580 unsigned long mask = GCForeground;
1581 COLORREF pixel;
1582 COLORREF background = di->relief_background;
1583 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
1584
1585 /* TODO: Free colors (if using palette)? */
1586
1587 /* Allocate new color. */
1588 xgcv.foreground = default_pixel;
1589 pixel = background;
1590 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1591 {
1592 relief->allocated_p = 1;
1593 xgcv.foreground = relief->pixel = pixel;
1594 }
1595
1596 if (relief->gc == 0)
1597 {
1598 #if 0 /* TODO: stipple */
1599 xgcv.stipple = dpyinfo->gray;
1600 mask |= GCStipple;
1601 #endif
1602 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1603 }
1604 else
1605 XChangeGC (NULL, relief->gc, mask, &xgcv);
1606 }
1607
1608
1609 /* Set up colors for the relief lines around glyph string S. */
1610
1611 static void
1612 x_setup_relief_colors (s)
1613 struct glyph_string *s;
1614 {
1615 struct w32_output *di = s->f->output_data.w32;
1616 COLORREF color;
1617
1618 if (s->face->use_box_color_for_shadows_p)
1619 color = s->face->box_color;
1620 else if (s->first_glyph->type == IMAGE_GLYPH
1621 && s->img->pixmap
1622 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1623 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1624 else
1625 color = s->gc->background;
1626
1627 if (di->white_relief.gc == 0
1628 || color != di->relief_background)
1629 {
1630 di->relief_background = color;
1631 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1632 WHITE_PIX_DEFAULT (s->f));
1633 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1634 BLACK_PIX_DEFAULT (s->f));
1635 }
1636 }
1637
1638
1639 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1640 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1641 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1642 relief. LEFT_P non-zero means draw a relief on the left side of
1643 the rectangle. RIGHT_P non-zero means draw a relief on the right
1644 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1645 when drawing. */
1646
1647 static void
1648 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1649 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
1650 struct frame *f;
1651 int left_x, top_y, right_x, bottom_y, width;
1652 int top_p, bot_p, left_p, right_p, raised_p;
1653 RECT *clip_rect;
1654 {
1655 int i;
1656 XGCValues gc;
1657 HDC hdc = get_frame_dc (f);
1658
1659 if (raised_p)
1660 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1661 else
1662 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1663
1664 w32_set_clip_rectangle (hdc, clip_rect);
1665
1666 /* Top. */
1667 if (top_p)
1668 for (i = 0; i < width; ++i)
1669 w32_fill_area (f, hdc, gc.foreground,
1670 left_x + i * left_p, top_y + i,
1671 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1672
1673 /* Left. */
1674 if (left_p)
1675 for (i = 0; i < width; ++i)
1676 w32_fill_area (f, hdc, gc.foreground,
1677 left_x + i, top_y + i, 1,
1678 bottom_y - top_y - 2 * i + 1);
1679
1680 if (raised_p)
1681 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1682 else
1683 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1684
1685 /* Bottom. */
1686 if (bot_p)
1687 for (i = 0; i < width; ++i)
1688 w32_fill_area (f, hdc, gc.foreground,
1689 left_x + i * left_p, bottom_y - i,
1690 right_x - left_x - i * (left_p + right_p) + 1, 1);
1691
1692 /* Right. */
1693 if (right_p)
1694 for (i = 0; i < width; ++i)
1695 w32_fill_area (f, hdc, gc.foreground,
1696 right_x - i, top_y + i + 1, 1,
1697 bottom_y - top_y - 2 * i - 1);
1698
1699 w32_set_clip_rectangle (hdc, NULL);
1700
1701 release_frame_dc (f, hdc);
1702 }
1703
1704
1705 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1706 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1707 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1708 left side of the rectangle. RIGHT_P non-zero means draw a line
1709 on the right side of the rectangle. CLIP_RECT is the clipping
1710 rectangle to use when drawing. */
1711
1712 static void
1713 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1714 left_p, right_p, clip_rect)
1715 struct glyph_string *s;
1716 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
1717 RECT *clip_rect;
1718 {
1719 w32_set_clip_rectangle (s->hdc, clip_rect);
1720
1721 /* Top. */
1722 w32_fill_area (s->f, s->hdc, s->face->box_color,
1723 left_x, top_y, right_x - left_x + 1, width);
1724
1725 /* Left. */
1726 if (left_p)
1727 {
1728 w32_fill_area (s->f, s->hdc, s->face->box_color,
1729 left_x, top_y, width, bottom_y - top_y + 1);
1730 }
1731
1732 /* Bottom. */
1733 w32_fill_area (s->f, s->hdc, s->face->box_color,
1734 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1735
1736 /* Right. */
1737 if (right_p)
1738 {
1739 w32_fill_area (s->f, s->hdc, s->face->box_color,
1740 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1741 }
1742
1743 w32_set_clip_rectangle (s->hdc, NULL);
1744 }
1745
1746
1747 /* Draw a box around glyph string S. */
1748
1749 static void
1750 x_draw_glyph_string_box (s)
1751 struct glyph_string *s;
1752 {
1753 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1754 int left_p, right_p;
1755 struct glyph *last_glyph;
1756 RECT clip_rect;
1757
1758 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1759 ? WINDOW_RIGHT_EDGE_X (s->w)
1760 : window_box_right (s->w, s->area));
1761
1762 /* The glyph that may have a right box line. */
1763 last_glyph = (s->cmp || s->img
1764 ? s->first_glyph
1765 : s->first_glyph + s->nchars - 1);
1766
1767 width = eabs (s->face->box_line_width);
1768 raised_p = s->face->box == FACE_RAISED_BOX;
1769 left_x = s->x;
1770 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1771 ? last_x - 1
1772 : min (last_x, s->x + s->background_width) - 1));
1773 top_y = s->y;
1774 bottom_y = top_y + s->height - 1;
1775
1776 left_p = (s->first_glyph->left_box_line_p
1777 || (s->hl == DRAW_MOUSE_FACE
1778 && (s->prev == NULL
1779 || s->prev->hl != s->hl)));
1780 right_p = (last_glyph->right_box_line_p
1781 || (s->hl == DRAW_MOUSE_FACE
1782 && (s->next == NULL
1783 || s->next->hl != s->hl)));
1784
1785 get_glyph_string_clip_rect (s, &clip_rect);
1786
1787 if (s->face->box == FACE_SIMPLE_BOX)
1788 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1789 left_p, right_p, &clip_rect);
1790 else
1791 {
1792 x_setup_relief_colors (s);
1793 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1794 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1795 }
1796 }
1797
1798
1799 /* Draw foreground of image glyph string S. */
1800
1801 static void
1802 x_draw_image_foreground (s)
1803 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 (s)
1894 struct glyph_string *s;
1895 {
1896 int x0, y0, x1, y1, thick, raised_p;
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 : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1919 raised_p = s->hl == DRAW_IMAGE_RAISED;
1920 }
1921 else
1922 {
1923 thick = eabs (s->img->relief);
1924 raised_p = s->img->relief > 0;
1925 }
1926
1927 x0 = x - thick;
1928 y0 = y - thick;
1929 x1 = x + s->slice.width + thick - 1;
1930 y1 = y + s->slice.height + thick - 1;
1931
1932 x_setup_relief_colors (s);
1933 get_glyph_string_clip_rect (s, &r);
1934 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
1935 s->slice.y == 0,
1936 s->slice.y + s->slice.height == s->img->height,
1937 s->slice.x == 0,
1938 s->slice.x + s->slice.width == s->img->width,
1939 &r);
1940 }
1941
1942
1943 /* Draw the foreground of image glyph string S to PIXMAP. */
1944
1945 static void
1946 w32_draw_image_foreground_1 (s, pixmap)
1947 struct glyph_string *s;
1948 HBITMAP pixmap;
1949 {
1950 HDC hdc = CreateCompatibleDC (s->hdc);
1951 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1952 int x = 0;
1953 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1954
1955 /* If first glyph of S has a left box line, start drawing it to the
1956 right of that line. */
1957 if (s->face->box != FACE_NO_BOX
1958 && s->first_glyph->left_box_line_p
1959 && s->slice.x == 0)
1960 x += eabs (s->face->box_line_width);
1961
1962 /* If there is a margin around the image, adjust x- and y-position
1963 by that margin. */
1964 if (s->slice.x == 0)
1965 x += s->img->hmargin;
1966 if (s->slice.y == 0)
1967 y += s->img->vmargin;
1968
1969 if (s->img->pixmap)
1970 {
1971 HDC compat_hdc = CreateCompatibleDC (hdc);
1972 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1973 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1974 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1975
1976 if (s->img->mask)
1977 {
1978 HDC mask_dc = CreateCompatibleDC (hdc);
1979 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1980
1981 SetTextColor (hdc, RGB (0, 0, 0));
1982 SetBkColor (hdc, RGB (255, 255, 255));
1983 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1984 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1985 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1986 mask_dc, s->slice.x, s->slice.y, SRCAND);
1987 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1988 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1989
1990 SelectObject (mask_dc, mask_orig_obj);
1991 DeleteDC (mask_dc);
1992 }
1993 else
1994 {
1995 SetTextColor (hdc, s->gc->foreground);
1996 SetBkColor (hdc, s->gc->background);
1997
1998 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1999 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2000
2001 /* When the image has a mask, we can expect that at
2002 least part of a mouse highlight or a block cursor will
2003 be visible. If the image doesn't have a mask, make
2004 a block cursor visible by drawing a rectangle around
2005 the image. I believe it's looking better if we do
2006 nothing here for mouse-face. */
2007 if (s->hl == DRAW_CURSOR)
2008 {
2009 int r = s->img->relief;
2010 if (r < 0) r = -r;
2011 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2012 s->slice.width + r*2 - 1,
2013 s->slice.height + r*2 - 1);
2014 }
2015 }
2016
2017 SelectObject (hdc, orig_brush);
2018 DeleteObject (fg_brush);
2019 SelectObject (compat_hdc, orig_obj);
2020 DeleteDC (compat_hdc);
2021 }
2022 else
2023 w32_draw_rectangle (hdc, s->gc, x, y,
2024 s->slice.width - 1, s->slice.height - 1);
2025
2026 SelectObject (hdc, orig_hdc_obj);
2027 DeleteDC (hdc);
2028 }
2029
2030
2031 /* Draw part of the background of glyph string S. X, Y, W, and H
2032 give the rectangle to draw. */
2033
2034 static void
2035 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2036 struct glyph_string *s;
2037 int x, y, w, h;
2038 {
2039 #if 0 /* TODO: stipple */
2040 if (s->stippled_p)
2041 {
2042 /* Fill background with a stipple pattern. */
2043 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2044 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2045 XSetFillStyle (s->display, s->gc, FillSolid);
2046 }
2047 else
2048 #endif
2049 x_clear_glyph_string_rect (s, x, y, w, h);
2050 }
2051
2052
2053 /* Draw image glyph string S.
2054
2055 s->y
2056 s->x +-------------------------
2057 | s->face->box
2058 |
2059 | +-------------------------
2060 | | s->img->vmargin
2061 | |
2062 | | +-------------------
2063 | | | the image
2064
2065 */
2066
2067 static void
2068 x_draw_image_glyph_string (s)
2069 struct glyph_string *s;
2070 {
2071 int x, y;
2072 int box_line_hwidth = eabs (s->face->box_line_width);
2073 int box_line_vwidth = max (s->face->box_line_width, 0);
2074 int height;
2075 HBITMAP pixmap = 0;
2076
2077 height = s->height - 2 * box_line_vwidth;
2078
2079 /* Fill background with face under the image. Do it only if row is
2080 taller than image or if image has a clip mask to reduce
2081 flickering. */
2082 s->stippled_p = s->face->stipple != 0;
2083 if (height > s->slice.height
2084 || s->img->hmargin
2085 || s->img->vmargin
2086 || s->img->mask
2087 || s->img->pixmap == 0
2088 || s->width != s->background_width)
2089 {
2090 x = s->x;
2091 if (s->first_glyph->left_box_line_p
2092 && s->slice.x == 0)
2093 x += box_line_hwidth;
2094
2095 y = s->y;
2096 if (s->slice.y == 0)
2097 y += box_line_vwidth;
2098
2099 #if 0 /* TODO: figure out if we need to do this on Windows. */
2100 if (s->img->mask)
2101 {
2102 /* Create a pixmap as large as the glyph string. Fill it
2103 with the background color. Copy the image to it, using
2104 its mask. Copy the temporary pixmap to the display. */
2105 Screen *screen = FRAME_X_SCREEN (s->f);
2106 int depth = DefaultDepthOfScreen (screen);
2107
2108 /* Create a pixmap as large as the glyph string. */
2109 pixmap = XCreatePixmap (s->display, s->window,
2110 s->background_width,
2111 s->height, depth);
2112
2113 /* Don't clip in the following because we're working on the
2114 pixmap. */
2115 XSetClipMask (s->display, s->gc, None);
2116
2117 /* Fill the pixmap with the background color/stipple. */
2118 if (s->stippled_p)
2119 {
2120 /* Fill background with a stipple pattern. */
2121 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2122 XFillRectangle (s->display, pixmap, s->gc,
2123 0, 0, s->background_width, s->height);
2124 XSetFillStyle (s->display, s->gc, FillSolid);
2125 }
2126 else
2127 {
2128 XGCValues xgcv;
2129 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2130 &xgcv);
2131 XSetForeground (s->display, s->gc, xgcv.background);
2132 XFillRectangle (s->display, pixmap, s->gc,
2133 0, 0, s->background_width, s->height);
2134 XSetForeground (s->display, s->gc, xgcv.foreground);
2135 }
2136 }
2137 else
2138 #endif
2139 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2140
2141 s->background_filled_p = 1;
2142 }
2143
2144 /* Draw the foreground. */
2145 if (pixmap != 0)
2146 {
2147 w32_draw_image_foreground_1 (s, pixmap);
2148 x_set_glyph_string_clipping (s);
2149 {
2150 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2151 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2152 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2153 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2154
2155 SetTextColor (s->hdc, s->gc->foreground);
2156 SetBkColor (s->hdc, s->gc->background);
2157 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2158 compat_hdc, 0, 0, SRCCOPY);
2159
2160 SelectObject (s->hdc, orig_brush);
2161 DeleteObject (fg_brush);
2162 SelectObject (compat_hdc, orig_obj);
2163 DeleteDC (compat_hdc);
2164 }
2165 DeleteObject (pixmap);
2166 pixmap = 0;
2167 }
2168 else
2169 x_draw_image_foreground (s);
2170
2171 /* If we must draw a relief around the image, do it. */
2172 if (s->img->relief
2173 || s->hl == DRAW_IMAGE_RAISED
2174 || s->hl == DRAW_IMAGE_SUNKEN)
2175 x_draw_image_relief (s);
2176 }
2177
2178
2179 /* Draw stretch glyph string S. */
2180
2181 static void
2182 x_draw_stretch_glyph_string (s)
2183 struct glyph_string *s;
2184 {
2185 xassert (s->first_glyph->type == STRETCH_GLYPH);
2186
2187 if (s->hl == DRAW_CURSOR
2188 && !x_stretch_cursor_p)
2189 {
2190 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2191 as wide as the stretch glyph. */
2192 int width, background_width = s->background_width;
2193 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2194
2195 if (x < left_x)
2196 {
2197 background_width -= left_x - x;
2198 x = left_x;
2199 }
2200 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2201
2202 /* Draw cursor. */
2203 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2204
2205 /* Clear rest using the GC of the original non-cursor face. */
2206 if (width < background_width)
2207 {
2208 XGCValues *gc = s->face->gc;
2209 int y = s->y;
2210 int w = background_width - width, h = s->height;
2211 RECT r;
2212 HDC hdc = s->hdc;
2213
2214 x += width;
2215 if (s->row->mouse_face_p
2216 && cursor_in_mouse_face_p (s->w))
2217 {
2218 x_set_mouse_face_gc (s);
2219 gc = s->gc;
2220 }
2221 else
2222 gc = s->face->gc;
2223
2224 get_glyph_string_clip_rect (s, &r);
2225 w32_set_clip_rectangle (hdc, &r);
2226
2227 #if 0 /* TODO: stipple */
2228 if (s->face->stipple)
2229 {
2230 /* Fill background with a stipple pattern. */
2231 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2232 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2233 XSetFillStyle (s->display, gc, FillSolid);
2234 }
2235 else
2236 #endif
2237 {
2238 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2239 }
2240 }
2241 }
2242 else if (!s->background_filled_p)
2243 {
2244 int background_width = s->background_width;
2245 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2246
2247 /* Don't draw into left margin, fringe or scrollbar area
2248 except for header line and mode line. */
2249 if (x < left_x && !s->row->mode_line_p)
2250 {
2251 background_width -= left_x - x;
2252 x = left_x;
2253 }
2254 if (background_width > 0)
2255 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2256 }
2257
2258 s->background_filled_p = 1;
2259 }
2260
2261
2262 /* Draw glyph string S. */
2263
2264 static void
2265 x_draw_glyph_string (s)
2266 struct glyph_string *s;
2267 {
2268 int relief_drawn_p = 0;
2269
2270 /* If S draws into the background of its successor, draw the
2271 background of the successor first so that S can draw into it.
2272 This makes S->next use XDrawString instead of XDrawImageString. */
2273 if (s->next && s->right_overhang && !s->for_overlaps)
2274 {
2275 int width;
2276 struct glyph_string *next;
2277 for (width = 0, next = s->next;
2278 next && width < s->right_overhang;
2279 width += next->width, next = next->next)
2280 if (next->first_glyph->type != IMAGE_GLYPH)
2281 {
2282 x_set_glyph_string_gc (next);
2283 x_set_glyph_string_clipping (next);
2284 if (next->first_glyph->type == STRETCH_GLYPH)
2285 x_draw_stretch_glyph_string (next);
2286 else
2287 x_draw_glyph_string_background (next, 1);
2288 next->num_clips = 0;
2289 }
2290 }
2291
2292 /* Set up S->gc, set clipping and draw S. */
2293 x_set_glyph_string_gc (s);
2294
2295 /* Draw relief (if any) in advance for char/composition so that the
2296 glyph string can be drawn over it. */
2297 if (!s->for_overlaps
2298 && s->face->box != FACE_NO_BOX
2299 && (s->first_glyph->type == CHAR_GLYPH
2300 || s->first_glyph->type == COMPOSITE_GLYPH))
2301
2302 {
2303 x_set_glyph_string_clipping (s);
2304 x_draw_glyph_string_background (s, 1);
2305 x_draw_glyph_string_box (s);
2306 x_set_glyph_string_clipping (s);
2307 relief_drawn_p = 1;
2308 }
2309 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2310 && !s->clip_tail
2311 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2312 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2313 /* We must clip just this glyph. left_overhang part has already
2314 drawn when s->prev was drawn, and right_overhang part will be
2315 drawn later when s->next is drawn. */
2316 x_set_glyph_string_clipping_exactly (s, s);
2317 else
2318 x_set_glyph_string_clipping (s);
2319
2320 switch (s->first_glyph->type)
2321 {
2322 case IMAGE_GLYPH:
2323 x_draw_image_glyph_string (s);
2324 break;
2325
2326 case STRETCH_GLYPH:
2327 x_draw_stretch_glyph_string (s);
2328 break;
2329
2330 case CHAR_GLYPH:
2331 if (s->for_overlaps)
2332 s->background_filled_p = 1;
2333 else
2334 x_draw_glyph_string_background (s, 0);
2335 x_draw_glyph_string_foreground (s);
2336 break;
2337
2338 case COMPOSITE_GLYPH:
2339 if (s->for_overlaps || (s->cmp_from > 0
2340 && ! s->first_glyph->u.cmp.automatic))
2341 s->background_filled_p = 1;
2342 else
2343 x_draw_glyph_string_background (s, 1);
2344 x_draw_composite_glyph_string_foreground (s);
2345 break;
2346
2347 default:
2348 abort ();
2349 }
2350
2351 if (!s->for_overlaps)
2352 {
2353 /* Draw underline. */
2354 if (s->face->underline_p)
2355 {
2356 unsigned long thickness, position;
2357 int y;
2358
2359 if (s->prev && s->prev->face->underline_p)
2360 {
2361 /* We use the same underline style as the previous one. */
2362 thickness = s->prev->underline_thickness;
2363 position = s->prev->underline_position;
2364 }
2365 else
2366 {
2367 /* Get the underline thickness. Default is 1 pixel. */
2368 if (s->font && s->font->underline_thickness > 0)
2369 thickness = s->font->underline_thickness;
2370 else
2371 thickness = 1;
2372 if (x_underline_at_descent_line)
2373 position = (s->height - thickness) - (s->ybase - s->y);
2374 else
2375 {
2376 /* Get the underline position. This is the recommended
2377 vertical offset in pixels from the baseline to the top of
2378 the underline. This is a signed value according to the
2379 specs, and its default is
2380
2381 ROUND ((maximum_descent) / 2), with
2382 ROUND (x) = floor (x + 0.5) */
2383
2384 if (x_use_underline_position_properties
2385 && s->font && s->font->underline_position >= 0)
2386 position = s->font->underline_position;
2387 else if (s->font)
2388 position = (s->font->descent + 1) / 2;
2389 }
2390 position = max (position, underline_minimum_offset);
2391 }
2392 /* Check the sanity of thickness and position. We should
2393 avoid drawing underline out of the current line area. */
2394 if (s->y + s->height <= s->ybase + position)
2395 position = (s->height - 1) - (s->ybase - s->y);
2396 if (s->y + s->height < s->ybase + position + thickness)
2397 thickness = (s->y + s->height) - (s->ybase + position);
2398 s->underline_thickness = thickness;
2399 s->underline_position =position;
2400 y = s->ybase + position;
2401 if (s->face->underline_defaulted_p)
2402 {
2403 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2404 y, s->width, 1);
2405 }
2406 else
2407 {
2408 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2409 y, s->width, 1);
2410 }
2411 }
2412 /* Draw overline. */
2413 if (s->face->overline_p)
2414 {
2415 unsigned long dy = 0, h = 1;
2416
2417 if (s->face->overline_color_defaulted_p)
2418 {
2419 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2420 s->y + dy, s->width, h);
2421 }
2422 else
2423 {
2424 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2425 s->y + dy, s->width, h);
2426 }
2427 }
2428
2429 /* Draw strike-through. */
2430 if (s->face->strike_through_p
2431 && !FONT_TEXTMETRIC(s->font).tmStruckOut)
2432 {
2433 unsigned long h = 1;
2434 unsigned long dy = (s->height - h) / 2;
2435
2436 if (s->face->strike_through_color_defaulted_p)
2437 {
2438 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2439 s->width, h);
2440 }
2441 else
2442 {
2443 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2444 s->y + dy, s->width, h);
2445 }
2446 }
2447
2448 /* Draw relief if not yet drawn. */
2449 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2450 x_draw_glyph_string_box (s);
2451
2452 if (s->prev)
2453 {
2454 struct glyph_string *prev;
2455
2456 for (prev = s->prev; prev; prev = prev->prev)
2457 if (prev->hl != s->hl
2458 && prev->x + prev->width + prev->right_overhang > s->x)
2459 {
2460 /* As prev was drawn while clipped to its own area, we
2461 must draw the right_overhang part using s->hl now. */
2462 enum draw_glyphs_face save = prev->hl;
2463
2464 prev->hl = s->hl;
2465 x_set_glyph_string_gc (prev);
2466 x_set_glyph_string_clipping_exactly (s, prev);
2467 if (prev->first_glyph->type == CHAR_GLYPH)
2468 x_draw_glyph_string_foreground (prev);
2469 else
2470 x_draw_composite_glyph_string_foreground (prev);
2471 w32_set_clip_rectangle (prev->hdc, NULL);
2472 prev->hl = save;
2473 prev->num_clips = 0;
2474 }
2475 }
2476
2477 if (s->next)
2478 {
2479 struct glyph_string *next;
2480
2481 for (next = s->next; next; next = next->next)
2482 if (next->hl != s->hl
2483 && next->x - next->left_overhang < s->x + s->width)
2484 {
2485 /* As next will be drawn while clipped to its own area,
2486 we must draw the left_overhang part using s->hl now. */
2487 enum draw_glyphs_face save = next->hl;
2488
2489 next->hl = s->hl;
2490 x_set_glyph_string_gc (next);
2491 x_set_glyph_string_clipping_exactly (s, next);
2492 if (next->first_glyph->type == CHAR_GLYPH)
2493 x_draw_glyph_string_foreground (next);
2494 else
2495 x_draw_composite_glyph_string_foreground (next);
2496 w32_set_clip_rectangle (next->hdc, NULL);
2497 next->hl = save;
2498 next->num_clips = 0;
2499 }
2500 }
2501 }
2502
2503 /* Reset clipping. */
2504 w32_set_clip_rectangle (s->hdc, NULL);
2505 s->num_clips = 0;
2506 }
2507
2508
2509 /* Shift display to make room for inserted glyphs. */
2510
2511 void
2512 w32_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2513 struct frame *f;
2514 int x, y, width, height, shift_by;
2515 {
2516 HDC hdc;
2517
2518 hdc = get_frame_dc (f);
2519 BitBlt (hdc, x + shift_by, y, width, height,
2520 hdc, x, y, SRCCOPY);
2521
2522 release_frame_dc (f, hdc);
2523 }
2524
2525
2526 /* Delete N glyphs at the nominal cursor position. Not implemented
2527 for X frames. */
2528
2529 static void
2530 x_delete_glyphs (f, n)
2531 struct frame *f;
2532 register int n;
2533 {
2534 if (! FRAME_W32_P (f))
2535 return;
2536
2537 abort ();
2538 }
2539
2540
2541 /* Clear entire frame. If updating_frame is non-null, clear that
2542 frame. Otherwise clear the selected frame. */
2543
2544 static void
2545 x_clear_frame (struct frame *f)
2546 {
2547 if (! FRAME_W32_P (f))
2548 return;
2549
2550 /* Clearing the frame will erase any cursor, so mark them all as no
2551 longer visible. */
2552 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2553 output_cursor.hpos = output_cursor.vpos = 0;
2554 output_cursor.x = -1;
2555
2556 /* We don't set the output cursor here because there will always
2557 follow an explicit cursor_to. */
2558 BLOCK_INPUT;
2559
2560 w32_clear_window (f);
2561
2562 /* We have to clear the scroll bars, too. If we have changed
2563 colors or something like that, then they should be notified. */
2564 x_scroll_bar_clear (f);
2565
2566 UNBLOCK_INPUT;
2567 }
2568
2569 \f
2570 /* Make audible bell. */
2571
2572 static void
2573 w32_ring_bell (struct frame *f)
2574 {
2575 BLOCK_INPUT;
2576
2577 if (FRAME_W32_P (f) && visible_bell)
2578 {
2579 int i;
2580 HWND hwnd = FRAME_W32_WINDOW (f);
2581
2582 for (i = 0; i < 5; i++)
2583 {
2584 FlashWindow (hwnd, TRUE);
2585 Sleep (10);
2586 }
2587 FlashWindow (hwnd, FALSE);
2588 }
2589 else
2590 w32_sys_ring_bell (f);
2591
2592 UNBLOCK_INPUT;
2593 }
2594
2595 \f
2596 /* Specify how many text lines, from the top of the window,
2597 should be affected by insert-lines and delete-lines operations.
2598 This, and those operations, are used only within an update
2599 that is bounded by calls to x_update_begin and x_update_end. */
2600
2601 static void
2602 w32_set_terminal_window (n)
2603 register int n;
2604 {
2605 /* This function intentionally left blank. */
2606 }
2607
2608 \f
2609 /***********************************************************************
2610 Line Dance
2611 ***********************************************************************/
2612
2613 /* Perform an insert-lines or delete-lines operation, inserting N
2614 lines or deleting -N lines at vertical position VPOS. */
2615
2616 static void
2617 x_ins_del_lines (f, vpos, n)
2618 struct frame *f;
2619 int vpos, n;
2620 {
2621 if (! FRAME_W32_P (f))
2622 return;
2623
2624 abort ();
2625 }
2626
2627
2628 /* Scroll part of the display as described by RUN. */
2629
2630 static void
2631 x_scroll_run (w, run)
2632 struct window *w;
2633 struct run *run;
2634 {
2635 struct frame *f = XFRAME (w->frame);
2636 int x, y, width, height, from_y, to_y, bottom_y;
2637 HWND hwnd = FRAME_W32_WINDOW (f);
2638 HRGN expect_dirty;
2639
2640 /* Get frame-relative bounding box of the text display area of W,
2641 without mode lines. Include in this box the left and right
2642 fringes of W. */
2643 window_box (w, -1, &x, &y, &width, &height);
2644
2645 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2646 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2647 bottom_y = y + height;
2648
2649 if (to_y < from_y)
2650 {
2651 /* Scrolling up. Make sure we don't copy part of the mode
2652 line at the bottom. */
2653 if (from_y + run->height > bottom_y)
2654 height = bottom_y - from_y;
2655 else
2656 height = run->height;
2657 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2658 }
2659 else
2660 {
2661 /* Scolling down. Make sure we don't copy over the mode line.
2662 at the bottom. */
2663 if (to_y + run->height > bottom_y)
2664 height = bottom_y - to_y;
2665 else
2666 height = run->height;
2667 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2668 }
2669
2670 BLOCK_INPUT;
2671
2672 /* Cursor off. Will be switched on again in x_update_window_end. */
2673 updated_window = w;
2674 x_clear_cursor (w);
2675
2676 {
2677 RECT from;
2678 RECT to;
2679 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2680 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2681
2682 from.left = to.left = x;
2683 from.right = to.right = x + width;
2684 from.top = from_y;
2685 from.bottom = from_y + height;
2686 to.top = y;
2687 to.bottom = bottom_y;
2688
2689 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2690 NULL, SW_INVALIDATE);
2691
2692 /* Combine this with what we expect to be dirty. This covers the
2693 case where not all of the region we expect is actually dirty. */
2694 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2695
2696 /* If the dirty region is not what we expected, redraw the entire frame. */
2697 if (!EqualRgn (combined, expect_dirty))
2698 SET_FRAME_GARBAGED (f);
2699
2700 DeleteObject (dirty);
2701 DeleteObject (combined);
2702 }
2703
2704 UNBLOCK_INPUT;
2705 DeleteObject (expect_dirty);
2706 }
2707
2708
2709 \f
2710 /***********************************************************************
2711 Exposure Events
2712 ***********************************************************************/
2713
2714 static void
2715 frame_highlight (f)
2716 struct frame *f;
2717 {
2718 x_update_cursor (f, 1);
2719 x_set_frame_alpha (f);
2720 }
2721
2722 static void
2723 frame_unhighlight (f)
2724 struct frame *f;
2725 {
2726 x_update_cursor (f, 1);
2727 x_set_frame_alpha (f);
2728 }
2729
2730 /* The focus has changed. Update the frames as necessary to reflect
2731 the new situation. Note that we can't change the selected frame
2732 here, because the Lisp code we are interrupting might become confused.
2733 Each event gets marked with the frame in which it occurred, so the
2734 Lisp code can tell when the switch took place by examining the events. */
2735
2736 static void
2737 x_new_focus_frame (dpyinfo, frame)
2738 struct w32_display_info *dpyinfo;
2739 struct frame *frame;
2740 {
2741 struct frame *old_focus = dpyinfo->w32_focus_frame;
2742
2743 if (frame != dpyinfo->w32_focus_frame)
2744 {
2745 /* Set this before calling other routines, so that they see
2746 the correct value of w32_focus_frame. */
2747 dpyinfo->w32_focus_frame = frame;
2748
2749 if (old_focus && old_focus->auto_lower)
2750 x_lower_frame (old_focus);
2751
2752 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2753 pending_autoraise_frame = dpyinfo->w32_focus_frame;
2754 else
2755 pending_autoraise_frame = 0;
2756 }
2757
2758 x_frame_rehighlight (dpyinfo);
2759 }
2760
2761
2762 /* Handle FocusIn and FocusOut state changes for FRAME.
2763 If FRAME has focus and there exists more than one frame, puts
2764 a FOCUS_IN_EVENT into *BUFP. */
2765
2766 static void
2767 x_focus_changed (type, state, dpyinfo, frame, bufp)
2768 int type;
2769 int state;
2770 struct w32_display_info *dpyinfo;
2771 struct frame *frame;
2772 struct input_event *bufp;
2773 {
2774 if (type == WM_SETFOCUS)
2775 {
2776 if (dpyinfo->w32_focus_event_frame != frame)
2777 {
2778 x_new_focus_frame (dpyinfo, frame);
2779 dpyinfo->w32_focus_event_frame = frame;
2780
2781 /* Don't stop displaying the initial startup message
2782 for a switch-frame event we don't need. */
2783 if (NILP (Vterminal_frame)
2784 && CONSP (Vframe_list)
2785 && !NILP (XCDR (Vframe_list)))
2786 {
2787 bufp->kind = FOCUS_IN_EVENT;
2788 XSETFRAME (bufp->frame_or_window, frame);
2789 }
2790 }
2791
2792 frame->output_data.x->focus_state |= state;
2793
2794 /* TODO: IME focus? */
2795 }
2796 else if (type == WM_KILLFOCUS)
2797 {
2798 frame->output_data.x->focus_state &= ~state;
2799
2800 if (dpyinfo->w32_focus_event_frame == frame)
2801 {
2802 dpyinfo->w32_focus_event_frame = 0;
2803 x_new_focus_frame (dpyinfo, 0);
2804 }
2805
2806 /* TODO: IME focus? */
2807 }
2808 }
2809
2810
2811 /* The focus may have changed. Figure out if it is a real focus change,
2812 by checking both FocusIn/Out and Enter/LeaveNotify events.
2813
2814 Returns FOCUS_IN_EVENT event in *BUFP. */
2815
2816 static void
2817 w32_detect_focus_change (dpyinfo, event, bufp)
2818 struct w32_display_info *dpyinfo;
2819 W32Msg *event;
2820 struct input_event *bufp;
2821 {
2822 struct frame *frame;
2823
2824 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2825 if (! frame)
2826 return;
2827
2828 /* On w32, this is only called from focus events, so no switch needed. */
2829 x_focus_changed (event->msg.message,
2830 (event->msg.message == WM_KILLFOCUS ?
2831 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2832 dpyinfo, frame, bufp);
2833 }
2834
2835
2836 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2837
2838 void
2839 x_mouse_leave (dpyinfo)
2840 struct w32_display_info *dpyinfo;
2841 {
2842 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2843 }
2844
2845 /* The focus has changed, or we have redirected a frame's focus to
2846 another frame (this happens when a frame uses a surrogate
2847 mini-buffer frame). Shift the highlight as appropriate.
2848
2849 The FRAME argument doesn't necessarily have anything to do with which
2850 frame is being highlighted or un-highlighted; we only use it to find
2851 the appropriate X display info. */
2852
2853 static void
2854 w32_frame_rehighlight (frame)
2855 struct frame *frame;
2856 {
2857 if (! FRAME_W32_P (frame))
2858 return;
2859 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
2860 }
2861
2862 static void
2863 x_frame_rehighlight (dpyinfo)
2864 struct w32_display_info *dpyinfo;
2865 {
2866 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2867
2868 if (dpyinfo->w32_focus_frame)
2869 {
2870 dpyinfo->x_highlight_frame
2871 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2872 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2873 : dpyinfo->w32_focus_frame);
2874 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2875 {
2876 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
2877 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2878 }
2879 }
2880 else
2881 dpyinfo->x_highlight_frame = 0;
2882
2883 if (dpyinfo->x_highlight_frame != old_highlight)
2884 {
2885 if (old_highlight)
2886 frame_unhighlight (old_highlight);
2887 if (dpyinfo->x_highlight_frame)
2888 frame_highlight (dpyinfo->x_highlight_frame);
2889 }
2890 }
2891 \f
2892 /* Keyboard processing - modifier keys, etc. */
2893
2894 /* Convert a keysym to its name. */
2895
2896 char *
2897 x_get_keysym_name (keysym)
2898 int keysym;
2899 {
2900 /* Make static so we can always return it */
2901 static char value[100];
2902
2903 BLOCK_INPUT;
2904 GetKeyNameText (keysym, value, 100);
2905 UNBLOCK_INPUT;
2906
2907 return value;
2908 }
2909
2910 static int codepage_for_locale(LCID locale)
2911 {
2912 char cp[20];
2913
2914 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
2915 return atoi (cp);
2916 else
2917 return CP_ACP;
2918 }
2919
2920 \f
2921 /* Mouse clicks and mouse movement. Rah. */
2922
2923 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2924 the state in PUP. XBUTTON provides extra information for extended mouse
2925 button messages. Returns FALSE if unable to parse the message. */
2926 BOOL
2927 parse_button (message, xbutton, pbutton, pup)
2928 int message;
2929 int xbutton;
2930 int * pbutton;
2931 int * pup;
2932 {
2933 int button = 0;
2934 int up = 0;
2935
2936 switch (message)
2937 {
2938 case WM_LBUTTONDOWN:
2939 button = 0;
2940 up = 0;
2941 break;
2942 case WM_LBUTTONUP:
2943 button = 0;
2944 up = 1;
2945 break;
2946 case WM_MBUTTONDOWN:
2947 if (NILP (Vw32_swap_mouse_buttons))
2948 button = 1;
2949 else
2950 button = 2;
2951 up = 0;
2952 break;
2953 case WM_MBUTTONUP:
2954 if (NILP (Vw32_swap_mouse_buttons))
2955 button = 1;
2956 else
2957 button = 2;
2958 up = 1;
2959 break;
2960 case WM_RBUTTONDOWN:
2961 if (NILP (Vw32_swap_mouse_buttons))
2962 button = 2;
2963 else
2964 button = 1;
2965 up = 0;
2966 break;
2967 case WM_RBUTTONUP:
2968 if (NILP (Vw32_swap_mouse_buttons))
2969 button = 2;
2970 else
2971 button = 1;
2972 up = 1;
2973 break;
2974 case WM_XBUTTONDOWN:
2975 button = xbutton + 2;
2976 up = 0;
2977 break;
2978 case WM_XBUTTONUP:
2979 button = xbutton + 2;
2980 up = 1;
2981 break;
2982 default:
2983 return (FALSE);
2984 }
2985
2986 if (pup) *pup = up;
2987 if (pbutton) *pbutton = button;
2988
2989 return (TRUE);
2990 }
2991
2992
2993 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2994
2995 If the event is a button press, then note that we have grabbed
2996 the mouse. */
2997
2998 static Lisp_Object
2999 construct_mouse_click (result, msg, f)
3000 struct input_event *result;
3001 W32Msg *msg;
3002 struct frame *f;
3003 {
3004 int button;
3005 int up;
3006
3007 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3008 &button, &up);
3009
3010 /* Make the event type NO_EVENT; we'll change that when we decide
3011 otherwise. */
3012 result->kind = MOUSE_CLICK_EVENT;
3013 result->code = button;
3014 result->timestamp = msg->msg.time;
3015 result->modifiers = (msg->dwModifiers
3016 | (up
3017 ? up_modifier
3018 : down_modifier));
3019
3020 XSETINT (result->x, LOWORD (msg->msg.lParam));
3021 XSETINT (result->y, HIWORD (msg->msg.lParam));
3022 XSETFRAME (result->frame_or_window, f);
3023 result->arg = Qnil;
3024 return Qnil;
3025 }
3026
3027 static Lisp_Object
3028 construct_mouse_wheel (result, msg, f)
3029 struct input_event *result;
3030 W32Msg *msg;
3031 struct frame *f;
3032 {
3033 POINT p;
3034 int delta;
3035
3036 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3037 : WHEEL_EVENT;
3038 result->code = 0;
3039 result->timestamp = msg->msg.time;
3040
3041 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3042 forward, away from the user (up); a negative value indicates that
3043 the wheel was rotated backward, toward the user (down). */
3044 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3045
3046 /* The up and down modifiers indicate if the wheel was rotated up or
3047 down based on WHEEL_DELTA value. */
3048 result->modifiers = (msg->dwModifiers
3049 | ((delta < 0 ) ? down_modifier : up_modifier));
3050
3051 /* With multiple monitors, we can legitimately get negative
3052 coordinates, so cast to short to interpret them correctly. */
3053 p.x = (short) LOWORD (msg->msg.lParam);
3054 p.y = (short) HIWORD (msg->msg.lParam);
3055 ScreenToClient (msg->msg.hwnd, &p);
3056 XSETINT (result->x, p.x);
3057 XSETINT (result->y, p.y);
3058 XSETFRAME (result->frame_or_window, f);
3059 result->arg = Qnil;
3060 return Qnil;
3061 }
3062
3063 static Lisp_Object
3064 construct_drag_n_drop (result, msg, f)
3065 struct input_event *result;
3066 W32Msg *msg;
3067 struct frame *f;
3068 {
3069 Lisp_Object files;
3070 Lisp_Object frame;
3071 HDROP hdrop;
3072 POINT p;
3073 WORD num_files;
3074 char *name;
3075 int i, len;
3076
3077 result->kind = DRAG_N_DROP_EVENT;
3078 result->code = 0;
3079 result->timestamp = msg->msg.time;
3080 result->modifiers = msg->dwModifiers;
3081
3082 hdrop = (HDROP) msg->msg.wParam;
3083 DragQueryPoint (hdrop, &p);
3084
3085 #if 0
3086 p.x = LOWORD (msg->msg.lParam);
3087 p.y = HIWORD (msg->msg.lParam);
3088 ScreenToClient (msg->msg.hwnd, &p);
3089 #endif
3090
3091 XSETINT (result->x, p.x);
3092 XSETINT (result->y, p.y);
3093
3094 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3095 files = Qnil;
3096
3097 for (i = 0; i < num_files; i++)
3098 {
3099 len = DragQueryFile (hdrop, i, NULL, 0);
3100 if (len <= 0)
3101 continue;
3102 name = alloca (len + 1);
3103 DragQueryFile (hdrop, i, name, len + 1);
3104 files = Fcons (DECODE_FILE (build_string (name)), files);
3105 }
3106
3107 DragFinish (hdrop);
3108
3109 XSETFRAME (frame, f);
3110 result->frame_or_window = frame;
3111 result->arg = files;
3112 return Qnil;
3113 }
3114
3115 \f
3116 /* Function to report a mouse movement to the mainstream Emacs code.
3117 The input handler calls this.
3118
3119 We have received a mouse movement event, which is given in *event.
3120 If the mouse is over a different glyph than it was last time, tell
3121 the mainstream emacs code by setting mouse_moved. If not, ask for
3122 another motion event, so we can check again the next time it moves. */
3123
3124 static MSG last_mouse_motion_event;
3125 static Lisp_Object last_mouse_motion_frame;
3126
3127 static int
3128 note_mouse_movement (frame, msg)
3129 FRAME_PTR frame;
3130 MSG *msg;
3131 {
3132 int mouse_x = LOWORD (msg->lParam);
3133 int mouse_y = HIWORD (msg->lParam);
3134
3135 last_mouse_movement_time = msg->time;
3136 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
3137 XSETFRAME (last_mouse_motion_frame, frame);
3138
3139 if (!FRAME_X_OUTPUT (frame))
3140 return 0;
3141
3142 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3143 {
3144 frame->mouse_moved = 1;
3145 last_mouse_scroll_bar = Qnil;
3146 note_mouse_highlight (frame, -1, -1);
3147 last_mouse_glyph_frame = 0;
3148 return 1;
3149 }
3150
3151 /* Has the mouse moved off the glyph it was on at the last sighting? */
3152 if (frame != last_mouse_glyph_frame
3153 || mouse_x < last_mouse_glyph.left
3154 || mouse_x >= last_mouse_glyph.right
3155 || mouse_y < last_mouse_glyph.top
3156 || mouse_y >= last_mouse_glyph.bottom)
3157 {
3158 frame->mouse_moved = 1;
3159 last_mouse_scroll_bar = Qnil;
3160 note_mouse_highlight (frame, mouse_x, mouse_y);
3161 /* Remember the mouse position here, as w32_mouse_position only
3162 gets called when mouse tracking is enabled but we also need
3163 to keep track of the mouse for help_echo and highlighting at
3164 other times. */
3165 remember_mouse_glyph (frame, mouse_x, mouse_y, &last_mouse_glyph);
3166 last_mouse_glyph_frame = frame;
3167 return 1;
3168 }
3169
3170 return 0;
3171 }
3172
3173 \f
3174 /************************************************************************
3175 Mouse Face
3176 ************************************************************************/
3177
3178 static struct scroll_bar *x_window_to_scroll_bar ();
3179 static void x_scroll_bar_report_motion ();
3180 static void x_check_fullscreen P_ ((struct frame *));
3181
3182 static void
3183 redo_mouse_highlight ()
3184 {
3185 if (!NILP (last_mouse_motion_frame)
3186 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3187 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3188 LOWORD (last_mouse_motion_event.lParam),
3189 HIWORD (last_mouse_motion_event.lParam));
3190 }
3191
3192 static void
3193 w32_define_cursor (window, cursor)
3194 Window window;
3195 Cursor cursor;
3196 {
3197 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3198 }
3199 /* Return the current position of the mouse.
3200 *fp should be a frame which indicates which display to ask about.
3201
3202 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3203 and *part to the frame, window, and scroll bar part that the mouse
3204 is over. Set *x and *y to the portion and whole of the mouse's
3205 position on the scroll bar.
3206
3207 If the mouse movement started elsewhere, set *fp to the frame the
3208 mouse is on, *bar_window to nil, and *x and *y to the character cell
3209 the mouse is over.
3210
3211 Set *time to the server time-stamp for the time at which the mouse
3212 was at this position.
3213
3214 Don't store anything if we don't have a valid set of values to report.
3215
3216 This clears the mouse_moved flag, so we can wait for the next mouse
3217 movement. */
3218
3219 static void
3220 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
3221 FRAME_PTR *fp;
3222 int insist;
3223 Lisp_Object *bar_window;
3224 enum scroll_bar_part *part;
3225 Lisp_Object *x, *y;
3226 unsigned long *time;
3227 {
3228 FRAME_PTR f1;
3229
3230 BLOCK_INPUT;
3231
3232 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3233 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3234 else
3235 {
3236 POINT pt;
3237
3238 Lisp_Object frame, tail;
3239
3240 /* Clear the mouse-moved flag for every frame on this display. */
3241 FOR_EACH_FRAME (tail, frame)
3242 XFRAME (frame)->mouse_moved = 0;
3243
3244 last_mouse_scroll_bar = Qnil;
3245
3246 GetCursorPos (&pt);
3247
3248 /* Now we have a position on the root; find the innermost window
3249 containing the pointer. */
3250 {
3251 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3252 && FRAME_LIVE_P (last_mouse_frame))
3253 {
3254 /* If mouse was grabbed on a frame, give coords for that frame
3255 even if the mouse is now outside it. */
3256 f1 = last_mouse_frame;
3257 }
3258 else
3259 {
3260 /* Is window under mouse one of our frames? */
3261 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
3262 WindowFromPoint (pt));
3263 }
3264
3265 /* If not, is it one of our scroll bars? */
3266 if (! f1)
3267 {
3268 struct scroll_bar *bar
3269 = x_window_to_scroll_bar (WindowFromPoint (pt));
3270
3271 if (bar)
3272 {
3273 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3274 }
3275 }
3276
3277 if (f1 == 0 && insist > 0)
3278 f1 = SELECTED_FRAME ();
3279
3280 if (f1)
3281 {
3282 /* Ok, we found a frame. Store all the values.
3283 last_mouse_glyph is a rectangle used to reduce the
3284 generation of mouse events. To not miss any motion
3285 events, we must divide the frame into rectangles of the
3286 size of the smallest character that could be displayed
3287 on it, i.e. into the same rectangles that matrices on
3288 the frame are divided into. */
3289
3290 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3291 remember_mouse_glyph (f1, pt.x, pt.y, &last_mouse_glyph);
3292 last_mouse_glyph_frame = f1;
3293
3294 *bar_window = Qnil;
3295 *part = 0;
3296 *fp = f1;
3297 XSETINT (*x, pt.x);
3298 XSETINT (*y, pt.y);
3299 *time = last_mouse_movement_time;
3300 }
3301 }
3302 }
3303
3304 UNBLOCK_INPUT;
3305 }
3306
3307 \f
3308 /***********************************************************************
3309 Tool-bars
3310 ***********************************************************************/
3311
3312 /* Handle mouse button event on the tool-bar of frame F, at
3313 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3314 or ButtonRelase. */
3315
3316 static void
3317 w32_handle_tool_bar_click (f, button_event)
3318 struct frame *f;
3319 struct input_event *button_event;
3320 {
3321 int x = XFASTINT (button_event->x);
3322 int y = XFASTINT (button_event->y);
3323
3324 if (button_event->modifiers & down_modifier)
3325 handle_tool_bar_click (f, x, y, 1, 0);
3326 else
3327 handle_tool_bar_click (f, x, y, 0,
3328 button_event->modifiers & ~up_modifier);
3329 }
3330
3331
3332 \f
3333 /***********************************************************************
3334 Scroll bars
3335 ***********************************************************************/
3336
3337 /* Scroll bar support. */
3338
3339 /* Given a window ID, find the struct scroll_bar which manages it.
3340 This can be called in GC, so we have to make sure to strip off mark
3341 bits. */
3342
3343 static struct scroll_bar *
3344 x_window_to_scroll_bar (window_id)
3345 Window window_id;
3346 {
3347 Lisp_Object tail;
3348
3349 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3350 {
3351 Lisp_Object frame, bar, condemned;
3352
3353 frame = XCAR (tail);
3354 /* All elements of Vframe_list should be frames. */
3355 if (! FRAMEP (frame))
3356 abort ();
3357
3358 /* Scan this frame's scroll bar list for a scroll bar with the
3359 right window ID. */
3360 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3361 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3362 /* This trick allows us to search both the ordinary and
3363 condemned scroll bar lists with one loop. */
3364 ! NILP (bar) || (bar = condemned,
3365 condemned = Qnil,
3366 ! NILP (bar));
3367 bar = XSCROLL_BAR (bar)->next)
3368 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3369 return XSCROLL_BAR (bar);
3370 }
3371
3372 return 0;
3373 }
3374
3375
3376 \f
3377 /* Set the thumb size and position of scroll bar BAR. We are currently
3378 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3379
3380 static void
3381 w32_set_scroll_bar_thumb (bar, portion, position, whole)
3382 struct scroll_bar *bar;
3383 int portion, position, whole;
3384 {
3385 Window w = SCROLL_BAR_W32_WINDOW (bar);
3386 /* We use the whole scroll-bar height in the calculations below, to
3387 avoid strange effects like scrolling backwards when just clicking
3388 on the handle (without moving it). */
3389 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height))
3390 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3391 int sb_page, sb_pos;
3392 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3393 SCROLLINFO si;
3394
3395 /* We used to change the nPage setting while dragging the handle,
3396 but that had very strange effects (such as scrolling backwards
3397 while dragging downwards).
3398
3399 Now, we don't change the nPage setting while dragging unless we
3400 get near to the end of the buffer, in which case we often have to
3401 resize the handle to "go all the way". */
3402
3403 if (draggingp)
3404 {
3405 int near_bottom_p;
3406 BLOCK_INPUT;
3407 si.cbSize = sizeof (si);
3408 si.fMask = SIF_POS | SIF_PAGE;
3409 GetScrollInfo(w, SB_CTL, &si);
3410 near_bottom_p = si.nPos + si.nPage >= range;
3411 UNBLOCK_INPUT;
3412 if (!near_bottom_p)
3413 return;
3414 }
3415
3416 if (whole)
3417 {
3418 /* Position scroll bar at rock bottom if the bottom of the
3419 buffer is visible. This avoids shinking the thumb away
3420 to nothing if it is held at the bottom of the buffer. */
3421 if (position + portion >= whole && !draggingp)
3422 {
3423 sb_page = range * (whole - position) / whole;
3424 sb_pos = range;
3425 }
3426 else
3427 {
3428 sb_pos = position * range / whole;
3429 sb_page = (min (portion, (whole - position)) * range) / whole;
3430 }
3431 }
3432 else
3433 {
3434 sb_page = range;
3435 sb_pos = 0;
3436 }
3437
3438 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3439
3440 BLOCK_INPUT;
3441
3442 si.cbSize = sizeof (si);
3443 si.fMask = SIF_PAGE | SIF_POS;
3444 si.nPage = sb_page;
3445 si.nPos = sb_pos;
3446
3447 SetScrollInfo (w, SB_CTL, &si, TRUE);
3448
3449 UNBLOCK_INPUT;
3450 }
3451
3452 \f
3453 /************************************************************************
3454 Scroll bars, general
3455 ************************************************************************/
3456
3457 static HWND
3458 my_create_scrollbar (f, bar)
3459 struct frame * f;
3460 struct scroll_bar * bar;
3461 {
3462 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3463 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3464 (LPARAM) bar);
3465 }
3466
3467 /*#define ATTACH_THREADS*/
3468
3469 static BOOL
3470 my_show_window (FRAME_PTR f, HWND hwnd, int how)
3471 {
3472 #ifndef ATTACH_THREADS
3473 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3474 (WPARAM) hwnd, (LPARAM) how);
3475 #else
3476 return ShowWindow (hwnd, how);
3477 #endif
3478 }
3479
3480 static void
3481 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3482 int x, int y, int cx, int cy, UINT flags)
3483 {
3484 #ifndef ATTACH_THREADS
3485 WINDOWPOS pos;
3486 pos.hwndInsertAfter = hwndAfter;
3487 pos.x = x;
3488 pos.y = y;
3489 pos.cx = cx;
3490 pos.cy = cy;
3491 pos.flags = flags;
3492 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3493 #else
3494 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3495 #endif
3496 }
3497
3498 static void
3499 my_set_focus (f, hwnd)
3500 struct frame * f;
3501 HWND hwnd;
3502 {
3503 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3504 (WPARAM) hwnd, 0);
3505 }
3506
3507 static void
3508 my_set_foreground_window (hwnd)
3509 HWND hwnd;
3510 {
3511 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3512 }
3513
3514
3515 static void
3516 my_destroy_window (f, hwnd)
3517 struct frame * f;
3518 HWND hwnd;
3519 {
3520 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3521 (WPARAM) hwnd, 0);
3522 }
3523
3524 /* Create a scroll bar and return the scroll bar vector for it. W is
3525 the Emacs window on which to create the scroll bar. TOP, LEFT,
3526 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
3527 scroll bar. */
3528
3529 static struct scroll_bar *
3530 x_scroll_bar_create (w, top, left, width, height)
3531 struct window *w;
3532 int top, left, width, height;
3533 {
3534 struct frame *f = XFRAME (WINDOW_FRAME (w));
3535 HWND hwnd;
3536 SCROLLINFO si;
3537 struct scroll_bar *bar
3538 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3539
3540 BLOCK_INPUT;
3541
3542 XSETWINDOW (bar->window, w);
3543 XSETINT (bar->top, top);
3544 XSETINT (bar->left, left);
3545 XSETINT (bar->width, width);
3546 XSETINT (bar->height, height);
3547 XSETINT (bar->start, 0);
3548 XSETINT (bar->end, 0);
3549 bar->dragging = Qnil;
3550 bar->fringe_extended_p = Qnil;
3551
3552 /* Requires geometry to be set before call to create the real window */
3553
3554 hwnd = my_create_scrollbar (f, bar);
3555
3556 si.cbSize = sizeof (si);
3557 si.fMask = SIF_ALL;
3558 si.nMin = 0;
3559 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3560 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3561 si.nPage = si.nMax;
3562 si.nPos = 0;
3563
3564 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3565
3566 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3567
3568 /* Add bar to its frame's list of scroll bars. */
3569 bar->next = FRAME_SCROLL_BARS (f);
3570 bar->prev = Qnil;
3571 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3572 if (! NILP (bar->next))
3573 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3574
3575 UNBLOCK_INPUT;
3576
3577 return bar;
3578 }
3579
3580
3581 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3582 nil. */
3583
3584 static void
3585 x_scroll_bar_remove (bar)
3586 struct scroll_bar *bar;
3587 {
3588 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3589
3590 BLOCK_INPUT;
3591
3592 /* Destroy the window. */
3593 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3594
3595 /* Disassociate this scroll bar from its window. */
3596 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3597
3598 UNBLOCK_INPUT;
3599 }
3600
3601 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3602 that we are displaying PORTION characters out of a total of WHOLE
3603 characters, starting at POSITION. If WINDOW has no scroll bar,
3604 create one. */
3605 static void
3606 w32_set_vertical_scroll_bar (w, portion, whole, position)
3607 struct window *w;
3608 int portion, whole, position;
3609 {
3610 struct frame *f = XFRAME (w->frame);
3611 struct scroll_bar *bar;
3612 int top, height, left, sb_left, width, sb_width;
3613 int window_y, window_height;
3614 int fringe_extended_p;
3615
3616 /* Get window dimensions. */
3617 window_box (w, -1, 0, &window_y, 0, &window_height);
3618 top = window_y;
3619 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3620 height = window_height;
3621
3622 /* Compute the left edge of the scroll bar area. */
3623 left = WINDOW_SCROLL_BAR_AREA_X (w);
3624
3625 /* Compute the width of the scroll bar which might be less than
3626 the width of the area reserved for the scroll bar. */
3627 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3628 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3629 else
3630 sb_width = width;
3631
3632 /* Compute the left edge of the scroll bar. */
3633 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3634 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3635 else
3636 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3637
3638 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3639 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
3640 && WINDOW_LEFT_FRINGE_WIDTH (w)
3641 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3642 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
3643 else
3644 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
3645 && WINDOW_RIGHT_FRINGE_WIDTH (w)
3646 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3647 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
3648
3649 /* Does the scroll bar exist yet? */
3650 if (NILP (w->vertical_scroll_bar))
3651 {
3652 HDC hdc;
3653 BLOCK_INPUT;
3654 if (width > 0 && height > 0)
3655 {
3656 hdc = get_frame_dc (f);
3657 if (fringe_extended_p)
3658 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3659 else
3660 w32_clear_area (f, hdc, left, top, width, height);
3661 release_frame_dc (f, hdc);
3662 }
3663 UNBLOCK_INPUT;
3664
3665 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3666 }
3667 else
3668 {
3669 /* It may just need to be moved and resized. */
3670 HWND hwnd;
3671
3672 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3673 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3674
3675 /* If already correctly positioned, do nothing. */
3676 if ( XINT (bar->left) == sb_left
3677 && XINT (bar->top) == top
3678 && XINT (bar->width) == sb_width
3679 && XINT (bar->height) == height
3680 && !NILP (bar->fringe_extended_p) == fringe_extended_p )
3681 {
3682 /* Redraw after clear_frame. */
3683 if (!my_show_window (f, hwnd, SW_NORMAL))
3684 InvalidateRect (hwnd, NULL, FALSE);
3685 }
3686 else
3687 {
3688 HDC hdc;
3689 SCROLLINFO si;
3690
3691 BLOCK_INPUT;
3692 if (width && height)
3693 {
3694 hdc = get_frame_dc (f);
3695 /* Since Windows scroll bars are smaller than the space reserved
3696 for them on the frame, we have to clear "under" them. */
3697 if (fringe_extended_p)
3698 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3699 else
3700 w32_clear_area (f, hdc, left, top, width, height);
3701 release_frame_dc (f, hdc);
3702 }
3703 /* Make sure scroll bar is "visible" before moving, to ensure the
3704 area of the parent window now exposed will be refreshed. */
3705 my_show_window (f, hwnd, SW_HIDE);
3706 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3707 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3708 max (height, 1), TRUE);
3709
3710 si.cbSize = sizeof (si);
3711 si.fMask = SIF_RANGE;
3712 si.nMin = 0;
3713 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3714 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3715
3716 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3717
3718 my_show_window (f, hwnd, SW_NORMAL);
3719 /* InvalidateRect (w, NULL, FALSE); */
3720
3721 /* Remember new settings. */
3722 XSETINT (bar->left, sb_left);
3723 XSETINT (bar->top, top);
3724 XSETINT (bar->width, sb_width);
3725 XSETINT (bar->height, height);
3726
3727 UNBLOCK_INPUT;
3728 }
3729 }
3730 bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
3731
3732 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3733
3734 XSETVECTOR (w->vertical_scroll_bar, bar);
3735 }
3736
3737
3738 /* The following three hooks are used when we're doing a thorough
3739 redisplay of the frame. We don't explicitly know which scroll bars
3740 are going to be deleted, because keeping track of when windows go
3741 away is a real pain - "Can you say set-window-configuration, boys
3742 and girls?" Instead, we just assert at the beginning of redisplay
3743 that *all* scroll bars are to be removed, and then save a scroll bar
3744 from the fiery pit when we actually redisplay its window. */
3745
3746 /* Arrange for all scroll bars on FRAME to be removed at the next call
3747 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3748 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3749
3750 static void
3751 w32_condemn_scroll_bars (frame)
3752 FRAME_PTR frame;
3753 {
3754 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3755 while (! NILP (FRAME_SCROLL_BARS (frame)))
3756 {
3757 Lisp_Object bar;
3758 bar = FRAME_SCROLL_BARS (frame);
3759 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
3760 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3761 XSCROLL_BAR (bar)->prev = Qnil;
3762 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3763 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3764 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
3765 }
3766 }
3767
3768
3769 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3770 Note that WINDOW isn't necessarily condemned at all. */
3771
3772 static void
3773 w32_redeem_scroll_bar (window)
3774 struct window *window;
3775 {
3776 struct scroll_bar *bar;
3777 struct frame *f;
3778
3779 /* We can't redeem this window's scroll bar if it doesn't have one. */
3780 if (NILP (window->vertical_scroll_bar))
3781 abort ();
3782
3783 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3784
3785 /* Unlink it from the condemned list. */
3786 f = XFRAME (WINDOW_FRAME (window));
3787 if (NILP (bar->prev))
3788 {
3789 /* If the prev pointer is nil, it must be the first in one of
3790 the lists. */
3791 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3792 /* It's not condemned. Everything's fine. */
3793 return;
3794 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3795 window->vertical_scroll_bar))
3796 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
3797 else
3798 /* If its prev pointer is nil, it must be at the front of
3799 one or the other! */
3800 abort ();
3801 }
3802 else
3803 XSCROLL_BAR (bar->prev)->next = bar->next;
3804
3805 if (! NILP (bar->next))
3806 XSCROLL_BAR (bar->next)->prev = bar->prev;
3807
3808 bar->next = FRAME_SCROLL_BARS (f);
3809 bar->prev = Qnil;
3810 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3811 if (! NILP (bar->next))
3812 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3813 }
3814
3815 /* Remove all scroll bars on FRAME that haven't been saved since the
3816 last call to `*condemn_scroll_bars_hook'. */
3817
3818 static void
3819 w32_judge_scroll_bars (f)
3820 FRAME_PTR f;
3821 {
3822 Lisp_Object bar, next;
3823
3824 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3825
3826 /* Clear out the condemned list now so we won't try to process any
3827 more events on the hapless scroll bars. */
3828 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3829
3830 for (; ! NILP (bar); bar = next)
3831 {
3832 struct scroll_bar *b = XSCROLL_BAR (bar);
3833
3834 x_scroll_bar_remove (b);
3835
3836 next = b->next;
3837 b->next = b->prev = Qnil;
3838 }
3839
3840 /* Now there should be no references to the condemned scroll bars,
3841 and they should get garbage-collected. */
3842 }
3843
3844 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3845 is set to something other than NO_EVENT, it is enqueued.
3846
3847 This may be called from a signal handler, so we have to ignore GC
3848 mark bits. */
3849
3850 static int
3851 w32_scroll_bar_handle_click (bar, msg, emacs_event)
3852 struct scroll_bar *bar;
3853 W32Msg *msg;
3854 struct input_event *emacs_event;
3855 {
3856 if (! WINDOWP (bar->window))
3857 abort ();
3858
3859 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
3860 emacs_event->code = 0;
3861 /* not really meaningful to distinguish up/down */
3862 emacs_event->modifiers = msg->dwModifiers;
3863 emacs_event->frame_or_window = bar->window;
3864 emacs_event->arg = Qnil;
3865 emacs_event->timestamp = msg->msg.time;
3866
3867 {
3868 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3869 int y;
3870 int dragging = !NILP (bar->dragging);
3871 SCROLLINFO si;
3872
3873 si.cbSize = sizeof (si);
3874 si.fMask = SIF_POS;
3875
3876 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
3877 y = si.nPos;
3878
3879 bar->dragging = Qnil;
3880
3881
3882 last_mouse_scroll_bar_pos = msg->msg.wParam;
3883
3884 switch (LOWORD (msg->msg.wParam))
3885 {
3886 case SB_LINEDOWN:
3887 emacs_event->part = scroll_bar_down_arrow;
3888 break;
3889 case SB_LINEUP:
3890 emacs_event->part = scroll_bar_up_arrow;
3891 break;
3892 case SB_PAGEUP:
3893 emacs_event->part = scroll_bar_above_handle;
3894 break;
3895 case SB_PAGEDOWN:
3896 emacs_event->part = scroll_bar_below_handle;
3897 break;
3898 case SB_TOP:
3899 emacs_event->part = scroll_bar_handle;
3900 y = 0;
3901 break;
3902 case SB_BOTTOM:
3903 emacs_event->part = scroll_bar_handle;
3904 y = top_range;
3905 break;
3906 case SB_THUMBTRACK:
3907 case SB_THUMBPOSITION:
3908 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3909 y = HIWORD (msg->msg.wParam);
3910 bar->dragging = Qt;
3911 emacs_event->part = scroll_bar_handle;
3912
3913 /* "Silently" update current position. */
3914 {
3915 SCROLLINFO si;
3916
3917 si.cbSize = sizeof (si);
3918 si.fMask = SIF_POS;
3919 si.nPos = y;
3920 /* Remember apparent position (we actually lag behind the real
3921 position, so don't set that directly. */
3922 last_scroll_bar_drag_pos = y;
3923
3924 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
3925 }
3926 break;
3927 case SB_ENDSCROLL:
3928 /* If this is the end of a drag sequence, then reset the scroll
3929 handle size to normal and do a final redraw. Otherwise do
3930 nothing. */
3931 if (dragging)
3932 {
3933 SCROLLINFO si;
3934 int start = XINT (bar->start);
3935 int end = XINT (bar->end);
3936
3937 si.cbSize = sizeof (si);
3938 si.fMask = SIF_PAGE | SIF_POS;
3939 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3940 si.nPos = last_scroll_bar_drag_pos;
3941 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
3942 }
3943 /* fall through */
3944 default:
3945 emacs_event->kind = NO_EVENT;
3946 return FALSE;
3947 }
3948
3949 XSETINT (emacs_event->x, y);
3950 XSETINT (emacs_event->y, top_range);
3951
3952 return TRUE;
3953 }
3954 }
3955
3956 /* Return information to the user about the current position of the mouse
3957 on the scroll bar. */
3958
3959 static void
3960 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
3961 FRAME_PTR *fp;
3962 Lisp_Object *bar_window;
3963 enum scroll_bar_part *part;
3964 Lisp_Object *x, *y;
3965 unsigned long *time;
3966 {
3967 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
3968 Window w = SCROLL_BAR_W32_WINDOW (bar);
3969 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3970 int pos;
3971 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3972 SCROLLINFO si;
3973
3974 BLOCK_INPUT;
3975
3976 *fp = f;
3977 *bar_window = bar->window;
3978
3979 si.cbSize = sizeof (si);
3980 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
3981
3982 GetScrollInfo (w, SB_CTL, &si);
3983 pos = si.nPos;
3984 top_range = si.nMax - si.nPage + 1;
3985
3986 switch (LOWORD (last_mouse_scroll_bar_pos))
3987 {
3988 case SB_THUMBPOSITION:
3989 case SB_THUMBTRACK:
3990 *part = scroll_bar_handle;
3991 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3992 pos = HIWORD (last_mouse_scroll_bar_pos);
3993 break;
3994 case SB_LINEDOWN:
3995 *part = scroll_bar_handle;
3996 pos++;
3997 break;
3998 default:
3999 *part = scroll_bar_handle;
4000 break;
4001 }
4002
4003 XSETINT (*x, pos);
4004 XSETINT (*y, top_range);
4005
4006 f->mouse_moved = 0;
4007 last_mouse_scroll_bar = Qnil;
4008
4009 *time = last_mouse_movement_time;
4010
4011 UNBLOCK_INPUT;
4012 }
4013
4014
4015 /* The screen has been cleared so we may have changed foreground or
4016 background colors, and the scroll bars may need to be redrawn.
4017 Clear out the scroll bars, and ask for expose events, so we can
4018 redraw them. */
4019
4020 void
4021 x_scroll_bar_clear (f)
4022 FRAME_PTR f;
4023 {
4024 Lisp_Object bar;
4025
4026 /* We can have scroll bars even if this is 0,
4027 if we just turned off scroll bar mode.
4028 But in that case we should not clear them. */
4029 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4030 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4031 bar = XSCROLL_BAR (bar)->next)
4032 {
4033 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4034 HDC hdc = GetDC (window);
4035 RECT rect;
4036
4037 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4038 arranges to refresh the scroll bar if hidden. */
4039 my_show_window (f, window, SW_HIDE);
4040
4041 GetClientRect (window, &rect);
4042 select_palette (f, hdc);
4043 w32_clear_rect (f, hdc, &rect);
4044 deselect_palette (f, hdc);
4045
4046 ReleaseDC (window, hdc);
4047 }
4048 }
4049
4050 \f
4051 /* The main W32 event-reading loop - w32_read_socket. */
4052
4053 /* Record the last 100 characters stored
4054 to help debug the loss-of-chars-during-GC problem. */
4055
4056 static int temp_index;
4057 static short temp_buffer[100];
4058
4059 /* Temporarily store lead byte of DBCS input sequences. */
4060 static char dbcs_lead = 0;
4061
4062 /* Read events coming from the W32 shell.
4063 This routine is called by the SIGIO handler.
4064 We return as soon as there are no more events to be read.
4065
4066 We return the number of characters stored into the buffer,
4067 thus pretending to be `read'.
4068
4069 EXPECTED is nonzero if the caller knows input is available.
4070
4071 Some of these messages are reposted back to the message queue since the
4072 system calls the windows proc directly in a context where we cannot return
4073 the data nor can we guarantee the state we are in. So if we dispatch them
4074 we will get into an infinite loop. To prevent this from ever happening we
4075 will set a variable to indicate we are in the read_socket call and indicate
4076 which message we are processing since the windows proc gets called
4077 recursively with different messages by the system.
4078 */
4079
4080 int
4081 w32_read_socket (sd, expected, hold_quit)
4082 register int sd;
4083 int expected;
4084 struct input_event *hold_quit;
4085 {
4086 int count = 0;
4087 int check_visibility = 0;
4088 W32Msg msg;
4089 struct frame *f;
4090 struct w32_display_info *dpyinfo = &one_w32_display_info;
4091
4092 if (interrupt_input_blocked)
4093 {
4094 interrupt_input_pending = 1;
4095 return -1;
4096 }
4097
4098 interrupt_input_pending = 0;
4099 BLOCK_INPUT;
4100
4101 /* So people can tell when we have read the available input. */
4102 input_signal_count++;
4103
4104 /* TODO: ghostscript integration. */
4105 while (get_next_msg (&msg, FALSE))
4106 {
4107 struct input_event inev;
4108 int do_help = 0;
4109
4110 EVENT_INIT (inev);
4111 inev.kind = NO_EVENT;
4112 inev.arg = Qnil;
4113
4114 switch (msg.msg.message)
4115 {
4116 case WM_EMACS_PAINT:
4117 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4118
4119 if (f)
4120 {
4121 if (msg.rect.right == msg.rect.left ||
4122 msg.rect.bottom == msg.rect.top)
4123 {
4124 /* We may get paint messages even though the client
4125 area is clipped - these are not expose events. */
4126 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4127 SDATA (f->name)));
4128 }
4129 else if (f->async_visible != 1)
4130 {
4131 /* Definitely not obscured, so mark as visible. */
4132 f->async_visible = 1;
4133 f->async_iconified = 0;
4134 SET_FRAME_GARBAGED (f);
4135 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4136 SDATA (f->name)));
4137
4138 /* WM_PAINT serves as MapNotify as well, so report
4139 visibility changes properly. */
4140 if (f->iconified)
4141 {
4142 inev.kind = DEICONIFY_EVENT;
4143 XSETFRAME (inev.frame_or_window, f);
4144 }
4145 else if (! NILP (Vframe_list)
4146 && ! NILP (XCDR (Vframe_list)))
4147 /* Force a redisplay sooner or later to update the
4148 frame titles in case this is the second frame. */
4149 record_asynch_buffer_change ();
4150 }
4151 else
4152 {
4153 HDC hdc = get_frame_dc (f);
4154
4155 /* Erase background again for safety. */
4156 w32_clear_rect (f, hdc, &msg.rect);
4157 release_frame_dc (f, hdc);
4158 expose_frame (f,
4159 msg.rect.left,
4160 msg.rect.top,
4161 msg.rect.right - msg.rect.left,
4162 msg.rect.bottom - msg.rect.top);
4163 }
4164 }
4165 break;
4166
4167 case WM_INPUTLANGCHANGE:
4168 /* Generate a language change event. */
4169 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4170
4171 /* lParam contains the input lang ID. Use it to update our
4172 record of the keyboard codepage. */
4173 keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4174 & 0xffff));
4175
4176 if (f)
4177 {
4178 inev.kind = LANGUAGE_CHANGE_EVENT;
4179 XSETFRAME (inev.frame_or_window, f);
4180 inev.code = msg.msg.wParam;
4181 inev.modifiers = msg.msg.lParam & 0xffff;
4182 }
4183 break;
4184
4185 case WM_KEYDOWN:
4186 case WM_SYSKEYDOWN:
4187 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4188
4189 if (f && !f->iconified)
4190 {
4191 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4192 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4193 {
4194 clear_mouse_face (dpyinfo);
4195 dpyinfo->mouse_face_hidden = 1;
4196 }
4197
4198 if (temp_index == sizeof temp_buffer / sizeof (short))
4199 temp_index = 0;
4200 temp_buffer[temp_index++] = msg.msg.wParam;
4201 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4202 inev.code = msg.msg.wParam;
4203 inev.modifiers = msg.dwModifiers;
4204 XSETFRAME (inev.frame_or_window, f);
4205 inev.timestamp = msg.msg.time;
4206 }
4207 break;
4208
4209 case WM_UNICHAR:
4210 case WM_SYSCHAR:
4211 case WM_CHAR:
4212 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4213
4214 if (f && !f->iconified)
4215 {
4216 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4217 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4218 {
4219 clear_mouse_face (dpyinfo);
4220 dpyinfo->mouse_face_hidden = 1;
4221 }
4222
4223 if (temp_index == sizeof temp_buffer / sizeof (short))
4224 temp_index = 0;
4225 temp_buffer[temp_index++] = msg.msg.wParam;
4226
4227 inev.modifiers = msg.dwModifiers;
4228 XSETFRAME (inev.frame_or_window, f);
4229 inev.timestamp = msg.msg.time;
4230
4231 if (msg.msg.message == WM_UNICHAR)
4232 {
4233 inev.code = msg.msg.wParam;
4234 }
4235 else if (msg.msg.wParam < 256)
4236 {
4237 wchar_t code;
4238 char dbcs[2];
4239 dbcs[0] = 0;
4240 dbcs[1] = (char) msg.msg.wParam;
4241
4242 if (dbcs_lead)
4243 {
4244 dbcs[0] = dbcs_lead;
4245 dbcs_lead = 0;
4246 if (!MultiByteToWideChar (keyboard_codepage, 0,
4247 dbcs, 2, &code, 1))
4248 {
4249 /* Garbage */
4250 DebPrint (("Invalid DBCS sequence: %d %d\n",
4251 dbcs[0], dbcs[1]));
4252 inev.kind = NO_EVENT;
4253 break;
4254 }
4255 }
4256 else if (IsDBCSLeadByteEx (keyboard_codepage,
4257 (BYTE) msg.msg.wParam))
4258 {
4259 dbcs_lead = (char) msg.msg.wParam;
4260 inev.kind = NO_EVENT;
4261 break;
4262 }
4263 else
4264 {
4265 if (!MultiByteToWideChar (keyboard_codepage, 0,
4266 &dbcs[1], 1, &code, 1))
4267 {
4268 /* What to do with garbage? */
4269 DebPrint (("Invalid character: %d\n", dbcs[1]));
4270 inev.kind = NO_EVENT;
4271 break;
4272 }
4273 }
4274 inev.code = code;
4275 }
4276 else
4277 {
4278 /* Windows shouldn't generate WM_CHAR events above 0xFF
4279 in non-Unicode message handlers. */
4280 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4281 inev.kind = NO_EVENT;
4282 break;
4283 }
4284 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4285 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4286 }
4287 break;
4288
4289 case WM_APPCOMMAND:
4290 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4291
4292 if (f && !f->iconified)
4293 {
4294 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4295 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4296 {
4297 clear_mouse_face (dpyinfo);
4298 dpyinfo->mouse_face_hidden = 1;
4299 }
4300
4301 if (temp_index == sizeof temp_buffer / sizeof (short))
4302 temp_index = 0;
4303 temp_buffer[temp_index++] = msg.msg.wParam;
4304 inev.kind = MULTIMEDIA_KEY_EVENT;
4305 inev.code = GET_APPCOMMAND_LPARAM(msg.msg.lParam);
4306 inev.modifiers = msg.dwModifiers;
4307 XSETFRAME (inev.frame_or_window, f);
4308 inev.timestamp = msg.msg.time;
4309 }
4310 break;
4311
4312 case WM_MOUSEMOVE:
4313 /* Ignore non-movement. */
4314 {
4315 int x = LOWORD (msg.msg.lParam);
4316 int y = HIWORD (msg.msg.lParam);
4317 if (x == last_mousemove_x && y == last_mousemove_y)
4318 break;
4319 last_mousemove_x = x;
4320 last_mousemove_y = y;
4321 }
4322
4323 previous_help_echo_string = help_echo_string;
4324 help_echo_string = Qnil;
4325
4326 if (dpyinfo->grabbed && last_mouse_frame
4327 && FRAME_LIVE_P (last_mouse_frame))
4328 f = last_mouse_frame;
4329 else
4330 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4331
4332 if (dpyinfo->mouse_face_hidden)
4333 {
4334 dpyinfo->mouse_face_hidden = 0;
4335 clear_mouse_face (dpyinfo);
4336 }
4337
4338 if (f)
4339 {
4340 /* Generate SELECT_WINDOW_EVENTs when needed. */
4341 if (!NILP (Vmouse_autoselect_window))
4342 {
4343 Lisp_Object window;
4344 int x = LOWORD (msg.msg.lParam);
4345 int y = HIWORD (msg.msg.lParam);
4346
4347 window = window_from_coordinates (f, x, y, 0, 0, 0, 0);
4348
4349 /* Window will be selected only when it is not
4350 selected now and last mouse movement event was
4351 not in it. Minibuffer window will be selected
4352 only when it is active. */
4353 if (WINDOWP(window)
4354 && !EQ (window, last_window)
4355 && !EQ (window, selected_window)
4356 /* For click-to-focus window managers
4357 create event iff we don't leave the
4358 selected frame. */
4359 && (focus_follows_mouse
4360 || (EQ (XWINDOW (window)->frame,
4361 XWINDOW (selected_window)->frame))))
4362 {
4363 inev.kind = SELECT_WINDOW_EVENT;
4364 inev.frame_or_window = window;
4365 }
4366
4367 last_window=window;
4368 }
4369 if (!note_mouse_movement (f, &msg.msg))
4370 help_echo_string = previous_help_echo_string;
4371 }
4372 else
4373 {
4374 /* If we move outside the frame, then we're
4375 certainly no longer on any text in the frame. */
4376 clear_mouse_face (dpyinfo);
4377 }
4378
4379 /* If the contents of the global variable help_echo_string
4380 has changed, generate a HELP_EVENT. */
4381 #if 0 /* The below is an invalid comparison when USE_LISP_UNION_TYPE.
4382 But it was originally changed to this to fix a bug, so I have
4383 not removed it completely in case the bug is still there. */
4384 if (help_echo_string != previous_help_echo_string ||
4385 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4386 #else /* This is what xterm.c does. */
4387 if (!NILP (help_echo_string)
4388 || !NILP (previous_help_echo_string))
4389 do_help = 1;
4390 #endif
4391 break;
4392
4393 case WM_LBUTTONDOWN:
4394 case WM_LBUTTONUP:
4395 case WM_MBUTTONDOWN:
4396 case WM_MBUTTONUP:
4397 case WM_RBUTTONDOWN:
4398 case WM_RBUTTONUP:
4399 case WM_XBUTTONDOWN:
4400 case WM_XBUTTONUP:
4401 {
4402 /* If we decide we want to generate an event to be seen
4403 by the rest of Emacs, we put it here. */
4404 int tool_bar_p = 0;
4405 int button;
4406 int up;
4407
4408 if (dpyinfo->grabbed && last_mouse_frame
4409 && FRAME_LIVE_P (last_mouse_frame))
4410 f = last_mouse_frame;
4411 else
4412 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4413
4414 if (f)
4415 {
4416 construct_mouse_click (&inev, &msg, f);
4417
4418 /* Is this in the tool-bar? */
4419 if (WINDOWP (f->tool_bar_window)
4420 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4421 {
4422 Lisp_Object window;
4423 int x = XFASTINT (inev.x);
4424 int y = XFASTINT (inev.y);
4425
4426 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
4427
4428 if (EQ (window, f->tool_bar_window))
4429 {
4430 w32_handle_tool_bar_click (f, &inev);
4431 tool_bar_p = 1;
4432 }
4433 }
4434
4435 if (tool_bar_p
4436 || (dpyinfo->w32_focus_frame
4437 && f != dpyinfo->w32_focus_frame))
4438 inev.kind = NO_EVENT;
4439 }
4440
4441 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4442 &button, &up);
4443
4444 if (up)
4445 {
4446 dpyinfo->grabbed &= ~ (1 << button);
4447 }
4448 else
4449 {
4450 dpyinfo->grabbed |= (1 << button);
4451 last_mouse_frame = f;
4452 /* Ignore any mouse motion that happened
4453 before this event; any subsequent mouse-movement
4454 Emacs events should reflect only motion after
4455 the ButtonPress. */
4456 if (f != 0)
4457 f->mouse_moved = 0;
4458
4459 if (!tool_bar_p)
4460 last_tool_bar_item = -1;
4461 }
4462 break;
4463 }
4464
4465 case WM_MOUSEWHEEL:
4466 case WM_MOUSEHWHEEL:
4467 {
4468 if (dpyinfo->grabbed && last_mouse_frame
4469 && FRAME_LIVE_P (last_mouse_frame))
4470 f = last_mouse_frame;
4471 else
4472 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4473
4474 if (f)
4475 {
4476
4477 if (!dpyinfo->w32_focus_frame
4478 || f == dpyinfo->w32_focus_frame)
4479 {
4480 /* Emit an Emacs wheel-up/down event. */
4481 construct_mouse_wheel (&inev, &msg, f);
4482 }
4483 /* Ignore any mouse motion that happened before this
4484 event; any subsequent mouse-movement Emacs events
4485 should reflect only motion after the
4486 ButtonPress. */
4487 f->mouse_moved = 0;
4488 }
4489 last_mouse_frame = f;
4490 last_tool_bar_item = -1;
4491 }
4492 break;
4493
4494 case WM_DROPFILES:
4495 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4496
4497 if (f)
4498 construct_drag_n_drop (&inev, &msg, f);
4499 break;
4500
4501 case WM_VSCROLL:
4502 {
4503 struct scroll_bar *bar =
4504 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4505
4506 if (bar)
4507 w32_scroll_bar_handle_click (bar, &msg, &inev);
4508 break;
4509 }
4510
4511 case WM_WINDOWPOSCHANGED:
4512 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4513 if (f)
4514 {
4515 if (f->want_fullscreen & FULLSCREEN_WAIT)
4516 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
4517 }
4518 check_visibility = 1;
4519 break;
4520
4521 case WM_ACTIVATE:
4522 case WM_ACTIVATEAPP:
4523 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4524 if (f)
4525 x_check_fullscreen (f);
4526 check_visibility = 1;
4527 break;
4528
4529 case WM_MOVE:
4530 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4531
4532 if (f && !f->async_iconified)
4533 {
4534 int x, y;
4535
4536 x_real_positions (f, &x, &y);
4537 f->left_pos = x;
4538 f->top_pos = y;
4539 }
4540
4541 check_visibility = 1;
4542 break;
4543
4544 case WM_SHOWWINDOW:
4545 /* wParam non-zero means Window is about to be shown, 0 means
4546 about to be hidden. */
4547 /* Redo the mouse-highlight after the tooltip has gone. */
4548 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4549 {
4550 tip_window = NULL;
4551 redo_mouse_highlight ();
4552 }
4553
4554 /* If window has been obscured or exposed by another window
4555 being maximised or minimised/restored, then recheck
4556 visibility of all frames. Direct changes to our own
4557 windows get handled by WM_SIZE. */
4558 #if 0
4559 if (msg.msg.lParam != 0)
4560 check_visibility = 1;
4561 else
4562 {
4563 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4564 f->async_visible = msg.msg.wParam;
4565 }
4566 #endif
4567
4568 check_visibility = 1;
4569 break;
4570
4571 case WM_SIZE:
4572 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4573
4574 /* Inform lisp of whether frame has been iconified etc. */
4575 if (f)
4576 {
4577 switch (msg.msg.wParam)
4578 {
4579 case SIZE_MINIMIZED:
4580 f->async_visible = 0;
4581 f->async_iconified = 1;
4582
4583 inev.kind = ICONIFY_EVENT;
4584 XSETFRAME (inev.frame_or_window, f);
4585 break;
4586
4587 case SIZE_MAXIMIZED:
4588 case SIZE_RESTORED:
4589 f->async_visible = 1;
4590 f->async_iconified = 0;
4591
4592 /* wait_reading_process_output will notice this and update
4593 the frame's display structures. */
4594 SET_FRAME_GARBAGED (f);
4595
4596 if (f->iconified)
4597 {
4598 int x, y;
4599
4600 /* Reset top and left positions of the Window
4601 here since Windows sends a WM_MOVE message
4602 BEFORE telling us the Window is minimized
4603 when the Window is iconified, with 3000,3000
4604 as the co-ords. */
4605 x_real_positions (f, &x, &y);
4606 f->left_pos = x;
4607 f->top_pos = y;
4608
4609 inev.kind = DEICONIFY_EVENT;
4610 XSETFRAME (inev.frame_or_window, f);
4611 }
4612 else if (! NILP (Vframe_list)
4613 && ! NILP (XCDR (Vframe_list)))
4614 /* Force a redisplay sooner or later
4615 to update the frame titles
4616 in case this is the second frame. */
4617 record_asynch_buffer_change ();
4618 break;
4619 }
4620 }
4621
4622 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
4623 {
4624 RECT rect;
4625 int rows;
4626 int columns;
4627 int width;
4628 int height;
4629
4630 GetClientRect (msg.msg.hwnd, &rect);
4631
4632 height = rect.bottom - rect.top;
4633 width = rect.right - rect.left;
4634
4635 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4636 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4637
4638 /* TODO: Clip size to the screen dimensions. */
4639
4640 /* Even if the number of character rows and columns has
4641 not changed, the font size may have changed, so we need
4642 to check the pixel dimensions as well. */
4643
4644 if (columns != FRAME_COLS (f)
4645 || rows != FRAME_LINES (f)
4646 || width != FRAME_PIXEL_WIDTH (f)
4647 || height != FRAME_PIXEL_HEIGHT (f))
4648 {
4649 change_frame_size (f, rows, columns, 0, 1, 0);
4650 SET_FRAME_GARBAGED (f);
4651 cancel_mouse_face (f);
4652 FRAME_PIXEL_WIDTH (f) = width;
4653 FRAME_PIXEL_HEIGHT (f) = height;
4654 f->win_gravity = NorthWestGravity;
4655 }
4656 }
4657
4658 check_visibility = 1;
4659 break;
4660
4661 case WM_MOUSELEAVE:
4662 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4663 if (f)
4664 {
4665 if (f == dpyinfo->mouse_face_mouse_frame)
4666 {
4667 /* If we move outside the frame, then we're
4668 certainly no longer on any text in the frame. */
4669 clear_mouse_face (dpyinfo);
4670 dpyinfo->mouse_face_mouse_frame = 0;
4671 }
4672
4673 /* Generate a nil HELP_EVENT to cancel a help-echo.
4674 Do it only if there's something to cancel.
4675 Otherwise, the startup message is cleared when
4676 the mouse leaves the frame. */
4677 if (any_help_event_p)
4678 do_help = -1;
4679 }
4680 break;
4681
4682 case WM_SETFOCUS:
4683 w32_detect_focus_change (dpyinfo, &msg, &inev);
4684
4685 dpyinfo->grabbed = 0;
4686 check_visibility = 1;
4687 break;
4688
4689 case WM_KILLFOCUS:
4690 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4691
4692 if (f)
4693 {
4694 if (f == dpyinfo->w32_focus_event_frame)
4695 dpyinfo->w32_focus_event_frame = 0;
4696
4697 if (f == dpyinfo->w32_focus_frame)
4698 x_new_focus_frame (dpyinfo, 0);
4699
4700 if (f == dpyinfo->mouse_face_mouse_frame)
4701 {
4702 /* If we move outside the frame, then we're
4703 certainly no longer on any text in the frame. */
4704 clear_mouse_face (dpyinfo);
4705 dpyinfo->mouse_face_mouse_frame = 0;
4706 }
4707
4708 /* Generate a nil HELP_EVENT to cancel a help-echo.
4709 Do it only if there's something to cancel.
4710 Otherwise, the startup message is cleared when
4711 the mouse leaves the frame. */
4712 if (any_help_event_p)
4713 do_help = -1;
4714 }
4715
4716 dpyinfo->grabbed = 0;
4717 check_visibility = 1;
4718 break;
4719
4720 case WM_CLOSE:
4721 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4722
4723 if (f)
4724 {
4725 inev.kind = DELETE_WINDOW_EVENT;
4726 XSETFRAME (inev.frame_or_window, f);
4727 }
4728 break;
4729
4730 case WM_INITMENU:
4731 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4732
4733 if (f)
4734 {
4735 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4736 XSETFRAME (inev.frame_or_window, f);
4737 }
4738 break;
4739
4740 case WM_COMMAND:
4741 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4742
4743 if (f)
4744 {
4745 extern void menubar_selection_callback
4746 (FRAME_PTR f, void * client_data);
4747 menubar_selection_callback (f, (void *)msg.msg.wParam);
4748 }
4749
4750 check_visibility = 1;
4751 break;
4752
4753 case WM_DISPLAYCHANGE:
4754 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4755
4756 if (f)
4757 {
4758 dpyinfo->n_cbits = msg.msg.wParam;
4759 DebPrint (("display change: %d %d\n",
4760 (short) LOWORD (msg.msg.lParam),
4761 (short) HIWORD (msg.msg.lParam)));
4762 }
4763
4764 check_visibility = 1;
4765 break;
4766
4767 default:
4768 /* Check for messages registered at runtime. */
4769 if (msg.msg.message == msh_mousewheel)
4770 {
4771 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4772 msg.msg.message = WM_MOUSEWHEEL;
4773 prepend_msg (&msg);
4774 }
4775 break;
4776 }
4777
4778 if (inev.kind != NO_EVENT)
4779 {
4780 kbd_buffer_store_event_hold (&inev, hold_quit);
4781 count++;
4782 }
4783
4784 if (do_help
4785 && !(hold_quit && hold_quit->kind != NO_EVENT))
4786 {
4787 Lisp_Object frame;
4788
4789 if (f)
4790 XSETFRAME (frame, f);
4791 else
4792 frame = Qnil;
4793
4794 if (do_help > 0)
4795 {
4796 if (NILP (help_echo_string))
4797 {
4798 help_echo_object = help_echo_window = Qnil;
4799 help_echo_pos = -1;
4800 }
4801
4802 any_help_event_p = 1;
4803 gen_help_event (help_echo_string, frame, help_echo_window,
4804 help_echo_object, help_echo_pos);
4805 }
4806 else
4807 {
4808 help_echo_string = Qnil;
4809 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4810 }
4811 count++;
4812 }
4813 }
4814
4815 /* If the focus was just given to an autoraising frame,
4816 raise it now. */
4817 /* ??? This ought to be able to handle more than one such frame. */
4818 if (pending_autoraise_frame)
4819 {
4820 x_raise_frame (pending_autoraise_frame);
4821 pending_autoraise_frame = 0;
4822 }
4823
4824 /* Check which frames are still visisble, if we have enqueued any user
4825 events or been notified of events that may affect visibility. We
4826 do this here because there doesn't seem to be any direct
4827 notification from Windows that the visibility of a window has
4828 changed (at least, not in all cases). */
4829 if (count > 0 || check_visibility)
4830 {
4831 Lisp_Object tail, frame;
4832
4833 FOR_EACH_FRAME (tail, frame)
4834 {
4835 FRAME_PTR f = XFRAME (frame);
4836 /* The tooltip has been drawn already. Avoid the
4837 SET_FRAME_GARBAGED below. */
4838 if (EQ (frame, tip_frame))
4839 continue;
4840
4841 /* Check "visible" frames and mark each as obscured or not.
4842 Note that async_visible is nonzero for unobscured and
4843 obscured frames, but zero for hidden and iconified frames. */
4844 if (FRAME_W32_P (f) && f->async_visible)
4845 {
4846 RECT clipbox;
4847 HDC hdc;
4848
4849 enter_crit ();
4850 /* Query clipping rectangle for the entire window area
4851 (GetWindowDC), not just the client portion (GetDC).
4852 Otherwise, the scrollbars and menubar aren't counted as
4853 part of the visible area of the frame, and we may think
4854 the frame is obscured when really a scrollbar is still
4855 visible and gets WM_PAINT messages above. */
4856 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
4857 GetClipBox (hdc, &clipbox);
4858 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
4859 leave_crit ();
4860
4861 if (clipbox.right == clipbox.left
4862 || clipbox.bottom == clipbox.top)
4863 {
4864 /* Frame has become completely obscured so mark as
4865 such (we do this by setting async_visible to 2 so
4866 that FRAME_VISIBLE_P is still true, but redisplay
4867 will skip it). */
4868 f->async_visible = 2;
4869
4870 if (!FRAME_OBSCURED_P (f))
4871 {
4872 DebPrint (("frame %p (%s) obscured\n", f,
4873 SDATA (f->name)));
4874 }
4875 }
4876 else
4877 {
4878 /* Frame is not obscured, so mark it as such. */
4879 f->async_visible = 1;
4880
4881 if (FRAME_OBSCURED_P (f))
4882 {
4883 SET_FRAME_GARBAGED (f);
4884 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
4885 SDATA (f->name)));
4886
4887 /* Force a redisplay sooner or later. */
4888 record_asynch_buffer_change ();
4889 }
4890 }
4891 }
4892 }
4893 }
4894
4895 UNBLOCK_INPUT;
4896 return count;
4897 }
4898
4899
4900 \f
4901 /***********************************************************************
4902 Text Cursor
4903 ***********************************************************************/
4904
4905 /* Set clipping for output in glyph row ROW. W is the window in which
4906 we operate. GC is the graphics context to set clipping in.
4907
4908 ROW may be a text row or, e.g., a mode line. Text rows must be
4909 clipped to the interior of the window dedicated to text display,
4910 mode lines must be clipped to the whole window. */
4911
4912 static void
4913 w32_clip_to_row (w, row, area, hdc)
4914 struct window *w;
4915 struct glyph_row *row;
4916 int area;
4917 HDC hdc;
4918 {
4919 struct frame *f = XFRAME (WINDOW_FRAME (w));
4920 RECT clip_rect;
4921 int window_x, window_y, window_width;
4922
4923 window_box (w, area, &window_x, &window_y, &window_width, 0);
4924
4925 clip_rect.left = window_x;
4926 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4927 clip_rect.top = max (clip_rect.top, window_y);
4928 clip_rect.right = clip_rect.left + window_width;
4929 clip_rect.bottom = clip_rect.top + row->visible_height;
4930
4931 w32_set_clip_rectangle (hdc, &clip_rect);
4932 }
4933
4934
4935 /* Draw a hollow box cursor on window W in glyph row ROW. */
4936
4937 static void
4938 x_draw_hollow_cursor (w, row)
4939 struct window *w;
4940 struct glyph_row *row;
4941 {
4942 struct frame *f = XFRAME (WINDOW_FRAME (w));
4943 HDC hdc;
4944 RECT rect;
4945 int left, top, h;
4946 struct glyph *cursor_glyph;
4947 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
4948
4949 /* Get the glyph the cursor is on. If we can't tell because
4950 the current matrix is invalid or such, give up. */
4951 cursor_glyph = get_phys_cursor_glyph (w);
4952 if (cursor_glyph == NULL)
4953 return;
4954
4955 /* Compute frame-relative coordinates for phys cursor. */
4956 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
4957 rect.left = left;
4958 rect.top = top;
4959 rect.bottom = rect.top + h;
4960 rect.right = rect.left + w->phys_cursor_width;
4961
4962 hdc = get_frame_dc (f);
4963 /* Set clipping, draw the rectangle, and reset clipping again. */
4964 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4965 FrameRect (hdc, &rect, hb);
4966 DeleteObject (hb);
4967 w32_set_clip_rectangle (hdc, NULL);
4968 release_frame_dc (f, hdc);
4969 }
4970
4971
4972 /* Draw a bar cursor on window W in glyph row ROW.
4973
4974 Implementation note: One would like to draw a bar cursor with an
4975 angle equal to the one given by the font property XA_ITALIC_ANGLE.
4976 Unfortunately, I didn't find a font yet that has this property set.
4977 --gerd. */
4978
4979 static void
4980 x_draw_bar_cursor (w, row, width, kind)
4981 struct window *w;
4982 struct glyph_row *row;
4983 int width;
4984 enum text_cursor_kinds kind;
4985 {
4986 struct frame *f = XFRAME (w->frame);
4987 struct glyph *cursor_glyph;
4988
4989 /* If cursor is out of bounds, don't draw garbage. This can happen
4990 in mini-buffer windows when switching between echo area glyphs
4991 and mini-buffer. */
4992 cursor_glyph = get_phys_cursor_glyph (w);
4993 if (cursor_glyph == NULL)
4994 return;
4995
4996 /* If on an image, draw like a normal cursor. That's usually better
4997 visible than drawing a bar, esp. if the image is large so that
4998 the bar might not be in the window. */
4999 if (cursor_glyph->type == IMAGE_GLYPH)
5000 {
5001 struct glyph_row *row;
5002 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5003 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5004 }
5005 else
5006 {
5007 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5008 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5009 int x;
5010 HDC hdc;
5011
5012 /* If the glyph's background equals the color we normally draw
5013 the bar cursor in, the bar cursor in its normal color is
5014 invisible. Use the glyph's foreground color instead in this
5015 case, on the assumption that the glyph's colors are chosen so
5016 that the glyph is legible. */
5017 if (face->background == cursor_color)
5018 cursor_color = face->foreground;
5019
5020 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5021
5022 hdc = get_frame_dc (f);
5023 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5024
5025 if (kind == BAR_CURSOR)
5026 {
5027 if (width < 0)
5028 width = FRAME_CURSOR_WIDTH (f);
5029 width = min (cursor_glyph->pixel_width, width);
5030
5031 w->phys_cursor_width = width;
5032
5033 w32_fill_area (f, hdc, cursor_color, x,
5034 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5035 width, row->height);
5036 }
5037 else
5038 {
5039 int dummy_x, dummy_y, dummy_h;
5040
5041 if (width < 0)
5042 width = row->height;
5043
5044 width = min (row->height, width);
5045
5046 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
5047 &dummy_y, &dummy_h);
5048 w32_fill_area (f, hdc, cursor_color, x,
5049 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5050 row->height - width),
5051 w->phys_cursor_width, width);
5052 }
5053
5054 w32_set_clip_rectangle (hdc, NULL);
5055 release_frame_dc (f, hdc);
5056 }
5057 }
5058
5059
5060 /* RIF: Define cursor CURSOR on frame F. */
5061
5062 static void
5063 w32_define_frame_cursor (f, cursor)
5064 struct frame *f;
5065 Cursor cursor;
5066 {
5067 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5068 }
5069
5070
5071 /* RIF: Clear area on frame F. */
5072
5073 static void
5074 w32_clear_frame_area (f, x, y, width, height)
5075 struct frame *f;
5076 int x, y, width, height;
5077 {
5078 HDC hdc;
5079
5080 hdc = get_frame_dc (f);
5081 w32_clear_area (f, hdc, x, y, width, height);
5082 release_frame_dc (f, hdc);
5083 }
5084
5085 /* RIF: Draw or clear cursor on window W. */
5086
5087 static void
5088 w32_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
5089 struct window *w;
5090 struct glyph_row *glyph_row;
5091 int x, y;
5092 int cursor_type, cursor_width;
5093 int on_p, active_p;
5094 {
5095 if (on_p)
5096 {
5097 /* If the user wants to use the system caret, make sure our own
5098 cursor remains invisible. */
5099 if (w32_use_visible_system_caret)
5100 {
5101 /* Call to erase_phys_cursor here seems to use the
5102 wrong values of w->phys_cursor, as they have been
5103 overwritten before this function was called. */
5104 if (w->phys_cursor_type != NO_CURSOR)
5105 erase_phys_cursor (w);
5106
5107 cursor_type = w->phys_cursor_type = NO_CURSOR;
5108 w->phys_cursor_width = -1;
5109 }
5110 else
5111 {
5112 w->phys_cursor_type = cursor_type;
5113 }
5114
5115 w->phys_cursor_on_p = 1;
5116
5117 /* If this is the active cursor, we need to track it with the
5118 system caret, so third party software like screen magnifiers
5119 and speech synthesizers can follow the cursor. */
5120 if (active_p)
5121 {
5122 struct frame *f = XFRAME (WINDOW_FRAME (w));
5123 HWND hwnd = FRAME_W32_WINDOW (f);
5124
5125 w32_system_caret_x
5126 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5127 w32_system_caret_y
5128 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5129 + glyph_row->ascent - w->phys_cursor_ascent);
5130
5131 /* If the size of the active cursor changed, destroy the old
5132 system caret. */
5133 if (w32_system_caret_hwnd
5134 && (w32_system_caret_height != w->phys_cursor_height))
5135 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5136
5137 w32_system_caret_height = w->phys_cursor_height;
5138
5139 /* Move the system caret. */
5140 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5141 }
5142
5143 if (glyph_row->exact_window_width_line_p
5144 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
5145 {
5146 glyph_row->cursor_in_fringe_p = 1;
5147 draw_fringe_bitmap (w, glyph_row, 0);
5148 return;
5149 }
5150
5151 switch (cursor_type)
5152 {
5153 case HOLLOW_BOX_CURSOR:
5154 x_draw_hollow_cursor (w, glyph_row);
5155 break;
5156
5157 case FILLED_BOX_CURSOR:
5158 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5159 break;
5160
5161 case BAR_CURSOR:
5162 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5163 break;
5164
5165 case HBAR_CURSOR:
5166 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5167 break;
5168
5169 case NO_CURSOR:
5170 w->phys_cursor_width = 0;
5171 break;
5172
5173 default:
5174 abort ();
5175 }
5176 }
5177 }
5178
5179
5180 \f
5181 /* Icons. */
5182
5183 int
5184 x_bitmap_icon (f, icon)
5185 struct frame *f;
5186 Lisp_Object icon;
5187 {
5188 HANDLE main_icon;
5189 HANDLE small_icon = NULL;
5190
5191 if (FRAME_W32_WINDOW (f) == 0)
5192 return 1;
5193
5194 if (NILP (icon))
5195 main_icon = LoadIcon (hinst, EMACS_CLASS);
5196 else if (STRINGP (icon))
5197 {
5198 /* Load the main icon from the named file. */
5199 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5200 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5201 /* Try to load a small icon to go with it. */
5202 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5203 GetSystemMetrics (SM_CXSMICON),
5204 GetSystemMetrics (SM_CYSMICON),
5205 LR_LOADFROMFILE);
5206 }
5207 else if (SYMBOLP (icon))
5208 {
5209 LPCTSTR name;
5210
5211 if (EQ (icon, intern ("application")))
5212 name = (LPCTSTR) IDI_APPLICATION;
5213 else if (EQ (icon, intern ("hand")))
5214 name = (LPCTSTR) IDI_HAND;
5215 else if (EQ (icon, intern ("question")))
5216 name = (LPCTSTR) IDI_QUESTION;
5217 else if (EQ (icon, intern ("exclamation")))
5218 name = (LPCTSTR) IDI_EXCLAMATION;
5219 else if (EQ (icon, intern ("asterisk")))
5220 name = (LPCTSTR) IDI_ASTERISK;
5221 else if (EQ (icon, intern ("winlogo")))
5222 name = (LPCTSTR) IDI_WINLOGO;
5223 else
5224 return 1;
5225
5226 main_icon = LoadIcon (NULL, name);
5227 }
5228 else
5229 return 1;
5230
5231 if (main_icon == NULL)
5232 return 1;
5233
5234 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5235 (LPARAM) main_icon);
5236
5237 /* If there is a small icon that goes with it, set that too. */
5238 if (small_icon)
5239 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5240 (LPARAM) small_icon);
5241
5242 return 0;
5243 }
5244
5245 \f
5246 /************************************************************************
5247 Handling X errors
5248 ************************************************************************/
5249
5250 /* Display Error Handling functions not used on W32. Listing them here
5251 helps diff stay in step when comparing w32term.c with xterm.c.
5252
5253 x_error_catcher (display, error)
5254 x_catch_errors (dpy)
5255 x_catch_errors_unwind (old_val)
5256 x_check_errors (dpy, format)
5257 x_fully_uncatch_errors ()
5258 x_catching_errors ()
5259 x_had_errors_p (dpy)
5260 x_clear_errors (dpy)
5261 x_uncatch_errors (dpy, count)
5262 x_trace_wire ()
5263 x_connection_signal (signalnum)
5264 x_connection_closed (dpy, error_message)
5265 x_error_quitter (display, error)
5266 x_error_handler (display, error)
5267 x_io_error_quitter (display)
5268
5269 */
5270
5271 \f
5272 /* Changing the font of the frame. */
5273
5274 Lisp_Object
5275 x_new_font (f, font_object, fontset)
5276 struct frame *f;
5277 Lisp_Object font_object;
5278 int fontset;
5279 {
5280 struct font *font = XFONT_OBJECT (font_object);
5281
5282 if (fontset < 0)
5283 fontset = fontset_from_font (font_object);
5284 FRAME_FONTSET (f) = fontset;
5285 if (FRAME_FONT (f) == font)
5286 /* This font is already set in frame F. There's nothing more to
5287 do. */
5288 return font_object;
5289
5290 FRAME_FONT (f) = font;
5291 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5292 FRAME_COLUMN_WIDTH (f) = font->average_width;
5293 FRAME_SPACE_WIDTH (f) = font->space_width;
5294 FRAME_LINE_HEIGHT (f) = font->height;
5295
5296 compute_fringe_widths (f, 1);
5297
5298 /* Compute the scroll bar width in character columns. */
5299 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5300 {
5301 int wid = FRAME_COLUMN_WIDTH (f);
5302 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5303 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5304 }
5305 else
5306 {
5307 int wid = FRAME_COLUMN_WIDTH (f);
5308 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5309 }
5310
5311 /* Now make the frame display the given font. */
5312 if (FRAME_X_WINDOW (f) != 0)
5313 {
5314 /* Don't change the size of a tip frame; there's no point in
5315 doing it because it's done in Fx_show_tip, and it leads to
5316 problems because the tip frame has no widget. */
5317 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5318 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5319 }
5320
5321 /* X version sets font of input methods here also. */
5322
5323 return font_object;
5324 }
5325
5326 \f
5327 /***********************************************************************
5328 TODO: W32 Input Methods
5329 ***********************************************************************/
5330 /* Listing missing functions from xterm.c helps diff stay in step.
5331
5332 xim_destroy_callback (xim, client_data, call_data)
5333 xim_open_dpy (dpyinfo, resource_name)
5334 struct xim_inst_t
5335 xim_instantiate_callback (display, client_data, call_data)
5336 xim_initialize (dpyinfo, resource_name)
5337 xim_close_dpy (dpyinfo)
5338
5339 */
5340
5341 \f
5342 /* Calculate the absolute position in frame F
5343 from its current recorded position values and gravity. */
5344
5345 void
5346 x_calc_absolute_position (f)
5347 struct frame *f;
5348 {
5349 int flags = f->size_hint_flags;
5350
5351 /* The sum of the widths of the frame's left and right borders, and
5352 the sum of the heights of the frame's top and bottom borders (in
5353 pixels) drawn by Windows. */
5354 unsigned int left_right_borders_width, top_bottom_borders_height;
5355
5356 /* Try to get the actual values of these two variables. We compute
5357 the border width (height) by subtracting the width (height) of
5358 the frame's client area from the width (height) of the frame's
5359 entire window. */
5360 WINDOWPLACEMENT wp = { 0 };
5361 RECT client_rect = { 0 };
5362
5363 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5364 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5365 {
5366 left_right_borders_width =
5367 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5368 (client_rect.right - client_rect.left);
5369
5370 top_bottom_borders_height =
5371 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5372 (client_rect.bottom - client_rect.top);
5373 }
5374 else
5375 {
5376 /* Use sensible default values. */
5377 left_right_borders_width = 8;
5378 top_bottom_borders_height = 32;
5379 }
5380
5381 /* Treat negative positions as relative to the rightmost bottommost
5382 position that fits on the screen. */
5383 if (flags & XNegative)
5384 f->left_pos = (x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f))
5385 - FRAME_PIXEL_WIDTH (f)
5386 + f->left_pos
5387 - (left_right_borders_width - 1));
5388
5389 if (flags & YNegative)
5390 f->top_pos = (x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f))
5391 - FRAME_PIXEL_HEIGHT (f)
5392 + f->top_pos
5393 - (top_bottom_borders_height - 1));
5394
5395 /* The left_pos and top_pos are now relative to the top and left
5396 screen edges, so the flags should correspond. */
5397 f->size_hint_flags &= ~ (XNegative | YNegative);
5398 }
5399
5400 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5401 to really change the position, and 0 when calling from
5402 x_make_frame_visible (in that case, XOFF and YOFF are the current
5403 position values). It is -1 when calling from x_set_frame_parameters,
5404 which means, do adjust for borders but don't change the gravity. */
5405
5406 void
5407 x_set_offset (f, xoff, yoff, change_gravity)
5408 struct frame *f;
5409 register int xoff, yoff;
5410 int change_gravity;
5411 {
5412 int modified_top, modified_left;
5413
5414 if (change_gravity > 0)
5415 {
5416 f->top_pos = yoff;
5417 f->left_pos = xoff;
5418 f->size_hint_flags &= ~ (XNegative | YNegative);
5419 if (xoff < 0)
5420 f->size_hint_flags |= XNegative;
5421 if (yoff < 0)
5422 f->size_hint_flags |= YNegative;
5423 f->win_gravity = NorthWestGravity;
5424 }
5425 x_calc_absolute_position (f);
5426
5427 BLOCK_INPUT;
5428 x_wm_set_size_hint (f, (long) 0, 0);
5429
5430 modified_left = f->left_pos;
5431 modified_top = f->top_pos;
5432
5433 my_set_window_pos (FRAME_W32_WINDOW (f),
5434 NULL,
5435 modified_left, modified_top,
5436 0, 0,
5437 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5438 UNBLOCK_INPUT;
5439 }
5440
5441
5442 /* Check if we need to resize the frame due to a fullscreen request.
5443 If so needed, resize the frame. */
5444 static void
5445 x_check_fullscreen (f)
5446 struct frame *f;
5447 {
5448 if (f->want_fullscreen & FULLSCREEN_BOTH)
5449 {
5450 int width, height, ign;
5451
5452 x_real_positions (f, &f->left_pos, &f->top_pos);
5453
5454 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5455
5456 /* We do not need to move the window, it shall be taken care of
5457 when setting WM manager hints. */
5458 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5459 {
5460 change_frame_size (f, height, width, 0, 1, 0);
5461 SET_FRAME_GARBAGED (f);
5462 cancel_mouse_face (f);
5463
5464 /* Wait for the change of frame size to occur */
5465 f->want_fullscreen |= FULLSCREEN_WAIT;
5466 }
5467 }
5468 }
5469
5470 /* Call this to change the size of frame F's x-window.
5471 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5472 for this size change and subsequent size changes.
5473 Otherwise we leave the window gravity unchanged. */
5474
5475 void
5476 x_set_window_size (f, change_gravity, cols, rows)
5477 struct frame *f;
5478 int change_gravity;
5479 int cols, rows;
5480 {
5481 int pixelwidth, pixelheight;
5482
5483 BLOCK_INPUT;
5484
5485 check_frame_size (f, &rows, &cols);
5486 f->scroll_bar_actual_width
5487 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5488
5489 compute_fringe_widths (f, 0);
5490
5491 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5492 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5493
5494 f->win_gravity = NorthWestGravity;
5495 x_wm_set_size_hint (f, (long) 0, 0);
5496
5497 {
5498 RECT rect;
5499
5500 rect.left = rect.top = 0;
5501 rect.right = pixelwidth;
5502 rect.bottom = pixelheight;
5503
5504 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
5505 FRAME_EXTERNAL_MENU_BAR (f));
5506
5507 my_set_window_pos (FRAME_W32_WINDOW (f),
5508 NULL,
5509 0, 0,
5510 rect.right - rect.left,
5511 rect.bottom - rect.top,
5512 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5513 }
5514
5515 #if 0
5516 /* The following mirrors what is done in xterm.c. It appears to be
5517 for informing lisp of the new size immediately, while the actual
5518 resize will happen asynchronously. But on Windows, the menu bar
5519 automatically wraps when the frame is too narrow to contain it,
5520 and that causes any calculations made here to come out wrong. The
5521 end is some nasty buggy behavior, including the potential loss
5522 of the minibuffer.
5523
5524 Disabling this code is either not sufficient to fix the problems
5525 completely, or it causes fresh problems, but at least it removes
5526 the most problematic symptom of the minibuffer becoming unusable.
5527
5528 -----------------------------------------------------------------
5529
5530 Now, strictly speaking, we can't be sure that this is accurate,
5531 but the window manager will get around to dealing with the size
5532 change request eventually, and we'll hear how it went when the
5533 ConfigureNotify event gets here.
5534
5535 We could just not bother storing any of this information here,
5536 and let the ConfigureNotify event set everything up, but that
5537 might be kind of confusing to the Lisp code, since size changes
5538 wouldn't be reported in the frame parameters until some random
5539 point in the future when the ConfigureNotify event arrives.
5540
5541 We pass 1 for DELAY since we can't run Lisp code inside of
5542 a BLOCK_INPUT. */
5543 change_frame_size (f, rows, cols, 0, 1, 0);
5544 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5545 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5546
5547 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5548 receive in the ConfigureNotify event; if we get what we asked
5549 for, then the event won't cause the screen to become garbaged, so
5550 we have to make sure to do it here. */
5551 SET_FRAME_GARBAGED (f);
5552
5553 /* If cursor was outside the new size, mark it as off. */
5554 mark_window_cursors_off (XWINDOW (f->root_window));
5555
5556 /* Clear out any recollection of where the mouse highlighting was,
5557 since it might be in a place that's outside the new frame size.
5558 Actually checking whether it is outside is a pain in the neck,
5559 so don't try--just let the highlighting be done afresh with new size. */
5560 cancel_mouse_face (f);
5561 #endif
5562
5563 UNBLOCK_INPUT;
5564 }
5565 \f
5566 /* Mouse warping. */
5567
5568 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5569
5570 void
5571 x_set_mouse_position (f, x, y)
5572 struct frame *f;
5573 int x, y;
5574 {
5575 int pix_x, pix_y;
5576
5577 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5578 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5579
5580 if (pix_x < 0) pix_x = 0;
5581 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5582
5583 if (pix_y < 0) pix_y = 0;
5584 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5585
5586 x_set_mouse_pixel_position (f, pix_x, pix_y);
5587 }
5588
5589 void
5590 x_set_mouse_pixel_position (f, pix_x, pix_y)
5591 struct frame *f;
5592 int pix_x, pix_y;
5593 {
5594 RECT rect;
5595 POINT pt;
5596
5597 BLOCK_INPUT;
5598
5599 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5600 pt.x = rect.left + pix_x;
5601 pt.y = rect.top + pix_y;
5602 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5603
5604 SetCursorPos (pt.x, pt.y);
5605
5606 UNBLOCK_INPUT;
5607 }
5608
5609 \f
5610 /* focus shifting, raising and lowering. */
5611
5612 void
5613 x_focus_on_frame (f)
5614 struct frame *f;
5615 {
5616 struct w32_display_info *dpyinfo = &one_w32_display_info;
5617
5618 /* Give input focus to frame. */
5619 BLOCK_INPUT;
5620 #if 0
5621 /* Try not to change its Z-order if possible. */
5622 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5623 my_set_focus (f, FRAME_W32_WINDOW (f));
5624 else
5625 #endif
5626 my_set_foreground_window (FRAME_W32_WINDOW (f));
5627 UNBLOCK_INPUT;
5628 }
5629
5630 void
5631 x_unfocus_frame (f)
5632 struct frame *f;
5633 {
5634 }
5635
5636 /* Raise frame F. */
5637 void
5638 x_raise_frame (f)
5639 struct frame *f;
5640 {
5641 BLOCK_INPUT;
5642
5643 /* Strictly speaking, raise-frame should only change the frame's Z
5644 order, leaving input focus unchanged. This is reasonable behavior
5645 on X where the usual policy is point-to-focus. However, this
5646 behavior would be very odd on Windows where the usual policy is
5647 click-to-focus.
5648
5649 On X, if the mouse happens to be over the raised frame, it gets
5650 input focus anyway (so the window with focus will never be
5651 completely obscured) - if not, then just moving the mouse over it
5652 is sufficient to give it focus. On Windows, the user must actually
5653 click on the frame (preferrably the title bar so as not to move
5654 point), which is more awkward. Also, no other Windows program
5655 raises a window to the top but leaves another window (possibly now
5656 completely obscured) with input focus.
5657
5658 Because there is a system setting on Windows that allows the user
5659 to choose the point to focus policy, we make the strict semantics
5660 optional, but by default we grab focus when raising. */
5661
5662 if (NILP (Vw32_grab_focus_on_raise))
5663 {
5664 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5665 not already the foreground application: the frame is raised
5666 above all other frames belonging to us, but not above the
5667 current top window. To achieve that, we have to resort to this
5668 more cumbersome method. */
5669
5670 HDWP handle = BeginDeferWindowPos (2);
5671 if (handle)
5672 {
5673 DeferWindowPos (handle,
5674 FRAME_W32_WINDOW (f),
5675 HWND_TOP,
5676 0, 0, 0, 0,
5677 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5678
5679 DeferWindowPos (handle,
5680 GetForegroundWindow (),
5681 FRAME_W32_WINDOW (f),
5682 0, 0, 0, 0,
5683 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5684
5685 EndDeferWindowPos (handle);
5686 }
5687 }
5688 else
5689 {
5690 my_set_foreground_window (FRAME_W32_WINDOW (f));
5691 }
5692
5693 UNBLOCK_INPUT;
5694 }
5695
5696 /* Lower frame F. */
5697 void
5698 x_lower_frame (f)
5699 struct frame *f;
5700 {
5701 BLOCK_INPUT;
5702 my_set_window_pos (FRAME_W32_WINDOW (f),
5703 HWND_BOTTOM,
5704 0, 0, 0, 0,
5705 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5706 UNBLOCK_INPUT;
5707 }
5708
5709 static void
5710 w32_frame_raise_lower (f, raise_flag)
5711 FRAME_PTR f;
5712 int raise_flag;
5713 {
5714 if (! FRAME_W32_P (f))
5715 return;
5716
5717 if (raise_flag)
5718 x_raise_frame (f);
5719 else
5720 x_lower_frame (f);
5721 }
5722 \f
5723 /* Change of visibility. */
5724
5725 /* This tries to wait until the frame is really visible.
5726 However, if the window manager asks the user where to position
5727 the frame, this will return before the user finishes doing that.
5728 The frame will not actually be visible at that time,
5729 but it will become visible later when the window manager
5730 finishes with it. */
5731
5732 void
5733 x_make_frame_visible (f)
5734 struct frame *f;
5735 {
5736 Lisp_Object type;
5737
5738 BLOCK_INPUT;
5739
5740 type = x_icon_type (f);
5741 if (!NILP (type))
5742 x_bitmap_icon (f, type);
5743
5744 if (! FRAME_VISIBLE_P (f))
5745 {
5746 /* We test FRAME_GARBAGED_P here to make sure we don't
5747 call x_set_offset a second time
5748 if we get to x_make_frame_visible a second time
5749 before the window gets really visible. */
5750 if (! FRAME_ICONIFIED_P (f)
5751 && ! f->output_data.w32->asked_for_visible)
5752 {
5753 RECT workarea_rect;
5754 RECT window_rect;
5755
5756 /* Adjust vertical window position in order to avoid being
5757 covered by a task bar placed at the bottom of the desktop. */
5758 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
5759 GetWindowRect(FRAME_W32_WINDOW(f), &window_rect);
5760 if (window_rect.bottom > workarea_rect.bottom
5761 && window_rect.top > workarea_rect.top)
5762 f->top_pos = max (window_rect.top
5763 - window_rect.bottom + workarea_rect.bottom,
5764 workarea_rect.top);
5765
5766 x_set_offset (f, f->left_pos, f->top_pos, 0);
5767 }
5768
5769 f->output_data.w32->asked_for_visible = 1;
5770
5771 /* The first of these seems to give more expected behavior, but
5772 was added as a commented out line in Sept 1997, with the
5773 second version remaining uncommented. There may have been
5774 some problem with it that led to it not being enabled,
5775 so the old version remains commented out below in case we
5776 decide we need to go back to it [23.0.60 2008-06-09]. */
5777 my_show_window (f, FRAME_W32_WINDOW (f),
5778 f->async_iconified ? SW_RESTORE : SW_SHOW);
5779 /* my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL); */
5780 }
5781
5782 /* Synchronize to ensure Emacs knows the frame is visible
5783 before we do anything else. We do this loop with input not blocked
5784 so that incoming events are handled. */
5785 {
5786 Lisp_Object frame;
5787 int count;
5788
5789 /* This must come after we set COUNT. */
5790 UNBLOCK_INPUT;
5791
5792 XSETFRAME (frame, f);
5793
5794 /* Wait until the frame is visible. Process X events until a
5795 MapNotify event has been seen, or until we think we won't get a
5796 MapNotify at all.. */
5797 for (count = input_signal_count + 10;
5798 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5799 {
5800 /* Force processing of queued events. */
5801 /* TODO: x_sync equivalent? */
5802
5803 /* Machines that do polling rather than SIGIO have been observed
5804 to go into a busy-wait here. So we'll fake an alarm signal
5805 to let the handler know that there's something to be read.
5806 We used to raise a real alarm, but it seems that the handler
5807 isn't always enabled here. This is probably a bug. */
5808 if (input_polling_used ())
5809 {
5810 /* It could be confusing if a real alarm arrives while processing
5811 the fake one. Turn it off and let the handler reset it. */
5812 int old_poll_suppress_count = poll_suppress_count;
5813 poll_suppress_count = 1;
5814 poll_for_input_1 ();
5815 poll_suppress_count = old_poll_suppress_count;
5816 }
5817 }
5818 FRAME_SAMPLE_VISIBILITY (f);
5819 }
5820 }
5821
5822 /* Change from mapped state to withdrawn state. */
5823
5824 /* Make the frame visible (mapped and not iconified). */
5825
5826 x_make_frame_invisible (f)
5827 struct frame *f;
5828 {
5829 /* Don't keep the highlight on an invisible frame. */
5830 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5831 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5832
5833 BLOCK_INPUT;
5834
5835 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
5836
5837 /* We can't distinguish this from iconification
5838 just by the event that we get from the server.
5839 So we can't win using the usual strategy of letting
5840 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5841 and synchronize with the server to make sure we agree. */
5842 f->visible = 0;
5843 FRAME_ICONIFIED_P (f) = 0;
5844 f->async_visible = 0;
5845 f->async_iconified = 0;
5846
5847 UNBLOCK_INPUT;
5848 }
5849
5850 /* Change window state from mapped to iconified. */
5851
5852 void
5853 x_iconify_frame (f)
5854 struct frame *f;
5855 {
5856 Lisp_Object type;
5857
5858 /* Don't keep the highlight on an invisible frame. */
5859 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5860 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5861
5862 if (f->async_iconified)
5863 return;
5864
5865 BLOCK_INPUT;
5866
5867 type = x_icon_type (f);
5868 if (!NILP (type))
5869 x_bitmap_icon (f, type);
5870
5871 /* Simulate the user minimizing the frame. */
5872 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
5873
5874 UNBLOCK_INPUT;
5875 }
5876
5877 \f
5878 /* Free X resources of frame F. */
5879
5880 void
5881 x_free_frame_resources (f)
5882 struct frame *f;
5883 {
5884 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5885
5886 BLOCK_INPUT;
5887
5888 /* We must free faces before destroying windows because some
5889 font-driver (e.g. xft) access a window while finishing a
5890 face. */
5891 if (FRAME_FACE_CACHE (f))
5892 free_frame_faces (f);
5893
5894 if (FRAME_W32_WINDOW (f))
5895 my_destroy_window (f, FRAME_W32_WINDOW (f));
5896
5897 free_frame_menubar (f);
5898
5899 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
5900 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
5901 unload_color (f, f->output_data.w32->cursor_pixel);
5902 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
5903 unload_color (f, f->output_data.w32->border_pixel);
5904 unload_color (f, f->output_data.w32->mouse_pixel);
5905 if (f->output_data.w32->white_relief.allocated_p)
5906 unload_color (f, f->output_data.w32->white_relief.pixel);
5907 if (f->output_data.w32->black_relief.allocated_p)
5908 unload_color (f, f->output_data.w32->black_relief.pixel);
5909
5910 if (FRAME_FACE_CACHE (f))
5911 free_frame_faces (f);
5912
5913 xfree (f->output_data.w32);
5914 f->output_data.w32 = NULL;
5915
5916 if (f == dpyinfo->w32_focus_frame)
5917 dpyinfo->w32_focus_frame = 0;
5918 if (f == dpyinfo->w32_focus_event_frame)
5919 dpyinfo->w32_focus_event_frame = 0;
5920 if (f == dpyinfo->x_highlight_frame)
5921 dpyinfo->x_highlight_frame = 0;
5922
5923 if (f == dpyinfo->mouse_face_mouse_frame)
5924 {
5925 dpyinfo->mouse_face_beg_row
5926 = dpyinfo->mouse_face_beg_col = -1;
5927 dpyinfo->mouse_face_end_row
5928 = dpyinfo->mouse_face_end_col = -1;
5929 dpyinfo->mouse_face_window = Qnil;
5930 dpyinfo->mouse_face_deferred_gc = 0;
5931 dpyinfo->mouse_face_mouse_frame = 0;
5932 }
5933
5934 UNBLOCK_INPUT;
5935 }
5936
5937
5938 /* Destroy the window of frame F. */
5939 void
5940 x_destroy_window (f)
5941 struct frame *f;
5942 {
5943 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5944
5945 x_free_frame_resources (f);
5946 dpyinfo->reference_count--;
5947 }
5948
5949 \f
5950 /* Setting window manager hints. */
5951
5952 /* Set the normal size hints for the window manager, for frame F.
5953 FLAGS is the flags word to use--or 0 meaning preserve the flags
5954 that the window now has.
5955 If USER_POSITION is nonzero, we set the USPosition
5956 flag (this is useful when FLAGS is 0). */
5957 void
5958 x_wm_set_size_hint (f, flags, user_position)
5959 struct frame *f;
5960 long flags;
5961 int user_position;
5962 {
5963 Window window = FRAME_W32_WINDOW (f);
5964
5965 enter_crit ();
5966
5967 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
5968 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
5969 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
5970 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
5971
5972 leave_crit ();
5973 }
5974
5975 /* Window manager things */
5976 void
5977 x_wm_set_icon_position (f, icon_x, icon_y)
5978 struct frame *f;
5979 int icon_x, icon_y;
5980 {
5981 #if 0
5982 Window window = FRAME_W32_WINDOW (f);
5983
5984 f->display.x->wm_hints.flags |= IconPositionHint;
5985 f->display.x->wm_hints.icon_x = icon_x;
5986 f->display.x->wm_hints.icon_y = icon_y;
5987
5988 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
5989 #endif
5990 }
5991
5992 \f
5993 /***********************************************************************
5994 Initialization
5995 ***********************************************************************/
5996
5997 static int w32_initialized = 0;
5998
5999 void
6000 w32_initialize_display_info (display_name)
6001 Lisp_Object display_name;
6002 {
6003 struct w32_display_info *dpyinfo = &one_w32_display_info;
6004
6005 bzero (dpyinfo, sizeof (*dpyinfo));
6006
6007 /* Put it on w32_display_name_list. */
6008 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
6009 w32_display_name_list);
6010 dpyinfo->name_list_element = XCAR (w32_display_name_list);
6011
6012 dpyinfo->w32_id_name
6013 = (char *) xmalloc (SCHARS (Vinvocation_name)
6014 + SCHARS (Vsystem_name)
6015 + 2);
6016 sprintf (dpyinfo->w32_id_name, "%s@%s",
6017 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6018
6019 /* Default Console mode values - overridden when running in GUI mode
6020 with values obtained from system metrics. */
6021 dpyinfo->resx = 1;
6022 dpyinfo->resy = 1;
6023 dpyinfo->n_planes = 1;
6024 dpyinfo->n_cbits = 4;
6025 dpyinfo->n_fonts = 0;
6026 dpyinfo->smallest_font_height = 1;
6027 dpyinfo->smallest_char_width = 1;
6028
6029 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6030 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6031 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
6032 dpyinfo->mouse_face_window = Qnil;
6033 dpyinfo->mouse_face_overlay = Qnil;
6034 dpyinfo->mouse_face_hidden = 0;
6035
6036 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6037 /* TODO: dpyinfo->gray */
6038
6039 }
6040
6041 /* Create an xrdb-style database of resources to supercede registry settings.
6042 The database is just a concatenation of C strings, finished by an additional
6043 \0. The strings are submitted to some basic normalization, so
6044
6045 [ *]option[ *]:[ *]value...
6046
6047 becomes
6048
6049 option:value...
6050
6051 but any whitespace following value is not removed. */
6052
6053 static char *
6054 w32_make_rdb (xrm_option)
6055 char *xrm_option;
6056 {
6057 char *buffer = xmalloc (strlen (xrm_option) + 2);
6058 char *current = buffer;
6059 char ch;
6060 int in_option = 1;
6061 int before_value = 0;
6062
6063 do {
6064 ch = *xrm_option++;
6065
6066 if (ch == '\n')
6067 {
6068 *current++ = '\0';
6069 in_option = 1;
6070 before_value = 0;
6071 }
6072 else if (ch != ' ')
6073 {
6074 *current++ = ch;
6075 if (in_option && (ch == ':'))
6076 {
6077 in_option = 0;
6078 before_value = 1;
6079 }
6080 else if (before_value)
6081 {
6082 before_value = 0;
6083 }
6084 }
6085 else if (!(in_option || before_value))
6086 {
6087 *current++ = ch;
6088 }
6089 } while (ch);
6090
6091 *current = '\0';
6092
6093 return buffer;
6094 }
6095
6096 void
6097 x_flush (struct frame * f)
6098 { /* Nothing to do */ }
6099
6100
6101 extern frame_parm_handler w32_frame_parm_handlers[];
6102
6103 static struct redisplay_interface w32_redisplay_interface =
6104 {
6105 w32_frame_parm_handlers,
6106 x_produce_glyphs,
6107 x_write_glyphs,
6108 x_insert_glyphs,
6109 x_clear_end_of_line,
6110 x_scroll_run,
6111 x_after_update_window_line,
6112 x_update_window_begin,
6113 x_update_window_end,
6114 x_cursor_to,
6115 x_flush,
6116 0, /* flush_display_optional */
6117 x_clear_window_mouse_face,
6118 x_get_glyph_overhangs,
6119 x_fix_overlapping_area,
6120 w32_draw_fringe_bitmap,
6121 w32_define_fringe_bitmap,
6122 w32_destroy_fringe_bitmap,
6123 w32_compute_glyph_string_overhangs,
6124 x_draw_glyph_string,
6125 w32_define_frame_cursor,
6126 w32_clear_frame_area,
6127 w32_draw_window_cursor,
6128 w32_draw_vertical_window_border,
6129 w32_shift_glyphs_for_insert
6130 };
6131
6132 static void x_delete_terminal (struct terminal *term);
6133
6134 static struct terminal *
6135 w32_create_terminal (struct w32_display_info *dpyinfo)
6136 {
6137 struct terminal *terminal;
6138
6139 terminal = create_terminal ();
6140
6141 terminal->type = output_w32;
6142 terminal->display_info.w32 = dpyinfo;
6143 dpyinfo->terminal = terminal;
6144
6145 /* MSVC does not type K&R functions with no arguments correctly, and
6146 so we must explicitly cast them. */
6147 terminal->clear_frame_hook = x_clear_frame;
6148 terminal->ins_del_lines_hook = x_ins_del_lines;
6149 terminal->delete_glyphs_hook = x_delete_glyphs;
6150 terminal->ring_bell_hook = w32_ring_bell;
6151 terminal->reset_terminal_modes_hook = w32_reset_terminal_modes;
6152 terminal->set_terminal_modes_hook = w32_set_terminal_modes;
6153 terminal->update_begin_hook = x_update_begin;
6154 terminal->update_end_hook = x_update_end;
6155 terminal->set_terminal_window_hook = w32_set_terminal_window;
6156 terminal->read_socket_hook = w32_read_socket;
6157 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6158 terminal->mouse_position_hook = w32_mouse_position;
6159 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6160 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6161 // terminal->fullscreen_hook = XTfullscreen_hook;
6162 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6163 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6164 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6165 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6166
6167 terminal->delete_frame_hook = x_destroy_window;
6168 terminal->delete_terminal_hook = x_delete_terminal;
6169
6170 terminal->rif = &w32_redisplay_interface;
6171 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
6172 terminal->char_ins_del_ok = 1;
6173 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
6174 terminal->fast_clear_end_of_line = 1; /* X does this well. */
6175 terminal->memory_below_frame = 0; /* We don't remember what scrolls
6176 off the bottom. */
6177
6178 /* We don't yet support separate terminals on W32, so don't try to share
6179 keyboards between virtual terminals that are on the same physical
6180 terminal like X does. */
6181 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
6182 init_kboard (terminal->kboard);
6183 terminal->kboard->Vwindow_system = intern ("w32");
6184 terminal->kboard->next_kboard = all_kboards;
6185 all_kboards = terminal->kboard;
6186 /* Don't let the initial kboard remain current longer than necessary.
6187 That would cause problems if a file loaded on startup tries to
6188 prompt in the mini-buffer. */
6189 if (current_kboard == initial_kboard)
6190 current_kboard = terminal->kboard;
6191 terminal->kboard->reference_count++;
6192
6193 return terminal;
6194 }
6195
6196 static void
6197 x_delete_terminal (struct terminal *terminal)
6198 {
6199 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6200 int i;
6201
6202 /* Protect against recursive calls. delete_frame in
6203 delete_terminal calls us back when it deletes our last frame. */
6204 if (!terminal->name)
6205 return;
6206
6207 BLOCK_INPUT;
6208
6209 x_delete_display (dpyinfo);
6210 UNBLOCK_INPUT;
6211 }
6212
6213 struct w32_display_info *
6214 w32_term_init (display_name, xrm_option, resource_name)
6215 Lisp_Object display_name;
6216 char *xrm_option;
6217 char *resource_name;
6218 {
6219 struct w32_display_info *dpyinfo;
6220 struct terminal *terminal;
6221 HDC hdc;
6222
6223 BLOCK_INPUT;
6224
6225 if (!w32_initialized)
6226 {
6227 w32_initialize ();
6228 w32_initialized = 1;
6229 }
6230
6231 w32_initialize_display_info (display_name);
6232
6233 dpyinfo = &one_w32_display_info;
6234 terminal = w32_create_terminal (dpyinfo);
6235
6236 /* Set the name of the terminal. */
6237 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
6238 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
6239 terminal->name[SBYTES (display_name)] = 0;
6240
6241 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6242
6243 /* Put this display on the chain. */
6244 dpyinfo->next = x_display_list;
6245 x_display_list = dpyinfo;
6246
6247 hdc = GetDC (NULL);
6248
6249 dpyinfo->root_window = GetDesktopWindow ();
6250 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6251 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6252 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6253 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6254 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6255 ReleaseDC (NULL, hdc);
6256
6257 /* initialise palette with white and black */
6258 {
6259 XColor color;
6260 w32_defined_color (0, "white", &color, 1);
6261 w32_defined_color (0, "black", &color, 1);
6262 }
6263
6264 /* Add the default keyboard. */
6265 add_keyboard_wait_descriptor (0);
6266
6267 /* Create Fringe Bitmaps and store them for later use.
6268
6269 On W32, bitmaps are all unsigned short, as Windows requires
6270 bitmap data to be Word aligned. For some reason they are
6271 horizontally reflected compared to how they appear on X, so we
6272 need to bitswap and convert to unsigned shorts before creating
6273 the bitmaps. */
6274 w32_init_fringe (terminal->rif);
6275
6276 #ifdef F_SETOWN
6277 fcntl (connection, F_SETOWN, getpid ());
6278 #endif /* ! defined (F_SETOWN) */
6279
6280 #ifdef SIGIO
6281 if (interrupt_input)
6282 init_sigio (connection);
6283 #endif /* ! defined (SIGIO) */
6284
6285 UNBLOCK_INPUT;
6286
6287 return dpyinfo;
6288 }
6289 \f
6290 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6291 void
6292 x_delete_display (dpyinfo)
6293 struct w32_display_info *dpyinfo;
6294 {
6295 /* Discard this display from w32_display_name_list and w32_display_list.
6296 We can't use Fdelq because that can quit. */
6297 if (! NILP (w32_display_name_list)
6298 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
6299 w32_display_name_list = XCDR (w32_display_name_list);
6300 else
6301 {
6302 Lisp_Object tail;
6303
6304 tail = w32_display_name_list;
6305 while (CONSP (tail) && CONSP (XCDR (tail)))
6306 {
6307 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
6308 {
6309 XSETCDR (tail, XCDR (XCDR (tail)));
6310 break;
6311 }
6312 tail = XCDR (tail);
6313 }
6314 }
6315
6316 /* free palette table */
6317 {
6318 struct w32_palette_entry * plist;
6319
6320 plist = dpyinfo->color_list;
6321 while (plist)
6322 {
6323 struct w32_palette_entry * pentry = plist;
6324 plist = plist->next;
6325 xfree (pentry);
6326 }
6327 dpyinfo->color_list = NULL;
6328 if (dpyinfo->palette)
6329 DeleteObject(dpyinfo->palette);
6330 }
6331 xfree (dpyinfo->w32_id_name);
6332
6333 w32_reset_fringes ();
6334 }
6335 \f
6336 /* Set up use of W32. */
6337
6338 DWORD WINAPI w32_msg_worker (void * arg);
6339
6340 static void
6341 w32_initialize ()
6342 {
6343 HANDLE shell;
6344 HRESULT (WINAPI * set_user_model) (PCWSTR);
6345
6346 baud_rate = 19200;
6347
6348 w32_system_caret_hwnd = NULL;
6349 w32_system_caret_height = 0;
6350 w32_system_caret_x = 0;
6351 w32_system_caret_y = 0;
6352
6353 /* On Windows 7 and later, we need to set the user model ID
6354 to associate emacsclient launched files with Emacs frames
6355 in the UI. */
6356 shell = GetModuleHandle ("shell32.dll");
6357 if (shell)
6358 {
6359 set_user_model
6360 = (void *) GetProcAddress (shell,
6361 "SetCurrentProcessExplicitAppUserModelID");
6362
6363 /* If the function is defined, then we are running on Windows 7
6364 or newer, and the UI uses this to group related windows
6365 together. Since emacs, runemacs, emacsclient are related, we
6366 want them grouped even though the executables are different,
6367 so we need to set a consistent ID between them. */
6368 if (set_user_model)
6369 set_user_model (L"GNU.Emacs");
6370 }
6371
6372 /* Initialize w32_use_visible_system_caret based on whether a screen
6373 reader is in use. */
6374 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6375 &w32_use_visible_system_caret, 0))
6376 w32_use_visible_system_caret = 0;
6377
6378 last_tool_bar_item = -1;
6379 any_help_event_p = 0;
6380
6381 /* Initialize input mode: interrupt_input off, no flow control, allow
6382 8 bit character input, standard quit char. */
6383 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6384
6385 {
6386 DWORD input_locale_id = (DWORD) GetKeyboardLayout (0);
6387 keyboard_codepage = codepage_for_locale ((LCID) (input_locale_id & 0xffff));
6388 }
6389
6390 /* Create the window thread - it will terminate itself when the app
6391 terminates */
6392 init_crit ();
6393
6394 dwMainThreadId = GetCurrentThreadId ();
6395 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6396 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6397
6398 /* Wait for thread to start */
6399 {
6400 MSG msg;
6401
6402 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6403
6404 hWindowsThread = CreateThread (NULL, 0,
6405 w32_msg_worker,
6406 0, 0, &dwWindowsThreadId);
6407
6408 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6409 }
6410
6411 /* It is desirable that mainThread should have the same notion of
6412 focus window and active window as windowsThread. Unfortunately, the
6413 following call to AttachThreadInput, which should do precisely what
6414 we need, causes major problems when Emacs is linked as a console
6415 program. Unfortunately, we have good reasons for doing that, so
6416 instead we need to send messages to windowsThread to make some API
6417 calls for us (ones that affect, or depend on, the active/focus
6418 window state.) */
6419 #ifdef ATTACH_THREADS
6420 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6421 #endif
6422
6423 /* Dynamically link to optional system components. */
6424 {
6425 HMODULE user_lib = GetModuleHandle ("user32.dll");
6426
6427 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6428
6429 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6430
6431 #undef LOAD_PROC
6432
6433 /* Ensure scrollbar handle is at least 5 pixels. */
6434 vertical_scroll_bar_min_handle = 5;
6435
6436 /* For either kind of scroll bar, take account of the arrows; these
6437 effectively form the border of the main scroll bar range. */
6438 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6439 = GetSystemMetrics (SM_CYVSCROLL);
6440 }
6441 }
6442
6443 void
6444 syms_of_w32term ()
6445 {
6446 staticpro (&w32_display_name_list);
6447 w32_display_name_list = Qnil;
6448
6449 staticpro (&last_mouse_scroll_bar);
6450 last_mouse_scroll_bar = Qnil;
6451
6452 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6453
6454 DEFVAR_INT ("w32-num-mouse-buttons",
6455 &w32_num_mouse_buttons,
6456 doc: /* Number of physical mouse buttons. */);
6457 w32_num_mouse_buttons = 2;
6458
6459 DEFVAR_LISP ("w32-swap-mouse-buttons",
6460 &Vw32_swap_mouse_buttons,
6461 doc: /* Swap the mapping of middle and right mouse buttons.
6462 When nil, middle button is mouse-2 and right button is mouse-3. */);
6463 Vw32_swap_mouse_buttons = Qnil;
6464
6465 DEFVAR_LISP ("w32-grab-focus-on-raise",
6466 &Vw32_grab_focus_on_raise,
6467 doc: /* Raised frame grabs input focus.
6468 When t, `raise-frame' grabs input focus as well. This fits well
6469 with the normal Windows click-to-focus policy, but might not be
6470 desirable when using a point-to-focus policy. */);
6471 Vw32_grab_focus_on_raise = Qt;
6472
6473 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6474 &Vw32_capslock_is_shiftlock,
6475 doc: /* Apply CapsLock state to non character input keys.
6476 When nil, CapsLock only affects normal character input keys. */);
6477 Vw32_capslock_is_shiftlock = Qnil;
6478
6479 DEFVAR_LISP ("w32-recognize-altgr",
6480 &Vw32_recognize_altgr,
6481 doc: /* Recognize right-alt and left-ctrl as AltGr.
6482 When nil, the right-alt and left-ctrl key combination is
6483 interpreted normally. */);
6484 Vw32_recognize_altgr = Qt;
6485
6486 DEFVAR_BOOL ("w32-use-visible-system-caret",
6487 &w32_use_visible_system_caret,
6488 doc: /* Flag to make the system caret visible.
6489 When this is non-nil, Emacs will indicate the position of point by
6490 using the system caret instead of drawing its own cursor. Some screen
6491 reader software does not track the system cursor properly when it is
6492 invisible, and gets confused by Emacs drawing its own cursor, so this
6493 variable is initialized to t when Emacs detects that screen reader
6494 software is running as it starts up.
6495
6496 When this variable is set, other variables affecting the appearance of
6497 the cursor have no effect. */);
6498
6499 w32_use_visible_system_caret = 0;
6500
6501 /* We don't yet support this, but defining this here avoids whining
6502 from cus-start.el and other places, like "M-x set-variable". */
6503 DEFVAR_BOOL ("x-use-underline-position-properties",
6504 &x_use_underline_position_properties,
6505 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
6506 A value of nil means ignore them. If you encounter fonts with bogus
6507 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6508 to 4.1, set this to nil. */);
6509 x_use_underline_position_properties = 0;
6510
6511 DEFVAR_BOOL ("x-underline-at-descent-line",
6512 &x_underline_at_descent_line,
6513 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
6514 A value of nil means to draw the underline according to the value of the
6515 variable `x-use-underline-position-properties', which is usually at the
6516 baseline level. The default value is nil. */);
6517 x_underline_at_descent_line = 0;
6518
6519 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
6520 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6521 Vx_toolkit_scroll_bars = Qt;
6522
6523 staticpro (&last_mouse_motion_frame);
6524 last_mouse_motion_frame = Qnil;
6525 }
6526
6527 /* arch-tag: 5fa70624-ab86-499c-8a85-473958ee4646
6528 (do not change this comment) */