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