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