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