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