(w32_createwindow): Remove the WS_CLIPCHILDREN style
[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{
858a55c1
AI
4303 int relief_drawn_p = 0;
4304
791f420f
JR
4305 /* If S draws into the background of its successor, draw the
4306 background of the successor first so that S can draw into it.
4307 This makes S->next use XDrawString instead of XDrawImageString. */
4308 if (s->next && s->right_overhang && !s->for_overlaps_p)
4309 {
4310 xassert (s->next->img == NULL);
4311 x_set_glyph_string_gc (s->next);
4312 x_set_glyph_string_clipping (s->next);
4313 x_draw_glyph_string_background (s->next, 1);
4314 }
4315
4316 /* Set up S->gc, set clipping and draw S. */
4317 x_set_glyph_string_gc (s);
4318 x_set_glyph_string_clipping (s);
4319
858a55c1
AI
4320 /* Draw relief (if any) in advance for char/composition so that the
4321 glyph string can be drawn over it. */
4322 if (!s->for_overlaps_p
4323 && s->face->box != FACE_NO_BOX
4324 && (s->first_glyph->type == CHAR_GLYPH
4325 || s->first_glyph->type == COMPOSITE_GLYPH))
4326
4327 {
4328 x_draw_glyph_string_background (s, 1);
4329 x_draw_glyph_string_box (s);
4330 relief_drawn_p = 1;
4331 }
4332
791f420f
JR
4333 switch (s->first_glyph->type)
4334 {
4335 case IMAGE_GLYPH:
4336 x_draw_image_glyph_string (s);
4337 break;
4338
4339 case STRETCH_GLYPH:
4340 x_draw_stretch_glyph_string (s);
4341 break;
4342
4343 case CHAR_GLYPH:
4344 if (s->for_overlaps_p)
4345 s->background_filled_p = 1;
4346 else
4347 x_draw_glyph_string_background (s, 0);
4348 x_draw_glyph_string_foreground (s);
4349 break;
4350
4351 case COMPOSITE_GLYPH:
4352 if (s->for_overlaps_p || s->gidx > 0)
4353 s->background_filled_p = 1;
4354 else
4355 x_draw_glyph_string_background (s, 1);
4356 x_draw_composite_glyph_string_foreground (s);
4357 break;
4358
4359 default:
4360 abort ();
4361 }
4362
4363 if (!s->for_overlaps_p)
4364 {
4365 /* Draw underline. */
9ef2e2cf
JR
4366 if (s->face->underline_p
4367 && (s->font->bdf || !s->font->tm.tmUnderlined))
791f420f
JR
4368 {
4369 unsigned long h = 1;
4370 unsigned long dy = s->height - h;
4371
4372 if (s->face->underline_defaulted_p)
4373 {
4374 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4375 s->y + dy, s->width, 1);
4376 }
4377 else
4378 {
4379 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4380 s->y + dy, s->width, 1);
4381 }
4382 }
4383
4384 /* Draw overline. */
4385 if (s->face->overline_p)
4386 {
4387 unsigned long dy = 0, h = 1;
4388
4389 if (s->face->overline_color_defaulted_p)
4390 {
4391 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4392 s->y + dy, s->width, h);
4393 }
4394 else
4395 {
4396 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4397 s->y + dy, s->width, h);
4398 }
4399 }
4400
4401 /* Draw strike-through. */
9ef2e2cf
JR
4402 if (s->face->strike_through_p
4403 && (s->font->bdf || !s->font->tm.tmStruckOut))
791f420f
JR
4404 {
4405 unsigned long h = 1;
4406 unsigned long dy = (s->height - h) / 2;
4407
4408 if (s->face->strike_through_color_defaulted_p)
4409 {
4410 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
4411 s->width, h);
4412 }
4413 else
4414 {
4415 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4416 s->y + dy, s->width, h);
4417 }
4418 }
4419
4420 /* Draw relief. */
858a55c1 4421 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
791f420f
JR
4422 x_draw_glyph_string_box (s);
4423 }
4424
4425 /* Reset clipping. */
4426 w32_set_clip_rectangle (s->hdc, NULL);
4427}
4428
4429
4430static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4431 struct face **, int));
4432
4433
c2cc16fa
JR
4434/* Fill glyph string S with composition components specified by S->cmp.
4435
791f420f
JR
4436 FACES is an array of faces for all components of this composition.
4437 S->gidx is the index of the first component for S.
4438 OVERLAPS_P non-zero means S should draw the foreground only, and
c2cc16fa 4439 use its physical height for clipping.
791f420f
JR
4440
4441 Value is the index of a component not in S. */
4442
4443static int
4444x_fill_composite_glyph_string (s, faces, overlaps_p)
4445 struct glyph_string *s;
4446 struct face **faces;
4447 int overlaps_p;
4448{
4449 int i;
4450
4451 xassert (s);
4452
4453 s->for_overlaps_p = overlaps_p;
4454
4455 s->face = faces[s->gidx];
4456 s->font = s->face->font;
4457 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4458
4459 /* For all glyphs of this composition, starting at the offset
4460 S->gidx, until we reach the end of the definition or encounter a
4461 glyph that requires the different face, add it to S. */
4462 ++s->nchars;
4463 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4464 ++s->nchars;
4465
4466 /* All glyph strings for the same composition has the same width,
4467 i.e. the width set for the first component of the composition. */
4468
4469 s->width = s->first_glyph->pixel_width;
4470
4471 /* If the specified font could not be loaded, use the frame's
4472 default font, but record the fact that we couldn't load it in
4473 the glyph string so that we can draw rectangles for the
4474 characters of the glyph string. */
4475 if (s->font == NULL)
4476 {
4477 s->font_not_found_p = 1;
4478 s->font = FRAME_FONT (s->f);
4479 }
4480
4481 /* Adjust base line for subscript/superscript text. */
4482 s->ybase += s->first_glyph->voffset;
4483
4484 xassert (s->face && s->face->gc);
4485
4486 /* This glyph string must always be drawn with 16-bit functions. */
4487 s->two_byte_p = 1;
4488
4489 return s->gidx + s->nchars;
4490}
4491
4492
c2cc16fa
JR
4493/* Fill glyph string S from a sequence of character glyphs.
4494
791f420f
JR
4495 FACE_ID is the face id of the string. START is the index of the
4496 first glyph to consider, END is the index of the last + 1.
4497 OVERLAPS_P non-zero means S should draw the foreground only, and
c2cc16fa 4498 use its physical height for clipping.
791f420f
JR
4499
4500 Value is the index of the first glyph not in S. */
4501
4502static int
4503x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4504 struct glyph_string *s;
4505 int face_id;
4506 int start, end, overlaps_p;
4507{
4508 struct glyph *glyph, *last;
4509 int voffset;
93ff4395 4510 int glyph_not_available_p;
791f420f
JR
4511
4512 xassert (s->f == XFRAME (s->w->frame));
4513 xassert (s->nchars == 0);
4514 xassert (start >= 0 && end > start);
4515
4516 s->for_overlaps_p = overlaps_p;
791f420f
JR
4517 glyph = s->row->glyphs[s->area] + start;
4518 last = s->row->glyphs[s->area] + end;
4519 voffset = glyph->voffset;
93ff4395
JR
4520
4521 glyph_not_available_p = glyph->glyph_not_available_p;
4522
791f420f
JR
4523 while (glyph < last
4524 && glyph->type == CHAR_GLYPH
4525 && glyph->voffset == voffset
93ff4395
JR
4526 /* Same face id implies same font, nowadays. */
4527 && glyph->face_id == face_id
4528 && glyph->glyph_not_available_p == glyph_not_available_p)
791f420f 4529 {
93ff4395
JR
4530 int two_byte_p;
4531
791f420f 4532 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
93ff4395
JR
4533 s->char2b + s->nchars,
4534 &two_byte_p);
4535 s->two_byte_p = two_byte_p;
791f420f
JR
4536 ++s->nchars;
4537 xassert (s->nchars <= end - start);
4538 s->width += glyph->pixel_width;
4539 ++glyph;
4540 }
4541
4542 s->font = s->face->font;
4543 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4544
4545 /* If the specified font could not be loaded, use the frame's font,
4546 but record the fact that we couldn't load it in
4547 S->font_not_found_p so that we can draw rectangles for the
4548 characters of the glyph string. */
93ff4395 4549 if (s->font == NULL || glyph_not_available_p)
791f420f
JR
4550 {
4551 s->font_not_found_p = 1;
4552 s->font = FRAME_FONT (s->f);
4553 }
4554
4555 /* Adjust base line for subscript/superscript text. */
4556 s->ybase += voffset;
4557
4558 xassert (s->face && s->face->gc);
4559 return glyph - s->row->glyphs[s->area];
4560}
4561
4562
4563/* Fill glyph string S from image glyph S->first_glyph. */
4564
4565static void
4566x_fill_image_glyph_string (s)
4567 struct glyph_string *s;
4568{
4569 xassert (s->first_glyph->type == IMAGE_GLYPH);
4570 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4571 xassert (s->img);
4572 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4573 s->font = s->face->font;
4574 s->width = s->first_glyph->pixel_width;
4575
4576 /* Adjust base line for subscript/superscript text. */
4577 s->ybase += s->first_glyph->voffset;
4578}
4579
4580
ec48c3a7 4581/* Fill glyph string S from a sequence of stretch glyphs.
791f420f 4582
ec48c3a7
JR
4583 ROW is the glyph row in which the glyphs are found, AREA is the
4584 area within the row. START is the index of the first glyph to
4585 consider, END is the index of the last + 1.
4586
4587 Value is the index of the first glyph not in S. */
4588
4589static int
4590x_fill_stretch_glyph_string (s, row, area, start, end)
791f420f 4591 struct glyph_string *s;
ec48c3a7
JR
4592 struct glyph_row *row;
4593 enum glyph_row_area area;
4594 int start, end;
791f420f 4595{
ec48c3a7
JR
4596 struct glyph *glyph, *last;
4597 int voffset, face_id;
4598
791f420f 4599 xassert (s->first_glyph->type == STRETCH_GLYPH);
ec48c3a7
JR
4600
4601 glyph = s->row->glyphs[s->area] + start;
4602 last = s->row->glyphs[s->area] + end;
4603 face_id = glyph->face_id;
4604 s->face = FACE_FROM_ID (s->f, face_id);
791f420f 4605 s->font = s->face->font;
ec48c3a7
JR
4606 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4607 s->width = glyph->pixel_width;
4608 voffset = glyph->voffset;
4609
4610 for (++glyph;
4611 (glyph < last
4612 && glyph->type == STRETCH_GLYPH
4613 && glyph->voffset == voffset
4614 && glyph->face_id == face_id);
4615 ++glyph)
4616 s->width += glyph->pixel_width;
791f420f
JR
4617
4618 /* Adjust base line for subscript/superscript text. */
ec48c3a7
JR
4619 s->ybase += voffset;
4620
4621 xassert (s->face && s->face->gc);
4622 return glyph - s->row->glyphs[s->area];
791f420f
JR
4623}
4624
4625
4626/* Initialize glyph string S. CHAR2B is a suitably allocated vector
4627 of XChar2b structures for S; it can't be allocated in
4628 x_init_glyph_string because it must be allocated via `alloca'. W
4629 is the window on which S is drawn. ROW and AREA are the glyph row
4630 and area within the row from which S is constructed. START is the
4631 index of the first glyph structure covered by S. HL is a
4632 face-override for drawing S. */
4633
4634static void
4635w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4636 struct glyph_string *s;
4637 HDC hdc;
4638 wchar_t *char2b;
4639 struct window *w;
4640 struct glyph_row *row;
4641 enum glyph_row_area area;
4642 int start;
4643 enum draw_glyphs_face hl;
4644{
4645 bzero (s, sizeof *s);
4646 s->w = w;
4647 s->f = XFRAME (w->frame);
4648 s->hdc = hdc;
4649 s->window = FRAME_W32_WINDOW (s->f);
4650 s->char2b = char2b;
4651 s->hl = hl;
4652 s->row = row;
4653 s->area = area;
4654 s->first_glyph = row->glyphs[area] + start;
4655 s->height = row->height;
4656 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4657
4658 /* Display the internal border below the tool-bar window. */
4659 if (s->w == XWINDOW (s->f->tool_bar_window))
4660 s->y -= s->f->output_data.w32->internal_border_width;
4661
4662 s->ybase = s->y + row->ascent;
4663}
4664
4665
4666/* Set background width of glyph string S. START is the index of the
4667 first glyph following S. LAST_X is the right-most x-position + 1
4668 in the drawing area. */
4669
4670static INLINE void
4671x_set_glyph_string_background_width (s, start, last_x)
4672 struct glyph_string *s;
4673 int start;
4674 int last_x;
4675{
4676 /* If the face of this glyph string has to be drawn to the end of
4677 the drawing area, set S->extends_to_end_of_line_p. */
4678 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4679
4680 if (start == s->row->used[s->area]
5b844253 4681 && s->area == TEXT_AREA
f4fccc1e
EZ
4682 && ((s->hl == DRAW_NORMAL_TEXT
4683 && (s->row->fill_line_p
4684 || s->face->background != default_face->background
4685 || s->face->stipple != default_face->stipple
4686 || s->row->mouse_face_p))
4687 || s->hl == DRAW_MOUSE_FACE))
791f420f
JR
4688 s->extends_to_end_of_line_p = 1;
4689
4690 /* If S extends its face to the end of the line, set its
4691 background_width to the distance to the right edge of the drawing
4692 area. */
4693 if (s->extends_to_end_of_line_p)
4694 s->background_width = last_x - s->x + 1;
4695 else
4696 s->background_width = s->width;
4697}
4698
4699
4700/* Add a glyph string for a stretch glyph to the list of strings
4701 between HEAD and TAIL. START is the index of the stretch glyph in
4702 row area AREA of glyph row ROW. END is the index of the last glyph
4703 in that glyph row area. X is the current output position assigned
4704 to the new glyph string constructed. HL overrides that face of the
4705 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4706 is the right-most x-position of the drawing area. */
4707
4708#define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4709 do \
4710 { \
4711 s = (struct glyph_string *) alloca (sizeof *s); \
4712 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
ec48c3a7 4713 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
791f420f 4714 x_append_glyph_string (&HEAD, &TAIL, s); \
791f420f
JR
4715 s->x = (X); \
4716 } \
4717 while (0)
4718
4719
4720/* Add a glyph string for an image glyph to the list of strings
4721 between HEAD and TAIL. START is the index of the image glyph in
4722 row area AREA of glyph row ROW. END is the index of the last glyph
4723 in that glyph row area. X is the current output position assigned
4724 to the new glyph string constructed. HL overrides that face of the
4725 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4726 is the right-most x-position of the drawing area. */
4727
4728#define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4729 do \
4730 { \
4731 s = (struct glyph_string *) alloca (sizeof *s); \
4732 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4733 x_fill_image_glyph_string (s); \
4734 x_append_glyph_string (&HEAD, &TAIL, s); \
4735 ++START; \
4736 s->x = (X); \
4737 } \
4738 while (0)
4739
4740
4741/* Add a glyph string for a sequence of character glyphs to the list
4742 of strings between HEAD and TAIL. START is the index of the first
4743 glyph in row area AREA of glyph row ROW that is part of the new
4744 glyph string. END is the index of the last glyph in that glyph row
4745 area. X is the current output position assigned to the new glyph
4746 string constructed. HL overrides that face of the glyph; e.g. it
4747 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4748 right-most x-position of the drawing area. */
4749
4750#define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4751 do \
4752 { \
ec48c3a7 4753 int c, face_id; \
791f420f
JR
4754 wchar_t *char2b; \
4755 \
4756 c = (ROW)->glyphs[AREA][START].u.ch; \
791f420f 4757 face_id = (ROW)->glyphs[AREA][START].face_id; \
9ef2e2cf 4758 \
791f420f
JR
4759 s = (struct glyph_string *) alloca (sizeof *s); \
4760 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4761 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4762 x_append_glyph_string (&HEAD, &TAIL, s); \
791f420f
JR
4763 s->x = (X); \
4764 START = x_fill_glyph_string (s, face_id, START, END, \
4765 OVERLAPS_P); \
4766 } \
4767 while (0)
4768
4769
4770/* Add a glyph string for a composite sequence to the list of strings
4771 between HEAD and TAIL. START is the index of the first glyph in
4772 row area AREA of glyph row ROW that is part of the new glyph
4773 string. END is the index of the last glyph in that glyph row area.
4774 X is the current output position assigned to the new glyph string
4775 constructed. HL overrides that face of the glyph; e.g. it is
4776 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4777 x-position of the drawing area. */
4778
4779#define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4780 do { \
4781 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4782 int face_id = (ROW)->glyphs[AREA][START].face_id; \
93ff4395 4783 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
791f420f
JR
4784 struct composition *cmp = composition_table[cmp_id]; \
4785 int glyph_len = cmp->glyph_len; \
4786 wchar_t *char2b; \
4787 struct face **faces; \
4788 struct glyph_string *first_s = NULL; \
4789 int n; \
4790 \
93ff4395 4791 base_face = base_face->ascii_face; \
791f420f
JR
4792 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4793 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4794 /* At first, fill in `char2b' and `faces'. */ \
4795 for (n = 0; n < glyph_len; n++) \
4796 { \
4797 int c = COMPOSITION_GLYPH (cmp, n); \
93ff4395
JR
4798 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4799 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4800 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4801 this_face_id, char2b + n, 1); \
791f420f
JR
4802 } \
4803 \
4804 /* Make glyph_strings for each glyph sequence that is drawable by \
4805 the same face, and append them to HEAD/TAIL. */ \
4806 for (n = 0; n < cmp->glyph_len;) \
4807 { \
4808 s = (struct glyph_string *) alloca (sizeof *s); \
4809 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4810 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4811 s->cmp = cmp; \
4812 s->gidx = n; \
791f420f
JR
4813 s->x = (X); \
4814 \
4815 if (n == 0) \
4816 first_s = s; \
4817 \
4818 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4819 } \
4820 \
4821 ++START; \
4822 s = first_s; \
4823 } while (0)
4824
9ef2e2cf 4825
791f420f
JR
4826/* Build a list of glyph strings between HEAD and TAIL for the glyphs
4827 of AREA of glyph row ROW on window W between indices START and END.
4828 HL overrides the face for drawing glyph strings, e.g. it is
4829 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4830 x-positions of the drawing area.
4831
4832 This is an ugly monster macro construct because we must use alloca
4833 to allocate glyph strings (because x_draw_glyphs can be called
4834 asynchronously). */
4835
4836#define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4837 do \
4838 { \
4839 HEAD = TAIL = NULL; \
4840 while (START < END) \
4841 { \
4842 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4843 switch (first_glyph->type) \
4844 { \
4845 case CHAR_GLYPH: \
4846 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4847 HEAD, TAIL, HL, X, LAST_X, \
4848 OVERLAPS_P); \
4849 break; \
4850 \
4851 case COMPOSITE_GLYPH: \
4852 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4853 END, HEAD, TAIL, HL, X, \
4854 LAST_X, OVERLAPS_P); \
4855 break; \
4856 \
4857 case STRETCH_GLYPH: \
4858 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4859 HEAD, TAIL, HL, X, LAST_X); \
4860 break; \
4861 \
4862 case IMAGE_GLYPH: \
4863 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4864 HEAD, TAIL, HL, X, LAST_X); \
4865 break; \
4866 \
4867 default: \
4868 abort (); \
4869 } \
4870 \
4871 x_set_glyph_string_background_width (s, START, LAST_X); \
4872 (X) += s->width; \
4873 } \
4874 } \
4875 while (0)
4876
4877
4878/* Draw glyphs between START and END in AREA of ROW on window W,
4879 starting at x-position X. X is relative to AREA in W. HL is a
4880 face-override with the following meaning:
4881
4882 DRAW_NORMAL_TEXT draw normally
4883 DRAW_CURSOR draw in cursor face
4884 DRAW_MOUSE_FACE draw in mouse face.
4885 DRAW_INVERSE_VIDEO draw in mode line face
4886 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4887 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4888
4889 If REAL_START is non-null, return in *REAL_START the real starting
4890 position for display. This can be different from START in case
4891 overlapping glyphs must be displayed. If REAL_END is non-null,
4892 return in *REAL_END the real end position for display. This can be
4893 different from END in case overlapping glyphs must be displayed.
4894
4895 If OVERLAPS_P is non-zero, draw only the foreground of characters
4896 and clip to the physical height of ROW.
4897
4898 Value is the x-position reached, relative to AREA of W. */
4899
4900static int
4901x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4902 overlaps_p)
4903 struct window *w;
4904 int x;
4905 struct glyph_row *row;
4906 enum glyph_row_area area;
4907 int start, end;
4908 enum draw_glyphs_face hl;
4909 int *real_start, *real_end;
4910 int overlaps_p;
4911{
4912 struct glyph_string *head, *tail;
4913 struct glyph_string *s;
4914 int last_x, area_width;
4915 int x_reached;
4916 int i, j;
4917 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4918
4919 /* Let's rather be paranoid than getting a SEGV. */
791f420f 4920 end = min (end, row->used[area]);
9127e20e
JR
4921 start = max (0, start);
4922 start = min (end, start);
791f420f
JR
4923 if (real_start)
4924 *real_start = start;
4925 if (real_end)
4926 *real_end = end;
4927
4928 /* Translate X to frame coordinates. Set last_x to the right
4929 end of the drawing area. */
4930 if (row->full_width_p)
4931 {
791f420f
JR
4932 /* X is relative to the left edge of W, without scroll bars
4933 or flag areas. */
ec48c3a7 4934 struct frame *f = XFRAME (WINDOW_FRAME (w));
791f420f
JR
4935 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4936 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
ee78dc32 4937
791f420f
JR
4938 x += window_left_x;
4939 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4940 last_x = window_left_x + area_width;
ee78dc32 4941
791f420f
JR
4942 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4943 {
4944 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4945 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4946 last_x += width;
4947 else
4948 x -= width;
4949 }
ee78dc32 4950
791f420f
JR
4951 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4952 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4953 }
4954 else
4955 {
4956 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4957 area_width = window_box_width (w, area);
4958 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4959 }
e12ca9c2 4960
791f420f
JR
4961 /* Build a doubly-linked list of glyph_string structures between
4962 head and tail from what we have to draw. Note that the macro
4963 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4964 the reason we use a separate variable `i'. */
4965 i = start;
4966 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4967 overlaps_p);
791f420f
JR
4968 if (tail)
4969 x_reached = tail->x + tail->background_width;
4970 else
4971 x_reached = x;
e12ca9c2 4972
791f420f
JR
4973 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4974 the row, redraw some glyphs in front or following the glyph
4975 strings built above. */
9127e20e 4976 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
791f420f
JR
4977 {
4978 int dummy_x = 0;
4979 struct glyph_string *h, *t;
4980
4981 /* Compute overhangs for all glyph strings. */
4982 for (s = head; s; s = s->next)
4983 x_compute_glyph_string_overhangs (s);
4984
4985 /* Prepend glyph strings for glyphs in front of the first glyph
4986 string that are overwritten because of the first glyph
4987 string's left overhang. The background of all strings
4988 prepended must be drawn because the first glyph string
4989 draws over it. */
4990 i = x_left_overwritten (head);
4991 if (i >= 0)
4992 {
4993 j = i;
4994 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
4995 DRAW_NORMAL_TEXT, dummy_x, last_x,
4996 overlaps_p);
4997 start = i;
4998 if (real_start)
4999 *real_start = start;
5000 x_compute_overhangs_and_x (t, head->x, 1);
5001 x_prepend_glyph_string_lists (&head, &tail, h, t);
5002 }
ee78dc32 5003
791f420f
JR
5004 /* Prepend glyph strings for glyphs in front of the first glyph
5005 string that overwrite that glyph string because of their
5006 right overhang. For these strings, only the foreground must
5007 be drawn, because it draws over the glyph string at `head'.
5008 The background must not be drawn because this would overwrite
5009 right overhangs of preceding glyphs for which no glyph
5010 strings exist. */
5011 i = x_left_overwriting (head);
5012 if (i >= 0)
5013 {
5014 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
5015 DRAW_NORMAL_TEXT, dummy_x, last_x,
5016 overlaps_p);
791f420f
JR
5017 for (s = h; s; s = s->next)
5018 s->background_filled_p = 1;
5019 if (real_start)
5020 *real_start = i;
5021 x_compute_overhangs_and_x (t, head->x, 1);
5022 x_prepend_glyph_string_lists (&head, &tail, h, t);
5023 }
cabb23bc 5024
791f420f
JR
5025 /* Append glyphs strings for glyphs following the last glyph
5026 string tail that are overwritten by tail. The background of
5027 these strings has to be drawn because tail's foreground draws
5028 over it. */
5029 i = x_right_overwritten (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 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5036 x_append_glyph_string_lists (&head, &tail, h, t);
5037 if (real_end)
5038 *real_end = i;
5039 }
cabb23bc 5040
791f420f
JR
5041 /* Append glyph strings for glyphs following the last glyph
5042 string tail that overwrite tail. The foreground of such
5043 glyphs has to be drawn because it writes into the background
5044 of tail. The background must not be drawn because it could
5045 paint over the foreground of following glyphs. */
5046 i = x_right_overwriting (tail);
5047 if (i >= 0)
5048 {
5049 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
5050 DRAW_NORMAL_TEXT, x, last_x,
5051 overlaps_p);
791f420f
JR
5052 for (s = h; s; s = s->next)
5053 s->background_filled_p = 1;
5054 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5055 x_append_glyph_string_lists (&head, &tail, h, t);
5056 if (real_end)
5057 *real_end = i;
5058 }
5059 }
cabb23bc 5060
791f420f
JR
5061 /* Draw all strings. */
5062 for (s = head; s; s = s->next)
5063 x_draw_glyph_string (s);
cabb23bc 5064
791f420f
JR
5065 /* Value is the x-position up to which drawn, relative to AREA of W.
5066 This doesn't include parts drawn because of overhangs. */
5067 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5068 if (!row->full_width_p)
5069 {
5070 if (area > LEFT_MARGIN_AREA)
5071 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5072 if (area > TEXT_AREA)
5073 x_reached -= window_box_width (w, TEXT_AREA);
5074 }
cabb23bc 5075
791f420f 5076 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
65c4903c 5077
791f420f
JR
5078 return x_reached;
5079}
65c4903c 5080
cabb23bc 5081
791f420f
JR
5082/* Fix the display of area AREA of overlapping row ROW in window W. */
5083
5084static void
5085x_fix_overlapping_area (w, row, area)
5086 struct window *w;
5087 struct glyph_row *row;
5088 enum glyph_row_area area;
5089{
5090 int i, x;
5091
5092 BLOCK_INPUT;
5093
5094 if (area == LEFT_MARGIN_AREA)
5095 x = 0;
5096 else if (area == TEXT_AREA)
5097 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5098 else
5099 x = (window_box_width (w, LEFT_MARGIN_AREA)
5100 + window_box_width (w, TEXT_AREA));
b1ae662f 5101
791f420f
JR
5102 for (i = 0; i < row->used[area];)
5103 {
5104 if (row->glyphs[area][i].overlaps_vertically_p)
ee78dc32 5105 {
791f420f 5106 int start = i, start_x = x;
ee78dc32 5107
791f420f
JR
5108 do
5109 {
5110 x += row->glyphs[area][i].pixel_width;
5111 ++i;
5112 }
5113 while (i < row->used[area]
5114 && row->glyphs[area][i].overlaps_vertically_p);
cabb23bc 5115
791f420f
JR
5116 x_draw_glyphs (w, start_x, row, area, start, i,
5117 (row->inverse_p
5118 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5119 NULL, NULL, 1);
5120 }
5121 else
5122 {
5123 x += row->glyphs[area][i].pixel_width;
5124 ++i;
5125 }
5126 }
5127
5128 UNBLOCK_INPUT;
ee78dc32
GV
5129}
5130
ee78dc32 5131
791f420f
JR
5132/* Output LEN glyphs starting at START at the nominal cursor position.
5133 Advance the nominal cursor over the text. The global variable
5134 updated_window contains the window being updated, updated_row is
5135 the glyph row being updated, and updated_area is the area of that
5136 row being updated. */
ee78dc32 5137
96214669 5138static void
791f420f
JR
5139x_write_glyphs (start, len)
5140 struct glyph *start;
ee78dc32
GV
5141 int len;
5142{
791f420f 5143 int x, hpos, real_start, real_end;
ee78dc32 5144
791f420f 5145 xassert (updated_window && updated_row);
ee78dc32 5146 BLOCK_INPUT;
791f420f
JR
5147
5148 /* Write glyphs. */
ee78dc32 5149
791f420f
JR
5150 hpos = start - updated_row->glyphs[updated_area];
5151 x = x_draw_glyphs (updated_window, output_cursor.x,
5152 updated_row, updated_area,
5153 hpos, hpos + len,
5154 (updated_row->inverse_p
5155 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5156 &real_start, &real_end, 0);
5157
5158 /* If we drew over the cursor, note that it is not visible any more. */
5159 note_overwritten_text_cursor (updated_window, real_start,
5160 real_end - real_start);
5161
5162 UNBLOCK_INPUT;
5163
5164 /* Advance the output cursor. */
5165 output_cursor.hpos += len;
5166 output_cursor.x = x;
5167}
ee78dc32 5168
ee78dc32 5169
791f420f 5170/* Insert LEN glyphs from START at the nominal cursor position. */
ee78dc32 5171
791f420f
JR
5172static void
5173x_insert_glyphs (start, len)
5174 struct glyph *start;
5175 register int len;
5176{
5177 struct frame *f;
5178 struct window *w;
5179 int line_height, shift_by_width, shifted_region_width;
5180 struct glyph_row *row;
5181 struct glyph *glyph;
5182 int frame_x, frame_y, hpos, real_start, real_end;
5183 HDC hdc;
5184
5185 xassert (updated_window && updated_row);
5186 BLOCK_INPUT;
5187 w = updated_window;
5188 f = XFRAME (WINDOW_FRAME (w));
5189 hdc = get_frame_dc (f);
5190
5191 /* Get the height of the line we are in. */
5192 row = updated_row;
5193 line_height = row->height;
5194
5195 /* Get the width of the glyphs to insert. */
5196 shift_by_width = 0;
5197 for (glyph = start; glyph < start + len; ++glyph)
5198 shift_by_width += glyph->pixel_width;
5199
5200 /* Get the width of the region to shift right. */
5201 shifted_region_width = (window_box_width (w, updated_area)
5202 - output_cursor.x
5203 - shift_by_width);
5204
5205 /* Shift right. */
5b844253 5206 frame_x = window_box_left (w, updated_area) + output_cursor.x;
791f420f
JR
5207 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5208 BitBlt (hdc, frame_x + shift_by_width, frame_y,
5209 shifted_region_width, line_height,
5210 hdc, frame_x, frame_y, SRCCOPY);
5211
5212 /* Write the glyphs. */
5213 hpos = start - row->glyphs[updated_area];
5214 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5215 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5216 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5217
5218 /* Advance the output cursor. */
5219 output_cursor.hpos += len;
5220 output_cursor.x += shift_by_width;
5221 release_frame_dc (f, hdc);
ee78dc32
GV
5222
5223 UNBLOCK_INPUT;
5224}
791f420f
JR
5225
5226
5227/* Delete N glyphs at the nominal cursor position. Not implemented
5228 for X frames. */
ee78dc32 5229
96214669 5230static void
791f420f
JR
5231x_delete_glyphs (n)
5232 register int n;
ee78dc32 5233{
7e6ac5b9
AI
5234 struct frame *f;
5235
5236 if (updating_frame)
5237 f = updating_frame;
5238 else
5239 f = SELECTED_FRAME ();
5240
5241 if (! FRAME_W32_P (f))
5242 return;
5243
791f420f
JR
5244 abort ();
5245}
ee78dc32 5246
ee78dc32 5247
791f420f
JR
5248/* Erase the current text line from the nominal cursor position
5249 (inclusive) to pixel column TO_X (exclusive). The idea is that
5250 everything from TO_X onward is already erased.
ee78dc32 5251
791f420f
JR
5252 TO_X is a pixel position relative to updated_area of
5253 updated_window. TO_X == -1 means clear to the end of this area. */
11606873 5254
791f420f
JR
5255static void
5256x_clear_end_of_line (to_x)
5257 int to_x;
5258{
5259 struct frame *f;
5260 struct window *w = updated_window;
5261 int max_x, min_y, max_y;
5262 int from_x, from_y, to_y;
5263
5264 xassert (updated_window && updated_row);
5265 f = XFRAME (w->frame);
5266
5267 if (updated_row->full_width_p)
5268 {
5269 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5270 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5271 && !w->pseudo_window_p)
5272 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5273 }
5274 else
5275 max_x = window_box_width (w, updated_area);
5276 max_y = window_text_bottom_y (w);
ee78dc32 5277
791f420f
JR
5278 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5279 of window. For TO_X > 0, truncate to end of drawing area. */
5280 if (to_x == 0)
5281 return;
5282 else if (to_x < 0)
5283 to_x = max_x;
5284 else
5285 to_x = min (to_x, max_x);
ee78dc32 5286
791f420f
JR
5287 to_y = min (max_y, output_cursor.y + updated_row->height);
5288
ee78dc32 5289 /* Notice if the cursor will be cleared by this operation. */
791f420f
JR
5290 if (!updated_row->full_width_p)
5291 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
ee78dc32 5292
791f420f
JR
5293 from_x = output_cursor.x;
5294
5295 /* Translate to frame coordinates. */
5296 if (updated_row->full_width_p)
5297 {
5298 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5299 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5300 }
5301 else
5302 {
5303 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5304 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5305 }
5306
5307 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5308 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5309 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5310
5311 /* Prevent inadvertently clearing to end of the X window. */
5312 if (to_x > from_x && to_y > from_y)
5313 {
00fe468b 5314 HDC hdc;
791f420f 5315 BLOCK_INPUT;
00fe468b
JR
5316 hdc = get_frame_dc (f);
5317
5318 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
5319 release_frame_dc (f, hdc);
791f420f
JR
5320 UNBLOCK_INPUT;
5321 }
ee78dc32
GV
5322}
5323
791f420f
JR
5324
5325/* Clear entire frame. If updating_frame is non-null, clear that
5326 frame. Otherwise clear the selected frame. */
5327
96214669 5328static void
791f420f 5329x_clear_frame ()
ee78dc32 5330{
791f420f 5331 struct frame *f;
ee78dc32 5332
791f420f
JR
5333 if (updating_frame)
5334 f = updating_frame;
5335 else
5336 f = SELECTED_FRAME ();
ee78dc32 5337
7e6ac5b9
AI
5338 if (! FRAME_W32_P (f))
5339 return;
5340
791f420f
JR
5341 /* Clearing the frame will erase any cursor, so mark them all as no
5342 longer visible. */
5343 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5344 output_cursor.hpos = output_cursor.vpos = 0;
5345 output_cursor.x = -1;
ee78dc32 5346
791f420f
JR
5347 /* We don't set the output cursor here because there will always
5348 follow an explicit cursor_to. */
ee78dc32
GV
5349 BLOCK_INPUT;
5350
fbd6baed 5351 w32_clear_window (f);
ee78dc32
GV
5352
5353 /* We have to clear the scroll bars, too. If we have changed
5354 colors or something like that, then they should be notified. */
5355 x_scroll_bar_clear (f);
5356
5357 UNBLOCK_INPUT;
5358}
791f420f 5359
ee78dc32
GV
5360\f
5361/* Make audible bell. */
5362
96214669
GV
5363static void
5364w32_ring_bell (void)
ee78dc32 5365{
7e6ac5b9
AI
5366 struct frame *f;
5367
5368 f = SELECTED_FRAME ();
5369
ee78dc32
GV
5370 BLOCK_INPUT;
5371
0f32f023 5372 if (FRAME_W32_P (f) && visible_bell)
8331e676
GV
5373 {
5374 int i;
791f420f 5375 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
8331e676
GV
5376
5377 for (i = 0; i < 5; i++)
5378 {
5379 FlashWindow (hwnd, TRUE);
5380 Sleep (10);
5381 }
5382 FlashWindow (hwnd, FALSE);
5383 }
ee78dc32 5384 else
fbd6baed 5385 w32_sys_ring_bell ();
ee78dc32
GV
5386
5387 UNBLOCK_INPUT;
ee78dc32 5388}
791f420f 5389
ee78dc32 5390\f
791f420f
JR
5391/* Specify how many text lines, from the top of the window,
5392 should be affected by insert-lines and delete-lines operations.
5393 This, and those operations, are used only within an update
5394 that is bounded by calls to x_update_begin and x_update_end. */
ee78dc32 5395
96214669 5396static void
791f420f
JR
5397w32_set_terminal_window (n)
5398 register int n;
ee78dc32 5399{
791f420f 5400 /* This function intentionally left blank. */
ee78dc32 5401}
791f420f
JR
5402\f
5403
5404\f
5405/***********************************************************************
5406 Line Dance
5407 ***********************************************************************/
5408
5409/* Perform an insert-lines or delete-lines operation, inserting N
5410 lines or deleting -N lines at vertical position VPOS. */
ee78dc32 5411
96214669 5412static void
791f420f
JR
5413x_ins_del_lines (vpos, n)
5414 int vpos, n;
ee78dc32 5415{
7e6ac5b9
AI
5416 struct frame *f;
5417
5418 if (updating_frame)
5419 f = updating_frame;
5420 else
5421 f = SELECTED_FRAME ();
5422
5423 if (! FRAME_W32_P (f))
5424 return;
5425
ee78dc32
GV
5426 abort ();
5427}
791f420f
JR
5428
5429
5430/* Scroll part of the display as described by RUN. */
5431
5432static void
5433x_scroll_run (w, run)
5434 struct window *w;
5435 struct run *run;
5436{
5437 struct frame *f = XFRAME (w->frame);
5438 int x, y, width, height, from_y, to_y, bottom_y;
5439 HDC hdc = get_frame_dc (f);
5440
5441 /* Get frame-relative bounding box of the text display area of W,
5442 without mode lines. Include in this box the flags areas to the
5443 left and right of W. */
5444 window_box (w, -1, &x, &y, &width, &height);
5445 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5446 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5447
5448 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5449 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5450 bottom_y = y + height;
5451
5452 if (to_y < from_y)
5453 {
5454 /* Scrolling up. Make sure we don't copy part of the mode
5455 line at the bottom. */
5456 if (from_y + run->height > bottom_y)
5457 height = bottom_y - from_y;
5458 else
5459 height = run->height;
5460 }
5461 else
5462 {
5463 /* Scolling down. Make sure we don't copy over the mode line.
5464 at the bottom. */
5465 if (to_y + run->height > bottom_y)
5466 height = bottom_y - to_y;
5467 else
5468 height = run->height;
5469 }
5470
5471 BLOCK_INPUT;
5472
5473 /* Cursor off. Will be switched on again in x_update_window_end. */
5474 updated_window = w;
5475 x_clear_cursor (w);
5476
5477 BitBlt (hdc, x, to_y, width, height, hdc, x, from_y, SRCCOPY);
5478
5479 UNBLOCK_INPUT;
5480 release_frame_dc (f, hdc);
5481}
5482
9ef2e2cf 5483
ee78dc32 5484\f
791f420f
JR
5485/***********************************************************************
5486 Exposure Events
5487 ***********************************************************************/
5488
5489/* Redisplay an exposed area of frame F. X and Y are the upper-left
5490 corner of the exposed rectangle. W and H are width and height of
5491 the exposed area. All are pixel values. W or H zero means redraw
5492 the entire frame. */
ee78dc32 5493
96214669 5494static void
791f420f
JR
5495expose_frame (f, x, y, w, h)
5496 struct frame *f;
5497 int x, y, w, h;
ee78dc32 5498{
791f420f
JR
5499 RECT r;
5500
5501 TRACE ((stderr, "expose_frame "));
5502
5503 /* No need to redraw if frame will be redrawn soon. */
5504 if (FRAME_GARBAGED_P (f))
5505 {
5506 TRACE ((stderr, " garbaged\n"));
5507 return;
5508 }
5509
5510 /* If basic faces haven't been realized yet, there is no point in
5511 trying to redraw anything. This can happen when we get an expose
5512 event while Emacs is starting, e.g. by moving another window. */
5513 if (FRAME_FACE_CACHE (f) == NULL
5514 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5515 {
5516 TRACE ((stderr, " no faces\n"));
5517 return;
5518 }
5519
5520 if (w == 0 || h == 0)
5521 {
5522 r.left = r.top = 0;
5523 r.right = CANON_X_UNIT (f) * f->width;
5524 r.bottom = CANON_Y_UNIT (f) * f->height;
5525 }
5526 else
5527 {
5528 r.left = x;
5529 r.top = y;
5530 r.right = x + w;
5531 r.bottom = y + h;
5532 }
5533
5534 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
5535 expose_window_tree (XWINDOW (f->root_window), &r);
5536
5537 if (WINDOWP (f->tool_bar_window))
5538 {
5539 struct window *w = XWINDOW (f->tool_bar_window);
5540 RECT window_rect;
5541 RECT intersection_rect;
5542 int window_x, window_y, window_width, window_height;
5543
5544 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5545 window_rect.left = window_x;
5546 window_rect.top = window_y;
5547 window_rect.right = window_x + window_width;
5548 window_rect.bottom = window_y + window_height;
5549
158cba56 5550 if (IntersectRect (&intersection_rect, &r, &window_rect))
791f420f
JR
5551 expose_window (w, &intersection_rect);
5552 }
5553}
5554
5555
5556/* Redraw (parts) of all windows in the window tree rooted at W that
5557 intersect R. R contains frame pixel coordinates. */
5558
5559static void
5560expose_window_tree (w, r)
5561 struct window *w;
5562 RECT *r;
5563{
5564 while (w)
5565 {
5566 if (!NILP (w->hchild))
5567 expose_window_tree (XWINDOW (w->hchild), r);
5568 else if (!NILP (w->vchild))
5569 expose_window_tree (XWINDOW (w->vchild), r);
5570 else
5571 {
5572 RECT window_rect;
5573 RECT intersection_rect;
5574 struct frame *f = XFRAME (w->frame);
5575 int window_x, window_y, window_width, window_height;
5576
5577 /* Frame-relative pixel rectangle of W. */
5578 window_box (w, -1, &window_x, &window_y, &window_width,
5579 &window_height);
5580 window_rect.left
5581 = (window_x
5582 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5583 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5584 window_rect.top = window_y;
5585 window_rect.right = window_rect.left
5586 + (window_width
5587 + FRAME_X_FLAGS_AREA_WIDTH (f)
5588 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5589 window_rect.bottom = window_rect.top
5590 + window_height + CURRENT_MODE_LINE_HEIGHT (w);
5591
158cba56 5592 if (IntersectRect (&intersection_rect, r, &window_rect))
791f420f
JR
5593 expose_window (w, &intersection_rect);
5594 }
5595
5596 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5597 }
5598}
5599
5600
5601/* Redraw the part of glyph row area AREA of glyph row ROW on window W
5602 which intersects rectangle R. R is in window-relative coordinates. */
5603
5604static void
5605expose_area (w, row, r, area)
5606 struct window *w;
5607 struct glyph_row *row;
5608 RECT *r;
5609 enum glyph_row_area area;
5610{
791f420f
JR
5611 struct glyph *first = row->glyphs[area];
5612 struct glyph *end = row->glyphs[area] + row->used[area];
5613 struct glyph *last;
2df85294 5614 int first_x, start_x, x;
791f420f 5615
791f420f
JR
5616 if (area == TEXT_AREA && row->fill_line_p)
5617 /* If row extends face to end of line write the whole line. */
c2cc16fa 5618 x_draw_glyphs (w, 0, row, area,
791f420f
JR
5619 0, row->used[area],
5620 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5621 NULL, NULL, 0);
5622 else
5623 {
2df85294
JR
5624 /* Set START_X to the window-relative start position for drawing glyphs of
5625 AREA. The first glyph of the text area can be partially visible.
5626 The first glyphs of other areas cannot. */
5627 if (area == LEFT_MARGIN_AREA)
5628 start_x = 0;
5629 else if (area == TEXT_AREA)
5630 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5631 else
5632 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5633 + window_box_width (w, TEXT_AREA));
5634 x = start_x;
5635
791f420f
JR
5636 /* Find the first glyph that must be redrawn. */
5637 while (first < end
5638 && x + first->pixel_width < r->left)
5639 {
5640 x += first->pixel_width;
5641 ++first;
5642 }
5643
5644 /* Find the last one. */
5645 last = first;
5646 first_x = x;
5647 while (last < end
5648 && x < r->right)
5649 {
5650 x += last->pixel_width;
5651 ++last;
5652 }
2df85294 5653
791f420f
JR
5654 /* Repaint. */
5655 if (last > first)
f201d732 5656 x_draw_glyphs (w, first_x - start_x, row, area,
791f420f
JR
5657 first - row->glyphs[area],
5658 last - row->glyphs[area],
5659 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5660 NULL, NULL, 0);
5661 }
5662}
5663
5664
5665/* Redraw the parts of the glyph row ROW on window W intersecting
5666 rectangle R. R is in window-relative coordinates. */
ee78dc32 5667
791f420f
JR
5668static void
5669expose_line (w, row, r)
5670 struct window *w;
5671 struct glyph_row *row;
5672 RECT *r;
5673{
5674 xassert (row->enabled_p);
5675
5676 if (row->mode_line_p || w->pseudo_window_p)
5677 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5678 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5679 NULL, NULL, 0);
ee78dc32 5680 else
791f420f
JR
5681 {
5682 if (row->used[LEFT_MARGIN_AREA])
5683 expose_area (w, row, r, LEFT_MARGIN_AREA);
5684 if (row->used[TEXT_AREA])
5685 expose_area (w, row, r, TEXT_AREA);
5686 if (row->used[RIGHT_MARGIN_AREA])
5687 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5688 x_draw_row_bitmaps (w, row);
5689 }
ee78dc32 5690}
ee78dc32 5691
ee78dc32 5692
791f420f 5693/* Return non-zero if W's cursor intersects rectangle R. */
96214669 5694
791f420f
JR
5695static int
5696x_phys_cursor_in_rect_p (w, r)
5697 struct window *w;
5698 RECT *r;
ee78dc32 5699{
791f420f
JR
5700 RECT cr, result;
5701 struct glyph *cursor_glyph;
ee78dc32 5702
791f420f
JR
5703 cursor_glyph = get_phys_cursor_glyph (w);
5704 if (cursor_glyph)
ee78dc32 5705 {
791f420f
JR
5706 cr.left = w->phys_cursor.x;
5707 cr.top = w->phys_cursor.y;
5708 cr.right = cr.left + cursor_glyph->pixel_width;
5709 cr.bottom = cr.top + w->phys_cursor_height;
158cba56 5710 return IntersectRect (&result, &cr, r);
ee78dc32
GV
5711 }
5712 else
791f420f 5713 return 0;
ee78dc32
GV
5714}
5715
791f420f
JR
5716
5717/* Redraw a rectangle of window W. R is a rectangle in window
5718 relative coordinates. Call this function with input blocked. */
5719
ee78dc32 5720static void
791f420f
JR
5721expose_window (w, r)
5722 struct window *w;
5723 RECT *r;
ee78dc32 5724{
791f420f
JR
5725 struct glyph_row *row;
5726 int y;
5727 int yb = window_text_bottom_y (w);
5728 int cursor_cleared_p;
5729
5730 /* If window is not yet fully initialized, do nothing. This can
5731 happen when toolkit scroll bars are used and a window is split.
5732 Reconfiguring the scroll bar will generate an expose for a newly
5733 created window. */
9436cdf9 5734 if (w->current_matrix == NULL || w == updated_window)
ee78dc32
GV
5735 return;
5736
791f420f
JR
5737 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5738 r->left, r->top, r->right, r->bottom));
ee78dc32 5739
791f420f
JR
5740 /* Convert to window coordinates. */
5741 r->left = FRAME_TO_WINDOW_PIXEL_X (w, r->left);
5742 r->top = FRAME_TO_WINDOW_PIXEL_Y (w, r->top);
5743 r->right = FRAME_TO_WINDOW_PIXEL_X (w, r->right);
5744 r->bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r->bottom);
ee78dc32 5745
791f420f
JR
5746 /* Turn off the cursor. */
5747 if (!w->pseudo_window_p
5748 && x_phys_cursor_in_rect_p (w, r))
5749 {
5750 x_clear_cursor (w);
5751 cursor_cleared_p = 1;
5752 }
5753 else
5754 cursor_cleared_p = 0;
5755
5756 /* Find the first row intersecting the rectangle R. */
5757 row = w->current_matrix->rows;
5758 y = 0;
5759 while (row->enabled_p
5760 && y < yb
5761 && y + row->height < r->top)
5762 {
5763 y += row->height;
5764 ++row;
5765 }
5766
5767 /* Display the text in the rectangle, one text line at a time. */
5768 while (row->enabled_p
5769 && y < yb
5770 && y < r->bottom)
5771 {
5772 expose_line (w, row, r);
5773 y += row->height;
5774 ++row;
5775 }
ee78dc32 5776
791f420f
JR
5777 /* Display the mode line if there is one. */
5778 if (WINDOW_WANTS_MODELINE_P (w)
5779 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5780 row->enabled_p)
5781 && row->y < r->bottom)
5782 expose_line (w, row, r);
689004fa 5783
791f420f
JR
5784 if (!w->pseudo_window_p)
5785 {
5786 /* Draw border between windows. */
5787 x_draw_vertical_border (w);
5788
5789 /* Turn the cursor on again. */
5790 if (cursor_cleared_p)
5791 x_update_window_cursor (w, 1);
5792 }
ee78dc32 5793}
ee78dc32 5794
ee78dc32
GV
5795\f
5796static void
5797frame_highlight (f)
5798 struct frame *f;
5799{
791f420f 5800 x_update_cursor (f, 1);
ee78dc32
GV
5801}
5802
5803static void
5804frame_unhighlight (f)
5805 struct frame *f;
5806{
791f420f 5807 x_update_cursor (f, 1);
ee78dc32
GV
5808}
5809
ee78dc32
GV
5810/* The focus has changed. Update the frames as necessary to reflect
5811 the new situation. Note that we can't change the selected frame
5812 here, because the Lisp code we are interrupting might become confused.
5813 Each event gets marked with the frame in which it occurred, so the
5814 Lisp code can tell when the switch took place by examining the events. */
5815
9ef2e2cf 5816static void
ee78dc32 5817x_new_focus_frame (dpyinfo, frame)
fbd6baed 5818 struct w32_display_info *dpyinfo;
ee78dc32
GV
5819 struct frame *frame;
5820{
fbd6baed 5821 struct frame *old_focus = dpyinfo->w32_focus_frame;
ee78dc32 5822
fbd6baed 5823 if (frame != dpyinfo->w32_focus_frame)
ee78dc32
GV
5824 {
5825 /* Set this before calling other routines, so that they see
fbd6baed
GV
5826 the correct value of w32_focus_frame. */
5827 dpyinfo->w32_focus_frame = frame;
ee78dc32
GV
5828
5829 if (old_focus && old_focus->auto_lower)
5830 x_lower_frame (old_focus);
5831
fbd6baed
GV
5832 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5833 pending_autoraise_frame = dpyinfo->w32_focus_frame;
ee78dc32
GV
5834 else
5835 pending_autoraise_frame = 0;
5836 }
5837
5838 x_frame_rehighlight (dpyinfo);
5839}
5840
5841/* Handle an event saying the mouse has moved out of an Emacs frame. */
5842
5843void
5844x_mouse_leave (dpyinfo)
fbd6baed 5845 struct w32_display_info *dpyinfo;
ee78dc32 5846{
fbd6baed 5847 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
ee78dc32
GV
5848}
5849
5850/* The focus has changed, or we have redirected a frame's focus to
5851 another frame (this happens when a frame uses a surrogate
9ef2e2cf 5852 mini-buffer frame). Shift the highlight as appropriate.
ee78dc32
GV
5853
5854 The FRAME argument doesn't necessarily have anything to do with which
9ef2e2cf
JR
5855 frame is being highlighted or un-highlighted; we only use it to find
5856 the appropriate X display info. */
5857
ee78dc32 5858static void
fbd6baed 5859w32_frame_rehighlight (frame)
ee78dc32
GV
5860 struct frame *frame;
5861{
7e6ac5b9
AI
5862 if (! FRAME_W32_P (frame))
5863 return;
fbd6baed 5864 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
ee78dc32
GV
5865}
5866
5867static void
5868x_frame_rehighlight (dpyinfo)
fbd6baed 5869 struct w32_display_info *dpyinfo;
ee78dc32 5870{
fbd6baed 5871 struct frame *old_highlight = dpyinfo->w32_highlight_frame;
ee78dc32 5872
fbd6baed 5873 if (dpyinfo->w32_focus_frame)
ee78dc32 5874 {
fbd6baed
GV
5875 dpyinfo->w32_highlight_frame
5876 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5877 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5878 : dpyinfo->w32_focus_frame);
5879 if (! FRAME_LIVE_P (dpyinfo->w32_highlight_frame))
ee78dc32 5880 {
fbd6baed
GV
5881 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
5882 dpyinfo->w32_highlight_frame = dpyinfo->w32_focus_frame;
ee78dc32
GV
5883 }
5884 }
5885 else
fbd6baed 5886 dpyinfo->w32_highlight_frame = 0;
ee78dc32 5887
fbd6baed 5888 if (dpyinfo->w32_highlight_frame != old_highlight)
ee78dc32
GV
5889 {
5890 if (old_highlight)
5891 frame_unhighlight (old_highlight);
fbd6baed
GV
5892 if (dpyinfo->w32_highlight_frame)
5893 frame_highlight (dpyinfo->w32_highlight_frame);
ee78dc32
GV
5894 }
5895}
5896\f
5897/* Keyboard processing - modifier keys, etc. */
5898
5899/* Convert a keysym to its name. */
5900
5901char *
5902x_get_keysym_name (keysym)
5903 int keysym;
5904{
5905 /* Make static so we can always return it */
5906 static char value[100];
5907
5908 BLOCK_INPUT;
9127e20e 5909 GetKeyNameText (keysym, value, 100);
ee78dc32
GV
5910 UNBLOCK_INPUT;
5911
5912 return value;
5913}
9ef2e2cf
JR
5914
5915
ee78dc32
GV
5916\f
5917/* Mouse clicks and mouse movement. Rah. */
5918
9ef2e2cf
JR
5919/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5920 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5921 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5922 not force the value into range. */
ee78dc32
GV
5923
5924void
5925pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5926 FRAME_PTR f;
5927 register int pix_x, pix_y;
5928 register int *x, *y;
5929 RECT *bounds;
5930 int noclip;
5931{
52cf03a1
GV
5932 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
5933 if (NILP (Vwindow_system))
5934 {
5935 *x = pix_x;
5936 *y = pix_y;
5937 return;
5938 }
5939
ee78dc32
GV
5940 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5941 even for negative values. */
5942 if (pix_x < 0)
9127e20e 5943 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
ee78dc32 5944 if (pix_y < 0)
fbd6baed 5945 pix_y -= (f)->output_data.w32->line_height - 1;
ee78dc32
GV
5946
5947 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5948 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5949
5950 if (bounds)
5951 {
5952 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
5953 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
9127e20e 5954 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
fbd6baed 5955 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
ee78dc32
GV
5956 }
5957
5958 if (!noclip)
5959 {
5960 if (pix_x < 0)
5961 pix_x = 0;
ec48c3a7
JR
5962 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5963 pix_x = FRAME_WINDOW_WIDTH (f);
ee78dc32
GV
5964
5965 if (pix_y < 0)
5966 pix_y = 0;
5967 else if (pix_y > f->height)
5968 pix_y = f->height;
5969 }
5970
5971 *x = pix_x;
5972 *y = pix_y;
5973}
5974
791f420f
JR
5975
5976/* Given HPOS/VPOS in the current matrix of W, return corresponding
5977 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5978 can't tell the positions because W's display is not up to date,
5979 return 0. */
5980
5981int
5982glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5983 struct window *w;
5984 int hpos, vpos;
5985 int *frame_x, *frame_y;
ee78dc32 5986{
791f420f
JR
5987 int success_p;
5988
5989 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5990 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5991
5992 if (display_completed)
52cf03a1 5993 {
791f420f
JR
5994 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5995 struct glyph *glyph = row->glyphs[TEXT_AREA];
5996 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5997
5998 *frame_y = row->y;
5999 *frame_x = row->x;
6000 while (glyph < end)
6001 {
6002 *frame_x += glyph->pixel_width;
6003 ++glyph;
6004 }
6005
6006 success_p = 1;
6007 }
6008 else
6009 {
6010 *frame_y = *frame_x = 0;
6011 success_p = 0;
52cf03a1
GV
6012 }
6013
791f420f
JR
6014 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6015 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6016 return success_p;
ee78dc32
GV
6017}
6018
6019BOOL
6020parse_button (message, pbutton, pup)
6021 int message;
6022 int * pbutton;
6023 int * pup;
6024{
6025 int button = 0;
6026 int up = 0;
6027
6028 switch (message)
6029 {
6030 case WM_LBUTTONDOWN:
6031 button = 0;
6032 up = 0;
6033 break;
6034 case WM_LBUTTONUP:
6035 button = 0;
6036 up = 1;
6037 break;
6038 case WM_MBUTTONDOWN:
fbd6baed 6039 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6040 button = 1;
6041 else
6042 button = 2;
ee78dc32
GV
6043 up = 0;
6044 break;
6045 case WM_MBUTTONUP:
fbd6baed 6046 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6047 button = 1;
6048 else
6049 button = 2;
ee78dc32
GV
6050 up = 1;
6051 break;
6052 case WM_RBUTTONDOWN:
fbd6baed 6053 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6054 button = 2;
6055 else
6056 button = 1;
ee78dc32
GV
6057 up = 0;
6058 break;
6059 case WM_RBUTTONUP:
fbd6baed 6060 if (NILP (Vw32_swap_mouse_buttons))
52cf03a1
GV
6061 button = 2;
6062 else
6063 button = 1;
ee78dc32
GV
6064 up = 1;
6065 break;
6066 default:
6067 return (FALSE);
6068 }
6069
6070 if (pup) *pup = up;
6071 if (pbutton) *pbutton = button;
6072
6073 return (TRUE);
6074}
6075
6076
6077/* Prepare a mouse-event in *RESULT for placement in the input queue.
6078
6079 If the event is a button press, then note that we have grabbed
6080 the mouse. */
6081
ec48c3a7 6082static Lisp_Object
ee78dc32
GV
6083construct_mouse_click (result, msg, f)
6084 struct input_event *result;
fbd6baed 6085 W32Msg *msg;
ee78dc32
GV
6086 struct frame *f;
6087{
6088 int button;
6089 int up;
6090
6091 parse_button (msg->msg.message, &button, &up);
6092
6093 /* Make the event type no_event; we'll change that when we decide
6094 otherwise. */
6095 result->kind = mouse_click;
6096 result->code = button;
6097 result->timestamp = msg->msg.time;
6098 result->modifiers = (msg->dwModifiers
6099 | (up
6100 ? up_modifier
6101 : down_modifier));
6102
ec48c3a7
JR
6103 XSETINT (result->x, LOWORD (msg->msg.lParam));
6104 XSETINT (result->y, HIWORD (msg->msg.lParam));
6105 XSETFRAME (result->frame_or_window, f);
6106 result->arg = Qnil;
6107 return Qnil;
ee78dc32
GV
6108}
6109
ec48c3a7 6110static Lisp_Object
689004fa
GV
6111construct_mouse_wheel (result, msg, f)
6112 struct input_event *result;
6113 W32Msg *msg;
6114 struct frame *f;
6115{
6116 POINT p;
6117 result->kind = mouse_wheel;
6118 result->code = (short) HIWORD (msg->msg.wParam);
6119 result->timestamp = msg->msg.time;
6120 result->modifiers = msg->dwModifiers;
6121 p.x = LOWORD (msg->msg.lParam);
6122 p.y = HIWORD (msg->msg.lParam);
9127e20e 6123 ScreenToClient (msg->msg.hwnd, &p);
689004fa
GV
6124 XSETINT (result->x, p.x);
6125 XSETINT (result->y, p.y);
6126 XSETFRAME (result->frame_or_window, f);
7e889510 6127 result->arg = Qnil;
ec48c3a7 6128 return Qnil;
689004fa
GV
6129}
6130
ec48c3a7 6131static Lisp_Object
12857dfd
RS
6132construct_drag_n_drop (result, msg, f)
6133 struct input_event *result;
6134 W32Msg *msg;
6135 struct frame *f;
6136{
6137 Lisp_Object files;
6138 Lisp_Object frame;
6139 HDROP hdrop;
6140 POINT p;
6141 WORD num_files;
6142 char *name;
6143 int i, len;
6144
6145 result->kind = drag_n_drop;
6146 result->code = 0;
6147 result->timestamp = msg->msg.time;
6148 result->modifiers = msg->dwModifiers;
6149
fb9cc9cc
AI
6150 hdrop = (HDROP) msg->msg.wParam;
6151 DragQueryPoint (hdrop, &p);
6152
e12ca9c2 6153#if 0
12857dfd
RS
6154 p.x = LOWORD (msg->msg.lParam);
6155 p.y = HIWORD (msg->msg.lParam);
6156 ScreenToClient (msg->msg.hwnd, &p);
e12ca9c2
AI
6157#endif
6158
12857dfd
RS
6159 XSETINT (result->x, p.x);
6160 XSETINT (result->y, p.y);
6161
12857dfd
RS
6162 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
6163 files = Qnil;
6164
6165 for (i = 0; i < num_files; i++)
6166 {
6167 len = DragQueryFile (hdrop, i, NULL, 0);
6168 if (len <= 0)
6169 continue;
6170 name = alloca (len + 1);
6171 DragQueryFile (hdrop, i, name, len + 1);
6172 files = Fcons (build_string (name), files);
6173 }
6174
6175 DragFinish (hdrop);
6176
6177 XSETFRAME (frame, f);
6178 result->frame_or_window = Fcons (frame, files);
7e889510 6179 result->arg = Qnil;
ec48c3a7 6180 return Qnil;
12857dfd
RS
6181}
6182
ee78dc32
GV
6183\f
6184/* Function to report a mouse movement to the mainstream Emacs code.
6185 The input handler calls this.
6186
6187 We have received a mouse movement event, which is given in *event.
6188 If the mouse is over a different glyph than it was last time, tell
6189 the mainstream emacs code by setting mouse_moved. If not, ask for
6190 another motion event, so we can check again the next time it moves. */
6191
791f420f
JR
6192static MSG last_mouse_motion_event;
6193static Lisp_Object last_mouse_motion_frame;
6194
ee78dc32
GV
6195static void
6196note_mouse_movement (frame, msg)
6197 FRAME_PTR frame;
6198 MSG *msg;
6199{
6200 last_mouse_movement_time = msg->time;
791f420f
JR
6201 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
6202 XSETFRAME (last_mouse_motion_frame, frame);
ee78dc32 6203
fbd6baed 6204 if (msg->hwnd != FRAME_W32_WINDOW (frame))
ee78dc32
GV
6205 {
6206 frame->mouse_moved = 1;
6207 last_mouse_scroll_bar = Qnil;
ee78dc32
GV
6208 note_mouse_highlight (frame, -1, -1);
6209 }
6210
6211 /* Has the mouse moved off the glyph it was on at the last sighting? */
6212 else if (LOWORD (msg->lParam) < last_mouse_glyph.left
6213 || LOWORD (msg->lParam) > last_mouse_glyph.right
9bf7b6aa 6214 || HIWORD (msg->lParam) < last_mouse_glyph.top
ee78dc32
GV
6215 || HIWORD (msg->lParam) > last_mouse_glyph.bottom)
6216 {
6217 frame->mouse_moved = 1;
6218 last_mouse_scroll_bar = Qnil;
ee78dc32
GV
6219 note_mouse_highlight (frame, LOWORD (msg->lParam), HIWORD (msg->lParam));
6220 }
6221}
6222
6223/* This is used for debugging, to turn off note_mouse_highlight. */
9ef2e2cf 6224
791f420f
JR
6225int disable_mouse_highlight;
6226
6227
6228\f
6229/************************************************************************
6230 Mouse Face
6231 ************************************************************************/
6232
6233/* Find the glyph under window-relative coordinates X/Y in window W.
6234 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6235 strings. Return in *HPOS and *VPOS the row and column number of
6236 the glyph found. Return in *AREA the glyph area containing X.
6237 Value is a pointer to the glyph found or null if X/Y is not on
6238 text, or we can't tell because W's current matrix is not up to
6239 date. */
6240
6241static struct glyph *
6242x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6243 struct window *w;
6244 int x, y;
6245 int *hpos, *vpos, *area;
6246{
6247 struct glyph *glyph, *end;
ec48c3a7 6248 struct glyph_row *row = NULL;
791f420f
JR
6249 int x0, i, left_area_width;
6250
6251 /* Find row containing Y. Give up if some row is not enabled. */
6252 for (i = 0; i < w->current_matrix->nrows; ++i)
6253 {
6254 row = MATRIX_ROW (w->current_matrix, i);
6255 if (!row->enabled_p)
6256 return NULL;
6257 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6258 break;
6259 }
6260
6261 *vpos = i;
6262 *hpos = 0;
6263
6264 /* Give up if Y is not in the window. */
6265 if (i == w->current_matrix->nrows)
6266 return NULL;
6267
6268 /* Get the glyph area containing X. */
6269 if (w->pseudo_window_p)
6270 {
6271 *area = TEXT_AREA;
6272 x0 = 0;
6273 }
6274 else
6275 {
6276 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6277 if (x < left_area_width)
6278 {
6279 *area = LEFT_MARGIN_AREA;
6280 x0 = 0;
6281 }
6282 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6283 {
6284 *area = TEXT_AREA;
6285 x0 = row->x + left_area_width;
6286 }
6287 else
6288 {
6289 *area = RIGHT_MARGIN_AREA;
6290 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6291 }
6292 }
6293
6294 /* Find glyph containing X. */
6295 glyph = row->glyphs[*area];
6296 end = glyph + row->used[*area];
6297 while (glyph < end)
6298 {
6299 if (x < x0 + glyph->pixel_width)
6300 {
6301 if (w->pseudo_window_p)
6302 break;
6303 else if (BUFFERP (glyph->object))
6304 break;
6305 }
6306
6307 x0 += glyph->pixel_width;
6308 ++glyph;
6309 }
6310
6311 if (glyph == end)
6312 return NULL;
6313
6314 *hpos = glyph - row->glyphs[*area];
6315 return glyph;
6316}
6317
6318
6319/* Convert frame-relative x/y to coordinates relative to window W.
6320 Takes pseudo-windows into account. */
6321
6322static void
6323frame_to_window_pixel_xy (w, x, y)
6324 struct window *w;
6325 int *x, *y;
6326{
6327 if (w->pseudo_window_p)
6328 {
6329 /* A pseudo-window is always full-width, and starts at the
6330 left edge of the frame, plus a frame border. */
6331 struct frame *f = XFRAME (w->frame);
6332 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6333 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6334 }
6335 else
6336 {
6337 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6338 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6339 }
6340}
6341
6342
c2cc16fa 6343/* Take proper action when mouse has moved to the mode or header line of
791f420f
JR
6344 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6345 mode line. X is relative to the start of the text display area of
6346 W, so the width of bitmap areas and scroll bars must be subtracted
6347 to get a position relative to the start of the mode line. */
6348
6349static void
6350note_mode_line_highlight (w, x, mode_line_p)
6351 struct window *w;
6352 int x, mode_line_p;
6353{
6354 struct frame *f = XFRAME (w->frame);
6355 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6356 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6357 struct glyph_row *row;
6358
6359 if (mode_line_p)
6360 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6361 else
6362 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6363
6364 if (row->enabled_p)
6365 {
6366 struct glyph *glyph, *end;
6367 Lisp_Object help, map;
6368 int x0;
6369
6370 /* Find the glyph under X. */
6371 glyph = row->glyphs[TEXT_AREA];
6372 end = glyph + row->used[TEXT_AREA];
6373 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6374 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
c2cc16fa 6375
791f420f
JR
6376 while (glyph < end
6377 && x >= x0 + glyph->pixel_width)
6378 {
6379 x0 += glyph->pixel_width;
6380 ++glyph;
6381 }
6382
6383 if (glyph < end
6384 && STRINGP (glyph->object)
6385 && XSTRING (glyph->object)->intervals
6386 && glyph->charpos >= 0
6387 && glyph->charpos < XSTRING (glyph->object)->size)
6388 {
6389 /* If we're on a string with `help-echo' text property,
6390 arrange for the help to be displayed. This is done by
6391 setting the global variable help_echo to the help string. */
6392 help = Fget_text_property (make_number (glyph->charpos),
6393 Qhelp_echo, glyph->object);
b7e80413 6394 if (!NILP (help))
ec48c3a7
JR
6395 {
6396 help_echo = help;
158cba56 6397 XSETWINDOW (help_echo_window, w);
ec48c3a7
JR
6398 help_echo_object = glyph->object;
6399 help_echo_pos = glyph->charpos;
6400 }
791f420f
JR
6401
6402 /* Change the mouse pointer according to what is under X/Y. */
6403 map = Fget_text_property (make_number (glyph->charpos),
6404 Qlocal_map, glyph->object);
02067692 6405 if (KEYMAPP (map))
791f420f 6406 cursor = f->output_data.w32->nontext_cursor;
c2cc16fa
JR
6407 else
6408 {
6409 map = Fget_text_property (make_number (glyph->charpos),
6410 Qkeymap, glyph->object);
6411 if (KEYMAPP (map))
6412 cursor = f->output_data.w32->nontext_cursor;
6413 }
791f420f
JR
6414 }
6415 }
9ef2e2cf 6416
01b220b6 6417#if 0 /* TODO: mouse cursor */
791f420f
JR
6418 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6419#endif
6420}
ee78dc32 6421
9ef2e2cf
JR
6422
6423/* Take proper action when the mouse has moved to position X, Y on
6424 frame F as regards highlighting characters that have mouse-face
6425 properties. Also de-highlighting chars where the mouse was before.
ee78dc32
GV
6426 X and Y can be negative or out of range. */
6427
6428static void
6429note_mouse_highlight (f, x, y)
9ef2e2cf 6430 struct frame *f;
ee78dc32
GV
6431 int x, y;
6432{
791f420f
JR
6433 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6434 int portion;
ee78dc32
GV
6435 Lisp_Object window;
6436 struct window *w;
6437
791f420f
JR
6438 /* When a menu is active, don't highlight because this looks odd. */
6439 if (popup_activated ())
6440 return;
6441
9ef2e2cf
JR
6442 if (disable_mouse_highlight
6443 || !f->glyphs_initialized_p)
ee78dc32
GV
6444 return;
6445
791f420f
JR
6446 dpyinfo->mouse_face_mouse_x = x;
6447 dpyinfo->mouse_face_mouse_y = y;
6448 dpyinfo->mouse_face_mouse_frame = f;
ee78dc32 6449
791f420f 6450 if (dpyinfo->mouse_face_defer)
ee78dc32
GV
6451 return;
6452
6453 if (gc_in_progress)
6454 {
791f420f 6455 dpyinfo->mouse_face_deferred_gc = 1;
ee78dc32
GV
6456 return;
6457 }
6458
ee78dc32 6459 /* Which window is that in? */
791f420f 6460 window = window_from_coordinates (f, x, y, &portion, 1);
ee78dc32
GV
6461
6462 /* If we were displaying active text in another window, clear that. */
791f420f
JR
6463 if (! EQ (window, dpyinfo->mouse_face_window))
6464 clear_mouse_face (dpyinfo);
6465
6466 /* Not on a window -> return. */
6467 if (!WINDOWP (window))
6468 return;
6469
6470 /* Convert to window-relative pixel coordinates. */
6471 w = XWINDOW (window);
6472 frame_to_window_pixel_xy (w, &x, &y);
6473
6474 /* Handle tool-bar window differently since it doesn't display a
6475 buffer. */
6476 if (EQ (window, f->tool_bar_window))
6477 {
6478 note_tool_bar_highlight (f, x, y);
6479 return;
6480 }
6481
6482 if (portion == 1 || portion == 3)
6483 {
6484 /* Mouse is on the mode or top line. */
6485 note_mode_line_highlight (w, x, portion == 1);
6486 return;
6487 }
01b220b6 6488#if 0 /* TODO: mouse cursor */
250cfece
JR
6489 else if (portion == 2)
6490 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6491 f->output_data.x->horizontal_drag_cursor);
791f420f
JR
6492 else
6493 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6494 f->output_data.x->text_cursor);
6495#endif
ee78dc32
GV
6496
6497 /* Are we in a window whose display is up to date?
6498 And verify the buffer's text has not changed. */
9ef2e2cf 6499 if (/* Within text portion of the window. */
791f420f 6500 portion == 0
ee78dc32 6501 && EQ (w->window_end_valid, w->buffer)
9ef2e2cf
JR
6502 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6503 && (XFASTINT (w->last_overlay_modified)
6504 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
ee78dc32 6505 {
791f420f
JR
6506 int hpos, vpos, pos, i, area;
6507 struct glyph *glyph;
6508
6509 /* Find the glyph under X/Y. */
6510 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6511
6512 /* Clear mouse face if X/Y not over text. */
6513 if (glyph == NULL
6514 || area != TEXT_AREA
6515 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6516 {
6517 clear_mouse_face (dpyinfo);
6518 return;
6519 }
6520
6521 pos = glyph->charpos;
6522 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6523
6524 /* Check for mouse-face and help-echo. */
6525 {
6526 Lisp_Object mouse_face, overlay, position;
6527 Lisp_Object *overlay_vec;
6528 int len, noverlays;
6529 struct buffer *obuf;
6530 int obegv, ozv;
6531
6532 /* If we get an out-of-range value, return now; avoid an error. */
6533 if (pos > BUF_Z (XBUFFER (w->buffer)))
6534 return;
6535
6536 /* Make the window's buffer temporarily current for
6537 overlays_at and compute_char_face. */
6538 obuf = current_buffer;
6539 current_buffer = XBUFFER (w->buffer);
6540 obegv = BEGV;
6541 ozv = ZV;
6542 BEGV = BEG;
6543 ZV = Z;
6544
6545 /* Is this char mouse-active or does it have help-echo? */
6546 XSETINT (position, pos);
6547
6548 /* Put all the overlays we want in a vector in overlay_vec.
6549 Store the length in len. If there are more than 10, make
6550 enough space for all, and try again. */
6551 len = 10;
6552 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
46636545 6553 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
791f420f
JR
6554 if (noverlays > len)
6555 {
6556 len = noverlays;
6557 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
46636545 6558 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
791f420f
JR
6559 }
6560
158cba56 6561 /* Sort overlays into increasing priority order. */
791f420f
JR
6562 noverlays = sort_overlays (overlay_vec, noverlays, w);
6563
6564 /* Check mouse-face highlighting. */
6565 if (! (EQ (window, dpyinfo->mouse_face_window)
6566 && vpos >= dpyinfo->mouse_face_beg_row
6567 && vpos <= dpyinfo->mouse_face_end_row
6568 && (vpos > dpyinfo->mouse_face_beg_row
6569 || hpos >= dpyinfo->mouse_face_beg_col)
6570 && (vpos < dpyinfo->mouse_face_end_row
6571 || hpos < dpyinfo->mouse_face_end_col
6572 || dpyinfo->mouse_face_past_end)))
6573 {
6574 /* Clear the display of the old active region, if any. */
6575 clear_mouse_face (dpyinfo);
6576
6577 /* Find the highest priority overlay that has a mouse-face prop. */
6578 overlay = Qnil;
158cba56 6579 for (i = noverlays - 1; i >= 0; --i)
791f420f
JR
6580 {
6581 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6582 if (!NILP (mouse_face))
6583 {
6584 overlay = overlay_vec[i];
6585 break;
6586 }
6587 }
6588
6589 /* If no overlay applies, get a text property. */
6590 if (NILP (overlay))
6591 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6592
6593 /* Handle the overlay case. */
6594 if (! NILP (overlay))
6595 {
6596 /* Find the range of text around this char that
6597 should be active. */
6598 Lisp_Object before, after;
6599 int ignore;
6600
6601 before = Foverlay_start (overlay);
6602 after = Foverlay_end (overlay);
6603 /* Record this as the current active region. */
6604 fast_find_position (w, XFASTINT (before),
6605 &dpyinfo->mouse_face_beg_col,
6606 &dpyinfo->mouse_face_beg_row,
6607 &dpyinfo->mouse_face_beg_x,
6608 &dpyinfo->mouse_face_beg_y);
6609 dpyinfo->mouse_face_past_end
6610 = !fast_find_position (w, XFASTINT (after),
6611 &dpyinfo->mouse_face_end_col,
6612 &dpyinfo->mouse_face_end_row,
6613 &dpyinfo->mouse_face_end_x,
6614 &dpyinfo->mouse_face_end_y);
6615 dpyinfo->mouse_face_window = window;
6616 dpyinfo->mouse_face_face_id
6617 = face_at_buffer_position (w, pos, 0, 0,
6618 &ignore, pos + 1, 1);
6619
6620 /* Display it as active. */
6621 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6622 }
6623 /* Handle the text property case. */
6624 else if (! NILP (mouse_face))
6625 {
6626 /* Find the range of text around this char that
6627 should be active. */
6628 Lisp_Object before, after, beginning, end;
6629 int ignore;
6630
6631 beginning = Fmarker_position (w->start);
6632 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6633 - XFASTINT (w->window_end_pos)));
6634 before
6635 = Fprevious_single_property_change (make_number (pos + 1),
6636 Qmouse_face,
6637 w->buffer, beginning);
6638 after
6639 = Fnext_single_property_change (position, Qmouse_face,
6640 w->buffer, end);
6641 /* Record this as the current active region. */
6642 fast_find_position (w, XFASTINT (before),
6643 &dpyinfo->mouse_face_beg_col,
6644 &dpyinfo->mouse_face_beg_row,
6645 &dpyinfo->mouse_face_beg_x,
6646 &dpyinfo->mouse_face_beg_y);
6647 dpyinfo->mouse_face_past_end
6648 = !fast_find_position (w, XFASTINT (after),
6649 &dpyinfo->mouse_face_end_col,
6650 &dpyinfo->mouse_face_end_row,
6651 &dpyinfo->mouse_face_end_x,
6652 &dpyinfo->mouse_face_end_y);
6653 dpyinfo->mouse_face_window = window;
6654 dpyinfo->mouse_face_face_id
6655 = face_at_buffer_position (w, pos, 0, 0,
6656 &ignore, pos + 1, 1);
6657
6658 /* Display it as active. */
6659 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6660 }
6661 }
6662
6663 /* Look for a `help-echo' property. */
6664 {
158cba56 6665 Lisp_Object help, overlay;
791f420f
JR
6666
6667 /* Check overlays first. */
c2cc16fa 6668 help = overlay = Qnil;
158cba56
JR
6669 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6670 {
6671 overlay = overlay_vec[i];
6672 help = Foverlay_get (overlay, Qhelp_echo);
6673 }
ec48c3a7
JR
6674
6675 if (!NILP (help))
6676 {
6677 help_echo = help;
158cba56
JR
6678 help_echo_window = window;
6679 help_echo_object = overlay;
ec48c3a7
JR
6680 help_echo_pos = pos;
6681 }
6682 else
6683 {
6684 /* Try text properties. */
6685 if ((STRINGP (glyph->object)
791f420f
JR
6686 && glyph->charpos >= 0
6687 && glyph->charpos < XSTRING (glyph->object)->size)
6688 || (BUFFERP (glyph->object)
6689 && glyph->charpos >= BEGV
ec48c3a7
JR
6690 && glyph->charpos < ZV))
6691 help = Fget_text_property (make_number (glyph->charpos),
6692 Qhelp_echo, glyph->object);
791f420f 6693
ec48c3a7
JR
6694 if (!NILP (help))
6695 {
6696 help_echo = help;
158cba56 6697 help_echo_window = window;
ec48c3a7
JR
6698 help_echo_object = glyph->object;
6699 help_echo_pos = glyph->charpos;
6700 }
6701 }
791f420f
JR
6702 }
6703
6704 BEGV = obegv;
6705 ZV = ozv;
6706 current_buffer = obuf;
6707 }
6708 }
6709}
6710
791f420f
JR
6711static void
6712redo_mouse_highlight ()
6713{
6714 if (!NILP (last_mouse_motion_frame)
6715 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6716 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6717 LOWORD (last_mouse_motion_event.lParam),
6718 HIWORD (last_mouse_motion_event.lParam));
6719}
6720
6721
6722\f
6723/***********************************************************************
6724 Tool-bars
6725 ***********************************************************************/
6726
6727static int x_tool_bar_item P_ ((struct frame *, int, int,
6728 struct glyph **, int *, int *, int *));
6729
6730/* Tool-bar item index of the item on which a mouse button was pressed
6731 or -1. */
6732
6733static int last_tool_bar_item;
6734
6735
6736/* Get information about the tool-bar item at position X/Y on frame F.
6737 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6738 the current matrix of the tool-bar window of F, or NULL if not
6739 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
d417b5e0 6740 item in F->tool_bar_items. Value is
791f420f
JR
6741
6742 -1 if X/Y is not on a tool-bar item
6743 0 if X/Y is on the same item that was highlighted before.
6744 1 otherwise. */
6745
6746static int
6747x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6748 struct frame *f;
6749 int x, y;
6750 struct glyph **glyph;
6751 int *hpos, *vpos, *prop_idx;
6752{
6753 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6754 struct window *w = XWINDOW (f->tool_bar_window);
6755 int area;
6756
6757 /* Find the glyph under X/Y. */
6758 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6759 if (*glyph == NULL)
6760 return -1;
6761
6762 /* Get the start of this tool-bar item's properties in
d417b5e0 6763 f->tool_bar_items. */
791f420f
JR
6764 if (!tool_bar_item_info (f, *glyph, prop_idx))
6765 return -1;
6766
6767 /* Is mouse on the highlighted item? */
6768 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6769 && *vpos >= dpyinfo->mouse_face_beg_row
6770 && *vpos <= dpyinfo->mouse_face_end_row
6771 && (*vpos > dpyinfo->mouse_face_beg_row
6772 || *hpos >= dpyinfo->mouse_face_beg_col)
6773 && (*vpos < dpyinfo->mouse_face_end_row
6774 || *hpos < dpyinfo->mouse_face_end_col
6775 || dpyinfo->mouse_face_past_end))
6776 return 0;
6777
6778 return 1;
6779}
6780
6781
9ef2e2cf 6782/* Handle mouse button event on the tool-bar of frame F, at
791f420f
JR
6783 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6784 or ButtonRelase. */
6785
6786static void
6787w32_handle_tool_bar_click (f, button_event)
6788 struct frame *f;
6789 struct input_event *button_event;
6790{
6791 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6792 struct window *w = XWINDOW (f->tool_bar_window);
6793 int hpos, vpos, prop_idx;
6794 struct glyph *glyph;
6795 Lisp_Object enabled_p;
6796 int x = XFASTINT (button_event->x);
6797 int y = XFASTINT (button_event->y);
6798
6799 /* If not on the highlighted tool-bar item, return. */
6800 frame_to_window_pixel_xy (w, &x, &y);
6801 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6802 return;
6803
6804 /* If item is disabled, do nothing. */
d417b5e0 6805 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
791f420f
JR
6806 if (NILP (enabled_p))
6807 return;
6808
6809 if (button_event->kind == mouse_click)
6810 {
6811 /* Show item in pressed state. */
6812 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6813 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6814 last_tool_bar_item = prop_idx;
6815 }
6816 else
6817 {
6818 Lisp_Object key, frame;
6819 struct input_event event;
6820
6821 /* Show item in released state. */
6822 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6823 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6824
d417b5e0 6825 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
791f420f
JR
6826
6827 XSETFRAME (frame, f);
6828 event.kind = TOOL_BAR_EVENT;
c8b33aa6
GM
6829 event.frame_or_window = frame;
6830 event.arg = frame;
791f420f
JR
6831 kbd_buffer_store_event (&event);
6832
6833 event.kind = TOOL_BAR_EVENT;
c8b33aa6
GM
6834 event.frame_or_window = frame;
6835 event.arg = key;
791f420f
JR
6836 event.modifiers = button_event->modifiers;
6837 kbd_buffer_store_event (&event);
6838 last_tool_bar_item = -1;
6839 }
6840}
6841
6842
9ef2e2cf 6843/* Possibly highlight a tool-bar item on frame F when mouse moves to
791f420f
JR
6844 tool-bar window-relative coordinates X/Y. Called from
6845 note_mouse_highlight. */
6846
6847static void
6848note_tool_bar_highlight (f, x, y)
6849 struct frame *f;
6850 int x, y;
6851{
6852 Lisp_Object window = f->tool_bar_window;
6853 struct window *w = XWINDOW (window);
6854 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6855 int hpos, vpos;
6856 struct glyph *glyph;
6857 struct glyph_row *row;
6858 int i;
6859 Lisp_Object enabled_p;
6860 int prop_idx;
6861 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6862 int mouse_down_p, rc;
6863
6864 /* Function note_mouse_highlight is called with negative x(y
6865 values when mouse moves outside of the frame. */
6866 if (x <= 0 || y <= 0)
6867 {
6868 clear_mouse_face (dpyinfo);
6869 return;
6870 }
6871
6872 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6873 if (rc < 0)
6874 {
6875 /* Not on tool-bar item. */
6876 clear_mouse_face (dpyinfo);
6877 return;
6878 }
6879 else if (rc == 0)
6880 /* On same tool-bar item as before. */
6881 goto set_help_echo;
ee78dc32 6882
791f420f
JR
6883 clear_mouse_face (dpyinfo);
6884
6885 /* Mouse is down, but on different tool-bar item? */
6886 mouse_down_p = (dpyinfo->grabbed
6887 && f == last_mouse_frame
6888 && FRAME_LIVE_P (f));
6889 if (mouse_down_p
6890 && last_tool_bar_item != prop_idx)
6891 return;
ee78dc32 6892
791f420f
JR
6893 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6894 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6895
6896 /* If tool-bar item is not enabled, don't highlight it. */
d417b5e0 6897 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
791f420f
JR
6898 if (!NILP (enabled_p))
6899 {
6900 /* Compute the x-position of the glyph. In front and past the
6901 image is a space. We include this is the highlighted area. */
6902 row = MATRIX_ROW (w->current_matrix, vpos);
6903 for (i = x = 0; i < hpos; ++i)
6904 x += row->glyphs[TEXT_AREA][i].pixel_width;
6905
6906 /* Record this as the current active region. */
6907 dpyinfo->mouse_face_beg_col = hpos;
6908 dpyinfo->mouse_face_beg_row = vpos;
6909 dpyinfo->mouse_face_beg_x = x;
6910 dpyinfo->mouse_face_beg_y = row->y;
6911 dpyinfo->mouse_face_past_end = 0;
6912
6913 dpyinfo->mouse_face_end_col = hpos + 1;
6914 dpyinfo->mouse_face_end_row = vpos;
6915 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6916 dpyinfo->mouse_face_end_y = row->y;
6917 dpyinfo->mouse_face_window = window;
6918 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6919
6920 /* Display it as active. */
6921 show_mouse_face (dpyinfo, draw);
6922 dpyinfo->mouse_face_image_state = draw;
ee78dc32 6923 }
791f420f
JR
6924
6925 set_help_echo:
6926
6927 /* Set help_echo to a help string.to display for this tool-bar item.
6928 w32_read_socket does the rest. */
158cba56 6929 help_echo_object = help_echo_window = Qnil;
ec48c3a7 6930 help_echo_pos = -1;
d417b5e0 6931 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
b7e80413 6932 if (NILP (help_echo))
d417b5e0 6933 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
ee78dc32 6934}
ee78dc32 6935
9ef2e2cf 6936
791f420f
JR
6937\f
6938/* Find the glyph matrix position of buffer position POS in window W.
6939 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6940 current glyphs must be up to date. If POS is above window start
6941 return (0, 0, 0, 0). If POS is after end of W, return end of
6942 last line in W. */
ee78dc32
GV
6943
6944static int
791f420f
JR
6945fast_find_position (w, pos, hpos, vpos, x, y)
6946 struct window *w;
ee78dc32 6947 int pos;
791f420f 6948 int *hpos, *vpos, *x, *y;
ee78dc32 6949{
ee78dc32 6950 int i;
ee78dc32 6951 int lastcol;
791f420f
JR
6952 int maybe_next_line_p = 0;
6953 int line_start_position;
6954 int yb = window_text_bottom_y (w);
6955 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6956 struct glyph_row *best_row = row;
6957 int row_vpos = 0, best_row_vpos = 0;
6958 int current_x;
6959
6960 while (row->y < yb)
ee78dc32 6961 {
791f420f
JR
6962 if (row->used[TEXT_AREA])
6963 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6964 else
6965 line_start_position = 0;
6966
6967 if (line_start_position > pos)
ee78dc32
GV
6968 break;
6969 /* If the position sought is the end of the buffer,
6970 don't include the blank lines at the bottom of the window. */
791f420f
JR
6971 else if (line_start_position == pos
6972 && pos == BUF_ZV (XBUFFER (w->buffer)))
ee78dc32 6973 {
791f420f 6974 maybe_next_line_p = 1;
ee78dc32
GV
6975 break;
6976 }
791f420f
JR
6977 else if (line_start_position > 0)
6978 {
6979 best_row = row;
6980 best_row_vpos = row_vpos;
6981 }
6982
9ef2e2cf
JR
6983 if (row->y + row->height >= yb)
6984 break;
6985
791f420f
JR
6986 ++row;
6987 ++row_vpos;
ee78dc32
GV
6988 }
6989
791f420f
JR
6990 /* Find the right column within BEST_ROW. */
6991 lastcol = 0;
6992 current_x = best_row->x;
6993 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
ee78dc32 6994 {
791f420f
JR
6995 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6996 int charpos;
6997
6998 charpos = glyph->charpos;
6999 if (charpos == pos)
ee78dc32 7000 {
791f420f
JR
7001 *hpos = i;
7002 *vpos = best_row_vpos;
7003 *x = current_x;
7004 *y = best_row->y;
ee78dc32
GV
7005 return 1;
7006 }
791f420f 7007 else if (charpos > pos)
ee78dc32 7008 break;
791f420f
JR
7009 else if (charpos > 0)
7010 lastcol = i;
7011
7012 current_x += glyph->pixel_width;
ee78dc32
GV
7013 }
7014
7015 /* If we're looking for the end of the buffer,
7016 and we didn't find it in the line we scanned,
7017 use the start of the following line. */
791f420f 7018 if (maybe_next_line_p)
ee78dc32 7019 {
791f420f
JR
7020 ++best_row;
7021 ++best_row_vpos;
7022 lastcol = 0;
7023 current_x = best_row->x;
ee78dc32
GV
7024 }
7025
791f420f
JR
7026 *vpos = best_row_vpos;
7027 *hpos = lastcol + 1;
7028 *x = current_x;
7029 *y = best_row->y;
ee78dc32
GV
7030 return 0;
7031}
7032
791f420f 7033
ee78dc32
GV
7034/* Display the active region described by mouse_face_*
7035 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7036
7037static void
791f420f 7038show_mouse_face (dpyinfo, draw)
fbd6baed 7039 struct w32_display_info *dpyinfo;
791f420f 7040 enum draw_glyphs_face draw;
ee78dc32
GV
7041{
7042 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
791f420f 7043 struct frame *f = XFRAME (WINDOW_FRAME (w));
ee78dc32 7044 int i;
791f420f
JR
7045 int cursor_off_p = 0;
7046 struct cursor_pos saved_cursor;
7047
7048 saved_cursor = output_cursor;
7049
7050 /* If window is in the process of being destroyed, don't bother
7051 to do anything. */
7052 if (w->current_matrix == NULL)
7053 goto set_x_cursor;
7054
7055 /* Recognize when we are called to operate on rows that don't exist
7056 anymore. This can happen when a window is split. */
7057 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7058 goto set_x_cursor;
7059
7060 set_output_cursor (&w->phys_cursor);
7061
7062 /* Note that mouse_face_beg_row etc. are window relative. */
7063 for (i = dpyinfo->mouse_face_beg_row;
7064 i <= dpyinfo->mouse_face_end_row;
7065 i++)
7066 {
7067 int start_hpos, end_hpos, start_x;
7068 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7069
7070 /* Don't do anything if row doesn't have valid contents. */
7071 if (!row->enabled_p)
7072 continue;
7073
7074 /* For all but the first row, the highlight starts at column 0. */
7075 if (i == dpyinfo->mouse_face_beg_row)
7076 {
7077 start_hpos = dpyinfo->mouse_face_beg_col;
7078 start_x = dpyinfo->mouse_face_beg_x;
7079 }
7080 else
7081 {
7082 start_hpos = 0;
7083 start_x = 0;
7084 }
7085
7086 if (i == dpyinfo->mouse_face_end_row)
7087 end_hpos = dpyinfo->mouse_face_end_col;
7088 else
7089 end_hpos = row->used[TEXT_AREA];
7090
7091 /* If the cursor's in the text we are about to rewrite, turn the
7092 cursor off. */
7093 if (!w->pseudo_window_p
7094 && i == output_cursor.vpos
7095 && output_cursor.hpos >= start_hpos - 1
7096 && output_cursor.hpos <= end_hpos)
ee78dc32 7097 {
791f420f
JR
7098 x_update_window_cursor (w, 0);
7099 cursor_off_p = 1;
ee78dc32
GV
7100 }
7101
791f420f 7102 if (end_hpos > start_hpos)
ec48c3a7 7103 {
ec48c3a7
JR
7104 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7105 start_hpos, end_hpos, draw, NULL, NULL, 0);
f4fccc1e 7106 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
ec48c3a7 7107 }
ee78dc32
GV
7108 }
7109
7110 /* If we turned the cursor off, turn it back on. */
791f420f
JR
7111 if (cursor_off_p)
7112 x_display_cursor (w, 1,
7113 output_cursor.hpos, output_cursor.vpos,
7114 output_cursor.x, output_cursor.y);
7115
7116 output_cursor = saved_cursor;
7117
7118 set_x_cursor:
01b220b6 7119#if 0 /* TODO: mouse cursor */
791f420f
JR
7120 /* Change the mouse cursor. */
7121 if (draw == DRAW_NORMAL_TEXT)
7122 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7123 f->output_data.x->text_cursor);
7124 else if (draw == DRAW_MOUSE_FACE)
7125 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7126 f->output_data.x->cross_cursor);
ee78dc32 7127 else
791f420f
JR
7128 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7129 f->output_data.x->nontext_cursor);
7130#endif
7131 ;
ee78dc32
GV
7132}
7133
7134/* Clear out the mouse-highlighted active region.
9ef2e2cf 7135 Redraw it un-highlighted first. */
ee78dc32 7136
791f420f 7137void
ee78dc32 7138clear_mouse_face (dpyinfo)
fbd6baed 7139 struct w32_display_info *dpyinfo;
ee78dc32 7140{
c2cc16fa
JR
7141#if 0 /* This prevents redrawing tool bar items when changing from one
7142 to another while a tooltip is open, so don't do it. */
25badd7a 7143 if (!NILP (tip_frame))
791f420f 7144 return;
c2cc16fa 7145#endif
791f420f 7146
ee78dc32 7147 if (! NILP (dpyinfo->mouse_face_window))
791f420f 7148 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
ee78dc32
GV
7149
7150 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7151 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7152 dpyinfo->mouse_face_window = Qnil;
7153}
31d4844a 7154
ec48c3a7
JR
7155
7156/* Clear any mouse-face on window W. This function is part of the
7157 redisplay interface, and is called from try_window_id and similar
7158 functions to ensure the mouse-highlight is off. */
7159
7160static void
7161x_clear_mouse_face (w)
7162 struct window *w;
7163{
7164 struct w32_display_info *dpyinfo
7165 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
7166 Lisp_Object window;
7167
9127e20e 7168 BLOCK_INPUT;
ec48c3a7
JR
7169 XSETWINDOW (window, w);
7170 if (EQ (window, dpyinfo->mouse_face_window))
7171 clear_mouse_face (dpyinfo);
9127e20e 7172 UNBLOCK_INPUT;
ec48c3a7
JR
7173}
7174
7175
31d4844a
KH
7176/* Just discard the mouse face information for frame F, if any.
7177 This is used when the size of F is changed. */
7178
7179void
7180cancel_mouse_face (f)
7181 FRAME_PTR f;
7182{
7183 Lisp_Object window;
7184 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7185
7186 window = dpyinfo->mouse_face_window;
7187 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7188 {
7189 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7190 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7191 dpyinfo->mouse_face_window = Qnil;
7192 }
7193}
ee78dc32 7194\f
9ef2e2cf 7195static struct scroll_bar *x_window_to_scroll_bar ();
ee78dc32
GV
7196static void x_scroll_bar_report_motion ();
7197
7198/* Return the current position of the mouse.
7199 *fp should be a frame which indicates which display to ask about.
7200
7201 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7202 and *part to the frame, window, and scroll bar part that the mouse
7203 is over. Set *x and *y to the portion and whole of the mouse's
7204 position on the scroll bar.
7205
7206 If the mouse movement started elsewhere, set *fp to the frame the
7207 mouse is on, *bar_window to nil, and *x and *y to the character cell
7208 the mouse is over.
7209
9ef2e2cf 7210 Set *time to the server time-stamp for the time at which the mouse
ee78dc32
GV
7211 was at this position.
7212
7213 Don't store anything if we don't have a valid set of values to report.
7214
7215 This clears the mouse_moved flag, so we can wait for the next mouse
9ef2e2cf 7216 movement. */
ee78dc32
GV
7217
7218static void
fbd6baed 7219w32_mouse_position (fp, insist, bar_window, part, x, y, time)
ee78dc32
GV
7220 FRAME_PTR *fp;
7221 int insist;
7222 Lisp_Object *bar_window;
7223 enum scroll_bar_part *part;
7224 Lisp_Object *x, *y;
7225 unsigned long *time;
7226{
7227 FRAME_PTR f1;
7228
7229 BLOCK_INPUT;
7230
95fa970d 7231 if (! NILP (last_mouse_scroll_bar) && insist == 0)
ee78dc32
GV
7232 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7233 else
7234 {
7235 POINT pt;
7236
7237 Lisp_Object frame, tail;
7238
7239 /* Clear the mouse-moved flag for every frame on this display. */
7240 FOR_EACH_FRAME (tail, frame)
7241 XFRAME (frame)->mouse_moved = 0;
7242
7243 last_mouse_scroll_bar = Qnil;
7244
7245 GetCursorPos (&pt);
7246
7247 /* Now we have a position on the root; find the innermost window
7248 containing the pointer. */
7249 {
fbd6baed 7250 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
ee78dc32
GV
7251 && FRAME_LIVE_P (last_mouse_frame))
7252 {
08712a41
AI
7253 /* If mouse was grabbed on a frame, give coords for that frame
7254 even if the mouse is now outside it. */
ee78dc32
GV
7255 f1 = last_mouse_frame;
7256 }
7257 else
7258 {
08712a41 7259 /* Is window under mouse one of our frames? */
9127e20e
JR
7260 f1 = x_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
7261 WindowFromPoint (pt));
ee78dc32
GV
7262 }
7263
7264 /* If not, is it one of our scroll bars? */
7265 if (! f1)
7266 {
9127e20e
JR
7267 struct scroll_bar *bar
7268 = x_window_to_scroll_bar (WindowFromPoint (pt));
ee78dc32
GV
7269
7270 if (bar)
7271 {
7272 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7273 }
7274 }
7275
95fa970d 7276 if (f1 == 0 && insist > 0)
791f420f 7277 f1 = SELECTED_FRAME ();
ee78dc32
GV
7278
7279 if (f1)
7280 {
791f420f
JR
7281 /* Ok, we found a frame. Store all the values.
7282 last_mouse_glyph is a rectangle used to reduce the
7283 generation of mouse events. To not miss any motion
7284 events, we must divide the frame into rectangles of the
7285 size of the smallest character that could be displayed
7286 on it, i.e. into the same rectangles that matrices on
7287 the frame are divided into. */
7288
7289#if OLD_REDISPLAY_CODE
ee78dc32
GV
7290 int ignore1, ignore2;
7291
fbd6baed 7292 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
ee78dc32 7293
ee78dc32
GV
7294 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
7295 &last_mouse_glyph,
fbd6baed 7296 FRAME_W32_DISPLAY_INFO (f1)->grabbed
ee78dc32 7297 || insist);
791f420f
JR
7298#else
7299 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7300 {
7301 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7302 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7303 int x = pt.x;
7304 int y = pt.y;
7305
7306 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7307 round down even for negative values. */
7308 if (x < 0)
7309 x -= width - 1;
7310 if (y < 0)
7311 y -= height - 1;
7312
7313 last_mouse_glyph.left = (x + width - 1) / width * width;
7314 last_mouse_glyph.top = (y + height - 1) / height * height;
7315 last_mouse_glyph.right = last_mouse_glyph.left + width;
7316 last_mouse_glyph.bottom = last_mouse_glyph.top + height;
7317 }
7318#endif
ee78dc32
GV
7319
7320 *bar_window = Qnil;
7321 *part = 0;
7322 *fp = f1;
7323 XSETINT (*x, pt.x);
7324 XSETINT (*y, pt.y);
7325 *time = last_mouse_movement_time;
7326 }
7327 }
7328 }
7329
7330 UNBLOCK_INPUT;
7331}
791f420f 7332
ee78dc32
GV
7333\f
7334/* Scroll bar support. */
7335
ec48c3a7 7336/* Given a window ID, find the struct scroll_bar which manages it.
ee78dc32
GV
7337 This can be called in GC, so we have to make sure to strip off mark
7338 bits. */
9ef2e2cf
JR
7339
7340static struct scroll_bar *
ee78dc32
GV
7341x_window_to_scroll_bar (window_id)
7342 Window window_id;
7343{
791f420f 7344 Lisp_Object tail;
ee78dc32
GV
7345
7346 for (tail = Vframe_list;
7347 XGCTYPE (tail) == Lisp_Cons;
8e713be6 7348 tail = XCDR (tail))
ee78dc32
GV
7349 {
7350 Lisp_Object frame, bar, condemned;
7351
8e713be6 7352 frame = XCAR (tail);
ee78dc32
GV
7353 /* All elements of Vframe_list should be frames. */
7354 if (! GC_FRAMEP (frame))
7355 abort ();
7356
7357 /* Scan this frame's scroll bar list for a scroll bar with the
7358 right window ID. */
7359 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7360 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7361 /* This trick allows us to search both the ordinary and
7362 condemned scroll bar lists with one loop. */
7363 ! GC_NILP (bar) || (bar = condemned,
7364 condemned = Qnil,
7365 ! GC_NILP (bar));
7366 bar = XSCROLL_BAR (bar)->next)
fbd6baed 7367 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
ee78dc32
GV
7368 return XSCROLL_BAR (bar);
7369 }
7370
7371 return 0;
7372}
7373
791f420f
JR
7374
7375\f
7376/* Set the thumb size and position of scroll bar BAR. We are currently
7377 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7378
7379static void
7380w32_set_scroll_bar_thumb (bar, portion, position, whole)
7381 struct scroll_bar *bar;
7382 int portion, position, whole;
7383{
7384 Window w = SCROLL_BAR_W32_WINDOW (bar);
d8e675f5 7385 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
791f420f
JR
7386 int sb_page, sb_pos;
7387 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
7388
7389 if (whole)
7390 {
7391 /* Position scroll bar at rock bottom if the bottom of the
7392 buffer is visible. This avoids shinking the thumb away
7393 to nothing if it is held at the bottom of the buffer. */
7394 if (position + portion >= whole)
7395 {
7396 sb_page = range * (whole - position) / whole
7397 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7398 sb_pos = range;
7399 }
7400
7401 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7402 sb_pos = position * range / whole;
7403 }
7404 else
7405 {
7406 sb_page = range;
7407 sb_pos = 0;
7408 }
7409
7410 BLOCK_INPUT;
7411
7412 if (pfnSetScrollInfo)
7413 {
7414 SCROLLINFO si;
7415
7416 si.cbSize = sizeof (si);
7417 /* Only update page size if currently dragging, to reduce
7418 flicker effects. */
7419 if (draggingp)
7420 si.fMask = SIF_PAGE;
7421 else
7422 si.fMask = SIF_PAGE | SIF_POS;
7423 si.nPage = sb_page;
7424 si.nPos = sb_pos;
7425
7426 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
7427 }
7428 else
7429 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
7430
7431 UNBLOCK_INPUT;
7432}
7433
7434\f
7435/************************************************************************
7436 Scroll bars, general
7437 ************************************************************************/
7438
ee78dc32
GV
7439HWND
7440my_create_scrollbar (f, bar)
7441 struct frame * f;
7442 struct scroll_bar * bar;
7443{
689004fa
GV
7444 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
7445 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
7446 (LPARAM) bar);
ee78dc32
GV
7447}
7448
52cf03a1
GV
7449//#define ATTACH_THREADS
7450
689004fa
GV
7451BOOL
7452my_show_window (FRAME_PTR f, HWND hwnd, int how)
52cf03a1
GV
7453{
7454#ifndef ATTACH_THREADS
689004fa
GV
7455 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
7456 (WPARAM) hwnd, (LPARAM) how);
52cf03a1 7457#else
689004fa 7458 return ShowWindow (hwnd, how);
52cf03a1
GV
7459#endif
7460}
7461
7462void
7463my_set_window_pos (HWND hwnd, HWND hwndAfter,
689004fa 7464 int x, int y, int cx, int cy, UINT flags)
52cf03a1
GV
7465{
7466#ifndef ATTACH_THREADS
689004fa
GV
7467 WINDOWPOS pos;
7468 pos.hwndInsertAfter = hwndAfter;
52cf03a1
GV
7469 pos.x = x;
7470 pos.y = y;
7471 pos.cx = cx;
7472 pos.cy = cy;
7473 pos.flags = flags;
7474 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
7475#else
7476 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
7477#endif
7478}
7479
ec48c3a7 7480void
689004fa
GV
7481my_set_focus (f, hwnd)
7482 struct frame * f;
7483 HWND hwnd;
7484{
7485 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
7486 (WPARAM) hwnd, 0);
7487}
7488
ec48c3a7 7489void
ef0e360f
GV
7490my_set_foreground_window (hwnd)
7491 HWND hwnd;
7492{
7493 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
7494}
7495
ee78dc32
GV
7496void
7497my_destroy_window (f, hwnd)
7498 struct frame * f;
7499 HWND hwnd;
7500{
fbd6baed 7501 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
ee78dc32
GV
7502 (WPARAM) hwnd, 0);
7503}
7504
791f420f
JR
7505/* Create a scroll bar and return the scroll bar vector for it. W is
7506 the Emacs window on which to create the scroll bar. TOP, LEFT,
7507 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7508 scroll bar. */
7509
ee78dc32 7510static struct scroll_bar *
791f420f
JR
7511x_scroll_bar_create (w, top, left, width, height)
7512 struct window *w;
ee78dc32
GV
7513 int top, left, width, height;
7514{
791f420f
JR
7515 struct frame *f = XFRAME (WINDOW_FRAME (w));
7516 HWND hwnd;
ee78dc32
GV
7517 struct scroll_bar *bar
7518 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
ee78dc32
GV
7519
7520 BLOCK_INPUT;
7521
791f420f 7522 XSETWINDOW (bar->window, w);
ee78dc32
GV
7523 XSETINT (bar->top, top);
7524 XSETINT (bar->left, left);
7525 XSETINT (bar->width, width);
7526 XSETINT (bar->height, height);
7527 XSETINT (bar->start, 0);
7528 XSETINT (bar->end, 0);
7529 bar->dragging = Qnil;
7530
7531 /* Requires geometry to be set before call to create the real window */
7532
7533 hwnd = my_create_scrollbar (f, bar);
7534
689004fa
GV
7535 if (pfnSetScrollInfo)
7536 {
7537 SCROLLINFO si;
7538
7539 si.cbSize = sizeof (si);
7540 si.fMask = SIF_ALL;
7541 si.nMin = 0;
791f420f
JR
7542 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7543 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
689004fa
GV
7544 si.nPage = si.nMax;
7545 si.nPos = 0;
7546
7547 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7548 }
7549 else
7550 {
791f420f
JR
7551 SetScrollRange (hwnd, SB_CTL, 0,
7552 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
689004fa
GV
7553 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
7554 }
ee78dc32 7555
fbd6baed 7556 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
ee78dc32
GV
7557
7558 /* Add bar to its frame's list of scroll bars. */
7559 bar->next = FRAME_SCROLL_BARS (f);
7560 bar->prev = Qnil;
7561 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7562 if (! NILP (bar->next))
7563 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7564
7565 UNBLOCK_INPUT;
7566
7567 return bar;
7568}
7569
ee78dc32 7570
791f420f
JR
7571/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7572 nil. */
ee78dc32 7573
ee78dc32
GV
7574static void
7575x_scroll_bar_remove (bar)
7576 struct scroll_bar *bar;
7577{
7578 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7579
7580 BLOCK_INPUT;
7581
7582 /* Destroy the window. */
fbd6baed 7583 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
ee78dc32
GV
7584
7585 /* Disassociate this scroll bar from its window. */
7586 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
7587
7588 UNBLOCK_INPUT;
7589}
7590
7591/* Set the handle of the vertical scroll bar for WINDOW to indicate
7592 that we are displaying PORTION characters out of a total of WHOLE
7593 characters, starting at POSITION. If WINDOW has no scroll bar,
7594 create one. */
7595static void
791f420f
JR
7596w32_set_vertical_scroll_bar (w, portion, whole, position)
7597 struct window *w;
ee78dc32
GV
7598 int portion, whole, position;
7599{
791f420f 7600 struct frame *f = XFRAME (w->frame);
ee78dc32 7601 struct scroll_bar *bar;
9ef2e2cf 7602 int top, height, left, sb_left, width, sb_width;
791f420f
JR
7603 int window_x, window_y, window_width, window_height;
7604
7605 /* Get window dimensions. */
7606 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7607 top = window_y;
7608 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7609 height = window_height;
7610
7611 /* Compute the left edge of the scroll bar area. */
7612 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7613 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7614 else
7615 left = XFASTINT (w->left);
7616 left *= CANON_X_UNIT (f);
7617 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7618
7619 /* Compute the width of the scroll bar which might be less than
7620 the width of the area reserved for the scroll bar. */
7621 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7622 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
7623 else
7624 sb_width = width;
ee78dc32 7625
791f420f
JR
7626 /* Compute the left edge of the scroll bar. */
7627 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7628 sb_left = left + width - sb_width - (width - sb_width) / 2;
ee78dc32 7629 else
791f420f
JR
7630 sb_left = left + (width - sb_width) / 2;
7631
7632 /* Does the scroll bar exist yet? */
7633 if (NILP (w->vertical_scroll_bar))
ee78dc32 7634 {
00fe468b 7635 HDC hdc;
791f420f 7636 BLOCK_INPUT;
00fe468b
JR
7637 hdc = get_frame_dc (f);
7638 w32_clear_area (f, hdc, left, top, width, height);
7639 release_frame_dc (f, hdc);
791f420f 7640 UNBLOCK_INPUT;
00fe468b 7641
791f420f 7642 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
ee78dc32 7643 }
791f420f
JR
7644 else
7645 {
7646 /* It may just need to be moved and resized. */
7647 HWND hwnd;
7648
7649 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7650 hwnd = SCROLL_BAR_W32_WINDOW (bar);
7651
7652 /* If already correctly positioned, do nothing. */
7653 if ( XINT (bar->left) == sb_left
7654 && XINT (bar->top) == top
7655 && XINT (bar->width) == sb_width
7656 && XINT (bar->height) == height )
7657 {
7658 /* Redraw after clear_frame. */
7659 if (!my_show_window (f, hwnd, SW_NORMAL))
7660 InvalidateRect (hwnd, NULL, FALSE);
7661 }
7662 else
7663 {
00fe468b 7664 HDC hdc;
791f420f
JR
7665 BLOCK_INPUT;
7666
00fe468b 7667 hdc = get_frame_dc (f);
791f420f
JR
7668 /* Since Windows scroll bars are smaller than the space reserved
7669 for them on the frame, we have to clear "under" them. */
00fe468b 7670 w32_clear_area (f, hdc,
791f420f
JR
7671 left,
7672 top,
7673 width,
7674 height);
00fe468b 7675 release_frame_dc (f, hdc);
791f420f
JR
7676
7677 /* Make sure scroll bar is "visible" before moving, to ensure the
7678 area of the parent window now exposed will be refreshed. */
7679 my_show_window (f, hwnd, SW_HIDE);
7680 MoveWindow (hwnd, sb_left, top,
7681 sb_width, height, TRUE);
7682 if (pfnSetScrollInfo)
7683 {
7684 SCROLLINFO si;
ee78dc32 7685
791f420f
JR
7686 si.cbSize = sizeof (si);
7687 si.fMask = SIF_RANGE;
7688 si.nMin = 0;
7689 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7690 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
ee78dc32 7691
00fe468b 7692 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
791f420f
JR
7693 }
7694 else
00fe468b 7695 SetScrollRange (hwnd, SB_CTL, 0,
791f420f
JR
7696 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7697 my_show_window (f, hwnd, SW_NORMAL);
7698 // InvalidateRect (w, NULL, FALSE);
7699
7700 /* Remember new settings. */
7701 XSETINT (bar->left, sb_left);
7702 XSETINT (bar->top, top);
7703 XSETINT (bar->width, sb_width);
7704 XSETINT (bar->height, height);
7705
7706 UNBLOCK_INPUT;
7707 }
7708 }
7709 w32_set_scroll_bar_thumb (bar, portion, position, whole);
ee78dc32 7710
791f420f 7711 XSETVECTOR (w->vertical_scroll_bar, bar);
ee78dc32
GV
7712}
7713
7714
7715/* The following three hooks are used when we're doing a thorough
7716 redisplay of the frame. We don't explicitly know which scroll bars
7717 are going to be deleted, because keeping track of when windows go
7718 away is a real pain - "Can you say set-window-configuration, boys
7719 and girls?" Instead, we just assert at the beginning of redisplay
7720 that *all* scroll bars are to be removed, and then save a scroll bar
7721 from the fiery pit when we actually redisplay its window. */
7722
7723/* Arrange for all scroll bars on FRAME to be removed at the next call
7724 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9ef2e2cf
JR
7725 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7726
ee78dc32 7727static void
fbd6baed 7728w32_condemn_scroll_bars (frame)
ee78dc32
GV
7729 FRAME_PTR frame;
7730{
ef0e360f
GV
7731 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7732 while (! NILP (FRAME_SCROLL_BARS (frame)))
7733 {
7734 Lisp_Object bar;
7735 bar = FRAME_SCROLL_BARS (frame);
7736 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7737 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7738 XSCROLL_BAR (bar)->prev = Qnil;
7739 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7740 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7741 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7742 }
ee78dc32
GV
7743}
7744
c2cc16fa 7745
9ef2e2cf 7746/* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
ee78dc32 7747 Note that WINDOW isn't necessarily condemned at all. */
c2cc16fa 7748
ee78dc32 7749static void
fbd6baed 7750w32_redeem_scroll_bar (window)
ee78dc32
GV
7751 struct window *window;
7752{
7753 struct scroll_bar *bar;
c2cc16fa 7754 struct frame *f;
ee78dc32
GV
7755
7756 /* We can't redeem this window's scroll bar if it doesn't have one. */
7757 if (NILP (window->vertical_scroll_bar))
7758 abort ();
7759
7760 bar = XSCROLL_BAR (window->vertical_scroll_bar);
7761
ef0e360f 7762 /* Unlink it from the condemned list. */
c2cc16fa
JR
7763 f = XFRAME (WINDOW_FRAME (window));
7764 if (NILP (bar->prev))
7765 {
7766 /* If the prev pointer is nil, it must be the first in one of
7767 the lists. */
7768 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
7769 /* It's not condemned. Everything's fine. */
7770 return;
7771 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7772 window->vertical_scroll_bar))
7773 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
7774 else
7775 /* If its prev pointer is nil, it must be at the front of
7776 one or the other! */
7777 abort ();
7778 }
7779 else
7780 XSCROLL_BAR (bar->prev)->next = bar->next;
ef0e360f 7781
c2cc16fa
JR
7782 if (! NILP (bar->next))
7783 XSCROLL_BAR (bar->next)->prev = bar->prev;
ef0e360f 7784
c2cc16fa
JR
7785 bar->next = FRAME_SCROLL_BARS (f);
7786 bar->prev = Qnil;
7787 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7788 if (! NILP (bar->next))
7789 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
ee78dc32
GV
7790}
7791
7792/* Remove all scroll bars on FRAME that haven't been saved since the
7793 last call to `*condemn_scroll_bars_hook'. */
9ef2e2cf 7794
ee78dc32 7795static void
fbd6baed 7796w32_judge_scroll_bars (f)
ee78dc32
GV
7797 FRAME_PTR f;
7798{
7799 Lisp_Object bar, next;
7800
7801 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7802
7803 /* Clear out the condemned list now so we won't try to process any
7804 more events on the hapless scroll bars. */
7805 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
7806
7807 for (; ! NILP (bar); bar = next)
7808 {
7809 struct scroll_bar *b = XSCROLL_BAR (bar);
7810
7811 x_scroll_bar_remove (b);
7812
7813 next = b->next;
7814 b->next = b->prev = Qnil;
7815 }
7816
7817 /* Now there should be no references to the condemned scroll bars,
7818 and they should get garbage-collected. */
7819}
7820
7821/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7822 is set to something other than no_event, it is enqueued.
7823
7824 This may be called from a signal handler, so we have to ignore GC
7825 mark bits. */
2c28562d 7826
52cf03a1 7827static int
c2cc16fa 7828w32_scroll_bar_handle_click (bar, msg, emacs_event)
ee78dc32 7829 struct scroll_bar *bar;
fbd6baed 7830 W32Msg *msg;
ee78dc32
GV
7831 struct input_event *emacs_event;
7832{
7833 if (! GC_WINDOWP (bar->window))
7834 abort ();
7835
fbd6baed 7836 emacs_event->kind = w32_scroll_bar_click;
ee78dc32 7837 emacs_event->code = 0;
52cf03a1
GV
7838 /* not really meaningful to distinguish up/down */
7839 emacs_event->modifiers = msg->dwModifiers;
ee78dc32 7840 emacs_event->frame_or_window = bar->window;
7e889510 7841 emacs_event->arg = Qnil;
ee78dc32
GV
7842 emacs_event->timestamp = msg->msg.time;
7843
7844 {
791f420f 7845 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
689004fa
GV
7846 int y;
7847 int dragging = !NILP (bar->dragging);
7848
7849 if (pfnGetScrollInfo)
7850 {
7851 SCROLLINFO si;
7852
7853 si.cbSize = sizeof (si);
7854 si.fMask = SIF_POS;
7855
7856 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
7857 y = si.nPos;
7858 }
7859 else
7860 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
7861
7862 bar->dragging = Qnil;
ee78dc32 7863
9ef2e2cf
JR
7864
7865 last_mouse_scroll_bar_pos = msg->msg.wParam;
7866
ee78dc32 7867 switch (LOWORD (msg->msg.wParam))
2c28562d 7868 {
2c28562d 7869 case SB_LINEDOWN:
52cf03a1 7870 emacs_event->part = scroll_bar_down_arrow;
ee78dc32 7871 break;
2c28562d 7872 case SB_LINEUP:
52cf03a1 7873 emacs_event->part = scroll_bar_up_arrow;
ee78dc32 7874 break;
2c28562d 7875 case SB_PAGEUP:
ee78dc32
GV
7876 emacs_event->part = scroll_bar_above_handle;
7877 break;
2c28562d 7878 case SB_PAGEDOWN:
ee78dc32
GV
7879 emacs_event->part = scroll_bar_below_handle;
7880 break;
2c28562d 7881 case SB_TOP:
ee78dc32
GV
7882 emacs_event->part = scroll_bar_handle;
7883 y = 0;
7884 break;
2c28562d 7885 case SB_BOTTOM:
ee78dc32
GV
7886 emacs_event->part = scroll_bar_handle;
7887 y = top_range;
7888 break;
689004fa 7889 case SB_THUMBTRACK:
2c28562d 7890 case SB_THUMBPOSITION:
791f420f
JR
7891 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7892 y = HIWORD (msg->msg.wParam);
689004fa 7893 bar->dragging = Qt;
ee78dc32 7894 emacs_event->part = scroll_bar_handle;
689004fa
GV
7895
7896 /* "Silently" update current position. */
7897 if (pfnSetScrollInfo)
7898 {
7899 SCROLLINFO si;
7900
7901 si.cbSize = sizeof (si);
7902 si.fMask = SIF_POS;
689004fa
GV
7903 si.nPos = y;
7904 /* Remember apparent position (we actually lag behind the real
7905 position, so don't set that directly. */
7906 last_scroll_bar_drag_pos = y;
7907
7908 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
7909 }
7910 else
7911 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
ee78dc32 7912 break;
2c28562d 7913 case SB_ENDSCROLL:
689004fa
GV
7914 /* If this is the end of a drag sequence, then reset the scroll
7915 handle size to normal and do a final redraw. Otherwise do
7916 nothing. */
7917 if (dragging)
7918 {
7919 if (pfnSetScrollInfo)
7920 {
7921 SCROLLINFO si;
7922 int start = XINT (bar->start);
7923 int end = XINT (bar->end);
7924
7925 si.cbSize = sizeof (si);
7926 si.fMask = SIF_PAGE | SIF_POS;
791f420f 7927 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
689004fa 7928 si.nPos = last_scroll_bar_drag_pos;
689004fa
GV
7929 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
7930 }
7931 else
7932 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
7933 }
7934 /* fall through */
2c28562d 7935 default:
689004fa 7936 emacs_event->kind = no_event;
52cf03a1 7937 return FALSE;
2c28562d 7938 }
52cf03a1 7939
ee78dc32
GV
7940 XSETINT (emacs_event->x, y);
7941 XSETINT (emacs_event->y, top_range);
52cf03a1
GV
7942
7943 return TRUE;
ee78dc32
GV
7944 }
7945}
7946
7947/* Return information to the user about the current position of the mouse
7948 on the scroll bar. */
9ef2e2cf 7949
ee78dc32
GV
7950static void
7951x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
7952 FRAME_PTR *fp;
7953 Lisp_Object *bar_window;
7954 enum scroll_bar_part *part;
7955 Lisp_Object *x, *y;
7956 unsigned long *time;
7957{
7958 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
fbd6baed 7959 Window w = SCROLL_BAR_W32_WINDOW (bar);
ee78dc32
GV
7960 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7961 int pos;
791f420f 7962 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
ee78dc32
GV
7963
7964 BLOCK_INPUT;
7965
7966 *fp = f;
7967 *bar_window = bar->window;
7968
689004fa
GV
7969 if (pfnGetScrollInfo)
7970 {
7971 SCROLLINFO si;
7972
7973 si.cbSize = sizeof (si);
7974 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
7975
7976 pfnGetScrollInfo (w, SB_CTL, &si);
7977 pos = si.nPos;
7978 top_range = si.nMax - si.nPage + 1;
7979 }
7980 else
7981 pos = GetScrollPos (w, SB_CTL);
ee78dc32
GV
7982
7983 switch (LOWORD (last_mouse_scroll_bar_pos))
7984 {
7985 case SB_THUMBPOSITION:
7986 case SB_THUMBTRACK:
7987 *part = scroll_bar_handle;
791f420f 7988 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
ee78dc32
GV
7989 pos = HIWORD (last_mouse_scroll_bar_pos);
7990 break;
7991 case SB_LINEDOWN:
7992 *part = scroll_bar_handle;
7993 pos++;
7994 break;
7995 default:
7996 *part = scroll_bar_handle;
7997 break;
7998 }
7999
9127e20e
JR
8000 XSETINT (*x, pos);
8001 XSETINT (*y, top_range);
ee78dc32
GV
8002
8003 f->mouse_moved = 0;
8004 last_mouse_scroll_bar = Qnil;
8005
8006 *time = last_mouse_movement_time;
8007
8008 UNBLOCK_INPUT;
8009}
8010
9ef2e2cf 8011
ee78dc32
GV
8012/* The screen has been cleared so we may have changed foreground or
8013 background colors, and the scroll bars may need to be redrawn.
8014 Clear out the scroll bars, and ask for expose events, so we can
8015 redraw them. */
9ef2e2cf 8016
791f420f 8017void
ee78dc32 8018x_scroll_bar_clear (f)
9ef2e2cf 8019 FRAME_PTR f;
ee78dc32 8020{
ee78dc32
GV
8021 Lisp_Object bar;
8022
9ef2e2cf
JR
8023 /* We can have scroll bars even if this is 0,
8024 if we just turned off scroll bar mode.
8025 But in that case we should not clear them. */
8026 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8027 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8028 bar = XSCROLL_BAR (bar)->next)
8029 {
8030 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
8031 HDC hdc = GetDC (window);
8032 RECT rect;
52cf03a1 8033
9ef2e2cf
JR
8034 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
8035 arranges to refresh the scroll bar if hidden. */
8036 my_show_window (f, window, SW_HIDE);
689004fa 8037
9ef2e2cf
JR
8038 GetClientRect (window, &rect);
8039 select_palette (f, hdc);
8040 w32_clear_rect (f, hdc, &rect);
8041 deselect_palette (f, hdc);
689004fa 8042
9ef2e2cf
JR
8043 ReleaseDC (window, hdc);
8044 }
52cf03a1
GV
8045}
8046
ee78dc32 8047\f
fbd6baed 8048/* The main W32 event-reading loop - w32_read_socket. */
ee78dc32 8049
9ef2e2cf 8050/* Time stamp of enter window event. This is only used by w32_read_socket,
ee78dc32
GV
8051 but we have to put it out here, since static variables within functions
8052 sometimes don't work. */
9ef2e2cf 8053
ee78dc32
GV
8054static Time enter_timestamp;
8055
8056/* Record the last 100 characters stored
8057 to help debug the loss-of-chars-during-GC problem. */
9ef2e2cf
JR
8058
8059static int temp_index;
8060static short temp_buffer[100];
ee78dc32 8061
afd153f0 8062
fbd6baed 8063/* Read events coming from the W32 shell.
ee78dc32
GV
8064 This routine is called by the SIGIO handler.
8065 We return as soon as there are no more events to be read.
8066
8067 Events representing keys are stored in buffer BUFP,
8068 which can hold up to NUMCHARS characters.
8069 We return the number of characters stored into the buffer,
8070 thus pretending to be `read'.
8071
ee78dc32
GV
8072 EXPECTED is nonzero if the caller knows input is available.
8073
8074 Some of these messages are reposted back to the message queue since the
e9e23e23
GV
8075 system calls the windows proc directly in a context where we cannot return
8076 the data nor can we guarantee the state we are in. So if we dispatch them
ee78dc32
GV
8077 we will get into an infinite loop. To prevent this from ever happening we
8078 will set a variable to indicate we are in the read_socket call and indicate
e9e23e23
GV
8079 which message we are processing since the windows proc gets called
8080 recursively with different messages by the system.
ee78dc32
GV
8081*/
8082
8083int
3aad7613 8084w32_read_socket (sd, bufp, numchars, expected)
ee78dc32 8085 register int sd;
9ef2e2cf
JR
8086 /* register */ struct input_event *bufp;
8087 /* register */ int numchars;
ee78dc32
GV
8088 int expected;
8089{
8090 int count = 0;
689004fa 8091 int check_visibility = 0;
fbd6baed 8092 W32Msg msg;
ee78dc32 8093 struct frame *f;
fbd6baed 8094 struct w32_display_info *dpyinfo = &one_w32_display_info;
ee78dc32
GV
8095
8096 if (interrupt_input_blocked)
8097 {
8098 interrupt_input_pending = 1;
8099 return -1;
8100 }
8101
8102 interrupt_input_pending = 0;
8103 BLOCK_INPUT;
8104
8105 /* So people can tell when we have read the available input. */
8106 input_signal_count++;
8107
8108 if (numchars <= 0)
8109 abort (); /* Don't think this happens. */
8110
01b220b6 8111 /* TODO: tooltips, tool-bars, ghostscript integration, mouse
791f420f 8112 cursors. */
52cf03a1 8113 while (get_next_msg (&msg, FALSE))
ee78dc32
GV
8114 {
8115 switch (msg.msg.message)
8116 {
ee78dc32 8117 case WM_PAINT:
ee78dc32
GV
8118 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8119
8120 if (f)
8121 {
014b6ea1
GV
8122 if (msg.rect.right == msg.rect.left ||
8123 msg.rect.bottom == msg.rect.top)
ee78dc32 8124 {
014b6ea1
GV
8125 /* We may get paint messages even though the client
8126 area is clipped - these are not expose events. */
8127 DebPrint (("clipped frame %04x (%s) got WM_PAINT\n", f,
8128 XSTRING (f->name)->data));
8129 }
8130 else if (f->async_visible != 1)
8131 {
8132 /* Definitely not obscured, so mark as visible. */
ee78dc32
GV
8133 f->async_visible = 1;
8134 f->async_iconified = 0;
8135 SET_FRAME_GARBAGED (f);
014b6ea1
GV
8136 DebPrint (("frame %04x (%s) reexposed\n", f,
8137 XSTRING (f->name)->data));
689004fa
GV
8138
8139 /* WM_PAINT serves as MapNotify as well, so report
8140 visibility changes properly. */
8141 if (f->iconified)
8142 {
8143 bufp->kind = deiconify_event;
8144 XSETFRAME (bufp->frame_or_window, f);
7e889510 8145 bufp->arg = Qnil;
689004fa
GV
8146 bufp++;
8147 count++;
8148 numchars--;
8149 }
9127e20e 8150 else if (! NILP (Vframe_list)
8e713be6 8151 && ! NILP (XCDR (Vframe_list)))
689004fa
GV
8152 /* Force a redisplay sooner or later to update the
8153 frame titles in case this is the second frame. */
8154 record_asynch_buffer_change ();
ee78dc32
GV
8155 }
8156 else
8157 {
00fe468b
JR
8158 HDC hdc = get_frame_dc (f);
8159
97e6de38 8160 /* Erase background again for safety. */
00fe468b
JR
8161 w32_clear_rect (f, hdc, &msg.rect);
8162 release_frame_dc (f, hdc);
791f420f
JR
8163 expose_frame (f,
8164 msg.rect.left,
8165 msg.rect.top,
8166 msg.rect.right - msg.rect.left,
8167 msg.rect.bottom - msg.rect.top);
ee78dc32
GV
8168 }
8169 }
52cf03a1 8170 break;
689004fa 8171
f98169a0
GV
8172 case WM_INPUTLANGCHANGE:
8173 /* Generate a language change event. */
8174 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8175
8176 if (f)
8177 {
8178 if (numchars == 0)
8179 abort ();
8180
8181 bufp->kind = language_change_event;
8182 XSETFRAME (bufp->frame_or_window, f);
7e889510 8183 bufp->arg = Qnil;
f98169a0
GV
8184 bufp->code = msg.msg.wParam;
8185 bufp->modifiers = msg.msg.lParam & 0xffff;
8186 bufp++;
8187 count++;
8188 numchars--;
8189 }
8190 break;
8191
ee78dc32
GV
8192 case WM_KEYDOWN:
8193 case WM_SYSKEYDOWN:
8194 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8195
8196 if (f && !f->iconified)
8197 {
8198 if (temp_index == sizeof temp_buffer / sizeof (short))
8199 temp_index = 0;
8200 temp_buffer[temp_index++] = msg.msg.wParam;
8201 bufp->kind = non_ascii_keystroke;
8202 bufp->code = msg.msg.wParam;
f98169a0 8203 bufp->modifiers = msg.dwModifiers;
ee78dc32 8204 XSETFRAME (bufp->frame_or_window, f);
7e889510 8205 bufp->arg = Qnil;
ee78dc32
GV
8206 bufp->timestamp = msg.msg.time;
8207 bufp++;
8208 numchars--;
8209 count++;
8210 }
8211 break;
689004fa 8212
ee78dc32
GV
8213 case WM_SYSCHAR:
8214 case WM_CHAR:
8215 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8216
8217 if (f && !f->iconified)
8218 {
f98169a0
GV
8219 if (temp_index == sizeof temp_buffer / sizeof (short))
8220 temp_index = 0;
8221 temp_buffer[temp_index++] = msg.msg.wParam;
8222 bufp->kind = ascii_keystroke;
8223 bufp->code = msg.msg.wParam;
8224 bufp->modifiers = msg.dwModifiers;
8225 XSETFRAME (bufp->frame_or_window, f);
7e889510 8226 bufp->arg = Qnil;
f98169a0
GV
8227 bufp->timestamp = msg.msg.time;
8228 bufp++;
8229 numchars--;
8230 count++;
ee78dc32
GV
8231 }
8232 break;
689004fa 8233
ee78dc32 8234 case WM_MOUSEMOVE:
791f420f 8235 previous_help_echo = help_echo;
158cba56
JR
8236 help_echo = help_echo_object = help_echo_window = Qnil;
8237 help_echo_pos = -1;
791f420f 8238
ee78dc32
GV
8239 if (dpyinfo->grabbed && last_mouse_frame
8240 && FRAME_LIVE_P (last_mouse_frame))
8241 f = last_mouse_frame;
8242 else
8243 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8244
8245 if (f)
8246 note_mouse_movement (f, &msg.msg);
8247 else
791f420f
JR
8248 {
8249 /* If we move outside the frame, then we're
8250 certainly no longer on any text in the frame. */
5b844253 8251 clear_mouse_face (dpyinfo);
791f420f
JR
8252 }
8253
8254 /* If the contents of the global variable help_echo
8255 has changed, generate a HELP_EVENT. */
b7e80413
SM
8256 if (!NILP (help_echo)
8257 || !NILP (previous_help_echo))
791f420f
JR
8258 {
8259 Lisp_Object frame;
ec48c3a7 8260 int n;
791f420f
JR
8261
8262 if (f)
8263 XSETFRAME (frame, f);
8264 else
8265 frame = Qnil;
8266
8267 any_help_event_p = 1;
b4165314
GM
8268 n = gen_help_event (bufp, numchars, help_echo, frame,
8269 help_echo_window, help_echo_object,
8270 help_echo_pos);
ec48c3a7 8271 bufp += n, count += n, numchars -= n;
791f420f
JR
8272 }
8273 break;
689004fa 8274
ee78dc32
GV
8275 case WM_LBUTTONDOWN:
8276 case WM_LBUTTONUP:
8277 case WM_MBUTTONDOWN:
8278 case WM_MBUTTONUP:
8279 case WM_RBUTTONDOWN:
8280 case WM_RBUTTONUP:
8281 {
791f420f
JR
8282 /* If we decide we want to generate an event to be seen
8283 by the rest of Emacs, we put it here. */
8284 struct input_event emacs_event;
8285 int tool_bar_p = 0;
ee78dc32
GV
8286 int button;
8287 int up;
791f420f
JR
8288
8289 emacs_event.kind = no_event;
ee78dc32
GV
8290
8291 if (dpyinfo->grabbed && last_mouse_frame
8292 && FRAME_LIVE_P (last_mouse_frame))
8293 f = last_mouse_frame;
8294 else
8295 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8296
8297 if (f)
8298 {
791f420f
JR
8299 construct_mouse_click (&emacs_event, &msg, f);
8300
8301 /* Is this in the tool-bar? */
8302 if (WINDOWP (f->tool_bar_window)
8303 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
8304 {
8305 Lisp_Object window;
d6ff54d5 8306 int p;
791f420f
JR
8307
8308 /* Set x and y. */
8309 window = window_from_coordinates (f,
8310 emacs_event.x,
8311 emacs_event.y,
8312 &p, 1);
8313 if (EQ (window, f->tool_bar_window))
8314 {
8315 w32_handle_tool_bar_click (f, &emacs_event);
8316 tool_bar_p = 1;
8317 }
8318 }
8319
8320 if (!tool_bar_p)
8321 if (!dpyinfo->w32_focus_frame
8322 || f == dpyinfo->w32_focus_frame
8323 && (numchars >= 1))
8324 {
8325 construct_mouse_click (bufp, &msg, f);
8326 bufp++;
8327 count++;
8328 numchars--;
8329 }
ee78dc32
GV
8330 }
8331
8332 parse_button (msg.msg.message, &button, &up);
8333
8334 if (up)
8335 {
8336 dpyinfo->grabbed &= ~ (1 << button);
8337 }
8338 else
8339 {
8340 dpyinfo->grabbed |= (1 << button);
8341 last_mouse_frame = f;
791f420f
JR
8342 /* Ignore any mouse motion that happened
8343 before this event; any subsequent mouse-movement
8344 Emacs events should reflect only motion after
8345 the ButtonPress. */
8346 if (f != 0)
8347 f->mouse_moved = 0;
8348
8349 if (!tool_bar_p)
8350 last_tool_bar_item = -1;
ee78dc32 8351 }
689004fa 8352 break;
ee78dc32
GV
8353 }
8354
689004fa
GV
8355 case WM_MOUSEWHEEL:
8356 if (dpyinfo->grabbed && last_mouse_frame
8357 && FRAME_LIVE_P (last_mouse_frame))
8358 f = last_mouse_frame;
8359 else
8360 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8361
8362 if (f)
8363 {
8364 if ((!dpyinfo->w32_focus_frame
8365 || f == dpyinfo->w32_focus_frame)
8366 && (numchars >= 1))
8367 {
8368 construct_mouse_wheel (bufp, &msg, f);
8369 bufp++;
8370 count++;
8371 numchars--;
8372 }
8373 }
ee78dc32 8374 break;
689004fa 8375
93ff4395
JR
8376 case WM_MENUSELECT:
8377 {
8378 HMENU menu = (HMENU) msg.msg.lParam;
8379 UINT menu_item = (UINT) LOWORD (msg.msg.wParam);
8380 UINT flags = (UINT) HIWORD (msg.msg.wParam);
9ef2e2cf 8381
93ff4395
JR
8382 w32_menu_display_help (menu, menu_item, flags);
8383 }
8384 break;
8385
12857dfd
RS
8386 case WM_DROPFILES:
8387 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8388
8389 if (f)
8390 {
8391 construct_drag_n_drop (bufp, &msg, f);
8392 bufp++;
8393 count++;
8394 numchars--;
8395 }
8396 break;
8397
ee78dc32
GV
8398 case WM_VSCROLL:
8399 {
689004fa
GV
8400 struct scroll_bar *bar =
8401 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
ee78dc32
GV
8402
8403 if (bar && numchars >= 1)
8404 {
c2cc16fa 8405 if (w32_scroll_bar_handle_click (bar, &msg, bufp))
52cf03a1
GV
8406 {
8407 bufp++;
8408 count++;
8409 numchars--;
8410 }
ee78dc32 8411 }
689004fa 8412 break;
ee78dc32
GV
8413 }
8414
689004fa
GV
8415 case WM_WINDOWPOSCHANGED:
8416 case WM_ACTIVATE:
8417 case WM_ACTIVATEAPP:
8418 check_visibility = 1;
ee78dc32 8419 break;
689004fa 8420
ee78dc32
GV
8421 case WM_MOVE:
8422 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8423
8424 if (f && !f->async_iconified)
8425 {
689004fa
GV
8426 int x, y;
8427
8428 x_real_positions (f, &x, &y);
8429 f->output_data.w32->left_pos = x;
8430 f->output_data.w32->top_pos = y;
ee78dc32 8431 }
689004fa
GV
8432
8433 check_visibility = 1;
8434 break;
8435
8436 case WM_SHOWWINDOW:
8437 /* If window has been obscured or exposed by another window
8438 being maximised or minimised/restored, then recheck
8439 visibility of all frames. Direct changes to our own
8440 windows get handled by WM_SIZE. */
8441#if 0
8442 if (msg.msg.lParam != 0)
8443 check_visibility = 1;
8444 else
8445 {
8446 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8447 f->async_visible = msg.msg.wParam;
8448 }
8449#endif
8450
8451 check_visibility = 1;
ee78dc32 8452 break;
689004fa 8453
ee78dc32
GV
8454 case WM_SIZE:
8455 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8456
689004fa
GV
8457 /* Inform lisp of whether frame has been iconified etc. */
8458 if (f)
ee78dc32 8459 {
689004fa 8460 switch (msg.msg.wParam)
ee78dc32 8461 {
689004fa
GV
8462 case SIZE_MINIMIZED:
8463 f->async_visible = 0;
ee78dc32
GV
8464 f->async_iconified = 1;
8465
8466 bufp->kind = iconify_event;
8467 XSETFRAME (bufp->frame_or_window, f);
7e889510 8468 bufp->arg = Qnil;
ee78dc32
GV
8469 bufp++;
8470 count++;
8471 numchars--;
689004fa
GV
8472 break;
8473
8474 case SIZE_MAXIMIZED:
8475 case SIZE_RESTORED:
ee78dc32
GV
8476 f->async_visible = 1;
8477 f->async_iconified = 0;
8478
8479 /* wait_reading_process_input will notice this and update
8480 the frame's display structures. */
8481 SET_FRAME_GARBAGED (f);
8482
8483 if (f->iconified)
8484 {
31d4844a
KH
8485 int x, y;
8486
8487 /* Reset top and left positions of the Window
8488 here since Windows sends a WM_MOVE message
8489 BEFORE telling us the Window is minimized
8490 when the Window is iconified, with 3000,3000
8491 as the co-ords. */
8492 x_real_positions (f, &x, &y);
8493 f->output_data.w32->left_pos = x;
8494 f->output_data.w32->top_pos = y;
8495
ee78dc32
GV
8496 bufp->kind = deiconify_event;
8497 XSETFRAME (bufp->frame_or_window, f);
7e889510 8498 bufp->arg = Qnil;
ee78dc32
GV
8499 bufp++;
8500 count++;
8501 numchars--;
8502 }
9ef2e2cf
JR
8503 else if (! NILP (Vframe_list)
8504 && ! NILP (XCDR (Vframe_list)))
ee78dc32
GV
8505 /* Force a redisplay sooner or later
8506 to update the frame titles
8507 in case this is the second frame. */
8508 record_asynch_buffer_change ();
689004fa 8509 break;
ee78dc32 8510 }
ee78dc32 8511 }
689004fa 8512
ef0e360f
GV
8513 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
8514 {
8515 RECT rect;
8516 int rows;
8517 int columns;
8518 int width;
8519 int height;
8520
9127e20e 8521 GetClientRect (msg.msg.hwnd, &rect);
ef0e360f
GV
8522
8523 height = rect.bottom - rect.top;
8524 width = rect.right - rect.left;
8525
8526 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
8527 columns = PIXEL_TO_CHAR_WIDTH (f, width);
8528
8529 /* TODO: Clip size to the screen dimensions. */
8530
8531 /* Even if the number of character rows and columns has
8532 not changed, the font size may have changed, so we need
8533 to check the pixel dimensions as well. */
8534
8535 if (columns != f->width
8536 || rows != f->height
8537 || width != f->output_data.w32->pixel_width
8538 || height != f->output_data.w32->pixel_height)
8539 {
791f420f 8540 change_frame_size (f, rows, columns, 0, 1, 0);
ef0e360f 8541 SET_FRAME_GARBAGED (f);
31d4844a 8542 cancel_mouse_face (f);
ef0e360f
GV
8543 f->output_data.w32->pixel_width = width;
8544 f->output_data.w32->pixel_height = height;
8545 f->output_data.w32->win_gravity = NorthWestGravity;
8546 }
8547 }
8548
689004fa
GV
8549 check_visibility = 1;
8550 break;
8551
8552 case WM_SETFOCUS:
791f420f
JR
8553 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
8554
8555 dpyinfo->w32_focus_event_frame = f;
8556
8557 if (f)
8558 x_new_focus_frame (dpyinfo, f);
8559
8560
8561 dpyinfo->grabbed = 0;
8562 check_visibility = 1;
8563 break;
8564
689004fa 8565 case WM_KILLFOCUS:
01b220b6 8566 /* TODO: some of this belongs in MOUSE_LEAVE */
791f420f 8567 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
08712a41 8568
791f420f
JR
8569 if (f)
8570 {
791f420f
JR
8571 if (f == dpyinfo->w32_focus_event_frame)
8572 dpyinfo->w32_focus_event_frame = 0;
8573
8574 if (f == dpyinfo->w32_focus_frame)
8575 x_new_focus_frame (dpyinfo, 0);
8576
8577 if (f == dpyinfo->mouse_face_mouse_frame)
8578 {
8579 /* If we move outside the frame, then we're
8580 certainly no longer on any text in the frame. */
8581 clear_mouse_face (dpyinfo);
8582 dpyinfo->mouse_face_mouse_frame = 0;
8583 }
9ef2e2cf 8584
791f420f
JR
8585 /* Generate a nil HELP_EVENT to cancel a help-echo.
8586 Do it only if there's something to cancel.
8587 Otherwise, the startup message is cleared when
8588 the mouse leaves the frame. */
8589 if (any_help_event_p)
8590 {
c2cc16fa 8591 Lisp_Object frame;
ec48c3a7
JR
8592 int n;
8593
791f420f 8594 XSETFRAME (frame, f);
c2cc16fa
JR
8595 help_echo = Qnil;
8596 n = gen_help_event (bufp, numchars,
8597 Qnil, frame, Qnil, Qnil, 0);
ec48c3a7 8598 bufp += n, count += n, numchars -=n;
791f420f
JR
8599 }
8600 }
689004fa 8601
08712a41 8602 dpyinfo->grabbed = 0;
689004fa 8603 check_visibility = 1;
ee78dc32 8604 break;
689004fa 8605
ee78dc32
GV
8606 case WM_CLOSE:
8607 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8608
8609 if (f)
8610 {
8611 if (numchars == 0)
8612 abort ();
8613
8614 bufp->kind = delete_window_event;
8615 XSETFRAME (bufp->frame_or_window, f);
7e889510 8616 bufp->arg = Qnil;
ee78dc32
GV
8617 bufp++;
8618 count++;
8619 numchars--;
8620 }
689004fa
GV
8621 break;
8622
8623 case WM_INITMENU:
8624 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8625
8626 if (f)
8627 {
8628 if (numchars == 0)
8629 abort ();
ee78dc32 8630
689004fa
GV
8631 bufp->kind = menu_bar_activate_event;
8632 XSETFRAME (bufp->frame_or_window, f);
7e889510 8633 bufp->arg = Qnil;
689004fa
GV
8634 bufp++;
8635 count++;
8636 numchars--;
8637 }
ee78dc32 8638 break;
689004fa 8639
ee78dc32
GV
8640 case WM_COMMAND:
8641 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
014b6ea1 8642
ee78dc32
GV
8643 if (f)
8644 {
f98169a0
GV
8645 extern void menubar_selection_callback
8646 (FRAME_PTR f, void * client_data);
014b6ea1 8647 menubar_selection_callback (f, (void *)msg.msg.wParam);
ee78dc32 8648 }
689004fa
GV
8649
8650 check_visibility = 1;
8651 break;
8652
8653 case WM_DISPLAYCHANGE:
8654 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8655
8656 if (f)
8657 {
8658 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
8659 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
8660 dpyinfo->n_cbits = msg.msg.wParam;
8661 DebPrint (("display change: %d %d\n", dpyinfo->width,
8662 dpyinfo->height));
8663 }
8664
8665 check_visibility = 1;
ee78dc32 8666 break;
e7efd97e
GV
8667
8668 default:
8669 /* Check for messages registered at runtime. */
8670 if (msg.msg.message == msh_mousewheel)
8671 {
8672 if (dpyinfo->grabbed && last_mouse_frame
8673 && FRAME_LIVE_P (last_mouse_frame))
8674 f = last_mouse_frame;
8675 else
8676 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8677
8678 if (f)
8679 {
8680 if ((!dpyinfo->w32_focus_frame
8681 || f == dpyinfo->w32_focus_frame)
8682 && (numchars >= 1))
8683 {
8684 construct_mouse_wheel (bufp, &msg, f);
8685 bufp++;
8686 count++;
8687 numchars--;
8688 }
8689 }
8690 }
8691 break;
ee78dc32
GV
8692 }
8693 }
8694
8695 /* If the focus was just given to an autoraising frame,
8696 raise it now. */
8697 /* ??? This ought to be able to handle more than one such frame. */
8698 if (pending_autoraise_frame)
8699 {
8700 x_raise_frame (pending_autoraise_frame);
8701 pending_autoraise_frame = 0;
8702 }
8703
689004fa
GV
8704 /* Check which frames are still visisble, if we have enqueued any user
8705 events or been notified of events that may affect visibility. We
8706 do this here because there doesn't seem to be any direct
8707 notification from Windows that the visibility of a window has
8708 changed (at least, not in all cases). */
8709 if (count > 0 || check_visibility)
8710 {
8711 Lisp_Object tail, frame;
8712
8713 FOR_EACH_FRAME (tail, frame)
8714 {
8715 FRAME_PTR f = XFRAME (frame);
8716 /* Check "visible" frames and mark each as obscured or not.
8717 Note that async_visible is nonzero for unobscured and
8718 obscured frames, but zero for hidden and iconified frames. */
8719 if (FRAME_W32_P (f) && f->async_visible)
8720 {
8721 RECT clipbox;
8722 HDC hdc = get_frame_dc (f);
8723 GetClipBox (hdc, &clipbox);
8724 release_frame_dc (f, hdc);
8725
8726 if (clipbox.right == clipbox.left
8727 || clipbox.bottom == clipbox.top)
8728 {
8729 /* Frame has become completely obscured so mark as
8730 such (we do this by setting async_visible to 2 so
8731 that FRAME_VISIBLE_P is still true, but redisplay
8732 will skip it). */
8733 f->async_visible = 2;
8734
8735 if (!FRAME_OBSCURED_P (f))
8736 {
8737 DebPrint (("frame %04x (%s) obscured\n", f,
8738 XSTRING (f->name)->data));
8739 }
8740 }
8741 else
8742 {
8743 /* Frame is not obscured, so mark it as such. */
8744 f->async_visible = 1;
8745
8746 if (FRAME_OBSCURED_P (f))
8747 {
8748 SET_FRAME_GARBAGED (f);
8749 DebPrint (("frame %04x (%s) reexposed\n", f,
8750 XSTRING (f->name)->data));
8751
8752 /* Force a redisplay sooner or later. */
8753 record_asynch_buffer_change ();
8754 }
8755 }
8756 }
8757 }
8758 }
8759
ee78dc32
GV
8760 UNBLOCK_INPUT;
8761 return count;
8762}
791f420f 8763
9ef2e2cf
JR
8764
8765
ee78dc32 8766\f
791f420f
JR
8767/***********************************************************************
8768 Text Cursor
8769 ***********************************************************************/
8770
8771/* Note if the text cursor of window W has been overwritten by a
8772 drawing operation that outputs N glyphs starting at HPOS in the
8773 line given by output_cursor.vpos. N < 0 means all the rest of the
8774 line after HPOS has been written. */
8775
8776static void
8777note_overwritten_text_cursor (w, hpos, n)
8778 struct window *w;
8779 int hpos, n;
8780{
8781 if (updated_area == TEXT_AREA
8782 && output_cursor.vpos == w->phys_cursor.vpos
8783 && hpos <= w->phys_cursor.hpos
8784 && (n < 0
8785 || hpos + n > w->phys_cursor.hpos))
8786 w->phys_cursor_on_p = 0;
8787}
8788
ee78dc32 8789
791f420f
JR
8790/* Set clipping for output in glyph row ROW. W is the window in which
8791 we operate. GC is the graphics context to set clipping in.
8792 WHOLE_LINE_P non-zero means include the areas used for truncation
8793 mark display and alike in the clipping rectangle.
ee78dc32 8794
791f420f
JR
8795 ROW may be a text row or, e.g., a mode line. Text rows must be
8796 clipped to the interior of the window dedicated to text display,
8797 mode lines must be clipped to the whole window. */
ee78dc32
GV
8798
8799static void
791f420f
JR
8800w32_clip_to_row (w, row, hdc, whole_line_p)
8801 struct window *w;
8802 struct glyph_row *row;
8803 HDC hdc;
8804 int whole_line_p;
ee78dc32 8805{
791f420f
JR
8806 struct frame *f = XFRAME (WINDOW_FRAME (w));
8807 RECT clip_rect;
8808 int window_x, window_y, window_width, window_height;
52cf03a1 8809
791f420f 8810 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
52cf03a1 8811
791f420f
JR
8812 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
8813 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8814 clip_rect.top = max (clip_rect.top, window_y);
8815 clip_rect.right = clip_rect.left + window_width;
8816 clip_rect.bottom = clip_rect.top + row->visible_height;
8817
8818 /* If clipping to the whole line, including trunc marks, extend
9ef2e2cf 8819 the rectangle to the left and increase its width. */
791f420f
JR
8820 if (whole_line_p)
8821 {
8822 clip_rect.left -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
8823 clip_rect.right += FRAME_X_FLAGS_AREA_WIDTH (f);
8824 }
8825
8826 w32_set_clip_rectangle (hdc, &clip_rect);
ee78dc32
GV
8827}
8828
791f420f
JR
8829
8830/* Draw a hollow box cursor on window W in glyph row ROW. */
ee78dc32
GV
8831
8832static void
791f420f
JR
8833x_draw_hollow_cursor (w, row)
8834 struct window *w;
8835 struct glyph_row *row;
ee78dc32 8836{
791f420f 8837 struct frame *f = XFRAME (WINDOW_FRAME (w));
988646fc 8838 HDC hdc;
791f420f
JR
8839 RECT rect;
8840 int wd;
8841 struct glyph *cursor_glyph;
8842 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
8843
8844 /* Compute frame-relative coordinates from window-relative
8845 coordinates. */
8846 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8847 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
8848 + row->ascent - w->phys_cursor_ascent);
8849 rect.bottom = rect.top + row->height - 1;
8850
8851 /* Get the glyph the cursor is on. If we can't tell because
8852 the current matrix is invalid or such, give up. */
8853 cursor_glyph = get_phys_cursor_glyph (w);
8854 if (cursor_glyph == NULL)
ee78dc32
GV
8855 return;
8856
791f420f
JR
8857 /* Compute the width of the rectangle to draw. If on a stretch
8858 glyph, and `x-stretch-block-cursor' is nil, don't draw a
8859 rectangle as wide as the glyph, but use a canonical character
8860 width instead. */
8861 wd = cursor_glyph->pixel_width - 1;
8862 if (cursor_glyph->type == STRETCH_GLYPH
8863 && !x_stretch_cursor_p)
8864 wd = min (CANON_X_UNIT (f), wd);
8865
8866 rect.right = rect.left + wd;
988646fc 8867 hdc = get_frame_dc (f);
791f420f
JR
8868 FrameRect (hdc, &rect, hb);
8869 DeleteObject (hb);
8870
8871 release_frame_dc (f, hdc);
ee78dc32
GV
8872}
8873
791f420f
JR
8874
8875/* Draw a bar cursor on window W in glyph row ROW.
8876
8877 Implementation note: One would like to draw a bar cursor with an
8878 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8879 Unfortunately, I didn't find a font yet that has this property set.
8880 --gerd. */
ee78dc32
GV
8881
8882static void
9ef2e2cf 8883x_draw_bar_cursor (w, row, width)
791f420f
JR
8884 struct window *w;
8885 struct glyph_row *row;
9ef2e2cf 8886 int width;
ee78dc32 8887{
c2cc16fa
JR
8888 struct frame *f = XFRAME (w->frame);
8889 struct glyph *cursor_glyph;
8890 int x;
8891 HDC hdc;
791f420f 8892
c2cc16fa
JR
8893 /* If cursor is out of bounds, don't draw garbage. This can happen
8894 in mini-buffer windows when switching between echo area glyphs
8895 and mini-buffer. */
8896 cursor_glyph = get_phys_cursor_glyph (w);
8897 if (cursor_glyph == NULL)
8898 return;
9ef2e2cf 8899
c2cc16fa
JR
8900 /* If on an image, draw like a normal cursor. That's usually better
8901 visible than drawing a bar, esp. if the image is large so that
8902 the bar might not be in the window. */
8903 if (cursor_glyph->type == IMAGE_GLYPH)
8904 {
8905 struct glyph_row *row;
8906 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8907 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
8908 }
8909 else
8910 {
8911 if (width < 0)
8912 width = f->output_data.w32->cursor_width;
158cba56 8913
c2cc16fa
JR
8914 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8915 hdc = get_frame_dc (f);
8916 w32_clip_to_row (w, row, hdc, 0);
8917 w32_fill_area (f, hdc, f->output_data.w32->cursor_pixel,
8918 x,
8919 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8920 min (cursor_glyph->pixel_width, width),
8921 row->height);
8922 release_frame_dc (f, hdc);
791f420f 8923 }
ee78dc32
GV
8924}
8925
791f420f
JR
8926
8927/* Clear the cursor of window W to background color, and mark the
8928 cursor as not shown. This is used when the text where the cursor
8929 is is about to be rewritten. */
8930
ee78dc32 8931static void
791f420f
JR
8932x_clear_cursor (w)
8933 struct window *w;
ee78dc32 8934{
791f420f
JR
8935 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
8936 x_update_window_cursor (w, 0);
8937}
ee78dc32 8938
ee78dc32 8939
791f420f
JR
8940/* Draw the cursor glyph of window W in glyph row ROW. See the
8941 comment of x_draw_glyphs for the meaning of HL. */
ee78dc32 8942
791f420f
JR
8943static void
8944x_draw_phys_cursor_glyph (w, row, hl)
8945 struct window *w;
8946 struct glyph_row *row;
8947 enum draw_glyphs_face hl;
8948{
8949 /* If cursor hpos is out of bounds, don't draw garbage. This can
8950 happen in mini-buffer windows when switching between echo area
8951 glyphs and mini-buffer. */
8952 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
ee78dc32 8953 {
791f420f
JR
8954 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
8955 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
8956 hl, 0, 0, 0);
8957
8958 /* When we erase the cursor, and ROW is overlapped by other
8959 rows, make sure that these overlapping parts of other rows
8960 are redrawn. */
8961 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
8962 {
8963 if (row > w->current_matrix->rows
8964 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
8965 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
8966
8967 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
8968 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
8969 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
8970 }
ee78dc32 8971 }
791f420f 8972}
ee78dc32 8973
791f420f
JR
8974
8975/* Erase the image of a cursor of window W from the screen. */
8976
8977static void
8978x_erase_phys_cursor (w)
8979 struct window *w;
8980{
8981 struct frame *f = XFRAME (w->frame);
8982 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8983 int hpos = w->phys_cursor.hpos;
8984 int vpos = w->phys_cursor.vpos;
8985 int mouse_face_here_p = 0;
8986 struct glyph_matrix *active_glyphs = w->current_matrix;
8987 struct glyph_row *cursor_row;
8988 struct glyph *cursor_glyph;
8989 enum draw_glyphs_face hl;
8990
8991 /* No cursor displayed or row invalidated => nothing to do on the
8992 screen. */
8993 if (w->phys_cursor_type == NO_CURSOR)
8994 goto mark_cursor_off;
8995
8996 /* VPOS >= active_glyphs->nrows means that window has been resized.
8997 Don't bother to erase the cursor. */
8998 if (vpos >= active_glyphs->nrows)
8999 goto mark_cursor_off;
9000
9001 /* If row containing cursor is marked invalid, there is nothing we
9002 can do. */
9003 cursor_row = MATRIX_ROW (active_glyphs, vpos);
9004 if (!cursor_row->enabled_p)
9005 goto mark_cursor_off;
9006
9007 /* This can happen when the new row is shorter than the old one.
9008 In this case, either x_draw_glyphs or clear_end_of_line
9009 should have cleared the cursor. Note that we wouldn't be
9010 able to erase the cursor in this case because we don't have a
9011 cursor glyph at hand. */
9012 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
9013 goto mark_cursor_off;
9014
9015 /* If the cursor is in the mouse face area, redisplay that when
9016 we clear the cursor. */
c2cc16fa
JR
9017 if (! NILP (dpyinfo->mouse_face_window)
9018 && w == XWINDOW (dpyinfo->mouse_face_window)
791f420f
JR
9019 && (vpos > dpyinfo->mouse_face_beg_row
9020 || (vpos == dpyinfo->mouse_face_beg_row
9021 && hpos >= dpyinfo->mouse_face_beg_col))
9022 && (vpos < dpyinfo->mouse_face_end_row
9023 || (vpos == dpyinfo->mouse_face_end_row
9024 && hpos < dpyinfo->mouse_face_end_col))
9025 /* Don't redraw the cursor's spot in mouse face if it is at the
9026 end of a line (on a newline). The cursor appears there, but
9027 mouse highlighting does not. */
9028 && cursor_row->used[TEXT_AREA] > hpos)
9029 mouse_face_here_p = 1;
9030
9031 /* Maybe clear the display under the cursor. */
9032 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
ee78dc32 9033 {
791f420f
JR
9034 int x;
9035 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
00fe468b 9036 HDC hdc;
ee78dc32 9037
791f420f
JR
9038 cursor_glyph = get_phys_cursor_glyph (w);
9039 if (cursor_glyph == NULL)
9040 goto mark_cursor_off;
ee78dc32 9041
791f420f 9042 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
00fe468b
JR
9043
9044 hdc = get_frame_dc (f);
9045 w32_clear_area (f, hdc, x,
791f420f
JR
9046 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
9047 cursor_row->y)),
9048 cursor_glyph->pixel_width,
9049 cursor_row->visible_height);
00fe468b 9050 release_frame_dc (f, hdc);
ee78dc32 9051 }
791f420f
JR
9052
9053 /* Erase the cursor by redrawing the character underneath it. */
9054 if (mouse_face_here_p)
9055 hl = DRAW_MOUSE_FACE;
9056 else if (cursor_row->inverse_p)
9057 hl = DRAW_INVERSE_VIDEO;
9058 else
9059 hl = DRAW_NORMAL_TEXT;
9060 x_draw_phys_cursor_glyph (w, cursor_row, hl);
9061
9062 mark_cursor_off:
9063 w->phys_cursor_on_p = 0;
9064 w->phys_cursor_type = NO_CURSOR;
ee78dc32
GV
9065}
9066
9067
791f420f
JR
9068/* Display or clear cursor of window W. If ON is zero, clear the
9069 cursor. If it is non-zero, display the cursor. If ON is nonzero,
9070 where to put the cursor is specified by HPOS, VPOS, X and Y. */
ee78dc32 9071
791f420f
JR
9072void
9073x_display_and_set_cursor (w, on, hpos, vpos, x, y)
9074 struct window *w;
9075 int on, hpos, vpos, x, y;
ee78dc32 9076{
791f420f
JR
9077 struct frame *f = XFRAME (w->frame);
9078 int new_cursor_type;
9ef2e2cf 9079 int new_cursor_width;
791f420f
JR
9080 struct glyph_matrix *current_glyphs;
9081 struct glyph_row *glyph_row;
9082 struct glyph *glyph;
ee78dc32
GV
9083
9084 /* This is pointless on invisible frames, and dangerous on garbaged
791f420f
JR
9085 windows and frames; in the latter case, the frame or window may
9086 be in the midst of changing its size, and x and y may be off the
9087 window. */
9088 if (! FRAME_VISIBLE_P (f)
9089 || FRAME_GARBAGED_P (f)
9090 || vpos >= w->current_matrix->nrows
9091 || hpos >= w->current_matrix->matrix_w)
ee78dc32
GV
9092 return;
9093
9094 /* If cursor is off and we want it off, return quickly. */
791f420f 9095 if (!on && !w->phys_cursor_on_p)
ee78dc32
GV
9096 return;
9097
791f420f
JR
9098 current_glyphs = w->current_matrix;
9099 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9100 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9101
9102 /* If cursor row is not enabled, we don't really know where to
9103 display the cursor. */
9104 if (!glyph_row->enabled_p)
9105 {
9106 w->phys_cursor_on_p = 0;
9107 return;
9108 }
9109
9110 xassert (interrupt_input_blocked);
9111
9112 /* Set new_cursor_type to the cursor we want to be displayed. In a
9113 mini-buffer window, we want the cursor only to appear if we are
9114 reading input from this window. For the selected window, we want
9115 the cursor type given by the frame parameter. If explicitly
9116 marked off, draw no cursor. In all other cases, we want a hollow
9117 box cursor. */
9ef2e2cf 9118 new_cursor_width = -1;
791f420f
JR
9119 if (cursor_in_echo_area
9120 && FRAME_HAS_MINIBUF_P (f)
9121 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
9122 {
9123 if (w == XWINDOW (echo_area_window))
9124 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9125 else
9126 new_cursor_type = HOLLOW_BOX_CURSOR;
9127 }
9128 else
9129 {
49be9f70
JR
9130 if (f != FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame
9131 || w != XWINDOW (f->selected_window))
791f420f 9132 {
a2bc11d4
JR
9133 extern int cursor_in_non_selected_windows;
9134
9127e20e
JR
9135 if (MINI_WINDOW_P (w)
9136 || !cursor_in_non_selected_windows
9137 || NILP (XBUFFER (w->buffer)->cursor_type))
791f420f
JR
9138 new_cursor_type = NO_CURSOR;
9139 else
9140 new_cursor_type = HOLLOW_BOX_CURSOR;
9141 }
9142 else if (w->cursor_off_p)
9143 new_cursor_type = NO_CURSOR;
9144 else
9ef2e2cf
JR
9145 {
9146 struct buffer *b = XBUFFER (w->buffer);
9147
9148 if (EQ (b->cursor_type, Qt))
9149 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9150 else
9151 new_cursor_type = x_specified_cursor_type (b->cursor_type,
9152 &new_cursor_width);
9153 }
791f420f
JR
9154 }
9155
9156 /* If cursor is currently being shown and we don't want it to be or
9157 it is in the wrong place, or the cursor type is not what we want,
ee78dc32 9158 erase it. */
791f420f 9159 if (w->phys_cursor_on_p
ee78dc32 9160 && (!on
791f420f
JR
9161 || w->phys_cursor.x != x
9162 || w->phys_cursor.y != y
9163 || new_cursor_type != w->phys_cursor_type))
9164 x_erase_phys_cursor (w);
9165
9166 /* If the cursor is now invisible and we want it to be visible,
9167 display it. */
9168 if (on && !w->phys_cursor_on_p)
9169 {
9170 w->phys_cursor_ascent = glyph_row->ascent;
9171 w->phys_cursor_height = glyph_row->height;
9172
9173 /* Set phys_cursor_.* before x_draw_.* is called because some
9174 of them may need the information. */
9175 w->phys_cursor.x = x;
9176 w->phys_cursor.y = glyph_row->y;
9177 w->phys_cursor.hpos = hpos;
9178 w->phys_cursor.vpos = vpos;
9179 w->phys_cursor_type = new_cursor_type;
9180 w->phys_cursor_on_p = 1;
9181
9182 switch (new_cursor_type)
ee78dc32 9183 {
791f420f
JR
9184 case HOLLOW_BOX_CURSOR:
9185 x_draw_hollow_cursor (w, glyph_row);
9186 break;
9187
9188 case FILLED_BOX_CURSOR:
9189 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9190 break;
9191
9192 case BAR_CURSOR:
9ef2e2cf 9193 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
791f420f 9194 break;
ee78dc32 9195
791f420f
JR
9196 case NO_CURSOR:
9197 break;
9198
9199 default:
9200 abort ();
9201 }
ee78dc32
GV
9202 }
9203}
9204
689004fa 9205
791f420f
JR
9206/* Display the cursor on window W, or clear it. X and Y are window
9207 relative pixel coordinates. HPOS and VPOS are glyph matrix
9208 positions. If W is not the selected window, display a hollow
9209 cursor. ON non-zero means display the cursor at X, Y which
9210 correspond to HPOS, VPOS, otherwise it is cleared. */
9211
9212void
9213x_display_cursor (w, on, hpos, vpos, x, y)
9214 struct window *w;
9215 int on, hpos, vpos, x, y;
ee78dc32
GV
9216{
9217 BLOCK_INPUT;
791f420f
JR
9218 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9219 UNBLOCK_INPUT;
9220}
9221
9222
9223/* Display the cursor on window W, or clear it, according to ON_P.
9224 Don't change the cursor's position. */
9225
9226void
9227x_update_cursor (f, on_p)
9228 struct frame *f;
9229 int on_p;
9230{
9231 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9232}
9233
9234
9235/* Call x_update_window_cursor with parameter ON_P on all leaf windows
9236 in the window tree rooted at W. */
ee78dc32 9237
791f420f
JR
9238static void
9239x_update_cursor_in_window_tree (w, on_p)
9240 struct window *w;
9241 int on_p;
9242{
9243 while (w)
689004fa 9244 {
791f420f
JR
9245 if (!NILP (w->hchild))
9246 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9247 else if (!NILP (w->vchild))
9248 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9249 else
9250 x_update_window_cursor (w, on_p);
9251
9252 w = NILP (w->next) ? 0 : XWINDOW (w->next);
689004fa 9253 }
791f420f 9254}
689004fa 9255
ee78dc32 9256
791f420f
JR
9257/* Switch the display of W's cursor on or off, according to the value
9258 of ON. */
9259
9260static void
9261x_update_window_cursor (w, on)
9262 struct window *w;
9263 int on;
9264{
9ef2e2cf
JR
9265 /* Don't update cursor in windows whose frame is in the process
9266 of being deleted. */
9267 if (w->current_matrix)
9268 {
9269 BLOCK_INPUT;
9270 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9271 w->phys_cursor.vpos, w->phys_cursor.x,
9272 w->phys_cursor.y);
9273 UNBLOCK_INPUT;
9274 }
ee78dc32 9275}
791f420f
JR
9276
9277
9ef2e2cf 9278
ee78dc32 9279\f
7f5d1df8
GV
9280/* Icons. */
9281
9282int
9283x_bitmap_icon (f, icon)
9284 struct frame *f;
9285 Lisp_Object icon;
9286{
7f5d1df8
GV
9287 HANDLE hicon;
9288
9289 if (FRAME_W32_WINDOW (f) == 0)
9290 return 1;
9291
9292 if (NILP (icon))
9293 hicon = LoadIcon (hinst, EMACS_CLASS);
9294 else if (STRINGP (icon))
9295 hicon = LoadImage (NULL, (LPCTSTR) XSTRING (icon)->data, IMAGE_ICON, 0, 0,
9296 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9297 else if (SYMBOLP (icon))
9298 {
9299 LPCTSTR name;
9300
9301 if (EQ (icon, intern ("application")))
9302 name = (LPCTSTR) IDI_APPLICATION;
9303 else if (EQ (icon, intern ("hand")))
9304 name = (LPCTSTR) IDI_HAND;
9305 else if (EQ (icon, intern ("question")))
9306 name = (LPCTSTR) IDI_QUESTION;
9307 else if (EQ (icon, intern ("exclamation")))
9308 name = (LPCTSTR) IDI_EXCLAMATION;
9309 else if (EQ (icon, intern ("asterisk")))
9310 name = (LPCTSTR) IDI_ASTERISK;
9311 else if (EQ (icon, intern ("winlogo")))
9312 name = (LPCTSTR) IDI_WINLOGO;
9313 else
9314 return 1;
9315
9316 hicon = LoadIcon (NULL, name);
9317 }
9318 else
9319 return 1;
9320
9321 if (hicon == NULL)
9322 return 1;
9323
791f420f
JR
9324 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
9325 (LPARAM) hicon);
7f5d1df8
GV
9326
9327 return 0;
9328}
9329
9330\f
c2cc16fa
JR
9331/************************************************************************
9332 Handling X errors
9333 ************************************************************************/
9334
9335/* Display Error Handling functions not used on W32. Listing them here
9336 helps diff stay in step when comparing w32term.c with xterm.c.
9337
9338x_error_catcher (display, error)
9339x_catch_errors (dpy)
9340x_catch_errors_unwind (old_val)
9341x_check_errors (dpy, format)
9342x_had_errors_p (dpy)
9343x_clear_errors (dpy)
9344x_uncatch_errors (dpy, count)
9345x_trace_wire ()
9346x_connection_signal (signalnum)
9347x_connection_closed (dpy, error_message)
9348x_error_quitter (display, error)
9349x_error_handler (display, error)
9350x_io_error_quitter (display)
9351
9352 */
9353
9354\f
ee78dc32
GV
9355/* Changing the font of the frame. */
9356
9357/* Give frame F the font named FONTNAME as its default font, and
9358 return the full name of that font. FONTNAME may be a wildcard
9359 pattern; in that case, we choose some font that fits the pattern.
9360 The return value shows which font we chose. */
9361
9362Lisp_Object
9363x_new_font (f, fontname)
9364 struct frame *f;
9365 register char *fontname;
9366{
cabb23bc 9367 struct font_info *fontp
93ff4395 9368 = FS_LOAD_FONT (f, 0, fontname, -1);
ee78dc32 9369
cabb23bc 9370 if (!fontp)
791f420f 9371 return Qnil;
ee78dc32 9372
cabb23bc 9373 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
791f420f 9374 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
cabb23bc 9375 FRAME_FONTSET (f) = -1;
ee78dc32
GV
9376
9377 /* Compute the scroll bar width in character columns. */
9378 if (f->scroll_bar_pixel_width > 0)
9379 {
ec48c3a7 9380 int wid = FONT_WIDTH (FRAME_FONT (f));
ee78dc32
GV
9381 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
9382 }
9383 else
ec48c3a7
JR
9384 {
9385 int wid = FONT_WIDTH (FRAME_FONT (f));
9386 f->scroll_bar_cols = (14 + wid - 1) / wid;
9387 }
ee78dc32
GV
9388
9389 /* Now make the frame display the given font. */
fbd6baed 9390 if (FRAME_W32_WINDOW (f) != 0)
ee78dc32
GV
9391 {
9392 frame_update_line_height (f);
49be9f70
JR
9393 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9394 x_set_window_size (f, 0, f->width, f->height);
ee78dc32
GV
9395 }
9396 else
9397 /* If we are setting a new frame's font for the first time,
9398 there are no faces yet, so this font's height is the line height. */
ec48c3a7 9399 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
ee78dc32 9400
791f420f 9401 return build_string (fontp->full_name);
ee78dc32
GV
9402}
9403\f
cabb23bc
GV
9404/* Give frame F the fontset named FONTSETNAME as its default font, and
9405 return the full name of that fontset. FONTSETNAME may be a wildcard
9406 pattern; in that case, we choose some fontset that fits the pattern.
9407 The return value shows which fontset we chose. */
9408
9409Lisp_Object
9410x_new_fontset (f, fontsetname)
9411 struct frame *f;
9412 char *fontsetname;
9413{
93ff4395 9414 int fontset = fs_query_fontset (build_string (fontsetname), 0);
cabb23bc
GV
9415 Lisp_Object result;
9416
9417 if (fontset < 0)
9418 return Qnil;
9419
9420 if (FRAME_FONTSET (f) == fontset)
9421 /* This fontset is already set in frame F. There's nothing more
9422 to do. */
93ff4395 9423 return fontset_name (fontset);
cabb23bc 9424
93ff4395 9425 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
cabb23bc
GV
9426
9427 if (!STRINGP (result))
9428 /* Can't load ASCII font. */
9429 return Qnil;
9430
9431 /* Since x_new_font doesn't update any fontset information, do it now. */
9432 FRAME_FONTSET(f) = fontset;
cabb23bc
GV
9433
9434 return build_string (fontsetname);
9435}
791f420f 9436
c2cc16fa
JR
9437\f
9438/***********************************************************************
9439 TODO: W32 Input Methods
9440 ***********************************************************************/
9441/* Listing missing functions from xterm.c helps diff stay in step.
791f420f 9442
c2cc16fa
JR
9443xim_destroy_callback (xim, client_data, call_data)
9444xim_open_dpy (dpyinfo, resource_name)
9445struct xim_inst_t
9446xim_instantiate_callback (display, client_data, call_data)
9447xim_initialize (dpyinfo, resource_name)
9448xim_close_dpy (dpyinfo)
791f420f 9449
c2cc16fa 9450 */
791f420f 9451
cabb23bc 9452\f
689004fa
GV
9453/* Calculate the absolute position in frame F
9454 from its current recorded position values and gravity. */
9455
9ef2e2cf 9456void
ee78dc32
GV
9457x_calc_absolute_position (f)
9458 struct frame *f;
9459{
ee78dc32 9460 POINT pt;
fbd6baed 9461 int flags = f->output_data.w32->size_hint_flags;
ee78dc32
GV
9462
9463 pt.x = pt.y = 0;
9464
9465 /* Find the position of the outside upper-left corner of
c2cc16fa
JR
9466 the inner window, with respect to the outer window.
9467 But do this only if we will need the results. */
fbd6baed 9468 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
ee78dc32
GV
9469 {
9470 BLOCK_INPUT;
fbd6baed
GV
9471 MapWindowPoints (FRAME_W32_WINDOW (f),
9472 f->output_data.w32->parent_desc,
ee78dc32
GV
9473 &pt, 1);
9474 UNBLOCK_INPUT;
9475 }
9476
9477 {
9478 RECT rt;
9479 rt.left = rt.right = rt.top = rt.bottom = 0;
9480
9481 BLOCK_INPUT;
fbd6baed 9482 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
97c23857 9483 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32
GV
9484 UNBLOCK_INPUT;
9485
9486 pt.x += (rt.right - rt.left);
9487 pt.y += (rt.bottom - rt.top);
9488 }
9489
9490 /* Treat negative positions as relative to the leftmost bottommost
9491 position that fits on the screen. */
9492 if (flags & XNegative)
fbd6baed
GV
9493 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
9494 - 2 * f->output_data.w32->border_width - pt.x
ee78dc32 9495 - PIXEL_WIDTH (f)
fbd6baed 9496 + f->output_data.w32->left_pos);
158cba56 9497
ee78dc32 9498 if (flags & YNegative)
fbd6baed
GV
9499 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
9500 - 2 * f->output_data.w32->border_width - pt.y
ee78dc32 9501 - PIXEL_HEIGHT (f)
fbd6baed 9502 + f->output_data.w32->top_pos);
ee78dc32
GV
9503 /* The left_pos and top_pos
9504 are now relative to the top and left screen edges,
9505 so the flags should correspond. */
fbd6baed 9506 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32
GV
9507}
9508
9509/* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9510 to really change the position, and 0 when calling from
9511 x_make_frame_visible (in that case, XOFF and YOFF are the current
9512 position values). It is -1 when calling from x_set_frame_parameters,
9513 which means, do adjust for borders but don't change the gravity. */
9514
9ef2e2cf 9515void
ee78dc32
GV
9516x_set_offset (f, xoff, yoff, change_gravity)
9517 struct frame *f;
9518 register int xoff, yoff;
9519 int change_gravity;
9520{
9521 int modified_top, modified_left;
9522
9523 if (change_gravity > 0)
9524 {
fbd6baed
GV
9525 f->output_data.w32->top_pos = yoff;
9526 f->output_data.w32->left_pos = xoff;
9527 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
ee78dc32 9528 if (xoff < 0)
fbd6baed 9529 f->output_data.w32->size_hint_flags |= XNegative;
ee78dc32 9530 if (yoff < 0)
fbd6baed
GV
9531 f->output_data.w32->size_hint_flags |= YNegative;
9532 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
9533 }
9534 x_calc_absolute_position (f);
9535
9536 BLOCK_INPUT;
9537 x_wm_set_size_hint (f, (long) 0, 0);
9538
fbd6baed
GV
9539 modified_left = f->output_data.w32->left_pos;
9540 modified_top = f->output_data.w32->top_pos;
ee78dc32 9541
fbd6baed 9542 my_set_window_pos (FRAME_W32_WINDOW (f),
52cf03a1
GV
9543 NULL,
9544 modified_left, modified_top,
cabb23bc 9545 0, 0,
689004fa 9546 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
ee78dc32
GV
9547 UNBLOCK_INPUT;
9548}
9549
9550/* Call this to change the size of frame F's x-window.
9551 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9552 for this size change and subsequent size changes.
9553 Otherwise we leave the window gravity unchanged. */
c2cc16fa 9554
791f420f 9555void
ee78dc32
GV
9556x_set_window_size (f, change_gravity, cols, rows)
9557 struct frame *f;
9558 int change_gravity;
9559 int cols, rows;
9560{
9561 int pixelwidth, pixelheight;
9562
9563 BLOCK_INPUT;
9564
9565 check_frame_size (f, &rows, &cols);
fbd6baed 9566 f->output_data.w32->vertical_scroll_bar_extra
ee78dc32
GV
9567 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9568 ? 0
fbd6baed 9569 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
791f420f
JR
9570 f->output_data.w32->flags_areas_extra
9571 = FRAME_FLAGS_AREA_WIDTH (f);
ee78dc32
GV
9572 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
9573 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
9574
fbd6baed 9575 f->output_data.w32->win_gravity = NorthWestGravity;
ee78dc32
GV
9576 x_wm_set_size_hint (f, (long) 0, 0);
9577
9578 {
9579 RECT rect;
9580
9581 rect.left = rect.top = 0;
9582 rect.right = pixelwidth;
9583 rect.bottom = pixelheight;
9584
fbd6baed 9585 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
97c23857 9586 FRAME_EXTERNAL_MENU_BAR (f));
ee78dc32 9587
fbd6baed 9588 my_set_window_pos (FRAME_W32_WINDOW (f),
52cf03a1
GV
9589 NULL,
9590 0, 0,
689004fa
GV
9591 rect.right - rect.left,
9592 rect.bottom - rect.top,
9593 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
ee78dc32
GV
9594 }
9595
9596 /* Now, strictly speaking, we can't be sure that this is accurate,
9597 but the window manager will get around to dealing with the size
9598 change request eventually, and we'll hear how it went when the
9599 ConfigureNotify event gets here.
9600
9601 We could just not bother storing any of this information here,
9602 and let the ConfigureNotify event set everything up, but that
ec48c3a7 9603 might be kind of confusing to the Lisp code, since size changes
ee78dc32 9604 wouldn't be reported in the frame parameters until some random
791f420f
JR
9605 point in the future when the ConfigureNotify event arrives.
9606
9607 We pass 1 for DELAY since we can't run Lisp code inside of
9608 a BLOCK_INPUT. */
9609 change_frame_size (f, rows, cols, 0, 1, 0);
ee78dc32
GV
9610 PIXEL_WIDTH (f) = pixelwidth;
9611 PIXEL_HEIGHT (f) = pixelheight;
9612
689004fa
GV
9613 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9614 receive in the ConfigureNotify event; if we get what we asked
9615 for, then the event won't cause the screen to become garbaged, so
9616 we have to make sure to do it here. */
9617 SET_FRAME_GARBAGED (f);
9618
ee78dc32 9619 /* If cursor was outside the new size, mark it as off. */
791f420f 9620 mark_window_cursors_off (XWINDOW (f->root_window));
ee78dc32 9621
689004fa
GV
9622 /* Clear out any recollection of where the mouse highlighting was,
9623 since it might be in a place that's outside the new frame size.
9624 Actually checking whether it is outside is a pain in the neck,
9625 so don't try--just let the highlighting be done afresh with new size. */
31d4844a
KH
9626 cancel_mouse_face (f);
9627
ee78dc32
GV
9628 UNBLOCK_INPUT;
9629}
9630\f
9631/* Mouse warping. */
9632
ec48c3a7
JR
9633void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
9634
9635void
9636x_set_mouse_position (f, x, y)
9637 struct frame *f;
9638 int x, y;
9639{
9640 int pix_x, pix_y;
9641
9642 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
9643 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
9644
9645 if (pix_x < 0) pix_x = 0;
9646 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
9647
9648 if (pix_y < 0) pix_y = 0;
9649 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
9650
9651 x_set_mouse_pixel_position (f, pix_x, pix_y);
9652}
9653
1bcd16f2
MB
9654void
9655x_set_mouse_pixel_position (f, pix_x, pix_y)
9656 struct frame *f;
9657 int pix_x, pix_y;
9658{
689004fa
GV
9659 RECT rect;
9660 POINT pt;
9661
1bcd16f2
MB
9662 BLOCK_INPUT;
9663
689004fa
GV
9664 GetClientRect (FRAME_W32_WINDOW (f), &rect);
9665 pt.x = rect.left + pix_x;
9666 pt.y = rect.top + pix_y;
9667 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
1bcd16f2 9668
689004fa 9669 SetCursorPos (pt.x, pt.y);
1bcd16f2
MB
9670
9671 UNBLOCK_INPUT;
9672}
9673
ee78dc32
GV
9674\f
9675/* focus shifting, raising and lowering. */
9676
ec48c3a7 9677void
ee78dc32
GV
9678x_focus_on_frame (f)
9679 struct frame *f;
9680{
689004fa
GV
9681 struct w32_display_info *dpyinfo = &one_w32_display_info;
9682
9683 /* Give input focus to frame. */
9684 BLOCK_INPUT;
9685#if 0
9686 /* Try not to change its Z-order if possible. */
9687 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
9688 my_set_focus (f, FRAME_W32_WINDOW (f));
9689 else
9690#endif
ef0e360f 9691 my_set_foreground_window (FRAME_W32_WINDOW (f));
689004fa 9692 UNBLOCK_INPUT;
ee78dc32
GV
9693}
9694
ec48c3a7 9695void
ee78dc32
GV
9696x_unfocus_frame (f)
9697 struct frame *f;
9698{
9699}
9700
9701/* Raise frame F. */
791f420f 9702void
ee78dc32
GV
9703x_raise_frame (f)
9704 struct frame *f;
9705{
689004fa
GV
9706 BLOCK_INPUT;
9707
9708 /* Strictly speaking, raise-frame should only change the frame's Z
9709 order, leaving input focus unchanged. This is reasonable behaviour
9710 on X where the usual policy is point-to-focus. However, this
9711 behaviour would be very odd on Windows where the usual policy is
9712 click-to-focus.
9713
9714 On X, if the mouse happens to be over the raised frame, it gets
9715 input focus anyway (so the window with focus will never be
9716 completely obscured) - if not, then just moving the mouse over it
9717 is sufficient to give it focus. On Windows, the user must actually
9718 click on the frame (preferrably the title bar so as not to move
9719 point), which is more awkward. Also, no other Windows program
9720 raises a window to the top but leaves another window (possibly now
9721 completely obscured) with input focus.
9722
9723 Because there is a system setting on Windows that allows the user
9724 to choose the point to focus policy, we make the strict semantics
9725 optional, but by default we grab focus when raising. */
9726
9727 if (NILP (Vw32_grab_focus_on_raise))
ee78dc32 9728 {
689004fa
GV
9729 /* The obvious call to my_set_window_pos doesn't work if Emacs is
9730 not already the foreground application: the frame is raised
9731 above all other frames belonging to us, but not above the
9732 current top window. To achieve that, we have to resort to this
9733 more cumbersome method. */
9734
9735 HDWP handle = BeginDeferWindowPos (2);
9736 if (handle)
9737 {
9738 DeferWindowPos (handle,
9739 FRAME_W32_WINDOW (f),
9740 HWND_TOP,
9741 0, 0, 0, 0,
9742 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9743
9744 DeferWindowPos (handle,
9745 GetForegroundWindow (),
9746 FRAME_W32_WINDOW (f),
9747 0, 0, 0, 0,
9748 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9749
9750 EndDeferWindowPos (handle);
9751 }
ee78dc32 9752 }
689004fa
GV
9753 else
9754 {
ef0e360f 9755 my_set_foreground_window (FRAME_W32_WINDOW (f));
689004fa
GV
9756 }
9757
9758 UNBLOCK_INPUT;
ee78dc32
GV
9759}
9760
9761/* Lower frame F. */
791f420f 9762void
ee78dc32
GV
9763x_lower_frame (f)
9764 struct frame *f;
9765{
689004fa
GV
9766 BLOCK_INPUT;
9767 my_set_window_pos (FRAME_W32_WINDOW (f),
9768 HWND_BOTTOM,
9769 0, 0, 0, 0,
9770 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9771 UNBLOCK_INPUT;
ee78dc32
GV
9772}
9773
9774static void
ec48c3a7 9775w32_frame_raise_lower (f, raise_flag)
ee78dc32 9776 FRAME_PTR f;
ec48c3a7 9777 int raise_flag;
ee78dc32 9778{
7e6ac5b9
AI
9779 if (! FRAME_W32_P (f))
9780 return;
9781
ec48c3a7 9782 if (raise_flag)
ee78dc32
GV
9783 x_raise_frame (f);
9784 else
9785 x_lower_frame (f);
9786}
9787\f
9788/* Change of visibility. */
9789
9790/* This tries to wait until the frame is really visible.
9791 However, if the window manager asks the user where to position
9792 the frame, this will return before the user finishes doing that.
9793 The frame will not actually be visible at that time,
9794 but it will become visible later when the window manager
9795 finishes with it. */
9796
ec48c3a7 9797void
ee78dc32
GV
9798x_make_frame_visible (f)
9799 struct frame *f;
9800{
7f5d1df8
GV
9801 Lisp_Object type;
9802
ee78dc32
GV
9803 BLOCK_INPUT;
9804
7f5d1df8
GV
9805 type = x_icon_type (f);
9806 if (!NILP (type))
9807 x_bitmap_icon (f, type);
9808
ee78dc32
GV
9809 if (! FRAME_VISIBLE_P (f))
9810 {
9811 /* We test FRAME_GARBAGED_P here to make sure we don't
9812 call x_set_offset a second time
9813 if we get to x_make_frame_visible a second time
9814 before the window gets really visible. */
9815 if (! FRAME_ICONIFIED_P (f)
fbd6baed 9816 && ! f->output_data.w32->asked_for_visible)
fbd6baed 9817 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
ee78dc32 9818
fbd6baed 9819 f->output_data.w32->asked_for_visible = 1;
52cf03a1 9820
689004fa
GV
9821// my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW);
9822 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
ee78dc32
GV
9823 }
9824
9825 /* Synchronize to ensure Emacs knows the frame is visible
9826 before we do anything else. We do this loop with input not blocked
9827 so that incoming events are handled. */
9828 {
9829 Lisp_Object frame;
791f420f 9830 int count;
ee78dc32
GV
9831
9832 /* This must come after we set COUNT. */
9833 UNBLOCK_INPUT;
9834
9835 XSETFRAME (frame, f);
9836
791f420f
JR
9837 /* Wait until the frame is visible. Process X events until a
9838 MapNotify event has been seen, or until we think we won't get a
9839 MapNotify at all.. */
9840 for (count = input_signal_count + 10;
9841 input_signal_count < count && !FRAME_VISIBLE_P (f);)
ee78dc32 9842 {
791f420f 9843 /* Force processing of queued events. */
01b220b6 9844 /* TODO: x_sync equivalent? */
791f420f 9845
ee78dc32
GV
9846 /* Machines that do polling rather than SIGIO have been observed
9847 to go into a busy-wait here. So we'll fake an alarm signal
9848 to let the handler know that there's something to be read.
9849 We used to raise a real alarm, but it seems that the handler
9850 isn't always enabled here. This is probably a bug. */
9851 if (input_polling_used ())
9852 {
9853 /* It could be confusing if a real alarm arrives while processing
9854 the fake one. Turn it off and let the handler reset it. */
a9b4e0ec
AI
9855 int old_poll_suppress_count = poll_suppress_count;
9856 poll_suppress_count = 1;
9857 poll_for_input_1 ();
9858 poll_suppress_count = old_poll_suppress_count;
ee78dc32 9859 }
ee78dc32
GV
9860 }
9861 FRAME_SAMPLE_VISIBILITY (f);
9862 }
9863}
9864
9865/* Change from mapped state to withdrawn state. */
9866
9867/* Make the frame visible (mapped and not iconified). */
9868
9869x_make_frame_invisible (f)
9870 struct frame *f;
9871{
ee78dc32 9872 /* Don't keep the highlight on an invisible frame. */
fbd6baed
GV
9873 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9874 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
ee78dc32
GV
9875
9876 BLOCK_INPUT;
9877
689004fa 9878 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
ee78dc32
GV
9879
9880 /* We can't distinguish this from iconification
9881 just by the event that we get from the server.
9882 So we can't win using the usual strategy of letting
9883 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9884 and synchronize with the server to make sure we agree. */
9885 f->visible = 0;
9886 FRAME_ICONIFIED_P (f) = 0;
9887 f->async_visible = 0;
9888 f->async_iconified = 0;
9889
9890 UNBLOCK_INPUT;
9891}
9892
9893/* Change window state from mapped to iconified. */
9894
52cf03a1
GV
9895void
9896x_iconify_frame (f)
ee78dc32
GV
9897 struct frame *f;
9898{
7f5d1df8 9899 Lisp_Object type;
ee78dc32
GV
9900
9901 /* Don't keep the highlight on an invisible frame. */
fbd6baed
GV
9902 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9903 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
ee78dc32
GV
9904
9905 if (f->async_iconified)
9906 return;
9907
9908 BLOCK_INPUT;
9909
7f5d1df8
GV
9910 type = x_icon_type (f);
9911 if (!NILP (type))
9912 x_bitmap_icon (f, type);
9913
689004fa 9914 /* Simulate the user minimizing the frame. */
4934bcdd 9915 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
ee78dc32
GV
9916
9917 UNBLOCK_INPUT;
9918}
c2cc16fa 9919
ee78dc32 9920\f
25badd7a 9921/* Free X resources of frame F. */
ee78dc32 9922
25badd7a
AI
9923void
9924x_free_frame_resources (f)
ee78dc32
GV
9925 struct frame *f;
9926{
fbd6baed 9927 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
ee78dc32
GV
9928
9929 BLOCK_INPUT;
9930
25badd7a
AI
9931 if (FRAME_W32_WINDOW (f))
9932 my_destroy_window (f, FRAME_W32_WINDOW (f));
9933
ee78dc32 9934 free_frame_menubar (f);
ee78dc32 9935
25badd7a
AI
9936 unload_color (f, f->output_data.x->foreground_pixel);
9937 unload_color (f, f->output_data.x->background_pixel);
9938 unload_color (f, f->output_data.w32->cursor_pixel);
9939 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
9940 unload_color (f, f->output_data.w32->border_pixel);
9941 unload_color (f, f->output_data.w32->mouse_pixel);
c2cc16fa
JR
9942 if (f->output_data.w32->white_relief.allocated_p)
9943 unload_color (f, f->output_data.w32->white_relief.pixel);
9944 if (f->output_data.w32->black_relief.allocated_p)
9945 unload_color (f, f->output_data.w32->black_relief.pixel);
9946
25badd7a
AI
9947 if (FRAME_FACE_CACHE (f))
9948 free_frame_faces (f);
9949
fbd6baed 9950 xfree (f->output_data.w32);
25badd7a
AI
9951 f->output_data.w32 = NULL;
9952
fbd6baed
GV
9953 if (f == dpyinfo->w32_focus_frame)
9954 dpyinfo->w32_focus_frame = 0;
9955 if (f == dpyinfo->w32_focus_event_frame)
9956 dpyinfo->w32_focus_event_frame = 0;
9957 if (f == dpyinfo->w32_highlight_frame)
9958 dpyinfo->w32_highlight_frame = 0;
ee78dc32 9959
ee78dc32
GV
9960 if (f == dpyinfo->mouse_face_mouse_frame)
9961 {
9962 dpyinfo->mouse_face_beg_row
9963 = dpyinfo->mouse_face_beg_col = -1;
9964 dpyinfo->mouse_face_end_row
9965 = dpyinfo->mouse_face_end_col = -1;
9966 dpyinfo->mouse_face_window = Qnil;
25badd7a
AI
9967 dpyinfo->mouse_face_deferred_gc = 0;
9968 dpyinfo->mouse_face_mouse_frame = 0;
ee78dc32
GV
9969 }
9970
9971 UNBLOCK_INPUT;
9972}
25badd7a
AI
9973
9974
9975/* Destroy the window of frame F. */
9976
9977x_destroy_window (f)
9978 struct frame *f;
9979{
9980 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9981
9982 x_free_frame_resources (f);
9983
9984 dpyinfo->reference_count--;
9985}
c2cc16fa 9986
ee78dc32
GV
9987\f
9988/* Setting window manager hints. */
9989
9990/* Set the normal size hints for the window manager, for frame F.
9991 FLAGS is the flags word to use--or 0 meaning preserve the flags
9992 that the window now has.
9993 If USER_POSITION is nonzero, we set the USPosition
9994 flag (this is useful when FLAGS is 0). */
791f420f 9995void
ee78dc32
GV
9996x_wm_set_size_hint (f, flags, user_position)
9997 struct frame *f;
9998 long flags;
9999 int user_position;
10000{
fbd6baed 10001 Window window = FRAME_W32_WINDOW (f);
ee78dc32 10002
97c23857 10003 enter_crit ();
ee78dc32 10004
689004fa
GV
10005 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
10006 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
10007 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
10008 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
ee78dc32 10009
97c23857 10010 leave_crit ();
ee78dc32
GV
10011}
10012
10013/* Window manager things */
10014x_wm_set_icon_position (f, icon_x, icon_y)
10015 struct frame *f;
10016 int icon_x, icon_y;
10017{
10018#if 0
fbd6baed 10019 Window window = FRAME_W32_WINDOW (f);
ee78dc32
GV
10020
10021 f->display.x->wm_hints.flags |= IconPositionHint;
10022 f->display.x->wm_hints.icon_x = icon_x;
10023 f->display.x->wm_hints.icon_y = icon_y;
10024
10025 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
10026#endif
10027}
10028
c2cc16fa
JR
10029\f
10030/***********************************************************************
10031 Fonts
10032 ***********************************************************************/
10033
10034/* The following functions are listed here to help diff stay in step
10035 with xterm.c. See w32fns.c for definitions.
10036
10037x_get_font_info (f, font_idx)
10038x_list_fonts (f, pattern, size, maxnames)
10039
10040 */
10041
10042#if GLYPH_DEBUG
10043
10044/* Check that FONT is valid on frame F. It is if it can be found in F's
10045 font table. */
10046
10047static void
10048x_check_font (f, font)
10049 struct frame *f;
10050 XFontStruct *font;
10051{
10052 int i;
10053 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10054
10055 xassert (font != NULL);
9ef2e2cf 10056
c2cc16fa
JR
10057 for (i = 0; i < dpyinfo->n_fonts; i++)
10058 if (dpyinfo->font_table[i].name
10059 && font == dpyinfo->font_table[i].font)
10060 break;
10061
10062 xassert (i < dpyinfo->n_fonts);
10063}
10064
10065#endif /* GLYPH_DEBUG != 0 */
10066
10067/* Set *W to the minimum width, *H to the minimum font height of FONT.
10068 Note: There are (broken) X fonts out there with invalid XFontStruct
10069 min_bounds contents. For example, handa@etl.go.jp reports that
10070 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10071 have font->min_bounds.width == 0. */
10072
10073static INLINE void
10074x_font_min_bounds (font, w, h)
10075 XFontStruct *font;
10076 int *w, *h;
10077{
10078 /*
10079 * TODO: Windows does not appear to offer min bound, only
10080 * average and maximum width, and maximum height.
10081 */
10082 *h = FONT_HEIGHT (font);
10083 *w = FONT_WIDTH (font);
10084}
10085
10086
10087/* Compute the smallest character width and smallest font height over
10088 all fonts available on frame F. Set the members smallest_char_width
10089 and smallest_font_height in F's x_display_info structure to
10090 the values computed. Value is non-zero if smallest_font_height or
10091 smallest_char_width become smaller than they were before. */
10092
10093int
10094x_compute_min_glyph_bounds (f)
10095 struct frame *f;
10096{
10097 int i;
10098 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
10099 XFontStruct *font;
10100 int old_width = dpyinfo->smallest_char_width;
10101 int old_height = dpyinfo->smallest_font_height;
10102
10103 dpyinfo->smallest_font_height = 100000;
10104 dpyinfo->smallest_char_width = 100000;
10105
10106 for (i = 0; i < dpyinfo->n_fonts; ++i)
10107 if (dpyinfo->font_table[i].name)
10108 {
10109 struct font_info *fontp = dpyinfo->font_table + i;
10110 int w, h;
10111
10112 font = (XFontStruct *) fontp->font;
10113 xassert (font != (XFontStruct *) ~0);
10114 x_font_min_bounds (font, &w, &h);
10115
10116 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10117 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10118 }
10119
10120 xassert (dpyinfo->smallest_char_width > 0
10121 && dpyinfo->smallest_font_height > 0);
10122
10123 return (dpyinfo->n_fonts == 1
10124 || dpyinfo->smallest_char_width < old_width
10125 || dpyinfo->smallest_font_height < old_height);
10126}
10127
10128/* The following functions are listed here to help diff stay in step
10129 with xterm.c. See w32fns.c for definitions.
10130
10131x_load_font (f, fontname, size)
10132x_query_font (f, fontname)
10133x_find_ccl_program (fontp)
10134
10135*/
ee78dc32 10136\f
791f420f
JR
10137/***********************************************************************
10138 Initialization
10139 ***********************************************************************/
ee78dc32 10140
fbd6baed 10141static int w32_initialized = 0;
ee78dc32 10142
f7737f5d
JR
10143void
10144w32_initialize_display_info (display_name)
10145 Lisp_Object display_name;
10146{
10147 struct w32_display_info *dpyinfo = &one_w32_display_info;
10148
10149 bzero (dpyinfo, sizeof (*dpyinfo));
10150
10151 /* Put it on w32_display_name_list. */
10152 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
10153 w32_display_name_list);
10154 dpyinfo->name_list_element = XCAR (w32_display_name_list);
10155
10156 dpyinfo->w32_id_name
10157 = (char *) xmalloc (XSTRING (Vinvocation_name)->size
10158 + XSTRING (Vsystem_name)->size
10159 + 2);
10160 sprintf (dpyinfo->w32_id_name, "%s@%s",
10161 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
10162
10163 /* Default Console mode values - overridden when running in GUI mode
10164 with values obtained from system metrics. */
10165 dpyinfo->resx = 1;
10166 dpyinfo->resy = 1;
10167 dpyinfo->height_in = 1;
10168 dpyinfo->width_in = 1;
10169 dpyinfo->n_planes = 1;
10170 dpyinfo->n_cbits = 4;
10171 dpyinfo->n_fonts = 0;
10172 dpyinfo->smallest_font_height = 1;
10173 dpyinfo->smallest_char_width = 1;
10174
10175 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10176 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10177 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10178 dpyinfo->mouse_face_window = Qnil;
10179
01b220b6 10180 /* TODO: dpyinfo->gray */
f7737f5d
JR
10181
10182}
10183
fbd6baed
GV
10184struct w32_display_info *
10185w32_term_init (display_name, xrm_option, resource_name)
ee78dc32
GV
10186 Lisp_Object display_name;
10187 char *xrm_option;
10188 char *resource_name;
10189{
fbd6baed 10190 struct w32_display_info *dpyinfo;
ee78dc32 10191 HDC hdc;
97c23857 10192
ee78dc32
GV
10193 BLOCK_INPUT;
10194
fbd6baed 10195 if (!w32_initialized)
ee78dc32 10196 {
fbd6baed
GV
10197 w32_initialize ();
10198 w32_initialized = 1;
ee78dc32
GV
10199 }
10200
10201 {
10202 int argc = 0;
10203 char *argv[3];
10204
10205 argv[0] = "";
10206 argc = 1;
10207 if (xrm_option)
10208 {
10209 argv[argc++] = "-xrm";
10210 argv[argc++] = xrm_option;
10211 }
10212 }
10213
f7737f5d 10214 w32_initialize_display_info (display_name);
ee78dc32 10215
f7737f5d 10216 dpyinfo = &one_w32_display_info;
8c3b00cb
AI
10217
10218 /* Put this display on the chain. */
10219 dpyinfo->next = x_display_list;
10220 x_display_list = dpyinfo;
ee78dc32 10221
52cf03a1 10222 hdc = GetDC (GetDesktopWindow ());
791f420f 10223
ee78dc32
GV
10224 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
10225 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
10226 dpyinfo->root_window = GetDesktopWindow ();
10227 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
10228 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
55689ab1
JR
10229 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
10230 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
52cf03a1 10231 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
791f420f 10232 dpyinfo->image_cache = make_image_cache ();
55689ab1
JR
10233 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
10234 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
ee78dc32 10235 ReleaseDC (GetDesktopWindow (), hdc);
52cf03a1
GV
10236
10237 /* initialise palette with white and black */
10238 {
10239 COLORREF color;
55689ab1
JR
10240 w32_defined_color (0, "white", &color, 1);
10241 w32_defined_color (0, "black", &color, 1);
52cf03a1
GV
10242 }
10243
f7737f5d
JR
10244 /* Create Row Bitmaps and store them for later use. */
10245 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
10246 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
10247 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
10248 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
10249 1, continued_bits);
10250 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
10251 1, 1, continuation_bits);
10252 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
10253
ee78dc32
GV
10254#ifndef F_SETOWN_BUG
10255#ifdef F_SETOWN
10256#ifdef F_SETOWN_SOCK_NEG
10257 /* stdin is a socket here */
10258 fcntl (connection, F_SETOWN, -getpid ());
10259#else /* ! defined (F_SETOWN_SOCK_NEG) */
10260 fcntl (connection, F_SETOWN, getpid ());
10261#endif /* ! defined (F_SETOWN_SOCK_NEG) */
10262#endif /* ! defined (F_SETOWN) */
10263#endif /* F_SETOWN_BUG */
10264
10265#ifdef SIGIO
10266 if (interrupt_input)
10267 init_sigio (connection);
10268#endif /* ! defined (SIGIO) */
10269
10270 UNBLOCK_INPUT;
10271
10272 return dpyinfo;
10273}
10274\f
10275/* Get rid of display DPYINFO, assuming all frames are already gone. */
10276
10277void
10278x_delete_display (dpyinfo)
fbd6baed 10279 struct w32_display_info *dpyinfo;
ee78dc32 10280{
fbd6baed 10281 /* Discard this display from w32_display_name_list and w32_display_list.
ee78dc32 10282 We can't use Fdelq because that can quit. */
fbd6baed 10283 if (! NILP (w32_display_name_list)
8e713be6
KR
10284 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
10285 w32_display_name_list = XCDR (w32_display_name_list);
ee78dc32
GV
10286 else
10287 {
10288 Lisp_Object tail;
10289
fbd6baed 10290 tail = w32_display_name_list;
8e713be6 10291 while (CONSP (tail) && CONSP (XCDR (tail)))
ee78dc32 10292 {
f7737f5d 10293 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
ee78dc32 10294 {
8e713be6 10295 XCDR (tail) = XCDR (XCDR (tail));
ee78dc32
GV
10296 break;
10297 }
8e713be6 10298 tail = XCDR (tail);
ee78dc32
GV
10299 }
10300 }
10301
52cf03a1
GV
10302 /* free palette table */
10303 {
fbd6baed 10304 struct w32_palette_entry * plist;
52cf03a1
GV
10305
10306 plist = dpyinfo->color_list;
10307 while (plist)
10308 {
fbd6baed 10309 struct w32_palette_entry * pentry = plist;
52cf03a1 10310 plist = plist->next;
9127e20e 10311 xfree (pentry);
52cf03a1
GV
10312 }
10313 dpyinfo->color_list = NULL;
10314 if (dpyinfo->palette)
10315 DeleteObject(dpyinfo->palette);
10316 }
ee78dc32 10317 xfree (dpyinfo->font_table);
fbd6baed 10318 xfree (dpyinfo->w32_id_name);
f7737f5d
JR
10319
10320 /* Destroy row bitmaps. */
10321 DeleteObject (left_bmp);
10322 DeleteObject (ov_bmp);
10323 DeleteObject (right_bmp);
10324 DeleteObject (continued_bmp);
10325 DeleteObject (continuation_bmp);
10326 DeleteObject (zv_bmp);
ee78dc32
GV
10327}
10328\f
fbd6baed 10329/* Set up use of W32. */
ee78dc32 10330
689004fa 10331DWORD w32_msg_worker ();
ee78dc32 10332
791f420f
JR
10333void
10334x_flush (struct frame * f)
10335{ /* Nothing to do */ }
10336
10337static struct redisplay_interface w32_redisplay_interface =
10338{
10339 x_produce_glyphs,
10340 x_write_glyphs,
10341 x_insert_glyphs,
10342 x_clear_end_of_line,
10343 x_scroll_run,
10344 x_after_update_window_line,
10345 x_update_window_begin,
10346 x_update_window_end,
10347 w32_cursor_to,
10348 x_flush,
ec48c3a7 10349 x_clear_mouse_face,
791f420f
JR
10350 x_get_glyph_overhangs,
10351 x_fix_overlapping_area
10352};
10353
10354void
fbd6baed
GV
10355w32_initialize ()
10356{
791f420f
JR
10357 rif = &w32_redisplay_interface;
10358
96214669
GV
10359 /* MSVC does not type K&R functions with no arguments correctly, and
10360 so we must explicitly cast them. */
791f420f 10361 clear_frame_hook = (void (*)(void)) x_clear_frame;
96214669 10362 ring_bell_hook = (void (*)(void)) w32_ring_bell;
791f420f
JR
10363 update_begin_hook = x_update_begin;
10364 update_end_hook = x_update_end;
0f32f023 10365
ee78dc32 10366 read_socket_hook = w32_read_socket;
0f32f023 10367
fbd6baed 10368 frame_up_to_date_hook = w32_frame_up_to_date;
0f32f023 10369
fbd6baed
GV
10370 mouse_position_hook = w32_mouse_position;
10371 frame_rehighlight_hook = w32_frame_rehighlight;
10372 frame_raise_lower_hook = w32_frame_raise_lower;
10373 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
10374 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
10375 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
10376 judge_scroll_bars_hook = w32_judge_scroll_bars;
791f420f 10377 estimate_mode_line_height_hook = x_estimate_mode_line_height;
ee78dc32
GV
10378
10379 scroll_region_ok = 1; /* we'll scroll partial frames */
49be9f70 10380 char_ins_del_ok = 1;
ee78dc32
GV
10381 line_ins_del_ok = 1; /* we'll just blt 'em */
10382 fast_clear_end_of_line = 1; /* X does this well */
10383 memory_below_frame = 0; /* we don't remember what scrolls
10384 off the bottom */
10385 baud_rate = 19200;
10386
791f420f
JR
10387 last_tool_bar_item = -1;
10388 any_help_event_p = 0;
10389
689004fa
GV
10390 /* Initialize input mode: interrupt_input off, no flow control, allow
10391 8 bit character input, standard quit char. */
10392 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
ee78dc32
GV
10393
10394 /* Create the window thread - it will terminate itself or when the app terminates */
10395
10396 init_crit ();
10397
10398 dwMainThreadId = GetCurrentThreadId ();
10399 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
10400 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
10401
10402 /* Wait for thread to start */
10403
10404 {
10405 MSG msg;
10406
10407 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
10408
e9e23e23 10409 hWindowsThread = CreateThread (NULL, 0,
689004fa 10410 (LPTHREAD_START_ROUTINE) w32_msg_worker,
e9e23e23 10411 0, 0, &dwWindowsThreadId);
ee78dc32
GV
10412
10413 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
10414 }
10415
52cf03a1 10416 /* It is desirable that mainThread should have the same notion of
e9e23e23 10417 focus window and active window as windowsThread. Unfortunately, the
52cf03a1
GV
10418 following call to AttachThreadInput, which should do precisely what
10419 we need, causes major problems when Emacs is linked as a console
10420 program. Unfortunately, we have good reasons for doing that, so
e9e23e23 10421 instead we need to send messages to windowsThread to make some API
52cf03a1
GV
10422 calls for us (ones that affect, or depend on, the active/focus
10423 window state. */
10424#ifdef ATTACH_THREADS
e9e23e23 10425 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
52cf03a1 10426#endif
689004fa
GV
10427
10428 /* Dynamically link to optional system components. */
10429 {
10430 HANDLE user_lib = LoadLibrary ("user32.dll");
10431
10432#define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
10433
10434 /* New proportional scroll bar functions. */
9127e20e
JR
10435 LOAD_PROC (SetScrollInfo);
10436 LOAD_PROC (GetScrollInfo);
689004fa
GV
10437
10438#undef LOAD_PROC
10439
10440 FreeLibrary (user_lib);
10441
10442 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
10443 otherwise use the fixed height. */
10444 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
10445 GetSystemMetrics (SM_CYVTHUMB);
10446
10447 /* For either kind of scroll bar, take account of the arrows; these
10448 effectively form the border of the main scroll bar range. */
10449 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
10450 = GetSystemMetrics (SM_CYVSCROLL);
10451 }
ee78dc32
GV
10452}
10453
10454void
fbd6baed 10455syms_of_w32term ()
ee78dc32 10456{
fbd6baed
GV
10457 staticpro (&w32_display_name_list);
10458 w32_display_name_list = Qnil;
ee78dc32
GV
10459
10460 staticpro (&last_mouse_scroll_bar);
10461 last_mouse_scroll_bar = Qnil;
10462
10463 staticpro (&Qvendor_specific_keysyms);
10464 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
52cf03a1 10465
fbd6baed
GV
10466 DEFVAR_INT ("w32-num-mouse-buttons",
10467 &Vw32_num_mouse_buttons,
52cf03a1 10468 "Number of physical mouse buttons.");
fbd6baed 10469 Vw32_num_mouse_buttons = Qnil;
52cf03a1 10470
fbd6baed
GV
10471 DEFVAR_LISP ("w32-swap-mouse-buttons",
10472 &Vw32_swap_mouse_buttons,
52cf03a1
GV
10473 "Swap the mapping of middle and right mouse buttons.\n\
10474When nil, middle button is mouse-2 and right button is mouse-3.");
fbd6baed 10475 Vw32_swap_mouse_buttons = Qnil;
689004fa
GV
10476
10477 DEFVAR_LISP ("w32-grab-focus-on-raise",
10478 &Vw32_grab_focus_on_raise,
10479 "Raised frame grabs input focus.\n\
10480When t, `raise-frame' grabs input focus as well. This fits well\n\
10481with the normal Windows click-to-focus policy, but might not be\n\
10482desirable when using a point-to-focus policy.");
10483 Vw32_grab_focus_on_raise = Qt;
10484
10485 DEFVAR_LISP ("w32-capslock-is-shiftlock",
10486 &Vw32_capslock_is_shiftlock,
10487 "Apply CapsLock state to non character input keys.\n\
10488When nil, CapsLock only affects normal character input keys.");
10489 Vw32_capslock_is_shiftlock = Qnil;
ef0e360f
GV
10490
10491 DEFVAR_LISP ("w32-recognize-altgr",
10492 &Vw32_recognize_altgr,
10493 "Recognize right-alt and left-ctrl as AltGr.\n\
10494When nil, the right-alt and left-ctrl key combination is\n\
10495interpreted normally.");
10496 Vw32_recognize_altgr = Qt;
bc6af935 10497
484fa2c1
GV
10498 DEFVAR_BOOL ("w32-enable-unicode-output",
10499 &w32_enable_unicode_output,
10500 "Enable the use of Unicode for text output if non-nil.\n\
cabb23bc
GV
10501Unicode output may prevent some third party applications for displaying\n\
10502Far-East Languages on Windows 95/98 from working properly.\n\
10503NT uses Unicode internally anyway, so this flag will probably have no\n\
10504affect on NT machines.");
484fa2c1 10505 w32_enable_unicode_output = 1;
a1b9438c 10506
791f420f 10507 help_echo = Qnil;
ec48c3a7
JR
10508 staticpro (&help_echo);
10509 help_echo_object = Qnil;
10510 staticpro (&help_echo_object);
158cba56
JR
10511 help_echo_window = Qnil;
10512 staticpro (&help_echo_window);
791f420f 10513 previous_help_echo = Qnil;
ec48c3a7 10514 staticpro (&previous_help_echo);
158cba56 10515 help_echo_pos = -1;
791f420f
JR
10516
10517 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
10518 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
10519For example, if a block cursor is over a tab, it will be drawn as\n\
10520wide as that tab on the display.");
10521 x_stretch_cursor_p = 0;
10522
5bf04520 10523 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
791f420f 10524 "If not nil, Emacs uses toolkit scroll bars.");
5bf04520 10525 Vx_toolkit_scroll_bars = Qt;
791f420f
JR
10526
10527 staticpro (&last_mouse_motion_frame);
10528 last_mouse_motion_frame = Qnil;
ee78dc32 10529}