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