src/w32*.c: Convert function definitions to standard C.
[emacs.git] / src / w32term.c
blob5dfe6a363d32c848a636b6116730d6ddeb090f7e
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <setjmp.h>
26 #include "lisp.h"
27 #include "blockinput.h"
28 #include "w32term.h"
30 #include "systty.h"
31 #include "systime.h"
33 #include <ctype.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <imm.h>
38 #include "charset.h"
39 #include "character.h"
40 #include "coding.h"
41 #include "ccl.h"
42 #include "frame.h"
43 #include "dispextern.h"
44 #include "fontset.h"
45 #include "termhooks.h"
46 #include "termopts.h"
47 #include "termchar.h"
48 #include "disptab.h"
49 #include "buffer.h"
50 #include "window.h"
51 #include "keyboard.h"
52 #include "intervals.h"
53 #include "process.h"
54 #include "atimer.h"
55 #include "keymap.h"
57 #include "w32heap.h"
58 #include <shellapi.h>
60 #include "font.h"
61 #include "w32font.h"
63 /* Fringe bitmaps. */
65 static int max_fringe_bmp = 0;
66 static HBITMAP *fringe_bmp = 0;
68 /* Non-nil means Emacs uses toolkit scroll bars. */
70 Lisp_Object Vx_toolkit_scroll_bars;
72 /* Temporary variables for w32_read_socket. */
74 static int last_mousemove_x = 0;
75 static int last_mousemove_y = 0;
77 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
78 #ifndef GET_WHEEL_DELTA_WPARAM
79 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
80 #endif
82 /* Non-zero means that a HELP_EVENT has been generated since Emacs
83 start. */
85 static int any_help_event_p;
87 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
88 static Lisp_Object last_window;
90 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
91 int x_use_underline_position_properties;
93 /* Non-zero means to draw the underline at the same place as the descent line. */
95 int x_underline_at_descent_line;
97 extern unsigned int msh_mousewheel;
99 extern void free_frame_menubar (struct frame *);
101 extern int w32_codepage_for_font (char *fontname);
102 extern Cursor w32_load_cursor (LPCTSTR name);
104 extern Lisp_Object Vwindow_system;
106 #define x_any_window_to_frame x_window_to_frame
107 #define x_top_window_to_frame x_window_to_frame
110 /* This is display since w32 does not support multiple ones. */
111 struct w32_display_info one_w32_display_info;
112 struct w32_display_info *x_display_list;
114 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
115 one for each element of w32_display_list and in the same order.
116 NAME is the name of the frame.
117 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
118 Lisp_Object w32_display_name_list;
121 #ifndef GLYPHSET
122 /* Pre Windows 2000, this was not available, but define it here so
123 that Emacs compiled on such a platform will run on newer versions. */
125 typedef struct tagWCRANGE
127 WCHAR wcLow;
128 USHORT cGlyphs;
129 } WCRANGE;
131 typedef struct tagGLYPHSET
133 DWORD cbThis;
134 DWORD flAccel;
135 DWORD cGlyphsSupported;
136 DWORD cRanges;
137 WCRANGE ranges[1];
138 } GLYPHSET;
140 #endif
142 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
143 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
145 #ifndef LWA_ALPHA
146 #define LWA_ALPHA 0x02
147 #endif
148 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
149 later targets by MSVC headers. */
150 #ifndef WS_EX_LAYERED
151 #define WS_EX_LAYERED 0x80000
152 #endif
154 /* Frame being updated by update_frame. This is declared in term.c.
155 This is set by update_begin and looked at by all the
156 w32 functions. It is zero while not inside an update.
157 In that case, the w32 functions assume that `SELECTED_FRAME ()'
158 is the frame to apply to. */
159 extern struct frame *updating_frame;
161 /* This is a frame waiting to be autoraised, within w32_read_socket. */
162 struct frame *pending_autoraise_frame;
164 /* The handle of the frame that currently owns the system caret. */
165 HWND w32_system_caret_hwnd;
166 int w32_system_caret_height;
167 int w32_system_caret_x;
168 int w32_system_caret_y;
169 int w32_use_visible_system_caret;
171 DWORD dwWindowsThreadId = 0;
172 HANDLE hWindowsThread = NULL;
173 DWORD dwMainThreadId = 0;
174 HANDLE hMainThread = NULL;
176 int vertical_scroll_bar_min_handle;
177 int vertical_scroll_bar_top_border;
178 int vertical_scroll_bar_bottom_border;
180 int last_scroll_bar_drag_pos;
182 /* Mouse movement. */
184 /* Where the mouse was last time we reported a mouse event. */
185 static RECT last_mouse_glyph;
186 static FRAME_PTR last_mouse_glyph_frame;
187 static Lisp_Object last_mouse_press_frame;
189 int w32_num_mouse_buttons;
191 Lisp_Object Vw32_swap_mouse_buttons;
193 /* Control whether x_raise_frame also sets input focus. */
194 Lisp_Object Vw32_grab_focus_on_raise;
196 /* Control whether Caps Lock affects non-ascii characters. */
197 Lisp_Object Vw32_capslock_is_shiftlock;
199 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
200 Lisp_Object Vw32_recognize_altgr;
202 /* The scroll bar in which the last motion event occurred.
204 If the last motion event occurred in a scroll bar, we set this
205 so w32_mouse_position can know whether to report a scroll bar motion or
206 an ordinary motion.
208 If the last motion event didn't occur in a scroll bar, we set this
209 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
210 static Lisp_Object last_mouse_scroll_bar;
211 static int last_mouse_scroll_bar_pos;
213 /* This is a hack. We would really prefer that w32_mouse_position would
214 return the time associated with the position it returns, but there
215 doesn't seem to be any way to wrest the time-stamp from the server
216 along with the position query. So, we just keep track of the time
217 of the last movement we received, and return that in hopes that
218 it's somewhat accurate. */
219 static Time last_mouse_movement_time;
221 /* Incremented by w32_read_socket whenever it really tries to read
222 events. */
223 #ifdef __STDC__
224 static int volatile input_signal_count;
225 #else
226 static int input_signal_count;
227 #endif
229 extern Lisp_Object Vcommand_line_args, Vsystem_name;
231 /* A mask of extra modifier bits to put into every keyboard char. */
232 extern EMACS_INT extra_keyboard_modifiers;
234 /* Keyboard code page - may be changed by language-change events. */
235 static int keyboard_codepage;
237 static void x_update_window_end (struct window *, int, int);
238 static void w32_handle_tool_bar_click (struct frame *,
239 struct input_event *);
240 static void w32_define_cursor (Window, Cursor);
242 void x_lower_frame (struct frame *);
243 void x_scroll_bar_clear (struct frame *);
244 void x_wm_set_size_hint (struct frame *, long, int);
245 void x_raise_frame (struct frame *);
246 void x_set_window_size (struct frame *, int, int, int);
247 void x_wm_set_window_state (struct frame *, int);
248 void x_wm_set_icon_pixmap (struct frame *, int);
249 static void w32_initialize (void);
250 static void x_update_end (struct frame *);
251 static void w32_frame_up_to_date (struct frame *);
252 static void w32_set_terminal_modes (struct terminal *);
253 static void w32_reset_terminal_modes (struct terminal *);
254 static void x_clear_frame (struct frame *);
255 static void frame_highlight (struct frame *);
256 static void frame_unhighlight (struct frame *);
257 static void x_new_focus_frame (struct w32_display_info *,
258 struct frame *);
259 static void x_focus_changed (int, int, struct w32_display_info *,
260 struct frame *, struct input_event *);
261 static void w32_detect_focus_change (struct w32_display_info *,
262 W32Msg *, struct input_event *);
263 static void w32_frame_rehighlight (struct frame *);
264 static void x_frame_rehighlight (struct w32_display_info *);
265 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
266 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
267 enum text_cursor_kinds);
268 static void w32_clip_to_row (struct window *, struct glyph_row *, int, HDC);
269 static BOOL my_show_window (struct frame *, HWND, int);
270 static void my_set_window_pos (HWND, HWND, int, int, int, int, UINT);
271 static void my_set_focus (struct frame *, HWND);
272 static void my_set_foreground_window (HWND);
273 static void my_destroy_window (struct frame *, HWND);
275 static Lisp_Object Qvendor_specific_keysyms;
278 /***********************************************************************
279 Debugging
280 ***********************************************************************/
282 #if 0
284 /* This is a function useful for recording debugging information about
285 the sequence of occurrences in this file. */
287 struct record
289 char *locus;
290 int type;
293 struct record event_record[100];
295 int event_record_index;
297 record_event (char *locus, int type)
299 if (event_record_index == sizeof (event_record) / sizeof (struct record))
300 event_record_index = 0;
302 event_record[event_record_index].locus = locus;
303 event_record[event_record_index].type = type;
304 event_record_index++;
307 #endif /* 0 */
310 void
311 XChangeGC (void *ignore, XGCValues *gc, unsigned long mask,
312 XGCValues *xgcv)
314 if (mask & GCForeground)
315 gc->foreground = xgcv->foreground;
316 if (mask & GCBackground)
317 gc->background = xgcv->background;
318 if (mask & GCFont)
319 gc->font = xgcv->font;
322 XGCValues *
323 XCreateGC (void *ignore, Window window, unsigned long mask, XGCValues *xgcv)
325 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
326 bzero (gc, sizeof (XGCValues));
328 XChangeGC (ignore, gc, mask, xgcv);
330 return gc;
333 void
334 XGetGCValues (void *ignore, XGCValues *gc,
335 unsigned long mask, XGCValues *xgcv)
337 XChangeGC (ignore, xgcv, mask, gc);
340 static void
341 w32_set_clip_rectangle (HDC hdc, RECT *rect)
343 if (rect)
345 HRGN clip_region = CreateRectRgnIndirect (rect);
346 SelectClipRgn (hdc, clip_region);
347 DeleteObject (clip_region);
349 else
350 SelectClipRgn (hdc, NULL);
354 /* Draw a hollow rectangle at the specified position. */
355 void
356 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
357 int width, int height)
359 HBRUSH hb, oldhb;
360 HPEN hp, oldhp;
362 hb = CreateSolidBrush (gc->background);
363 hp = CreatePen (PS_SOLID, 0, gc->foreground);
364 oldhb = SelectObject (hdc, hb);
365 oldhp = SelectObject (hdc, hp);
367 Rectangle (hdc, x, y, x + width, y + height);
369 SelectObject (hdc, oldhb);
370 SelectObject (hdc, oldhp);
371 DeleteObject (hb);
372 DeleteObject (hp);
375 /* Draw a filled rectangle at the specified position. */
376 void
377 w32_fill_rect (FRAME_PTR f, HDC hdc, COLORREF pix, RECT *lprect)
379 HBRUSH hb;
381 hb = CreateSolidBrush (pix);
382 FillRect (hdc, lprect, hb);
383 DeleteObject (hb);
386 void
387 w32_clear_window (FRAME_PTR f)
389 RECT rect;
390 HDC hdc = get_frame_dc (f);
392 /* Under certain conditions, this can be called at startup with
393 a console frame pointer before the GUI frame is created. An HDC
394 of 0 indicates this. */
395 if (hdc)
397 GetClientRect (FRAME_W32_WINDOW (f), &rect);
398 w32_clear_rect (f, hdc, &rect);
401 release_frame_dc (f, hdc);
404 #define OPAQUE_FRAME 255
406 void
407 x_set_frame_alpha (struct frame *f)
409 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
410 double alpha = 1.0;
411 double alpha_min = 1.0;
412 BYTE opac;
413 LONG ex_style;
414 HWND window = FRAME_W32_WINDOW (f);
416 /* Older versions of Windows do not support transparency. */
417 if (!pfnSetLayeredWindowAttributes)
418 return;
420 if (dpyinfo->x_highlight_frame == f)
421 alpha = f->alpha[0];
422 else
423 alpha = f->alpha[1];
425 if (FLOATP (Vframe_alpha_lower_limit))
426 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
427 else if (INTEGERP (Vframe_alpha_lower_limit))
428 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
430 if (alpha < 0.0)
431 return;
432 else if (alpha > 1.0)
433 alpha = 1.0;
434 else if (alpha < alpha_min && alpha_min <= 1.0)
435 alpha = alpha_min;
437 opac = alpha * OPAQUE_FRAME;
439 ex_style = GetWindowLong (window, GWL_EXSTYLE);
441 if (opac == OPAQUE_FRAME)
442 ex_style &= ~WS_EX_LAYERED;
443 else
444 ex_style |= WS_EX_LAYERED;
446 SetWindowLong (window, GWL_EXSTYLE, ex_style);
448 if (opac != OPAQUE_FRAME)
449 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
453 x_display_pixel_height (struct w32_display_info *dpyinfo)
455 HDC dc = GetDC (NULL);
456 int pixels = GetDeviceCaps (dc, VERTRES);
457 ReleaseDC (NULL, dc);
458 return pixels;
462 x_display_pixel_width (struct w32_display_info *dpyinfo)
464 HDC dc = GetDC (NULL);
465 int pixels = GetDeviceCaps (dc, HORZRES);
466 ReleaseDC (NULL, dc);
467 return pixels;
471 /***********************************************************************
472 Starting and ending an update
473 ***********************************************************************/
475 /* Start an update of frame F. This function is installed as a hook
476 for update_begin, i.e. it is called when update_begin is called.
477 This function is called prior to calls to x_update_window_begin for
478 each window being updated. */
480 static void
481 x_update_begin (struct frame *f)
483 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
485 if (! FRAME_W32_P (f))
486 return;
488 /* Regenerate display palette before drawing if list of requested
489 colors has changed. */
490 if (display_info->regen_palette)
492 w32_regenerate_palette (f);
493 display_info->regen_palette = FALSE;
498 /* Start update of window W. Set the global variable updated_window
499 to the window being updated and set output_cursor to the cursor
500 position of W. */
502 static void
503 x_update_window_begin (struct window *w)
505 struct frame *f = XFRAME (WINDOW_FRAME (w));
506 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
508 /* Hide the system caret during an update. */
509 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
511 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
514 updated_window = w;
515 set_output_cursor (&w->cursor);
517 BLOCK_INPUT;
519 if (f == display_info->mouse_face_mouse_frame)
521 /* Don't do highlighting for mouse motion during the update. */
522 display_info->mouse_face_defer = 1;
524 /* If F needs to be redrawn, simply forget about any prior mouse
525 highlighting. */
526 if (FRAME_GARBAGED_P (f))
527 display_info->mouse_face_window = Qnil;
529 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
530 their mouse_face_p flag set, which means that they are always
531 unequal to rows in a desired matrix which never have that
532 flag set. So, rows containing mouse-face glyphs are never
533 scrolled, and we don't have to switch the mouse highlight off
534 here to prevent it from being scrolled. */
536 /* Can we tell that this update does not affect the window
537 where the mouse highlight is? If so, no need to turn off.
538 Likewise, don't do anything if the frame is garbaged;
539 in that case, the frame's current matrix that we would use
540 is all wrong, and we will redisplay that line anyway. */
541 if (!NILP (display_info->mouse_face_window)
542 && w == XWINDOW (display_info->mouse_face_window))
544 int i;
546 for (i = 0; i < w->desired_matrix->nrows; ++i)
547 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
548 break;
550 if (i < w->desired_matrix->nrows)
551 clear_mouse_face (display_info);
553 #endif /* 0 */
556 UNBLOCK_INPUT;
559 /* Draw a vertical window border from (x,y0) to (x,y1) */
561 static void
562 w32_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
564 struct frame *f = XFRAME (WINDOW_FRAME (w));
565 RECT r;
566 HDC hdc;
567 struct face *face;
569 r.left = x;
570 r.right = x + 1;
571 r.top = y0;
572 r.bottom = y1;
574 hdc = get_frame_dc (f);
575 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
576 if (face)
577 w32_fill_rect (f, hdc, face->foreground, &r);
578 else
579 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
581 release_frame_dc (f, hdc);
585 /* End update of window W (which is equal to updated_window).
587 Draw vertical borders between horizontally adjacent windows, and
588 display W's cursor if CURSOR_ON_P is non-zero.
590 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
591 glyphs in mouse-face were overwritten. In that case we have to
592 make sure that the mouse-highlight is properly redrawn.
594 W may be a menu bar pseudo-window in case we don't have X toolkit
595 support. Such windows don't have a cursor, so don't display it
596 here. */
598 static void
599 x_update_window_end (struct window *w, int cursor_on_p,
600 int mouse_face_overwritten_p)
602 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
604 if (!w->pseudo_window_p)
606 BLOCK_INPUT;
608 if (cursor_on_p)
609 display_and_set_cursor (w, 1, output_cursor.hpos,
610 output_cursor.vpos,
611 output_cursor.x, output_cursor.y);
613 if (draw_window_fringes (w, 1))
614 x_draw_vertical_border (w);
616 UNBLOCK_INPUT;
619 /* If a row with mouse-face was overwritten, arrange for
620 XTframe_up_to_date to redisplay the mouse highlight. */
621 if (mouse_face_overwritten_p)
623 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
624 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
625 dpyinfo->mouse_face_window = Qnil;
628 /* Unhide the caret. This won't actually show the cursor, unless it
629 was visible before the corresponding call to HideCaret in
630 x_update_window_begin. */
631 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
633 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
636 updated_window = NULL;
640 /* End update of frame F. This function is installed as a hook in
641 update_end. */
643 static void
644 x_update_end (struct frame *f)
646 if (! FRAME_W32_P (f))
647 return;
649 /* Mouse highlight may be displayed again. */
650 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
654 /* This function is called from various places in xdisp.c whenever a
655 complete update has been performed. The global variable
656 updated_window is not available here. */
658 static void
659 w32_frame_up_to_date (struct frame *f)
661 if (FRAME_W32_P (f))
663 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
665 if (dpyinfo->mouse_face_deferred_gc
666 || f == dpyinfo->mouse_face_mouse_frame)
668 BLOCK_INPUT;
669 if (dpyinfo->mouse_face_mouse_frame)
670 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
671 dpyinfo->mouse_face_mouse_x,
672 dpyinfo->mouse_face_mouse_y);
673 dpyinfo->mouse_face_deferred_gc = 0;
674 UNBLOCK_INPUT;
680 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
681 arrow bitmaps, or clear the fringes if no bitmaps are required
682 before DESIRED_ROW is made current. The window being updated is
683 found in updated_window. This function is called from
684 update_window_line only if it is known that there are differences
685 between bitmaps to be drawn between current row and DESIRED_ROW. */
687 static void
688 x_after_update_window_line (struct glyph_row *desired_row)
690 struct window *w = updated_window;
691 struct frame *f;
692 int width, height;
694 xassert (w);
696 if (!desired_row->mode_line_p && !w->pseudo_window_p)
697 desired_row->redraw_fringe_bitmaps_p = 1;
699 /* When a window has disappeared, make sure that no rest of
700 full-width rows stays visible in the internal border. Could
701 check here if updated_window is the leftmost/rightmost window,
702 but I guess it's not worth doing since vertically split windows
703 are almost never used, internal border is rarely set, and the
704 overhead is very small. */
705 if (windows_or_buffers_changed
706 && desired_row->full_width_p
707 && (f = XFRAME (w->frame),
708 width = FRAME_INTERNAL_BORDER_WIDTH (f),
709 width != 0)
710 && (height = desired_row->visible_height,
711 height > 0))
713 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
715 BLOCK_INPUT;
717 HDC hdc = get_frame_dc (f);
718 w32_clear_area (f, hdc, 0, y, width, height);
719 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
720 y, width, height);
721 release_frame_dc (f, hdc);
723 UNBLOCK_INPUT;
728 /* Draw the bitmap WHICH in one of the left or right fringes of
729 window W. ROW is the glyph row for which to display the bitmap; it
730 determines the vertical position at which the bitmap has to be
731 drawn. */
733 static void
734 w32_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
735 struct draw_fringe_bitmap_params *p)
737 struct frame *f = XFRAME (WINDOW_FRAME (w));
738 HDC hdc;
739 struct face *face = p->face;
740 int rowY;
742 hdc = get_frame_dc (f);
744 if (!p->overlay_p)
746 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
748 /* If the fringe is adjacent to the left (right) scroll bar of a
749 leftmost (rightmost, respectively) window, then extend its
750 background to the gap between the fringe and the bar. */
751 if ((WINDOW_LEFTMOST_P (w)
752 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
753 || (WINDOW_RIGHTMOST_P (w)
754 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
756 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
758 if (sb_width > 0)
760 int left = WINDOW_SCROLL_BAR_AREA_X (w);
761 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
762 * FRAME_COLUMN_WIDTH (f));
764 if (bx < 0)
766 /* Bitmap fills the fringe. */
767 if (left + width == p->x)
768 bx = left + sb_width;
769 else if (p->x + p->wd == left)
770 bx = left;
771 if (bx >= 0)
773 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
775 nx = width - sb_width;
776 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
777 row->y));
778 ny = row->visible_height;
781 else
783 if (left + width == bx)
785 bx = left + sb_width;
786 nx += width - sb_width;
788 else if (bx + nx == left)
789 nx += width - sb_width;
794 if (bx >= 0 && nx > 0)
795 w32_fill_area (f, hdc, face->background, bx, by, nx, ny);
798 /* Must clip because of partially visible lines. */
799 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
800 if (p->y < rowY)
802 /* Adjust position of "bottom aligned" bitmap on partially
803 visible last row. */
804 int oldY = row->y;
805 int oldVH = row->visible_height;
806 row->visible_height = p->h;
807 row->y -= rowY - p->y;
808 w32_clip_to_row (w, row, -1, hdc);
809 row->y = oldY;
810 row->visible_height = oldVH;
812 else
813 w32_clip_to_row (w, row, -1, hdc);
815 if (p->which && p->which < max_fringe_bmp)
817 HBITMAP pixmap = fringe_bmp[p->which];
818 HDC compat_hdc;
819 HANDLE horig_obj;
821 compat_hdc = CreateCompatibleDC (hdc);
823 SaveDC (hdc);
825 horig_obj = SelectObject (compat_hdc, pixmap);
827 /* Paint overlays transparently. */
828 if (p->overlay_p)
830 HBRUSH h_brush, h_orig_brush;
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);
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);
847 SelectObject (hdc, h_orig_brush);
848 DeleteObject (h_brush);
850 else
852 SetTextColor (hdc, face->background);
853 SetBkColor (hdc, (p->cursor_p
854 ? f->output_data.w32->cursor_pixel
855 : face->foreground));
857 BitBlt (hdc, p->x, p->y, p->wd, p->h,
858 compat_hdc, 0, p->dh,
859 SRCCOPY);
862 SelectObject (compat_hdc, horig_obj);
863 DeleteDC (compat_hdc);
864 RestoreDC (hdc, -1);
867 w32_set_clip_rectangle (hdc, NULL);
869 release_frame_dc (f, hdc);
872 static void
873 w32_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
875 if (which >= max_fringe_bmp)
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;
884 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
887 static void
888 w32_destroy_fringe_bitmap (int which)
890 if (which >= max_fringe_bmp)
891 return;
893 if (fringe_bmp[which])
894 DeleteObject (fringe_bmp[which]);
895 fringe_bmp[which] = 0;
900 /* This is called when starting Emacs and when restarting after
901 suspend. When starting Emacs, no window is mapped. And nothing
902 must be done to Emacs's own window if it is suspended (though that
903 rarely happens). */
905 static void
906 w32_set_terminal_modes (struct terminal *term)
910 /* This is called when exiting or suspending Emacs. Exiting will make
911 the W32 windows go away, and suspending requires no action. */
913 static void
914 w32_reset_terminal_modes (struct terminal *term)
920 /***********************************************************************
921 Display Iterator
922 ***********************************************************************/
924 /* Function prototypes of this page. */
926 static void x_set_glyph_string_clipping (struct glyph_string *);
927 static void x_set_glyph_string_gc (struct glyph_string *);
928 static void x_draw_glyph_string_background (struct glyph_string *,
929 int);
930 static void x_draw_glyph_string_foreground (struct glyph_string *);
931 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
932 static void x_draw_glyph_string_box (struct glyph_string *);
933 static void x_draw_glyph_string (struct glyph_string *);
934 static void x_set_cursor_gc (struct glyph_string *);
935 static void x_set_mode_line_face_gc (struct glyph_string *);
936 static void x_set_mouse_face_gc (struct glyph_string *);
937 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
938 static void w32_setup_relief_color (struct frame *, struct relief *,
939 double, int, COLORREF);
940 static void x_setup_relief_colors (struct glyph_string *);
941 static void x_draw_image_glyph_string (struct glyph_string *);
942 static void x_draw_image_relief (struct glyph_string *);
943 static void x_draw_image_foreground (struct glyph_string *);
944 static void w32_draw_image_foreground_1 (struct glyph_string *, HBITMAP);
945 static void x_clear_glyph_string_rect (struct glyph_string *, int,
946 int, int, int);
947 static void w32_draw_relief_rect (struct frame *, int, int, int, int,
948 int, int, int, int, int, int,
949 RECT *);
950 static void w32_draw_box_rect (struct glyph_string *, int, int, int, int,
951 int, int, int, RECT *);
954 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
955 face. */
957 static void
958 x_set_cursor_gc (struct glyph_string *s)
960 if (s->font == FRAME_FONT (s->f)
961 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
962 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
963 && !s->cmp)
964 s->gc = s->f->output_data.w32->cursor_gc;
965 else
967 /* Cursor on non-default face: must merge. */
968 XGCValues xgcv;
969 unsigned long mask;
971 xgcv.background = s->f->output_data.w32->cursor_pixel;
972 xgcv.foreground = s->face->background;
974 /* If the glyph would be invisible, try a different foreground. */
975 if (xgcv.foreground == xgcv.background)
976 xgcv.foreground = s->face->foreground;
977 if (xgcv.foreground == xgcv.background)
978 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
979 if (xgcv.foreground == xgcv.background)
980 xgcv.foreground = s->face->foreground;
982 /* Make sure the cursor is distinct from text in this face. */
983 if (xgcv.background == s->face->background
984 && xgcv.foreground == s->face->foreground)
986 xgcv.background = s->face->foreground;
987 xgcv.foreground = s->face->background;
990 IF_DEBUG (x_check_font (s->f, s->font));
991 xgcv.font = s->font;
992 mask = GCForeground | GCBackground | GCFont;
994 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
995 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
996 mask, &xgcv);
997 else
998 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
999 = XCreateGC (NULL, s->window, mask, &xgcv);
1001 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1006 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1008 static void
1009 x_set_mouse_face_gc (struct glyph_string *s)
1011 int face_id;
1012 struct face *face;
1014 /* What face has to be used last for the mouse face? */
1015 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
1016 face = FACE_FROM_ID (s->f, face_id);
1017 if (face == NULL)
1018 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1020 if (s->first_glyph->type == CHAR_GLYPH)
1021 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1022 else
1023 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1024 s->face = FACE_FROM_ID (s->f, face_id);
1025 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1027 /* If font in this face is same as S->font, use it. */
1028 if (s->font == s->face->font)
1029 s->gc = s->face->gc;
1030 else
1032 /* Otherwise construct scratch_cursor_gc with values from FACE
1033 but font FONT. */
1034 XGCValues xgcv;
1035 unsigned long mask;
1037 xgcv.background = s->face->background;
1038 xgcv.foreground = s->face->foreground;
1039 IF_DEBUG (x_check_font (s->f, s->font));
1040 xgcv.font = s->font;
1041 mask = GCForeground | GCBackground | GCFont;
1043 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1044 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1045 mask, &xgcv);
1046 else
1047 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1048 = XCreateGC (NULL, s->window, mask, &xgcv);
1050 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1053 xassert (s->gc != 0);
1057 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1058 Faces to use in the mode line have already been computed when the
1059 matrix was built, so there isn't much to do, here. */
1061 static INLINE void
1062 x_set_mode_line_face_gc (struct glyph_string *s)
1064 s->gc = s->face->gc;
1068 /* Set S->gc of glyph string S for drawing that glyph string. Set
1069 S->stippled_p to a non-zero value if the face of S has a stipple
1070 pattern. */
1072 static INLINE void
1073 x_set_glyph_string_gc (struct glyph_string *s)
1075 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1077 if (s->hl == DRAW_NORMAL_TEXT)
1079 s->gc = s->face->gc;
1080 s->stippled_p = s->face->stipple != 0;
1082 else if (s->hl == DRAW_INVERSE_VIDEO)
1084 x_set_mode_line_face_gc (s);
1085 s->stippled_p = s->face->stipple != 0;
1087 else if (s->hl == DRAW_CURSOR)
1089 x_set_cursor_gc (s);
1090 s->stippled_p = 0;
1092 else if (s->hl == DRAW_MOUSE_FACE)
1094 x_set_mouse_face_gc (s);
1095 s->stippled_p = s->face->stipple != 0;
1097 else if (s->hl == DRAW_IMAGE_RAISED
1098 || s->hl == DRAW_IMAGE_SUNKEN)
1100 s->gc = s->face->gc;
1101 s->stippled_p = s->face->stipple != 0;
1103 else
1105 s->gc = s->face->gc;
1106 s->stippled_p = s->face->stipple != 0;
1109 /* GC must have been set. */
1110 xassert (s->gc != 0);
1114 /* Set clipping for output of glyph string S. S may be part of a mode
1115 line or menu if we don't have X toolkit support. */
1117 static INLINE void
1118 x_set_glyph_string_clipping (struct glyph_string *s)
1120 RECT *r = s->clip;
1121 int n = get_glyph_string_clip_rects (s, r, 2);
1123 if (n == 1)
1124 w32_set_clip_rectangle (s->hdc, r);
1125 else if (n > 1)
1127 HRGN full_clip, clip1, clip2;
1128 clip1 = CreateRectRgnIndirect (r);
1129 clip2 = CreateRectRgnIndirect (r + 1);
1130 if (CombineRgn (full_clip, clip1, clip2, RGN_OR) != ERROR)
1132 SelectClipRgn (s->hdc, full_clip);
1134 DeleteObject (clip1);
1135 DeleteObject (clip2);
1136 DeleteObject (full_clip);
1138 s->num_clips = n;
1141 /* Set SRC's clipping for output of glyph string DST. This is called
1142 when we are drawing DST's left_overhang or right_overhang only in
1143 the area of SRC. */
1145 static void
1146 x_set_glyph_string_clipping_exactly (struct glyph_string *src,
1147 struct glyph_string *dst)
1149 RECT r;
1151 r.left = src->x;
1152 r.right = r.left + src->width;
1153 r.top = src->y;
1154 r.bottom = r.top + src->height;
1155 dst->clip[0] = r;
1156 dst->num_clips = 1;
1157 w32_set_clip_rectangle (dst->hdc, &r);
1160 /* RIF:
1161 Compute left and right overhang of glyph string S. */
1163 static void
1164 w32_compute_glyph_string_overhangs (struct glyph_string *s)
1166 if (s->cmp == NULL
1167 && s->first_glyph->type == CHAR_GLYPH
1168 && !s->font_not_found_p)
1170 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1171 struct font *font = s->font;
1172 struct font_metrics metrics;
1173 int i;
1175 for (i = 0; i < s->nchars; i++)
1176 code[i] = s->char2b[i];
1177 font->driver->text_extents (font, code, s->nchars, &metrics);
1178 s->right_overhang = (metrics.rbearing > metrics.width
1179 ? metrics.rbearing - metrics.width : 0);
1180 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1182 else if (s->cmp)
1184 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1185 s->left_overhang = -s->cmp->lbearing;
1189 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1191 static INLINE void
1192 x_clear_glyph_string_rect (struct glyph_string *s,
1193 int x, int y, int w, int h)
1195 int real_x = x;
1196 int real_y = y;
1197 int real_w = w;
1198 int real_h = h;
1199 #if 0
1200 /* Take clipping into account. */
1201 if (s->gc->clip_mask == Rect)
1203 real_x = max (real_x, s->gc->clip_rectangle.left);
1204 real_y = max (real_y, s->gc->clip_rectangle.top);
1205 real_w = min (real_w, s->gc->clip_rectangle.right
1206 - s->gc->clip_rectangle.left);
1207 real_h = min (real_h, s->gc->clip_rectangle.bottom
1208 - s->gc->clip_rectangle.top);
1210 #endif
1211 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1212 real_w, real_h);
1216 /* Draw the background of glyph_string S. If S->background_filled_p
1217 is non-zero don't draw it. FORCE_P non-zero means draw the
1218 background even if it wouldn't be drawn normally. This is used
1219 when a string preceding S draws into the background of S, or S
1220 contains the first component of a composition. */
1222 static void
1223 x_draw_glyph_string_background (struct glyph_string *s, int force_p)
1225 /* Nothing to do if background has already been drawn or if it
1226 shouldn't be drawn in the first place. */
1227 if (!s->background_filled_p)
1229 int box_line_width = max (s->face->box_line_width, 0);
1231 #if 0 /* TODO: stipple */
1232 if (s->stippled_p)
1234 /* Fill background with a stipple pattern. */
1235 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1236 XFillRectangle (s->display, s->window, s->gc, s->x,
1237 s->y + box_line_width,
1238 s->background_width,
1239 s->height - 2 * box_line_width);
1240 XSetFillStyle (s->display, s->gc, FillSolid);
1241 s->background_filled_p = 1;
1243 else
1244 #endif
1245 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1246 || s->font_not_found_p
1247 || s->extends_to_end_of_line_p
1248 || force_p)
1250 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1251 s->background_width,
1252 s->height - 2 * box_line_width);
1253 s->background_filled_p = 1;
1259 /* Draw the foreground of glyph string S. */
1261 static void
1262 x_draw_glyph_string_foreground (struct glyph_string *s)
1264 int i, x;
1266 /* If first glyph of S has a left box line, start drawing the text
1267 of S to the right of that box line. */
1268 if (s->face->box != FACE_NO_BOX
1269 && s->first_glyph->left_box_line_p)
1270 x = s->x + eabs (s->face->box_line_width);
1271 else
1272 x = s->x;
1274 SetTextColor (s->hdc, s->gc->foreground);
1275 SetBkColor (s->hdc, s->gc->background);
1276 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1278 /* Draw characters of S as rectangles if S's font could not be
1279 loaded. */
1280 if (s->font_not_found_p)
1282 for (i = 0; i < s->nchars; ++i)
1284 struct glyph *g = s->first_glyph + i;
1286 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1287 s->height - 1);
1288 x += g->pixel_width;
1291 else
1293 struct font *font = s->font;
1294 int boff = font->baseline_offset;
1295 int y;
1296 HFONT old_font;
1298 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1300 if (font->vertical_centering)
1301 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1303 y = s->ybase - boff;
1304 if (s->for_overlaps
1305 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1306 font->driver->draw (s, 0, s->nchars, x, y, 0);
1307 else
1308 font->driver->draw (s, 0, s->nchars, x, y, 1);
1309 if (s->face->overstrike)
1310 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1312 SelectObject (s->hdc, old_font);
1316 /* Draw the foreground of composite glyph string S. */
1318 static void
1319 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1321 int i, j, x;
1322 struct font *font = s->font;
1324 /* If first glyph of S has a left box line, start drawing the text
1325 of S to the right of that box line. */
1326 if (s->face && s->face->box != FACE_NO_BOX
1327 && s->first_glyph->left_box_line_p)
1328 x = s->x + eabs (s->face->box_line_width);
1329 else
1330 x = s->x;
1332 /* S is a glyph string for a composition. S->cmp_from is the index
1333 of the first character drawn for glyphs of this composition.
1334 S->cmp_from == 0 means we are drawing the very first character of
1335 this composition. */
1337 SetTextColor (s->hdc, s->gc->foreground);
1338 SetBkColor (s->hdc, s->gc->background);
1339 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1341 /* Draw a rectangle for the composition if the font for the very
1342 first character of the composition could not be loaded. */
1343 if (s->font_not_found_p)
1345 if (s->cmp_from == 0)
1346 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1347 s->height - 1);
1349 else if (! s->first_glyph->u.cmp.automatic)
1351 int y = s->ybase;
1352 int width = 0;
1353 HFONT old_font;
1355 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1357 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1358 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1360 int xx = x + s->cmp->offsets[j * 2];
1361 int yy = y - s->cmp->offsets[j * 2 + 1];
1363 font->driver->draw (s, j, j + 1, xx, yy, 0);
1364 if (s->face->overstrike)
1365 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1367 SelectObject (s->hdc, old_font);
1369 else
1371 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1372 Lisp_Object glyph;
1373 int y = s->ybase;
1374 int width = 0;
1375 HFONT old_font;
1377 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1379 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1381 glyph = LGSTRING_GLYPH (gstring, i);
1382 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1383 width += LGLYPH_WIDTH (glyph);
1384 else
1386 int xoff, yoff, wadjust;
1388 if (j < i)
1390 font->driver->draw (s, j, i, x, y, 0);
1391 x += width;
1393 xoff = LGLYPH_XOFF (glyph);
1394 yoff = LGLYPH_YOFF (glyph);
1395 wadjust = LGLYPH_WADJUST (glyph);
1396 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1397 x += wadjust;
1398 j = i + 1;
1399 width = 0;
1402 if (j < i)
1403 font->driver->draw (s, j, i, x, y, 0);
1405 SelectObject (s->hdc, old_font);
1410 /* Brightness beyond which a color won't have its highlight brightness
1411 boosted.
1413 Nominally, highlight colors for `3d' faces are calculated by
1414 brightening an object's color by a constant scale factor, but this
1415 doesn't yield good results for dark colors, so for colors who's
1416 brightness is less than this value (on a scale of 0-255) have to
1417 use an additional additive factor.
1419 The value here is set so that the default menu-bar/mode-line color
1420 (grey75) will not have its highlights changed at all. */
1421 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1424 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1425 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1426 If this produces the same color as COLOR, try a color where all RGB
1427 values have DELTA added. Return the allocated color in *COLOR.
1428 DISPLAY is the X display, CMAP is the colormap to operate on.
1429 Value is non-zero if successful. */
1431 static int
1432 w32_alloc_lighter_color (struct frame *f, COLORREF *color,
1433 double factor, int delta)
1435 COLORREF new;
1436 long bright;
1438 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1439 delta /= 256;
1441 /* Change RGB values by specified FACTOR. Avoid overflow! */
1442 xassert (factor >= 0);
1443 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1444 min (0xff, factor * GetGValue (*color)),
1445 min (0xff, factor * GetBValue (*color)));
1447 /* Calculate brightness of COLOR. */
1448 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1449 + GetBValue (*color)) / 6;
1451 /* We only boost colors that are darker than
1452 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1453 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1454 /* Make an additive adjustment to NEW, because it's dark enough so
1455 that scaling by FACTOR alone isn't enough. */
1457 /* How far below the limit this color is (0 - 1, 1 being darker). */
1458 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1459 /* The additive adjustment. */
1460 int min_delta = delta * dimness * factor / 2;
1462 if (factor < 1)
1463 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1464 max (0, min (0xff, min_delta - GetGValue (*color))),
1465 max (0, min (0xff, min_delta - GetBValue (*color))));
1466 else
1467 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1468 max (0, min (0xff, min_delta + GetGValue (*color))),
1469 max (0, min (0xff, min_delta + GetBValue (*color))));
1472 if (new == *color)
1473 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1474 max (0, min (0xff, delta + GetGValue (*color))),
1475 max (0, min (0xff, delta + GetBValue (*color))));
1477 /* TODO: Map to palette and retry with delta if same? */
1478 /* TODO: Free colors (if using palette)? */
1480 if (new == *color)
1481 return 0;
1483 *color = new;
1485 return 1;
1488 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1489 colors in COLORS. On W32, we no longer try to map colors to
1490 a palette. */
1491 void
1492 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1494 int i;
1496 for (i = 0; i < ncolors; i++)
1498 DWORD pixel = colors[i].pixel;
1499 /* Convert to a 16 bit value in range 0 - 0xffff. */
1500 colors[i].red = GetRValue (pixel) * 257;
1501 colors[i].green = GetGValue (pixel) * 257;
1502 colors[i].blue = GetBValue (pixel) * 257;
1506 void
1507 x_query_color (struct frame *f, XColor *color)
1509 x_query_colors (f, color, 1);
1513 /* Set up the foreground color for drawing relief lines of glyph
1514 string S. RELIEF is a pointer to a struct relief containing the GC
1515 with which lines will be drawn. Use a color that is FACTOR or
1516 DELTA lighter or darker than the relief's background which is found
1517 in S->f->output_data.x->relief_background. If such a color cannot
1518 be allocated, use DEFAULT_PIXEL, instead. */
1520 static void
1521 w32_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1522 int delta, COLORREF default_pixel)
1524 XGCValues xgcv;
1525 struct w32_output *di = f->output_data.w32;
1526 unsigned long mask = GCForeground;
1527 COLORREF pixel;
1528 COLORREF background = di->relief_background;
1529 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
1531 /* TODO: Free colors (if using palette)? */
1533 /* Allocate new color. */
1534 xgcv.foreground = default_pixel;
1535 pixel = background;
1536 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1538 relief->allocated_p = 1;
1539 xgcv.foreground = relief->pixel = pixel;
1542 if (relief->gc == 0)
1544 #if 0 /* TODO: stipple */
1545 xgcv.stipple = dpyinfo->gray;
1546 mask |= GCStipple;
1547 #endif
1548 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1550 else
1551 XChangeGC (NULL, relief->gc, mask, &xgcv);
1555 /* Set up colors for the relief lines around glyph string S. */
1557 static void
1558 x_setup_relief_colors (struct glyph_string *s)
1560 struct w32_output *di = s->f->output_data.w32;
1561 COLORREF color;
1563 if (s->face->use_box_color_for_shadows_p)
1564 color = s->face->box_color;
1565 else if (s->first_glyph->type == IMAGE_GLYPH
1566 && s->img->pixmap
1567 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1568 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1569 else
1570 color = s->gc->background;
1572 if (di->white_relief.gc == 0
1573 || color != di->relief_background)
1575 di->relief_background = color;
1576 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1577 WHITE_PIX_DEFAULT (s->f));
1578 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1579 BLACK_PIX_DEFAULT (s->f));
1584 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1585 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1586 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1587 relief. LEFT_P non-zero means draw a relief on the left side of
1588 the rectangle. RIGHT_P non-zero means draw a relief on the right
1589 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1590 when drawing. */
1592 static void
1593 w32_draw_relief_rect (struct frame *f,
1594 int left_x, int top_y, int right_x, int bottom_y, int width,
1595 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1596 RECT *clip_rect)
1598 int i;
1599 XGCValues gc;
1600 HDC hdc = get_frame_dc (f);
1602 if (raised_p)
1603 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1604 else
1605 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1607 w32_set_clip_rectangle (hdc, clip_rect);
1609 /* Top. */
1610 if (top_p)
1611 for (i = 0; i < width; ++i)
1612 w32_fill_area (f, hdc, gc.foreground,
1613 left_x + i * left_p, top_y + i,
1614 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1616 /* Left. */
1617 if (left_p)
1618 for (i = 0; i < width; ++i)
1619 w32_fill_area (f, hdc, gc.foreground,
1620 left_x + i, top_y + i, 1,
1621 bottom_y - top_y - 2 * i + 1);
1623 if (raised_p)
1624 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1625 else
1626 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1628 /* Bottom. */
1629 if (bot_p)
1630 for (i = 0; i < width; ++i)
1631 w32_fill_area (f, hdc, gc.foreground,
1632 left_x + i * left_p, bottom_y - i,
1633 right_x - left_x - i * (left_p + right_p) + 1, 1);
1635 /* Right. */
1636 if (right_p)
1637 for (i = 0; i < width; ++i)
1638 w32_fill_area (f, hdc, gc.foreground,
1639 right_x - i, top_y + i + 1, 1,
1640 bottom_y - top_y - 2 * i - 1);
1642 w32_set_clip_rectangle (hdc, NULL);
1644 release_frame_dc (f, hdc);
1648 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1649 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1650 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1651 left side of the rectangle. RIGHT_P non-zero means draw a line
1652 on the right side of the rectangle. CLIP_RECT is the clipping
1653 rectangle to use when drawing. */
1655 static void
1656 w32_draw_box_rect (struct glyph_string *s,
1657 int left_x, int top_y, int right_x, int bottom_y, int width,
1658 int left_p, int right_p, RECT *clip_rect)
1660 w32_set_clip_rectangle (s->hdc, clip_rect);
1662 /* Top. */
1663 w32_fill_area (s->f, s->hdc, s->face->box_color,
1664 left_x, top_y, right_x - left_x + 1, width);
1666 /* Left. */
1667 if (left_p)
1669 w32_fill_area (s->f, s->hdc, s->face->box_color,
1670 left_x, top_y, width, bottom_y - top_y + 1);
1673 /* Bottom. */
1674 w32_fill_area (s->f, s->hdc, s->face->box_color,
1675 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1677 /* Right. */
1678 if (right_p)
1680 w32_fill_area (s->f, s->hdc, s->face->box_color,
1681 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1684 w32_set_clip_rectangle (s->hdc, NULL);
1688 /* Draw a box around glyph string S. */
1690 static void
1691 x_draw_glyph_string_box (struct glyph_string *s)
1693 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1694 int left_p, right_p;
1695 struct glyph *last_glyph;
1696 RECT clip_rect;
1698 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1699 ? WINDOW_RIGHT_EDGE_X (s->w)
1700 : window_box_right (s->w, s->area));
1702 /* The glyph that may have a right box line. */
1703 last_glyph = (s->cmp || s->img
1704 ? s->first_glyph
1705 : s->first_glyph + s->nchars - 1);
1707 width = eabs (s->face->box_line_width);
1708 raised_p = s->face->box == FACE_RAISED_BOX;
1709 left_x = s->x;
1710 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1711 ? last_x - 1
1712 : min (last_x, s->x + s->background_width) - 1));
1713 top_y = s->y;
1714 bottom_y = top_y + s->height - 1;
1716 left_p = (s->first_glyph->left_box_line_p
1717 || (s->hl == DRAW_MOUSE_FACE
1718 && (s->prev == NULL
1719 || s->prev->hl != s->hl)));
1720 right_p = (last_glyph->right_box_line_p
1721 || (s->hl == DRAW_MOUSE_FACE
1722 && (s->next == NULL
1723 || s->next->hl != s->hl)));
1725 get_glyph_string_clip_rect (s, &clip_rect);
1727 if (s->face->box == FACE_SIMPLE_BOX)
1728 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1729 left_p, right_p, &clip_rect);
1730 else
1732 x_setup_relief_colors (s);
1733 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1734 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1739 /* Draw foreground of image glyph string S. */
1741 static void
1742 x_draw_image_foreground (struct glyph_string *s)
1744 int x = s->x;
1745 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1747 /* If first glyph of S has a left box line, start drawing it to the
1748 right of that line. */
1749 if (s->face->box != FACE_NO_BOX
1750 && s->first_glyph->left_box_line_p
1751 && s->slice.x == 0)
1752 x += eabs (s->face->box_line_width);
1754 /* If there is a margin around the image, adjust x- and y-position
1755 by that margin. */
1756 if (s->slice.x == 0)
1757 x += s->img->hmargin;
1758 if (s->slice.y == 0)
1759 y += s->img->vmargin;
1761 SaveDC (s->hdc);
1763 if (s->img->pixmap)
1765 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1766 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1767 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1768 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1769 SetBkColor (compat_hdc, RGB (255, 255, 255));
1770 SetTextColor (s->hdc, RGB (0, 0, 0));
1771 x_set_glyph_string_clipping (s);
1773 if (s->img->mask)
1775 HDC mask_dc = CreateCompatibleDC (s->hdc);
1776 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1778 SetTextColor (s->hdc, RGB (255, 255, 255));
1779 SetBkColor (s->hdc, RGB (0, 0, 0));
1781 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1782 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1783 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1784 mask_dc, s->slice.x, s->slice.y, SRCAND);
1785 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1786 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1788 SelectObject (mask_dc, mask_orig_obj);
1789 DeleteDC (mask_dc);
1791 else
1793 SetTextColor (s->hdc, s->gc->foreground);
1794 SetBkColor (s->hdc, s->gc->background);
1796 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1797 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1799 /* When the image has a mask, we can expect that at
1800 least part of a mouse highlight or a block cursor will
1801 be visible. If the image doesn't have a mask, make
1802 a block cursor visible by drawing a rectangle around
1803 the image. I believe it's looking better if we do
1804 nothing here for mouse-face. */
1805 if (s->hl == DRAW_CURSOR)
1807 int r = s->img->relief;
1808 if (r < 0) r = -r;
1809 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1810 s->slice.width + r*2 - 1,
1811 s->slice.height + r*2 - 1);
1815 w32_set_clip_rectangle (s->hdc, NULL);
1816 SelectObject (s->hdc, orig_brush);
1817 DeleteObject (fg_brush);
1818 SelectObject (compat_hdc, orig_obj);
1819 DeleteDC (compat_hdc);
1821 else
1822 w32_draw_rectangle (s->hdc, s->gc, x, y,
1823 s->slice.width - 1, s->slice.height - 1);
1825 RestoreDC (s->hdc ,-1);
1829 /* Draw a relief around the image glyph string S. */
1831 static void
1832 x_draw_image_relief (struct glyph_string *s)
1834 int x0, y0, x1, y1, thick, raised_p;
1835 RECT r;
1836 int x = s->x;
1837 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1839 /* If first glyph of S has a left box line, start drawing it to the
1840 right of that line. */
1841 if (s->face->box != FACE_NO_BOX
1842 && s->first_glyph->left_box_line_p
1843 && s->slice.x == 0)
1844 x += eabs (s->face->box_line_width);
1846 /* If there is a margin around the image, adjust x- and y-position
1847 by that margin. */
1848 if (s->slice.x == 0)
1849 x += s->img->hmargin;
1850 if (s->slice.y == 0)
1851 y += s->img->vmargin;
1853 if (s->hl == DRAW_IMAGE_SUNKEN
1854 || s->hl == DRAW_IMAGE_RAISED)
1856 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1857 raised_p = s->hl == DRAW_IMAGE_RAISED;
1859 else
1861 thick = eabs (s->img->relief);
1862 raised_p = s->img->relief > 0;
1865 x0 = x - thick;
1866 y0 = y - thick;
1867 x1 = x + s->slice.width + thick - 1;
1868 y1 = y + s->slice.height + thick - 1;
1870 x_setup_relief_colors (s);
1871 get_glyph_string_clip_rect (s, &r);
1872 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
1873 s->slice.y == 0,
1874 s->slice.y + s->slice.height == s->img->height,
1875 s->slice.x == 0,
1876 s->slice.x + s->slice.width == s->img->width,
1877 &r);
1881 /* Draw the foreground of image glyph string S to PIXMAP. */
1883 static void
1884 w32_draw_image_foreground_1 (struct glyph_string *s, HBITMAP pixmap)
1886 HDC hdc = CreateCompatibleDC (s->hdc);
1887 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1888 int x = 0;
1889 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1891 /* If first glyph of S has a left box line, start drawing it to the
1892 right of that line. */
1893 if (s->face->box != FACE_NO_BOX
1894 && s->first_glyph->left_box_line_p
1895 && s->slice.x == 0)
1896 x += eabs (s->face->box_line_width);
1898 /* If there is a margin around the image, adjust x- and y-position
1899 by that margin. */
1900 if (s->slice.x == 0)
1901 x += s->img->hmargin;
1902 if (s->slice.y == 0)
1903 y += s->img->vmargin;
1905 if (s->img->pixmap)
1907 HDC compat_hdc = CreateCompatibleDC (hdc);
1908 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1909 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1910 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1912 if (s->img->mask)
1914 HDC mask_dc = CreateCompatibleDC (hdc);
1915 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1917 SetTextColor (hdc, RGB (0, 0, 0));
1918 SetBkColor (hdc, RGB (255, 255, 255));
1919 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1920 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1921 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1922 mask_dc, s->slice.x, s->slice.y, SRCAND);
1923 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1924 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1926 SelectObject (mask_dc, mask_orig_obj);
1927 DeleteDC (mask_dc);
1929 else
1931 SetTextColor (hdc, s->gc->foreground);
1932 SetBkColor (hdc, s->gc->background);
1934 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1935 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1937 /* When the image has a mask, we can expect that at
1938 least part of a mouse highlight or a block cursor will
1939 be visible. If the image doesn't have a mask, make
1940 a block cursor visible by drawing a rectangle around
1941 the image. I believe it's looking better if we do
1942 nothing here for mouse-face. */
1943 if (s->hl == DRAW_CURSOR)
1945 int r = s->img->relief;
1946 if (r < 0) r = -r;
1947 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
1948 s->slice.width + r*2 - 1,
1949 s->slice.height + r*2 - 1);
1953 SelectObject (hdc, orig_brush);
1954 DeleteObject (fg_brush);
1955 SelectObject (compat_hdc, orig_obj);
1956 DeleteDC (compat_hdc);
1958 else
1959 w32_draw_rectangle (hdc, s->gc, x, y,
1960 s->slice.width - 1, s->slice.height - 1);
1962 SelectObject (hdc, orig_hdc_obj);
1963 DeleteDC (hdc);
1967 /* Draw part of the background of glyph string S. X, Y, W, and H
1968 give the rectangle to draw. */
1970 static void
1971 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
1973 #if 0 /* TODO: stipple */
1974 if (s->stippled_p)
1976 /* Fill background with a stipple pattern. */
1977 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1978 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1979 XSetFillStyle (s->display, s->gc, FillSolid);
1981 else
1982 #endif
1983 x_clear_glyph_string_rect (s, x, y, w, h);
1987 /* Draw image glyph string S.
1989 s->y
1990 s->x +-------------------------
1991 | s->face->box
1993 | +-------------------------
1994 | | s->img->vmargin
1996 | | +-------------------
1997 | | | the image
2001 static void
2002 x_draw_image_glyph_string (struct glyph_string *s)
2004 int x, y;
2005 int box_line_hwidth = eabs (s->face->box_line_width);
2006 int box_line_vwidth = max (s->face->box_line_width, 0);
2007 int height;
2008 HBITMAP pixmap = 0;
2010 height = s->height - 2 * box_line_vwidth;
2012 /* Fill background with face under the image. Do it only if row is
2013 taller than image or if image has a clip mask to reduce
2014 flickering. */
2015 s->stippled_p = s->face->stipple != 0;
2016 if (height > s->slice.height
2017 || s->img->hmargin
2018 || s->img->vmargin
2019 || s->img->mask
2020 || s->img->pixmap == 0
2021 || s->width != s->background_width)
2023 x = s->x;
2024 if (s->first_glyph->left_box_line_p
2025 && s->slice.x == 0)
2026 x += box_line_hwidth;
2028 y = s->y;
2029 if (s->slice.y == 0)
2030 y += box_line_vwidth;
2032 #if 0 /* TODO: figure out if we need to do this on Windows. */
2033 if (s->img->mask)
2035 /* Create a pixmap as large as the glyph string. Fill it
2036 with the background color. Copy the image to it, using
2037 its mask. Copy the temporary pixmap to the display. */
2038 Screen *screen = FRAME_X_SCREEN (s->f);
2039 int depth = DefaultDepthOfScreen (screen);
2041 /* Create a pixmap as large as the glyph string. */
2042 pixmap = XCreatePixmap (s->display, s->window,
2043 s->background_width,
2044 s->height, depth);
2046 /* Don't clip in the following because we're working on the
2047 pixmap. */
2048 XSetClipMask (s->display, s->gc, None);
2050 /* Fill the pixmap with the background color/stipple. */
2051 if (s->stippled_p)
2053 /* Fill background with a stipple pattern. */
2054 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2055 XFillRectangle (s->display, pixmap, s->gc,
2056 0, 0, s->background_width, s->height);
2057 XSetFillStyle (s->display, s->gc, FillSolid);
2059 else
2061 XGCValues xgcv;
2062 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2063 &xgcv);
2064 XSetForeground (s->display, s->gc, xgcv.background);
2065 XFillRectangle (s->display, pixmap, s->gc,
2066 0, 0, s->background_width, s->height);
2067 XSetForeground (s->display, s->gc, xgcv.foreground);
2070 else
2071 #endif
2072 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2074 s->background_filled_p = 1;
2077 /* Draw the foreground. */
2078 if (pixmap != 0)
2080 w32_draw_image_foreground_1 (s, pixmap);
2081 x_set_glyph_string_clipping (s);
2083 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2084 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2085 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2086 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2088 SetTextColor (s->hdc, s->gc->foreground);
2089 SetBkColor (s->hdc, s->gc->background);
2090 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2091 compat_hdc, 0, 0, SRCCOPY);
2093 SelectObject (s->hdc, orig_brush);
2094 DeleteObject (fg_brush);
2095 SelectObject (compat_hdc, orig_obj);
2096 DeleteDC (compat_hdc);
2098 DeleteObject (pixmap);
2099 pixmap = 0;
2101 else
2102 x_draw_image_foreground (s);
2104 /* If we must draw a relief around the image, do it. */
2105 if (s->img->relief
2106 || s->hl == DRAW_IMAGE_RAISED
2107 || s->hl == DRAW_IMAGE_SUNKEN)
2108 x_draw_image_relief (s);
2112 /* Draw stretch glyph string S. */
2114 static void
2115 x_draw_stretch_glyph_string (struct glyph_string *s)
2117 xassert (s->first_glyph->type == STRETCH_GLYPH);
2119 if (s->hl == DRAW_CURSOR
2120 && !x_stretch_cursor_p)
2122 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2123 as wide as the stretch glyph. */
2124 int width, background_width = s->background_width;
2125 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2127 if (x < left_x)
2129 background_width -= left_x - x;
2130 x = left_x;
2132 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2134 /* Draw cursor. */
2135 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2137 /* Clear rest using the GC of the original non-cursor face. */
2138 if (width < background_width)
2140 XGCValues *gc = s->face->gc;
2141 int y = s->y;
2142 int w = background_width - width, h = s->height;
2143 RECT r;
2144 HDC hdc = s->hdc;
2146 x += width;
2147 if (s->row->mouse_face_p
2148 && cursor_in_mouse_face_p (s->w))
2150 x_set_mouse_face_gc (s);
2151 gc = s->gc;
2153 else
2154 gc = s->face->gc;
2156 get_glyph_string_clip_rect (s, &r);
2157 w32_set_clip_rectangle (hdc, &r);
2159 #if 0 /* TODO: stipple */
2160 if (s->face->stipple)
2162 /* Fill background with a stipple pattern. */
2163 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2164 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2165 XSetFillStyle (s->display, gc, FillSolid);
2167 else
2168 #endif
2170 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2174 else if (!s->background_filled_p)
2176 int background_width = s->background_width;
2177 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2179 /* Don't draw into left margin, fringe or scrollbar area
2180 except for header line and mode line. */
2181 if (x < left_x && !s->row->mode_line_p)
2183 background_width -= left_x - x;
2184 x = left_x;
2186 if (background_width > 0)
2187 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2190 s->background_filled_p = 1;
2194 /* Draw glyph string S. */
2196 static void
2197 x_draw_glyph_string (struct glyph_string *s)
2199 int relief_drawn_p = 0;
2201 /* If S draws into the background of its successor, draw the
2202 background of the successor first so that S can draw into it.
2203 This makes S->next use XDrawString instead of XDrawImageString. */
2204 if (s->next && s->right_overhang && !s->for_overlaps)
2206 int width;
2207 struct glyph_string *next;
2208 for (width = 0, next = s->next;
2209 next && width < s->right_overhang;
2210 width += next->width, next = next->next)
2211 if (next->first_glyph->type != IMAGE_GLYPH)
2213 x_set_glyph_string_gc (next);
2214 x_set_glyph_string_clipping (next);
2215 if (next->first_glyph->type == STRETCH_GLYPH)
2216 x_draw_stretch_glyph_string (next);
2217 else
2218 x_draw_glyph_string_background (next, 1);
2219 next->num_clips = 0;
2223 /* Set up S->gc, set clipping and draw S. */
2224 x_set_glyph_string_gc (s);
2226 /* Draw relief (if any) in advance for char/composition so that the
2227 glyph string can be drawn over it. */
2228 if (!s->for_overlaps
2229 && s->face->box != FACE_NO_BOX
2230 && (s->first_glyph->type == CHAR_GLYPH
2231 || s->first_glyph->type == COMPOSITE_GLYPH))
2234 x_set_glyph_string_clipping (s);
2235 x_draw_glyph_string_background (s, 1);
2236 x_draw_glyph_string_box (s);
2237 x_set_glyph_string_clipping (s);
2238 relief_drawn_p = 1;
2240 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2241 && !s->clip_tail
2242 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2243 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2244 /* We must clip just this glyph. left_overhang part has already
2245 drawn when s->prev was drawn, and right_overhang part will be
2246 drawn later when s->next is drawn. */
2247 x_set_glyph_string_clipping_exactly (s, s);
2248 else
2249 x_set_glyph_string_clipping (s);
2251 switch (s->first_glyph->type)
2253 case IMAGE_GLYPH:
2254 x_draw_image_glyph_string (s);
2255 break;
2257 case STRETCH_GLYPH:
2258 x_draw_stretch_glyph_string (s);
2259 break;
2261 case CHAR_GLYPH:
2262 if (s->for_overlaps)
2263 s->background_filled_p = 1;
2264 else
2265 x_draw_glyph_string_background (s, 0);
2266 x_draw_glyph_string_foreground (s);
2267 break;
2269 case COMPOSITE_GLYPH:
2270 if (s->for_overlaps || (s->cmp_from > 0
2271 && ! s->first_glyph->u.cmp.automatic))
2272 s->background_filled_p = 1;
2273 else
2274 x_draw_glyph_string_background (s, 1);
2275 x_draw_composite_glyph_string_foreground (s);
2276 break;
2278 default:
2279 abort ();
2282 if (!s->for_overlaps)
2284 /* Draw underline. */
2285 if (s->face->underline_p)
2287 unsigned long thickness, position;
2288 int y;
2290 if (s->prev && s->prev->face->underline_p)
2292 /* We use the same underline style as the previous one. */
2293 thickness = s->prev->underline_thickness;
2294 position = s->prev->underline_position;
2296 else
2298 /* Get the underline thickness. Default is 1 pixel. */
2299 if (s->font && s->font->underline_thickness > 0)
2300 thickness = s->font->underline_thickness;
2301 else
2302 thickness = 1;
2303 if (x_underline_at_descent_line)
2304 position = (s->height - thickness) - (s->ybase - s->y);
2305 else
2307 /* Get the underline position. This is the recommended
2308 vertical offset in pixels from the baseline to the top of
2309 the underline. This is a signed value according to the
2310 specs, and its default is
2312 ROUND ((maximum_descent) / 2), with
2313 ROUND (x) = floor (x + 0.5) */
2315 if (x_use_underline_position_properties
2316 && s->font && s->font->underline_position >= 0)
2317 position = s->font->underline_position;
2318 else if (s->font)
2319 position = (s->font->descent + 1) / 2;
2321 position = max (position, underline_minimum_offset);
2323 /* Check the sanity of thickness and position. We should
2324 avoid drawing underline out of the current line area. */
2325 if (s->y + s->height <= s->ybase + position)
2326 position = (s->height - 1) - (s->ybase - s->y);
2327 if (s->y + s->height < s->ybase + position + thickness)
2328 thickness = (s->y + s->height) - (s->ybase + position);
2329 s->underline_thickness = thickness;
2330 s->underline_position =position;
2331 y = s->ybase + position;
2332 if (s->face->underline_defaulted_p)
2334 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2335 y, s->width, 1);
2337 else
2339 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2340 y, s->width, 1);
2343 /* Draw overline. */
2344 if (s->face->overline_p)
2346 unsigned long dy = 0, h = 1;
2348 if (s->face->overline_color_defaulted_p)
2350 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2351 s->y + dy, s->width, h);
2353 else
2355 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2356 s->y + dy, s->width, h);
2360 /* Draw strike-through. */
2361 if (s->face->strike_through_p
2362 && !FONT_TEXTMETRIC(s->font).tmStruckOut)
2364 unsigned long h = 1;
2365 unsigned long dy = (s->height - h) / 2;
2367 if (s->face->strike_through_color_defaulted_p)
2369 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2370 s->width, h);
2372 else
2374 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2375 s->y + dy, s->width, h);
2379 /* Draw relief if not yet drawn. */
2380 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2381 x_draw_glyph_string_box (s);
2383 if (s->prev)
2385 struct glyph_string *prev;
2387 for (prev = s->prev; prev; prev = prev->prev)
2388 if (prev->hl != s->hl
2389 && prev->x + prev->width + prev->right_overhang > s->x)
2391 /* As prev was drawn while clipped to its own area, we
2392 must draw the right_overhang part using s->hl now. */
2393 enum draw_glyphs_face save = prev->hl;
2395 prev->hl = s->hl;
2396 x_set_glyph_string_gc (prev);
2397 x_set_glyph_string_clipping_exactly (s, prev);
2398 if (prev->first_glyph->type == CHAR_GLYPH)
2399 x_draw_glyph_string_foreground (prev);
2400 else
2401 x_draw_composite_glyph_string_foreground (prev);
2402 w32_set_clip_rectangle (prev->hdc, NULL);
2403 prev->hl = save;
2404 prev->num_clips = 0;
2408 if (s->next)
2410 struct glyph_string *next;
2412 for (next = s->next; next; next = next->next)
2413 if (next->hl != s->hl
2414 && next->x - next->left_overhang < s->x + s->width)
2416 /* As next will be drawn while clipped to its own area,
2417 we must draw the left_overhang part using s->hl now. */
2418 enum draw_glyphs_face save = next->hl;
2420 next->hl = s->hl;
2421 x_set_glyph_string_gc (next);
2422 x_set_glyph_string_clipping_exactly (s, next);
2423 if (next->first_glyph->type == CHAR_GLYPH)
2424 x_draw_glyph_string_foreground (next);
2425 else
2426 x_draw_composite_glyph_string_foreground (next);
2427 w32_set_clip_rectangle (next->hdc, NULL);
2428 next->hl = save;
2429 next->num_clips = 0;
2434 /* Reset clipping. */
2435 w32_set_clip_rectangle (s->hdc, NULL);
2436 s->num_clips = 0;
2440 /* Shift display to make room for inserted glyphs. */
2442 void
2443 w32_shift_glyphs_for_insert (struct frame *f, int x, int y,
2444 int width, int height, int shift_by)
2446 HDC hdc;
2448 hdc = get_frame_dc (f);
2449 BitBlt (hdc, x + shift_by, y, width, height,
2450 hdc, x, y, SRCCOPY);
2452 release_frame_dc (f, hdc);
2456 /* Delete N glyphs at the nominal cursor position. Not implemented
2457 for X frames. */
2459 static void
2460 x_delete_glyphs (struct frame *f, register int n)
2462 if (! FRAME_W32_P (f))
2463 return;
2465 abort ();
2469 /* Clear entire frame. If updating_frame is non-null, clear that
2470 frame. Otherwise clear the selected frame. */
2472 static void
2473 x_clear_frame (struct frame *f)
2475 if (! FRAME_W32_P (f))
2476 return;
2478 /* Clearing the frame will erase any cursor, so mark them all as no
2479 longer visible. */
2480 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2481 output_cursor.hpos = output_cursor.vpos = 0;
2482 output_cursor.x = -1;
2484 /* We don't set the output cursor here because there will always
2485 follow an explicit cursor_to. */
2486 BLOCK_INPUT;
2488 w32_clear_window (f);
2490 /* We have to clear the scroll bars, too. If we have changed
2491 colors or something like that, then they should be notified. */
2492 x_scroll_bar_clear (f);
2494 UNBLOCK_INPUT;
2498 /* Make audible bell. */
2500 static void
2501 w32_ring_bell (struct frame *f)
2503 BLOCK_INPUT;
2505 if (FRAME_W32_P (f) && visible_bell)
2507 int i;
2508 HWND hwnd = FRAME_W32_WINDOW (f);
2510 for (i = 0; i < 5; i++)
2512 FlashWindow (hwnd, TRUE);
2513 Sleep (10);
2515 FlashWindow (hwnd, FALSE);
2517 else
2518 w32_sys_ring_bell (f);
2520 UNBLOCK_INPUT;
2524 /* Specify how many text lines, from the top of the window,
2525 should be affected by insert-lines and delete-lines operations.
2526 This, and those operations, are used only within an update
2527 that is bounded by calls to x_update_begin and x_update_end. */
2529 static void
2530 w32_set_terminal_window (struct frame *f, int n)
2532 /* This function intentionally left blank. */
2536 /***********************************************************************
2537 Line Dance
2538 ***********************************************************************/
2540 /* Perform an insert-lines or delete-lines operation, inserting N
2541 lines or deleting -N lines at vertical position VPOS. */
2543 static void
2544 x_ins_del_lines (struct frame *f, int vpos, int n)
2546 if (! FRAME_W32_P (f))
2547 return;
2549 abort ();
2553 /* Scroll part of the display as described by RUN. */
2555 static void
2556 x_scroll_run (struct window *w, struct run *run)
2558 struct frame *f = XFRAME (w->frame);
2559 int x, y, width, height, from_y, to_y, bottom_y;
2560 HWND hwnd = FRAME_W32_WINDOW (f);
2561 HRGN expect_dirty;
2563 /* Get frame-relative bounding box of the text display area of W,
2564 without mode lines. Include in this box the left and right
2565 fringes of W. */
2566 window_box (w, -1, &x, &y, &width, &height);
2568 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2569 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2570 bottom_y = y + height;
2572 if (to_y < from_y)
2574 /* Scrolling up. Make sure we don't copy part of the mode
2575 line at the bottom. */
2576 if (from_y + run->height > bottom_y)
2577 height = bottom_y - from_y;
2578 else
2579 height = run->height;
2580 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2582 else
2584 /* Scolling down. Make sure we don't copy over the mode line.
2585 at the bottom. */
2586 if (to_y + run->height > bottom_y)
2587 height = bottom_y - to_y;
2588 else
2589 height = run->height;
2590 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2593 BLOCK_INPUT;
2595 /* Cursor off. Will be switched on again in x_update_window_end. */
2596 updated_window = w;
2597 x_clear_cursor (w);
2600 RECT from;
2601 RECT to;
2602 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2603 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2605 from.left = to.left = x;
2606 from.right = to.right = x + width;
2607 from.top = from_y;
2608 from.bottom = from_y + height;
2609 to.top = y;
2610 to.bottom = bottom_y;
2612 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2613 NULL, SW_INVALIDATE);
2615 /* Combine this with what we expect to be dirty. This covers the
2616 case where not all of the region we expect is actually dirty. */
2617 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2619 /* If the dirty region is not what we expected, redraw the entire frame. */
2620 if (!EqualRgn (combined, expect_dirty))
2621 SET_FRAME_GARBAGED (f);
2623 DeleteObject (dirty);
2624 DeleteObject (combined);
2627 UNBLOCK_INPUT;
2628 DeleteObject (expect_dirty);
2633 /***********************************************************************
2634 Exposure Events
2635 ***********************************************************************/
2637 static void
2638 frame_highlight (struct frame *f)
2640 x_update_cursor (f, 1);
2641 x_set_frame_alpha (f);
2644 static void
2645 frame_unhighlight (struct frame *f)
2647 x_update_cursor (f, 1);
2648 x_set_frame_alpha (f);
2651 /* The focus has changed. Update the frames as necessary to reflect
2652 the new situation. Note that we can't change the selected frame
2653 here, because the Lisp code we are interrupting might become confused.
2654 Each event gets marked with the frame in which it occurred, so the
2655 Lisp code can tell when the switch took place by examining the events. */
2657 static void
2658 x_new_focus_frame (struct w32_display_info *dpyinfo, struct frame *frame)
2660 struct frame *old_focus = dpyinfo->w32_focus_frame;
2662 if (frame != dpyinfo->w32_focus_frame)
2664 /* Set this before calling other routines, so that they see
2665 the correct value of w32_focus_frame. */
2666 dpyinfo->w32_focus_frame = frame;
2668 if (old_focus && old_focus->auto_lower)
2669 x_lower_frame (old_focus);
2671 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2672 pending_autoraise_frame = dpyinfo->w32_focus_frame;
2673 else
2674 pending_autoraise_frame = 0;
2677 x_frame_rehighlight (dpyinfo);
2681 /* Handle FocusIn and FocusOut state changes for FRAME.
2682 If FRAME has focus and there exists more than one frame, puts
2683 a FOCUS_IN_EVENT into *BUFP. */
2685 static void
2686 x_focus_changed (int type, int state, struct w32_display_info *dpyinfo,
2687 struct frame *frame, struct input_event *bufp)
2689 if (type == WM_SETFOCUS)
2691 if (dpyinfo->w32_focus_event_frame != frame)
2693 x_new_focus_frame (dpyinfo, frame);
2694 dpyinfo->w32_focus_event_frame = frame;
2696 /* Don't stop displaying the initial startup message
2697 for a switch-frame event we don't need. */
2698 if (NILP (Vterminal_frame)
2699 && CONSP (Vframe_list)
2700 && !NILP (XCDR (Vframe_list)))
2702 bufp->kind = FOCUS_IN_EVENT;
2703 XSETFRAME (bufp->frame_or_window, frame);
2707 frame->output_data.x->focus_state |= state;
2709 /* TODO: IME focus? */
2711 else if (type == WM_KILLFOCUS)
2713 frame->output_data.x->focus_state &= ~state;
2715 if (dpyinfo->w32_focus_event_frame == frame)
2717 dpyinfo->w32_focus_event_frame = 0;
2718 x_new_focus_frame (dpyinfo, 0);
2721 /* TODO: IME focus? */
2726 /* The focus may have changed. Figure out if it is a real focus change,
2727 by checking both FocusIn/Out and Enter/LeaveNotify events.
2729 Returns FOCUS_IN_EVENT event in *BUFP. */
2731 static void
2732 w32_detect_focus_change (struct w32_display_info *dpyinfo, W32Msg *event,
2733 struct input_event *bufp)
2735 struct frame *frame;
2737 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2738 if (! frame)
2739 return;
2741 /* On w32, this is only called from focus events, so no switch needed. */
2742 x_focus_changed (event->msg.message,
2743 (event->msg.message == WM_KILLFOCUS ?
2744 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2745 dpyinfo, frame, bufp);
2749 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2751 void
2752 x_mouse_leave (struct w32_display_info *dpyinfo)
2754 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2757 /* The focus has changed, or we have redirected a frame's focus to
2758 another frame (this happens when a frame uses a surrogate
2759 mini-buffer frame). Shift the highlight as appropriate.
2761 The FRAME argument doesn't necessarily have anything to do with which
2762 frame is being highlighted or un-highlighted; we only use it to find
2763 the appropriate X display info. */
2765 static void
2766 w32_frame_rehighlight (struct frame *frame)
2768 if (! FRAME_W32_P (frame))
2769 return;
2770 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
2773 static void
2774 x_frame_rehighlight (struct w32_display_info *dpyinfo)
2776 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2778 if (dpyinfo->w32_focus_frame)
2780 dpyinfo->x_highlight_frame
2781 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2782 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2783 : dpyinfo->w32_focus_frame);
2784 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2786 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
2787 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2790 else
2791 dpyinfo->x_highlight_frame = 0;
2793 if (dpyinfo->x_highlight_frame != old_highlight)
2795 if (old_highlight)
2796 frame_unhighlight (old_highlight);
2797 if (dpyinfo->x_highlight_frame)
2798 frame_highlight (dpyinfo->x_highlight_frame);
2802 /* Keyboard processing - modifier keys, etc. */
2804 /* Convert a keysym to its name. */
2806 char *
2807 x_get_keysym_name (int keysym)
2809 /* Make static so we can always return it */
2810 static char value[100];
2812 BLOCK_INPUT;
2813 GetKeyNameText (keysym, value, 100);
2814 UNBLOCK_INPUT;
2816 return value;
2819 static int
2820 codepage_for_locale (LCID locale)
2822 char cp[20];
2824 if (GetLocaleInfo (locale, LOCALE_IDEFAULTANSICODEPAGE, cp, 20) > 0)
2825 return atoi (cp);
2826 else
2827 return CP_ACP;
2831 /* Mouse clicks and mouse movement. Rah. */
2833 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2834 the state in PUP. XBUTTON provides extra information for extended mouse
2835 button messages. Returns FALSE if unable to parse the message. */
2836 BOOL
2837 parse_button (int message, int xbutton, int * pbutton, int * pup)
2839 int button = 0;
2840 int up = 0;
2842 switch (message)
2844 case WM_LBUTTONDOWN:
2845 button = 0;
2846 up = 0;
2847 break;
2848 case WM_LBUTTONUP:
2849 button = 0;
2850 up = 1;
2851 break;
2852 case WM_MBUTTONDOWN:
2853 if (NILP (Vw32_swap_mouse_buttons))
2854 button = 1;
2855 else
2856 button = 2;
2857 up = 0;
2858 break;
2859 case WM_MBUTTONUP:
2860 if (NILP (Vw32_swap_mouse_buttons))
2861 button = 1;
2862 else
2863 button = 2;
2864 up = 1;
2865 break;
2866 case WM_RBUTTONDOWN:
2867 if (NILP (Vw32_swap_mouse_buttons))
2868 button = 2;
2869 else
2870 button = 1;
2871 up = 0;
2872 break;
2873 case WM_RBUTTONUP:
2874 if (NILP (Vw32_swap_mouse_buttons))
2875 button = 2;
2876 else
2877 button = 1;
2878 up = 1;
2879 break;
2880 case WM_XBUTTONDOWN:
2881 button = xbutton + 2;
2882 up = 0;
2883 break;
2884 case WM_XBUTTONUP:
2885 button = xbutton + 2;
2886 up = 1;
2887 break;
2888 default:
2889 return (FALSE);
2892 if (pup) *pup = up;
2893 if (pbutton) *pbutton = button;
2895 return (TRUE);
2899 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2901 If the event is a button press, then note that we have grabbed
2902 the mouse. */
2904 static Lisp_Object
2905 construct_mouse_click (struct input_event *result, W32Msg *msg, struct frame *f)
2907 int button;
2908 int up;
2910 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
2911 &button, &up);
2913 /* Make the event type NO_EVENT; we'll change that when we decide
2914 otherwise. */
2915 result->kind = MOUSE_CLICK_EVENT;
2916 result->code = button;
2917 result->timestamp = msg->msg.time;
2918 result->modifiers = (msg->dwModifiers
2919 | (up
2920 ? up_modifier
2921 : down_modifier));
2923 XSETINT (result->x, LOWORD (msg->msg.lParam));
2924 XSETINT (result->y, HIWORD (msg->msg.lParam));
2925 XSETFRAME (result->frame_or_window, f);
2926 result->arg = Qnil;
2927 return Qnil;
2930 static Lisp_Object
2931 construct_mouse_wheel (struct input_event *result, W32Msg *msg, struct frame *f)
2933 POINT p;
2934 int delta;
2936 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
2937 : WHEEL_EVENT;
2938 result->code = 0;
2939 result->timestamp = msg->msg.time;
2941 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
2942 forward, away from the user (up); a negative value indicates that
2943 the wheel was rotated backward, toward the user (down). */
2944 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
2946 /* The up and down modifiers indicate if the wheel was rotated up or
2947 down based on WHEEL_DELTA value. */
2948 result->modifiers = (msg->dwModifiers
2949 | ((delta < 0 ) ? down_modifier : up_modifier));
2951 /* With multiple monitors, we can legitimately get negative
2952 coordinates, so cast to short to interpret them correctly. */
2953 p.x = (short) LOWORD (msg->msg.lParam);
2954 p.y = (short) HIWORD (msg->msg.lParam);
2955 ScreenToClient (msg->msg.hwnd, &p);
2956 XSETINT (result->x, p.x);
2957 XSETINT (result->y, p.y);
2958 XSETFRAME (result->frame_or_window, f);
2959 result->arg = Qnil;
2960 return Qnil;
2963 static Lisp_Object
2964 construct_drag_n_drop (struct input_event *result, W32Msg *msg, struct frame *f)
2966 Lisp_Object files;
2967 Lisp_Object frame;
2968 HDROP hdrop;
2969 POINT p;
2970 WORD num_files;
2971 char *name;
2972 int i, len;
2974 result->kind = DRAG_N_DROP_EVENT;
2975 result->code = 0;
2976 result->timestamp = msg->msg.time;
2977 result->modifiers = msg->dwModifiers;
2979 hdrop = (HDROP) msg->msg.wParam;
2980 DragQueryPoint (hdrop, &p);
2982 #if 0
2983 p.x = LOWORD (msg->msg.lParam);
2984 p.y = HIWORD (msg->msg.lParam);
2985 ScreenToClient (msg->msg.hwnd, &p);
2986 #endif
2988 XSETINT (result->x, p.x);
2989 XSETINT (result->y, p.y);
2991 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
2992 files = Qnil;
2994 for (i = 0; i < num_files; i++)
2996 len = DragQueryFile (hdrop, i, NULL, 0);
2997 if (len <= 0)
2998 continue;
2999 name = alloca (len + 1);
3000 DragQueryFile (hdrop, i, name, len + 1);
3001 files = Fcons (DECODE_FILE (build_string (name)), files);
3004 DragFinish (hdrop);
3006 XSETFRAME (frame, f);
3007 result->frame_or_window = frame;
3008 result->arg = files;
3009 return Qnil;
3013 /* Function to report a mouse movement to the mainstream Emacs code.
3014 The input handler calls this.
3016 We have received a mouse movement event, which is given in *event.
3017 If the mouse is over a different glyph than it was last time, tell
3018 the mainstream emacs code by setting mouse_moved. If not, ask for
3019 another motion event, so we can check again the next time it moves. */
3021 static MSG last_mouse_motion_event;
3022 static Lisp_Object last_mouse_motion_frame;
3024 static int
3025 note_mouse_movement (FRAME_PTR frame, MSG *msg)
3027 int mouse_x = LOWORD (msg->lParam);
3028 int mouse_y = HIWORD (msg->lParam);
3030 last_mouse_movement_time = msg->time;
3031 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
3032 XSETFRAME (last_mouse_motion_frame, frame);
3034 if (!FRAME_X_OUTPUT (frame))
3035 return 0;
3037 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3039 frame->mouse_moved = 1;
3040 last_mouse_scroll_bar = Qnil;
3041 note_mouse_highlight (frame, -1, -1);
3042 last_mouse_glyph_frame = 0;
3043 return 1;
3046 /* Has the mouse moved off the glyph it was on at the last sighting? */
3047 if (frame != last_mouse_glyph_frame
3048 || mouse_x < last_mouse_glyph.left
3049 || mouse_x >= last_mouse_glyph.right
3050 || mouse_y < last_mouse_glyph.top
3051 || mouse_y >= last_mouse_glyph.bottom)
3053 frame->mouse_moved = 1;
3054 last_mouse_scroll_bar = Qnil;
3055 note_mouse_highlight (frame, mouse_x, mouse_y);
3056 /* Remember the mouse position here, as w32_mouse_position only
3057 gets called when mouse tracking is enabled but we also need
3058 to keep track of the mouse for help_echo and highlighting at
3059 other times. */
3060 remember_mouse_glyph (frame, mouse_x, mouse_y, &last_mouse_glyph);
3061 last_mouse_glyph_frame = frame;
3062 return 1;
3065 return 0;
3069 /************************************************************************
3070 Mouse Face
3071 ************************************************************************/
3073 static struct scroll_bar *x_window_to_scroll_bar (Window);
3074 static void x_scroll_bar_report_motion (FRAME_PTR *, Lisp_Object *,
3075 enum scroll_bar_part *,
3076 Lisp_Object *, Lisp_Object *,
3077 unsigned long *);
3078 static void x_check_fullscreen (struct frame *);
3080 static void
3081 redo_mouse_highlight (void)
3083 if (!NILP (last_mouse_motion_frame)
3084 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3085 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3086 LOWORD (last_mouse_motion_event.lParam),
3087 HIWORD (last_mouse_motion_event.lParam));
3090 static void
3091 w32_define_cursor (Window window, Cursor cursor)
3093 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3095 /* Return the current position of the mouse.
3096 *fp should be a frame which indicates which display to ask about.
3098 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3099 and *part to the frame, window, and scroll bar part that the mouse
3100 is over. Set *x and *y to the portion and whole of the mouse's
3101 position on the scroll bar.
3103 If the mouse movement started elsewhere, set *fp to the frame the
3104 mouse is on, *bar_window to nil, and *x and *y to the character cell
3105 the mouse is over.
3107 Set *time to the server time-stamp for the time at which the mouse
3108 was at this position.
3110 Don't store anything if we don't have a valid set of values to report.
3112 This clears the mouse_moved flag, so we can wait for the next mouse
3113 movement. */
3115 static void
3116 w32_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
3117 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3118 unsigned long *time)
3120 FRAME_PTR f1;
3122 BLOCK_INPUT;
3124 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3125 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3126 else
3128 POINT pt;
3130 Lisp_Object frame, tail;
3132 /* Clear the mouse-moved flag for every frame on this display. */
3133 FOR_EACH_FRAME (tail, frame)
3134 XFRAME (frame)->mouse_moved = 0;
3136 last_mouse_scroll_bar = Qnil;
3138 GetCursorPos (&pt);
3140 /* Now we have a position on the root; find the innermost window
3141 containing the pointer. */
3143 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3144 && FRAME_LIVE_P (last_mouse_frame))
3146 /* If mouse was grabbed on a frame, give coords for that frame
3147 even if the mouse is now outside it. */
3148 f1 = last_mouse_frame;
3150 else
3152 /* Is window under mouse one of our frames? */
3153 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
3154 WindowFromPoint (pt));
3157 /* If not, is it one of our scroll bars? */
3158 if (! f1)
3160 struct scroll_bar *bar
3161 = x_window_to_scroll_bar (WindowFromPoint (pt));
3163 if (bar)
3165 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3169 if (f1 == 0 && insist > 0)
3170 f1 = SELECTED_FRAME ();
3172 if (f1)
3174 /* Ok, we found a frame. Store all the values.
3175 last_mouse_glyph is a rectangle used to reduce the
3176 generation of mouse events. To not miss any motion
3177 events, we must divide the frame into rectangles of the
3178 size of the smallest character that could be displayed
3179 on it, i.e. into the same rectangles that matrices on
3180 the frame are divided into. */
3182 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3183 remember_mouse_glyph (f1, pt.x, pt.y, &last_mouse_glyph);
3184 last_mouse_glyph_frame = f1;
3186 *bar_window = Qnil;
3187 *part = 0;
3188 *fp = f1;
3189 XSETINT (*x, pt.x);
3190 XSETINT (*y, pt.y);
3191 *time = last_mouse_movement_time;
3196 UNBLOCK_INPUT;
3200 /***********************************************************************
3201 Tool-bars
3202 ***********************************************************************/
3204 /* Handle mouse button event on the tool-bar of frame F, at
3205 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3206 or ButtonRelase. */
3208 static void
3209 w32_handle_tool_bar_click (struct frame *f, struct input_event *button_event)
3211 int x = XFASTINT (button_event->x);
3212 int y = XFASTINT (button_event->y);
3214 if (button_event->modifiers & down_modifier)
3215 handle_tool_bar_click (f, x, y, 1, 0);
3216 else
3217 handle_tool_bar_click (f, x, y, 0,
3218 button_event->modifiers & ~up_modifier);
3223 /***********************************************************************
3224 Scroll bars
3225 ***********************************************************************/
3227 /* Scroll bar support. */
3229 /* Given a window ID, find the struct scroll_bar which manages it.
3230 This can be called in GC, so we have to make sure to strip off mark
3231 bits. */
3233 static struct scroll_bar *
3234 x_window_to_scroll_bar (Window window_id)
3236 Lisp_Object tail;
3238 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3240 Lisp_Object frame, bar, condemned;
3242 frame = XCAR (tail);
3243 /* All elements of Vframe_list should be frames. */
3244 if (! FRAMEP (frame))
3245 abort ();
3247 /* Scan this frame's scroll bar list for a scroll bar with the
3248 right window ID. */
3249 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3250 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3251 /* This trick allows us to search both the ordinary and
3252 condemned scroll bar lists with one loop. */
3253 ! NILP (bar) || (bar = condemned,
3254 condemned = Qnil,
3255 ! NILP (bar));
3256 bar = XSCROLL_BAR (bar)->next)
3257 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3258 return XSCROLL_BAR (bar);
3261 return 0;
3266 /* Set the thumb size and position of scroll bar BAR. We are currently
3267 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3269 static void
3270 w32_set_scroll_bar_thumb (struct scroll_bar *bar,
3271 int portion, int position, int whole)
3273 Window w = SCROLL_BAR_W32_WINDOW (bar);
3274 /* We use the whole scroll-bar height in the calculations below, to
3275 avoid strange effects like scrolling backwards when just clicking
3276 on the handle (without moving it). */
3277 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height))
3278 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3279 int sb_page, sb_pos;
3280 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3281 SCROLLINFO si;
3283 /* We used to change the nPage setting while dragging the handle,
3284 but that had very strange effects (such as scrolling backwards
3285 while dragging downwards).
3287 Now, we don't change the nPage setting while dragging unless we
3288 get near to the end of the buffer, in which case we often have to
3289 resize the handle to "go all the way". */
3291 if (draggingp)
3293 int near_bottom_p;
3294 BLOCK_INPUT;
3295 si.cbSize = sizeof (si);
3296 si.fMask = SIF_POS | SIF_PAGE;
3297 GetScrollInfo(w, SB_CTL, &si);
3298 near_bottom_p = si.nPos + si.nPage >= range;
3299 UNBLOCK_INPUT;
3300 if (!near_bottom_p)
3301 return;
3304 if (whole)
3306 /* Position scroll bar at rock bottom if the bottom of the
3307 buffer is visible. This avoids shinking the thumb away
3308 to nothing if it is held at the bottom of the buffer. */
3309 if (position + portion >= whole && !draggingp)
3311 sb_page = range * (whole - position) / whole;
3312 sb_pos = range;
3314 else
3316 sb_pos = position * range / whole;
3317 sb_page = (min (portion, (whole - position)) * range) / whole;
3320 else
3322 sb_page = range;
3323 sb_pos = 0;
3326 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3328 BLOCK_INPUT;
3330 si.cbSize = sizeof (si);
3331 si.fMask = SIF_PAGE | SIF_POS;
3332 si.nPage = sb_page;
3333 si.nPos = sb_pos;
3335 SetScrollInfo (w, SB_CTL, &si, TRUE);
3337 UNBLOCK_INPUT;
3341 /************************************************************************
3342 Scroll bars, general
3343 ************************************************************************/
3345 static HWND
3346 my_create_scrollbar (struct frame * f, struct scroll_bar * bar)
3348 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3349 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3350 (LPARAM) bar);
3353 /*#define ATTACH_THREADS*/
3355 static BOOL
3356 my_show_window (FRAME_PTR f, HWND hwnd, int how)
3358 #ifndef ATTACH_THREADS
3359 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3360 (WPARAM) hwnd, (LPARAM) how);
3361 #else
3362 return ShowWindow (hwnd, how);
3363 #endif
3366 static void
3367 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3368 int x, int y, int cx, int cy, UINT flags)
3370 #ifndef ATTACH_THREADS
3371 WINDOWPOS pos;
3372 pos.hwndInsertAfter = hwndAfter;
3373 pos.x = x;
3374 pos.y = y;
3375 pos.cx = cx;
3376 pos.cy = cy;
3377 pos.flags = flags;
3378 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3379 #else
3380 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3381 #endif
3384 static void
3385 my_set_focus (struct frame * f, HWND hwnd)
3387 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3388 (WPARAM) hwnd, 0);
3391 static void
3392 my_set_foreground_window (HWND hwnd)
3394 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3398 static void
3399 my_destroy_window (struct frame * f, HWND hwnd)
3401 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3402 (WPARAM) hwnd, 0);
3405 /* Create a scroll bar and return the scroll bar vector for it. W is
3406 the Emacs window on which to create the scroll bar. TOP, LEFT,
3407 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
3408 scroll bar. */
3410 static struct scroll_bar *
3411 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
3413 struct frame *f = XFRAME (WINDOW_FRAME (w));
3414 HWND hwnd;
3415 SCROLLINFO si;
3416 struct scroll_bar *bar
3417 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3419 BLOCK_INPUT;
3421 XSETWINDOW (bar->window, w);
3422 XSETINT (bar->top, top);
3423 XSETINT (bar->left, left);
3424 XSETINT (bar->width, width);
3425 XSETINT (bar->height, height);
3426 XSETINT (bar->start, 0);
3427 XSETINT (bar->end, 0);
3428 bar->dragging = Qnil;
3429 bar->fringe_extended_p = Qnil;
3431 /* Requires geometry to be set before call to create the real window */
3433 hwnd = my_create_scrollbar (f, bar);
3435 si.cbSize = sizeof (si);
3436 si.fMask = SIF_ALL;
3437 si.nMin = 0;
3438 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3439 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3440 si.nPage = si.nMax;
3441 si.nPos = 0;
3443 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3445 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3447 /* Add bar to its frame's list of scroll bars. */
3448 bar->next = FRAME_SCROLL_BARS (f);
3449 bar->prev = Qnil;
3450 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3451 if (! NILP (bar->next))
3452 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3454 UNBLOCK_INPUT;
3456 return bar;
3460 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3461 nil. */
3463 static void
3464 x_scroll_bar_remove (struct scroll_bar *bar)
3466 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3468 BLOCK_INPUT;
3470 /* Destroy the window. */
3471 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3473 /* Disassociate this scroll bar from its window. */
3474 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3476 UNBLOCK_INPUT;
3479 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3480 that we are displaying PORTION characters out of a total of WHOLE
3481 characters, starting at POSITION. If WINDOW has no scroll bar,
3482 create one. */
3483 static void
3484 w32_set_vertical_scroll_bar (struct window *w,
3485 int portion, int whole, int position)
3487 struct frame *f = XFRAME (w->frame);
3488 struct scroll_bar *bar;
3489 int top, height, left, sb_left, width, sb_width;
3490 int window_y, window_height;
3491 int fringe_extended_p;
3493 /* Get window dimensions. */
3494 window_box (w, -1, 0, &window_y, 0, &window_height);
3495 top = window_y;
3496 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3497 height = window_height;
3499 /* Compute the left edge of the scroll bar area. */
3500 left = WINDOW_SCROLL_BAR_AREA_X (w);
3502 /* Compute the width of the scroll bar which might be less than
3503 the width of the area reserved for the scroll bar. */
3504 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3505 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3506 else
3507 sb_width = width;
3509 /* Compute the left edge of the scroll bar. */
3510 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3511 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3512 else
3513 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3515 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3516 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
3517 && WINDOW_LEFT_FRINGE_WIDTH (w)
3518 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3519 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
3520 else
3521 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
3522 && WINDOW_RIGHT_FRINGE_WIDTH (w)
3523 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3524 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
3526 /* Does the scroll bar exist yet? */
3527 if (NILP (w->vertical_scroll_bar))
3529 HDC hdc;
3530 BLOCK_INPUT;
3531 if (width > 0 && height > 0)
3533 hdc = get_frame_dc (f);
3534 if (fringe_extended_p)
3535 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3536 else
3537 w32_clear_area (f, hdc, left, top, width, height);
3538 release_frame_dc (f, hdc);
3540 UNBLOCK_INPUT;
3542 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3544 else
3546 /* It may just need to be moved and resized. */
3547 HWND hwnd;
3549 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3550 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3552 /* If already correctly positioned, do nothing. */
3553 if ( XINT (bar->left) == sb_left
3554 && XINT (bar->top) == top
3555 && XINT (bar->width) == sb_width
3556 && XINT (bar->height) == height
3557 && !NILP (bar->fringe_extended_p) == fringe_extended_p )
3559 /* Redraw after clear_frame. */
3560 if (!my_show_window (f, hwnd, SW_NORMAL))
3561 InvalidateRect (hwnd, NULL, FALSE);
3563 else
3565 HDC hdc;
3566 SCROLLINFO si;
3568 BLOCK_INPUT;
3569 if (width && height)
3571 hdc = get_frame_dc (f);
3572 /* Since Windows scroll bars are smaller than the space reserved
3573 for them on the frame, we have to clear "under" them. */
3574 if (fringe_extended_p)
3575 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3576 else
3577 w32_clear_area (f, hdc, left, top, width, height);
3578 release_frame_dc (f, hdc);
3580 /* Make sure scroll bar is "visible" before moving, to ensure the
3581 area of the parent window now exposed will be refreshed. */
3582 my_show_window (f, hwnd, SW_HIDE);
3583 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3584 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3585 max (height, 1), TRUE);
3587 si.cbSize = sizeof (si);
3588 si.fMask = SIF_RANGE;
3589 si.nMin = 0;
3590 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3591 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3593 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3595 my_show_window (f, hwnd, SW_NORMAL);
3596 /* InvalidateRect (w, NULL, FALSE); */
3598 /* Remember new settings. */
3599 XSETINT (bar->left, sb_left);
3600 XSETINT (bar->top, top);
3601 XSETINT (bar->width, sb_width);
3602 XSETINT (bar->height, height);
3604 UNBLOCK_INPUT;
3607 bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
3609 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3611 XSETVECTOR (w->vertical_scroll_bar, bar);
3615 /* The following three hooks are used when we're doing a thorough
3616 redisplay of the frame. We don't explicitly know which scroll bars
3617 are going to be deleted, because keeping track of when windows go
3618 away is a real pain - "Can you say set-window-configuration, boys
3619 and girls?" Instead, we just assert at the beginning of redisplay
3620 that *all* scroll bars are to be removed, and then save a scroll bar
3621 from the fiery pit when we actually redisplay its window. */
3623 /* Arrange for all scroll bars on FRAME to be removed at the next call
3624 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3625 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3627 static void
3628 w32_condemn_scroll_bars (FRAME_PTR frame)
3630 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3631 while (! NILP (FRAME_SCROLL_BARS (frame)))
3633 Lisp_Object bar;
3634 bar = FRAME_SCROLL_BARS (frame);
3635 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
3636 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3637 XSCROLL_BAR (bar)->prev = Qnil;
3638 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3639 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3640 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
3645 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3646 Note that WINDOW isn't necessarily condemned at all. */
3648 static void
3649 w32_redeem_scroll_bar (struct window *window)
3651 struct scroll_bar *bar;
3652 struct frame *f;
3654 /* We can't redeem this window's scroll bar if it doesn't have one. */
3655 if (NILP (window->vertical_scroll_bar))
3656 abort ();
3658 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3660 /* Unlink it from the condemned list. */
3661 f = XFRAME (WINDOW_FRAME (window));
3662 if (NILP (bar->prev))
3664 /* If the prev pointer is nil, it must be the first in one of
3665 the lists. */
3666 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3667 /* It's not condemned. Everything's fine. */
3668 return;
3669 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3670 window->vertical_scroll_bar))
3671 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
3672 else
3673 /* If its prev pointer is nil, it must be at the front of
3674 one or the other! */
3675 abort ();
3677 else
3678 XSCROLL_BAR (bar->prev)->next = bar->next;
3680 if (! NILP (bar->next))
3681 XSCROLL_BAR (bar->next)->prev = bar->prev;
3683 bar->next = FRAME_SCROLL_BARS (f);
3684 bar->prev = Qnil;
3685 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3686 if (! NILP (bar->next))
3687 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3690 /* Remove all scroll bars on FRAME that haven't been saved since the
3691 last call to `*condemn_scroll_bars_hook'. */
3693 static void
3694 w32_judge_scroll_bars (FRAME_PTR f)
3696 Lisp_Object bar, next;
3698 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3700 /* Clear out the condemned list now so we won't try to process any
3701 more events on the hapless scroll bars. */
3702 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3704 for (; ! NILP (bar); bar = next)
3706 struct scroll_bar *b = XSCROLL_BAR (bar);
3708 x_scroll_bar_remove (b);
3710 next = b->next;
3711 b->next = b->prev = Qnil;
3714 /* Now there should be no references to the condemned scroll bars,
3715 and they should get garbage-collected. */
3718 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3719 is set to something other than NO_EVENT, it is enqueued.
3721 This may be called from a signal handler, so we have to ignore GC
3722 mark bits. */
3724 static int
3725 w32_scroll_bar_handle_click (struct scroll_bar *bar, W32Msg *msg,
3726 struct input_event *emacs_event)
3728 if (! WINDOWP (bar->window))
3729 abort ();
3731 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
3732 emacs_event->code = 0;
3733 /* not really meaningful to distinguish up/down */
3734 emacs_event->modifiers = msg->dwModifiers;
3735 emacs_event->frame_or_window = bar->window;
3736 emacs_event->arg = Qnil;
3737 emacs_event->timestamp = msg->msg.time;
3740 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3741 int y;
3742 int dragging = !NILP (bar->dragging);
3743 SCROLLINFO si;
3745 si.cbSize = sizeof (si);
3746 si.fMask = SIF_POS;
3748 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
3749 y = si.nPos;
3751 bar->dragging = Qnil;
3754 last_mouse_scroll_bar_pos = msg->msg.wParam;
3756 switch (LOWORD (msg->msg.wParam))
3758 case SB_LINEDOWN:
3759 emacs_event->part = scroll_bar_down_arrow;
3760 break;
3761 case SB_LINEUP:
3762 emacs_event->part = scroll_bar_up_arrow;
3763 break;
3764 case SB_PAGEUP:
3765 emacs_event->part = scroll_bar_above_handle;
3766 break;
3767 case SB_PAGEDOWN:
3768 emacs_event->part = scroll_bar_below_handle;
3769 break;
3770 case SB_TOP:
3771 emacs_event->part = scroll_bar_handle;
3772 y = 0;
3773 break;
3774 case SB_BOTTOM:
3775 emacs_event->part = scroll_bar_handle;
3776 y = top_range;
3777 break;
3778 case SB_THUMBTRACK:
3779 case SB_THUMBPOSITION:
3780 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3781 y = HIWORD (msg->msg.wParam);
3782 bar->dragging = Qt;
3783 emacs_event->part = scroll_bar_handle;
3785 /* "Silently" update current position. */
3787 SCROLLINFO si;
3789 si.cbSize = sizeof (si);
3790 si.fMask = SIF_POS;
3791 si.nPos = y;
3792 /* Remember apparent position (we actually lag behind the real
3793 position, so don't set that directly. */
3794 last_scroll_bar_drag_pos = y;
3796 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
3798 break;
3799 case SB_ENDSCROLL:
3800 /* If this is the end of a drag sequence, then reset the scroll
3801 handle size to normal and do a final redraw. Otherwise do
3802 nothing. */
3803 if (dragging)
3805 SCROLLINFO si;
3806 int start = XINT (bar->start);
3807 int end = XINT (bar->end);
3809 si.cbSize = sizeof (si);
3810 si.fMask = SIF_PAGE | SIF_POS;
3811 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3812 si.nPos = last_scroll_bar_drag_pos;
3813 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
3815 /* fall through */
3816 default:
3817 emacs_event->kind = NO_EVENT;
3818 return FALSE;
3821 XSETINT (emacs_event->x, y);
3822 XSETINT (emacs_event->y, top_range);
3824 return TRUE;
3828 /* Return information to the user about the current position of the mouse
3829 on the scroll bar. */
3831 static void
3832 x_scroll_bar_report_motion (FRAME_PTR *fp, Lisp_Object *bar_window,
3833 enum scroll_bar_part *part,
3834 Lisp_Object *x, Lisp_Object *y,
3835 unsigned long *time)
3837 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
3838 Window w = SCROLL_BAR_W32_WINDOW (bar);
3839 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3840 int pos;
3841 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3842 SCROLLINFO si;
3844 BLOCK_INPUT;
3846 *fp = f;
3847 *bar_window = bar->window;
3849 si.cbSize = sizeof (si);
3850 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
3852 GetScrollInfo (w, SB_CTL, &si);
3853 pos = si.nPos;
3854 top_range = si.nMax - si.nPage + 1;
3856 switch (LOWORD (last_mouse_scroll_bar_pos))
3858 case SB_THUMBPOSITION:
3859 case SB_THUMBTRACK:
3860 *part = scroll_bar_handle;
3861 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3862 pos = HIWORD (last_mouse_scroll_bar_pos);
3863 break;
3864 case SB_LINEDOWN:
3865 *part = scroll_bar_handle;
3866 pos++;
3867 break;
3868 default:
3869 *part = scroll_bar_handle;
3870 break;
3873 XSETINT (*x, pos);
3874 XSETINT (*y, top_range);
3876 f->mouse_moved = 0;
3877 last_mouse_scroll_bar = Qnil;
3879 *time = last_mouse_movement_time;
3881 UNBLOCK_INPUT;
3885 /* The screen has been cleared so we may have changed foreground or
3886 background colors, and the scroll bars may need to be redrawn.
3887 Clear out the scroll bars, and ask for expose events, so we can
3888 redraw them. */
3890 void
3891 x_scroll_bar_clear (FRAME_PTR f)
3893 Lisp_Object bar;
3895 /* We can have scroll bars even if this is 0,
3896 if we just turned off scroll bar mode.
3897 But in that case we should not clear them. */
3898 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3899 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
3900 bar = XSCROLL_BAR (bar)->next)
3902 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
3903 HDC hdc = GetDC (window);
3904 RECT rect;
3906 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
3907 arranges to refresh the scroll bar if hidden. */
3908 my_show_window (f, window, SW_HIDE);
3910 GetClientRect (window, &rect);
3911 select_palette (f, hdc);
3912 w32_clear_rect (f, hdc, &rect);
3913 deselect_palette (f, hdc);
3915 ReleaseDC (window, hdc);
3920 /* The main W32 event-reading loop - w32_read_socket. */
3922 /* Record the last 100 characters stored
3923 to help debug the loss-of-chars-during-GC problem. */
3925 static int temp_index;
3926 static short temp_buffer[100];
3928 /* Temporarily store lead byte of DBCS input sequences. */
3929 static char dbcs_lead = 0;
3931 /* Read events coming from the W32 shell.
3932 This routine is called by the SIGIO handler.
3933 We return as soon as there are no more events to be read.
3935 We return the number of characters stored into the buffer,
3936 thus pretending to be `read'.
3938 EXPECTED is nonzero if the caller knows input is available.
3940 Some of these messages are reposted back to the message queue since the
3941 system calls the windows proc directly in a context where we cannot return
3942 the data nor can we guarantee the state we are in. So if we dispatch them
3943 we will get into an infinite loop. To prevent this from ever happening we
3944 will set a variable to indicate we are in the read_socket call and indicate
3945 which message we are processing since the windows proc gets called
3946 recursively with different messages by the system.
3950 w32_read_socket (struct terminal *terminal, int expected,
3951 struct input_event *hold_quit)
3953 int count = 0;
3954 int check_visibility = 0;
3955 W32Msg msg;
3956 struct frame *f;
3957 struct w32_display_info *dpyinfo = &one_w32_display_info;
3959 if (interrupt_input_blocked)
3961 interrupt_input_pending = 1;
3962 return -1;
3965 interrupt_input_pending = 0;
3966 BLOCK_INPUT;
3968 /* So people can tell when we have read the available input. */
3969 input_signal_count++;
3971 /* TODO: ghostscript integration. */
3972 while (get_next_msg (&msg, FALSE))
3974 struct input_event inev;
3975 int do_help = 0;
3977 EVENT_INIT (inev);
3978 inev.kind = NO_EVENT;
3979 inev.arg = Qnil;
3981 switch (msg.msg.message)
3983 case WM_EMACS_PAINT:
3984 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
3986 if (f)
3988 if (msg.rect.right == msg.rect.left ||
3989 msg.rect.bottom == msg.rect.top)
3991 /* We may get paint messages even though the client
3992 area is clipped - these are not expose events. */
3993 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
3994 SDATA (f->name)));
3996 else if (f->async_visible != 1)
3998 /* Definitely not obscured, so mark as visible. */
3999 f->async_visible = 1;
4000 f->async_iconified = 0;
4001 SET_FRAME_GARBAGED (f);
4002 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4003 SDATA (f->name)));
4005 /* WM_PAINT serves as MapNotify as well, so report
4006 visibility changes properly. */
4007 if (f->iconified)
4009 inev.kind = DEICONIFY_EVENT;
4010 XSETFRAME (inev.frame_or_window, f);
4012 else if (! NILP (Vframe_list)
4013 && ! NILP (XCDR (Vframe_list)))
4014 /* Force a redisplay sooner or later to update the
4015 frame titles in case this is the second frame. */
4016 record_asynch_buffer_change ();
4018 else
4020 HDC hdc = get_frame_dc (f);
4022 /* Erase background again for safety. */
4023 w32_clear_rect (f, hdc, &msg.rect);
4024 release_frame_dc (f, hdc);
4025 expose_frame (f,
4026 msg.rect.left,
4027 msg.rect.top,
4028 msg.rect.right - msg.rect.left,
4029 msg.rect.bottom - msg.rect.top);
4032 break;
4034 case WM_INPUTLANGCHANGE:
4035 /* Generate a language change event. */
4036 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4038 /* lParam contains the input lang ID. Use it to update our
4039 record of the keyboard codepage. */
4040 keyboard_codepage = codepage_for_locale ((LCID)(msg.msg.lParam
4041 & 0xffff));
4043 if (f)
4045 inev.kind = LANGUAGE_CHANGE_EVENT;
4046 XSETFRAME (inev.frame_or_window, f);
4047 inev.code = msg.msg.wParam;
4048 inev.modifiers = msg.msg.lParam & 0xffff;
4050 break;
4052 case WM_KEYDOWN:
4053 case WM_SYSKEYDOWN:
4054 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4056 if (f && !f->iconified)
4058 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4059 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4061 clear_mouse_face (dpyinfo);
4062 dpyinfo->mouse_face_hidden = 1;
4065 if (temp_index == sizeof temp_buffer / sizeof (short))
4066 temp_index = 0;
4067 temp_buffer[temp_index++] = msg.msg.wParam;
4068 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4069 inev.code = msg.msg.wParam;
4070 inev.modifiers = msg.dwModifiers;
4071 XSETFRAME (inev.frame_or_window, f);
4072 inev.timestamp = msg.msg.time;
4074 break;
4076 case WM_UNICHAR:
4077 case WM_SYSCHAR:
4078 case WM_CHAR:
4079 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4081 if (f && !f->iconified)
4083 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4084 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4086 clear_mouse_face (dpyinfo);
4087 dpyinfo->mouse_face_hidden = 1;
4090 if (temp_index == sizeof temp_buffer / sizeof (short))
4091 temp_index = 0;
4092 temp_buffer[temp_index++] = msg.msg.wParam;
4094 inev.modifiers = msg.dwModifiers;
4095 XSETFRAME (inev.frame_or_window, f);
4096 inev.timestamp = msg.msg.time;
4098 if (msg.msg.message == WM_UNICHAR)
4100 inev.code = msg.msg.wParam;
4102 else if (msg.msg.wParam < 256)
4104 wchar_t code;
4105 char dbcs[2];
4106 dbcs[0] = 0;
4107 dbcs[1] = (char) msg.msg.wParam;
4109 if (dbcs_lead)
4111 dbcs[0] = dbcs_lead;
4112 dbcs_lead = 0;
4113 if (!MultiByteToWideChar (keyboard_codepage, 0,
4114 dbcs, 2, &code, 1))
4116 /* Garbage */
4117 DebPrint (("Invalid DBCS sequence: %d %d\n",
4118 dbcs[0], dbcs[1]));
4119 inev.kind = NO_EVENT;
4120 break;
4123 else if (IsDBCSLeadByteEx (keyboard_codepage,
4124 (BYTE) msg.msg.wParam))
4126 dbcs_lead = (char) msg.msg.wParam;
4127 inev.kind = NO_EVENT;
4128 break;
4130 else
4132 if (!MultiByteToWideChar (keyboard_codepage, 0,
4133 &dbcs[1], 1, &code, 1))
4135 /* What to do with garbage? */
4136 DebPrint (("Invalid character: %d\n", dbcs[1]));
4137 inev.kind = NO_EVENT;
4138 break;
4141 inev.code = code;
4143 else
4145 /* Windows shouldn't generate WM_CHAR events above 0xFF
4146 in non-Unicode message handlers. */
4147 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4148 inev.kind = NO_EVENT;
4149 break;
4151 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4152 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4154 break;
4156 case WM_APPCOMMAND:
4157 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4159 if (f && !f->iconified)
4161 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4162 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4164 clear_mouse_face (dpyinfo);
4165 dpyinfo->mouse_face_hidden = 1;
4168 if (temp_index == sizeof temp_buffer / sizeof (short))
4169 temp_index = 0;
4170 temp_buffer[temp_index++] = msg.msg.wParam;
4171 inev.kind = MULTIMEDIA_KEY_EVENT;
4172 inev.code = GET_APPCOMMAND_LPARAM(msg.msg.lParam);
4173 inev.modifiers = msg.dwModifiers;
4174 XSETFRAME (inev.frame_or_window, f);
4175 inev.timestamp = msg.msg.time;
4177 break;
4179 case WM_MOUSEMOVE:
4180 /* Ignore non-movement. */
4182 int x = LOWORD (msg.msg.lParam);
4183 int y = HIWORD (msg.msg.lParam);
4184 if (x == last_mousemove_x && y == last_mousemove_y)
4185 break;
4186 last_mousemove_x = x;
4187 last_mousemove_y = y;
4190 previous_help_echo_string = help_echo_string;
4191 help_echo_string = Qnil;
4193 if (dpyinfo->grabbed && last_mouse_frame
4194 && FRAME_LIVE_P (last_mouse_frame))
4195 f = last_mouse_frame;
4196 else
4197 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4199 if (dpyinfo->mouse_face_hidden)
4201 dpyinfo->mouse_face_hidden = 0;
4202 clear_mouse_face (dpyinfo);
4205 if (f)
4207 /* Generate SELECT_WINDOW_EVENTs when needed. */
4208 if (!NILP (Vmouse_autoselect_window))
4210 Lisp_Object window;
4211 int x = LOWORD (msg.msg.lParam);
4212 int y = HIWORD (msg.msg.lParam);
4214 window = window_from_coordinates (f, x, y, 0, 0, 0, 0);
4216 /* Window will be selected only when it is not
4217 selected now and last mouse movement event was
4218 not in it. Minibuffer window will be selected
4219 only when it is active. */
4220 if (WINDOWP(window)
4221 && !EQ (window, last_window)
4222 && !EQ (window, selected_window)
4223 /* For click-to-focus window managers
4224 create event iff we don't leave the
4225 selected frame. */
4226 && (focus_follows_mouse
4227 || (EQ (XWINDOW (window)->frame,
4228 XWINDOW (selected_window)->frame))))
4230 inev.kind = SELECT_WINDOW_EVENT;
4231 inev.frame_or_window = window;
4234 last_window=window;
4236 if (!note_mouse_movement (f, &msg.msg))
4237 help_echo_string = previous_help_echo_string;
4239 else
4241 /* If we move outside the frame, then we're
4242 certainly no longer on any text in the frame. */
4243 clear_mouse_face (dpyinfo);
4246 /* If the contents of the global variable help_echo_string
4247 has changed, generate a HELP_EVENT. */
4248 #if 0 /* The below is an invalid comparison when USE_LISP_UNION_TYPE.
4249 But it was originally changed to this to fix a bug, so I have
4250 not removed it completely in case the bug is still there. */
4251 if (help_echo_string != previous_help_echo_string ||
4252 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4253 #else /* This is what xterm.c does. */
4254 if (!NILP (help_echo_string)
4255 || !NILP (previous_help_echo_string))
4256 do_help = 1;
4257 #endif
4258 break;
4260 case WM_LBUTTONDOWN:
4261 case WM_LBUTTONUP:
4262 case WM_MBUTTONDOWN:
4263 case WM_MBUTTONUP:
4264 case WM_RBUTTONDOWN:
4265 case WM_RBUTTONUP:
4266 case WM_XBUTTONDOWN:
4267 case WM_XBUTTONUP:
4269 /* If we decide we want to generate an event to be seen
4270 by the rest of Emacs, we put it here. */
4271 int tool_bar_p = 0;
4272 int button;
4273 int up;
4275 if (dpyinfo->grabbed && last_mouse_frame
4276 && FRAME_LIVE_P (last_mouse_frame))
4277 f = last_mouse_frame;
4278 else
4279 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4281 if (f)
4283 construct_mouse_click (&inev, &msg, f);
4285 /* Is this in the tool-bar? */
4286 if (WINDOWP (f->tool_bar_window)
4287 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4289 Lisp_Object window;
4290 int x = XFASTINT (inev.x);
4291 int y = XFASTINT (inev.y);
4293 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
4295 if (EQ (window, f->tool_bar_window))
4297 w32_handle_tool_bar_click (f, &inev);
4298 tool_bar_p = 1;
4302 if (tool_bar_p
4303 || (dpyinfo->w32_focus_frame
4304 && f != dpyinfo->w32_focus_frame))
4305 inev.kind = NO_EVENT;
4308 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4309 &button, &up);
4311 if (up)
4313 dpyinfo->grabbed &= ~ (1 << button);
4315 else
4317 dpyinfo->grabbed |= (1 << button);
4318 last_mouse_frame = f;
4319 /* Ignore any mouse motion that happened
4320 before this event; any subsequent mouse-movement
4321 Emacs events should reflect only motion after
4322 the ButtonPress. */
4323 if (f != 0)
4324 f->mouse_moved = 0;
4326 if (!tool_bar_p)
4327 last_tool_bar_item = -1;
4329 break;
4332 case WM_MOUSEWHEEL:
4333 case WM_MOUSEHWHEEL:
4335 if (dpyinfo->grabbed && last_mouse_frame
4336 && FRAME_LIVE_P (last_mouse_frame))
4337 f = last_mouse_frame;
4338 else
4339 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4341 if (f)
4344 if (!dpyinfo->w32_focus_frame
4345 || f == dpyinfo->w32_focus_frame)
4347 /* Emit an Emacs wheel-up/down event. */
4348 construct_mouse_wheel (&inev, &msg, f);
4350 /* Ignore any mouse motion that happened before this
4351 event; any subsequent mouse-movement Emacs events
4352 should reflect only motion after the
4353 ButtonPress. */
4354 f->mouse_moved = 0;
4356 last_mouse_frame = f;
4357 last_tool_bar_item = -1;
4359 break;
4361 case WM_DROPFILES:
4362 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4364 if (f)
4365 construct_drag_n_drop (&inev, &msg, f);
4366 break;
4368 case WM_VSCROLL:
4370 struct scroll_bar *bar =
4371 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4373 if (bar)
4374 w32_scroll_bar_handle_click (bar, &msg, &inev);
4375 break;
4378 case WM_WINDOWPOSCHANGED:
4379 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4380 if (f)
4382 if (f->want_fullscreen & FULLSCREEN_WAIT)
4383 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
4385 check_visibility = 1;
4386 break;
4388 case WM_ACTIVATE:
4389 case WM_ACTIVATEAPP:
4390 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4391 if (f)
4392 x_check_fullscreen (f);
4393 check_visibility = 1;
4394 break;
4396 case WM_MOVE:
4397 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4399 if (f && !f->async_iconified)
4401 int x, y;
4403 x_real_positions (f, &x, &y);
4404 f->left_pos = x;
4405 f->top_pos = y;
4408 check_visibility = 1;
4409 break;
4411 case WM_SHOWWINDOW:
4412 /* wParam non-zero means Window is about to be shown, 0 means
4413 about to be hidden. */
4414 /* Redo the mouse-highlight after the tooltip has gone. */
4415 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4417 tip_window = NULL;
4418 redo_mouse_highlight ();
4421 /* If window has been obscured or exposed by another window
4422 being maximised or minimised/restored, then recheck
4423 visibility of all frames. Direct changes to our own
4424 windows get handled by WM_SIZE. */
4425 #if 0
4426 if (msg.msg.lParam != 0)
4427 check_visibility = 1;
4428 else
4430 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4431 f->async_visible = msg.msg.wParam;
4433 #endif
4435 check_visibility = 1;
4436 break;
4438 case WM_SIZE:
4439 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4441 /* Inform lisp of whether frame has been iconified etc. */
4442 if (f)
4444 switch (msg.msg.wParam)
4446 case SIZE_MINIMIZED:
4447 f->async_visible = 0;
4448 f->async_iconified = 1;
4450 inev.kind = ICONIFY_EVENT;
4451 XSETFRAME (inev.frame_or_window, f);
4452 break;
4454 case SIZE_MAXIMIZED:
4455 case SIZE_RESTORED:
4456 f->async_visible = 1;
4457 f->async_iconified = 0;
4459 /* wait_reading_process_output will notice this and update
4460 the frame's display structures. */
4461 SET_FRAME_GARBAGED (f);
4463 if (f->iconified)
4465 int x, y;
4467 /* Reset top and left positions of the Window
4468 here since Windows sends a WM_MOVE message
4469 BEFORE telling us the Window is minimized
4470 when the Window is iconified, with 3000,3000
4471 as the co-ords. */
4472 x_real_positions (f, &x, &y);
4473 f->left_pos = x;
4474 f->top_pos = y;
4476 inev.kind = DEICONIFY_EVENT;
4477 XSETFRAME (inev.frame_or_window, f);
4479 else if (! NILP (Vframe_list)
4480 && ! NILP (XCDR (Vframe_list)))
4481 /* Force a redisplay sooner or later
4482 to update the frame titles
4483 in case this is the second frame. */
4484 record_asynch_buffer_change ();
4485 break;
4489 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
4491 RECT rect;
4492 int rows;
4493 int columns;
4494 int width;
4495 int height;
4497 GetClientRect (msg.msg.hwnd, &rect);
4499 height = rect.bottom - rect.top;
4500 width = rect.right - rect.left;
4502 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4503 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4505 /* TODO: Clip size to the screen dimensions. */
4507 /* Even if the number of character rows and columns has
4508 not changed, the font size may have changed, so we need
4509 to check the pixel dimensions as well. */
4511 if (columns != FRAME_COLS (f)
4512 || rows != FRAME_LINES (f)
4513 || width != FRAME_PIXEL_WIDTH (f)
4514 || height != FRAME_PIXEL_HEIGHT (f))
4516 change_frame_size (f, rows, columns, 0, 1, 0);
4517 SET_FRAME_GARBAGED (f);
4518 cancel_mouse_face (f);
4519 FRAME_PIXEL_WIDTH (f) = width;
4520 FRAME_PIXEL_HEIGHT (f) = height;
4521 f->win_gravity = NorthWestGravity;
4525 check_visibility = 1;
4526 break;
4528 case WM_MOUSELEAVE:
4529 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4530 if (f)
4532 if (f == dpyinfo->mouse_face_mouse_frame)
4534 /* If we move outside the frame, then we're
4535 certainly no longer on any text in the frame. */
4536 clear_mouse_face (dpyinfo);
4537 dpyinfo->mouse_face_mouse_frame = 0;
4540 /* Generate a nil HELP_EVENT to cancel a help-echo.
4541 Do it only if there's something to cancel.
4542 Otherwise, the startup message is cleared when
4543 the mouse leaves the frame. */
4544 if (any_help_event_p)
4545 do_help = -1;
4547 break;
4549 case WM_SETFOCUS:
4550 w32_detect_focus_change (dpyinfo, &msg, &inev);
4552 dpyinfo->grabbed = 0;
4553 check_visibility = 1;
4554 break;
4556 case WM_KILLFOCUS:
4557 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4559 if (f)
4561 if (f == dpyinfo->w32_focus_event_frame)
4562 dpyinfo->w32_focus_event_frame = 0;
4564 if (f == dpyinfo->w32_focus_frame)
4565 x_new_focus_frame (dpyinfo, 0);
4567 if (f == dpyinfo->mouse_face_mouse_frame)
4569 /* If we move outside the frame, then we're
4570 certainly no longer on any text in the frame. */
4571 clear_mouse_face (dpyinfo);
4572 dpyinfo->mouse_face_mouse_frame = 0;
4575 /* Generate a nil HELP_EVENT to cancel a help-echo.
4576 Do it only if there's something to cancel.
4577 Otherwise, the startup message is cleared when
4578 the mouse leaves the frame. */
4579 if (any_help_event_p)
4580 do_help = -1;
4583 dpyinfo->grabbed = 0;
4584 check_visibility = 1;
4585 break;
4587 case WM_CLOSE:
4588 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4590 if (f)
4592 inev.kind = DELETE_WINDOW_EVENT;
4593 XSETFRAME (inev.frame_or_window, f);
4595 break;
4597 case WM_INITMENU:
4598 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4600 if (f)
4602 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4603 XSETFRAME (inev.frame_or_window, f);
4605 break;
4607 case WM_COMMAND:
4608 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4610 if (f)
4612 extern void menubar_selection_callback
4613 (FRAME_PTR f, void * client_data);
4614 menubar_selection_callback (f, (void *)msg.msg.wParam);
4617 check_visibility = 1;
4618 break;
4620 case WM_DISPLAYCHANGE:
4621 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4623 if (f)
4625 dpyinfo->n_cbits = msg.msg.wParam;
4626 DebPrint (("display change: %d %d\n",
4627 (short) LOWORD (msg.msg.lParam),
4628 (short) HIWORD (msg.msg.lParam)));
4631 check_visibility = 1;
4632 break;
4634 default:
4635 /* Check for messages registered at runtime. */
4636 if (msg.msg.message == msh_mousewheel)
4638 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4639 msg.msg.message = WM_MOUSEWHEEL;
4640 prepend_msg (&msg);
4642 break;
4645 if (inev.kind != NO_EVENT)
4647 kbd_buffer_store_event_hold (&inev, hold_quit);
4648 count++;
4651 if (do_help
4652 && !(hold_quit && hold_quit->kind != NO_EVENT))
4654 Lisp_Object frame;
4656 if (f)
4657 XSETFRAME (frame, f);
4658 else
4659 frame = Qnil;
4661 if (do_help > 0)
4663 if (NILP (help_echo_string))
4665 help_echo_object = help_echo_window = Qnil;
4666 help_echo_pos = -1;
4669 any_help_event_p = 1;
4670 gen_help_event (help_echo_string, frame, help_echo_window,
4671 help_echo_object, help_echo_pos);
4673 else
4675 help_echo_string = Qnil;
4676 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4678 count++;
4682 /* If the focus was just given to an autoraising frame,
4683 raise it now. */
4684 /* ??? This ought to be able to handle more than one such frame. */
4685 if (pending_autoraise_frame)
4687 x_raise_frame (pending_autoraise_frame);
4688 pending_autoraise_frame = 0;
4691 /* Check which frames are still visisble, if we have enqueued any user
4692 events or been notified of events that may affect visibility. We
4693 do this here because there doesn't seem to be any direct
4694 notification from Windows that the visibility of a window has
4695 changed (at least, not in all cases). */
4696 if (count > 0 || check_visibility)
4698 Lisp_Object tail, frame;
4700 FOR_EACH_FRAME (tail, frame)
4702 FRAME_PTR f = XFRAME (frame);
4703 /* The tooltip has been drawn already. Avoid the
4704 SET_FRAME_GARBAGED below. */
4705 if (EQ (frame, tip_frame))
4706 continue;
4708 /* Check "visible" frames and mark each as obscured or not.
4709 Note that async_visible is nonzero for unobscured and
4710 obscured frames, but zero for hidden and iconified frames. */
4711 if (FRAME_W32_P (f) && f->async_visible)
4713 RECT clipbox;
4714 HDC hdc;
4716 enter_crit ();
4717 /* Query clipping rectangle for the entire window area
4718 (GetWindowDC), not just the client portion (GetDC).
4719 Otherwise, the scrollbars and menubar aren't counted as
4720 part of the visible area of the frame, and we may think
4721 the frame is obscured when really a scrollbar is still
4722 visible and gets WM_PAINT messages above. */
4723 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
4724 GetClipBox (hdc, &clipbox);
4725 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
4726 leave_crit ();
4728 if (clipbox.right == clipbox.left
4729 || clipbox.bottom == clipbox.top)
4731 /* Frame has become completely obscured so mark as
4732 such (we do this by setting async_visible to 2 so
4733 that FRAME_VISIBLE_P is still true, but redisplay
4734 will skip it). */
4735 f->async_visible = 2;
4737 if (!FRAME_OBSCURED_P (f))
4739 DebPrint (("frame %p (%s) obscured\n", f,
4740 SDATA (f->name)));
4743 else
4745 /* Frame is not obscured, so mark it as such. */
4746 f->async_visible = 1;
4748 if (FRAME_OBSCURED_P (f))
4750 SET_FRAME_GARBAGED (f);
4751 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
4752 SDATA (f->name)));
4754 /* Force a redisplay sooner or later. */
4755 record_asynch_buffer_change ();
4762 UNBLOCK_INPUT;
4763 return count;
4768 /***********************************************************************
4769 Text Cursor
4770 ***********************************************************************/
4772 /* Set clipping for output in glyph row ROW. W is the window in which
4773 we operate. GC is the graphics context to set clipping in.
4775 ROW may be a text row or, e.g., a mode line. Text rows must be
4776 clipped to the interior of the window dedicated to text display,
4777 mode lines must be clipped to the whole window. */
4779 static void
4780 w32_clip_to_row (struct window *w, struct glyph_row *row, int area, HDC hdc)
4782 struct frame *f = XFRAME (WINDOW_FRAME (w));
4783 RECT clip_rect;
4784 int window_x, window_y, window_width;
4786 window_box (w, area, &window_x, &window_y, &window_width, 0);
4788 clip_rect.left = window_x;
4789 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4790 clip_rect.top = max (clip_rect.top, window_y);
4791 clip_rect.right = clip_rect.left + window_width;
4792 clip_rect.bottom = clip_rect.top + row->visible_height;
4794 w32_set_clip_rectangle (hdc, &clip_rect);
4798 /* Draw a hollow box cursor on window W in glyph row ROW. */
4800 static void
4801 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
4803 struct frame *f = XFRAME (WINDOW_FRAME (w));
4804 HDC hdc;
4805 RECT rect;
4806 int left, top, h;
4807 struct glyph *cursor_glyph;
4808 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
4810 /* Get the glyph the cursor is on. If we can't tell because
4811 the current matrix is invalid or such, give up. */
4812 cursor_glyph = get_phys_cursor_glyph (w);
4813 if (cursor_glyph == NULL)
4814 return;
4816 /* Compute frame-relative coordinates for phys cursor. */
4817 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
4818 rect.left = left;
4819 rect.top = top;
4820 rect.bottom = rect.top + h;
4821 rect.right = rect.left + w->phys_cursor_width;
4823 hdc = get_frame_dc (f);
4824 /* Set clipping, draw the rectangle, and reset clipping again. */
4825 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4826 FrameRect (hdc, &rect, hb);
4827 DeleteObject (hb);
4828 w32_set_clip_rectangle (hdc, NULL);
4829 release_frame_dc (f, hdc);
4833 /* Draw a bar cursor on window W in glyph row ROW.
4835 Implementation note: One would like to draw a bar cursor with an
4836 angle equal to the one given by the font property XA_ITALIC_ANGLE.
4837 Unfortunately, I didn't find a font yet that has this property set.
4838 --gerd. */
4840 static void
4841 x_draw_bar_cursor (struct window *w, struct glyph_row *row,
4842 int width, enum text_cursor_kinds kind)
4844 struct frame *f = XFRAME (w->frame);
4845 struct glyph *cursor_glyph;
4847 /* If cursor is out of bounds, don't draw garbage. This can happen
4848 in mini-buffer windows when switching between echo area glyphs
4849 and mini-buffer. */
4850 cursor_glyph = get_phys_cursor_glyph (w);
4851 if (cursor_glyph == NULL)
4852 return;
4854 /* If on an image, draw like a normal cursor. That's usually better
4855 visible than drawing a bar, esp. if the image is large so that
4856 the bar might not be in the window. */
4857 if (cursor_glyph->type == IMAGE_GLYPH)
4859 struct glyph_row *row;
4860 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
4861 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
4863 else
4865 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
4866 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
4867 int x;
4868 HDC hdc;
4870 /* If the glyph's background equals the color we normally draw
4871 the bar cursor in, the bar cursor in its normal color is
4872 invisible. Use the glyph's foreground color instead in this
4873 case, on the assumption that the glyph's colors are chosen so
4874 that the glyph is legible. */
4875 if (face->background == cursor_color)
4876 cursor_color = face->foreground;
4878 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
4880 hdc = get_frame_dc (f);
4881 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4883 if (kind == BAR_CURSOR)
4885 if (width < 0)
4886 width = FRAME_CURSOR_WIDTH (f);
4887 width = min (cursor_glyph->pixel_width, width);
4889 w->phys_cursor_width = width;
4891 w32_fill_area (f, hdc, cursor_color, x,
4892 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
4893 width, row->height);
4895 else
4897 int dummy_x, dummy_y, dummy_h;
4899 if (width < 0)
4900 width = row->height;
4902 width = min (row->height, width);
4904 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
4905 &dummy_y, &dummy_h);
4906 w32_fill_area (f, hdc, cursor_color, x,
4907 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
4908 row->height - width),
4909 w->phys_cursor_width, width);
4912 w32_set_clip_rectangle (hdc, NULL);
4913 release_frame_dc (f, hdc);
4918 /* RIF: Define cursor CURSOR on frame F. */
4920 static void
4921 w32_define_frame_cursor (struct frame *f, Cursor cursor)
4923 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
4927 /* RIF: Clear area on frame F. */
4929 static void
4930 w32_clear_frame_area (struct frame *f, int x, int y, int width, int height)
4932 HDC hdc;
4934 hdc = get_frame_dc (f);
4935 w32_clear_area (f, hdc, x, y, width, height);
4936 release_frame_dc (f, hdc);
4939 /* RIF: Draw or clear cursor on window W. */
4941 static void
4942 w32_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
4943 int x, int y, int cursor_type, int cursor_width,
4944 int on_p, int active_p)
4946 if (on_p)
4948 /* If the user wants to use the system caret, make sure our own
4949 cursor remains invisible. */
4950 if (w32_use_visible_system_caret)
4952 /* Call to erase_phys_cursor here seems to use the
4953 wrong values of w->phys_cursor, as they have been
4954 overwritten before this function was called. */
4955 if (w->phys_cursor_type != NO_CURSOR)
4956 erase_phys_cursor (w);
4958 cursor_type = w->phys_cursor_type = NO_CURSOR;
4959 w->phys_cursor_width = -1;
4961 else
4963 w->phys_cursor_type = cursor_type;
4966 w->phys_cursor_on_p = 1;
4968 /* If this is the active cursor, we need to track it with the
4969 system caret, so third party software like screen magnifiers
4970 and speech synthesizers can follow the cursor. */
4971 if (active_p)
4973 struct frame *f = XFRAME (WINDOW_FRAME (w));
4974 HWND hwnd = FRAME_W32_WINDOW (f);
4976 w32_system_caret_x
4977 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
4978 w32_system_caret_y
4979 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
4980 + glyph_row->ascent - w->phys_cursor_ascent);
4982 PostMessage (hwnd, WM_IME_STARTCOMPOSITION, 0, 0);
4984 /* If the size of the active cursor changed, destroy the old
4985 system caret. */
4986 if (w32_system_caret_hwnd
4987 && (w32_system_caret_height != w->phys_cursor_height))
4988 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
4990 w32_system_caret_height = w->phys_cursor_height;
4992 /* Move the system caret. */
4993 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
4996 if (glyph_row->exact_window_width_line_p
4997 && (glyph_row->reversed_p
4998 ? (w->phys_cursor.hpos < 0)
4999 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
5001 glyph_row->cursor_in_fringe_p = 1;
5002 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
5003 return;
5006 switch (cursor_type)
5008 case HOLLOW_BOX_CURSOR:
5009 x_draw_hollow_cursor (w, glyph_row);
5010 break;
5012 case FILLED_BOX_CURSOR:
5013 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5014 break;
5016 case BAR_CURSOR:
5017 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5018 break;
5020 case HBAR_CURSOR:
5021 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5022 break;
5024 case NO_CURSOR:
5025 w->phys_cursor_width = 0;
5026 break;
5028 default:
5029 abort ();
5036 /* Icons. */
5039 x_bitmap_icon (struct frame *f, Lisp_Object icon)
5041 HANDLE main_icon;
5042 HANDLE small_icon = NULL;
5044 if (FRAME_W32_WINDOW (f) == 0)
5045 return 1;
5047 if (NILP (icon))
5048 main_icon = LoadIcon (hinst, EMACS_CLASS);
5049 else if (STRINGP (icon))
5051 /* Load the main icon from the named file. */
5052 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5053 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5054 /* Try to load a small icon to go with it. */
5055 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5056 GetSystemMetrics (SM_CXSMICON),
5057 GetSystemMetrics (SM_CYSMICON),
5058 LR_LOADFROMFILE);
5060 else if (SYMBOLP (icon))
5062 LPCTSTR name;
5064 if (EQ (icon, intern ("application")))
5065 name = (LPCTSTR) IDI_APPLICATION;
5066 else if (EQ (icon, intern ("hand")))
5067 name = (LPCTSTR) IDI_HAND;
5068 else if (EQ (icon, intern ("question")))
5069 name = (LPCTSTR) IDI_QUESTION;
5070 else if (EQ (icon, intern ("exclamation")))
5071 name = (LPCTSTR) IDI_EXCLAMATION;
5072 else if (EQ (icon, intern ("asterisk")))
5073 name = (LPCTSTR) IDI_ASTERISK;
5074 else if (EQ (icon, intern ("winlogo")))
5075 name = (LPCTSTR) IDI_WINLOGO;
5076 else
5077 return 1;
5079 main_icon = LoadIcon (NULL, name);
5081 else
5082 return 1;
5084 if (main_icon == NULL)
5085 return 1;
5087 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5088 (LPARAM) main_icon);
5090 /* If there is a small icon that goes with it, set that too. */
5091 if (small_icon)
5092 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5093 (LPARAM) small_icon);
5095 return 0;
5099 /************************************************************************
5100 Handling X errors
5101 ************************************************************************/
5103 /* Display Error Handling functions not used on W32. Listing them here
5104 helps diff stay in step when comparing w32term.c with xterm.c.
5106 x_error_catcher (display, error)
5107 x_catch_errors (dpy)
5108 x_catch_errors_unwind (old_val)
5109 x_check_errors (dpy, format)
5110 x_fully_uncatch_errors ()
5111 x_catching_errors ()
5112 x_had_errors_p (dpy)
5113 x_clear_errors (dpy)
5114 x_uncatch_errors (dpy, count)
5115 x_trace_wire ()
5116 x_connection_signal (signalnum)
5117 x_connection_closed (dpy, error_message)
5118 x_error_quitter (display, error)
5119 x_error_handler (display, error)
5120 x_io_error_quitter (display)
5125 /* Changing the font of the frame. */
5127 Lisp_Object
5128 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
5130 struct font *font = XFONT_OBJECT (font_object);
5132 if (fontset < 0)
5133 fontset = fontset_from_font (font_object);
5134 FRAME_FONTSET (f) = fontset;
5135 if (FRAME_FONT (f) == font)
5136 /* This font is already set in frame F. There's nothing more to
5137 do. */
5138 return font_object;
5140 FRAME_FONT (f) = font;
5141 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5142 FRAME_COLUMN_WIDTH (f) = font->average_width;
5143 FRAME_SPACE_WIDTH (f) = font->space_width;
5144 FRAME_LINE_HEIGHT (f) = font->height;
5146 compute_fringe_widths (f, 1);
5148 /* Compute the scroll bar width in character columns. */
5149 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5151 int wid = FRAME_COLUMN_WIDTH (f);
5152 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5153 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5155 else
5157 int wid = FRAME_COLUMN_WIDTH (f);
5158 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5161 /* Now make the frame display the given font. */
5162 if (FRAME_X_WINDOW (f) != 0)
5164 /* Don't change the size of a tip frame; there's no point in
5165 doing it because it's done in Fx_show_tip, and it leads to
5166 problems because the tip frame has no widget. */
5167 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5168 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5171 /* X version sets font of input methods here also. */
5173 return font_object;
5177 /***********************************************************************
5178 TODO: W32 Input Methods
5179 ***********************************************************************/
5180 /* Listing missing functions from xterm.c helps diff stay in step.
5182 xim_destroy_callback (xim, client_data, call_data)
5183 xim_open_dpy (dpyinfo, resource_name)
5184 struct xim_inst_t
5185 xim_instantiate_callback (display, client_data, call_data)
5186 xim_initialize (dpyinfo, resource_name)
5187 xim_close_dpy (dpyinfo)
5192 /* Calculate the absolute position in frame F
5193 from its current recorded position values and gravity. */
5195 void
5196 x_calc_absolute_position (struct frame *f)
5198 int flags = f->size_hint_flags;
5200 /* The sum of the widths of the frame's left and right borders, and
5201 the sum of the heights of the frame's top and bottom borders (in
5202 pixels) drawn by Windows. */
5203 unsigned int left_right_borders_width, top_bottom_borders_height;
5205 /* Try to get the actual values of these two variables. We compute
5206 the border width (height) by subtracting the width (height) of
5207 the frame's client area from the width (height) of the frame's
5208 entire window. */
5209 WINDOWPLACEMENT wp = { 0 };
5210 RECT client_rect = { 0 };
5212 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5213 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5215 left_right_borders_width =
5216 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5217 (client_rect.right - client_rect.left);
5219 top_bottom_borders_height =
5220 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5221 (client_rect.bottom - client_rect.top);
5223 else
5225 /* Use sensible default values. */
5226 left_right_borders_width = 8;
5227 top_bottom_borders_height = 32;
5230 /* Treat negative positions as relative to the rightmost bottommost
5231 position that fits on the screen. */
5232 if (flags & XNegative)
5233 f->left_pos = (x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f))
5234 - FRAME_PIXEL_WIDTH (f)
5235 + f->left_pos
5236 - (left_right_borders_width - 1));
5238 if (flags & YNegative)
5239 f->top_pos = (x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f))
5240 - FRAME_PIXEL_HEIGHT (f)
5241 + f->top_pos
5242 - (top_bottom_borders_height - 1));
5244 /* The left_pos and top_pos are now relative to the top and left
5245 screen edges, so the flags should correspond. */
5246 f->size_hint_flags &= ~ (XNegative | YNegative);
5249 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5250 to really change the position, and 0 when calling from
5251 x_make_frame_visible (in that case, XOFF and YOFF are the current
5252 position values). It is -1 when calling from x_set_frame_parameters,
5253 which means, do adjust for borders but don't change the gravity. */
5255 void
5256 x_set_offset (struct frame *f, register int xoff, register int yoff,
5257 int change_gravity)
5259 int modified_top, modified_left;
5261 if (change_gravity > 0)
5263 f->top_pos = yoff;
5264 f->left_pos = xoff;
5265 f->size_hint_flags &= ~ (XNegative | YNegative);
5266 if (xoff < 0)
5267 f->size_hint_flags |= XNegative;
5268 if (yoff < 0)
5269 f->size_hint_flags |= YNegative;
5270 f->win_gravity = NorthWestGravity;
5272 x_calc_absolute_position (f);
5274 BLOCK_INPUT;
5275 x_wm_set_size_hint (f, (long) 0, 0);
5277 modified_left = f->left_pos;
5278 modified_top = f->top_pos;
5280 my_set_window_pos (FRAME_W32_WINDOW (f),
5281 NULL,
5282 modified_left, modified_top,
5283 0, 0,
5284 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5285 UNBLOCK_INPUT;
5289 /* Check if we need to resize the frame due to a fullscreen request.
5290 If so needed, resize the frame. */
5291 static void
5292 x_check_fullscreen (struct frame *f)
5294 if (f->want_fullscreen & FULLSCREEN_BOTH)
5296 int width, height, ign;
5298 x_real_positions (f, &f->left_pos, &f->top_pos);
5300 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5302 /* We do not need to move the window, it shall be taken care of
5303 when setting WM manager hints. */
5304 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5306 change_frame_size (f, height, width, 0, 1, 0);
5307 SET_FRAME_GARBAGED (f);
5308 cancel_mouse_face (f);
5310 /* Wait for the change of frame size to occur */
5311 f->want_fullscreen |= FULLSCREEN_WAIT;
5316 /* Call this to change the size of frame F's x-window.
5317 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5318 for this size change and subsequent size changes.
5319 Otherwise we leave the window gravity unchanged. */
5321 void
5322 x_set_window_size (struct frame *f, int change_gravity, int cols, int rows)
5324 int pixelwidth, pixelheight;
5326 BLOCK_INPUT;
5328 check_frame_size (f, &rows, &cols);
5329 f->scroll_bar_actual_width
5330 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5332 compute_fringe_widths (f, 0);
5334 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5335 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5337 f->win_gravity = NorthWestGravity;
5338 x_wm_set_size_hint (f, (long) 0, 0);
5341 RECT rect;
5343 rect.left = rect.top = 0;
5344 rect.right = pixelwidth;
5345 rect.bottom = pixelheight;
5347 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
5348 FRAME_EXTERNAL_MENU_BAR (f));
5350 my_set_window_pos (FRAME_W32_WINDOW (f),
5351 NULL,
5352 0, 0,
5353 rect.right - rect.left,
5354 rect.bottom - rect.top,
5355 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5358 #if 0
5359 /* The following mirrors what is done in xterm.c. It appears to be
5360 for informing lisp of the new size immediately, while the actual
5361 resize will happen asynchronously. But on Windows, the menu bar
5362 automatically wraps when the frame is too narrow to contain it,
5363 and that causes any calculations made here to come out wrong. The
5364 end is some nasty buggy behavior, including the potential loss
5365 of the minibuffer.
5367 Disabling this code is either not sufficient to fix the problems
5368 completely, or it causes fresh problems, but at least it removes
5369 the most problematic symptom of the minibuffer becoming unusable.
5371 -----------------------------------------------------------------
5373 Now, strictly speaking, we can't be sure that this is accurate,
5374 but the window manager will get around to dealing with the size
5375 change request eventually, and we'll hear how it went when the
5376 ConfigureNotify event gets here.
5378 We could just not bother storing any of this information here,
5379 and let the ConfigureNotify event set everything up, but that
5380 might be kind of confusing to the Lisp code, since size changes
5381 wouldn't be reported in the frame parameters until some random
5382 point in the future when the ConfigureNotify event arrives.
5384 We pass 1 for DELAY since we can't run Lisp code inside of
5385 a BLOCK_INPUT. */
5386 change_frame_size (f, rows, cols, 0, 1, 0);
5387 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5388 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5390 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5391 receive in the ConfigureNotify event; if we get what we asked
5392 for, then the event won't cause the screen to become garbaged, so
5393 we have to make sure to do it here. */
5394 SET_FRAME_GARBAGED (f);
5396 /* If cursor was outside the new size, mark it as off. */
5397 mark_window_cursors_off (XWINDOW (f->root_window));
5399 /* Clear out any recollection of where the mouse highlighting was,
5400 since it might be in a place that's outside the new frame size.
5401 Actually checking whether it is outside is a pain in the neck,
5402 so don't try--just let the highlighting be done afresh with new size. */
5403 cancel_mouse_face (f);
5404 #endif
5406 UNBLOCK_INPUT;
5409 /* Mouse warping. */
5411 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5413 void
5414 x_set_mouse_position (struct frame *f, int x, int y)
5416 int pix_x, pix_y;
5418 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5419 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5421 if (pix_x < 0) pix_x = 0;
5422 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5424 if (pix_y < 0) pix_y = 0;
5425 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5427 x_set_mouse_pixel_position (f, pix_x, pix_y);
5430 void
5431 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
5433 RECT rect;
5434 POINT pt;
5436 BLOCK_INPUT;
5438 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5439 pt.x = rect.left + pix_x;
5440 pt.y = rect.top + pix_y;
5441 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5443 SetCursorPos (pt.x, pt.y);
5445 UNBLOCK_INPUT;
5449 /* focus shifting, raising and lowering. */
5451 void
5452 x_focus_on_frame (struct frame *f)
5454 struct w32_display_info *dpyinfo = &one_w32_display_info;
5456 /* Give input focus to frame. */
5457 BLOCK_INPUT;
5458 #if 0
5459 /* Try not to change its Z-order if possible. */
5460 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5461 my_set_focus (f, FRAME_W32_WINDOW (f));
5462 else
5463 #endif
5464 my_set_foreground_window (FRAME_W32_WINDOW (f));
5465 UNBLOCK_INPUT;
5468 void
5469 x_unfocus_frame (struct frame *f)
5473 /* Raise frame F. */
5474 void
5475 x_raise_frame (struct frame *f)
5477 BLOCK_INPUT;
5479 /* Strictly speaking, raise-frame should only change the frame's Z
5480 order, leaving input focus unchanged. This is reasonable behavior
5481 on X where the usual policy is point-to-focus. However, this
5482 behavior would be very odd on Windows where the usual policy is
5483 click-to-focus.
5485 On X, if the mouse happens to be over the raised frame, it gets
5486 input focus anyway (so the window with focus will never be
5487 completely obscured) - if not, then just moving the mouse over it
5488 is sufficient to give it focus. On Windows, the user must actually
5489 click on the frame (preferrably the title bar so as not to move
5490 point), which is more awkward. Also, no other Windows program
5491 raises a window to the top but leaves another window (possibly now
5492 completely obscured) with input focus.
5494 Because there is a system setting on Windows that allows the user
5495 to choose the point to focus policy, we make the strict semantics
5496 optional, but by default we grab focus when raising. */
5498 if (NILP (Vw32_grab_focus_on_raise))
5500 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5501 not already the foreground application: the frame is raised
5502 above all other frames belonging to us, but not above the
5503 current top window. To achieve that, we have to resort to this
5504 more cumbersome method. */
5506 HDWP handle = BeginDeferWindowPos (2);
5507 if (handle)
5509 DeferWindowPos (handle,
5510 FRAME_W32_WINDOW (f),
5511 HWND_TOP,
5512 0, 0, 0, 0,
5513 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5515 DeferWindowPos (handle,
5516 GetForegroundWindow (),
5517 FRAME_W32_WINDOW (f),
5518 0, 0, 0, 0,
5519 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5521 EndDeferWindowPos (handle);
5524 else
5526 my_set_foreground_window (FRAME_W32_WINDOW (f));
5529 UNBLOCK_INPUT;
5532 /* Lower frame F. */
5533 void
5534 x_lower_frame (struct frame *f)
5536 BLOCK_INPUT;
5537 my_set_window_pos (FRAME_W32_WINDOW (f),
5538 HWND_BOTTOM,
5539 0, 0, 0, 0,
5540 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5541 UNBLOCK_INPUT;
5544 static void
5545 w32_frame_raise_lower (FRAME_PTR f, int raise_flag)
5547 if (! FRAME_W32_P (f))
5548 return;
5550 if (raise_flag)
5551 x_raise_frame (f);
5552 else
5553 x_lower_frame (f);
5556 /* Change of visibility. */
5558 /* This tries to wait until the frame is really visible.
5559 However, if the window manager asks the user where to position
5560 the frame, this will return before the user finishes doing that.
5561 The frame will not actually be visible at that time,
5562 but it will become visible later when the window manager
5563 finishes with it. */
5565 void
5566 x_make_frame_visible (struct frame *f)
5568 Lisp_Object type;
5570 BLOCK_INPUT;
5572 type = x_icon_type (f);
5573 if (!NILP (type))
5574 x_bitmap_icon (f, type);
5576 if (! FRAME_VISIBLE_P (f))
5578 /* We test FRAME_GARBAGED_P here to make sure we don't
5579 call x_set_offset a second time
5580 if we get to x_make_frame_visible a second time
5581 before the window gets really visible. */
5582 if (! FRAME_ICONIFIED_P (f)
5583 && ! f->output_data.w32->asked_for_visible)
5585 RECT workarea_rect;
5586 RECT window_rect;
5588 /* Adjust vertical window position in order to avoid being
5589 covered by a task bar placed at the bottom of the desktop. */
5590 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
5591 GetWindowRect(FRAME_W32_WINDOW(f), &window_rect);
5592 if (window_rect.bottom > workarea_rect.bottom
5593 && window_rect.top > workarea_rect.top)
5594 f->top_pos = max (window_rect.top
5595 - window_rect.bottom + workarea_rect.bottom,
5596 workarea_rect.top);
5598 x_set_offset (f, f->left_pos, f->top_pos, 0);
5601 f->output_data.w32->asked_for_visible = 1;
5603 /* The first of these seems to give more expected behavior, but
5604 was added as a commented out line in Sept 1997, with the
5605 second version remaining uncommented. There may have been
5606 some problem with it that led to it not being enabled,
5607 so the old version remains commented out below in case we
5608 decide we need to go back to it [23.0.60 2008-06-09]. */
5609 my_show_window (f, FRAME_W32_WINDOW (f),
5610 f->async_iconified ? SW_RESTORE : SW_SHOW);
5611 /* my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL); */
5614 /* Synchronize to ensure Emacs knows the frame is visible
5615 before we do anything else. We do this loop with input not blocked
5616 so that incoming events are handled. */
5618 Lisp_Object frame;
5619 int count;
5621 /* This must come after we set COUNT. */
5622 UNBLOCK_INPUT;
5624 XSETFRAME (frame, f);
5626 /* Wait until the frame is visible. Process X events until a
5627 MapNotify event has been seen, or until we think we won't get a
5628 MapNotify at all.. */
5629 for (count = input_signal_count + 10;
5630 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5632 /* Force processing of queued events. */
5633 /* TODO: x_sync equivalent? */
5635 /* Machines that do polling rather than SIGIO have been observed
5636 to go into a busy-wait here. So we'll fake an alarm signal
5637 to let the handler know that there's something to be read.
5638 We used to raise a real alarm, but it seems that the handler
5639 isn't always enabled here. This is probably a bug. */
5640 if (input_polling_used ())
5642 /* It could be confusing if a real alarm arrives while processing
5643 the fake one. Turn it off and let the handler reset it. */
5644 int old_poll_suppress_count = poll_suppress_count;
5645 poll_suppress_count = 1;
5646 poll_for_input_1 ();
5647 poll_suppress_count = old_poll_suppress_count;
5650 FRAME_SAMPLE_VISIBILITY (f);
5654 /* Change from mapped state to withdrawn state. */
5656 /* Make the frame visible (mapped and not iconified). */
5658 void
5659 x_make_frame_invisible (struct frame *f)
5661 /* Don't keep the highlight on an invisible frame. */
5662 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5663 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5665 BLOCK_INPUT;
5667 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
5669 /* We can't distinguish this from iconification
5670 just by the event that we get from the server.
5671 So we can't win using the usual strategy of letting
5672 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5673 and synchronize with the server to make sure we agree. */
5674 f->visible = 0;
5675 FRAME_ICONIFIED_P (f) = 0;
5676 f->async_visible = 0;
5677 f->async_iconified = 0;
5679 UNBLOCK_INPUT;
5682 /* Change window state from mapped to iconified. */
5684 void
5685 x_iconify_frame (struct frame *f)
5687 Lisp_Object type;
5689 /* Don't keep the highlight on an invisible frame. */
5690 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5691 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5693 if (f->async_iconified)
5694 return;
5696 BLOCK_INPUT;
5698 type = x_icon_type (f);
5699 if (!NILP (type))
5700 x_bitmap_icon (f, type);
5702 /* Simulate the user minimizing the frame. */
5703 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
5705 UNBLOCK_INPUT;
5709 /* Free X resources of frame F. */
5711 void
5712 x_free_frame_resources (f)
5713 struct frame *f;
5715 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5717 BLOCK_INPUT;
5719 /* We must free faces before destroying windows because some
5720 font-driver (e.g. xft) access a window while finishing a
5721 face. */
5722 if (FRAME_FACE_CACHE (f))
5723 free_frame_faces (f);
5725 if (FRAME_W32_WINDOW (f))
5726 my_destroy_window (f, FRAME_W32_WINDOW (f));
5728 free_frame_menubar (f);
5730 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
5731 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
5732 unload_color (f, f->output_data.w32->cursor_pixel);
5733 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
5734 unload_color (f, f->output_data.w32->border_pixel);
5735 unload_color (f, f->output_data.w32->mouse_pixel);
5736 if (f->output_data.w32->white_relief.allocated_p)
5737 unload_color (f, f->output_data.w32->white_relief.pixel);
5738 if (f->output_data.w32->black_relief.allocated_p)
5739 unload_color (f, f->output_data.w32->black_relief.pixel);
5741 if (FRAME_FACE_CACHE (f))
5742 free_frame_faces (f);
5744 xfree (f->output_data.w32);
5745 f->output_data.w32 = NULL;
5747 if (f == dpyinfo->w32_focus_frame)
5748 dpyinfo->w32_focus_frame = 0;
5749 if (f == dpyinfo->w32_focus_event_frame)
5750 dpyinfo->w32_focus_event_frame = 0;
5751 if (f == dpyinfo->x_highlight_frame)
5752 dpyinfo->x_highlight_frame = 0;
5754 if (f == dpyinfo->mouse_face_mouse_frame)
5756 dpyinfo->mouse_face_beg_row
5757 = dpyinfo->mouse_face_beg_col = -1;
5758 dpyinfo->mouse_face_end_row
5759 = dpyinfo->mouse_face_end_col = -1;
5760 dpyinfo->mouse_face_window = Qnil;
5761 dpyinfo->mouse_face_deferred_gc = 0;
5762 dpyinfo->mouse_face_mouse_frame = 0;
5765 UNBLOCK_INPUT;
5769 /* Destroy the window of frame F. */
5770 void
5771 x_destroy_window (struct frame *f)
5773 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5775 x_free_frame_resources (f);
5776 dpyinfo->reference_count--;
5780 /* Setting window manager hints. */
5782 /* Set the normal size hints for the window manager, for frame F.
5783 FLAGS is the flags word to use--or 0 meaning preserve the flags
5784 that the window now has.
5785 If USER_POSITION is nonzero, we set the USPosition
5786 flag (this is useful when FLAGS is 0). */
5787 void
5788 x_wm_set_size_hint (struct frame *f, long flags, int user_position)
5790 Window window = FRAME_W32_WINDOW (f);
5792 enter_crit ();
5794 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
5795 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
5796 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
5797 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
5799 leave_crit ();
5802 /* Window manager things */
5803 void
5804 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
5806 #if 0
5807 Window window = FRAME_W32_WINDOW (f);
5809 f->display.x->wm_hints.flags |= IconPositionHint;
5810 f->display.x->wm_hints.icon_x = icon_x;
5811 f->display.x->wm_hints.icon_y = icon_y;
5813 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
5814 #endif
5818 /***********************************************************************
5819 Initialization
5820 ***********************************************************************/
5822 static int w32_initialized = 0;
5824 void
5825 w32_initialize_display_info (Lisp_Object display_name)
5827 struct w32_display_info *dpyinfo = &one_w32_display_info;
5829 bzero (dpyinfo, sizeof (*dpyinfo));
5831 /* Put it on w32_display_name_list. */
5832 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
5833 w32_display_name_list);
5834 dpyinfo->name_list_element = XCAR (w32_display_name_list);
5836 dpyinfo->w32_id_name
5837 = (char *) xmalloc (SCHARS (Vinvocation_name)
5838 + SCHARS (Vsystem_name)
5839 + 2);
5840 sprintf (dpyinfo->w32_id_name, "%s@%s",
5841 SDATA (Vinvocation_name), SDATA (Vsystem_name));
5843 /* Default Console mode values - overridden when running in GUI mode
5844 with values obtained from system metrics. */
5845 dpyinfo->resx = 1;
5846 dpyinfo->resy = 1;
5847 dpyinfo->n_planes = 1;
5848 dpyinfo->n_cbits = 4;
5849 dpyinfo->n_fonts = 0;
5850 dpyinfo->smallest_font_height = 1;
5851 dpyinfo->smallest_char_width = 1;
5853 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
5854 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
5855 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
5856 dpyinfo->mouse_face_window = Qnil;
5857 dpyinfo->mouse_face_overlay = Qnil;
5858 dpyinfo->mouse_face_hidden = 0;
5860 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
5861 /* TODO: dpyinfo->gray */
5865 /* Create an xrdb-style database of resources to supercede registry settings.
5866 The database is just a concatenation of C strings, finished by an additional
5867 \0. The strings are submitted to some basic normalization, so
5869 [ *]option[ *]:[ *]value...
5871 becomes
5873 option:value...
5875 but any whitespace following value is not removed. */
5877 static char *
5878 w32_make_rdb (char *xrm_option)
5880 char *buffer = xmalloc (strlen (xrm_option) + 2);
5881 char *current = buffer;
5882 char ch;
5883 int in_option = 1;
5884 int before_value = 0;
5886 do {
5887 ch = *xrm_option++;
5889 if (ch == '\n')
5891 *current++ = '\0';
5892 in_option = 1;
5893 before_value = 0;
5895 else if (ch != ' ')
5897 *current++ = ch;
5898 if (in_option && (ch == ':'))
5900 in_option = 0;
5901 before_value = 1;
5903 else if (before_value)
5905 before_value = 0;
5908 else if (!(in_option || before_value))
5910 *current++ = ch;
5912 } while (ch);
5914 *current = '\0';
5916 return buffer;
5919 void
5920 x_flush (struct frame * f)
5921 { /* Nothing to do */ }
5924 extern frame_parm_handler w32_frame_parm_handlers[];
5926 static struct redisplay_interface w32_redisplay_interface =
5928 w32_frame_parm_handlers,
5929 x_produce_glyphs,
5930 x_write_glyphs,
5931 x_insert_glyphs,
5932 x_clear_end_of_line,
5933 x_scroll_run,
5934 x_after_update_window_line,
5935 x_update_window_begin,
5936 x_update_window_end,
5937 x_cursor_to,
5938 x_flush,
5939 0, /* flush_display_optional */
5940 x_clear_window_mouse_face,
5941 x_get_glyph_overhangs,
5942 x_fix_overlapping_area,
5943 w32_draw_fringe_bitmap,
5944 w32_define_fringe_bitmap,
5945 w32_destroy_fringe_bitmap,
5946 w32_compute_glyph_string_overhangs,
5947 x_draw_glyph_string,
5948 w32_define_frame_cursor,
5949 w32_clear_frame_area,
5950 w32_draw_window_cursor,
5951 w32_draw_vertical_window_border,
5952 w32_shift_glyphs_for_insert
5955 static void x_delete_terminal (struct terminal *term);
5957 static struct terminal *
5958 w32_create_terminal (struct w32_display_info *dpyinfo)
5960 struct terminal *terminal;
5962 terminal = create_terminal ();
5964 terminal->type = output_w32;
5965 terminal->display_info.w32 = dpyinfo;
5966 dpyinfo->terminal = terminal;
5968 /* MSVC does not type K&R functions with no arguments correctly, and
5969 so we must explicitly cast them. */
5970 terminal->clear_frame_hook = x_clear_frame;
5971 terminal->ins_del_lines_hook = x_ins_del_lines;
5972 terminal->delete_glyphs_hook = x_delete_glyphs;
5973 terminal->ring_bell_hook = w32_ring_bell;
5974 terminal->reset_terminal_modes_hook = w32_reset_terminal_modes;
5975 terminal->set_terminal_modes_hook = w32_set_terminal_modes;
5976 terminal->update_begin_hook = x_update_begin;
5977 terminal->update_end_hook = x_update_end;
5978 terminal->set_terminal_window_hook = w32_set_terminal_window;
5979 terminal->read_socket_hook = w32_read_socket;
5980 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
5981 terminal->mouse_position_hook = w32_mouse_position;
5982 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
5983 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
5984 // terminal->fullscreen_hook = XTfullscreen_hook;
5985 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
5986 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
5987 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
5988 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
5990 terminal->delete_frame_hook = x_destroy_window;
5991 terminal->delete_terminal_hook = x_delete_terminal;
5993 terminal->rif = &w32_redisplay_interface;
5994 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
5995 terminal->char_ins_del_ok = 1;
5996 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
5997 terminal->fast_clear_end_of_line = 1; /* X does this well. */
5998 terminal->memory_below_frame = 0; /* We don't remember what scrolls
5999 off the bottom. */
6001 /* We don't yet support separate terminals on W32, so don't try to share
6002 keyboards between virtual terminals that are on the same physical
6003 terminal like X does. */
6004 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
6005 init_kboard (terminal->kboard);
6006 terminal->kboard->Vwindow_system = intern ("w32");
6007 terminal->kboard->next_kboard = all_kboards;
6008 all_kboards = terminal->kboard;
6009 /* Don't let the initial kboard remain current longer than necessary.
6010 That would cause problems if a file loaded on startup tries to
6011 prompt in the mini-buffer. */
6012 if (current_kboard == initial_kboard)
6013 current_kboard = terminal->kboard;
6014 terminal->kboard->reference_count++;
6016 return terminal;
6019 static void
6020 x_delete_terminal (struct terminal *terminal)
6022 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6023 int i;
6025 /* Protect against recursive calls. delete_frame in
6026 delete_terminal calls us back when it deletes our last frame. */
6027 if (!terminal->name)
6028 return;
6030 BLOCK_INPUT;
6032 x_delete_display (dpyinfo);
6033 UNBLOCK_INPUT;
6036 struct w32_display_info *
6037 w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
6039 struct w32_display_info *dpyinfo;
6040 struct terminal *terminal;
6041 HDC hdc;
6043 BLOCK_INPUT;
6045 if (!w32_initialized)
6047 w32_initialize ();
6048 w32_initialized = 1;
6051 w32_initialize_display_info (display_name);
6053 dpyinfo = &one_w32_display_info;
6054 terminal = w32_create_terminal (dpyinfo);
6056 /* Set the name of the terminal. */
6057 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
6058 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
6059 terminal->name[SBYTES (display_name)] = 0;
6061 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6063 /* Put this display on the chain. */
6064 dpyinfo->next = x_display_list;
6065 x_display_list = dpyinfo;
6067 hdc = GetDC (NULL);
6069 dpyinfo->root_window = GetDesktopWindow ();
6070 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6071 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6072 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6073 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6074 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6075 ReleaseDC (NULL, hdc);
6077 /* initialise palette with white and black */
6079 XColor color;
6080 w32_defined_color (0, "white", &color, 1);
6081 w32_defined_color (0, "black", &color, 1);
6084 /* Add the default keyboard. */
6085 add_keyboard_wait_descriptor (0);
6087 /* Create Fringe Bitmaps and store them for later use.
6089 On W32, bitmaps are all unsigned short, as Windows requires
6090 bitmap data to be Word aligned. For some reason they are
6091 horizontally reflected compared to how they appear on X, so we
6092 need to bitswap and convert to unsigned shorts before creating
6093 the bitmaps. */
6094 w32_init_fringe (terminal->rif);
6096 #ifdef F_SETOWN
6097 fcntl (connection, F_SETOWN, getpid ());
6098 #endif /* ! defined (F_SETOWN) */
6100 #ifdef SIGIO
6101 if (interrupt_input)
6102 init_sigio (connection);
6103 #endif /* ! defined (SIGIO) */
6105 UNBLOCK_INPUT;
6107 return dpyinfo;
6110 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6111 void
6112 x_delete_display (struct w32_display_info *dpyinfo)
6114 /* Discard this display from w32_display_name_list and w32_display_list.
6115 We can't use Fdelq because that can quit. */
6116 if (! NILP (w32_display_name_list)
6117 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
6118 w32_display_name_list = XCDR (w32_display_name_list);
6119 else
6121 Lisp_Object tail;
6123 tail = w32_display_name_list;
6124 while (CONSP (tail) && CONSP (XCDR (tail)))
6126 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
6128 XSETCDR (tail, XCDR (XCDR (tail)));
6129 break;
6131 tail = XCDR (tail);
6135 /* free palette table */
6137 struct w32_palette_entry * plist;
6139 plist = dpyinfo->color_list;
6140 while (plist)
6142 struct w32_palette_entry * pentry = plist;
6143 plist = plist->next;
6144 xfree (pentry);
6146 dpyinfo->color_list = NULL;
6147 if (dpyinfo->palette)
6148 DeleteObject(dpyinfo->palette);
6150 xfree (dpyinfo->w32_id_name);
6152 w32_reset_fringes ();
6155 /* Set up use of W32. */
6157 DWORD WINAPI w32_msg_worker (void * arg);
6159 static void
6160 w32_initialize (void)
6162 HANDLE shell;
6163 HRESULT (WINAPI * set_user_model) (wchar_t * id);
6165 baud_rate = 19200;
6167 w32_system_caret_hwnd = NULL;
6168 w32_system_caret_height = 0;
6169 w32_system_caret_x = 0;
6170 w32_system_caret_y = 0;
6172 /* On Windows 7 and later, we need to set the user model ID
6173 to associate emacsclient launched files with Emacs frames
6174 in the UI. */
6175 shell = GetModuleHandle ("shell32.dll");
6176 if (shell)
6178 set_user_model
6179 = (void *) GetProcAddress (shell,
6180 "SetCurrentProcessExplicitAppUserModelID");
6182 /* If the function is defined, then we are running on Windows 7
6183 or newer, and the UI uses this to group related windows
6184 together. Since emacs, runemacs, emacsclient are related, we
6185 want them grouped even though the executables are different,
6186 so we need to set a consistent ID between them. */
6187 if (set_user_model)
6188 set_user_model (L"GNU.Emacs");
6191 /* Initialize w32_use_visible_system_caret based on whether a screen
6192 reader is in use. */
6193 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6194 &w32_use_visible_system_caret, 0))
6195 w32_use_visible_system_caret = 0;
6197 last_tool_bar_item = -1;
6198 any_help_event_p = 0;
6200 /* Initialize input mode: interrupt_input off, no flow control, allow
6201 8 bit character input, standard quit char. */
6202 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6205 DWORD input_locale_id = (DWORD) GetKeyboardLayout (0);
6206 keyboard_codepage = codepage_for_locale ((LCID) (input_locale_id & 0xffff));
6209 /* Create the window thread - it will terminate itself when the app
6210 terminates */
6211 init_crit ();
6213 dwMainThreadId = GetCurrentThreadId ();
6214 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6215 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6217 /* Wait for thread to start */
6219 MSG msg;
6221 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6223 hWindowsThread = CreateThread (NULL, 0,
6224 w32_msg_worker,
6225 0, 0, &dwWindowsThreadId);
6227 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6230 /* It is desirable that mainThread should have the same notion of
6231 focus window and active window as windowsThread. Unfortunately, the
6232 following call to AttachThreadInput, which should do precisely what
6233 we need, causes major problems when Emacs is linked as a console
6234 program. Unfortunately, we have good reasons for doing that, so
6235 instead we need to send messages to windowsThread to make some API
6236 calls for us (ones that affect, or depend on, the active/focus
6237 window state.) */
6238 #ifdef ATTACH_THREADS
6239 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6240 #endif
6242 /* Dynamically link to optional system components. */
6244 HMODULE user_lib = GetModuleHandle ("user32.dll");
6246 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6248 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6250 #undef LOAD_PROC
6252 /* Ensure scrollbar handle is at least 5 pixels. */
6253 vertical_scroll_bar_min_handle = 5;
6255 /* For either kind of scroll bar, take account of the arrows; these
6256 effectively form the border of the main scroll bar range. */
6257 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6258 = GetSystemMetrics (SM_CYVSCROLL);
6262 void
6263 syms_of_w32term (void)
6265 staticpro (&w32_display_name_list);
6266 w32_display_name_list = Qnil;
6268 staticpro (&last_mouse_scroll_bar);
6269 last_mouse_scroll_bar = Qnil;
6271 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6273 DEFVAR_INT ("w32-num-mouse-buttons",
6274 &w32_num_mouse_buttons,
6275 doc: /* Number of physical mouse buttons. */);
6276 w32_num_mouse_buttons = 2;
6278 DEFVAR_LISP ("w32-swap-mouse-buttons",
6279 &Vw32_swap_mouse_buttons,
6280 doc: /* Swap the mapping of middle and right mouse buttons.
6281 When nil, middle button is mouse-2 and right button is mouse-3. */);
6282 Vw32_swap_mouse_buttons = Qnil;
6284 DEFVAR_LISP ("w32-grab-focus-on-raise",
6285 &Vw32_grab_focus_on_raise,
6286 doc: /* Raised frame grabs input focus.
6287 When t, `raise-frame' grabs input focus as well. This fits well
6288 with the normal Windows click-to-focus policy, but might not be
6289 desirable when using a point-to-focus policy. */);
6290 Vw32_grab_focus_on_raise = Qt;
6292 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6293 &Vw32_capslock_is_shiftlock,
6294 doc: /* Apply CapsLock state to non character input keys.
6295 When nil, CapsLock only affects normal character input keys. */);
6296 Vw32_capslock_is_shiftlock = Qnil;
6298 DEFVAR_LISP ("w32-recognize-altgr",
6299 &Vw32_recognize_altgr,
6300 doc: /* Recognize right-alt and left-ctrl as AltGr.
6301 When nil, the right-alt and left-ctrl key combination is
6302 interpreted normally. */);
6303 Vw32_recognize_altgr = Qt;
6305 DEFVAR_BOOL ("w32-use-visible-system-caret",
6306 &w32_use_visible_system_caret,
6307 doc: /* Flag to make the system caret visible.
6308 When this is non-nil, Emacs will indicate the position of point by
6309 using the system caret instead of drawing its own cursor. Some screen
6310 reader software does not track the system cursor properly when it is
6311 invisible, and gets confused by Emacs drawing its own cursor, so this
6312 variable is initialized to t when Emacs detects that screen reader
6313 software is running as it starts up.
6315 When this variable is set, other variables affecting the appearance of
6316 the cursor have no effect. */);
6318 w32_use_visible_system_caret = 0;
6320 /* We don't yet support this, but defining this here avoids whining
6321 from cus-start.el and other places, like "M-x set-variable". */
6322 DEFVAR_BOOL ("x-use-underline-position-properties",
6323 &x_use_underline_position_properties,
6324 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
6325 A value of nil means ignore them. If you encounter fonts with bogus
6326 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6327 to 4.1, set this to nil. */);
6328 x_use_underline_position_properties = 0;
6330 DEFVAR_BOOL ("x-underline-at-descent-line",
6331 &x_underline_at_descent_line,
6332 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
6333 A value of nil means to draw the underline according to the value of the
6334 variable `x-use-underline-position-properties', which is usually at the
6335 baseline level. The default value is nil. */);
6336 x_underline_at_descent_line = 0;
6338 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
6339 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6340 Vx_toolkit_scroll_bars = Qt;
6342 staticpro (&last_mouse_motion_frame);
6343 last_mouse_motion_frame = Qnil;
6346 /* arch-tag: 5fa70624-ab86-499c-8a85-473958ee4646
6347 (do not change this comment) */