]> code.delx.au - gnu-emacs/blob - src/w32term.c
Disable multicolor fonts on OS X since they are not supported on free systems
[gnu-emacs] / src / w32term.c
1 /* Implementation of GUI terminal on the Microsoft Windows API.
2
3 Copyright (C) 1989, 1993-2016 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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 3 of the License, or (at
10 your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include "lisp.h"
24 #include "blockinput.h"
25 #include "w32term.h"
26 #include "w32common.h" /* for OS version info */
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #ifdef CYGWIN
32 #include <fcntl.h> /* for O_RDWR */
33 #endif
34 #include <imm.h>
35
36 #include "coding.h"
37 #include "frame.h"
38 #include "fontset.h"
39 #include "termhooks.h"
40 #include "termopts.h"
41 #include "termchar.h"
42 #include "buffer.h"
43 #include "window.h"
44 #include "keyboard.h"
45 #include "menu.h" /* for w32_menu_show */
46
47 #ifdef WINDOWSNT
48 #include "w32.h" /* for filename_from_utf16, filename_from_ansi */
49 #endif
50
51 #ifndef WINDOWSNT
52 #include <io.h> /* for get_osfhandle */
53 #endif
54
55 #include <shellapi.h>
56
57 #include "font.h"
58 #include "w32font.h"
59
60 #if 0 /* TODO: stipple */
61 #include "bitmaps/gray.xbm"
62 #endif
63 \f
64 /* Fringe bitmaps. */
65
66 static int max_fringe_bmp = 0;
67 static HBITMAP *fringe_bmp = 0;
68
69 /* Temporary variables for w32_read_socket. */
70
71 static int last_mousemove_x = 0;
72 static int last_mousemove_y = 0;
73
74 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
75 #ifndef GET_WHEEL_DELTA_WPARAM
76 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
77 #endif
78
79 /* Non-zero means that a HELP_EVENT has been generated since Emacs
80 start. */
81
82 static int any_help_event_p;
83
84 extern unsigned int msh_mousewheel;
85
86 extern void free_frame_menubar (struct frame *);
87
88 extern int w32_codepage_for_font (char *fontname);
89 extern Cursor w32_load_cursor (LPCTSTR name);
90
91 #define x_any_window_to_frame x_window_to_frame
92 #define x_top_window_to_frame x_window_to_frame
93
94 \f
95 /* This is display since w32 does not support multiple ones. */
96 struct w32_display_info one_w32_display_info;
97 struct w32_display_info *x_display_list;
98
99 #if _WIN32_WINNT < 0x0500 && !defined(MINGW_W64)
100 /* Pre Windows 2000, this was not available, but define it here so
101 that Emacs compiled on such a platform will run on newer versions.
102 MinGW64 defines these unconditionally, so avoid redefining. */
103
104 typedef struct tagWCRANGE
105 {
106 WCHAR wcLow;
107 USHORT cGlyphs;
108 } WCRANGE;
109
110 typedef struct tagGLYPHSET
111 {
112 DWORD cbThis;
113 DWORD flAccel;
114 DWORD cGlyphsSupported;
115 DWORD cRanges;
116 WCRANGE ranges[1];
117 } GLYPHSET;
118
119 #endif /* compiling for pre-Win2k */
120
121 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
122 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
123
124 #ifndef LWA_ALPHA
125 #define LWA_ALPHA 0x02
126 #endif
127 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
128 later targets by MSVC headers. */
129 #ifndef WS_EX_LAYERED
130 #define WS_EX_LAYERED 0x80000
131 #endif
132
133 /* SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN are not defined on 95 and
134 NT4. */
135 #ifndef SM_CXVIRTUALSCREEN
136 #define SM_CXVIRTUALSCREEN 78
137 #endif
138 #ifndef SM_CYVIRTUALSCREEN
139 #define SM_CYVIRTUALSCREEN 79
140 #endif
141
142 /* The handle of the frame that currently owns the system caret. */
143 HWND w32_system_caret_hwnd;
144 int w32_system_caret_height;
145 int w32_system_caret_x;
146 int w32_system_caret_y;
147 struct window *w32_system_caret_window;
148 int w32_system_caret_hdr_height;
149 int w32_system_caret_mode_height;
150 DWORD dwWindowsThreadId = 0;
151 HANDLE hWindowsThread = NULL;
152 DWORD dwMainThreadId = 0;
153 HANDLE hMainThread = NULL;
154
155 int vertical_scroll_bar_min_handle;
156 int horizontal_scroll_bar_min_handle;
157 int vertical_scroll_bar_top_border;
158 int vertical_scroll_bar_bottom_border;
159 int horizontal_scroll_bar_left_border;
160 int horizontal_scroll_bar_right_border;
161
162 int last_scroll_bar_drag_pos;
163
164 /* Keyboard code page - may be changed by language-change events. */
165 int w32_keyboard_codepage;
166
167 /* Incremented by w32_read_socket whenever it really tries to read
168 events. */
169 static int volatile input_signal_count;
170
171 #ifdef CYGWIN
172 int w32_message_fd = -1;
173 #endif /* CYGWIN */
174
175 static void w32_handle_tool_bar_click (struct frame *,
176 struct input_event *);
177 static void w32_define_cursor (Window, Cursor);
178
179 void x_lower_frame (struct frame *);
180 void x_scroll_bar_clear (struct frame *);
181 void x_wm_set_size_hint (struct frame *, long, bool);
182 void x_raise_frame (struct frame *);
183 void x_set_window_size (struct frame *, bool, int, int, bool);
184 void x_wm_set_window_state (struct frame *, int);
185 void x_wm_set_icon_pixmap (struct frame *, int);
186 static void w32_initialize (void);
187 static void x_update_end (struct frame *);
188 static void w32_frame_up_to_date (struct frame *);
189 static void x_clear_frame (struct frame *);
190 static void frame_highlight (struct frame *);
191 static void frame_unhighlight (struct frame *);
192 static void x_new_focus_frame (struct w32_display_info *,
193 struct frame *);
194 static void x_focus_changed (int, int, struct w32_display_info *,
195 struct frame *, struct input_event *);
196 static void w32_detect_focus_change (struct w32_display_info *,
197 W32Msg *, struct input_event *);
198 static void w32_frame_rehighlight (struct frame *);
199 static void x_frame_rehighlight (struct w32_display_info *);
200 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
201 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
202 enum text_cursor_kinds);
203 static void w32_clip_to_row (struct window *, struct glyph_row *,
204 enum glyph_row_area, HDC);
205 static BOOL my_show_window (struct frame *, HWND, int);
206 static void my_set_window_pos (HWND, HWND, int, int, int, int, UINT);
207 #if 0
208 static void my_set_focus (struct frame *, HWND);
209 #endif
210 static void my_set_foreground_window (HWND);
211 static void my_destroy_window (struct frame *, HWND);
212 static void w32fullscreen_hook (struct frame *);
213
214 #ifdef GLYPH_DEBUG
215 static void x_check_font (struct frame *, struct font *);
216 #endif
217
218 \f
219 /***********************************************************************
220 Debugging
221 ***********************************************************************/
222
223 #if 0
224
225 /* This is a function useful for recording debugging information about
226 the sequence of occurrences in this file. */
227
228 struct record
229 {
230 char *locus;
231 int type;
232 };
233
234 struct record event_record[100];
235
236 int event_record_index;
237
238 record_event (char *locus, int type)
239 {
240 if (event_record_index == sizeof (event_record) / sizeof (struct record))
241 event_record_index = 0;
242
243 event_record[event_record_index].locus = locus;
244 event_record[event_record_index].type = type;
245 event_record_index++;
246 }
247
248 #endif /* 0 */
249 \f
250
251 void
252 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
253 XGCValues *xgcv)
254 {
255 if (mask & GCForeground)
256 gc->foreground = xgcv->foreground;
257 if (mask & GCBackground)
258 gc->background = xgcv->background;
259 if (mask & GCFont)
260 gc->font = xgcv->font;
261 }
262
263 XGCValues *
264 XCreateGC (void *ignore, Window window, unsigned long mask, XGCValues *xgcv)
265 {
266 XGCValues *gc = xzalloc (sizeof (XGCValues));
267
268 XChangeGC (ignore, gc, mask, xgcv);
269
270 return gc;
271 }
272
273 void
274 XGetGCValues (void *ignore, XGCValues *gc,
275 unsigned long mask, XGCValues *xgcv)
276 {
277 XChangeGC (ignore, xgcv, mask, gc);
278 }
279
280 static void
281 w32_set_clip_rectangle (HDC hdc, RECT *rect)
282 {
283 if (rect)
284 {
285 HRGN clip_region = CreateRectRgnIndirect (rect);
286 SelectClipRgn (hdc, clip_region);
287 DeleteObject (clip_region);
288 }
289 else
290 SelectClipRgn (hdc, NULL);
291 }
292
293 /* Restore clipping rectangle in S */
294 static void
295 w32_restore_glyph_string_clip (struct glyph_string *s)
296 {
297 RECT *r = s->clip;
298 int n = s->num_clips;
299
300 if (n == 1)
301 w32_set_clip_rectangle (s->hdc, r);
302 else if (n > 1)
303 {
304 HRGN clip1 = CreateRectRgnIndirect (r);
305 HRGN clip2 = CreateRectRgnIndirect (r + 1);
306 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
307 SelectClipRgn (s->hdc, clip1);
308 DeleteObject (clip1);
309 DeleteObject (clip2);
310 }
311 }
312
313 /*
314 Draw a wavy line under S. The wave fills wave_height pixels from y0.
315
316 x0 wave_length = 2
317 --
318 y0 * * * * *
319 |* * * * * * * * *
320 wave_height = 3 | * * * *
321
322 */
323
324 void
325 w32_draw_underwave (struct glyph_string *s, COLORREF color)
326 {
327 int wave_height = 3, wave_length = 2;
328 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
329 XRectangle wave_clip, string_clip, final_clip;
330 RECT w32_final_clip, w32_string_clip;
331 HPEN hp, oldhp;
332
333 dx = wave_length;
334 dy = wave_height - 1;
335 x0 = s->x;
336 y0 = s->ybase - wave_height + 3;
337 width = s->width;
338 xmax = x0 + width;
339
340 /* Find and set clipping rectangle */
341
342 wave_clip.x = x0;
343 wave_clip.y = y0;
344 wave_clip.width = width;
345 wave_clip.height = wave_height;
346
347 get_glyph_string_clip_rect (s, &w32_string_clip);
348 CONVERT_TO_XRECT (string_clip, w32_string_clip);
349
350 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
351 return;
352
353 hp = CreatePen (PS_SOLID, 0, color);
354 oldhp = SelectObject (s->hdc, hp);
355 CONVERT_FROM_XRECT (final_clip, w32_final_clip);
356 w32_set_clip_rectangle (s->hdc, &w32_final_clip);
357
358 /* Draw the waves */
359
360 x1 = x0 - (x0 % dx);
361 x2 = x1 + dx;
362 odd = (x1/dx) % 2;
363 y1 = y2 = y0;
364
365 if (odd)
366 y1 += dy;
367 else
368 y2 += dy;
369
370 MoveToEx (s->hdc, x1, y1, NULL);
371
372 while (x1 <= xmax)
373 {
374 LineTo (s->hdc, x2, y2);
375 x1 = x2, y1 = y2;
376 x2 += dx, y2 = y0 + odd*dy;
377 odd = !odd;
378 }
379
380 /* Restore previous pen and clipping rectangle(s) */
381 w32_restore_glyph_string_clip (s);
382 SelectObject (s->hdc, oldhp);
383 DeleteObject (hp);
384 }
385
386 /* Draw a hollow rectangle at the specified position. */
387 void
388 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
389 int width, int height)
390 {
391 HBRUSH hb, oldhb;
392 HPEN hp, oldhp;
393
394 hb = CreateSolidBrush (gc->background);
395 hp = CreatePen (PS_SOLID, 0, gc->foreground);
396 oldhb = SelectObject (hdc, hb);
397 oldhp = SelectObject (hdc, hp);
398
399 /* We enlarge WIDTH and HEIGHT by 1 to be bug-compatible to the
400 brain-dead design of XDrawRectangle, which draws a rectangle that
401 is 1 pixel wider and higher than its arguments WIDTH and HEIGHT.
402 This allows us to keep the code that calls this function similar
403 to the corresponding code in xterm.c. For the details, see
404 http://lists.gnu.org/archives/html/emacs-devel/2014-10/msg00546.html. */
405 Rectangle (hdc, x, y, x + width + 1, y + height + 1);
406
407 SelectObject (hdc, oldhb);
408 SelectObject (hdc, oldhp);
409 DeleteObject (hb);
410 DeleteObject (hp);
411 }
412
413 /* Draw a filled rectangle at the specified position. */
414 void
415 w32_fill_rect (struct frame *f, HDC hdc, COLORREF pix, RECT *lprect)
416 {
417 HBRUSH hb;
418
419 hb = CreateSolidBrush (pix);
420 FillRect (hdc, lprect, hb);
421 DeleteObject (hb);
422 }
423
424 void
425 w32_clear_window (struct frame *f)
426 {
427 RECT rect;
428 HDC hdc = get_frame_dc (f);
429
430 /* Under certain conditions, this can be called at startup with
431 a console frame pointer before the GUI frame is created. An HDC
432 of 0 indicates this. */
433 if (hdc)
434 {
435 GetClientRect (FRAME_W32_WINDOW (f), &rect);
436 w32_clear_rect (f, hdc, &rect);
437 }
438
439 release_frame_dc (f, hdc);
440 }
441
442 #define OPAQUE_FRAME 255
443
444 void
445 x_set_frame_alpha (struct frame *f)
446 {
447 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
448 double alpha = 1.0;
449 double alpha_min = 1.0;
450 BYTE opac;
451 LONG ex_style;
452 HWND window = FRAME_W32_WINDOW (f);
453
454 /* Older versions of Windows do not support transparency. */
455 if (!pfnSetLayeredWindowAttributes)
456 return;
457
458 if (dpyinfo->w32_focus_frame == f)
459 alpha = f->alpha[0];
460 else
461 alpha = f->alpha[1];
462
463 if (FLOATP (Vframe_alpha_lower_limit))
464 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
465 else if (INTEGERP (Vframe_alpha_lower_limit))
466 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
467
468 if (alpha < 0.0)
469 return;
470 else if (alpha > 1.0)
471 alpha = 1.0;
472 else if (alpha < alpha_min && alpha_min <= 1.0)
473 alpha = alpha_min;
474
475 opac = alpha * OPAQUE_FRAME;
476
477 ex_style = GetWindowLong (window, GWL_EXSTYLE);
478
479 if (opac == OPAQUE_FRAME)
480 ex_style &= ~WS_EX_LAYERED;
481 else
482 ex_style |= WS_EX_LAYERED;
483
484 SetWindowLong (window, GWL_EXSTYLE, ex_style);
485
486 if (opac != OPAQUE_FRAME)
487 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
488 }
489
490 int
491 x_display_pixel_height (struct w32_display_info *dpyinfo)
492 {
493 int pixels = GetSystemMetrics (SM_CYVIRTUALSCREEN);
494
495 if (pixels == 0)
496 /* Fallback for Windows 95 or NT 4.0. */
497 pixels = GetSystemMetrics (SM_CYSCREEN);
498
499 return pixels;
500 }
501
502 int
503 x_display_pixel_width (struct w32_display_info *dpyinfo)
504 {
505 int pixels = GetSystemMetrics (SM_CXVIRTUALSCREEN);
506
507 if (pixels == 0)
508 /* Fallback for Windows 95 or NT 4.0. */
509 pixels = GetSystemMetrics (SM_CXSCREEN);
510
511 return pixels;
512 }
513
514 \f
515 /***********************************************************************
516 Starting and ending an update
517 ***********************************************************************/
518
519 /* Start an update of frame F. This function is installed as a hook
520 for update_begin, i.e. it is called when update_begin is called.
521 This function is called prior to calls to x_update_window_begin for
522 each window being updated. */
523
524 static void
525 x_update_begin (struct frame *f)
526 {
527 struct w32_display_info *display_info = FRAME_DISPLAY_INFO (f);
528
529 if (! FRAME_W32_P (f))
530 return;
531
532 /* Regenerate display palette before drawing if list of requested
533 colors has changed. */
534 if (display_info->regen_palette)
535 {
536 w32_regenerate_palette (f);
537 display_info->regen_palette = FALSE;
538 }
539 }
540
541
542 /* Start update of window W. */
543
544 static void
545 x_update_window_begin (struct window *w)
546 {
547 struct frame *f = XFRAME (WINDOW_FRAME (w));
548 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
549
550 /* Hide the system caret during an update. */
551 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
552 {
553 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
554 }
555
556 w->output_cursor = w->cursor;
557
558 block_input ();
559
560 if (f == hlinfo->mouse_face_mouse_frame)
561 {
562 /* Don't do highlighting for mouse motion during the update. */
563 hlinfo->mouse_face_defer = true;
564
565 /* If F needs to be redrawn, simply forget about any prior mouse
566 highlighting. */
567 if (FRAME_GARBAGED_P (f))
568 hlinfo->mouse_face_window = Qnil;
569
570 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
571 their mouse_face_p flag set, which means that they are always
572 unequal to rows in a desired matrix which never have that
573 flag set. So, rows containing mouse-face glyphs are never
574 scrolled, and we don't have to switch the mouse highlight off
575 here to prevent it from being scrolled. */
576
577 /* Can we tell that this update does not affect the window
578 where the mouse highlight is? If so, no need to turn off.
579 Likewise, don't do anything if the frame is garbaged;
580 in that case, the frame's current matrix that we would use
581 is all wrong, and we will redisplay that line anyway. */
582 if (!NILP (hlinfo->mouse_face_window)
583 && w == XWINDOW (hlinfo->mouse_face_window))
584 {
585 int i;
586
587 for (i = 0; i < w->desired_matrix->nrows; ++i)
588 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
589 break;
590
591 if (i < w->desired_matrix->nrows)
592 clear_mouse_face (hlinfo);
593 }
594 #endif /* 0 */
595 }
596
597 unblock_input ();
598 }
599
600 /* Draw a vertical window border from (x,y0) to (x,y1) */
601
602 static void
603 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
604 {
605 struct frame *f = XFRAME (WINDOW_FRAME (w));
606 RECT r;
607 HDC hdc;
608 struct face *face;
609
610 r.left = x;
611 r.right = x + 1;
612 r.top = y0;
613 r.bottom = y1;
614
615 hdc = get_frame_dc (f);
616 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
617 if (face)
618 w32_fill_rect (f, hdc, face->foreground, &r);
619 else
620 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
621
622 release_frame_dc (f, hdc);
623 }
624
625
626 /* Draw a window divider from (x0, y0) to (x1, y1) */
627
628 static void
629 w32_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
630 {
631 struct frame *f = XFRAME (WINDOW_FRAME (w));
632 HDC hdc = get_frame_dc (f);
633 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
634 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
635 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
636 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
637 unsigned long color_first = (face_first
638 ? face_first->foreground
639 : FRAME_FOREGROUND_PIXEL (f));
640 unsigned long color_last = (face_last
641 ? face_last->foreground
642 : FRAME_FOREGROUND_PIXEL (f));
643
644 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
645 /* Vertical. */
646 {
647 w32_fill_area_abs (f, hdc, color_first, x0, y0, x0 + 1, y1);
648 w32_fill_area_abs (f, hdc, color, x0 + 1, y0, x1 - 1, y1);
649 w32_fill_area_abs (f, hdc, color_last, x1 - 1, y0, x1, y1);
650 }
651 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
652 /* Horizontal. */
653 {
654 w32_fill_area_abs (f, hdc, color_first, x0, y0, x1, y0 + 1);
655 w32_fill_area_abs (f, hdc, color, x0, y0 + 1, x1, y1 - 1);
656 w32_fill_area_abs (f, hdc, color_last, x0, y1 - 1, x1, y1);
657 }
658 else
659 w32_fill_area_abs (f, hdc, color, x0, y0, x1, y1);
660
661 release_frame_dc (f, hdc);
662 }
663
664 /* End update of window W.
665
666 Draw vertical borders between horizontally adjacent windows, and
667 display W's cursor if CURSOR_ON_P is non-zero.
668
669 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
670 glyphs in mouse-face were overwritten. In that case we have to
671 make sure that the mouse-highlight is properly redrawn.
672
673 W may be a menu bar pseudo-window in case we don't have X toolkit
674 support. Such windows don't have a cursor, so don't display it
675 here. */
676
677 static void
678 x_update_window_end (struct window *w, bool cursor_on_p,
679 bool mouse_face_overwritten_p)
680 {
681 if (!w->pseudo_window_p)
682 {
683 block_input ();
684
685 if (cursor_on_p)
686 display_and_set_cursor (w, true,
687 w->output_cursor.hpos, w->output_cursor.vpos,
688 w->output_cursor.x, w->output_cursor.y);
689
690 if (draw_window_fringes (w, true))
691 {
692 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
693 x_draw_right_divider (w);
694 else
695 x_draw_vertical_border (w);
696 }
697
698 unblock_input ();
699 }
700
701 /* If a row with mouse-face was overwritten, arrange for
702 XTframe_up_to_date to redisplay the mouse highlight. */
703 if (mouse_face_overwritten_p)
704 {
705 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
706
707 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
708 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
709 hlinfo->mouse_face_window = Qnil;
710 }
711
712 /* Unhide the caret. This won't actually show the cursor, unless it
713 was visible before the corresponding call to HideCaret in
714 x_update_window_begin. */
715 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
716 {
717 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
718 }
719 }
720
721
722 /* End update of frame F. This function is installed as a hook in
723 update_end. */
724
725 static void
726 x_update_end (struct frame *f)
727 {
728 if (! FRAME_W32_P (f))
729 return;
730
731 /* Mouse highlight may be displayed again. */
732 MOUSE_HL_INFO (f)->mouse_face_defer = false;
733 }
734
735
736 /* This function is called from various places in xdisp.c
737 whenever a complete update has been performed. */
738
739 static void
740 w32_frame_up_to_date (struct frame *f)
741 {
742 if (FRAME_W32_P (f))
743 FRAME_MOUSE_UPDATE (f);
744 }
745
746
747 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
748 arrow bitmaps, or clear the fringes if no bitmaps are required
749 before DESIRED_ROW is made current. This function is called from
750 update_window_line only if it is known that there are differences
751 between bitmaps to be drawn between current row and DESIRED_ROW. */
752
753 static void
754 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
755 {
756 struct frame *f;
757 int width, height;
758
759 eassert (w);
760
761 if (!desired_row->mode_line_p && !w->pseudo_window_p)
762 desired_row->redraw_fringe_bitmaps_p = true;
763
764 /* When a window has disappeared, make sure that no rest of
765 full-width rows stays visible in the internal border. Could
766 check here if updated window is the leftmost/rightmost window,
767 but I guess it's not worth doing since vertically split windows
768 are almost never used, internal border is rarely set, and the
769 overhead is very small. */
770 if (windows_or_buffers_changed
771 && desired_row->full_width_p
772 && (f = XFRAME (w->frame),
773 width = FRAME_INTERNAL_BORDER_WIDTH (f),
774 width != 0)
775 && (height = desired_row->visible_height,
776 height > 0))
777 {
778 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
779
780 block_input ();
781 {
782 HDC hdc = get_frame_dc (f);
783 w32_clear_area (f, hdc, 0, y, width, height);
784 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
785 y, width, height);
786 release_frame_dc (f, hdc);
787 }
788 unblock_input ();
789 }
790 }
791
792
793 /* Draw the bitmap WHICH in one of the left or right fringes of
794 window W. ROW is the glyph row for which to display the bitmap; it
795 determines the vertical position at which the bitmap has to be
796 drawn. */
797
798 static void
799 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
800 struct draw_fringe_bitmap_params *p)
801 {
802 struct frame *f = XFRAME (WINDOW_FRAME (w));
803 HDC hdc;
804 struct face *face = p->face;
805
806 hdc = get_frame_dc (f);
807
808 /* Must clip because of partially visible lines. */
809 w32_clip_to_row (w, row, ANY_AREA, hdc);
810
811 if (p->bx >= 0 && !p->overlay_p)
812 w32_fill_area (f, hdc, face->background,
813 p->bx, p->by, p->nx, p->ny);
814
815 if (p->which && p->which < max_fringe_bmp)
816 {
817 HBITMAP pixmap = fringe_bmp[p->which];
818 HDC compat_hdc;
819 HANDLE horig_obj;
820
821 compat_hdc = CreateCompatibleDC (hdc);
822
823 SaveDC (hdc);
824
825 horig_obj = SelectObject (compat_hdc, pixmap);
826
827 /* Paint overlays transparently. */
828 if (p->overlay_p)
829 {
830 HBRUSH h_brush, h_orig_brush;
831
832 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
833 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
834 h_brush = CreateSolidBrush (face->foreground);
835 h_orig_brush = SelectObject (hdc, h_brush);
836
837 BitBlt (hdc, p->x, p->y, p->wd, p->h,
838 compat_hdc, 0, p->dh,
839 DSTINVERT);
840 BitBlt (hdc, p->x, p->y, p->wd, p->h,
841 compat_hdc, 0, p->dh,
842 0x2E064A);
843 BitBlt (hdc, p->x, p->y, p->wd, p->h,
844 compat_hdc, 0, p->dh,
845 DSTINVERT);
846
847 SelectObject (hdc, h_orig_brush);
848 DeleteObject (h_brush);
849 }
850 else
851 {
852 SetTextColor (hdc, face->background);
853 SetBkColor (hdc, (p->cursor_p
854 ? f->output_data.w32->cursor_pixel
855 : face->foreground));
856
857 BitBlt (hdc, p->x, p->y, p->wd, p->h,
858 compat_hdc, 0, p->dh,
859 SRCCOPY);
860 }
861
862 SelectObject (compat_hdc, horig_obj);
863 DeleteDC (compat_hdc);
864 RestoreDC (hdc, -1);
865 }
866
867 w32_set_clip_rectangle (hdc, NULL);
868
869 release_frame_dc (f, hdc);
870 }
871
872 static void
873 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
874 {
875 if (which >= max_fringe_bmp)
876 {
877 int i = max_fringe_bmp;
878 max_fringe_bmp = which + 20;
879 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
880 while (i < max_fringe_bmp)
881 fringe_bmp[i++] = 0;
882 }
883
884 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
885 }
886
887 static void
888 w32_destroy_fringe_bitmap (int which)
889 {
890 if (which >= max_fringe_bmp)
891 return;
892
893 if (fringe_bmp[which])
894 DeleteObject (fringe_bmp[which]);
895 fringe_bmp[which] = 0;
896 }
897
898 /***********************************************************************
899 Display Iterator
900 ***********************************************************************/
901
902 /* Function prototypes of this page. */
903
904 static void x_set_glyph_string_clipping (struct glyph_string *);
905 static void x_set_glyph_string_gc (struct glyph_string *);
906 static void x_draw_glyph_string_background (struct glyph_string *,
907 bool);
908 static void x_draw_glyph_string_foreground (struct glyph_string *);
909 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
910 static void x_draw_glyph_string_box (struct glyph_string *);
911 static void x_draw_glyph_string (struct glyph_string *);
912 static void x_set_cursor_gc (struct glyph_string *);
913 static void x_set_mode_line_face_gc (struct glyph_string *);
914 static void x_set_mouse_face_gc (struct glyph_string *);
915 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
916 static void w32_setup_relief_color (struct frame *, struct relief *,
917 double, int, COLORREF);
918 static void x_setup_relief_colors (struct glyph_string *);
919 static void x_draw_image_glyph_string (struct glyph_string *);
920 static void x_draw_image_relief (struct glyph_string *);
921 static void x_draw_image_foreground (struct glyph_string *);
922 static void w32_draw_image_foreground_1 (struct glyph_string *, HBITMAP);
923 static void x_clear_glyph_string_rect (struct glyph_string *, int,
924 int, int, int);
925 static void w32_draw_relief_rect (struct frame *, int, int, int, int,
926 int, int, int, int, int, int,
927 RECT *);
928 static void w32_draw_box_rect (struct glyph_string *, int, int, int, int,
929 int, bool, bool, RECT *);
930
931
932 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
933 face. */
934
935 static void
936 x_set_cursor_gc (struct glyph_string *s)
937 {
938 if (s->font == FRAME_FONT (s->f)
939 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
940 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
941 && !s->cmp)
942 s->gc = s->f->output_data.w32->cursor_gc;
943 else
944 {
945 /* Cursor on non-default face: must merge. */
946 XGCValues xgcv;
947 unsigned long mask;
948
949 xgcv.background = s->f->output_data.w32->cursor_pixel;
950 xgcv.foreground = s->face->background;
951
952 /* If the glyph would be invisible, try a different foreground. */
953 if (xgcv.foreground == xgcv.background)
954 xgcv.foreground = s->face->foreground;
955 if (xgcv.foreground == xgcv.background)
956 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
957 if (xgcv.foreground == xgcv.background)
958 xgcv.foreground = s->face->foreground;
959
960 /* Make sure the cursor is distinct from text in this face. */
961 if (xgcv.background == s->face->background
962 && xgcv.foreground == s->face->foreground)
963 {
964 xgcv.background = s->face->foreground;
965 xgcv.foreground = s->face->background;
966 }
967
968 IF_DEBUG (x_check_font (s->f, s->font));
969 xgcv.font = s->font;
970 mask = GCForeground | GCBackground | GCFont;
971
972 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
973 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
974 mask, &xgcv);
975 else
976 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
977 = XCreateGC (NULL, s->window, mask, &xgcv);
978
979 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
980 }
981 }
982
983
984 /* Set up S->gc of glyph string S for drawing text in mouse face. */
985
986 static void
987 x_set_mouse_face_gc (struct glyph_string *s)
988 {
989 int face_id;
990 struct face *face;
991
992 /* What face has to be used last for the mouse face? */
993 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
994 face = FACE_FROM_ID (s->f, face_id);
995 if (face == NULL)
996 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
997
998 if (s->first_glyph->type == CHAR_GLYPH)
999 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1000 else
1001 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1002 s->face = FACE_FROM_ID (s->f, face_id);
1003 prepare_face_for_display (s->f, s->face);
1004
1005 /* If font in this face is same as S->font, use it. */
1006 if (s->font == s->face->font)
1007 s->gc = s->face->gc;
1008 else
1009 {
1010 /* Otherwise construct scratch_cursor_gc with values from FACE
1011 but font FONT. */
1012 XGCValues xgcv;
1013 unsigned long mask;
1014
1015 xgcv.background = s->face->background;
1016 xgcv.foreground = s->face->foreground;
1017 IF_DEBUG (x_check_font (s->f, s->font));
1018 xgcv.font = s->font;
1019 mask = GCForeground | GCBackground | GCFont;
1020
1021 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1022 XChangeGC (NULL, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1023 mask, &xgcv);
1024 else
1025 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1026 = XCreateGC (NULL, s->window, mask, &xgcv);
1027
1028 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1029 }
1030
1031 eassert (s->gc != 0);
1032 }
1033
1034
1035 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1036 Faces to use in the mode line have already been computed when the
1037 matrix was built, so there isn't much to do, here. */
1038
1039 static inline void
1040 x_set_mode_line_face_gc (struct glyph_string *s)
1041 {
1042 s->gc = s->face->gc;
1043 }
1044
1045
1046 /* Set S->gc of glyph string S for drawing that glyph string. Set
1047 S->stippled_p to a non-zero value if the face of S has a stipple
1048 pattern. */
1049
1050 static inline void
1051 x_set_glyph_string_gc (struct glyph_string *s)
1052 {
1053 prepare_face_for_display (s->f, s->face);
1054
1055 if (s->hl == DRAW_NORMAL_TEXT)
1056 {
1057 s->gc = s->face->gc;
1058 s->stippled_p = s->face->stipple != 0;
1059 }
1060 else if (s->hl == DRAW_INVERSE_VIDEO)
1061 {
1062 x_set_mode_line_face_gc (s);
1063 s->stippled_p = s->face->stipple != 0;
1064 }
1065 else if (s->hl == DRAW_CURSOR)
1066 {
1067 x_set_cursor_gc (s);
1068 s->stippled_p = false;
1069 }
1070 else if (s->hl == DRAW_MOUSE_FACE)
1071 {
1072 x_set_mouse_face_gc (s);
1073 s->stippled_p = s->face->stipple != 0;
1074 }
1075 else if (s->hl == DRAW_IMAGE_RAISED
1076 || s->hl == DRAW_IMAGE_SUNKEN)
1077 {
1078 s->gc = s->face->gc;
1079 s->stippled_p = s->face->stipple != 0;
1080 }
1081 else
1082 emacs_abort ();
1083
1084 /* GC must have been set. */
1085 eassert (s->gc != 0);
1086 }
1087
1088
1089 /* Set clipping for output of glyph string S. S may be part of a mode
1090 line or menu if we don't have X toolkit support. */
1091
1092 static inline void
1093 x_set_glyph_string_clipping (struct glyph_string *s)
1094 {
1095 RECT *r = s->clip;
1096 int n = get_glyph_string_clip_rects (s, r, 2);
1097
1098 if (n == 1)
1099 w32_set_clip_rectangle (s->hdc, r);
1100 else if (n > 1)
1101 {
1102 HRGN clip1 = CreateRectRgnIndirect (r);
1103 HRGN clip2 = CreateRectRgnIndirect (r + 1);
1104 if (CombineRgn (clip1, clip1, clip2, RGN_OR) != ERROR)
1105 SelectClipRgn (s->hdc, clip1);
1106 DeleteObject (clip1);
1107 DeleteObject (clip2);
1108 }
1109 s->num_clips = n;
1110 }
1111
1112 /* Set SRC's clipping for output of glyph string DST. This is called
1113 when we are drawing DST's left_overhang or right_overhang only in
1114 the area of SRC. */
1115
1116 static void
1117 x_set_glyph_string_clipping_exactly (struct glyph_string *src,
1118 struct glyph_string *dst)
1119 {
1120 RECT r;
1121
1122 r.left = src->x;
1123 r.right = r.left + src->width;
1124 r.top = src->y;
1125 r.bottom = r.top + src->height;
1126 dst->clip[0] = r;
1127 dst->num_clips = 1;
1128 w32_set_clip_rectangle (dst->hdc, &r);
1129 }
1130
1131 /* RIF:
1132 Compute left and right overhang of glyph string S. */
1133
1134 static void
1135 w32_compute_glyph_string_overhangs (struct glyph_string *s)
1136 {
1137 if (s->cmp == NULL
1138 && s->first_glyph->type == CHAR_GLYPH
1139 && !s->font_not_found_p)
1140 {
1141 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1142 struct font *font = s->font;
1143 struct font_metrics metrics;
1144 int i;
1145
1146 for (i = 0; i < s->nchars; i++)
1147 code[i] = s->char2b[i];
1148 font->driver->text_extents (font, code, s->nchars, &metrics);
1149 s->right_overhang = (metrics.rbearing > metrics.width
1150 ? metrics.rbearing - metrics.width : 0);
1151 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1152 }
1153 else if (s->cmp)
1154 {
1155 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1156 s->left_overhang = -s->cmp->lbearing;
1157 }
1158 }
1159
1160 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1161
1162 static inline void
1163 x_clear_glyph_string_rect (struct glyph_string *s,
1164 int x, int y, int w, int h)
1165 {
1166 int real_x = x;
1167 int real_y = y;
1168 int real_w = w;
1169 int real_h = h;
1170 #if 0
1171 /* Take clipping into account. */
1172 if (s->gc->clip_mask == Rect)
1173 {
1174 real_x = max (real_x, s->gc->clip_rectangle.left);
1175 real_y = max (real_y, s->gc->clip_rectangle.top);
1176 real_w = min (real_w, s->gc->clip_rectangle.right
1177 - s->gc->clip_rectangle.left);
1178 real_h = min (real_h, s->gc->clip_rectangle.bottom
1179 - s->gc->clip_rectangle.top);
1180 }
1181 #endif
1182 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1183 real_w, real_h);
1184 }
1185
1186
1187 /* Draw the background of glyph_string S. If S->background_filled_p
1188 is non-zero don't draw it. FORCE_P non-zero means draw the
1189 background even if it wouldn't be drawn normally. This is used
1190 when a string preceding S draws into the background of S, or S
1191 contains the first component of a composition. */
1192
1193 static void
1194 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1195 {
1196 /* Nothing to do if background has already been drawn or if it
1197 shouldn't be drawn in the first place. */
1198 if (!s->background_filled_p)
1199 {
1200 int box_line_width = max (s->face->box_line_width, 0);
1201
1202 #if 0 /* TODO: stipple */
1203 if (s->stippled_p)
1204 {
1205 /* Fill background with a stipple pattern. */
1206 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1207 XFillRectangle (s->display, s->window, s->gc, s->x,
1208 s->y + box_line_width,
1209 s->background_width,
1210 s->height - 2 * box_line_width);
1211 XSetFillStyle (s->display, s->gc, FillSolid);
1212 s->background_filled_p = true;
1213 }
1214 else
1215 #endif
1216 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1217 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1218 font dimensions, since the actual glyphs might be
1219 much smaller. So in that case we always clear the
1220 rectangle with background color. */
1221 || FONT_TOO_HIGH (s->font)
1222 || s->font_not_found_p
1223 || s->extends_to_end_of_line_p
1224 || force_p)
1225 {
1226 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1227 s->background_width,
1228 s->height - 2 * box_line_width);
1229 s->background_filled_p = true;
1230 }
1231 }
1232 }
1233
1234
1235 /* Draw the foreground of glyph string S. */
1236
1237 static void
1238 x_draw_glyph_string_foreground (struct glyph_string *s)
1239 {
1240 int i, x;
1241
1242 /* If first glyph of S has a left box line, start drawing the text
1243 of S to the right of that box line. */
1244 if (s->face->box != FACE_NO_BOX
1245 && s->first_glyph->left_box_line_p)
1246 x = s->x + eabs (s->face->box_line_width);
1247 else
1248 x = s->x;
1249
1250 SetTextColor (s->hdc, s->gc->foreground);
1251 SetBkColor (s->hdc, s->gc->background);
1252 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1253
1254 /* Draw characters of S as rectangles if S's font could not be
1255 loaded. */
1256 if (s->font_not_found_p)
1257 {
1258 for (i = 0; i < s->nchars; ++i)
1259 {
1260 struct glyph *g = s->first_glyph + i;
1261
1262 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1263 s->height - 1);
1264 x += g->pixel_width;
1265 }
1266 }
1267 else
1268 {
1269 struct font *font = s->font;
1270 int boff = font->baseline_offset;
1271 int y;
1272 HFONT old_font;
1273
1274 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1275
1276 if (font->vertical_centering)
1277 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1278
1279 y = s->ybase - boff;
1280 if (s->for_overlaps
1281 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1282 font->driver->draw (s, 0, s->nchars, x, y, false);
1283 else
1284 font->driver->draw (s, 0, s->nchars, x, y, true);
1285 if (s->face->overstrike)
1286 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1287
1288 SelectObject (s->hdc, old_font);
1289 }
1290 }
1291
1292 /* Draw the foreground of composite glyph string S. */
1293
1294 static void
1295 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1296 {
1297 int i, j, x;
1298 struct font *font = s->font;
1299
1300 /* If first glyph of S has a left box line, start drawing the text
1301 of S to the right of that box line. */
1302 if (s->face && s->face->box != FACE_NO_BOX
1303 && s->first_glyph->left_box_line_p)
1304 x = s->x + eabs (s->face->box_line_width);
1305 else
1306 x = s->x;
1307
1308 /* S is a glyph string for a composition. S->cmp_from is the index
1309 of the first character drawn for glyphs of this composition.
1310 S->cmp_from == 0 means we are drawing the very first character of
1311 this composition. */
1312
1313 SetTextColor (s->hdc, s->gc->foreground);
1314 SetBkColor (s->hdc, s->gc->background);
1315 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1316
1317 /* Draw a rectangle for the composition if the font for the very
1318 first character of the composition could not be loaded. */
1319 if (s->font_not_found_p)
1320 {
1321 if (s->cmp_from == 0)
1322 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1323 s->height - 1);
1324 }
1325 else if (! s->first_glyph->u.cmp.automatic)
1326 {
1327 int y = s->ybase;
1328 HFONT old_font;
1329
1330 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1331
1332 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1333 /* TAB in a composition means display glyphs with padding
1334 space on the left or right. */
1335 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1336 {
1337 int xx = x + s->cmp->offsets[j * 2];
1338 int yy = y - s->cmp->offsets[j * 2 + 1];
1339
1340 font->driver->draw (s, j, j + 1, xx, yy, false);
1341 if (s->face->overstrike)
1342 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1343 }
1344 SelectObject (s->hdc, old_font);
1345 }
1346 else
1347 {
1348 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1349 Lisp_Object glyph;
1350 int y = s->ybase;
1351 int width = 0;
1352 HFONT old_font;
1353
1354 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1355
1356 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1357 {
1358 glyph = LGSTRING_GLYPH (gstring, i);
1359 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1360 width += LGLYPH_WIDTH (glyph);
1361 else
1362 {
1363 int xoff, yoff, wadjust;
1364
1365 if (j < i)
1366 {
1367 font->driver->draw (s, j, i, x, y, false);
1368 x += width;
1369 }
1370 xoff = LGLYPH_XOFF (glyph);
1371 yoff = LGLYPH_YOFF (glyph);
1372 wadjust = LGLYPH_WADJUST (glyph);
1373 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1374 x += wadjust;
1375 j = i + 1;
1376 width = 0;
1377 }
1378 }
1379 if (j < i)
1380 font->driver->draw (s, j, i, x, y, false);
1381
1382 SelectObject (s->hdc, old_font);
1383 }
1384 }
1385
1386
1387 /* Draw the foreground of glyph string S for glyphless characters. */
1388
1389 static void
1390 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1391 {
1392 struct glyph *glyph = s->first_glyph;
1393 XChar2b char2b[8];
1394 int x, i, j;
1395 bool with_background;
1396
1397 /* If first glyph of S has a left box line, start drawing the text
1398 of S to the right of that box line. */
1399 if (s->face->box != FACE_NO_BOX
1400 && s->first_glyph->left_box_line_p)
1401 x = s->x + eabs (s->face->box_line_width);
1402 else
1403 x = s->x;
1404
1405 SetTextColor (s->hdc, s->gc->foreground);
1406 SetBkColor (s->hdc, s->gc->background);
1407 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1408
1409 s->char2b = char2b;
1410 with_background = ((s->for_overlaps
1411 || (s->background_filled_p && s->hl != DRAW_CURSOR))) == 0;
1412 for (i = 0; i < s->nchars; i++, glyph++)
1413 {
1414 char buf[7], *str = NULL;
1415 int len = glyph->u.glyphless.len;
1416
1417 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1418 {
1419 if (len > 0
1420 && CHAR_TABLE_P (Vglyphless_char_display)
1421 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1422 >= 1))
1423 {
1424 Lisp_Object acronym
1425 = (! glyph->u.glyphless.for_no_font
1426 ? CHAR_TABLE_REF (Vglyphless_char_display,
1427 glyph->u.glyphless.ch)
1428 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1429 if (STRINGP (acronym))
1430 str = SSDATA (acronym);
1431 }
1432 }
1433 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1434 {
1435 sprintf ((char *) buf, "%0*X",
1436 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1437 glyph->u.glyphless.ch);
1438 str = buf;
1439 }
1440
1441 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1442 w32_draw_rectangle (s->hdc, s->gc,
1443 x, s->ybase - glyph->ascent,
1444 glyph->pixel_width - 1,
1445 glyph->ascent + glyph->descent - 1);
1446 if (str)
1447 {
1448 struct font *font = s->font;
1449 int upper_len = (len + 1) / 2;
1450 unsigned code;
1451 HFONT old_font;
1452
1453 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1454 /* It is certain that all LEN characters in STR are ASCII. */
1455 for (j = 0; j < len; j++)
1456 {
1457 code = font->driver->encode_char (font, str[j]);
1458 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1459 }
1460 font->driver->draw (s, 0, upper_len,
1461 x + glyph->slice.glyphless.upper_xoff,
1462 s->ybase + glyph->slice.glyphless.upper_yoff,
1463 with_background);
1464 font->driver->draw (s, upper_len, len,
1465 x + glyph->slice.glyphless.lower_xoff,
1466 s->ybase + glyph->slice.glyphless.lower_yoff,
1467 with_background);
1468 SelectObject (s->hdc, old_font);
1469 }
1470 x += glyph->pixel_width;
1471 }
1472 }
1473
1474
1475 /* Brightness beyond which a color won't have its highlight brightness
1476 boosted.
1477
1478 Nominally, highlight colors for `3d' faces are calculated by
1479 brightening an object's color by a constant scale factor, but this
1480 doesn't yield good results for dark colors, so for colors whose
1481 brightness is less than this value (on a scale of 0-255) have to
1482 use an additional additive factor.
1483
1484 The value here is set so that the default menu-bar/mode-line color
1485 (grey75) will not have its highlights changed at all. */
1486 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1487
1488
1489 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1490 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1491 If this produces the same color as COLOR, try a color where all RGB
1492 values have DELTA added. Return the allocated color in *COLOR.
1493 DISPLAY is the X display, CMAP is the colormap to operate on.
1494 Value is non-zero if successful. */
1495
1496 static int
1497 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1498 double factor, int delta)
1499 {
1500 COLORREF new;
1501 long bright;
1502
1503 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1504 delta /= 256;
1505
1506 /* Change RGB values by specified FACTOR. Avoid overflow! */
1507 eassert (factor >= 0);
1508 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1509 min (0xff, factor * GetGValue (*color)),
1510 min (0xff, factor * GetBValue (*color)));
1511
1512 /* Calculate brightness of COLOR. */
1513 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1514 + GetBValue (*color)) / 6;
1515
1516 /* We only boost colors that are darker than
1517 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1518 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1519 /* Make an additive adjustment to NEW, because it's dark enough so
1520 that scaling by FACTOR alone isn't enough. */
1521 {
1522 /* How far below the limit this color is (0 - 1, 1 being darker). */
1523 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1524 /* The additive adjustment. */
1525 int min_delta = delta * dimness * factor / 2;
1526
1527 if (factor < 1)
1528 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1529 max (0, min (0xff, min_delta - GetGValue (*color))),
1530 max (0, min (0xff, min_delta - GetBValue (*color))));
1531 else
1532 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1533 max (0, min (0xff, min_delta + GetGValue (*color))),
1534 max (0, min (0xff, min_delta + GetBValue (*color))));
1535 }
1536
1537 if (new == *color)
1538 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1539 max (0, min (0xff, delta + GetGValue (*color))),
1540 max (0, min (0xff, delta + GetBValue (*color))));
1541
1542 /* TODO: Map to palette and retry with delta if same? */
1543 /* TODO: Free colors (if using palette)? */
1544
1545 if (new == *color)
1546 return 0;
1547
1548 *color = new;
1549
1550 return 1;
1551 }
1552
1553 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1554 colors in COLORS. On W32, we no longer try to map colors to
1555 a palette. */
1556 void
1557 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1558 {
1559 int i;
1560
1561 for (i = 0; i < ncolors; i++)
1562 {
1563 DWORD pixel = colors[i].pixel;
1564 /* Convert to a 16 bit value in range 0 - 0xffff. */
1565 colors[i].red = GetRValue (pixel) * 257;
1566 colors[i].green = GetGValue (pixel) * 257;
1567 colors[i].blue = GetBValue (pixel) * 257;
1568 }
1569 }
1570
1571 void
1572 x_query_color (struct frame *f, XColor *color)
1573 {
1574 x_query_colors (f, color, 1);
1575 }
1576
1577
1578 /* Set up the foreground color for drawing relief lines of glyph
1579 string S. RELIEF is a pointer to a struct relief containing the GC
1580 with which lines will be drawn. Use a color that is FACTOR or
1581 DELTA lighter or darker than the relief's background which is found
1582 in S->f->output_data.x->relief_background. If such a color cannot
1583 be allocated, use DEFAULT_PIXEL, instead. */
1584
1585 static void
1586 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1587 int delta, COLORREF default_pixel)
1588 {
1589 XGCValues xgcv;
1590 struct w32_output *di = f->output_data.w32;
1591 unsigned long mask = GCForeground;
1592 COLORREF pixel;
1593 COLORREF background = di->relief_background;
1594 #if 0
1595 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1596 #endif
1597
1598 /* TODO: Free colors (if using palette)? */
1599
1600 /* Allocate new color. */
1601 xgcv.foreground = default_pixel;
1602 pixel = background;
1603 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1604 xgcv.foreground = relief->pixel = pixel;
1605
1606 if (relief->gc == 0)
1607 {
1608 #if 0 /* TODO: stipple */
1609 xgcv.stipple = dpyinfo->gray;
1610 mask |= GCStipple;
1611 #endif
1612 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1613 }
1614 else
1615 XChangeGC (NULL, relief->gc, mask, &xgcv);
1616 }
1617
1618
1619 /* Set up colors for the relief lines around glyph string S. */
1620
1621 static void
1622 x_setup_relief_colors (struct glyph_string *s)
1623 {
1624 struct w32_output *di = s->f->output_data.w32;
1625 COLORREF color;
1626
1627 if (s->face->use_box_color_for_shadows_p)
1628 color = s->face->box_color;
1629 else if (s->first_glyph->type == IMAGE_GLYPH
1630 && s->img->pixmap
1631 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1632 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1633 else
1634 color = s->gc->background;
1635
1636 if (di->white_relief.gc == 0
1637 || color != di->relief_background)
1638 {
1639 di->relief_background = color;
1640 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1641 WHITE_PIX_DEFAULT (s->f));
1642 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1643 BLACK_PIX_DEFAULT (s->f));
1644 }
1645 }
1646
1647
1648 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1649 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1650 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1651 relief. LEFT_P non-zero means draw a relief on the left side of
1652 the rectangle. RIGHT_P non-zero means draw a relief on the right
1653 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1654 when drawing. */
1655
1656 static void
1657 w32_draw_relief_rect (struct frame *f,
1658 int left_x, int top_y, int right_x, int bottom_y,
1659 int width, int raised_p,
1660 int top_p, int bot_p, int left_p, int right_p,
1661 RECT *clip_rect)
1662 {
1663 int i;
1664 XGCValues gc;
1665 HDC hdc = get_frame_dc (f);
1666
1667 if (raised_p)
1668 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1669 else
1670 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1671
1672 w32_set_clip_rectangle (hdc, clip_rect);
1673
1674 /* Top. */
1675 if (top_p)
1676 for (i = 0; i < width; ++i)
1677 w32_fill_area (f, hdc, gc.foreground,
1678 left_x + i * left_p, top_y + i,
1679 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1680
1681 /* Left. */
1682 if (left_p)
1683 for (i = 0; i < width; ++i)
1684 w32_fill_area (f, hdc, gc.foreground,
1685 left_x + i, top_y + (i + 1) * top_p, 1,
1686 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1687
1688 if (raised_p)
1689 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1690 else
1691 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1692
1693 /* Bottom. */
1694 if (bot_p)
1695 for (i = 0; i < width; ++i)
1696 w32_fill_area (f, hdc, gc.foreground,
1697 left_x + i * left_p, bottom_y - i,
1698 right_x - left_x - i * (left_p + right_p) + 1, 1);
1699
1700 /* Right. */
1701 if (right_p)
1702 for (i = 0; i < width; ++i)
1703 w32_fill_area (f, hdc, gc.foreground,
1704 right_x - i, top_y + (i + 1) * top_p, 1,
1705 bottom_y - top_y - (i + 1) * (bot_p + top_p) + 1);
1706
1707 w32_set_clip_rectangle (hdc, NULL);
1708
1709 release_frame_dc (f, hdc);
1710 }
1711
1712
1713 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1714 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1715 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1716 left side of the rectangle. RIGHT_P non-zero means draw a line
1717 on the right side of the rectangle. CLIP_RECT is the clipping
1718 rectangle to use when drawing. */
1719
1720 static void
1721 w32_draw_box_rect (struct glyph_string *s,
1722 int left_x, int top_y, int right_x, int bottom_y, int width,
1723 bool left_p, bool right_p, RECT *clip_rect)
1724 {
1725 w32_set_clip_rectangle (s->hdc, clip_rect);
1726
1727 /* Top. */
1728 w32_fill_area (s->f, s->hdc, s->face->box_color,
1729 left_x, top_y, right_x - left_x + 1, width);
1730
1731 /* Left. */
1732 if (left_p)
1733 {
1734 w32_fill_area (s->f, s->hdc, s->face->box_color,
1735 left_x, top_y, width, bottom_y - top_y + 1);
1736 }
1737
1738 /* Bottom. */
1739 w32_fill_area (s->f, s->hdc, s->face->box_color,
1740 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1741
1742 /* Right. */
1743 if (right_p)
1744 {
1745 w32_fill_area (s->f, s->hdc, s->face->box_color,
1746 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1747 }
1748
1749 w32_set_clip_rectangle (s->hdc, NULL);
1750 }
1751
1752
1753 /* Draw a box around glyph string S. */
1754
1755 static void
1756 x_draw_glyph_string_box (struct glyph_string *s)
1757 {
1758 int width, left_x, right_x, top_y, bottom_y, last_x;
1759 bool left_p, right_p, raised_p;
1760 struct glyph *last_glyph;
1761 RECT clip_rect;
1762
1763 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1764 ? WINDOW_RIGHT_EDGE_X (s->w)
1765 : window_box_right (s->w, s->area));
1766
1767 /* The glyph that may have a right box line. */
1768 last_glyph = (s->cmp || s->img
1769 ? s->first_glyph
1770 : s->first_glyph + s->nchars - 1);
1771
1772 width = eabs (s->face->box_line_width);
1773 raised_p = s->face->box == FACE_RAISED_BOX;
1774 left_x = s->x;
1775 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1776 ? last_x - 1
1777 : min (last_x, s->x + s->background_width) - 1));
1778 top_y = s->y;
1779 bottom_y = top_y + s->height - 1;
1780
1781 left_p = (s->first_glyph->left_box_line_p
1782 || (s->hl == DRAW_MOUSE_FACE
1783 && (s->prev == NULL
1784 || s->prev->hl != s->hl)));
1785 right_p = (last_glyph->right_box_line_p
1786 || (s->hl == DRAW_MOUSE_FACE
1787 && (s->next == NULL
1788 || s->next->hl != s->hl)));
1789
1790 get_glyph_string_clip_rect (s, &clip_rect);
1791
1792 if (s->face->box == FACE_SIMPLE_BOX)
1793 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1794 left_p, right_p, &clip_rect);
1795 else
1796 {
1797 x_setup_relief_colors (s);
1798 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1799 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1800 }
1801 }
1802
1803
1804 /* Draw foreground of image glyph string S. */
1805
1806 static void
1807 x_draw_image_foreground (struct glyph_string *s)
1808 {
1809 int x = s->x;
1810 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1811
1812 /* If first glyph of S has a left box line, start drawing it to the
1813 right of that line. */
1814 if (s->face->box != FACE_NO_BOX
1815 && s->first_glyph->left_box_line_p
1816 && s->slice.x == 0)
1817 x += eabs (s->face->box_line_width);
1818
1819 /* If there is a margin around the image, adjust x- and y-position
1820 by that margin. */
1821 if (s->slice.x == 0)
1822 x += s->img->hmargin;
1823 if (s->slice.y == 0)
1824 y += s->img->vmargin;
1825
1826 SaveDC (s->hdc);
1827
1828 if (s->img->pixmap)
1829 {
1830 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1831 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1832 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1833 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1834 SetBkColor (compat_hdc, RGB (255, 255, 255));
1835 SetTextColor (s->hdc, RGB (0, 0, 0));
1836 x_set_glyph_string_clipping (s);
1837
1838 if (s->img->mask)
1839 {
1840 HDC mask_dc = CreateCompatibleDC (s->hdc);
1841 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1842
1843 SetTextColor (s->hdc, RGB (255, 255, 255));
1844 SetBkColor (s->hdc, RGB (0, 0, 0));
1845
1846 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1847 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1848 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1849 mask_dc, s->slice.x, s->slice.y, SRCAND);
1850 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1851 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1852
1853 SelectObject (mask_dc, mask_orig_obj);
1854 DeleteDC (mask_dc);
1855 }
1856 else
1857 {
1858 SetTextColor (s->hdc, s->gc->foreground);
1859 SetBkColor (s->hdc, s->gc->background);
1860
1861 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1862 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1863
1864 /* When the image has a mask, we can expect that at
1865 least part of a mouse highlight or a block cursor will
1866 be visible. If the image doesn't have a mask, make
1867 a block cursor visible by drawing a rectangle around
1868 the image. I believe it's looking better if we do
1869 nothing here for mouse-face. */
1870 if (s->hl == DRAW_CURSOR)
1871 {
1872 int r = s->img->relief;
1873 if (r < 0) r = -r;
1874 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1875 s->slice.width + r*2 - 1,
1876 s->slice.height + r*2 - 1);
1877 }
1878 }
1879
1880 w32_set_clip_rectangle (s->hdc, NULL);
1881 SelectObject (s->hdc, orig_brush);
1882 DeleteObject (fg_brush);
1883 SelectObject (compat_hdc, orig_obj);
1884 DeleteDC (compat_hdc);
1885 }
1886 else
1887 w32_draw_rectangle (s->hdc, s->gc, x, y,
1888 s->slice.width - 1, s->slice.height - 1);
1889
1890 RestoreDC (s->hdc ,-1);
1891 }
1892
1893
1894 /* Draw a relief around the image glyph string S. */
1895
1896 static void
1897 x_draw_image_relief (struct glyph_string *s)
1898 {
1899 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
1900 int extra_x, extra_y;
1901 RECT r;
1902 int x = s->x;
1903 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1904
1905 /* If first glyph of S has a left box line, start drawing it to the
1906 right of that line. */
1907 if (s->face->box != FACE_NO_BOX
1908 && s->first_glyph->left_box_line_p
1909 && s->slice.x == 0)
1910 x += eabs (s->face->box_line_width);
1911
1912 /* If there is a margin around the image, adjust x- and y-position
1913 by that margin. */
1914 if (s->slice.x == 0)
1915 x += s->img->hmargin;
1916 if (s->slice.y == 0)
1917 y += s->img->vmargin;
1918
1919 if (s->hl == DRAW_IMAGE_SUNKEN
1920 || s->hl == DRAW_IMAGE_RAISED)
1921 {
1922 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief
1923 : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1924 raised_p = s->hl == DRAW_IMAGE_RAISED;
1925 }
1926 else
1927 {
1928 thick = eabs (s->img->relief);
1929 raised_p = s->img->relief > 0;
1930 }
1931
1932 x1 = x + s->slice.width - 1;
1933 y1 = y + s->slice.height - 1;
1934
1935 extra_x = extra_y = 0;
1936 if (s->face->id == TOOL_BAR_FACE_ID)
1937 {
1938 if (CONSP (Vtool_bar_button_margin)
1939 && INTEGERP (XCAR (Vtool_bar_button_margin))
1940 && INTEGERP (XCDR (Vtool_bar_button_margin)))
1941 {
1942 extra_x = XINT (XCAR (Vtool_bar_button_margin));
1943 extra_y = XINT (XCDR (Vtool_bar_button_margin));
1944 }
1945 else if (INTEGERP (Vtool_bar_button_margin))
1946 extra_x = extra_y = XINT (Vtool_bar_button_margin);
1947 }
1948
1949 top_p = bot_p = left_p = right_p = 0;
1950
1951 if (s->slice.x == 0)
1952 x -= thick + extra_x, left_p = 1;
1953 if (s->slice.y == 0)
1954 y -= thick + extra_y, top_p = 1;
1955 if (s->slice.x + s->slice.width == s->img->width)
1956 x1 += thick + extra_x, right_p = 1;
1957 if (s->slice.y + s->slice.height == s->img->height)
1958 y1 += thick + extra_y, bot_p = 1;
1959
1960 x_setup_relief_colors (s);
1961 get_glyph_string_clip_rect (s, &r);
1962 w32_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
1963 top_p, bot_p, left_p, right_p, &r);
1964 }
1965
1966
1967 /* Draw the foreground of image glyph string S to PIXMAP. */
1968
1969 static void
1970 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
1971 {
1972 HDC hdc = CreateCompatibleDC (s->hdc);
1973 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1974 int x = 0;
1975 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1976
1977 /* If first glyph of S has a left box line, start drawing it to the
1978 right of that line. */
1979 if (s->face->box != FACE_NO_BOX
1980 && s->first_glyph->left_box_line_p
1981 && s->slice.x == 0)
1982 x += eabs (s->face->box_line_width);
1983
1984 /* If there is a margin around the image, adjust x- and y-position
1985 by that margin. */
1986 if (s->slice.x == 0)
1987 x += s->img->hmargin;
1988 if (s->slice.y == 0)
1989 y += s->img->vmargin;
1990
1991 if (s->img->pixmap)
1992 {
1993 HDC compat_hdc = CreateCompatibleDC (hdc);
1994 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1995 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1996 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1997
1998 if (s->img->mask)
1999 {
2000 HDC mask_dc = CreateCompatibleDC (hdc);
2001 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
2002
2003 SetTextColor (hdc, RGB (0, 0, 0));
2004 SetBkColor (hdc, RGB (255, 255, 255));
2005 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2006 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2007 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2008 mask_dc, s->slice.x, s->slice.y, SRCAND);
2009 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2010 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2011
2012 SelectObject (mask_dc, mask_orig_obj);
2013 DeleteDC (mask_dc);
2014 }
2015 else
2016 {
2017 SetTextColor (hdc, s->gc->foreground);
2018 SetBkColor (hdc, s->gc->background);
2019
2020 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2021 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2022
2023 /* When the image has a mask, we can expect that at
2024 least part of a mouse highlight or a block cursor will
2025 be visible. If the image doesn't have a mask, make
2026 a block cursor visible by drawing a rectangle around
2027 the image. I believe it's looking better if we do
2028 nothing here for mouse-face. */
2029 if (s->hl == DRAW_CURSOR)
2030 {
2031 int r = s->img->relief;
2032 if (r < 0) r = -r;
2033 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2034 s->slice.width + r*2 - 1,
2035 s->slice.height + r*2 - 1);
2036 }
2037 }
2038
2039 SelectObject (hdc, orig_brush);
2040 DeleteObject (fg_brush);
2041 SelectObject (compat_hdc, orig_obj);
2042 DeleteDC (compat_hdc);
2043 }
2044 else
2045 w32_draw_rectangle (hdc, s->gc, x, y,
2046 s->slice.width - 1, s->slice.height - 1);
2047
2048 SelectObject (hdc, orig_hdc_obj);
2049 DeleteDC (hdc);
2050 }
2051
2052
2053 /* Draw part of the background of glyph string S. X, Y, W, and H
2054 give the rectangle to draw. */
2055
2056 static void
2057 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2058 {
2059 #if 0 /* TODO: stipple */
2060 if (s->stippled_p)
2061 {
2062 /* Fill background with a stipple pattern. */
2063 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2064 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2065 XSetFillStyle (s->display, s->gc, FillSolid);
2066 }
2067 else
2068 #endif
2069 x_clear_glyph_string_rect (s, x, y, w, h);
2070 }
2071
2072
2073 /* Draw image glyph string S.
2074
2075 s->y
2076 s->x +-------------------------
2077 | s->face->box
2078 |
2079 | +-------------------------
2080 | | s->img->vmargin
2081 | |
2082 | | +-------------------
2083 | | | the image
2084
2085 */
2086
2087 static void
2088 x_draw_image_glyph_string (struct glyph_string *s)
2089 {
2090 int x, y;
2091 int box_line_hwidth = eabs (s->face->box_line_width);
2092 int box_line_vwidth = max (s->face->box_line_width, 0);
2093 int height, width;
2094 HBITMAP pixmap = 0;
2095
2096 height = s->height;
2097 if (s->slice.y == 0)
2098 height -= box_line_vwidth;
2099 if (s->slice.y + s->slice.height >= s->img->height)
2100 height -= box_line_vwidth;
2101
2102 /* Fill background with face under the image. Do it only if row is
2103 taller than image or if image has a clip mask to reduce
2104 flickering. */
2105 s->stippled_p = s->face->stipple != 0;
2106 if (height > s->slice.height
2107 || s->img->hmargin
2108 || s->img->vmargin
2109 || s->img->mask
2110 || s->img->pixmap == 0
2111 || s->width != s->background_width)
2112 {
2113 width = s->background_width;
2114 x = s->x;
2115 if (s->first_glyph->left_box_line_p
2116 && s->slice.x == 0)
2117 {
2118 x += box_line_hwidth;
2119 width -= box_line_hwidth;
2120 }
2121
2122 y = s->y;
2123 if (s->slice.y == 0)
2124 y += box_line_vwidth;
2125
2126 #if 0 /* TODO: figure out if we need to do this on Windows. */
2127 if (s->img->mask)
2128 {
2129 /* Create a pixmap as large as the glyph string. Fill it
2130 with the background color. Copy the image to it, using
2131 its mask. Copy the temporary pixmap to the display. */
2132 Screen *screen = FRAME_X_SCREEN (s->f);
2133 int depth = DefaultDepthOfScreen (screen);
2134
2135 /* Create a pixmap as large as the glyph string. */
2136 pixmap = XCreatePixmap (s->display, s->window,
2137 s->background_width,
2138 s->height, depth);
2139
2140 /* Don't clip in the following because we're working on the
2141 pixmap. */
2142 XSetClipMask (s->display, s->gc, None);
2143
2144 /* Fill the pixmap with the background color/stipple. */
2145 if (s->stippled_p)
2146 {
2147 /* Fill background with a stipple pattern. */
2148 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2149 XFillRectangle (s->display, pixmap, s->gc,
2150 0, 0, s->background_width, s->height);
2151 XSetFillStyle (s->display, s->gc, FillSolid);
2152 }
2153 else
2154 {
2155 XGCValues xgcv;
2156 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2157 &xgcv);
2158 XSetForeground (s->display, s->gc, xgcv.background);
2159 XFillRectangle (s->display, pixmap, s->gc,
2160 0, 0, s->background_width, s->height);
2161 XSetForeground (s->display, s->gc, xgcv.foreground);
2162 }
2163 }
2164 else
2165 #endif
2166 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2167
2168 s->background_filled_p = true;
2169 }
2170
2171 /* Draw the foreground. */
2172 if (pixmap != 0)
2173 {
2174 w32_draw_image_foreground_1 (s, pixmap);
2175 x_set_glyph_string_clipping (s);
2176 {
2177 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2178 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2179 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2180 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2181
2182 SetTextColor (s->hdc, s->gc->foreground);
2183 SetBkColor (s->hdc, s->gc->background);
2184 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2185 compat_hdc, 0, 0, SRCCOPY);
2186
2187 SelectObject (s->hdc, orig_brush);
2188 DeleteObject (fg_brush);
2189 SelectObject (compat_hdc, orig_obj);
2190 DeleteDC (compat_hdc);
2191 }
2192 DeleteObject (pixmap);
2193 pixmap = 0;
2194 }
2195 else
2196 x_draw_image_foreground (s);
2197
2198 /* If we must draw a relief around the image, do it. */
2199 if (s->img->relief
2200 || s->hl == DRAW_IMAGE_RAISED
2201 || s->hl == DRAW_IMAGE_SUNKEN)
2202 x_draw_image_relief (s);
2203 }
2204
2205
2206 /* Draw stretch glyph string S. */
2207
2208 static void
2209 x_draw_stretch_glyph_string (struct glyph_string *s)
2210 {
2211 eassert (s->first_glyph->type == STRETCH_GLYPH);
2212
2213 if (s->hl == DRAW_CURSOR
2214 && !x_stretch_cursor_p)
2215 {
2216 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2217 wide as the stretch glyph. */
2218 int width, background_width = s->background_width;
2219 int x = s->x;
2220
2221 if (!s->row->reversed_p)
2222 {
2223 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2224
2225 if (x < left_x)
2226 {
2227 background_width -= left_x - x;
2228 x = left_x;
2229 }
2230 }
2231 else
2232 {
2233 /* In R2L rows, draw the cursor on the right edge of the
2234 stretch glyph. */
2235 int right_x = window_box_right (s->w, TEXT_AREA);
2236
2237 if (x + background_width > right_x)
2238 background_width -= x - right_x;
2239 x += background_width;
2240 }
2241 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2242 if (s->row->reversed_p)
2243 x -= width;
2244
2245 /* Draw cursor. */
2246 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2247
2248 /* Clear rest using the GC of the original non-cursor face. */
2249 if (width < background_width)
2250 {
2251 XGCValues *gc = s->face->gc;
2252 int y = s->y;
2253 int w = background_width - width, h = s->height;
2254 RECT r;
2255 HDC hdc = s->hdc;
2256
2257 if (!s->row->reversed_p)
2258 x += width;
2259 else
2260 x = s->x;
2261 if (s->row->mouse_face_p
2262 && cursor_in_mouse_face_p (s->w))
2263 {
2264 x_set_mouse_face_gc (s);
2265 gc = s->gc;
2266 }
2267 else
2268 gc = s->face->gc;
2269
2270 get_glyph_string_clip_rect (s, &r);
2271 w32_set_clip_rectangle (hdc, &r);
2272
2273 #if 0 /* TODO: stipple */
2274 if (s->face->stipple)
2275 {
2276 /* Fill background with a stipple pattern. */
2277 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2278 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2279 XSetFillStyle (s->display, gc, FillSolid);
2280 }
2281 else
2282 #endif
2283 {
2284 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2285 }
2286 }
2287 }
2288 else if (!s->background_filled_p)
2289 {
2290 int background_width = s->background_width;
2291 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2292
2293 /* Don't draw into left margin, fringe or scrollbar area
2294 except for header line and mode line. */
2295 if (x < left_x && !s->row->mode_line_p)
2296 {
2297 background_width -= left_x - x;
2298 x = left_x;
2299 }
2300 if (background_width > 0)
2301 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2302 }
2303
2304 s->background_filled_p = true;
2305 }
2306
2307
2308 /* Draw glyph string S. */
2309
2310 static void
2311 x_draw_glyph_string (struct glyph_string *s)
2312 {
2313 bool relief_drawn_p = 0;
2314
2315 /* If S draws into the background of its successor, draw the
2316 background of the successor first so that S can draw into it.
2317 This makes S->next use XDrawString instead of XDrawImageString. */
2318 if (s->next && s->right_overhang && !s->for_overlaps)
2319 {
2320 int width;
2321 struct glyph_string *next;
2322 for (width = 0, next = s->next;
2323 next && width < s->right_overhang;
2324 width += next->width, next = next->next)
2325 if (next->first_glyph->type != IMAGE_GLYPH)
2326 {
2327 x_set_glyph_string_gc (next);
2328 x_set_glyph_string_clipping (next);
2329 if (next->first_glyph->type == STRETCH_GLYPH)
2330 x_draw_stretch_glyph_string (next);
2331 else
2332 x_draw_glyph_string_background (next, true);
2333 next->num_clips = 0;
2334 }
2335 }
2336
2337 /* Set up S->gc, set clipping and draw S. */
2338 x_set_glyph_string_gc (s);
2339
2340 /* Draw relief (if any) in advance for char/composition so that the
2341 glyph string can be drawn over it. */
2342 if (!s->for_overlaps
2343 && s->face->box != FACE_NO_BOX
2344 && (s->first_glyph->type == CHAR_GLYPH
2345 || s->first_glyph->type == COMPOSITE_GLYPH))
2346
2347 {
2348 x_set_glyph_string_clipping (s);
2349 x_draw_glyph_string_background (s, true);
2350 x_draw_glyph_string_box (s);
2351 x_set_glyph_string_clipping (s);
2352 relief_drawn_p = 1;
2353 }
2354 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2355 && !s->clip_tail
2356 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2357 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2358 /* We must clip just this glyph. left_overhang part has already
2359 drawn when s->prev was drawn, and right_overhang part will be
2360 drawn later when s->next is drawn. */
2361 x_set_glyph_string_clipping_exactly (s, s);
2362 else
2363 x_set_glyph_string_clipping (s);
2364
2365 switch (s->first_glyph->type)
2366 {
2367 case IMAGE_GLYPH:
2368 x_draw_image_glyph_string (s);
2369 break;
2370
2371 case STRETCH_GLYPH:
2372 x_draw_stretch_glyph_string (s);
2373 break;
2374
2375 case CHAR_GLYPH:
2376 if (s->for_overlaps)
2377 s->background_filled_p = true;
2378 else
2379 x_draw_glyph_string_background (s, false);
2380 x_draw_glyph_string_foreground (s);
2381 break;
2382
2383 case COMPOSITE_GLYPH:
2384 if (s->for_overlaps || (s->cmp_from > 0
2385 && ! s->first_glyph->u.cmp.automatic))
2386 s->background_filled_p = true;
2387 else
2388 x_draw_glyph_string_background (s, true);
2389 x_draw_composite_glyph_string_foreground (s);
2390 break;
2391
2392 case GLYPHLESS_GLYPH:
2393 if (s->for_overlaps)
2394 s->background_filled_p = true;
2395 else
2396 x_draw_glyph_string_background (s, false);
2397 x_draw_glyphless_glyph_string_foreground (s);
2398 break;
2399
2400 default:
2401 emacs_abort ();
2402 }
2403
2404 if (!s->for_overlaps)
2405 {
2406 /* Draw underline. */
2407 if (s->face->underline_p)
2408 {
2409 if (s->face->underline_type == FACE_UNDER_WAVE)
2410 {
2411 COLORREF color;
2412
2413 if (s->face->underline_defaulted_p)
2414 color = s->gc->foreground;
2415 else
2416 color = s->face->underline_color;
2417
2418 w32_draw_underwave (s, color);
2419 }
2420 else if (s->face->underline_type == FACE_UNDER_LINE)
2421 {
2422 unsigned long thickness, position;
2423 int y;
2424
2425 if (s->prev && s->prev->face->underline_p
2426 && s->prev->face->underline_type == FACE_UNDER_LINE)
2427 {
2428 /* We use the same underline style as the previous one. */
2429 thickness = s->prev->underline_thickness;
2430 position = s->prev->underline_position;
2431 }
2432 else
2433 {
2434 /* Get the underline thickness. Default is 1 pixel. */
2435 if (s->font && s->font->underline_thickness > 0)
2436 thickness = s->font->underline_thickness;
2437 else
2438 thickness = 1;
2439 if (x_underline_at_descent_line)
2440 position = (s->height - thickness) - (s->ybase - s->y);
2441 else
2442 {
2443 /* Get the underline position. This is the recommended
2444 vertical offset in pixels from the baseline to the top of
2445 the underline. This is a signed value according to the
2446 specs, and its default is
2447
2448 ROUND ((maximum_descent) / 2), with
2449 ROUND (x) = floor (x + 0.5) */
2450
2451 if (x_use_underline_position_properties
2452 && s->font && s->font->underline_position >= 0)
2453 position = s->font->underline_position;
2454 else if (s->font)
2455 position = (s->font->descent + 1) / 2;
2456 }
2457 position = max (position, underline_minimum_offset);
2458 }
2459 /* Check the sanity of thickness and position. We should
2460 avoid drawing underline out of the current line area. */
2461 if (s->y + s->height <= s->ybase + position)
2462 position = (s->height - 1) - (s->ybase - s->y);
2463 if (s->y + s->height < s->ybase + position + thickness)
2464 thickness = (s->y + s->height) - (s->ybase + position);
2465 s->underline_thickness = thickness;
2466 s->underline_position =position;
2467 y = s->ybase + position;
2468 if (s->face->underline_defaulted_p)
2469 {
2470 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2471 y, s->width, 1);
2472 }
2473 else
2474 {
2475 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2476 y, s->width, 1);
2477 }
2478 }
2479 }
2480 /* Draw overline. */
2481 if (s->face->overline_p)
2482 {
2483 unsigned long dy = 0, h = 1;
2484
2485 if (s->face->overline_color_defaulted_p)
2486 {
2487 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2488 s->y + dy, s->width, h);
2489 }
2490 else
2491 {
2492 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2493 s->y + dy, s->width, h);
2494 }
2495 }
2496
2497 /* Draw strike-through. */
2498 if (s->face->strike_through_p
2499 && !FONT_TEXTMETRIC (s->font).tmStruckOut)
2500 {
2501 unsigned long h = 1;
2502 unsigned long dy = (s->height - h) / 2;
2503
2504 if (s->face->strike_through_color_defaulted_p)
2505 {
2506 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2507 s->width, h);
2508 }
2509 else
2510 {
2511 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2512 s->y + dy, s->width, h);
2513 }
2514 }
2515
2516 /* Draw relief if not yet drawn. */
2517 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2518 x_draw_glyph_string_box (s);
2519
2520 if (s->prev)
2521 {
2522 struct glyph_string *prev;
2523
2524 for (prev = s->prev; prev; prev = prev->prev)
2525 if (prev->hl != s->hl
2526 && prev->x + prev->width + prev->right_overhang > s->x)
2527 {
2528 /* As prev was drawn while clipped to its own area, we
2529 must draw the right_overhang part using s->hl now. */
2530 enum draw_glyphs_face save = prev->hl;
2531
2532 prev->hl = s->hl;
2533 x_set_glyph_string_gc (prev);
2534 x_set_glyph_string_clipping_exactly (s, prev);
2535 if (prev->first_glyph->type == CHAR_GLYPH)
2536 x_draw_glyph_string_foreground (prev);
2537 else
2538 x_draw_composite_glyph_string_foreground (prev);
2539 w32_set_clip_rectangle (prev->hdc, NULL);
2540 prev->hl = save;
2541 prev->num_clips = 0;
2542 }
2543 }
2544
2545 if (s->next)
2546 {
2547 struct glyph_string *next;
2548
2549 for (next = s->next; next; next = next->next)
2550 if (next->hl != s->hl
2551 && next->x - next->left_overhang < s->x + s->width)
2552 {
2553 /* As next will be drawn while clipped to its own area,
2554 we must draw the left_overhang part using s->hl now. */
2555 enum draw_glyphs_face save = next->hl;
2556
2557 next->hl = s->hl;
2558 x_set_glyph_string_gc (next);
2559 x_set_glyph_string_clipping_exactly (s, next);
2560 if (next->first_glyph->type == CHAR_GLYPH)
2561 x_draw_glyph_string_foreground (next);
2562 else
2563 x_draw_composite_glyph_string_foreground (next);
2564 w32_set_clip_rectangle (next->hdc, NULL);
2565 next->hl = save;
2566 next->num_clips = 0;
2567 next->clip_head = s->next;
2568 }
2569 }
2570 }
2571
2572 /* Reset clipping. */
2573 w32_set_clip_rectangle (s->hdc, NULL);
2574 s->num_clips = 0;
2575 }
2576
2577
2578 /* Shift display to make room for inserted glyphs. */
2579
2580 void
2581 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2582 int width, int height, int shift_by)
2583 {
2584 HDC hdc;
2585
2586 hdc = get_frame_dc (f);
2587 BitBlt (hdc, x + shift_by, y, width, height,
2588 hdc, x, y, SRCCOPY);
2589
2590 release_frame_dc (f, hdc);
2591 }
2592
2593
2594 /* Delete N glyphs at the nominal cursor position. Not implemented
2595 for X frames. */
2596
2597 static void
2598 x_delete_glyphs (struct frame *f, register int n)
2599 {
2600 if (! FRAME_W32_P (f))
2601 return;
2602
2603 emacs_abort ();
2604 }
2605
2606
2607 /* Clear entire frame. */
2608
2609 static void
2610 x_clear_frame (struct frame *f)
2611 {
2612 if (! FRAME_W32_P (f))
2613 return;
2614
2615 /* Clearing the frame will erase any cursor, so mark them all as no
2616 longer visible. */
2617 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2618
2619 block_input ();
2620
2621 w32_clear_window (f);
2622
2623 /* We have to clear the scroll bars, too. If we have changed
2624 colors or something like that, then they should be notified. */
2625 x_scroll_bar_clear (f);
2626
2627 unblock_input ();
2628 }
2629
2630 \f
2631 /* Make audible bell. */
2632
2633 static void
2634 w32_ring_bell (struct frame *f)
2635 {
2636 block_input ();
2637
2638 if (FRAME_W32_P (f) && visible_bell)
2639 {
2640 int i;
2641 HWND hwnd = FRAME_W32_WINDOW (f);
2642
2643 for (i = 0; i < 5; i++)
2644 {
2645 FlashWindow (hwnd, TRUE);
2646 Sleep (10);
2647 }
2648 FlashWindow (hwnd, FALSE);
2649 }
2650 else
2651 w32_sys_ring_bell (f);
2652
2653 unblock_input ();
2654 }
2655
2656 /***********************************************************************
2657 Line Dance
2658 ***********************************************************************/
2659
2660 /* Perform an insert-lines or delete-lines operation, inserting N
2661 lines or deleting -N lines at vertical position VPOS. */
2662
2663 static void
2664 x_ins_del_lines (struct frame *f, int vpos, int n)
2665 {
2666 if (! FRAME_W32_P (f))
2667 return;
2668
2669 emacs_abort ();
2670 }
2671
2672
2673 /* Scroll part of the display as described by RUN. */
2674
2675 static void
2676 x_scroll_run (struct window *w, struct run *run)
2677 {
2678 struct frame *f = XFRAME (w->frame);
2679 int x, y, width, height, from_y, to_y, bottom_y;
2680 HWND hwnd = FRAME_W32_WINDOW (f);
2681 HRGN expect_dirty;
2682
2683 /* Get frame-relative bounding box of the text display area of W,
2684 without mode lines. Include in this box the left and right
2685 fringes of W. */
2686 window_box (w, ANY_AREA, &x, &y, &width, &height);
2687
2688 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2689 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2690 bottom_y = y + height;
2691
2692 if (to_y < from_y)
2693 {
2694 /* Scrolling up. Make sure we don't copy part of the mode
2695 line at the bottom. */
2696 if (from_y + run->height > bottom_y)
2697 height = bottom_y - from_y;
2698 else
2699 height = run->height;
2700 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2701 }
2702 else
2703 {
2704 /* Scrolling down. Make sure we don't copy over the mode line.
2705 at the bottom. */
2706 if (to_y + run->height > bottom_y)
2707 height = bottom_y - to_y;
2708 else
2709 height = run->height;
2710 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2711 }
2712
2713 block_input ();
2714
2715 /* Cursor off. Will be switched on again in x_update_window_end. */
2716 x_clear_cursor (w);
2717
2718 {
2719 RECT from;
2720 RECT to;
2721 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2722 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2723
2724 from.left = to.left = x;
2725 from.right = to.right = x + width;
2726 from.top = from_y;
2727 from.bottom = from_y + height;
2728 to.top = y;
2729 to.bottom = bottom_y;
2730
2731 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2732 NULL, SW_INVALIDATE);
2733
2734 /* Combine this with what we expect to be dirty. This covers the
2735 case where not all of the region we expect is actually dirty. */
2736 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2737
2738 /* If the dirty region is not what we expected, redraw the entire frame. */
2739 if (!EqualRgn (combined, expect_dirty))
2740 SET_FRAME_GARBAGED (f);
2741
2742 DeleteObject (dirty);
2743 DeleteObject (combined);
2744 }
2745
2746 unblock_input ();
2747 DeleteObject (expect_dirty);
2748 }
2749
2750
2751 \f
2752 /***********************************************************************
2753 Exposure Events
2754 ***********************************************************************/
2755
2756 static void
2757 frame_highlight (struct frame *f)
2758 {
2759 x_update_cursor (f, 1);
2760 x_set_frame_alpha (f);
2761 }
2762
2763 static void
2764 frame_unhighlight (struct frame *f)
2765 {
2766 x_update_cursor (f, 1);
2767 x_set_frame_alpha (f);
2768 }
2769
2770 /* The focus has changed. Update the frames as necessary to reflect
2771 the new situation. Note that we can't change the selected frame
2772 here, because the Lisp code we are interrupting might become confused.
2773 Each event gets marked with the frame in which it occurred, so the
2774 Lisp code can tell when the switch took place by examining the events. */
2775
2776 static void
2777 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2778 {
2779 struct frame *old_focus = dpyinfo->w32_focus_frame;
2780
2781 if (frame != dpyinfo->w32_focus_frame)
2782 {
2783 /* Set this before calling other routines, so that they see
2784 the correct value of w32_focus_frame. */
2785 dpyinfo->w32_focus_frame = frame;
2786
2787 if (old_focus && old_focus->auto_lower)
2788 x_lower_frame (old_focus);
2789
2790 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2791 dpyinfo->w32_pending_autoraise_frame = dpyinfo->w32_focus_frame;
2792 else
2793 dpyinfo->w32_pending_autoraise_frame = NULL;
2794 }
2795
2796 x_frame_rehighlight (dpyinfo);
2797 }
2798
2799
2800 /* Handle FocusIn and FocusOut state changes for FRAME.
2801 If FRAME has focus and there exists more than one frame, puts
2802 a FOCUS_IN_EVENT into *BUFP. */
2803
2804 static void
2805 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2806 struct frame *frame, struct input_event *bufp)
2807 {
2808 if (type == WM_SETFOCUS)
2809 {
2810 if (dpyinfo->w32_focus_event_frame != frame)
2811 {
2812 x_new_focus_frame (dpyinfo, frame);
2813 dpyinfo->w32_focus_event_frame = frame;
2814
2815 /* Don't stop displaying the initial startup message
2816 for a switch-frame event we don't need. */
2817 if (NILP (Vterminal_frame)
2818 && CONSP (Vframe_list)
2819 && !NILP (XCDR (Vframe_list)))
2820 {
2821 bufp->arg = Qt;
2822 }
2823 else
2824 {
2825 bufp->arg = Qnil;
2826 }
2827
2828 bufp->kind = FOCUS_IN_EVENT;
2829 XSETFRAME (bufp->frame_or_window, frame);
2830 }
2831
2832 frame->output_data.x->focus_state |= state;
2833
2834 /* TODO: IME focus? */
2835 }
2836 else if (type == WM_KILLFOCUS)
2837 {
2838 frame->output_data.x->focus_state &= ~state;
2839
2840 if (dpyinfo->w32_focus_event_frame == frame)
2841 {
2842 dpyinfo->w32_focus_event_frame = 0;
2843 x_new_focus_frame (dpyinfo, 0);
2844
2845 bufp->kind = FOCUS_OUT_EVENT;
2846 XSETFRAME (bufp->frame_or_window, frame);
2847 }
2848
2849 /* TODO: IME focus? */
2850 }
2851 }
2852
2853
2854 /* The focus may have changed. Figure out if it is a real focus change,
2855 by checking both FocusIn/Out and Enter/LeaveNotify events.
2856
2857 Returns FOCUS_IN_EVENT event in *BUFP. */
2858
2859 static void
2860 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2861 struct input_event *bufp)
2862 {
2863 struct frame *frame;
2864
2865 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2866 if (! frame)
2867 return;
2868
2869 /* On w32, this is only called from focus events, so no switch needed. */
2870 x_focus_changed (event->msg.message,
2871 (event->msg.message == WM_KILLFOCUS ?
2872 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2873 dpyinfo, frame, bufp);
2874 }
2875
2876
2877 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2878
2879 void
2880 x_mouse_leave (struct w32_display_info *dpyinfo)
2881 {
2882 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2883 }
2884
2885 /* The focus has changed, or we have redirected a frame's focus to
2886 another frame (this happens when a frame uses a surrogate
2887 mini-buffer frame). Shift the highlight as appropriate.
2888
2889 The FRAME argument doesn't necessarily have anything to do with which
2890 frame is being highlighted or un-highlighted; we only use it to find
2891 the appropriate X display info. */
2892
2893 static void
2894 w32_frame_rehighlight (struct frame *frame)
2895 {
2896 if (! FRAME_W32_P (frame))
2897 return;
2898 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
2899 }
2900
2901 static void
2902 x_frame_rehighlight (