(Info-dir-remove-duplicates): Avoid infloop when
[bpt/emacs.git] / src / w32term.c
CommitLineData
e9e23e23 1/* Implementation of GUI terminal on the Microsoft W32 API.
c2cc16fa 2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
9ef2e2cf 3 Free Software Foundation, Inc.
ee78dc32
GV
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ef2e2cf
JR
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1fb87c77 20Boston, MA 02111-1307, USA. */
ee78dc32 21
ee78dc32 22#include <config.h>
68c45bf0 23#include <signal.h>
ee78dc32 24#include <stdio.h>
791f420f 25#include <stdlib.h>
ee78dc32 26#include "lisp.h"
2091aeb2 27#include "charset.h"
ee78dc32
GV
28#include "blockinput.h"
29
e12ca9c2 30#include "w32heap.h"
689004fa 31#include "w32term.h"
791f420f 32#include "w32bdf.h"
12857dfd 33#include <shellapi.h>
ee78dc32
GV
34
35#include "systty.h"
36#include "systime.h"
f7737f5d 37#include "atimer.h"
8feddab4 38#include "keymap.h"
ee78dc32
GV
39
40#include <ctype.h>
41#include <errno.h>
42#include <setjmp.h>
43#include <sys/stat.h>
44
38006079 45#include "keyboard.h"
ee78dc32
GV
46#include "frame.h"
47#include "dispextern.h"
93ff4395 48#include "fontset.h"
ee78dc32
GV
49#include "termhooks.h"
50#include "termopts.h"
51#include "termchar.h"
52#include "gnu.h"
53#include "disptab.h"
54#include "buffer.h"
55#include "window.h"
ee78dc32 56#include "intervals.h"
791f420f 57#include "composite.h"
ef0e360f 58#include "coding.h"
ee78dc32 59
791f420f
JR
60#define abs(x) ((x) < 0 ? -(x) : (x))
61
62#define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
63
64\f
33c34bea 65/* Fringe bitmaps. */
791f420f 66
33c34bea 67enum fringe_bitmap_type
791f420f 68{
33c34bea 69 NO_FRINGE_BITMAP,
791f420f
JR
70 LEFT_TRUNCATION_BITMAP,
71 RIGHT_TRUNCATION_BITMAP,
72 OVERLAY_ARROW_BITMAP,
73 CONTINUED_LINE_BITMAP,
74 CONTINUATION_LINE_BITMAP,
75 ZV_LINE_BITMAP
76};
77
78/* Bitmaps are all unsigned short, as Windows requires bitmap data to
79 be Word aligned. For some reason they are horizontally reflected
80 compared to how they appear on X, so changes in xterm.c should be
81 reflected here. */
82
83/* Bitmap drawn to indicate lines not displaying text if
84 `indicate-empty-lines' is non-nil. */
85
86#define zv_width 8
d33c49e8
KS
87#define zv_height 72
88#define zv_period 3
b8b47c19 89static unsigned short zv_bits[] = {
d33c49e8
KS
90 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
91 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
92 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
93 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
94 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
95 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
96 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
97 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
f7737f5d 98static HBITMAP zv_bmp;
791f420f
JR
99
100/* An arrow like this: `<-'. */
101
102#define left_width 8
103#define left_height 8
104static unsigned short left_bits[] = {
105 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
f7737f5d 106static HBITMAP left_bmp;
791f420f
JR
107
108/* Right truncation arrow bitmap `->'. */
109
110#define right_width 8
111#define right_height 8
112static unsigned short right_bits[] = {
113 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
f7737f5d 114static HBITMAP right_bmp;
791f420f
JR
115
116/* Marker for continued lines. */
117
118#define continued_width 8
119#define continued_height 8
120static unsigned short continued_bits[] = {
121 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
f7737f5d 122static HBITMAP continued_bmp;
791f420f
JR
123
124/* Marker for continuation lines. */
125
126#define continuation_width 8
127#define continuation_height 8
128static unsigned short continuation_bits[] = {
129 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
f7737f5d 130static HBITMAP continuation_bmp;
791f420f
JR
131
132/* Overlay arrow bitmap. */
133
134#if 0
135/* A bomb. */
136#define ov_width 8
137#define ov_height 8
138static unsigned short ov_bits[] = {
139 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
140#else
141/* A triangular arrow. */
142#define ov_width 8
143#define ov_height 8
144static unsigned short ov_bits[] = {
145 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
791f420f 146#endif
f7737f5d 147static HBITMAP ov_bmp;
791f420f
JR
148
149extern Lisp_Object Qhelp_echo;
150
151\f
5bf04520 152/* Non-nil means Emacs uses toolkit scroll bars. */
791f420f 153
5bf04520 154Lisp_Object Vx_toolkit_scroll_bars;
791f420f
JR
155
156/* If a string, w32_read_socket generates an event to display that string.
157 (The display is done in read_char.) */
d67d1627 158
791f420f 159static Lisp_Object help_echo;
158cba56 160static Lisp_Object help_echo_window;
ec48c3a7
JR
161static Lisp_Object help_echo_object;
162static int help_echo_pos;
791f420f 163
706ddb8f 164/* Temporary variables for w32_read_socket. */
791f420f
JR
165
166static Lisp_Object previous_help_echo;
706ddb8f
JR
167static int last_mousemove_x = 0;
168static int last_mousemove_y = 0;
791f420f
JR
169
170/* Non-zero means that a HELP_EVENT has been generated since Emacs
171 start. */
172
173static int any_help_event_p;
174
549808db
JR
175/* Non-zero means autoselect window with the mouse cursor. */
176
ee8ceff8 177int mouse_autoselect_window;
549808db 178
791f420f
JR
179/* Non-zero means draw block and hollow cursor as wide as the glyph
180 under it. For example, if a block cursor is over a tab, it will be
181 drawn as wide as that tab on the display. */
182
183int x_stretch_cursor_p;
184
9f5a911b
JR
185/* Non-zero means make use of UNDERLINE_POSITION font properties. */
186
187int x_use_underline_position_properties;
188
e7efd97e
GV
189extern unsigned int msh_mousewheel;
190
ee78dc32
GV
191extern void free_frame_menubar ();
192
9ef2e2cf
JR
193extern int w32_codepage_for_font (char *fontname);
194
82f9d565
AI
195extern glyph_metric *w32_BDF_TextMetric(bdffont *fontp,
196 unsigned char *text, int dim);
52cf03a1
GV
197extern Lisp_Object Vwindow_system;
198
ee78dc32
GV
199#define x_any_window_to_frame x_window_to_frame
200#define x_top_window_to_frame x_window_to_frame
201
202\f
fbd6baed
GV
203/* This is display since w32 does not support multiple ones. */
204struct w32_display_info one_w32_display_info;
8c3b00cb 205struct w32_display_info *x_display_list;
ee78dc32
GV
206
207/* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
fbd6baed 208 one for each element of w32_display_list and in the same order.
ee78dc32
GV
209 NAME is the name of the frame.
210 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
fbd6baed 211Lisp_Object w32_display_name_list;
ee78dc32
GV
212
213/* Frame being updated by update_frame. This is declared in term.c.
214 This is set by update_begin and looked at by all the
fbd6baed 215 w32 functions. It is zero while not inside an update.
791f420f 216 In that case, the w32 functions assume that `SELECTED_FRAME ()'
ee78dc32
GV
217 is the frame to apply to. */
218extern struct frame *updating_frame;
219
220/* This is a frame waiting to be autoraised, within w32_read_socket. */
221struct frame *pending_autoraise_frame;
222
ee78dc32 223/* Nominal cursor position -- where to draw output.
791f420f
JR
224 HPOS and VPOS are window relative glyph matrix coordinates.
225 X and Y are window relative pixel coordinates. */
ee78dc32 226
791f420f 227struct cursor_pos output_cursor;
ee78dc32 228
abb15ebd
JR
229/* The handle of the frame that currently owns the system caret. */
230HWND w32_system_caret_hwnd;
abb15ebd
JR
231int w32_system_caret_height;
232int w32_system_caret_x;
233int w32_system_caret_y;
99558ce8 234int w32_use_visible_system_caret;
abb15ebd 235
484fa2c1 236/* Flag to enable Unicode output in case users wish to use programs
cabb23bc
GV
237 like Twinbridge on '95 rather than installed system level support
238 for Far East languages. */
484fa2c1 239int w32_enable_unicode_output;
cabb23bc 240
e9e23e23
GV
241DWORD dwWindowsThreadId = 0;
242HANDLE hWindowsThread = NULL;
ee78dc32
GV
243DWORD dwMainThreadId = 0;
244HANDLE hMainThread = NULL;
245
689004fa
GV
246#ifndef SIF_ALL
247/* These definitions are new with Windows 95. */
248#define SIF_RANGE 0x0001
249#define SIF_PAGE 0x0002
250#define SIF_POS 0x0004
251#define SIF_DISABLENOSCROLL 0x0008
252#define SIF_TRACKPOS 0x0010
253#define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
254
255typedef struct tagSCROLLINFO
256{
257 UINT cbSize;
258 UINT fMask;
259 int nMin;
260 int nMax;
261 UINT nPage;
262 int nPos;
263 int nTrackPos;
264} SCROLLINFO, FAR *LPSCROLLINFO;
265typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
266#endif /* SIF_ALL */
267
268/* Dynamic linking to new proportional scroll bar functions. */
269int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
270BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
271
272int vertical_scroll_bar_min_handle;
273int vertical_scroll_bar_top_border;
274int vertical_scroll_bar_bottom_border;
275
276int last_scroll_bar_drag_pos;
277
ee78dc32
GV
278/* Mouse movement. */
279
280/* Where the mouse was last time we reported a mouse event. */
9ef2e2cf 281
791f420f 282FRAME_PTR last_mouse_frame;
ee78dc32 283static RECT last_mouse_glyph;
791f420f 284static Lisp_Object last_mouse_press_frame;
ee78dc32 285
fbd6baed 286Lisp_Object Vw32_num_mouse_buttons;
52cf03a1 287
fbd6baed 288Lisp_Object Vw32_swap_mouse_buttons;
52cf03a1 289
689004fa
GV
290/* Control whether x_raise_frame also sets input focus. */
291Lisp_Object Vw32_grab_focus_on_raise;
292
293/* Control whether Caps Lock affects non-ascii characters. */
294Lisp_Object Vw32_capslock_is_shiftlock;
295
ef0e360f
GV
296/* Control whether right-alt and left-ctrl should be recognized as AltGr. */
297Lisp_Object Vw32_recognize_altgr;
298
ee78dc32
GV
299/* The scroll bar in which the last motion event occurred.
300
301 If the last motion event occurred in a scroll bar, we set this
fbd6baed 302 so w32_mouse_position can know whether to report a scroll bar motion or
ee78dc32
GV
303 an ordinary motion.
304
305 If the last motion event didn't occur in a scroll bar, we set this
fbd6baed 306 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
9ef2e2cf
JR
307static Lisp_Object last_mouse_scroll_bar;
308static int last_mouse_scroll_bar_pos;
ee78dc32 309
fbd6baed 310/* This is a hack. We would really prefer that w32_mouse_position would
ee78dc32 311 return the time associated with the position it returns, but there
9ef2e2cf 312 doesn't seem to be any way to wrest the time-stamp from the server
ee78dc32
GV
313 along with the position query. So, we just keep track of the time
314 of the last movement we received, and return that in hopes that
315 it's somewhat accurate. */
ee78dc32 316
9ef2e2cf
JR
317static Time last_mouse_movement_time;
318
319/* Incremented by w32_read_socket whenever it really tries to read
320 events. */
a1b9438c 321
ee78dc32
GV
322#ifdef __STDC__
323static int volatile input_signal_count;
324#else
325static int input_signal_count;
326#endif
327
328extern Lisp_Object Vcommand_line_args, Vsystem_name;
329
330extern Lisp_Object Qface, Qmouse_face;
331
38006079 332#ifndef USE_CRT_DLL
ee78dc32 333extern int errno;
38006079 334#endif
ee78dc32
GV
335
336/* A mask of extra modifier bits to put into every keyboard char. */
9ef2e2cf 337
31ade731 338extern EMACS_INT extra_keyboard_modifiers;
ee78dc32 339
791f420f
JR
340/* Enumeration for overriding/changing the face to use for drawing
341 glyphs in x_draw_glyphs. */
342
343enum draw_glyphs_face
344{
345 DRAW_NORMAL_TEXT,
346 DRAW_INVERSE_VIDEO,
347 DRAW_CURSOR,
348 DRAW_MOUSE_FACE,
349 DRAW_IMAGE_RAISED,
350 DRAW_IMAGE_SUNKEN
351};
352
38006079 353static void x_update_window_end P_ ((struct window *, int, int));
791f420f
JR
354static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
355void w32_delete_display P_ ((struct w32_display_info *));
356static int fast_find_position P_ ((struct window *, int, int *, int *,
9f5a911b
JR
357 int *, int *, Lisp_Object));
358static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
359 int *, int *, int *, int *, int));
791f420f
JR
360static void set_output_cursor P_ ((struct cursor_pos *));
361static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
9f5a911b 362 int *, int *, int *, int));
791f420f 363static void note_mode_line_highlight P_ ((struct window *, int, int));
791f420f
JR
364static void note_mouse_highlight P_ ((struct frame *, int, int));
365static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
366static void w32_handle_tool_bar_click P_ ((struct frame *,
367 struct input_event *));
368static void show_mouse_face P_ ((struct w32_display_info *,
369 enum draw_glyphs_face));
9f5a911b
JR
370static int cursor_in_mouse_face_p P_ ((struct window *));
371static int clear_mouse_face P_ ((struct w32_display_info *));
791f420f
JR
372
373void x_lower_frame P_ ((struct frame *));
374void x_scroll_bar_clear P_ ((struct frame *));
375void x_wm_set_size_hint P_ ((struct frame *, long, int));
376void x_raise_frame P_ ((struct frame *));
377void x_set_window_size P_ ((struct frame *, int, int, int));
378void x_wm_set_window_state P_ ((struct frame *, int));
379void x_wm_set_icon_pixmap P_ ((struct frame *, int));
380void w32_initialize P_ ((void));
381static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
382int x_compute_min_glyph_bounds P_ ((struct frame *));
383static void x_draw_phys_cursor_glyph P_ ((struct window *,
384 struct glyph_row *,
385 enum draw_glyphs_face));
386static void x_update_end P_ ((struct frame *));
387static void w32_frame_up_to_date P_ ((struct frame *));
791f420f
JR
388static void w32_set_terminal_modes P_ ((void));
389static void w32_reset_terminal_modes P_ ((void));
390static void w32_cursor_to P_ ((int, int, int, int));
391static void x_write_glyphs P_ ((struct glyph *, int));
392static void x_clear_end_of_line P_ ((int));
393static void x_clear_frame P_ ((void));
394static void x_clear_cursor P_ ((struct window *));
395static void frame_highlight P_ ((struct frame *));
396static void frame_unhighlight P_ ((struct frame *));
73dc743c
JR
397static void x_new_focus_frame P_ ((struct w32_display_info *,
398 struct frame *));
791f420f
JR
399static void w32_frame_rehighlight P_ ((struct frame *));
400static void x_frame_rehighlight P_ ((struct w32_display_info *));
401static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
70fa9f1f
JR
402static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
403 enum text_cursor_kinds));
791f420f 404static void expose_frame P_ ((struct frame *, int, int, int, int));
9f5a911b 405static int expose_window_tree P_ ((struct window *, RECT *));
95f2448c
RS
406static void expose_overlaps P_ ((struct window *, struct glyph_row *,
407 struct glyph_row *));
9f5a911b 408static int expose_window P_ ((struct window *, RECT *));
791f420f
JR
409static void expose_area P_ ((struct window *, struct glyph_row *,
410 RECT *, enum glyph_row_area));
9f5a911b
JR
411static int expose_line P_ ((struct window *, struct glyph_row *,
412 RECT *));
791f420f
JR
413void x_update_cursor P_ ((struct frame *, int));
414static void x_update_cursor_in_window_tree P_ ((struct window *, int));
415static void x_update_window_cursor P_ ((struct window *, int));
416static void x_erase_phys_cursor P_ ((struct window *));
417void x_display_cursor P_ ((struct window *w, int, int, int, int, int));
418void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
6637c996 419static void w32_draw_fringe_bitmap P_ ((struct window *, HDC hdc,
d67d1627 420 struct glyph_row *,
d33c49e8 421 enum fringe_bitmap_type, int left_p));
c2cc16fa
JR
422static void w32_clip_to_row P_ ((struct window *, struct glyph_row *,
423 HDC, int));
791f420f 424static int x_phys_cursor_in_rect_p P_ ((struct window *, RECT *));
6637c996
JR
425static void x_draw_row_fringe_bitmaps P_ ((struct window *,
426 struct glyph_row *));
427static void notice_overwritten_cursor P_ ((struct window *,
428 enum glyph_row_area,
429 int, int, int, int));
791f420f 430
ee78dc32
GV
431static Lisp_Object Qvendor_specific_keysyms;
432
ee78dc32 433\f
9ef2e2cf
JR
434/***********************************************************************
435 Debugging
436 ***********************************************************************/
437
ee78dc32 438#if 0
9ef2e2cf
JR
439
440/* This is a function useful for recording debugging information about
441 the sequence of occurrences in this file. */
ee78dc32 442
d67d1627 443struct record
ee78dc32
GV
444{
445 char *locus;
446 int type;
447};
448
449struct record event_record[100];
450
451int event_record_index;
452
453record_event (locus, type)
454 char *locus;
455 int type;
456{
457 if (event_record_index == sizeof (event_record) / sizeof (struct record))
458 event_record_index = 0;
459
460 event_record[event_record_index].locus = locus;
461 event_record[event_record_index].type = type;
462 event_record_index++;
463}
464
465#endif /* 0 */
466\f
791f420f
JR
467
468void XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
469 XGCValues *xgcv)
470{
471 if (mask & GCForeground)
472 gc->foreground = xgcv->foreground;
473 if (mask & GCBackground)
474 gc->background = xgcv->background;
475 if (mask & GCFont)
476 gc->font = xgcv->font;
477}
478
479XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
480 XGCValues *xgcv)
481{
482 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
483 bzero (gc, sizeof (XGCValues));
484
485 XChangeGC (ignore, gc, mask, xgcv);
486
487 return gc;
488}
489
490void XGetGCValues (void* ignore, XGCValues *gc,
491 unsigned long mask, XGCValues *xgcv)
492{
493 XChangeGC (ignore, xgcv, mask, gc);
494}
495
791f420f
JR
496static void
497w32_set_clip_rectangle (HDC hdc, RECT *rect)
498{
499 if (rect)
500 {
501 HRGN clip_region = CreateRectRgnIndirect (rect);
502 SelectClipRgn (hdc, clip_region);
503 DeleteObject (clip_region);
504 }
505 else
506 SelectClipRgn (hdc, NULL);
507}
508
791f420f
JR
509
510/* Draw a hollow rectangle at the specified position. */
511void
512w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
513 int width, int height)
514{
515 HBRUSH hb, oldhb;
516 HPEN hp, oldhp;
517
518 hb = CreateSolidBrush (gc->background);
519 hp = CreatePen (PS_SOLID, 0, gc->foreground);
520 oldhb = SelectObject (hdc, hb);
521 oldhp = SelectObject (hdc, hp);
522
523 Rectangle (hdc, x, y, x + width, y + height);
524
525 SelectObject (hdc, oldhb);
526 SelectObject (hdc, oldhp);
527 DeleteObject (hb);
528 DeleteObject (hp);
529}
530
531/* Draw a filled rectangle at the specified position. */
d67d1627 532void
00fe468b 533w32_fill_rect (f, hdc, pix, lprect)
ee78dc32 534 FRAME_PTR f;
00fe468b 535 HDC hdc;
ee78dc32
GV
536 COLORREF pix;
537 RECT * lprect;
538{
ee78dc32 539 HBRUSH hb;
791f420f 540
ee78dc32 541 hb = CreateSolidBrush (pix);
ee78dc32 542 FillRect (hdc, lprect, hb);
ee78dc32 543 DeleteObject (hb);
ee78dc32
GV
544}
545
d67d1627 546void
fbd6baed 547w32_clear_window (f)
ee78dc32
GV
548 FRAME_PTR f;
549{
550 RECT rect;
00fe468b 551 HDC hdc = get_frame_dc (f);
52cf03a1 552
e4a52412
JR
553 /* Under certain conditions, this can be called at startup with
554 a console frame pointer before the GUI frame is created. An HDC
555 of 0 indicates this. */
556 if (hdc)
557 {
558 GetClientRect (FRAME_W32_WINDOW (f), &rect);
559 w32_clear_rect (f, hdc, &rect);
560 }
561
00fe468b 562 release_frame_dc (f, hdc);
ee78dc32
GV
563}
564
565\f
791f420f
JR
566/***********************************************************************
567 Starting and ending an update
568 ***********************************************************************/
d67d1627 569
791f420f
JR
570/* Start an update of frame F. This function is installed as a hook
571 for update_begin, i.e. it is called when update_begin is called.
572 This function is called prior to calls to x_update_window_begin for
99012074 573 each window being updated. */
ee78dc32 574
96214669 575static void
791f420f 576x_update_begin (f)
ee78dc32
GV
577 struct frame *f;
578{
99012074
AI
579 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
580
7e6ac5b9
AI
581 if (! FRAME_W32_P (f))
582 return;
583
99012074
AI
584 /* Regenerate display palette before drawing if list of requested
585 colors has changed. */
586 if (display_info->regen_palette)
587 {
588 w32_regenerate_palette (f);
589 display_info->regen_palette = FALSE;
590 }
791f420f 591}
ee78dc32 592
791f420f
JR
593
594/* Start update of window W. Set the global variable updated_window
595 to the window being updated and set output_cursor to the cursor
596 position of W. */
597
598static void
599x_update_window_begin (w)
600 struct window *w;
601{
602 struct frame *f = XFRAME (WINDOW_FRAME (w));
603 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
604
99558ce8 605 /* Hide the system caret during an update. */
ab76d376
JR
606 if (w32_use_visible_system_caret)
607 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
99558ce8 608
791f420f
JR
609 updated_window = w;
610 set_output_cursor (&w->cursor);
ee78dc32
GV
611
612 BLOCK_INPUT;
613
791f420f 614 if (f == display_info->mouse_face_mouse_frame)
ee78dc32
GV
615 {
616 /* Don't do highlighting for mouse motion during the update. */
791f420f 617 display_info->mouse_face_defer = 1;
ee78dc32 618
9ef2e2cf
JR
619 /* If F needs to be redrawn, simply forget about any prior mouse
620 highlighting. */
ee78dc32 621 if (FRAME_GARBAGED_P (f))
791f420f
JR
622 display_info->mouse_face_window = Qnil;
623
ec48c3a7
JR
624#if 0 /* Rows in a current matrix containing glyphs in mouse-face have
625 their mouse_face_p flag set, which means that they are always
626 unequal to rows in a desired matrix which never have that
627 flag set. So, rows containing mouse-face glyphs are never
628 scrolled, and we don't have to switch the mouse highlight off
629 here to prevent it from being scrolled. */
d67d1627 630
791f420f
JR
631 /* Can we tell that this update does not affect the window
632 where the mouse highlight is? If so, no need to turn off.
633 Likewise, don't do anything if the frame is garbaged;
634 in that case, the frame's current matrix that we would use
635 is all wrong, and we will redisplay that line anyway. */
636 if (!NILP (display_info->mouse_face_window)
637 && w == XWINDOW (display_info->mouse_face_window))
ee78dc32 638 {
791f420f 639 int i;
ee78dc32 640
791f420f
JR
641 for (i = 0; i < w->desired_matrix->nrows; ++i)
642 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
ee78dc32
GV
643 break;
644
791f420f
JR
645 if (i < w->desired_matrix->nrows)
646 clear_mouse_face (display_info);
ee78dc32 647 }
ec48c3a7 648#endif /* 0 */
ee78dc32
GV
649 }
650
651 UNBLOCK_INPUT;
652}
653
791f420f
JR
654
655/* Draw a vertical window border to the right of window W if W doesn't
656 have vertical scroll bars. */
657
96214669 658static void
791f420f
JR
659x_draw_vertical_border (w)
660 struct window *w;
ee78dc32 661{
791f420f 662 struct frame *f = XFRAME (WINDOW_FRAME (w));
d67d1627 663
791f420f
JR
664 /* Redraw borders between horizontally adjacent windows. Don't
665 do it for frames with vertical scroll bars because either the
666 right scroll bar of a window, or the left scroll bar of its
667 neighbor will suffice as a border. */
668 if (!WINDOW_RIGHTMOST_P (w)
669 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
670 {
671 RECT r;
00fe468b 672 HDC hdc;
ee78dc32 673
9f5a911b
JR
674 window_box_edges (w, -1, (int *) &r.left, (int *) &r.top,
675 (int *) &r.right, (int *) &r.bottom);
33c34bea 676 r.left = r.right + FRAME_X_RIGHT_FRINGE_WIDTH (f);
791f420f
JR
677 r.right = r.left + 1;
678 r.bottom -= 1;
ee78dc32 679
00fe468b 680 hdc = get_frame_dc (f);
e4a52412 681 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
00fe468b 682 release_frame_dc (f, hdc);
791f420f
JR
683 }
684}
ee78dc32 685
d67d1627 686
ec48c3a7
JR
687/* End update of window W (which is equal to updated_window).
688
689 Draw vertical borders between horizontally adjacent windows, and
690 display W's cursor if CURSOR_ON_P is non-zero.
691
692 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
693 glyphs in mouse-face were overwritten. In that case we have to
694 make sure that the mouse-highlight is properly redrawn.
695
696 W may be a menu bar pseudo-window in case we don't have X toolkit
697 support. Such windows don't have a cursor, so don't display it
698 here. */
791f420f
JR
699
700static void
ec48c3a7 701x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
791f420f 702 struct window *w;
ec48c3a7 703 int cursor_on_p, mouse_face_overwritten_p;
791f420f 704{
9f5a911b
JR
705 struct w32_display_info *dpyinfo
706 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
707
791f420f
JR
708 if (!w->pseudo_window_p)
709 {
710 BLOCK_INPUT;
ec48c3a7 711
791f420f
JR
712 if (cursor_on_p)
713 x_display_and_set_cursor (w, 1, output_cursor.hpos,
714 output_cursor.vpos,
715 output_cursor.x, output_cursor.y);
d67d1627 716
791f420f
JR
717 x_draw_vertical_border (w);
718 UNBLOCK_INPUT;
719 }
9f5a911b
JR
720
721 /* If a row with mouse-face was overwritten, arrange for
722 XTframe_up_to_date to redisplay the mouse highlight. */
723 if (mouse_face_overwritten_p)
724 {
725 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
726 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
727 dpyinfo->mouse_face_window = Qnil;
728 }
729
99558ce8
JR
730 /* Unhide the caret. This won't actually show the cursor, unless it
731 was visible before the corresponding call to HideCaret in
732 x_update_window_begin. */
ab76d376
JR
733 if (w32_use_visible_system_caret)
734 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
99558ce8 735
791f420f
JR
736 updated_window = NULL;
737}
738
9ef2e2cf
JR
739
740/* End update of frame F. This function is installed as a hook in
741 update_end. */
742
791f420f
JR
743static void
744x_update_end (f)
745 struct frame *f;
746{
7e6ac5b9
AI
747 if (! FRAME_W32_P (f))
748 return;
749
791f420f
JR
750 /* Mouse highlight may be displayed again. */
751 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
ee78dc32
GV
752}
753
9ef2e2cf 754
791f420f
JR
755/* This function is called from various places in xdisp.c whenever a
756 complete update has been performed. The global variable
757 updated_window is not available here. */
ee78dc32 758
96214669 759static void
fbd6baed 760w32_frame_up_to_date (f)
791f420f 761 struct frame *f;
ee78dc32 762{
791f420f 763 if (FRAME_W32_P (f))
ee78dc32 764 {
791f420f
JR
765 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
766 if (dpyinfo->mouse_face_deferred_gc
767 || f == dpyinfo->mouse_face_mouse_frame)
768 {
769 BLOCK_INPUT;
770 if (dpyinfo->mouse_face_mouse_frame)
771 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
772 dpyinfo->mouse_face_mouse_x,
773 dpyinfo->mouse_face_mouse_y);
774 dpyinfo->mouse_face_deferred_gc = 0;
775 UNBLOCK_INPUT;
776 }
ee78dc32
GV
777 }
778}
791f420f
JR
779
780
781/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
33c34bea 782 arrow bitmaps, or clear the fringes if no bitmaps are required
791f420f 783 before DESIRED_ROW is made current. The window being updated is
b71b8111 784 found in updated_window. This function is called from
791f420f
JR
785 update_window_line only if it is known that there are differences
786 between bitmaps to be drawn between current row and DESIRED_ROW. */
787
788static void
789x_after_update_window_line (desired_row)
790 struct glyph_row *desired_row;
791{
792 struct window *w = updated_window;
6ff3e5e3
JR
793 struct frame *f;
794 int width, height;
795
791f420f 796 xassert (w);
d67d1627 797
791f420f
JR
798 if (!desired_row->mode_line_p && !w->pseudo_window_p)
799 {
800 BLOCK_INPUT;
33c34bea 801 x_draw_row_fringe_bitmaps (w, desired_row);
6ff3e5e3
JR
802 UNBLOCK_INPUT;
803 }
791f420f 804
6ff3e5e3
JR
805 /* When a window has disappeared, make sure that no rest of
806 full-width rows stays visible in the internal border. Could
807 check here if updated_window is the leftmost/rightmost window,
808 but I guess it's not worth doing since vertically split windows
809 are almost never used, internal border is rarely set, and the
810 overhead is very small. */
811 if (windows_or_buffers_changed
812 && desired_row->full_width_p
813 && (f = XFRAME (w->frame),
814 width = FRAME_INTERNAL_BORDER_WIDTH (f),
815 width != 0)
816 && (height = desired_row->visible_height,
817 height > 0))
818 {
819 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
820 /* Internal border is drawn below the tool bar. */
821 if (WINDOWP (f->tool_bar_window)
822 && w == XWINDOW (f->tool_bar_window))
823 y -= width;
824
825 BLOCK_INPUT;
826 {
827 HDC hdc = get_frame_dc (f);
828 w32_clear_area (f, hdc, 0, y, width, height);
829 w32_clear_area (f, hdc, f->output_data.w32->pixel_width - width,
830 y, width, height);
831 release_frame_dc (f, hdc);
832 }
791f420f
JR
833 UNBLOCK_INPUT;
834 }
835}
836
837
33c34bea 838/* Draw the bitmap WHICH in one of the left or right fringes of
791f420f
JR
839 window W. ROW is the glyph row for which to display the bitmap; it
840 determines the vertical position at which the bitmap has to be
841 drawn. */
842
843static void
d33c49e8 844w32_draw_fringe_bitmap (w, hdc, row, which, left_p)
791f420f 845 struct window *w;
00fe468b 846 HDC hdc;
791f420f 847 struct glyph_row *row;
33c34bea 848 enum fringe_bitmap_type which;
d33c49e8 849 int left_p;
791f420f
JR
850{
851 struct frame *f = XFRAME (WINDOW_FRAME (w));
791f420f
JR
852 HDC compat_hdc;
853 int x, y, wd, h, dy;
d33c49e8 854 int b1, b2;
791f420f 855 HBITMAP pixmap;
791f420f
JR
856 HANDLE horig_obj;
857 struct face *face;
858
859 /* Must clip because of partially visible lines. */
860 w32_clip_to_row (w, row, hdc, 1);
861
d33c49e8
KS
862 /* Convert row to frame coordinates. */
863 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
864
791f420f
JR
865 switch (which)
866 {
d33c49e8
KS
867 case NO_FRINGE_BITMAP:
868 wd = 0;
869 h = 0;
870 break;
871
791f420f
JR
872 case LEFT_TRUNCATION_BITMAP:
873 wd = left_width;
874 h = left_height;
f7737f5d 875 pixmap = left_bmp;
791f420f 876 break;
d67d1627 877
791f420f
JR
878 case OVERLAY_ARROW_BITMAP:
879 wd = ov_width;
880 h = ov_height;
f7737f5d 881 pixmap = ov_bmp;
791f420f 882 break;
d67d1627 883
791f420f
JR
884 case RIGHT_TRUNCATION_BITMAP:
885 wd = right_width;
886 h = right_height;
f7737f5d 887 pixmap = right_bmp;
791f420f
JR
888 break;
889
890 case CONTINUED_LINE_BITMAP:
891 wd = continued_width;
892 h = continued_height;
f7737f5d 893 pixmap = continued_bmp;
791f420f 894 break;
d67d1627 895
791f420f
JR
896 case CONTINUATION_LINE_BITMAP:
897 wd = continuation_width;
898 h = continuation_height;
f7737f5d 899 pixmap = continuation_bmp;
791f420f
JR
900 break;
901
902 case ZV_LINE_BITMAP:
903 wd = zv_width;
d33c49e8 904 h = zv_height - (y % zv_period);
f7737f5d 905 pixmap = zv_bmp;
791f420f
JR
906 break;
907
908 default:
909 abort ();
910 }
911
d33c49e8
KS
912 /* Clip bitmap if too high. */
913 if (h > row->height)
914 h = row->height;
915
916 /* Set dy to the offset in the row to start drawing the bitmap. */
791f420f
JR
917 dy = (row->height - h) / 2;
918
f7737f5d 919 /* Draw the bitmap. */
33c34bea 920 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
d33c49e8
KS
921 PREPARE_FACE_FOR_DISPLAY (f, face);
922
923 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
924 the fringe. */
925 b1 = -1;
926 if (left_p)
927 {
928 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
929 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
930 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
931 - wd
932 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
933 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
934 {
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 b1 = (window_box_left (w, -1)
940 - FRAME_X_LEFT_FRINGE_WIDTH (f)
941 + border);
942 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
943 }
944 }
945 else
946 {
947 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
948 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
949 x = (window_box_right (w, -1)
950 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
951 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
952 the fringe. */
953 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
954 {
955 b1 = window_box_right (w, -1);
956 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
957 }
958 }
959
960 if (b1 >= 0)
961 {
962 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
d67d1627 963
d33c49e8
KS
964 w32_fill_area (f, hdc, face->background,
965 b1,
966 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
967 row->y)),
968 b2,
969 row->visible_height);
970 }
971
972 if (which == NO_FRINGE_BITMAP)
973 return;
791f420f
JR
974
975 compat_hdc = CreateCompatibleDC (hdc);
976 SaveDC (hdc);
9436cdf9 977
791f420f 978 horig_obj = SelectObject (compat_hdc, pixmap);
9436cdf9
JR
979 SetTextColor (hdc, face->background);
980 SetBkColor (hdc, face->foreground);
981
d33c49e8
KS
982 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0,
983 (which == ZV_LINE_BITMAP ? (row->y % zv_period) : 0),
984 SRCCOPY);
9436cdf9 985
791f420f 986 SelectObject (compat_hdc, horig_obj);
791f420f
JR
987 DeleteDC (compat_hdc);
988 RestoreDC (hdc, -1);
791f420f
JR
989}
990
991
33c34bea 992/* Draw fringe bitmaps for glyph row ROW on window W. Call this
791f420f
JR
993 function with input blocked. */
994
995static void
33c34bea 996x_draw_row_fringe_bitmaps (w, row)
791f420f
JR
997 struct window *w;
998 struct glyph_row *row;
999{
1000 struct frame *f = XFRAME (w->frame);
33c34bea 1001 enum fringe_bitmap_type bitmap;
fccb8288 1002 HDC hdc;
791f420f
JR
1003
1004 xassert (interrupt_input_blocked);
1005
1006 /* If row is completely invisible, because of vscrolling, we
1007 don't have to draw anything. */
1008 if (row->visible_height <= 0)
1009 return;
1010
fccb8288
EZ
1011 hdc = get_frame_dc (f);
1012
d33c49e8
KS
1013 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1014 {
1015 /* Decide which bitmap to draw in the left fringe. */
1016 if (row->overlay_arrow_p)
1017 bitmap = OVERLAY_ARROW_BITMAP;
1018 else if (row->truncated_on_left_p)
1019 bitmap = LEFT_TRUNCATION_BITMAP;
1020 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1021 bitmap = CONTINUATION_LINE_BITMAP;
1022 else if (row->indicate_empty_line_p)
1023 bitmap = ZV_LINE_BITMAP;
1024 else
1025 bitmap = NO_FRINGE_BITMAP;
791f420f 1026
d33c49e8
KS
1027 w32_draw_fringe_bitmap (w, hdc, row, bitmap, 1);
1028 }
791f420f 1029
d33c49e8 1030 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
791f420f 1031 {
d33c49e8
KS
1032 /* Decide which bitmap to draw in the right fringe. */
1033 if (row->truncated_on_right_p)
1034 bitmap = RIGHT_TRUNCATION_BITMAP;
1035 else if (row->continued_p)
1036 bitmap = CONTINUED_LINE_BITMAP;
1037 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1038 bitmap = ZV_LINE_BITMAP;
1039 else
1040 bitmap = NO_FRINGE_BITMAP;
791f420f 1041
d33c49e8 1042 w32_draw_fringe_bitmap (w, hdc, row, bitmap, 0);
791f420f
JR
1043 }
1044
791f420f
JR
1045 release_frame_dc (f, hdc);
1046}
1047
ee78dc32 1048\f
9ef2e2cf
JR
1049/* This is called when starting Emacs and when restarting after
1050 suspend. When starting Emacs, no window is mapped. And nothing
1051 must be done to Emacs's own window if it is suspended (though that
1052 rarely happens). */
ee78dc32 1053
96214669
GV
1054static void
1055w32_set_terminal_modes (void)
ee78dc32
GV
1056{
1057}
1058
9ef2e2cf
JR
1059/* This is called when exiting or suspending Emacs. Exiting will make
1060 the W32 windows go away, and suspending requires no action. */
ee78dc32 1061
96214669
GV
1062static void
1063w32_reset_terminal_modes (void)
ee78dc32
GV
1064{
1065}
791f420f 1066
9ef2e2cf 1067
ee78dc32 1068\f
791f420f
JR
1069/***********************************************************************
1070 Output Cursor
1071 ***********************************************************************/
1072
1073/* Set the global variable output_cursor to CURSOR. All cursor
1074 positions are relative to updated_window. */
9ef2e2cf 1075
791f420f
JR
1076static void
1077set_output_cursor (cursor)
1078 struct cursor_pos *cursor;
1079{
1080 output_cursor.hpos = cursor->hpos;
1081 output_cursor.vpos = cursor->vpos;
1082 output_cursor.x = cursor->x;
1083 output_cursor.y = cursor->y;
1084}
1085
1086
1087/* Set a nominal cursor position.
1088
1089 HPOS and VPOS are column/row positions in a window glyph matrix. X
1090 and Y are window text area relative pixel positions.
d67d1627 1091
791f420f
JR
1092 If this is done during an update, updated_window will contain the
1093 window that is being updated and the position is the future output
1094 cursor position for that window. If updated_window is null, use
1095 selected_window and display the cursor at the given position. */
ee78dc32 1096
96214669 1097static void
791f420f
JR
1098w32_cursor_to (vpos, hpos, y, x)
1099 int vpos, hpos, y, x;
ee78dc32 1100{
791f420f 1101 struct window *w;
ee78dc32 1102
791f420f
JR
1103 /* If updated_window is not set, work on selected_window. */
1104 if (updated_window)
1105 w = updated_window;
1106 else
1107 w = XWINDOW (selected_window);
1108
1109 /* Set the output cursor. */
1110 output_cursor.hpos = hpos;
1111 output_cursor.vpos = vpos;
1112 output_cursor.x = x;
1113 output_cursor.y = y;
ee78dc32 1114
791f420f
JR
1115 /* If not called as part of an update, really display the cursor.
1116 This will also set the cursor position of W. */
1117 if (updated_window == NULL)
ee78dc32
GV
1118 {
1119 BLOCK_INPUT;
791f420f 1120 x_display_cursor (w, 1, hpos, vpos, x, y);
ee78dc32
GV
1121 UNBLOCK_INPUT;
1122 }
1123}
791f420f 1124
9ef2e2cf 1125
ee78dc32 1126\f
791f420f
JR
1127/***********************************************************************
1128 Display Iterator
1129 ***********************************************************************/
1130
1131/* Function prototypes of this page. */
1132
1133static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1134 struct glyph *,
93ff4395
JR
1135 wchar_t *,
1136 int *));
791f420f
JR
1137static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1138 int, wchar_t *, int));
82f9d565 1139static XCharStruct *w32_per_char_metric P_ ((XFontStruct *,
9ef2e2cf
JR
1140 wchar_t *,
1141 enum w32_char_font_type));
1142static enum w32_char_font_type
1143 w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
791f420f
JR
1144static void x_append_glyph P_ ((struct it *));
1145static void x_append_composite_glyph P_ ((struct it *));
1146static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1147 int, int, double));
1148static void x_produce_glyphs P_ ((struct it *));
1149static void x_produce_image_glyph P_ ((struct it *it));
a1b9438c 1150
a1b9438c 1151
791f420f
JR
1152/* Dealing with bits of wchar_t as if they were an XChar2B. */
1153#define BUILD_WCHAR_T(byte1, byte2) \
1154 ((wchar_t)((((byte1) & 0x00ff) << 8) | ((byte2) & 0x00ff)))
a1b9438c 1155
a1b9438c 1156
791f420f
JR
1157#define BYTE1(ch) \
1158 (((ch) & 0xff00) >> 8)
a1b9438c 1159
791f420f
JR
1160#define BYTE2(ch) \
1161 ((ch) & 0x00ff)
cabb23bc 1162
791f420f
JR
1163
1164/* Get metrics of character CHAR2B in FONT. Value is always non-null.
1165 If CHAR2B is not contained in FONT, the font's default character
9ef2e2cf 1166 metric is returned. */
791f420f 1167
82f9d565
AI
1168static int
1169w32_bdf_per_char_metric (font, char2b, dim, pcm)
9127e20e
JR
1170 XFontStruct *font;
1171 wchar_t *char2b;
1172 int dim;
82f9d565 1173 XCharStruct * pcm;
9127e20e
JR
1174{
1175 glyph_metric * bdf_metric;
1176 char buf[2];
9127e20e
JR
1177
1178 if (dim == 1)
4b99045f 1179 buf[0] = (char)(*char2b);
9127e20e
JR
1180 else
1181 {
1182 buf[0] = BYTE1 (*char2b);
1183 buf[1] = BYTE2 (*char2b);
1184 }
1185
1186 bdf_metric = w32_BDF_TextMetric (font->bdf, buf, dim);
1187
1188 if (bdf_metric)
1189 {
1190 pcm->width = bdf_metric->dwidth;
1191 pcm->lbearing = bdf_metric->bbox;
1192 pcm->rbearing = bdf_metric->dwidth
1193 - (bdf_metric->bbox + bdf_metric->bbw);
1194 pcm->ascent = bdf_metric->bboy + bdf_metric->bbh;
00c96995 1195 pcm->descent = -bdf_metric->bboy;
82f9d565
AI
1196
1197 return 1;
9127e20e 1198 }
82f9d565 1199 return 0;
9127e20e
JR
1200}
1201
1202
82f9d565
AI
1203static int
1204w32_native_per_char_metric (font, char2b, font_type, pcm)
791f420f
JR
1205 XFontStruct *font;
1206 wchar_t *char2b;
9ef2e2cf 1207 enum w32_char_font_type font_type;
82f9d565 1208 XCharStruct * pcm;
cabb23bc 1209{
82f9d565
AI
1210 HDC hdc = GetDC (NULL);
1211 HFONT old_font;
1212 BOOL retval = FALSE;
cabb23bc 1213
791f420f 1214 xassert (font && char2b);
82f9d565
AI
1215 xassert (font->hfont);
1216 xassert (font_type == UNICODE_FONT || font_type == ANSI_FONT);
cabb23bc 1217
82f9d565 1218 old_font = SelectObject (hdc, font->hfont);
791f420f 1219
dfaaaafb 1220 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
791f420f 1221 {
dfaaaafb
AI
1222 ABC char_widths;
1223
1224 if (font_type == UNICODE_FONT)
1225 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
82f9d565 1226 else
dfaaaafb
AI
1227 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
1228
1229 if (retval)
1230 {
f6dd0c50
JR
1231#if 0
1232 /* Disabled until we can find a way to get the right results
1233 on all versions of Windows. */
1234
73dc743c
JR
1235 /* Don't trust the ABC widths. For synthesized fonts they are
1236 wrong, and so is the result of GetCharWidth()! */
1237 int real_width;
1238 GetCharWidth (hdc, *char2b, *char2b, &real_width);
f6dd0c50 1239#endif
dfaaaafb 1240 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
f6dd0c50 1241#if 0
73dc743c
JR
1242 /* As far as I can tell, this is the best way to determine what
1243 ExtTextOut will do with the broken font. */
1244 if (pcm->width != real_width)
1245 pcm->width = (pcm->width + real_width) / 2;
f6dd0c50 1246#endif
dfaaaafb 1247 pcm->lbearing = char_widths.abcA;
73dc743c 1248 pcm->rbearing = char_widths.abcA + char_widths.abcB;
82f9d565
AI
1249 pcm->ascent = FONT_BASE (font);
1250 pcm->descent = FONT_DESCENT (font);
dfaaaafb 1251 }
791f420f 1252 }
82f9d565
AI
1253
1254 if (!retval)
791f420f 1255 {
82f9d565
AI
1256 /* Either font is not a True-type font, or GetCharABCWidthsW
1257 failed (it is not supported on Windows 9x for instance), so we
1258 can't determine the full info we would like. All is not lost
1259 though - we can call GetTextExtentPoint32 to get rbearing and
1260 deduce width based on the font's per-string overhang. lbearing
1261 is assumed to be zero. */
01b220b6
JR
1262
1263 /* TODO: Some Thai characters (and other composites if Windows
1264 supports them) do have lbearing, and report their total width
1265 as zero. Need some way of handling them when
1266 GetCharABCWidthsW fails. */
82f9d565
AI
1267 SIZE sz;
1268
1269 if (font_type == UNICODE_FONT)
1270 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
1271 else
1272 retval = GetTextExtentPoint32A (hdc, (char*)char2b, 1, &sz);
791f420f
JR
1273
1274 if (retval)
dfaaaafb 1275 {
82f9d565
AI
1276 pcm->width = sz.cx - font->tm.tmOverhang;
1277 pcm->rbearing = sz.cx;
dfaaaafb 1278 pcm->lbearing = 0;
82f9d565
AI
1279 pcm->ascent = FONT_BASE (font);
1280 pcm->descent = FONT_DESCENT (font);
dfaaaafb 1281 }
791f420f
JR
1282 }
1283
791f420f 1284
93ff4395
JR
1285 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1286 {
82f9d565 1287 retval = FALSE;
93ff4395
JR
1288 }
1289
82f9d565
AI
1290 SelectObject (hdc, old_font);
1291 ReleaseDC (NULL, hdc);
1292
1293 return retval;
1294}
1295
1296
1297static XCharStruct *
1298w32_per_char_metric (font, char2b, font_type)
1299 XFontStruct *font;
1300 wchar_t *char2b;
1301 enum w32_char_font_type font_type;
1302{
1303 /* The result metric information. */
1304 XCharStruct *pcm;
1305 BOOL retval;
1306
1307 xassert (font && char2b);
1308 xassert (font_type != UNKNOWN_FONT);
1309
1310 /* Handle the common cases quickly. */
4b99045f 1311 if (!font->bdf && font->per_char == NULL)
82f9d565
AI
1312 /* TODO: determine whether char2b exists in font? */
1313 return &font->max_bounds;
4b99045f 1314 else if (!font->bdf && *char2b < 128)
82f9d565
AI
1315 return &font->per_char[*char2b];
1316
1317 pcm = &font->scratch;
1318
1319 if (font_type == BDF_1D_FONT)
1320 retval = w32_bdf_per_char_metric (font, char2b, 1, pcm);
1321 else if (font_type == BDF_2D_FONT)
1322 retval = w32_bdf_per_char_metric (font, char2b, 2, pcm);
1323 else
1324 retval = w32_native_per_char_metric (font, char2b, font_type, pcm);
1325
1326 if (retval)
1327 return pcm;
1328
1329 return NULL;
1330}
1331
1332void
1333w32_cache_char_metrics (font)
1334 XFontStruct *font;
1335{
1336 wchar_t char2b = L'x';
1337
1338 /* Cache char metrics for the common cases. */
1339 if (font->bdf)
1340 {
1341 /* TODO: determine whether font is fixed-pitch. */
00c96995
JR
1342 if (!w32_bdf_per_char_metric (font, &char2b, 1, &font->max_bounds))
1343 {
1344 /* Use the font width and height as max bounds, as not all BDF
1345 fonts contain the letter 'x'. */
1346 font->max_bounds.width = FONT_MAX_WIDTH (font);
1347 font->max_bounds.lbearing = -font->bdf->llx;
1348 font->max_bounds.rbearing = FONT_MAX_WIDTH (font) - font->bdf->urx;
1349 font->max_bounds.ascent = FONT_BASE (font);
1350 font->max_bounds.descent = FONT_DESCENT (font);
1351 }
82f9d565
AI
1352 }
1353 else
1354 {
f1eed8ff
JR
1355 if (((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH) != 0)
1356 /* Some fonts (eg DBCS fonts) are marked as fixed width even
1357 though they contain characters of different widths. */
1358 || (font->tm.tmMaxCharWidth != font->tm.tmAveCharWidth))
82f9d565
AI
1359 {
1360 /* Font is not fixed pitch, so cache per_char info for the
1361 ASCII characters. It would be much more work, and probably
1362 not worth it, to cache other chars, since we may change
1363 between using Unicode and ANSI text drawing functions at
1364 run-time. */
1365 int i;
1366
1367 font->per_char = xmalloc (128 * sizeof(XCharStruct));
1368 for (i = 0; i < 128; i++)
1369 {
1370 char2b = i;
1371 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1372 &font->per_char[i]);
1373 }
1374 }
1375 else
1376 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1377 &font->max_bounds);
1378 }
791f420f
JR
1379}
1380
1381
9ef2e2cf
JR
1382/* Determine if a font is double byte. */
1383int w32_font_is_double_byte (XFontStruct *font)
1384{
1385 return font->double_byte_p;
1386}
1387
1388
d67d1627 1389static BOOL
d6ff54d5
JR
1390w32_use_unicode_for_codepage (codepage)
1391 int codepage;
1392{
1393 /* If the current codepage is supported, use Unicode for output. */
1394 return (w32_enable_unicode_output
1395 && codepage != CP_8BIT
1396 && (codepage == CP_UNICODE || IsValidCodePage (codepage)));
1397}
1398
791f420f
JR
1399/* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1400 the two-byte form of C. Encoding is returned in *CHAR2B. */
1401
9ef2e2cf
JR
1402static INLINE enum w32_char_font_type
1403w32_encode_char (c, char2b, font_info, two_byte_p)
791f420f
JR
1404 int c;
1405 wchar_t *char2b;
1406 struct font_info *font_info;
9ef2e2cf 1407 int * two_byte_p;
791f420f
JR
1408{
1409 int charset = CHAR_CHARSET (c);
1410 int codepage;
9ef2e2cf
JR
1411 int unicode_p = 0;
1412
791f420f
JR
1413 XFontStruct *font = font_info->font;
1414
9127e20e 1415 xassert (two_byte_p);
9ef2e2cf
JR
1416
1417 *two_byte_p = w32_font_is_double_byte (font);
1418
791f420f
JR
1419 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1420 This may be either a program in a special encoder language or a
1421 fixed encoding. */
1422 if (font_info->font_encoder)
1423 {
1424 /* It's a program. */
1425 struct ccl_program *ccl = font_info->font_encoder;
1426
1427 if (CHARSET_DIMENSION (charset) == 1)
1428 {
1429 ccl->reg[0] = charset;
1430 ccl->reg[1] = BYTE2 (*char2b);
1431 }
1432 else
1433 {
1434 ccl->reg[0] = charset;
1435 ccl->reg[1] = BYTE1 (*char2b);
1436 ccl->reg[2] = BYTE2 (*char2b);
1437 }
1438
1439 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1440
1441 /* We assume that MSBs are appropriately set/reset by CCL
1442 program. */
9ef2e2cf 1443 if (!*two_byte_p) /* 1-byte font */
791f420f 1444 *char2b = BUILD_WCHAR_T (0, ccl->reg[1]);
791f420f
JR
1445 else
1446 *char2b = BUILD_WCHAR_T (ccl->reg[1], ccl->reg[2]);
791f420f
JR
1447 }
1448 else if (font_info->encoding[charset])
1449 {
1450 /* Fixed encoding scheme. See fontset.h for the meaning of the
1451 encoding numbers. */
1452 int enc = font_info->encoding[charset];
d67d1627 1453
791f420f
JR
1454 if ((enc == 1 || enc == 2)
1455 && CHARSET_DIMENSION (charset) == 2)
1456 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b) | 0x80, BYTE2 (*char2b));
d67d1627 1457
791f420f
JR
1458 if (enc == 1 || enc == 3
1459 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1460 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b), BYTE2 (*char2b) | 0x80);
1461 else if (enc == 4)
1462 {
1463 int sjis1, sjis2;
1464
1465 ENCODE_SJIS (BYTE1 (*char2b), BYTE2 (*char2b),
1466 sjis1, sjis2);
1467 *char2b = BUILD_WCHAR_T (sjis1, sjis2);
1468 }
1469 }
0e804d38 1470 codepage = font_info->codepage;
791f420f
JR
1471
1472 /* If charset is not ASCII or Latin-1, may need to move it into
1473 Unicode space. */
1474 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
49be9f70
JR
1475 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1
1476 && charset != CHARSET_8_BIT_CONTROL && charset != CHARSET_8_BIT_GRAPHIC)
791f420f
JR
1477 {
1478 char temp[3];
1479 temp[0] = BYTE1 (*char2b);
1480 temp[1] = BYTE2 (*char2b);
1481 temp[2] = '\0';
02f593f3
JR
1482 if (codepage != CP_UNICODE)
1483 {
1484 if (temp[0])
1485 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1486 else
1487 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1488 }
9ef2e2cf
JR
1489 unicode_p = 1;
1490 *two_byte_p = 1;
1491 }
1492 if (!font)
1493 return UNKNOWN_FONT;
9127e20e
JR
1494 else if (font->bdf && CHARSET_DIMENSION (charset) == 1)
1495 return BDF_1D_FONT;
9ef2e2cf 1496 else if (font->bdf)
9127e20e 1497 return BDF_2D_FONT;
9ef2e2cf
JR
1498 else if (unicode_p)
1499 return UNICODE_FONT;
1500 else
1501 return ANSI_FONT;
791f420f
JR
1502}
1503
1504
1505/* Get face and two-byte form of character C in face FACE_ID on frame
1506 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1507 means we want to display multibyte text. Value is a pointer to a
1508 realized face that is ready for display. */
1509
1510static INLINE struct face *
1511x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1512 struct frame *f;
1513 int c, face_id;
1514 wchar_t *char2b;
1515 int multibyte_p;
1516{
1517 struct face *face = FACE_FROM_ID (f, face_id);
1518
1519 if (!multibyte_p)
1520 {
1521 /* Unibyte case. We don't have to encode, but we have to make
1522 sure to use a face suitable for unibyte. */
1523 *char2b = BUILD_WCHAR_T (0, c);
93ff4395
JR
1524 face_id = FACE_FOR_CHAR (f, face, c);
1525 face = FACE_FROM_ID (f, face_id);
791f420f
JR
1526 }
1527 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1528 {
1529 /* Case of ASCII in a face known to fit ASCII. */
1530 *char2b = BUILD_WCHAR_T (0, c);
1531 }
1532 else
1533 {
1534 int c1, c2, charset;
d67d1627 1535
791f420f
JR
1536 /* Split characters into bytes. If c2 is -1 afterwards, C is
1537 really a one-byte character so that byte1 is zero. */
1538 SPLIT_CHAR (c, charset, c1, c2);
1539 if (c2 > 0)
1540 *char2b = BUILD_WCHAR_T (c1, c2);
1541 else
1542 *char2b = BUILD_WCHAR_T (0, c1);
d67d1627 1543
791f420f 1544 /* Maybe encode the character in *CHAR2B. */
93ff4395 1545 if (face->font != NULL)
791f420f
JR
1546 {
1547 struct font_info *font_info
1548 = FONT_INFO_FROM_ID (f, face->font_info_id);
1549 if (font_info)
9ef2e2cf 1550 w32_encode_char (c, char2b, font_info, &multibyte_p);
791f420f
JR
1551 }
1552 }
1553
1554 /* Make sure X resources of the face are allocated. */
1555 xassert (face != NULL);
1556 PREPARE_FACE_FOR_DISPLAY (f, face);
d67d1627 1557
791f420f
JR
1558 return face;
1559}
1560
1561
1562/* Get face and two-byte form of character glyph GLYPH on frame F.
1563 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1564 a pointer to a realized face that is ready for display. */
1565
1566static INLINE struct face *
93ff4395 1567x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
791f420f
JR
1568 struct frame *f;
1569 struct glyph *glyph;
1570 wchar_t *char2b;
93ff4395 1571 int *two_byte_p;
791f420f
JR
1572{
1573 struct face *face;
9ef2e2cf 1574 int dummy = 0;
791f420f
JR
1575
1576 xassert (glyph->type == CHAR_GLYPH);
1577 face = FACE_FROM_ID (f, glyph->face_id);
1578
93ff4395
JR
1579 if (two_byte_p)
1580 *two_byte_p = 0;
9ef2e2cf
JR
1581 else
1582 two_byte_p = &dummy;
93ff4395 1583
791f420f
JR
1584 if (!glyph->multibyte_p)
1585 {
1586 /* Unibyte case. We don't have to encode, but we have to make
1587 sure to use a face suitable for unibyte. */
1588 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1589 }
1590 else if (glyph->u.ch < 128
1591 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1592 {
1593 /* Case of ASCII in a face known to fit ASCII. */
1594 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1595 }
1596 else
1597 {
1598 int c1, c2, charset;
d67d1627 1599
791f420f
JR
1600 /* Split characters into bytes. If c2 is -1 afterwards, C is
1601 really a one-byte character so that byte1 is zero. */
1602 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1603 if (c2 > 0)
1604 *char2b = BUILD_WCHAR_T (c1, c2);
1605 else
1606 *char2b = BUILD_WCHAR_T (0, c1);
1607
1608 /* Maybe encode the character in *CHAR2B. */
1609 if (charset != CHARSET_ASCII)
1610 {
1611 struct font_info *font_info
1612 = FONT_INFO_FROM_ID (f, face->font_info_id);
1613 if (font_info)
1614 {
9ef2e2cf
JR
1615 glyph->w32_font_type
1616 = w32_encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
791f420f
JR
1617 }
1618 }
1619 }
1620
1621 /* Make sure X resources of the face are allocated. */
1622 xassert (face != NULL);
1623 PREPARE_FACE_FOR_DISPLAY (f, face);
1624 return face;
1625}
1626
1627
d67d1627 1628/* Store one glyph for IT->char_to_display in IT->glyph_row.
791f420f
JR
1629 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1630
1631static INLINE void
1632x_append_glyph (it)
1633 struct it *it;
1634{
1635 struct glyph *glyph;
1636 enum glyph_row_area area = it->area;
d67d1627 1637
791f420f
JR
1638 xassert (it->glyph_row);
1639 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
d67d1627 1640
791f420f
JR
1641 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1642 if (glyph < it->glyph_row->glyphs[area + 1])
1643 {
791f420f
JR
1644 glyph->charpos = CHARPOS (it->position);
1645 glyph->object = it->object;
ec48c3a7 1646 glyph->pixel_width = it->pixel_width;
791f420f 1647 glyph->voffset = it->voffset;
ec48c3a7 1648 glyph->type = CHAR_GLYPH;
791f420f 1649 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1650 glyph->left_box_line_p = it->start_of_box_run_p;
1651 glyph->right_box_line_p = it->end_of_box_run_p;
791f420f
JR
1652 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1653 || it->phys_descent > it->descent);
ec48c3a7 1654 glyph->padding_p = 0;
93ff4395 1655 glyph->glyph_not_available_p = it->glyph_not_available_p;
ec48c3a7
JR
1656 glyph->face_id = it->face_id;
1657 glyph->u.ch = it->char_to_display;
1658 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1659 ++it->glyph_row->used[area];
1660 }
1661}
1662
d67d1627 1663/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
791f420f
JR
1664 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1665
1666static INLINE void
1667x_append_composite_glyph (it)
1668 struct it *it;
1669{
1670 struct glyph *glyph;
1671 enum glyph_row_area area = it->area;
d67d1627 1672
791f420f 1673 xassert (it->glyph_row);
d67d1627 1674
791f420f
JR
1675 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1676 if (glyph < it->glyph_row->glyphs[area + 1])
1677 {
791f420f
JR
1678 glyph->charpos = CHARPOS (it->position);
1679 glyph->object = it->object;
ec48c3a7 1680 glyph->pixel_width = it->pixel_width;
791f420f 1681 glyph->voffset = it->voffset;
ec48c3a7 1682 glyph->type = COMPOSITE_GLYPH;
791f420f 1683 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1684 glyph->left_box_line_p = it->start_of_box_run_p;
1685 glyph->right_box_line_p = it->end_of_box_run_p;
791f420f
JR
1686 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1687 || it->phys_descent > it->descent);
ec48c3a7
JR
1688 glyph->padding_p = 0;
1689 glyph->glyph_not_available_p = 0;
1690 glyph->face_id = it->face_id;
1691 glyph->u.cmp_id = it->cmp_id;
1692 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1693 ++it->glyph_row->used[area];
1694 }
1695}
1696
1697
1698/* Change IT->ascent and IT->height according to the setting of
1699 IT->voffset. */
1700
1701static INLINE void
1702take_vertical_position_into_account (it)
1703 struct it *it;
1704{
1705 if (it->voffset)
1706 {
1707 if (it->voffset < 0)
1708 /* Increase the ascent so that we can display the text higher
1709 in the line. */
1710 it->ascent += abs (it->voffset);
1711 else
1712 /* Increase the descent so that we can display the text lower
1713 in the line. */
1714 it->descent += it->voffset;
1715 }
1716}
1717
1718
1719/* Produce glyphs/get display metrics for the image IT is loaded with.
1720 See the description of struct display_iterator in dispextern.h for
1721 an overview of struct display_iterator. */
1722
1723static void
1724x_produce_image_glyph (it)
1725 struct it *it;
1726{
1727 struct image *img;
1728 struct face *face;
1729
1730 xassert (it->what == IT_IMAGE);
1731
1732 face = FACE_FROM_ID (it->f, it->face_id);
1733 img = IMAGE_FROM_ID (it->f, it->image_id);
1734 xassert (img);
1735
1736 /* Make sure X resources of the face and image are loaded. */
1737 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1738 prepare_image_for_display (it->f, img);
1739
9ef2e2cf 1740 it->ascent = it->phys_ascent = image_ascent (img, face);
d6ff54d5
JR
1741 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1742 it->pixel_width = img->width + 2 * img->hmargin;
791f420f
JR
1743
1744 it->nglyphs = 1;
d67d1627 1745
791f420f
JR
1746 if (face->box != FACE_NO_BOX)
1747 {
60222d69
AI
1748 if (face->box_line_width > 0)
1749 {
1750 it->ascent += face->box_line_width;
1751 it->descent += face->box_line_width;
1752 }
d67d1627 1753
791f420f 1754 if (it->start_of_box_run_p)
60222d69 1755 it->pixel_width += abs (face->box_line_width);
791f420f 1756 if (it->end_of_box_run_p)
60222d69 1757 it->pixel_width += abs (face->box_line_width);
791f420f
JR
1758 }
1759
1760 take_vertical_position_into_account (it);
d67d1627 1761
791f420f
JR
1762 if (it->glyph_row)
1763 {
1764 struct glyph *glyph;
1765 enum glyph_row_area area = it->area;
d67d1627 1766
791f420f
JR
1767 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1768 if (glyph < it->glyph_row->glyphs[area + 1])
1769 {
791f420f
JR
1770 glyph->charpos = CHARPOS (it->position);
1771 glyph->object = it->object;
ec48c3a7 1772 glyph->pixel_width = it->pixel_width;
791f420f 1773 glyph->voffset = it->voffset;
ec48c3a7 1774 glyph->type = IMAGE_GLYPH;
791f420f 1775 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1776 glyph->left_box_line_p = it->start_of_box_run_p;
1777 glyph->right_box_line_p = it->end_of_box_run_p;
1778 glyph->overlaps_vertically_p = 0;
1779 glyph->padding_p = 0;
1780 glyph->glyph_not_available_p = 0;
1781 glyph->face_id = it->face_id;
1782 glyph->u.img_id = img->id;
1783 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1784 ++it->glyph_row->used[area];
1785 }
1786 }
1787}
1788
1789
1790/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
d67d1627
JB
1791 of the glyph, WIDTH and HEIGHT are the width and height of the
1792 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
791f420f 1793 ascent of the glyph (0 <= ASCENT <= 1). */
d67d1627 1794
791f420f
JR
1795static void
1796x_append_stretch_glyph (it, object, width, height, ascent)
1797 struct it *it;
1798 Lisp_Object object;
1799 int width, height;
1800 double ascent;
1801{
1802 struct glyph *glyph;
1803 enum glyph_row_area area = it->area;
1804
1805 xassert (ascent >= 0 && ascent <= 1);
d67d1627 1806
791f420f
JR
1807 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1808 if (glyph < it->glyph_row->glyphs[area + 1])
1809 {
791f420f
JR
1810 glyph->charpos = CHARPOS (it->position);
1811 glyph->object = object;
ec48c3a7 1812 glyph->pixel_width = width;
791f420f 1813 glyph->voffset = it->voffset;
ec48c3a7 1814 glyph->type = STRETCH_GLYPH;
791f420f 1815 glyph->multibyte_p = it->multibyte_p;
ec48c3a7
JR
1816 glyph->left_box_line_p = it->start_of_box_run_p;
1817 glyph->right_box_line_p = it->end_of_box_run_p;
1818 glyph->overlaps_vertically_p = 0;
1819 glyph->padding_p = 0;
1820 glyph->glyph_not_available_p = 0;
1821 glyph->face_id = it->face_id;
1822 glyph->u.stretch.ascent = height * ascent;
1823 glyph->u.stretch.height = height;
1824 glyph->w32_font_type = UNKNOWN_FONT;
791f420f
JR
1825 ++it->glyph_row->used[area];
1826 }
1827}
1828
1829
1830/* Produce a stretch glyph for iterator IT. IT->object is the value
1831 of the glyph property displayed. The value must be a list
1832 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1833 being recognized:
1834
1835 1. `:width WIDTH' specifies that the space should be WIDTH *
d67d1627 1836 canonical char width wide. WIDTH may be an integer or floating
791f420f
JR
1837 point number.
1838
1839 2. `:relative-width FACTOR' specifies that the width of the stretch
1840 should be computed from the width of the first character having the
1841 `glyph' property, and should be FACTOR times that width.
1842
1843 3. `:align-to HPOS' specifies that the space should be wide enough
1844 to reach HPOS, a value in canonical character units.
1845
d67d1627 1846 Exactly one of the above pairs must be present.
791f420f
JR
1847
1848 4. `:height HEIGHT' specifies that the height of the stretch produced
1849 should be HEIGHT, measured in canonical character units.
1850
269b7745 1851 5. `:relative-height FACTOR' specifies that the height of the
791f420f
JR
1852 stretch should be FACTOR times the height of the characters having
1853 the glyph property.
1854
1855 Either none or exactly one of 4 or 5 must be present.
1856
1857 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1858 of the stretch should be used for the ascent of the stretch.
1859 ASCENT must be in the range 0 <= ASCENT <= 100. */
1860
1861#define NUMVAL(X) \
1862 ((INTEGERP (X) || FLOATP (X)) \
1863 ? XFLOATINT (X) \
1864 : - 1)
1865
1866
1867static void
1868x_produce_stretch_glyph (it)
1869 struct it *it;
1870{
1871 /* (space :width WIDTH :height HEIGHT. */
c2cc16fa
JR
1872#if GLYPH_DEBUG
1873 extern Lisp_Object Qspace;
1874#endif
1875 extern Lisp_Object QCwidth, QCheight, QCascent;
791f420f
JR
1876 extern Lisp_Object QCrelative_width, QCrelative_height;
1877 extern Lisp_Object QCalign_to;
1878 Lisp_Object prop, plist;
1879 double width = 0, height = 0, ascent = 0;
1880 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1881 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1882
1883 PREPARE_FACE_FOR_DISPLAY (it->f, face);
d67d1627 1884
791f420f
JR
1885 /* List should start with `space'. */
1886 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1887 plist = XCDR (it->object);
1888
1889 /* Compute the width of the stretch. */
1890 if (prop = Fplist_get (plist, QCwidth),
1891 NUMVAL (prop) > 0)
1892 /* Absolute width `:width WIDTH' specified and valid. */
1893 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1894 else if (prop = Fplist_get (plist, QCrelative_width),
1895 NUMVAL (prop) > 0)
1896 {
1897 /* Relative width `:relative-width FACTOR' specified and valid.
1898 Compute the width of the characters having the `glyph'
1899 property. */
1900 struct it it2;
1901 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
d67d1627 1902
791f420f
JR
1903 it2 = *it;
1904 if (it->multibyte_p)
1905 {
1906 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1907 - IT_BYTEPOS (*it));
1908 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1909 }
1910 else
1911 it2.c = *p, it2.len = 1;
1912
1913 it2.glyph_row = NULL;
1914 it2.what = IT_CHARACTER;
1915 x_produce_glyphs (&it2);
1916 width = NUMVAL (prop) * it2.pixel_width;
1917 }
1918 else if (prop = Fplist_get (plist, QCalign_to),
1919 NUMVAL (prop) > 0)
1920 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1921 else
1922 /* Nothing specified -> width defaults to canonical char width. */
1923 width = CANON_X_UNIT (it->f);
d67d1627 1924
791f420f
JR
1925 /* Compute height. */
1926 if (prop = Fplist_get (plist, QCheight),
1927 NUMVAL (prop) > 0)
1928 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1929 else if (prop = Fplist_get (plist, QCrelative_height),
1930 NUMVAL (prop) > 0)
1931 height = FONT_HEIGHT (font) * NUMVAL (prop);
1932 else
1933 height = FONT_HEIGHT (font);
1934
d67d1627 1935 /* Compute percentage of height used for ascent. If
791f420f
JR
1936 `:ascent ASCENT' is present and valid, use that. Otherwise,
1937 derive the ascent from the font in use. */
1938 if (prop = Fplist_get (plist, QCascent),
1939 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1940 ascent = NUMVAL (prop) / 100.0;
1941 else
1942 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
1943
1944 if (width <= 0)
1945 width = 1;
1946 if (height <= 0)
1947 height = 1;
1948
1949 if (it->glyph_row)
1950 {
1951 Lisp_Object object = it->stack[it->sp - 1].string;
1952 if (!STRINGP (object))
1953 object = it->w->buffer;
1954 x_append_stretch_glyph (it, object, width, height, ascent);
1955 }
1956
1957 it->pixel_width = width;
1958 it->ascent = it->phys_ascent = height * ascent;
1959 it->descent = it->phys_descent = height - it->ascent;
1960 it->nglyphs = 1;
1961
1962 if (face->box != FACE_NO_BOX)
1963 {
60222d69
AI
1964 if (face->box_line_width > 0)
1965 {
1966 it->ascent += face->box_line_width;
1967 it->descent += face->box_line_width;
1968 }
d67d1627 1969
791f420f 1970 if (it->start_of_box_run_p)
60222d69 1971 it->pixel_width += abs (face->box_line_width);
791f420f 1972 if (it->end_of_box_run_p)
60222d69 1973 it->pixel_width += abs (face->box_line_width);
791f420f 1974 }
d67d1627 1975
791f420f
JR
1976 take_vertical_position_into_account (it);
1977}
1978
1979/* Return proper value to be used as baseline offset of font that has
1980 ASCENT and DESCENT to draw characters by the font at the vertical
1981 center of the line of frame F.
1982
1983 Here, out task is to find the value of BOFF in the following figure;
1984
1985 -------------------------+-----------+-
1986 -+-+---------+-+ | |
1987 | | | | | |
1988 | | | | F_ASCENT F_HEIGHT
1989 | | | ASCENT | |
1990 HEIGHT | | | | |
1991 | | |-|-+------+-----------|------- baseline
1992 | | | | BOFF | |
1993 | |---------|-+-+ | |
1994 | | | DESCENT | |
1995 -+-+---------+-+ F_DESCENT |
1996 -------------------------+-----------+-
1997
1998 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1999 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
2000 DESCENT = FONT->descent
2001 HEIGHT = FONT_HEIGHT (FONT)
2002 F_DESCENT = (F->output_data.x->font->descent
2003 - F->output_data.x->baseline_offset)
2004 F_HEIGHT = FRAME_LINE_HEIGHT (F)
2005*/
2006
2007#define VCENTER_BASELINE_OFFSET(FONT, F) \
458f45fa
KH
2008 (FONT_DESCENT (FONT) \
2009 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
2010 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
2011 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
791f420f
JR
2012
2013/* Produce glyphs/get display metrics for the display element IT is
2014 loaded with. See the description of struct display_iterator in
2015 dispextern.h for an overview of struct display_iterator. */
2016
2017static void
2018x_produce_glyphs (it)
2019 struct it *it;
2020{
93ff4395
JR
2021 it->glyph_not_available_p = 0;
2022
791f420f
JR
2023 if (it->what == IT_CHARACTER)
2024 {
2025 wchar_t char2b;
2026 XFontStruct *font;
93ff4395 2027 struct face *face = FACE_FROM_ID (it->f, it->face_id);
791f420f 2028 XCharStruct *pcm;
93ff4395 2029 int font_not_found_p;
791f420f
JR
2030 struct font_info *font_info;
2031 int boff; /* baseline offset */
2d0c0bd7
JR
2032 /* We may change it->multibyte_p upon unibyte<->multibyte
2033 conversion. So, save the current value now and restore it
2034 later.
2035
2036 Note: It seems that we don't have to record multibyte_p in
2037 struct glyph because the character code itself tells if or
2038 not the character is multibyte. Thus, in the future, we must
2039 consider eliminating the field `multibyte_p' in the struct
2040 glyph.
2041 */
2042 int saved_multibyte_p = it->multibyte_p;
791f420f 2043
93ff4395
JR
2044 /* Maybe translate single-byte characters to multibyte, or the
2045 other way. */
791f420f 2046 it->char_to_display = it->c;
93ff4395
JR
2047 if (!ASCII_BYTE_P (it->c))
2048 {
2049 if (unibyte_display_via_language_environment
2050 && SINGLE_BYTE_CHAR_P (it->c)
2051 && (it->c >= 0240
2052 || !NILP (Vnonascii_translation_table)))
2053 {
2054 it->char_to_display = unibyte_char_to_multibyte (it->c);
2d0c0bd7 2055 it->multibyte_p = 1;
93ff4395
JR
2056 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2057 face = FACE_FROM_ID (it->f, it->face_id);
2058 }
2059 else if (!SINGLE_BYTE_CHAR_P (it->c)
2060 && !it->multibyte_p)
2061 {
9f5a911b 2062 it->multibyte_p = 1;
93ff4395
JR
2063 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2064 face = FACE_FROM_ID (it->f, it->face_id);
2065 }
2066 }
d67d1627 2067
93ff4395
JR
2068 /* Get font to use. Encode IT->char_to_display. */
2069 x_get_char_face_and_encoding (it->f, it->char_to_display,
2070 it->face_id, &char2b,
2071 it->multibyte_p);
791f420f
JR
2072 font = face->font;
2073
2074 /* When no suitable font found, use the default font. */
2075 font_not_found_p = font == NULL;
2076 if (font_not_found_p)
2077 {
2078 font = FRAME_FONT (it->f);
2079 boff = it->f->output_data.w32->baseline_offset;
2080 font_info = NULL;
2081 }
2082 else
2083 {
2084 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2085 boff = font_info->baseline_offset;
2086 if (font_info->vertical_centering)
2087 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2088 }
2089
791f420f
JR
2090 if (it->char_to_display >= ' '
2091 && (!it->multibyte_p || it->char_to_display < 128))
2092 {
2093 /* Either unibyte or ASCII. */
2094 int stretched_p;
2095
2096 it->nglyphs = 1;
2097
82f9d565 2098 pcm = w32_per_char_metric (font, &char2b,
9127e20e 2099 font->bdf ? BDF_1D_FONT : ANSI_FONT);
791f420f
JR
2100 it->ascent = FONT_BASE (font) + boff;
2101 it->descent = FONT_DESCENT (font) - boff;
791f420f 2102
9ef2e2cf
JR
2103 if (pcm)
2104 {
2105 it->phys_ascent = pcm->ascent + boff;
2106 it->phys_descent = pcm->descent - boff;
2107 it->pixel_width = pcm->width;
2108 }
2109 else
2110 {
2111 it->glyph_not_available_p = 1;
9127e20e
JR
2112 it->phys_ascent = FONT_BASE (font) + boff;
2113 it->phys_descent = FONT_DESCENT (font) - boff;
2114 it->pixel_width = FONT_WIDTH (font);
9ef2e2cf 2115 }
d67d1627 2116
791f420f
JR
2117 /* If this is a space inside a region of text with
2118 `space-width' property, change its width. */
2119 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
2120 if (stretched_p)
2121 it->pixel_width *= XFLOATINT (it->space_width);
2122
2123 /* If face has a box, add the box thickness to the character
2124 height. If character has a box line to the left and/or
2125 right, add the box line width to the character's width. */
2126 if (face->box != FACE_NO_BOX)
2127 {
2128 int thick = face->box_line_width;
d67d1627 2129
60222d69
AI
2130 if (thick > 0)
2131 {
2132 it->ascent += thick;
2133 it->descent += thick;
2134 }
2135 else
2136 thick = -thick;
d67d1627 2137
791f420f
JR
2138 if (it->start_of_box_run_p)
2139 it->pixel_width += thick;
2140 if (it->end_of_box_run_p)
2141 it->pixel_width += thick;
2142 }
2143
2144 /* If face has an overline, add the height of the overline
2145 (1 pixel) and a 1 pixel margin to the character height. */
2146 if (face->overline_p)
2147 it->ascent += 2;
2148
2149 take_vertical_position_into_account (it);
d67d1627 2150
791f420f
JR
2151 /* If we have to actually produce glyphs, do it. */
2152 if (it->glyph_row)
2153 {
2154 if (stretched_p)
2155 {
2156 /* Translate a space with a `space-width' property
2157 into a stretch glyph. */
2158 double ascent = (double) FONT_BASE (font)
2159 / FONT_HEIGHT (font);
d67d1627 2160 x_append_stretch_glyph (it, it->object, it->pixel_width,
791f420f
JR
2161 it->ascent + it->descent, ascent);
2162 }
2163 else
2164 x_append_glyph (it);
2165
2166 /* If characters with lbearing or rbearing are displayed
2167 in this line, record that fact in a flag of the
2168 glyph row. This is used to optimize X output code. */
9ef2e2cf 2169 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
791f420f
JR
2170 it->glyph_row->contains_overlapping_glyphs_p = 1;
2171 }
2172 }
2173 else if (it->char_to_display == '\n')
2174 {
2175 /* A newline has no width but we need the height of the line. */
2176 it->pixel_width = 0;
2177 it->nglyphs = 0;
2178 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2179 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
d67d1627 2180
60222d69
AI
2181 if (face->box != FACE_NO_BOX
2182 && face->box_line_width > 0)
791f420f 2183 {
60222d69
AI
2184 it->ascent += face->box_line_width;
2185 it->descent += face->box_line_width;
791f420f
JR
2186 }
2187 }
2188 else if (it->char_to_display == '\t')
2189 {
2190 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
9ef2e2cf 2191 int x = it->current_x + it->continuation_lines_width;
791f420f 2192 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
d67d1627 2193
9127e20e
JR
2194 /* If the distance from the current position to the next tab
2195 stop is less than a canonical character width, use the
2196 tab stop after that. */
2197 if (next_tab_x - x < CANON_X_UNIT (it->f))
2198 next_tab_x += tab_width;
2199
791f420f
JR
2200 it->pixel_width = next_tab_x - x;
2201 it->nglyphs = 1;
2202 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2203 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
d67d1627 2204
791f420f
JR
2205 if (it->glyph_row)
2206 {
2207 double ascent = (double) it->ascent / (it->ascent + it->descent);
d67d1627 2208 x_append_stretch_glyph (it, it->object, it->pixel_width,
791f420f
JR
2209 it->ascent + it->descent, ascent);
2210 }
2211 }
d67d1627 2212 else
791f420f 2213 {
00fe468b
JR
2214 /* A multi-byte character.
2215 If we found a font, this font should give us the right
791f420f
JR
2216 metrics. If we didn't find a font, use the frame's
2217 default font and calculate the width of the character
2218 from the charset width; this is what old redisplay code
2219 did. */
9127e20e
JR
2220 enum w32_char_font_type type;
2221
2222 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2223 type = BDF_1D_FONT;
2224 else if (font->bdf)
2225 type = BDF_2D_FONT;
2226 else
2227 type = UNICODE_FONT;
2228
82f9d565 2229 pcm = w32_per_char_metric (font, &char2b, type);
00fe468b 2230
93ff4395
JR
2231 if (font_not_found_p || !pcm)
2232 {
2233 int charset = CHAR_CHARSET (it->char_to_display);
00fe468b 2234
93ff4395
JR
2235 it->glyph_not_available_p = 1;
2236 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2237 * CHARSET_WIDTH (charset));
2238 it->phys_ascent = FONT_BASE (font) + boff;
2239 it->phys_descent = FONT_DESCENT (font) - boff;
2240 }
2241 else
2242 {
9ef2e2cf 2243 it->pixel_width = pcm->width;
93ff4395
JR
2244 it->phys_ascent = pcm->ascent + boff;
2245 it->phys_descent = pcm->descent - boff;
2246 if (it->glyph_row
2247 && (pcm->lbearing < 0
2248 || pcm->rbearing > pcm->width))
2249 it->glyph_row->contains_overlapping_glyphs_p = 1;
00fe468b 2250 }
791f420f
JR
2251 it->nglyphs = 1;
2252 it->ascent = FONT_BASE (font) + boff;
2253 it->descent = FONT_DESCENT (font) - boff;
791f420f
JR
2254 if (face->box != FACE_NO_BOX)
2255 {
2256 int thick = face->box_line_width;
60222d69
AI
2257
2258 if (thick > 0)
2259 {
2260 it->ascent += thick;
2261 it->descent += thick;
2262 }
2263 else
2264 thick = - thick;
d67d1627 2265
791f420f
JR
2266 if (it->start_of_box_run_p)
2267 it->pixel_width += thick;
2268 if (it->end_of_box_run_p)
2269 it->pixel_width += thick;
2270 }
d67d1627 2271
791f420f
JR
2272 /* If face has an overline, add the height of the overline
2273 (1 pixel) and a 1 pixel margin to the character height. */
2274 if (face->overline_p)
2275 it->ascent += 2;
2276
2277 take_vertical_position_into_account (it);
d67d1627 2278
791f420f
JR
2279 if (it->glyph_row)
2280 x_append_glyph (it);
2281 }
2d0c0bd7 2282 it->multibyte_p = saved_multibyte_p;
791f420f
JR
2283 }
2284 else if (it->what == IT_COMPOSITION)
2285 {
01b220b6
JR
2286 /* Note: A composition is represented as one glyph in the
2287 glyph matrix. There are no padding glyphs. */
2288 wchar_t char2b;
2289 XFontStruct *font;
2290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2291 XCharStruct *pcm;
2292 int font_not_found_p;
2293 struct font_info *font_info;
2294 int boff; /* baseline offset */
2295 struct composition *cmp = composition_table[it->cmp_id];
2296
2297 /* Maybe translate single-byte characters to multibyte. */
2298 it->char_to_display = it->c;
2299 if (unibyte_display_via_language_environment
2300 && SINGLE_BYTE_CHAR_P (it->c)
2301 && (it->c >= 0240
2302 || (it->c >= 0200
2303 && !NILP (Vnonascii_translation_table))))
2304 {
2305 it->char_to_display = unibyte_char_to_multibyte (it->c);
2306 }
2307
2308 /* Get face and font to use. Encode IT->char_to_display. */
2309 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2310 face = FACE_FROM_ID (it->f, it->face_id);
2311 x_get_char_face_and_encoding (it->f, it->char_to_display,
2312 it->face_id, &char2b, it->multibyte_p);
2313 font = face->font;
2314
2315 /* When no suitable font found, use the default font. */
2316 font_not_found_p = font == NULL;
2317 if (font_not_found_p)
2318 {
2319 font = FRAME_FONT (it->f);
2320 boff = it->f->output_data.w32->baseline_offset;
2321 font_info = NULL;
2322 }
2323 else
2324 {
2325 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2326 boff = font_info->baseline_offset;
2327 if (font_info->vertical_centering)
2328 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2329 }
2330
2331 /* There are no padding glyphs, so there is only one glyph to
2332 produce for the composition. Important is that pixel_width,
2333 ascent and descent are the values of what is drawn by
2334 draw_glyphs (i.e. the values of the overall glyphs composed). */
2335 it->nglyphs = 1;
2336
2337 /* If we have not yet calculated pixel size data of glyphs of
2338 the composition for the current face font, calculate them
2339 now. Theoretically, we have to check all fonts for the
2340 glyphs, but that requires much time and memory space. So,
2341 here we check only the font of the first glyph. This leads
2342 to incorrect display very rarely, and C-l (recenter) can
2343 correct the display anyway. */
2344 if (cmp->font != (void *) font)
2345 {
2346 /* Ascent and descent of the font of the first character of
2347 this composition (adjusted by baseline offset). Ascent
2348 and descent of overall glyphs should not be less than
2349 them respectively. */
2350 int font_ascent = FONT_BASE (font) + boff;
2351 int font_descent = FONT_DESCENT (font) - boff;
2352 /* Bounding box of the overall glyphs. */
2353 int leftmost, rightmost, lowest, highest;
2354 int i, width, ascent, descent;
1521ce96 2355 enum w32_char_font_type font_type;
01b220b6
JR
2356
2357 cmp->font = (void *) font;
2358
2359 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2360 font_type = BDF_1D_FONT;
2361 else if (font->bdf)
2362 font_type = BDF_2D_FONT;
2363 else
2364 font_type = UNICODE_FONT;
2365
2366 /* Initialize the bounding box. */
f201d732
JR
2367 if (font_info
2368 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
01b220b6
JR
2369 {
2370 width = pcm->width;
2371 ascent = pcm->ascent;
2372 descent = pcm->descent;
2373 }
2374 else
2375 {
2376 width = FONT_WIDTH (font);
2377 ascent = FONT_BASE (font);
2378 descent = FONT_DESCENT (font);
2379 }
d67d1627 2380
01b220b6
JR
2381 rightmost = width;
2382 lowest = - descent + boff;
2383 highest = ascent + boff;
2384 leftmost = 0;
d67d1627 2385
01b220b6
JR
2386 if (font_info
2387 && font_info->default_ascent
2388 && CHAR_TABLE_P (Vuse_default_ascent)
2389 && !NILP (Faref (Vuse_default_ascent,
2390 make_number (it->char_to_display))))
2391 highest = font_info->default_ascent + boff;
2392
2393 /* Draw the first glyph at the normal position. It may be
2394 shifted to right later if some other glyphs are drawn at
2395 the left. */
2396 cmp->offsets[0] = 0;
2397 cmp->offsets[1] = boff;
2398
2399 /* Set cmp->offsets for the remaining glyphs. */
2400 for (i = 1; i < cmp->glyph_len; i++)
2401 {
2402 int left, right, btm, top;
2403 int ch = COMPOSITION_GLYPH (cmp, i);
2404 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2405
2406 face = FACE_FROM_ID (it->f, face_id);
2407 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2408 it->multibyte_p);
2409 font = face->font;
2410 if (font == NULL)
2411 {
2412 font = FRAME_FONT (it->f);
2413 boff = it->f->output_data.w32->baseline_offset;
2414 font_info = NULL;
2415 }
2416 else
2417 {
2418 font_info
2419 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2420 boff = font_info->baseline_offset;
2421 if (font_info->vertical_centering)
2422 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2423 }
2424
2425 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (ch)) == 1)
2426 font_type = BDF_1D_FONT;
2427 else if (font->bdf)
2428 font_type = BDF_2D_FONT;
2429 else
2430 font_type = UNICODE_FONT;
2431
f201d732
JR
2432 if (font_info
2433 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
01b220b6
JR
2434 {
2435 width = pcm->width;
2436 ascent = pcm->ascent;
2437 descent = pcm->descent;
2438 }
2439 else
2440 {
2441 width = FONT_WIDTH (font);
f201d732
JR
2442 ascent = 1;
2443 descent = 0;
01b220b6
JR
2444 }
2445
2446 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2447 {
2448 /* Relative composition with or without
2449 alternate chars. */
2450 left = (leftmost + rightmost - width) / 2;
2451 btm = - descent + boff;
2452 if (font_info && font_info->relative_compose
2453 && (! CHAR_TABLE_P (Vignore_relative_composition)
2454 || NILP (Faref (Vignore_relative_composition,
2455 make_number (ch)))))
2456 {
2457
2458 if (- descent >= font_info->relative_compose)
2459 /* One extra pixel between two glyphs. */
2460 btm = highest + 1;
2461 else if (ascent <= 0)
2462 /* One extra pixel between two glyphs. */
2463 btm = lowest - 1 - ascent - descent;
2464 }
2465 }
2466 else
2467 {
2468 /* A composition rule is specified by an integer
2469 value that encodes global and new reference
2470 points (GREF and NREF). GREF and NREF are
2471 specified by numbers as below:
2472
2473 0---1---2 -- ascent
2474 | |
2475 | |
2476 | |
2477 9--10--11 -- center
2478 | |
2479 ---3---4---5--- baseline
2480 | |
2481 6---7---8 -- descent
2482 */
2483 int rule = COMPOSITION_RULE (cmp, i);
2484 int gref, nref, grefx, grefy, nrefx, nrefy;
2485
2486 COMPOSITION_DECODE_RULE (rule, gref, nref);
2487 grefx = gref % 3, nrefx = nref % 3;
2488 grefy = gref / 3, nrefy = nref / 3;
2489
2490 left = (leftmost
2491 + grefx * (rightmost - leftmost) / 2
2492 - nrefx * width / 2);
2493 btm = ((grefy == 0 ? highest
2494 : grefy == 1 ? 0
2495 : grefy == 2 ? lowest
2496 : (highest + lowest) / 2)
2497 - (nrefy == 0 ? ascent + descent
2498 : nrefy == 1 ? descent - boff
2499 : nrefy == 2 ? 0
2500 : (ascent + descent) / 2));
2501 }
2502
2503 cmp->offsets[i * 2] = left;
2504 cmp->offsets[i * 2 + 1] = btm + descent;
2505
2506 /* Update the bounding box of the overall glyphs. */
2507 right = left + width;
2508 top = btm + descent + ascent;
2509 if (left < leftmost)
2510 leftmost = left;
2511 if (right > rightmost)
2512 rightmost = right;
2513 if (top > highest)
2514 highest = top;
2515 if (btm < lowest)
2516 lowest = btm;
2517 }
2518
2519 /* If there are glyphs whose x-offsets are negative,
2520 shift all glyphs to the right and make all x-offsets
2521 non-negative. */
2522 if (leftmost < 0)
2523 {
2524 for (i = 0; i < cmp->glyph_len; i++)
2525 cmp->offsets[i * 2] -= leftmost;
2526 rightmost -= leftmost;
2527 }
2528
2529 cmp->pixel_width = rightmost;
2530 cmp->ascent = highest;
2531 cmp->descent = - lowest;
2532 if (cmp->ascent < font_ascent)
2533 cmp->ascent = font_ascent;
2534 if (cmp->descent < font_descent)
2535 cmp->descent = font_descent;
2536 }
2537
2538 it->pixel_width = cmp->pixel_width;
2539 it->ascent = it->phys_ascent = cmp->ascent;
2540 it->descent = it->phys_descent = cmp->descent;
2541
2542 if (face->box != FACE_NO_BOX)
2543 {
2544 int thick = face->box_line_width;
60222d69
AI
2545
2546 if (thick > 0)
2547 {
2548 it->ascent += thick;
2549 it->descent += thick;
2550 }
2551 else
2552 thick = - thick;
d67d1627 2553
01b220b6
JR
2554 if (it->start_of_box_run_p)
2555 it->pixel_width += thick;
2556 if (it->end_of_box_run_p)
2557 it->pixel_width += thick;
2558 }
d67d1627 2559
01b220b6
JR
2560 /* If face has an overline, add the height of the overline
2561 (1 pixel) and a 1 pixel margin to the character height. */
2562 if (face->overline_p)
2563 it->ascent += 2;
2564
2565 take_vertical_position_into_account (it);
d67d1627 2566
01b220b6
JR
2567 if (it->glyph_row)
2568 x_append_composite_glyph (it);
791f420f
JR
2569 }
2570 else if (it->what == IT_IMAGE)
2571 x_produce_image_glyph (it);
2572 else if (it->what == IT_STRETCH)
2573 x_produce_stretch_glyph (it);
2574
c2cc16fa
JR
2575 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2576 because this isn't true for images with `:ascent 100'. */
2577 xassert (it->ascent >= 0 && it->descent >= 0);
791f420f
JR
2578 if (it->area == TEXT_AREA)
2579 it->current_x += it->pixel_width;
2580
9ef2e2cf
JR
2581 it->descent += it->extra_line_spacing;
2582
791f420f
JR
2583 it->max_ascent = max (it->max_ascent, it->ascent);
2584 it->max_descent = max (it->max_descent, it->descent);
2585 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2586 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2587}
2588
2589
2590/* Estimate the pixel height of the mode or top line on frame F.
2591 FACE_ID specifies what line's height to estimate. */
2592
2593int
2594x_estimate_mode_line_height (f, face_id)
2595 struct frame *f;
2596 enum face_id face_id;
2597{
250cfece 2598 int height = FONT_HEIGHT (FRAME_FONT (f));
791f420f
JR
2599
2600 /* This function is called so early when Emacs starts that the face
2601 cache and mode line face are not yet initialized. */
2602 if (FRAME_FACE_CACHE (f))
2603 {
2604 struct face *face = FACE_FROM_ID (f, face_id);
9ef2e2cf 2605 if (face)
250cfece
JR
2606 {
2607 if (face->font)
2608 height = FONT_HEIGHT (face->font);
60222d69
AI
2609 if (face->box_line_width > 0)
2610 height += 2 * face->box_line_width;
250cfece 2611 }
791f420f 2612 }
d67d1627 2613
791f420f
JR
2614 return height;
2615}
cabb23bc 2616
cabb23bc 2617\f
791f420f
JR
2618/***********************************************************************
2619 Glyph display
2620 ***********************************************************************/
2621
2622/* A sequence of glyphs to be drawn in the same face.
2623
2624 This data structure is not really completely X specific, so it
2625 could possibly, at least partially, be useful for other systems. It
2626 is currently not part of the external redisplay interface because
2627 it's not clear what other systems will need. */
2628
2629struct glyph_string
2630{
2631 /* X-origin of the string. */
2632 int x;
2633
2634 /* Y-origin and y-position of the base line of this string. */
2635 int y, ybase;
2636
2637 /* The width of the string, not including a face extension. */
2638 int width;
2639
2640 /* The width of the string, including a face extension. */
2641 int background_width;
2642
2643 /* The height of this string. This is the height of the line this
2644 string is drawn in, and can be different from the height of the
2645 font the string is drawn in. */
2646 int height;
2647
2648 /* Number of pixels this string overwrites in front of its x-origin.
2649 This number is zero if the string has an lbearing >= 0; it is
2650 -lbearing, if the string has an lbearing < 0. */
2651 int left_overhang;
2652
2653 /* Number of pixels this string overwrites past its right-most
2654 nominal x-position, i.e. x + width. Zero if the string's
2655 rbearing is <= its nominal width, rbearing - width otherwise. */
2656 int right_overhang;
2657
2658 /* The frame on which the glyph string is drawn. */
2659 struct frame *f;
2660
2661 /* The window on which the glyph string is drawn. */
2662 struct window *w;
2663
2664 /* X display and window for convenience. */
2665 Window window;
2666
2667 /* The glyph row for which this string was built. It determines the
2668 y-origin and height of the string. */
2669 struct glyph_row *row;
2670
2671 /* The area within row. */
2672 enum glyph_row_area area;
2673
2674 /* Characters to be drawn, and number of characters. */
2675 wchar_t *char2b;
2676 int nchars;
2677
791f420f
JR
2678 /* A face-override for drawing cursors, mouse face and similar. */
2679 enum draw_glyphs_face hl;
2680
2681 /* Face in which this string is to be drawn. */
2682 struct face *face;
2683
2684 /* Font in which this string is to be drawn. */
2685 XFontStruct *font;
2686
2687 /* Font info for this string. */
2688 struct font_info *font_info;
2689
2690 /* Non-null means this string describes (part of) a composition.
2691 All characters from char2b are drawn composed. */
2692 struct composition *cmp;
2693
2694 /* Index of this glyph string's first character in the glyph
2695 definition of CMP. If this is zero, this glyph string describes
2696 the first character of a composition. */
2697 int gidx;
2698
2699 /* 1 means this glyph strings face has to be drawn to the right end
2700 of the window's drawing area. */
2701 unsigned extends_to_end_of_line_p : 1;
2702
2703 /* 1 means the background of this string has been drawn. */
2704 unsigned background_filled_p : 1;
2705
2706 /* 1 means glyph string must be drawn with 16-bit functions. */
2707 unsigned two_byte_p : 1;
2708
2709 /* 1 means that the original font determined for drawing this glyph
2710 string could not be loaded. The member `font' has been set to
2711 the frame's default font in this case. */
2712 unsigned font_not_found_p : 1;
2713
2714 /* 1 means that the face in which this glyph string is drawn has a
2715 stipple pattern. */
2716 unsigned stippled_p : 1;
2717
2718 /* 1 means only the foreground of this glyph string must be drawn,
2719 and we should use the physical height of the line this glyph
2720 string appears in as clip rect. */
2721 unsigned for_overlaps_p : 1;
2722
2723 /* The GC to use for drawing this glyph string. */
2724 XGCValues *gc;
2725
2726 HDC hdc;
2727
2728 /* A pointer to the first glyph in the string. This glyph
2729 corresponds to char2b[0]. Needed to draw rectangles if
2730 font_not_found_p is 1. */
2731 struct glyph *first_glyph;
2732
2733 /* Image, if any. */
2734 struct image *img;
2735
2736 struct glyph_string *next, *prev;
2737};
2738
2739
9ef2e2cf 2740/* Encapsulate the different ways of displaying text under W32. */
791f420f 2741
1cb8c82e
JR
2742static void
2743w32_text_out (s, x, y,chars,nchars)
791f420f
JR
2744 struct glyph_string * s;
2745 int x, y;
2746 wchar_t * chars;
2747 int nchars;
2748{
9ef2e2cf 2749 int charset_dim = w32_font_is_double_byte (s->gc->font) ? 2 : 1;
791f420f
JR
2750 if (s->gc->font->bdf)
2751 w32_BDF_TextOut (s->gc->font->bdf, s->hdc,
00c96995
JR
2752 x, y, (char *) chars, charset_dim,
2753 nchars * charset_dim, 0);
9ef2e2cf 2754 else if (s->first_glyph->w32_font_type == UNICODE_FONT)
791f420f
JR
2755 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
2756 else
1cb8c82e
JR
2757 ExtTextOutA (s->hdc, x, y, 0, NULL, (char *) chars,
2758 nchars * charset_dim, NULL);
791f420f
JR
2759}
2760
9f5a911b 2761#if GLYPH_DEBUG
791f420f
JR
2762
2763static void
2764x_dump_glyph_string (s)
2765 struct glyph_string *s;
2766{
2767 fprintf (stderr, "glyph string\n");
2768 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2769 s->x, s->y, s->width, s->height);
2770 fprintf (stderr, " ybase = %d\n", s->ybase);
2771 fprintf (stderr, " hl = %d\n", s->hl);
2772 fprintf (stderr, " left overhang = %d, right = %d\n",
2773 s->left_overhang, s->right_overhang);
2774 fprintf (stderr, " nchars = %d\n", s->nchars);
2775 fprintf (stderr, " extends to end of line = %d\n",
2776 s->extends_to_end_of_line_p);
2777 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2778 fprintf (stderr, " bg width = %d\n", s->background_width);
2779}
2780
2781#endif /* GLYPH_DEBUG */
2782
2783
2784
2785static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2786 struct glyph_string **,
2787 struct glyph_string *,
2788 struct glyph_string *));
2789static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2790 struct glyph_string **,
2791 struct glyph_string *,
2792 struct glyph_string *));
2793static void x_append_glyph_string P_ ((struct glyph_string **,
2794 struct glyph_string **,
2795 struct glyph_string *));
2796static int x_left_overwritten P_ ((struct glyph_string *));
2797static int x_left_overwriting P_ ((struct glyph_string *));
2798static int x_right_overwritten P_ ((struct glyph_string *));
2799static int x_right_overwriting P_ ((struct glyph_string *));
c2cc16fa
JR
2800static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2801 int));
791f420f 2802static void w32_init_glyph_string P_ ((struct glyph_string *, HDC hdc,
c2cc16fa
JR
2803 wchar_t *, struct window *,
2804 struct glyph_row *,
d67d1627 2805 enum glyph_row_area, int,
c2cc16fa 2806 enum draw_glyphs_face));
791f420f
JR
2807static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2808 enum glyph_row_area, int, int,
6ff3e5e3 2809 enum draw_glyphs_face, int));
791f420f
JR
2810static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2811static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2812static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2813 int));
2814static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2815static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
791f420f
JR
2816static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2817static void x_draw_glyph_string P_ ((struct glyph_string *));
2818static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2819static void x_set_cursor_gc P_ ((struct glyph_string *));
2820static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2821static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2822static void w32_get_glyph_overhangs P_ ((HDC hdc, struct glyph *,
2823 struct frame *,
9ef2e2cf 2824 int *, int *));
791f420f
JR
2825static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2826static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
2827static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
2828 double, int, COLORREF));
2829static void x_setup_relief_colors P_ ((struct glyph_string *));
2830static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2831static void x_draw_image_relief P_ ((struct glyph_string *));
2832static void x_draw_image_foreground P_ ((struct glyph_string *));
2833static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
2834static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2835static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2836 int, int, int));
2837static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2838 int, int, int, int, RECT *));
2839static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2840 int, int, int, RECT *));
2841static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2842 enum glyph_row_area));
c2cc16fa
JR
2843static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2844 struct glyph_row *,
2845 enum glyph_row_area, int, int));
2846
2847#if GLYPH_DEBUG
2848static void x_check_font P_ ((struct frame *, XFontStruct *));
2849#endif
791f420f 2850
d67d1627 2851
791f420f
JR
2852/* Append the list of glyph strings with head H and tail T to the list
2853 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2854
2855static INLINE void
2856x_append_glyph_string_lists (head, tail, h, t)
2857 struct glyph_string **head, **tail;
2858 struct glyph_string *h, *t;
2859{
2860 if (h)
2861 {
2862 if (*head)
2863 (*tail)->next = h;
2864 else
2865 *head = h;
2866 h->prev = *tail;
2867 *tail = t;
2868 }
2869}
2870
2871
2872/* Prepend the list of glyph strings with head H and tail T to the
2873 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2874 result. */
2875
2876static INLINE void
2877x_prepend_glyph_string_lists (head, tail, h, t)
2878 struct glyph_string **head, **tail;
2879 struct glyph_string *h, *t;
2880{
2881 if (h)
2882 {
2883 if (*head)
2884 (*head)->prev = t;
2885 else
2886 *tail = t;
2887 t->next = *head;
2888 *head = h;
2889 }
2890}
2891
2892
2893/* Append glyph string S to the list with head *HEAD and tail *TAIL.
2894 Set *HEAD and *TAIL to the resulting list. */
2895
2896static INLINE void
2897x_append_glyph_string (head, tail, s)
2898 struct glyph_string **head, **tail;
2899 struct glyph_string *s;
2900{
2901 s->next = s->prev = NULL;
2902 x_append_glyph_string_lists (head, tail, s, s);
2903}
2904
2905
2906/* Set S->gc to a suitable GC for drawing glyph string S in cursor
2907 face. */
2908
2909static void
2910x_set_cursor_gc (s)
2911 struct glyph_string *s;
2912{
2913 if (s->font == FRAME_FONT (s->f)
2914 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2915 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2916 && !s->cmp)
2917 s->gc = s->f->output_data.w32->cursor_gc;
2918 else
2919 {
2920 /* Cursor on non-default face: must merge. */
2921 XGCValues xgcv;
2922 unsigned long mask;
2923
2924 xgcv.background = s->f->output_data.w32->cursor_pixel;
2925 xgcv.foreground = s->face->background;
2926
2927 /* If the glyph would be invisible, try a different foreground. */
2928 if (xgcv.foreground == xgcv.background)
2929 xgcv.foreground = s->face->foreground;
2930 if (xgcv.foreground == xgcv.background)
2931 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
2932 if (xgcv.foreground == xgcv.background)
2933 xgcv.foreground = s->face->foreground;
2934
2935 /* Make sure the cursor is distinct from text in this face. */
2936 if (xgcv.background == s->face->background
2937 && xgcv.foreground == s->face->foreground)
2938 {
2939 xgcv.background = s->face->foreground;
2940 xgcv.foreground = s->face->background;
2941 }
2942
2943 IF_DEBUG (x_check_font (s->f, s->font));
2944 xgcv.font = s->font;
2945 mask = GCForeground | GCBackground | GCFont;
2946
2947 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2948 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2949 mask, &xgcv);
2950 else
2951 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2952 = XCreateGC (NULL, s->window, mask, &xgcv);
2953
2954 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2955 }
2956}
2957
2958
2959/* Set up S->gc of glyph string S for drawing text in mouse face. */
d67d1627 2960
791f420f
JR
2961static void
2962x_set_mouse_face_gc (s)
2963 struct glyph_string *s;
d67d1627 2964{
791f420f 2965 int face_id;
93ff4395 2966 struct face *face;
791f420f 2967
c2cc16fa 2968 /* What face has to be used last for the mouse face? */
791f420f 2969 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
93ff4395 2970 face = FACE_FROM_ID (s->f, face_id);
c2cc16fa
JR
2971 if (face == NULL)
2972 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
d67d1627 2973
c2cc16fa
JR
2974 if (s->first_glyph->type == CHAR_GLYPH)
2975 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2976 else
2977 face_id = FACE_FOR_CHAR (s->f, face, 0);
791f420f
JR
2978 s->face = FACE_FROM_ID (s->f, face_id);
2979 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2980
2981 /* If font in this face is same as S->font, use it. */
2982 if (s->font == s->face->font)
2983 s->gc = s->face->gc;
2984 else
2985 {
2986 /* Otherwise construct scratch_cursor_gc with values from FACE
2987 but font FONT. */
2988 XGCValues xgcv;
2989 unsigned long mask;
d67d1627 2990
791f420f
JR
2991 xgcv.background = s->face->background;
2992 xgcv.foreground = s->face->foreground;
2993 IF_DEBUG (x_check_font (s->f, s->font));
2994 xgcv.font = s->font;
2995 mask = GCForeground | GCBackground | GCFont;
d67d1627 2996
791f420f
JR
2997 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2998 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2999 mask, &xgcv);
3000 else
3001 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
3002 = XCreateGC (NULL, s->window, mask, &xgcv);
d67d1627 3003
791f420f
JR
3004 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
3005 }
3006
3007 xassert (s->gc != 0);
3008}
3009
3010
3011/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
3012 Faces to use in the mode line have already been computed when the
3013 matrix was built, so there isn't much to do, here. */
3014
3015static INLINE void
3016x_set_mode_line_face_gc (s)
3017 struct glyph_string *s;
d67d1627 3018{
791f420f 3019 s->gc = s->face->gc;
791f420f
JR
3020}
3021
3022
3023/* Set S->gc of glyph string S for drawing that glyph string. Set
3024 S->stippled_p to a non-zero value if the face of S has a stipple
3025 pattern. */
3026
3027static INLINE void
3028x_set_glyph_string_gc (s)
3029 struct glyph_string *s;
3030{
ec48c3a7 3031 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
d67d1627 3032
791f420f
JR
3033 if (s->hl == DRAW_NORMAL_TEXT)
3034 {
3035 s->gc = s->face->gc;
3036 s->stippled_p = s->face->stipple != 0;
3037 }
3038 else if (s->hl == DRAW_INVERSE_VIDEO)
3039 {
3040 x_set_mode_line_face_gc (s);
3041 s->stippled_p = s->face->stipple != 0;
3042 }
3043 else if (s->hl == DRAW_CURSOR)
3044 {
3045 x_set_cursor_gc (s);
3046 s->stippled_p = 0;
3047 }
3048 else if (s->hl == DRAW_MOUSE_FACE)
3049 {
3050 x_set_mouse_face_gc (s);
3051 s->stippled_p = s->face->stipple != 0;
3052 }
3053 else if (s->hl == DRAW_IMAGE_RAISED
3054 || s->hl == DRAW_IMAGE_SUNKEN)
3055 {
3056 s->gc = s->face->gc;
3057 s->stippled_p = s->face->stipple != 0;
3058 }
3059 else
3060 {
3061 s->gc = s->face->gc;
3062 s->stippled_p = s->face->stipple != 0;
3063 }
3064
3065 /* GC must have been set. */
3066 xassert (s->gc != 0);
3067}
3068
3069
3070/* Return in *R the clipping rectangle for glyph string S. */
3071
3072static void
3073w32_get_glyph_string_clip_rect (s, r)
3074 struct glyph_string *s;
3075 RECT *r;
3076{
3077 int r_height, r_width;
3078
3079 if (s->row->full_width_p)
3080 {
3081 /* Draw full-width. X coordinates are relative to S->w->left. */
3082 int canon_x = CANON_X_UNIT (s->f);
d67d1627 3083
791f420f
JR
3084 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
3085 r_width = XFASTINT (s->w->width) * canon_x;
3086
3087 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
3088 {
3089 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
3090 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
3091 r->left -= width;
3092 }
d67d1627 3093
791f420f
JR
3094 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
3095
3096 /* Unless displaying a mode or menu bar line, which are always
3097 fully visible, clip to the visible part of the row. */
3098 if (s->w->pseudo_window_p)
3099 r_height = s->row->visible_height;
3100 else
3101 r_height = s->height;
3102 }
3103 else
3104 {
3105 /* This is a text line that may be partially visible. */
3106 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
3107 r_width = window_box_width (s->w, s->area);
3108 r_height = s->row->visible_height;
3109 }
3110
791f420f
JR
3111 /* If S draws overlapping rows, it's sufficient to use the top and
3112 bottom of the window for clipping because this glyph string
3113 intentionally draws over other lines. */
3114 if (s->for_overlaps_p)
3115 {
3116 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3117 r_height = window_text_bottom_y (s->w) - r->top;
3118 }
9f5a911b
JR
3119 else
3120 {
3121 /* Don't use S->y for clipping because it doesn't take partially
3122 visible lines into account. For example, it can be negative for
3123 partially visible lines at the top of a window. */
3124 if (!s->row->full_width_p
3125 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
3126 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3127 else
3128 r->top = max (0, s->row->y);
3129
3130 /* If drawing a tool-bar window, draw it over the internal border
3131 at the top of the window. */
3132 if (s->w == XWINDOW (s->f->tool_bar_window))
3133 r->top -= s->f->output_data.w32->internal_border_width;
3134 }
3135
791f420f
JR
3136 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
3137
2c26ee51
JR
3138 /* If drawing the cursor, don't let glyph draw outside its
3139 advertised boundaries. Cleartype does this under some circumstances. */
3140 if (s->hl == DRAW_CURSOR)
3141 {
3142 if (s->x > r->left)
3143 {
3144 r_width -= s->x - r->left;
3145 r->left = s->x;
3146 }
3147 r_width = min (r_width, s->first_glyph->pixel_width);
3148 }
3149
791f420f
JR
3150 r->bottom = r->top + r_height;
3151 r->right = r->left + r_width;
3152}
3153
3154
3155/* Set clipping for output of glyph string S. S may be part of a mode
3156 line or menu if we don't have X toolkit support. */
3157
3158static INLINE void
3159x_set_glyph_string_clipping (s)
3160 struct glyph_string *s;
3161{
3162 RECT r;
3163 w32_get_glyph_string_clip_rect (s, &r);
3164 w32_set_clip_rectangle (s->hdc, &r);
3165}
3166
3167
3168/* Compute left and right overhang of glyph string S. If S is a glyph
3169 string for a composition, assume overhangs don't exist. */
3170
3171static INLINE void
3172x_compute_glyph_string_overhangs (s)
3173 struct glyph_string *s;
3174{
01b220b6 3175 /* TODO: Windows does not appear to have a method for
158cba56
JR
3176 getting this info without getting the ABC widths for each
3177 individual character and working it out manually. */
791f420f
JR
3178}
3179
3180
3181/* Compute overhangs and x-positions for glyph string S and its
3182 predecessors, or successors. X is the starting x-position for S.
3183 BACKWARD_P non-zero means process predecessors. */
d67d1627 3184
791f420f
JR
3185static void
3186x_compute_overhangs_and_x (s, x, backward_p)
3187 struct glyph_string *s;
3188 int x;
3189 int backward_p;
3190{
3191 if (backward_p)
3192 {
3193 while (s)
3194 {
3195 x_compute_glyph_string_overhangs (s);
3196 x -= s->width;
3197 s->x = x;
3198 s = s->prev;
3199 }
3200 }
3201 else
3202 {
3203 while (s)
3204 {
3205 x_compute_glyph_string_overhangs (s);
3206 s->x = x;
3207 x += s->width;
3208 s = s->next;
3209 }
3210 }
3211}
3212
3213
3214/* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
3215 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
3216 assumed to be zero. */
3217
3218static void
9ef2e2cf 3219w32_get_glyph_overhangs (hdc, glyph, f, left, right)
00fe468b 3220 HDC hdc;
791f420f
JR
3221 struct glyph *glyph;
3222 struct frame *f;
9ef2e2cf 3223 int *left, *right;
791f420f 3224{
791f420f 3225 *left = *right = 0;
d67d1627 3226
791f420f
JR
3227 if (glyph->type == CHAR_GLYPH)
3228 {
3229 XFontStruct *font;
3230 struct face *face;
791f420f 3231 wchar_t char2b;
93ff4395
JR
3232 XCharStruct *pcm;
3233
3234 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
791f420f 3235 font = face->font;
9ef2e2cf 3236
93ff4395 3237 if (font
82f9d565 3238 && (pcm = w32_per_char_metric (font, &char2b,
9ef2e2cf 3239 glyph->w32_font_type)))
791f420f 3240 {
791f420f
JR
3241 if (pcm->rbearing > pcm->width)
3242 *right = pcm->rbearing - pcm->width;
3243 if (pcm->lbearing < 0)
3244 *left = -pcm->lbearing;
3245 }
3246 }
791f420f
JR
3247}
3248
3249
3250static void
3251x_get_glyph_overhangs (glyph, f, left, right)
3252 struct glyph *glyph;
3253 struct frame *f;
3254 int *left, *right;
3255{
3256 HDC hdc = get_frame_dc (f);
9ef2e2cf
JR
3257 /* Convert to unicode! */
3258 w32_get_glyph_overhangs (hdc, glyph, f, left, right);
791f420f
JR
3259 release_frame_dc (f, hdc);
3260}
3261
3262
3263/* Return the index of the first glyph preceding glyph string S that
3264 is overwritten by S because of S's left overhang. Value is -1
3265 if no glyphs are overwritten. */
3266
3267static int
3268x_left_overwritten (s)
3269 struct glyph_string *s;
3270{
3271 int k;
d67d1627 3272
791f420f
JR
3273 if (s->left_overhang)
3274 {
3275 int x = 0, i;
3276 struct glyph *glyphs = s->row->glyphs[s->area];
3277 int first = s->first_glyph - glyphs;
3278
3279 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3280 x -= glyphs[i].pixel_width;
3281
3282 k = i + 1;
3283 }
3284 else
3285 k = -1;
3286
3287 return k;
3288}
3289
3290
3291/* Return the index of the first glyph preceding glyph string S that
3292 is overwriting S because of its right overhang. Value is -1 if no
3293 glyph in front of S overwrites S. */
3294
3295static int
3296x_left_overwriting (s)
3297 struct glyph_string *s;
3298{
3299 int i, k, x;
3300 struct glyph *glyphs = s->row->glyphs[s->area];
3301 int first = s->first_glyph - glyphs;
3302
3303 k = -1;
3304 x = 0;
3305 for (i = first - 1; i >= 0; --i)
3306 {
3307 int left, right;
9ef2e2cf 3308 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
791f420f
JR
3309 if (x + right > 0)
3310 k = i;
3311 x -= glyphs[i].pixel_width;
3312 }
3313
3314 return k;
3315}
3316
3317
3318/* Return the index of the last glyph following glyph string S that is
3319 not overwritten by S because of S's right overhang. Value is -1 if
3320 no such glyph is found. */
3321
3322static int
3323x_right_overwritten (s)
3324 struct glyph_string *s;
3325{
3326 int k = -1;
3327
3328 if (s->right_overhang)
3329 {
3330 int x = 0, i;
3331 struct glyph *glyphs = s->row->glyphs[s->area];
3332 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3333 int end = s->row->used[s->area];
d67d1627 3334
791f420f
JR
3335 for (i = first; i < end && s->right_overhang > x; ++i)
3336 x += glyphs[i].pixel_width;
3337
3338 k = i;
3339 }
3340
3341 return k;
3342}
3343
3344
3345/* Return the index of the last glyph following glyph string S that
3346 overwrites S because of its left overhang. Value is negative
3347 if no such glyph is found. */
3348
3349static int
3350x_right_overwriting (s)
3351 struct glyph_string *s;
3352{
3353 int i, k, x;
3354 int end = s->row->used[s->area];
3355 struct glyph *glyphs = s->row->glyphs[s->area];
3356 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3357
3358 k = -1;
3359 x = 0;
3360 for (i = first; i < end; ++i)
3361 {
3362 int left, right;
9ef2e2cf 3363 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
791f420f
JR
3364 if (x - left < 0)
3365 k = i;
3366 x += glyphs[i].pixel_width;
3367 }
3368
3369 return k;
3370}
3371
3372
3373/* Fill rectangle X, Y, W, H with background color of glyph string S. */
3374
3375static INLINE void
3376x_clear_glyph_string_rect (s, x, y, w, h)
3377 struct glyph_string *s;
3378 int x, y, w, h;
3379{
3380 int real_x = x;
3381 int real_y = y;
3382 int real_w = w;
3383 int real_h = h;
3384#if 0
3385 /* Take clipping into account. */
3386 if (s->gc->clip_mask == Rect)
3387 {
3388 real_x = max (real_x, s->gc->clip_rectangle.left);
3389 real_y = max (real_y, s->gc->clip_rectangle.top);
3390 real_w = min (real_w, s->gc->clip_rectangle.right
3391 - s->gc->clip_rectangle.left);
3392 real_h = min (real_h, s->gc->clip_rectangle.bottom
3393 - s->gc->clip_rectangle.top);
3394 }
3395#endif
3396 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
3397 real_w, real_h);
3398}
3399
3400
3401/* Draw the background of glyph_string S. If S->background_filled_p
3402 is non-zero don't draw it. FORCE_P non-zero means draw the
3403 background even if it wouldn't be drawn normally. This is used
3404 when a string preceding S draws into the background of S, or S
3405 contains the first component of a composition. */
3406
3407static void
3408x_draw_glyph_string_background (s, force_p)
3409 struct glyph_string *s;
3410 int force_p;
3411{
3412 /* Nothing to do if background has already been drawn or if it
3413 shouldn't be drawn in the first place. */
3414 if (!s->background_filled_p)
3415 {
60222d69
AI
3416 int box_line_width = max (s->face->box_line_width, 0);
3417
01b220b6 3418#if 0 /* TODO: stipple */
791f420f
JR
3419 if (s->stippled_p)
3420 {
3421 /* Fill background with a stipple pattern. */
3422 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3423 XFillRectangle (s->display, s->window, s->gc, s->x,
60222d69 3424 s->y + box_line_width,
791f420f 3425 s->background_width,
60222d69 3426 s->height - 2 * box_line_width);
791f420f
JR
3427 XSetFillStyle (s->display, s->gc, FillSolid);
3428 s->background_filled_p = 1;
3429 }
3430 else
3431#endif
60222d69 3432 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
791f420f
JR
3433 || s->font_not_found_p
3434 || s->extends_to_end_of_line_p
9127e20e 3435 || s->font->bdf
791f420f
JR
3436 || force_p)
3437 {
60222d69 3438 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
791f420f 3439 s->background_width,
60222d69 3440 s->height - 2 * box_line_width);
791f420f
JR
3441 s->background_filled_p = 1;
3442 }
3443 }
3444}
3445
3446
3447/* Draw the foreground of glyph string S. */
3448
3449static void
3450x_draw_glyph_string_foreground (s)
3451 struct glyph_string *s;
3452{
3453 int i, x;
01b220b6 3454 HFONT old_font;
791f420f
JR
3455
3456 /* If first glyph of S has a left box line, start drawing the text
3457 of S to the right of that box line. */
3458 if (s->face->box != FACE_NO_BOX
3459 && s->first_glyph->left_box_line_p)
60222d69 3460 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3461 else
3462 x = s->x;
3463
3464 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
3465 SetBkMode (s->hdc, TRANSPARENT);
3466 else
3467 SetBkMode (s->hdc, OPAQUE);
3468
3469 SetTextColor (s->hdc, s->gc->foreground);
3470 SetBkColor (s->hdc, s->gc->background);
3471 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3472
3473 if (s->font && s->font->hfont)
01b220b6 3474 old_font = SelectObject (s->hdc, s->font->hfont);
791f420f
JR
3475
3476 /* Draw characters of S as rectangles if S's font could not be
3477 loaded. */
3478 if (s->font_not_found_p)
3479 {
3480 for (i = 0; i < s->nchars; ++i)
3481 {
3482 struct glyph *g = s->first_glyph + i;
3483
3484 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
3485 s->height - 1);
3486 x += g->pixel_width;
3487 }
3488 }
3489 else
3490 {
3491 char *char1b = (char *) s->char2b;
3492 int boff = s->font_info->baseline_offset;
3493
3494 if (s->font_info->vertical_centering)
3495 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3496
3497 /* If we can use 8-bit functions, condense S->char2b. */
3498 if (!s->two_byte_p)
3499 for (i = 0; i < s->nchars; ++i)
3500 char1b[i] = BYTE2 (s->char2b[i]);
3501
3502 /* Draw text with TextOut and friends. */
1cb8c82e 3503 w32_text_out (s, x, s->ybase - boff, s->char2b, s->nchars);
791f420f 3504 }
01b220b6
JR
3505 if (s->font && s->font->hfont)
3506 SelectObject (s->hdc, old_font);
791f420f
JR
3507}
3508
3509/* Draw the foreground of composite glyph string S. */
3510
3511static void
3512x_draw_composite_glyph_string_foreground (s)
3513 struct glyph_string *s;
3514{
3515 int i, x;
01b220b6 3516 HFONT old_font;
791f420f
JR
3517
3518 /* If first glyph of S has a left box line, start drawing the text
3519 of S to the right of that box line. */
3520 if (s->face->box != FACE_NO_BOX
3521 && s->first_glyph->left_box_line_p)
60222d69 3522 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3523 else
3524 x = s->x;
3525
3526 /* S is a glyph string for a composition. S->gidx is the index of
3527 the first character drawn for glyphs of this composition.
3528 S->gidx == 0 means we are drawing the very first character of
3529 this composition. */
3530
3531 SetTextColor (s->hdc, s->gc->foreground);
3532 SetBkColor (s->hdc, s->gc->background);
3533 SetBkMode (s->hdc, TRANSPARENT);
3534 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3535
01b220b6
JR
3536 if (s->font && s->font->hfont)
3537 old_font = SelectObject (s->hdc, s->font->hfont);
3538
791f420f
JR
3539 /* Draw a rectangle for the composition if the font for the very
3540 first character of the composition could not be loaded. */
3541 if (s->font_not_found_p)
3542 {
3543 if (s->gidx == 0)
3544 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
3545 s->height - 1);
3546 }
3547 else
3548 {
3549 for (i = 0; i < s->nchars; i++, ++s->gidx)
1cb8c82e 3550 w32_text_out (s, x + s->cmp->offsets[s->gidx * 2],
01b220b6
JR
3551 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3552 s->char2b + i, 1);
791f420f 3553 }
01b220b6
JR
3554 if (s->font && s->font->hfont)
3555 SelectObject (s->hdc, old_font);
791f420f
JR
3556}
3557
e5fa381b
JR
3558
3559/* Brightness beyond which a color won't have its highlight brightness
3560 boosted.
3561
3562 Nominally, highlight colors for `3d' faces are calculated by
3563 brightening an object's color by a constant scale factor, but this
3564 doesn't yield good results for dark colors, so for colors who's
3565 brightness is less than this value (on a scale of 0-255) have to
3566 use an additional additive factor.
3567
3568 The value here is set so that the default menu-bar/mode-line color
3569 (grey75) will not have its highlights changed at all. */
3570#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
3571
3572
791f420f
JR
3573/* Allocate a color which is lighter or darker than *COLOR by FACTOR
3574 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3575 If this produces the same color as COLOR, try a color where all RGB
3576 values have DELTA added. Return the allocated color in *COLOR.
3577 DISPLAY is the X display, CMAP is the colormap to operate on.
3578 Value is non-zero if successful. */
3579
3580static int
3581w32_alloc_lighter_color (f, color, factor, delta)
3582 struct frame *f;
3583 COLORREF *color;
3584 double factor;
3585 int delta;
3586{
3587 COLORREF new;
e5fa381b
JR
3588 long bright;
3589
3590 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
3591 delta /= 256;
791f420f
JR
3592
3593 /* Change RGB values by specified FACTOR. Avoid overflow! */
3594 xassert (factor >= 0);
3595 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
3596 min (0xff, factor * GetGValue (*color)),
3597 min (0xff, factor * GetBValue (*color)));
e5fa381b
JR
3598
3599 /* Calculate brightness of COLOR. */
3600 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
3601 + GetBValue (*color)) / 6;
3602
3603 /* We only boost colors that are darker than
3604 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3605 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3606 /* Make an additive adjustment to NEW, because it's dark enough so
3607 that scaling by FACTOR alone isn't enough. */
3608 {
3609 /* How far below the limit this color is (0 - 1, 1 being darker). */
3610 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3611 /* The additive adjustment. */
3612 int min_delta = delta * dimness * factor / 2;
d67d1627 3613
e5fa381b
JR
3614 if (factor < 1)
3615 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
3616 max (0, min (0xff, min_delta - GetGValue (*color))),
3617 max (0, min (0xff, min_delta - GetBValue (*color))));
3618 else
3619 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
3620 max (0, min (0xff, min_delta + GetGValue (*color))),
3621 max (0, min (0xff, min_delta + GetBValue (*color))));
3622 }
d67d1627 3623
791f420f
JR
3624 if (new == *color)
3625 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
3626 max (0, min (0xff, delta + GetGValue (*color))),
3627 max (0, min (0xff, delta + GetBValue (*color))));
3628
01b220b6
JR
3629 /* TODO: Map to palette and retry with delta if same? */
3630 /* TODO: Free colors (if using palette)? */
93ff4395 3631
791f420f
JR
3632 if (new == *color)
3633 return 0;
3634
3635 *color = new;
3636
3637 return 1;
3638}
3639
3640
3641/* Set up the foreground color for drawing relief lines of glyph
3642 string S. RELIEF is a pointer to a struct relief containing the GC
3643 with which lines will be drawn. Use a color that is FACTOR or
3644 DELTA lighter or darker than the relief's background which is found
3645 in S->f->output_data.x->relief_background. If such a color cannot
3646 be allocated, use DEFAULT_PIXEL, instead. */
d67d1627 3647
791f420f
JR
3648static void
3649w32_setup_relief_color (f, relief, factor, delta, default_pixel)
3650 struct frame *f;
3651 struct relief *relief;
3652 double factor;
3653 int delta;
3654 COLORREF default_pixel;
3655{
3656 XGCValues xgcv;
3657 struct w32_output *di = f->output_data.w32;
3658 unsigned long mask = GCForeground;
3659 COLORREF pixel;
3660 COLORREF background = di->relief_background;
3661 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3662
01b220b6 3663 /* TODO: Free colors (if using palette)? */
93ff4395 3664
791f420f
JR
3665 /* Allocate new color. */
3666 xgcv.foreground = default_pixel;
3667 pixel = background;
3668 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
3669 {
3670 relief->allocated_p = 1;
3671 xgcv.foreground = relief->pixel = pixel;
3672 }
d67d1627 3673
791f420f
JR
3674 if (relief->gc == 0)
3675 {
01b220b6 3676#if 0 /* TODO: stipple */
791f420f
JR
3677 xgcv.stipple = dpyinfo->gray;
3678 mask |= GCStipple;
3679#endif
3680 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
3681 }
3682 else
3683 XChangeGC (NULL, relief->gc, mask, &xgcv);
3684}
3685
3686
3687/* Set up colors for the relief lines around glyph string S. */
3688
3689static void
3690x_setup_relief_colors (s)
3691 struct glyph_string *s;
3692{
3693 struct w32_output *di = s->f->output_data.w32;
3694 COLORREF color;
3695
3696 if (s->face->use_box_color_for_shadows_p)
3697 color = s->face->box_color;
6ff3e5e3 3698 else if (s->first_glyph->type == IMAGE_GLYPH
6637c996 3699 && s->img->pixmap
6ff3e5e3
JR
3700 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3701 color = IMAGE_BACKGROUND (s->img, s->f, 0);
791f420f
JR
3702 else
3703 color = s->gc->background;
3704
3705 if (di->white_relief.gc == 0
3706 || color != di->relief_background)
3707 {
3708 di->relief_background = color;
3709 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3710 WHITE_PIX_DEFAULT (s->f));
3711 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3712 BLACK_PIX_DEFAULT (s->f));
3713 }
3714}
3715
3716
3717/* Draw a relief on frame F inside the rectangle given by LEFT_X,
3718 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3719 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3720 relief. LEFT_P non-zero means draw a relief on the left side of
3721 the rectangle. RIGHT_P non-zero means draw a relief on the right
3722 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3723 when drawing. */
3724
3725static void
3726w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3727 raised_p, left_p, right_p, clip_rect)
3728 struct frame *f;
3729 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3730 RECT *clip_rect;
3731{
3732 int i;
3733 XGCValues gc;
3734 HDC hdc = get_frame_dc (f);
3735
3736 if (raised_p)
e5fa381b 3737 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
791f420f 3738 else
e5fa381b 3739 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
791f420f
JR
3740
3741 w32_set_clip_rectangle (hdc, clip_rect);
3742
3743 /* Top. */
3744 for (i = 0; i < width; ++i)
9f5a911b
JR
3745 w32_fill_area (f, hdc, gc.foreground,
3746 left_x + i * left_p, top_y + i,
df3bd29d 3747 right_x - left_x - i * (left_p + right_p ) + 1, 1);
791f420f
JR
3748
3749 /* Left. */
3750 if (left_p)
3751 for (i = 0; i < width; ++i)
9f5a911b
JR
3752 w32_fill_area (f, hdc, gc.foreground,
3753 left_x + i, top_y + i, 1,
df3bd29d 3754 bottom_y - top_y - 2 * i + 1);
791f420f
JR
3755
3756 if (raised_p)
e5fa381b 3757 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
791f420f 3758 else
e5fa381b 3759 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
d67d1627 3760
791f420f
JR
3761 /* Bottom. */
3762 for (i = 0; i < width; ++i)
d67d1627 3763 w32_fill_area (f, hdc, gc.foreground,
9f5a911b 3764 left_x + i * left_p, bottom_y - i,
df3bd29d 3765 right_x - left_x - i * (left_p + right_p) + 1, 1);
791f420f
JR
3766
3767 /* Right. */
3768 if (right_p)
3769 for (i = 0; i < width; ++i)
9f5a911b
JR
3770 w32_fill_area (f, hdc, gc.foreground,
3771 right_x - i, top_y + i + 1, 1,
df3bd29d 3772 bottom_y - top_y - 2 * i - 1);
791f420f
JR
3773
3774 w32_set_clip_rectangle (hdc, NULL);
d67d1627 3775
791f420f
JR
3776 release_frame_dc (f, hdc);
3777}
3778
3779
3780/* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3781 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3782 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3783 left side of the rectangle. RIGHT_P non-zero means draw a line
3784 on the right side of the rectangle. CLIP_RECT is the clipping
3785 rectangle to use when drawing. */
3786
3787static void
3788w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3789 left_p, right_p, clip_rect)
3790 struct glyph_string *s;
3791 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3792 RECT *clip_rect;
3793{
00fe468b 3794 w32_set_clip_rectangle (s->hdc, clip_rect);
d67d1627 3795
791f420f 3796 /* Top. */
00fe468b 3797 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3798 left_x, top_y, right_x - left_x + 1, width);
791f420f
JR
3799
3800 /* Left. */
3801 if (left_p)
3802 {
00fe468b 3803 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3804 left_x, top_y, width, bottom_y - top_y + 1);
791f420f 3805 }
d67d1627 3806
791f420f 3807 /* Bottom. */
00fe468b 3808 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3809 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
d67d1627 3810
791f420f
JR
3811 /* Right. */
3812 if (right_p)
3813 {
00fe468b 3814 w32_fill_area (s->f, s->hdc, s->face->box_color,
2d0c0bd7 3815 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
791f420f
JR
3816 }
3817
00fe468b 3818 w32_set_clip_rectangle (s->hdc, NULL);
791f420f
JR
3819}
3820
3821
3822/* Draw a box around glyph string S. */
3823
3824static void
3825x_draw_glyph_string_box (s)
3826 struct glyph_string *s;
3827{
3828 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3829 int left_p, right_p;
3830 struct glyph *last_glyph;
3831 RECT clip_rect;
3832
3833 last_x = window_box_right (s->w, s->area);
3834 if (s->row->full_width_p
3835 && !s->w->pseudo_window_p)
3836 {
33c34bea 3837 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
791f420f
JR
3838 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3839 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3840 }
d67d1627 3841
791f420f
JR
3842 /* The glyph that may have a right box line. */
3843 last_glyph = (s->cmp || s->img
3844 ? s->first_glyph
3845 : s->first_glyph + s->nchars - 1);
3846
60222d69 3847 width = abs (s->face->box_line_width);
791f420f
JR
3848 raised_p = s->face->box == FACE_RAISED_BOX;
3849 left_x = s->x;
9f5a911b 3850 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
791f420f
JR
3851 ? last_x - 1
3852 : min (last_x, s->x + s->background_width) - 1));
3853 top_y = s->y;
3854 bottom_y = top_y + s->height - 1;
3855
3856 left_p = (s->first_glyph->left_box_line_p
3857 || (s->hl == DRAW_MOUSE_FACE
3858 && (s->prev == NULL
3859 || s->prev->hl != s->hl)));
3860 right_p = (last_glyph->right_box_line_p
3861 || (s->hl == DRAW_MOUSE_FACE
3862 && (s->next == NULL
3863 || s->next->hl != s->hl)));
d67d1627 3864
791f420f
JR
3865 w32_get_glyph_string_clip_rect (s, &clip_rect);
3866
3867 if (s->face->box == FACE_SIMPLE_BOX)
3868 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3869 left_p, right_p, &clip_rect);
3870 else
3871 {
3872 x_setup_relief_colors (s);
3873 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3874 width, raised_p, left_p, right_p, &clip_rect);
3875 }
3876}
3877
3878
3879/* Draw foreground of image glyph string S. */
3880
3881static void
3882x_draw_image_foreground (s)
3883 struct glyph_string *s;
3884{
3885 int x;
9ef2e2cf 3886 int y = s->ybase - image_ascent (s->img, s->face);
791f420f
JR
3887
3888 /* If first glyph of S has a left box line, start drawing it to the
3889 right of that line. */
3890 if (s->face->box != FACE_NO_BOX
3891 && s->first_glyph->left_box_line_p)
60222d69 3892 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3893 else
3894 x = s->x;
3895
3896 /* If there is a margin around the image, adjust x- and y-position
3897 by that margin. */
d6ff54d5
JR
3898 x += s->img->hmargin;
3899 y += s->img->vmargin;
791f420f
JR
3900
3901 SaveDC (s->hdc);
3902
3903 if (s->img->pixmap)
3904 {
d30591dc
JR
3905 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3906 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3907 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3908 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3909 SetBkColor (compat_hdc, RGB (255, 255, 255));
3910 SetTextColor (s->hdc, RGB (0, 0, 0));
3911 x_set_glyph_string_clipping (s);
3912
791f420f
JR
3913 if (s->img->mask)
3914 {
d30591dc
JR
3915 HDC mask_dc = CreateCompatibleDC (s->hdc);
3916 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
3917
3918 SetTextColor (s->hdc, RGB (255, 255, 255));
3919 SetBkColor (s->hdc, RGB (0, 0, 0));
3920
3921 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
4108c39d 3922 compat_hdc, 0, 0, SRCINVERT);
d30591dc
JR
3923 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3924 mask_dc, 0, 0, SRCAND);
3925 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
4108c39d 3926 compat_hdc, 0, 0, SRCINVERT);
d30591dc
JR
3927
3928 SelectObject (mask_dc, mask_orig_obj);
3929 DeleteDC (mask_dc);
791f420f
JR
3930 }
3931 else
791f420f 3932 {
d30591dc
JR
3933 SetTextColor (s->hdc, s->gc->foreground);
3934 SetBkColor (s->hdc, s->gc->background);
3935
9436cdf9 3936 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
4108c39d 3937 compat_hdc, 0, 0, SRCCOPY);
791f420f
JR
3938
3939 /* When the image has a mask, we can expect that at
3940 least part of a mouse highlight or a block cursor will
3941 be visible. If the image doesn't have a mask, make
3942 a block cursor visible by drawing a rectangle around
3943 the image. I believe it's looking better if we do
3944 nothing here for mouse-face. */
3945 if (s->hl == DRAW_CURSOR)
c65eff23
KS
3946 {
3947 int r = s->img->relief;
3948 if (r < 0) r = -r;
3949 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
3950 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
3951 }
791f420f 3952 }
d30591dc
JR
3953
3954 w32_set_clip_rectangle (s->hdc, NULL);
3955 SelectObject (s->hdc, orig_brush);
3956 DeleteObject (fg_brush);
3957 SelectObject (compat_hdc, orig_obj);
3958 DeleteDC (compat_hdc);
791f420f
JR
3959 }
3960 else
3961 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width -1,
3962 s->img->height - 1);
ee78dc32 3963
791f420f
JR
3964 RestoreDC (s->hdc ,-1);
3965}
ee78dc32 3966
791f420f
JR
3967
3968
3969/* Draw a relief around the image glyph string S. */
3970
3971static void
3972x_draw_image_relief (s)
3973 struct glyph_string *s;
3974{
3975 int x0, y0, x1, y1, thick, raised_p;
3976 RECT r;
3977 int x;
9ef2e2cf 3978 int y = s->ybase - image_ascent (s->img, s->face);
d67d1627 3979
791f420f
JR
3980 /* If first glyph of S has a left box line, start drawing it to the
3981 right of that line. */
3982 if (s->face->box != FACE_NO_BOX
3983 && s->first_glyph->left_box_line_p)
60222d69 3984 x = s->x + abs (s->face->box_line_width);
791f420f
JR
3985 else
3986 x = s->x;
d67d1627 3987
791f420f
JR
3988 /* If there is a margin around the image, adjust x- and y-position
3989 by that margin. */
d6ff54d5
JR
3990 x += s->img->hmargin;
3991 y += s->img->vmargin;
d67d1627 3992
791f420f
JR
3993 if (s->hl == DRAW_IMAGE_SUNKEN
3994 || s->hl == DRAW_IMAGE_RAISED)
3995 {
6637c996 3996 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
791f420f
JR
3997 raised_p = s->hl == DRAW_IMAGE_RAISED;
3998 }
3999 else
ee78dc32 4000 {
791f420f
JR
4001 thick = abs (s->img->relief);
4002 raised_p = s->img->relief > 0;
4003 }
d67d1627 4004
791f420f
JR
4005 x0 = x - thick;
4006 y0 = y - thick;
4007 x1 = x + s->img->width + thick - 1;
4008 y1 = y + s->img->height + thick - 1;
d67d1627 4009
791f420f
JR
4010 x_setup_relief_colors (s);
4011 w32_get_glyph_string_clip_rect (s, &r);
4012 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4013}
ee78dc32 4014
791f420f
JR
4015
4016/* Draw the foreground of image glyph string S to PIXMAP. */
4017
4018static void
4019w32_draw_image_foreground_1 (s, pixmap)
4020 struct glyph_string *s;
4021 HBITMAP pixmap;
4022{
4023 HDC hdc = CreateCompatibleDC (s->hdc);
4024 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
4025 int x;
9ef2e2cf 4026 int y = s->ybase - s->y - image_ascent (s->img, s->face);
791f420f
JR
4027
4028 /* If first glyph of S has a left box line, start drawing it to the
4029 right of that line. */
4030 if (s->face->box != FACE_NO_BOX
4031 && s->first_glyph->left_box_line_p)
60222d69 4032 x = abs (s->face->box_line_width);
791f420f
JR
4033 else
4034 x = 0;
4035
4036 /* If there is a margin around the image, adjust x- and y-position
4037 by that margin. */
d6ff54d5
JR
4038 x += s->img->hmargin;
4039 y += s->img->vmargin;
791f420f
JR
4040
4041 if (s->img->pixmap)
4042 {
d30591dc
JR
4043 HDC compat_hdc = CreateCompatibleDC (hdc);
4044 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4045 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
4046 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
4047
791f420f
JR
4048 if (s->img->mask)
4049 {
d30591dc
JR
4050 HDC mask_dc = CreateCompatibleDC (hdc);
4051 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
4052
4053 SetTextColor (hdc, RGB (0, 0, 0));
4054 SetBkColor (hdc, RGB (255, 255, 255));
4055 BitBlt (hdc, x, y, s->img->width, s->img->height,
4056 compat_hdc, 0, 0, SRCINVERT);
4057 BitBlt (hdc, x, y, s->img->width, s->img->height,
4058 mask_dc, 0, 0, SRCAND);
4059 BitBlt (hdc, x, y, s->img->width, s->img->height,
4060 compat_hdc, 0, 0, SRCINVERT);
4061
4062 SelectObject (mask_dc, mask_orig_obj);
4063 DeleteDC (mask_dc);
791f420f
JR
4064 }
4065 else
ef0e360f 4066 {
d30591dc
JR
4067 SetTextColor (hdc, s->gc->foreground);
4068 SetBkColor (hdc, s->gc->background);
4069
9436cdf9 4070 BitBlt (hdc, x, y, s->img->width, s->img->height,
4108c39d 4071 compat_hdc, 0, 0, SRCCOPY);
791f420f
JR
4072
4073 /* When the image has a mask, we can expect that at
4074 least part of a mouse highlight or a block cursor will
4075 be visible. If the image doesn't have a mask, make
4076 a block cursor visible by drawing a rectangle around
4077 the image. I believe it's looking better if we do
4078 nothing here for mouse-face. */
4079 if (s->hl == DRAW_CURSOR)
c65eff23
KS
4080 {
4081 int r = s->img->relief;
4082 if (r < 0) r = -r;
d30591dc 4083 w32_draw_rectangle (hdc, s->gc, x - r, y - r ,
c65eff23
KS
4084 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4085 }
ef0e360f 4086 }
d30591dc
JR
4087
4088 SelectObject (hdc, orig_brush);
4089 DeleteObject (fg_brush);
4090 SelectObject (compat_hdc, orig_obj);
4091 DeleteDC (compat_hdc);
791f420f
JR
4092 }
4093 else
4094 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4095 s->img->height - 1);
4096
4097 SelectObject (hdc, orig_hdc_obj);
4098 DeleteDC (hdc);
4099}
4100
4101
4102/* Draw part of the background of glyph string S. X, Y, W, and H
4103 give the rectangle to draw. */
4104
4105static void
4106x_draw_glyph_string_bg_rect (s, x, y, w, h)
4107 struct glyph_string *s;
4108 int x, y, w, h;
4109{
01b220b6 4110#if 0 /* TODO: stipple */
791f420f
JR
4111 if (s->stippled_p)
4112 {
4113 /* Fill background with a stipple pattern. */
4114 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4115 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4116 XSetFillStyle (s->display, s->gc, FillSolid);
4117 }
4118 else
4119#endif
4120 x_clear_glyph_string_rect (s, x, y, w, h);
4121}
4122
4123
d67d1627 4124/* Draw image glyph string S.
791f420f
JR
4125
4126 s->y
4127 s->x +-------------------------
4128 | s->face->box
4129 |
4130 | +-------------------------
d6ff54d5 4131 | | s->img->vmargin
791f420f
JR
4132 | |
4133 | | +-------------------
4134 | | | the image
cabb23bc 4135
791f420f
JR
4136 */
4137
4138static void
4139x_draw_image_glyph_string (s)
4140 struct glyph_string *s;
4141{
4142 int x, y;
60222d69
AI
4143 int box_line_hwidth = abs (s->face->box_line_width);
4144 int box_line_vwidth = max (s->face->box_line_width, 0);
791f420f
JR
4145 int height;
4146 HBITMAP pixmap = 0;
4147
60222d69 4148 height = s->height - 2 * box_line_vwidth;
791f420f
JR
4149
4150 /* Fill background with face under the image. Do it only if row is
4151 taller than image or if image has a clip mask to reduce
4152 flickering. */
4153 s->stippled_p = s->face->stipple != 0;
4154 if (height > s->img->height
d6ff54d5 4155 || s->img->hmargin
c2cc16fa 4156 || s->img->vmargin
791f420f 4157 || s->img->mask
791f420f
JR
4158 || s->img->pixmap == 0
4159 || s->width != s->background_width)
4160 {
60222d69
AI
4161 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4162 x = s->x + box_line_hwidth;
791f420f
JR
4163 else
4164 x = s->x;
d67d1627 4165
60222d69 4166 y = s->y + box_line_vwidth;
d30591dc 4167#if 0 /* TODO: figure out if we need to do this on Windows. */
791f420f 4168 if (s->img->mask)
ee78dc32 4169 {
c2cc16fa
JR
4170 /* Create a pixmap as large as the glyph string. Fill it
4171 with the background color. Copy the image to it, using
4172 its mask. Copy the temporary pixmap to the display. */
791f420f
JR
4173 Screen *screen = FRAME_X_SCREEN (s->f);
4174 int depth = DefaultDepthOfScreen (screen);
4175
4176 /* Create a pixmap as large as the glyph string. */
4177 pixmap = XCreatePixmap (s->display, s->window,
4178 s->background_width,
4179 s->height, depth);
d67d1627 4180
791f420f
JR
4181 /* Don't clip in the following because we're working on the
4182 pixmap. */
4183 XSetClipMask (s->display, s->gc, None);
ee78dc32 4184
791f420f
JR
4185 /* Fill the pixmap with the background color/stipple. */
4186 if (s->stippled_p)
4187 {
4188 /* Fill background with a stipple pattern. */
4189 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4190 XFillRectangle (s->display, pixmap, s->gc,
4191 0, 0, s->background_width, s->height);
4192 XSetFillStyle (s->display, s->gc, FillSolid);
4193 }
ef0e360f 4194 else
791f420f
JR
4195 {
4196 XGCValues xgcv;
4197 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4198 &xgcv);
4199 XSetForeground (s->display, s->gc, xgcv.background);
4200 XFillRectangle (s->display, pixmap, s->gc,
4201 0, 0, s->background_width, s->height);
4202 XSetForeground (s->display, s->gc, xgcv.foreground);
4203 }
ee78dc32 4204 }
791f420f
JR
4205 else
4206#endif
791f420f 4207 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
d67d1627 4208
791f420f
JR
4209 s->background_filled_p = 1;
4210 }
ee78dc32 4211
791f420f
JR
4212 /* Draw the foreground. */
4213 if (pixmap != 0)
4214 {
4215 w32_draw_image_foreground_1 (s, pixmap);
4216 x_set_glyph_string_clipping (s);
ee78dc32 4217 {
791f420f
JR
4218 HDC compat_hdc = CreateCompatibleDC (s->hdc);
4219 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4220 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
4221 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
4222
4223 SetTextColor (s->hdc, s->gc->foreground);
4224 SetBkColor (s->hdc, s->gc->background);
791f420f
JR
4225 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4226 compat_hdc, 0, 0, SRCCOPY);
d30591dc 4227
791f420f
JR
4228 SelectObject (s->hdc, orig_brush);
4229 DeleteObject (fg_brush);
4230 SelectObject (compat_hdc, orig_obj);
4231 DeleteDC (compat_hdc);
4232 }
4233 DeleteObject (pixmap);
4234 pixmap = 0;
4235 }
4236 else
4237 x_draw_image_foreground (s);
ee78dc32 4238
791f420f
JR
4239 /* If we must draw a relief around the image, do it. */
4240 if (s->img->relief
4241 || s->hl == DRAW_IMAGE_RAISED
4242 || s->hl == DRAW_IMAGE_SUNKEN)
4243 x_draw_image_relief (s);
4244}
ee78dc32 4245
cabb23bc 4246
791f420f 4247/* Draw stretch glyph string S. */
cabb23bc 4248
791f420f
JR
4249static void
4250x_draw_stretch_glyph_string (s)
4251 struct glyph_string *s;
4252{
4253 xassert (s->first_glyph->type == STRETCH_GLYPH);
4254 s->stippled_p = s->face->stipple != 0;
65c4903c 4255
791f420f
JR
4256 if (s->hl == DRAW_CURSOR
4257 && !x_stretch_cursor_p)
4258 {
4259 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4260 as wide as the stretch glyph. */
4261 int width = min (CANON_X_UNIT (s->f), s->background_width);
cabb23bc 4262
791f420f
JR
4263 /* Draw cursor. */
4264 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
cabb23bc 4265
791f420f
JR
4266 /* Clear rest using the GC of the original non-cursor face. */
4267 if (width < s->background_width)
4268 {
4269 XGCValues *gc = s->face->gc;
4270 int x = s->x + width, y = s->y;
4271 int w = s->background_width - width, h = s->height;
4272 RECT r;
4273 HDC hdc = s->hdc;
9f5a911b
JR
4274
4275 if (s->row->mouse_face_p
4276 && cursor_in_mouse_face_p (s->w))
4277 {
4278 x_set_mouse_face_gc (s);
4279 gc = s->gc;
4280 }
4281 else
4282 gc = s->face->gc;
d67d1627 4283
791f420f
JR
4284 w32_get_glyph_string_clip_rect (s, &r);
4285 w32_set_clip_rectangle (hdc, &r);
4286
01b220b6 4287#if 0 /* TODO: stipple */
791f420f
JR
4288 if (s->face->stipple)
4289 {
4290 /* Fill background with a stipple pattern. */
4291 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4292 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4293 XSetFillStyle (s->display, gc, FillSolid);
4294 }
4295 else
4296#endif
4297 {
4298 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
4299 }
4300 }
4301 }
9f5a911b 4302 else if (!s->background_filled_p)
791f420f
JR
4303 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4304 s->height);
d67d1627 4305
791f420f
JR
4306 s->background_filled_p = 1;
4307}
cabb23bc 4308
cabb23bc 4309
791f420f
JR
4310/* Draw glyph string S. */
4311
4312static void
4313x_draw_glyph_string (s)
4314 struct glyph_string *s;
4315{
858a55c1
AI
4316 int relief_drawn_p = 0;
4317
791f420f
JR
4318 /* If S draws into the background of its successor, draw the
4319 background of the successor first so that S can draw into it.
4320 This makes S->next use XDrawString instead of XDrawImageString. */
4321 if (s->next && s->right_overhang && !s->for_overlaps_p)
4322 {
4323 xassert (s->next->img == NULL);
4324 x_set_glyph_string_gc (s->next);
4325 x_set_glyph_string_clipping (s->next);
4326 x_draw_glyph_string_background (s->next, 1);
4327 }
4328
4329 /* Set up S->gc, set clipping and draw S. */
4330 x_set_glyph_string_gc (s);
791f420f 4331
858a55c1
AI
4332 /* Draw relief (if any) in advance for char/composition so that the
4333 glyph string can be drawn over it. */
4334 if (!s->for_overlaps_p
4335 && s->face->box != FACE_NO_BOX
4336 && (s->first_glyph->type == CHAR_GLYPH
4337 || s->first_glyph->type == COMPOSITE_GLYPH))
4338
4339 {
9f5a911b 4340 x_set_glyph_string_clipping (s);
858a55c1
AI
4341 x_draw_glyph_string_background (s, 1);
4342 x_draw_glyph_string_box (s);
9f5a911b 4343 x_set_glyph_string_clipping (s);
858a55c1
AI
4344 relief_drawn_p = 1;
4345 }
9f5a911b
JR
4346 else
4347 x_set_glyph_string_clipping (s);
858a55c1 4348
791f420f
JR
4349 switch (s->first_glyph->type)
4350 {
4351 case IMAGE_GLYPH:
4352 x_draw_image_glyph_string (s);
4353 break;
4354
4355 case STRETCH_GLYPH:
4356 x_draw_stretch_glyph_string (s);
4357 break;
4358
4359 case CHAR_GLYPH:
4360 if (s->for_overlaps_p)
4361 s->background_filled_p = 1;
4362 else
4363 x_draw_glyph_string_background (s, 0);
4364 x_draw_glyph_string_foreground (s);
4365 break;
4366
4367 case COMPOSITE_GLYPH:
4368 if (s->for_overlaps_p || s->gidx > 0)
4369 s->background_filled_p = 1;
4370 else
4371 x_draw_glyph_string_background (s, 1);
4372 x_draw_composite_glyph_string_foreground (s);
4373 break;
4374
4375 default:
4376 abort ();
4377 }
4378
4379 if (!s->for_overlaps_p)
4380 {
4381 /* Draw underline. */
9ef2e2cf
JR
4382 if (s->face->underline_p
4383 && (s->font->bdf || !s->font->tm.tmUnderlined))
791f420f
JR
4384 {
4385 unsigned long h = 1;
4386 unsigned long dy = s->height - h;
4387
9f5a911b
JR
4388 /* TODO: Use font information for positioning and thickness
4389 of underline. See OUTLINETEXTMETRIC, and xterm.c. */
791f420f
JR
4390 if (s->face->underline_defaulted_p)
4391 {
4392 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4393 s->y + dy, s->width, 1);
4394 }
4395 else
4396 {
4397 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4398 s->y + dy, s->width, 1);
4399 }
4400 }
4401
4402 /* Draw overline. */
4403 if (s->face->overline_p)
4404 {
4405 unsigned long dy = 0, h = 1;
4406
4407 if (s->face->overline_color_defaulted_p)
4408 {
4409 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4410 s->y + dy, s->width, h);
4411 }
4412 else
4413 {
4414 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4415 s->y + dy, s->width, h);
4416 }
4417 }
4418
4419 /* Draw strike-through. */
9ef2e2cf
JR
4420 if (s->face->strike_through_p
4421 && (s->font->bdf || !s->font->tm.tmStruckOut))
791f420f
JR
4422 {
4423 unsigned long h = 1;
4424 unsigned long dy = (s->height - h) / 2;
4425
4426 if (s->face->strike_through_color_defaulted_p)
4427 {
4428 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
4429 s->width, h);
4430 }
4431 else
4432 {
4433 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4434 s->y + dy, s->width, h);
4435 }
4436 }
4437
4438 /* Draw relief. */
858a55c1 4439 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
791f420f
JR
4440 x_draw_glyph_string_box (s);
4441 }
4442
4443 /* Reset clipping. */
4444 w32_set_clip_rectangle (s->hdc, NULL);
4445}
4446
4447
4448static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4449 struct face **, int));
4450
4451
c2cc16fa 4452/* Fill glyph string S with composition components specified by S->cmp.
d67d1627 4453
791f420f
JR
4454 FACES is an array of faces for all components of this composition.
4455 S->gidx is the index of the first component for S.
4456 OVERLAPS_P non-zero means S should draw the foreground only, and
c2cc16fa 4457 use its physical height for clipping.
791f420f
JR
4458
4459 Value is the index of a component not in S. */
4460
4461static int
4462x_fill_composite_glyph_string (s, faces, overlaps_p)
4463 struct glyph_string *s;
4464 struct face **faces;
4465 int overlaps_p;
4466{
4467 int i;
4468
4469 xassert (s);
4470
4471 s->for_overlaps_p = overlaps_p;
4472
4473 s->face = faces[s->gidx];
4474 s->font = s->face->font;
4475 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4476
4477 /* For all glyphs of this composition, starting at the offset
4478 S->gidx, until we reach the end of the definition or encounter a
4479 glyph that requires the different face, add it to S. */
4480 ++s->nchars;
4481 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4482 ++s->nchars;
4483
4484 /* All glyph strings for the same composition has the same width,
4485 i.e. the width set for the first component of the composition. */
4486
4487 s->width = s->first_glyph->pixel_width;
4488
4489 /* If the specified font could not be loaded, use the frame's
4490 default font, but record the fact that we couldn't load it in
4491 the glyph string so that we can draw rectangles for the
4492 characters of the glyph string. */
4493 if (s->font == NULL)
4494 {
4495 s->font_not_found_p = 1;
4496 s->font = FRAME_FONT (s->f);
4497 }
4498
4499 /* Adjust base line for subscript/superscript text. */
4500 s->ybase += s->first_glyph->voffset;
d67d1627 4501
791f420f
JR
4502 xassert (s->face && s->face->gc);
4503
4504 /* This glyph string must always be drawn with 16-bit functions. */
4505 s->two_byte_p = 1;
4506
4507 return s->gidx + s->nchars;
4508}
4509
4510
c2cc16fa 4511/* Fill glyph string S from a sequence of character glyphs.
d67d1627 4512
791f420f
JR
4513 FACE_ID is the face id of the string. START is the index of the
4514 first glyph to consider, END is the index of the last + 1.
4515 OVERLAPS_P non-zero means S should draw the foreground only, and
c2cc16fa 4516 use its physical height for clipping.
791f420f
JR
4517
4518 Value is the index of the first glyph not in S. */
4519
4520static int
4521x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4522 struct glyph_string *s;
4523 int face_id;
4524 int start, end, overlaps_p;
4525{
4526 struct glyph *glyph, *last;
4527 int voffset;
93ff4395 4528 int glyph_not_available_p;
791f420f
JR
4529
4530 xassert (s->f == XFRAME (s->w->frame));
4531 xassert (s->nchars == 0);
4532 xassert (start >= 0 && end > start);
4533
4534 s->for_overlaps_p = overlaps_p;
791f420f
JR
4535 glyph = s->row->glyphs[s->area] + start;
4536 last = s->row->glyphs[s->area] + end;
4537 voffset = glyph->voffset;
93ff4395
JR
4538
4539 glyph_not_available_p = glyph->glyph_not_available_p;
4540
791f420f
JR
4541 while (glyph < last
4542 && glyph->type == CHAR_GLYPH
4543 && glyph->voffset == voffset
93ff4395
JR
4544 /* Same face id implies same font, nowadays. */
4545 && glyph->face_id == face_id
4546 && glyph->glyph_not_available_p == glyph_not_available_p)
791f420f 4547 {
93ff4395
JR
4548 int two_byte_p;
4549
791f420f 4550 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
93ff4395
JR
4551 s->char2b + s->nchars,
4552 &two_byte_p);
4553 s->two_byte_p = two_byte_p;
791f420f
JR
4554 ++s->nchars;
4555 xassert (s->nchars <= end - start);
4556 s->width += glyph->pixel_width;
4557 ++glyph;
4558 }
4559
4560 s->font = s->face->font;
4561 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
d67d1627 4562
791f420f
JR
4563 /* If the specified font could not be loaded, use the frame's font,
4564 but record the fact that we couldn't load it in
4565 S->font_not_found_p so that we can draw rectangles for the
4566 characters of the glyph string. */
93ff4395 4567 if (s->font == NULL || glyph_not_available_p)
791f420f
JR
4568 {
4569 s->font_not_found_p = 1;
4570 s->font = FRAME_FONT (s->f);
4571 }
4572
4573 /* Adjust base line for subscript/superscript text. */
4574 s->ybase += voffset;
d67d1627 4575
791f420f
JR
4576 xassert (s->face && s->face->gc);
4577 return glyph - s->row->glyphs[s->area];
4578}
4579
4580
4581/* Fill glyph string S from image glyph S->first_glyph. */
4582
4583static void
4584x_fill_image_glyph_string (s)
4585 struct glyph_string *s;
4586{
4587 xassert (s->first_glyph->type == IMAGE_GLYPH);
4588 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4589 xassert (s->img);
4590 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4591 s->font = s->face->font;
4592 s->width = s->first_glyph->pixel_width;
d67d1627 4593
791f420f
JR
4594 /* Adjust base line for subscript/superscript text. */
4595 s->ybase += s->first_glyph->voffset;
4596}
4597
4598
ec48c3a7 4599/* Fill glyph string S from a sequence of stretch glyphs.
791f420f 4600
ec48c3a7
JR
4601 ROW is the glyph row in which the glyphs are found, AREA is the
4602 area within the row. START is the index of the first glyph to
4603 consider, END is the index of the last + 1.
4604
4605 Value is the index of the first glyph not in S. */
4606
4607static int
4608x_fill_stretch_glyph_string (s, row, area, start, end)
791f420f 4609 struct glyph_string *s;
ec48c3a7
JR
4610 struct glyph_row *row;
4611 enum glyph_row_area area;
4612 int start, end;
791f420f 4613{
ec48c3a7
JR
4614 struct glyph *glyph, *last;
4615 int voffset, face_id;
d67d1627 4616
791f420f 4617 xassert (s->first_glyph->type == STRETCH_GLYPH);
d67d1627 4618
ec48c3a7
JR
4619 glyph = s->row->glyphs[s->area] + start;
4620 last = s->row->glyphs[s->area] + end;
4621 face_id = glyph->face_id;
4622 s->face = FACE_FROM_ID (s->f, face_id);
791f420f 4623 s->font = s->face->font;
ec48c3a7
JR
4624 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4625 s->width = glyph->pixel_width;
4626 voffset = glyph->voffset;
4627
4628 for (++glyph;
4629 (glyph < last
4630 && glyph->type == STRETCH_GLYPH
4631 && glyph->voffset == voffset
4632 && glyph->face_id == face_id);
4633 ++glyph)
4634 s->width += glyph->pixel_width;
d67d1627 4635
791f420f 4636 /* Adjust base line for subscript/superscript text. */
ec48c3a7
JR
4637 s->ybase += voffset;
4638
9f5a911b 4639 xassert (s->face);
ec48c3a7 4640 return glyph - s->row->glyphs[s->area];
791f420f
JR
4641}
4642
4643
4644/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4645 of XChar2b structures for S; it can't be allocated in
4646 x_init_glyph_string because it must be allocated via `alloca'. W
4647 is the window on which S is drawn. ROW and AREA are the glyph row
4648 and area within the row from which S is constructed. START is the
4649 index of the first glyph structure covered by S. HL is a
4650 face-override for drawing S. */
d67d1627 4651
791f420f
JR
4652static void
4653w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4654 struct glyph_string *s;
4655 HDC hdc;
4656 wchar_t *char2b;
4657 struct window *w;
4658 struct glyph_row *row;
4659 enum glyph_row_area area;
4660 int start;
4661 enum draw_glyphs_face hl;
4662{
4663 bzero (s, sizeof *s);
4664 s->w = w;
4665 s->f = XFRAME (w->frame);
4666 s->hdc = hdc;
4667 s->window = FRAME_W32_WINDOW (s->f);
4668 s->char2b = char2b;
4669 s->hl = hl;
4670 s->row = row;
4671 s->area = area;
4672 s->first_glyph = row->glyphs[area] + start;
4673 s->height = row->height;
4674 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4675
4676 /* Display the internal border below the tool-bar window. */
4677 if (s->w == XWINDOW (s->f->tool_bar_window))
4678 s->y -= s->f->output_data.w32->internal_border_width;
d67d1627 4679
791f420f
JR
4680 s->ybase = s->y + row->ascent;
4681}
4682
4683
4684/* Set background width of glyph string S. START is the index of the
4685 first glyph following S. LAST_X is the right-most x-position + 1
4686 in the drawing area. */
4687
4688static INLINE void
4689x_set_glyph_string_background_width (s, start, last_x)
4690 struct glyph_string *s;
4691 int start;
4692 int last_x;
4693{
4694 /* If the face of this glyph string has to be drawn to the end of
4695 the drawing area, set S->extends_to_end_of_line_p. */
4696 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
d67d1627 4697
791f420f 4698 if (start == s->row->used[s->area]
5b844253 4699 && s->area == TEXT_AREA
f4fccc1e
EZ
4700 && ((s->hl == DRAW_NORMAL_TEXT
4701 && (s->row->fill_line_p
4702 || s->face->background != default_face->background
4703 || s->face->stipple != default_face->stipple
4704 || s->row->mouse_face_p))
6ff3e5e3
JR
4705 || s->hl == DRAW_MOUSE_FACE
4706 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4707 && s->row->fill_line_p)))
791f420f 4708 s->extends_to_end_of_line_p = 1;
d67d1627 4709
791f420f
JR
4710 /* If S extends its face to the end of the line, set its
4711 background_width to the distance to the right edge of the drawing
4712 area. */
4713 if (s->extends_to_end_of_line_p)
4714 s->background_width = last_x - s->x + 1;
4715 else
4716 s->background_width = s->width;
4717}
4718
4719
4720/* Add a glyph string for a stretch glyph to the list of strings
4721 between HEAD and TAIL. START is the index of the stretch glyph in
4722 row area AREA of glyph row ROW. END is the index of the last glyph
4723 in that glyph row area. X is the current output position assigned
4724 to the new glyph string constructed. HL overrides that face of the
4725 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4726 is the right-most x-position of the drawing area. */
4727
4728#define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4729 do \
4730 { \
4731 s = (struct glyph_string *) alloca (sizeof *s); \
4732 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
ec48c3a7 4733 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
791f420f 4734 x_append_glyph_string (&HEAD, &TAIL, s); \
791f420f
JR
4735 s->x = (X); \
4736 } \
4737 while (0)
4738
4739
4740/* Add a glyph string for an image glyph to the list of strings
4741 between HEAD and TAIL. START is the index of the image glyph in
4742 row area AREA of glyph row ROW. END is the index of the last glyph
4743 in that glyph row area. X is the current output position assigned
4744 to the new glyph string constructed. HL overrides that face of the
4745 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4746 is the right-most x-position of the drawing area. */
4747
4748#define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4749 do \
4750 { \
4751 s = (struct glyph_string *) alloca (sizeof *s); \
4752 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4753 x_fill_image_glyph_string (s); \
4754 x_append_glyph_string (&HEAD, &TAIL, s); \
4755 ++START; \
4756 s->x = (X); \
4757 } \
4758 while (0)
4759
4760
4761/* Add a glyph string for a sequence of character glyphs to the list
4762 of strings between HEAD and TAIL. START is the index of the first
4763 glyph in row area AREA of glyph row ROW that is part of the new
4764 glyph string. END is the index of the last glyph in that glyph row
4765 area. X is the current output position assigned to the new glyph
4766 string constructed. HL overrides that face of the glyph; e.g. it
4767 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4768 right-most x-position of the drawing area. */
4769
4770#define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4771 do \
4772 { \
ec48c3a7 4773 int c, face_id; \
791f420f
JR
4774 wchar_t *char2b; \
4775 \
4776 c = (ROW)->glyphs[AREA][START].u.ch; \
791f420f 4777 face_id = (ROW)->glyphs[AREA][START].face_id; \
9ef2e2cf 4778 \
791f420f
JR
4779 s = (struct glyph_string *) alloca (sizeof *s); \
4780 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4781 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4782 x_append_glyph_string (&HEAD, &TAIL, s); \
791f420f
JR
4783 s->x = (X); \
4784 START = x_fill_glyph_string (s, face_id, START, END, \
4785 OVERLAPS_P); \
4786 } \
4787 while (0)
d67d1627 4788
791f420f
JR
4789
4790/* Add a glyph string for a composite sequence to the list of strings
4791 between HEAD and TAIL. START is the index of the first glyph in
4792 row area AREA of glyph row ROW that is part of the new glyph
4793 string. END is the index of the last glyph in that glyph row area.
4794 X is the current output position assigned to the new glyph string
4795 constructed. HL overrides that face of the glyph; e.g. it is
4796 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4797 x-position of the drawing area. */
4798
4799#define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4800 do { \
4801 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4802 int face_id = (ROW)->glyphs[AREA][START].face_id; \
93ff4395 4803 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
791f420f
JR
4804 struct composition *cmp = composition_table[cmp_id]; \
4805 int glyph_len = cmp->glyph_len; \
4806 wchar_t *char2b; \
4807 struct face **faces; \
4808 struct glyph_string *first_s = NULL; \
4809 int n; \
4810 \
93ff4395 4811 base_face = base_face->ascii_face; \
791f420f
JR
4812 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4813 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4814 /* At first, fill in `char2b' and `faces'. */ \
4815 for (n = 0; n < glyph_len; n++) \
4816 { \
4817 int c = COMPOSITION_GLYPH (cmp, n); \
93ff4395
JR
4818 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4819 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4820 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4821 this_face_id, char2b + n, 1); \
791f420f
JR
4822 } \
4823 \
4824 /* Make glyph_strings for each glyph sequence that is drawable by \
4825 the same face, and append them to HEAD/TAIL. */ \
4826 for (n = 0; n < cmp->glyph_len;) \
4827 { \
4828 s = (struct glyph_string *) alloca (sizeof *s); \
4829 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4830 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4831 s->cmp = cmp; \
4832 s->gidx = n; \
791f420f
JR
4833 s->x = (X); \
4834 \
4835 if (n == 0) \
4836 first_s = s; \
4837 \
4838 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4839 } \
4840 \
4841 ++START; \
4842 s = first_s; \
4843 } while (0)
4844
9ef2e2cf 4845
791f420f
JR
4846/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4847 of AREA of glyph row ROW on window W between indices START and END.
4848 HL overrides the face for drawing glyph strings, e.g. it is
4849 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4850 x-positions of the drawing area.
4851
4852 This is an ugly monster macro construct because we must use alloca
4853 to allocate glyph strings (because x_draw_glyphs can be called
4854 asynchronously). */
4855
4856#define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4857 do \
4858 { \
4859 HEAD = TAIL = NULL; \
4860 while (START < END) \
4861 { \
4862 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4863 switch (first_glyph->type) \
4864 { \
4865 case CHAR_GLYPH: \
4866 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4867 HEAD, TAIL, HL, X, LAST_X, \
4868 OVERLAPS_P); \
4869 break; \
4870 \
4871 case COMPOSITE_GLYPH: \
4872 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4873 END, HEAD, TAIL, HL, X, \
4874 LAST_X, OVERLAPS_P); \
4875 break; \
4876 \
4877 case STRETCH_GLYPH: \
4878 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4879 HEAD, TAIL, HL, X, LAST_X); \
4880 break; \
4881 \
4882 case IMAGE_GLYPH: \
4883 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4884 HEAD, TAIL, HL, X, LAST_X); \
4885 break; \
4886 \
4887 default: \
4888 abort (); \
4889 } \
4890 \
4891 x_set_glyph_string_background_width (s, START, LAST_X); \
4892 (X) += s->width; \
4893 } \
4894 } \
4895 while (0)
4896
4897
4898/* Draw glyphs between START and END in AREA of ROW on window W,
4899 starting at x-position X. X is relative to AREA in W. HL is a
4900 face-override with the following meaning:
4901
4902 DRAW_NORMAL_TEXT draw normally
4903 DRAW_CURSOR draw in cursor face
4904 DRAW_MOUSE_FACE draw in mouse face.
4905 DRAW_INVERSE_VIDEO draw in mode line face
4906 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4907 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4908
791f420f
JR
4909 If OVERLAPS_P is non-zero, draw only the foreground of characters
4910 and clip to the physical height of ROW.
4911
4912 Value is the x-position reached, relative to AREA of W. */
d67d1627 4913
791f420f 4914static int
6ff3e5e3 4915x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
791f420f
JR
4916 struct window *w;
4917 int x;
4918 struct glyph_row *row;
4919 enum glyph_row_area area;
4920 int start, end;
4921 enum draw_glyphs_face hl;
791f420f
JR
4922 int overlaps_p;
4923{
4924 struct glyph_string *head, *tail;
4925 struct glyph_string *s;
4926 int last_x, area_width;
4927 int x_reached;
4928 int i, j;
4929 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4930
4931 /* Let's rather be paranoid than getting a SEGV. */
791f420f 4932 end = min (end, row->used[area]);
9127e20e
JR
4933 start = max (0, start);
4934 start = min (end, start);
791f420f
JR
4935
4936 /* Translate X to frame coordinates. Set last_x to the right
4937 end of the drawing area. */
4938 if (row->full_width_p)
4939 {
791f420f 4940 /* X is relative to the left edge of W, without scroll bars
33c34bea 4941 or fringes. */
ec48c3a7 4942 struct frame *f = XFRAME (WINDOW_FRAME (w));
791f420f 4943 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
ee78dc32 4944
791f420f
JR
4945 x += window_left_x;
4946 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4947 last_x = window_left_x + area_width;
ee78dc32 4948
791f420f
JR
4949 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4950 {
4951 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4952 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4953 last_x += width;
4954 else
4955 x -= width;
4956 }
ee78dc32 4957
791f420f
JR
4958 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4959 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4960 }
4961 else
4962 {
4963 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4964 area_width = window_box_width (w, area);
4965 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4966 }
e12ca9c2 4967
791f420f
JR
4968 /* Build a doubly-linked list of glyph_string structures between
4969 head and tail from what we have to draw. Note that the macro
4970 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4971 the reason we use a separate variable `i'. */
4972 i = start;
4973 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4974 overlaps_p);
791f420f
JR
4975 if (tail)
4976 x_reached = tail->x + tail->background_width;
4977 else
4978 x_reached = x;
e12ca9c2 4979
791f420f
JR
4980 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4981 the row, redraw some glyphs in front or following the glyph
4982 strings built above. */
9127e20e 4983 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
791f420f
JR
4984 {
4985 int dummy_x = 0;
4986 struct glyph_string *h, *t;
4987
4988 /* Compute overhangs for all glyph strings. */
4989 for (s = head; s; s = s->next)
4990 x_compute_glyph_string_overhangs (s);
4991
4992 /* Prepend glyph strings for glyphs in front of the first glyph
4993 string that are overwritten because of the first glyph
4994 string's left overhang. The background of all strings
d67d1627 4995 prepended must be drawn because the first glyph string
791f420f
JR
4996 draws over it. */
4997 i = x_left_overwritten (head);
4998 if (i >= 0)
4999 {
5000 j = i;
5001 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
5002 DRAW_NORMAL_TEXT, dummy_x, last_x,
5003 overlaps_p);
5004 start = i;
791f420f
JR
5005 x_compute_overhangs_and_x (t, head->x, 1);
5006 x_prepend_glyph_string_lists (&head, &tail, h, t);
5007 }
ee78dc32 5008
791f420f
JR
5009 /* Prepend glyph strings for glyphs in front of the first glyph
5010 string that overwrite that glyph string because of their
5011 right overhang. For these strings, only the foreground must
5012 be drawn, because it draws over the glyph string at `head'.
5013 The background must not be drawn because this would overwrite
5014 right overhangs of preceding glyphs for which no glyph
5015 strings exist. */
5016 i = x_left_overwriting (head);
5017 if (i >= 0)
5018 {
5019 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
5020 DRAW_NORMAL_TEXT, dummy_x, last_x,
5021 overlaps_p);
791f420f
JR
5022 for (s = h; s; s = s->next)
5023 s->background_filled_p = 1;
791f420f
JR
5024 x_compute_overhangs_and_x (t, head->x, 1);
5025 x_prepend_glyph_string_lists (&head, &tail, h, t);
5026 }
cabb23bc 5027
791f420f
JR
5028 /* Append glyphs strings for glyphs following the last glyph
5029 string tail that are overwritten by tail. The background of
5030 these strings has to be drawn because tail's foreground draws
5031 over it. */
5032 i = x_right_overwritten (tail);
5033 if (i >= 0)
5034 {
5035 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5036 DRAW_NORMAL_TEXT, x, last_x,
5037 overlaps_p);
791f420f
JR
5038 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5039 x_append_glyph_string_lists (&head, &tail, h, t);
791f420f 5040 }
cabb23bc 5041
791f420f
JR
5042 /* Append glyph strings for glyphs following the last glyph
5043 string tail that overwrite tail. The foreground of such
5044 glyphs has to be drawn because it writes into the background
5045 of tail. The background must not be drawn because it could
5046 paint over the foreground of following glyphs. */
5047 i = x_right_overwriting (tail);
5048 if (i >= 0)
5049 {
5050 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5051 DRAW_NORMAL_TEXT, x, last_x,
5052 overlaps_p);
791f420f
JR
5053 for (s = h; s; s = s->next)
5054 s->background_filled_p = 1;
5055 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5056 x_append_glyph_string_lists (&head, &tail, h, t);
791f420f
JR
5057 }
5058 }
cabb23bc 5059
791f420f
JR
5060 /* Draw all strings. */
5061 for (s = head; s; s = s->next)
5062 x_draw_glyph_string (s);
cabb23bc 5063
30c6d055
JR
5064 if (area == TEXT_AREA
5065 && !row->full_width_p
5066 /* When drawing overlapping rows, only the glyph strings'
5067 foreground is drawn, which doesn't erase a cursor
5068 completely. */
5069 && !overlaps_p)
6ff3e5e3
JR
5070 {
5071 int x0 = head ? head->x : x;
5072 int x1 = tail ? tail->x + tail->background_width : x;
d67d1627 5073
6ff3e5e3
JR
5074 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5075 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
d67d1627 5076
6ff3e5e3
JR
5077 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
5078 {
5079 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5080 x0 -= left_area_width;
5081 x1 -= left_area_width;
5082 }
5083
6637c996
JR
5084 notice_overwritten_cursor (w, area, x0, x1,
5085 row->y, MATRIX_ROW_BOTTOM_Y (row));
6ff3e5e3
JR
5086 }
5087
791f420f
JR
5088 /* Value is the x-position up to which drawn, relative to AREA of W.
5089 This doesn't include parts drawn because of overhangs. */
5090 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5091 if (!row->full_width_p)
5092 {
5093 if (area > LEFT_MARGIN_AREA)
5094 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5095 if (area > TEXT_AREA)
5096 x_reached -= window_box_width (w, TEXT_AREA);
5097 }
cabb23bc 5098
791f420f 5099 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
65c4903c 5100
791f420f
JR
5101 return x_reached;
5102}
65c4903c 5103
cabb23bc 5104
791f420f
JR
5105/* Fix the display of area AREA of overlapping row ROW in window W. */
5106
5107static void
5108x_fix_overlapping_area (w, row, area)
5109 struct window *w;
5110 struct glyph_row *row;
5111 enum glyph_row_area area;
5112{
5113 int i, x;
d67d1627 5114
791f420f 5115 BLOCK_INPUT;
d67d1627 5116
791f420f
JR
5117 if (area == LEFT_MARGIN_AREA)
5118 x = 0;
5119 else if (area == TEXT_AREA)
5120 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5121 else
5122 x = (window_box_width (w, LEFT_MARGIN_AREA)
5123 + window_box_width (w, TEXT_AREA));
b1ae662f 5124
791f420f
JR
5125 for (i = 0; i < row->used[area];)
5126 {
5127 if (row->glyphs[area][i].overlaps_vertically_p)
ee78dc32 5128 {
791f420f 5129 int start = i, start_x = x;
ee78dc32 5130
791f420f
JR
5131 do
5132 {
5133 x += row->glyphs[area][i].pixel_width;
5134 ++i;
5135 }
5136 while (i < row->used[area]
5137 && row->glyphs[area][i].overlaps_vertically_p);
cabb23bc 5138
791f420f 5139 x_draw_glyphs (w, start_x, row, area, start, i,
6ff3e5e3 5140 DRAW_NORMAL_TEXT, 1);
791f420f
JR
5141 }
5142 else
5143 {
5144 x += row->glyphs[area][i].pixel_width;
5145 ++i;
5146 }
5147 }
d67d1627 5148
791f420f 5149 UNBLOCK_INPUT;
ee78dc32
GV
5150}
5151
ee78dc32 5152
791f420f
JR
5153/* Output LEN glyphs starting at START at the nominal cursor position.
5154 Advance the nominal cursor over the text. The global variable
5155 updated_window contains the window being updated, updated_row is
5156 the glyph row being updated, and updated_area is the area of that
5157 row being updated. */
ee78dc32 5158
96214669 5159static void
791f420f
JR
5160x_write_glyphs (start, len)
5161 struct glyph *start;
ee78dc32
GV
5162 int len;
5163{
6ff3e5e3 5164 int x, hpos;
ee78dc32 5165
791f420f 5166 xassert (updated_window && updated_row);
ee78dc32 5167 BLOCK_INPUT;
d67d1627 5168
791f420f 5169 /* Write glyphs. */
ee78dc32 5170
791f420f
JR
5171 hpos = start - updated_row->glyphs[updated_area];
5172 x = x_draw_glyphs (updated_window, output_cursor.x,
5173 updated_row, updated_area,
5174 hpos, hpos + len,
6ff3e5e3 5175 DRAW_NORMAL_TEXT, 0);
791f420f
JR
5176
5177 UNBLOCK_INPUT;
d67d1627 5178
791f420f
JR
5179 /* Advance the output cursor. */
5180 output_cursor.hpos += len;
5181 output_cursor.x = x;
5182}
ee78dc32 5183
ee78dc32 5184
791f420f 5185/* Insert LEN glyphs from START at the nominal cursor position. */
ee78dc32 5186
791f420f
JR
5187static void
5188x_insert_glyphs (start, len)
5189 struct glyph *start;
5190 register int len;
5191{
5192 struct frame *f;
5193 struct window *w;
5194 int line_height, shift_by_width, shifted_region_width;
5195 struct glyph_row *row;
5196 struct glyph *glyph;
6ff3e5e3 5197 int frame_x, frame_y, hpos;
791f420f
JR
5198 HDC hdc;
5199
5200 xassert (updated_window && updated_row);
5201 BLOCK_INPUT;
5202 w = updated_window;
5203 f = XFRAME (WINDOW_FRAME (w));
5204 hdc = get_frame_dc (f);
5205
5206 /* Get the height of the line we are in. */
5207 row = updated_row;
5208 line_height = row->height;
5209
5210 /* Get the width of the glyphs to insert. */
5211 shift_by_width = 0;
5212 for (glyph = start; glyph < start + len; ++glyph)
5213 shift_by_width += glyph->pixel_width;
5214
5215 /* Get the width of the region to shift right. */
5216 shifted_region_width = (window_box_width (w, updated_area)
5217 - output_cursor.x
5218 - shift_by_width);
5219
5220 /* Shift right. */
5b844253 5221 frame_x = window_box_left (w, updated_area) + output_cursor.x;
791f420f
JR
5222 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5223 BitBlt (hdc, frame_x + shift_by_width, frame_y,
5224 shifted_region_width, line_height,
5225 hdc, frame_x, frame_y, SRCCOPY);
5226
5227 /* Write the glyphs. */
5228 hpos = start - row->glyphs[updated_area];
5229 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
6ff3e5e3
JR
5230 DRAW_NORMAL_TEXT, 0);
5231
791f420f
JR
5232 /* Advance the output cursor. */
5233 output_cursor.hpos += len;
5234 output_cursor.x += shift_by_width;
5235 release_frame_dc (f, hdc);
ee78dc32
GV
5236
5237 UNBLOCK_INPUT;
5238}
791f420f
JR
5239
5240
5241/* Delete N glyphs at the nominal cursor position. Not implemented
5242 for X frames. */
ee78dc32 5243
96214669 5244static void
791f420f
JR
5245x_delete_glyphs (n)
5246 register int n;
ee78dc32 5247{
7e6ac5b9
AI
5248 struct frame *f;
5249
5250 if (updating_frame)
5251 f = updating_frame;
5252 else
5253 f = SELECTED_FRAME ();
5254
5255 if (! FRAME_W32_P (f))
5256 return;
5257
791f420f
JR
5258 abort ();
5259}
ee78dc32 5260
ee78dc32 5261
791f420f
JR
5262/* Erase the current text line from the nominal cursor position
5263 (inclusive) to pixel column TO_X (exclusive). The idea is that
5264 everything from TO_X onward is already erased.
ee78dc32 5265
791f420f
JR
5266 TO_X is a pixel position relative to updated_area of
5267 updated_window. TO_X == -1 means clear to the end of this area. */
11606873 5268
791f420f
JR
5269static void
5270x_clear_end_of_line (to_x)
5271 int to_x;
5272{
5273 struct frame *f;
5274 struct window *w = updated_window;
5275 int max_x, min_y, max_y;
5276 int from_x, from_y, to_y;
d67d1627 5277
791f420f
JR
5278 xassert (updated_window && updated_row);
5279 f = XFRAME (w->frame);
d67d1627 5280
791f420f
JR
5281 if (updated_row->full_width_p)
5282 {
5283 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5284 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5285 && !w->pseudo_window_p)
5286 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5287 }
5288 else
5289 max_x = window_box_width (w, updated_area);
5290 max_y = window_text_bottom_y (w);
ee78dc32 5291
791f420f
JR
5292 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5293 of window. For TO_X > 0, truncate to end of drawing area. */
5294 if (to_x == 0)
5295 return;
5296 else if (to_x < 0)
5297 to_x = max_x;
5298 else
5299 to_x = min (to_x, max_x);
ee78dc32 5300
791f420f 5301 to_y = min (max_y, output_cursor.y + updated_row->height);
d67d1627 5302
ee78dc32 5303 /* Notice if the cursor will be cleared by this operation. */
791f420f 5304 if (!updated_row->full_width_p)
6637c996
JR
5305 notice_overwritten_cursor (w, updated_area,
5306 output_cursor.x, -1,
5307 updated_row->y,
5308 MATRIX_ROW_BOTTOM_Y (updated_row));
ee78dc32 5309
791f420f 5310 from_x = output_cursor.x;
6637c996 5311
791f420f
JR
5312 /* Translate to frame coordinates. */
5313 if (updated_row->full_width_p)
5314 {
5315 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5316 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5317 }
5318 else
5319 {
5320 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5321 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5322 }
d67d1627 5323
791f420f
JR
5324 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5325 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5326 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
d67d1627 5327
791f420f
JR
5328 /* Prevent inadvertently clearing to end of the X window. */
5329 if (to_x > from_x && to_y > from_y)
5330 {
00fe468b 5331 HDC hdc;
791f420f 5332 BLOCK_INPUT;
00fe468b
JR
5333 hdc = get_frame_dc (f);
5334
5335 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
5336 release_frame_dc (f, hdc);
791f420f
JR
5337 UNBLOCK_INPUT;
5338 }
ee78dc32
GV
5339}
5340
791f420f
JR
5341
5342/* Clear entire frame. If updating_frame is non-null, clear that
5343 frame. Otherwise clear the selected frame. */
5344
96214669 5345static void
791f420f 5346x_clear_frame ()
ee78dc32 5347{
791f420f 5348 struct frame *f;
ee78dc32 5349
791f420f
JR
5350 if (updating_frame)
5351 f = updating_frame;
5352 else
5353 f = SELECTED_FRAME ();
ee78dc32 5354
7e6ac5b9
AI
5355 if (! FRAME_W32_P (f))
5356 return;
5357
791f420f
JR
5358 /* Clearing the frame will erase any cursor, so mark them all as no
5359 longer visible. */
5360 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5361 output_cursor.hpos = output_cursor.vpos = 0;
5362 output_cursor.x = -1;
ee78dc32 5363
791f420f
JR
5364 /* We don't set the output cursor here because there will always
5365 follow an explicit cursor_to. */
ee78dc32
GV
5366 BLOCK_INPUT;
5367
fbd6baed 5368 w32_clear_window (f);
ee78dc32
GV
5369
5370 /* We have to clear the scroll bars, too. If we have changed
5371 colors or something like that, then they should be notified. */
5372 x_scroll_bar_clear (f);
5373
5374 UNBLOCK_INPUT;
5375}
791f420f 5376
ee78dc32
GV
5377\f
5378/* Make audible bell. */
5379
96214669
GV
5380static void
5381w32_ring_bell (void)
ee78dc32 5382{
7e6ac5b9
AI
5383 struct frame *f;
5384
5385 f = SELECTED_FRAME ();
5386
ee78dc32
GV
5387 BLOCK_INPUT;
5388
0f32f023 5389 if (FRAME_W32_P (f) && visible_bell)
8331e676
GV
5390 {
5391 int i;
791f420f 5392 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
8331e676 5393
d67d1627 5394 for (i = 0; i < 5; i++)
8331e676
GV
5395 {
5396 FlashWindow (hwnd, TRUE);
5397 Sleep (10);
5398 }
5399 FlashWindow (hwnd, FALSE);
5400 }
ee78dc32 5401 else
fbd6baed 5402 w32_sys_ring_bell ();
ee78dc32
GV
5403
5404 UNBLOCK_INPUT;
ee78dc32 5405}
791f420f 5406
ee78dc32 5407\f
791f420f
JR
5408/* Specify how many text lines, from the top of the window,
5409 should be affected by insert-lines and delete-lines operations.
5410 This, and those operations, are used only within an update
5411 that is bounded by calls to x_update_begin and x_update_end. */
ee78dc32 5412
96214669 5413static void
791f420f
JR
5414w32_set_terminal_window (n)
5415 register int n;
ee78dc32 5416{
791f420f 5417 /* This function intentionally left blank. */
ee78dc32 5418}
791f420f
JR
5419\f
5420
5421\f
5422/***********************************************************************
5423 Line Dance
5424 ***********************************************************************/
5425
5426/* Perform an insert-lines or delete-lines operation, inserting N
5427 lines or deleting -N lines at vertical position VPOS. */
ee78dc32 5428
96214669 5429static void
791f420f
JR
5430x_ins_del_lines (vpos, n)
5431 int vpos, n;
ee78dc32 5432{
7e6ac5b9
AI
5433 struct frame *f;
5434
5435 if (updating_frame)
5436 f = updating_frame;
5437 else
5438 f = SELECTED_FRAME ();
5439
5440 if (! FRAME_W32_P (f))
5441 return;
5442
ee78dc32
GV
5443 abort ();
5444}
791f420f
JR
5445
5446
5447/* Scroll part of the display as described by RUN. */
5448
5449static void
5450x_scroll_run (w, run)
5451 struct window *w;
5452 struct run *run;
5453{
5454 struct frame *f = XFRAME (w->frame);
5455 int x, y, width, height, from_y, to_y, bottom_y;
4d6e8199
JR
5456 HWND hwnd = FRAME_W32_WINDOW (f);
5457 HRGN expect_dirty;
791f420f
JR
5458
5459 /* Get frame-relative bounding box of the text display area of W,
33c34bea
KS
5460 without mode lines. Include in this box the left and right
5461 fringes of W. */
791f420f 5462 window_box (w, -1, &x, &y, &width, &height);
33c34bea
KS
5463 width += FRAME_X_FRINGE_WIDTH (f);
5464 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
791f420f
JR
5465
5466 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5467 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5468 bottom_y = y + height;
5469
5470 if (to_y < from_y)
5471 {
5472 /* Scrolling up. Make sure we don't copy part of the mode
5473 line at the bottom. */
5474 if (from_y + run->height > bottom_y)
5475 height = bottom_y - from_y;
5476 else
5477 height = run->height;
4d6e8199 5478 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
791f420f
JR
5479 }
5480 else
5481 {
5482 /* Scolling down. Make sure we don't copy over the mode line.
5483 at the bottom. */
5484 if (to_y + run->height > bottom_y)
5485 height = bottom_y - to_y;
5486 else
5487 height = run->height;
4d6e8199 5488 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
791f420f
JR
5489 }
5490
5491 BLOCK_INPUT;
d67d1627 5492
791f420f
JR
5493 /* Cursor off. Will be switched on again in x_update_window_end. */
5494 updated_window = w;
5495 x_clear_cursor (w);
5496
4d6e8199
JR
5497 {
5498 RECT from;
5499 RECT to;
5500 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
5501 HRGN combined = CreateRectRgn (0, 0, 0, 0);
5502
5503 from.left = to.left = x;
5504 from.right = to.right = x + width;
5505 from.top = from_y;
5506 from.bottom = from_y + height;
5507 to.top = y;
5508 to.bottom = bottom_y;
5509
5510 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
5511 NULL, SW_INVALIDATE);
5512
5513 /* Combine this with what we expect to be dirty. This covers the
5514 case where not all of the region we expect is actually dirty. */
5515 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
5516
5517 /* If the dirty region is not what we expected, redraw the entire frame. */
5518 if (!EqualRgn (combined, expect_dirty))
5519 SET_FRAME_GARBAGED (f);
5520 }
5521
791f420f 5522 UNBLOCK_INPUT;
791f420f
JR
5523}
5524
9ef2e2cf 5525
ee78dc32 5526\f
791f420f
JR
5527/***********************************************************************
5528 Exposure Events
5529 ***********************************************************************/
d67d1627 5530
791f420f
JR
5531/* Redisplay an exposed area of frame F. X and Y are the upper-left
5532 corner of the exposed rectangle. W and H are width and height of
5533 the exposed area. All are pixel values. W or H zero means redraw
5534 the entire frame. */
ee78dc32 5535
96214669 5536static void
791f420f
JR
5537expose_frame (f, x, y, w, h)
5538 struct frame *f;
5539 int x, y, w, h;
ee78dc32 5540{
791f420f 5541 RECT r;
9f5a911b 5542 int mouse_face_overwritten_p = 0;
791f420f
JR
5543
5544 TRACE ((stderr, "expose_frame "));
5545
5546 /* No need to redraw if frame will be redrawn soon. */
5547 if (FRAME_GARBAGED_P (f))
5548 {
5549 TRACE ((stderr, " garbaged\n"));
5550 return;
5551 }
5552
5553 /* If basic faces haven't been realized yet, there is no point in
5554 trying to redraw anything. This can happen when we get an expose
5555 event while Emacs is starting, e.g. by moving another window. */
5556 if (FRAME_FACE_CACHE (f) == NULL
5557 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5558 {
5559 TRACE ((stderr, " no faces\n"));
5560 return;
5561 }
5562
5563 if (w == 0 || h == 0)
5564 {
5565 r.left = r.top = 0;
5566 r.right = CANON_X_UNIT (f) * f->width;
5567 r.bottom = CANON_Y_UNIT (f) * f->height;
5568 }
5569 else
5570 {
5571 r.left = x;
5572 r.top = y;
5573 r.right = x + w;
5574 r.bottom = y + h;
5575 }
5576
5577 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
9f5a911b 5578 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
791f420f
JR
5579
5580 if (WINDOWP (f->tool_bar_window))
9f5a911b
JR
5581 mouse_face_overwritten_p
5582 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5583
5584 /* Some window managers support a focus-follows-mouse style with
5585 delayed raising of frames. Imagine a partially obscured frame,
5586 and moving the mouse into partially obscured mouse-face on that
5587 frame. The visible part of the mouse-face will be highlighted,
5588 then the WM raises the obscured frame. With at least one WM, KDE
5589 2.1, Emacs is not getting any event for the raising of the frame
5590 (even tried with SubstructureRedirectMask), only Expose events.
5591 These expose events will draw text normally, i.e. not
5592 highlighted. Which means we must redo the highlight here.
5593 Subsume it under ``we love X''. --gerd 2001-08-15 */
5594 /* Included in Windows version because Windows most likely does not
5595 do the right thing if any third party tool offers
5596 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
5597 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
791f420f 5598 {
9f5a911b
JR
5599 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5600 if (f == dpyinfo->mouse_face_mouse_frame)
5601 {
5602 int x = dpyinfo->mouse_face_mouse_x;
5603 int y = dpyinfo->mouse_face_mouse_y;
5604 clear_mouse_face (dpyinfo);
5605 note_mouse_highlight (f, x, y);
5606 }
791f420f
JR
5607 }
5608}
5609
5610
5611/* Redraw (parts) of all windows in the window tree rooted at W that
5612 intersect R. R contains frame pixel coordinates. */
5613
9f5a911b 5614static int
791f420f
JR
5615expose_window_tree (w, r)
5616 struct window *w;
5617 RECT *r;
5618{
9f5a911b
JR
5619 struct frame *f = XFRAME (w->frame);
5620 int mouse_face_overwritten_p = 0;
5621
5622 while (w && !FRAME_GARBAGED_P (f))
791f420f
JR
5623 {
5624 if (!NILP (w->hchild))
9f5a911b
JR
5625 mouse_face_overwritten_p
5626 |= expose_window_tree (XWINDOW (w->hchild), r);
791f420f 5627 else if (!NILP (w->vchild))
9f5a911b
JR
5628 mouse_face_overwritten_p
5629 |= expose_window_tree (XWINDOW (w->vchild), r);
791f420f 5630 else
9f5a911b 5631 mouse_face_overwritten_p |= expose_window (w, r);
791f420f 5632
9f5a911b 5633 w = NILP (w->next) ? NULL : XWINDOW (w->next);
791f420f 5634 }
9f5a911b
JR
5635
5636 return mouse_face_overwritten_p;
791f420f
JR
5637}
5638
5639
5640/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5641 which intersects rectangle R. R is in window-relative coordinates. */
5642
5643static void
5644expose_area (w, row, r, area)
5645 struct window *w;
5646 struct glyph_row *row;
5647 RECT *r;
5648 enum glyph_row_area area;
5649{
791f420f
JR
5650 struct glyph *first = row->glyphs[area];
5651 struct glyph *end = row->glyphs[area] + row->used[area];
5652 struct glyph *last;
2df85294 5653 int first_x, start_x, x;
791f420f 5654
791f420f
JR
5655 if (area == TEXT_AREA && row->fill_line_p)
5656 /* If row extends face to end of line write the whole line. */
c2cc16fa 5657 x_draw_glyphs (w, 0, row, area,
791f420f 5658 0, row->used[area],
6ff3e5e3 5659 DRAW_NORMAL_TEXT, 0);
791f420f
JR
5660 else
5661 {
2df85294
JR
5662 /* Set START_X to the window-relative start position for drawing glyphs of
5663 AREA. The first glyph of the text area can be partially visible.
5664 The first glyphs of other areas cannot. */
5665 if (area == LEFT_MARGIN_AREA)
5666 start_x = 0;
5667 else if (area == TEXT_AREA)
5668 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5669 else
5670 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5671 + window_box_width (w, TEXT_AREA));
5672 x = start_x;
5673
791f420f
JR
5674 /* Find the first glyph that must be redrawn. */
5675 while (first < end
5676 && x + first->pixel_width < r->left)
5677 {
5678 x += first->pixel_width;
5679 ++first;
5680 }
d67d1627 5681
791f420f
JR
5682 /* Find the last one. */
5683 last = first;
5684 first_x = x;
5685 while (last < end
5686 && x < r->right)
5687 {
5688 x += last->pixel_width;
5689 ++last;
5690 }
2df85294 5691
791f420f
JR
5692 /* Repaint. */
5693 if (last > first)
f201d732 5694 x_draw_glyphs (w, first_x - start_x, row, area,
791f420f
JR
5695 first - row->glyphs[area],
5696 last - row->glyphs[area],
6ff3e5e3 5697 DRAW_NORMAL_TEXT, 0);
791f420f
JR
5698 }
5699}
5700
5701
5702/* Redraw the parts of the glyph row ROW on window W intersecting
9f5a911b
JR
5703 rectangle R. R is in window-relative coordinates. Value is
5704 non-zero if mouse face was overwritten. */
ee78dc32 5705
9f5a911b 5706static int
791f420f
JR
5707expose_line (w, row, r)
5708 struct window *w;
5709 struct glyph_row *row;
5710 RECT *r;
5711{
5712 xassert (row->enabled_p);
d67d1627 5713
791f420f
JR
5714 if (row->mode_line_p || w->pseudo_window_p)
5715 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
6ff3e5e3 5716 DRAW_NORMAL_TEXT, 0);
ee78dc32 5717 else
791f420f
JR
5718 {
5719 if (row->used[LEFT_MARGIN_AREA])
5720 expose_area (w, row, r, LEFT_MARGIN_AREA);
5721 if (row->used[TEXT_AREA])
5722 expose_area (w, row, r, TEXT_AREA);
5723 if (row->used[RIGHT_MARGIN_AREA])
5724 expose_area (w, row, r, RIGHT_MARGIN_AREA);
33c34bea 5725 x_draw_row_fringe_bitmaps (w, row);
791f420f 5726 }
9f5a911b
JR
5727
5728 return row->mouse_face_p;
ee78dc32 5729}
ee78dc32 5730
ee78dc32 5731
791f420f 5732/* Return non-zero if W's cursor intersects rectangle R. */
96214669 5733
791f420f
JR
5734static int
5735x_phys_cursor_in_rect_p (w, r)
5736 struct window *w;
5737 RECT *r;
ee78dc32 5738{
791f420f
JR
5739 RECT cr, result;
5740 struct glyph *cursor_glyph;
ee78dc32 5741
791f420f
JR
5742 cursor_glyph = get_phys_cursor_glyph (w);
5743 if (cursor_glyph)
ee78dc32 5744 {
791f420f
JR
5745 cr.left = w->phys_cursor.x;
5746 cr.top = w->phys_cursor.y;
5747 cr.right = cr.left + cursor_glyph->pixel_width;
5748 cr.bottom = cr.top + w->phys_cursor_height;
158cba56 5749 return IntersectRect (&result, &cr, r);
ee78dc32
GV
5750 }
5751 else
791f420f 5752 return 0;
ee78dc32
GV
5753}
5754
791f420f 5755
95f2448c
RS
5756/* Redraw those parts of glyphs rows during expose event handling that
5757 overlap other rows. Redrawing of an exposed line writes over parts
5758 of lines overlapping that exposed line; this function fixes that.
5759
5760 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
5761 row in W's current matrix that is exposed and overlaps other rows.
5762 LAST_OVERLAPPING_ROW is the last such row. */
5763
5764static void
5765expose_overlaps (w, first_overlapping_row, last_overlapping_row)
5766 struct window *w;
5767 struct glyph_row *first_overlapping_row;
5768 struct glyph_row *last_overlapping_row;
5769{
5770 struct glyph_row *row;
d67d1627 5771
95f2448c
RS
5772 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
5773 if (row->overlapping_p)
5774 {
5775 xassert (row->enabled_p && !row->mode_line_p);
d67d1627 5776
95f2448c
RS
5777 if (row->used[LEFT_MARGIN_AREA])
5778 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
d67d1627 5779
95f2448c
RS
5780 if (row->used[TEXT_AREA])
5781 x_fix_overlapping_area (w, row, TEXT_AREA);
d67d1627 5782
95f2448c
RS
5783 if (row->used[RIGHT_MARGIN_AREA])
5784 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
5785 }
5786}
5787
5788
9f5a911b
JR
5789/* Redraw the part of window W intersection rectagle FR. Pixel
5790 coordinates in FR are frame relative. Call this function with
5791 input blocked. Value is non-zero if the exposure overwrites
5792 mouse-face. */
791f420f 5793
9f5a911b
JR
5794static int
5795expose_window (w, fr)
791f420f 5796 struct window *w;
9f5a911b 5797 RECT *fr;
ee78dc32 5798{
9f5a911b
JR
5799 struct frame *f = XFRAME (w->frame);
5800 RECT wr, r;
5801 int mouse_face_overwritten_p = 0;
791f420f
JR
5802
5803 /* If window is not yet fully initialized, do nothing. This can
5804 happen when toolkit scroll bars are used and a window is split.
5805 Reconfiguring the scroll bar will generate an expose for a newly
5806 created window. */
9f5a911b
JR
5807 if (w->current_matrix == NULL)
5808 return 0;
ee78dc32 5809
9f5a911b
JR
5810 /* When we're currently updating the window, display and current
5811 matrix usually don't agree. Arrange for a thorough display
5812 later. */
5813 if (w == updated_window)
791f420f 5814 {
9f5a911b
JR
5815 SET_FRAME_GARBAGED (f);
5816 return 0;
791f420f 5817 }
791f420f 5818
9f5a911b
JR
5819 /* Frame-relative pixel rectangle of W. */
5820 wr.left = XFASTINT (w->left) * CANON_X_UNIT (f);
5821 wr.top = XFASTINT (w->top) * CANON_Y_UNIT (f);
5822 wr.right = wr.left + XFASTINT (w->width) * CANON_X_UNIT (f);
5823 wr.bottom = wr.top + XFASTINT (w->height) * CANON_Y_UNIT (f);
5824
5825 if (IntersectRect(&r, fr, &wr))
791f420f 5826 {
9f5a911b
JR
5827 int yb = window_text_bottom_y (w);
5828 struct glyph_row *row;
5829 int cursor_cleared_p;
95f2448c 5830 struct glyph_row *first_overlapping_row, *last_overlapping_row;
ee78dc32 5831
9f5a911b
JR
5832 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5833 r.left, r.top, r.right, r.bottom));
689004fa 5834
9f5a911b
JR
5835 /* Convert to window coordinates. */
5836 r.left = FRAME_TO_WINDOW_PIXEL_X (w, r.left);
5837 r.right = FRAME_TO_WINDOW_PIXEL_X (w, r.right);
5838 r.top = FRAME_TO_WINDOW_PIXEL_Y (w, r.top);
5839 r.bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r.bottom);
5840
5841 /* Turn off the cursor. */
5842 if (!w->pseudo_window_p
5843 && x_phys_cursor_in_rect_p (w, &r))
5844 {
5845 x_clear_cursor (w);
5846 cursor_cleared_p = 1;
5847 }
5848 else
5849 cursor_cleared_p = 0;
5850
95f2448c
RS
5851 /* Update lines intersecting rectangle R. */
5852 first_overlapping_row = last_overlapping_row = NULL;
9f5a911b
JR
5853 for (row = w->current_matrix->rows;
5854 row->enabled_p;
5855 ++row)
5856 {
5857 int y0 = row->y;
5858 int y1 = MATRIX_ROW_BOTTOM_Y (row);
5859
5860 if ((y0 >= r.top && y0 < r.bottom)
5861 || (y1 > r.top && y1 < r.bottom)
5862 || (r.top >= y0 && r.top < y1)
5863 || (r.bottom > y0 && r.bottom < y1))
5864 {
95f2448c
RS
5865 if (row->overlapping_p)
5866 {
5867 if (first_overlapping_row == NULL)
5868 first_overlapping_row = row;
5869 last_overlapping_row = row;
5870 }
d67d1627 5871
9f5a911b
JR
5872 if (expose_line (w, row, &r))
5873 mouse_face_overwritten_p = 1;
5874 }
5875
5876 if (y1 >= yb)
5877 break;
5878 }
5879
5880 /* Display the mode line if there is one. */
5881 if (WINDOW_WANTS_MODELINE_P (w)
5882 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5883 row->enabled_p)
5884 && row->y < r.bottom)
5885 {
5886 if (expose_line (w, row, &r))
5887 mouse_face_overwritten_p = 1;
5888 }
5889
5890 if (!w->pseudo_window_p)
5891 {
95f2448c
RS
5892 /* Fix the display of overlapping rows. */
5893 if (first_overlapping_row)
5894 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
d67d1627 5895
9f5a911b
JR
5896 /* Draw border between windows. */
5897 x_draw_vertical_border (w);
5898
5899 /* Turn the cursor on again. */
5900 if (cursor_cleared_p)
5901 x_update_window_cursor (w, 1);
5902 }
791f420f 5903 }
9f5a911b
JR
5904
5905 return mouse_face_overwritten_p;
ee78dc32 5906}
ee78dc32 5907
ee78dc32
GV
5908\f
5909static void
5910frame_highlight (f)
5911 struct frame *f;
5912{
791f420f 5913 x_update_cursor (f, 1);
ee78dc32
GV
5914}
5915
5916static void
5917frame_unhighlight (f)
5918 struct frame *f;
5919{
791f420f 5920 x_update_cursor (f, 1);
ee78dc32
GV
5921}
5922
ee78dc32
GV
5923/* The focus has changed. Update the frames as necessary to reflect
5924 the new situation. Note that we can't change the selected frame
5925 here, because the Lisp code we are interrupting might become confused.
5926 Each event gets marked with the frame in which it occurred, so the
5927 Lisp code can tell when the switch took place by examining the events. */
5928
9ef2e2cf 5929static void
ee78dc32 5930x_new_focus_frame (dpyinfo, frame)
fbd6baed 5931 struct w32_display_info *dpyinfo;
ee78dc32
GV
5932 struct frame *frame;
5933{
fbd6baed 5934 struct frame *old_focus = dpyinfo->w32_focus_frame;
ee78dc32 5935
fbd6baed 5936 if (frame != dpyinfo->w32_focus_frame)
ee78dc32
GV
5937 {
5938 /* Set this before calling other routines, so that they see
fbd6baed
GV
5939 the correct value of w32_focus_frame. */
5940 dpyinfo->w32_focus_frame = frame;
ee78dc32
GV
5941
5942 if (old_focus && old_focus->auto_lower)
5943 x_lower_frame (old_focus);
5944
fbd6baed
GV
5945 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5946 pending_autoraise_frame = dpyinfo->w32_focus_frame;
ee78dc32
GV
5947 else
5948 pending_autoraise_frame = 0;
5949 }
5950
5951 x_frame_rehighlight (dpyinfo);
5952}
5953
5954/* Handle an event saying the mouse has moved out of an Emacs frame. */
5955
5956void
5957x_mouse_leave (dpyinfo)
fbd6baed 5958 struct w32_display_info *dpyinfo;
ee78dc32 5959{
fbd6baed 5960 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
ee78dc32
GV
5961}
5962
5963/* The focus has changed, or we have redirected a frame's focus to
5964 another frame (this happens when a frame uses a surrogate
9ef2e2cf 5965 mini-buffer frame). Shift the highlight as appropriate.
ee78dc32
GV
5966
5967 The FRAME argument doesn't necessarily have anything to do with which
9ef2e2cf
JR
5968 frame is being highlighted or un-highlighted; we only use it to find
5969 the appropriate X display info. */
5970
ee78dc32 5971static void
fbd6baed 5972w32_frame_rehighlight (frame)
ee78dc32
GV
5973 struct frame *frame;
5974{
7e6ac5b9
AI
5975 if (! FRAME_W32_P (frame))
5976 return;
fbd6baed 5977 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
ee78dc32
GV
5978}
5979
5980static void
5981x_frame_rehighlight (dpyinfo)
fbd6baed 5982 struct w32_display_info *dpyinfo;
ee78dc32 5983{
4baaed0f 5984 struct frame *old_highlight = dpyinfo->x_highlight_frame;
ee78dc32 5985
fbd6baed 5986 if (dpyinfo->w32_focus_frame)
ee78dc32 5987 {
4baaed0f 5988 dpyinfo->x_highlight_frame
fbd6baed
GV
5989 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5990 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5991 : dpyinfo->w32_focus_frame);
4baaed0f 5992 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
ee78dc32 5993 {
fbd6baed 5994 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
4baaed0f 5995 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
ee78dc32
GV
5996 }
5997 }
5998 else
4baaed0f 5999 dpyinfo->x_highlight_frame = 0;
ee78dc32 6000
4baaed0f 6001 if (dpyinfo->x_highlight_frame != old_highlight)
ee78dc32
GV
6002 {
6003 if (old_highlight)
6004 frame_unhighlight (old_highlight);
4baaed0f
KS
6005 if (dpyinfo->x_highlight_frame)
6006 frame_highlight (dpyinfo->x_highlight_frame);
ee78dc32
GV
6007 }
6008}
6009\f
6010/* Keyboard processing - modifier keys, etc. */
6011
6012/* Convert a keysym to its name. */
6013
6014char *
6015x_get_keysym_name (keysym)
6016 int keysym;
6017{
6018 /* Make static so we can always return it */
6019 static char value[100];
6020
6021 BLOCK_INPUT;
9127e20e 6022 GetKeyNameText (keysym, value, 100);
ee78dc32
GV
6023 UNBLOCK_INPUT;
6024
6025 return value;
6026}
9ef2e2cf
JR
6027
6028
ee78dc32
GV
6029\f
6030/* Mouse clicks and mouse movement. Rah. */
6031
9ef2e2cf
JR
6032/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6033 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6034 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6035 not force the value into range. */
ee78dc32
GV
6036
6037void
6038pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6039 FRAME_PTR f;
6040 register int pix_x, pix_y;
6041 register int *x, *y;
6042 RECT *bounds;
6043 int noclip;
6044{
52cf03a1
GV
6045 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
6046 if (NILP (Vwindow_system))
6047 {
6048 *x = pix_x;
6049 *y = pix_y;
6050 return;
6051 }
6052
ee78dc32
GV
6053 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6054 even for negative values. */
6055 if (pix_x < 0)
9127e20e 6056 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
ee78dc32 6057 if (pix_y < 0)
fbd6baed 6058 pix_y -= (f)->output_data.w32->line_height - 1;
ee78dc32
GV
6059
6060 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6061 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6062
6063 if (bounds)
6064 {
6065 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
6066 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
9127e20e 6067 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
fbd6baed 6068 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
ee78dc32
GV
6069 }
6070
6071 if (!noclip)
6072 {
6073 if (pix_x < 0)
6074 pix_x = 0;
ec48c3a7
JR
6075 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6076 pix_x = FRAME_WINDOW_WIDTH (f);
ee78dc32
GV
6077
6078 if (pix_y < 0)
6079 pix_y = 0;
6080 else if (pix_y > f->height)
6081 pix_y = f->height;
6082 }
6083
6084 *x = pix_x;
6085 *y = pix_y;
6086}
6087
791f420f
JR
6088
6089/* Given HPOS/VPOS in the current matrix of W, return corresponding
6090 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6091 can't tell the positions because W's display is not up to date,
6092 return 0. */
6093
6094int
6095glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6096 struct window *w;
6097 int hpos, vpos;
6098 int *frame_x, *frame_y;
ee78dc32 6099{
791f420f
JR
6100 int success_p;
6101
6102 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6103 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6104
6105 if (display_completed)
52cf03a1 6106 {
791f420f
JR
6107 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6108 struct glyph *glyph = row->glyphs[TEXT_AREA];
6109 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6110
6111 *frame_y = row->y;
6112 *frame_x = row->x;
6113 while (glyph < end)
6114 {
6115 *frame_x += glyph->pixel_width;
6116 ++glyph;
6117 }
6118
6119 success_p = 1;
6120 }
6121 else
6122 {
6123 *frame_y = *frame_x = 0;
6124 success_p = 0;
52cf03a1
GV
6125 }
6126
791f420f
JR
6127 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6128 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6129 return success_p;
ee78dc32
GV
6130}
6131
53823ab9
JR
6132/* Parse a button MESSAGE. The button index is returned in PBUTTON, and
6133 the state in PUP. XBUTTON provides extra information for extended mouse
6134 button messages. Returns FALSE if unable to parse the message. */
d67d1627 6135BOOL
53823ab9 6136parse_button (message, xbutton, pbutton, pup)
ee78dc32 6137 int message;
53823ab9 6138 int xbutton;
ee78dc32
GV
6139 int * pbutton;
6140 int * pup;
6141{
6142 int button = 0;
6143 int up = 0;
d67d1627 6144
ee78dc32
GV
6145 switch (message)
6146 {
6147 case WM_LBUTTONDOWN:
6148 button = 0;
6149 up = 0;
6150 break;
6151 case WM_LBUTTONUP:
6152 button = 0;
6153 up = 1;
6154 break;
6155 case WM_MBUTTONDOWN:
fbd6baed 6156 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6157 button = 1;
6158 else
6159 button = 2;
ee78dc32
GV
6160 up = 0;
6161 break;
6162 case WM_MBUTTONUP:
fbd6baed 6163 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6164 button = 1;
6165 else
6166 button = 2;
ee78dc32
GV
6167 up = 1;
6168 break;
6169 case WM_RBUTTONDOWN:
fbd6baed 6170 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6171 button = 2;
6172 else
6173 button = 1;
ee78dc32
GV
6174 up = 0;
6175 break;
6176 case WM_RBUTTONUP:
fbd6baed 6177 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6178 button = 2;
6179 else
6180 button = 1;
ee78dc32
GV
6181 up = 1;
6182 break;
53823ab9
JR
6183 case WM_XBUTTONDOWN:
6184 button = xbutton + 2;
6185 up = 0;
6186 break;
6187 case WM_XBUTTONUP:
6188 button = xbutton + 2;
6189 up = 1;
6190 break;
ee78dc32
GV
6191 default:
6192 return (FALSE);
6193 }
d67d1627 6194
ee78dc32
GV
6195 if (pup) *pup = up;
6196 if (pbutton) *pbutton = button;
d67d1627 6197
ee78dc32
GV
6198 return (TRUE);
6199}
6200
6201
6202/* Prepare a mouse-event in *RESULT for placement in the input queue.
6203
6204 If the event is a button press, then note that we have grabbed
6205 the mouse. */
6206
ec48c3a7 6207static Lisp_Object
ee78dc32
GV
6208construct_mouse_click (result, msg, f)
6209 struct input_event *result;
fbd6baed 6210 W32Msg *msg;
ee78dc32
GV
6211 struct frame *f;
6212{
6213 int button;
6214 int up;
6215
53823ab9
JR
6216 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
6217 &button, &up);
ee78dc32 6218
3b8f9651 6219 /* Make the event type NO_EVENT; we'll change that when we decide
ee78dc32 6220 otherwise. */
3b8f9651 6221 result->kind = MOUSE_CLICK_EVENT;
ee78dc32
GV
6222 result->code = button;
6223 result->timestamp = msg->msg.time;
6224 result->modifiers = (msg->dwModifiers
6225 | (up
6226 ? up_modifier
6227 : down_modifier));
6228
ec48c3a7
JR
6229 XSETINT (result->x, LOWORD (msg->msg.lParam));
6230 XSETINT (result->y, HIWORD (msg->msg.lParam));
6231 XSETFRAME (result->frame_or_window, f);
6232 result->arg = Qnil;
6233 return Qnil;
ee78dc32
GV
6234}
6235
ec48c3a7 6236static Lisp_Object
689004fa
GV
6237construct_mouse_wheel (result, msg, f)
6238 struct input_event *result;
6239 W32Msg *msg;
6240 struct frame *f;
6241{
6242 POINT p;
3b8f9651 6243 result->kind = MOUSE_WHEEL_EVENT;
689004fa
GV
6244 result->code = (short) HIWORD (msg->msg.wParam);
6245 result->timestamp = msg->msg.time;
6246 result->modifiers = msg->dwModifiers;
6247 p.x = LOWORD (msg->msg.lParam);
6248 p.y = HIWORD (msg->msg.lParam);
9127e20e 6249 ScreenToClient (msg->msg.hwnd, &p);
689004fa
GV
6250 XSETINT (result->x, p.x);
6251 XSETINT (result->y, p.y);
6252 XSETFRAME (result->frame_or_window, f);
7e889510 6253 result->arg = Qnil;
ec48c3a7 6254 return Qnil;
689004fa
GV
6255}
6256
ec48c3a7 6257static Lisp_Object
12857dfd
RS
6258construct_drag_n_drop (result, msg, f)
6259 struct input_event *result;
6260 W32Msg *msg;
6261 struct frame *f;
6262{
6263 Lisp_Object files;
6264 Lisp_Object frame;
6265 HDROP hdrop;
6266 POINT p;
6267 WORD num_files;
6268 char *name;
6269 int i, len;
6270
3b8f9651 6271 result->kind = DRAG_N_DROP_EVENT;
12857dfd
RS
6272 result->code = 0;
6273 result->timestamp = msg->msg.time;
6274 result->modifiers = msg->dwModifiers;
6275
fb9cc9cc
AI
6276 hdrop = (HDROP) msg->msg.wParam;
6277 DragQueryPoint (hdrop, &p);
6278
e12ca9c2 6279#if 0
12857dfd
RS
6280 p.x = LOWORD (msg->msg.lParam);
6281 p.y = HIWORD (msg->msg.lParam);
6282 ScreenToClient (msg->msg.hwnd, &p);
e12ca9c2
AI
6283#endif
6284
12857dfd
RS
6285 XSETINT (result->x, p.x);
6286 XSETINT (result->y, p.y);
6287
12857dfd
RS
6288 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
6289 files = Qnil;
6290
6291 for (i = 0; i < num_files; i++)
6292 {
6293 len = DragQueryFile (hdrop, i, NULL, 0);
6294 if (len <= 0)
6295 continue;
6296 name = alloca (len + 1);
6297 DragQueryFile (hdrop, i, name, len + 1);
54a0ce0f 6298 files = Fcons (DECODE_FILE (build_string (name)), files);
12857dfd
RS
6299 }
6300
6301 DragFinish (hdrop);
6302
6303 XSETFRAME (frame, f);
6304 result->frame_or_window = Fcons (frame, files);
7e889510 6305 result->arg = Qnil;
ec48c3a7 6306 return Qnil;
12857dfd
RS
6307}
6308
ee78dc32
GV
6309\f
6310/* Function to report a mouse movement to the mainstream Emacs code.
6311 The input handler calls this.
6312
6313 We have received a mouse movement event, which is given in *event.
6314 If the mouse is over a different glyph than it was last time, tell
6315 the mainstream emacs code by setting mouse_moved. If not, ask for
6316 another motion event, so we can check again the next time it moves. */
6317
791f420f
JR
6318static MSG last_mouse_motion_event;
6319static Lisp_Object last_mouse_motion_frame;
6320
9f5a911b
JR
6321static void remember_mouse_glyph P_ ((struct frame *, int, int));
6322
ee78dc32
GV
6323static void
6324note_mouse_movement (frame, msg)
6325 FRAME_PTR frame;
6326 MSG *msg;
6327{
9f5a911b
JR
6328 int mouse_x = LOWORD (msg->lParam);
6329 int mouse_y = HIWORD (msg->lParam);
6330
ee78dc32 6331 last_mouse_movement_time = msg->time;
791f420f
JR
6332 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
6333 XSETFRAME (last_mouse_motion_frame, frame);
ee78dc32 6334
b0981e14 6335#if 0 /* Calling Lisp asynchronously is not safe. */
ee8ceff8 6336 if (mouse_autoselect_window)
549808db
JR
6337 {
6338 int area;
6339 Lisp_Object window;
6340 static Lisp_Object last_window;
6341
6342 window = window_from_coordinates (frame, mouse_x, mouse_y, &area, 0);
6343
6344 /* Window will be selected only when it is not selected now and
42d9d13a 6345 last mouse movement event was not in it. Minibuffer window
549808db
JR
6346 will be selected iff it is active. */
6347 if (!EQ (window, last_window)
6348 && !EQ (window, selected_window)
6349 && (!MINI_WINDOW_P (XWINDOW (window))
6350 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6351 Fselect_window (window);
6352
6353 last_window=window;
6354 }
b0981e14 6355#endif
549808db 6356
fbd6baed 6357 if (msg->hwnd != FRAME_W32_WINDOW (frame))
ee78dc32
GV
6358 {
6359 frame->mouse_moved = 1;
6360 last_mouse_scroll_bar = Qnil;
ee78dc32
GV
6361 note_mouse_highlight (frame, -1, -1);
6362 }
6363
6364 /* Has the mouse moved off the glyph it was on at the last sighting? */
9f5a911b
JR
6365 else if (mouse_x < last_mouse_glyph.left
6366 || mouse_x > last_mouse_glyph.right
6367 || mouse_y < last_mouse_glyph.top
6368 || mouse_y > last_mouse_glyph.bottom)
ee78dc32
GV
6369 {
6370 frame->mouse_moved = 1;
6371 last_mouse_scroll_bar = Qnil;
9f5a911b
JR
6372 note_mouse_highlight (frame, mouse_x, mouse_y);
6373 /* Remember the mouse position here, as w32_mouse_position only
6374 gets called when mouse tracking is enabled but we also need
6375 to keep track of the mouse for help_echo and highlighting at
6376 other times. */
6377 remember_mouse_glyph (frame, mouse_x, mouse_y);
ee78dc32
GV
6378 }
6379}
6380
791f420f
JR
6381\f
6382/************************************************************************
6383 Mouse Face
6384 ************************************************************************/
6385
6386/* Find the glyph under window-relative coordinates X/Y in window W.
6387 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6388 strings. Return in *HPOS and *VPOS the row and column number of
6389 the glyph found. Return in *AREA the glyph area containing X.
6390 Value is a pointer to the glyph found or null if X/Y is not on
6391 text, or we can't tell because W's current matrix is not up to
6392 date. */
6393
6394static struct glyph *
9f5a911b 6395x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
791f420f
JR
6396 struct window *w;
6397 int x, y;
6398 int *hpos, *vpos, *area;
9f5a911b 6399 int buffer_only_p;
791f420f
JR
6400{
6401 struct glyph *glyph, *end;
ec48c3a7 6402 struct glyph_row *row = NULL;
791f420f
JR
6403 int x0, i, left_area_width;
6404
6405 /* Find row containing Y. Give up if some row is not enabled. */
6406 for (i = 0; i < w->current_matrix->nrows; ++i)
6407 {
6408 row = MATRIX_ROW (w->current_matrix, i);
6409 if (!row->enabled_p)
6410 return NULL;
6411 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6412 break;
6413 }
6414
6415 *vpos = i;
6416 *hpos = 0;
6417
6418 /* Give up if Y is not in the window. */
6419 if (i == w->current_matrix->nrows)
6420 return NULL;
6421
6422 /* Get the glyph area containing X. */
6423 if (w->pseudo_window_p)
6424 {
6425 *area = TEXT_AREA;
6426 x0 = 0;
6427 }
6428 else
6429 {
6430 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6431 if (x < left_area_width)
6432 {
6433 *area = LEFT_MARGIN_AREA;
6434 x0 = 0;
6435 }
6436 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6437 {
6438 *area = TEXT_AREA;
6439 x0 = row->x + left_area_width;
6440 }
6441 else
6442 {
6443 *area = RIGHT_MARGIN_AREA;
6444 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6445 }
6446 }
6447
6448 /* Find glyph containing X. */
6449 glyph = row->glyphs[*area];
6450 end = glyph + row->used[*area];
6451 while (glyph < end)
6452 {
6453 if (x < x0 + glyph->pixel_width)
6454 {
6455 if (w->pseudo_window_p)
6456 break;
9f5a911b 6457 else if (!buffer_only_p || BUFFERP (glyph->object))
791f420f
JR
6458 break;
6459 }
d67d1627 6460
791f420f
JR
6461 x0 += glyph->pixel_width;
6462 ++glyph;
6463 }
6464
6465 if (glyph == end)
6466 return NULL;
6467
6468 *hpos = glyph - row->glyphs[*area];
6469 return glyph;
6470}
6471
6472
6473/* Convert frame-relative x/y to coordinates relative to window W.
6474 Takes pseudo-windows into account. */
6475
6476static void
6477frame_to_window_pixel_xy (w, x, y)
6478 struct window *w;
6479 int *x, *y;
6480{
6481 if (w->pseudo_window_p)
6482 {
6483 /* A pseudo-window is always full-width, and starts at the
6484 left edge of the frame, plus a frame border. */
6485 struct frame *f = XFRAME (w->frame);
6486 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6487 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6488 }
6489 else
6490 {
6491 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6492 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6493 }
6494}
6495
6496
c2cc16fa 6497/* Take proper action when mouse has moved to the mode or header line of
791f420f
JR
6498 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6499 mode line. X is relative to the start of the text display area of
33c34bea 6500 W, so the width of fringes and scroll bars must be subtracted
791f420f
JR
6501 to get a position relative to the start of the mode line. */
6502
6503static void
6504note_mode_line_highlight (w, x, mode_line_p)
6505 struct window *w;
6506 int x, mode_line_p;
6507{
6508 struct frame *f = XFRAME (w->frame);
6509 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6510 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6511 struct glyph_row *row;
6512
6513 if (mode_line_p)
6514 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6515 else
6516 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6517
6518 if (row->enabled_p)
6519 {
6520 struct glyph *glyph, *end;
6521 Lisp_Object help, map;
6522 int x0;
d67d1627 6523
791f420f
JR
6524 /* Find the glyph under X. */
6525 glyph = row->glyphs[TEXT_AREA];
6526 end = glyph + row->used[TEXT_AREA];
6527 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
33c34bea 6528 + FRAME_X_LEFT_FRINGE_WIDTH (f));
d67d1627 6529
791f420f
JR
6530 while (glyph < end
6531 && x >= x0 + glyph->pixel_width)
6532 {
6533 x0 += glyph->pixel_width;
6534 ++glyph;
6535 }
6536
6537 if (glyph < end
6538 && STRINGP (glyph->object)
d5db4077 6539 && STRING_INTERVALS (glyph->object)
791f420f 6540 && glyph->charpos >= 0
d5db4077 6541 && glyph->charpos < SCHARS (glyph->object))
791f420f
JR
6542 {
6543 /* If we're on a string with `help-echo' text property,
6544 arrange for the help to be displayed. This is done by
6545 setting the global variable help_echo to the help string. */
6546 help = Fget_text_property (make_number (glyph->charpos),
6547 Qhelp_echo, glyph->object);
b7e80413 6548 if (!NILP (help))
ec48c3a7
JR
6549 {
6550 help_echo = help;
158cba56 6551 XSETWINDOW (help_echo_window, w);
ec48c3a7
JR
6552 help_echo_object = glyph->object;
6553 help_echo_pos = glyph->charpos;
6554 }
791f420f
JR
6555
6556 /* Change the mouse pointer according to what is under X/Y. */
6557 map = Fget_text_property (make_number (glyph->charpos),
6558 Qlocal_map, glyph->object);
02067692 6559 if (KEYMAPP (map))
791f420f 6560 cursor = f->output_data.w32->nontext_cursor;
c2cc16fa
JR
6561 else
6562 {
6563 map = Fget_text_property (make_number (glyph->charpos),
6564 Qkeymap, glyph->object);
6565 if (KEYMAPP (map))
6566 cursor = f->output_data.w32->nontext_cursor;
6567 }
791f420f
JR
6568 }
6569 }
01b220b6 6570#if 0 /* TODO: mouse cursor */
791f420f
JR
6571 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6572#endif
6573}
ee78dc32 6574
9ef2e2cf
JR
6575
6576/* Take proper action when the mouse has moved to position X, Y on
6577 frame F as regards highlighting characters that have mouse-face
6578 properties. Also de-highlighting chars where the mouse was before.
ee78dc32
GV
6579 X and Y can be negative or out of range. */
6580
6581static void
6582note_mouse_highlight (f, x, y)
9ef2e2cf 6583 struct frame *f;
ee78dc32
GV
6584 int x, y;
6585{
791f420f
JR
6586 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6587 int portion;
ee78dc32
GV
6588 Lisp_Object window;
6589 struct window *w;
9f5a911b 6590 struct buffer *b;
ee78dc32 6591
791f420f
JR
6592 /* When a menu is active, don't highlight because this looks odd. */
6593 if (popup_activated ())
6594 return;
6595
3d26a7c2 6596 if (NILP (Vmouse_highlight)
9ef2e2cf 6597 || !f->glyphs_initialized_p)
ee78dc32
GV
6598 return;
6599
791f420f
JR
6600 dpyinfo->mouse_face_mouse_x = x;
6601 dpyinfo->mouse_face_mouse_y = y;
6602 dpyinfo->mouse_face_mouse_frame = f;
ee78dc32 6603
791f420f 6604 if (dpyinfo->mouse_face_defer)
ee78dc32
GV
6605 return;
6606
6607 if (gc_in_progress)
6608 {
791f420f 6609 dpyinfo->mouse_face_deferred_gc = 1;
ee78dc32
GV
6610 return;
6611 }
6612
ee78dc32 6613 /* Which window is that in? */
791f420f 6614 window = window_from_coordinates (f, x, y, &portion, 1);
ee78dc32
GV
6615
6616 /* If we were displaying active text in another window, clear that. */
791f420f
JR
6617 if (! EQ (window, dpyinfo->mouse_face_window))
6618 clear_mouse_face (dpyinfo);
6619
6620 /* Not on a window -> return. */
6621 if (!WINDOWP (window))
6622 return;
6623
abb15ebd
JR
6624 /* Reset help_echo. It will get recomputed below. */
6625 help_echo = Qnil;
6626
791f420f
JR
6627 /* Convert to window-relative pixel coordinates. */
6628 w = XWINDOW (window);
6629 frame_to_window_pixel_xy (w, &x, &y);
6630
6631 /* Handle tool-bar window differently since it doesn't display a
6632 buffer. */
6633 if (EQ (window, f->tool_bar_window))
6634 {
6635 note_tool_bar_highlight (f, x, y);
6636 return;
6637 }
6638
9f5a911b 6639 /* Mouse is on the mode or header line? */
791f420f
JR
6640 if (portion == 1 || portion == 3)
6641 {
791f420f
JR
6642 note_mode_line_highlight (w, x, portion == 1);
6643 return;
6644 }
01b220b6 6645#if 0 /* TODO: mouse cursor */
9f5a911b
JR
6646 if (portion == 2)
6647 cursor = f->output_data.x->horizontal_drag_cursor;
791f420f 6648 else
9f5a911b 6649 cursor = f->output_data.x->text_cursor;
791f420f 6650#endif
ee78dc32
GV
6651 /* Are we in a window whose display is up to date?
6652 And verify the buffer's text has not changed. */
9f5a911b 6653 b = XBUFFER (w->buffer);
9ef2e2cf 6654 if (/* Within text portion of the window. */
791f420f 6655 portion == 0
ee78dc32 6656 && EQ (w->window_end_valid, w->buffer)
9f5a911b
JR
6657 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6658 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
ee78dc32 6659 {
791f420f
JR
6660 int hpos, vpos, pos, i, area;
6661 struct glyph *glyph;
9f5a911b
JR
6662 Lisp_Object object;
6663 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6664 Lisp_Object *overlay_vec = NULL;
6665 int len, noverlays;
6666 struct buffer *obuf;
6667 int obegv, ozv, same_region;
791f420f
JR
6668
6669 /* Find the glyph under X/Y. */
9f5a911b 6670 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
791f420f
JR
6671
6672 /* Clear mouse face if X/Y not over text. */
6673 if (glyph == NULL
6674 || area != TEXT_AREA
6675 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6676 {
6677 clear_mouse_face (dpyinfo);
9f5a911b
JR
6678 /* TODO: mouse cursor */
6679 goto set_cursor;
791f420f
JR
6680 }
6681
6682 pos = glyph->charpos;
9f5a911b
JR
6683 object = glyph->object;
6684 if (!STRINGP (object) && !BUFFERP (object))
6685 goto set_cursor;
6686
6687 /* If we get an out-of-range value, return now; avoid an error. */
6688 if (BUFFERP (object) && pos > BUF_Z (b))
6689 goto set_cursor;
6690
6691 /* Make the window's buffer temporarily current for
6692 overlays_at and compute_char_face. */
6693 obuf = current_buffer;
6694 current_buffer = b;
6695 obegv = BEGV;
6696 ozv = ZV;
6697 BEGV = BEG;
6698 ZV = Z;
6699
6700 /* Is this char mouse-active or does it have help-echo? */
6701 position = make_number (pos);
6702
6703 if (BUFFERP (object))
6704 {
6705 /* Put all the overlays we want in a vector in overlay_vec.
6706 Store the length in len. If there are more than 10, make
6707 enough space for all, and try again. */
6708 len = 10;
6709 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6710 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6711 if (noverlays > len)
6712 {
6713 len = noverlays;
6714 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6715 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6716 }
6717
6718 /* Sort overlays into increasing priority order. */
6719 noverlays = sort_overlays (overlay_vec, noverlays, w);
6720 }
6721 else
6722 noverlays = 0;
6723
6724 same_region = (EQ (window, dpyinfo->mouse_face_window)
6725 && vpos >= dpyinfo->mouse_face_beg_row
6726 && vpos <= dpyinfo->mouse_face_end_row
6727 && (vpos > dpyinfo->mouse_face_beg_row
6728 || hpos >= dpyinfo->mouse_face_beg_col)
6729 && (vpos < dpyinfo->mouse_face_end_row
6730 || hpos < dpyinfo->mouse_face_end_col
6731 || dpyinfo->mouse_face_past_end));
6732
6733 /* TODO: if (same_region)
6734 mouse cursor */
6735
6736 /* Check mouse-face highlighting. */
6737 if (! same_region
6738 /* If there exists an overlay with mouse-face overlapping
6739 the one we are currently highlighting, we have to
6740 check if we enter the overlapping overlay, and then
6741 highlight that. */
6742 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6743 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6744 {
6745 /* Find the highest priority overlay that has a mouse-face
6746 property. */
6747 overlay = Qnil;
6748 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
6749 {
6750 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6751 if (!NILP (mouse_face))
6752 overlay = overlay_vec[i];
6753 }
791f420f 6754
9f5a911b
JR
6755 /* If we're actually highlighting the same overlay as
6756 before, there's no need to do that again. */
6757 if (!NILP (overlay)
6758 && EQ (overlay, dpyinfo->mouse_face_overlay))
6759 goto check_help_echo;
6760
6761 dpyinfo->mouse_face_overlay = overlay;
6762
6763 /* Clear the display of the old active region, if any. */
6764 clear_mouse_face (dpyinfo);
6765 /* TODO: mouse cursor changes. */
6766
6767 /* If no overlay applies, get a text property. */
6768 if (NILP (overlay))
6769 mouse_face = Fget_text_property (position, Qmouse_face, object);
6770
6771 /* Handle the overlay case. */
6772 if (!NILP (overlay))
6773 {
6774 /* Find the range of text around this char that
6775 should be active. */
6776 Lisp_Object before, after;
6777 int ignore;
6778
6779 before = Foverlay_start (overlay);
6780 after = Foverlay_end (overlay);
6781 /* Record this as the current active region. */
6782 fast_find_position (w, XFASTINT (before),
6783 &dpyinfo->mouse_face_beg_col,
6784 &dpyinfo->mouse_face_beg_row,
6785 &dpyinfo->mouse_face_beg_x,
6786 &dpyinfo->mouse_face_beg_y, Qnil);
6787
6788 dpyinfo->mouse_face_past_end
6789 = !fast_find_position (w, XFASTINT (after),
6790 &dpyinfo->mouse_face_end_col,
6791 &dpyinfo->mouse_face_end_row,
6792 &dpyinfo->mouse_face_end_x,
6793 &dpyinfo->mouse_face_end_y, Qnil);
6794 dpyinfo->mouse_face_window = window;
6795
6796 dpyinfo->mouse_face_face_id
6797 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
6798 &ignore, pos + 1,
6799 !dpyinfo->mouse_face_hidden);
9f5a911b
JR
6800
6801 /* Display it as active. */
6802 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6803 /* TODO: mouse cursor changes. */
6804 }
6805 /* Handle the text property case. */
6806 else if (! NILP (mouse_face) && BUFFERP (object))
6807 {
6808 /* Find the range of text around this char that
6809 should be active. */
6810 Lisp_Object before, after, beginning, end;
6811 int ignore;
6812
6813 beginning = Fmarker_position (w->start);
6814 end = make_number (BUF_Z (XBUFFER (object))
6815 - XFASTINT (w->window_end_pos));
6816 before
6817 = Fprevious_single_property_change (make_number (pos + 1),
6818 Qmouse_face,
6819 object, beginning);
6820 after
6821 = Fnext_single_property_change (position, Qmouse_face,
6822 object, end);
6823
6824 /* Record this as the current active region. */
6825 fast_find_position (w, XFASTINT (before),
6826 &dpyinfo->mouse_face_beg_col,
6827 &dpyinfo->mouse_face_beg_row,
6828 &dpyinfo->mouse_face_beg_x,
6829 &dpyinfo->mouse_face_beg_y, Qnil);
6830 dpyinfo->mouse_face_past_end
6831 = !fast_find_position (w, XFASTINT (after),
6832 &dpyinfo->mouse_face_end_col,
6833 &dpyinfo->mouse_face_end_row,
6834 &dpyinfo->mouse_face_end_x,
6835 &dpyinfo->mouse_face_end_y, Qnil);
6836 dpyinfo->mouse_face_window = window;
6837
6838 if (BUFFERP (object))
6839 dpyinfo->mouse_face_face_id
6840 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
6841 &ignore, pos + 1,
6842 !dpyinfo->mouse_face_hidden);
9f5a911b
JR
6843
6844 /* Display it as active. */
6845 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6846 /* TODO: mouse cursor changes. */
6847 }
6848 else if (!NILP (mouse_face) && STRINGP (object))
6849 {
6850 Lisp_Object b, e;
6851 int ignore;
6852
6853 b = Fprevious_single_property_change (make_number (pos + 1),
6854 Qmouse_face,
6855 object, Qnil);
6856 e = Fnext_single_property_change (position, Qmouse_face,
6857 object, Qnil);
6858 if (NILP (b))
6859 b = make_number (0);
6860 if (NILP (e))
d5db4077 6861 e = make_number (SCHARS (object) - 1);
9f5a911b
JR
6862 fast_find_string_pos (w, XINT (b), object,
6863 &dpyinfo->mouse_face_beg_col,
6864 &dpyinfo->mouse_face_beg_row,
6865 &dpyinfo->mouse_face_beg_x,
6866 &dpyinfo->mouse_face_beg_y, 0);
6867 fast_find_string_pos (w, XINT (e), object,
6868 &dpyinfo->mouse_face_end_col,
6869 &dpyinfo->mouse_face_end_row,
6870 &dpyinfo->mouse_face_end_x,
6871 &dpyinfo->mouse_face_end_y, 1);
6872 dpyinfo->mouse_face_past_end = 0;
6873 dpyinfo->mouse_face_window = window;
6874 dpyinfo->mouse_face_face_id
6875 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
6876 glyph->face_id, 1);
6877 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6878 /* TODO: mouse cursor changes. */
6879 }
6880 else if (STRINGP (object) && NILP (mouse_face))
6881 {
6882 /* A string which doesn't have mouse-face, but
6883 the text ``under'' it might have. */
6884 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
6885 int start = MATRIX_ROW_START_CHARPOS (r);
d67d1627 6886
9f5a911b
JR
6887 pos = string_buffer_position (w, object, start);
6888 if (pos > 0)
6889 mouse_face = get_char_property_and_overlay (make_number (pos),
6890 Qmouse_face,
6891 w->buffer,
6892 &overlay);
6893 if (!NILP (mouse_face) && !NILP (overlay))
6894 {
6895 Lisp_Object before = Foverlay_start (overlay);
6896 Lisp_Object after = Foverlay_end (overlay);
6ff3e5e3 6897 int ignore;
9f5a911b
JR
6898
6899 /* Note that we might not be able to find position
6900 BEFORE in the glyph matrix if the overlay is
6901 entirely covered by a `display' property. In
6902 this case, we overshoot. So let's stop in
6903 the glyph matrix before glyphs for OBJECT. */
6904 fast_find_position (w, XFASTINT (before),
6905 &dpyinfo->mouse_face_beg_col,
6906 &dpyinfo->mouse_face_beg_row,
6907 &dpyinfo->mouse_face_beg_x,
6908 &dpyinfo->mouse_face_beg_y,
6909 object);
d67d1627 6910
9f5a911b
JR
6911 dpyinfo->mouse_face_past_end
6912 = !fast_find_position (w, XFASTINT (after),
6913 &dpyinfo->mouse_face_end_col,
6914 &dpyinfo->mouse_face_end_row,
6915 &dpyinfo->mouse_face_end_x,
6916 &dpyinfo->mouse_face_end_y,
6917 Qnil);
6918 dpyinfo->mouse_face_window = window;
6919 dpyinfo->mouse_face_face_id
6920 = face_at_buffer_position (w, pos, 0, 0,
1b7334a4
KS
6921 &ignore, pos + 1,
6922 !dpyinfo->mouse_face_hidden);
9f5a911b
JR
6923
6924 /* Display it as active. */
6925 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6926 /* TODO: mouse cursor changes. */
6927 }
6928 }
6929 }
6930
6931 check_help_echo:
6932
6933 /* Look for a `help-echo' property. */
791f420f 6934 {
9f5a911b
JR
6935 Lisp_Object help, overlay;
6936
6937 /* Check overlays first. */
6938 help = overlay = Qnil;
6939 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
791f420f 6940 {
9f5a911b 6941 overlay = overlay_vec[i];
d67d1627 6942 help = Foverlay_get (overlay, Qhelp_echo);
791f420f
JR
6943 }
6944
9f5a911b 6945 if (!NILP (help))
791f420f 6946 {
9f5a911b
JR
6947 help_echo = help;
6948 help_echo_window = window;
6949 help_echo_object = overlay;
6950 help_echo_pos = pos;
6951 }
6952 else
6953 {
6954 Lisp_Object object = glyph->object;
6955 int charpos = glyph->charpos;
ec48c3a7 6956
9f5a911b
JR
6957 /* Try text properties. */
6958 if (STRINGP (object)
6959 && charpos >= 0
d5db4077 6960 && charpos < SCHARS (object))
9f5a911b
JR
6961 {
6962 help = Fget_text_property (make_number (charpos),
6963 Qhelp_echo, object);
6964 if (NILP (help))
6965 {
6966 /* If the string itself doesn't specify a help-echo,
6967 see if the buffer text ``under'' it does. */
6968 struct glyph_row *r
6969 = MATRIX_ROW (w->current_matrix, vpos);
6970 int start = MATRIX_ROW_START_CHARPOS (r);
6971 int pos = string_buffer_position (w, object, start);
6972 if (pos > 0)
6973 {
6974 help = Fget_char_property (make_number (pos),
6975 Qhelp_echo, w->buffer);
6976 if (!NILP (help))
6977 {
6978 charpos = pos;
6979 object = w->buffer;
6980 }
6981 }
6982 }
6983 }
6984 else if (BUFFERP (object)
6985 && charpos >= BEGV
6986 && charpos < ZV)
6987 help = Fget_text_property (make_number (charpos), Qhelp_echo,
6988 object);
d67d1627 6989
9f5a911b
JR
6990 if (!NILP (help))
6991 {
6992 help_echo = help;
6993 help_echo_window = window;
6994 help_echo_object = object;
6995 help_echo_pos = charpos;
6996 }
6997 }
791f420f 6998 }
9f5a911b
JR
6999
7000 BEGV = obegv;
7001 ZV = ozv;
7002 current_buffer = obuf;
791f420f 7003 }
9f5a911b
JR
7004
7005 set_cursor:
7006 /* TODO: mouse cursor changes. */
7007 ;
791f420f
JR
7008}
7009
791f420f
JR
7010static void
7011redo_mouse_highlight ()
7012{
7013 if (!NILP (last_mouse_motion_frame)
7014 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7015 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7016 LOWORD (last_mouse_motion_event.lParam),
7017 HIWORD (last_mouse_motion_event.lParam));
7018}
7019
7020
7021\f
7022/***********************************************************************
7023 Tool-bars
7024 ***********************************************************************/
7025
7026static int x_tool_bar_item P_ ((struct frame *, int, int,
7027 struct glyph **, int *, int *, int *));
7028
7029/* Tool-bar item index of the item on which a mouse button was pressed
7030 or -1. */
7031
7032static int last_tool_bar_item;
7033
7034
7035/* Get information about the tool-bar item at position X/Y on frame F.
7036 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7037 the current matrix of the tool-bar window of F, or NULL if not
7038 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
d417b5e0 7039 item in F->tool_bar_items. Value is
791f420f
JR
7040
7041 -1 if X/Y is not on a tool-bar item
7042 0 if X/Y is on the same item that was highlighted before.
7043 1 otherwise. */
7044
7045static int
7046x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7047 struct frame *f;
7048 int x, y;
7049 struct glyph **glyph;
7050 int *hpos, *vpos, *prop_idx;
7051{
7052 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7053 struct window *w = XWINDOW (f->tool_bar_window);
7054 int area;
7055
7056 /* Find the glyph under X/Y. */
9f5a911b 7057 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
791f420f
JR
7058 if (*glyph == NULL)
7059 return -1;
7060
7061 /* Get the start of this tool-bar item's properties in
d417b5e0 7062 f->tool_bar_items. */
791f420f
JR
7063 if (!tool_bar_item_info (f, *glyph, prop_idx))
7064 return -1;
7065
7066 /* Is mouse on the highlighted item? */
7067 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7068 && *vpos >= dpyinfo->mouse_face_beg_row
7069 && *vpos <= dpyinfo->mouse_face_end_row
7070 && (*vpos > dpyinfo->mouse_face_beg_row
7071 || *hpos >= dpyinfo->mouse_face_beg_col)
7072 && (*vpos < dpyinfo->mouse_face_end_row
7073 || *hpos < dpyinfo->mouse_face_end_col
7074 || dpyinfo->mouse_face_past_end))
7075 return 0;
d67d1627 7076
791f420f
JR
7077 return 1;
7078}
7079
7080
9ef2e2cf 7081/* Handle mouse button event on the tool-bar of frame F, at
791f420f
JR
7082 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
7083 or ButtonRelase. */
7084
7085static void
7086w32_handle_tool_bar_click (f, button_event)
7087 struct frame *f;
7088 struct input_event *button_event;
7089{
7090 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7091 struct window *w = XWINDOW (f->tool_bar_window);
7092 int hpos, vpos, prop_idx;
7093 struct glyph *glyph;
7094 Lisp_Object enabled_p;
7095 int x = XFASTINT (button_event->x);
7096 int y = XFASTINT (button_event->y);
d67d1627 7097
791f420f
JR
7098 /* If not on the highlighted tool-bar item, return. */
7099 frame_to_window_pixel_xy (w, &x, &y);
7100 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7101 return;
7102
7103 /* If item is disabled, do nothing. */
d417b5e0 7104 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
791f420f
JR
7105 if (NILP (enabled_p))
7106 return;
d67d1627 7107
d4ae780a 7108 if (button_event->modifiers & down_modifier)
791f420f
JR
7109 {
7110 /* Show item in pressed state. */
7111 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7112 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7113 last_tool_bar_item = prop_idx;
7114 }
7115 else
7116 {
7117 Lisp_Object key, frame;
7118 struct input_event event;
7119
7120 /* Show item in released state. */
7121 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7122 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7123
d417b5e0 7124 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
791f420f
JR
7125
7126 XSETFRAME (frame, f);
7127 event.kind = TOOL_BAR_EVENT;
c8b33aa6
GM
7128 event.frame_or_window = frame;
7129 event.arg = frame;
791f420f
JR
7130 kbd_buffer_store_event (&event);
7131
7132 event.kind = TOOL_BAR_EVENT;
c8b33aa6
GM
7133 event.frame_or_window = frame;
7134 event.arg = key;
d4ae780a
JR
7135 /* The keyboard buffer doesn't like the up modifier being set. */
7136 event.modifiers = button_event->modifiers & ~up_modifier;
791f420f
JR
7137 kbd_buffer_store_event (&event);
7138 last_tool_bar_item = -1;
7139 }
7140}
7141
7142
9ef2e2cf 7143/* Possibly highlight a tool-bar item on frame F when mouse moves to
791f420f
JR
7144 tool-bar window-relative coordinates X/Y. Called from
7145 note_mouse_highlight. */
7146
7147static void
7148note_tool_bar_highlight (f, x, y)
7149 struct frame *f;
7150 int x, y;
7151{
7152 Lisp_Object window = f->tool_bar_window;
7153 struct window *w = XWINDOW (window);
7154 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7155 int hpos, vpos;
7156 struct glyph *glyph;
7157 struct glyph_row *row;
7158 int i;
7159 Lisp_Object enabled_p;
7160 int prop_idx;
7161 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7162 int mouse_down_p, rc;
7163
7164 /* Function note_mouse_highlight is called with negative x(y
7165 values when mouse moves outside of the frame. */
7166 if (x <= 0 || y <= 0)
7167 {
7168 clear_mouse_face (dpyinfo);
7169 return;
7170 }
7171
7172 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7173 if (rc < 0)
7174 {
7175 /* Not on tool-bar item. */
7176 clear_mouse_face (dpyinfo);
7177 return;
7178 }
7179 else if (rc == 0)
7180 /* On same tool-bar item as before. */
7181 goto set_help_echo;
ee78dc32 7182
791f420f 7183 clear_mouse_face (dpyinfo);
d67d1627 7184
791f420f
JR
7185 /* Mouse is down, but on different tool-bar item? */
7186 mouse_down_p = (dpyinfo->grabbed
7187 && f == last_mouse_frame
7188 && FRAME_LIVE_P (f));
7189 if (mouse_down_p
7190 && last_tool_bar_item != prop_idx)
7191 return;
ee78dc32 7192
791f420f
JR
7193 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7194 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
d67d1627 7195
791f420f 7196 /* If tool-bar item is not enabled, don't highlight it. */
d417b5e0 7197 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
791f420f
JR
7198 if (!NILP (enabled_p))
7199 {
7200 /* Compute the x-position of the glyph. In front and past the
7201 image is a space. We include this is the highlighted area. */
7202 row = MATRIX_ROW (w->current_matrix, vpos);
7203 for (i = x = 0; i < hpos; ++i)
7204 x += row->glyphs[TEXT_AREA][i].pixel_width;
d67d1627 7205
791f420f
JR
7206 /* Record this as the current active region. */
7207 dpyinfo->mouse_face_beg_col = hpos;
7208 dpyinfo->mouse_face_beg_row = vpos;
7209 dpyinfo->mouse_face_beg_x = x;
7210 dpyinfo->mouse_face_beg_y = row->y;
7211 dpyinfo->mouse_face_past_end = 0;
d67d1627 7212
791f420f
JR
7213 dpyinfo->mouse_face_end_col = hpos + 1;
7214 dpyinfo->mouse_face_end_row = vpos;
7215 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7216 dpyinfo->mouse_face_end_y = row->y;
7217 dpyinfo->mouse_face_window = window;
7218 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
d67d1627 7219
791f420f
JR
7220 /* Display it as active. */
7221 show_mouse_face (dpyinfo, draw);
7222 dpyinfo->mouse_face_image_state = draw;
ee78dc32 7223 }
d67d1627 7224
791f420f 7225 set_help_echo:
d67d1627 7226
791f420f
JR
7227 /* Set help_echo to a help string.to display for this tool-bar item.
7228 w32_read_socket does the rest. */
158cba56 7229 help_echo_object = help_echo_window = Qnil;
ec48c3a7 7230 help_echo_pos = -1;
d417b5e0 7231 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 7232 if (NILP (help_echo))
d417b5e0 7233 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
ee78dc32 7234}
ee78dc32 7235
9ef2e2cf 7236
791f420f 7237\f
9f5a911b
JR
7238/* Find the glyph matrix position of buffer position CHARPOS in window
7239 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7240 current glyphs must be up to date. If CHARPOS is above window
7241 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7242 of last line in W. In the row containing CHARPOS, stop before glyphs
7243 having STOP as object. */
7244
7245#if 0 /* This is a version of fast_find_position that's more correct
7246 in the presence of hscrolling, for example. I didn't install
7247 it right away because the problem fixed is minor, it failed
d67d1627 7248 in 20.x as well, and I think it's too risky to install
9f5a911b 7249 so near the release of 21.1. 2001-09-25 gerd. */
ee78dc32
GV
7250
7251static int
9f5a911b
JR
7252fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7253 struct window *w;
7254 int charpos;
7255 int *hpos, *vpos, *x, *y;
7256 Lisp_Object stop;
7257{
7258 struct glyph_row *row, *first;
7259 struct glyph *glyph, *end;
7260 int i, past_end = 0;
7261
7262 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
62e33982 7263 row = row_containing_pos (w, charpos, first, NULL, 0);
9f5a911b
JR
7264 if (row == NULL)
7265 {
7266 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7267 {
7268 *x = *y = *hpos = *vpos = 0;
7269 return 0;
7270 }
7271 else
7272 {
7273 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7274 past_end = 1;
7275 }
7276 }
7277
7278 *x = row->x;
7279 *y = row->y;
7280 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
d67d1627 7281
9f5a911b
JR
7282 glyph = row->glyphs[TEXT_AREA];
7283 end = glyph + row->used[TEXT_AREA];
d67d1627 7284
9f5a911b
JR
7285 /* Skip over glyphs not having an object at the start of the row.
7286 These are special glyphs like truncation marks on terminal
7287 frames. */
7288 if (row->displays_text_p)
7289 while (glyph < end
7290 && INTEGERP (glyph->object)
7291 && !EQ (stop, glyph->object)
7292 && glyph->charpos < 0)
7293 {
7294 *x += glyph->pixel_width;
7295 ++glyph;
7296 }
7297
7298 while (glyph < end
7299 && !INTEGERP (glyph->object)
7300 && !EQ (stop, glyph->object)
7301 && (!BUFFERP (glyph->object)
7302 || glyph->charpos < charpos))
7303 {
7304 *x += glyph->pixel_width;
7305 ++glyph;
7306 }
7307
7308 *hpos = glyph - row->glyphs[TEXT_AREA];
7309 return past_end;
7310}
7311
7312#else /* not 0 */
7313
7314static int
7315fast_find_position (w, pos, hpos, vpos, x, y, stop)
791f420f 7316 struct window *w;
ee78dc32 7317 int pos;
791f420f 7318 int *hpos, *vpos, *x, *y;
9f5a911b 7319 Lisp_Object stop;
ee78dc32 7320{
ee78dc32 7321 int i;
ee78dc32 7322 int lastcol;
791f420f
JR
7323 int maybe_next_line_p = 0;
7324 int line_start_position;
7325 int yb = window_text_bottom_y (w);
9f5a911b
JR
7326 struct glyph_row *row, *best_row;
7327 int row_vpos, best_row_vpos;
791f420f
JR
7328 int current_x;
7329
9f5a911b
JR
7330 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7331 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7332
791f420f 7333 while (row->y < yb)
ee78dc32 7334 {
791f420f
JR
7335 if (row->used[TEXT_AREA])
7336 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7337 else
7338 line_start_position = 0;
7339
7340 if (line_start_position > pos)
ee78dc32
GV
7341 break;
7342 /* If the position sought is the end of the buffer,
7343 don't include the blank lines at the bottom of the window. */
791f420f
JR
7344 else if (line_start_position == pos
7345 && pos == BUF_ZV (XBUFFER (w->buffer)))
ee78dc32 7346 {
791f420f 7347 maybe_next_line_p = 1;
ee78dc32
GV
7348 break;
7349 }
791f420f
JR
7350 else if (line_start_position > 0)
7351 {
7352 best_row = row;
7353 best_row_vpos = row_vpos;
7354 }
7355
9ef2e2cf
JR
7356 if (row->y + row->height >= yb)
7357 break;
7358
791f420f
JR
7359 ++row;
7360 ++row_vpos;
ee78dc32
GV
7361 }
7362
791f420f
JR
7363 /* Find the right column within BEST_ROW. */
7364 lastcol = 0;
7365 current_x = best_row->x;
7366 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
ee78dc32 7367 {
791f420f 7368 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
9f5a911b 7369 int charpos = glyph->charpos;
791f420f 7370
9f5a911b 7371 if (BUFFERP (glyph->object))
ee78dc32 7372 {
9f5a911b
JR
7373 if (charpos == pos)
7374 {
7375 *hpos = i;
7376 *vpos = best_row_vpos;
7377 *x = current_x;
7378 *y = best_row->y;
7379 return 1;
7380 }
7381 else if (charpos > pos)
7382 break;
ee78dc32 7383 }
9f5a911b 7384 else if (EQ (glyph->object, stop))
ee78dc32 7385 break;
791f420f 7386
9f5a911b
JR
7387 if (charpos > 0)
7388 lastcol = i;
791f420f 7389 current_x += glyph->pixel_width;
ee78dc32
GV
7390 }
7391
7392 /* If we're looking for the end of the buffer,
7393 and we didn't find it in the line we scanned,
7394 use the start of the following line. */
791f420f 7395 if (maybe_next_line_p)
ee78dc32 7396 {
791f420f
JR
7397 ++best_row;
7398 ++best_row_vpos;
7399 lastcol = 0;
7400 current_x = best_row->x;
ee78dc32
GV
7401 }
7402
791f420f
JR
7403 *vpos = best_row_vpos;
7404 *hpos = lastcol + 1;
7405 *x = current_x;
7406 *y = best_row->y;
ee78dc32
GV
7407 return 0;
7408}
7409
9f5a911b
JR
7410#endif /* not 0 */
7411
7412
269b7745 7413/* Find the position of the glyph for position POS in OBJECT in
9f5a911b
JR
7414 window W's current matrix, and return in *X/*Y the pixel
7415 coordinates, and return in *HPOS/*VPOS the column/row of the glyph.
7416
7417 RIGHT_P non-zero means return the position of the right edge of the
7418 glyph, RIGHT_P zero means return the left edge position.
7419
7420 If no glyph for POS exists in the matrix, return the position of
7421 the glyph with the next smaller position that is in the matrix, if
7422 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7423 exists in the matrix, return the position of the glyph with the
7424 next larger position in OBJECT.
7425
7426 Value is non-zero if a glyph was found. */
7427
7428static int
7429fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7430 struct window *w;
7431 int pos;
7432 Lisp_Object object;
7433 int *hpos, *vpos, *x, *y;
7434 int right_p;
7435{
7436 int yb = window_text_bottom_y (w);
7437 struct glyph_row *r;
7438 struct glyph *best_glyph = NULL;
7439 struct glyph_row *best_row = NULL;
7440 int best_x = 0;
7441
7442 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7443 r->enabled_p && r->y < yb;
7444 ++r)
7445 {
7446 struct glyph *g = r->glyphs[TEXT_AREA];
7447 struct glyph *e = g + r->used[TEXT_AREA];
7448 int gx;
7449
7450 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7451 if (EQ (g->object, object))
7452 {
7453 if (g->charpos == pos)
7454 {
7455 best_glyph = g;
7456 best_x = gx;
7457 best_row = r;
7458 goto found;
7459 }
7460 else if (best_glyph == NULL
7461 || ((abs (g->charpos - pos)
7462 < abs (best_glyph->charpos - pos))
7463 && (right_p
7464 ? g->charpos < pos
7465 : g->charpos > pos)))
7466 {
7467 best_glyph = g;
7468 best_x = gx;
7469 best_row = r;
7470 }
7471 }
7472 }
7473
7474 found:
7475
7476 if (best_glyph)
7477 {
7478 *x = best_x;
7479 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7480
7481 if (right_p)
7482 {
7483 *x += best_glyph->pixel_width;
7484 ++*hpos;
7485 }
d67d1627 7486
9f5a911b
JR
7487 *y = best_row->y;
7488 *vpos = best_row - w->current_matrix->rows;
7489 }
7490
7491 return best_glyph != NULL;
7492}
7493
791f420f 7494
ee78dc32
GV
7495/* Display the active region described by mouse_face_*
7496 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7497
7498static void
791f420f 7499show_mouse_face (dpyinfo, draw)
fbd6baed 7500 struct w32_display_info *dpyinfo;
791f420f 7501 enum draw_glyphs_face draw;
ee78dc32
GV
7502{
7503 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
791f420f 7504 struct frame *f = XFRAME (WINDOW_FRAME (w));
d67d1627 7505
6ff3e5e3
JR
7506 if (/* If window is in the process of being destroyed, don't bother
7507 to do anything. */
7508 w->current_matrix != NULL
3d26a7c2
KS
7509 /* Don't update mouse highlight if hidden */
7510 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
6ff3e5e3
JR
7511 /* Recognize when we are called to operate on rows that don't exist
7512 anymore. This can happen when a window is split. */
7513 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7514 {
7515 int phys_cursor_on_p = w->phys_cursor_on_p;
7516 struct glyph_row *row, *first, *last;
7517
7518 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7519 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
d67d1627 7520
6ff3e5e3
JR
7521 for (row = first; row <= last && row->enabled_p; ++row)
7522 {
7523 int start_hpos, end_hpos, start_x;
791f420f 7524
6ff3e5e3
JR
7525 /* For all but the first row, the highlight starts at column 0. */
7526 if (row == first)
7527 {
7528 start_hpos = dpyinfo->mouse_face_beg_col;
7529 start_x = dpyinfo->mouse_face_beg_x;
7530 }
7531 else
7532 {
7533 start_hpos = 0;
7534 start_x = 0;
7535 }
791f420f 7536
6ff3e5e3
JR
7537 if (row == last)
7538 end_hpos = dpyinfo->mouse_face_end_col;
7539 else
7540 end_hpos = row->used[TEXT_AREA];
791f420f 7541
6ff3e5e3
JR
7542 if (end_hpos > start_hpos)
7543 {
d67d1627 7544 x_draw_glyphs (w, start_x, row, TEXT_AREA,
6ff3e5e3 7545 start_hpos, end_hpos, draw, 0);
791f420f 7546
6637c996
JR
7547 row->mouse_face_p
7548 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
6ff3e5e3 7549 }
ee78dc32
GV
7550 }
7551
6ff3e5e3
JR
7552 /* When we've written over the cursor, arrange for it to
7553 be displayed again. */
7554 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7555 x_display_cursor (w, 1,
7556 w->phys_cursor.hpos, w->phys_cursor.vpos,
7557 w->phys_cursor.x, w->phys_cursor.y);
ee78dc32
GV
7558 }
7559
01b220b6 7560#if 0 /* TODO: mouse cursor */
791f420f
JR
7561 /* Change the mouse cursor. */
7562 if (draw == DRAW_NORMAL_TEXT)
7563 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7564 f->output_data.x->text_cursor);
7565 else if (draw == DRAW_MOUSE_FACE)
7566 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7567 f->output_data.x->cross_cursor);
ee78dc32 7568 else
791f420f
JR
7569 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7570 f->output_data.x->nontext_cursor);
7571#endif
ee78dc32
GV
7572}
7573
7574/* Clear out the mouse-highlighted active region.
9ef2e2cf 7575 Redraw it un-highlighted first. */
ee78dc32 7576
9f5a911b 7577static int
ee78dc32 7578clear_mouse_face (dpyinfo)
fbd6baed 7579 struct w32_display_info *dpyinfo;
ee78dc32 7580{
9f5a911b 7581 int cleared = 0;
791f420f 7582
ee78dc32 7583 if (! NILP (dpyinfo->mouse_face_window))
9f5a911b
JR
7584 {
7585 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7586 cleared = 1;
7587 }
ee78dc32
GV
7588
7589 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7590 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7591 dpyinfo->mouse_face_window = Qnil;
9f5a911b
JR
7592 dpyinfo->mouse_face_overlay = Qnil;
7593 return cleared;
ee78dc32 7594}
31d4844a 7595
ec48c3a7
JR
7596
7597/* Clear any mouse-face on window W. This function is part of the
7598 redisplay interface, and is called from try_window_id and similar
7599 functions to ensure the mouse-highlight is off. */
7600
7601static void
7602x_clear_mouse_face (w)
7603 struct window *w;
7604{
7605 struct w32_display_info *dpyinfo
7606 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
7607 Lisp_Object window;
7608
9127e20e 7609 BLOCK_INPUT;
ec48c3a7
JR
7610 XSETWINDOW (window, w);
7611 if (EQ (window, dpyinfo->mouse_face_window))
7612 clear_mouse_face (dpyinfo);
9127e20e 7613 UNBLOCK_INPUT;
ec48c3a7
JR
7614}
7615
7616
31d4844a
KH
7617/* Just discard the mouse face information for frame F, if any.
7618 This is used when the size of F is changed. */
7619
7620void
7621cancel_mouse_face (f)
7622 FRAME_PTR f;
7623{
7624 Lisp_Object window;
7625 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7626
7627 window = dpyinfo->mouse_face_window;
7628 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7629 {
7630 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7631 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7632 dpyinfo->mouse_face_window = Qnil;
7633 }
7634}
ee78dc32 7635\f
9ef2e2cf 7636static struct scroll_bar *x_window_to_scroll_bar ();
ee78dc32 7637static void x_scroll_bar_report_motion ();
549808db
JR
7638static void x_check_fullscreen P_ ((struct frame *));
7639static void x_check_fullscreen_move P_ ((struct frame *));
9f5a911b
JR
7640static int glyph_rect P_ ((struct frame *f, int, int, RECT *));
7641
7642
7643/* Try to determine frame pixel position and size of the glyph under
7644 frame pixel coordinates X/Y on frame F . Return the position and
7645 size in *RECT. Value is non-zero if we could compute these
7646 values. */
7647
7648static int
7649glyph_rect (f, x, y, rect)
7650 struct frame *f;
7651 int x, y;
7652 RECT *rect;
7653{
7654 Lisp_Object window;
1e2eddb4 7655 int part;
9f5a911b
JR
7656
7657 window = window_from_coordinates (f, x, y, &part, 0);
7658 if (!NILP (window))
7659 {
7660 struct window *w = XWINDOW (window);
7661 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7662 struct glyph_row *end = r + w->current_matrix->nrows - 1;
9f5a911b
JR
7663
7664 frame_to_window_pixel_xy (w, &x, &y);
1e2eddb4
JR
7665
7666 for (; r < end && r->enabled_p; ++r)
7667 if (r->y <= y && r->y + r->height > y)
9f5a911b 7668 {
1e2eddb4 7669 /* Found the row at y. */
9f5a911b
JR
7670 struct glyph *g = r->glyphs[TEXT_AREA];
7671 struct glyph *end = g + r->used[TEXT_AREA];
7672 int gx;
1e2eddb4
JR
7673
7674 rect->top = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7675 rect->bottom = rect->top + r->height;
7676
7677 if (x < r->x)
7678 {
7679 /* x is to the left of the first glyph in the row. */
7680 rect->left = XINT (w->left);
7681 rect->right = WINDOW_TO_FRAME_PIXEL_X (w, r->x);
7682 return 1;
7683 }
7684
7685 for (gx = r->x; g < end; gx += g->pixel_width, ++g)
7686 if (gx <= x && gx + g->pixel_width > x)
9f5a911b 7687 {
1e2eddb4 7688 /* x is on a glyph. */
9f5a911b 7689 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
9f5a911b 7690 rect->right = rect->left + g->pixel_width;
1e2eddb4 7691 return 1;
9f5a911b 7692 }
1e2eddb4
JR
7693
7694 /* x is to the right of the last glyph in the row. */
7695 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7696 rect->right = XINT (w->left) + XINT (w->width);
7697 return 1;
9f5a911b
JR
7698 }
7699 }
7700
1e2eddb4
JR
7701 /* The y is not on any row. */
7702 return 0;
9f5a911b
JR
7703}
7704
7705/* Record the position of the mouse in last_mouse_glyph. */
565153ff
JR
7706static void
7707remember_mouse_glyph (f1, gx, gy)
9f5a911b
JR
7708 struct frame * f1;
7709 int gx, gy;
7710{
7711 if (!glyph_rect (f1, gx, gy, &last_mouse_glyph))
7712 {
7713 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7714 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7715
7716 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7717 round down even for negative values. */
7718 if (gx < 0)
7719 gx -= width - 1;
7720 if (gy < 0)
7721 gy -= height - 1;
7722#if 0
7723 /* This was the original code from XTmouse_position, but it seems
7724 to give the position of the glyph diagonally next to the one
7725 the mouse is over. */
7726 gx = (gx + width - 1) / width * width;
7727 gy = (gy + height - 1) / height * height;
7728#else
7729 gx = gx / width * width;
7730 gy = gy / height * height;
7731#endif
7732
7733 last_mouse_glyph.left = gx;
7734 last_mouse_glyph.top = gy;
7735 last_mouse_glyph.right = gx + width;
7736 last_mouse_glyph.bottom = gy + height;
7737 }
7738}
ee78dc32
GV
7739
7740/* Return the current position of the mouse.
7741 *fp should be a frame which indicates which display to ask about.
7742
7743 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7744 and *part to the frame, window, and scroll bar part that the mouse
7745 is over. Set *x and *y to the portion and whole of the mouse's
7746 position on the scroll bar.
7747
7748 If the mouse movement started elsewhere, set *fp to the frame the
7749 mouse is on, *bar_window to nil, and *x and *y to the character cell
7750 the mouse is over.
7751
9ef2e2cf 7752 Set *time to the server time-stamp for the time at which the mouse
ee78dc32
GV
7753 was at this position.
7754
7755 Don't store anything if we don't have a valid set of values to report.
7756
7757 This clears the mouse_moved flag, so we can wait for the next mouse
9ef2e2cf 7758 movement. */
ee78dc32
GV
7759
7760static void
fbd6baed 7761w32_mouse_position (fp, insist, bar_window, part, x, y, time)
ee78dc32
GV
7762 FRAME_PTR *fp;
7763 int insist;
7764 Lisp_Object *bar_window;
7765 enum scroll_bar_part *part;
7766 Lisp_Object *x, *y;
7767 unsigned long *time;
7768{
7769 FRAME_PTR f1;
7770
7771 BLOCK_INPUT;
7772
95fa970d 7773 if (! NILP (last_mouse_scroll_bar) && insist == 0)
ee78dc32
GV
7774 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7775 else
7776 {
7777 POINT pt;
7778
7779 Lisp_Object frame, tail;
7780
7781 /* Clear the mouse-moved flag for every frame on this display. */
7782 FOR_EACH_FRAME (tail, frame)
7783 XFRAME (frame)->mouse_moved = 0;
7784
7785 last_mouse_scroll_bar = Qnil;
d67d1627 7786
ee78dc32
GV
7787 GetCursorPos (&pt);
7788
7789 /* Now we have a position on the root; find the innermost window
7790 containing the pointer. */
7791 {
fbd6baed 7792 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
ee78dc32
GV
7793 && FRAME_LIVE_P (last_mouse_frame))
7794 {
08712a41
AI
7795 /* If mouse was grabbed on a frame, give coords for that frame
7796 even if the mouse is now outside it. */
ee78dc32
GV
7797 f1 = last_mouse_frame;
7798 }
7799 else
7800 {
08712a41 7801 /* Is window under mouse one of our frames? */
9f5a911b 7802 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
9127e20e 7803 WindowFromPoint (pt));
ee78dc32
GV
7804 }
7805
7806 /* If not, is it one of our scroll bars? */
7807 if (! f1)
7808 {
9127e20e
JR
7809 struct scroll_bar *bar
7810 = x_window_to_scroll_bar (WindowFromPoint (pt));
ee78dc32
GV
7811
7812 if (bar)
7813 {
7814 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7815 }
7816 }
7817
95fa970d 7818 if (f1 == 0 && insist > 0)
791f420f 7819 f1 = SELECTED_FRAME ();
ee78dc32
GV
7820
7821 if (f1)
7822 {
791f420f
JR
7823 /* Ok, we found a frame. Store all the values.
7824 last_mouse_glyph is a rectangle used to reduce the
7825 generation of mouse events. To not miss any motion
7826 events, we must divide the frame into rectangles of the
7827 size of the smallest character that could be displayed
7828 on it, i.e. into the same rectangles that matrices on
7829 the frame are divided into. */
7830
7831#if OLD_REDISPLAY_CODE
ee78dc32
GV
7832 int ignore1, ignore2;
7833
fbd6baed 7834 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
ee78dc32 7835
ee78dc32
GV
7836 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
7837 &last_mouse_glyph,
fbd6baed 7838 FRAME_W32_DISPLAY_INFO (f1)->grabbed
ee78dc32 7839 || insist);
791f420f
JR
7840#else
7841 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
9f5a911b 7842 remember_mouse_glyph (f1, pt.x, pt.y);
791f420f 7843#endif
ee78dc32
GV
7844
7845 *bar_window = Qnil;
7846 *part = 0;
7847 *fp = f1;
7848 XSETINT (*x, pt.x);
7849 XSETINT (*y, pt.y);
7850 *time = last_mouse_movement_time;
7851 }
7852 }
7853 }
7854
7855 UNBLOCK_INPUT;
7856}
791f420f 7857
ee78dc32
GV
7858\f
7859/* Scroll bar support. */
7860
ec48c3a7 7861/* Given a window ID, find the struct scroll_bar which manages it.
ee78dc32
GV
7862 This can be called in GC, so we have to make sure to strip off mark
7863 bits. */
9ef2e2cf
JR
7864
7865static struct scroll_bar *
ee78dc32
GV
7866x_window_to_scroll_bar (window_id)
7867 Window window_id;
7868{
791f420f 7869 Lisp_Object tail;
ee78dc32
GV
7870
7871 for (tail = Vframe_list;
7872 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7873 tail = XCDR (tail))
ee78dc32
GV
7874 {
7875 Lisp_Object frame, bar, condemned;
7876
8e713be6 7877 frame = XCAR (tail);
ee78dc32
GV
7878 /* All elements of Vframe_list should be frames. */
7879 if (! GC_FRAMEP (frame))
7880 abort ();
7881
7882 /* Scan this frame's scroll bar list for a scroll bar with the
7883 right window ID. */
7884 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7885 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7886 /* This trick allows us to search both the ordinary and
7887 condemned scroll bar lists with one loop. */
7888 ! GC_NILP (bar) || (bar = condemned,
7889 condemned = Qnil,
7890 ! GC_NILP (bar));
7891 bar = XSCROLL_BAR (bar)->next)
fbd6baed 7892 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
ee78dc32
GV
7893 return XSCROLL_BAR (bar);
7894 }
7895
7896 return 0;
7897}
7898
791f420f
JR
7899
7900\f
7901/* Set the thumb size and position of scroll bar BAR. We are currently
7902 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7903
7904static void
7905w32_set_scroll_bar_thumb (bar, portion, position, whole)
7906 struct scroll_bar *bar;
7907 int portion, position, whole;
7908{
7909 Window w = SCROLL_BAR_W32_WINDOW (bar);
d8e675f5 7910 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
791f420f
JR
7911 int sb_page, sb_pos;
7912 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
7913
7914 if (whole)
7915 {
7916 /* Position scroll bar at rock bottom if the bottom of the
7917 buffer is visible. This avoids shinking the thumb away
7918 to nothing if it is held at the bottom of the buffer. */
7919 if (position + portion >= whole)
7920 {
7921 sb_page = range * (whole - position) / whole
7922 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7923 sb_pos = range;
7924 }
7925
7926 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7927 sb_pos = position * range / whole;
7928 }
7929 else
7930 {
7931 sb_page = range;
7932 sb_pos = 0;
7933 }
7934
7935 BLOCK_INPUT;
7936
7937 if (pfnSetScrollInfo)
7938 {
7939 SCROLLINFO si;
7940
7941 si.cbSize = sizeof (si);
7942 /* Only update page size if currently dragging, to reduce
7943 flicker effects. */
7944 if (draggingp)
7945 si.fMask = SIF_PAGE;
7946 else
7947 si.fMask = SIF_PAGE | SIF_POS;
7948 si.nPage = sb_page;
7949 si.nPos = sb_pos;
7950
7951 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
7952 }
7953 else
7954 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
7955
7956 UNBLOCK_INPUT;
7957}
7958
7959\f
7960/************************************************************************
7961 Scroll bars, general
7962 ************************************************************************/
d67d1627
JB
7963
7964HWND
ee78dc32
GV
7965my_create_scrollbar (f, bar)
7966 struct frame * f;
7967 struct scroll_bar * bar;
7968{
689004fa 7969 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
d67d1627 7970 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
689004fa 7971 (LPARAM) bar);
ee78dc32
GV
7972}
7973
ab76d376 7974/*#define ATTACH_THREADS*/
52cf03a1 7975
689004fa
GV
7976BOOL
7977my_show_window (FRAME_PTR f, HWND hwnd, int how)
52cf03a1
GV
7978{
7979#ifndef ATTACH_THREADS
689004fa
GV
7980 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
7981 (WPARAM) hwnd, (LPARAM) how);
52cf03a1 7982#else
689004fa 7983 return ShowWindow (hwnd, how);
52cf03a1
GV
7984#endif
7985}
7986
7987void
7988my_set_window_pos (HWND hwnd, HWND hwndAfter,
689004fa 7989 int x, int y, int cx, int cy, UINT flags)
52cf03a1
GV
7990{
7991#ifndef ATTACH_THREADS
689004fa
GV
7992 WINDOWPOS pos;
7993 pos.hwndInsertAfter = hwndAfter;
52cf03a1
GV
7994 pos.x = x;
7995 pos.y = y;
7996 pos.cx = cx;
7997 pos.cy = cy;
7998 pos.flags = flags;
7999 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
8000#else
8001 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
8002#endif
8003}
8004
ec48c3a7 8005void
689004fa
GV
8006my_set_focus (f, hwnd)
8007 struct frame * f;
8008 HWND hwnd;
8009{
d67d1627 8010 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
689004fa
GV
8011 (WPARAM) hwnd, 0);
8012}
8013
ec48c3a7 8014void
ef0e360f
GV
8015my_set_foreground_window (hwnd)
8016 HWND hwnd;
8017{
8018 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
8019}
8020
ee78dc32
GV
8021void
8022my_destroy_window (f, hwnd)
8023 struct frame * f;
8024 HWND hwnd;
8025{
d67d1627 8026 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
ee78dc32
GV
8027 (WPARAM) hwnd, 0);
8028}
8029
791f420f
JR
8030/* Create a scroll bar and return the scroll bar vector for it. W is
8031 the Emacs window on which to create the scroll bar. TOP, LEFT,
8032 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8033 scroll bar. */
8034
ee78dc32 8035static struct scroll_bar *
791f420f
JR
8036x_scroll_bar_create (w, top, left, width, height)
8037 struct window *w;
ee78dc32
GV
8038 int top, left, width, height;
8039{
791f420f
JR
8040 struct frame *f = XFRAME (WINDOW_FRAME (w));
8041 HWND hwnd;
ee78dc32
GV
8042 struct scroll_bar *bar
8043 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
ee78dc32
GV
8044
8045 BLOCK_INPUT;
8046
791f420f 8047 XSETWINDOW (bar->window, w);
ee78dc32
GV
8048 XSETINT (bar->top, top);
8049 XSETINT (bar->left, left);
8050 XSETINT (bar->width, width);
8051 XSETINT (bar->height, height);
8052 XSETINT (bar->start, 0);
8053 XSETINT (bar->end, 0);
8054 bar->dragging = Qnil;
8055
8056 /* Requires geometry to be set before call to create the real window */
8057
8058 hwnd = my_create_scrollbar (f, bar);
8059
689004fa
GV
8060 if (pfnSetScrollInfo)
8061 {
8062 SCROLLINFO si;
8063
8064 si.cbSize = sizeof (si);
8065 si.fMask = SIF_ALL;
8066 si.nMin = 0;
791f420f
JR
8067 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
8068 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
689004fa
GV
8069 si.nPage = si.nMax;
8070 si.nPos = 0;
8071
8072 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
8073 }
8074 else
8075 {
791f420f
JR
8076 SetScrollRange (hwnd, SB_CTL, 0,
8077 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
689004fa
GV
8078 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
8079 }
ee78dc32 8080
fbd6baed 8081 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
ee78dc32
GV
8082
8083 /* Add bar to its frame's list of scroll bars. */
8084 bar->next = FRAME_SCROLL_BARS (f);
8085 bar->prev = Qnil;
8086 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8087 if (! NILP (bar->next))
8088 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8089
8090 UNBLOCK_INPUT;
8091
8092 return bar;
8093}
8094
ee78dc32 8095
791f420f
JR
8096/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8097 nil. */
ee78dc32 8098
ee78dc32
GV
8099static void
8100x_scroll_bar_remove (bar)
8101 struct scroll_bar *bar;
8102{
8103 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8104
8105 BLOCK_INPUT;
8106
8107 /* Destroy the window. */
fbd6baed 8108 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
ee78dc32
GV
8109
8110 /* Disassociate this scroll bar from its window. */
8111 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8112
8113 UNBLOCK_INPUT;
8114}
8115
8116/* Set the handle of the vertical scroll bar for WINDOW to indicate
8117 that we are displaying PORTION characters out of a total of WHOLE
8118 characters, starting at POSITION. If WINDOW has no scroll bar,
8119 create one. */
8120static void
791f420f
JR
8121w32_set_vertical_scroll_bar (w, portion, whole, position)
8122 struct window *w;
ee78dc32
GV
8123 int portion, whole, position;
8124{
791f420f 8125 struct frame *f = XFRAME (w->frame);
ee78dc32 8126 struct scroll_bar *bar;
9ef2e2cf 8127 int top, height, left, sb_left, width, sb_width;
791f420f
JR
8128 int window_x, window_y, window_width, window_height;
8129
8130 /* Get window dimensions. */
8131 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8132 top = window_y;
8133 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8134 height = window_height;
8135
8136 /* Compute the left edge of the scroll bar area. */
8137 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8138 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8139 else
8140 left = XFASTINT (w->left);
8141 left *= CANON_X_UNIT (f);
8142 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8143
8144 /* Compute the width of the scroll bar which might be less than
8145 the width of the area reserved for the scroll bar. */
8146 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8147 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8148 else
8149 sb_width = width;
ee78dc32 8150
791f420f
JR
8151 /* Compute the left edge of the scroll bar. */
8152 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
d67d1627 8153 sb_left = left + width - sb_width - (width - sb_width) / 2;
ee78dc32 8154 else
791f420f
JR
8155 sb_left = left + (width - sb_width) / 2;
8156
8157 /* Does the scroll bar exist yet? */
8158 if (NILP (w->vertical_scroll_bar))
ee78dc32 8159 {
00fe468b 8160 HDC hdc;
791f420f 8161 BLOCK_INPUT;
6ff3e5e3 8162 if (width > 0 && height > 0)
9f5a911b
JR
8163 {
8164 hdc = get_frame_dc (f);
8165 w32_clear_area (f, hdc, left, top, width, height);
8166 release_frame_dc (f, hdc);
8167 }
791f420f 8168 UNBLOCK_INPUT;
00fe468b 8169
791f420f 8170 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
ee78dc32 8171 }
791f420f
JR
8172 else
8173 {
d67d1627 8174 /* It may just need to be moved and resized. */
791f420f
JR
8175 HWND hwnd;
8176
8177 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8178 hwnd = SCROLL_BAR_W32_WINDOW (bar);
8179
8180 /* If already correctly positioned, do nothing. */
8181 if ( XINT (bar->left) == sb_left
8182 && XINT (bar->top) == top
8183 && XINT (bar->width) == sb_width
8184 && XINT (bar->height) == height )
8185 {
8186 /* Redraw after clear_frame. */
8187 if (!my_show_window (f, hwnd, SW_NORMAL))
8188 InvalidateRect (hwnd, NULL, FALSE);
8189 }
8190 else
8191 {
00fe468b 8192 HDC hdc;
791f420f 8193 BLOCK_INPUT;
9f5a911b
JR
8194 if (width && height)
8195 {
8196 hdc = get_frame_dc (f);
8197 /* Since Windows scroll bars are smaller than the space reserved
8198 for them on the frame, we have to clear "under" them. */
8199 w32_clear_area (f, hdc,
8200 left,
8201 top,
8202 width,
8203 height);
8204 release_frame_dc (f, hdc);
8205 }
791f420f
JR
8206 /* Make sure scroll bar is "visible" before moving, to ensure the
8207 area of the parent window now exposed will be refreshed. */
8208 my_show_window (f, hwnd, SW_HIDE);
9f5a911b
JR
8209 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8210 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8211 max (height, 1), TRUE);
791f420f
JR
8212 if (pfnSetScrollInfo)
8213 {
8214 SCROLLINFO si;
ee78dc32 8215
791f420f
JR
8216 si.cbSize = sizeof (si);
8217 si.fMask = SIF_RANGE;
8218 si.nMin = 0;
8219 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
8220 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
ee78dc32 8221
00fe468b 8222 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
791f420f
JR
8223 }
8224 else
00fe468b 8225 SetScrollRange (hwnd, SB_CTL, 0,
791f420f
JR
8226 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
8227 my_show_window (f, hwnd, SW_NORMAL);
ab76d376 8228 /* InvalidateRect (w, NULL, FALSE); */
791f420f
JR
8229
8230 /* Remember new settings. */
8231 XSETINT (bar->left, sb_left);
8232 XSETINT (bar->top, top);
8233 XSETINT (bar->width, sb_width);
8234 XSETINT (bar->height, height);
8235
8236 UNBLOCK_INPUT;
8237 }
8238 }
8239 w32_set_scroll_bar_thumb (bar, portion, position, whole);
ee78dc32 8240
791f420f 8241 XSETVECTOR (w->vertical_scroll_bar, bar);
ee78dc32
GV
8242}
8243
8244
8245/* The following three hooks are used when we're doing a thorough
8246 redisplay of the frame. We don't explicitly know which scroll bars
8247 are going to be deleted, because keeping track of when windows go
8248 away is a real pain - "Can you say set-window-configuration, boys
8249 and girls?" Instead, we just assert at the beginning of redisplay
8250 that *all* scroll bars are to be removed, and then save a scroll bar
8251 from the fiery pit when we actually redisplay its window. */
8252
8253/* Arrange for all scroll bars on FRAME to be removed at the next call
8254 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9ef2e2cf
JR
8255 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8256
ee78dc32 8257static void
fbd6baed 8258w32_condemn_scroll_bars (frame)
ee78dc32
GV
8259 FRAME_PTR frame;
8260{
ef0e360f
GV
8261 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8262 while (! NILP (FRAME_SCROLL_BARS (frame)))
8263 {
8264 Lisp_Object bar;
8265 bar = FRAME_SCROLL_BARS (frame);
8266 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8267 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8268 XSCROLL_BAR (bar)->prev = Qnil;
8269 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8270 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8271 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8272 }
ee78dc32
GV
8273}
8274
c2cc16fa 8275
9ef2e2cf 8276/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
ee78dc32 8277 Note that WINDOW isn't necessarily condemned at all. */
c2cc16fa 8278
ee78dc32 8279static void
fbd6baed 8280w32_redeem_scroll_bar (window)
ee78dc32
GV
8281 struct window *window;
8282{
8283 struct scroll_bar *bar;
c2cc16fa 8284 struct frame *f;
ee78dc32
GV
8285
8286 /* We can't redeem this window's scroll bar if it doesn't have one. */
8287 if (NILP (window->vertical_scroll_bar))
8288 abort ();
8289
8290 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8291
ef0e360f 8292 /* Unlink it from the condemned list. */
c2cc16fa
JR
8293 f = XFRAME (WINDOW_FRAME (window));
8294 if (NILP (bar->prev))
8295 {
8296 /* If the prev pointer is nil, it must be the first in one of
8297 the lists. */
8298 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8299 /* It's not condemned. Everything's fine. */
8300 return;
8301 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8302 window->vertical_scroll_bar))
8303 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8304 else
8305 /* If its prev pointer is nil, it must be at the front of
8306 one or the other! */
8307 abort ();
8308 }
8309 else
8310 XSCROLL_BAR (bar->prev)->next = bar->next;
ef0e360f 8311
c2cc16fa
JR
8312 if (! NILP (bar->next))
8313 XSCROLL_BAR (bar->next)->prev = bar->prev;
ef0e360f 8314
c2cc16fa
JR
8315 bar->next = FRAME_SCROLL_BARS (f);
8316 bar->prev = Qnil;
8317 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8318 if (! NILP (bar->next))
8319 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
ee78dc32
GV
8320}
8321
8322/* Remove all scroll bars on FRAME that haven't been saved since the
8323 last call to `*condemn_scroll_bars_hook'. */
9ef2e2cf 8324
ee78dc32 8325static void
fbd6baed 8326w32_judge_scroll_bars (f)
ee78dc32
GV
8327 FRAME_PTR f;
8328{
8329 Lisp_Object bar, next;
8330
8331 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8332
8333 /* Clear out the condemned list now so we won't try to process any
8334 more events on the hapless scroll bars. */
8335 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8336
8337 for (; ! NILP (bar); bar = next)
8338 {
8339 struct scroll_bar *b = XSCROLL_BAR (bar);
8340
8341 x_scroll_bar_remove (b);
8342
8343 next = b->next;
8344 b->next = b->prev = Qnil;
8345 }
8346
8347 /* Now there should be no references to the condemned scroll bars,
8348 and they should get garbage-collected. */
8349}
8350
8351/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3b8f9651 8352 is set to something other than NO_EVENT, it is enqueued.
ee78dc32
GV
8353
8354 This may be called from a signal handler, so we have to ignore GC
8355 mark bits. */
2c28562d 8356
52cf03a1 8357static int
c2cc16fa 8358w32_scroll_bar_handle_click (bar, msg, emacs_event)
ee78dc32 8359 struct scroll_bar *bar;
fbd6baed 8360 W32Msg *msg;
ee78dc32
GV
8361 struct input_event *emacs_event;
8362{
8363 if (! GC_WINDOWP (bar->window))
8364 abort ();
8365
3b8f9651 8366 emacs_event->kind = W32_SCROLL_BAR_CLICK_EVENT;
ee78dc32 8367 emacs_event->code = 0;
52cf03a1
GV
8368 /* not really meaningful to distinguish up/down */
8369 emacs_event->modifiers = msg->dwModifiers;
ee78dc32 8370 emacs_event->frame_or_window = bar->window;
7e889510 8371 emacs_event->arg = Qnil;
ee78dc32
GV
8372 emacs_event->timestamp = msg->msg.time;
8373
8374 {
791f420f 8375 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
689004fa
GV
8376 int y;
8377 int dragging = !NILP (bar->dragging);
8378
8379 if (pfnGetScrollInfo)
8380 {
8381 SCROLLINFO si;
8382
8383 si.cbSize = sizeof (si);
8384 si.fMask = SIF_POS;
8385
8386 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
8387 y = si.nPos;
8388 }
8389 else
8390 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
8391
8392 bar->dragging = Qnil;
ee78dc32 8393
9ef2e2cf
JR
8394
8395 last_mouse_scroll_bar_pos = msg->msg.wParam;
d67d1627 8396
ee78dc32 8397 switch (LOWORD (msg->msg.wParam))
2c28562d 8398 {
2c28562d 8399 case SB_LINEDOWN:
52cf03a1 8400 emacs_event->part = scroll_bar_down_arrow;
ee78dc32 8401 break;
2c28562d 8402 case SB_LINEUP:
52cf03a1 8403 emacs_event->part = scroll_bar_up_arrow;
ee78dc32 8404 break;
2c28562d 8405 case SB_PAGEUP:
ee78dc32
GV
8406 emacs_event->part = scroll_bar_above_handle;
8407 break;
2c28562d 8408 case SB_PAGEDOWN:
ee78dc32
GV
8409 emacs_event->part = scroll_bar_below_handle;
8410 break;
2c28562d 8411 case SB_TOP:
ee78dc32
GV
8412 emacs_event->part = scroll_bar_handle;
8413 y = 0;
8414 break;
2c28562d 8415 case SB_BOTTOM:
ee78dc32
GV
8416 emacs_event->part = scroll_bar_handle;
8417 y = top_range;
8418 break;
689004fa 8419 case SB_THUMBTRACK:
2c28562d 8420 case SB_THUMBPOSITION:
791f420f
JR
8421 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
8422 y = HIWORD (msg->msg.wParam);
689004fa 8423 bar->dragging = Qt;
ee78dc32 8424 emacs_event->part = scroll_bar_handle;
689004fa
GV
8425
8426 /* "Silently" update current position. */
8427 if (pfnSetScrollInfo)
8428 {
8429 SCROLLINFO si;
8430
8431 si.cbSize = sizeof (si);
8432 si.fMask = SIF_POS;
689004fa
GV
8433 si.nPos = y;
8434 /* Remember apparent position (we actually lag behind the real
8435 position, so don't set that directly. */
8436 last_scroll_bar_drag_pos = y;
8437
8438 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
8439 }
8440 else
8441 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
ee78dc32 8442 break;
2c28562d 8443 case SB_ENDSCROLL:
689004fa
GV
8444 /* If this is the end of a drag sequence, then reset the scroll
8445 handle size to normal and do a final redraw. Otherwise do
8446 nothing. */
8447 if (dragging)
8448 {
8449 if (pfnSetScrollInfo)
8450 {
8451 SCROLLINFO si;
8452 int start = XINT (bar->start);
8453 int end = XINT (bar->end);
8454
8455 si.cbSize = sizeof (si);
8456 si.fMask = SIF_PAGE | SIF_POS;
791f420f 8457 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
689004fa 8458 si.nPos = last_scroll_bar_drag_pos;
689004fa
GV
8459 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
8460 }
8461 else
8462 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
8463 }
8464 /* fall through */
2c28562d 8465 default:
3b8f9651 8466 emacs_event->kind = NO_EVENT;
52cf03a1 8467 return FALSE;
2c28562d 8468 }
52cf03a1 8469
ee78dc32
GV
8470 XSETINT (emacs_event->x, y);
8471 XSETINT (emacs_event->y, top_range);
52cf03a1
GV
8472
8473 return TRUE;
ee78dc32
GV
8474 }
8475}
8476
8477/* Return information to the user about the current position of the mouse
8478 on the scroll bar. */
9ef2e2cf 8479
ee78dc32
GV
8480static void
8481x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8482 FRAME_PTR *fp;
8483 Lisp_Object *bar_window;
8484 enum scroll_bar_part *part;
8485 Lisp_Object *x, *y;
8486 unsigned long *time;
8487{
8488 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
fbd6baed 8489 Window w = SCROLL_BAR_W32_WINDOW (bar);
ee78dc32
GV
8490 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8491 int pos;
791f420f 8492 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ee78dc32
GV
8493
8494 BLOCK_INPUT;
8495
8496 *fp = f;
8497 *bar_window = bar->window;
8498
689004fa
GV
8499 if (pfnGetScrollInfo)
8500 {
8501 SCROLLINFO si;
8502
8503 si.cbSize = sizeof (si);
8504 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
8505
8506 pfnGetScrollInfo (w, SB_CTL, &si);
8507 pos = si.nPos;
8508 top_range = si.nMax - si.nPage + 1;
8509 }
8510 else
8511 pos = GetScrollPos (w, SB_CTL);
ee78dc32
GV
8512
8513 switch (LOWORD (last_mouse_scroll_bar_pos))
8514 {
8515 case SB_THUMBPOSITION:
8516 case SB_THUMBTRACK:
8517 *part = scroll_bar_handle;
791f420f 8518 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
ee78dc32
GV
8519 pos = HIWORD (last_mouse_scroll_bar_pos);
8520 break;
8521 case SB_LINEDOWN:
8522 *part = scroll_bar_handle;
8523 pos++;
8524 break;
8525 default:
8526 *part = scroll_bar_handle;
8527 break;
8528 }
8529
9127e20e
JR
8530 XSETINT (*x, pos);
8531 XSETINT (*y, top_range);
ee78dc32
GV
8532
8533 f->mouse_moved = 0;
8534 last_mouse_scroll_bar = Qnil;
8535
8536 *time = last_mouse_movement_time;
8537
8538 UNBLOCK_INPUT;
8539}
8540
9ef2e2cf 8541
ee78dc32
GV
8542/* The screen has been cleared so we may have changed foreground or
8543 background colors, and the scroll bars may need to be redrawn.
8544 Clear out the scroll bars, and ask for expose events, so we can
8545 redraw them. */
9ef2e2cf 8546
791f420f 8547void
ee78dc32 8548x_scroll_bar_clear (f)
9ef2e2cf 8549 FRAME_PTR f;
ee78dc32 8550{
ee78dc32
GV
8551 Lisp_Object bar;
8552
9ef2e2cf
JR
8553 /* We can have scroll bars even if this is 0,
8554 if we just turned off scroll bar mode.
8555 But in that case we should not clear them. */
8556 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8557 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8558 bar = XSCROLL_BAR (bar)->next)
8559 {
8560 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
8561 HDC hdc = GetDC (window);
8562 RECT rect;
52cf03a1 8563
9ef2e2cf
JR
8564 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
8565 arranges to refresh the scroll bar if hidden. */
8566 my_show_window (f, window, SW_HIDE);
689004fa 8567
9ef2e2cf
JR
8568 GetClientRect (window, &rect);
8569 select_palette (f, hdc);
8570 w32_clear_rect (f, hdc, &rect);
8571 deselect_palette (f, hdc);
689004fa 8572
9ef2e2cf
JR
8573 ReleaseDC (window, hdc);
8574 }
52cf03a1
GV
8575}
8576
ee78dc32 8577\f
fbd6baed 8578/* The main W32 event-reading loop - w32_read_socket. */
ee78dc32
GV
8579
8580/* Record the last 100 characters stored
8581 to help debug the loss-of-chars-during-GC problem. */
9ef2e2cf
JR
8582
8583static int temp_index;
8584static short temp_buffer[100];
ee78dc32 8585
afd153f0 8586
fbd6baed 8587/* Read events coming from the W32 shell.
ee78dc32
GV
8588 This routine is called by the SIGIO handler.
8589 We return as soon as there are no more events to be read.
8590
8591 Events representing keys are stored in buffer BUFP,
8592 which can hold up to NUMCHARS characters.
8593 We return the number of characters stored into the buffer,
8594 thus pretending to be `read'.
8595
d67d1627 8596 EXPECTED is nonzero if the caller knows input is available.
ee78dc32
GV
8597
8598 Some of these messages are reposted back to the message queue since the
d67d1627 8599 system calls the windows proc directly in a context where we cannot return
e9e23e23 8600 the data nor can we guarantee the state we are in. So if we dispatch them
ee78dc32
GV
8601 we will get into an infinite loop. To prevent this from ever happening we
8602 will set a variable to indicate we are in the read_socket call and indicate
d67d1627 8603 which message we are processing since the windows proc gets called
e9e23e23 8604 recursively with different messages by the system.
ee78dc32
GV
8605*/
8606
8607int
3aad7613 8608w32_read_socket (sd, bufp, numchars, expected)
ee78dc32 8609 register int sd;
9ef2e2cf
JR
8610 /* register */ struct input_event *bufp;
8611 /* register */ int numchars;
ee78dc32
GV
8612 int expected;
8613{
8614 int count = 0;
689004fa 8615 int check_visibility = 0;
fbd6baed 8616 W32Msg msg;
ee78dc32 8617 struct frame *f;
fbd6baed 8618 struct w32_display_info *dpyinfo = &one_w32_display_info;
ee78dc32
GV
8619
8620 if (interrupt_input_blocked)
8621 {
8622 interrupt_input_pending = 1;
8623 return -1;
8624 }
8625
8626 interrupt_input_pending = 0;
8627 BLOCK_INPUT;
8628
8629 /* So people can tell when we have read the available input. */
8630 input_signal_count++;
8631
8632 if (numchars <= 0)
8633 abort (); /* Don't think this happens. */
8634
b71b8111 8635 /* TODO: tool-bars, ghostscript integration, mouse
791f420f 8636 cursors. */
52cf03a1 8637 while (get_next_msg (&msg, FALSE))
ee78dc32
GV
8638 {
8639 switch (msg.msg.message)
8640 {
ee78dc32 8641 case WM_PAINT:
9f5a911b 8642 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
ee78dc32 8643
d67d1627 8644 if (f)
9f5a911b
JR
8645 {
8646 if (msg.rect.right == msg.rect.left ||
8647 msg.rect.bottom == msg.rect.top)
8648 {
8649 /* We may get paint messages even though the client
8650 area is clipped - these are not expose events. */
8651 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
d5db4077 8652 SDATA (f->name)));
9f5a911b
JR
8653 }
8654 else if (f->async_visible != 1)
8655 {
8656 /* Definitely not obscured, so mark as visible. */
8657 f->async_visible = 1;
8658 f->async_iconified = 0;
8659 SET_FRAME_GARBAGED (f);
8660 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
d5db4077 8661 SDATA (f->name)));
9f5a911b
JR
8662
8663 /* WM_PAINT serves as MapNotify as well, so report
8664 visibility changes properly. */
8665 if (f->iconified)
8666 {
3b8f9651 8667 bufp->kind = DEICONIFY_EVENT;
9f5a911b
JR
8668 XSETFRAME (bufp->frame_or_window, f);
8669 bufp->arg = Qnil;
8670 bufp++;
8671 count++;
8672 numchars--;
8673 }
8674 else if (! NILP (Vframe_list)
8675 && ! NILP (XCDR (Vframe_list)))
8676 /* Force a redisplay sooner or later to update the
8677 frame titles in case this is the second frame. */
8678 record_asynch_buffer_change ();
8679 }
8680 else
8681 {
8682 HDC hdc = get_frame_dc (f);
8683
8684 /* Erase background again for safety. */
8685 w32_clear_rect (f, hdc, &msg.rect);
8686 release_frame_dc (f, hdc);
8687 expose_frame (f,
8688 msg.rect.left,
8689 msg.rect.top,
8690 msg.rect.right - msg.rect.left,
8691 msg.rect.bottom - msg.rect.top);
8692 }
8693 }
52cf03a1 8694 break;
689004fa 8695
f98169a0
GV
8696 case WM_INPUTLANGCHANGE:
8697 /* Generate a language change event. */
8698 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8699
8700 if (f)
8701 {
8702 if (numchars == 0)
8703 abort ();
d67d1627 8704
3b8f9651 8705 bufp->kind = LANGUAGE_CHANGE_EVENT;
f98169a0 8706 XSETFRAME (bufp->frame_or_window, f);
7e889510 8707 bufp->arg = Qnil;
f98169a0
GV
8708 bufp->code = msg.msg.wParam;
8709 bufp->modifiers = msg.msg.lParam & 0xffff;
8710 bufp++;
8711 count++;
8712 numchars--;
8713 }
8714 break;
8715
ee78dc32
GV
8716 case WM_KEYDOWN:
8717 case WM_SYSKEYDOWN:
8718 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 8719
ee78dc32
GV
8720 if (f && !f->iconified)
8721 {
3d26a7c2
KS
8722 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
8723 {
8724 dpyinfo->mouse_face_hidden = 1;
8725 clear_mouse_face (dpyinfo);
8726 }
8727
ee78dc32
GV
8728 if (temp_index == sizeof temp_buffer / sizeof (short))
8729 temp_index = 0;
8730 temp_buffer[temp_index++] = msg.msg.wParam;
3b8f9651 8731 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
ee78dc32 8732 bufp->code = msg.msg.wParam;
f98169a0 8733 bufp->modifiers = msg.dwModifiers;
ee78dc32 8734 XSETFRAME (bufp->frame_or_window, f);
7e889510 8735 bufp->arg = Qnil;
ee78dc32
GV
8736 bufp->timestamp = msg.msg.time;
8737 bufp++;
8738 numchars--;
8739 count++;
8740 }
8741 break;
689004fa 8742
ee78dc32
GV
8743 case WM_SYSCHAR:
8744 case WM_CHAR:
8745 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 8746
ee78dc32
GV
8747 if (f && !f->iconified)
8748 {
3d26a7c2
KS
8749 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
8750 {
8751 dpyinfo->mouse_face_hidden = 1;
8752 clear_mouse_face (dpyinfo);
8753 }
8754
f98169a0
GV
8755 if (temp_index == sizeof temp_buffer / sizeof (short))
8756 temp_index = 0;
8757 temp_buffer[temp_index++] = msg.msg.wParam;
3b8f9651 8758 bufp->kind = ASCII_KEYSTROKE_EVENT;
f98169a0
GV
8759 bufp->code = msg.msg.wParam;
8760 bufp->modifiers = msg.dwModifiers;
8761 XSETFRAME (bufp->frame_or_window, f);
7e889510 8762 bufp->arg = Qnil;
f98169a0
GV
8763 bufp->timestamp = msg.msg.time;
8764 bufp++;
8765 numchars--;
8766 count++;
ee78dc32
GV
8767 }
8768 break;
689004fa 8769
ee78dc32 8770 case WM_MOUSEMOVE:
706ddb8f
JR
8771 /* Ignore non-movement. */
8772 {
8773 int x = LOWORD (msg.msg.lParam);
8774 int y = HIWORD (msg.msg.lParam);
8775 if (x == last_mousemove_x && y == last_mousemove_y)
8776 break;
8777 last_mousemove_x = x;
8778 last_mousemove_y = y;
8779 }
8780
791f420f 8781 previous_help_echo = help_echo;
791f420f 8782
ee78dc32
GV
8783 if (dpyinfo->grabbed && last_mouse_frame
8784 && FRAME_LIVE_P (last_mouse_frame))
8785 f = last_mouse_frame;
8786 else
8787 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 8788
3d26a7c2
KS
8789 if (dpyinfo->mouse_face_hidden)
8790 {
8791 dpyinfo->mouse_face_hidden = 0;
8792 clear_mouse_face (dpyinfo);
8793 }
8794
ee78dc32
GV
8795 if (f)
8796 note_mouse_movement (f, &msg.msg);
8797 else
791f420f
JR
8798 {
8799 /* If we move outside the frame, then we're
8800 certainly no longer on any text in the frame. */
5b844253 8801 clear_mouse_face (dpyinfo);
791f420f
JR
8802 }
8803
8804 /* If the contents of the global variable help_echo
8805 has changed, generate a HELP_EVENT. */
706ddb8f
JR
8806 if (help_echo != previous_help_echo ||
8807 (!NILP (help_echo) && !STRINGP (help_echo) && f->mouse_moved))
791f420f
JR
8808 {
8809 Lisp_Object frame;
ec48c3a7 8810 int n;
791f420f 8811
706ddb8f
JR
8812 if (help_echo == Qnil)
8813 {
8814 help_echo_object = help_echo_window = Qnil;
8815 help_echo_pos = -1;
8816 }
8817
791f420f
JR
8818 if (f)
8819 XSETFRAME (frame, f);
8820 else
8821 frame = Qnil;
8822
8823 any_help_event_p = 1;
b4165314
GM
8824 n = gen_help_event (bufp, numchars, help_echo, frame,
8825 help_echo_window, help_echo_object,
8826 help_echo_pos);
ec48c3a7 8827 bufp += n, count += n, numchars -= n;
791f420f
JR
8828 }
8829 break;
689004fa 8830
ee78dc32
GV
8831 case WM_LBUTTONDOWN:
8832 case WM_LBUTTONUP:
8833 case WM_MBUTTONDOWN:
8834 case WM_MBUTTONUP:
8835 case WM_RBUTTONDOWN:
8836 case WM_RBUTTONUP:
53823ab9
JR
8837 case WM_XBUTTONDOWN:
8838 case WM_XBUTTONUP:
ee78dc32 8839 {
791f420f
JR
8840 /* If we decide we want to generate an event to be seen
8841 by the rest of Emacs, we put it here. */
8842 struct input_event emacs_event;
8843 int tool_bar_p = 0;
ee78dc32
GV
8844 int button;
8845 int up;
791f420f 8846
3b8f9651 8847 emacs_event.kind = NO_EVENT;
d67d1627 8848
ee78dc32
GV
8849 if (dpyinfo->grabbed && last_mouse_frame
8850 && FRAME_LIVE_P (last_mouse_frame))
8851 f = last_mouse_frame;
8852 else
8853 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 8854
ee78dc32
GV
8855 if (f)
8856 {
791f420f 8857 construct_mouse_click (&emacs_event, &msg, f);
d67d1627 8858
791f420f
JR
8859 /* Is this in the tool-bar? */
8860 if (WINDOWP (f->tool_bar_window)
8861 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
8862 {
8863 Lisp_Object window;
e81916d1
JR
8864 int p, x, y;
8865
8866 x = XFASTINT (emacs_event.x);
8867 y = XFASTINT (emacs_event.y);
791f420f
JR
8868
8869 /* Set x and y. */
b8b47c19
JR
8870 window = window_from_coordinates (f, x, y, &p, 1);
8871
791f420f
JR
8872 if (EQ (window, f->tool_bar_window))
8873 {
8874 w32_handle_tool_bar_click (f, &emacs_event);
8875 tool_bar_p = 1;
8876 }
8877 }
8878
8879 if (!tool_bar_p)
8880 if (!dpyinfo->w32_focus_frame
8881 || f == dpyinfo->w32_focus_frame
8882 && (numchars >= 1))
8883 {
8884 construct_mouse_click (bufp, &msg, f);
8885 bufp++;
8886 count++;
8887 numchars--;
8888 }
ee78dc32 8889 }
d67d1627 8890
53823ab9
JR
8891 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
8892 &button, &up);
8893
ee78dc32
GV
8894 if (up)
8895 {
8896 dpyinfo->grabbed &= ~ (1 << button);
8897 }
8898 else
8899 {
8900 dpyinfo->grabbed |= (1 << button);
8901 last_mouse_frame = f;
791f420f
JR
8902 /* Ignore any mouse motion that happened
8903 before this event; any subsequent mouse-movement
8904 Emacs events should reflect only motion after
8905 the ButtonPress. */
8906 if (f != 0)
8907 f->mouse_moved = 0;
8908
8909 if (!tool_bar_p)
8910 last_tool_bar_item = -1;
ee78dc32 8911 }
689004fa 8912 break;
ee78dc32 8913 }
d67d1627 8914
689004fa
GV
8915 case WM_MOUSEWHEEL:
8916 if (dpyinfo->grabbed && last_mouse_frame
8917 && FRAME_LIVE_P (last_mouse_frame))
8918 f = last_mouse_frame;
8919 else
8920 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8921
8922 if (f)
8923 {
d67d1627 8924 if ((!dpyinfo->w32_focus_frame
689004fa
GV
8925 || f == dpyinfo->w32_focus_frame)
8926 && (numchars >= 1))
8927 {
8928 construct_mouse_wheel (bufp, &msg, f);
8929 bufp++;
8930 count++;
8931 numchars--;
8932 }
8933 }
ee78dc32 8934 break;
689004fa 8935
12857dfd
RS
8936 case WM_DROPFILES:
8937 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8938
8939 if (f)
8940 {
8941 construct_drag_n_drop (bufp, &msg, f);
8942 bufp++;
8943 count++;
8944 numchars--;
8945 }
8946 break;
8947
ee78dc32
GV
8948 case WM_VSCROLL:
8949 {
689004fa
GV
8950 struct scroll_bar *bar =
8951 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
d67d1627 8952
ee78dc32
GV
8953 if (bar && numchars >= 1)
8954 {
c2cc16fa 8955 if (w32_scroll_bar_handle_click (bar, &msg, bufp))
52cf03a1
GV
8956 {
8957 bufp++;
8958 count++;
8959 numchars--;
8960 }
ee78dc32 8961 }
689004fa 8962 break;
ee78dc32 8963 }
d67d1627 8964
689004fa 8965 case WM_WINDOWPOSCHANGED:
549808db
JR
8966 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8967 if (f)
8968 {
8969 x_check_fullscreen_move(f);
8970 if (f->output_data.w32->want_fullscreen & FULLSCREEN_WAIT)
8971 f->output_data.w32->want_fullscreen &=
8972 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
8973 }
8974 check_visibility = 1;
8975 break;
8976
689004fa
GV
8977 case WM_ACTIVATE:
8978 case WM_ACTIVATEAPP:
549808db
JR
8979 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8980 if (f)
8981 x_check_fullscreen (f);
689004fa 8982 check_visibility = 1;
ee78dc32 8983 break;
689004fa 8984
ee78dc32
GV
8985 case WM_MOVE:
8986 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 8987
ee78dc32
GV
8988 if (f && !f->async_iconified)
8989 {
689004fa
GV
8990 int x, y;
8991
8992 x_real_positions (f, &x, &y);
8993 f->output_data.w32->left_pos = x;
8994 f->output_data.w32->top_pos = y;
ee78dc32 8995 }
689004fa
GV
8996
8997 check_visibility = 1;
8998 break;
8999
9000 case WM_SHOWWINDOW:
b71b8111
JR
9001 /* wParam non-zero means Window is about to be shown, 0 means
9002 about to be hidden. */
9003 /* Redo the mouse-highlight after the tooltip has gone. */
9004 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
9005 {
9006 tip_window = NULL;
9007 redo_mouse_highlight ();
9008 }
9009
689004fa
GV
9010 /* If window has been obscured or exposed by another window
9011 being maximised or minimised/restored, then recheck
9012 visibility of all frames. Direct changes to our own
9013 windows get handled by WM_SIZE. */
9014#if 0
9015 if (msg.msg.lParam != 0)
9016 check_visibility = 1;
9017 else
9018 {
9019 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9020 f->async_visible = msg.msg.wParam;
9021 }
9022#endif
9023
9024 check_visibility = 1;
ee78dc32 9025 break;
689004fa 9026
ee78dc32
GV
9027 case WM_SIZE:
9028 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 9029
689004fa
GV
9030 /* Inform lisp of whether frame has been iconified etc. */
9031 if (f)
ee78dc32 9032 {
689004fa 9033 switch (msg.msg.wParam)
ee78dc32 9034 {
689004fa
GV
9035 case SIZE_MINIMIZED:
9036 f->async_visible = 0;
ee78dc32 9037 f->async_iconified = 1;
d67d1627 9038
3b8f9651 9039 bufp->kind = ICONIFY_EVENT;
ee78dc32 9040 XSETFRAME (bufp->frame_or_window, f);
7e889510 9041 bufp->arg = Qnil;
ee78dc32
GV
9042 bufp++;
9043 count++;
9044 numchars--;
689004fa
GV
9045 break;
9046
9047 case SIZE_MAXIMIZED:
9048 case SIZE_RESTORED:
ee78dc32
GV
9049 f->async_visible = 1;
9050 f->async_iconified = 0;
d67d1627 9051
ee78dc32
GV
9052 /* wait_reading_process_input will notice this and update
9053 the frame's display structures. */
9054 SET_FRAME_GARBAGED (f);
d67d1627 9055
ee78dc32
GV
9056 if (f->iconified)
9057 {
31d4844a
KH
9058 int x, y;
9059
9060 /* Reset top and left positions of the Window
9061 here since Windows sends a WM_MOVE message
9062 BEFORE telling us the Window is minimized
9063 when the Window is iconified, with 3000,3000
9064 as the co-ords. */
9065 x_real_positions (f, &x, &y);
9066 f->output_data.w32->left_pos = x;
9067 f->output_data.w32->top_pos = y;
9068
3b8f9651 9069 bufp->kind = DEICONIFY_EVENT;
ee78dc32 9070 XSETFRAME (bufp->frame_or_window, f);
7e889510 9071 bufp->arg = Qnil;
ee78dc32
GV
9072 bufp++;
9073 count++;
9074 numchars--;
9075 }
9ef2e2cf
JR
9076 else if (! NILP (Vframe_list)
9077 && ! NILP (XCDR (Vframe_list)))
ee78dc32
GV
9078 /* Force a redisplay sooner or later
9079 to update the frame titles
9080 in case this is the second frame. */
9081 record_asynch_buffer_change ();
689004fa 9082 break;
ee78dc32 9083 }
ee78dc32 9084 }
689004fa 9085
ef0e360f
GV
9086 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
9087 {
9088 RECT rect;
9089 int rows;
9090 int columns;
9091 int width;
9092 int height;
d67d1627 9093
9127e20e 9094 GetClientRect (msg.msg.hwnd, &rect);
d67d1627 9095
ef0e360f
GV
9096 height = rect.bottom - rect.top;
9097 width = rect.right - rect.left;
d67d1627 9098
ef0e360f
GV
9099 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
9100 columns = PIXEL_TO_CHAR_WIDTH (f, width);
d67d1627 9101
ef0e360f 9102 /* TODO: Clip size to the screen dimensions. */
d67d1627 9103
ef0e360f
GV
9104 /* Even if the number of character rows and columns has
9105 not changed, the font size may have changed, so we need
9106 to check the pixel dimensions as well. */
d67d1627 9107
ef0e360f
GV
9108 if (columns != f->width
9109 || rows != f->height
9110 || width != f->output_data.w32->pixel_width
9111 || height != f->output_data.w32->pixel_height)
9112 {
791f420f 9113 change_frame_size (f, rows, columns, 0, 1, 0);
ef0e360f 9114 SET_FRAME_GARBAGED (f);
31d4844a 9115 cancel_mouse_face (f);
ef0e360f
GV
9116 f->output_data.w32->pixel_width = width;
9117 f->output_data.w32->pixel_height = height;
9118 f->output_data.w32->win_gravity = NorthWestGravity;
9119 }
9120 }
9121
689004fa
GV
9122 check_visibility = 1;
9123 break;
9124
1576fbfa
JR
9125 case WM_MOUSELEAVE:
9126 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
9127 if (f)
9128 {
9129 if (f == dpyinfo->mouse_face_mouse_frame)
9130 {
9131 /* If we move outside the frame, then we're
9132 certainly no longer on any text in the frame. */
9133 clear_mouse_face (dpyinfo);
9134 dpyinfo->mouse_face_mouse_frame = 0;
9135 }
9136
9137 /* Generate a nil HELP_EVENT to cancel a help-echo.
9138 Do it only if there's something to cancel.
9139 Otherwise, the startup message is cleared when
9140 the mouse leaves the frame. */
9141 if (any_help_event_p)
9142 {
9143 Lisp_Object frame;
9144 int n;
9145
9146 XSETFRAME (frame, f);
9147 help_echo = Qnil;
9148 n = gen_help_event (bufp, numchars,
9149 Qnil, frame, Qnil, Qnil, 0);
9150 bufp += n, count += n, numchars -= n;
9151 }
9152 }
9153 break;
d67d1627 9154
689004fa 9155 case WM_SETFOCUS:
791f420f
JR
9156 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
9157
9158 dpyinfo->w32_focus_event_frame = f;
d67d1627 9159
791f420f
JR
9160 if (f)
9161 x_new_focus_frame (dpyinfo, f);
9162
9163
9164 dpyinfo->grabbed = 0;
9165 check_visibility = 1;
9166 break;
9167
689004fa 9168 case WM_KILLFOCUS:
01b220b6 9169 /* TODO: some of this belongs in MOUSE_LEAVE */
791f420f 9170 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
08712a41 9171
791f420f
JR
9172 if (f)
9173 {
791f420f
JR
9174 if (f == dpyinfo->w32_focus_event_frame)
9175 dpyinfo->w32_focus_event_frame = 0;
9176
9177 if (f == dpyinfo->w32_focus_frame)
9178 x_new_focus_frame (dpyinfo, 0);
9179
9180 if (f == dpyinfo->mouse_face_mouse_frame)
9181 {
9182 /* If we move outside the frame, then we're
9183 certainly no longer on any text in the frame. */
9184 clear_mouse_face (dpyinfo);
9185 dpyinfo->mouse_face_mouse_frame = 0;
9186 }
9ef2e2cf 9187
791f420f
JR
9188 /* Generate a nil HELP_EVENT to cancel a help-echo.
9189 Do it only if there's something to cancel.
9190 Otherwise, the startup message is cleared when
9191 the mouse leaves the frame. */
9192 if (any_help_event_p)
9193 {
c2cc16fa 9194 Lisp_Object frame;
ec48c3a7
JR
9195 int n;
9196
791f420f 9197 XSETFRAME (frame, f);
c2cc16fa
JR
9198 help_echo = Qnil;
9199 n = gen_help_event (bufp, numchars,
9200 Qnil, frame, Qnil, Qnil, 0);
ec48c3a7 9201 bufp += n, count += n, numchars -=n;
791f420f
JR
9202 }
9203 }
689004fa 9204
08712a41 9205 dpyinfo->grabbed = 0;
689004fa 9206 check_visibility = 1;
ee78dc32 9207 break;
689004fa 9208
ee78dc32
GV
9209 case WM_CLOSE:
9210 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 9211
ee78dc32
GV
9212 if (f)
9213 {
9214 if (numchars == 0)
9215 abort ();
d67d1627 9216
3b8f9651 9217 bufp->kind = DELETE_WINDOW_EVENT;
ee78dc32 9218 XSETFRAME (bufp->frame_or_window, f);
7e889510 9219 bufp->arg = Qnil;
ee78dc32
GV
9220 bufp++;
9221 count++;
9222 numchars--;
9223 }
689004fa
GV
9224 break;
9225
9226 case WM_INITMENU:
9227 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 9228
689004fa
GV
9229 if (f)
9230 {
9231 if (numchars == 0)
9232 abort ();
d67d1627 9233
3b8f9651 9234 bufp->kind = MENU_BAR_ACTIVATE_EVENT;
689004fa 9235 XSETFRAME (bufp->frame_or_window, f);
7e889510 9236 bufp->arg = Qnil;
689004fa
GV
9237 bufp++;
9238 count++;
9239 numchars--;
9240 }
ee78dc32 9241 break;
689004fa 9242
ee78dc32
GV
9243 case WM_COMMAND:
9244 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
014b6ea1 9245
ee78dc32
GV
9246 if (f)
9247 {
f98169a0
GV
9248 extern void menubar_selection_callback
9249 (FRAME_PTR f, void * client_data);
014b6ea1 9250 menubar_selection_callback (f, (void *)msg.msg.wParam);
ee78dc32 9251 }
689004fa
GV
9252
9253 check_visibility = 1;
9254 break;
9255
9256 case WM_DISPLAYCHANGE:
9257 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
9258
d67d1627 9259 if (f)
689004fa
GV
9260 {
9261 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
9262 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
9263 dpyinfo->n_cbits = msg.msg.wParam;
9264 DebPrint (("display change: %d %d\n", dpyinfo->width,
9265 dpyinfo->height));
9266 }
d67d1627 9267
689004fa 9268 check_visibility = 1;
ee78dc32 9269 break;
e7efd97e
GV
9270
9271 default:
9272 /* Check for messages registered at runtime. */
9273 if (msg.msg.message == msh_mousewheel)
9274 {
d67d1627 9275 if (dpyinfo->grabbed && last_mouse_frame
e7efd97e
GV
9276 && FRAME_LIVE_P (last_mouse_frame))
9277 f = last_mouse_frame;
9278 else
9279 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
d67d1627 9280
e7efd97e
GV
9281 if (f)
9282 {
d67d1627 9283 if ((!dpyinfo->w32_focus_frame
e7efd97e
GV
9284 || f == dpyinfo->w32_focus_frame)
9285 && (numchars >= 1))
9286 {
9287 construct_mouse_wheel (bufp, &msg, f);
9288 bufp++;
9289 count++;
9290 numchars--;
9291 }
9292 }
9293 }
9294 break;
ee78dc32
GV
9295 }
9296 }
9297
9298 /* If the focus was just given to an autoraising frame,
9299 raise it now. */
9300 /* ??? This ought to be able to handle more than one such frame. */
9301 if (pending_autoraise_frame)
9302 {
9303 x_raise_frame (pending_autoraise_frame);
9304 pending_autoraise_frame = 0;
9305 }
9306
689004fa
GV
9307 /* Check which frames are still visisble, if we have enqueued any user
9308 events or been notified of events that may affect visibility. We
9309 do this here because there doesn't seem to be any direct
9310 notification from Windows that the visibility of a window has
9311 changed (at least, not in all cases). */
9312 if (count > 0 || check_visibility)
9313 {
9314 Lisp_Object tail, frame;
9315
9316 FOR_EACH_FRAME (tail, frame)
9317 {
9318 FRAME_PTR f = XFRAME (frame);
b71b8111
JR
9319 /* The tooltip has been drawn already. Avoid the
9320 SET_FRAME_GARBAGED below. */
eb77a5f4 9321 if (EQ (frame, tip_frame))
b71b8111
JR
9322 continue;
9323
689004fa
GV
9324 /* Check "visible" frames and mark each as obscured or not.
9325 Note that async_visible is nonzero for unobscured and
9326 obscured frames, but zero for hidden and iconified frames. */
9327 if (FRAME_W32_P (f) && f->async_visible)
9328 {
9329 RECT clipbox;
66e5868a
AI
9330 HDC hdc;
9331
9332 enter_crit ();
9333 /* Query clipping rectangle for the entire window area
9334 (GetWindowDC), not just the client portion (GetDC).
9335 Otherwise, the scrollbars and menubar aren't counted as
9336 part of the visible area of the frame, and we may think
9337 the frame is obscured when really a scrollbar is still
9338 visible and gets WM_PAINT messages above. */
9339 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
689004fa 9340 GetClipBox (hdc, &clipbox);
66e5868a
AI
9341 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
9342 leave_crit ();
689004fa
GV
9343
9344 if (clipbox.right == clipbox.left
9345 || clipbox.bottom == clipbox.top)
9346 {
9347 /* Frame has become completely obscured so mark as
9348 such (we do this by setting async_visible to 2 so
9349 that FRAME_VISIBLE_P is still true, but redisplay
9350 will skip it). */
9351 f->async_visible = 2;
9352
9353 if (!FRAME_OBSCURED_P (f))
9354 {
8f860180 9355 DebPrint (("frame %p (%s) obscured\n", f,
d5db4077 9356 SDATA (f->name)));
689004fa
GV
9357 }
9358 }
9359 else
9360 {
9361 /* Frame is not obscured, so mark it as such. */
9362 f->async_visible = 1;
9363
9364 if (FRAME_OBSCURED_P (f))
9365 {
9366 SET_FRAME_GARBAGED (f);
8f860180 9367 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
d5db4077 9368 SDATA (f->name)));
689004fa
GV
9369
9370 /* Force a redisplay sooner or later. */
9371 record_asynch_buffer_change ();
9372 }
9373 }
9374 }
9375 }
9376 }
9377
ee78dc32
GV
9378 UNBLOCK_INPUT;
9379 return count;
9380}
791f420f 9381
9ef2e2cf
JR
9382
9383
ee78dc32 9384\f
791f420f
JR
9385/***********************************************************************
9386 Text Cursor
9387 ***********************************************************************/
9388
6ff3e5e3
JR
9389/* Notice if the text cursor of window W has been overwritten by a
9390 drawing operation that outputs glyphs starting at START_X and
9391 ending at END_X in the line given by output_cursor.vpos.
9392 Coordinates are area-relative. END_X < 0 means all the rest
9393 of the line after START_X has been written. */
791f420f
JR
9394
9395static void
6637c996 9396notice_overwritten_cursor (w, area, x0, x1, y0, y1)
791f420f 9397 struct window *w;
6637c996
JR
9398 enum glyph_row_area area;
9399 int x0, x1, y0, y1;
791f420f 9400{
6637c996 9401 if (area == TEXT_AREA
6ff3e5e3 9402 && w->phys_cursor_on_p
6637c996
JR
9403 && y0 <= w->phys_cursor.y
9404 && y1 >= w->phys_cursor.y + w->phys_cursor_height
9405 && x0 <= w->phys_cursor.x
9406 && (x1 < 0 || x1 > w->phys_cursor.x))
791f420f
JR
9407 w->phys_cursor_on_p = 0;
9408}
9409
ee78dc32 9410
791f420f
JR
9411/* Set clipping for output in glyph row ROW. W is the window in which
9412 we operate. GC is the graphics context to set clipping in.
9413 WHOLE_LINE_P non-zero means include the areas used for truncation
9414 mark display and alike in the clipping rectangle.
ee78dc32 9415
791f420f
JR
9416 ROW may be a text row or, e.g., a mode line. Text rows must be
9417 clipped to the interior of the window dedicated to text display,
9418 mode lines must be clipped to the whole window. */
ee78dc32
GV
9419
9420static void
791f420f
JR
9421w32_clip_to_row (w, row, hdc, whole_line_p)
9422 struct window *w;
9423 struct glyph_row *row;
9424 HDC hdc;
9425 int whole_line_p;
ee78dc32 9426{
791f420f
JR
9427 struct frame *f = XFRAME (WINDOW_FRAME (w));
9428 RECT clip_rect;
9429 int window_x, window_y, window_width, window_height;
52cf03a1 9430
791f420f 9431 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
52cf03a1 9432
791f420f
JR
9433 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9434 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9435 clip_rect.top = max (clip_rect.top, window_y);
9436 clip_rect.right = clip_rect.left + window_width;
9437 clip_rect.bottom = clip_rect.top + row->visible_height;
9438
9439 /* If clipping to the whole line, including trunc marks, extend
9ef2e2cf 9440 the rectangle to the left and increase its width. */
791f420f
JR
9441 if (whole_line_p)
9442 {
33c34bea
KS
9443 clip_rect.left -= FRAME_X_LEFT_FRINGE_WIDTH (f);
9444 clip_rect.right += FRAME_X_FRINGE_WIDTH (f);
791f420f
JR
9445 }
9446
9447 w32_set_clip_rectangle (hdc, &clip_rect);
ee78dc32
GV
9448}
9449
791f420f
JR
9450
9451/* Draw a hollow box cursor on window W in glyph row ROW. */
ee78dc32
GV
9452
9453static void
791f420f
JR
9454x_draw_hollow_cursor (w, row)
9455 struct window *w;
9456 struct glyph_row *row;
ee78dc32 9457{
791f420f 9458 struct frame *f = XFRAME (WINDOW_FRAME (w));
988646fc 9459 HDC hdc;
791f420f
JR
9460 RECT rect;
9461 int wd;
9462 struct glyph *cursor_glyph;
9463 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
9464
9465 /* Compute frame-relative coordinates from window-relative
9466 coordinates. */
9467 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9468 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9469 + row->ascent - w->phys_cursor_ascent);
d6dc9151 9470 rect.bottom = rect.top + row->height;
791f420f
JR
9471
9472 /* Get the glyph the cursor is on. If we can't tell because
9473 the current matrix is invalid or such, give up. */
9474 cursor_glyph = get_phys_cursor_glyph (w);
9475 if (cursor_glyph == NULL)
ee78dc32
GV
9476 return;
9477
791f420f
JR
9478 /* Compute the width of the rectangle to draw. If on a stretch
9479 glyph, and `x-stretch-block-cursor' is nil, don't draw a
9480 rectangle as wide as the glyph, but use a canonical character
9481 width instead. */
d6dc9151 9482 wd = cursor_glyph->pixel_width;
791f420f
JR
9483 if (cursor_glyph->type == STRETCH_GLYPH
9484 && !x_stretch_cursor_p)
9485 wd = min (CANON_X_UNIT (f), wd);
9486
9487 rect.right = rect.left + wd;
988646fc 9488 hdc = get_frame_dc (f);
791f420f
JR
9489 FrameRect (hdc, &rect, hb);
9490 DeleteObject (hb);
9491
9492 release_frame_dc (f, hdc);
ee78dc32
GV
9493}
9494
791f420f
JR
9495
9496/* Draw a bar cursor on window W in glyph row ROW.
9497
9498 Implementation note: One would like to draw a bar cursor with an
9499 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9500 Unfortunately, I didn't find a font yet that has this property set.
9501 --gerd. */
ee78dc32
GV
9502
9503static void
17456df1 9504x_draw_bar_cursor (w, row, width, kind)
791f420f
JR
9505 struct window *w;
9506 struct glyph_row *row;
9ef2e2cf 9507 int width;
17456df1 9508 enum text_cursor_kinds kind;
ee78dc32 9509{
c2cc16fa
JR
9510 struct frame *f = XFRAME (w->frame);
9511 struct glyph *cursor_glyph;
9512 int x;
9513 HDC hdc;
791f420f 9514
c2cc16fa
JR
9515 /* If cursor is out of bounds, don't draw garbage. This can happen
9516 in mini-buffer windows when switching between echo area glyphs
9517 and mini-buffer. */
9518 cursor_glyph = get_phys_cursor_glyph (w);
9519 if (cursor_glyph == NULL)
9520 return;
9ef2e2cf 9521
c2cc16fa
JR
9522 /* If on an image, draw like a normal cursor. That's usually better
9523 visible than drawing a bar, esp. if the image is large so that
9524 the bar might not be in the window. */
9525 if (cursor_glyph->type == IMAGE_GLYPH)
9526 {
9527 struct glyph_row *row;
9528 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9529 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
9530 }
9531 else
9532 {
6ff3e5e3
JR
9533 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
9534 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9535
c2cc16fa 9536 if (width < 0)
fdbe859c 9537 width = FRAME_CURSOR_WIDTH (f);
17456df1 9538 width = min (cursor_glyph->pixel_width, width);
158cba56 9539
6ff3e5e3
JR
9540 /* If the glyph's background equals the color we normally draw
9541 the bar cursor in, the bar cursor in its normal color is
9542 invisible. Use the glyph's foreground color instead in this
9543 case, on the assumption that the glyph's colors are chosen so
9544 that the glyph is legible. */
9545 if (face->background == cursor_color)
9546 cursor_color = face->foreground;
9547
c2cc16fa
JR
9548 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9549 hdc = get_frame_dc (f);
9550 w32_clip_to_row (w, row, hdc, 0);
17456df1
JR
9551
9552 if (kind == BAR_CURSOR)
9553 {
9554 w32_fill_area (f, hdc, cursor_color, x,
9555 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9556 width, row->height);
9557 }
9558 else
9559 {
9560 w32_fill_area (f, hdc, cursor_color, x,
9561 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9562 row->height - width),
9563 cursor_glyph->pixel_width, width);
9564 }
c2cc16fa 9565 release_frame_dc (f, hdc);
791f420f 9566 }
ee78dc32
GV
9567}
9568
791f420f
JR
9569
9570/* Clear the cursor of window W to background color, and mark the
9571 cursor as not shown. This is used when the text where the cursor
9572 is is about to be rewritten. */
9573
ee78dc32 9574static void
791f420f
JR
9575x_clear_cursor (w)
9576 struct window *w;
ee78dc32 9577{
791f420f
JR
9578 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
9579 x_update_window_cursor (w, 0);
9580}
ee78dc32 9581
ee78dc32 9582
791f420f
JR
9583/* Draw the cursor glyph of window W in glyph row ROW. See the
9584 comment of x_draw_glyphs for the meaning of HL. */
ee78dc32 9585
791f420f
JR
9586static void
9587x_draw_phys_cursor_glyph (w, row, hl)
9588 struct window *w;
9589 struct glyph_row *row;
9590 enum draw_glyphs_face hl;
9591{
9592 /* If cursor hpos is out of bounds, don't draw garbage. This can
9593 happen in mini-buffer windows when switching between echo area
9594 glyphs and mini-buffer. */
9595 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
ee78dc32 9596 {
6ff3e5e3 9597 int on_p = w->phys_cursor_on_p;
791f420f
JR
9598 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
9599 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
6ff3e5e3
JR
9600 hl, 0);
9601 w->phys_cursor_on_p = on_p;
791f420f
JR
9602
9603 /* When we erase the cursor, and ROW is overlapped by other
9604 rows, make sure that these overlapping parts of other rows
9605 are redrawn. */
9606 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
9607 {
9608 if (row > w->current_matrix->rows
9609 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
9610 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
9611
9612 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
9613 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
9614 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
9615 }
ee78dc32 9616 }
791f420f 9617}
ee78dc32 9618
791f420f
JR
9619
9620/* Erase the image of a cursor of window W from the screen. */
9621
9622static void
9623x_erase_phys_cursor (w)
9624 struct window *w;
9625{
9626 struct frame *f = XFRAME (w->frame);
9627 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9628 int hpos = w->phys_cursor.hpos;
9629 int vpos = w->phys_cursor.vpos;
9630 int mouse_face_here_p = 0;
9631 struct glyph_matrix *active_glyphs = w->current_matrix;
9632 struct glyph_row *cursor_row;
9633 struct glyph *cursor_glyph;
9634 enum draw_glyphs_face hl;
9635
9636 /* No cursor displayed or row invalidated => nothing to do on the
9637 screen. */
9638 if (w->phys_cursor_type == NO_CURSOR)
9639 goto mark_cursor_off;
d67d1627 9640
791f420f
JR
9641 /* VPOS >= active_glyphs->nrows means that window has been resized.
9642 Don't bother to erase the cursor. */
9643 if (vpos >= active_glyphs->nrows)
9644 goto mark_cursor_off;
9645
9646 /* If row containing cursor is marked invalid, there is nothing we
9647 can do. */
9648 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9649 if (!cursor_row->enabled_p)
9650 goto mark_cursor_off;
d67d1627 9651
a2c6de8e
KS
9652 /* If row is completely invisible, don't attempt to delete a cursor which
9653 isn't there. This may happen if cursor is at top of window, and
9654 we switch to a buffer with a header line in that window. */
9655 if (cursor_row->visible_height <= 0)
9656 goto mark_cursor_off;
d67d1627 9657
791f420f
JR
9658 /* This can happen when the new row is shorter than the old one.
9659 In this case, either x_draw_glyphs or clear_end_of_line
9660 should have cleared the cursor. Note that we wouldn't be
9661 able to erase the cursor in this case because we don't have a
9662 cursor glyph at hand. */
9663 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9664 goto mark_cursor_off;
95f2448c 9665
791f420f
JR
9666 /* If the cursor is in the mouse face area, redisplay that when
9667 we clear the cursor. */
c2cc16fa
JR
9668 if (! NILP (dpyinfo->mouse_face_window)
9669 && w == XWINDOW (dpyinfo->mouse_face_window)
791f420f
JR
9670 && (vpos > dpyinfo->mouse_face_beg_row
9671 || (vpos == dpyinfo->mouse_face_beg_row
9672 && hpos >= dpyinfo->mouse_face_beg_col))
9673 && (vpos < dpyinfo->mouse_face_end_row
9674 || (vpos == dpyinfo->mouse_face_end_row
9675 && hpos < dpyinfo->mouse_face_end_col))
9676 /* Don't redraw the cursor's spot in mouse face if it is at the
9677 end of a line (on a newline). The cursor appears there, but
9678 mouse highlighting does not. */
9679 && cursor_row->used[TEXT_AREA] > hpos)
9680 mouse_face_here_p = 1;
9681
9682 /* Maybe clear the display under the cursor. */
9683 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
ee78dc32 9684 {
791f420f
JR
9685 int x;
9686 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
00fe468b 9687 HDC hdc;
ee78dc32 9688
791f420f
JR
9689 cursor_glyph = get_phys_cursor_glyph (w);
9690 if (cursor_glyph == NULL)
9691 goto mark_cursor_off;
ee78dc32 9692
791f420f 9693 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
00fe468b
JR
9694
9695 hdc = get_frame_dc (f);
9696 w32_clear_area (f, hdc, x,
791f420f
JR
9697 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9698 cursor_row->y)),
9699 cursor_glyph->pixel_width,
9700 cursor_row->visible_height);
00fe468b 9701 release_frame_dc (f, hdc);
ee78dc32 9702 }
d67d1627 9703
791f420f
JR
9704 /* Erase the cursor by redrawing the character underneath it. */
9705 if (mouse_face_here_p)
9706 hl = DRAW_MOUSE_FACE;
791f420f
JR
9707 else
9708 hl = DRAW_NORMAL_TEXT;
9709 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9710
9711 mark_cursor_off:
9712 w->phys_cursor_on_p = 0;
9713 w->phys_cursor_type = NO_CURSOR;
ee78dc32
GV
9714}
9715
9716
9f5a911b
JR
9717/* Non-zero if physical cursor of window W is within mouse face. */
9718
9719static int
9720cursor_in_mouse_face_p (w)
9721 struct window *w;
9722{
9723 struct w32_display_info *dpyinfo
9724 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
9725 int in_mouse_face = 0;
d67d1627 9726
9f5a911b
JR
9727 if (WINDOWP (dpyinfo->mouse_face_window)
9728 && XWINDOW (dpyinfo->mouse_face_window) == w)
9729 {
9730 int hpos = w->phys_cursor.hpos;
9731 int vpos = w->phys_cursor.vpos;
9732
9733 if (vpos >= dpyinfo->mouse_face_beg_row
9734 && vpos <= dpyinfo->mouse_face_end_row
9735 && (vpos > dpyinfo->mouse_face_beg_row
9736 || hpos >= dpyinfo->mouse_face_beg_col)
9737 && (vpos < dpyinfo->mouse_face_end_row
9738 || hpos < dpyinfo->mouse_face_end_col
9739 || dpyinfo->mouse_face_past_end))
9740 in_mouse_face = 1;
9741 }
9742
9743 return in_mouse_face;
9744}
9745
9746
791f420f
JR
9747/* Display or clear cursor of window W. If ON is zero, clear the
9748 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9749 where to put the cursor is specified by HPOS, VPOS, X and Y. */
ee78dc32 9750
791f420f
JR
9751void
9752x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9753 struct window *w;
9754 int on, hpos, vpos, x, y;
ee78dc32 9755{
791f420f
JR
9756 struct frame *f = XFRAME (w->frame);
9757 int new_cursor_type;
9ef2e2cf 9758 int new_cursor_width;
1cdf2e12 9759 int active_cursor;
791f420f
JR
9760 struct glyph_matrix *current_glyphs;
9761 struct glyph_row *glyph_row;
9762 struct glyph *glyph;
ee78dc32
GV
9763
9764 /* This is pointless on invisible frames, and dangerous on garbaged
791f420f
JR
9765 windows and frames; in the latter case, the frame or window may
9766 be in the midst of changing its size, and x and y may be off the
9767 window. */
9768 if (! FRAME_VISIBLE_P (f)
9769 || FRAME_GARBAGED_P (f)
9770 || vpos >= w->current_matrix->nrows
9771 || hpos >= w->current_matrix->matrix_w)
ee78dc32
GV
9772 return;
9773
9774 /* If cursor is off and we want it off, return quickly. */
791f420f 9775 if (!on && !w->phys_cursor_on_p)
ee78dc32
GV
9776 return;
9777
791f420f
JR
9778 current_glyphs = w->current_matrix;
9779 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9780 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
d67d1627
JB
9781
9782 /* If cursor row is not enabled, we don't really know where to
791f420f
JR
9783 display the cursor. */
9784 if (!glyph_row->enabled_p)
9785 {
9786 w->phys_cursor_on_p = 0;
9787 return;
9788 }
9789
9790 xassert (interrupt_input_blocked);
9791
fdbe859c 9792 /* Set new_cursor_type to the cursor we want to be displayed. */
1cdf2e12 9793 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
95f2448c 9794
791f420f
JR
9795 /* If cursor is currently being shown and we don't want it to be or
9796 it is in the wrong place, or the cursor type is not what we want,
ee78dc32 9797 erase it. */
791f420f 9798 if (w->phys_cursor_on_p
ee78dc32 9799 && (!on
791f420f
JR
9800 || w->phys_cursor.x != x
9801 || w->phys_cursor.y != y
9652402e 9802 || new_cursor_type != w->phys_cursor_type
fdbe859c 9803 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
e1429afe 9804 && new_cursor_width != w->phys_cursor_width)))
791f420f
JR
9805 x_erase_phys_cursor (w);
9806
95f2448c
RS
9807 /* Don't check phys_cursor_on_p here because that flag is only set
9808 to zero in some cases where we know that the cursor has been
9809 completely erased, to avoid the extra work of erasing the cursor
9810 twice. In other words, phys_cursor_on_p can be 1 and the cursor
9811 still not be visible, or it has only been partly erased. */
9812 if (on)
791f420f
JR
9813 {
9814 w->phys_cursor_ascent = glyph_row->ascent;
9815 w->phys_cursor_height = glyph_row->height;
d67d1627 9816
791f420f
JR
9817 /* Set phys_cursor_.* before x_draw_.* is called because some
9818 of them may need the information. */
9819 w->phys_cursor.x = x;
9820 w->phys_cursor.y = glyph_row->y;
9821 w->phys_cursor.hpos = hpos;
9822 w->phys_cursor.vpos = vpos;
99558ce8
JR
9823
9824 /* If the user wants to use the system caret, make sure our own
9825 cursor remains invisible. */
9826 if (w32_use_visible_system_caret)
9827 {
9828 if (w->phys_cursor_type != NO_CURSOR)
9829 x_erase_phys_cursor (w);
9830
9831 new_cursor_type = w->phys_cursor_type = NO_CURSOR;
e1429afe 9832 w->phys_cursor_width = -1;
99558ce8
JR
9833 }
9834 else
e1429afe
KS
9835 {
9836 w->phys_cursor_type = new_cursor_type;
9837 w->phys_cursor_width = new_cursor_width;
9838 }
99558ce8 9839
791f420f
JR
9840 w->phys_cursor_on_p = 1;
9841
abb15ebd
JR
9842 /* If this is the active cursor, we need to track it with the
9843 system caret, so third party software like screen magnifiers
9844 and speech synthesizers can follow the cursor. */
9845 if (active_cursor)
9846 {
99558ce8 9847 HWND hwnd = FRAME_W32_WINDOW (f);
abb15ebd 9848
99558ce8
JR
9849 w32_system_caret_x
9850 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9851 w32_system_caret_y
9852 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
9853 + glyph_row->ascent - w->phys_cursor_ascent);
9854
9855 /* If the size of the active cursor changed, destroy the old
9856 system caret. */
9857 if (w32_system_caret_hwnd
9858 && (w32_system_caret_height != w->phys_cursor_height))
9859 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
9860
9861 w32_system_caret_height = w->phys_cursor_height;
9862
9863 /* Move the system caret. */
9864 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
abb15ebd
JR
9865 }
9866
791f420f 9867 switch (new_cursor_type)
ee78dc32 9868 {
791f420f
JR
9869 case HOLLOW_BOX_CURSOR:
9870 x_draw_hollow_cursor (w, glyph_row);
9871 break;
9872
9873 case FILLED_BOX_CURSOR:
9874 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9875 break;
9876
9877 case BAR_CURSOR:
17456df1
JR
9878 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
9879 break;
9880
9881 case HBAR_CURSOR:
9882 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
791f420f 9883 break;
ee78dc32 9884
791f420f
JR
9885 case NO_CURSOR:
9886 break;
9887
9888 default:
9889 abort ();
9890 }
ee78dc32
GV
9891 }
9892}
9893
689004fa 9894
791f420f
JR
9895/* Display the cursor on window W, or clear it. X and Y are window
9896 relative pixel coordinates. HPOS and VPOS are glyph matrix
9897 positions. If W is not the selected window, display a hollow
9898 cursor. ON non-zero means display the cursor at X, Y which
9899 correspond to HPOS, VPOS, otherwise it is cleared. */
9900
9901void
9902x_display_cursor (w, on, hpos, vpos, x, y)
9903 struct window *w;
9904 int on, hpos, vpos, x, y;
ee78dc32
GV
9905{
9906 BLOCK_INPUT;
791f420f
JR
9907 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9908 UNBLOCK_INPUT;
9909}
9910
9911
9912/* Display the cursor on window W, or clear it, according to ON_P.
9913 Don't change the cursor's position. */
9914
9915void
9916x_update_cursor (f, on_p)
9917 struct frame *f;
9918 int on_p;
9919{
9920 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9921}
9922
9923
9924/* Call x_update_window_cursor with parameter ON_P on all leaf windows
9925 in the window tree rooted at W. */
ee78dc32 9926
791f420f
JR
9927static void
9928x_update_cursor_in_window_tree (w, on_p)
9929 struct window *w;
9930 int on_p;
9931{
9932 while (w)
689004fa 9933 {
791f420f
JR
9934 if (!NILP (w->hchild))
9935 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9936 else if (!NILP (w->vchild))
9937 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9938 else
9939 x_update_window_cursor (w, on_p);
9940
9941 w = NILP (w->next) ? 0 : XWINDOW (w->next);
689004fa 9942 }
791f420f 9943}
689004fa 9944
ee78dc32 9945
791f420f
JR
9946/* Switch the display of W's cursor on or off, according to the value
9947 of ON. */
9948
9949static void
9950x_update_window_cursor (w, on)
9951 struct window *w;
9952 int on;
9953{
9ef2e2cf
JR
9954 /* Don't update cursor in windows whose frame is in the process
9955 of being deleted. */
9956 if (w->current_matrix)
9957 {
9958 BLOCK_INPUT;
9959 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9960 w->phys_cursor.vpos, w->phys_cursor.x,
9961 w->phys_cursor.y);
9962 UNBLOCK_INPUT;
9963 }
ee78dc32 9964}
791f420f
JR
9965
9966
9ef2e2cf 9967
ee78dc32 9968\f
7f5d1df8
GV
9969/* Icons. */
9970
9971int
9972x_bitmap_icon (f, icon)
9973 struct frame *f;
9974 Lisp_Object icon;
9975{
7f5d1df8
GV
9976 HANDLE hicon;
9977
9978 if (FRAME_W32_WINDOW (f) == 0)
9979 return 1;
9980
9981 if (NILP (icon))
9982 hicon = LoadIcon (hinst, EMACS_CLASS);
9983 else if (STRINGP (icon))
d5db4077 9984 hicon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
7f5d1df8
GV
9985 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9986 else if (SYMBOLP (icon))
9987 {
9988 LPCTSTR name;
9989
9990 if (EQ (icon, intern ("application")))
9991 name = (LPCTSTR) IDI_APPLICATION;
9992 else if (EQ (icon, intern ("hand")))
9993 name = (LPCTSTR) IDI_HAND;
9994 else if (EQ (icon, intern ("question")))
9995 name = (LPCTSTR) IDI_QUESTION;
9996 else if (EQ (icon, intern ("exclamation")))
9997 name = (LPCTSTR) IDI_EXCLAMATION;
9998 else if (EQ (icon, intern ("asterisk")))
9999 name = (LPCTSTR) IDI_ASTERISK;
10000 else if (EQ (icon, intern ("winlogo")))
10001 name = (LPCTSTR) IDI_WINLOGO;
10002 else
10003 return 1;
10004
10005 hicon = LoadIcon (NULL, name);
10006 }
10007 else
10008 return 1;
10009
10010 if (hicon == NULL)
10011 return 1;
10012
791f420f
JR
10013 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
10014 (LPARAM) hicon);
7f5d1df8
GV
10015
10016 return 0;
10017}
10018
10019\f
c2cc16fa
JR
10020/************************************************************************
10021 Handling X errors
10022 ************************************************************************/
10023
10024/* Display Error Handling functions not used on W32. Listing them here
10025 helps diff stay in step when comparing w32term.c with xterm.c.
10026
10027x_error_catcher (display, error)
10028x_catch_errors (dpy)
10029x_catch_errors_unwind (old_val)
10030x_check_errors (dpy, format)
10031x_had_errors_p (dpy)
10032x_clear_errors (dpy)
10033x_uncatch_errors (dpy, count)
10034x_trace_wire ()
10035x_connection_signal (signalnum)
10036x_connection_closed (dpy, error_message)
10037x_error_quitter (display, error)
10038x_error_handler (display, error)
10039x_io_error_quitter (display)
10040
10041 */
10042
10043\f
ee78dc32
GV
10044/* Changing the font of the frame. */
10045
10046/* Give frame F the font named FONTNAME as its default font, and
10047 return the full name of that font. FONTNAME may be a wildcard
10048 pattern; in that case, we choose some font that fits the pattern.
10049 The return value shows which font we chose. */
10050
10051Lisp_Object
10052x_new_font (f, fontname)
10053 struct frame *f;
10054 register char *fontname;
10055{
cabb23bc 10056 struct font_info *fontp
93ff4395 10057 = FS_LOAD_FONT (f, 0, fontname, -1);
ee78dc32 10058
cabb23bc 10059 if (!fontp)
791f420f 10060 return Qnil;
ee78dc32 10061
cabb23bc 10062 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
791f420f 10063 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
cabb23bc 10064 FRAME_FONTSET (f) = -1;
ee78dc32
GV
10065
10066 /* Compute the scroll bar width in character columns. */
10067 if (f->scroll_bar_pixel_width > 0)
10068 {
ec48c3a7 10069 int wid = FONT_WIDTH (FRAME_FONT (f));
ee78dc32
GV
10070 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10071 }
10072 else
ec48c3a7
JR
10073 {
10074 int wid = FONT_WIDTH (FRAME_FONT (f));
10075 f->scroll_bar_cols = (14 + wid - 1) / wid;
10076 }
ee78dc32
GV
10077
10078 /* Now make the frame display the given font. */
fbd6baed 10079 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
10080 {
10081 frame_update_line_height (f);
49be9f70
JR
10082 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10083 x_set_window_size (f, 0, f->width, f->height);
ee78dc32
GV
10084 }
10085 else
10086 /* If we are setting a new frame's font for the first time,
10087 there are no faces yet, so this font's height is the line height. */
ec48c3a7 10088 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
ee78dc32 10089
791f420f 10090 return build_string (fontp->full_name);
ee78dc32
GV
10091}
10092\f
cabb23bc
GV
10093/* Give frame F the fontset named FONTSETNAME as its default font, and
10094 return the full name of that fontset. FONTSETNAME may be a wildcard
10095 pattern; in that case, we choose some fontset that fits the pattern.
10096 The return value shows which fontset we chose. */
10097
10098Lisp_Object
10099x_new_fontset (f, fontsetname)
10100 struct frame *f;
10101 char *fontsetname;
10102{
93ff4395 10103 int fontset = fs_query_fontset (build_string (fontsetname), 0);
cabb23bc
GV
10104 Lisp_Object result;
10105
10106 if (fontset < 0)
10107 return Qnil;
10108
10109 if (FRAME_FONTSET (f) == fontset)
10110 /* This fontset is already set in frame F. There's nothing more
10111 to do. */
93ff4395 10112 return fontset_name (fontset);
cabb23bc 10113
d5db4077 10114 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
cabb23bc
GV
10115
10116 if (!STRINGP (result))
10117 /* Can't load ASCII font. */
10118 return Qnil;
10119
10120 /* Since x_new_font doesn't update any fontset information, do it now. */
10121 FRAME_FONTSET(f) = fontset;
cabb23bc
GV
10122
10123 return build_string (fontsetname);
10124}
791f420f 10125
d33c49e8
KS
10126/* Compute actual fringe widths */
10127
10128void
10129x_compute_fringe_widths (f, redraw)
10130 struct frame *f;
10131 int redraw;
10132{
10133 int o_left = f->output_data.w32->left_fringe_width;
10134 int o_right = f->output_data.w32->right_fringe_width;
10135 int o_cols = f->output_data.w32->fringe_cols;
10136
10137 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
10138 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
10139 int left_fringe_width, right_fringe_width;
10140
10141 if (!NILP (left_fringe))
10142 left_fringe = Fcdr (left_fringe);
10143 if (!NILP (right_fringe))
10144 right_fringe = Fcdr (right_fringe);
10145
10146 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
10147 XINT (left_fringe));
10148 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
10149 XINT (right_fringe));
10150
10151 if (left_fringe_width || right_fringe_width)
10152 {
10153 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
10154 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
10155 int conf_wid = left_wid + right_wid;
10156 int font_wid = FONT_WIDTH (f->output_data.w32->font);
10157 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
10158 int real_wid = cols * font_wid;
10159 if (left_wid && right_wid)
10160 {
10161 if (left_fringe_width < 0)
10162 {
10163 /* Left fringe width is fixed, adjust right fringe if necessary */
10164 f->output_data.w32->left_fringe_width = left_wid;
10165 f->output_data.w32->right_fringe_width = real_wid - left_wid;
10166 }
10167 else if (right_fringe_width < 0)
10168 {
10169 /* Right fringe width is fixed, adjust left fringe if necessary */
10170 f->output_data.w32->left_fringe_width = real_wid - right_wid;
10171 f->output_data.w32->right_fringe_width = right_wid;
10172 }
10173 else
10174 {
10175 /* Adjust both fringes with an equal amount.
10176 Note that we are doing integer arithmetic here, so don't
10177 lose a pixel if the total width is an odd number. */
10178 int fill = real_wid - conf_wid;
10179 f->output_data.w32->left_fringe_width = left_wid + fill/2;
10180 f->output_data.w32->right_fringe_width = right_wid + fill - fill/2;
10181 }
10182 }
10183 else if (left_fringe_width)
10184 {
10185 f->output_data.w32->left_fringe_width = real_wid;
10186 f->output_data.w32->right_fringe_width = 0;
10187 }
10188 else
10189 {
10190 f->output_data.w32->left_fringe_width = 0;
10191 f->output_data.w32->right_fringe_width = real_wid;
10192 }
10193 f->output_data.w32->fringe_cols = cols;
10194 f->output_data.w32->fringes_extra = real_wid;
10195 }
10196 else
10197 {
10198 f->output_data.w32->left_fringe_width = 0;
10199 f->output_data.w32->right_fringe_width = 0;
10200 f->output_data.w32->fringe_cols = 0;
10201 f->output_data.w32->fringes_extra = 0;
10202 }
10203
10204 if (redraw && FRAME_VISIBLE_P (f))
10205 if (o_left != f->output_data.w32->left_fringe_width ||
10206 o_right != f->output_data.w32->right_fringe_width ||
10207 o_cols != f->output_data.w32->fringe_cols)
10208 redraw_frame (f);
10209}
c2cc16fa
JR
10210\f
10211/***********************************************************************
10212 TODO: W32 Input Methods
10213 ***********************************************************************/
10214/* Listing missing functions from xterm.c helps diff stay in step.
791f420f 10215
c2cc16fa
JR
10216xim_destroy_callback (xim, client_data, call_data)
10217xim_open_dpy (dpyinfo, resource_name)
10218struct xim_inst_t
10219xim_instantiate_callback (display, client_data, call_data)
10220xim_initialize (dpyinfo, resource_name)
10221xim_close_dpy (dpyinfo)
791f420f 10222
c2cc16fa 10223 */
791f420f 10224
cabb23bc 10225\f
689004fa
GV
10226/* Calculate the absolute position in frame F
10227 from its current recorded position values and gravity. */
10228
9ef2e2cf 10229void
ee78dc32
GV
10230x_calc_absolute_position (f)
10231 struct frame *f;
10232{
ee78dc32 10233 POINT pt;
fbd6baed 10234 int flags = f->output_data.w32->size_hint_flags;
ee78dc32
GV
10235
10236 pt.x = pt.y = 0;
10237
10238 /* Find the position of the outside upper-left corner of
c2cc16fa
JR
10239 the inner window, with respect to the outer window.
10240 But do this only if we will need the results. */
fbd6baed 10241 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
ee78dc32
GV
10242 {
10243 BLOCK_INPUT;
fbd6baed
GV
10244 MapWindowPoints (FRAME_W32_WINDOW (f),
10245 f->output_data.w32->parent_desc,
ee78dc32
GV
10246 &pt, 1);
10247 UNBLOCK_INPUT;
10248 }
10249
10250 {
10251 RECT rt;
10252 rt.left = rt.right = rt.top = rt.bottom = 0;
d67d1627 10253
ee78dc32 10254 BLOCK_INPUT;
fbd6baed 10255 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
97c23857 10256 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32
GV
10257 UNBLOCK_INPUT;
10258
10259 pt.x += (rt.right - rt.left);
10260 pt.y += (rt.bottom - rt.top);
10261 }
10262
10263 /* Treat negative positions as relative to the leftmost bottommost
10264 position that fits on the screen. */
10265 if (flags & XNegative)
fbd6baed
GV
10266 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
10267 - 2 * f->output_data.w32->border_width - pt.x
ee78dc32 10268 - PIXEL_WIDTH (f)
fbd6baed 10269 + f->output_data.w32->left_pos);
158cba56 10270
ee78dc32 10271 if (flags & YNegative)
fbd6baed
GV
10272 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
10273 - 2 * f->output_data.w32->border_width - pt.y
ee78dc32 10274 - PIXEL_HEIGHT (f)
fbd6baed 10275 + f->output_data.w32->top_pos);
ee78dc32
GV
10276 /* The left_pos and top_pos
10277 are now relative to the top and left screen edges,
10278 so the flags should correspond. */
fbd6baed 10279 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32
GV
10280}
10281
10282/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10283 to really change the position, and 0 when calling from
10284 x_make_frame_visible (in that case, XOFF and YOFF are the current
10285 position values). It is -1 when calling from x_set_frame_parameters,
10286 which means, do adjust for borders but don't change the gravity. */
10287
9ef2e2cf 10288void
ee78dc32
GV
10289x_set_offset (f, xoff, yoff, change_gravity)
10290 struct frame *f;
10291 register int xoff, yoff;
10292 int change_gravity;
10293{
10294 int modified_top, modified_left;
10295
10296 if (change_gravity > 0)
10297 {
fbd6baed
GV
10298 f->output_data.w32->top_pos = yoff;
10299 f->output_data.w32->left_pos = xoff;
10300 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 10301 if (xoff < 0)
fbd6baed 10302 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 10303 if (yoff < 0)
fbd6baed
GV
10304 f->output_data.w32->size_hint_flags |= YNegative;
10305 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
10306 }
10307 x_calc_absolute_position (f);
10308
10309 BLOCK_INPUT;
10310 x_wm_set_size_hint (f, (long) 0, 0);
10311
fbd6baed
GV
10312 modified_left = f->output_data.w32->left_pos;
10313 modified_top = f->output_data.w32->top_pos;
ee78dc32 10314
fbd6baed 10315 my_set_window_pos (FRAME_W32_WINDOW (f),
52cf03a1
GV
10316 NULL,
10317 modified_left, modified_top,
cabb23bc 10318 0, 0,
689004fa 10319 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
ee78dc32
GV
10320 UNBLOCK_INPUT;
10321}
10322
549808db
JR
10323
10324/* Check if we need to resize the frame due to a fullscreen request.
10325 If so needed, resize the frame. */
10326static void
10327x_check_fullscreen (f)
10328 struct frame *f;
10329{
10330 if (f->output_data.w32->want_fullscreen & FULLSCREEN_BOTH)
10331 {
10332 int width, height, ign;
d67d1627 10333
549808db
JR
10334 x_real_positions (f, &f->output_data.w32->left_pos,
10335 &f->output_data.w32->top_pos);
10336
10337 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
d67d1627 10338
549808db
JR
10339 /* We do not need to move the window, it shall be taken care of
10340 when setting WM manager hints.
10341 If the frame is visible already, the position is checked by
10342 x_check_fullscreen_move. */
10343 if (f->width != width || f->height != height)
10344 {
10345 change_frame_size (f, height, width, 0, 1, 0);
10346 SET_FRAME_GARBAGED (f);
10347 cancel_mouse_face (f);
10348
10349 /* Wait for the change of frame size to occur */
10350 f->output_data.w32->want_fullscreen |= FULLSCREEN_WAIT;
10351 }
10352 }
10353}
10354
10355/* If frame parameters are set after the frame is mapped, we need to move
10356 the window. This is done in xfns.c.
10357 Some window managers moves the window to the right position, some
10358 moves the outer window manager window to the specified position.
10359 Here we check that we are in the right spot. If not, make a second
10360 move, assuming we are dealing with the second kind of window manager. */
10361static void
10362x_check_fullscreen_move (f)
10363 struct frame *f;
10364{
10365 if (f->output_data.w32->want_fullscreen & FULLSCREEN_MOVE_WAIT)
10366 {
10367 int expect_top = f->output_data.w32->top_pos;
10368 int expect_left = f->output_data.w32->left_pos;
10369
10370 if (f->output_data.w32->want_fullscreen & FULLSCREEN_HEIGHT)
10371 expect_top = 0;
10372 if (f->output_data.w32->want_fullscreen & FULLSCREEN_WIDTH)
10373 expect_left = 0;
d67d1627 10374
549808db
JR
10375 if (expect_top != f->output_data.w32->top_pos
10376 || expect_left != f->output_data.w32->left_pos)
10377 x_set_offset (f, expect_left, expect_top, 1);
10378
10379 /* Just do this once */
10380 f->output_data.w32->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
10381 }
10382}
10383
10384
10385/* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
10386 wanted positions of the WM window (not emacs window).
10387 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
10388 window (FRAME_X_WINDOW).
10389 */
10390void
10391x_fullscreen_adjust (f, width, height, top_pos, left_pos)
10392 struct frame *f;
10393 int *width;
10394 int *height;
10395 int *top_pos;
10396 int *left_pos;
10397{
10398 int newwidth = f->width, newheight = f->height;
10399
10400 *top_pos = f->output_data.w32->top_pos;
10401 *left_pos = f->output_data.w32->left_pos;
d67d1627 10402
549808db
JR
10403 if (f->output_data.w32->want_fullscreen & FULLSCREEN_HEIGHT)
10404 {
10405 int ph;
d67d1627 10406
549808db
JR
10407 ph = FRAME_X_DISPLAY_INFO (f)->height;
10408 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
10409 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
10410 - f->output_data.w32->y_pixels_diff;
10411 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
10412 *top_pos = 0;
10413 }
10414
10415 if (f->output_data.w32->want_fullscreen & FULLSCREEN_WIDTH)
10416 {
10417 int pw;
d67d1627 10418
549808db
JR
10419 pw = FRAME_X_DISPLAY_INFO (f)->width;
10420 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
10421 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
10422 - f->output_data.w32->x_pixels_diff;
10423 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
10424 *left_pos = 0;
10425 }
10426
10427 *width = newwidth;
10428 *height = newheight;
10429}
10430
10431
ee78dc32
GV
10432/* Call this to change the size of frame F's x-window.
10433 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
10434 for this size change and subsequent size changes.
10435 Otherwise we leave the window gravity unchanged. */
c2cc16fa 10436
791f420f 10437void
ee78dc32
GV
10438x_set_window_size (f, change_gravity, cols, rows)
10439 struct frame *f;
10440 int change_gravity;
10441 int cols, rows;
10442{
10443 int pixelwidth, pixelheight;
d67d1627 10444
ee78dc32 10445 BLOCK_INPUT;
d67d1627 10446
ee78dc32 10447 check_frame_size (f, &rows, &cols);
fbd6baed 10448 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
10449 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
10450 ? 0
fbd6baed 10451 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
d33c49e8
KS
10452
10453 x_compute_fringe_widths (f, 0);
10454
ee78dc32
GV
10455 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
10456 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
d67d1627 10457
fbd6baed 10458 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32 10459 x_wm_set_size_hint (f, (long) 0, 0);
d67d1627 10460
ee78dc32
GV
10461 {
10462 RECT rect;
10463
10464 rect.left = rect.top = 0;
10465 rect.right = pixelwidth;
10466 rect.bottom = pixelheight;
d67d1627 10467
fbd6baed 10468 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
97c23857 10469 FRAME_EXTERNAL_MENU_BAR (f));
d67d1627 10470
fbd6baed 10471 my_set_window_pos (FRAME_W32_WINDOW (f),
d67d1627 10472 NULL,
52cf03a1 10473 0, 0,
689004fa
GV
10474 rect.right - rect.left,
10475 rect.bottom - rect.top,
10476 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
ee78dc32 10477 }
d67d1627 10478
ee78dc32
GV
10479 /* Now, strictly speaking, we can't be sure that this is accurate,
10480 but the window manager will get around to dealing with the size
10481 change request eventually, and we'll hear how it went when the
10482 ConfigureNotify event gets here.
d67d1627 10483
ee78dc32
GV
10484 We could just not bother storing any of this information here,
10485 and let the ConfigureNotify event set everything up, but that
ec48c3a7 10486 might be kind of confusing to the Lisp code, since size changes
ee78dc32 10487 wouldn't be reported in the frame parameters until some random
791f420f
JR
10488 point in the future when the ConfigureNotify event arrives.
10489
10490 We pass 1 for DELAY since we can't run Lisp code inside of
10491 a BLOCK_INPUT. */
10492 change_frame_size (f, rows, cols, 0, 1, 0);
ee78dc32
GV
10493 PIXEL_WIDTH (f) = pixelwidth;
10494 PIXEL_HEIGHT (f) = pixelheight;
10495
689004fa
GV
10496 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10497 receive in the ConfigureNotify event; if we get what we asked
10498 for, then the event won't cause the screen to become garbaged, so
10499 we have to make sure to do it here. */
10500 SET_FRAME_GARBAGED (f);
10501
ee78dc32 10502 /* If cursor was outside the new size, mark it as off. */
791f420f 10503 mark_window_cursors_off (XWINDOW (f->root_window));
ee78dc32 10504
689004fa 10505 /* Clear out any recollection of where the mouse highlighting was,
d67d1627 10506 since it might be in a place that's outside the new frame size.
689004fa
GV
10507 Actually checking whether it is outside is a pain in the neck,
10508 so don't try--just let the highlighting be done afresh with new size. */
31d4844a
KH
10509 cancel_mouse_face (f);
10510
ee78dc32
GV
10511 UNBLOCK_INPUT;
10512}
10513\f
10514/* Mouse warping. */
10515
ec48c3a7
JR
10516void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
10517
10518void
10519x_set_mouse_position (f, x, y)
10520 struct frame *f;
10521 int x, y;
10522{
10523 int pix_x, pix_y;
10524
10525 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
10526 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
10527
10528 if (pix_x < 0) pix_x = 0;
10529 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
10530
10531 if (pix_y < 0) pix_y = 0;
10532 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
10533
10534 x_set_mouse_pixel_position (f, pix_x, pix_y);
10535}
10536
1bcd16f2
MB
10537void
10538x_set_mouse_pixel_position (f, pix_x, pix_y)
10539 struct frame *f;
10540 int pix_x, pix_y;
10541{
689004fa
GV
10542 RECT rect;
10543 POINT pt;
10544
1bcd16f2
MB
10545 BLOCK_INPUT;
10546
689004fa
GV
10547 GetClientRect (FRAME_W32_WINDOW (f), &rect);
10548 pt.x = rect.left + pix_x;
10549 pt.y = rect.top + pix_y;
10550 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
1bcd16f2 10551
689004fa 10552 SetCursorPos (pt.x, pt.y);
1bcd16f2
MB
10553
10554 UNBLOCK_INPUT;
10555}
10556
ee78dc32
GV
10557\f
10558/* focus shifting, raising and lowering. */
10559
ec48c3a7 10560void
ee78dc32
GV
10561x_focus_on_frame (f)
10562 struct frame *f;
10563{
689004fa
GV
10564 struct w32_display_info *dpyinfo = &one_w32_display_info;
10565
10566 /* Give input focus to frame. */
10567 BLOCK_INPUT;
10568#if 0
10569 /* Try not to change its Z-order if possible. */
10570 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
10571 my_set_focus (f, FRAME_W32_WINDOW (f));
10572 else
10573#endif
ef0e360f 10574 my_set_foreground_window (FRAME_W32_WINDOW (f));
689004fa 10575 UNBLOCK_INPUT;
ee78dc32
GV
10576}
10577
ec48c3a7 10578void
ee78dc32
GV
10579x_unfocus_frame (f)
10580 struct frame *f;
10581{
10582}
10583
10584/* Raise frame F. */
791f420f 10585void
ee78dc32
GV
10586x_raise_frame (f)
10587 struct frame *f;
10588{
689004fa
GV
10589 BLOCK_INPUT;
10590
10591 /* Strictly speaking, raise-frame should only change the frame's Z
10592 order, leaving input focus unchanged. This is reasonable behaviour
10593 on X where the usual policy is point-to-focus. However, this
10594 behaviour would be very odd on Windows where the usual policy is
10595 click-to-focus.
10596
10597 On X, if the mouse happens to be over the raised frame, it gets
10598 input focus anyway (so the window with focus will never be
10599 completely obscured) - if not, then just moving the mouse over it
10600 is sufficient to give it focus. On Windows, the user must actually
10601 click on the frame (preferrably the title bar so as not to move
10602 point), which is more awkward. Also, no other Windows program
10603 raises a window to the top but leaves another window (possibly now
10604 completely obscured) with input focus.
10605
10606 Because there is a system setting on Windows that allows the user
10607 to choose the point to focus policy, we make the strict semantics
10608 optional, but by default we grab focus when raising. */
10609
10610 if (NILP (Vw32_grab_focus_on_raise))
ee78dc32 10611 {
689004fa
GV
10612 /* The obvious call to my_set_window_pos doesn't work if Emacs is
10613 not already the foreground application: the frame is raised
10614 above all other frames belonging to us, but not above the
10615 current top window. To achieve that, we have to resort to this
10616 more cumbersome method. */
10617
10618 HDWP handle = BeginDeferWindowPos (2);
10619 if (handle)
10620 {
10621 DeferWindowPos (handle,
10622 FRAME_W32_WINDOW (f),
10623 HWND_TOP,
10624 0, 0, 0, 0,
10625 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
10626
10627 DeferWindowPos (handle,
10628 GetForegroundWindow (),
10629 FRAME_W32_WINDOW (f),
10630 0, 0, 0, 0,
10631 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
10632
10633 EndDeferWindowPos (handle);
10634 }
ee78dc32 10635 }
689004fa
GV
10636 else
10637 {
ef0e360f 10638 my_set_foreground_window (FRAME_W32_WINDOW (f));
689004fa
GV
10639 }
10640
10641 UNBLOCK_INPUT;
ee78dc32
GV
10642}
10643
10644/* Lower frame F. */
791f420f 10645void
ee78dc32
GV
10646x_lower_frame (f)
10647 struct frame *f;
10648{
689004fa
GV
10649 BLOCK_INPUT;
10650 my_set_window_pos (FRAME_W32_WINDOW (f),
10651 HWND_BOTTOM,
10652 0, 0, 0, 0,
10653 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
10654 UNBLOCK_INPUT;
ee78dc32
GV
10655}
10656
10657static void
ec48c3a7 10658w32_frame_raise_lower (f, raise_flag)
ee78dc32 10659 FRAME_PTR f;
ec48c3a7 10660 int raise_flag;
ee78dc32 10661{
7e6ac5b9
AI
10662 if (! FRAME_W32_P (f))
10663 return;
10664
ec48c3a7 10665 if (raise_flag)
ee78dc32
GV
10666 x_raise_frame (f);
10667 else
10668 x_lower_frame (f);
10669}
10670\f
10671/* Change of visibility. */
10672
10673/* This tries to wait until the frame is really visible.
10674 However, if the window manager asks the user where to position
10675 the frame, this will return before the user finishes doing that.
10676 The frame will not actually be visible at that time,
10677 but it will become visible later when the window manager
10678 finishes with it. */
10679
ec48c3a7 10680void
ee78dc32
GV
10681x_make_frame_visible (f)
10682 struct frame *f;
10683{
7f5d1df8
GV
10684 Lisp_Object type;
10685
ee78dc32
GV
10686 BLOCK_INPUT;
10687
7f5d1df8
GV
10688 type = x_icon_type (f);
10689 if (!NILP (type))
10690 x_bitmap_icon (f, type);
10691
ee78dc32
GV
10692 if (! FRAME_VISIBLE_P (f))
10693 {
10694 /* We test FRAME_GARBAGED_P here to make sure we don't
10695 call x_set_offset a second time
10696 if we get to x_make_frame_visible a second time
10697 before the window gets really visible. */
10698 if (! FRAME_ICONIFIED_P (f)
fbd6baed 10699 && ! f->output_data.w32->asked_for_visible)
fbd6baed 10700 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
ee78dc32 10701
fbd6baed 10702 f->output_data.w32->asked_for_visible = 1;
52cf03a1 10703
4d6e8199 10704/* my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW); */
689004fa 10705 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
ee78dc32
GV
10706 }
10707
10708 /* Synchronize to ensure Emacs knows the frame is visible
10709 before we do anything else. We do this loop with input not blocked
10710 so that incoming events are handled. */
10711 {
10712 Lisp_Object frame;
791f420f 10713 int count;
ee78dc32
GV
10714
10715 /* This must come after we set COUNT. */
10716 UNBLOCK_INPUT;
10717
10718 XSETFRAME (frame, f);
10719
791f420f
JR
10720 /* Wait until the frame is visible. Process X events until a
10721 MapNotify event has been seen, or until we think we won't get a
10722 MapNotify at all.. */
10723 for (count = input_signal_count + 10;
10724 input_signal_count < count && !FRAME_VISIBLE_P (f);)
ee78dc32 10725 {
791f420f 10726 /* Force processing of queued events. */
01b220b6 10727 /* TODO: x_sync equivalent? */
791f420f 10728
ee78dc32
GV
10729 /* Machines that do polling rather than SIGIO have been observed
10730 to go into a busy-wait here. So we'll fake an alarm signal
10731 to let the handler know that there's something to be read.
10732 We used to raise a real alarm, but it seems that the handler
10733 isn't always enabled here. This is probably a bug. */
10734 if (input_polling_used ())
10735 {
10736 /* It could be confusing if a real alarm arrives while processing
10737 the fake one. Turn it off and let the handler reset it. */
a9b4e0ec
AI
10738 int old_poll_suppress_count = poll_suppress_count;
10739 poll_suppress_count = 1;
10740 poll_for_input_1 ();
10741 poll_suppress_count = old_poll_suppress_count;
ee78dc32 10742 }
ee78dc32
GV
10743 }
10744 FRAME_SAMPLE_VISIBILITY (f);
10745 }
10746}
10747
10748/* Change from mapped state to withdrawn state. */
10749
10750/* Make the frame visible (mapped and not iconified). */
10751
10752x_make_frame_invisible (f)
10753 struct frame *f;
10754{
ee78dc32 10755 /* Don't keep the highlight on an invisible frame. */
4baaed0f
KS
10756 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
10757 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
d67d1627 10758
ee78dc32 10759 BLOCK_INPUT;
d67d1627 10760
689004fa 10761 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
d67d1627 10762
ee78dc32
GV
10763 /* We can't distinguish this from iconification
10764 just by the event that we get from the server.
10765 So we can't win using the usual strategy of letting
10766 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10767 and synchronize with the server to make sure we agree. */
10768 f->visible = 0;
10769 FRAME_ICONIFIED_P (f) = 0;
10770 f->async_visible = 0;
10771 f->async_iconified = 0;
d67d1627 10772
ee78dc32
GV
10773 UNBLOCK_INPUT;
10774}
10775
10776/* Change window state from mapped to iconified. */
10777
52cf03a1
GV
10778void
10779x_iconify_frame (f)
ee78dc32
GV
10780 struct frame *f;
10781{
7f5d1df8 10782 Lisp_Object type;
ee78dc32
GV
10783
10784 /* Don't keep the highlight on an invisible frame. */
4baaed0f
KS
10785 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
10786 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
ee78dc32
GV
10787
10788 if (f->async_iconified)
10789 return;
10790
10791 BLOCK_INPUT;
10792
7f5d1df8
GV
10793 type = x_icon_type (f);
10794 if (!NILP (type))
10795 x_bitmap_icon (f, type);
10796
689004fa 10797 /* Simulate the user minimizing the frame. */
4934bcdd 10798 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
ee78dc32
GV
10799
10800 UNBLOCK_INPUT;
10801}
c2cc16fa 10802
ee78dc32 10803\f
25badd7a 10804/* Free X resources of frame F. */
ee78dc32 10805
25badd7a
AI
10806void
10807x_free_frame_resources (f)
ee78dc32
GV
10808 struct frame *f;
10809{
fbd6baed 10810 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
10811
10812 BLOCK_INPUT;
10813
25badd7a
AI
10814 if (FRAME_W32_WINDOW (f))
10815 my_destroy_window (f, FRAME_W32_WINDOW (f));
d67d1627 10816
ee78dc32 10817 free_frame_menubar (f);
ee78dc32 10818
25badd7a
AI
10819 unload_color (f, f->output_data.x->foreground_pixel);
10820 unload_color (f, f->output_data.x->background_pixel);
10821 unload_color (f, f->output_data.w32->cursor_pixel);
10822 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
10823 unload_color (f, f->output_data.w32->border_pixel);
10824 unload_color (f, f->output_data.w32->mouse_pixel);
c2cc16fa
JR
10825 if (f->output_data.w32->white_relief.allocated_p)
10826 unload_color (f, f->output_data.w32->white_relief.pixel);
10827 if (f->output_data.w32->black_relief.allocated_p)
10828 unload_color (f, f->output_data.w32->black_relief.pixel);
10829
25badd7a
AI
10830 if (FRAME_FACE_CACHE (f))
10831 free_frame_faces (f);
d67d1627 10832
fbd6baed 10833 xfree (f->output_data.w32);
25badd7a 10834 f->output_data.w32 = NULL;
d67d1627 10835
fbd6baed
GV
10836 if (f == dpyinfo->w32_focus_frame)
10837 dpyinfo->w32_focus_frame = 0;
10838 if (f == dpyinfo->w32_focus_event_frame)
10839 dpyinfo->w32_focus_event_frame = 0;
4baaed0f
KS
10840 if (f == dpyinfo->x_highlight_frame)
10841 dpyinfo->x_highlight_frame = 0;
ee78dc32 10842
ee78dc32
GV
10843 if (f == dpyinfo->mouse_face_mouse_frame)
10844 {
10845 dpyinfo->mouse_face_beg_row
10846 = dpyinfo->mouse_face_beg_col = -1;
10847 dpyinfo->mouse_face_end_row
10848 = dpyinfo->mouse_face_end_col = -1;
10849 dpyinfo->mouse_face_window = Qnil;
25badd7a
AI
10850 dpyinfo->mouse_face_deferred_gc = 0;
10851 dpyinfo->mouse_face_mouse_frame = 0;
ee78dc32
GV
10852 }
10853
10854 UNBLOCK_INPUT;
10855}
25badd7a
AI
10856
10857
10858/* Destroy the window of frame F. */
10859
10860x_destroy_window (f)
10861 struct frame *f;
10862{
10863 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10864
10865 x_free_frame_resources (f);
10866
10867 dpyinfo->reference_count--;
10868}
c2cc16fa 10869
ee78dc32
GV
10870\f
10871/* Setting window manager hints. */
10872
10873/* Set the normal size hints for the window manager, for frame F.
10874 FLAGS is the flags word to use--or 0 meaning preserve the flags
10875 that the window now has.
10876 If USER_POSITION is nonzero, we set the USPosition
10877 flag (this is useful when FLAGS is 0). */
791f420f 10878void
ee78dc32
GV
10879x_wm_set_size_hint (f, flags, user_position)
10880 struct frame *f;
10881 long flags;
10882 int user_position;
10883{
fbd6baed 10884 Window window = FRAME_W32_WINDOW (f);
ee78dc32 10885
97c23857 10886 enter_crit ();
ee78dc32 10887
689004fa
GV
10888 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
10889 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
10890 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
10891 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
ee78dc32 10892
97c23857 10893 leave_crit ();
ee78dc32
GV
10894}
10895
10896/* Window manager things */
10897x_wm_set_icon_position (f, icon_x, icon_y)
10898 struct frame *f;
10899 int icon_x, icon_y;
10900{
10901#if 0
fbd6baed 10902 Window window = FRAME_W32_WINDOW (f);
ee78dc32
GV
10903
10904 f->display.x->wm_hints.flags |= IconPositionHint;
10905 f->display.x->wm_hints.icon_x = icon_x;
10906 f->display.x->wm_hints.icon_y = icon_y;
10907
10908 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
10909#endif
10910}
10911
c2cc16fa
JR
10912\f
10913/***********************************************************************
10914 Fonts
10915 ***********************************************************************/
10916
10917/* The following functions are listed here to help diff stay in step
10918 with xterm.c. See w32fns.c for definitions.
10919
10920x_get_font_info (f, font_idx)
10921x_list_fonts (f, pattern, size, maxnames)
10922
10923 */
10924
10925#if GLYPH_DEBUG
10926
10927/* Check that FONT is valid on frame F. It is if it can be found in F's
10928 font table. */
10929
10930static void
10931x_check_font (f, font)
10932 struct frame *f;
10933 XFontStruct *font;
10934{
10935 int i;
10936 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10937
10938 xassert (font != NULL);
9ef2e2cf 10939
c2cc16fa 10940 for (i = 0; i < dpyinfo->n_fonts; i++)
d67d1627 10941 if (dpyinfo->font_table[i].name
c2cc16fa
JR
10942 && font == dpyinfo->font_table[i].font)
10943 break;
10944
10945 xassert (i < dpyinfo->n_fonts);
10946}
10947
10948#endif /* GLYPH_DEBUG != 0 */
10949
10950/* Set *W to the minimum width, *H to the minimum font height of FONT.
10951 Note: There are (broken) X fonts out there with invalid XFontStruct
10952 min_bounds contents. For example, handa@etl.go.jp reports that
10953 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10954 have font->min_bounds.width == 0. */
10955
10956static INLINE void
10957x_font_min_bounds (font, w, h)
10958 XFontStruct *font;
10959 int *w, *h;
10960{
10961 /*
10962 * TODO: Windows does not appear to offer min bound, only
10963 * average and maximum width, and maximum height.
10964 */
10965 *h = FONT_HEIGHT (font);
10966 *w = FONT_WIDTH (font);
10967}
10968
10969
10970/* Compute the smallest character width and smallest font height over
10971 all fonts available on frame F. Set the members smallest_char_width
10972 and smallest_font_height in F's x_display_info structure to
10973 the values computed. Value is non-zero if smallest_font_height or
10974 smallest_char_width become smaller than they were before. */
10975
10976int
10977x_compute_min_glyph_bounds (f)
10978 struct frame *f;
10979{
10980 int i;
10981 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10982 XFontStruct *font;
10983 int old_width = dpyinfo->smallest_char_width;
10984 int old_height = dpyinfo->smallest_font_height;
d67d1627 10985
c2cc16fa
JR
10986 dpyinfo->smallest_font_height = 100000;
10987 dpyinfo->smallest_char_width = 100000;
d67d1627 10988
c2cc16fa
JR
10989 for (i = 0; i < dpyinfo->n_fonts; ++i)
10990 if (dpyinfo->font_table[i].name)
10991 {
10992 struct font_info *fontp = dpyinfo->font_table + i;
10993 int w, h;
d67d1627 10994
c2cc16fa
JR
10995 font = (XFontStruct *) fontp->font;
10996 xassert (font != (XFontStruct *) ~0);
10997 x_font_min_bounds (font, &w, &h);
d67d1627 10998
c2cc16fa
JR
10999 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
11000 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
11001 }
11002
11003 xassert (dpyinfo->smallest_char_width > 0
11004 && dpyinfo->smallest_font_height > 0);
11005
11006 return (dpyinfo->n_fonts == 1
11007 || dpyinfo->smallest_char_width < old_width
11008 || dpyinfo->smallest_font_height < old_height);
11009}
11010
11011/* The following functions are listed here to help diff stay in step
11012 with xterm.c. See w32fns.c for definitions.
11013
11014x_load_font (f, fontname, size)
11015x_query_font (f, fontname)
11016x_find_ccl_program (fontp)
11017
11018*/
ee78dc32 11019\f
791f420f
JR
11020/***********************************************************************
11021 Initialization
11022 ***********************************************************************/
ee78dc32 11023
fbd6baed 11024static int w32_initialized = 0;
ee78dc32 11025
f7737f5d
JR
11026void
11027w32_initialize_display_info (display_name)
11028 Lisp_Object display_name;
11029{
11030 struct w32_display_info *dpyinfo = &one_w32_display_info;
11031
11032 bzero (dpyinfo, sizeof (*dpyinfo));
11033
11034 /* Put it on w32_display_name_list. */
11035 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
11036 w32_display_name_list);
11037 dpyinfo->name_list_element = XCAR (w32_display_name_list);
d67d1627 11038
f7737f5d 11039 dpyinfo->w32_id_name
d5db4077
KR
11040 = (char *) xmalloc (SCHARS (Vinvocation_name)
11041 + SCHARS (Vsystem_name)
f7737f5d
JR
11042 + 2);
11043 sprintf (dpyinfo->w32_id_name, "%s@%s",
d5db4077 11044 SDATA (Vinvocation_name), SDATA (Vsystem_name));
f7737f5d
JR
11045
11046 /* Default Console mode values - overridden when running in GUI mode
11047 with values obtained from system metrics. */
11048 dpyinfo->resx = 1;
11049 dpyinfo->resy = 1;
11050 dpyinfo->height_in = 1;
11051 dpyinfo->width_in = 1;
11052 dpyinfo->n_planes = 1;
11053 dpyinfo->n_cbits = 4;
11054 dpyinfo->n_fonts = 0;
11055 dpyinfo->smallest_font_height = 1;
11056 dpyinfo->smallest_char_width = 1;
11057
11058 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
11059 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
11060 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
11061 dpyinfo->mouse_face_window = Qnil;
9f5a911b 11062 dpyinfo->mouse_face_overlay = Qnil;
3d26a7c2 11063 dpyinfo->mouse_face_hidden = 0;
01b220b6 11064 /* TODO: dpyinfo->gray */
f7737f5d
JR
11065
11066}
11067
fbd6baed
GV
11068struct w32_display_info *
11069w32_term_init (display_name, xrm_option, resource_name)
ee78dc32
GV
11070 Lisp_Object display_name;
11071 char *xrm_option;
11072 char *resource_name;
11073{
fbd6baed 11074 struct w32_display_info *dpyinfo;
ee78dc32 11075 HDC hdc;
d67d1627 11076
ee78dc32 11077 BLOCK_INPUT;
d67d1627 11078
fbd6baed 11079 if (!w32_initialized)
ee78dc32 11080 {
fbd6baed
GV
11081 w32_initialize ();
11082 w32_initialized = 1;
ee78dc32 11083 }
d67d1627 11084
ee78dc32
GV
11085 {
11086 int argc = 0;
11087 char *argv[3];
11088
11089 argv[0] = "";
11090 argc = 1;
11091 if (xrm_option)
11092 {
11093 argv[argc++] = "-xrm";
11094 argv[argc++] = xrm_option;
11095 }
11096 }
d67d1627 11097
f7737f5d 11098 w32_initialize_display_info (display_name);
ee78dc32 11099
f7737f5d 11100 dpyinfo = &one_w32_display_info;
8c3b00cb
AI
11101
11102 /* Put this display on the chain. */
11103 dpyinfo->next = x_display_list;
11104 x_display_list = dpyinfo;
d67d1627 11105
52cf03a1 11106 hdc = GetDC (GetDesktopWindow ());
791f420f 11107
ee78dc32
GV
11108 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
11109 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
11110 dpyinfo->root_window = GetDesktopWindow ();
11111 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
11112 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
55689ab1
JR
11113 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
11114 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
52cf03a1 11115 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
791f420f 11116 dpyinfo->image_cache = make_image_cache ();
55689ab1
JR
11117 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
11118 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
ee78dc32 11119 ReleaseDC (GetDesktopWindow (), hdc);
52cf03a1
GV
11120
11121 /* initialise palette with white and black */
11122 {
316ed821 11123 XColor color;
55689ab1
JR
11124 w32_defined_color (0, "white", &color, 1);
11125 w32_defined_color (0, "black", &color, 1);
52cf03a1
GV
11126 }
11127
f7737f5d
JR
11128 /* Create Row Bitmaps and store them for later use. */
11129 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
11130 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
11131 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
11132 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
11133 1, continued_bits);
11134 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
11135 1, 1, continuation_bits);
11136 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
11137
ee78dc32
GV
11138#ifndef F_SETOWN_BUG
11139#ifdef F_SETOWN
11140#ifdef F_SETOWN_SOCK_NEG
11141 /* stdin is a socket here */
11142 fcntl (connection, F_SETOWN, -getpid ());
11143#else /* ! defined (F_SETOWN_SOCK_NEG) */
11144 fcntl (connection, F_SETOWN, getpid ());
11145#endif /* ! defined (F_SETOWN_SOCK_NEG) */
11146#endif /* ! defined (F_SETOWN) */
11147#endif /* F_SETOWN_BUG */
11148
11149#ifdef SIGIO
11150 if (interrupt_input)
11151 init_sigio (connection);
11152#endif /* ! defined (SIGIO) */
11153
11154 UNBLOCK_INPUT;
11155
11156 return dpyinfo;
11157}
11158\f
11159/* Get rid of display DPYINFO, assuming all frames are already gone. */
11160
11161void
11162x_delete_display (dpyinfo)
fbd6baed 11163 struct w32_display_info *dpyinfo;
ee78dc32 11164{
fbd6baed 11165 /* Discard this display from w32_display_name_list and w32_display_list.
ee78dc32 11166 We can't use Fdelq because that can quit. */
fbd6baed 11167 if (! NILP (w32_display_name_list)
8e713be6
KR
11168 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
11169 w32_display_name_list = XCDR (w32_display_name_list);
ee78dc32
GV
11170 else
11171 {
11172 Lisp_Object tail;
11173
fbd6baed 11174 tail = w32_display_name_list;
8e713be6 11175 while (CONSP (tail) && CONSP (XCDR (tail)))
ee78dc32 11176 {
f7737f5d 11177 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
ee78dc32 11178 {
f3fbd155 11179 XSETCDR (tail, XCDR (XCDR (tail)));
ee78dc32
GV
11180 break;
11181 }
8e713be6 11182 tail = XCDR (tail);
ee78dc32
GV
11183 }
11184 }
11185
52cf03a1
GV
11186 /* free palette table */
11187 {
fbd6baed 11188 struct w32_palette_entry * plist;
52cf03a1
GV
11189
11190 plist = dpyinfo->color_list;
11191 while (plist)
11192 {
fbd6baed 11193 struct w32_palette_entry * pentry = plist;
52cf03a1 11194 plist = plist->next;
9127e20e 11195 xfree (pentry);
52cf03a1
GV
11196 }
11197 dpyinfo->color_list = NULL;
11198 if (dpyinfo->palette)
11199 DeleteObject(dpyinfo->palette);
11200 }
ee78dc32 11201 xfree (dpyinfo->font_table);
fbd6baed 11202 xfree (dpyinfo->w32_id_name);
f7737f5d
JR
11203
11204 /* Destroy row bitmaps. */
11205 DeleteObject (left_bmp);
11206 DeleteObject (ov_bmp);
11207 DeleteObject (right_bmp);
11208 DeleteObject (continued_bmp);
11209 DeleteObject (continuation_bmp);
11210 DeleteObject (zv_bmp);
ee78dc32
GV
11211}
11212\f
fbd6baed 11213/* Set up use of W32. */
ee78dc32 11214
689004fa 11215DWORD w32_msg_worker ();
ee78dc32 11216
791f420f
JR
11217void
11218x_flush (struct frame * f)
11219{ /* Nothing to do */ }
11220
11221static struct redisplay_interface w32_redisplay_interface =
11222{
11223 x_produce_glyphs,
11224 x_write_glyphs,
11225 x_insert_glyphs,
11226 x_clear_end_of_line,
11227 x_scroll_run,
11228 x_after_update_window_line,
11229 x_update_window_begin,
11230 x_update_window_end,
11231 w32_cursor_to,
11232 x_flush,
ec48c3a7 11233 x_clear_mouse_face,
791f420f
JR
11234 x_get_glyph_overhangs,
11235 x_fix_overlapping_area
11236};
11237
11238void
fbd6baed
GV
11239w32_initialize ()
11240{
791f420f
JR
11241 rif = &w32_redisplay_interface;
11242
96214669
GV
11243 /* MSVC does not type K&R functions with no arguments correctly, and
11244 so we must explicitly cast them. */
791f420f 11245 clear_frame_hook = (void (*)(void)) x_clear_frame;
96214669 11246 ring_bell_hook = (void (*)(void)) w32_ring_bell;
791f420f
JR
11247 update_begin_hook = x_update_begin;
11248 update_end_hook = x_update_end;
0f32f023 11249
ee78dc32 11250 read_socket_hook = w32_read_socket;
0f32f023 11251
fbd6baed 11252 frame_up_to_date_hook = w32_frame_up_to_date;
0f32f023 11253
fbd6baed
GV
11254 mouse_position_hook = w32_mouse_position;
11255 frame_rehighlight_hook = w32_frame_rehighlight;
11256 frame_raise_lower_hook = w32_frame_raise_lower;
11257 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
11258 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
11259 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
11260 judge_scroll_bars_hook = w32_judge_scroll_bars;
791f420f 11261 estimate_mode_line_height_hook = x_estimate_mode_line_height;
ee78dc32
GV
11262
11263 scroll_region_ok = 1; /* we'll scroll partial frames */
49be9f70 11264 char_ins_del_ok = 1;
ee78dc32
GV
11265 line_ins_del_ok = 1; /* we'll just blt 'em */
11266 fast_clear_end_of_line = 1; /* X does this well */
11267 memory_below_frame = 0; /* we don't remember what scrolls
11268 off the bottom */
11269 baud_rate = 19200;
11270
abb15ebd
JR
11271 w32_system_caret_hwnd = NULL;
11272 w32_system_caret_height = 0;
abb15ebd
JR
11273 w32_system_caret_x = 0;
11274 w32_system_caret_y = 0;
11275
791f420f
JR
11276 last_tool_bar_item = -1;
11277 any_help_event_p = 0;
11278
689004fa
GV
11279 /* Initialize input mode: interrupt_input off, no flow control, allow
11280 8 bit character input, standard quit char. */
11281 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
ee78dc32
GV
11282
11283 /* Create the window thread - it will terminate itself or when the app terminates */
11284
11285 init_crit ();
11286
11287 dwMainThreadId = GetCurrentThreadId ();
d67d1627 11288 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
ee78dc32
GV
11289 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
11290
11291 /* Wait for thread to start */
11292
11293 {
11294 MSG msg;
11295
11296 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
11297
d67d1627
JB
11298 hWindowsThread = CreateThread (NULL, 0,
11299 (LPTHREAD_START_ROUTINE) w32_msg_worker,
e9e23e23 11300 0, 0, &dwWindowsThreadId);
ee78dc32
GV
11301
11302 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
11303 }
d67d1627 11304
52cf03a1 11305 /* It is desirable that mainThread should have the same notion of
e9e23e23 11306 focus window and active window as windowsThread. Unfortunately, the
52cf03a1
GV
11307 following call to AttachThreadInput, which should do precisely what
11308 we need, causes major problems when Emacs is linked as a console
11309 program. Unfortunately, we have good reasons for doing that, so
e9e23e23 11310 instead we need to send messages to windowsThread to make some API
52cf03a1
GV
11311 calls for us (ones that affect, or depend on, the active/focus
11312 window state. */
11313#ifdef ATTACH_THREADS
e9e23e23 11314 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
52cf03a1 11315#endif
689004fa
GV
11316
11317 /* Dynamically link to optional system components. */
11318 {
11319 HANDLE user_lib = LoadLibrary ("user32.dll");
11320
11321#define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
11322
11323 /* New proportional scroll bar functions. */
9127e20e
JR
11324 LOAD_PROC (SetScrollInfo);
11325 LOAD_PROC (GetScrollInfo);
689004fa
GV
11326
11327#undef LOAD_PROC
11328
11329 FreeLibrary (user_lib);
11330
11331 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
11332 otherwise use the fixed height. */
11333 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
11334 GetSystemMetrics (SM_CYVTHUMB);
11335
11336 /* For either kind of scroll bar, take account of the arrows; these
11337 effectively form the border of the main scroll bar range. */
11338 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
11339 = GetSystemMetrics (SM_CYVSCROLL);
11340 }
ee78dc32
GV
11341}
11342
11343void
fbd6baed 11344syms_of_w32term ()
ee78dc32 11345{
fbd6baed
GV
11346 staticpro (&w32_display_name_list);
11347 w32_display_name_list = Qnil;
ee78dc32
GV
11348
11349 staticpro (&last_mouse_scroll_bar);
11350 last_mouse_scroll_bar = Qnil;
11351
11352 staticpro (&Qvendor_specific_keysyms);
11353 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
52cf03a1 11354
fbd6baed
GV
11355 DEFVAR_INT ("w32-num-mouse-buttons",
11356 &Vw32_num_mouse_buttons,
33f09670 11357 doc: /* Number of physical mouse buttons. */);
fbd6baed 11358 Vw32_num_mouse_buttons = Qnil;
52cf03a1 11359
fbd6baed
GV
11360 DEFVAR_LISP ("w32-swap-mouse-buttons",
11361 &Vw32_swap_mouse_buttons,
33f09670
JR
11362 doc: /* Swap the mapping of middle and right mouse buttons.
11363When nil, middle button is mouse-2 and right button is mouse-3. */);
fbd6baed 11364 Vw32_swap_mouse_buttons = Qnil;
689004fa
GV
11365
11366 DEFVAR_LISP ("w32-grab-focus-on-raise",
11367 &Vw32_grab_focus_on_raise,
33f09670
JR
11368 doc: /* Raised frame grabs input focus.
11369When t, `raise-frame' grabs input focus as well. This fits well
11370with the normal Windows click-to-focus policy, but might not be
11371desirable when using a point-to-focus policy. */);
689004fa
GV
11372 Vw32_grab_focus_on_raise = Qt;
11373
11374 DEFVAR_LISP ("w32-capslock-is-shiftlock",
11375 &Vw32_capslock_is_shiftlock,
33f09670
JR
11376 doc: /* Apply CapsLock state to non character input keys.
11377When nil, CapsLock only affects normal character input keys. */);
689004fa 11378 Vw32_capslock_is_shiftlock = Qnil;
ef0e360f
GV
11379
11380 DEFVAR_LISP ("w32-recognize-altgr",
11381 &Vw32_recognize_altgr,
33f09670
JR
11382 doc: /* Recognize right-alt and left-ctrl as AltGr.
11383When nil, the right-alt and left-ctrl key combination is
d67d1627 11384interpreted normally. */);
ef0e360f 11385 Vw32_recognize_altgr = Qt;
bc6af935 11386
484fa2c1
GV
11387 DEFVAR_BOOL ("w32-enable-unicode-output",
11388 &w32_enable_unicode_output,
33f09670
JR
11389 doc: /* Enable the use of Unicode for text output if non-nil.
11390Unicode output may prevent some third party applications for displaying
11391Far-East Languages on Windows 95/98 from working properly.
11392NT uses Unicode internally anyway, so this flag will probably have no
11393affect on NT machines. */);
484fa2c1 11394 w32_enable_unicode_output = 1;
a1b9438c 11395
791f420f 11396 help_echo = Qnil;
ec48c3a7
JR
11397 staticpro (&help_echo);
11398 help_echo_object = Qnil;
11399 staticpro (&help_echo_object);
158cba56
JR
11400 help_echo_window = Qnil;
11401 staticpro (&help_echo_window);
791f420f 11402 previous_help_echo = Qnil;
ec48c3a7 11403 staticpro (&previous_help_echo);
158cba56 11404 help_echo_pos = -1;
791f420f 11405
ee8ceff8 11406 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
549808db 11407 doc: /* *Non-nil means autoselect window with mouse pointer. */);
ee8ceff8 11408 mouse_autoselect_window = 0;
549808db 11409
99558ce8
JR
11410 DEFVAR_BOOL ("w32-use-visible-system-caret",
11411 &w32_use_visible_system_caret,
11412 doc: /* Flag to make the system caret visible.
11413When this is non-nil, Emacs will indicate the position of point by
11414using the system caret instead of drawing its own cursor. Some screen
11415reader software does not track the system cursor properly when it is
11416invisible, and gets confused by Emacs drawing its own cursor, so this
d67d1627 11417variable is initialized to t when Emacs detects that screen reader
99558ce8
JR
11418software is running as it starts up.
11419
11420When this variable is set, other variables affecting the appearance of
11421the cursor have no effect. */);
11422
11423 /* Initialize w32_use_visible_system_caret based on whether a screen
11424 reader is in use. */
ab76d376
JR
11425 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
11426 &w32_use_visible_system_caret, 0))
11427 w32_use_visible_system_caret = 0;
99558ce8 11428
791f420f 11429 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
33f09670
JR
11430 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
11431For example, if a block cursor is over a tab, it will be drawn as
11432wide as that tab on the display. */);
791f420f
JR
11433 x_stretch_cursor_p = 0;
11434
9f5a911b
JR
11435 DEFVAR_BOOL ("x-use-underline-position-properties",
11436 &x_use_underline_position_properties,
33f09670 11437 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
f0529b5b 11438nil means ignore them. If you encounter fonts with bogus
33f09670
JR
11439UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11440to 4.1, set this to nil. */);
9f5a911b 11441 x_use_underline_position_properties = 1;
9f5a911b 11442
5bf04520 11443 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
33f09670 11444 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
5bf04520 11445 Vx_toolkit_scroll_bars = Qt;
791f420f
JR
11446
11447 staticpro (&last_mouse_motion_frame);
11448 last_mouse_motion_frame = Qnil;
ee78dc32 11449}