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