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