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