*** empty log message ***
[emacs.git] / src / w32term.c
blob6116f9ac8d8674f9f3bf3d5f7fbf1d0f940ccbf1
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "lisp.h"
27 #include "charset.h"
28 #include "blockinput.h"
30 #include "w32heap.h"
31 #include "w32term.h"
32 #include "w32bdf.h"
33 #include <shellapi.h>
35 #include "systty.h"
36 #include "systime.h"
37 #include "atimer.h"
39 #include <ctype.h>
40 #include <errno.h>
41 #include <setjmp.h>
42 #include <sys/stat.h>
44 #include "keyboard.h"
45 #include "frame.h"
46 #include "dispextern.h"
47 #include "fontset.h"
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"
55 #include "intervals.h"
56 #include "composite.h"
57 #include "coding.h"
59 #undef min
60 #undef max
61 #define min(x, y) (((x) < (y)) ? (x) : (y))
62 #define max(x, y) (((x) > (y)) ? (x) : (y))
64 #define abs(x) ((x) < 0 ? -(x) : (x))
66 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
69 /* Bitmaps for truncated lines. */
71 enum bitmap_type
73 NO_BITMAP,
74 LEFT_TRUNCATION_BITMAP,
75 RIGHT_TRUNCATION_BITMAP,
76 OVERLAY_ARROW_BITMAP,
77 CONTINUED_LINE_BITMAP,
78 CONTINUATION_LINE_BITMAP,
79 ZV_LINE_BITMAP
82 /* Bitmaps are all unsigned short, as Windows requires bitmap data to
83 be Word aligned. For some reason they are horizontally reflected
84 compared to how they appear on X, so changes in xterm.c should be
85 reflected here. */
87 /* Bitmap drawn to indicate lines not displaying text if
88 `indicate-empty-lines' is non-nil. */
90 #define zv_width 8
91 #define zv_height 8
92 static unsigned short zv_bits[] = {
93 0x00, 0x00, 0x78, 0x78, 0x78, 0x78, 0x00, 0x00};
94 static HBITMAP zv_bmp;
96 /* An arrow like this: `<-'. */
98 #define left_width 8
99 #define left_height 8
100 static unsigned short left_bits[] = {
101 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
102 static HBITMAP left_bmp;
104 /* Right truncation arrow bitmap `->'. */
106 #define right_width 8
107 #define right_height 8
108 static unsigned short right_bits[] = {
109 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
110 static HBITMAP right_bmp;
112 /* Marker for continued lines. */
114 #define continued_width 8
115 #define continued_height 8
116 static unsigned short continued_bits[] = {
117 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
118 static HBITMAP continued_bmp;
120 /* Marker for continuation lines. */
122 #define continuation_width 8
123 #define continuation_height 8
124 static unsigned short continuation_bits[] = {
125 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
126 static HBITMAP continuation_bmp;
128 /* Overlay arrow bitmap. */
130 #if 0
131 /* A bomb. */
132 #define ov_width 8
133 #define ov_height 8
134 static unsigned short ov_bits[] = {
135 0x0c, 0x10, 0x3c, 0x7e, 0x5e, 0x5e, 0x46, 0x3c};
136 #else
137 /* A triangular arrow. */
138 #define ov_width 8
139 #define ov_height 8
140 static unsigned short ov_bits[] = {
141 0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
142 #endif
143 static HBITMAP ov_bmp;
145 extern Lisp_Object Qhelp_echo;
148 /* Non-zero means Emacs uses toolkit scroll bars. */
150 int x_toolkit_scroll_bars_p;
152 /* If a string, w32_read_socket generates an event to display that string.
153 (The display is done in read_char.) */
155 static Lisp_Object help_echo;
156 static Lisp_Object help_echo_window;
157 static Lisp_Object help_echo_object;
158 static int help_echo_pos;
160 /* Temporary variable for w32_read_socket. */
162 static Lisp_Object previous_help_echo;
164 /* Non-zero means that a HELP_EVENT has been generated since Emacs
165 start. */
167 static int any_help_event_p;
169 /* Non-zero means draw block and hollow cursor as wide as the glyph
170 under it. For example, if a block cursor is over a tab, it will be
171 drawn as wide as that tab on the display. */
173 int x_stretch_cursor_p;
175 extern unsigned int msh_mousewheel;
177 extern void free_frame_menubar ();
179 extern void w32_menu_display_help (HMENU menu, UINT menu_item, UINT flags);
181 extern int w32_codepage_for_font (char *fontname);
183 extern glyph_metric *w32_BDF_TextMetric(bdffont *fontp,
184 unsigned char *text, int dim);
185 extern Lisp_Object Vwindow_system;
187 #define x_any_window_to_frame x_window_to_frame
188 #define x_top_window_to_frame x_window_to_frame
191 /* This is display since w32 does not support multiple ones. */
192 struct w32_display_info one_w32_display_info;
194 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
195 one for each element of w32_display_list and in the same order.
196 NAME is the name of the frame.
197 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
198 Lisp_Object w32_display_name_list;
200 /* Frame being updated by update_frame. This is declared in term.c.
201 This is set by update_begin and looked at by all the
202 w32 functions. It is zero while not inside an update.
203 In that case, the w32 functions assume that `SELECTED_FRAME ()'
204 is the frame to apply to. */
205 extern struct frame *updating_frame;
207 /* This is a frame waiting to be autoraised, within w32_read_socket. */
208 struct frame *pending_autoraise_frame;
210 /* Nominal cursor position -- where to draw output.
211 HPOS and VPOS are window relative glyph matrix coordinates.
212 X and Y are window relative pixel coordinates. */
214 struct cursor_pos output_cursor;
216 /* Flag to enable Unicode output in case users wish to use programs
217 like Twinbridge on '95 rather than installed system level support
218 for Far East languages. */
219 int w32_enable_unicode_output;
221 DWORD dwWindowsThreadId = 0;
222 HANDLE hWindowsThread = NULL;
223 DWORD dwMainThreadId = 0;
224 HANDLE hMainThread = NULL;
226 #ifndef SIF_ALL
227 /* These definitions are new with Windows 95. */
228 #define SIF_RANGE 0x0001
229 #define SIF_PAGE 0x0002
230 #define SIF_POS 0x0004
231 #define SIF_DISABLENOSCROLL 0x0008
232 #define SIF_TRACKPOS 0x0010
233 #define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
235 typedef struct tagSCROLLINFO
237 UINT cbSize;
238 UINT fMask;
239 int nMin;
240 int nMax;
241 UINT nPage;
242 int nPos;
243 int nTrackPos;
244 } SCROLLINFO, FAR *LPSCROLLINFO;
245 typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
246 #endif /* SIF_ALL */
248 /* Dynamic linking to new proportional scroll bar functions. */
249 int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
250 BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
252 int vertical_scroll_bar_min_handle;
253 int vertical_scroll_bar_top_border;
254 int vertical_scroll_bar_bottom_border;
256 int last_scroll_bar_drag_pos;
258 /* Mouse movement. */
260 /* Where the mouse was last time we reported a mouse event. */
262 FRAME_PTR last_mouse_frame;
263 static RECT last_mouse_glyph;
264 static Lisp_Object last_mouse_press_frame;
266 Lisp_Object Vw32_num_mouse_buttons;
268 Lisp_Object Vw32_swap_mouse_buttons;
270 /* Control whether x_raise_frame also sets input focus. */
271 Lisp_Object Vw32_grab_focus_on_raise;
273 /* Control whether Caps Lock affects non-ascii characters. */
274 Lisp_Object Vw32_capslock_is_shiftlock;
276 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
277 Lisp_Object Vw32_recognize_altgr;
279 /* The scroll bar in which the last motion event occurred.
281 If the last motion event occurred in a scroll bar, we set this
282 so w32_mouse_position can know whether to report a scroll bar motion or
283 an ordinary motion.
285 If the last motion event didn't occur in a scroll bar, we set this
286 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
287 static Lisp_Object last_mouse_scroll_bar;
288 static int last_mouse_scroll_bar_pos;
290 /* This is a hack. We would really prefer that w32_mouse_position would
291 return the time associated with the position it returns, but there
292 doesn't seem to be any way to wrest the time-stamp from the server
293 along with the position query. So, we just keep track of the time
294 of the last movement we received, and return that in hopes that
295 it's somewhat accurate. */
297 static Time last_mouse_movement_time;
299 /* Incremented by w32_read_socket whenever it really tries to read
300 events. */
302 #ifdef __STDC__
303 static int volatile input_signal_count;
304 #else
305 static int input_signal_count;
306 #endif
308 extern Lisp_Object Vcommand_line_args, Vsystem_name;
310 extern Lisp_Object Qface, Qmouse_face;
312 #ifndef USE_CRT_DLL
313 extern int errno;
314 #endif
316 /* A mask of extra modifier bits to put into every keyboard char. */
318 extern int extra_keyboard_modifiers;
320 /* Enumeration for overriding/changing the face to use for drawing
321 glyphs in x_draw_glyphs. */
323 enum draw_glyphs_face
325 DRAW_NORMAL_TEXT,
326 DRAW_INVERSE_VIDEO,
327 DRAW_CURSOR,
328 DRAW_MOUSE_FACE,
329 DRAW_IMAGE_RAISED,
330 DRAW_IMAGE_SUNKEN
333 static void x_update_window_end P_ ((struct window *, int, int));
334 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
335 void w32_delete_display P_ ((struct w32_display_info *));
336 static int fast_find_position P_ ((struct window *, int, int *, int *,
337 int *, int *));
338 static void set_output_cursor P_ ((struct cursor_pos *));
339 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
340 int *, int *, int *));
341 static void note_mode_line_highlight P_ ((struct window *, int, int));
342 static void x_check_font P_ ((struct frame *, XFontStruct *));
343 static void note_mouse_highlight P_ ((struct frame *, int, int));
344 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
345 static void w32_handle_tool_bar_click P_ ((struct frame *,
346 struct input_event *));
347 static void show_mouse_face P_ ((struct w32_display_info *,
348 enum draw_glyphs_face));
349 void clear_mouse_face P_ ((struct w32_display_info *));
351 void x_lower_frame P_ ((struct frame *));
352 void x_scroll_bar_clear P_ ((struct frame *));
353 void x_wm_set_size_hint P_ ((struct frame *, long, int));
354 void x_raise_frame P_ ((struct frame *));
355 void x_set_window_size P_ ((struct frame *, int, int, int));
356 void x_wm_set_window_state P_ ((struct frame *, int));
357 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
358 void w32_initialize P_ ((void));
359 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
360 int x_compute_min_glyph_bounds P_ ((struct frame *));
361 static void x_draw_phys_cursor_glyph P_ ((struct window *,
362 struct glyph_row *,
363 enum draw_glyphs_face));
364 static void x_update_end P_ ((struct frame *));
365 static void w32_frame_up_to_date P_ ((struct frame *));
366 static void w32_reassert_line_highlight P_ ((int, int));
367 static void x_change_line_highlight P_ ((int, int, int, int));
368 static void w32_set_terminal_modes P_ ((void));
369 static void w32_reset_terminal_modes P_ ((void));
370 static void w32_cursor_to P_ ((int, int, int, int));
371 static void x_write_glyphs P_ ((struct glyph *, int));
372 static void x_clear_end_of_line P_ ((int));
373 static void x_clear_frame P_ ((void));
374 static void x_clear_cursor P_ ((struct window *));
375 static void frame_highlight P_ ((struct frame *));
376 static void frame_unhighlight P_ ((struct frame *));
377 static void w32_new_focus_frame P_ ((struct w32_display_info *,
378 struct frame *));
379 static void w32_frame_rehighlight P_ ((struct frame *));
380 static void x_frame_rehighlight P_ ((struct w32_display_info *));
381 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
382 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
383 static void expose_frame P_ ((struct frame *, int, int, int, int));
384 static void expose_window_tree P_ ((struct window *, RECT *));
385 static void expose_window P_ ((struct window *, RECT *));
386 static void expose_area P_ ((struct window *, struct glyph_row *,
387 RECT *, enum glyph_row_area));
388 static void expose_line P_ ((struct window *, struct glyph_row *,
389 RECT *));
390 void x_update_cursor P_ ((struct frame *, int));
391 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
392 static void x_update_window_cursor P_ ((struct window *, int));
393 static void x_erase_phys_cursor P_ ((struct window *));
394 void x_display_cursor P_ ((struct window *w, int, int, int, int, int));
395 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
396 static void w32_draw_bitmap P_ ((struct window *, HDC hdc, struct glyph_row *,
397 enum bitmap_type));
398 static int x_phys_cursor_in_rect_p P_ ((struct window *, RECT *));
399 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
400 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
401 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *,
402 HDC, int));
404 static Lisp_Object Qvendor_specific_keysyms;
407 /***********************************************************************
408 Debugging
409 ***********************************************************************/
411 #if 0
413 /* This is a function useful for recording debugging information about
414 the sequence of occurrences in this file. */
416 struct record
418 char *locus;
419 int type;
422 struct record event_record[100];
424 int event_record_index;
426 record_event (locus, type)
427 char *locus;
428 int type;
430 if (event_record_index == sizeof (event_record) / sizeof (struct record))
431 event_record_index = 0;
433 event_record[event_record_index].locus = locus;
434 event_record[event_record_index].type = type;
435 event_record_index++;
438 #endif /* 0 */
441 void XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
442 XGCValues *xgcv)
444 if (mask & GCForeground)
445 gc->foreground = xgcv->foreground;
446 if (mask & GCBackground)
447 gc->background = xgcv->background;
448 if (mask & GCFont)
449 gc->font = xgcv->font;
452 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
453 XGCValues *xgcv)
455 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
456 bzero (gc, sizeof (XGCValues));
458 XChangeGC (ignore, gc, mask, xgcv);
460 return gc;
463 void XGetGCValues (void* ignore, XGCValues *gc,
464 unsigned long mask, XGCValues *xgcv)
466 XChangeGC (ignore, xgcv, mask, gc);
469 static void
470 w32_set_clip_rectangle (HDC hdc, RECT *rect)
472 if (rect)
474 HRGN clip_region = CreateRectRgnIndirect (rect);
475 SelectClipRgn (hdc, clip_region);
476 DeleteObject (clip_region);
478 else
479 SelectClipRgn (hdc, NULL);
483 /* Draw a hollow rectangle at the specified position. */
484 void
485 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
486 int width, int height)
488 HBRUSH hb, oldhb;
489 HPEN hp, oldhp;
491 hb = CreateSolidBrush (gc->background);
492 hp = CreatePen (PS_SOLID, 0, gc->foreground);
493 oldhb = SelectObject (hdc, hb);
494 oldhp = SelectObject (hdc, hp);
496 Rectangle (hdc, x, y, x + width, y + height);
498 SelectObject (hdc, oldhb);
499 SelectObject (hdc, oldhp);
500 DeleteObject (hb);
501 DeleteObject (hp);
504 /* Draw a filled rectangle at the specified position. */
505 void
506 w32_fill_rect (f, hdc, pix, lprect)
507 FRAME_PTR f;
508 HDC hdc;
509 COLORREF pix;
510 RECT * lprect;
512 HBRUSH hb;
513 RECT rect;
515 hb = CreateSolidBrush (pix);
516 FillRect (hdc, lprect, hb);
517 DeleteObject (hb);
520 void
521 w32_clear_window (f)
522 FRAME_PTR f;
524 RECT rect;
525 HDC hdc = get_frame_dc (f);
527 /* Under certain conditions, this can be called at startup with
528 a console frame pointer before the GUI frame is created. An HDC
529 of 0 indicates this. */
530 if (hdc)
532 GetClientRect (FRAME_W32_WINDOW (f), &rect);
533 w32_clear_rect (f, hdc, &rect);
536 release_frame_dc (f, hdc);
540 /***********************************************************************
541 Starting and ending an update
542 ***********************************************************************/
544 /* Start an update of frame F. This function is installed as a hook
545 for update_begin, i.e. it is called when update_begin is called.
546 This function is called prior to calls to x_update_window_begin for
547 each window being updated. Currently, there is nothing to do here
548 because all interesting stuff is done on a window basis. */
550 static void
551 x_update_begin (f)
552 struct frame *f;
554 /* Nothing to do. We have to do something though, otherwise the
555 function gets optimized away and the hook is no longer valid. */
556 struct frame *cf = f;
560 /* Start update of window W. Set the global variable updated_window
561 to the window being updated and set output_cursor to the cursor
562 position of W. */
564 static void
565 x_update_window_begin (w)
566 struct window *w;
568 struct frame *f = XFRAME (WINDOW_FRAME (w));
569 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
571 updated_window = w;
572 set_output_cursor (&w->cursor);
574 BLOCK_INPUT;
576 /* Regenerate display palette before drawing if list of requested
577 colors has changed. */
578 if (display_info->regen_palette)
580 w32_regenerate_palette (f);
581 display_info->regen_palette = FALSE;
584 if (f == display_info->mouse_face_mouse_frame)
586 /* Don't do highlighting for mouse motion during the update. */
587 display_info->mouse_face_defer = 1;
589 /* If F needs to be redrawn, simply forget about any prior mouse
590 highlighting. */
591 if (FRAME_GARBAGED_P (f))
592 display_info->mouse_face_window = Qnil;
594 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
595 their mouse_face_p flag set, which means that they are always
596 unequal to rows in a desired matrix which never have that
597 flag set. So, rows containing mouse-face glyphs are never
598 scrolled, and we don't have to switch the mouse highlight off
599 here to prevent it from being scrolled. */
601 /* Can we tell that this update does not affect the window
602 where the mouse highlight is? If so, no need to turn off.
603 Likewise, don't do anything if the frame is garbaged;
604 in that case, the frame's current matrix that we would use
605 is all wrong, and we will redisplay that line anyway. */
606 if (!NILP (display_info->mouse_face_window)
607 && w == XWINDOW (display_info->mouse_face_window))
609 int i;
611 for (i = 0; i < w->desired_matrix->nrows; ++i)
612 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
613 break;
615 if (i < w->desired_matrix->nrows)
616 clear_mouse_face (display_info);
618 #endif /* 0 */
621 UNBLOCK_INPUT;
625 /* Draw a vertical window border to the right of window W if W doesn't
626 have vertical scroll bars. */
628 static void
629 x_draw_vertical_border (w)
630 struct window *w;
632 struct frame *f = XFRAME (WINDOW_FRAME (w));
634 /* Redraw borders between horizontally adjacent windows. Don't
635 do it for frames with vertical scroll bars because either the
636 right scroll bar of a window, or the left scroll bar of its
637 neighbor will suffice as a border. */
638 if (!WINDOW_RIGHTMOST_P (w)
639 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
641 RECT r;
642 HDC hdc;
644 window_box_edges (w, -1, &r.left, &r.top, &r.right, &r.bottom);
645 r.left = r.right + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
646 r.right = r.left + 1;
647 r.bottom -= 1;
649 hdc = get_frame_dc (f);
650 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
651 release_frame_dc (f, hdc);
656 /* End update of window W (which is equal to updated_window).
658 Draw vertical borders between horizontally adjacent windows, and
659 display W's cursor if CURSOR_ON_P is non-zero.
661 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
662 glyphs in mouse-face were overwritten. In that case we have to
663 make sure that the mouse-highlight is properly redrawn.
665 W may be a menu bar pseudo-window in case we don't have X toolkit
666 support. Such windows don't have a cursor, so don't display it
667 here. */
669 static void
670 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
671 struct window *w;
672 int cursor_on_p, mouse_face_overwritten_p;
674 if (!w->pseudo_window_p)
676 struct w32_display_info *dpyinfo
677 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
679 BLOCK_INPUT;
681 /* If a row with mouse-face was overwritten, arrange for
682 XTframe_up_to_date to redisplay the mouse highlight. */
683 if (mouse_face_overwritten_p)
685 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
686 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
687 dpyinfo->mouse_face_window = Qnil;
690 if (cursor_on_p)
691 x_display_and_set_cursor (w, 1, output_cursor.hpos,
692 output_cursor.vpos,
693 output_cursor.x, output_cursor.y);
694 x_draw_vertical_border (w);
695 UNBLOCK_INPUT;
698 updated_window = NULL;
702 /* End update of frame F. This function is installed as a hook in
703 update_end. */
705 static void
706 x_update_end (f)
707 struct frame *f;
709 /* Mouse highlight may be displayed again. */
710 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
714 /* This function is called from various places in xdisp.c whenever a
715 complete update has been performed. The global variable
716 updated_window is not available here. */
718 static void
719 w32_frame_up_to_date (f)
720 struct frame *f;
722 if (FRAME_W32_P (f))
724 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
725 if (dpyinfo->mouse_face_deferred_gc
726 || f == dpyinfo->mouse_face_mouse_frame)
728 BLOCK_INPUT;
729 if (dpyinfo->mouse_face_mouse_frame)
730 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
731 dpyinfo->mouse_face_mouse_x,
732 dpyinfo->mouse_face_mouse_y);
733 dpyinfo->mouse_face_deferred_gc = 0;
734 UNBLOCK_INPUT;
740 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
741 arrow bitmaps, or clear the areas where they would be displayed
742 before DESIRED_ROW is made current. The window being updated is
743 found in updated_window. This function It is called from
744 update_window_line only if it is known that there are differences
745 between bitmaps to be drawn between current row and DESIRED_ROW. */
747 static void
748 x_after_update_window_line (desired_row)
749 struct glyph_row *desired_row;
751 struct window *w = updated_window;
753 xassert (w);
755 if (!desired_row->mode_line_p && !w->pseudo_window_p)
757 BLOCK_INPUT;
758 x_draw_row_bitmaps (w, desired_row);
760 /* When a window has disappeared, make sure that no rest of
761 full-width rows stays visible in the internal border. */
762 if (windows_or_buffers_changed)
764 struct frame *f = XFRAME (w->frame);
765 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
766 int height = desired_row->visible_height;
767 int x = (window_box_right (w, -1)
768 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
769 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
770 HDC hdc = get_frame_dc (f);
772 w32_clear_area (f, hdc, x, y, width, height);
773 release_frame_dc (f, hdc);
776 UNBLOCK_INPUT;
781 /* Draw the bitmap WHICH in one of the areas to the left or right of
782 window W. ROW is the glyph row for which to display the bitmap; it
783 determines the vertical position at which the bitmap has to be
784 drawn. */
786 static void
787 w32_draw_bitmap (w, hdc, row, which)
788 struct window *w;
789 HDC hdc;
790 struct glyph_row *row;
791 enum bitmap_type which;
793 struct frame *f = XFRAME (WINDOW_FRAME (w));
794 Window window = FRAME_W32_WINDOW (f);
795 HDC compat_hdc;
796 int x, y, wd, h, dy;
797 HBITMAP pixmap;
798 HBRUSH fg_brush, orig_brush;
799 HANDLE horig_obj;
800 struct face *face;
802 /* Must clip because of partially visible lines. */
803 w32_clip_to_row (w, row, hdc, 1);
805 switch (which)
807 case LEFT_TRUNCATION_BITMAP:
808 wd = left_width;
809 h = left_height;
810 pixmap = left_bmp;
811 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
812 - wd
813 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
814 break;
816 case OVERLAY_ARROW_BITMAP:
817 wd = ov_width;
818 h = ov_height;
819 pixmap = ov_bmp;
820 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
821 - wd
822 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
823 break;
825 case RIGHT_TRUNCATION_BITMAP:
826 wd = right_width;
827 h = right_height;
828 pixmap = right_bmp;
829 x = window_box_right (w, -1);
830 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
831 break;
833 case CONTINUED_LINE_BITMAP:
834 wd = continued_width;
835 h = continued_height;
836 pixmap = continued_bmp;
837 x = window_box_right (w, -1);
838 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
839 break;
841 case CONTINUATION_LINE_BITMAP:
842 wd = continuation_width;
843 h = continuation_height;
844 pixmap = continuation_bmp;
845 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
846 - wd
847 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
848 break;
850 case ZV_LINE_BITMAP:
851 wd = zv_width;
852 h = zv_height;
853 pixmap = zv_bmp;
854 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
855 - wd
856 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
857 break;
859 default:
860 abort ();
863 /* Convert to frame coordinates. Set dy to the offset in the row to
864 start drawing the bitmap. */
865 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
866 dy = (row->height - h) / 2;
868 /* Draw the bitmap. */
869 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
871 compat_hdc = CreateCompatibleDC (hdc);
872 SaveDC (hdc);
873 fg_brush = CreateSolidBrush (face->foreground);
874 orig_brush = SelectObject (hdc, fg_brush);
875 horig_obj = SelectObject (compat_hdc, pixmap);
876 SetTextColor (hdc, face->foreground);
877 SetBkColor (hdc, face->background);
878 #if 0 /* From w32bdf.c (which is from Meadow). */
879 /* Old versions - in case we find a reason to fall back on them. */
880 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, SRCCOPY);
881 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, 0xB8074A);
882 #else
883 BitBlt (hdc, x, y + dy, wd, h, compat_hdc, 0, 0, 0xE20746);
884 #endif
885 SelectObject (compat_hdc, horig_obj);
886 SelectObject (hdc, orig_brush);
887 DeleteObject (fg_brush);
888 DeleteDC (compat_hdc);
889 RestoreDC (hdc, -1);
893 /* Draw flags bitmaps for glyph row ROW on window W. Call this
894 function with input blocked. */
896 static void
897 x_draw_row_bitmaps (w, row)
898 struct window *w;
899 struct glyph_row *row;
901 struct frame *f = XFRAME (w->frame);
902 enum bitmap_type bitmap;
903 struct face *face;
904 int header_line_height = -1;
905 HDC hdc = get_frame_dc (f);
907 xassert (interrupt_input_blocked);
909 /* If row is completely invisible, because of vscrolling, we
910 don't have to draw anything. */
911 if (row->visible_height <= 0)
912 return;
914 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
915 PREPARE_FACE_FOR_DISPLAY (f, face);
917 /* Decide which bitmap to draw at the left side. */
918 if (row->overlay_arrow_p)
919 bitmap = OVERLAY_ARROW_BITMAP;
920 else if (row->truncated_on_left_p)
921 bitmap = LEFT_TRUNCATION_BITMAP;
922 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
923 bitmap = CONTINUATION_LINE_BITMAP;
924 else if (row->indicate_empty_line_p)
925 bitmap = ZV_LINE_BITMAP;
926 else
927 bitmap = NO_BITMAP;
929 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
930 the flags area. */
931 if (bitmap == NO_BITMAP
932 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
933 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 int left = window_box_left (w, -1);
941 if (header_line_height < 0)
942 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
944 w32_fill_area (f, hdc, face->background,
945 left - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) + border,
946 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
947 row->y)),
948 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
949 row->visible_height);
952 /* Draw the left bitmap. */
953 if (bitmap != NO_BITMAP)
954 w32_draw_bitmap (w, hdc, row, bitmap);
956 /* Decide which bitmap to draw at the right side. */
957 if (row->truncated_on_right_p)
958 bitmap = RIGHT_TRUNCATION_BITMAP;
959 else if (row->continued_p)
960 bitmap = CONTINUED_LINE_BITMAP;
961 else
962 bitmap = NO_BITMAP;
964 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
965 the flags area. */
966 if (bitmap == NO_BITMAP
967 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
968 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
970 int right = window_box_right (w, -1);
972 if (header_line_height < 0)
973 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
975 w32_fill_area (f, hdc, face->background,
976 right,
977 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
978 row->y)),
979 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
980 row->visible_height);
983 /* Draw the right bitmap. */
984 if (bitmap != NO_BITMAP)
985 w32_draw_bitmap (w, hdc, row, bitmap);
987 release_frame_dc (f, hdc);
991 /***********************************************************************
992 Line Highlighting
993 ***********************************************************************/
995 /* External interface to control of standout mode. Not used for W32
996 frames. Aborts when called. */
998 static void
999 w32_reassert_line_highlight (new, vpos)
1000 int new, vpos;
1002 abort ();
1005 /* Call this when about to modify line at position VPOS and change
1006 whether it is highlighted. Not used for W32 frames. Aborts when
1007 called. */
1009 static void
1010 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1011 int new_highlight, vpos, y, first_unused_hpos;
1013 abort ();
1016 /* This is called when starting Emacs and when restarting after
1017 suspend. When starting Emacs, no window is mapped. And nothing
1018 must be done to Emacs's own window if it is suspended (though that
1019 rarely happens). */
1021 static void
1022 w32_set_terminal_modes (void)
1026 /* This is called when exiting or suspending Emacs. Exiting will make
1027 the W32 windows go away, and suspending requires no action. */
1029 static void
1030 w32_reset_terminal_modes (void)
1036 /***********************************************************************
1037 Output Cursor
1038 ***********************************************************************/
1040 /* Set the global variable output_cursor to CURSOR. All cursor
1041 positions are relative to updated_window. */
1043 static void
1044 set_output_cursor (cursor)
1045 struct cursor_pos *cursor;
1047 output_cursor.hpos = cursor->hpos;
1048 output_cursor.vpos = cursor->vpos;
1049 output_cursor.x = cursor->x;
1050 output_cursor.y = cursor->y;
1054 /* Set a nominal cursor position.
1056 HPOS and VPOS are column/row positions in a window glyph matrix. X
1057 and Y are window text area relative pixel positions.
1059 If this is done during an update, updated_window will contain the
1060 window that is being updated and the position is the future output
1061 cursor position for that window. If updated_window is null, use
1062 selected_window and display the cursor at the given position. */
1064 static void
1065 w32_cursor_to (vpos, hpos, y, x)
1066 int vpos, hpos, y, x;
1068 struct window *w;
1070 /* If updated_window is not set, work on selected_window. */
1071 if (updated_window)
1072 w = updated_window;
1073 else
1074 w = XWINDOW (selected_window);
1076 /* Set the output cursor. */
1077 output_cursor.hpos = hpos;
1078 output_cursor.vpos = vpos;
1079 output_cursor.x = x;
1080 output_cursor.y = y;
1082 /* If not called as part of an update, really display the cursor.
1083 This will also set the cursor position of W. */
1084 if (updated_window == NULL)
1086 BLOCK_INPUT;
1087 x_display_cursor (w, 1, hpos, vpos, x, y);
1088 UNBLOCK_INPUT;
1094 /***********************************************************************
1095 Display Iterator
1096 ***********************************************************************/
1098 /* Function prototypes of this page. */
1100 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1101 struct glyph *,
1102 wchar_t *,
1103 int *));
1104 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1105 int, wchar_t *, int));
1106 static XCharStruct *w32_per_char_metric P_ ((XFontStruct *,
1107 wchar_t *,
1108 enum w32_char_font_type));
1109 static enum w32_char_font_type
1110 w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
1111 static void x_append_glyph P_ ((struct it *));
1112 static void x_append_composite_glyph P_ ((struct it *));
1113 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1114 int, int, double));
1115 static void x_produce_glyphs P_ ((struct it *));
1116 static void x_produce_image_glyph P_ ((struct it *it));
1119 /* Dealing with bits of wchar_t as if they were an XChar2B. */
1120 #define BUILD_WCHAR_T(byte1, byte2) \
1121 ((wchar_t)((((byte1) & 0x00ff) << 8) | ((byte2) & 0x00ff)))
1124 #define BYTE1(ch) \
1125 (((ch) & 0xff00) >> 8)
1127 #define BYTE2(ch) \
1128 ((ch) & 0x00ff)
1131 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
1132 If CHAR2B is not contained in FONT, the font's default character
1133 metric is returned. */
1135 static int
1136 w32_bdf_per_char_metric (font, char2b, dim, pcm)
1137 XFontStruct *font;
1138 wchar_t *char2b;
1139 int dim;
1140 XCharStruct * pcm;
1142 glyph_metric * bdf_metric;
1143 char buf[2];
1145 if (dim == 1)
1146 buf[0] = (char)(*char2b);
1147 else
1149 buf[0] = BYTE1 (*char2b);
1150 buf[1] = BYTE2 (*char2b);
1153 bdf_metric = w32_BDF_TextMetric (font->bdf, buf, dim);
1155 if (bdf_metric)
1157 pcm->width = bdf_metric->dwidth;
1158 pcm->lbearing = bdf_metric->bbox;
1159 pcm->rbearing = bdf_metric->dwidth
1160 - (bdf_metric->bbox + bdf_metric->bbw);
1161 pcm->ascent = bdf_metric->bboy + bdf_metric->bbh;
1162 pcm->descent = -bdf_metric->bboy;
1164 return 1;
1166 return 0;
1170 static int
1171 w32_native_per_char_metric (font, char2b, font_type, pcm)
1172 XFontStruct *font;
1173 wchar_t *char2b;
1174 enum w32_char_font_type font_type;
1175 XCharStruct * pcm;
1177 HDC hdc = GetDC (NULL);
1178 HFONT old_font;
1179 BOOL retval = FALSE;
1181 xassert (font && char2b);
1182 xassert (font->hfont);
1183 xassert (font_type == UNICODE_FONT || font_type == ANSI_FONT);
1185 old_font = SelectObject (hdc, font->hfont);
1187 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
1189 ABC char_widths;
1191 if (font_type == UNICODE_FONT)
1192 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
1193 else
1194 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
1196 if (retval)
1198 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
1199 pcm->lbearing = char_widths.abcA;
1200 pcm->rbearing = pcm->width - char_widths.abcC;
1201 pcm->ascent = FONT_BASE (font);
1202 pcm->descent = FONT_DESCENT (font);
1206 if (!retval)
1208 /* Either font is not a True-type font, or GetCharABCWidthsW
1209 failed (it is not supported on Windows 9x for instance), so we
1210 can't determine the full info we would like. All is not lost
1211 though - we can call GetTextExtentPoint32 to get rbearing and
1212 deduce width based on the font's per-string overhang. lbearing
1213 is assumed to be zero. */
1215 /* TODO: Some Thai characters (and other composites if Windows
1216 supports them) do have lbearing, and report their total width
1217 as zero. Need some way of handling them when
1218 GetCharABCWidthsW fails. */
1219 SIZE sz;
1221 if (font_type == UNICODE_FONT)
1222 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
1223 else
1224 retval = GetTextExtentPoint32A (hdc, (char*)char2b, 1, &sz);
1226 if (retval)
1228 pcm->width = sz.cx - font->tm.tmOverhang;
1229 pcm->rbearing = sz.cx;
1230 pcm->lbearing = 0;
1231 pcm->ascent = FONT_BASE (font);
1232 pcm->descent = FONT_DESCENT (font);
1237 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1239 retval = FALSE;
1242 SelectObject (hdc, old_font);
1243 ReleaseDC (NULL, hdc);
1245 return retval;
1249 static XCharStruct *
1250 w32_per_char_metric (font, char2b, font_type)
1251 XFontStruct *font;
1252 wchar_t *char2b;
1253 enum w32_char_font_type font_type;
1255 /* The result metric information. */
1256 XCharStruct *pcm;
1257 BOOL retval;
1259 xassert (font && char2b);
1260 xassert (font_type != UNKNOWN_FONT);
1262 /* Handle the common cases quickly. */
1263 if (!font->bdf && font->per_char == NULL)
1264 /* TODO: determine whether char2b exists in font? */
1265 return &font->max_bounds;
1266 else if (!font->bdf && *char2b < 128)
1267 return &font->per_char[*char2b];
1269 pcm = &font->scratch;
1271 if (font_type == BDF_1D_FONT)
1272 retval = w32_bdf_per_char_metric (font, char2b, 1, pcm);
1273 else if (font_type == BDF_2D_FONT)
1274 retval = w32_bdf_per_char_metric (font, char2b, 2, pcm);
1275 else
1276 retval = w32_native_per_char_metric (font, char2b, font_type, pcm);
1278 if (retval)
1279 return pcm;
1281 return NULL;
1284 void
1285 w32_cache_char_metrics (font)
1286 XFontStruct *font;
1288 wchar_t char2b = L'x';
1290 /* Cache char metrics for the common cases. */
1291 if (font->bdf)
1293 /* TODO: determine whether font is fixed-pitch. */
1294 if (!w32_bdf_per_char_metric (font, &char2b, 1, &font->max_bounds))
1296 /* Use the font width and height as max bounds, as not all BDF
1297 fonts contain the letter 'x'. */
1298 font->max_bounds.width = FONT_MAX_WIDTH (font);
1299 font->max_bounds.lbearing = -font->bdf->llx;
1300 font->max_bounds.rbearing = FONT_MAX_WIDTH (font) - font->bdf->urx;
1301 font->max_bounds.ascent = FONT_BASE (font);
1302 font->max_bounds.descent = FONT_DESCENT (font);
1305 else
1307 if (((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH) != 0)
1308 /* Some fonts (eg DBCS fonts) are marked as fixed width even
1309 though they contain characters of different widths. */
1310 || (font->tm.tmMaxCharWidth != font->tm.tmAveCharWidth))
1312 /* Font is not fixed pitch, so cache per_char info for the
1313 ASCII characters. It would be much more work, and probably
1314 not worth it, to cache other chars, since we may change
1315 between using Unicode and ANSI text drawing functions at
1316 run-time. */
1317 int i;
1319 font->per_char = xmalloc (128 * sizeof(XCharStruct));
1320 for (i = 0; i < 128; i++)
1322 char2b = i;
1323 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1324 &font->per_char[i]);
1327 else
1328 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1329 &font->max_bounds);
1334 /* Determine if a font is double byte. */
1335 int w32_font_is_double_byte (XFontStruct *font)
1337 return font->double_byte_p;
1341 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1342 the two-byte form of C. Encoding is returned in *CHAR2B. */
1344 static INLINE enum w32_char_font_type
1345 w32_encode_char (c, char2b, font_info, two_byte_p)
1346 int c;
1347 wchar_t *char2b;
1348 struct font_info *font_info;
1349 int * two_byte_p;
1351 int charset = CHAR_CHARSET (c);
1352 int codepage;
1353 int unicode_p = 0;
1355 XFontStruct *font = font_info->font;
1357 xassert (two_byte_p);
1359 *two_byte_p = w32_font_is_double_byte (font);
1361 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1362 This may be either a program in a special encoder language or a
1363 fixed encoding. */
1364 if (font_info->font_encoder)
1366 /* It's a program. */
1367 struct ccl_program *ccl = font_info->font_encoder;
1369 if (CHARSET_DIMENSION (charset) == 1)
1371 ccl->reg[0] = charset;
1372 ccl->reg[1] = BYTE2 (*char2b);
1374 else
1376 ccl->reg[0] = charset;
1377 ccl->reg[1] = BYTE1 (*char2b);
1378 ccl->reg[2] = BYTE2 (*char2b);
1381 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1383 /* We assume that MSBs are appropriately set/reset by CCL
1384 program. */
1385 if (!*two_byte_p) /* 1-byte font */
1386 *char2b = BUILD_WCHAR_T (0, ccl->reg[1]);
1387 else
1388 *char2b = BUILD_WCHAR_T (ccl->reg[1], ccl->reg[2]);
1390 else if (font_info->encoding[charset])
1392 /* Fixed encoding scheme. See fontset.h for the meaning of the
1393 encoding numbers. */
1394 int enc = font_info->encoding[charset];
1396 if ((enc == 1 || enc == 2)
1397 && CHARSET_DIMENSION (charset) == 2)
1398 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b) | 0x80, BYTE2 (*char2b));
1400 if (enc == 1 || enc == 3
1401 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1402 *char2b = BUILD_WCHAR_T (BYTE1 (*char2b), BYTE2 (*char2b) | 0x80);
1403 else if (enc == 4)
1405 int sjis1, sjis2;
1407 ENCODE_SJIS (BYTE1 (*char2b), BYTE2 (*char2b),
1408 sjis1, sjis2);
1409 *char2b = BUILD_WCHAR_T (sjis1, sjis2);
1412 codepage = w32_codepage_for_font (font_info->name);
1414 /* If charset is not ASCII or Latin-1, may need to move it into
1415 Unicode space. */
1416 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
1417 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1)
1419 char temp[3];
1420 temp[0] = BYTE1 (*char2b);
1421 temp[1] = BYTE2 (*char2b);
1422 temp[2] = '\0';
1423 if (codepage != CP_UNICODE)
1425 if (temp[0])
1426 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1427 else
1428 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1430 unicode_p = 1;
1431 *two_byte_p = 1;
1433 if (!font)
1434 return UNKNOWN_FONT;
1435 else if (font->bdf && CHARSET_DIMENSION (charset) == 1)
1436 return BDF_1D_FONT;
1437 else if (font->bdf)
1438 return BDF_2D_FONT;
1439 else if (unicode_p)
1440 return UNICODE_FONT;
1441 else
1442 return ANSI_FONT;
1446 /* Get face and two-byte form of character C in face FACE_ID on frame
1447 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1448 means we want to display multibyte text. Value is a pointer to a
1449 realized face that is ready for display. */
1451 static INLINE struct face *
1452 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1453 struct frame *f;
1454 int c, face_id;
1455 wchar_t *char2b;
1456 int multibyte_p;
1458 struct face *face = FACE_FROM_ID (f, face_id);
1460 if (!multibyte_p)
1462 /* Unibyte case. We don't have to encode, but we have to make
1463 sure to use a face suitable for unibyte. */
1464 *char2b = BUILD_WCHAR_T (0, c);
1465 face_id = FACE_FOR_CHAR (f, face, c);
1466 face = FACE_FROM_ID (f, face_id);
1468 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1470 /* Case of ASCII in a face known to fit ASCII. */
1471 *char2b = BUILD_WCHAR_T (0, c);
1473 else
1475 int c1, c2, charset;
1477 /* Split characters into bytes. If c2 is -1 afterwards, C is
1478 really a one-byte character so that byte1 is zero. */
1479 SPLIT_CHAR (c, charset, c1, c2);
1480 if (c2 > 0)
1481 *char2b = BUILD_WCHAR_T (c1, c2);
1482 else
1483 *char2b = BUILD_WCHAR_T (0, c1);
1485 /* Maybe encode the character in *CHAR2B. */
1486 if (face->font != NULL)
1488 struct font_info *font_info
1489 = FONT_INFO_FROM_ID (f, face->font_info_id);
1490 if (font_info)
1491 w32_encode_char (c, char2b, font_info, &multibyte_p);
1495 /* Make sure X resources of the face are allocated. */
1496 xassert (face != NULL);
1497 PREPARE_FACE_FOR_DISPLAY (f, face);
1499 return face;
1503 /* Get face and two-byte form of character glyph GLYPH on frame F.
1504 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1505 a pointer to a realized face that is ready for display. */
1507 static INLINE struct face *
1508 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1509 struct frame *f;
1510 struct glyph *glyph;
1511 wchar_t *char2b;
1512 int *two_byte_p;
1514 struct face *face;
1515 int dummy = 0;
1517 xassert (glyph->type == CHAR_GLYPH);
1518 face = FACE_FROM_ID (f, glyph->face_id);
1520 if (two_byte_p)
1521 *two_byte_p = 0;
1522 else
1523 two_byte_p = &dummy;
1525 if (!glyph->multibyte_p)
1527 /* Unibyte case. We don't have to encode, but we have to make
1528 sure to use a face suitable for unibyte. */
1529 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1531 else if (glyph->u.ch < 128
1532 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1534 /* Case of ASCII in a face known to fit ASCII. */
1535 *char2b = BUILD_WCHAR_T (0, glyph->u.ch);
1537 else
1539 int c1, c2, charset;
1541 /* Split characters into bytes. If c2 is -1 afterwards, C is
1542 really a one-byte character so that byte1 is zero. */
1543 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1544 if (c2 > 0)
1545 *char2b = BUILD_WCHAR_T (c1, c2);
1546 else
1547 *char2b = BUILD_WCHAR_T (0, c1);
1549 /* Maybe encode the character in *CHAR2B. */
1550 if (charset != CHARSET_ASCII)
1552 struct font_info *font_info
1553 = FONT_INFO_FROM_ID (f, face->font_info_id);
1554 if (font_info)
1556 glyph->w32_font_type
1557 = w32_encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
1562 /* Make sure X resources of the face are allocated. */
1563 xassert (face != NULL);
1564 PREPARE_FACE_FOR_DISPLAY (f, face);
1565 return face;
1569 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1570 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1572 static INLINE void
1573 x_append_glyph (it)
1574 struct it *it;
1576 struct glyph *glyph;
1577 enum glyph_row_area area = it->area;
1579 xassert (it->glyph_row);
1580 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1582 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1583 if (glyph < it->glyph_row->glyphs[area + 1])
1585 glyph->charpos = CHARPOS (it->position);
1586 glyph->object = it->object;
1587 glyph->pixel_width = it->pixel_width;
1588 glyph->voffset = it->voffset;
1589 glyph->type = CHAR_GLYPH;
1590 glyph->multibyte_p = it->multibyte_p;
1591 glyph->left_box_line_p = it->start_of_box_run_p;
1592 glyph->right_box_line_p = it->end_of_box_run_p;
1593 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1594 || it->phys_descent > it->descent);
1595 glyph->padding_p = 0;
1596 glyph->glyph_not_available_p = it->glyph_not_available_p;
1597 glyph->face_id = it->face_id;
1598 glyph->u.ch = it->char_to_display;
1599 glyph->w32_font_type = UNKNOWN_FONT;
1600 ++it->glyph_row->used[area];
1604 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1605 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1607 static INLINE void
1608 x_append_composite_glyph (it)
1609 struct it *it;
1611 struct glyph *glyph;
1612 enum glyph_row_area area = it->area;
1614 xassert (it->glyph_row);
1616 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1617 if (glyph < it->glyph_row->glyphs[area + 1])
1619 glyph->charpos = CHARPOS (it->position);
1620 glyph->object = it->object;
1621 glyph->pixel_width = it->pixel_width;
1622 glyph->voffset = it->voffset;
1623 glyph->type = COMPOSITE_GLYPH;
1624 glyph->multibyte_p = it->multibyte_p;
1625 glyph->left_box_line_p = it->start_of_box_run_p;
1626 glyph->right_box_line_p = it->end_of_box_run_p;
1627 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1628 || it->phys_descent > it->descent);
1629 glyph->padding_p = 0;
1630 glyph->glyph_not_available_p = 0;
1631 glyph->face_id = it->face_id;
1632 glyph->u.cmp_id = it->cmp_id;
1633 glyph->w32_font_type = UNKNOWN_FONT;
1634 ++it->glyph_row->used[area];
1639 /* Change IT->ascent and IT->height according to the setting of
1640 IT->voffset. */
1642 static INLINE void
1643 take_vertical_position_into_account (it)
1644 struct it *it;
1646 if (it->voffset)
1648 if (it->voffset < 0)
1649 /* Increase the ascent so that we can display the text higher
1650 in the line. */
1651 it->ascent += abs (it->voffset);
1652 else
1653 /* Increase the descent so that we can display the text lower
1654 in the line. */
1655 it->descent += it->voffset;
1660 /* Produce glyphs/get display metrics for the image IT is loaded with.
1661 See the description of struct display_iterator in dispextern.h for
1662 an overview of struct display_iterator. */
1664 static void
1665 x_produce_image_glyph (it)
1666 struct it *it;
1668 struct image *img;
1669 struct face *face;
1671 xassert (it->what == IT_IMAGE);
1673 face = FACE_FROM_ID (it->f, it->face_id);
1674 img = IMAGE_FROM_ID (it->f, it->image_id);
1675 xassert (img);
1677 /* Make sure X resources of the face and image are loaded. */
1678 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1679 prepare_image_for_display (it->f, img);
1681 it->ascent = it->phys_ascent = image_ascent (img, face);
1682 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1683 it->pixel_width = img->width + 2 * img->margin;
1685 it->nglyphs = 1;
1687 if (face->box != FACE_NO_BOX)
1689 it->ascent += face->box_line_width;
1690 it->descent += face->box_line_width;
1692 if (it->start_of_box_run_p)
1693 it->pixel_width += face->box_line_width;
1694 if (it->end_of_box_run_p)
1695 it->pixel_width += face->box_line_width;
1698 take_vertical_position_into_account (it);
1700 if (it->glyph_row)
1702 struct glyph *glyph;
1703 enum glyph_row_area area = it->area;
1705 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1706 if (glyph < it->glyph_row->glyphs[area + 1])
1708 glyph->charpos = CHARPOS (it->position);
1709 glyph->object = it->object;
1710 glyph->pixel_width = it->pixel_width;
1711 glyph->voffset = it->voffset;
1712 glyph->type = IMAGE_GLYPH;
1713 glyph->multibyte_p = it->multibyte_p;
1714 glyph->left_box_line_p = it->start_of_box_run_p;
1715 glyph->right_box_line_p = it->end_of_box_run_p;
1716 glyph->overlaps_vertically_p = 0;
1717 glyph->padding_p = 0;
1718 glyph->glyph_not_available_p = 0;
1719 glyph->face_id = it->face_id;
1720 glyph->u.img_id = img->id;
1721 glyph->w32_font_type = UNKNOWN_FONT;
1722 ++it->glyph_row->used[area];
1728 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1729 of the glyph, WIDTH and HEIGHT are the width and height of the
1730 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1731 ascent of the glyph (0 <= ASCENT <= 1). */
1733 static void
1734 x_append_stretch_glyph (it, object, width, height, ascent)
1735 struct it *it;
1736 Lisp_Object object;
1737 int width, height;
1738 double ascent;
1740 struct glyph *glyph;
1741 enum glyph_row_area area = it->area;
1743 xassert (ascent >= 0 && ascent <= 1);
1745 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1746 if (glyph < it->glyph_row->glyphs[area + 1])
1748 glyph->charpos = CHARPOS (it->position);
1749 glyph->object = object;
1750 glyph->pixel_width = width;
1751 glyph->voffset = it->voffset;
1752 glyph->type = STRETCH_GLYPH;
1753 glyph->multibyte_p = it->multibyte_p;
1754 glyph->left_box_line_p = it->start_of_box_run_p;
1755 glyph->right_box_line_p = it->end_of_box_run_p;
1756 glyph->overlaps_vertically_p = 0;
1757 glyph->padding_p = 0;
1758 glyph->glyph_not_available_p = 0;
1759 glyph->face_id = it->face_id;
1760 glyph->u.stretch.ascent = height * ascent;
1761 glyph->u.stretch.height = height;
1762 glyph->w32_font_type = UNKNOWN_FONT;
1763 ++it->glyph_row->used[area];
1768 /* Produce a stretch glyph for iterator IT. IT->object is the value
1769 of the glyph property displayed. The value must be a list
1770 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1771 being recognized:
1773 1. `:width WIDTH' specifies that the space should be WIDTH *
1774 canonical char width wide. WIDTH may be an integer or floating
1775 point number.
1777 2. `:relative-width FACTOR' specifies that the width of the stretch
1778 should be computed from the width of the first character having the
1779 `glyph' property, and should be FACTOR times that width.
1781 3. `:align-to HPOS' specifies that the space should be wide enough
1782 to reach HPOS, a value in canonical character units.
1784 Exactly one of the above pairs must be present.
1786 4. `:height HEIGHT' specifies that the height of the stretch produced
1787 should be HEIGHT, measured in canonical character units.
1789 5. `:relative-height FACTOR' specifies that the height of the the
1790 stretch should be FACTOR times the height of the characters having
1791 the glyph property.
1793 Either none or exactly one of 4 or 5 must be present.
1795 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1796 of the stretch should be used for the ascent of the stretch.
1797 ASCENT must be in the range 0 <= ASCENT <= 100. */
1799 #define NUMVAL(X) \
1800 ((INTEGERP (X) || FLOATP (X)) \
1801 ? XFLOATINT (X) \
1802 : - 1)
1805 static void
1806 x_produce_stretch_glyph (it)
1807 struct it *it;
1809 /* (space :width WIDTH :height HEIGHT. */
1810 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1811 extern Lisp_Object QCrelative_width, QCrelative_height;
1812 extern Lisp_Object QCalign_to;
1813 Lisp_Object prop, plist;
1814 double width = 0, height = 0, ascent = 0;
1815 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1816 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1818 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1820 /* List should start with `space'. */
1821 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1822 plist = XCDR (it->object);
1824 /* Compute the width of the stretch. */
1825 if (prop = Fplist_get (plist, QCwidth),
1826 NUMVAL (prop) > 0)
1827 /* Absolute width `:width WIDTH' specified and valid. */
1828 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1829 else if (prop = Fplist_get (plist, QCrelative_width),
1830 NUMVAL (prop) > 0)
1832 /* Relative width `:relative-width FACTOR' specified and valid.
1833 Compute the width of the characters having the `glyph'
1834 property. */
1835 struct it it2;
1836 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1838 it2 = *it;
1839 if (it->multibyte_p)
1841 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1842 - IT_BYTEPOS (*it));
1843 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1845 else
1846 it2.c = *p, it2.len = 1;
1848 it2.glyph_row = NULL;
1849 it2.what = IT_CHARACTER;
1850 x_produce_glyphs (&it2);
1851 width = NUMVAL (prop) * it2.pixel_width;
1853 else if (prop = Fplist_get (plist, QCalign_to),
1854 NUMVAL (prop) > 0)
1855 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1856 else
1857 /* Nothing specified -> width defaults to canonical char width. */
1858 width = CANON_X_UNIT (it->f);
1860 /* Compute height. */
1861 if (prop = Fplist_get (plist, QCheight),
1862 NUMVAL (prop) > 0)
1863 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1864 else if (prop = Fplist_get (plist, QCrelative_height),
1865 NUMVAL (prop) > 0)
1866 height = FONT_HEIGHT (font) * NUMVAL (prop);
1867 else
1868 height = FONT_HEIGHT (font);
1870 /* Compute percentage of height used for ascent. If
1871 `:ascent ASCENT' is present and valid, use that. Otherwise,
1872 derive the ascent from the font in use. */
1873 if (prop = Fplist_get (plist, QCascent),
1874 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1875 ascent = NUMVAL (prop) / 100.0;
1876 else
1877 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
1879 if (width <= 0)
1880 width = 1;
1881 if (height <= 0)
1882 height = 1;
1884 if (it->glyph_row)
1886 Lisp_Object object = it->stack[it->sp - 1].string;
1887 if (!STRINGP (object))
1888 object = it->w->buffer;
1889 x_append_stretch_glyph (it, object, width, height, ascent);
1892 it->pixel_width = width;
1893 it->ascent = it->phys_ascent = height * ascent;
1894 it->descent = it->phys_descent = height - it->ascent;
1895 it->nglyphs = 1;
1897 if (face->box != FACE_NO_BOX)
1899 it->ascent += face->box_line_width;
1900 it->descent += face->box_line_width;
1902 if (it->start_of_box_run_p)
1903 it->pixel_width += face->box_line_width;
1904 if (it->end_of_box_run_p)
1905 it->pixel_width += face->box_line_width;
1908 take_vertical_position_into_account (it);
1911 /* Return proper value to be used as baseline offset of font that has
1912 ASCENT and DESCENT to draw characters by the font at the vertical
1913 center of the line of frame F.
1915 Here, out task is to find the value of BOFF in the following figure;
1917 -------------------------+-----------+-
1918 -+-+---------+-+ | |
1919 | | | | | |
1920 | | | | F_ASCENT F_HEIGHT
1921 | | | ASCENT | |
1922 HEIGHT | | | | |
1923 | | |-|-+------+-----------|------- baseline
1924 | | | | BOFF | |
1925 | |---------|-+-+ | |
1926 | | | DESCENT | |
1927 -+-+---------+-+ F_DESCENT |
1928 -------------------------+-----------+-
1930 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1931 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1932 DESCENT = FONT->descent
1933 HEIGHT = FONT_HEIGHT (FONT)
1934 F_DESCENT = (F->output_data.x->font->descent
1935 - F->output_data.x->baseline_offset)
1936 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1939 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1940 (FONT_DESCENT (FONT) \
1941 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1942 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1943 - (FONT_DESCENT (FRAME_FONT (F)) - FRAME_BASELINE_OFFSET (F)))
1945 /* Produce glyphs/get display metrics for the display element IT is
1946 loaded with. See the description of struct display_iterator in
1947 dispextern.h for an overview of struct display_iterator. */
1949 static void
1950 x_produce_glyphs (it)
1951 struct it *it;
1953 it->glyph_not_available_p = 0;
1955 if (it->what == IT_CHARACTER)
1957 wchar_t char2b;
1958 XFontStruct *font;
1959 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1960 XCharStruct *pcm;
1961 int font_not_found_p;
1962 struct font_info *font_info;
1963 int boff; /* baseline offset */
1964 /* We may change it->multibyte_p upon unibyte<->multibyte
1965 conversion. So, save the current value now and restore it
1966 later.
1968 Note: It seems that we don't have to record multibyte_p in
1969 struct glyph because the character code itself tells if or
1970 not the character is multibyte. Thus, in the future, we must
1971 consider eliminating the field `multibyte_p' in the struct
1972 glyph.
1974 int saved_multibyte_p = it->multibyte_p;
1976 /* Maybe translate single-byte characters to multibyte, or the
1977 other way. */
1978 it->char_to_display = it->c;
1979 if (!ASCII_BYTE_P (it->c))
1981 if (unibyte_display_via_language_environment
1982 && SINGLE_BYTE_CHAR_P (it->c)
1983 && (it->c >= 0240
1984 || !NILP (Vnonascii_translation_table)))
1986 it->char_to_display = unibyte_char_to_multibyte (it->c);
1987 it->multibyte_p = 1;
1988 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1989 face = FACE_FROM_ID (it->f, it->face_id);
1991 else if (!SINGLE_BYTE_CHAR_P (it->c)
1992 && !it->multibyte_p)
1994 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1995 it->multibyte_p = 0;
1996 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1997 face = FACE_FROM_ID (it->f, it->face_id);
2001 /* Get font to use. Encode IT->char_to_display. */
2002 x_get_char_face_and_encoding (it->f, it->char_to_display,
2003 it->face_id, &char2b,
2004 it->multibyte_p);
2005 font = face->font;
2007 /* When no suitable font found, use the default font. */
2008 font_not_found_p = font == NULL;
2009 if (font_not_found_p)
2011 font = FRAME_FONT (it->f);
2012 boff = it->f->output_data.w32->baseline_offset;
2013 font_info = NULL;
2015 else
2017 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2018 boff = font_info->baseline_offset;
2019 if (font_info->vertical_centering)
2020 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2023 if (it->char_to_display >= ' '
2024 && (!it->multibyte_p || it->char_to_display < 128))
2026 /* Either unibyte or ASCII. */
2027 int stretched_p;
2029 it->nglyphs = 1;
2031 pcm = w32_per_char_metric (font, &char2b,
2032 font->bdf ? BDF_1D_FONT : ANSI_FONT);
2033 it->ascent = FONT_BASE (font) + boff;
2034 it->descent = FONT_DESCENT (font) - boff;
2036 if (pcm)
2038 it->phys_ascent = pcm->ascent + boff;
2039 it->phys_descent = pcm->descent - boff;
2040 it->pixel_width = pcm->width;
2042 else
2044 it->glyph_not_available_p = 1;
2045 it->phys_ascent = FONT_BASE (font) + boff;
2046 it->phys_descent = FONT_DESCENT (font) - boff;
2047 it->pixel_width = FONT_WIDTH (font);
2050 /* If this is a space inside a region of text with
2051 `space-width' property, change its width. */
2052 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
2053 if (stretched_p)
2054 it->pixel_width *= XFLOATINT (it->space_width);
2056 /* If face has a box, add the box thickness to the character
2057 height. If character has a box line to the left and/or
2058 right, add the box line width to the character's width. */
2059 if (face->box != FACE_NO_BOX)
2061 int thick = face->box_line_width;
2063 it->ascent += thick;
2064 it->descent += thick;
2066 if (it->start_of_box_run_p)
2067 it->pixel_width += thick;
2068 if (it->end_of_box_run_p)
2069 it->pixel_width += thick;
2072 /* If face has an overline, add the height of the overline
2073 (1 pixel) and a 1 pixel margin to the character height. */
2074 if (face->overline_p)
2075 it->ascent += 2;
2077 take_vertical_position_into_account (it);
2079 /* If we have to actually produce glyphs, do it. */
2080 if (it->glyph_row)
2082 if (stretched_p)
2084 /* Translate a space with a `space-width' property
2085 into a stretch glyph. */
2086 double ascent = (double) FONT_BASE (font)
2087 / FONT_HEIGHT (font);
2088 x_append_stretch_glyph (it, it->object, it->pixel_width,
2089 it->ascent + it->descent, ascent);
2091 else
2092 x_append_glyph (it);
2094 /* If characters with lbearing or rbearing are displayed
2095 in this line, record that fact in a flag of the
2096 glyph row. This is used to optimize X output code. */
2097 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
2098 it->glyph_row->contains_overlapping_glyphs_p = 1;
2101 else if (it->char_to_display == '\n')
2103 /* A newline has no width but we need the height of the line. */
2104 it->pixel_width = 0;
2105 it->nglyphs = 0;
2106 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2107 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2109 if (face->box != FACE_NO_BOX)
2111 int thick = face->box_line_width;
2112 it->ascent += thick;
2113 it->descent += thick;
2116 else if (it->char_to_display == '\t')
2118 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
2119 int x = it->current_x + it->continuation_lines_width;
2120 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
2122 /* If the distance from the current position to the next tab
2123 stop is less than a canonical character width, use the
2124 tab stop after that. */
2125 if (next_tab_x - x < CANON_X_UNIT (it->f))
2126 next_tab_x += tab_width;
2128 it->pixel_width = next_tab_x - x;
2129 it->nglyphs = 1;
2130 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
2131 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
2133 if (it->glyph_row)
2135 double ascent = (double) it->ascent / (it->ascent + it->descent);
2136 x_append_stretch_glyph (it, it->object, it->pixel_width,
2137 it->ascent + it->descent, ascent);
2140 else
2142 /* A multi-byte character.
2143 If we found a font, this font should give us the right
2144 metrics. If we didn't find a font, use the frame's
2145 default font and calculate the width of the character
2146 from the charset width; this is what old redisplay code
2147 did. */
2148 enum w32_char_font_type type;
2150 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2151 type = BDF_1D_FONT;
2152 else if (font->bdf)
2153 type = BDF_2D_FONT;
2154 else
2155 type = UNICODE_FONT;
2157 pcm = w32_per_char_metric (font, &char2b, type);
2159 if (font_not_found_p || !pcm)
2161 int charset = CHAR_CHARSET (it->char_to_display);
2163 it->glyph_not_available_p = 1;
2164 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2165 * CHARSET_WIDTH (charset));
2166 it->phys_ascent = FONT_BASE (font) + boff;
2167 it->phys_descent = FONT_DESCENT (font) - boff;
2169 else
2171 it->pixel_width = pcm->width;
2172 it->phys_ascent = pcm->ascent + boff;
2173 it->phys_descent = pcm->descent - boff;
2174 if (it->glyph_row
2175 && (pcm->lbearing < 0
2176 || pcm->rbearing > pcm->width))
2177 it->glyph_row->contains_overlapping_glyphs_p = 1;
2179 it->nglyphs = 1;
2180 it->ascent = FONT_BASE (font) + boff;
2181 it->descent = FONT_DESCENT (font) - boff;
2183 if (face->box != FACE_NO_BOX)
2185 int thick = face->box_line_width;
2186 it->ascent += thick;
2187 it->descent += thick;
2189 if (it->start_of_box_run_p)
2190 it->pixel_width += thick;
2191 if (it->end_of_box_run_p)
2192 it->pixel_width += thick;
2195 /* If face has an overline, add the height of the overline
2196 (1 pixel) and a 1 pixel margin to the character height. */
2197 if (face->overline_p)
2198 it->ascent += 2;
2200 take_vertical_position_into_account (it);
2202 if (it->glyph_row)
2203 x_append_glyph (it);
2205 it->multibyte_p = saved_multibyte_p;
2207 else if (it->what == IT_COMPOSITION)
2209 /* Note: A composition is represented as one glyph in the
2210 glyph matrix. There are no padding glyphs. */
2211 wchar_t char2b;
2212 XFontStruct *font;
2213 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2214 XCharStruct *pcm;
2215 int font_not_found_p;
2216 struct font_info *font_info;
2217 int boff; /* baseline offset */
2218 struct composition *cmp = composition_table[it->cmp_id];
2220 /* Maybe translate single-byte characters to multibyte. */
2221 it->char_to_display = it->c;
2222 if (unibyte_display_via_language_environment
2223 && SINGLE_BYTE_CHAR_P (it->c)
2224 && (it->c >= 0240
2225 || (it->c >= 0200
2226 && !NILP (Vnonascii_translation_table))))
2228 it->char_to_display = unibyte_char_to_multibyte (it->c);
2231 /* Get face and font to use. Encode IT->char_to_display. */
2232 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2233 face = FACE_FROM_ID (it->f, it->face_id);
2234 x_get_char_face_and_encoding (it->f, it->char_to_display,
2235 it->face_id, &char2b, it->multibyte_p);
2236 font = face->font;
2238 /* When no suitable font found, use the default font. */
2239 font_not_found_p = font == NULL;
2240 if (font_not_found_p)
2242 font = FRAME_FONT (it->f);
2243 boff = it->f->output_data.w32->baseline_offset;
2244 font_info = NULL;
2246 else
2248 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2249 boff = font_info->baseline_offset;
2250 if (font_info->vertical_centering)
2251 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2254 /* There are no padding glyphs, so there is only one glyph to
2255 produce for the composition. Important is that pixel_width,
2256 ascent and descent are the values of what is drawn by
2257 draw_glyphs (i.e. the values of the overall glyphs composed). */
2258 it->nglyphs = 1;
2260 /* If we have not yet calculated pixel size data of glyphs of
2261 the composition for the current face font, calculate them
2262 now. Theoretically, we have to check all fonts for the
2263 glyphs, but that requires much time and memory space. So,
2264 here we check only the font of the first glyph. This leads
2265 to incorrect display very rarely, and C-l (recenter) can
2266 correct the display anyway. */
2267 if (cmp->font != (void *) font)
2269 /* Ascent and descent of the font of the first character of
2270 this composition (adjusted by baseline offset). Ascent
2271 and descent of overall glyphs should not be less than
2272 them respectively. */
2273 int font_ascent = FONT_BASE (font) + boff;
2274 int font_descent = FONT_DESCENT (font) - boff;
2275 /* Bounding box of the overall glyphs. */
2276 int leftmost, rightmost, lowest, highest;
2277 int i, width, ascent, descent;
2278 enum w32_char_font_type font_type;
2280 cmp->font = (void *) font;
2282 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (it->c)) == 1)
2283 font_type = BDF_1D_FONT;
2284 else if (font->bdf)
2285 font_type = BDF_2D_FONT;
2286 else
2287 font_type = UNICODE_FONT;
2289 /* Initialize the bounding box. */
2290 if (font_info
2291 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
2293 width = pcm->width;
2294 ascent = pcm->ascent;
2295 descent = pcm->descent;
2297 else
2299 width = FONT_WIDTH (font);
2300 ascent = FONT_BASE (font);
2301 descent = FONT_DESCENT (font);
2304 rightmost = width;
2305 lowest = - descent + boff;
2306 highest = ascent + boff;
2307 leftmost = 0;
2309 if (font_info
2310 && font_info->default_ascent
2311 && CHAR_TABLE_P (Vuse_default_ascent)
2312 && !NILP (Faref (Vuse_default_ascent,
2313 make_number (it->char_to_display))))
2314 highest = font_info->default_ascent + boff;
2316 /* Draw the first glyph at the normal position. It may be
2317 shifted to right later if some other glyphs are drawn at
2318 the left. */
2319 cmp->offsets[0] = 0;
2320 cmp->offsets[1] = boff;
2322 /* Set cmp->offsets for the remaining glyphs. */
2323 for (i = 1; i < cmp->glyph_len; i++)
2325 int left, right, btm, top;
2326 int ch = COMPOSITION_GLYPH (cmp, i);
2327 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2329 face = FACE_FROM_ID (it->f, face_id);
2330 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2331 it->multibyte_p);
2332 font = face->font;
2333 if (font == NULL)
2335 font = FRAME_FONT (it->f);
2336 boff = it->f->output_data.w32->baseline_offset;
2337 font_info = NULL;
2339 else
2341 font_info
2342 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2343 boff = font_info->baseline_offset;
2344 if (font_info->vertical_centering)
2345 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2348 if (font->bdf && CHARSET_DIMENSION (CHAR_CHARSET (ch)) == 1)
2349 font_type = BDF_1D_FONT;
2350 else if (font->bdf)
2351 font_type = BDF_2D_FONT;
2352 else
2353 font_type = UNICODE_FONT;
2355 if (font_info
2356 && (pcm = w32_per_char_metric (font, &char2b, font_type)))
2358 width = pcm->width;
2359 ascent = pcm->ascent;
2360 descent = pcm->descent;
2362 else
2364 width = FONT_WIDTH (font);
2365 ascent = 1;
2366 descent = 0;
2369 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2371 /* Relative composition with or without
2372 alternate chars. */
2373 left = (leftmost + rightmost - width) / 2;
2374 btm = - descent + boff;
2375 if (font_info && font_info->relative_compose
2376 && (! CHAR_TABLE_P (Vignore_relative_composition)
2377 || NILP (Faref (Vignore_relative_composition,
2378 make_number (ch)))))
2381 if (- descent >= font_info->relative_compose)
2382 /* One extra pixel between two glyphs. */
2383 btm = highest + 1;
2384 else if (ascent <= 0)
2385 /* One extra pixel between two glyphs. */
2386 btm = lowest - 1 - ascent - descent;
2389 else
2391 /* A composition rule is specified by an integer
2392 value that encodes global and new reference
2393 points (GREF and NREF). GREF and NREF are
2394 specified by numbers as below:
2396 0---1---2 -- ascent
2400 9--10--11 -- center
2402 ---3---4---5--- baseline
2404 6---7---8 -- descent
2406 int rule = COMPOSITION_RULE (cmp, i);
2407 int gref, nref, grefx, grefy, nrefx, nrefy;
2409 COMPOSITION_DECODE_RULE (rule, gref, nref);
2410 grefx = gref % 3, nrefx = nref % 3;
2411 grefy = gref / 3, nrefy = nref / 3;
2413 left = (leftmost
2414 + grefx * (rightmost - leftmost) / 2
2415 - nrefx * width / 2);
2416 btm = ((grefy == 0 ? highest
2417 : grefy == 1 ? 0
2418 : grefy == 2 ? lowest
2419 : (highest + lowest) / 2)
2420 - (nrefy == 0 ? ascent + descent
2421 : nrefy == 1 ? descent - boff
2422 : nrefy == 2 ? 0
2423 : (ascent + descent) / 2));
2426 cmp->offsets[i * 2] = left;
2427 cmp->offsets[i * 2 + 1] = btm + descent;
2429 /* Update the bounding box of the overall glyphs. */
2430 right = left + width;
2431 top = btm + descent + ascent;
2432 if (left < leftmost)
2433 leftmost = left;
2434 if (right > rightmost)
2435 rightmost = right;
2436 if (top > highest)
2437 highest = top;
2438 if (btm < lowest)
2439 lowest = btm;
2442 /* If there are glyphs whose x-offsets are negative,
2443 shift all glyphs to the right and make all x-offsets
2444 non-negative. */
2445 if (leftmost < 0)
2447 for (i = 0; i < cmp->glyph_len; i++)
2448 cmp->offsets[i * 2] -= leftmost;
2449 rightmost -= leftmost;
2452 cmp->pixel_width = rightmost;
2453 cmp->ascent = highest;
2454 cmp->descent = - lowest;
2455 if (cmp->ascent < font_ascent)
2456 cmp->ascent = font_ascent;
2457 if (cmp->descent < font_descent)
2458 cmp->descent = font_descent;
2461 it->pixel_width = cmp->pixel_width;
2462 it->ascent = it->phys_ascent = cmp->ascent;
2463 it->descent = it->phys_descent = cmp->descent;
2465 if (face->box != FACE_NO_BOX)
2467 int thick = face->box_line_width;
2468 it->ascent += thick;
2469 it->descent += thick;
2471 if (it->start_of_box_run_p)
2472 it->pixel_width += thick;
2473 if (it->end_of_box_run_p)
2474 it->pixel_width += thick;
2477 /* If face has an overline, add the height of the overline
2478 (1 pixel) and a 1 pixel margin to the character height. */
2479 if (face->overline_p)
2480 it->ascent += 2;
2482 take_vertical_position_into_account (it);
2484 if (it->glyph_row)
2485 x_append_composite_glyph (it);
2487 else if (it->what == IT_IMAGE)
2488 x_produce_image_glyph (it);
2489 else if (it->what == IT_STRETCH)
2490 x_produce_stretch_glyph (it);
2492 /* Accumulate dimensions. */
2493 xassert (it->ascent >= 0 && it->descent > 0);
2494 if (it->area == TEXT_AREA)
2495 it->current_x += it->pixel_width;
2497 it->descent += it->extra_line_spacing;
2499 it->max_ascent = max (it->max_ascent, it->ascent);
2500 it->max_descent = max (it->max_descent, it->descent);
2501 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2502 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2506 /* Estimate the pixel height of the mode or top line on frame F.
2507 FACE_ID specifies what line's height to estimate. */
2510 x_estimate_mode_line_height (f, face_id)
2511 struct frame *f;
2512 enum face_id face_id;
2514 int height = FONT_HEIGHT (FRAME_FONT (f));
2516 /* This function is called so early when Emacs starts that the face
2517 cache and mode line face are not yet initialized. */
2518 if (FRAME_FACE_CACHE (f))
2520 struct face *face = FACE_FROM_ID (f, face_id);
2521 if (face)
2523 if (face->font)
2524 height = FONT_HEIGHT (face->font);
2525 height += 2 * face->box_line_width;
2530 return height;
2535 BOOL
2536 w32_use_unicode_for_codepage (codepage)
2537 int codepage;
2539 /* If the current codepage is supported, use Unicode for output. */
2540 return (w32_enable_unicode_output
2541 && codepage != CP_8BIT
2542 && (codepage == CP_UNICODE || IsValidCodePage (codepage)));
2546 /***********************************************************************
2547 Glyph display
2548 ***********************************************************************/
2550 /* A sequence of glyphs to be drawn in the same face.
2552 This data structure is not really completely X specific, so it
2553 could possibly, at least partially, be useful for other systems. It
2554 is currently not part of the external redisplay interface because
2555 it's not clear what other systems will need. */
2557 struct glyph_string
2559 /* X-origin of the string. */
2560 int x;
2562 /* Y-origin and y-position of the base line of this string. */
2563 int y, ybase;
2565 /* The width of the string, not including a face extension. */
2566 int width;
2568 /* The width of the string, including a face extension. */
2569 int background_width;
2571 /* The height of this string. This is the height of the line this
2572 string is drawn in, and can be different from the height of the
2573 font the string is drawn in. */
2574 int height;
2576 /* Number of pixels this string overwrites in front of its x-origin.
2577 This number is zero if the string has an lbearing >= 0; it is
2578 -lbearing, if the string has an lbearing < 0. */
2579 int left_overhang;
2581 /* Number of pixels this string overwrites past its right-most
2582 nominal x-position, i.e. x + width. Zero if the string's
2583 rbearing is <= its nominal width, rbearing - width otherwise. */
2584 int right_overhang;
2586 /* The frame on which the glyph string is drawn. */
2587 struct frame *f;
2589 /* The window on which the glyph string is drawn. */
2590 struct window *w;
2592 /* X display and window for convenience. */
2593 Window window;
2595 /* The glyph row for which this string was built. It determines the
2596 y-origin and height of the string. */
2597 struct glyph_row *row;
2599 /* The area within row. */
2600 enum glyph_row_area area;
2602 /* Characters to be drawn, and number of characters. */
2603 wchar_t *char2b;
2604 int nchars;
2606 /* A face-override for drawing cursors, mouse face and similar. */
2607 enum draw_glyphs_face hl;
2609 /* Face in which this string is to be drawn. */
2610 struct face *face;
2612 /* Font in which this string is to be drawn. */
2613 XFontStruct *font;
2615 /* Font info for this string. */
2616 struct font_info *font_info;
2618 /* Non-null means this string describes (part of) a composition.
2619 All characters from char2b are drawn composed. */
2620 struct composition *cmp;
2622 /* Index of this glyph string's first character in the glyph
2623 definition of CMP. If this is zero, this glyph string describes
2624 the first character of a composition. */
2625 int gidx;
2627 /* 1 means this glyph strings face has to be drawn to the right end
2628 of the window's drawing area. */
2629 unsigned extends_to_end_of_line_p : 1;
2631 /* 1 means the background of this string has been drawn. */
2632 unsigned background_filled_p : 1;
2634 /* 1 means glyph string must be drawn with 16-bit functions. */
2635 unsigned two_byte_p : 1;
2637 /* 1 means that the original font determined for drawing this glyph
2638 string could not be loaded. The member `font' has been set to
2639 the frame's default font in this case. */
2640 unsigned font_not_found_p : 1;
2642 /* 1 means that the face in which this glyph string is drawn has a
2643 stipple pattern. */
2644 unsigned stippled_p : 1;
2646 /* 1 means only the foreground of this glyph string must be drawn,
2647 and we should use the physical height of the line this glyph
2648 string appears in as clip rect. */
2649 unsigned for_overlaps_p : 1;
2651 /* The GC to use for drawing this glyph string. */
2652 XGCValues *gc;
2654 HDC hdc;
2656 /* A pointer to the first glyph in the string. This glyph
2657 corresponds to char2b[0]. Needed to draw rectangles if
2658 font_not_found_p is 1. */
2659 struct glyph *first_glyph;
2661 /* Image, if any. */
2662 struct image *img;
2664 struct glyph_string *next, *prev;
2668 /* Encapsulate the different ways of displaying text under W32. */
2670 void W32_TEXTOUT (s, x, y,chars,nchars)
2671 struct glyph_string * s;
2672 int x, y;
2673 wchar_t * chars;
2674 int nchars;
2676 int charset_dim = w32_font_is_double_byte (s->gc->font) ? 2 : 1;
2677 if (s->gc->font->bdf)
2678 w32_BDF_TextOut (s->gc->font->bdf, s->hdc,
2679 x, y, (char *) chars, charset_dim,
2680 nchars * charset_dim, 0);
2681 else if (s->first_glyph->w32_font_type == UNICODE_FONT)
2682 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
2683 else
2684 ExtTextOut (s->hdc, x, y, 0, NULL, (char *) chars,
2685 nchars * charset_dim, NULL);
2688 #if 0
2690 static void
2691 x_dump_glyph_string (s)
2692 struct glyph_string *s;
2694 fprintf (stderr, "glyph string\n");
2695 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2696 s->x, s->y, s->width, s->height);
2697 fprintf (stderr, " ybase = %d\n", s->ybase);
2698 fprintf (stderr, " hl = %d\n", s->hl);
2699 fprintf (stderr, " left overhang = %d, right = %d\n",
2700 s->left_overhang, s->right_overhang);
2701 fprintf (stderr, " nchars = %d\n", s->nchars);
2702 fprintf (stderr, " extends to end of line = %d\n",
2703 s->extends_to_end_of_line_p);
2704 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2705 fprintf (stderr, " bg width = %d\n", s->background_width);
2708 #endif /* GLYPH_DEBUG */
2712 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2713 struct glyph_string **,
2714 struct glyph_string *,
2715 struct glyph_string *));
2716 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2717 struct glyph_string **,
2718 struct glyph_string *,
2719 struct glyph_string *));
2720 static void x_append_glyph_string P_ ((struct glyph_string **,
2721 struct glyph_string **,
2722 struct glyph_string *));
2723 static int x_left_overwritten P_ ((struct glyph_string *));
2724 static int x_left_overwriting P_ ((struct glyph_string *));
2725 static int x_right_overwritten P_ ((struct glyph_string *));
2726 static int x_right_overwriting P_ ((struct glyph_string *));
2727 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int,
2728 int, int));
2729 static void w32_init_glyph_string P_ ((struct glyph_string *, HDC hdc,
2730 wchar_t *, struct window *,
2731 struct glyph_row *,
2732 enum glyph_row_area, int,
2733 enum draw_glyphs_face));
2734 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2735 enum glyph_row_area, int, int,
2736 enum draw_glyphs_face, int *, int *, int));
2737 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2738 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2739 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2740 int));
2741 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2742 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2743 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2744 static void x_draw_glyph_string P_ ((struct glyph_string *));
2745 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2746 static void x_set_cursor_gc P_ ((struct glyph_string *));
2747 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2748 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2749 static void w32_get_glyph_overhangs P_ ((HDC hdc, struct glyph *,
2750 struct frame *,
2751 int *, int *));
2752 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2753 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
2754 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
2755 double, int, COLORREF));
2756 static void x_setup_relief_colors P_ ((struct glyph_string *));
2757 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2758 static void x_draw_image_relief P_ ((struct glyph_string *));
2759 static void x_draw_image_foreground P_ ((struct glyph_string *));
2760 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
2761 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2762 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2763 int, int, int));
2764 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2765 int, int, int, int, RECT *));
2766 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2767 int, int, int, RECT *));
2768 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2769 enum glyph_row_area));
2772 /* Append the list of glyph strings with head H and tail T to the list
2773 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2775 static INLINE void
2776 x_append_glyph_string_lists (head, tail, h, t)
2777 struct glyph_string **head, **tail;
2778 struct glyph_string *h, *t;
2780 if (h)
2782 if (*head)
2783 (*tail)->next = h;
2784 else
2785 *head = h;
2786 h->prev = *tail;
2787 *tail = t;
2792 /* Prepend the list of glyph strings with head H and tail T to the
2793 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2794 result. */
2796 static INLINE void
2797 x_prepend_glyph_string_lists (head, tail, h, t)
2798 struct glyph_string **head, **tail;
2799 struct glyph_string *h, *t;
2801 if (h)
2803 if (*head)
2804 (*head)->prev = t;
2805 else
2806 *tail = t;
2807 t->next = *head;
2808 *head = h;
2813 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2814 Set *HEAD and *TAIL to the resulting list. */
2816 static INLINE void
2817 x_append_glyph_string (head, tail, s)
2818 struct glyph_string **head, **tail;
2819 struct glyph_string *s;
2821 s->next = s->prev = NULL;
2822 x_append_glyph_string_lists (head, tail, s, s);
2826 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2827 face. */
2829 static void
2830 x_set_cursor_gc (s)
2831 struct glyph_string *s;
2833 if (s->font == FRAME_FONT (s->f)
2834 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2835 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2836 && !s->cmp)
2837 s->gc = s->f->output_data.w32->cursor_gc;
2838 else
2840 /* Cursor on non-default face: must merge. */
2841 XGCValues xgcv;
2842 unsigned long mask;
2844 xgcv.background = s->f->output_data.w32->cursor_pixel;
2845 xgcv.foreground = s->face->background;
2847 /* If the glyph would be invisible, try a different foreground. */
2848 if (xgcv.foreground == xgcv.background)
2849 xgcv.foreground = s->face->foreground;
2850 if (xgcv.foreground == xgcv.background)
2851 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
2852 if (xgcv.foreground == xgcv.background)
2853 xgcv.foreground = s->face->foreground;
2855 /* Make sure the cursor is distinct from text in this face. */
2856 if (xgcv.background == s->face->background
2857 && xgcv.foreground == s->face->foreground)
2859 xgcv.background = s->face->foreground;
2860 xgcv.foreground = s->face->background;
2863 IF_DEBUG (x_check_font (s->f, s->font));
2864 xgcv.font = s->font;
2865 mask = GCForeground | GCBackground | GCFont;
2867 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2868 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2869 mask, &xgcv);
2870 else
2871 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2872 = XCreateGC (NULL, s->window, mask, &xgcv);
2874 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2879 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2881 static void
2882 x_set_mouse_face_gc (s)
2883 struct glyph_string *s;
2885 int face_id;
2886 struct face *face;
2888 /* What face has to be used for the mouse face? */
2889 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
2890 face = FACE_FROM_ID (s->f, face_id);
2891 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2892 s->face = FACE_FROM_ID (s->f, face_id);
2893 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2895 /* If font in this face is same as S->font, use it. */
2896 if (s->font == s->face->font)
2897 s->gc = s->face->gc;
2898 else
2900 /* Otherwise construct scratch_cursor_gc with values from FACE
2901 but font FONT. */
2902 XGCValues xgcv;
2903 unsigned long mask;
2905 xgcv.background = s->face->background;
2906 xgcv.foreground = s->face->foreground;
2907 IF_DEBUG (x_check_font (s->f, s->font));
2908 xgcv.font = s->font;
2909 mask = GCForeground | GCBackground | GCFont;
2911 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2912 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2913 mask, &xgcv);
2914 else
2915 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
2916 = XCreateGC (NULL, s->window, mask, &xgcv);
2918 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2921 xassert (s->gc != 0);
2925 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2926 Faces to use in the mode line have already been computed when the
2927 matrix was built, so there isn't much to do, here. */
2929 static INLINE void
2930 x_set_mode_line_face_gc (s)
2931 struct glyph_string *s;
2933 s->gc = s->face->gc;
2937 /* Set S->gc of glyph string S for drawing that glyph string. Set
2938 S->stippled_p to a non-zero value if the face of S has a stipple
2939 pattern. */
2941 static INLINE void
2942 x_set_glyph_string_gc (s)
2943 struct glyph_string *s;
2945 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2947 if (s->hl == DRAW_NORMAL_TEXT)
2949 s->gc = s->face->gc;
2950 s->stippled_p = s->face->stipple != 0;
2952 else if (s->hl == DRAW_INVERSE_VIDEO)
2954 x_set_mode_line_face_gc (s);
2955 s->stippled_p = s->face->stipple != 0;
2957 else if (s->hl == DRAW_CURSOR)
2959 x_set_cursor_gc (s);
2960 s->stippled_p = 0;
2962 else if (s->hl == DRAW_MOUSE_FACE)
2964 x_set_mouse_face_gc (s);
2965 s->stippled_p = s->face->stipple != 0;
2967 else if (s->hl == DRAW_IMAGE_RAISED
2968 || s->hl == DRAW_IMAGE_SUNKEN)
2970 s->gc = s->face->gc;
2971 s->stippled_p = s->face->stipple != 0;
2973 else
2975 s->gc = s->face->gc;
2976 s->stippled_p = s->face->stipple != 0;
2979 /* GC must have been set. */
2980 xassert (s->gc != 0);
2984 /* Return in *R the clipping rectangle for glyph string S. */
2986 static void
2987 w32_get_glyph_string_clip_rect (s, r)
2988 struct glyph_string *s;
2989 RECT *r;
2991 int r_height, r_width;
2993 if (s->row->full_width_p)
2995 /* Draw full-width. X coordinates are relative to S->w->left. */
2996 int canon_x = CANON_X_UNIT (s->f);
2998 r->left = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2999 r_width = XFASTINT (s->w->width) * canon_x;
3001 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
3003 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
3004 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
3005 r->left -= width;
3008 r->left += FRAME_INTERNAL_BORDER_WIDTH (s->f);
3010 /* Unless displaying a mode or menu bar line, which are always
3011 fully visible, clip to the visible part of the row. */
3012 if (s->w->pseudo_window_p)
3013 r_height = s->row->visible_height;
3014 else
3015 r_height = s->height;
3017 else
3019 /* This is a text line that may be partially visible. */
3020 r->left = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
3021 r_width = window_box_width (s->w, s->area);
3022 r_height = s->row->visible_height;
3025 /* Don't use S->y for clipping because it doesn't take partially
3026 visible lines into account. For example, it can be negative for
3027 partially visible lines at the top of a window. */
3028 if (!s->row->full_width_p
3029 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
3030 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3031 else
3032 r->top = max (0, s->row->y);
3034 /* If drawing a tool-bar window, draw it over the internal border
3035 at the top of the window. */
3036 if (s->w == XWINDOW (s->f->tool_bar_window))
3037 r->top -= s->f->output_data.w32->internal_border_width;
3039 /* If S draws overlapping rows, it's sufficient to use the top and
3040 bottom of the window for clipping because this glyph string
3041 intentionally draws over other lines. */
3042 if (s->for_overlaps_p)
3044 r->top = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
3045 r_height = window_text_bottom_y (s->w) - r->top;
3048 r->top = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->top);
3050 r->bottom = r->top + r_height;
3051 r->right = r->left + r_width;
3055 /* Set clipping for output of glyph string S. S may be part of a mode
3056 line or menu if we don't have X toolkit support. */
3058 static INLINE void
3059 x_set_glyph_string_clipping (s)
3060 struct glyph_string *s;
3062 RECT r;
3063 w32_get_glyph_string_clip_rect (s, &r);
3064 w32_set_clip_rectangle (s->hdc, &r);
3068 /* Compute left and right overhang of glyph string S. If S is a glyph
3069 string for a composition, assume overhangs don't exist. */
3071 static INLINE void
3072 x_compute_glyph_string_overhangs (s)
3073 struct glyph_string *s;
3075 /* TODO: Windows does not appear to have a method for
3076 getting this info without getting the ABC widths for each
3077 individual character and working it out manually. */
3081 /* Compute overhangs and x-positions for glyph string S and its
3082 predecessors, or successors. X is the starting x-position for S.
3083 BACKWARD_P non-zero means process predecessors. */
3085 static void
3086 x_compute_overhangs_and_x (s, x, backward_p)
3087 struct glyph_string *s;
3088 int x;
3089 int backward_p;
3091 if (backward_p)
3093 while (s)
3095 x_compute_glyph_string_overhangs (s);
3096 x -= s->width;
3097 s->x = x;
3098 s = s->prev;
3101 else
3103 while (s)
3105 x_compute_glyph_string_overhangs (s);
3106 s->x = x;
3107 x += s->width;
3108 s = s->next;
3114 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
3115 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
3116 assumed to be zero. */
3118 static void
3119 w32_get_glyph_overhangs (hdc, glyph, f, left, right)
3120 HDC hdc;
3121 struct glyph *glyph;
3122 struct frame *f;
3123 int *left, *right;
3125 int c;
3127 *left = *right = 0;
3129 if (glyph->type == CHAR_GLYPH)
3131 XFontStruct *font;
3132 struct face *face;
3133 wchar_t char2b;
3134 XCharStruct *pcm;
3136 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
3137 font = face->font;
3139 if (font
3140 && (pcm = w32_per_char_metric (font, &char2b,
3141 glyph->w32_font_type)))
3143 if (pcm->rbearing > pcm->width)
3144 *right = pcm->rbearing - pcm->width;
3145 if (pcm->lbearing < 0)
3146 *left = -pcm->lbearing;
3152 static void
3153 x_get_glyph_overhangs (glyph, f, left, right)
3154 struct glyph *glyph;
3155 struct frame *f;
3156 int *left, *right;
3158 HDC hdc = get_frame_dc (f);
3159 /* Convert to unicode! */
3160 w32_get_glyph_overhangs (hdc, glyph, f, left, right);
3161 release_frame_dc (f, hdc);
3165 /* Return the index of the first glyph preceding glyph string S that
3166 is overwritten by S because of S's left overhang. Value is -1
3167 if no glyphs are overwritten. */
3169 static int
3170 x_left_overwritten (s)
3171 struct glyph_string *s;
3173 int k;
3175 if (s->left_overhang)
3177 int x = 0, i;
3178 struct glyph *glyphs = s->row->glyphs[s->area];
3179 int first = s->first_glyph - glyphs;
3181 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
3182 x -= glyphs[i].pixel_width;
3184 k = i + 1;
3186 else
3187 k = -1;
3189 return k;
3193 /* Return the index of the first glyph preceding glyph string S that
3194 is overwriting S because of its right overhang. Value is -1 if no
3195 glyph in front of S overwrites S. */
3197 static int
3198 x_left_overwriting (s)
3199 struct glyph_string *s;
3201 int i, k, x;
3202 struct glyph *glyphs = s->row->glyphs[s->area];
3203 int first = s->first_glyph - glyphs;
3205 k = -1;
3206 x = 0;
3207 for (i = first - 1; i >= 0; --i)
3209 int left, right;
3210 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
3211 if (x + right > 0)
3212 k = i;
3213 x -= glyphs[i].pixel_width;
3216 return k;
3220 /* Return the index of the last glyph following glyph string S that is
3221 not overwritten by S because of S's right overhang. Value is -1 if
3222 no such glyph is found. */
3224 static int
3225 x_right_overwritten (s)
3226 struct glyph_string *s;
3228 int k = -1;
3230 if (s->right_overhang)
3232 int x = 0, i;
3233 struct glyph *glyphs = s->row->glyphs[s->area];
3234 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3235 int end = s->row->used[s->area];
3237 for (i = first; i < end && s->right_overhang > x; ++i)
3238 x += glyphs[i].pixel_width;
3240 k = i;
3243 return k;
3247 /* Return the index of the last glyph following glyph string S that
3248 overwrites S because of its left overhang. Value is negative
3249 if no such glyph is found. */
3251 static int
3252 x_right_overwriting (s)
3253 struct glyph_string *s;
3255 int i, k, x;
3256 int end = s->row->used[s->area];
3257 struct glyph *glyphs = s->row->glyphs[s->area];
3258 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3260 k = -1;
3261 x = 0;
3262 for (i = first; i < end; ++i)
3264 int left, right;
3265 w32_get_glyph_overhangs (s->hdc, glyphs + i, s->f, &left, &right);
3266 if (x - left < 0)
3267 k = i;
3268 x += glyphs[i].pixel_width;
3271 return k;
3275 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3277 static INLINE void
3278 x_clear_glyph_string_rect (s, x, y, w, h)
3279 struct glyph_string *s;
3280 int x, y, w, h;
3282 int real_x = x;
3283 int real_y = y;
3284 int real_w = w;
3285 int real_h = h;
3286 #if 0
3287 /* Take clipping into account. */
3288 if (s->gc->clip_mask == Rect)
3290 real_x = max (real_x, s->gc->clip_rectangle.left);
3291 real_y = max (real_y, s->gc->clip_rectangle.top);
3292 real_w = min (real_w, s->gc->clip_rectangle.right
3293 - s->gc->clip_rectangle.left);
3294 real_h = min (real_h, s->gc->clip_rectangle.bottom
3295 - s->gc->clip_rectangle.top);
3297 #endif
3298 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
3299 real_w, real_h);
3303 /* Draw the background of glyph_string S. If S->background_filled_p
3304 is non-zero don't draw it. FORCE_P non-zero means draw the
3305 background even if it wouldn't be drawn normally. This is used
3306 when a string preceding S draws into the background of S, or S
3307 contains the first component of a composition. */
3309 static void
3310 x_draw_glyph_string_background (s, force_p)
3311 struct glyph_string *s;
3312 int force_p;
3314 /* Nothing to do if background has already been drawn or if it
3315 shouldn't be drawn in the first place. */
3316 if (!s->background_filled_p)
3318 #if 0 /* TODO: stipple */
3319 if (s->stippled_p)
3321 /* Fill background with a stipple pattern. */
3322 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3323 XFillRectangle (s->display, s->window, s->gc, s->x,
3324 s->y + s->face->box_line_width,
3325 s->background_width,
3326 s->height - 2 * s->face->box_line_width);
3327 XSetFillStyle (s->display, s->gc, FillSolid);
3328 s->background_filled_p = 1;
3330 else
3331 #endif
3332 if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3333 || s->font_not_found_p
3334 || s->extends_to_end_of_line_p
3335 || s->font->bdf
3336 || force_p)
3338 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3339 s->background_width,
3340 s->height - 2 * s->face->box_line_width);
3341 s->background_filled_p = 1;
3347 /* Draw the foreground of glyph string S. */
3349 static void
3350 x_draw_glyph_string_foreground (s)
3351 struct glyph_string *s;
3353 int i, x;
3354 HFONT old_font;
3356 /* If first glyph of S has a left box line, start drawing the text
3357 of S to the right of that box line. */
3358 if (s->face->box != FACE_NO_BOX
3359 && s->first_glyph->left_box_line_p)
3360 x = s->x + s->face->box_line_width;
3361 else
3362 x = s->x;
3364 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
3365 SetBkMode (s->hdc, TRANSPARENT);
3366 else
3367 SetBkMode (s->hdc, OPAQUE);
3369 SetTextColor (s->hdc, s->gc->foreground);
3370 SetBkColor (s->hdc, s->gc->background);
3371 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3373 if (s->font && s->font->hfont)
3374 old_font = SelectObject (s->hdc, s->font->hfont);
3376 /* Draw characters of S as rectangles if S's font could not be
3377 loaded. */
3378 if (s->font_not_found_p)
3380 for (i = 0; i < s->nchars; ++i)
3382 struct glyph *g = s->first_glyph + i;
3384 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
3385 s->height - 1);
3386 x += g->pixel_width;
3389 else
3391 char *char1b = (char *) s->char2b;
3392 int boff = s->font_info->baseline_offset;
3394 if (s->font_info->vertical_centering)
3395 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3397 /* If we can use 8-bit functions, condense S->char2b. */
3398 if (!s->two_byte_p)
3399 for (i = 0; i < s->nchars; ++i)
3400 char1b[i] = BYTE2 (s->char2b[i]);
3402 /* Draw text with TextOut and friends. */
3403 W32_TEXTOUT (s, x, s->ybase - boff, s->char2b, s->nchars);
3405 if (s->font && s->font->hfont)
3406 SelectObject (s->hdc, old_font);
3409 /* Draw the foreground of composite glyph string S. */
3411 static void
3412 x_draw_composite_glyph_string_foreground (s)
3413 struct glyph_string *s;
3415 int i, x;
3416 HFONT old_font;
3418 /* If first glyph of S has a left box line, start drawing the text
3419 of S to the right of that box line. */
3420 if (s->face->box != FACE_NO_BOX
3421 && s->first_glyph->left_box_line_p)
3422 x = s->x + s->face->box_line_width;
3423 else
3424 x = s->x;
3426 /* S is a glyph string for a composition. S->gidx is the index of
3427 the first character drawn for glyphs of this composition.
3428 S->gidx == 0 means we are drawing the very first character of
3429 this composition. */
3431 SetTextColor (s->hdc, s->gc->foreground);
3432 SetBkColor (s->hdc, s->gc->background);
3433 SetBkMode (s->hdc, TRANSPARENT);
3434 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
3436 if (s->font && s->font->hfont)
3437 old_font = SelectObject (s->hdc, s->font->hfont);
3439 /* Draw a rectangle for the composition if the font for the very
3440 first character of the composition could not be loaded. */
3441 if (s->font_not_found_p)
3443 if (s->gidx == 0)
3444 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
3445 s->height - 1);
3447 else
3449 for (i = 0; i < s->nchars; i++, ++s->gidx)
3450 W32_TEXTOUT (s, x + s->cmp->offsets[s->gidx * 2],
3451 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3452 s->char2b + i, 1);
3454 if (s->font && s->font->hfont)
3455 SelectObject (s->hdc, old_font);
3459 /* Brightness beyond which a color won't have its highlight brightness
3460 boosted.
3462 Nominally, highlight colors for `3d' faces are calculated by
3463 brightening an object's color by a constant scale factor, but this
3464 doesn't yield good results for dark colors, so for colors who's
3465 brightness is less than this value (on a scale of 0-255) have to
3466 use an additional additive factor.
3468 The value here is set so that the default menu-bar/mode-line color
3469 (grey75) will not have its highlights changed at all. */
3470 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
3473 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
3474 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3475 If this produces the same color as COLOR, try a color where all RGB
3476 values have DELTA added. Return the allocated color in *COLOR.
3477 DISPLAY is the X display, CMAP is the colormap to operate on.
3478 Value is non-zero if successful. */
3480 static int
3481 w32_alloc_lighter_color (f, color, factor, delta)
3482 struct frame *f;
3483 COLORREF *color;
3484 double factor;
3485 int delta;
3487 COLORREF new;
3488 long bright;
3490 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
3491 delta /= 256;
3493 /* Change RGB values by specified FACTOR. Avoid overflow! */
3494 xassert (factor >= 0);
3495 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
3496 min (0xff, factor * GetGValue (*color)),
3497 min (0xff, factor * GetBValue (*color)));
3499 /* Calculate brightness of COLOR. */
3500 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
3501 + GetBValue (*color)) / 6;
3503 /* We only boost colors that are darker than
3504 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3505 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3506 /* Make an additive adjustment to NEW, because it's dark enough so
3507 that scaling by FACTOR alone isn't enough. */
3509 /* How far below the limit this color is (0 - 1, 1 being darker). */
3510 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3511 /* The additive adjustment. */
3512 int min_delta = delta * dimness * factor / 2;
3514 if (factor < 1)
3515 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
3516 max (0, min (0xff, min_delta - GetGValue (*color))),
3517 max (0, min (0xff, min_delta - GetBValue (*color))));
3518 else
3519 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
3520 max (0, min (0xff, min_delta + GetGValue (*color))),
3521 max (0, min (0xff, min_delta + GetBValue (*color))));
3524 if (new == *color)
3525 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
3526 max (0, min (0xff, delta + GetGValue (*color))),
3527 max (0, min (0xff, delta + GetBValue (*color))));
3529 /* TODO: Map to palette and retry with delta if same? */
3530 /* TODO: Free colors (if using palette)? */
3532 if (new == *color)
3533 return 0;
3535 *color = new;
3537 return 1;
3541 /* Set up the foreground color for drawing relief lines of glyph
3542 string S. RELIEF is a pointer to a struct relief containing the GC
3543 with which lines will be drawn. Use a color that is FACTOR or
3544 DELTA lighter or darker than the relief's background which is found
3545 in S->f->output_data.x->relief_background. If such a color cannot
3546 be allocated, use DEFAULT_PIXEL, instead. */
3548 static void
3549 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
3550 struct frame *f;
3551 struct relief *relief;
3552 double factor;
3553 int delta;
3554 COLORREF default_pixel;
3556 XGCValues xgcv;
3557 struct w32_output *di = f->output_data.w32;
3558 unsigned long mask = GCForeground;
3559 COLORREF pixel;
3560 COLORREF background = di->relief_background;
3561 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
3563 /* TODO: Free colors (if using palette)? */
3565 /* Allocate new color. */
3566 xgcv.foreground = default_pixel;
3567 pixel = background;
3568 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
3570 relief->allocated_p = 1;
3571 xgcv.foreground = relief->pixel = pixel;
3574 if (relief->gc == 0)
3576 #if 0 /* TODO: stipple */
3577 xgcv.stipple = dpyinfo->gray;
3578 mask |= GCStipple;
3579 #endif
3580 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
3582 else
3583 XChangeGC (NULL, relief->gc, mask, &xgcv);
3587 /* Set up colors for the relief lines around glyph string S. */
3589 static void
3590 x_setup_relief_colors (s)
3591 struct glyph_string *s;
3593 struct w32_output *di = s->f->output_data.w32;
3594 COLORREF color;
3596 if (s->face->use_box_color_for_shadows_p)
3597 color = s->face->box_color;
3598 else
3599 color = s->gc->background;
3601 if (di->white_relief.gc == 0
3602 || color != di->relief_background)
3604 di->relief_background = color;
3605 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3606 WHITE_PIX_DEFAULT (s->f));
3607 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3608 BLACK_PIX_DEFAULT (s->f));
3613 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3614 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3615 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3616 relief. LEFT_P non-zero means draw a relief on the left side of
3617 the rectangle. RIGHT_P non-zero means draw a relief on the right
3618 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3619 when drawing. */
3621 static void
3622 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3623 raised_p, left_p, right_p, clip_rect)
3624 struct frame *f;
3625 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3626 RECT *clip_rect;
3628 int i;
3629 XGCValues gc;
3630 HDC hdc = get_frame_dc (f);
3632 if (raised_p)
3633 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3634 else
3635 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
3637 w32_set_clip_rectangle (hdc, clip_rect);
3639 /* Top. */
3640 for (i = 0; i < width; ++i)
3642 w32_fill_area (f, hdc, gc.foreground,
3643 left_x + i * left_p, top_y + i,
3644 (right_x + 1 - i * right_p) - (left_x + i * left_p), 1);
3647 /* Left. */
3648 if (left_p)
3649 for (i = 0; i < width; ++i)
3651 w32_fill_area (f, hdc, gc.foreground,
3652 left_x + i, top_y + i, 1,
3653 (bottom_y - i) - (top_y + i));
3656 w32_set_clip_rectangle (hdc, NULL);
3658 if (raised_p)
3659 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
3660 else
3661 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
3664 w32_set_clip_rectangle (hdc, clip_rect);
3666 /* Bottom. */
3667 for (i = 0; i < width; ++i)
3669 w32_fill_area (f, hdc, gc.foreground,
3670 left_x + i * left_p, bottom_y - i,
3671 (right_x + 1 - i * right_p) - left_x + i * left_p, 1);
3674 /* Right. */
3675 if (right_p)
3676 for (i = 0; i < width; ++i)
3678 w32_fill_area (f, hdc, gc.foreground,
3679 right_x - i, top_y + i + 1, 1,
3680 (bottom_y - i) - (top_y + i + 1));
3683 w32_set_clip_rectangle (hdc, NULL);
3685 release_frame_dc (f, hdc);
3689 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3690 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3691 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3692 left side of the rectangle. RIGHT_P non-zero means draw a line
3693 on the right side of the rectangle. CLIP_RECT is the clipping
3694 rectangle to use when drawing. */
3696 static void
3697 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3698 left_p, right_p, clip_rect)
3699 struct glyph_string *s;
3700 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3701 RECT *clip_rect;
3703 w32_set_clip_rectangle (s->hdc, clip_rect);
3705 /* Top. */
3706 w32_fill_area (s->f, s->hdc, s->face->box_color,
3707 left_x, top_y, right_x - left_x + 1, width);
3709 /* Left. */
3710 if (left_p)
3712 w32_fill_area (s->f, s->hdc, s->face->box_color,
3713 left_x, top_y, width, bottom_y - top_y + 1);
3716 /* Bottom. */
3717 w32_fill_area (s->f, s->hdc, s->face->box_color,
3718 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3720 /* Right. */
3721 if (right_p)
3723 w32_fill_area (s->f, s->hdc, s->face->box_color,
3724 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3727 w32_set_clip_rectangle (s->hdc, NULL);
3731 /* Draw a box around glyph string S. */
3733 static void
3734 x_draw_glyph_string_box (s)
3735 struct glyph_string *s;
3737 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3738 int left_p, right_p;
3739 struct glyph *last_glyph;
3740 RECT clip_rect;
3742 last_x = window_box_right (s->w, s->area);
3743 if (s->row->full_width_p
3744 && !s->w->pseudo_window_p)
3746 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3747 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3748 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3751 /* The glyph that may have a right box line. */
3752 last_glyph = (s->cmp || s->img
3753 ? s->first_glyph
3754 : s->first_glyph + s->nchars - 1);
3756 width = s->face->box_line_width;
3757 raised_p = s->face->box == FACE_RAISED_BOX;
3758 left_x = s->x;
3759 right_x = ((s->row->full_width_p
3760 ? last_x - 1
3761 : min (last_x, s->x + s->background_width) - 1));
3762 top_y = s->y;
3763 bottom_y = top_y + s->height - 1;
3765 left_p = (s->first_glyph->left_box_line_p
3766 || (s->hl == DRAW_MOUSE_FACE
3767 && (s->prev == NULL
3768 || s->prev->hl != s->hl)));
3769 right_p = (last_glyph->right_box_line_p
3770 || (s->hl == DRAW_MOUSE_FACE
3771 && (s->next == NULL
3772 || s->next->hl != s->hl)));
3774 w32_get_glyph_string_clip_rect (s, &clip_rect);
3776 if (s->face->box == FACE_SIMPLE_BOX)
3777 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3778 left_p, right_p, &clip_rect);
3779 else
3781 x_setup_relief_colors (s);
3782 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3783 width, raised_p, left_p, right_p, &clip_rect);
3788 /* Draw foreground of image glyph string S. */
3790 static void
3791 x_draw_image_foreground (s)
3792 struct glyph_string *s;
3794 int x;
3795 int y = s->ybase - image_ascent (s->img, s->face);
3797 /* If first glyph of S has a left box line, start drawing it to the
3798 right of that line. */
3799 if (s->face->box != FACE_NO_BOX
3800 && s->first_glyph->left_box_line_p)
3801 x = s->x + s->face->box_line_width;
3802 else
3803 x = s->x;
3805 /* If there is a margin around the image, adjust x- and y-position
3806 by that margin. */
3807 if (s->img->margin)
3809 x += s->img->margin;
3810 y += s->img->margin;
3813 SaveDC (s->hdc);
3815 if (s->img->pixmap)
3817 #if 0 /* TODO: image mask */
3818 if (s->img->mask)
3820 /* We can't set both a clip mask and use XSetClipRectangles
3821 because the latter also sets a clip mask. We also can't
3822 trust on the shape extension to be available
3823 (XShapeCombineRegion). So, compute the rectangle to draw
3824 manually. */
3825 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3826 | GCFunction);
3827 XGCValues xgcv;
3828 XRectangle clip_rect, image_rect, r;
3830 xgcv.clip_mask = s->img->mask;
3831 xgcv.clip_x_origin = x;
3832 xgcv.clip_y_origin = y;
3833 xgcv.function = GXcopy;
3834 XChangeGC (s->display, s->gc, mask, &xgcv);
3836 w32_get_glyph_string_clip_rect (s, &clip_rect);
3837 image_rect.x = x;
3838 image_rect.y = y;
3839 image_rect.width = s->img->width;
3840 image_rect.height = s->img->height;
3841 if (IntersectRect (&r, &clip_rect, &image_rect))
3842 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3843 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3845 else
3846 #endif
3848 HDC compat_hdc = CreateCompatibleDC (s->hdc);
3849 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3850 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
3851 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3852 x_set_glyph_string_clipping (s);
3854 SetTextColor (s->hdc, s->gc->foreground);
3855 SetBkColor (s->hdc, s->gc->background);
3856 #if 0 /* From w32bdf.c (which is from Meadow). */
3857 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3858 compat_hdc, 0, 0, SRCCOPY);
3859 #else
3860 BitBlt (s->hdc, x, y, s->img->width, s->img->height,
3861 compat_hdc, 0, 0, 0xB8074A);
3862 #endif
3863 SelectObject (s->hdc, orig_brush);
3864 DeleteObject (fg_brush);
3865 SelectObject (compat_hdc, orig_obj);
3866 DeleteDC (compat_hdc);
3868 /* When the image has a mask, we can expect that at
3869 least part of a mouse highlight or a block cursor will
3870 be visible. If the image doesn't have a mask, make
3871 a block cursor visible by drawing a rectangle around
3872 the image. I believe it's looking better if we do
3873 nothing here for mouse-face. */
3874 if (s->hl == DRAW_CURSOR)
3875 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width - 1,
3876 s->img->height - 1);
3877 w32_set_clip_rectangle (s->hdc, NULL);
3880 else
3881 w32_draw_rectangle (s->hdc, s->gc, x, y, s->img->width -1,
3882 s->img->height - 1);
3884 RestoreDC (s->hdc ,-1);
3889 /* Draw a relief around the image glyph string S. */
3891 static void
3892 x_draw_image_relief (s)
3893 struct glyph_string *s;
3895 int x0, y0, x1, y1, thick, raised_p;
3896 RECT r;
3897 int x;
3898 int y = s->ybase - image_ascent (s->img, s->face);
3900 /* If first glyph of S has a left box line, start drawing it to the
3901 right of that line. */
3902 if (s->face->box != FACE_NO_BOX
3903 && s->first_glyph->left_box_line_p)
3904 x = s->x + s->face->box_line_width;
3905 else
3906 x = s->x;
3908 /* If there is a margin around the image, adjust x- and y-position
3909 by that margin. */
3910 if (s->img->margin)
3912 x += s->img->margin;
3913 y += s->img->margin;
3916 if (s->hl == DRAW_IMAGE_SUNKEN
3917 || s->hl == DRAW_IMAGE_RAISED)
3919 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3920 raised_p = s->hl == DRAW_IMAGE_RAISED;
3922 else
3924 thick = abs (s->img->relief);
3925 raised_p = s->img->relief > 0;
3928 x0 = x - thick;
3929 y0 = y - thick;
3930 x1 = x + s->img->width + thick - 1;
3931 y1 = y + s->img->height + thick - 1;
3933 x_setup_relief_colors (s);
3934 w32_get_glyph_string_clip_rect (s, &r);
3935 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3939 /* Draw the foreground of image glyph string S to PIXMAP. */
3941 static void
3942 w32_draw_image_foreground_1 (s, pixmap)
3943 struct glyph_string *s;
3944 HBITMAP pixmap;
3946 HDC hdc = CreateCompatibleDC (s->hdc);
3947 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
3948 int x;
3949 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3951 /* If first glyph of S has a left box line, start drawing it to the
3952 right of that line. */
3953 if (s->face->box != FACE_NO_BOX
3954 && s->first_glyph->left_box_line_p)
3955 x = s->face->box_line_width;
3956 else
3957 x = 0;
3959 /* If there is a margin around the image, adjust x- and y-position
3960 by that margin. */
3961 if (s->img->margin)
3963 x += s->img->margin;
3964 y += s->img->margin;
3967 if (s->img->pixmap)
3969 #if 0 /* TODO: image mask */
3970 if (s->img->mask)
3972 /* We can't set both a clip mask and use XSetClipRectangles
3973 because the latter also sets a clip mask. We also can't
3974 trust on the shape extension to be available
3975 (XShapeCombineRegion). So, compute the rectangle to draw
3976 manually. */
3977 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3978 | GCFunction);
3979 XGCValues xgcv;
3981 xgcv.clip_mask = s->img->mask;
3982 xgcv.clip_x_origin = x;
3983 xgcv.clip_y_origin = y;
3984 xgcv.function = GXcopy;
3985 XChangeGC (s->display, s->gc, mask, &xgcv);
3987 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3988 0, 0, s->img->width, s->img->height, x, y);
3989 XSetClipMask (s->display, s->gc, None);
3991 else
3992 #endif
3994 HDC compat_hdc = CreateCompatibleDC (hdc);
3995 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
3996 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
3997 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
3999 SetTextColor (hdc, s->gc->foreground);
4000 SetBkColor (hdc, s->gc->background);
4001 #if 0 /* From w32bdf.c (which is from Meadow). */
4002 BitBlt (hdc, x, y, s->img->width, s->img->height,
4003 compat_hdc, 0, 0, SRCCOPY);
4004 #else
4005 BitBlt (hdc, x, y, s->img->width, s->img->height,
4006 compat_hdc, 0, 0, 0xB8074A);
4007 #endif
4008 SelectObject (hdc, orig_brush);
4009 DeleteObject (fg_brush);
4010 SelectObject (compat_hdc, orig_obj);
4011 DeleteDC (compat_hdc);
4013 /* When the image has a mask, we can expect that at
4014 least part of a mouse highlight or a block cursor will
4015 be visible. If the image doesn't have a mask, make
4016 a block cursor visible by drawing a rectangle around
4017 the image. I believe it's looking better if we do
4018 nothing here for mouse-face. */
4019 if (s->hl == DRAW_CURSOR)
4020 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4021 s->img->height - 1);
4024 else
4025 w32_draw_rectangle (hdc, s->gc, x, y, s->img->width - 1,
4026 s->img->height - 1);
4028 SelectObject (hdc, orig_hdc_obj);
4029 DeleteDC (hdc);
4033 /* Draw part of the background of glyph string S. X, Y, W, and H
4034 give the rectangle to draw. */
4036 static void
4037 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4038 struct glyph_string *s;
4039 int x, y, w, h;
4041 #if 0 /* TODO: stipple */
4042 if (s->stippled_p)
4044 /* Fill background with a stipple pattern. */
4045 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4046 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4047 XSetFillStyle (s->display, s->gc, FillSolid);
4049 else
4050 #endif
4051 x_clear_glyph_string_rect (s, x, y, w, h);
4055 /* Draw image glyph string S.
4057 s->y
4058 s->x +-------------------------
4059 | s->face->box
4061 | +-------------------------
4062 | | s->img->margin
4064 | | +-------------------
4065 | | | the image
4069 static void
4070 x_draw_image_glyph_string (s)
4071 struct glyph_string *s;
4073 int x, y;
4074 int box_line_width = s->face->box_line_width;
4075 int margin = s->img->margin;
4076 int height;
4077 HBITMAP pixmap = 0;
4079 height = s->height - 2 * box_line_width;
4081 /* Fill background with face under the image. Do it only if row is
4082 taller than image or if image has a clip mask to reduce
4083 flickering. */
4084 s->stippled_p = s->face->stipple != 0;
4085 if (height > s->img->height
4086 || margin
4087 #if 0 /* TODO: image mask */
4088 || s->img->mask
4089 #endif
4090 || s->img->pixmap == 0
4091 || s->width != s->background_width)
4093 if (box_line_width && s->first_glyph->left_box_line_p)
4094 x = s->x + box_line_width;
4095 else
4096 x = s->x;
4098 y = s->y + box_line_width;
4099 #if 0 /* TODO: image mask */
4100 if (s->img->mask)
4102 /* Create a pixmap as large as the glyph string Fill it with
4103 the background color. Copy the image to it, using its
4104 mask. Copy the temporary pixmap to the display. */
4105 Screen *screen = FRAME_X_SCREEN (s->f);
4106 int depth = DefaultDepthOfScreen (screen);
4108 /* Create a pixmap as large as the glyph string. */
4109 pixmap = XCreatePixmap (s->display, s->window,
4110 s->background_width,
4111 s->height, depth);
4113 /* Don't clip in the following because we're working on the
4114 pixmap. */
4115 XSetClipMask (s->display, s->gc, None);
4117 /* Fill the pixmap with the background color/stipple. */
4118 if (s->stippled_p)
4120 /* Fill background with a stipple pattern. */
4121 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4122 XFillRectangle (s->display, pixmap, s->gc,
4123 0, 0, s->background_width, s->height);
4124 XSetFillStyle (s->display, s->gc, FillSolid);
4126 else
4128 XGCValues xgcv;
4129 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4130 &xgcv);
4131 XSetForeground (s->display, s->gc, xgcv.background);
4132 XFillRectangle (s->display, pixmap, s->gc,
4133 0, 0, s->background_width, s->height);
4134 XSetForeground (s->display, s->gc, xgcv.foreground);
4137 else
4138 #endif
4139 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4141 s->background_filled_p = 1;
4144 /* Draw the foreground. */
4145 if (pixmap != 0)
4147 w32_draw_image_foreground_1 (s, pixmap);
4148 x_set_glyph_string_clipping (s);
4150 HDC compat_hdc = CreateCompatibleDC (s->hdc);
4151 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
4152 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
4153 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
4155 SetTextColor (s->hdc, s->gc->foreground);
4156 SetBkColor (s->hdc, s->gc->background);
4157 #if 0 /* From w32bdf.c (which is from Meadow). */
4158 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4159 compat_hdc, 0, 0, SRCCOPY);
4160 #else
4161 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
4162 compat_hdc, 0, 0, 0xB8074A);
4163 #endif
4164 SelectObject (s->hdc, orig_brush);
4165 DeleteObject (fg_brush);
4166 SelectObject (compat_hdc, orig_obj);
4167 DeleteDC (compat_hdc);
4169 DeleteObject (pixmap);
4170 pixmap = 0;
4172 else
4173 x_draw_image_foreground (s);
4175 /* If we must draw a relief around the image, do it. */
4176 if (s->img->relief
4177 || s->hl == DRAW_IMAGE_RAISED
4178 || s->hl == DRAW_IMAGE_SUNKEN)
4179 x_draw_image_relief (s);
4183 /* Draw stretch glyph string S. */
4185 static void
4186 x_draw_stretch_glyph_string (s)
4187 struct glyph_string *s;
4189 xassert (s->first_glyph->type == STRETCH_GLYPH);
4190 s->stippled_p = s->face->stipple != 0;
4192 if (s->hl == DRAW_CURSOR
4193 && !x_stretch_cursor_p)
4195 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4196 as wide as the stretch glyph. */
4197 int width = min (CANON_X_UNIT (s->f), s->background_width);
4199 /* Draw cursor. */
4200 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4202 /* Clear rest using the GC of the original non-cursor face. */
4203 if (width < s->background_width)
4205 XGCValues *gc = s->face->gc;
4206 int x = s->x + width, y = s->y;
4207 int w = s->background_width - width, h = s->height;
4208 RECT r;
4209 HDC hdc = s->hdc;
4210 w32_get_glyph_string_clip_rect (s, &r);
4211 w32_set_clip_rectangle (hdc, &r);
4213 #if 0 /* TODO: stipple */
4214 if (s->face->stipple)
4216 /* Fill background with a stipple pattern. */
4217 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4218 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4219 XSetFillStyle (s->display, gc, FillSolid);
4221 else
4222 #endif
4224 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
4228 else
4229 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4230 s->height);
4232 s->background_filled_p = 1;
4236 /* Draw glyph string S. */
4238 static void
4239 x_draw_glyph_string (s)
4240 struct glyph_string *s;
4242 /* If S draws into the background of its successor, draw the
4243 background of the successor first so that S can draw into it.
4244 This makes S->next use XDrawString instead of XDrawImageString. */
4245 if (s->next && s->right_overhang && !s->for_overlaps_p)
4247 xassert (s->next->img == NULL);
4248 x_set_glyph_string_gc (s->next);
4249 x_set_glyph_string_clipping (s->next);
4250 x_draw_glyph_string_background (s->next, 1);
4253 /* Set up S->gc, set clipping and draw S. */
4254 x_set_glyph_string_gc (s);
4255 x_set_glyph_string_clipping (s);
4257 switch (s->first_glyph->type)
4259 case IMAGE_GLYPH:
4260 x_draw_image_glyph_string (s);
4261 break;
4263 case STRETCH_GLYPH:
4264 x_draw_stretch_glyph_string (s);
4265 break;
4267 case CHAR_GLYPH:
4268 if (s->for_overlaps_p)
4269 s->background_filled_p = 1;
4270 else
4271 x_draw_glyph_string_background (s, 0);
4272 x_draw_glyph_string_foreground (s);
4273 break;
4275 case COMPOSITE_GLYPH:
4276 if (s->for_overlaps_p || s->gidx > 0)
4277 s->background_filled_p = 1;
4278 else
4279 x_draw_glyph_string_background (s, 1);
4280 x_draw_composite_glyph_string_foreground (s);
4281 break;
4283 default:
4284 abort ();
4287 if (!s->for_overlaps_p)
4289 /* Draw underline. */
4290 if (s->face->underline_p
4291 && (s->font->bdf || !s->font->tm.tmUnderlined))
4293 unsigned long h = 1;
4294 unsigned long dy = s->height - h;
4296 if (s->face->underline_defaulted_p)
4298 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4299 s->y + dy, s->width, 1);
4301 else
4303 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4304 s->y + dy, s->width, 1);
4308 /* Draw overline. */
4309 if (s->face->overline_p)
4311 unsigned long dy = 0, h = 1;
4313 if (s->face->overline_color_defaulted_p)
4315 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
4316 s->y + dy, s->width, h);
4318 else
4320 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4321 s->y + dy, s->width, h);
4325 /* Draw strike-through. */
4326 if (s->face->strike_through_p
4327 && (s->font->bdf || !s->font->tm.tmStruckOut))
4329 unsigned long h = 1;
4330 unsigned long dy = (s->height - h) / 2;
4332 if (s->face->strike_through_color_defaulted_p)
4334 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
4335 s->width, h);
4337 else
4339 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
4340 s->y + dy, s->width, h);
4344 /* Draw relief. */
4345 if (s->face->box != FACE_NO_BOX)
4346 x_draw_glyph_string_box (s);
4349 /* Reset clipping. */
4350 w32_set_clip_rectangle (s->hdc, NULL);
4354 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4355 struct face **, int));
4358 /* Load glyph string S with a composition components specified by S->cmp.
4359 FACES is an array of faces for all components of this composition.
4360 S->gidx is the index of the first component for S.
4361 OVERLAPS_P non-zero means S should draw the foreground only, and
4362 use its lines physical height for clipping.
4364 Value is the index of a component not in S. */
4366 static int
4367 x_fill_composite_glyph_string (s, faces, overlaps_p)
4368 struct glyph_string *s;
4369 struct face **faces;
4370 int overlaps_p;
4372 int i;
4374 xassert (s);
4376 s->for_overlaps_p = overlaps_p;
4378 s->face = faces[s->gidx];
4379 s->font = s->face->font;
4380 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4382 /* For all glyphs of this composition, starting at the offset
4383 S->gidx, until we reach the end of the definition or encounter a
4384 glyph that requires the different face, add it to S. */
4385 ++s->nchars;
4386 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4387 ++s->nchars;
4389 /* All glyph strings for the same composition has the same width,
4390 i.e. the width set for the first component of the composition. */
4392 s->width = s->first_glyph->pixel_width;
4394 /* If the specified font could not be loaded, use the frame's
4395 default font, but record the fact that we couldn't load it in
4396 the glyph string so that we can draw rectangles for the
4397 characters of the glyph string. */
4398 if (s->font == NULL)
4400 s->font_not_found_p = 1;
4401 s->font = FRAME_FONT (s->f);
4404 /* Adjust base line for subscript/superscript text. */
4405 s->ybase += s->first_glyph->voffset;
4407 xassert (s->face && s->face->gc);
4409 /* This glyph string must always be drawn with 16-bit functions. */
4410 s->two_byte_p = 1;
4412 return s->gidx + s->nchars;
4416 /* Load glyph string S with a sequence of characters.
4417 FACE_ID is the face id of the string. START is the index of the
4418 first glyph to consider, END is the index of the last + 1.
4419 OVERLAPS_P non-zero means S should draw the foreground only, and
4420 use its lines physical height for clipping.
4422 Value is the index of the first glyph not in S. */
4424 static int
4425 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4426 struct glyph_string *s;
4427 int face_id;
4428 int start, end, overlaps_p;
4430 struct glyph *glyph, *last;
4431 int voffset;
4432 int glyph_not_available_p;
4434 xassert (s->f == XFRAME (s->w->frame));
4435 xassert (s->nchars == 0);
4436 xassert (start >= 0 && end > start);
4438 s->for_overlaps_p = overlaps_p;
4439 glyph = s->row->glyphs[s->area] + start;
4440 last = s->row->glyphs[s->area] + end;
4441 voffset = glyph->voffset;
4443 glyph_not_available_p = glyph->glyph_not_available_p;
4445 while (glyph < last
4446 && glyph->type == CHAR_GLYPH
4447 && glyph->voffset == voffset
4448 /* Same face id implies same font, nowadays. */
4449 && glyph->face_id == face_id
4450 && glyph->glyph_not_available_p == glyph_not_available_p)
4452 int two_byte_p;
4454 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4455 s->char2b + s->nchars,
4456 &two_byte_p);
4457 s->two_byte_p = two_byte_p;
4458 ++s->nchars;
4459 xassert (s->nchars <= end - start);
4460 s->width += glyph->pixel_width;
4461 ++glyph;
4464 s->font = s->face->font;
4465 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4467 /* If the specified font could not be loaded, use the frame's font,
4468 but record the fact that we couldn't load it in
4469 S->font_not_found_p so that we can draw rectangles for the
4470 characters of the glyph string. */
4471 if (s->font == NULL || glyph_not_available_p)
4473 s->font_not_found_p = 1;
4474 s->font = FRAME_FONT (s->f);
4477 /* Adjust base line for subscript/superscript text. */
4478 s->ybase += voffset;
4480 xassert (s->face && s->face->gc);
4481 return glyph - s->row->glyphs[s->area];
4485 /* Fill glyph string S from image glyph S->first_glyph. */
4487 static void
4488 x_fill_image_glyph_string (s)
4489 struct glyph_string *s;
4491 xassert (s->first_glyph->type == IMAGE_GLYPH);
4492 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4493 xassert (s->img);
4494 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4495 s->font = s->face->font;
4496 s->width = s->first_glyph->pixel_width;
4498 /* Adjust base line for subscript/superscript text. */
4499 s->ybase += s->first_glyph->voffset;
4503 /* Fill glyph string S from a sequence of stretch glyphs.
4505 ROW is the glyph row in which the glyphs are found, AREA is the
4506 area within the row. START is the index of the first glyph to
4507 consider, END is the index of the last + 1.
4509 Value is the index of the first glyph not in S. */
4511 static int
4512 x_fill_stretch_glyph_string (s, row, area, start, end)
4513 struct glyph_string *s;
4514 struct glyph_row *row;
4515 enum glyph_row_area area;
4516 int start, end;
4518 struct glyph *glyph, *last;
4519 int voffset, face_id;
4521 xassert (s->first_glyph->type == STRETCH_GLYPH);
4523 glyph = s->row->glyphs[s->area] + start;
4524 last = s->row->glyphs[s->area] + end;
4525 face_id = glyph->face_id;
4526 s->face = FACE_FROM_ID (s->f, face_id);
4527 s->font = s->face->font;
4528 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4529 s->width = glyph->pixel_width;
4530 voffset = glyph->voffset;
4532 for (++glyph;
4533 (glyph < last
4534 && glyph->type == STRETCH_GLYPH
4535 && glyph->voffset == voffset
4536 && glyph->face_id == face_id);
4537 ++glyph)
4538 s->width += glyph->pixel_width;
4540 /* Adjust base line for subscript/superscript text. */
4541 s->ybase += voffset;
4543 xassert (s->face && s->face->gc);
4544 return glyph - s->row->glyphs[s->area];
4548 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4549 of XChar2b structures for S; it can't be allocated in
4550 x_init_glyph_string because it must be allocated via `alloca'. W
4551 is the window on which S is drawn. ROW and AREA are the glyph row
4552 and area within the row from which S is constructed. START is the
4553 index of the first glyph structure covered by S. HL is a
4554 face-override for drawing S. */
4556 static void
4557 w32_init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
4558 struct glyph_string *s;
4559 HDC hdc;
4560 wchar_t *char2b;
4561 struct window *w;
4562 struct glyph_row *row;
4563 enum glyph_row_area area;
4564 int start;
4565 enum draw_glyphs_face hl;
4567 bzero (s, sizeof *s);
4568 s->w = w;
4569 s->f = XFRAME (w->frame);
4570 s->hdc = hdc;
4571 s->window = FRAME_W32_WINDOW (s->f);
4572 s->char2b = char2b;
4573 s->hl = hl;
4574 s->row = row;
4575 s->area = area;
4576 s->first_glyph = row->glyphs[area] + start;
4577 s->height = row->height;
4578 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4580 /* Display the internal border below the tool-bar window. */
4581 if (s->w == XWINDOW (s->f->tool_bar_window))
4582 s->y -= s->f->output_data.w32->internal_border_width;
4584 s->ybase = s->y + row->ascent;
4588 /* Set background width of glyph string S. START is the index of the
4589 first glyph following S. LAST_X is the right-most x-position + 1
4590 in the drawing area. */
4592 static INLINE void
4593 x_set_glyph_string_background_width (s, start, last_x)
4594 struct glyph_string *s;
4595 int start;
4596 int last_x;
4598 /* If the face of this glyph string has to be drawn to the end of
4599 the drawing area, set S->extends_to_end_of_line_p. */
4600 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4602 if (start == s->row->used[s->area]
4603 && s->hl == DRAW_NORMAL_TEXT
4604 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4605 || s->face->background != default_face->background
4606 || s->face->stipple != default_face->stipple))
4607 s->extends_to_end_of_line_p = 1;
4609 /* If S extends its face to the end of the line, set its
4610 background_width to the distance to the right edge of the drawing
4611 area. */
4612 if (s->extends_to_end_of_line_p)
4613 s->background_width = last_x - s->x + 1;
4614 else
4615 s->background_width = s->width;
4619 /* Add a glyph string for a stretch glyph to the list of strings
4620 between HEAD and TAIL. START is the index of the stretch glyph in
4621 row area AREA of glyph row ROW. END is the index of the last glyph
4622 in that glyph row area. X is the current output position assigned
4623 to the new glyph string constructed. HL overrides that face of the
4624 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4625 is the right-most x-position of the drawing area. */
4627 #define BUILD_STRETCH_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4628 do \
4630 s = (struct glyph_string *) alloca (sizeof *s); \
4631 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4632 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4633 x_append_glyph_string (&HEAD, &TAIL, s); \
4634 s->x = (X); \
4636 while (0)
4639 /* Add a glyph string for an image glyph to the list of strings
4640 between HEAD and TAIL. START is the index of the image glyph in
4641 row area AREA of glyph row ROW. END is the index of the last glyph
4642 in that glyph row area. X is the current output position assigned
4643 to the new glyph string constructed. HL overrides that face of the
4644 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4645 is the right-most x-position of the drawing area. */
4647 #define BUILD_IMAGE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4648 do \
4650 s = (struct glyph_string *) alloca (sizeof *s); \
4651 w32_init_glyph_string (s, hdc, NULL, W, ROW, AREA, START, HL); \
4652 x_fill_image_glyph_string (s); \
4653 x_append_glyph_string (&HEAD, &TAIL, s); \
4654 ++START; \
4655 s->x = (X); \
4657 while (0)
4660 /* Add a glyph string for a sequence of character glyphs to the list
4661 of strings between HEAD and TAIL. START is the index of the first
4662 glyph in row area AREA of glyph row ROW that is part of the new
4663 glyph string. END is the index of the last glyph in that glyph row
4664 area. X is the current output position assigned to the new glyph
4665 string constructed. HL overrides that face of the glyph; e.g. it
4666 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4667 right-most x-position of the drawing area. */
4669 #define BUILD_CHAR_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4670 do \
4672 int c, face_id; \
4673 wchar_t *char2b; \
4675 c = (ROW)->glyphs[AREA][START].u.ch; \
4676 face_id = (ROW)->glyphs[AREA][START].face_id; \
4678 s = (struct glyph_string *) alloca (sizeof *s); \
4679 char2b = (wchar_t *) alloca ((END - START) * sizeof *char2b); \
4680 w32_init_glyph_string (s, hdc, char2b, W, ROW, AREA, START, HL); \
4681 x_append_glyph_string (&HEAD, &TAIL, s); \
4682 s->x = (X); \
4683 START = x_fill_glyph_string (s, face_id, START, END, \
4684 OVERLAPS_P); \
4686 while (0)
4689 /* Add a glyph string for a composite sequence to the list of strings
4690 between HEAD and TAIL. START is the index of the first glyph in
4691 row area AREA of glyph row ROW that is part of the new glyph
4692 string. END is the index of the last glyph in that glyph row area.
4693 X is the current output position assigned to the new glyph string
4694 constructed. HL overrides that face of the glyph; e.g. it is
4695 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4696 x-position of the drawing area. */
4698 #define BUILD_COMPOSITE_GLYPH_STRING(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4699 do { \
4700 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4701 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4702 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4703 struct composition *cmp = composition_table[cmp_id]; \
4704 int glyph_len = cmp->glyph_len; \
4705 wchar_t *char2b; \
4706 struct face **faces; \
4707 struct glyph_string *first_s = NULL; \
4708 int n; \
4710 base_face = base_face->ascii_face; \
4711 char2b = (wchar_t *) alloca ((sizeof *char2b) * glyph_len); \
4712 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4713 /* At first, fill in `char2b' and `faces'. */ \
4714 for (n = 0; n < glyph_len; n++) \
4716 int c = COMPOSITION_GLYPH (cmp, n); \
4717 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4718 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4719 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4720 this_face_id, char2b + n, 1); \
4723 /* Make glyph_strings for each glyph sequence that is drawable by \
4724 the same face, and append them to HEAD/TAIL. */ \
4725 for (n = 0; n < cmp->glyph_len;) \
4727 s = (struct glyph_string *) alloca (sizeof *s); \
4728 w32_init_glyph_string (s, hdc, char2b + n, W, ROW, AREA, START, HL); \
4729 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4730 s->cmp = cmp; \
4731 s->gidx = n; \
4732 s->x = (X); \
4734 if (n == 0) \
4735 first_s = s; \
4737 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4740 ++START; \
4741 s = first_s; \
4742 } while (0)
4745 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4746 of AREA of glyph row ROW on window W between indices START and END.
4747 HL overrides the face for drawing glyph strings, e.g. it is
4748 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4749 x-positions of the drawing area.
4751 This is an ugly monster macro construct because we must use alloca
4752 to allocate glyph strings (because x_draw_glyphs can be called
4753 asynchronously). */
4755 #define BUILD_GLYPH_STRINGS(hdc, W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4756 do \
4758 HEAD = TAIL = NULL; \
4759 while (START < END) \
4761 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4762 switch (first_glyph->type) \
4764 case CHAR_GLYPH: \
4765 BUILD_CHAR_GLYPH_STRINGS (hdc, W, ROW, AREA, START, END, \
4766 HEAD, TAIL, HL, X, LAST_X, \
4767 OVERLAPS_P); \
4768 break; \
4770 case COMPOSITE_GLYPH: \
4771 BUILD_COMPOSITE_GLYPH_STRING (hdc, W, ROW, AREA, START, \
4772 END, HEAD, TAIL, HL, X, \
4773 LAST_X, OVERLAPS_P); \
4774 break; \
4776 case STRETCH_GLYPH: \
4777 BUILD_STRETCH_GLYPH_STRING (hdc, W, ROW, AREA, START, END,\
4778 HEAD, TAIL, HL, X, LAST_X); \
4779 break; \
4781 case IMAGE_GLYPH: \
4782 BUILD_IMAGE_GLYPH_STRING (hdc, W, ROW, AREA, START, END, \
4783 HEAD, TAIL, HL, X, LAST_X); \
4784 break; \
4786 default: \
4787 abort (); \
4790 x_set_glyph_string_background_width (s, START, LAST_X); \
4791 (X) += s->width; \
4794 while (0)
4797 /* Draw glyphs between START and END in AREA of ROW on window W,
4798 starting at x-position X. X is relative to AREA in W. HL is a
4799 face-override with the following meaning:
4801 DRAW_NORMAL_TEXT draw normally
4802 DRAW_CURSOR draw in cursor face
4803 DRAW_MOUSE_FACE draw in mouse face.
4804 DRAW_INVERSE_VIDEO draw in mode line face
4805 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4806 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4808 If REAL_START is non-null, return in *REAL_START the real starting
4809 position for display. This can be different from START in case
4810 overlapping glyphs must be displayed. If REAL_END is non-null,
4811 return in *REAL_END the real end position for display. This can be
4812 different from END in case overlapping glyphs must be displayed.
4814 If OVERLAPS_P is non-zero, draw only the foreground of characters
4815 and clip to the physical height of ROW.
4817 Value is the x-position reached, relative to AREA of W. */
4819 static int
4820 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4821 overlaps_p)
4822 struct window *w;
4823 int x;
4824 struct glyph_row *row;
4825 enum glyph_row_area area;
4826 int start, end;
4827 enum draw_glyphs_face hl;
4828 int *real_start, *real_end;
4829 int overlaps_p;
4831 struct glyph_string *head, *tail;
4832 struct glyph_string *s;
4833 int last_x, area_width;
4834 int x_reached;
4835 int i, j;
4836 HDC hdc = get_frame_dc (XFRAME (WINDOW_FRAME (w)));
4838 /* Let's rather be paranoid than getting a SEGV. */
4839 end = min (end, row->used[area]);
4840 start = max (0, start);
4841 start = min (end, start);
4843 if (real_start)
4844 *real_start = start;
4845 if (real_end)
4846 *real_end = end;
4848 /* Translate X to frame coordinates. Set last_x to the right
4849 end of the drawing area. */
4850 if (row->full_width_p)
4852 /* X is relative to the left edge of W, without scroll bars
4853 or flag areas. */
4854 struct frame *f = XFRAME (WINDOW_FRAME (w));
4855 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4856 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4858 x += window_left_x;
4859 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4860 last_x = window_left_x + area_width;
4862 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4864 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4865 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4866 last_x += width;
4867 else
4868 x -= width;
4871 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4872 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4874 else
4876 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4877 area_width = window_box_width (w, area);
4878 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4881 /* Build a doubly-linked list of glyph_string structures between
4882 head and tail from what we have to draw. Note that the macro
4883 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4884 the reason we use a separate variable `i'. */
4885 i = start;
4886 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, end, head, tail, hl, x, last_x,
4887 overlaps_p);
4888 if (tail)
4889 x_reached = tail->x + tail->background_width;
4890 else
4891 x_reached = x;
4893 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4894 the row, redraw some glyphs in front or following the glyph
4895 strings built above. */
4896 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4898 int dummy_x = 0;
4899 struct glyph_string *h, *t;
4901 /* Compute overhangs for all glyph strings. */
4902 for (s = head; s; s = s->next)
4903 x_compute_glyph_string_overhangs (s);
4905 /* Prepend glyph strings for glyphs in front of the first glyph
4906 string that are overwritten because of the first glyph
4907 string's left overhang. The background of all strings
4908 prepended must be drawn because the first glyph string
4909 draws over it. */
4910 i = x_left_overwritten (head);
4911 if (i >= 0)
4913 j = i;
4914 BUILD_GLYPH_STRINGS (hdc, w, row, area, j, start, h, t,
4915 DRAW_NORMAL_TEXT, dummy_x, last_x,
4916 overlaps_p);
4917 start = i;
4918 if (real_start)
4919 *real_start = start;
4920 x_compute_overhangs_and_x (t, head->x, 1);
4921 x_prepend_glyph_string_lists (&head, &tail, h, t);
4924 /* Prepend glyph strings for glyphs in front of the first glyph
4925 string that overwrite that glyph string because of their
4926 right overhang. For these strings, only the foreground must
4927 be drawn, because it draws over the glyph string at `head'.
4928 The background must not be drawn because this would overwrite
4929 right overhangs of preceding glyphs for which no glyph
4930 strings exist. */
4931 i = x_left_overwriting (head);
4932 if (i >= 0)
4934 BUILD_GLYPH_STRINGS (hdc, w, row, area, i, start, h, t,
4935 DRAW_NORMAL_TEXT, dummy_x, last_x,
4936 overlaps_p);
4937 for (s = h; s; s = s->next)
4938 s->background_filled_p = 1;
4939 if (real_start)
4940 *real_start = i;
4941 x_compute_overhangs_and_x (t, head->x, 1);
4942 x_prepend_glyph_string_lists (&head, &tail, h, t);
4945 /* Append glyphs strings for glyphs following the last glyph
4946 string tail that are overwritten by tail. The background of
4947 these strings has to be drawn because tail's foreground draws
4948 over it. */
4949 i = x_right_overwritten (tail);
4950 if (i >= 0)
4952 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
4953 DRAW_NORMAL_TEXT, x, last_x,
4954 overlaps_p);
4955 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4956 x_append_glyph_string_lists (&head, &tail, h, t);
4957 if (real_end)
4958 *real_end = i;
4961 /* Append glyph strings for glyphs following the last glyph
4962 string tail that overwrite tail. The foreground of such
4963 glyphs has to be drawn because it writes into the background
4964 of tail. The background must not be drawn because it could
4965 paint over the foreground of following glyphs. */
4966 i = x_right_overwriting (tail);
4967 if (i >= 0)
4969 BUILD_GLYPH_STRINGS (hdc, w, row, area, end, i, h, t,
4970 DRAW_NORMAL_TEXT, x, last_x,
4971 overlaps_p);
4972 for (s = h; s; s = s->next)
4973 s->background_filled_p = 1;
4974 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4975 x_append_glyph_string_lists (&head, &tail, h, t);
4976 if (real_end)
4977 *real_end = i;
4981 /* Draw all strings. */
4982 for (s = head; s; s = s->next)
4983 x_draw_glyph_string (s);
4985 /* Value is the x-position up to which drawn, relative to AREA of W.
4986 This doesn't include parts drawn because of overhangs. */
4987 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4988 if (!row->full_width_p)
4990 if (area > LEFT_MARGIN_AREA)
4991 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4992 if (area > TEXT_AREA)
4993 x_reached -= window_box_width (w, TEXT_AREA);
4996 release_frame_dc (XFRAME (WINDOW_FRAME (w)), hdc);
4998 return x_reached;
5002 /* Fix the display of area AREA of overlapping row ROW in window W. */
5004 static void
5005 x_fix_overlapping_area (w, row, area)
5006 struct window *w;
5007 struct glyph_row *row;
5008 enum glyph_row_area area;
5010 int i, x;
5012 BLOCK_INPUT;
5014 if (area == LEFT_MARGIN_AREA)
5015 x = 0;
5016 else if (area == TEXT_AREA)
5017 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5018 else
5019 x = (window_box_width (w, LEFT_MARGIN_AREA)
5020 + window_box_width (w, TEXT_AREA));
5022 for (i = 0; i < row->used[area];)
5024 if (row->glyphs[area][i].overlaps_vertically_p)
5026 int start = i, start_x = x;
5030 x += row->glyphs[area][i].pixel_width;
5031 ++i;
5033 while (i < row->used[area]
5034 && row->glyphs[area][i].overlaps_vertically_p);
5036 x_draw_glyphs (w, start_x, row, area, start, i,
5037 (row->inverse_p
5038 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5039 NULL, NULL, 1);
5041 else
5043 x += row->glyphs[area][i].pixel_width;
5044 ++i;
5048 UNBLOCK_INPUT;
5052 /* Output LEN glyphs starting at START at the nominal cursor position.
5053 Advance the nominal cursor over the text. The global variable
5054 updated_window contains the window being updated, updated_row is
5055 the glyph row being updated, and updated_area is the area of that
5056 row being updated. */
5058 static void
5059 x_write_glyphs (start, len)
5060 struct glyph *start;
5061 int len;
5063 int x, hpos, real_start, real_end;
5065 xassert (updated_window && updated_row);
5066 BLOCK_INPUT;
5068 /* Write glyphs. */
5070 hpos = start - updated_row->glyphs[updated_area];
5071 x = x_draw_glyphs (updated_window, output_cursor.x,
5072 updated_row, updated_area,
5073 hpos, hpos + len,
5074 (updated_row->inverse_p
5075 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5076 &real_start, &real_end, 0);
5078 /* If we drew over the cursor, note that it is not visible any more. */
5079 note_overwritten_text_cursor (updated_window, real_start,
5080 real_end - real_start);
5082 UNBLOCK_INPUT;
5084 /* Advance the output cursor. */
5085 output_cursor.hpos += len;
5086 output_cursor.x = x;
5090 /* Insert LEN glyphs from START at the nominal cursor position. */
5092 static void
5093 x_insert_glyphs (start, len)
5094 struct glyph *start;
5095 register int len;
5097 struct frame *f;
5098 struct window *w;
5099 int line_height, shift_by_width, shifted_region_width;
5100 struct glyph_row *row;
5101 struct glyph *glyph;
5102 int frame_x, frame_y, hpos, real_start, real_end;
5103 HDC hdc;
5105 xassert (updated_window && updated_row);
5106 BLOCK_INPUT;
5107 w = updated_window;
5108 f = XFRAME (WINDOW_FRAME (w));
5109 hdc = get_frame_dc (f);
5111 /* Get the height of the line we are in. */
5112 row = updated_row;
5113 line_height = row->height;
5115 /* Get the width of the glyphs to insert. */
5116 shift_by_width = 0;
5117 for (glyph = start; glyph < start + len; ++glyph)
5118 shift_by_width += glyph->pixel_width;
5120 /* Get the width of the region to shift right. */
5121 shifted_region_width = (window_box_width (w, updated_area)
5122 - output_cursor.x
5123 - shift_by_width);
5125 /* Shift right. */
5126 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5127 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5128 BitBlt (hdc, frame_x + shift_by_width, frame_y,
5129 shifted_region_width, line_height,
5130 hdc, frame_x, frame_y, SRCCOPY);
5132 /* Write the glyphs. */
5133 hpos = start - row->glyphs[updated_area];
5134 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5135 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5136 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5138 /* Advance the output cursor. */
5139 output_cursor.hpos += len;
5140 output_cursor.x += shift_by_width;
5141 release_frame_dc (f, hdc);
5143 UNBLOCK_INPUT;
5147 /* Delete N glyphs at the nominal cursor position. Not implemented
5148 for X frames. */
5150 static void
5151 x_delete_glyphs (n)
5152 register int n;
5154 abort ();
5158 /* Erase the current text line from the nominal cursor position
5159 (inclusive) to pixel column TO_X (exclusive). The idea is that
5160 everything from TO_X onward is already erased.
5162 TO_X is a pixel position relative to updated_area of
5163 updated_window. TO_X == -1 means clear to the end of this area. */
5165 static void
5166 x_clear_end_of_line (to_x)
5167 int to_x;
5169 struct frame *f;
5170 struct window *w = updated_window;
5171 int max_x, min_y, max_y;
5172 int from_x, from_y, to_y;
5174 xassert (updated_window && updated_row);
5175 f = XFRAME (w->frame);
5177 if (updated_row->full_width_p)
5179 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5180 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5181 && !w->pseudo_window_p)
5182 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5184 else
5185 max_x = window_box_width (w, updated_area);
5186 max_y = window_text_bottom_y (w);
5188 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5189 of window. For TO_X > 0, truncate to end of drawing area. */
5190 if (to_x == 0)
5191 return;
5192 else if (to_x < 0)
5193 to_x = max_x;
5194 else
5195 to_x = min (to_x, max_x);
5197 to_y = min (max_y, output_cursor.y + updated_row->height);
5199 /* Notice if the cursor will be cleared by this operation. */
5200 if (!updated_row->full_width_p)
5201 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5203 from_x = output_cursor.x;
5205 /* Translate to frame coordinates. */
5206 if (updated_row->full_width_p)
5208 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5209 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5211 else
5213 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5214 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5217 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5218 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5219 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5221 /* Prevent inadvertently clearing to end of the X window. */
5222 if (to_x > from_x && to_y > from_y)
5224 HDC hdc;
5225 BLOCK_INPUT;
5226 hdc = get_frame_dc (f);
5228 w32_clear_area (f, hdc, from_x, from_y, to_x - from_x, to_y - from_y);
5229 release_frame_dc (f, hdc);
5230 UNBLOCK_INPUT;
5235 /* Clear entire frame. If updating_frame is non-null, clear that
5236 frame. Otherwise clear the selected frame. */
5238 static void
5239 x_clear_frame ()
5241 struct frame *f;
5243 if (updating_frame)
5244 f = updating_frame;
5245 else
5246 f = SELECTED_FRAME ();
5248 /* Clearing the frame will erase any cursor, so mark them all as no
5249 longer visible. */
5250 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5251 output_cursor.hpos = output_cursor.vpos = 0;
5252 output_cursor.x = -1;
5254 /* We don't set the output cursor here because there will always
5255 follow an explicit cursor_to. */
5256 BLOCK_INPUT;
5258 w32_clear_window (f);
5260 /* We have to clear the scroll bars, too. If we have changed
5261 colors or something like that, then they should be notified. */
5262 x_scroll_bar_clear (f);
5264 UNBLOCK_INPUT;
5268 /* Make audible bell. */
5270 static void
5271 w32_ring_bell (void)
5273 BLOCK_INPUT;
5275 if (visible_bell)
5277 int i;
5278 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
5280 for (i = 0; i < 5; i++)
5282 FlashWindow (hwnd, TRUE);
5283 Sleep (10);
5285 FlashWindow (hwnd, FALSE);
5287 else
5288 w32_sys_ring_bell ();
5290 UNBLOCK_INPUT;
5294 /* Specify how many text lines, from the top of the window,
5295 should be affected by insert-lines and delete-lines operations.
5296 This, and those operations, are used only within an update
5297 that is bounded by calls to x_update_begin and x_update_end. */
5299 static void
5300 w32_set_terminal_window (n)
5301 register int n;
5303 /* This function intentionally left blank. */
5308 /***********************************************************************
5309 Line Dance
5310 ***********************************************************************/
5312 /* Perform an insert-lines or delete-lines operation, inserting N
5313 lines or deleting -N lines at vertical position VPOS. */
5315 static void
5316 x_ins_del_lines (vpos, n)
5317 int vpos, n;
5319 abort ();
5323 /* Scroll part of the display as described by RUN. */
5325 static void
5326 x_scroll_run (w, run)
5327 struct window *w;
5328 struct run *run;
5330 struct frame *f = XFRAME (w->frame);
5331 int x, y, width, height, from_y, to_y, bottom_y;
5332 HDC hdc = get_frame_dc (f);
5334 /* Get frame-relative bounding box of the text display area of W,
5335 without mode lines. Include in this box the flags areas to the
5336 left and right of W. */
5337 window_box (w, -1, &x, &y, &width, &height);
5338 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5339 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5341 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5342 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5343 bottom_y = y + height;
5345 if (to_y < from_y)
5347 /* Scrolling up. Make sure we don't copy part of the mode
5348 line at the bottom. */
5349 if (from_y + run->height > bottom_y)
5350 height = bottom_y - from_y;
5351 else
5352 height = run->height;
5354 else
5356 /* Scolling down. Make sure we don't copy over the mode line.
5357 at the bottom. */
5358 if (to_y + run->height > bottom_y)
5359 height = bottom_y - to_y;
5360 else
5361 height = run->height;
5364 BLOCK_INPUT;
5366 /* Cursor off. Will be switched on again in x_update_window_end. */
5367 updated_window = w;
5368 x_clear_cursor (w);
5370 BitBlt (hdc, x, to_y, width, height, hdc, x, from_y, SRCCOPY);
5372 UNBLOCK_INPUT;
5373 release_frame_dc (f, hdc);
5378 /***********************************************************************
5379 Exposure Events
5380 ***********************************************************************/
5382 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5383 corner of the exposed rectangle. W and H are width and height of
5384 the exposed area. All are pixel values. W or H zero means redraw
5385 the entire frame. */
5387 static void
5388 expose_frame (f, x, y, w, h)
5389 struct frame *f;
5390 int x, y, w, h;
5392 RECT r;
5394 TRACE ((stderr, "expose_frame "));
5396 /* No need to redraw if frame will be redrawn soon. */
5397 if (FRAME_GARBAGED_P (f))
5399 TRACE ((stderr, " garbaged\n"));
5400 return;
5403 /* If basic faces haven't been realized yet, there is no point in
5404 trying to redraw anything. This can happen when we get an expose
5405 event while Emacs is starting, e.g. by moving another window. */
5406 if (FRAME_FACE_CACHE (f) == NULL
5407 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5409 TRACE ((stderr, " no faces\n"));
5410 return;
5413 if (w == 0 || h == 0)
5415 r.left = r.top = 0;
5416 r.right = CANON_X_UNIT (f) * f->width;
5417 r.bottom = CANON_Y_UNIT (f) * f->height;
5419 else
5421 r.left = x;
5422 r.top = y;
5423 r.right = x + w;
5424 r.bottom = y + h;
5427 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.left, r.top, r.right, r.bottom));
5428 expose_window_tree (XWINDOW (f->root_window), &r);
5430 if (WINDOWP (f->tool_bar_window))
5432 struct window *w = XWINDOW (f->tool_bar_window);
5433 RECT window_rect;
5434 RECT intersection_rect;
5435 int window_x, window_y, window_width, window_height;
5437 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5438 window_rect.left = window_x;
5439 window_rect.top = window_y;
5440 window_rect.right = window_x + window_width;
5441 window_rect.bottom = window_y + window_height;
5443 if (IntersectRect (&intersection_rect, &r, &window_rect))
5444 expose_window (w, &intersection_rect);
5449 /* Redraw (parts) of all windows in the window tree rooted at W that
5450 intersect R. R contains frame pixel coordinates. */
5452 static void
5453 expose_window_tree (w, r)
5454 struct window *w;
5455 RECT *r;
5457 while (w)
5459 if (!NILP (w->hchild))
5460 expose_window_tree (XWINDOW (w->hchild), r);
5461 else if (!NILP (w->vchild))
5462 expose_window_tree (XWINDOW (w->vchild), r);
5463 else
5465 RECT window_rect;
5466 RECT intersection_rect;
5467 struct frame *f = XFRAME (w->frame);
5468 int window_x, window_y, window_width, window_height;
5470 /* Frame-relative pixel rectangle of W. */
5471 window_box (w, -1, &window_x, &window_y, &window_width,
5472 &window_height);
5473 window_rect.left
5474 = (window_x
5475 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5476 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_Y_UNIT (f));
5477 window_rect.top = window_y;
5478 window_rect.right = window_rect.left
5479 + (window_width
5480 + FRAME_X_FLAGS_AREA_WIDTH (f)
5481 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5482 window_rect.bottom = window_rect.top
5483 + window_height + CURRENT_MODE_LINE_HEIGHT (w);
5485 if (IntersectRect (&intersection_rect, r, &window_rect))
5486 expose_window (w, &intersection_rect);
5489 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5494 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5495 which intersects rectangle R. R is in window-relative coordinates. */
5497 static void
5498 expose_area (w, row, r, area)
5499 struct window *w;
5500 struct glyph_row *row;
5501 RECT *r;
5502 enum glyph_row_area area;
5504 struct glyph *first = row->glyphs[area];
5505 struct glyph *end = row->glyphs[area] + row->used[area];
5506 struct glyph *last;
5507 int first_x, start_x, x;
5509 /* Set x to the window-relative start position for drawing glyphs of
5510 AREA. The first glyph of the text area can be partially visible.
5511 The first glyphs of other areas cannot. */
5512 if (area == LEFT_MARGIN_AREA)
5513 x = 0;
5514 else if (area == TEXT_AREA)
5515 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5516 else
5517 x = (window_box_width (w, LEFT_MARGIN_AREA)
5518 + window_box_width (w, TEXT_AREA));
5520 if (area == TEXT_AREA && row->fill_line_p)
5521 /* If row extends face to end of line write the whole line. */
5522 x_draw_glyphs (w, x, row, area,
5523 0, row->used[area],
5524 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5525 NULL, NULL, 0);
5526 else
5528 /* Set START_X to the window-relative start position for drawing glyphs of
5529 AREA. The first glyph of the text area can be partially visible.
5530 The first glyphs of other areas cannot. */
5531 if (area == LEFT_MARGIN_AREA)
5532 start_x = 0;
5533 else if (area == TEXT_AREA)
5534 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5535 else
5536 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5537 + window_box_width (w, TEXT_AREA));
5538 x = start_x;
5540 /* Find the first glyph that must be redrawn. */
5541 while (first < end
5542 && x + first->pixel_width < r->left)
5544 x += first->pixel_width;
5545 ++first;
5548 /* Find the last one. */
5549 last = first;
5550 first_x = x;
5551 while (last < end
5552 && x < r->right)
5554 x += last->pixel_width;
5555 ++last;
5558 /* Repaint. */
5559 if (last > first)
5560 x_draw_glyphs (w, first_x - start_x, row, area,
5561 first - row->glyphs[area],
5562 last - row->glyphs[area],
5563 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5564 NULL, NULL, 0);
5569 /* Redraw the parts of the glyph row ROW on window W intersecting
5570 rectangle R. R is in window-relative coordinates. */
5572 static void
5573 expose_line (w, row, r)
5574 struct window *w;
5575 struct glyph_row *row;
5576 RECT *r;
5578 xassert (row->enabled_p);
5580 if (row->mode_line_p || w->pseudo_window_p)
5581 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5582 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5583 NULL, NULL, 0);
5584 else
5586 if (row->used[LEFT_MARGIN_AREA])
5587 expose_area (w, row, r, LEFT_MARGIN_AREA);
5588 if (row->used[TEXT_AREA])
5589 expose_area (w, row, r, TEXT_AREA);
5590 if (row->used[RIGHT_MARGIN_AREA])
5591 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5592 x_draw_row_bitmaps (w, row);
5597 /* Return non-zero if W's cursor intersects rectangle R. */
5599 static int
5600 x_phys_cursor_in_rect_p (w, r)
5601 struct window *w;
5602 RECT *r;
5604 RECT cr, result;
5605 struct glyph *cursor_glyph;
5607 cursor_glyph = get_phys_cursor_glyph (w);
5608 if (cursor_glyph)
5610 cr.left = w->phys_cursor.x;
5611 cr.top = w->phys_cursor.y;
5612 cr.right = cr.left + cursor_glyph->pixel_width;
5613 cr.bottom = cr.top + w->phys_cursor_height;
5614 return IntersectRect (&result, &cr, r);
5616 else
5617 return 0;
5621 /* Redraw a rectangle of window W. R is a rectangle in window
5622 relative coordinates. Call this function with input blocked. */
5624 static void
5625 expose_window (w, r)
5626 struct window *w;
5627 RECT *r;
5629 struct glyph_row *row;
5630 int y;
5631 int yb = window_text_bottom_y (w);
5632 int cursor_cleared_p;
5634 /* If window is not yet fully initialized, do nothing. This can
5635 happen when toolkit scroll bars are used and a window is split.
5636 Reconfiguring the scroll bar will generate an expose for a newly
5637 created window. */
5638 if (w->current_matrix == NULL)
5639 return;
5641 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5642 r->left, r->top, r->right, r->bottom));
5644 /* Convert to window coordinates. */
5645 r->left = FRAME_TO_WINDOW_PIXEL_X (w, r->left);
5646 r->top = FRAME_TO_WINDOW_PIXEL_Y (w, r->top);
5647 r->right = FRAME_TO_WINDOW_PIXEL_X (w, r->right);
5648 r->bottom = FRAME_TO_WINDOW_PIXEL_Y (w, r->bottom);
5650 /* Turn off the cursor. */
5651 if (!w->pseudo_window_p
5652 && x_phys_cursor_in_rect_p (w, r))
5654 x_clear_cursor (w);
5655 cursor_cleared_p = 1;
5657 else
5658 cursor_cleared_p = 0;
5660 /* Find the first row intersecting the rectangle R. */
5661 row = w->current_matrix->rows;
5662 y = 0;
5663 while (row->enabled_p
5664 && y < yb
5665 && y + row->height < r->top)
5667 y += row->height;
5668 ++row;
5671 /* Display the text in the rectangle, one text line at a time. */
5672 while (row->enabled_p
5673 && y < yb
5674 && y < r->bottom)
5676 expose_line (w, row, r);
5677 y += row->height;
5678 ++row;
5681 /* Display the mode line if there is one. */
5682 if (WINDOW_WANTS_MODELINE_P (w)
5683 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5684 row->enabled_p)
5685 && row->y < r->bottom)
5686 expose_line (w, row, r);
5688 if (!w->pseudo_window_p)
5690 /* Draw border between windows. */
5691 x_draw_vertical_border (w);
5693 /* Turn the cursor on again. */
5694 if (cursor_cleared_p)
5695 x_update_window_cursor (w, 1);
5700 static void
5701 frame_highlight (f)
5702 struct frame *f;
5704 x_update_cursor (f, 1);
5707 static void
5708 frame_unhighlight (f)
5709 struct frame *f;
5711 x_update_cursor (f, 1);
5714 /* The focus has changed. Update the frames as necessary to reflect
5715 the new situation. Note that we can't change the selected frame
5716 here, because the Lisp code we are interrupting might become confused.
5717 Each event gets marked with the frame in which it occurred, so the
5718 Lisp code can tell when the switch took place by examining the events. */
5720 static void
5721 x_new_focus_frame (dpyinfo, frame)
5722 struct w32_display_info *dpyinfo;
5723 struct frame *frame;
5725 struct frame *old_focus = dpyinfo->w32_focus_frame;
5727 if (frame != dpyinfo->w32_focus_frame)
5729 /* Set this before calling other routines, so that they see
5730 the correct value of w32_focus_frame. */
5731 dpyinfo->w32_focus_frame = frame;
5733 if (old_focus && old_focus->auto_lower)
5734 x_lower_frame (old_focus);
5736 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
5737 pending_autoraise_frame = dpyinfo->w32_focus_frame;
5738 else
5739 pending_autoraise_frame = 0;
5742 x_frame_rehighlight (dpyinfo);
5745 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5747 void
5748 x_mouse_leave (dpyinfo)
5749 struct w32_display_info *dpyinfo;
5751 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
5754 /* The focus has changed, or we have redirected a frame's focus to
5755 another frame (this happens when a frame uses a surrogate
5756 mini-buffer frame). Shift the highlight as appropriate.
5758 The FRAME argument doesn't necessarily have anything to do with which
5759 frame is being highlighted or un-highlighted; we only use it to find
5760 the appropriate X display info. */
5762 static void
5763 w32_frame_rehighlight (frame)
5764 struct frame *frame;
5766 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
5769 static void
5770 x_frame_rehighlight (dpyinfo)
5771 struct w32_display_info *dpyinfo;
5773 struct frame *old_highlight = dpyinfo->w32_highlight_frame;
5775 if (dpyinfo->w32_focus_frame)
5777 dpyinfo->w32_highlight_frame
5778 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
5779 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
5780 : dpyinfo->w32_focus_frame);
5781 if (! FRAME_LIVE_P (dpyinfo->w32_highlight_frame))
5783 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
5784 dpyinfo->w32_highlight_frame = dpyinfo->w32_focus_frame;
5787 else
5788 dpyinfo->w32_highlight_frame = 0;
5790 if (dpyinfo->w32_highlight_frame != old_highlight)
5792 if (old_highlight)
5793 frame_unhighlight (old_highlight);
5794 if (dpyinfo->w32_highlight_frame)
5795 frame_highlight (dpyinfo->w32_highlight_frame);
5799 /* Keyboard processing - modifier keys, etc. */
5801 /* Convert a keysym to its name. */
5803 char *
5804 x_get_keysym_name (keysym)
5805 int keysym;
5807 /* Make static so we can always return it */
5808 static char value[100];
5810 BLOCK_INPUT;
5811 GetKeyNameText (keysym, value, 100);
5812 UNBLOCK_INPUT;
5814 return value;
5819 /* Mouse clicks and mouse movement. Rah. */
5821 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5822 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5823 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5824 not force the value into range. */
5826 void
5827 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5828 FRAME_PTR f;
5829 register int pix_x, pix_y;
5830 register int *x, *y;
5831 RECT *bounds;
5832 int noclip;
5834 /* Support tty mode: if Vwindow_system is nil, behave correctly. */
5835 if (NILP (Vwindow_system))
5837 *x = pix_x;
5838 *y = pix_y;
5839 return;
5842 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5843 even for negative values. */
5844 if (pix_x < 0)
5845 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
5846 if (pix_y < 0)
5847 pix_y -= (f)->output_data.w32->line_height - 1;
5849 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5850 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5852 if (bounds)
5854 bounds->left = CHAR_TO_PIXEL_COL (f, pix_x);
5855 bounds->top = CHAR_TO_PIXEL_ROW (f, pix_y);
5856 bounds->right = bounds->left + FONT_WIDTH (FRAME_FONT (f)) - 1;
5857 bounds->bottom = bounds->top + f->output_data.w32->line_height - 1;
5860 if (!noclip)
5862 if (pix_x < 0)
5863 pix_x = 0;
5864 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5865 pix_x = FRAME_WINDOW_WIDTH (f);
5867 if (pix_y < 0)
5868 pix_y = 0;
5869 else if (pix_y > f->height)
5870 pix_y = f->height;
5873 *x = pix_x;
5874 *y = pix_y;
5878 /* Given HPOS/VPOS in the current matrix of W, return corresponding
5879 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5880 can't tell the positions because W's display is not up to date,
5881 return 0. */
5884 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5885 struct window *w;
5886 int hpos, vpos;
5887 int *frame_x, *frame_y;
5889 int success_p;
5891 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5892 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5894 if (display_completed)
5896 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5897 struct glyph *glyph = row->glyphs[TEXT_AREA];
5898 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5900 *frame_y = row->y;
5901 *frame_x = row->x;
5902 while (glyph < end)
5904 *frame_x += glyph->pixel_width;
5905 ++glyph;
5908 success_p = 1;
5910 else
5912 *frame_y = *frame_x = 0;
5913 success_p = 0;
5916 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5917 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5918 return success_p;
5921 BOOL
5922 parse_button (message, pbutton, pup)
5923 int message;
5924 int * pbutton;
5925 int * pup;
5927 int button = 0;
5928 int up = 0;
5930 switch (message)
5932 case WM_LBUTTONDOWN:
5933 button = 0;
5934 up = 0;
5935 break;
5936 case WM_LBUTTONUP:
5937 button = 0;
5938 up = 1;
5939 break;
5940 case WM_MBUTTONDOWN:
5941 if (NILP (Vw32_swap_mouse_buttons))
5942 button = 1;
5943 else
5944 button = 2;
5945 up = 0;
5946 break;
5947 case WM_MBUTTONUP:
5948 if (NILP (Vw32_swap_mouse_buttons))
5949 button = 1;
5950 else
5951 button = 2;
5952 up = 1;
5953 break;
5954 case WM_RBUTTONDOWN:
5955 if (NILP (Vw32_swap_mouse_buttons))
5956 button = 2;
5957 else
5958 button = 1;
5959 up = 0;
5960 break;
5961 case WM_RBUTTONUP:
5962 if (NILP (Vw32_swap_mouse_buttons))
5963 button = 2;
5964 else
5965 button = 1;
5966 up = 1;
5967 break;
5968 default:
5969 return (FALSE);
5972 if (pup) *pup = up;
5973 if (pbutton) *pbutton = button;
5975 return (TRUE);
5979 /* Prepare a mouse-event in *RESULT for placement in the input queue.
5981 If the event is a button press, then note that we have grabbed
5982 the mouse. */
5984 static Lisp_Object
5985 construct_mouse_click (result, msg, f)
5986 struct input_event *result;
5987 W32Msg *msg;
5988 struct frame *f;
5990 int button;
5991 int up;
5993 parse_button (msg->msg.message, &button, &up);
5995 /* Make the event type no_event; we'll change that when we decide
5996 otherwise. */
5997 result->kind = mouse_click;
5998 result->code = button;
5999 result->timestamp = msg->msg.time;
6000 result->modifiers = (msg->dwModifiers
6001 | (up
6002 ? up_modifier
6003 : down_modifier));
6005 XSETINT (result->x, LOWORD (msg->msg.lParam));
6006 XSETINT (result->y, HIWORD (msg->msg.lParam));
6007 XSETFRAME (result->frame_or_window, f);
6008 result->arg = Qnil;
6009 return Qnil;
6012 static Lisp_Object
6013 construct_mouse_wheel (result, msg, f)
6014 struct input_event *result;
6015 W32Msg *msg;
6016 struct frame *f;
6018 POINT p;
6019 result->kind = mouse_wheel;
6020 result->code = (short) HIWORD (msg->msg.wParam);
6021 result->timestamp = msg->msg.time;
6022 result->modifiers = msg->dwModifiers;
6023 p.x = LOWORD (msg->msg.lParam);
6024 p.y = HIWORD (msg->msg.lParam);
6025 ScreenToClient (msg->msg.hwnd, &p);
6026 XSETINT (result->x, p.x);
6027 XSETINT (result->y, p.y);
6028 XSETFRAME (result->frame_or_window, f);
6029 result->arg = Qnil;
6030 return Qnil;
6033 static Lisp_Object
6034 construct_drag_n_drop (result, msg, f)
6035 struct input_event *result;
6036 W32Msg *msg;
6037 struct frame *f;
6039 Lisp_Object files;
6040 Lisp_Object frame;
6041 HDROP hdrop;
6042 POINT p;
6043 WORD num_files;
6044 char *name;
6045 int i, len;
6047 result->kind = drag_n_drop;
6048 result->code = 0;
6049 result->timestamp = msg->msg.time;
6050 result->modifiers = msg->dwModifiers;
6052 hdrop = (HDROP) msg->msg.wParam;
6053 DragQueryPoint (hdrop, &p);
6055 #if 0
6056 p.x = LOWORD (msg->msg.lParam);
6057 p.y = HIWORD (msg->msg.lParam);
6058 ScreenToClient (msg->msg.hwnd, &p);
6059 #endif
6061 XSETINT (result->x, p.x);
6062 XSETINT (result->y, p.y);
6064 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
6065 files = Qnil;
6067 for (i = 0; i < num_files; i++)
6069 len = DragQueryFile (hdrop, i, NULL, 0);
6070 if (len <= 0)
6071 continue;
6072 name = alloca (len + 1);
6073 DragQueryFile (hdrop, i, name, len + 1);
6074 files = Fcons (build_string (name), files);
6077 DragFinish (hdrop);
6079 XSETFRAME (frame, f);
6080 result->frame_or_window = Fcons (frame, files);
6081 result->arg = Qnil;
6082 return Qnil;
6086 /* Function to report a mouse movement to the mainstream Emacs code.
6087 The input handler calls this.
6089 We have received a mouse movement event, which is given in *event.
6090 If the mouse is over a different glyph than it was last time, tell
6091 the mainstream emacs code by setting mouse_moved. If not, ask for
6092 another motion event, so we can check again the next time it moves. */
6094 static MSG last_mouse_motion_event;
6095 static Lisp_Object last_mouse_motion_frame;
6097 static void
6098 note_mouse_movement (frame, msg)
6099 FRAME_PTR frame;
6100 MSG *msg;
6102 last_mouse_movement_time = msg->time;
6103 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
6104 XSETFRAME (last_mouse_motion_frame, frame);
6106 if (msg->hwnd != FRAME_W32_WINDOW (frame))
6108 frame->mouse_moved = 1;
6109 last_mouse_scroll_bar = Qnil;
6110 note_mouse_highlight (frame, -1, -1);
6113 /* Has the mouse moved off the glyph it was on at the last sighting? */
6114 else if (LOWORD (msg->lParam) < last_mouse_glyph.left
6115 || LOWORD (msg->lParam) > last_mouse_glyph.right
6116 || HIWORD (msg->lParam) < last_mouse_glyph.top
6117 || HIWORD (msg->lParam) > last_mouse_glyph.bottom)
6119 frame->mouse_moved = 1;
6120 last_mouse_scroll_bar = Qnil;
6122 note_mouse_highlight (frame, LOWORD (msg->lParam), HIWORD (msg->lParam));
6126 /* This is used for debugging, to turn off note_mouse_highlight. */
6128 int disable_mouse_highlight;
6132 /************************************************************************
6133 Mouse Face
6134 ************************************************************************/
6136 /* Find the glyph under window-relative coordinates X/Y in window W.
6137 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6138 strings. Return in *HPOS and *VPOS the row and column number of
6139 the glyph found. Return in *AREA the glyph area containing X.
6140 Value is a pointer to the glyph found or null if X/Y is not on
6141 text, or we can't tell because W's current matrix is not up to
6142 date. */
6144 static struct glyph *
6145 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6146 struct window *w;
6147 int x, y;
6148 int *hpos, *vpos, *area;
6150 struct glyph *glyph, *end;
6151 struct glyph_row *row = NULL;
6152 int x0, i, left_area_width;
6154 /* Find row containing Y. Give up if some row is not enabled. */
6155 for (i = 0; i < w->current_matrix->nrows; ++i)
6157 row = MATRIX_ROW (w->current_matrix, i);
6158 if (!row->enabled_p)
6159 return NULL;
6160 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6161 break;
6164 *vpos = i;
6165 *hpos = 0;
6167 /* Give up if Y is not in the window. */
6168 if (i == w->current_matrix->nrows)
6169 return NULL;
6171 /* Get the glyph area containing X. */
6172 if (w->pseudo_window_p)
6174 *area = TEXT_AREA;
6175 x0 = 0;
6177 else
6179 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6180 if (x < left_area_width)
6182 *area = LEFT_MARGIN_AREA;
6183 x0 = 0;
6185 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6187 *area = TEXT_AREA;
6188 x0 = row->x + left_area_width;
6190 else
6192 *area = RIGHT_MARGIN_AREA;
6193 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6197 /* Find glyph containing X. */
6198 glyph = row->glyphs[*area];
6199 end = glyph + row->used[*area];
6200 while (glyph < end)
6202 if (x < x0 + glyph->pixel_width)
6204 if (w->pseudo_window_p)
6205 break;
6206 else if (BUFFERP (glyph->object))
6207 break;
6210 x0 += glyph->pixel_width;
6211 ++glyph;
6214 if (glyph == end)
6215 return NULL;
6217 *hpos = glyph - row->glyphs[*area];
6218 return glyph;
6222 /* Convert frame-relative x/y to coordinates relative to window W.
6223 Takes pseudo-windows into account. */
6225 static void
6226 frame_to_window_pixel_xy (w, x, y)
6227 struct window *w;
6228 int *x, *y;
6230 if (w->pseudo_window_p)
6232 /* A pseudo-window is always full-width, and starts at the
6233 left edge of the frame, plus a frame border. */
6234 struct frame *f = XFRAME (w->frame);
6235 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6236 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6238 else
6240 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6241 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6246 /* Take proper action when mouse has moved to the mode or top line of
6247 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6248 mode line. X is relative to the start of the text display area of
6249 W, so the width of bitmap areas and scroll bars must be subtracted
6250 to get a position relative to the start of the mode line. */
6252 static void
6253 note_mode_line_highlight (w, x, mode_line_p)
6254 struct window *w;
6255 int x, mode_line_p;
6257 struct frame *f = XFRAME (w->frame);
6258 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6259 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6260 struct glyph_row *row;
6262 if (mode_line_p)
6263 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6264 else
6265 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6267 if (row->enabled_p)
6269 struct glyph *glyph, *end;
6270 Lisp_Object help, map;
6271 int x0;
6273 /* Find the glyph under X. */
6274 glyph = row->glyphs[TEXT_AREA];
6275 end = glyph + row->used[TEXT_AREA];
6276 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6277 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6278 while (glyph < end
6279 && x >= x0 + glyph->pixel_width)
6281 x0 += glyph->pixel_width;
6282 ++glyph;
6285 if (glyph < end
6286 && STRINGP (glyph->object)
6287 && XSTRING (glyph->object)->intervals
6288 && glyph->charpos >= 0
6289 && glyph->charpos < XSTRING (glyph->object)->size)
6291 /* If we're on a string with `help-echo' text property,
6292 arrange for the help to be displayed. This is done by
6293 setting the global variable help_echo to the help string. */
6294 help = Fget_text_property (make_number (glyph->charpos),
6295 Qhelp_echo, glyph->object);
6296 if (!NILP (help))
6298 help_echo = help;
6299 XSETWINDOW (help_echo_window, w);
6300 help_echo_object = glyph->object;
6301 help_echo_pos = glyph->charpos;
6304 /* Change the mouse pointer according to what is under X/Y. */
6305 map = Fget_text_property (make_number (glyph->charpos),
6306 Qlocal_map, glyph->object);
6307 if (KEYMAPP (map))
6308 cursor = f->output_data.w32->nontext_cursor;
6312 #if 0 /* TODO: mouse cursor */
6313 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6314 #endif
6318 /* Take proper action when the mouse has moved to position X, Y on
6319 frame F as regards highlighting characters that have mouse-face
6320 properties. Also de-highlighting chars where the mouse was before.
6321 X and Y can be negative or out of range. */
6323 static void
6324 note_mouse_highlight (f, x, y)
6325 struct frame *f;
6326 int x, y;
6328 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6329 int portion;
6330 Lisp_Object window;
6331 struct window *w;
6333 /* When a menu is active, don't highlight because this looks odd. */
6334 if (popup_activated ())
6335 return;
6337 if (disable_mouse_highlight
6338 || !f->glyphs_initialized_p)
6339 return;
6341 dpyinfo->mouse_face_mouse_x = x;
6342 dpyinfo->mouse_face_mouse_y = y;
6343 dpyinfo->mouse_face_mouse_frame = f;
6345 if (dpyinfo->mouse_face_defer)
6346 return;
6348 if (gc_in_progress)
6350 dpyinfo->mouse_face_deferred_gc = 1;
6351 return;
6354 /* Which window is that in? */
6355 window = window_from_coordinates (f, x, y, &portion, 1);
6357 /* If we were displaying active text in another window, clear that. */
6358 if (! EQ (window, dpyinfo->mouse_face_window))
6359 clear_mouse_face (dpyinfo);
6361 /* Not on a window -> return. */
6362 if (!WINDOWP (window))
6363 return;
6365 /* Convert to window-relative pixel coordinates. */
6366 w = XWINDOW (window);
6367 frame_to_window_pixel_xy (w, &x, &y);
6369 /* Handle tool-bar window differently since it doesn't display a
6370 buffer. */
6371 if (EQ (window, f->tool_bar_window))
6373 note_tool_bar_highlight (f, x, y);
6374 return;
6377 if (portion == 1 || portion == 3)
6379 /* Mouse is on the mode or top line. */
6380 note_mode_line_highlight (w, x, portion == 1);
6381 return;
6383 #if 0 /* TODO: mouse cursor */
6384 else if (portion == 2)
6385 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6386 f->output_data.x->horizontal_drag_cursor);
6387 else
6388 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6389 f->output_data.x->text_cursor);
6390 #endif
6392 /* Are we in a window whose display is up to date?
6393 And verify the buffer's text has not changed. */
6394 if (/* Within text portion of the window. */
6395 portion == 0
6396 && EQ (w->window_end_valid, w->buffer)
6397 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6398 && (XFASTINT (w->last_overlay_modified)
6399 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6401 int hpos, vpos, pos, i, area;
6402 struct glyph *glyph;
6404 /* Find the glyph under X/Y. */
6405 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6407 /* Clear mouse face if X/Y not over text. */
6408 if (glyph == NULL
6409 || area != TEXT_AREA
6410 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6412 clear_mouse_face (dpyinfo);
6413 return;
6416 pos = glyph->charpos;
6417 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6419 /* Check for mouse-face and help-echo. */
6421 Lisp_Object mouse_face, overlay, position;
6422 Lisp_Object *overlay_vec;
6423 int len, noverlays;
6424 struct buffer *obuf;
6425 int obegv, ozv;
6427 /* If we get an out-of-range value, return now; avoid an error. */
6428 if (pos > BUF_Z (XBUFFER (w->buffer)))
6429 return;
6431 /* Make the window's buffer temporarily current for
6432 overlays_at and compute_char_face. */
6433 obuf = current_buffer;
6434 current_buffer = XBUFFER (w->buffer);
6435 obegv = BEGV;
6436 ozv = ZV;
6437 BEGV = BEG;
6438 ZV = Z;
6440 /* Is this char mouse-active or does it have help-echo? */
6441 XSETINT (position, pos);
6443 /* Put all the overlays we want in a vector in overlay_vec.
6444 Store the length in len. If there are more than 10, make
6445 enough space for all, and try again. */
6446 len = 10;
6447 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6448 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6449 if (noverlays > len)
6451 len = noverlays;
6452 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6453 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6456 /* Sort overlays into increasing priority order. */
6457 noverlays = sort_overlays (overlay_vec, noverlays, w);
6459 /* Check mouse-face highlighting. */
6460 if (! (EQ (window, dpyinfo->mouse_face_window)
6461 && vpos >= dpyinfo->mouse_face_beg_row
6462 && vpos <= dpyinfo->mouse_face_end_row
6463 && (vpos > dpyinfo->mouse_face_beg_row
6464 || hpos >= dpyinfo->mouse_face_beg_col)
6465 && (vpos < dpyinfo->mouse_face_end_row
6466 || hpos < dpyinfo->mouse_face_end_col
6467 || dpyinfo->mouse_face_past_end)))
6469 /* Clear the display of the old active region, if any. */
6470 clear_mouse_face (dpyinfo);
6472 /* Find the highest priority overlay that has a mouse-face prop. */
6473 overlay = Qnil;
6474 for (i = noverlays - 1; i >= 0; --i)
6476 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6477 if (!NILP (mouse_face))
6479 overlay = overlay_vec[i];
6480 break;
6484 /* If no overlay applies, get a text property. */
6485 if (NILP (overlay))
6486 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6488 /* Handle the overlay case. */
6489 if (! NILP (overlay))
6491 /* Find the range of text around this char that
6492 should be active. */
6493 Lisp_Object before, after;
6494 int ignore;
6496 before = Foverlay_start (overlay);
6497 after = Foverlay_end (overlay);
6498 /* Record this as the current active region. */
6499 fast_find_position (w, XFASTINT (before),
6500 &dpyinfo->mouse_face_beg_col,
6501 &dpyinfo->mouse_face_beg_row,
6502 &dpyinfo->mouse_face_beg_x,
6503 &dpyinfo->mouse_face_beg_y);
6504 dpyinfo->mouse_face_past_end
6505 = !fast_find_position (w, XFASTINT (after),
6506 &dpyinfo->mouse_face_end_col,
6507 &dpyinfo->mouse_face_end_row,
6508 &dpyinfo->mouse_face_end_x,
6509 &dpyinfo->mouse_face_end_y);
6510 dpyinfo->mouse_face_window = window;
6511 dpyinfo->mouse_face_face_id
6512 = face_at_buffer_position (w, pos, 0, 0,
6513 &ignore, pos + 1, 1);
6515 /* Display it as active. */
6516 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6518 /* Handle the text property case. */
6519 else if (! NILP (mouse_face))
6521 /* Find the range of text around this char that
6522 should be active. */
6523 Lisp_Object before, after, beginning, end;
6524 int ignore;
6526 beginning = Fmarker_position (w->start);
6527 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6528 - XFASTINT (w->window_end_pos)));
6529 before
6530 = Fprevious_single_property_change (make_number (pos + 1),
6531 Qmouse_face,
6532 w->buffer, beginning);
6533 after
6534 = Fnext_single_property_change (position, Qmouse_face,
6535 w->buffer, end);
6536 /* Record this as the current active region. */
6537 fast_find_position (w, XFASTINT (before),
6538 &dpyinfo->mouse_face_beg_col,
6539 &dpyinfo->mouse_face_beg_row,
6540 &dpyinfo->mouse_face_beg_x,
6541 &dpyinfo->mouse_face_beg_y);
6542 dpyinfo->mouse_face_past_end
6543 = !fast_find_position (w, XFASTINT (after),
6544 &dpyinfo->mouse_face_end_col,
6545 &dpyinfo->mouse_face_end_row,
6546 &dpyinfo->mouse_face_end_x,
6547 &dpyinfo->mouse_face_end_y);
6548 dpyinfo->mouse_face_window = window;
6549 dpyinfo->mouse_face_face_id
6550 = face_at_buffer_position (w, pos, 0, 0,
6551 &ignore, pos + 1, 1);
6553 /* Display it as active. */
6554 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6558 /* Look for a `help-echo' property. */
6560 Lisp_Object help, overlay;
6562 /* Check overlays first. */
6563 help = Qnil;
6564 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6566 overlay = overlay_vec[i];
6567 help = Foverlay_get (overlay, Qhelp_echo);
6570 if (!NILP (help))
6572 help_echo = help;
6573 help_echo_window = window;
6574 help_echo_object = overlay;
6575 help_echo_pos = pos;
6577 else
6579 /* Try text properties. */
6580 if ((STRINGP (glyph->object)
6581 && glyph->charpos >= 0
6582 && glyph->charpos < XSTRING (glyph->object)->size)
6583 || (BUFFERP (glyph->object)
6584 && glyph->charpos >= BEGV
6585 && glyph->charpos < ZV))
6586 help = Fget_text_property (make_number (glyph->charpos),
6587 Qhelp_echo, glyph->object);
6589 if (!NILP (help))
6591 help_echo = help;
6592 help_echo_window = window;
6593 help_echo_object = glyph->object;
6594 help_echo_pos = glyph->charpos;
6599 BEGV = obegv;
6600 ZV = ozv;
6601 current_buffer = obuf;
6606 static void
6607 redo_mouse_highlight ()
6609 if (!NILP (last_mouse_motion_frame)
6610 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6611 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6612 LOWORD (last_mouse_motion_event.lParam),
6613 HIWORD (last_mouse_motion_event.lParam));
6618 /***********************************************************************
6619 Tool-bars
6620 ***********************************************************************/
6622 static int x_tool_bar_item P_ ((struct frame *, int, int,
6623 struct glyph **, int *, int *, int *));
6625 /* Tool-bar item index of the item on which a mouse button was pressed
6626 or -1. */
6628 static int last_tool_bar_item;
6631 /* Get information about the tool-bar item at position X/Y on frame F.
6632 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6633 the current matrix of the tool-bar window of F, or NULL if not
6634 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6635 item in F->tool_bar_items. Value is
6637 -1 if X/Y is not on a tool-bar item
6638 0 if X/Y is on the same item that was highlighted before.
6639 1 otherwise. */
6641 static int
6642 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6643 struct frame *f;
6644 int x, y;
6645 struct glyph **glyph;
6646 int *hpos, *vpos, *prop_idx;
6648 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6649 struct window *w = XWINDOW (f->tool_bar_window);
6650 int area;
6652 /* Find the glyph under X/Y. */
6653 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6654 if (*glyph == NULL)
6655 return -1;
6657 /* Get the start of this tool-bar item's properties in
6658 f->tool_bar_items. */
6659 if (!tool_bar_item_info (f, *glyph, prop_idx))
6660 return -1;
6662 /* Is mouse on the highlighted item? */
6663 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6664 && *vpos >= dpyinfo->mouse_face_beg_row
6665 && *vpos <= dpyinfo->mouse_face_end_row
6666 && (*vpos > dpyinfo->mouse_face_beg_row
6667 || *hpos >= dpyinfo->mouse_face_beg_col)
6668 && (*vpos < dpyinfo->mouse_face_end_row
6669 || *hpos < dpyinfo->mouse_face_end_col
6670 || dpyinfo->mouse_face_past_end))
6671 return 0;
6673 return 1;
6677 /* Handle mouse button event on the tool-bar of frame F, at
6678 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6679 or ButtonRelase. */
6681 static void
6682 w32_handle_tool_bar_click (f, button_event)
6683 struct frame *f;
6684 struct input_event *button_event;
6686 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6687 struct window *w = XWINDOW (f->tool_bar_window);
6688 int hpos, vpos, prop_idx;
6689 struct glyph *glyph;
6690 Lisp_Object enabled_p;
6691 int x = XFASTINT (button_event->x);
6692 int y = XFASTINT (button_event->y);
6694 /* If not on the highlighted tool-bar item, return. */
6695 frame_to_window_pixel_xy (w, &x, &y);
6696 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6697 return;
6699 /* If item is disabled, do nothing. */
6700 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6701 if (NILP (enabled_p))
6702 return;
6704 if (button_event->kind == mouse_click)
6706 /* Show item in pressed state. */
6707 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6708 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6709 last_tool_bar_item = prop_idx;
6711 else
6713 Lisp_Object key, frame;
6714 struct input_event event;
6716 /* Show item in released state. */
6717 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6718 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6720 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
6722 XSETFRAME (frame, f);
6723 event.kind = TOOL_BAR_EVENT;
6724 event.frame_or_window = frame;
6725 event.arg = frame;
6726 kbd_buffer_store_event (&event);
6728 event.kind = TOOL_BAR_EVENT;
6729 event.frame_or_window = frame;
6730 event.arg = key;
6731 event.modifiers = button_event->modifiers;
6732 kbd_buffer_store_event (&event);
6733 last_tool_bar_item = -1;
6738 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6739 tool-bar window-relative coordinates X/Y. Called from
6740 note_mouse_highlight. */
6742 static void
6743 note_tool_bar_highlight (f, x, y)
6744 struct frame *f;
6745 int x, y;
6747 Lisp_Object window = f->tool_bar_window;
6748 struct window *w = XWINDOW (window);
6749 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6750 int hpos, vpos;
6751 struct glyph *glyph;
6752 struct glyph_row *row;
6753 int i;
6754 Lisp_Object enabled_p;
6755 int prop_idx;
6756 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6757 int mouse_down_p, rc;
6759 /* Function note_mouse_highlight is called with negative x(y
6760 values when mouse moves outside of the frame. */
6761 if (x <= 0 || y <= 0)
6763 clear_mouse_face (dpyinfo);
6764 return;
6767 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6768 if (rc < 0)
6770 /* Not on tool-bar item. */
6771 clear_mouse_face (dpyinfo);
6772 return;
6774 else if (rc == 0)
6775 /* On same tool-bar item as before. */
6776 goto set_help_echo;
6778 clear_mouse_face (dpyinfo);
6780 /* Mouse is down, but on different tool-bar item? */
6781 mouse_down_p = (dpyinfo->grabbed
6782 && f == last_mouse_frame
6783 && FRAME_LIVE_P (f));
6784 if (mouse_down_p
6785 && last_tool_bar_item != prop_idx)
6786 return;
6788 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6789 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6791 /* If tool-bar item is not enabled, don't highlight it. */
6792 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6793 if (!NILP (enabled_p))
6795 /* Compute the x-position of the glyph. In front and past the
6796 image is a space. We include this is the highlighted area. */
6797 row = MATRIX_ROW (w->current_matrix, vpos);
6798 for (i = x = 0; i < hpos; ++i)
6799 x += row->glyphs[TEXT_AREA][i].pixel_width;
6801 /* Record this as the current active region. */
6802 dpyinfo->mouse_face_beg_col = hpos;
6803 dpyinfo->mouse_face_beg_row = vpos;
6804 dpyinfo->mouse_face_beg_x = x;
6805 dpyinfo->mouse_face_beg_y = row->y;
6806 dpyinfo->mouse_face_past_end = 0;
6808 dpyinfo->mouse_face_end_col = hpos + 1;
6809 dpyinfo->mouse_face_end_row = vpos;
6810 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6811 dpyinfo->mouse_face_end_y = row->y;
6812 dpyinfo->mouse_face_window = window;
6813 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6815 /* Display it as active. */
6816 show_mouse_face (dpyinfo, draw);
6817 dpyinfo->mouse_face_image_state = draw;
6820 set_help_echo:
6822 /* Set help_echo to a help string.to display for this tool-bar item.
6823 w32_read_socket does the rest. */
6824 help_echo_object = help_echo_window = Qnil;
6825 help_echo_pos = -1;
6826 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
6827 if (NILP (help_echo))
6828 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
6833 /* Find the glyph matrix position of buffer position POS in window W.
6834 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6835 current glyphs must be up to date. If POS is above window start
6836 return (0, 0, 0, 0). If POS is after end of W, return end of
6837 last line in W. */
6839 static int
6840 fast_find_position (w, pos, hpos, vpos, x, y)
6841 struct window *w;
6842 int pos;
6843 int *hpos, *vpos, *x, *y;
6845 int i;
6846 int lastcol;
6847 int maybe_next_line_p = 0;
6848 int line_start_position;
6849 int yb = window_text_bottom_y (w);
6850 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6851 struct glyph_row *best_row = row;
6852 int row_vpos = 0, best_row_vpos = 0;
6853 int current_x;
6855 while (row->y < yb)
6857 if (row->used[TEXT_AREA])
6858 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6859 else
6860 line_start_position = 0;
6862 if (line_start_position > pos)
6863 break;
6864 /* If the position sought is the end of the buffer,
6865 don't include the blank lines at the bottom of the window. */
6866 else if (line_start_position == pos
6867 && pos == BUF_ZV (XBUFFER (w->buffer)))
6869 maybe_next_line_p = 1;
6870 break;
6872 else if (line_start_position > 0)
6874 best_row = row;
6875 best_row_vpos = row_vpos;
6878 if (row->y + row->height >= yb)
6879 break;
6881 ++row;
6882 ++row_vpos;
6885 /* Find the right column within BEST_ROW. */
6886 lastcol = 0;
6887 current_x = best_row->x;
6888 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6890 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6891 int charpos;
6893 charpos = glyph->charpos;
6894 if (charpos == pos)
6896 *hpos = i;
6897 *vpos = best_row_vpos;
6898 *x = current_x;
6899 *y = best_row->y;
6900 return 1;
6902 else if (charpos > pos)
6903 break;
6904 else if (charpos > 0)
6905 lastcol = i;
6907 current_x += glyph->pixel_width;
6910 /* If we're looking for the end of the buffer,
6911 and we didn't find it in the line we scanned,
6912 use the start of the following line. */
6913 if (maybe_next_line_p)
6915 ++best_row;
6916 ++best_row_vpos;
6917 lastcol = 0;
6918 current_x = best_row->x;
6921 *vpos = best_row_vpos;
6922 *hpos = lastcol + 1;
6923 *x = current_x;
6924 *y = best_row->y;
6925 return 0;
6929 /* Display the active region described by mouse_face_*
6930 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6932 static void
6933 show_mouse_face (dpyinfo, draw)
6934 struct w32_display_info *dpyinfo;
6935 enum draw_glyphs_face draw;
6937 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6938 struct frame *f = XFRAME (WINDOW_FRAME (w));
6939 int i;
6940 int cursor_off_p = 0;
6941 struct cursor_pos saved_cursor;
6943 saved_cursor = output_cursor;
6945 /* If window is in the process of being destroyed, don't bother
6946 to do anything. */
6947 if (w->current_matrix == NULL)
6948 goto set_x_cursor;
6950 /* Recognize when we are called to operate on rows that don't exist
6951 anymore. This can happen when a window is split. */
6952 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6953 goto set_x_cursor;
6955 set_output_cursor (&w->phys_cursor);
6957 /* Note that mouse_face_beg_row etc. are window relative. */
6958 for (i = dpyinfo->mouse_face_beg_row;
6959 i <= dpyinfo->mouse_face_end_row;
6960 i++)
6962 int start_hpos, end_hpos, start_x;
6963 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6965 /* Don't do anything if row doesn't have valid contents. */
6966 if (!row->enabled_p)
6967 continue;
6969 /* For all but the first row, the highlight starts at column 0. */
6970 if (i == dpyinfo->mouse_face_beg_row)
6972 start_hpos = dpyinfo->mouse_face_beg_col;
6973 start_x = dpyinfo->mouse_face_beg_x;
6975 else
6977 start_hpos = 0;
6978 start_x = 0;
6981 if (i == dpyinfo->mouse_face_end_row)
6982 end_hpos = dpyinfo->mouse_face_end_col;
6983 else
6984 end_hpos = row->used[TEXT_AREA];
6986 /* If the cursor's in the text we are about to rewrite, turn the
6987 cursor off. */
6988 if (!w->pseudo_window_p
6989 && i == output_cursor.vpos
6990 && output_cursor.hpos >= start_hpos - 1
6991 && output_cursor.hpos <= end_hpos)
6993 x_update_window_cursor (w, 0);
6994 cursor_off_p = 1;
6997 if (end_hpos > start_hpos)
6999 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7000 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7001 start_hpos, end_hpos, draw, NULL, NULL, 0);
7005 /* If we turned the cursor off, turn it back on. */
7006 if (cursor_off_p)
7007 x_display_cursor (w, 1,
7008 output_cursor.hpos, output_cursor.vpos,
7009 output_cursor.x, output_cursor.y);
7011 output_cursor = saved_cursor;
7013 set_x_cursor:
7014 #if 0 /* TODO: mouse cursor */
7015 /* Change the mouse cursor. */
7016 if (draw == DRAW_NORMAL_TEXT)
7017 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7018 f->output_data.x->text_cursor);
7019 else if (draw == DRAW_MOUSE_FACE)
7020 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7021 f->output_data.x->cross_cursor);
7022 else
7023 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7024 f->output_data.x->nontext_cursor);
7025 #endif
7029 /* Clear out the mouse-highlighted active region.
7030 Redraw it un-highlighted first. */
7032 void
7033 clear_mouse_face (dpyinfo)
7034 struct w32_display_info *dpyinfo;
7036 if (tip_frame)
7037 return;
7039 if (! NILP (dpyinfo->mouse_face_window))
7040 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7042 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7043 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7044 dpyinfo->mouse_face_window = Qnil;
7048 /* Clear any mouse-face on window W. This function is part of the
7049 redisplay interface, and is called from try_window_id and similar
7050 functions to ensure the mouse-highlight is off. */
7052 static void
7053 x_clear_mouse_face (w)
7054 struct window *w;
7056 struct w32_display_info *dpyinfo
7057 = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
7058 Lisp_Object window;
7060 BLOCK_INPUT;
7061 XSETWINDOW (window, w);
7062 if (EQ (window, dpyinfo->mouse_face_window))
7063 clear_mouse_face (dpyinfo);
7064 UNBLOCK_INPUT;
7068 /* Just discard the mouse face information for frame F, if any.
7069 This is used when the size of F is changed. */
7071 void
7072 cancel_mouse_face (f)
7073 FRAME_PTR f;
7075 Lisp_Object window;
7076 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
7078 window = dpyinfo->mouse_face_window;
7079 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7081 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7082 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7083 dpyinfo->mouse_face_window = Qnil;
7087 static struct scroll_bar *x_window_to_scroll_bar ();
7088 static void x_scroll_bar_report_motion ();
7090 /* Return the current position of the mouse.
7091 *fp should be a frame which indicates which display to ask about.
7093 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7094 and *part to the frame, window, and scroll bar part that the mouse
7095 is over. Set *x and *y to the portion and whole of the mouse's
7096 position on the scroll bar.
7098 If the mouse movement started elsewhere, set *fp to the frame the
7099 mouse is on, *bar_window to nil, and *x and *y to the character cell
7100 the mouse is over.
7102 Set *time to the server time-stamp for the time at which the mouse
7103 was at this position.
7105 Don't store anything if we don't have a valid set of values to report.
7107 This clears the mouse_moved flag, so we can wait for the next mouse
7108 movement. */
7110 static void
7111 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
7112 FRAME_PTR *fp;
7113 int insist;
7114 Lisp_Object *bar_window;
7115 enum scroll_bar_part *part;
7116 Lisp_Object *x, *y;
7117 unsigned long *time;
7119 FRAME_PTR f1;
7121 BLOCK_INPUT;
7123 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7124 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7125 else
7127 POINT pt;
7129 Lisp_Object frame, tail;
7131 /* Clear the mouse-moved flag for every frame on this display. */
7132 FOR_EACH_FRAME (tail, frame)
7133 XFRAME (frame)->mouse_moved = 0;
7135 last_mouse_scroll_bar = Qnil;
7137 GetCursorPos (&pt);
7139 /* Now we have a position on the root; find the innermost window
7140 containing the pointer. */
7142 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7143 && FRAME_LIVE_P (last_mouse_frame))
7145 /* If mouse was grabbed on a frame, give coords for that frame
7146 even if the mouse is now outside it. */
7147 f1 = last_mouse_frame;
7149 else
7151 /* Is window under mouse one of our frames? */
7152 f1 = x_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
7153 WindowFromPoint (pt));
7156 /* If not, is it one of our scroll bars? */
7157 if (! f1)
7159 struct scroll_bar *bar
7160 = x_window_to_scroll_bar (WindowFromPoint (pt));
7162 if (bar)
7164 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7168 if (f1 == 0 && insist > 0)
7169 f1 = SELECTED_FRAME ();
7171 if (f1)
7173 /* Ok, we found a frame. Store all the values.
7174 last_mouse_glyph is a rectangle used to reduce the
7175 generation of mouse events. To not miss any motion
7176 events, we must divide the frame into rectangles of the
7177 size of the smallest character that could be displayed
7178 on it, i.e. into the same rectangles that matrices on
7179 the frame are divided into. */
7181 #if OLD_REDISPLAY_CODE
7182 int ignore1, ignore2;
7184 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7186 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
7187 &last_mouse_glyph,
7188 FRAME_W32_DISPLAY_INFO (f1)->grabbed
7189 || insist);
7190 #else
7191 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
7193 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7194 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7195 int x = pt.x;
7196 int y = pt.y;
7198 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7199 round down even for negative values. */
7200 if (x < 0)
7201 x -= width - 1;
7202 if (y < 0)
7203 y -= height - 1;
7205 last_mouse_glyph.left = (x + width - 1) / width * width;
7206 last_mouse_glyph.top = (y + height - 1) / height * height;
7207 last_mouse_glyph.right = last_mouse_glyph.left + width;
7208 last_mouse_glyph.bottom = last_mouse_glyph.top + height;
7210 #endif
7212 *bar_window = Qnil;
7213 *part = 0;
7214 *fp = f1;
7215 XSETINT (*x, pt.x);
7216 XSETINT (*y, pt.y);
7217 *time = last_mouse_movement_time;
7222 UNBLOCK_INPUT;
7226 /* Scroll bar support. */
7228 /* Given a window ID, find the struct scroll_bar which manages it.
7229 This can be called in GC, so we have to make sure to strip off mark
7230 bits. */
7232 static struct scroll_bar *
7233 x_window_to_scroll_bar (window_id)
7234 Window window_id;
7236 Lisp_Object tail;
7238 for (tail = Vframe_list;
7239 XGCTYPE (tail) == Lisp_Cons;
7240 tail = XCDR (tail))
7242 Lisp_Object frame, bar, condemned;
7244 frame = XCAR (tail);
7245 /* All elements of Vframe_list should be frames. */
7246 if (! GC_FRAMEP (frame))
7247 abort ();
7249 /* Scan this frame's scroll bar list for a scroll bar with the
7250 right window ID. */
7251 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7252 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7253 /* This trick allows us to search both the ordinary and
7254 condemned scroll bar lists with one loop. */
7255 ! GC_NILP (bar) || (bar = condemned,
7256 condemned = Qnil,
7257 ! GC_NILP (bar));
7258 bar = XSCROLL_BAR (bar)->next)
7259 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
7260 return XSCROLL_BAR (bar);
7263 return 0;
7268 /* Set the thumb size and position of scroll bar BAR. We are currently
7269 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7271 static void
7272 w32_set_scroll_bar_thumb (bar, portion, position, whole)
7273 struct scroll_bar *bar;
7274 int portion, position, whole;
7276 Window w = SCROLL_BAR_W32_WINDOW (bar);
7277 int range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7278 int sb_page, sb_pos;
7279 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
7281 if (whole)
7283 /* Position scroll bar at rock bottom if the bottom of the
7284 buffer is visible. This avoids shinking the thumb away
7285 to nothing if it is held at the bottom of the buffer. */
7286 if (position + portion >= whole)
7288 sb_page = range * (whole - position) / whole
7289 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7290 sb_pos = range;
7293 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7294 sb_pos = position * range / whole;
7296 else
7298 sb_page = range;
7299 sb_pos = 0;
7302 BLOCK_INPUT;
7304 if (pfnSetScrollInfo)
7306 SCROLLINFO si;
7308 si.cbSize = sizeof (si);
7309 /* Only update page size if currently dragging, to reduce
7310 flicker effects. */
7311 if (draggingp)
7312 si.fMask = SIF_PAGE;
7313 else
7314 si.fMask = SIF_PAGE | SIF_POS;
7315 si.nPage = sb_page;
7316 si.nPos = sb_pos;
7318 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
7320 else
7321 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
7323 UNBLOCK_INPUT;
7327 /************************************************************************
7328 Scroll bars, general
7329 ************************************************************************/
7331 HWND
7332 my_create_scrollbar (f, bar)
7333 struct frame * f;
7334 struct scroll_bar * bar;
7336 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
7337 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
7338 (LPARAM) bar);
7341 //#define ATTACH_THREADS
7343 BOOL
7344 my_show_window (FRAME_PTR f, HWND hwnd, int how)
7346 #ifndef ATTACH_THREADS
7347 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
7348 (WPARAM) hwnd, (LPARAM) how);
7349 #else
7350 return ShowWindow (hwnd, how);
7351 #endif
7354 void
7355 my_set_window_pos (HWND hwnd, HWND hwndAfter,
7356 int x, int y, int cx, int cy, UINT flags)
7358 #ifndef ATTACH_THREADS
7359 WINDOWPOS pos;
7360 pos.hwndInsertAfter = hwndAfter;
7361 pos.x = x;
7362 pos.y = y;
7363 pos.cx = cx;
7364 pos.cy = cy;
7365 pos.flags = flags;
7366 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
7367 #else
7368 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
7369 #endif
7372 void
7373 my_set_focus (f, hwnd)
7374 struct frame * f;
7375 HWND hwnd;
7377 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
7378 (WPARAM) hwnd, 0);
7381 void
7382 my_set_foreground_window (hwnd)
7383 HWND hwnd;
7385 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
7388 void
7389 my_destroy_window (f, hwnd)
7390 struct frame * f;
7391 HWND hwnd;
7393 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
7394 (WPARAM) hwnd, 0);
7397 /* Create a scroll bar and return the scroll bar vector for it. W is
7398 the Emacs window on which to create the scroll bar. TOP, LEFT,
7399 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7400 scroll bar. */
7402 static struct scroll_bar *
7403 x_scroll_bar_create (w, top, left, width, height)
7404 struct window *w;
7405 int top, left, width, height;
7407 struct frame *f = XFRAME (WINDOW_FRAME (w));
7408 HWND hwnd;
7409 struct scroll_bar *bar
7410 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7412 BLOCK_INPUT;
7414 XSETWINDOW (bar->window, w);
7415 XSETINT (bar->top, top);
7416 XSETINT (bar->left, left);
7417 XSETINT (bar->width, width);
7418 XSETINT (bar->height, height);
7419 XSETINT (bar->start, 0);
7420 XSETINT (bar->end, 0);
7421 bar->dragging = Qnil;
7423 /* Requires geometry to be set before call to create the real window */
7425 hwnd = my_create_scrollbar (f, bar);
7427 if (pfnSetScrollInfo)
7429 SCROLLINFO si;
7431 si.cbSize = sizeof (si);
7432 si.fMask = SIF_ALL;
7433 si.nMin = 0;
7434 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7435 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7436 si.nPage = si.nMax;
7437 si.nPos = 0;
7439 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7441 else
7443 SetScrollRange (hwnd, SB_CTL, 0,
7444 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7445 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
7448 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
7450 /* Add bar to its frame's list of scroll bars. */
7451 bar->next = FRAME_SCROLL_BARS (f);
7452 bar->prev = Qnil;
7453 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7454 if (! NILP (bar->next))
7455 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7457 UNBLOCK_INPUT;
7459 return bar;
7463 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
7464 nil. */
7466 static void
7467 x_scroll_bar_remove (bar)
7468 struct scroll_bar *bar;
7470 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7472 BLOCK_INPUT;
7474 /* Destroy the window. */
7475 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
7477 /* Disassociate this scroll bar from its window. */
7478 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
7480 UNBLOCK_INPUT;
7483 /* Set the handle of the vertical scroll bar for WINDOW to indicate
7484 that we are displaying PORTION characters out of a total of WHOLE
7485 characters, starting at POSITION. If WINDOW has no scroll bar,
7486 create one. */
7487 static void
7488 w32_set_vertical_scroll_bar (w, portion, whole, position)
7489 struct window *w;
7490 int portion, whole, position;
7492 struct frame *f = XFRAME (w->frame);
7493 struct scroll_bar *bar;
7494 int top, height, left, sb_left, width, sb_width;
7495 int window_x, window_y, window_width, window_height;
7497 /* Get window dimensions. */
7498 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7499 top = window_y;
7500 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
7501 height = window_height;
7503 /* Compute the left edge of the scroll bar area. */
7504 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7505 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
7506 else
7507 left = XFASTINT (w->left);
7508 left *= CANON_X_UNIT (f);
7509 left += FRAME_INTERNAL_BORDER_WIDTH (f);
7511 /* Compute the width of the scroll bar which might be less than
7512 the width of the area reserved for the scroll bar. */
7513 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
7514 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
7515 else
7516 sb_width = width;
7518 /* Compute the left edge of the scroll bar. */
7519 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
7520 sb_left = left + width - sb_width - (width - sb_width) / 2;
7521 else
7522 sb_left = left + (width - sb_width) / 2;
7524 /* Does the scroll bar exist yet? */
7525 if (NILP (w->vertical_scroll_bar))
7527 HDC hdc;
7528 BLOCK_INPUT;
7529 hdc = get_frame_dc (f);
7530 w32_clear_area (f, hdc, left, top, width, height);
7531 release_frame_dc (f, hdc);
7532 UNBLOCK_INPUT;
7534 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
7536 else
7538 /* It may just need to be moved and resized. */
7539 HWND hwnd;
7541 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7542 hwnd = SCROLL_BAR_W32_WINDOW (bar);
7544 /* If already correctly positioned, do nothing. */
7545 if ( XINT (bar->left) == sb_left
7546 && XINT (bar->top) == top
7547 && XINT (bar->width) == sb_width
7548 && XINT (bar->height) == height )
7550 /* Redraw after clear_frame. */
7551 if (!my_show_window (f, hwnd, SW_NORMAL))
7552 InvalidateRect (hwnd, NULL, FALSE);
7554 else
7556 HDC hdc;
7557 BLOCK_INPUT;
7559 hdc = get_frame_dc (f);
7560 /* Since Windows scroll bars are smaller than the space reserved
7561 for them on the frame, we have to clear "under" them. */
7562 w32_clear_area (f, hdc,
7563 left,
7564 top,
7565 width,
7566 height);
7567 release_frame_dc (f, hdc);
7569 /* Make sure scroll bar is "visible" before moving, to ensure the
7570 area of the parent window now exposed will be refreshed. */
7571 my_show_window (f, hwnd, SW_HIDE);
7572 MoveWindow (hwnd, sb_left, top,
7573 sb_width, height, TRUE);
7574 if (pfnSetScrollInfo)
7576 SCROLLINFO si;
7578 si.cbSize = sizeof (si);
7579 si.fMask = SIF_RANGE;
7580 si.nMin = 0;
7581 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
7582 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7584 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
7586 else
7587 SetScrollRange (hwnd, SB_CTL, 0,
7588 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
7589 my_show_window (f, hwnd, SW_NORMAL);
7590 // InvalidateRect (w, NULL, FALSE);
7592 /* Remember new settings. */
7593 XSETINT (bar->left, sb_left);
7594 XSETINT (bar->top, top);
7595 XSETINT (bar->width, sb_width);
7596 XSETINT (bar->height, height);
7598 UNBLOCK_INPUT;
7601 w32_set_scroll_bar_thumb (bar, portion, position, whole);
7603 XSETVECTOR (w->vertical_scroll_bar, bar);
7607 /* The following three hooks are used when we're doing a thorough
7608 redisplay of the frame. We don't explicitly know which scroll bars
7609 are going to be deleted, because keeping track of when windows go
7610 away is a real pain - "Can you say set-window-configuration, boys
7611 and girls?" Instead, we just assert at the beginning of redisplay
7612 that *all* scroll bars are to be removed, and then save a scroll bar
7613 from the fiery pit when we actually redisplay its window. */
7615 /* Arrange for all scroll bars on FRAME to be removed at the next call
7616 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
7617 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7619 static void
7620 w32_condemn_scroll_bars (frame)
7621 FRAME_PTR frame;
7623 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
7624 while (! NILP (FRAME_SCROLL_BARS (frame)))
7626 Lisp_Object bar;
7627 bar = FRAME_SCROLL_BARS (frame);
7628 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
7629 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7630 XSCROLL_BAR (bar)->prev = Qnil;
7631 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7632 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
7633 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
7637 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7638 Note that WINDOW isn't necessarily condemned at all. */
7639 static void
7640 w32_redeem_scroll_bar (window)
7641 struct window *window;
7643 struct scroll_bar *bar;
7645 /* We can't redeem this window's scroll bar if it doesn't have one. */
7646 if (NILP (window->vertical_scroll_bar))
7647 abort ();
7649 bar = XSCROLL_BAR (window->vertical_scroll_bar);
7651 /* Unlink it from the condemned list. */
7653 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
7655 if (NILP (bar->prev))
7657 /* If the prev pointer is nil, it must be the first in one of
7658 the lists. */
7659 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
7660 /* It's not condemned. Everything's fine. */
7661 return;
7662 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7663 window->vertical_scroll_bar))
7664 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
7665 else
7666 /* If its prev pointer is nil, it must be at the front of
7667 one or the other! */
7668 abort ();
7670 else
7671 XSCROLL_BAR (bar->prev)->next = bar->next;
7673 if (! NILP (bar->next))
7674 XSCROLL_BAR (bar->next)->prev = bar->prev;
7676 bar->next = FRAME_SCROLL_BARS (f);
7677 bar->prev = Qnil;
7678 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7679 if (! NILP (bar->next))
7680 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7684 /* Remove all scroll bars on FRAME that haven't been saved since the
7685 last call to `*condemn_scroll_bars_hook'. */
7687 static void
7688 w32_judge_scroll_bars (f)
7689 FRAME_PTR f;
7691 Lisp_Object bar, next;
7693 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7695 /* Clear out the condemned list now so we won't try to process any
7696 more events on the hapless scroll bars. */
7697 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
7699 for (; ! NILP (bar); bar = next)
7701 struct scroll_bar *b = XSCROLL_BAR (bar);
7703 x_scroll_bar_remove (b);
7705 next = b->next;
7706 b->next = b->prev = Qnil;
7709 /* Now there should be no references to the condemned scroll bars,
7710 and they should get garbage-collected. */
7713 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7714 is set to something other than no_event, it is enqueued.
7716 This may be called from a signal handler, so we have to ignore GC
7717 mark bits. */
7719 static int
7720 x_scroll_bar_handle_click (bar, msg, emacs_event)
7721 struct scroll_bar *bar;
7722 W32Msg *msg;
7723 struct input_event *emacs_event;
7725 if (! GC_WINDOWP (bar->window))
7726 abort ();
7728 emacs_event->kind = w32_scroll_bar_click;
7729 emacs_event->code = 0;
7730 /* not really meaningful to distinguish up/down */
7731 emacs_event->modifiers = msg->dwModifiers;
7732 emacs_event->frame_or_window = bar->window;
7733 emacs_event->arg = Qnil;
7734 emacs_event->timestamp = msg->msg.time;
7737 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7738 int y;
7739 int dragging = !NILP (bar->dragging);
7741 if (pfnGetScrollInfo)
7743 SCROLLINFO si;
7745 si.cbSize = sizeof (si);
7746 si.fMask = SIF_POS;
7748 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
7749 y = si.nPos;
7751 else
7752 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
7754 bar->dragging = Qnil;
7757 last_mouse_scroll_bar_pos = msg->msg.wParam;
7759 switch (LOWORD (msg->msg.wParam))
7761 case SB_LINEDOWN:
7762 emacs_event->part = scroll_bar_down_arrow;
7763 break;
7764 case SB_LINEUP:
7765 emacs_event->part = scroll_bar_up_arrow;
7766 break;
7767 case SB_PAGEUP:
7768 emacs_event->part = scroll_bar_above_handle;
7769 break;
7770 case SB_PAGEDOWN:
7771 emacs_event->part = scroll_bar_below_handle;
7772 break;
7773 case SB_TOP:
7774 emacs_event->part = scroll_bar_handle;
7775 y = 0;
7776 break;
7777 case SB_BOTTOM:
7778 emacs_event->part = scroll_bar_handle;
7779 y = top_range;
7780 break;
7781 case SB_THUMBTRACK:
7782 case SB_THUMBPOSITION:
7783 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7784 y = HIWORD (msg->msg.wParam);
7785 bar->dragging = Qt;
7786 emacs_event->part = scroll_bar_handle;
7788 /* "Silently" update current position. */
7789 if (pfnSetScrollInfo)
7791 SCROLLINFO si;
7793 si.cbSize = sizeof (si);
7794 si.fMask = SIF_POS;
7795 si.nPos = y;
7796 /* Remember apparent position (we actually lag behind the real
7797 position, so don't set that directly. */
7798 last_scroll_bar_drag_pos = y;
7800 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
7802 else
7803 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
7804 break;
7805 case SB_ENDSCROLL:
7806 /* If this is the end of a drag sequence, then reset the scroll
7807 handle size to normal and do a final redraw. Otherwise do
7808 nothing. */
7809 if (dragging)
7811 if (pfnSetScrollInfo)
7813 SCROLLINFO si;
7814 int start = XINT (bar->start);
7815 int end = XINT (bar->end);
7817 si.cbSize = sizeof (si);
7818 si.fMask = SIF_PAGE | SIF_POS;
7819 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
7820 si.nPos = last_scroll_bar_drag_pos;
7821 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
7823 else
7824 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
7826 /* fall through */
7827 default:
7828 emacs_event->kind = no_event;
7829 return FALSE;
7832 XSETINT (emacs_event->x, y);
7833 XSETINT (emacs_event->y, top_range);
7835 return TRUE;
7839 /* Return information to the user about the current position of the mouse
7840 on the scroll bar. */
7842 static void
7843 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
7844 FRAME_PTR *fp;
7845 Lisp_Object *bar_window;
7846 enum scroll_bar_part *part;
7847 Lisp_Object *x, *y;
7848 unsigned long *time;
7850 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
7851 Window w = SCROLL_BAR_W32_WINDOW (bar);
7852 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7853 int pos;
7854 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7856 BLOCK_INPUT;
7858 *fp = f;
7859 *bar_window = bar->window;
7861 if (pfnGetScrollInfo)
7863 SCROLLINFO si;
7865 si.cbSize = sizeof (si);
7866 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
7868 pfnGetScrollInfo (w, SB_CTL, &si);
7869 pos = si.nPos;
7870 top_range = si.nMax - si.nPage + 1;
7872 else
7873 pos = GetScrollPos (w, SB_CTL);
7875 switch (LOWORD (last_mouse_scroll_bar_pos))
7877 case SB_THUMBPOSITION:
7878 case SB_THUMBTRACK:
7879 *part = scroll_bar_handle;
7880 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
7881 pos = HIWORD (last_mouse_scroll_bar_pos);
7882 break;
7883 case SB_LINEDOWN:
7884 *part = scroll_bar_handle;
7885 pos++;
7886 break;
7887 default:
7888 *part = scroll_bar_handle;
7889 break;
7892 XSETINT (*x, pos);
7893 XSETINT (*y, top_range);
7895 f->mouse_moved = 0;
7896 last_mouse_scroll_bar = Qnil;
7898 *time = last_mouse_movement_time;
7900 UNBLOCK_INPUT;
7904 /* The screen has been cleared so we may have changed foreground or
7905 background colors, and the scroll bars may need to be redrawn.
7906 Clear out the scroll bars, and ask for expose events, so we can
7907 redraw them. */
7909 void
7910 x_scroll_bar_clear (f)
7911 FRAME_PTR f;
7913 Lisp_Object bar;
7915 /* We can have scroll bars even if this is 0,
7916 if we just turned off scroll bar mode.
7917 But in that case we should not clear them. */
7918 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7919 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7920 bar = XSCROLL_BAR (bar)->next)
7922 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
7923 HDC hdc = GetDC (window);
7924 RECT rect;
7926 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
7927 arranges to refresh the scroll bar if hidden. */
7928 my_show_window (f, window, SW_HIDE);
7930 GetClientRect (window, &rect);
7931 select_palette (f, hdc);
7932 w32_clear_rect (f, hdc, &rect);
7933 deselect_palette (f, hdc);
7935 ReleaseDC (window, hdc);
7939 show_scroll_bars (f, how)
7940 FRAME_PTR f;
7941 int how;
7943 Lisp_Object bar;
7945 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7946 bar = XSCROLL_BAR (bar)->next)
7948 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
7949 my_show_window (f, window, how);
7954 /* The main W32 event-reading loop - w32_read_socket. */
7956 /* Time stamp of enter window event. This is only used by w32_read_socket,
7957 but we have to put it out here, since static variables within functions
7958 sometimes don't work. */
7960 static Time enter_timestamp;
7962 /* Record the last 100 characters stored
7963 to help debug the loss-of-chars-during-GC problem. */
7965 static int temp_index;
7966 static short temp_buffer[100];
7969 /* Read events coming from the W32 shell.
7970 This routine is called by the SIGIO handler.
7971 We return as soon as there are no more events to be read.
7973 Events representing keys are stored in buffer BUFP,
7974 which can hold up to NUMCHARS characters.
7975 We return the number of characters stored into the buffer,
7976 thus pretending to be `read'.
7978 EXPECTED is nonzero if the caller knows input is available.
7980 Some of these messages are reposted back to the message queue since the
7981 system calls the windows proc directly in a context where we cannot return
7982 the data nor can we guarantee the state we are in. So if we dispatch them
7983 we will get into an infinite loop. To prevent this from ever happening we
7984 will set a variable to indicate we are in the read_socket call and indicate
7985 which message we are processing since the windows proc gets called
7986 recursively with different messages by the system.
7990 w32_read_socket (sd, bufp, numchars, expected)
7991 register int sd;
7992 /* register */ struct input_event *bufp;
7993 /* register */ int numchars;
7994 int expected;
7996 int count = 0;
7997 int check_visibility = 0;
7998 W32Msg msg;
7999 struct frame *f;
8000 struct w32_display_info *dpyinfo = &one_w32_display_info;
8002 if (interrupt_input_blocked)
8004 interrupt_input_pending = 1;
8005 return -1;
8008 interrupt_input_pending = 0;
8009 BLOCK_INPUT;
8011 /* So people can tell when we have read the available input. */
8012 input_signal_count++;
8014 if (numchars <= 0)
8015 abort (); /* Don't think this happens. */
8017 /* TODO: tooltips, tool-bars, ghostscript integration, mouse
8018 cursors. */
8019 while (get_next_msg (&msg, FALSE))
8021 switch (msg.msg.message)
8023 case WM_PAINT:
8024 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8026 if (f)
8028 if (msg.rect.right == msg.rect.left ||
8029 msg.rect.bottom == msg.rect.top)
8031 /* We may get paint messages even though the client
8032 area is clipped - these are not expose events. */
8033 DebPrint (("clipped frame %04x (%s) got WM_PAINT\n", f,
8034 XSTRING (f->name)->data));
8036 else if (f->async_visible != 1)
8038 /* Definitely not obscured, so mark as visible. */
8039 f->async_visible = 1;
8040 f->async_iconified = 0;
8041 SET_FRAME_GARBAGED (f);
8042 DebPrint (("frame %04x (%s) reexposed\n", f,
8043 XSTRING (f->name)->data));
8045 /* WM_PAINT serves as MapNotify as well, so report
8046 visibility changes properly. */
8047 if (f->iconified)
8049 bufp->kind = deiconify_event;
8050 XSETFRAME (bufp->frame_or_window, f);
8051 bufp->arg = Qnil;
8052 bufp++;
8053 count++;
8054 numchars--;
8056 else if (! NILP (Vframe_list)
8057 && ! NILP (XCDR (Vframe_list)))
8058 /* Force a redisplay sooner or later to update the
8059 frame titles in case this is the second frame. */
8060 record_asynch_buffer_change ();
8062 else
8064 HDC hdc = get_frame_dc (f);
8066 /* Erase background again for safety. */
8067 w32_clear_rect (f, hdc, &msg.rect);
8068 release_frame_dc (f, hdc);
8069 expose_frame (f,
8070 msg.rect.left,
8071 msg.rect.top,
8072 msg.rect.right - msg.rect.left,
8073 msg.rect.bottom - msg.rect.top);
8076 break;
8078 case WM_INPUTLANGCHANGE:
8079 /* Generate a language change event. */
8080 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8082 if (f)
8084 if (numchars == 0)
8085 abort ();
8087 bufp->kind = language_change_event;
8088 XSETFRAME (bufp->frame_or_window, f);
8089 bufp->arg = Qnil;
8090 bufp->code = msg.msg.wParam;
8091 bufp->modifiers = msg.msg.lParam & 0xffff;
8092 bufp++;
8093 count++;
8094 numchars--;
8096 break;
8098 case WM_KEYDOWN:
8099 case WM_SYSKEYDOWN:
8100 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8102 if (f && !f->iconified)
8104 if (temp_index == sizeof temp_buffer / sizeof (short))
8105 temp_index = 0;
8106 temp_buffer[temp_index++] = msg.msg.wParam;
8107 bufp->kind = non_ascii_keystroke;
8108 bufp->code = msg.msg.wParam;
8109 bufp->modifiers = msg.dwModifiers;
8110 XSETFRAME (bufp->frame_or_window, f);
8111 bufp->arg = Qnil;
8112 bufp->timestamp = msg.msg.time;
8113 bufp++;
8114 numchars--;
8115 count++;
8117 break;
8119 case WM_SYSCHAR:
8120 case WM_CHAR:
8121 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8123 if (f && !f->iconified)
8125 if (temp_index == sizeof temp_buffer / sizeof (short))
8126 temp_index = 0;
8127 temp_buffer[temp_index++] = msg.msg.wParam;
8128 bufp->kind = ascii_keystroke;
8129 bufp->code = msg.msg.wParam;
8130 bufp->modifiers = msg.dwModifiers;
8131 XSETFRAME (bufp->frame_or_window, f);
8132 bufp->arg = Qnil;
8133 bufp->timestamp = msg.msg.time;
8134 bufp++;
8135 numchars--;
8136 count++;
8138 break;
8140 case WM_MOUSEMOVE:
8141 previous_help_echo = help_echo;
8142 help_echo = help_echo_object = help_echo_window = Qnil;
8143 help_echo_pos = -1;
8145 if (dpyinfo->grabbed && last_mouse_frame
8146 && FRAME_LIVE_P (last_mouse_frame))
8147 f = last_mouse_frame;
8148 else
8149 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8151 if (f)
8152 note_mouse_movement (f, &msg.msg);
8153 else
8155 /* If we move outside the frame, then we're
8156 certainly no longer on any text in the frame. */
8157 clear_mouse_face (FRAME_W32_DISPLAY_INFO (f));
8160 /* If the contents of the global variable help_echo
8161 has changed, generate a HELP_EVENT. */
8162 if (!NILP (help_echo)
8163 || !NILP (previous_help_echo))
8165 Lisp_Object frame;
8166 int n;
8168 if (f)
8169 XSETFRAME (frame, f);
8170 else
8171 frame = Qnil;
8173 any_help_event_p = 1;
8174 n = gen_help_event (bufp, numchars, help_echo, frame,
8175 help_echo_window, help_echo_object,
8176 help_echo_pos);
8177 bufp += n, count += n, numchars -= n;
8179 break;
8181 case WM_LBUTTONDOWN:
8182 case WM_LBUTTONUP:
8183 case WM_MBUTTONDOWN:
8184 case WM_MBUTTONUP:
8185 case WM_RBUTTONDOWN:
8186 case WM_RBUTTONUP:
8188 /* If we decide we want to generate an event to be seen
8189 by the rest of Emacs, we put it here. */
8190 struct input_event emacs_event;
8191 int tool_bar_p = 0;
8192 int button;
8193 int up;
8195 emacs_event.kind = no_event;
8197 if (dpyinfo->grabbed && last_mouse_frame
8198 && FRAME_LIVE_P (last_mouse_frame))
8199 f = last_mouse_frame;
8200 else
8201 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8203 if (f)
8205 construct_mouse_click (&emacs_event, &msg, f);
8207 /* Is this in the tool-bar? */
8208 if (WINDOWP (f->tool_bar_window)
8209 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
8211 Lisp_Object window;
8212 int p, x, y;
8214 /* Set x and y. */
8215 window = window_from_coordinates (f,
8216 emacs_event.x,
8217 emacs_event.y,
8218 &p, 1);
8219 if (EQ (window, f->tool_bar_window))
8221 w32_handle_tool_bar_click (f, &emacs_event);
8222 tool_bar_p = 1;
8226 if (!tool_bar_p)
8227 if (!dpyinfo->w32_focus_frame
8228 || f == dpyinfo->w32_focus_frame
8229 && (numchars >= 1))
8231 construct_mouse_click (bufp, &msg, f);
8232 bufp++;
8233 count++;
8234 numchars--;
8238 parse_button (msg.msg.message, &button, &up);
8240 if (up)
8242 dpyinfo->grabbed &= ~ (1 << button);
8244 else
8246 dpyinfo->grabbed |= (1 << button);
8247 last_mouse_frame = f;
8248 /* Ignore any mouse motion that happened
8249 before this event; any subsequent mouse-movement
8250 Emacs events should reflect only motion after
8251 the ButtonPress. */
8252 if (f != 0)
8253 f->mouse_moved = 0;
8255 if (!tool_bar_p)
8256 last_tool_bar_item = -1;
8258 break;
8261 case WM_MOUSEWHEEL:
8262 if (dpyinfo->grabbed && last_mouse_frame
8263 && FRAME_LIVE_P (last_mouse_frame))
8264 f = last_mouse_frame;
8265 else
8266 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8268 if (f)
8270 if ((!dpyinfo->w32_focus_frame
8271 || f == dpyinfo->w32_focus_frame)
8272 && (numchars >= 1))
8274 construct_mouse_wheel (bufp, &msg, f);
8275 bufp++;
8276 count++;
8277 numchars--;
8280 break;
8282 case WM_MENUSELECT:
8284 HMENU menu = (HMENU) msg.msg.lParam;
8285 UINT menu_item = (UINT) LOWORD (msg.msg.wParam);
8286 UINT flags = (UINT) HIWORD (msg.msg.wParam);
8288 w32_menu_display_help (menu, menu_item, flags);
8290 break;
8292 case WM_DROPFILES:
8293 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8295 if (f)
8297 construct_drag_n_drop (bufp, &msg, f);
8298 bufp++;
8299 count++;
8300 numchars--;
8302 break;
8304 case WM_VSCROLL:
8306 struct scroll_bar *bar =
8307 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
8309 if (bar && numchars >= 1)
8311 if (x_scroll_bar_handle_click (bar, &msg, bufp))
8313 bufp++;
8314 count++;
8315 numchars--;
8318 break;
8321 case WM_WINDOWPOSCHANGED:
8322 case WM_ACTIVATE:
8323 case WM_ACTIVATEAPP:
8324 check_visibility = 1;
8325 break;
8327 case WM_MOVE:
8328 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8330 if (f && !f->async_iconified)
8332 int x, y;
8334 x_real_positions (f, &x, &y);
8335 f->output_data.w32->left_pos = x;
8336 f->output_data.w32->top_pos = y;
8339 check_visibility = 1;
8340 break;
8342 case WM_SHOWWINDOW:
8343 /* If window has been obscured or exposed by another window
8344 being maximised or minimised/restored, then recheck
8345 visibility of all frames. Direct changes to our own
8346 windows get handled by WM_SIZE. */
8347 #if 0
8348 if (msg.msg.lParam != 0)
8349 check_visibility = 1;
8350 else
8352 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8353 f->async_visible = msg.msg.wParam;
8355 #endif
8357 check_visibility = 1;
8358 break;
8360 case WM_SIZE:
8361 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8363 /* Inform lisp of whether frame has been iconified etc. */
8364 if (f)
8366 switch (msg.msg.wParam)
8368 case SIZE_MINIMIZED:
8369 f->async_visible = 0;
8370 f->async_iconified = 1;
8372 bufp->kind = iconify_event;
8373 XSETFRAME (bufp->frame_or_window, f);
8374 bufp->arg = Qnil;
8375 bufp++;
8376 count++;
8377 numchars--;
8378 break;
8380 case SIZE_MAXIMIZED:
8381 case SIZE_RESTORED:
8382 f->async_visible = 1;
8383 f->async_iconified = 0;
8385 /* wait_reading_process_input will notice this and update
8386 the frame's display structures. */
8387 SET_FRAME_GARBAGED (f);
8389 if (f->iconified)
8391 int x, y;
8393 /* Reset top and left positions of the Window
8394 here since Windows sends a WM_MOVE message
8395 BEFORE telling us the Window is minimized
8396 when the Window is iconified, with 3000,3000
8397 as the co-ords. */
8398 x_real_positions (f, &x, &y);
8399 f->output_data.w32->left_pos = x;
8400 f->output_data.w32->top_pos = y;
8402 bufp->kind = deiconify_event;
8403 XSETFRAME (bufp->frame_or_window, f);
8404 bufp->arg = Qnil;
8405 bufp++;
8406 count++;
8407 numchars--;
8409 else if (! NILP (Vframe_list)
8410 && ! NILP (XCDR (Vframe_list)))
8411 /* Force a redisplay sooner or later
8412 to update the frame titles
8413 in case this is the second frame. */
8414 record_asynch_buffer_change ();
8415 break;
8419 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
8421 RECT rect;
8422 int rows;
8423 int columns;
8424 int width;
8425 int height;
8427 GetClientRect (msg.msg.hwnd, &rect);
8429 height = rect.bottom - rect.top;
8430 width = rect.right - rect.left;
8432 rows = PIXEL_TO_CHAR_HEIGHT (f, height);
8433 columns = PIXEL_TO_CHAR_WIDTH (f, width);
8435 /* TODO: Clip size to the screen dimensions. */
8437 /* Even if the number of character rows and columns has
8438 not changed, the font size may have changed, so we need
8439 to check the pixel dimensions as well. */
8441 if (columns != f->width
8442 || rows != f->height
8443 || width != f->output_data.w32->pixel_width
8444 || height != f->output_data.w32->pixel_height)
8446 change_frame_size (f, rows, columns, 0, 1, 0);
8447 SET_FRAME_GARBAGED (f);
8448 cancel_mouse_face (f);
8449 f->output_data.w32->pixel_width = width;
8450 f->output_data.w32->pixel_height = height;
8451 f->output_data.w32->win_gravity = NorthWestGravity;
8455 check_visibility = 1;
8456 break;
8458 case WM_SETFOCUS:
8459 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
8461 dpyinfo->w32_focus_event_frame = f;
8463 if (f)
8464 x_new_focus_frame (dpyinfo, f);
8467 dpyinfo->grabbed = 0;
8468 check_visibility = 1;
8469 break;
8471 case WM_KILLFOCUS:
8472 /* TODO: some of this belongs in MOUSE_LEAVE */
8473 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
8475 if (f)
8477 Lisp_Object frame;
8479 if (f == dpyinfo->w32_focus_event_frame)
8480 dpyinfo->w32_focus_event_frame = 0;
8482 if (f == dpyinfo->w32_focus_frame)
8483 x_new_focus_frame (dpyinfo, 0);
8485 if (f == dpyinfo->mouse_face_mouse_frame)
8487 /* If we move outside the frame, then we're
8488 certainly no longer on any text in the frame. */
8489 clear_mouse_face (dpyinfo);
8490 dpyinfo->mouse_face_mouse_frame = 0;
8493 /* Generate a nil HELP_EVENT to cancel a help-echo.
8494 Do it only if there's something to cancel.
8495 Otherwise, the startup message is cleared when
8496 the mouse leaves the frame. */
8497 if (any_help_event_p)
8499 int n;
8501 XSETFRAME (frame, f);
8502 n = gen_help_event (bufp, numchars, Qnil, frame,
8503 Qnil, Qnil, 0);
8504 bufp += n, count += n, numchars -=n;
8508 dpyinfo->grabbed = 0;
8509 check_visibility = 1;
8510 break;
8512 case WM_CLOSE:
8513 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8515 if (f)
8517 if (numchars == 0)
8518 abort ();
8520 bufp->kind = delete_window_event;
8521 XSETFRAME (bufp->frame_or_window, f);
8522 bufp->arg = Qnil;
8523 bufp++;
8524 count++;
8525 numchars--;
8527 break;
8529 case WM_INITMENU:
8530 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8532 if (f)
8534 if (numchars == 0)
8535 abort ();
8537 bufp->kind = menu_bar_activate_event;
8538 XSETFRAME (bufp->frame_or_window, f);
8539 bufp->arg = Qnil;
8540 bufp++;
8541 count++;
8542 numchars--;
8544 break;
8546 case WM_COMMAND:
8547 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8549 if (f)
8551 extern void menubar_selection_callback
8552 (FRAME_PTR f, void * client_data);
8553 menubar_selection_callback (f, (void *)msg.msg.wParam);
8556 check_visibility = 1;
8557 break;
8559 case WM_DISPLAYCHANGE:
8560 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8562 if (f)
8564 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
8565 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
8566 dpyinfo->n_cbits = msg.msg.wParam;
8567 DebPrint (("display change: %d %d\n", dpyinfo->width,
8568 dpyinfo->height));
8571 check_visibility = 1;
8572 break;
8574 default:
8575 /* Check for messages registered at runtime. */
8576 if (msg.msg.message == msh_mousewheel)
8578 if (dpyinfo->grabbed && last_mouse_frame
8579 && FRAME_LIVE_P (last_mouse_frame))
8580 f = last_mouse_frame;
8581 else
8582 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
8584 if (f)
8586 if ((!dpyinfo->w32_focus_frame
8587 || f == dpyinfo->w32_focus_frame)
8588 && (numchars >= 1))
8590 construct_mouse_wheel (bufp, &msg, f);
8591 bufp++;
8592 count++;
8593 numchars--;
8597 break;
8601 /* If the focus was just given to an autoraising frame,
8602 raise it now. */
8603 /* ??? This ought to be able to handle more than one such frame. */
8604 if (pending_autoraise_frame)
8606 x_raise_frame (pending_autoraise_frame);
8607 pending_autoraise_frame = 0;
8610 /* Check which frames are still visisble, if we have enqueued any user
8611 events or been notified of events that may affect visibility. We
8612 do this here because there doesn't seem to be any direct
8613 notification from Windows that the visibility of a window has
8614 changed (at least, not in all cases). */
8615 if (count > 0 || check_visibility)
8617 Lisp_Object tail, frame;
8619 FOR_EACH_FRAME (tail, frame)
8621 FRAME_PTR f = XFRAME (frame);
8622 /* Check "visible" frames and mark each as obscured or not.
8623 Note that async_visible is nonzero for unobscured and
8624 obscured frames, but zero for hidden and iconified frames. */
8625 if (FRAME_W32_P (f) && f->async_visible)
8627 RECT clipbox;
8628 HDC hdc = get_frame_dc (f);
8629 GetClipBox (hdc, &clipbox);
8630 release_frame_dc (f, hdc);
8632 if (clipbox.right == clipbox.left
8633 || clipbox.bottom == clipbox.top)
8635 /* Frame has become completely obscured so mark as
8636 such (we do this by setting async_visible to 2 so
8637 that FRAME_VISIBLE_P is still true, but redisplay
8638 will skip it). */
8639 f->async_visible = 2;
8641 if (!FRAME_OBSCURED_P (f))
8643 DebPrint (("frame %04x (%s) obscured\n", f,
8644 XSTRING (f->name)->data));
8647 else
8649 /* Frame is not obscured, so mark it as such. */
8650 f->async_visible = 1;
8652 if (FRAME_OBSCURED_P (f))
8654 SET_FRAME_GARBAGED (f);
8655 DebPrint (("frame %04x (%s) reexposed\n", f,
8656 XSTRING (f->name)->data));
8658 /* Force a redisplay sooner or later. */
8659 record_asynch_buffer_change ();
8666 UNBLOCK_INPUT;
8667 return count;
8673 /***********************************************************************
8674 Text Cursor
8675 ***********************************************************************/
8677 /* Note if the text cursor of window W has been overwritten by a
8678 drawing operation that outputs N glyphs starting at HPOS in the
8679 line given by output_cursor.vpos. N < 0 means all the rest of the
8680 line after HPOS has been written. */
8682 static void
8683 note_overwritten_text_cursor (w, hpos, n)
8684 struct window *w;
8685 int hpos, n;
8687 if (updated_area == TEXT_AREA
8688 && output_cursor.vpos == w->phys_cursor.vpos
8689 && hpos <= w->phys_cursor.hpos
8690 && (n < 0
8691 || hpos + n > w->phys_cursor.hpos))
8692 w->phys_cursor_on_p = 0;
8696 /* Set clipping for output in glyph row ROW. W is the window in which
8697 we operate. GC is the graphics context to set clipping in.
8698 WHOLE_LINE_P non-zero means include the areas used for truncation
8699 mark display and alike in the clipping rectangle.
8701 ROW may be a text row or, e.g., a mode line. Text rows must be
8702 clipped to the interior of the window dedicated to text display,
8703 mode lines must be clipped to the whole window. */
8705 static void
8706 w32_clip_to_row (w, row, hdc, whole_line_p)
8707 struct window *w;
8708 struct glyph_row *row;
8709 HDC hdc;
8710 int whole_line_p;
8712 struct frame *f = XFRAME (WINDOW_FRAME (w));
8713 RECT clip_rect;
8714 int window_x, window_y, window_width, window_height;
8716 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8718 clip_rect.left = WINDOW_TO_FRAME_PIXEL_X (w, 0);
8719 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8720 clip_rect.top = max (clip_rect.top, window_y);
8721 clip_rect.right = clip_rect.left + window_width;
8722 clip_rect.bottom = clip_rect.top + row->visible_height;
8724 /* If clipping to the whole line, including trunc marks, extend
8725 the rectangle to the left and increase its width. */
8726 if (whole_line_p)
8728 clip_rect.left -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
8729 clip_rect.right += FRAME_X_FLAGS_AREA_WIDTH (f);
8732 w32_set_clip_rectangle (hdc, &clip_rect);
8736 /* Draw a hollow box cursor on window W in glyph row ROW. */
8738 static void
8739 x_draw_hollow_cursor (w, row)
8740 struct window *w;
8741 struct glyph_row *row;
8743 struct frame *f = XFRAME (WINDOW_FRAME (w));
8744 HDC hdc = get_frame_dc (f);
8745 RECT rect;
8746 int wd;
8747 struct glyph *cursor_glyph;
8748 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
8750 /* Compute frame-relative coordinates from window-relative
8751 coordinates. */
8752 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8753 rect.top = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
8754 + row->ascent - w->phys_cursor_ascent);
8755 rect.bottom = rect.top + row->height - 1;
8757 /* Get the glyph the cursor is on. If we can't tell because
8758 the current matrix is invalid or such, give up. */
8759 cursor_glyph = get_phys_cursor_glyph (w);
8760 if (cursor_glyph == NULL)
8761 return;
8763 /* Compute the width of the rectangle to draw. If on a stretch
8764 glyph, and `x-stretch-block-cursor' is nil, don't draw a
8765 rectangle as wide as the glyph, but use a canonical character
8766 width instead. */
8767 wd = cursor_glyph->pixel_width - 1;
8768 if (cursor_glyph->type == STRETCH_GLYPH
8769 && !x_stretch_cursor_p)
8770 wd = min (CANON_X_UNIT (f), wd);
8772 rect.right = rect.left + wd;
8774 FrameRect (hdc, &rect, hb);
8775 DeleteObject (hb);
8777 release_frame_dc (f, hdc);
8781 /* Draw a bar cursor on window W in glyph row ROW.
8783 Implementation note: One would like to draw a bar cursor with an
8784 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8785 Unfortunately, I didn't find a font yet that has this property set.
8786 --gerd. */
8788 static void
8789 x_draw_bar_cursor (w, row, width)
8790 struct window *w;
8791 struct glyph_row *row;
8792 int width;
8794 /* If cursor hpos is out of bounds, don't draw garbage. This can
8795 happen in mini-buffer windows when switching between echo area
8796 glyphs and mini-buffer. */
8797 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
8799 struct frame *f = XFRAME (w->frame);
8800 struct glyph *cursor_glyph;
8801 int x;
8802 HDC hdc;
8804 cursor_glyph = get_phys_cursor_glyph (w);
8805 if (cursor_glyph == NULL)
8806 return;
8808 /* If on an image, draw like a normal cursor. That's usually better
8809 visible than drawing a bar, esp. if the image is large so that
8810 the bar might not be in the window. */
8811 if (cursor_glyph->type == IMAGE_GLYPH)
8813 struct glyph_row *row;
8814 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8815 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
8817 else
8820 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8822 if (width < 0)
8823 width = f->output_data.w32->cursor_width;
8825 hdc = get_frame_dc (f);
8826 w32_fill_area (f, hdc, f->output_data.w32->cursor_pixel,
8828 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8829 min (cursor_glyph->pixel_width, width),
8830 row->height);
8831 release_frame_dc (f, hdc);
8837 /* Clear the cursor of window W to background color, and mark the
8838 cursor as not shown. This is used when the text where the cursor
8839 is is about to be rewritten. */
8841 static void
8842 x_clear_cursor (w)
8843 struct window *w;
8845 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
8846 x_update_window_cursor (w, 0);
8850 /* Draw the cursor glyph of window W in glyph row ROW. See the
8851 comment of x_draw_glyphs for the meaning of HL. */
8853 static void
8854 x_draw_phys_cursor_glyph (w, row, hl)
8855 struct window *w;
8856 struct glyph_row *row;
8857 enum draw_glyphs_face hl;
8859 /* If cursor hpos is out of bounds, don't draw garbage. This can
8860 happen in mini-buffer windows when switching between echo area
8861 glyphs and mini-buffer. */
8862 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
8864 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
8865 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
8866 hl, 0, 0, 0);
8868 /* When we erase the cursor, and ROW is overlapped by other
8869 rows, make sure that these overlapping parts of other rows
8870 are redrawn. */
8871 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
8873 if (row > w->current_matrix->rows
8874 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
8875 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
8877 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
8878 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
8879 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
8885 /* Erase the image of a cursor of window W from the screen. */
8887 static void
8888 x_erase_phys_cursor (w)
8889 struct window *w;
8891 struct frame *f = XFRAME (w->frame);
8892 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
8893 int hpos = w->phys_cursor.hpos;
8894 int vpos = w->phys_cursor.vpos;
8895 int mouse_face_here_p = 0;
8896 struct glyph_matrix *active_glyphs = w->current_matrix;
8897 struct glyph_row *cursor_row;
8898 struct glyph *cursor_glyph;
8899 enum draw_glyphs_face hl;
8901 /* No cursor displayed or row invalidated => nothing to do on the
8902 screen. */
8903 if (w->phys_cursor_type == NO_CURSOR)
8904 goto mark_cursor_off;
8906 /* VPOS >= active_glyphs->nrows means that window has been resized.
8907 Don't bother to erase the cursor. */
8908 if (vpos >= active_glyphs->nrows)
8909 goto mark_cursor_off;
8911 /* If row containing cursor is marked invalid, there is nothing we
8912 can do. */
8913 cursor_row = MATRIX_ROW (active_glyphs, vpos);
8914 if (!cursor_row->enabled_p)
8915 goto mark_cursor_off;
8917 /* This can happen when the new row is shorter than the old one.
8918 In this case, either x_draw_glyphs or clear_end_of_line
8919 should have cleared the cursor. Note that we wouldn't be
8920 able to erase the cursor in this case because we don't have a
8921 cursor glyph at hand. */
8922 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
8923 goto mark_cursor_off;
8925 /* If the cursor is in the mouse face area, redisplay that when
8926 we clear the cursor. */
8927 if (w == XWINDOW (dpyinfo->mouse_face_window)
8928 && (vpos > dpyinfo->mouse_face_beg_row
8929 || (vpos == dpyinfo->mouse_face_beg_row
8930 && hpos >= dpyinfo->mouse_face_beg_col))
8931 && (vpos < dpyinfo->mouse_face_end_row
8932 || (vpos == dpyinfo->mouse_face_end_row
8933 && hpos < dpyinfo->mouse_face_end_col))
8934 /* Don't redraw the cursor's spot in mouse face if it is at the
8935 end of a line (on a newline). The cursor appears there, but
8936 mouse highlighting does not. */
8937 && cursor_row->used[TEXT_AREA] > hpos)
8938 mouse_face_here_p = 1;
8940 /* Maybe clear the display under the cursor. */
8941 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
8943 int x;
8944 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8945 HDC hdc;
8947 cursor_glyph = get_phys_cursor_glyph (w);
8948 if (cursor_glyph == NULL)
8949 goto mark_cursor_off;
8951 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8953 hdc = get_frame_dc (f);
8954 w32_clear_area (f, hdc, x,
8955 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
8956 cursor_row->y)),
8957 cursor_glyph->pixel_width,
8958 cursor_row->visible_height);
8959 release_frame_dc (f, hdc);
8962 /* Erase the cursor by redrawing the character underneath it. */
8963 if (mouse_face_here_p)
8964 hl = DRAW_MOUSE_FACE;
8965 else if (cursor_row->inverse_p)
8966 hl = DRAW_INVERSE_VIDEO;
8967 else
8968 hl = DRAW_NORMAL_TEXT;
8969 x_draw_phys_cursor_glyph (w, cursor_row, hl);
8971 mark_cursor_off:
8972 w->phys_cursor_on_p = 0;
8973 w->phys_cursor_type = NO_CURSOR;
8977 /* Display or clear cursor of window W. If ON is zero, clear the
8978 cursor. If it is non-zero, display the cursor. If ON is nonzero,
8979 where to put the cursor is specified by HPOS, VPOS, X and Y. */
8981 void
8982 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
8983 struct window *w;
8984 int on, hpos, vpos, x, y;
8986 struct frame *f = XFRAME (w->frame);
8987 int new_cursor_type;
8988 int new_cursor_width;
8989 struct glyph_matrix *current_glyphs;
8990 struct glyph_row *glyph_row;
8991 struct glyph *glyph;
8993 /* This is pointless on invisible frames, and dangerous on garbaged
8994 windows and frames; in the latter case, the frame or window may
8995 be in the midst of changing its size, and x and y may be off the
8996 window. */
8997 if (! FRAME_VISIBLE_P (f)
8998 || FRAME_GARBAGED_P (f)
8999 || vpos >= w->current_matrix->nrows
9000 || hpos >= w->current_matrix->matrix_w)
9001 return;
9003 /* If cursor is off and we want it off, return quickly. */
9004 if (!on && !w->phys_cursor_on_p)
9005 return;
9007 current_glyphs = w->current_matrix;
9008 glyph_row = MATRIX_ROW (current_glyphs, vpos);
9009 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
9011 /* If cursor row is not enabled, we don't really know where to
9012 display the cursor. */
9013 if (!glyph_row->enabled_p)
9015 w->phys_cursor_on_p = 0;
9016 return;
9019 xassert (interrupt_input_blocked);
9021 /* Set new_cursor_type to the cursor we want to be displayed. In a
9022 mini-buffer window, we want the cursor only to appear if we are
9023 reading input from this window. For the selected window, we want
9024 the cursor type given by the frame parameter. If explicitly
9025 marked off, draw no cursor. In all other cases, we want a hollow
9026 box cursor. */
9027 new_cursor_width = -1;
9028 if (cursor_in_echo_area
9029 && FRAME_HAS_MINIBUF_P (f)
9030 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
9032 if (w == XWINDOW (echo_area_window))
9033 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9034 else
9035 new_cursor_type = HOLLOW_BOX_CURSOR;
9037 else
9039 if (w != XWINDOW (selected_window)
9040 || f != FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame)
9042 extern int cursor_in_non_selected_windows;
9044 if (MINI_WINDOW_P (w)
9045 || !cursor_in_non_selected_windows
9046 || NILP (XBUFFER (w->buffer)->cursor_type))
9047 new_cursor_type = NO_CURSOR;
9048 else
9049 new_cursor_type = HOLLOW_BOX_CURSOR;
9051 else if (w->cursor_off_p)
9052 new_cursor_type = NO_CURSOR;
9053 else
9055 struct buffer *b = XBUFFER (w->buffer);
9057 if (EQ (b->cursor_type, Qt))
9058 new_cursor_type = FRAME_DESIRED_CURSOR (f);
9059 else
9060 new_cursor_type = x_specified_cursor_type (b->cursor_type,
9061 &new_cursor_width);
9065 /* If cursor is currently being shown and we don't want it to be or
9066 it is in the wrong place, or the cursor type is not what we want,
9067 erase it. */
9068 if (w->phys_cursor_on_p
9069 && (!on
9070 || w->phys_cursor.x != x
9071 || w->phys_cursor.y != y
9072 || new_cursor_type != w->phys_cursor_type))
9073 x_erase_phys_cursor (w);
9075 /* If the cursor is now invisible and we want it to be visible,
9076 display it. */
9077 if (on && !w->phys_cursor_on_p)
9079 w->phys_cursor_ascent = glyph_row->ascent;
9080 w->phys_cursor_height = glyph_row->height;
9082 /* Set phys_cursor_.* before x_draw_.* is called because some
9083 of them may need the information. */
9084 w->phys_cursor.x = x;
9085 w->phys_cursor.y = glyph_row->y;
9086 w->phys_cursor.hpos = hpos;
9087 w->phys_cursor.vpos = vpos;
9088 w->phys_cursor_type = new_cursor_type;
9089 w->phys_cursor_on_p = 1;
9091 switch (new_cursor_type)
9093 case HOLLOW_BOX_CURSOR:
9094 x_draw_hollow_cursor (w, glyph_row);
9095 break;
9097 case FILLED_BOX_CURSOR:
9098 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9099 break;
9101 case BAR_CURSOR:
9102 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
9103 break;
9105 case NO_CURSOR:
9106 break;
9108 default:
9109 abort ();
9115 /* Display the cursor on window W, or clear it. X and Y are window
9116 relative pixel coordinates. HPOS and VPOS are glyph matrix
9117 positions. If W is not the selected window, display a hollow
9118 cursor. ON non-zero means display the cursor at X, Y which
9119 correspond to HPOS, VPOS, otherwise it is cleared. */
9121 void
9122 x_display_cursor (w, on, hpos, vpos, x, y)
9123 struct window *w;
9124 int on, hpos, vpos, x, y;
9126 BLOCK_INPUT;
9127 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
9128 UNBLOCK_INPUT;
9132 /* Display the cursor on window W, or clear it, according to ON_P.
9133 Don't change the cursor's position. */
9135 void
9136 x_update_cursor (f, on_p)
9137 struct frame *f;
9138 int on_p;
9140 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
9144 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
9145 in the window tree rooted at W. */
9147 static void
9148 x_update_cursor_in_window_tree (w, on_p)
9149 struct window *w;
9150 int on_p;
9152 while (w)
9154 if (!NILP (w->hchild))
9155 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
9156 else if (!NILP (w->vchild))
9157 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
9158 else
9159 x_update_window_cursor (w, on_p);
9161 w = NILP (w->next) ? 0 : XWINDOW (w->next);
9166 /* Switch the display of W's cursor on or off, according to the value
9167 of ON. */
9169 static void
9170 x_update_window_cursor (w, on)
9171 struct window *w;
9172 int on;
9174 /* Don't update cursor in windows whose frame is in the process
9175 of being deleted. */
9176 if (w->current_matrix)
9178 BLOCK_INPUT;
9179 x_display_and_set_cursor (w, on, w->phys_cursor.hpos,
9180 w->phys_cursor.vpos, w->phys_cursor.x,
9181 w->phys_cursor.y);
9182 UNBLOCK_INPUT;
9189 /* Icons. */
9192 x_bitmap_icon (f, icon)
9193 struct frame *f;
9194 Lisp_Object icon;
9196 int mask, bitmap_id;
9197 Window icon_window;
9198 HANDLE hicon;
9200 if (FRAME_W32_WINDOW (f) == 0)
9201 return 1;
9203 if (NILP (icon))
9204 hicon = LoadIcon (hinst, EMACS_CLASS);
9205 else if (STRINGP (icon))
9206 hicon = LoadImage (NULL, (LPCTSTR) XSTRING (icon)->data, IMAGE_ICON, 0, 0,
9207 LR_DEFAULTSIZE | LR_LOADFROMFILE);
9208 else if (SYMBOLP (icon))
9210 LPCTSTR name;
9212 if (EQ (icon, intern ("application")))
9213 name = (LPCTSTR) IDI_APPLICATION;
9214 else if (EQ (icon, intern ("hand")))
9215 name = (LPCTSTR) IDI_HAND;
9216 else if (EQ (icon, intern ("question")))
9217 name = (LPCTSTR) IDI_QUESTION;
9218 else if (EQ (icon, intern ("exclamation")))
9219 name = (LPCTSTR) IDI_EXCLAMATION;
9220 else if (EQ (icon, intern ("asterisk")))
9221 name = (LPCTSTR) IDI_ASTERISK;
9222 else if (EQ (icon, intern ("winlogo")))
9223 name = (LPCTSTR) IDI_WINLOGO;
9224 else
9225 return 1;
9227 hicon = LoadIcon (NULL, name);
9229 else
9230 return 1;
9232 if (hicon == NULL)
9233 return 1;
9235 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
9236 (LPARAM) hicon);
9238 return 0;
9242 /* Changing the font of the frame. */
9244 /* Give frame F the font named FONTNAME as its default font, and
9245 return the full name of that font. FONTNAME may be a wildcard
9246 pattern; in that case, we choose some font that fits the pattern.
9247 The return value shows which font we chose. */
9249 Lisp_Object
9250 x_new_font (f, fontname)
9251 struct frame *f;
9252 register char *fontname;
9254 struct font_info *fontp
9255 = FS_LOAD_FONT (f, 0, fontname, -1);
9257 if (!fontp)
9258 return Qnil;
9260 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
9261 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
9262 FRAME_FONTSET (f) = -1;
9264 /* Compute the scroll bar width in character columns. */
9265 if (f->scroll_bar_pixel_width > 0)
9267 int wid = FONT_WIDTH (FRAME_FONT (f));
9268 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
9270 else
9272 int wid = FONT_WIDTH (FRAME_FONT (f));
9273 f->scroll_bar_cols = (14 + wid - 1) / wid;
9276 /* Now make the frame display the given font. */
9277 if (FRAME_W32_WINDOW (f) != 0)
9279 frame_update_line_height (f);
9280 x_set_window_size (f, 0, f->width, f->height);
9282 else
9283 /* If we are setting a new frame's font for the first time,
9284 there are no faces yet, so this font's height is the line height. */
9285 f->output_data.w32->line_height = FONT_HEIGHT (FRAME_FONT (f));
9287 return build_string (fontp->full_name);
9290 /* Give frame F the fontset named FONTSETNAME as its default font, and
9291 return the full name of that fontset. FONTSETNAME may be a wildcard
9292 pattern; in that case, we choose some fontset that fits the pattern.
9293 The return value shows which fontset we chose. */
9295 Lisp_Object
9296 x_new_fontset (f, fontsetname)
9297 struct frame *f;
9298 char *fontsetname;
9300 int fontset = fs_query_fontset (build_string (fontsetname), 0);
9301 Lisp_Object result;
9302 char *fontname;
9304 if (fontset < 0)
9305 return Qnil;
9307 if (FRAME_FONTSET (f) == fontset)
9308 /* This fontset is already set in frame F. There's nothing more
9309 to do. */
9310 return fontset_name (fontset);
9312 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
9314 if (!STRINGP (result))
9315 /* Can't load ASCII font. */
9316 return Qnil;
9318 /* Since x_new_font doesn't update any fontset information, do it now. */
9319 FRAME_FONTSET(f) = fontset;
9321 return build_string (fontsetname);
9325 #if GLYPH_DEBUG
9327 /* Check that FONT is valid on frame F. It is if it can be found in F's
9328 font table. */
9330 static void
9331 x_check_font (f, font)
9332 struct frame *f;
9333 XFontStruct *font;
9335 int i;
9336 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9338 xassert (font != NULL);
9340 for (i = 0; i < dpyinfo->n_fonts; i++)
9341 if (dpyinfo->font_table[i].name
9342 && font == dpyinfo->font_table[i].font)
9343 break;
9345 xassert (i < dpyinfo->n_fonts);
9348 #endif /* GLYPH_DEBUG != 0 */
9350 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9351 Note: There are (broken) X fonts out there with invalid XFontStruct
9352 min_bounds contents. For example, handa@etl.go.jp reports that
9353 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9354 have font->min_bounds.width == 0. */
9356 static INLINE void
9357 x_font_min_bounds (font, w, h)
9358 XFontStruct *font;
9359 int *w, *h;
9362 * TODO: Windows does not appear to offer min bound, only
9363 * average and maximum width, and maximum height.
9365 *h = FONT_HEIGHT (font);
9366 *w = FONT_WIDTH (font);
9370 /* Compute the smallest character width and smallest font height over
9371 all fonts available on frame F. Set the members smallest_char_width
9372 and smallest_font_height in F's x_display_info structure to
9373 the values computed. Value is non-zero if smallest_font_height or
9374 smallest_char_width become smaller than they were before. */
9377 x_compute_min_glyph_bounds (f)
9378 struct frame *f;
9380 int i;
9381 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9382 XFontStruct *font;
9383 int old_width = dpyinfo->smallest_char_width;
9384 int old_height = dpyinfo->smallest_font_height;
9386 dpyinfo->smallest_font_height = 100000;
9387 dpyinfo->smallest_char_width = 100000;
9389 for (i = 0; i < dpyinfo->n_fonts; ++i)
9390 if (dpyinfo->font_table[i].name)
9392 struct font_info *fontp = dpyinfo->font_table + i;
9393 int w, h;
9395 font = (XFontStruct *) fontp->font;
9396 xassert (font != (XFontStruct *) ~0);
9397 x_font_min_bounds (font, &w, &h);
9399 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9400 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9403 xassert (dpyinfo->smallest_char_width > 0
9404 && dpyinfo->smallest_font_height > 0);
9406 return (dpyinfo->n_fonts == 1
9407 || dpyinfo->smallest_char_width < old_width
9408 || dpyinfo->smallest_font_height < old_height);
9412 /* Calculate the absolute position in frame F
9413 from its current recorded position values and gravity. */
9415 void
9416 x_calc_absolute_position (f)
9417 struct frame *f;
9419 Window child;
9420 POINT pt;
9421 int flags = f->output_data.w32->size_hint_flags;
9423 pt.x = pt.y = 0;
9425 /* Find the position of the outside upper-left corner of
9426 the inner window, with respect to the outer window. */
9427 if (f->output_data.w32->parent_desc != FRAME_W32_DISPLAY_INFO (f)->root_window)
9429 BLOCK_INPUT;
9430 MapWindowPoints (FRAME_W32_WINDOW (f),
9431 f->output_data.w32->parent_desc,
9432 &pt, 1);
9433 UNBLOCK_INPUT;
9437 RECT rt;
9438 rt.left = rt.right = rt.top = rt.bottom = 0;
9440 BLOCK_INPUT;
9441 AdjustWindowRect(&rt, f->output_data.w32->dwStyle,
9442 FRAME_EXTERNAL_MENU_BAR (f));
9443 UNBLOCK_INPUT;
9445 pt.x += (rt.right - rt.left);
9446 pt.y += (rt.bottom - rt.top);
9449 /* Treat negative positions as relative to the leftmost bottommost
9450 position that fits on the screen. */
9451 if (flags & XNegative)
9452 f->output_data.w32->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
9453 - 2 * f->output_data.w32->border_width - pt.x
9454 - PIXEL_WIDTH (f)
9455 + f->output_data.w32->left_pos);
9457 if (flags & YNegative)
9458 f->output_data.w32->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
9459 - 2 * f->output_data.w32->border_width - pt.y
9460 - PIXEL_HEIGHT (f)
9461 + f->output_data.w32->top_pos);
9462 /* The left_pos and top_pos
9463 are now relative to the top and left screen edges,
9464 so the flags should correspond. */
9465 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
9468 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9469 to really change the position, and 0 when calling from
9470 x_make_frame_visible (in that case, XOFF and YOFF are the current
9471 position values). It is -1 when calling from x_set_frame_parameters,
9472 which means, do adjust for borders but don't change the gravity. */
9474 void
9475 x_set_offset (f, xoff, yoff, change_gravity)
9476 struct frame *f;
9477 register int xoff, yoff;
9478 int change_gravity;
9480 int modified_top, modified_left;
9482 if (change_gravity > 0)
9484 f->output_data.w32->top_pos = yoff;
9485 f->output_data.w32->left_pos = xoff;
9486 f->output_data.w32->size_hint_flags &= ~ (XNegative | YNegative);
9487 if (xoff < 0)
9488 f->output_data.w32->size_hint_flags |= XNegative;
9489 if (yoff < 0)
9490 f->output_data.w32->size_hint_flags |= YNegative;
9491 f->output_data.w32->win_gravity = NorthWestGravity;
9493 x_calc_absolute_position (f);
9495 BLOCK_INPUT;
9496 x_wm_set_size_hint (f, (long) 0, 0);
9498 modified_left = f->output_data.w32->left_pos;
9499 modified_top = f->output_data.w32->top_pos;
9501 my_set_window_pos (FRAME_W32_WINDOW (f),
9502 NULL,
9503 modified_left, modified_top,
9504 0, 0,
9505 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
9506 UNBLOCK_INPUT;
9509 /* Call this to change the size of frame F's x-window.
9510 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9511 for this size change and subsequent size changes.
9512 Otherwise we leave the window gravity unchanged. */
9513 void
9514 x_set_window_size (f, change_gravity, cols, rows)
9515 struct frame *f;
9516 int change_gravity;
9517 int cols, rows;
9519 int pixelwidth, pixelheight;
9521 BLOCK_INPUT;
9523 check_frame_size (f, &rows, &cols);
9524 f->output_data.w32->vertical_scroll_bar_extra
9525 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
9527 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.w32->font)));
9528 f->output_data.w32->flags_areas_extra
9529 = FRAME_FLAGS_AREA_WIDTH (f);
9530 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
9531 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
9533 f->output_data.w32->win_gravity = NorthWestGravity;
9534 x_wm_set_size_hint (f, (long) 0, 0);
9537 RECT rect;
9539 rect.left = rect.top = 0;
9540 rect.right = pixelwidth;
9541 rect.bottom = pixelheight;
9543 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
9544 FRAME_EXTERNAL_MENU_BAR (f));
9546 my_set_window_pos (FRAME_W32_WINDOW (f),
9547 NULL,
9548 0, 0,
9549 rect.right - rect.left,
9550 rect.bottom - rect.top,
9551 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
9554 /* Now, strictly speaking, we can't be sure that this is accurate,
9555 but the window manager will get around to dealing with the size
9556 change request eventually, and we'll hear how it went when the
9557 ConfigureNotify event gets here.
9559 We could just not bother storing any of this information here,
9560 and let the ConfigureNotify event set everything up, but that
9561 might be kind of confusing to the Lisp code, since size changes
9562 wouldn't be reported in the frame parameters until some random
9563 point in the future when the ConfigureNotify event arrives.
9565 We pass 1 for DELAY since we can't run Lisp code inside of
9566 a BLOCK_INPUT. */
9567 change_frame_size (f, rows, cols, 0, 1, 0);
9568 PIXEL_WIDTH (f) = pixelwidth;
9569 PIXEL_HEIGHT (f) = pixelheight;
9571 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9572 receive in the ConfigureNotify event; if we get what we asked
9573 for, then the event won't cause the screen to become garbaged, so
9574 we have to make sure to do it here. */
9575 SET_FRAME_GARBAGED (f);
9577 /* If cursor was outside the new size, mark it as off. */
9578 mark_window_cursors_off (XWINDOW (f->root_window));
9580 /* Clear out any recollection of where the mouse highlighting was,
9581 since it might be in a place that's outside the new frame size.
9582 Actually checking whether it is outside is a pain in the neck,
9583 so don't try--just let the highlighting be done afresh with new size. */
9584 cancel_mouse_face (f);
9586 UNBLOCK_INPUT;
9589 /* Mouse warping. */
9591 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
9593 void
9594 x_set_mouse_position (f, x, y)
9595 struct frame *f;
9596 int x, y;
9598 int pix_x, pix_y;
9600 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.w32->font) / 2;
9601 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.w32->line_height / 2;
9603 if (pix_x < 0) pix_x = 0;
9604 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
9606 if (pix_y < 0) pix_y = 0;
9607 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
9609 x_set_mouse_pixel_position (f, pix_x, pix_y);
9612 void
9613 x_set_mouse_pixel_position (f, pix_x, pix_y)
9614 struct frame *f;
9615 int pix_x, pix_y;
9617 RECT rect;
9618 POINT pt;
9620 BLOCK_INPUT;
9622 GetClientRect (FRAME_W32_WINDOW (f), &rect);
9623 pt.x = rect.left + pix_x;
9624 pt.y = rect.top + pix_y;
9625 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
9627 SetCursorPos (pt.x, pt.y);
9629 UNBLOCK_INPUT;
9633 /* focus shifting, raising and lowering. */
9635 void
9636 x_focus_on_frame (f)
9637 struct frame *f;
9639 struct w32_display_info *dpyinfo = &one_w32_display_info;
9641 /* Give input focus to frame. */
9642 BLOCK_INPUT;
9643 #if 0
9644 /* Try not to change its Z-order if possible. */
9645 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
9646 my_set_focus (f, FRAME_W32_WINDOW (f));
9647 else
9648 #endif
9649 my_set_foreground_window (FRAME_W32_WINDOW (f));
9650 UNBLOCK_INPUT;
9653 void
9654 x_unfocus_frame (f)
9655 struct frame *f;
9659 /* Raise frame F. */
9660 void
9661 x_raise_frame (f)
9662 struct frame *f;
9664 BLOCK_INPUT;
9666 /* Strictly speaking, raise-frame should only change the frame's Z
9667 order, leaving input focus unchanged. This is reasonable behaviour
9668 on X where the usual policy is point-to-focus. However, this
9669 behaviour would be very odd on Windows where the usual policy is
9670 click-to-focus.
9672 On X, if the mouse happens to be over the raised frame, it gets
9673 input focus anyway (so the window with focus will never be
9674 completely obscured) - if not, then just moving the mouse over it
9675 is sufficient to give it focus. On Windows, the user must actually
9676 click on the frame (preferrably the title bar so as not to move
9677 point), which is more awkward. Also, no other Windows program
9678 raises a window to the top but leaves another window (possibly now
9679 completely obscured) with input focus.
9681 Because there is a system setting on Windows that allows the user
9682 to choose the point to focus policy, we make the strict semantics
9683 optional, but by default we grab focus when raising. */
9685 if (NILP (Vw32_grab_focus_on_raise))
9687 /* The obvious call to my_set_window_pos doesn't work if Emacs is
9688 not already the foreground application: the frame is raised
9689 above all other frames belonging to us, but not above the
9690 current top window. To achieve that, we have to resort to this
9691 more cumbersome method. */
9693 HDWP handle = BeginDeferWindowPos (2);
9694 if (handle)
9696 DeferWindowPos (handle,
9697 FRAME_W32_WINDOW (f),
9698 HWND_TOP,
9699 0, 0, 0, 0,
9700 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9702 DeferWindowPos (handle,
9703 GetForegroundWindow (),
9704 FRAME_W32_WINDOW (f),
9705 0, 0, 0, 0,
9706 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9708 EndDeferWindowPos (handle);
9711 else
9713 my_set_foreground_window (FRAME_W32_WINDOW (f));
9716 UNBLOCK_INPUT;
9719 /* Lower frame F. */
9720 void
9721 x_lower_frame (f)
9722 struct frame *f;
9724 BLOCK_INPUT;
9725 my_set_window_pos (FRAME_W32_WINDOW (f),
9726 HWND_BOTTOM,
9727 0, 0, 0, 0,
9728 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
9729 UNBLOCK_INPUT;
9732 static void
9733 w32_frame_raise_lower (f, raise_flag)
9734 FRAME_PTR f;
9735 int raise_flag;
9737 if (raise_flag)
9738 x_raise_frame (f);
9739 else
9740 x_lower_frame (f);
9743 /* Change of visibility. */
9745 /* This tries to wait until the frame is really visible.
9746 However, if the window manager asks the user where to position
9747 the frame, this will return before the user finishes doing that.
9748 The frame will not actually be visible at that time,
9749 but it will become visible later when the window manager
9750 finishes with it. */
9752 void
9753 x_make_frame_visible (f)
9754 struct frame *f;
9756 Lisp_Object type;
9758 BLOCK_INPUT;
9760 type = x_icon_type (f);
9761 if (!NILP (type))
9762 x_bitmap_icon (f, type);
9764 if (! FRAME_VISIBLE_P (f))
9766 /* We test FRAME_GARBAGED_P here to make sure we don't
9767 call x_set_offset a second time
9768 if we get to x_make_frame_visible a second time
9769 before the window gets really visible. */
9770 if (! FRAME_ICONIFIED_P (f)
9771 && ! f->output_data.w32->asked_for_visible)
9772 x_set_offset (f, f->output_data.w32->left_pos, f->output_data.w32->top_pos, 0);
9774 f->output_data.w32->asked_for_visible = 1;
9776 // my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW);
9777 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
9780 /* Synchronize to ensure Emacs knows the frame is visible
9781 before we do anything else. We do this loop with input not blocked
9782 so that incoming events are handled. */
9784 Lisp_Object frame;
9785 int count;
9787 /* This must come after we set COUNT. */
9788 UNBLOCK_INPUT;
9790 XSETFRAME (frame, f);
9792 /* Wait until the frame is visible. Process X events until a
9793 MapNotify event has been seen, or until we think we won't get a
9794 MapNotify at all.. */
9795 for (count = input_signal_count + 10;
9796 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9798 /* Force processing of queued events. */
9799 /* TODO: x_sync equivalent? */
9801 /* Machines that do polling rather than SIGIO have been observed
9802 to go into a busy-wait here. So we'll fake an alarm signal
9803 to let the handler know that there's something to be read.
9804 We used to raise a real alarm, but it seems that the handler
9805 isn't always enabled here. This is probably a bug. */
9806 if (input_polling_used ())
9808 /* It could be confusing if a real alarm arrives while processing
9809 the fake one. Turn it off and let the handler reset it. */
9810 int old_poll_suppress_count = poll_suppress_count;
9811 poll_suppress_count = 1;
9812 poll_for_input_1 ();
9813 poll_suppress_count = old_poll_suppress_count;
9816 FRAME_SAMPLE_VISIBILITY (f);
9820 /* Change from mapped state to withdrawn state. */
9822 /* Make the frame visible (mapped and not iconified). */
9824 x_make_frame_invisible (f)
9825 struct frame *f;
9827 /* Don't keep the highlight on an invisible frame. */
9828 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9829 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
9831 BLOCK_INPUT;
9833 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
9835 /* We can't distinguish this from iconification
9836 just by the event that we get from the server.
9837 So we can't win using the usual strategy of letting
9838 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9839 and synchronize with the server to make sure we agree. */
9840 f->visible = 0;
9841 FRAME_ICONIFIED_P (f) = 0;
9842 f->async_visible = 0;
9843 f->async_iconified = 0;
9845 UNBLOCK_INPUT;
9848 /* Change window state from mapped to iconified. */
9850 void
9851 x_iconify_frame (f)
9852 struct frame *f;
9854 int result;
9855 Lisp_Object type;
9857 /* Don't keep the highlight on an invisible frame. */
9858 if (FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame == f)
9859 FRAME_W32_DISPLAY_INFO (f)->w32_highlight_frame = 0;
9861 if (f->async_iconified)
9862 return;
9864 BLOCK_INPUT;
9866 type = x_icon_type (f);
9867 if (!NILP (type))
9868 x_bitmap_icon (f, type);
9870 /* Simulate the user minimizing the frame. */
9871 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
9873 UNBLOCK_INPUT;
9876 /* Destroy the window of frame F. */
9878 x_destroy_window (f)
9879 struct frame *f;
9881 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
9883 BLOCK_INPUT;
9885 my_destroy_window (f, FRAME_W32_WINDOW (f));
9886 free_frame_menubar (f);
9887 free_frame_faces (f);
9889 xfree (f->output_data.w32);
9890 f->output_data.w32 = 0;
9891 if (f == dpyinfo->w32_focus_frame)
9892 dpyinfo->w32_focus_frame = 0;
9893 if (f == dpyinfo->w32_focus_event_frame)
9894 dpyinfo->w32_focus_event_frame = 0;
9895 if (f == dpyinfo->w32_highlight_frame)
9896 dpyinfo->w32_highlight_frame = 0;
9898 dpyinfo->reference_count--;
9900 if (f == dpyinfo->mouse_face_mouse_frame)
9902 dpyinfo->mouse_face_beg_row
9903 = dpyinfo->mouse_face_beg_col = -1;
9904 dpyinfo->mouse_face_end_row
9905 = dpyinfo->mouse_face_end_col = -1;
9906 dpyinfo->mouse_face_window = Qnil;
9909 UNBLOCK_INPUT;
9912 /* Setting window manager hints. */
9914 /* Set the normal size hints for the window manager, for frame F.
9915 FLAGS is the flags word to use--or 0 meaning preserve the flags
9916 that the window now has.
9917 If USER_POSITION is nonzero, we set the USPosition
9918 flag (this is useful when FLAGS is 0). */
9919 void
9920 x_wm_set_size_hint (f, flags, user_position)
9921 struct frame *f;
9922 long flags;
9923 int user_position;
9925 Window window = FRAME_W32_WINDOW (f);
9927 enter_crit ();
9929 SetWindowLong (window, WND_FONTWIDTH_INDEX, FONT_WIDTH (f->output_data.w32->font));
9930 SetWindowLong (window, WND_LINEHEIGHT_INDEX, f->output_data.w32->line_height);
9931 SetWindowLong (window, WND_BORDER_INDEX, f->output_data.w32->internal_border_width);
9932 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->output_data.w32->vertical_scroll_bar_extra);
9934 leave_crit ();
9937 /* Window manager things */
9938 x_wm_set_icon_position (f, icon_x, icon_y)
9939 struct frame *f;
9940 int icon_x, icon_y;
9942 #if 0
9943 Window window = FRAME_W32_WINDOW (f);
9945 f->display.x->wm_hints.flags |= IconPositionHint;
9946 f->display.x->wm_hints.icon_x = icon_x;
9947 f->display.x->wm_hints.icon_y = icon_y;
9949 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
9950 #endif
9955 /***********************************************************************
9956 Initialization
9957 ***********************************************************************/
9959 static int w32_initialized = 0;
9961 void
9962 w32_initialize_display_info (display_name)
9963 Lisp_Object display_name;
9965 struct w32_display_info *dpyinfo = &one_w32_display_info;
9967 bzero (dpyinfo, sizeof (*dpyinfo));
9969 /* Put it on w32_display_name_list. */
9970 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
9971 w32_display_name_list);
9972 dpyinfo->name_list_element = XCAR (w32_display_name_list);
9974 dpyinfo->w32_id_name
9975 = (char *) xmalloc (XSTRING (Vinvocation_name)->size
9976 + XSTRING (Vsystem_name)->size
9977 + 2);
9978 sprintf (dpyinfo->w32_id_name, "%s@%s",
9979 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
9981 /* Default Console mode values - overridden when running in GUI mode
9982 with values obtained from system metrics. */
9983 dpyinfo->resx = 1;
9984 dpyinfo->resy = 1;
9985 dpyinfo->height_in = 1;
9986 dpyinfo->width_in = 1;
9987 dpyinfo->n_planes = 1;
9988 dpyinfo->n_cbits = 4;
9989 dpyinfo->n_fonts = 0;
9990 dpyinfo->smallest_font_height = 1;
9991 dpyinfo->smallest_char_width = 1;
9993 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
9994 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
9995 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
9996 dpyinfo->mouse_face_window = Qnil;
9998 /* TODO: dpyinfo->gray */
10002 struct w32_display_info *
10003 w32_term_init (display_name, xrm_option, resource_name)
10004 Lisp_Object display_name;
10005 char *xrm_option;
10006 char *resource_name;
10008 struct w32_display_info *dpyinfo;
10009 HDC hdc;
10011 BLOCK_INPUT;
10013 if (!w32_initialized)
10015 w32_initialize ();
10016 w32_initialized = 1;
10020 int argc = 0;
10021 char *argv[3];
10023 argv[0] = "";
10024 argc = 1;
10025 if (xrm_option)
10027 argv[argc++] = "-xrm";
10028 argv[argc++] = xrm_option;
10032 w32_initialize_display_info (display_name);
10034 dpyinfo = &one_w32_display_info;
10036 hdc = GetDC (GetDesktopWindow ());
10038 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
10039 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
10040 dpyinfo->root_window = GetDesktopWindow ();
10041 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
10042 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
10043 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
10044 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
10045 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
10046 dpyinfo->image_cache = make_image_cache ();
10047 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
10048 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
10049 ReleaseDC (GetDesktopWindow (), hdc);
10051 /* initialise palette with white and black */
10053 COLORREF color;
10054 w32_defined_color (0, "white", &color, 1);
10055 w32_defined_color (0, "black", &color, 1);
10058 /* Create Row Bitmaps and store them for later use. */
10059 left_bmp = CreateBitmap (left_width, left_height, 1, 1, left_bits);
10060 ov_bmp = CreateBitmap (ov_width, ov_height, 1, 1, ov_bits);
10061 right_bmp = CreateBitmap (right_width, right_height, 1, 1, right_bits);
10062 continued_bmp = CreateBitmap (continued_width, continued_height, 1,
10063 1, continued_bits);
10064 continuation_bmp = CreateBitmap (continuation_width, continuation_height,
10065 1, 1, continuation_bits);
10066 zv_bmp = CreateBitmap (zv_width, zv_height, 1, 1, zv_bits);
10068 #ifndef F_SETOWN_BUG
10069 #ifdef F_SETOWN
10070 #ifdef F_SETOWN_SOCK_NEG
10071 /* stdin is a socket here */
10072 fcntl (connection, F_SETOWN, -getpid ());
10073 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10074 fcntl (connection, F_SETOWN, getpid ());
10075 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10076 #endif /* ! defined (F_SETOWN) */
10077 #endif /* F_SETOWN_BUG */
10079 #ifdef SIGIO
10080 if (interrupt_input)
10081 init_sigio (connection);
10082 #endif /* ! defined (SIGIO) */
10084 UNBLOCK_INPUT;
10086 return dpyinfo;
10089 /* Get rid of display DPYINFO, assuming all frames are already gone. */
10091 void
10092 x_delete_display (dpyinfo)
10093 struct w32_display_info *dpyinfo;
10095 /* Discard this display from w32_display_name_list and w32_display_list.
10096 We can't use Fdelq because that can quit. */
10097 if (! NILP (w32_display_name_list)
10098 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
10099 w32_display_name_list = XCDR (w32_display_name_list);
10100 else
10102 Lisp_Object tail;
10104 tail = w32_display_name_list;
10105 while (CONSP (tail) && CONSP (XCDR (tail)))
10107 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10109 XCDR (tail) = XCDR (XCDR (tail));
10110 break;
10112 tail = XCDR (tail);
10116 /* free palette table */
10118 struct w32_palette_entry * plist;
10120 plist = dpyinfo->color_list;
10121 while (plist)
10123 struct w32_palette_entry * pentry = plist;
10124 plist = plist->next;
10125 xfree (pentry);
10127 dpyinfo->color_list = NULL;
10128 if (dpyinfo->palette)
10129 DeleteObject(dpyinfo->palette);
10131 xfree (dpyinfo->font_table);
10132 xfree (dpyinfo->w32_id_name);
10134 /* Destroy row bitmaps. */
10135 DeleteObject (left_bmp);
10136 DeleteObject (ov_bmp);
10137 DeleteObject (right_bmp);
10138 DeleteObject (continued_bmp);
10139 DeleteObject (continuation_bmp);
10140 DeleteObject (zv_bmp);
10143 /* Set up use of W32. */
10145 DWORD w32_msg_worker ();
10147 void
10148 x_flush (struct frame * f)
10149 { /* Nothing to do */ }
10151 static struct redisplay_interface w32_redisplay_interface =
10153 x_produce_glyphs,
10154 x_write_glyphs,
10155 x_insert_glyphs,
10156 x_clear_end_of_line,
10157 x_scroll_run,
10158 x_after_update_window_line,
10159 x_update_window_begin,
10160 x_update_window_end,
10161 w32_cursor_to,
10162 x_flush,
10163 x_clear_mouse_face,
10164 x_get_glyph_overhangs,
10165 x_fix_overlapping_area
10168 void
10169 w32_initialize ()
10171 rif = &w32_redisplay_interface;
10173 /* MSVC does not type K&R functions with no arguments correctly, and
10174 so we must explicitly cast them. */
10175 clear_frame_hook = (void (*)(void)) x_clear_frame;
10176 ins_del_lines_hook = x_ins_del_lines;
10177 change_line_highlight_hook = x_change_line_highlight;
10178 delete_glyphs_hook = x_delete_glyphs;
10179 ring_bell_hook = (void (*)(void)) w32_ring_bell;
10180 reset_terminal_modes_hook = (void (*)(void)) w32_reset_terminal_modes;
10181 set_terminal_modes_hook = (void (*)(void)) w32_set_terminal_modes;
10182 update_begin_hook = x_update_begin;
10183 update_end_hook = x_update_end;
10184 set_terminal_window_hook = w32_set_terminal_window;
10185 read_socket_hook = w32_read_socket;
10186 frame_up_to_date_hook = w32_frame_up_to_date;
10187 reassert_line_highlight_hook = w32_reassert_line_highlight;
10188 mouse_position_hook = w32_mouse_position;
10189 frame_rehighlight_hook = w32_frame_rehighlight;
10190 frame_raise_lower_hook = w32_frame_raise_lower;
10191 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
10192 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
10193 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
10194 judge_scroll_bars_hook = w32_judge_scroll_bars;
10195 estimate_mode_line_height_hook = x_estimate_mode_line_height;
10197 scroll_region_ok = 1; /* we'll scroll partial frames */
10198 char_ins_del_ok = 0; /* just as fast to write the line */
10199 line_ins_del_ok = 1; /* we'll just blt 'em */
10200 fast_clear_end_of_line = 1; /* X does this well */
10201 memory_below_frame = 0; /* we don't remember what scrolls
10202 off the bottom */
10203 baud_rate = 19200;
10205 last_tool_bar_item = -1;
10206 any_help_event_p = 0;
10208 /* Initialize input mode: interrupt_input off, no flow control, allow
10209 8 bit character input, standard quit char. */
10210 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
10212 /* Create the window thread - it will terminate itself or when the app terminates */
10214 init_crit ();
10216 dwMainThreadId = GetCurrentThreadId ();
10217 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
10218 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
10220 /* Wait for thread to start */
10223 MSG msg;
10225 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
10227 hWindowsThread = CreateThread (NULL, 0,
10228 (LPTHREAD_START_ROUTINE) w32_msg_worker,
10229 0, 0, &dwWindowsThreadId);
10231 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
10234 /* It is desirable that mainThread should have the same notion of
10235 focus window and active window as windowsThread. Unfortunately, the
10236 following call to AttachThreadInput, which should do precisely what
10237 we need, causes major problems when Emacs is linked as a console
10238 program. Unfortunately, we have good reasons for doing that, so
10239 instead we need to send messages to windowsThread to make some API
10240 calls for us (ones that affect, or depend on, the active/focus
10241 window state. */
10242 #ifdef ATTACH_THREADS
10243 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
10244 #endif
10246 /* Dynamically link to optional system components. */
10248 HANDLE user_lib = LoadLibrary ("user32.dll");
10250 #define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
10252 /* New proportional scroll bar functions. */
10253 LOAD_PROC (SetScrollInfo);
10254 LOAD_PROC (GetScrollInfo);
10256 #undef LOAD_PROC
10258 FreeLibrary (user_lib);
10260 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
10261 otherwise use the fixed height. */
10262 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
10263 GetSystemMetrics (SM_CYVTHUMB);
10265 /* For either kind of scroll bar, take account of the arrows; these
10266 effectively form the border of the main scroll bar range. */
10267 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
10268 = GetSystemMetrics (SM_CYVSCROLL);
10272 void
10273 syms_of_w32term ()
10275 staticpro (&w32_display_name_list);
10276 w32_display_name_list = Qnil;
10278 staticpro (&last_mouse_scroll_bar);
10279 last_mouse_scroll_bar = Qnil;
10281 staticpro (&Qvendor_specific_keysyms);
10282 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10284 DEFVAR_INT ("w32-num-mouse-buttons",
10285 &Vw32_num_mouse_buttons,
10286 "Number of physical mouse buttons.");
10287 Vw32_num_mouse_buttons = Qnil;
10289 DEFVAR_LISP ("w32-swap-mouse-buttons",
10290 &Vw32_swap_mouse_buttons,
10291 "Swap the mapping of middle and right mouse buttons.\n\
10292 When nil, middle button is mouse-2 and right button is mouse-3.");
10293 Vw32_swap_mouse_buttons = Qnil;
10295 DEFVAR_LISP ("w32-grab-focus-on-raise",
10296 &Vw32_grab_focus_on_raise,
10297 "Raised frame grabs input focus.\n\
10298 When t, `raise-frame' grabs input focus as well. This fits well\n\
10299 with the normal Windows click-to-focus policy, but might not be\n\
10300 desirable when using a point-to-focus policy.");
10301 Vw32_grab_focus_on_raise = Qt;
10303 DEFVAR_LISP ("w32-capslock-is-shiftlock",
10304 &Vw32_capslock_is_shiftlock,
10305 "Apply CapsLock state to non character input keys.\n\
10306 When nil, CapsLock only affects normal character input keys.");
10307 Vw32_capslock_is_shiftlock = Qnil;
10309 DEFVAR_LISP ("w32-recognize-altgr",
10310 &Vw32_recognize_altgr,
10311 "Recognize right-alt and left-ctrl as AltGr.\n\
10312 When nil, the right-alt and left-ctrl key combination is\n\
10313 interpreted normally.");
10314 Vw32_recognize_altgr = Qt;
10316 DEFVAR_BOOL ("w32-enable-unicode-output",
10317 &w32_enable_unicode_output,
10318 "Enable the use of Unicode for text output if non-nil.\n\
10319 Unicode output may prevent some third party applications for displaying\n\
10320 Far-East Languages on Windows 95/98 from working properly.\n\
10321 NT uses Unicode internally anyway, so this flag will probably have no\n\
10322 affect on NT machines.");
10323 w32_enable_unicode_output = 1;
10325 help_echo = Qnil;
10326 staticpro (&help_echo);
10327 help_echo_object = Qnil;
10328 staticpro (&help_echo_object);
10329 help_echo_window = Qnil;
10330 staticpro (&help_echo_window);
10331 previous_help_echo = Qnil;
10332 staticpro (&previous_help_echo);
10333 help_echo_pos = -1;
10335 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
10336 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
10337 For example, if a block cursor is over a tab, it will be drawn as\n\
10338 wide as that tab on the display.");
10339 x_stretch_cursor_p = 0;
10341 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
10342 "If not nil, Emacs uses toolkit scroll bars.");
10343 x_toolkit_scroll_bars_p = 1;
10345 staticpro (&last_mouse_motion_frame);
10346 last_mouse_motion_frame = Qnil;