Add 2009 to copyright years.
[emacs.git] / src / w32term.c
blob136c421c38935c1fdc7b0f7332f2fd275c1fcfec
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 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 "lisp.h"
26 #include "blockinput.h"
27 #include "w32term.h"
29 #include "systty.h"
30 #include "systime.h"
32 #include <ctype.h>
33 #include <errno.h>
34 #include <setjmp.h>
35 #include <sys/stat.h>
37 #include "charset.h"
38 #include "character.h"
39 #include "coding.h"
40 #include "ccl.h"
41 #include "frame.h"
42 #include "dispextern.h"
43 #include "fontset.h"
44 #include "termhooks.h"
45 #include "termopts.h"
46 #include "termchar.h"
47 #include "disptab.h"
48 #include "buffer.h"
49 #include "window.h"
50 #include "keyboard.h"
51 #include "intervals.h"
52 #include "process.h"
53 #include "atimer.h"
54 #include "keymap.h"
56 #include "w32heap.h"
57 #include <shellapi.h>
59 #include "font.h"
60 #include "w32font.h"
62 /* Fringe bitmaps. */
64 static int max_fringe_bmp = 0;
65 static HBITMAP *fringe_bmp = 0;
67 /* Non-nil means Emacs uses toolkit scroll bars. */
69 Lisp_Object Vx_toolkit_scroll_bars;
71 /* Temporary variables for w32_read_socket. */
73 static int last_mousemove_x = 0;
74 static int last_mousemove_y = 0;
76 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
77 #ifndef GET_WHEEL_DELTA_WPARAM
78 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
79 #endif
81 /* Non-zero means that a HELP_EVENT has been generated since Emacs
82 start. */
84 static int any_help_event_p;
86 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
87 static Lisp_Object last_window;
89 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
90 int x_use_underline_position_properties;
92 /* Non-zero means to draw the underline at the same place as the descent line. */
94 int x_underline_at_descent_line;
96 extern unsigned int msh_mousewheel;
98 extern void free_frame_menubar ();
100 extern int w32_codepage_for_font (char *fontname);
101 extern Cursor w32_load_cursor (LPCTSTR name);
103 extern Lisp_Object Vwindow_system;
105 #define x_any_window_to_frame x_window_to_frame
106 #define x_top_window_to_frame x_window_to_frame
109 /* This is display since w32 does not support multiple ones. */
110 struct w32_display_info one_w32_display_info;
111 struct w32_display_info *x_display_list;
113 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
114 one for each element of w32_display_list and in the same order.
115 NAME is the name of the frame.
116 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
117 Lisp_Object w32_display_name_list;
120 #ifndef GLYPHSET
121 /* Pre Windows 2000, this was not available, but define it here so
122 that Emacs compiled on such a platform will run on newer versions. */
124 typedef struct tagWCRANGE
126 WCHAR wcLow;
127 USHORT cGlyphs;
128 } WCRANGE;
130 typedef struct tagGLYPHSET
132 DWORD cbThis;
133 DWORD flAccel;
134 DWORD cGlyphsSupported;
135 DWORD cRanges;
136 WCRANGE ranges[1];
137 } GLYPHSET;
139 #endif
141 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
142 BOOL (PASCAL *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
144 #ifndef LWA_ALPHA
145 #define LWA_ALPHA 0x02
146 #endif
147 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
148 later targets by MSVC headers. */
149 #ifndef WS_EX_LAYERED
150 #define WS_EX_LAYERED 0x80000
151 #endif
153 /* Frame being updated by update_frame. This is declared in term.c.
154 This is set by update_begin and looked at by all the
155 w32 functions. It is zero while not inside an update.
156 In that case, the w32 functions assume that `SELECTED_FRAME ()'
157 is the frame to apply to. */
158 extern struct frame *updating_frame;
160 /* This is a frame waiting to be autoraised, within w32_read_socket. */
161 struct frame *pending_autoraise_frame;
163 /* The handle of the frame that currently owns the system caret. */
164 HWND w32_system_caret_hwnd;
165 int w32_system_caret_height;
166 int w32_system_caret_x;
167 int w32_system_caret_y;
168 int w32_use_visible_system_caret;
170 DWORD dwWindowsThreadId = 0;
171 HANDLE hWindowsThread = NULL;
172 DWORD dwMainThreadId = 0;
173 HANDLE hMainThread = NULL;
175 int vertical_scroll_bar_min_handle;
176 int vertical_scroll_bar_top_border;
177 int vertical_scroll_bar_bottom_border;
179 int last_scroll_bar_drag_pos;
181 /* Mouse movement. */
183 /* 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. */
220 static Time last_mouse_movement_time;
222 /* Incremented by w32_read_socket whenever it really tries to read
223 events. */
225 #ifdef __STDC__
226 static int volatile input_signal_count;
227 #else
228 static int input_signal_count;
229 #endif
231 extern Lisp_Object Vcommand_line_args, Vsystem_name;
233 #ifndef USE_CRT_DLL
234 extern int errno;
235 #endif
237 /* A mask of extra modifier bits to put into every keyboard char. */
239 extern EMACS_INT extra_keyboard_modifiers;
241 static void x_update_window_end P_ ((struct window *, int, int));
242 static void w32_handle_tool_bar_click P_ ((struct frame *,
243 struct input_event *));
244 static void w32_define_cursor P_ ((Window, Cursor));
246 void x_lower_frame P_ ((struct frame *));
247 void x_scroll_bar_clear P_ ((struct frame *));
248 void x_wm_set_size_hint P_ ((struct frame *, long, int));
249 void x_raise_frame P_ ((struct frame *));
250 void x_set_window_size P_ ((struct frame *, int, int, int));
251 void x_wm_set_window_state P_ ((struct frame *, int));
252 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
253 static void w32_initialize P_ ((void));
254 static void x_update_end P_ ((struct frame *));
255 static void w32_frame_up_to_date P_ ((struct frame *));
256 static void w32_set_terminal_modes P_ ((struct terminal *));
257 static void w32_reset_terminal_modes P_ ((struct terminal *));
258 static void x_clear_frame P_ ((struct frame *));
259 static void frame_highlight P_ ((struct frame *));
260 static void frame_unhighlight P_ ((struct frame *));
261 static void x_new_focus_frame P_ ((struct w32_display_info *,
262 struct frame *));
263 static void x_focus_changed P_ ((int, int, struct w32_display_info *,
264 struct frame *, struct input_event *));
265 static void w32_detect_focus_change P_ ((struct w32_display_info *,
266 W32Msg *, struct input_event *));
267 static void w32_frame_rehighlight P_ ((struct frame *));
268 static void x_frame_rehighlight P_ ((struct w32_display_info *));
269 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
270 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
271 enum text_cursor_kinds));
272 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *, int, HDC));
273 static BOOL my_show_window P_ ((struct frame *, HWND, int));
274 static void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
275 static void my_set_focus P_ ((struct frame *, HWND));
276 static void my_set_foreground_window P_ ((HWND));
277 static void my_destroy_window P_ ((struct frame *, HWND));
279 static Lisp_Object Qvendor_specific_keysyms;
282 /***********************************************************************
283 Debugging
284 ***********************************************************************/
286 #if 0
288 /* This is a function useful for recording debugging information about
289 the sequence of occurrences in this file. */
291 struct record
293 char *locus;
294 int type;
297 struct record event_record[100];
299 int event_record_index;
301 record_event (locus, type)
302 char *locus;
303 int type;
305 if (event_record_index == sizeof (event_record) / sizeof (struct record))
306 event_record_index = 0;
308 event_record[event_record_index].locus = locus;
309 event_record[event_record_index].type = type;
310 event_record_index++;
313 #endif /* 0 */
316 void
317 XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
318 XGCValues *xgcv)
320 if (mask & GCForeground)
321 gc->foreground = xgcv->foreground;
322 if (mask & GCBackground)
323 gc->background = xgcv->background;
324 if (mask & GCFont)
325 gc->font = xgcv->font;
328 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
329 XGCValues *xgcv)
331 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
332 bzero (gc, sizeof (XGCValues));
334 XChangeGC (ignore, gc, mask, xgcv);
336 return gc;
339 void
340 XGetGCValues (void* ignore, XGCValues *gc,
341 unsigned long mask, XGCValues *xgcv)
343 XChangeGC (ignore, xgcv, mask, gc);
346 static void
347 w32_set_clip_rectangle (HDC hdc, RECT *rect)
349 if (rect)
351 HRGN clip_region = CreateRectRgnIndirect (rect);
352 SelectClipRgn (hdc, clip_region);
353 DeleteObject (clip_region);
355 else
356 SelectClipRgn (hdc, NULL);
360 /* Draw a hollow rectangle at the specified position. */
361 void
362 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
363 int width, int height)
365 HBRUSH hb, oldhb;
366 HPEN hp, oldhp;
368 hb = CreateSolidBrush (gc->background);
369 hp = CreatePen (PS_SOLID, 0, gc->foreground);
370 oldhb = SelectObject (hdc, hb);
371 oldhp = SelectObject (hdc, hp);
373 Rectangle (hdc, x, y, x + width, y + height);
375 SelectObject (hdc, oldhb);
376 SelectObject (hdc, oldhp);
377 DeleteObject (hb);
378 DeleteObject (hp);
381 /* Draw a filled rectangle at the specified position. */
382 void
383 w32_fill_rect (f, hdc, pix, lprect)
384 FRAME_PTR f;
385 HDC hdc;
386 COLORREF pix;
387 RECT * lprect;
389 HBRUSH hb;
391 hb = CreateSolidBrush (pix);
392 FillRect (hdc, lprect, hb);
393 DeleteObject (hb);
396 void
397 w32_clear_window (f)
398 FRAME_PTR f;
400 RECT rect;
401 HDC hdc = get_frame_dc (f);
403 /* Under certain conditions, this can be called at startup with
404 a console frame pointer before the GUI frame is created. An HDC
405 of 0 indicates this. */
406 if (hdc)
408 GetClientRect (FRAME_W32_WINDOW (f), &rect);
409 w32_clear_rect (f, hdc, &rect);
412 release_frame_dc (f, hdc);
415 #define OPAQUE_FRAME 255
417 void
418 x_set_frame_alpha (f)
419 struct frame *f;
421 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
422 double alpha = 1.0;
423 double alpha_min = 1.0;
424 BYTE opac;
425 LONG ex_style;
426 HWND window = FRAME_W32_WINDOW (f);
428 /* Older versions of Windows do not support transparency. */
429 if (!pfnSetLayeredWindowAttributes)
430 return;
432 if (dpyinfo->x_highlight_frame == f)
433 alpha = f->alpha[0];
434 else
435 alpha = f->alpha[1];
437 if (FLOATP (Vframe_alpha_lower_limit))
438 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
439 else if (INTEGERP (Vframe_alpha_lower_limit))
440 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
442 if (alpha < 0.0)
443 return;
444 else if (alpha > 1.0)
445 alpha = 1.0;
446 else if (alpha < alpha_min && alpha_min <= 1.0)
447 alpha = alpha_min;
449 opac = alpha * OPAQUE_FRAME;
451 ex_style = GetWindowLong (window, GWL_EXSTYLE);
453 if (opac == OPAQUE_FRAME)
454 ex_style &= ~WS_EX_LAYERED;
455 else
456 ex_style |= WS_EX_LAYERED;
458 SetWindowLong (window, GWL_EXSTYLE, ex_style);
460 if (opac != OPAQUE_FRAME)
461 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
465 x_display_pixel_height (dpyinfo)
466 struct w32_display_info *dpyinfo;
468 return GetDeviceCaps (GetDC (GetDesktopWindow ()), VERTRES);
472 x_display_pixel_width (dpyinfo)
473 struct w32_display_info *dpyinfo;
475 return GetDeviceCaps (GetDC (GetDesktopWindow ()), HORZRES);
479 /***********************************************************************
480 Starting and ending an update
481 ***********************************************************************/
483 /* Start an update of frame F. This function is installed as a hook
484 for update_begin, i.e. it is called when update_begin is called.
485 This function is called prior to calls to x_update_window_begin for
486 each window being updated. */
488 static void
489 x_update_begin (f)
490 struct frame *f;
492 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
494 if (! FRAME_W32_P (f))
495 return;
497 /* Regenerate display palette before drawing if list of requested
498 colors has changed. */
499 if (display_info->regen_palette)
501 w32_regenerate_palette (f);
502 display_info->regen_palette = FALSE;
507 /* Start update of window W. Set the global variable updated_window
508 to the window being updated and set output_cursor to the cursor
509 position of W. */
511 static void
512 x_update_window_begin (w)
513 struct window *w;
515 struct frame *f = XFRAME (WINDOW_FRAME (w));
516 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
518 /* Hide the system caret during an update. */
519 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
521 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
524 updated_window = w;
525 set_output_cursor (&w->cursor);
527 BLOCK_INPUT;
529 if (f == display_info->mouse_face_mouse_frame)
531 /* Don't do highlighting for mouse motion during the update. */
532 display_info->mouse_face_defer = 1;
534 /* If F needs to be redrawn, simply forget about any prior mouse
535 highlighting. */
536 if (FRAME_GARBAGED_P (f))
537 display_info->mouse_face_window = Qnil;
539 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
540 their mouse_face_p flag set, which means that they are always
541 unequal to rows in a desired matrix which never have that
542 flag set. So, rows containing mouse-face glyphs are never
543 scrolled, and we don't have to switch the mouse highlight off
544 here to prevent it from being scrolled. */
546 /* Can we tell that this update does not affect the window
547 where the mouse highlight is? If so, no need to turn off.
548 Likewise, don't do anything if the frame is garbaged;
549 in that case, the frame's current matrix that we would use
550 is all wrong, and we will redisplay that line anyway. */
551 if (!NILP (display_info->mouse_face_window)
552 && w == XWINDOW (display_info->mouse_face_window))
554 int i;
556 for (i = 0; i < w->desired_matrix->nrows; ++i)
557 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
558 break;
560 if (i < w->desired_matrix->nrows)
561 clear_mouse_face (display_info);
563 #endif /* 0 */
566 UNBLOCK_INPUT;
569 /* Draw a vertical window border from (x,y0) to (x,y1) */
571 static void
572 w32_draw_vertical_window_border (w, x, y0, y1)
573 struct window *w;
574 int x, y0, y1;
576 struct frame *f = XFRAME (WINDOW_FRAME (w));
577 RECT r;
578 HDC hdc;
579 struct face *face;
581 r.left = x;
582 r.right = x + 1;
583 r.top = y0;
584 r.bottom = y1;
586 hdc = get_frame_dc (f);
587 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
588 if (face)
589 w32_fill_rect (f, hdc, face->foreground, &r);
590 else
591 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
593 release_frame_dc (f, hdc);
597 /* End update of window W (which is equal to updated_window).
599 Draw vertical borders between horizontally adjacent windows, and
600 display W's cursor if CURSOR_ON_P is non-zero.
602 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
603 glyphs in mouse-face were overwritten. In that case we have to
604 make sure that the mouse-highlight is properly redrawn.
606 W may be a menu bar pseudo-window in case we don't have X toolkit
607 support. Such windows don't have a cursor, so don't display it
608 here. */
610 static void
611 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
612 struct window *w;
613 int cursor_on_p, mouse_face_overwritten_p;
615 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
617 if (!w->pseudo_window_p)
619 BLOCK_INPUT;
621 if (cursor_on_p)
622 display_and_set_cursor (w, 1, output_cursor.hpos,
623 output_cursor.vpos,
624 output_cursor.x, output_cursor.y);
626 if (draw_window_fringes (w, 1))
627 x_draw_vertical_border (w);
629 UNBLOCK_INPUT;
632 /* If a row with mouse-face was overwritten, arrange for
633 XTframe_up_to_date to redisplay the mouse highlight. */
634 if (mouse_face_overwritten_p)
636 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
637 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
638 dpyinfo->mouse_face_window = Qnil;
641 /* Unhide the caret. This won't actually show the cursor, unless it
642 was visible before the corresponding call to HideCaret in
643 x_update_window_begin. */
644 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
646 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
649 updated_window = NULL;
653 /* End update of frame F. This function is installed as a hook in
654 update_end. */
656 static void
657 x_update_end (f)
658 struct frame *f;
660 if (! FRAME_W32_P (f))
661 return;
663 /* Mouse highlight may be displayed again. */
664 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
668 /* This function is called from various places in xdisp.c whenever a
669 complete update has been performed. The global variable
670 updated_window is not available here. */
672 static void
673 w32_frame_up_to_date (f)
674 struct frame *f;
676 if (FRAME_W32_P (f))
678 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
680 if (dpyinfo->mouse_face_deferred_gc
681 || f == dpyinfo->mouse_face_mouse_frame)
683 BLOCK_INPUT;
684 if (dpyinfo->mouse_face_mouse_frame)
685 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
686 dpyinfo->mouse_face_mouse_x,
687 dpyinfo->mouse_face_mouse_y);
688 dpyinfo->mouse_face_deferred_gc = 0;
689 UNBLOCK_INPUT;
695 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
696 arrow bitmaps, or clear the fringes if no bitmaps are required
697 before DESIRED_ROW is made current. The window being updated is
698 found in updated_window. This function is called from
699 update_window_line only if it is known that there are differences
700 between bitmaps to be drawn between current row and DESIRED_ROW. */
702 static void
703 x_after_update_window_line (desired_row)
704 struct glyph_row *desired_row;
706 struct window *w = updated_window;
707 struct frame *f;
708 int width, height;
710 xassert (w);
712 if (!desired_row->mode_line_p && !w->pseudo_window_p)
713 desired_row->redraw_fringe_bitmaps_p = 1;
715 /* When a window has disappeared, make sure that no rest of
716 full-width rows stays visible in the internal border. Could
717 check here if updated_window is the leftmost/rightmost window,
718 but I guess it's not worth doing since vertically split windows
719 are almost never used, internal border is rarely set, and the
720 overhead is very small. */
721 if (windows_or_buffers_changed
722 && desired_row->full_width_p
723 && (f = XFRAME (w->frame),
724 width = FRAME_INTERNAL_BORDER_WIDTH (f),
725 width != 0)
726 && (height = desired_row->visible_height,
727 height > 0))
729 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
731 /* Internal border is drawn below the tool bar. */
732 if (WINDOWP (f->tool_bar_window)
733 && w == XWINDOW (f->tool_bar_window))
734 y -= width;
736 BLOCK_INPUT;
738 HDC hdc = get_frame_dc (f);
739 w32_clear_area (f, hdc, 0, y, width, height);
740 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
741 y, width, height);
742 release_frame_dc (f, hdc);
744 UNBLOCK_INPUT;
749 /* Draw the bitmap WHICH in one of the left or right fringes of
750 window W. ROW is the glyph row for which to display the bitmap; it
751 determines the vertical position at which the bitmap has to be
752 drawn. */
754 static void
755 w32_draw_fringe_bitmap (w, row, p)
756 struct window *w;
757 struct glyph_row *row;
758 struct draw_fringe_bitmap_params *p;
760 struct frame *f = XFRAME (WINDOW_FRAME (w));
761 HDC hdc;
762 struct face *face = p->face;
763 int rowY;
765 hdc = get_frame_dc (f);
767 if (!p->overlay_p)
769 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
771 /* If the fringe is adjacent to the left (right) scroll bar of a
772 leftmost (rightmost, respectively) window, then extend its
773 background to the gap between the fringe and the bar. */
774 if ((WINDOW_LEFTMOST_P (w)
775 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
776 || (WINDOW_RIGHTMOST_P (w)
777 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
779 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
781 if (sb_width > 0)
783 int left = WINDOW_SCROLL_BAR_AREA_X (w);
784 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
785 * FRAME_COLUMN_WIDTH (f));
787 if (bx < 0)
789 /* Bitmap fills the fringe. */
790 if (left + width == p->x)
791 bx = left + sb_width;
792 else if (p->x + p->wd == left)
793 bx = left;
794 if (bx >= 0)
796 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
798 nx = width - sb_width;
799 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
800 row->y));
801 ny = row->visible_height;
804 else
806 if (left + width == bx)
808 bx = left + sb_width;
809 nx += width - sb_width;
811 else if (bx + nx == left)
812 nx += width - sb_width;
817 if (bx >= 0 && nx > 0)
818 w32_fill_area (f, hdc, face->background, bx, by, nx, ny);
821 /* Must clip because of partially visible lines. */
822 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
823 if (p->y < rowY)
825 /* Adjust position of "bottom aligned" bitmap on partially
826 visible last row. */
827 int oldY = row->y;
828 int oldVH = row->visible_height;
829 row->visible_height = p->h;
830 row->y -= rowY - p->y;
831 w32_clip_to_row (w, row, -1, hdc);
832 row->y = oldY;
833 row->visible_height = oldVH;
835 else
836 w32_clip_to_row (w, row, -1, hdc);
838 if (p->which && p->which < max_fringe_bmp)
840 HBITMAP pixmap = fringe_bmp[p->which];
841 HDC compat_hdc;
842 HANDLE horig_obj;
844 compat_hdc = CreateCompatibleDC (hdc);
846 SaveDC (hdc);
848 horig_obj = SelectObject (compat_hdc, pixmap);
850 /* Paint overlays transparently. */
851 if (p->overlay_p)
853 HBRUSH h_brush, h_orig_brush;
855 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
856 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
857 h_brush = CreateSolidBrush (face->foreground);
858 h_orig_brush = SelectObject (hdc, h_brush);
860 BitBlt (hdc, p->x, p->y, p->wd, p->h,
861 compat_hdc, 0, p->dh,
862 DSTINVERT);
863 BitBlt (hdc, p->x, p->y, p->wd, p->h,
864 compat_hdc, 0, p->dh,
865 0x2E064A);
866 BitBlt (hdc, p->x, p->y, p->wd, p->h,
867 compat_hdc, 0, p->dh,
868 DSTINVERT);
870 SelectObject (hdc, h_orig_brush);
871 DeleteObject (h_brush);
873 else
875 SetTextColor (hdc, face->background);
876 SetBkColor (hdc, (p->cursor_p
877 ? f->output_data.w32->cursor_pixel
878 : face->foreground));
880 BitBlt (hdc, p->x, p->y, p->wd, p->h,
881 compat_hdc, 0, p->dh,
882 SRCCOPY);
885 SelectObject (compat_hdc, horig_obj);
886 DeleteDC (compat_hdc);
887 RestoreDC (hdc, -1);
890 w32_set_clip_rectangle (hdc, NULL);
892 release_frame_dc (f, hdc);
895 static void
896 w32_define_fringe_bitmap (which, bits, h, wd)
897 int which;
898 unsigned short *bits;
899 int h, wd;
901 if (which >= max_fringe_bmp)
903 int i = max_fringe_bmp;
904 max_fringe_bmp = which + 20;
905 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
906 while (i < max_fringe_bmp)
907 fringe_bmp[i++] = 0;
910 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
913 static void
914 w32_destroy_fringe_bitmap (which)
915 int which;
917 if (which >= max_fringe_bmp)
918 return;
920 if (fringe_bmp[which])
921 DeleteObject (fringe_bmp[which]);
922 fringe_bmp[which] = 0;
927 /* This is called when starting Emacs and when restarting after
928 suspend. When starting Emacs, no window is mapped. And nothing
929 must be done to Emacs's own window if it is suspended (though that
930 rarely happens). */
932 static void
933 w32_set_terminal_modes (struct terminal *term)
937 /* This is called when exiting or suspending Emacs. Exiting will make
938 the W32 windows go away, and suspending requires no action. */
940 static void
941 w32_reset_terminal_modes (struct terminal *term)
947 /***********************************************************************
948 Display Iterator
949 ***********************************************************************/
951 /* Function prototypes of this page. */
953 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
954 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
955 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
956 int));
957 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
958 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
959 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
960 static void x_draw_glyph_string P_ ((struct glyph_string *));
961 static void x_set_cursor_gc P_ ((struct glyph_string *));
962 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
963 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
964 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
965 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
966 double, int, COLORREF));
967 static void x_setup_relief_colors P_ ((struct glyph_string *));
968 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
969 static void x_draw_image_relief P_ ((struct glyph_string *));
970 static void x_draw_image_foreground P_ ((struct glyph_string *));
971 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
972 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
973 int, int, int));
974 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
975 int, int, int, int, int, int,
976 RECT *));
977 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
978 int, int, int, RECT *));
981 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
982 face. */
984 static void
985 x_set_cursor_gc (s)
986 struct glyph_string *s;
988 if (s->font == FRAME_FONT (s->f)
989 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
990 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
991 && !s->cmp)
992 s->gc = s->f->output_data.w32->cursor_gc;
993 else
995 /* Cursor on non-default face: must merge. */
996 XGCValues xgcv;
997 unsigned long mask;
999 xgcv.background = s->f->output_data.w32->cursor_pixel;
1000 xgcv.foreground = s->face->background;
1002 /* If the glyph would be invisible, try a different foreground. */
1003 if (xgcv.foreground == xgcv.background)
1004 xgcv.foreground = s->face->foreground;
1005 if (xgcv.foreground == xgcv.background)
1006 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
1007 if (xgcv.foreground == xgcv.background)
1008 xgcv.foreground = s->face->foreground;
1010 /* Make sure the cursor is distinct from text in this face. */
1011 if (xgcv.background == s->face->background
1012 && xgcv.foreground == s->face->foreground)
1014 xgcv.background = s->face->foreground;
1015 xgcv.foreground = s->face->background;
1018 IF_DEBUG (x_check_font (s->f, s->font));
1019 xgcv.font = s->font;
1020 mask = GCForeground | GCBackground | GCFont;
1022 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1023 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1024 mask, &xgcv);
1025 else
1026 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1027 = XCreateGC (NULL, s->window, mask, &xgcv);
1029 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1034 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1036 static void
1037 x_set_mouse_face_gc (s)
1038 struct glyph_string *s;
1040 int face_id;
1041 struct face *face;
1043 /* What face has to be used last for the mouse face? */
1044 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
1045 face = FACE_FROM_ID (s->f, face_id);
1046 if (face == NULL)
1047 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1049 if (s->first_glyph->type == CHAR_GLYPH)
1050 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1051 else
1052 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1053 s->face = FACE_FROM_ID (s->f, face_id);
1054 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1056 /* If font in this face is same as S->font, use it. */
1057 if (s->font == s->face->font)
1058 s->gc = s->face->gc;
1059 else
1061 /* Otherwise construct scratch_cursor_gc with values from FACE
1062 but font FONT. */
1063 XGCValues xgcv;
1064 unsigned long mask;
1066 xgcv.background = s->face->background;
1067 xgcv.foreground = s->face->foreground;
1068 IF_DEBUG (x_check_font (s->f, s->font));
1069 xgcv.font = s->font;
1070 mask = GCForeground | GCBackground | GCFont;
1072 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1073 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1074 mask, &xgcv);
1075 else
1076 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1077 = XCreateGC (NULL, s->window, mask, &xgcv);
1079 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1082 xassert (s->gc != 0);
1086 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1087 Faces to use in the mode line have already been computed when the
1088 matrix was built, so there isn't much to do, here. */
1090 static INLINE void
1091 x_set_mode_line_face_gc (s)
1092 struct glyph_string *s;
1094 s->gc = s->face->gc;
1098 /* Set S->gc of glyph string S for drawing that glyph string. Set
1099 S->stippled_p to a non-zero value if the face of S has a stipple
1100 pattern. */
1102 static INLINE void
1103 x_set_glyph_string_gc (s)
1104 struct glyph_string *s;
1106 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1108 if (s->hl == DRAW_NORMAL_TEXT)
1110 s->gc = s->face->gc;
1111 s->stippled_p = s->face->stipple != 0;
1113 else if (s->hl == DRAW_INVERSE_VIDEO)
1115 x_set_mode_line_face_gc (s);
1116 s->stippled_p = s->face->stipple != 0;
1118 else if (s->hl == DRAW_CURSOR)
1120 x_set_cursor_gc (s);
1121 s->stippled_p = 0;
1123 else if (s->hl == DRAW_MOUSE_FACE)
1125 x_set_mouse_face_gc (s);
1126 s->stippled_p = s->face->stipple != 0;
1128 else if (s->hl == DRAW_IMAGE_RAISED
1129 || s->hl == DRAW_IMAGE_SUNKEN)
1131 s->gc = s->face->gc;
1132 s->stippled_p = s->face->stipple != 0;
1134 else
1136 s->gc = s->face->gc;
1137 s->stippled_p = s->face->stipple != 0;
1140 /* GC must have been set. */
1141 xassert (s->gc != 0);
1145 /* Set clipping for output of glyph string S. S may be part of a mode
1146 line or menu if we don't have X toolkit support. */
1148 static INLINE void
1149 x_set_glyph_string_clipping (s)
1150 struct glyph_string *s;
1152 RECT *r = s->clip;
1153 int n = get_glyph_string_clip_rects (s, r, 2);
1155 if (n == 1)
1156 w32_set_clip_rectangle (s->hdc, r);
1157 else if (n > 1)
1159 HRGN full_clip, clip1, clip2;
1160 clip1 = CreateRectRgnIndirect (r);
1161 clip2 = CreateRectRgnIndirect (r + 1);
1162 if (CombineRgn (full_clip, clip1, clip2, RGN_OR) != ERROR)
1164 SelectClipRgn (s->hdc, full_clip);
1166 DeleteObject (clip1);
1167 DeleteObject (clip2);
1168 DeleteObject (full_clip);
1170 s->num_clips = n;
1173 /* Set SRC's clipping for output of glyph string DST. This is called
1174 when we are drawing DST's left_overhang or right_overhang only in
1175 the area of SRC. */
1177 static void
1178 x_set_glyph_string_clipping_exactly (src, dst)
1179 struct glyph_string *src, *dst;
1181 RECT r;
1183 r.left = src->x;
1184 r.right = r.left + src->width;
1185 r.top = src->y;
1186 r.bottom = r.top + src->height;
1187 dst->clip[0] = r;
1188 dst->num_clips = 1;
1189 w32_set_clip_rectangle (dst->hdc, &r);
1192 /* RIF:
1193 Compute left and right overhang of glyph string S. */
1195 static void
1196 w32_compute_glyph_string_overhangs (s)
1197 struct glyph_string *s;
1199 if (s->cmp == NULL
1200 && s->first_glyph->type == CHAR_GLYPH
1201 && !s->font_not_found_p)
1203 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1204 struct font *font = s->font;
1205 struct font_metrics metrics;
1206 int i;
1208 for (i = 0; i < s->nchars; i++)
1209 code[i] = s->char2b[i];
1210 font->driver->text_extents (font, code, s->nchars, &metrics);
1211 s->right_overhang = (metrics.rbearing > metrics.width
1212 ? metrics.rbearing - metrics.width : 0);
1213 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1215 else if (s->cmp)
1217 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1218 s->left_overhang = -s->cmp->lbearing;
1222 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1224 static INLINE void
1225 x_clear_glyph_string_rect (s, x, y, w, h)
1226 struct glyph_string *s;
1227 int x, y, w, h;
1229 int real_x = x;
1230 int real_y = y;
1231 int real_w = w;
1232 int real_h = h;
1233 #if 0
1234 /* Take clipping into account. */
1235 if (s->gc->clip_mask == Rect)
1237 real_x = max (real_x, s->gc->clip_rectangle.left);
1238 real_y = max (real_y, s->gc->clip_rectangle.top);
1239 real_w = min (real_w, s->gc->clip_rectangle.right
1240 - s->gc->clip_rectangle.left);
1241 real_h = min (real_h, s->gc->clip_rectangle.bottom
1242 - s->gc->clip_rectangle.top);
1244 #endif
1245 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1246 real_w, real_h);
1250 /* Draw the background of glyph_string S. If S->background_filled_p
1251 is non-zero don't draw it. FORCE_P non-zero means draw the
1252 background even if it wouldn't be drawn normally. This is used
1253 when a string preceding S draws into the background of S, or S
1254 contains the first component of a composition. */
1256 static void
1257 x_draw_glyph_string_background (s, force_p)
1258 struct glyph_string *s;
1259 int force_p;
1261 /* Nothing to do if background has already been drawn or if it
1262 shouldn't be drawn in the first place. */
1263 if (!s->background_filled_p)
1265 int box_line_width = max (s->face->box_line_width, 0);
1267 #if 0 /* TODO: stipple */
1268 if (s->stippled_p)
1270 /* Fill background with a stipple pattern. */
1271 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1272 XFillRectangle (s->display, s->window, s->gc, s->x,
1273 s->y + box_line_width,
1274 s->background_width,
1275 s->height - 2 * box_line_width);
1276 XSetFillStyle (s->display, s->gc, FillSolid);
1277 s->background_filled_p = 1;
1279 else
1280 #endif
1281 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1282 || s->font_not_found_p
1283 || s->extends_to_end_of_line_p
1284 || force_p)
1286 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1287 s->background_width,
1288 s->height - 2 * box_line_width);
1289 s->background_filled_p = 1;
1295 /* Draw the foreground of glyph string S. */
1297 static void
1298 x_draw_glyph_string_foreground (s)
1299 struct glyph_string *s;
1301 int i, x;
1303 /* If first glyph of S has a left box line, start drawing the text
1304 of S to the right of that box line. */
1305 if (s->face->box != FACE_NO_BOX
1306 && s->first_glyph->left_box_line_p)
1307 x = s->x + eabs (s->face->box_line_width);
1308 else
1309 x = s->x;
1311 SetTextColor (s->hdc, s->gc->foreground);
1312 SetBkColor (s->hdc, s->gc->background);
1313 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1315 /* Draw characters of S as rectangles if S's font could not be
1316 loaded. */
1317 if (s->font_not_found_p)
1319 for (i = 0; i < s->nchars; ++i)
1321 struct glyph *g = s->first_glyph + i;
1323 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1324 s->height - 1);
1325 x += g->pixel_width;
1328 else
1330 struct font *font = s->font;
1331 int boff = font->baseline_offset;
1332 int y;
1333 HFONT old_font;
1335 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1337 if (font->vertical_centering)
1338 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1340 y = s->ybase - boff;
1341 if (s->for_overlaps
1342 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1343 font->driver->draw (s, 0, s->nchars, x, y, 0);
1344 else
1345 font->driver->draw (s, 0, s->nchars, x, y, 1);
1346 if (s->face->overstrike)
1347 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1349 SelectObject (s->hdc, old_font);
1353 /* Draw the foreground of composite glyph string S. */
1355 static void
1356 x_draw_composite_glyph_string_foreground (s)
1357 struct glyph_string *s;
1359 int i, j, x;
1360 struct font *font = s->font;
1362 /* If first glyph of S has a left box line, start drawing the text
1363 of S to the right of that box line. */
1364 if (s->face && s->face->box != FACE_NO_BOX
1365 && s->first_glyph->left_box_line_p)
1366 x = s->x + eabs (s->face->box_line_width);
1367 else
1368 x = s->x;
1370 /* S is a glyph string for a composition. S->cmp_from is the index
1371 of the first character drawn for glyphs of this composition.
1372 S->cmp_from == 0 means we are drawing the very first character of
1373 this composition. */
1375 SetTextColor (s->hdc, s->gc->foreground);
1376 SetBkColor (s->hdc, s->gc->background);
1377 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1379 /* Draw a rectangle for the composition if the font for the very
1380 first character of the composition could not be loaded. */
1381 if (s->font_not_found_p)
1383 if (s->cmp_from == 0)
1384 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1385 s->height - 1);
1387 else if (! s->first_glyph->u.cmp.automatic)
1389 int y = s->ybase;
1390 int width = 0;
1391 HFONT old_font;
1393 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1395 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1396 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1398 int xx = x + s->cmp->offsets[j * 2];
1399 int yy = y - s->cmp->offsets[j * 2 + 1];
1401 font->driver->draw (s, j, j + 1, xx, yy, 0);
1402 if (s->face->overstrike)
1403 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1405 SelectObject (s->hdc, old_font);
1407 else
1409 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1410 Lisp_Object glyph;
1411 int y = s->ybase;
1412 int width = 0;
1413 HFONT old_font;
1415 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1417 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1419 glyph = LGSTRING_GLYPH (gstring, i);
1420 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1421 width += LGLYPH_WIDTH (glyph);
1422 else
1424 int xoff, yoff, wadjust;
1426 if (j < i)
1428 font->driver->draw (s, j, i, x, y, 0);
1429 x += width;
1431 xoff = LGLYPH_XOFF (glyph);
1432 yoff = LGLYPH_YOFF (glyph);
1433 wadjust = LGLYPH_WADJUST (glyph);
1434 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1435 x += wadjust;
1436 j = i + 1;
1437 width = 0;
1440 if (j < i)
1441 font->driver->draw (s, j, i, x, y, 0);
1443 SelectObject (s->hdc, old_font);
1448 /* Brightness beyond which a color won't have its highlight brightness
1449 boosted.
1451 Nominally, highlight colors for `3d' faces are calculated by
1452 brightening an object's color by a constant scale factor, but this
1453 doesn't yield good results for dark colors, so for colors who's
1454 brightness is less than this value (on a scale of 0-255) have to
1455 use an additional additive factor.
1457 The value here is set so that the default menu-bar/mode-line color
1458 (grey75) will not have its highlights changed at all. */
1459 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1462 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1463 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1464 If this produces the same color as COLOR, try a color where all RGB
1465 values have DELTA added. Return the allocated color in *COLOR.
1466 DISPLAY is the X display, CMAP is the colormap to operate on.
1467 Value is non-zero if successful. */
1469 static int
1470 w32_alloc_lighter_color (f, color, factor, delta)
1471 struct frame *f;
1472 COLORREF *color;
1473 double factor;
1474 int delta;
1476 COLORREF new;
1477 long bright;
1479 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1480 delta /= 256;
1482 /* Change RGB values by specified FACTOR. Avoid overflow! */
1483 xassert (factor >= 0);
1484 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1485 min (0xff, factor * GetGValue (*color)),
1486 min (0xff, factor * GetBValue (*color)));
1488 /* Calculate brightness of COLOR. */
1489 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1490 + GetBValue (*color)) / 6;
1492 /* We only boost colors that are darker than
1493 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1494 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1495 /* Make an additive adjustment to NEW, because it's dark enough so
1496 that scaling by FACTOR alone isn't enough. */
1498 /* How far below the limit this color is (0 - 1, 1 being darker). */
1499 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1500 /* The additive adjustment. */
1501 int min_delta = delta * dimness * factor / 2;
1503 if (factor < 1)
1504 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1505 max (0, min (0xff, min_delta - GetGValue (*color))),
1506 max (0, min (0xff, min_delta - GetBValue (*color))));
1507 else
1508 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1509 max (0, min (0xff, min_delta + GetGValue (*color))),
1510 max (0, min (0xff, min_delta + GetBValue (*color))));
1513 if (new == *color)
1514 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1515 max (0, min (0xff, delta + GetGValue (*color))),
1516 max (0, min (0xff, delta + GetBValue (*color))));
1518 /* TODO: Map to palette and retry with delta if same? */
1519 /* TODO: Free colors (if using palette)? */
1521 if (new == *color)
1522 return 0;
1524 *color = new;
1526 return 1;
1529 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1530 colors in COLORS. On W32, we no longer try to map colors to
1531 a palette. */
1532 void
1533 x_query_colors (f, colors, ncolors)
1534 struct frame *f;
1535 XColor *colors;
1536 int ncolors;
1538 int i;
1540 for (i = 0; i < ncolors; i++)
1542 DWORD pixel = colors[i].pixel;
1543 /* Convert to a 16 bit value in range 0 - 0xffff. */
1544 colors[i].red = GetRValue (pixel) * 257;
1545 colors[i].green = GetGValue (pixel) * 257;
1546 colors[i].blue = GetBValue (pixel) * 257;
1550 void
1551 x_query_color (f, color)
1552 struct frame *f;
1553 XColor *color;
1555 x_query_colors (f, color, 1);
1559 /* Set up the foreground color for drawing relief lines of glyph
1560 string S. RELIEF is a pointer to a struct relief containing the GC
1561 with which lines will be drawn. Use a color that is FACTOR or
1562 DELTA lighter or darker than the relief's background which is found
1563 in S->f->output_data.x->relief_background. If such a color cannot
1564 be allocated, use DEFAULT_PIXEL, instead. */
1566 static void
1567 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
1568 struct frame *f;
1569 struct relief *relief;
1570 double factor;
1571 int delta;
1572 COLORREF default_pixel;
1574 XGCValues xgcv;
1575 struct w32_output *di = f->output_data.w32;
1576 unsigned long mask = GCForeground;
1577 COLORREF pixel;
1578 COLORREF background = di->relief_background;
1579 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
1581 /* TODO: Free colors (if using palette)? */
1583 /* Allocate new color. */
1584 xgcv.foreground = default_pixel;
1585 pixel = background;
1586 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1588 relief->allocated_p = 1;
1589 xgcv.foreground = relief->pixel = pixel;
1592 if (relief->gc == 0)
1594 #if 0 /* TODO: stipple */
1595 xgcv.stipple = dpyinfo->gray;
1596 mask |= GCStipple;
1597 #endif
1598 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1600 else
1601 XChangeGC (NULL, relief->gc, mask, &xgcv);
1605 /* Set up colors for the relief lines around glyph string S. */
1607 static void
1608 x_setup_relief_colors (s)
1609 struct glyph_string *s;
1611 struct w32_output *di = s->f->output_data.w32;
1612 COLORREF color;
1614 if (s->face->use_box_color_for_shadows_p)
1615 color = s->face->box_color;
1616 else if (s->first_glyph->type == IMAGE_GLYPH
1617 && s->img->pixmap
1618 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1619 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1620 else
1621 color = s->gc->background;
1623 if (di->white_relief.gc == 0
1624 || color != di->relief_background)
1626 di->relief_background = color;
1627 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1628 WHITE_PIX_DEFAULT (s->f));
1629 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1630 BLACK_PIX_DEFAULT (s->f));
1635 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1636 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1637 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1638 relief. LEFT_P non-zero means draw a relief on the left side of
1639 the rectangle. RIGHT_P non-zero means draw a relief on the right
1640 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1641 when drawing. */
1643 static void
1644 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1645 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
1646 struct frame *f;
1647 int left_x, top_y, right_x, bottom_y, width;
1648 int top_p, bot_p, left_p, right_p, raised_p;
1649 RECT *clip_rect;
1651 int i;
1652 XGCValues gc;
1653 HDC hdc = get_frame_dc (f);
1655 if (raised_p)
1656 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1657 else
1658 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1660 w32_set_clip_rectangle (hdc, clip_rect);
1662 /* Top. */
1663 if (top_p)
1664 for (i = 0; i < width; ++i)
1665 w32_fill_area (f, hdc, gc.foreground,
1666 left_x + i * left_p, top_y + i,
1667 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1669 /* Left. */
1670 if (left_p)
1671 for (i = 0; i < width; ++i)
1672 w32_fill_area (f, hdc, gc.foreground,
1673 left_x + i, top_y + i, 1,
1674 bottom_y - top_y - 2 * i + 1);
1676 if (raised_p)
1677 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1678 else
1679 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1681 /* Bottom. */
1682 if (bot_p)
1683 for (i = 0; i < width; ++i)
1684 w32_fill_area (f, hdc, gc.foreground,
1685 left_x + i * left_p, bottom_y - i,
1686 right_x - left_x - i * (left_p + right_p) + 1, 1);
1688 /* Right. */
1689 if (right_p)
1690 for (i = 0; i < width; ++i)
1691 w32_fill_area (f, hdc, gc.foreground,
1692 right_x - i, top_y + i + 1, 1,
1693 bottom_y - top_y - 2 * i - 1);
1695 w32_set_clip_rectangle (hdc, NULL);
1697 release_frame_dc (f, hdc);
1701 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1702 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1703 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1704 left side of the rectangle. RIGHT_P non-zero means draw a line
1705 on the right side of the rectangle. CLIP_RECT is the clipping
1706 rectangle to use when drawing. */
1708 static void
1709 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1710 left_p, right_p, clip_rect)
1711 struct glyph_string *s;
1712 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
1713 RECT *clip_rect;
1715 w32_set_clip_rectangle (s->hdc, clip_rect);
1717 /* Top. */
1718 w32_fill_area (s->f, s->hdc, s->face->box_color,
1719 left_x, top_y, right_x - left_x + 1, width);
1721 /* Left. */
1722 if (left_p)
1724 w32_fill_area (s->f, s->hdc, s->face->box_color,
1725 left_x, top_y, width, bottom_y - top_y + 1);
1728 /* Bottom. */
1729 w32_fill_area (s->f, s->hdc, s->face->box_color,
1730 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1732 /* Right. */
1733 if (right_p)
1735 w32_fill_area (s->f, s->hdc, s->face->box_color,
1736 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1739 w32_set_clip_rectangle (s->hdc, NULL);
1743 /* Draw a box around glyph string S. */
1745 static void
1746 x_draw_glyph_string_box (s)
1747 struct glyph_string *s;
1749 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1750 int left_p, right_p;
1751 struct glyph *last_glyph;
1752 RECT clip_rect;
1754 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1755 ? WINDOW_RIGHT_EDGE_X (s->w)
1756 : window_box_right (s->w, s->area));
1758 /* The glyph that may have a right box line. */
1759 last_glyph = (s->cmp || s->img
1760 ? s->first_glyph
1761 : s->first_glyph + s->nchars - 1);
1763 width = eabs (s->face->box_line_width);
1764 raised_p = s->face->box == FACE_RAISED_BOX;
1765 left_x = s->x;
1766 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1767 ? last_x - 1
1768 : min (last_x, s->x + s->background_width) - 1));
1769 top_y = s->y;
1770 bottom_y = top_y + s->height - 1;
1772 left_p = (s->first_glyph->left_box_line_p
1773 || (s->hl == DRAW_MOUSE_FACE
1774 && (s->prev == NULL
1775 || s->prev->hl != s->hl)));
1776 right_p = (last_glyph->right_box_line_p
1777 || (s->hl == DRAW_MOUSE_FACE
1778 && (s->next == NULL
1779 || s->next->hl != s->hl)));
1781 get_glyph_string_clip_rect (s, &clip_rect);
1783 if (s->face->box == FACE_SIMPLE_BOX)
1784 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1785 left_p, right_p, &clip_rect);
1786 else
1788 x_setup_relief_colors (s);
1789 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1790 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1795 /* Draw foreground of image glyph string S. */
1797 static void
1798 x_draw_image_foreground (s)
1799 struct glyph_string *s;
1801 int x = s->x;
1802 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1804 /* If first glyph of S has a left box line, start drawing it to the
1805 right of that line. */
1806 if (s->face->box != FACE_NO_BOX
1807 && s->first_glyph->left_box_line_p
1808 && s->slice.x == 0)
1809 x += eabs (s->face->box_line_width);
1811 /* If there is a margin around the image, adjust x- and y-position
1812 by that margin. */
1813 if (s->slice.x == 0)
1814 x += s->img->hmargin;
1815 if (s->slice.y == 0)
1816 y += s->img->vmargin;
1818 SaveDC (s->hdc);
1820 if (s->img->pixmap)
1822 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1823 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1824 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1825 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1826 SetBkColor (compat_hdc, RGB (255, 255, 255));
1827 SetTextColor (s->hdc, RGB (0, 0, 0));
1828 x_set_glyph_string_clipping (s);
1830 if (s->img->mask)
1832 HDC mask_dc = CreateCompatibleDC (s->hdc);
1833 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1835 SetTextColor (s->hdc, RGB (255, 255, 255));
1836 SetBkColor (s->hdc, RGB (0, 0, 0));
1838 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1839 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1840 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1841 mask_dc, s->slice.x, s->slice.y, SRCAND);
1842 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1843 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1845 SelectObject (mask_dc, mask_orig_obj);
1846 DeleteDC (mask_dc);
1848 else
1850 SetTextColor (s->hdc, s->gc->foreground);
1851 SetBkColor (s->hdc, s->gc->background);
1853 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1854 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1856 /* When the image has a mask, we can expect that at
1857 least part of a mouse highlight or a block cursor will
1858 be visible. If the image doesn't have a mask, make
1859 a block cursor visible by drawing a rectangle around
1860 the image. I believe it's looking better if we do
1861 nothing here for mouse-face. */
1862 if (s->hl == DRAW_CURSOR)
1864 int r = s->img->relief;
1865 if (r < 0) r = -r;
1866 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1867 s->slice.width + r*2 - 1,
1868 s->slice.height + r*2 - 1);
1872 w32_set_clip_rectangle (s->hdc, NULL);
1873 SelectObject (s->hdc, orig_brush);
1874 DeleteObject (fg_brush);
1875 SelectObject (compat_hdc, orig_obj);
1876 DeleteDC (compat_hdc);
1878 else
1879 w32_draw_rectangle (s->hdc, s->gc, x, y,
1880 s->slice.width - 1, s->slice.height - 1);
1882 RestoreDC (s->hdc ,-1);
1886 /* Draw a relief around the image glyph string S. */
1888 static void
1889 x_draw_image_relief (s)
1890 struct glyph_string *s;
1892 int x0, y0, x1, y1, thick, raised_p;
1893 RECT r;
1894 int x = s->x;
1895 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1897 /* If first glyph of S has a left box line, start drawing it to the
1898 right of that line. */
1899 if (s->face->box != FACE_NO_BOX
1900 && s->first_glyph->left_box_line_p
1901 && s->slice.x == 0)
1902 x += eabs (s->face->box_line_width);
1904 /* If there is a margin around the image, adjust x- and y-position
1905 by that margin. */
1906 if (s->slice.x == 0)
1907 x += s->img->hmargin;
1908 if (s->slice.y == 0)
1909 y += s->img->vmargin;
1911 if (s->hl == DRAW_IMAGE_SUNKEN
1912 || s->hl == DRAW_IMAGE_RAISED)
1914 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1915 raised_p = s->hl == DRAW_IMAGE_RAISED;
1917 else
1919 thick = eabs (s->img->relief);
1920 raised_p = s->img->relief > 0;
1923 x0 = x - thick;
1924 y0 = y - thick;
1925 x1 = x + s->slice.width + thick - 1;
1926 y1 = y + s->slice.height + thick - 1;
1928 x_setup_relief_colors (s);
1929 get_glyph_string_clip_rect (s, &r);
1930 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
1931 s->slice.y == 0,
1932 s->slice.y + s->slice.height == s->img->height,
1933 s->slice.x == 0,
1934 s->slice.x + s->slice.width == s->img->width,
1935 &r);
1939 /* Draw the foreground of image glyph string S to PIXMAP. */
1941 static void
1942 w32_draw_image_foreground_1 (s, pixmap)
1943 struct glyph_string *s;
1944 HBITMAP pixmap;
1946 HDC hdc = CreateCompatibleDC (s->hdc);
1947 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1948 int x = 0;
1949 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1951 /* If first glyph of S has a left box line, start drawing it to the
1952 right of that line. */
1953 if (s->face->box != FACE_NO_BOX
1954 && s->first_glyph->left_box_line_p
1955 && s->slice.x == 0)
1956 x += eabs (s->face->box_line_width);
1958 /* If there is a margin around the image, adjust x- and y-position
1959 by that margin. */
1960 if (s->slice.x == 0)
1961 x += s->img->hmargin;
1962 if (s->slice.y == 0)
1963 y += s->img->vmargin;
1965 if (s->img->pixmap)
1967 HDC compat_hdc = CreateCompatibleDC (hdc);
1968 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1969 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1970 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1972 if (s->img->mask)
1974 HDC mask_dc = CreateCompatibleDC (hdc);
1975 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1977 SetTextColor (hdc, RGB (0, 0, 0));
1978 SetBkColor (hdc, RGB (255, 255, 255));
1979 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1980 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1981 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1982 mask_dc, s->slice.x, s->slice.y, SRCAND);
1983 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1984 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1986 SelectObject (mask_dc, mask_orig_obj);
1987 DeleteDC (mask_dc);
1989 else
1991 SetTextColor (hdc, s->gc->foreground);
1992 SetBkColor (hdc, s->gc->background);
1994 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1995 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1997 /* When the image has a mask, we can expect that at
1998 least part of a mouse highlight or a block cursor will
1999 be visible. If the image doesn't have a mask, make
2000 a block cursor visible by drawing a rectangle around
2001 the image. I believe it's looking better if we do
2002 nothing here for mouse-face. */
2003 if (s->hl == DRAW_CURSOR)
2005 int r = s->img->relief;
2006 if (r < 0) r = -r;
2007 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2008 s->slice.width + r*2 - 1,
2009 s->slice.height + r*2 - 1);
2013 SelectObject (hdc, orig_brush);
2014 DeleteObject (fg_brush);
2015 SelectObject (compat_hdc, orig_obj);
2016 DeleteDC (compat_hdc);
2018 else
2019 w32_draw_rectangle (hdc, s->gc, x, y,
2020 s->slice.width - 1, s->slice.height - 1);
2022 SelectObject (hdc, orig_hdc_obj);
2023 DeleteDC (hdc);
2027 /* Draw part of the background of glyph string S. X, Y, W, and H
2028 give the rectangle to draw. */
2030 static void
2031 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2032 struct glyph_string *s;
2033 int x, y, w, h;
2035 #if 0 /* TODO: stipple */
2036 if (s->stippled_p)
2038 /* Fill background with a stipple pattern. */
2039 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2040 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2041 XSetFillStyle (s->display, s->gc, FillSolid);
2043 else
2044 #endif
2045 x_clear_glyph_string_rect (s, x, y, w, h);
2049 /* Draw image glyph string S.
2051 s->y
2052 s->x +-------------------------
2053 | s->face->box
2055 | +-------------------------
2056 | | s->img->vmargin
2058 | | +-------------------
2059 | | | the image
2063 static void
2064 x_draw_image_glyph_string (s)
2065 struct glyph_string *s;
2067 int x, y;
2068 int box_line_hwidth = eabs (s->face->box_line_width);
2069 int box_line_vwidth = max (s->face->box_line_width, 0);
2070 int height;
2071 HBITMAP pixmap = 0;
2073 height = s->height - 2 * box_line_vwidth;
2075 /* Fill background with face under the image. Do it only if row is
2076 taller than image or if image has a clip mask to reduce
2077 flickering. */
2078 s->stippled_p = s->face->stipple != 0;
2079 if (height > s->slice.height
2080 || s->img->hmargin
2081 || s->img->vmargin
2082 || s->img->mask
2083 || s->img->pixmap == 0
2084 || s->width != s->background_width)
2086 x = s->x;
2087 if (s->first_glyph->left_box_line_p
2088 && s->slice.x == 0)
2089 x += box_line_hwidth;
2091 y = s->y;
2092 if (s->slice.y == 0)
2093 y += box_line_vwidth;
2095 #if 0 /* TODO: figure out if we need to do this on Windows. */
2096 if (s->img->mask)
2098 /* Create a pixmap as large as the glyph string. Fill it
2099 with the background color. Copy the image to it, using
2100 its mask. Copy the temporary pixmap to the display. */
2101 Screen *screen = FRAME_X_SCREEN (s->f);
2102 int depth = DefaultDepthOfScreen (screen);
2104 /* Create a pixmap as large as the glyph string. */
2105 pixmap = XCreatePixmap (s->display, s->window,
2106 s->background_width,
2107 s->height, depth);
2109 /* Don't clip in the following because we're working on the
2110 pixmap. */
2111 XSetClipMask (s->display, s->gc, None);
2113 /* Fill the pixmap with the background color/stipple. */
2114 if (s->stippled_p)
2116 /* Fill background with a stipple pattern. */
2117 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2118 XFillRectangle (s->display, pixmap, s->gc,
2119 0, 0, s->background_width, s->height);
2120 XSetFillStyle (s->display, s->gc, FillSolid);
2122 else
2124 XGCValues xgcv;
2125 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2126 &xgcv);
2127 XSetForeground (s->display, s->gc, xgcv.background);
2128 XFillRectangle (s->display, pixmap, s->gc,
2129 0, 0, s->background_width, s->height);
2130 XSetForeground (s->display, s->gc, xgcv.foreground);
2133 else
2134 #endif
2135 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2137 s->background_filled_p = 1;
2140 /* Draw the foreground. */
2141 if (pixmap != 0)
2143 w32_draw_image_foreground_1 (s, pixmap);
2144 x_set_glyph_string_clipping (s);
2146 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2147 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2148 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2149 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2151 SetTextColor (s->hdc, s->gc->foreground);
2152 SetBkColor (s->hdc, s->gc->background);
2153 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2154 compat_hdc, 0, 0, SRCCOPY);
2156 SelectObject (s->hdc, orig_brush);
2157 DeleteObject (fg_brush);
2158 SelectObject (compat_hdc, orig_obj);
2159 DeleteDC (compat_hdc);
2161 DeleteObject (pixmap);
2162 pixmap = 0;
2164 else
2165 x_draw_image_foreground (s);
2167 /* If we must draw a relief around the image, do it. */
2168 if (s->img->relief
2169 || s->hl == DRAW_IMAGE_RAISED
2170 || s->hl == DRAW_IMAGE_SUNKEN)
2171 x_draw_image_relief (s);
2175 /* Draw stretch glyph string S. */
2177 static void
2178 x_draw_stretch_glyph_string (s)
2179 struct glyph_string *s;
2181 xassert (s->first_glyph->type == STRETCH_GLYPH);
2183 if (s->hl == DRAW_CURSOR
2184 && !x_stretch_cursor_p)
2186 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2187 as wide as the stretch glyph. */
2188 int width, background_width = s->background_width;
2189 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2191 if (x < left_x)
2193 background_width -= left_x - x;
2194 x = left_x;
2196 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2198 /* Draw cursor. */
2199 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2201 /* Clear rest using the GC of the original non-cursor face. */
2202 if (width < background_width)
2204 XGCValues *gc = s->face->gc;
2205 int y = s->y;
2206 int w = background_width - width, h = s->height;
2207 RECT r;
2208 HDC hdc = s->hdc;
2210 x += width;
2211 if (s->row->mouse_face_p
2212 && cursor_in_mouse_face_p (s->w))
2214 x_set_mouse_face_gc (s);
2215 gc = s->gc;
2217 else
2218 gc = s->face->gc;
2220 get_glyph_string_clip_rect (s, &r);
2221 w32_set_clip_rectangle (hdc, &r);
2223 #if 0 /* TODO: stipple */
2224 if (s->face->stipple)
2226 /* Fill background with a stipple pattern. */
2227 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2228 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2229 XSetFillStyle (s->display, gc, FillSolid);
2231 else
2232 #endif
2234 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2238 else if (!s->background_filled_p)
2240 int background_width = s->background_width;
2241 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2243 /* Don't draw into left margin, fringe or scrollbar area
2244 except for header line and mode line. */
2245 if (x < left_x && !s->row->mode_line_p)
2247 background_width -= left_x - x;
2248 x = left_x;
2250 if (background_width > 0)
2251 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2254 s->background_filled_p = 1;
2258 /* Draw glyph string S. */
2260 static void
2261 x_draw_glyph_string (s)
2262 struct glyph_string *s;
2264 int relief_drawn_p = 0;
2266 /* If S draws into the background of its successor, draw the
2267 background of the successor first so that S can draw into it.
2268 This makes S->next use XDrawString instead of XDrawImageString. */
2269 if (s->next && s->right_overhang && !s->for_overlaps)
2271 int width;
2272 struct glyph_string *next;
2273 for (width = 0, next = s->next;
2274 next && width < s->right_overhang;
2275 width += next->width, next = next->next)
2276 if (next->first_glyph->type != IMAGE_GLYPH)
2278 x_set_glyph_string_gc (next);
2279 x_set_glyph_string_clipping (next);
2280 if (next->first_glyph->type == STRETCH_GLYPH)
2281 x_draw_stretch_glyph_string (next);
2282 else
2283 x_draw_glyph_string_background (next, 1);
2284 next->num_clips = 0;
2288 /* Set up S->gc, set clipping and draw S. */
2289 x_set_glyph_string_gc (s);
2291 /* Draw relief (if any) in advance for char/composition so that the
2292 glyph string can be drawn over it. */
2293 if (!s->for_overlaps
2294 && s->face->box != FACE_NO_BOX
2295 && (s->first_glyph->type == CHAR_GLYPH
2296 || s->first_glyph->type == COMPOSITE_GLYPH))
2299 x_set_glyph_string_clipping (s);
2300 x_draw_glyph_string_background (s, 1);
2301 x_draw_glyph_string_box (s);
2302 x_set_glyph_string_clipping (s);
2303 relief_drawn_p = 1;
2305 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2306 && !s->clip_tail
2307 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2308 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2309 /* We must clip just this glyph. left_overhang part has already
2310 drawn when s->prev was drawn, and right_overhang part will be
2311 drawn later when s->next is drawn. */
2312 x_set_glyph_string_clipping_exactly (s, s);
2313 else
2314 x_set_glyph_string_clipping (s);
2316 switch (s->first_glyph->type)
2318 case IMAGE_GLYPH:
2319 x_draw_image_glyph_string (s);
2320 break;
2322 case STRETCH_GLYPH:
2323 x_draw_stretch_glyph_string (s);
2324 break;
2326 case CHAR_GLYPH:
2327 if (s->for_overlaps)
2328 s->background_filled_p = 1;
2329 else
2330 x_draw_glyph_string_background (s, 0);
2331 x_draw_glyph_string_foreground (s);
2332 break;
2334 case COMPOSITE_GLYPH:
2335 if (s->for_overlaps || (s->cmp_from > 0
2336 && ! s->first_glyph->u.cmp.automatic))
2337 s->background_filled_p = 1;
2338 else
2339 x_draw_glyph_string_background (s, 1);
2340 x_draw_composite_glyph_string_foreground (s);
2341 break;
2343 default:
2344 abort ();
2347 if (!s->for_overlaps)
2349 /* Draw underline. */
2350 if (s->face->underline_p)
2352 unsigned long thickness, position;
2353 int y;
2355 if (s->prev && s->prev->face->underline_p)
2357 /* We use the same underline style as the previous one. */
2358 thickness = s->prev->underline_thickness;
2359 position = s->prev->underline_position;
2361 else
2363 /* Get the underline thickness. Default is 1 pixel. */
2364 if (s->font && s->font->underline_thickness > 0)
2365 thickness = s->font->underline_thickness;
2366 else
2367 thickness = 1;
2368 if (x_underline_at_descent_line)
2369 position = (s->height - thickness) - (s->ybase - s->y);
2370 else
2372 /* Get the underline position. This is the recommended
2373 vertical offset in pixels from the baseline to the top of
2374 the underline. This is a signed value according to the
2375 specs, and its default is
2377 ROUND ((maximum_descent) / 2), with
2378 ROUND (x) = floor (x + 0.5) */
2380 if (x_use_underline_position_properties
2381 && s->font && s->font->underline_position >= 0)
2382 position = s->font->underline_position;
2383 else if (s->font)
2384 position = (s->font->descent + 1) / 2;
2386 position = max (position, underline_minimum_offset);
2388 /* Check the sanity of thickness and position. We should
2389 avoid drawing underline out of the current line area. */
2390 if (s->y + s->height <= s->ybase + position)
2391 position = (s->height - 1) - (s->ybase - s->y);
2392 if (s->y + s->height < s->ybase + position + thickness)
2393 thickness = (s->y + s->height) - (s->ybase + position);
2394 s->underline_thickness = thickness;
2395 s->underline_position =position;
2396 y = s->ybase + position;
2397 if (s->face->underline_defaulted_p)
2399 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2400 y, s->background_width, 1);
2402 else
2404 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2405 y, s->background_width, 1);
2408 /* Draw overline. */
2409 if (s->face->overline_p)
2411 unsigned long dy = 0, h = 1;
2413 if (s->face->overline_color_defaulted_p)
2415 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2416 s->y + dy, s->background_width, h);
2418 else
2420 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2421 s->y + dy, s->background_width, h);
2425 /* Draw strike-through. */
2426 if (s->face->strike_through_p
2427 && !FONT_TEXTMETRIC(s->font).tmStruckOut)
2429 unsigned long h = 1;
2430 unsigned long dy = (s->height - h) / 2;
2432 if (s->face->strike_through_color_defaulted_p)
2434 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2435 s->width, h);
2437 else
2439 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2440 s->y + dy, s->width, h);
2444 /* Draw relief if not yet drawn. */
2445 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2446 x_draw_glyph_string_box (s);
2448 if (s->prev)
2450 struct glyph_string *prev;
2452 for (prev = s->prev; prev; prev = prev->prev)
2453 if (prev->hl != s->hl
2454 && prev->x + prev->width + prev->right_overhang > s->x)
2456 /* As prev was drawn while clipped to its own area, we
2457 must draw the right_overhang part using s->hl now. */
2458 enum draw_glyphs_face save = prev->hl;
2460 prev->hl = s->hl;
2461 x_set_glyph_string_gc (prev);
2462 x_set_glyph_string_clipping_exactly (s, prev);
2463 if (prev->first_glyph->type == CHAR_GLYPH)
2464 x_draw_glyph_string_foreground (prev);
2465 else
2466 x_draw_composite_glyph_string_foreground (prev);
2467 w32_set_clip_rectangle (prev->hdc, NULL);
2468 prev->hl = save;
2469 prev->num_clips = 0;
2473 if (s->next)
2475 struct glyph_string *next;
2477 for (next = s->next; next; next = next->next)
2478 if (next->hl != s->hl
2479 && next->x - next->left_overhang < s->x + s->width)
2481 /* As next will be drawn while clipped to its own area,
2482 we must draw the left_overhang part using s->hl now. */
2483 enum draw_glyphs_face save = next->hl;
2485 next->hl = s->hl;
2486 x_set_glyph_string_gc (next);
2487 x_set_glyph_string_clipping_exactly (s, next);
2488 if (next->first_glyph->type == CHAR_GLYPH)
2489 x_draw_glyph_string_foreground (next);
2490 else
2491 x_draw_composite_glyph_string_foreground (next);
2492 w32_set_clip_rectangle (next->hdc, NULL);
2493 next->hl = save;
2494 next->num_clips = 0;
2499 /* Reset clipping. */
2500 w32_set_clip_rectangle (s->hdc, NULL);
2501 s->num_clips = 0;
2505 /* Shift display to make room for inserted glyphs. */
2507 void
2508 w32_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2509 struct frame *f;
2510 int x, y, width, height, shift_by;
2512 HDC hdc;
2514 hdc = get_frame_dc (f);
2515 BitBlt (hdc, x + shift_by, y, width, height,
2516 hdc, x, y, SRCCOPY);
2518 release_frame_dc (f, hdc);
2522 /* Delete N glyphs at the nominal cursor position. Not implemented
2523 for X frames. */
2525 static void
2526 x_delete_glyphs (f, n)
2527 struct frame *f;
2528 register int n;
2530 if (! FRAME_W32_P (f))
2531 return;
2533 abort ();
2537 /* Clear entire frame. If updating_frame is non-null, clear that
2538 frame. Otherwise clear the selected frame. */
2540 static void
2541 x_clear_frame (struct frame *f)
2543 if (! FRAME_W32_P (f))
2544 return;
2546 /* Clearing the frame will erase any cursor, so mark them all as no
2547 longer visible. */
2548 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2549 output_cursor.hpos = output_cursor.vpos = 0;
2550 output_cursor.x = -1;
2552 /* We don't set the output cursor here because there will always
2553 follow an explicit cursor_to. */
2554 BLOCK_INPUT;
2556 w32_clear_window (f);
2558 /* We have to clear the scroll bars, too. If we have changed
2559 colors or something like that, then they should be notified. */
2560 x_scroll_bar_clear (f);
2562 UNBLOCK_INPUT;
2566 /* Make audible bell. */
2568 static void
2569 w32_ring_bell (struct frame *f)
2571 BLOCK_INPUT;
2573 if (FRAME_W32_P (f) && visible_bell)
2575 int i;
2576 HWND hwnd = FRAME_W32_WINDOW (f);
2578 for (i = 0; i < 5; i++)
2580 FlashWindow (hwnd, TRUE);
2581 Sleep (10);
2583 FlashWindow (hwnd, FALSE);
2585 else
2586 w32_sys_ring_bell (f);
2588 UNBLOCK_INPUT;
2592 /* Specify how many text lines, from the top of the window,
2593 should be affected by insert-lines and delete-lines operations.
2594 This, and those operations, are used only within an update
2595 that is bounded by calls to x_update_begin and x_update_end. */
2597 static void
2598 w32_set_terminal_window (n)
2599 register int n;
2601 /* This function intentionally left blank. */
2605 /***********************************************************************
2606 Line Dance
2607 ***********************************************************************/
2609 /* Perform an insert-lines or delete-lines operation, inserting N
2610 lines or deleting -N lines at vertical position VPOS. */
2612 static void
2613 x_ins_del_lines (f, vpos, n)
2614 struct frame *f;
2615 int vpos, n;
2617 if (! FRAME_W32_P (f))
2618 return;
2620 abort ();
2624 /* Scroll part of the display as described by RUN. */
2626 static void
2627 x_scroll_run (w, run)
2628 struct window *w;
2629 struct run *run;
2631 struct frame *f = XFRAME (w->frame);
2632 int x, y, width, height, from_y, to_y, bottom_y;
2633 HWND hwnd = FRAME_W32_WINDOW (f);
2634 HRGN expect_dirty;
2636 /* Get frame-relative bounding box of the text display area of W,
2637 without mode lines. Include in this box the left and right
2638 fringes of W. */
2639 window_box (w, -1, &x, &y, &width, &height);
2641 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2642 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2643 bottom_y = y + height;
2645 if (to_y < from_y)
2647 /* Scrolling up. Make sure we don't copy part of the mode
2648 line at the bottom. */
2649 if (from_y + run->height > bottom_y)
2650 height = bottom_y - from_y;
2651 else
2652 height = run->height;
2653 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2655 else
2657 /* Scolling down. Make sure we don't copy over the mode line.
2658 at the bottom. */
2659 if (to_y + run->height > bottom_y)
2660 height = bottom_y - to_y;
2661 else
2662 height = run->height;
2663 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2666 BLOCK_INPUT;
2668 /* Cursor off. Will be switched on again in x_update_window_end. */
2669 updated_window = w;
2670 x_clear_cursor (w);
2673 RECT from;
2674 RECT to;
2675 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2676 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2678 from.left = to.left = x;
2679 from.right = to.right = x + width;
2680 from.top = from_y;
2681 from.bottom = from_y + height;
2682 to.top = y;
2683 to.bottom = bottom_y;
2685 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2686 NULL, SW_INVALIDATE);
2688 /* Combine this with what we expect to be dirty. This covers the
2689 case where not all of the region we expect is actually dirty. */
2690 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2692 /* If the dirty region is not what we expected, redraw the entire frame. */
2693 if (!EqualRgn (combined, expect_dirty))
2694 SET_FRAME_GARBAGED (f);
2696 DeleteObject (dirty);
2697 DeleteObject (combined);
2700 UNBLOCK_INPUT;
2701 DeleteObject (expect_dirty);
2706 /***********************************************************************
2707 Exposure Events
2708 ***********************************************************************/
2710 static void
2711 frame_highlight (f)
2712 struct frame *f;
2714 x_update_cursor (f, 1);
2715 x_set_frame_alpha (f);
2718 static void
2719 frame_unhighlight (f)
2720 struct frame *f;
2722 x_update_cursor (f, 1);
2723 x_set_frame_alpha (f);
2726 /* The focus has changed. Update the frames as necessary to reflect
2727 the new situation. Note that we can't change the selected frame
2728 here, because the Lisp code we are interrupting might become confused.
2729 Each event gets marked with the frame in which it occurred, so the
2730 Lisp code can tell when the switch took place by examining the events. */
2732 static void
2733 x_new_focus_frame (dpyinfo, frame)
2734 struct w32_display_info *dpyinfo;
2735 struct frame *frame;
2737 struct frame *old_focus = dpyinfo->w32_focus_frame;
2739 if (frame != dpyinfo->w32_focus_frame)
2741 /* Set this before calling other routines, so that they see
2742 the correct value of w32_focus_frame. */
2743 dpyinfo->w32_focus_frame = frame;
2745 if (old_focus && old_focus->auto_lower)
2746 x_lower_frame (old_focus);
2748 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2749 pending_autoraise_frame = dpyinfo->w32_focus_frame;
2750 else
2751 pending_autoraise_frame = 0;
2754 x_frame_rehighlight (dpyinfo);
2758 /* Handle FocusIn and FocusOut state changes for FRAME.
2759 If FRAME has focus and there exists more than one frame, puts
2760 a FOCUS_IN_EVENT into *BUFP. */
2762 static void
2763 x_focus_changed (type, state, dpyinfo, frame, bufp)
2764 int type;
2765 int state;
2766 struct w32_display_info *dpyinfo;
2767 struct frame *frame;
2768 struct input_event *bufp;
2770 if (type == WM_SETFOCUS)
2772 if (dpyinfo->w32_focus_event_frame != frame)
2774 x_new_focus_frame (dpyinfo, frame);
2775 dpyinfo->w32_focus_event_frame = frame;
2777 /* Don't stop displaying the initial startup message
2778 for a switch-frame event we don't need. */
2779 if (NILP (Vterminal_frame)
2780 && CONSP (Vframe_list)
2781 && !NILP (XCDR (Vframe_list)))
2783 bufp->kind = FOCUS_IN_EVENT;
2784 XSETFRAME (bufp->frame_or_window, frame);
2788 frame->output_data.x->focus_state |= state;
2790 /* TODO: IME focus? */
2792 else if (type == WM_KILLFOCUS)
2794 frame->output_data.x->focus_state &= ~state;
2796 if (dpyinfo->w32_focus_event_frame == frame)
2798 dpyinfo->w32_focus_event_frame = 0;
2799 x_new_focus_frame (dpyinfo, 0);
2802 /* TODO: IME focus? */
2807 /* The focus may have changed. Figure out if it is a real focus change,
2808 by checking both FocusIn/Out and Enter/LeaveNotify events.
2810 Returns FOCUS_IN_EVENT event in *BUFP. */
2812 static void
2813 w32_detect_focus_change (dpyinfo, event, bufp)
2814 struct w32_display_info *dpyinfo;
2815 W32Msg *event;
2816 struct input_event *bufp;
2818 struct frame *frame;
2820 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2821 if (! frame)
2822 return;
2824 /* On w32, this is only called from focus events, so no switch needed. */
2825 x_focus_changed (event->msg.message,
2826 (event->msg.message == WM_KILLFOCUS ?
2827 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2828 dpyinfo, frame, bufp);
2832 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2834 void
2835 x_mouse_leave (dpyinfo)
2836 struct w32_display_info *dpyinfo;
2838 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2841 /* The focus has changed, or we have redirected a frame's focus to
2842 another frame (this happens when a frame uses a surrogate
2843 mini-buffer frame). Shift the highlight as appropriate.
2845 The FRAME argument doesn't necessarily have anything to do with which
2846 frame is being highlighted or un-highlighted; we only use it to find
2847 the appropriate X display info. */
2849 static void
2850 w32_frame_rehighlight (frame)
2851 struct frame *frame;
2853 if (! FRAME_W32_P (frame))
2854 return;
2855 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
2858 static void
2859 x_frame_rehighlight (dpyinfo)
2860 struct w32_display_info *dpyinfo;
2862 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2864 if (dpyinfo->w32_focus_frame)
2866 dpyinfo->x_highlight_frame
2867 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2868 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2869 : dpyinfo->w32_focus_frame);
2870 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2872 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
2873 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2876 else
2877 dpyinfo->x_highlight_frame = 0;
2879 if (dpyinfo->x_highlight_frame != old_highlight)
2881 if (old_highlight)
2882 frame_unhighlight (old_highlight);
2883 if (dpyinfo->x_highlight_frame)
2884 frame_highlight (dpyinfo->x_highlight_frame);
2888 /* Keyboard processing - modifier keys, etc. */
2890 /* Convert a keysym to its name. */
2892 char *
2893 x_get_keysym_name (keysym)
2894 int keysym;
2896 /* Make static so we can always return it */
2897 static char value[100];
2899 BLOCK_INPUT;
2900 GetKeyNameText (keysym, value, 100);
2901 UNBLOCK_INPUT;
2903 return value;
2908 /* Mouse clicks and mouse movement. Rah. */
2910 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2911 the state in PUP. XBUTTON provides extra information for extended mouse
2912 button messages. Returns FALSE if unable to parse the message. */
2913 BOOL
2914 parse_button (message, xbutton, pbutton, pup)
2915 int message;
2916 int xbutton;
2917 int * pbutton;
2918 int * pup;
2920 int button = 0;
2921 int up = 0;
2923 switch (message)
2925 case WM_LBUTTONDOWN:
2926 button = 0;
2927 up = 0;
2928 break;
2929 case WM_LBUTTONUP:
2930 button = 0;
2931 up = 1;
2932 break;
2933 case WM_MBUTTONDOWN:
2934 if (NILP (Vw32_swap_mouse_buttons))
2935 button = 1;
2936 else
2937 button = 2;
2938 up = 0;
2939 break;
2940 case WM_MBUTTONUP:
2941 if (NILP (Vw32_swap_mouse_buttons))
2942 button = 1;
2943 else
2944 button = 2;
2945 up = 1;
2946 break;
2947 case WM_RBUTTONDOWN:
2948 if (NILP (Vw32_swap_mouse_buttons))
2949 button = 2;
2950 else
2951 button = 1;
2952 up = 0;
2953 break;
2954 case WM_RBUTTONUP:
2955 if (NILP (Vw32_swap_mouse_buttons))
2956 button = 2;
2957 else
2958 button = 1;
2959 up = 1;
2960 break;
2961 case WM_XBUTTONDOWN:
2962 button = xbutton + 2;
2963 up = 0;
2964 break;
2965 case WM_XBUTTONUP:
2966 button = xbutton + 2;
2967 up = 1;
2968 break;
2969 default:
2970 return (FALSE);
2973 if (pup) *pup = up;
2974 if (pbutton) *pbutton = button;
2976 return (TRUE);
2980 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2982 If the event is a button press, then note that we have grabbed
2983 the mouse. */
2985 static Lisp_Object
2986 construct_mouse_click (result, msg, f)
2987 struct input_event *result;
2988 W32Msg *msg;
2989 struct frame *f;
2991 int button;
2992 int up;
2994 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
2995 &button, &up);
2997 /* Make the event type NO_EVENT; we'll change that when we decide
2998 otherwise. */
2999 result->kind = MOUSE_CLICK_EVENT;
3000 result->code = button;
3001 result->timestamp = msg->msg.time;
3002 result->modifiers = (msg->dwModifiers
3003 | (up
3004 ? up_modifier
3005 : down_modifier));
3007 XSETINT (result->x, LOWORD (msg->msg.lParam));
3008 XSETINT (result->y, HIWORD (msg->msg.lParam));
3009 XSETFRAME (result->frame_or_window, f);
3010 result->arg = Qnil;
3011 return Qnil;
3014 static Lisp_Object
3015 construct_mouse_wheel (result, msg, f)
3016 struct input_event *result;
3017 W32Msg *msg;
3018 struct frame *f;
3020 POINT p;
3021 int delta;
3023 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3024 : WHEEL_EVENT;
3025 result->code = 0;
3026 result->timestamp = msg->msg.time;
3028 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3029 forward, away from the user (up); a negative value indicates that
3030 the wheel was rotated backward, toward the user (down). */
3031 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3033 /* The up and down modifiers indicate if the wheel was rotated up or
3034 down based on WHEEL_DELTA value. */
3035 result->modifiers = (msg->dwModifiers
3036 | ((delta < 0 ) ? down_modifier : up_modifier));
3038 /* With multiple monitors, we can legitimately get negative
3039 coordinates, so cast to short to interpret them correctly. */
3040 p.x = (short) LOWORD (msg->msg.lParam);
3041 p.y = (short) HIWORD (msg->msg.lParam);
3042 ScreenToClient (msg->msg.hwnd, &p);
3043 XSETINT (result->x, p.x);
3044 XSETINT (result->y, p.y);
3045 XSETFRAME (result->frame_or_window, f);
3046 result->arg = Qnil;
3047 return Qnil;
3050 static Lisp_Object
3051 construct_drag_n_drop (result, msg, f)
3052 struct input_event *result;
3053 W32Msg *msg;
3054 struct frame *f;
3056 Lisp_Object files;
3057 Lisp_Object frame;
3058 HDROP hdrop;
3059 POINT p;
3060 WORD num_files;
3061 char *name;
3062 int i, len;
3064 result->kind = DRAG_N_DROP_EVENT;
3065 result->code = 0;
3066 result->timestamp = msg->msg.time;
3067 result->modifiers = msg->dwModifiers;
3069 hdrop = (HDROP) msg->msg.wParam;
3070 DragQueryPoint (hdrop, &p);
3072 #if 0
3073 p.x = LOWORD (msg->msg.lParam);
3074 p.y = HIWORD (msg->msg.lParam);
3075 ScreenToClient (msg->msg.hwnd, &p);
3076 #endif
3078 XSETINT (result->x, p.x);
3079 XSETINT (result->y, p.y);
3081 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3082 files = Qnil;
3084 for (i = 0; i < num_files; i++)
3086 len = DragQueryFile (hdrop, i, NULL, 0);
3087 if (len <= 0)
3088 continue;
3089 name = alloca (len + 1);
3090 DragQueryFile (hdrop, i, name, len + 1);
3091 files = Fcons (DECODE_FILE (build_string (name)), files);
3094 DragFinish (hdrop);
3096 XSETFRAME (frame, f);
3097 result->frame_or_window = frame;
3098 result->arg = files;
3099 return Qnil;
3103 /* Function to report a mouse movement to the mainstream Emacs code.
3104 The input handler calls this.
3106 We have received a mouse movement event, which is given in *event.
3107 If the mouse is over a different glyph than it was last time, tell
3108 the mainstream emacs code by setting mouse_moved. If not, ask for
3109 another motion event, so we can check again the next time it moves. */
3111 static MSG last_mouse_motion_event;
3112 static Lisp_Object last_mouse_motion_frame;
3114 static int
3115 note_mouse_movement (frame, msg)
3116 FRAME_PTR frame;
3117 MSG *msg;
3119 int mouse_x = LOWORD (msg->lParam);
3120 int mouse_y = HIWORD (msg->lParam);
3122 last_mouse_movement_time = msg->time;
3123 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
3124 XSETFRAME (last_mouse_motion_frame, frame);
3126 if (!FRAME_X_OUTPUT (frame))
3127 return 0;
3129 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3131 frame->mouse_moved = 1;
3132 last_mouse_scroll_bar = Qnil;
3133 note_mouse_highlight (frame, -1, -1);
3134 last_mouse_glyph_frame = 0;
3135 return 1;
3138 /* Has the mouse moved off the glyph it was on at the last sighting? */
3139 if (frame != last_mouse_glyph_frame
3140 || mouse_x < last_mouse_glyph.left
3141 || mouse_x >= last_mouse_glyph.right
3142 || mouse_y < last_mouse_glyph.top
3143 || mouse_y >= last_mouse_glyph.bottom)
3145 frame->mouse_moved = 1;
3146 last_mouse_scroll_bar = Qnil;
3147 note_mouse_highlight (frame, mouse_x, mouse_y);
3148 /* Remember the mouse position here, as w32_mouse_position only
3149 gets called when mouse tracking is enabled but we also need
3150 to keep track of the mouse for help_echo and highlighting at
3151 other times. */
3152 remember_mouse_glyph (frame, mouse_x, mouse_y, &last_mouse_glyph);
3153 last_mouse_glyph_frame = frame;
3154 return 1;
3157 return 0;
3161 /************************************************************************
3162 Mouse Face
3163 ************************************************************************/
3165 static struct scroll_bar *x_window_to_scroll_bar ();
3166 static void x_scroll_bar_report_motion ();
3167 static void x_check_fullscreen P_ ((struct frame *));
3169 static void
3170 redo_mouse_highlight ()
3172 if (!NILP (last_mouse_motion_frame)
3173 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3174 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3175 LOWORD (last_mouse_motion_event.lParam),
3176 HIWORD (last_mouse_motion_event.lParam));
3179 static void
3180 w32_define_cursor (window, cursor)
3181 Window window;
3182 Cursor cursor;
3184 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3186 /* Return the current position of the mouse.
3187 *fp should be a frame which indicates which display to ask about.
3189 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3190 and *part to the frame, window, and scroll bar part that the mouse
3191 is over. Set *x and *y to the portion and whole of the mouse's
3192 position on the scroll bar.
3194 If the mouse movement started elsewhere, set *fp to the frame the
3195 mouse is on, *bar_window to nil, and *x and *y to the character cell
3196 the mouse is over.
3198 Set *time to the server time-stamp for the time at which the mouse
3199 was at this position.
3201 Don't store anything if we don't have a valid set of values to report.
3203 This clears the mouse_moved flag, so we can wait for the next mouse
3204 movement. */
3206 static void
3207 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
3208 FRAME_PTR *fp;
3209 int insist;
3210 Lisp_Object *bar_window;
3211 enum scroll_bar_part *part;
3212 Lisp_Object *x, *y;
3213 unsigned long *time;
3215 FRAME_PTR f1;
3217 BLOCK_INPUT;
3219 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3220 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3221 else
3223 POINT pt;
3225 Lisp_Object frame, tail;
3227 /* Clear the mouse-moved flag for every frame on this display. */
3228 FOR_EACH_FRAME (tail, frame)
3229 XFRAME (frame)->mouse_moved = 0;
3231 last_mouse_scroll_bar = Qnil;
3233 GetCursorPos (&pt);
3235 /* Now we have a position on the root; find the innermost window
3236 containing the pointer. */
3238 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3239 && FRAME_LIVE_P (last_mouse_frame))
3241 /* If mouse was grabbed on a frame, give coords for that frame
3242 even if the mouse is now outside it. */
3243 f1 = last_mouse_frame;
3245 else
3247 /* Is window under mouse one of our frames? */
3248 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
3249 WindowFromPoint (pt));
3252 /* If not, is it one of our scroll bars? */
3253 if (! f1)
3255 struct scroll_bar *bar
3256 = x_window_to_scroll_bar (WindowFromPoint (pt));
3258 if (bar)
3260 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3264 if (f1 == 0 && insist > 0)
3265 f1 = SELECTED_FRAME ();
3267 if (f1)
3269 /* Ok, we found a frame. Store all the values.
3270 last_mouse_glyph is a rectangle used to reduce the
3271 generation of mouse events. To not miss any motion
3272 events, we must divide the frame into rectangles of the
3273 size of the smallest character that could be displayed
3274 on it, i.e. into the same rectangles that matrices on
3275 the frame are divided into. */
3277 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3278 remember_mouse_glyph (f1, pt.x, pt.y, &last_mouse_glyph);
3279 last_mouse_glyph_frame = f1;
3281 *bar_window = Qnil;
3282 *part = 0;
3283 *fp = f1;
3284 XSETINT (*x, pt.x);
3285 XSETINT (*y, pt.y);
3286 *time = last_mouse_movement_time;
3291 UNBLOCK_INPUT;
3295 /***********************************************************************
3296 Tool-bars
3297 ***********************************************************************/
3299 /* Handle mouse button event on the tool-bar of frame F, at
3300 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3301 or ButtonRelase. */
3303 static void
3304 w32_handle_tool_bar_click (f, button_event)
3305 struct frame *f;
3306 struct input_event *button_event;
3308 int x = XFASTINT (button_event->x);
3309 int y = XFASTINT (button_event->y);
3311 if (button_event->modifiers & down_modifier)
3312 handle_tool_bar_click (f, x, y, 1, 0);
3313 else
3314 handle_tool_bar_click (f, x, y, 0,
3315 button_event->modifiers & ~up_modifier);
3320 /***********************************************************************
3321 Scroll bars
3322 ***********************************************************************/
3324 /* Scroll bar support. */
3326 /* Given a window ID, find the struct scroll_bar which manages it.
3327 This can be called in GC, so we have to make sure to strip off mark
3328 bits. */
3330 static struct scroll_bar *
3331 x_window_to_scroll_bar (window_id)
3332 Window window_id;
3334 Lisp_Object tail;
3336 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3338 Lisp_Object frame, bar, condemned;
3340 frame = XCAR (tail);
3341 /* All elements of Vframe_list should be frames. */
3342 if (! FRAMEP (frame))
3343 abort ();
3345 /* Scan this frame's scroll bar list for a scroll bar with the
3346 right window ID. */
3347 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3348 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3349 /* This trick allows us to search both the ordinary and
3350 condemned scroll bar lists with one loop. */
3351 ! NILP (bar) || (bar = condemned,
3352 condemned = Qnil,
3353 ! NILP (bar));
3354 bar = XSCROLL_BAR (bar)->next)
3355 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3356 return XSCROLL_BAR (bar);
3359 return 0;
3364 /* Set the thumb size and position of scroll bar BAR. We are currently
3365 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3367 static void
3368 w32_set_scroll_bar_thumb (bar, portion, position, whole)
3369 struct scroll_bar *bar;
3370 int portion, position, whole;
3372 Window w = SCROLL_BAR_W32_WINDOW (bar);
3373 /* We use the whole scroll-bar height in the calculations below, to
3374 avoid strange effects like scrolling backwards when just clicking
3375 on the handle (without moving it). */
3376 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height))
3377 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3378 int sb_page, sb_pos;
3379 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3380 SCROLLINFO si;
3382 /* We used to change the nPage setting while dragging the handle,
3383 but that had very strange effects (such as scrolling backwards
3384 while dragging downwards).
3386 Now, we don't change the nPage setting while dragging unless we
3387 get near to the end of the buffer, in which case we often have to
3388 resize the handle to "go all the way". */
3390 if (draggingp)
3392 int near_bottom_p;
3393 BLOCK_INPUT;
3394 si.cbSize = sizeof (si);
3395 si.fMask = SIF_POS | SIF_PAGE;
3396 GetScrollInfo(w, SB_CTL, &si);
3397 near_bottom_p = si.nPos + si.nPage >= range;
3398 UNBLOCK_INPUT;
3399 if (!near_bottom_p)
3400 return;
3403 if (whole)
3405 /* Position scroll bar at rock bottom if the bottom of the
3406 buffer is visible. This avoids shinking the thumb away
3407 to nothing if it is held at the bottom of the buffer. */
3408 if (position + portion >= whole && !draggingp)
3410 sb_page = range * (whole - position) / whole;
3411 sb_pos = range;
3413 else
3415 sb_pos = position * range / whole;
3416 sb_page = (min (portion, (whole - position)) * range) / whole;
3419 else
3421 sb_page = range;
3422 sb_pos = 0;
3425 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3427 BLOCK_INPUT;
3429 si.cbSize = sizeof (si);
3430 si.fMask = SIF_PAGE | SIF_POS;
3431 si.nPage = sb_page;
3432 si.nPos = sb_pos;
3434 SetScrollInfo (w, SB_CTL, &si, TRUE);
3436 UNBLOCK_INPUT;
3440 /************************************************************************
3441 Scroll bars, general
3442 ************************************************************************/
3444 static HWND
3445 my_create_scrollbar (f, bar)
3446 struct frame * f;
3447 struct scroll_bar * bar;
3449 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3450 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3451 (LPARAM) bar);
3454 /*#define ATTACH_THREADS*/
3456 static BOOL
3457 my_show_window (FRAME_PTR f, HWND hwnd, int how)
3459 #ifndef ATTACH_THREADS
3460 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3461 (WPARAM) hwnd, (LPARAM) how);
3462 #else
3463 return ShowWindow (hwnd, how);
3464 #endif
3467 static void
3468 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3469 int x, int y, int cx, int cy, UINT flags)
3471 #ifndef ATTACH_THREADS
3472 WINDOWPOS pos;
3473 pos.hwndInsertAfter = hwndAfter;
3474 pos.x = x;
3475 pos.y = y;
3476 pos.cx = cx;
3477 pos.cy = cy;
3478 pos.flags = flags;
3479 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3480 #else
3481 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3482 #endif
3485 static void
3486 my_set_focus (f, hwnd)
3487 struct frame * f;
3488 HWND hwnd;
3490 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3491 (WPARAM) hwnd, 0);
3494 static void
3495 my_set_foreground_window (hwnd)
3496 HWND hwnd;
3498 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3502 static void
3503 my_destroy_window (f, hwnd)
3504 struct frame * f;
3505 HWND hwnd;
3507 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3508 (WPARAM) hwnd, 0);
3511 /* Create a scroll bar and return the scroll bar vector for it. W is
3512 the Emacs window on which to create the scroll bar. TOP, LEFT,
3513 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
3514 scroll bar. */
3516 static struct scroll_bar *
3517 x_scroll_bar_create (w, top, left, width, height)
3518 struct window *w;
3519 int top, left, width, height;
3521 struct frame *f = XFRAME (WINDOW_FRAME (w));
3522 HWND hwnd;
3523 SCROLLINFO si;
3524 struct scroll_bar *bar
3525 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3527 BLOCK_INPUT;
3529 XSETWINDOW (bar->window, w);
3530 XSETINT (bar->top, top);
3531 XSETINT (bar->left, left);
3532 XSETINT (bar->width, width);
3533 XSETINT (bar->height, height);
3534 XSETINT (bar->start, 0);
3535 XSETINT (bar->end, 0);
3536 bar->dragging = Qnil;
3537 bar->fringe_extended_p = Qnil;
3539 /* Requires geometry to be set before call to create the real window */
3541 hwnd = my_create_scrollbar (f, bar);
3543 si.cbSize = sizeof (si);
3544 si.fMask = SIF_ALL;
3545 si.nMin = 0;
3546 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3547 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3548 si.nPage = si.nMax;
3549 si.nPos = 0;
3551 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3553 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3555 /* Add bar to its frame's list of scroll bars. */
3556 bar->next = FRAME_SCROLL_BARS (f);
3557 bar->prev = Qnil;
3558 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3559 if (! NILP (bar->next))
3560 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3562 UNBLOCK_INPUT;
3564 return bar;
3568 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3569 nil. */
3571 static void
3572 x_scroll_bar_remove (bar)
3573 struct scroll_bar *bar;
3575 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3577 BLOCK_INPUT;
3579 /* Destroy the window. */
3580 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3582 /* Disassociate this scroll bar from its window. */
3583 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3585 UNBLOCK_INPUT;
3588 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3589 that we are displaying PORTION characters out of a total of WHOLE
3590 characters, starting at POSITION. If WINDOW has no scroll bar,
3591 create one. */
3592 static void
3593 w32_set_vertical_scroll_bar (w, portion, whole, position)
3594 struct window *w;
3595 int portion, whole, position;
3597 struct frame *f = XFRAME (w->frame);
3598 struct scroll_bar *bar;
3599 int top, height, left, sb_left, width, sb_width;
3600 int window_y, window_height;
3601 int fringe_extended_p;
3603 /* Get window dimensions. */
3604 window_box (w, -1, 0, &window_y, 0, &window_height);
3605 top = window_y;
3606 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3607 height = window_height;
3609 /* Compute the left edge of the scroll bar area. */
3610 left = WINDOW_SCROLL_BAR_AREA_X (w);
3612 /* Compute the width of the scroll bar which might be less than
3613 the width of the area reserved for the scroll bar. */
3614 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3615 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3616 else
3617 sb_width = width;
3619 /* Compute the left edge of the scroll bar. */
3620 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3621 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3622 else
3623 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3625 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3626 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
3627 && WINDOW_LEFT_FRINGE_WIDTH (w)
3628 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3629 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
3630 else
3631 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
3632 && WINDOW_RIGHT_FRINGE_WIDTH (w)
3633 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3634 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
3636 /* Does the scroll bar exist yet? */
3637 if (NILP (w->vertical_scroll_bar))
3639 HDC hdc;
3640 BLOCK_INPUT;
3641 if (width > 0 && height > 0)
3643 hdc = get_frame_dc (f);
3644 if (fringe_extended_p)
3645 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3646 else
3647 w32_clear_area (f, hdc, left, top, width, height);
3648 release_frame_dc (f, hdc);
3650 UNBLOCK_INPUT;
3652 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3654 else
3656 /* It may just need to be moved and resized. */
3657 HWND hwnd;
3659 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3660 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3662 /* If already correctly positioned, do nothing. */
3663 if ( XINT (bar->left) == sb_left
3664 && XINT (bar->top) == top
3665 && XINT (bar->width) == sb_width
3666 && XINT (bar->height) == height
3667 && !NILP (bar->fringe_extended_p) == fringe_extended_p )
3669 /* Redraw after clear_frame. */
3670 if (!my_show_window (f, hwnd, SW_NORMAL))
3671 InvalidateRect (hwnd, NULL, FALSE);
3673 else
3675 HDC hdc;
3676 SCROLLINFO si;
3678 BLOCK_INPUT;
3679 if (width && height)
3681 hdc = get_frame_dc (f);
3682 /* Since Windows scroll bars are smaller than the space reserved
3683 for them on the frame, we have to clear "under" them. */
3684 if (fringe_extended_p)
3685 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3686 else
3687 w32_clear_area (f, hdc, left, top, width, height);
3688 release_frame_dc (f, hdc);
3690 /* Make sure scroll bar is "visible" before moving, to ensure the
3691 area of the parent window now exposed will be refreshed. */
3692 my_show_window (f, hwnd, SW_HIDE);
3693 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3694 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3695 max (height, 1), TRUE);
3697 si.cbSize = sizeof (si);
3698 si.fMask = SIF_RANGE;
3699 si.nMin = 0;
3700 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3701 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3703 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3705 my_show_window (f, hwnd, SW_NORMAL);
3706 /* InvalidateRect (w, NULL, FALSE); */
3708 /* Remember new settings. */
3709 XSETINT (bar->left, sb_left);
3710 XSETINT (bar->top, top);
3711 XSETINT (bar->width, sb_width);
3712 XSETINT (bar->height, height);
3714 UNBLOCK_INPUT;
3717 bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
3719 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3721 XSETVECTOR (w->vertical_scroll_bar, bar);
3725 /* The following three hooks are used when we're doing a thorough
3726 redisplay of the frame. We don't explicitly know which scroll bars
3727 are going to be deleted, because keeping track of when windows go
3728 away is a real pain - "Can you say set-window-configuration, boys
3729 and girls?" Instead, we just assert at the beginning of redisplay
3730 that *all* scroll bars are to be removed, and then save a scroll bar
3731 from the fiery pit when we actually redisplay its window. */
3733 /* Arrange for all scroll bars on FRAME to be removed at the next call
3734 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3735 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3737 static void
3738 w32_condemn_scroll_bars (frame)
3739 FRAME_PTR frame;
3741 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3742 while (! NILP (FRAME_SCROLL_BARS (frame)))
3744 Lisp_Object bar;
3745 bar = FRAME_SCROLL_BARS (frame);
3746 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
3747 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3748 XSCROLL_BAR (bar)->prev = Qnil;
3749 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3750 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3751 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
3756 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3757 Note that WINDOW isn't necessarily condemned at all. */
3759 static void
3760 w32_redeem_scroll_bar (window)
3761 struct window *window;
3763 struct scroll_bar *bar;
3764 struct frame *f;
3766 /* We can't redeem this window's scroll bar if it doesn't have one. */
3767 if (NILP (window->vertical_scroll_bar))
3768 abort ();
3770 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3772 /* Unlink it from the condemned list. */
3773 f = XFRAME (WINDOW_FRAME (window));
3774 if (NILP (bar->prev))
3776 /* If the prev pointer is nil, it must be the first in one of
3777 the lists. */
3778 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3779 /* It's not condemned. Everything's fine. */
3780 return;
3781 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3782 window->vertical_scroll_bar))
3783 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
3784 else
3785 /* If its prev pointer is nil, it must be at the front of
3786 one or the other! */
3787 abort ();
3789 else
3790 XSCROLL_BAR (bar->prev)->next = bar->next;
3792 if (! NILP (bar->next))
3793 XSCROLL_BAR (bar->next)->prev = bar->prev;
3795 bar->next = FRAME_SCROLL_BARS (f);
3796 bar->prev = Qnil;
3797 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3798 if (! NILP (bar->next))
3799 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3802 /* Remove all scroll bars on FRAME that haven't been saved since the
3803 last call to `*condemn_scroll_bars_hook'. */
3805 static void
3806 w32_judge_scroll_bars (f)
3807 FRAME_PTR f;
3809 Lisp_Object bar, next;
3811 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3813 /* Clear out the condemned list now so we won't try to process any
3814 more events on the hapless scroll bars. */
3815 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3817 for (; ! NILP (bar); bar = next)
3819 struct scroll_bar *b = XSCROLL_BAR (bar);
3821 x_scroll_bar_remove (b);
3823 next = b->next;
3824 b->next = b->prev = Qnil;
3827 /* Now there should be no references to the condemned scroll bars,
3828 and they should get garbage-collected. */
3831 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3832 is set to something other than NO_EVENT, it is enqueued.
3834 This may be called from a signal handler, so we have to ignore GC
3835 mark bits. */
3837 static int
3838 w32_scroll_bar_handle_click (bar, msg, emacs_event)
3839 struct scroll_bar *bar;
3840 W32Msg *msg;
3841 struct input_event *emacs_event;
3843 if (! WINDOWP (bar->window))
3844 abort ();
3846 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
3847 emacs_event->code = 0;
3848 /* not really meaningful to distinguish up/down */
3849 emacs_event->modifiers = msg->dwModifiers;
3850 emacs_event->frame_or_window = bar->window;
3851 emacs_event->arg = Qnil;
3852 emacs_event->timestamp = msg->msg.time;
3855 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3856 int y;
3857 int dragging = !NILP (bar->dragging);
3858 SCROLLINFO si;
3860 si.cbSize = sizeof (si);
3861 si.fMask = SIF_POS;
3863 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
3864 y = si.nPos;
3866 bar->dragging = Qnil;
3869 last_mouse_scroll_bar_pos = msg->msg.wParam;
3871 switch (LOWORD (msg->msg.wParam))
3873 case SB_LINEDOWN:
3874 emacs_event->part = scroll_bar_down_arrow;
3875 break;
3876 case SB_LINEUP:
3877 emacs_event->part = scroll_bar_up_arrow;
3878 break;
3879 case SB_PAGEUP:
3880 emacs_event->part = scroll_bar_above_handle;
3881 break;
3882 case SB_PAGEDOWN:
3883 emacs_event->part = scroll_bar_below_handle;
3884 break;
3885 case SB_TOP:
3886 emacs_event->part = scroll_bar_handle;
3887 y = 0;
3888 break;
3889 case SB_BOTTOM:
3890 emacs_event->part = scroll_bar_handle;
3891 y = top_range;
3892 break;
3893 case SB_THUMBTRACK:
3894 case SB_THUMBPOSITION:
3895 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3896 y = HIWORD (msg->msg.wParam);
3897 bar->dragging = Qt;
3898 emacs_event->part = scroll_bar_handle;
3900 /* "Silently" update current position. */
3902 SCROLLINFO si;
3904 si.cbSize = sizeof (si);
3905 si.fMask = SIF_POS;
3906 si.nPos = y;
3907 /* Remember apparent position (we actually lag behind the real
3908 position, so don't set that directly. */
3909 last_scroll_bar_drag_pos = y;
3911 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
3913 break;
3914 case SB_ENDSCROLL:
3915 /* If this is the end of a drag sequence, then reset the scroll
3916 handle size to normal and do a final redraw. Otherwise do
3917 nothing. */
3918 if (dragging)
3920 SCROLLINFO si;
3921 int start = XINT (bar->start);
3922 int end = XINT (bar->end);
3924 si.cbSize = sizeof (si);
3925 si.fMask = SIF_PAGE | SIF_POS;
3926 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3927 si.nPos = last_scroll_bar_drag_pos;
3928 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
3930 /* fall through */
3931 default:
3932 emacs_event->kind = NO_EVENT;
3933 return FALSE;
3936 XSETINT (emacs_event->x, y);
3937 XSETINT (emacs_event->y, top_range);
3939 return TRUE;
3943 /* Return information to the user about the current position of the mouse
3944 on the scroll bar. */
3946 static void
3947 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
3948 FRAME_PTR *fp;
3949 Lisp_Object *bar_window;
3950 enum scroll_bar_part *part;
3951 Lisp_Object *x, *y;
3952 unsigned long *time;
3954 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
3955 Window w = SCROLL_BAR_W32_WINDOW (bar);
3956 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3957 int pos;
3958 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3959 SCROLLINFO si;
3961 BLOCK_INPUT;
3963 *fp = f;
3964 *bar_window = bar->window;
3966 si.cbSize = sizeof (si);
3967 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
3969 GetScrollInfo (w, SB_CTL, &si);
3970 pos = si.nPos;
3971 top_range = si.nMax - si.nPage + 1;
3973 switch (LOWORD (last_mouse_scroll_bar_pos))
3975 case SB_THUMBPOSITION:
3976 case SB_THUMBTRACK:
3977 *part = scroll_bar_handle;
3978 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3979 pos = HIWORD (last_mouse_scroll_bar_pos);
3980 break;
3981 case SB_LINEDOWN:
3982 *part = scroll_bar_handle;
3983 pos++;
3984 break;
3985 default:
3986 *part = scroll_bar_handle;
3987 break;
3990 XSETINT (*x, pos);
3991 XSETINT (*y, top_range);
3993 f->mouse_moved = 0;
3994 last_mouse_scroll_bar = Qnil;
3996 *time = last_mouse_movement_time;
3998 UNBLOCK_INPUT;
4002 /* The screen has been cleared so we may have changed foreground or
4003 background colors, and the scroll bars may need to be redrawn.
4004 Clear out the scroll bars, and ask for expose events, so we can
4005 redraw them. */
4007 void
4008 x_scroll_bar_clear (f)
4009 FRAME_PTR f;
4011 Lisp_Object bar;
4013 /* We can have scroll bars even if this is 0,
4014 if we just turned off scroll bar mode.
4015 But in that case we should not clear them. */
4016 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4017 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4018 bar = XSCROLL_BAR (bar)->next)
4020 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4021 HDC hdc = GetDC (window);
4022 RECT rect;
4024 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4025 arranges to refresh the scroll bar if hidden. */
4026 my_show_window (f, window, SW_HIDE);
4028 GetClientRect (window, &rect);
4029 select_palette (f, hdc);
4030 w32_clear_rect (f, hdc, &rect);
4031 deselect_palette (f, hdc);
4033 ReleaseDC (window, hdc);
4038 /* The main W32 event-reading loop - w32_read_socket. */
4040 /* Record the last 100 characters stored
4041 to help debug the loss-of-chars-during-GC problem. */
4043 static int temp_index;
4044 static short temp_buffer[100];
4046 /* Temporarily store lead byte of DBCS input sequences. */
4047 static char dbcs_lead = 0;
4049 /* Read events coming from the W32 shell.
4050 This routine is called by the SIGIO handler.
4051 We return as soon as there are no more events to be read.
4053 We return the number of characters stored into the buffer,
4054 thus pretending to be `read'.
4056 EXPECTED is nonzero if the caller knows input is available.
4058 Some of these messages are reposted back to the message queue since the
4059 system calls the windows proc directly in a context where we cannot return
4060 the data nor can we guarantee the state we are in. So if we dispatch them
4061 we will get into an infinite loop. To prevent this from ever happening we
4062 will set a variable to indicate we are in the read_socket call and indicate
4063 which message we are processing since the windows proc gets called
4064 recursively with different messages by the system.
4068 w32_read_socket (sd, expected, hold_quit)
4069 register int sd;
4070 int expected;
4071 struct input_event *hold_quit;
4073 int count = 0;
4074 int check_visibility = 0;
4075 W32Msg msg;
4076 struct frame *f;
4077 struct w32_display_info *dpyinfo = &one_w32_display_info;
4079 if (interrupt_input_blocked)
4081 interrupt_input_pending = 1;
4082 return -1;
4085 interrupt_input_pending = 0;
4086 BLOCK_INPUT;
4088 /* So people can tell when we have read the available input. */
4089 input_signal_count++;
4091 /* TODO: ghostscript integration. */
4092 while (get_next_msg (&msg, FALSE))
4094 struct input_event inev;
4095 int do_help = 0;
4097 EVENT_INIT (inev);
4098 inev.kind = NO_EVENT;
4099 inev.arg = Qnil;
4101 switch (msg.msg.message)
4103 case WM_PAINT:
4104 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4106 if (f)
4108 if (msg.rect.right == msg.rect.left ||
4109 msg.rect.bottom == msg.rect.top)
4111 /* We may get paint messages even though the client
4112 area is clipped - these are not expose events. */
4113 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4114 SDATA (f->name)));
4116 else if (f->async_visible != 1)
4118 /* Definitely not obscured, so mark as visible. */
4119 f->async_visible = 1;
4120 f->async_iconified = 0;
4121 SET_FRAME_GARBAGED (f);
4122 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4123 SDATA (f->name)));
4125 /* WM_PAINT serves as MapNotify as well, so report
4126 visibility changes properly. */
4127 if (f->iconified)
4129 inev.kind = DEICONIFY_EVENT;
4130 XSETFRAME (inev.frame_or_window, f);
4132 else if (! NILP (Vframe_list)
4133 && ! NILP (XCDR (Vframe_list)))
4134 /* Force a redisplay sooner or later to update the
4135 frame titles in case this is the second frame. */
4136 record_asynch_buffer_change ();
4138 else
4140 HDC hdc = get_frame_dc (f);
4142 /* Erase background again for safety. */
4143 w32_clear_rect (f, hdc, &msg.rect);
4144 release_frame_dc (f, hdc);
4145 expose_frame (f,
4146 msg.rect.left,
4147 msg.rect.top,
4148 msg.rect.right - msg.rect.left,
4149 msg.rect.bottom - msg.rect.top);
4152 break;
4154 case WM_INPUTLANGCHANGE:
4155 /* Generate a language change event. */
4156 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4158 if (f)
4160 inev.kind = LANGUAGE_CHANGE_EVENT;
4161 XSETFRAME (inev.frame_or_window, f);
4162 inev.code = msg.msg.wParam;
4163 inev.modifiers = msg.msg.lParam & 0xffff;
4165 break;
4167 case WM_KEYDOWN:
4168 case WM_SYSKEYDOWN:
4169 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4171 if (f && !f->iconified)
4173 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4174 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4176 clear_mouse_face (dpyinfo);
4177 dpyinfo->mouse_face_hidden = 1;
4180 if (temp_index == sizeof temp_buffer / sizeof (short))
4181 temp_index = 0;
4182 temp_buffer[temp_index++] = msg.msg.wParam;
4183 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4184 inev.code = msg.msg.wParam;
4185 inev.modifiers = msg.dwModifiers;
4186 XSETFRAME (inev.frame_or_window, f);
4187 inev.timestamp = msg.msg.time;
4189 break;
4191 case WM_UNICHAR:
4192 case WM_SYSCHAR:
4193 case WM_CHAR:
4194 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4196 if (f && !f->iconified)
4198 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4199 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4201 clear_mouse_face (dpyinfo);
4202 dpyinfo->mouse_face_hidden = 1;
4205 if (temp_index == sizeof temp_buffer / sizeof (short))
4206 temp_index = 0;
4207 temp_buffer[temp_index++] = msg.msg.wParam;
4209 inev.modifiers = msg.dwModifiers;
4210 XSETFRAME (inev.frame_or_window, f);
4211 inev.timestamp = msg.msg.time;
4213 if (msg.msg.message == WM_UNICHAR)
4215 inev.code = msg.msg.wParam;
4217 else if (msg.msg.wParam < 256)
4219 wchar_t code;
4220 char dbcs[2];
4221 dbcs[0] = 0;
4222 dbcs[1] = (char) msg.msg.wParam;
4224 if (dbcs_lead)
4226 dbcs[0] = dbcs_lead;
4227 dbcs_lead = 0;
4228 if (!MultiByteToWideChar (CP_ACP, 0, dbcs, 2, &code, 1))
4230 /* Garbage */
4231 DebPrint (("Invalid DBCS sequence: %d %d\n",
4232 dbcs[0], dbcs[1]));
4233 inev.kind = NO_EVENT;
4234 break;
4237 else if (IsDBCSLeadByteEx (CP_ACP, (BYTE) msg.msg.wParam))
4239 dbcs_lead = (char) msg.msg.wParam;
4240 inev.kind = NO_EVENT;
4241 break;
4243 else
4245 if (!MultiByteToWideChar (CP_ACP, 0, &dbcs[1], 1,
4246 &code, 1))
4248 /* What to do with garbage? */
4249 DebPrint (("Invalid character: %d\n", dbcs[1]));
4250 inev.kind = NO_EVENT;
4251 break;
4254 inev.code = code;
4256 else
4258 /* Windows shouldn't generate WM_CHAR events above 0xFF
4259 in non-Unicode message handlers. */
4260 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4261 inev.kind = NO_EVENT;
4262 break;
4264 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4265 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4267 break;
4269 case WM_APPCOMMAND:
4270 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4272 if (f && !f->iconified)
4274 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4275 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4277 clear_mouse_face (dpyinfo);
4278 dpyinfo->mouse_face_hidden = 1;
4281 if (temp_index == sizeof temp_buffer / sizeof (short))
4282 temp_index = 0;
4283 temp_buffer[temp_index++] = msg.msg.wParam;
4284 inev.kind = MULTIMEDIA_KEY_EVENT;
4285 inev.code = GET_APPCOMMAND_LPARAM(msg.msg.lParam);
4286 inev.modifiers = msg.dwModifiers;
4287 XSETFRAME (inev.frame_or_window, f);
4288 inev.timestamp = msg.msg.time;
4290 break;
4292 case WM_MOUSEMOVE:
4293 /* Ignore non-movement. */
4295 int x = LOWORD (msg.msg.lParam);
4296 int y = HIWORD (msg.msg.lParam);
4297 if (x == last_mousemove_x && y == last_mousemove_y)
4298 break;
4299 last_mousemove_x = x;
4300 last_mousemove_y = y;
4303 previous_help_echo_string = help_echo_string;
4304 help_echo_string = Qnil;
4306 if (dpyinfo->grabbed && last_mouse_frame
4307 && FRAME_LIVE_P (last_mouse_frame))
4308 f = last_mouse_frame;
4309 else
4310 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4312 if (dpyinfo->mouse_face_hidden)
4314 dpyinfo->mouse_face_hidden = 0;
4315 clear_mouse_face (dpyinfo);
4318 if (f)
4320 /* Generate SELECT_WINDOW_EVENTs when needed. */
4321 if (!NILP (Vmouse_autoselect_window))
4323 Lisp_Object window;
4324 int x = LOWORD (msg.msg.lParam);
4325 int y = HIWORD (msg.msg.lParam);
4327 window = window_from_coordinates (f, x, y, 0, 0, 0, 0);
4329 /* Window will be selected only when it is not
4330 selected now and last mouse movement event was
4331 not in it. Minibuffer window will be selected
4332 only when it is active. */
4333 if (WINDOWP(window)
4334 && !EQ (window, last_window)
4335 && !EQ (window, selected_window)
4336 /* For click-to-focus window managers
4337 create event iff we don't leave the
4338 selected frame. */
4339 && (focus_follows_mouse
4340 || (EQ (XWINDOW (window)->frame,
4341 XWINDOW (selected_window)->frame))))
4343 inev.kind = SELECT_WINDOW_EVENT;
4344 inev.frame_or_window = window;
4347 last_window=window;
4349 if (!note_mouse_movement (f, &msg.msg))
4350 help_echo_string = previous_help_echo_string;
4352 else
4354 /* If we move outside the frame, then we're
4355 certainly no longer on any text in the frame. */
4356 clear_mouse_face (dpyinfo);
4359 /* If the contents of the global variable help_echo_string
4360 has changed, generate a HELP_EVENT. */
4361 #if 0 /* The below is an invalid comparison when USE_LISP_UNION_TYPE.
4362 But it was originally changed to this to fix a bug, so I have
4363 not removed it completely in case the bug is still there. */
4364 if (help_echo_string != previous_help_echo_string ||
4365 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4366 #else /* This is what xterm.c does. */
4367 if (!NILP (help_echo_string)
4368 || !NILP (previous_help_echo_string))
4369 do_help = 1;
4370 #endif
4371 break;
4373 case WM_LBUTTONDOWN:
4374 case WM_LBUTTONUP:
4375 case WM_MBUTTONDOWN:
4376 case WM_MBUTTONUP:
4377 case WM_RBUTTONDOWN:
4378 case WM_RBUTTONUP:
4379 case WM_XBUTTONDOWN:
4380 case WM_XBUTTONUP:
4382 /* If we decide we want to generate an event to be seen
4383 by the rest of Emacs, we put it here. */
4384 int tool_bar_p = 0;
4385 int button;
4386 int up;
4388 if (dpyinfo->grabbed && last_mouse_frame
4389 && FRAME_LIVE_P (last_mouse_frame))
4390 f = last_mouse_frame;
4391 else
4392 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4394 if (f)
4396 construct_mouse_click (&inev, &msg, f);
4398 /* Is this in the tool-bar? */
4399 if (WINDOWP (f->tool_bar_window)
4400 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4402 Lisp_Object window;
4403 int x = XFASTINT (inev.x);
4404 int y = XFASTINT (inev.y);
4406 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
4408 if (EQ (window, f->tool_bar_window))
4410 w32_handle_tool_bar_click (f, &inev);
4411 tool_bar_p = 1;
4415 if (tool_bar_p
4416 || (dpyinfo->w32_focus_frame
4417 && f != dpyinfo->w32_focus_frame))
4418 inev.kind = NO_EVENT;
4421 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4422 &button, &up);
4424 if (up)
4426 dpyinfo->grabbed &= ~ (1 << button);
4428 else
4430 dpyinfo->grabbed |= (1 << button);
4431 last_mouse_frame = f;
4432 /* Ignore any mouse motion that happened
4433 before this event; any subsequent mouse-movement
4434 Emacs events should reflect only motion after
4435 the ButtonPress. */
4436 if (f != 0)
4437 f->mouse_moved = 0;
4439 if (!tool_bar_p)
4440 last_tool_bar_item = -1;
4442 break;
4445 case WM_MOUSEWHEEL:
4446 case WM_MOUSEHWHEEL:
4448 if (dpyinfo->grabbed && last_mouse_frame
4449 && FRAME_LIVE_P (last_mouse_frame))
4450 f = last_mouse_frame;
4451 else
4452 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4454 if (f)
4457 if (!dpyinfo->w32_focus_frame
4458 || f == dpyinfo->w32_focus_frame)
4460 /* Emit an Emacs wheel-up/down event. */
4461 construct_mouse_wheel (&inev, &msg, f);
4463 /* Ignore any mouse motion that happened before this
4464 event; any subsequent mouse-movement Emacs events
4465 should reflect only motion after the
4466 ButtonPress. */
4467 f->mouse_moved = 0;
4469 last_mouse_frame = f;
4470 last_tool_bar_item = -1;
4472 break;
4474 case WM_DROPFILES:
4475 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4477 if (f)
4478 construct_drag_n_drop (&inev, &msg, f);
4479 break;
4481 case WM_VSCROLL:
4483 struct scroll_bar *bar =
4484 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4486 if (bar)
4487 w32_scroll_bar_handle_click (bar, &msg, &inev);
4488 break;
4491 case WM_WINDOWPOSCHANGED:
4492 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4493 if (f)
4495 if (f->want_fullscreen & FULLSCREEN_WAIT)
4496 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
4498 check_visibility = 1;
4499 break;
4501 case WM_ACTIVATE:
4502 case WM_ACTIVATEAPP:
4503 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4504 if (f)
4505 x_check_fullscreen (f);
4506 check_visibility = 1;
4507 break;
4509 case WM_MOVE:
4510 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4512 if (f && !f->async_iconified)
4514 int x, y;
4516 x_real_positions (f, &x, &y);
4517 f->left_pos = x;
4518 f->top_pos = y;
4521 check_visibility = 1;
4522 break;
4524 case WM_SHOWWINDOW:
4525 /* wParam non-zero means Window is about to be shown, 0 means
4526 about to be hidden. */
4527 /* Redo the mouse-highlight after the tooltip has gone. */
4528 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4530 tip_window = NULL;
4531 redo_mouse_highlight ();
4534 /* If window has been obscured or exposed by another window
4535 being maximised or minimised/restored, then recheck
4536 visibility of all frames. Direct changes to our own
4537 windows get handled by WM_SIZE. */
4538 #if 0
4539 if (msg.msg.lParam != 0)
4540 check_visibility = 1;
4541 else
4543 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4544 f->async_visible = msg.msg.wParam;
4546 #endif
4548 check_visibility = 1;
4549 break;
4551 case WM_SIZE:
4552 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4554 /* Inform lisp of whether frame has been iconified etc. */
4555 if (f)
4557 switch (msg.msg.wParam)
4559 case SIZE_MINIMIZED:
4560 f->async_visible = 0;
4561 f->async_iconified = 1;
4563 inev.kind = ICONIFY_EVENT;
4564 XSETFRAME (inev.frame_or_window, f);
4565 break;
4567 case SIZE_MAXIMIZED:
4568 case SIZE_RESTORED:
4569 f->async_visible = 1;
4570 f->async_iconified = 0;
4572 /* wait_reading_process_output will notice this and update
4573 the frame's display structures. */
4574 SET_FRAME_GARBAGED (f);
4576 if (f->iconified)
4578 int x, y;
4580 /* Reset top and left positions of the Window
4581 here since Windows sends a WM_MOVE message
4582 BEFORE telling us the Window is minimized
4583 when the Window is iconified, with 3000,3000
4584 as the co-ords. */
4585 x_real_positions (f, &x, &y);
4586 f->left_pos = x;
4587 f->top_pos = y;
4589 inev.kind = DEICONIFY_EVENT;
4590 XSETFRAME (inev.frame_or_window, f);
4592 else if (! NILP (Vframe_list)
4593 && ! NILP (XCDR (Vframe_list)))
4594 /* Force a redisplay sooner or later
4595 to update the frame titles
4596 in case this is the second frame. */
4597 record_asynch_buffer_change ();
4598 break;
4602 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
4604 RECT rect;
4605 int rows;
4606 int columns;
4607 int width;
4608 int height;
4610 GetClientRect (msg.msg.hwnd, &rect);
4612 height = rect.bottom - rect.top;
4613 width = rect.right - rect.left;
4615 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4616 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4618 /* TODO: Clip size to the screen dimensions. */
4620 /* Even if the number of character rows and columns has
4621 not changed, the font size may have changed, so we need
4622 to check the pixel dimensions as well. */
4624 if (columns != FRAME_COLS (f)
4625 || rows != FRAME_LINES (f)
4626 || width != FRAME_PIXEL_WIDTH (f)
4627 || height != FRAME_PIXEL_HEIGHT (f))
4629 change_frame_size (f, rows, columns, 0, 1, 0);
4630 SET_FRAME_GARBAGED (f);
4631 cancel_mouse_face (f);
4632 FRAME_PIXEL_WIDTH (f) = width;
4633 FRAME_PIXEL_HEIGHT (f) = height;
4634 f->win_gravity = NorthWestGravity;
4638 check_visibility = 1;
4639 break;
4641 case WM_MOUSELEAVE:
4642 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4643 if (f)
4645 if (f == dpyinfo->mouse_face_mouse_frame)
4647 /* If we move outside the frame, then we're
4648 certainly no longer on any text in the frame. */
4649 clear_mouse_face (dpyinfo);
4650 dpyinfo->mouse_face_mouse_frame = 0;
4653 /* Generate a nil HELP_EVENT to cancel a help-echo.
4654 Do it only if there's something to cancel.
4655 Otherwise, the startup message is cleared when
4656 the mouse leaves the frame. */
4657 if (any_help_event_p)
4658 do_help = -1;
4660 break;
4662 case WM_SETFOCUS:
4663 w32_detect_focus_change (dpyinfo, &msg, &inev);
4665 dpyinfo->grabbed = 0;
4666 check_visibility = 1;
4667 break;
4669 case WM_KILLFOCUS:
4670 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4672 if (f)
4674 if (f == dpyinfo->w32_focus_event_frame)
4675 dpyinfo->w32_focus_event_frame = 0;
4677 if (f == dpyinfo->w32_focus_frame)
4678 x_new_focus_frame (dpyinfo, 0);
4680 if (f == dpyinfo->mouse_face_mouse_frame)
4682 /* If we move outside the frame, then we're
4683 certainly no longer on any text in the frame. */
4684 clear_mouse_face (dpyinfo);
4685 dpyinfo->mouse_face_mouse_frame = 0;
4688 /* Generate a nil HELP_EVENT to cancel a help-echo.
4689 Do it only if there's something to cancel.
4690 Otherwise, the startup message is cleared when
4691 the mouse leaves the frame. */
4692 if (any_help_event_p)
4693 do_help = -1;
4696 dpyinfo->grabbed = 0;
4697 check_visibility = 1;
4698 break;
4700 case WM_CLOSE:
4701 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4703 if (f)
4705 inev.kind = DELETE_WINDOW_EVENT;
4706 XSETFRAME (inev.frame_or_window, f);
4708 break;
4710 case WM_INITMENU:
4711 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4713 if (f)
4715 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4716 XSETFRAME (inev.frame_or_window, f);
4718 break;
4720 case WM_COMMAND:
4721 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4723 if (f)
4725 extern void menubar_selection_callback
4726 (FRAME_PTR f, void * client_data);
4727 menubar_selection_callback (f, (void *)msg.msg.wParam);
4730 check_visibility = 1;
4731 break;
4733 case WM_DISPLAYCHANGE:
4734 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4736 if (f)
4738 dpyinfo->n_cbits = msg.msg.wParam;
4739 DebPrint (("display change: %d %d\n",
4740 (short) LOWORD (msg.msg.lParam),
4741 (short) HIWORD (msg.msg.lParam)));
4744 check_visibility = 1;
4745 break;
4747 default:
4748 /* Check for messages registered at runtime. */
4749 if (msg.msg.message == msh_mousewheel)
4751 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4752 msg.msg.message = WM_MOUSEWHEEL;
4753 prepend_msg (&msg);
4755 break;
4758 if (inev.kind != NO_EVENT)
4760 kbd_buffer_store_event_hold (&inev, hold_quit);
4761 count++;
4764 if (do_help
4765 && !(hold_quit && hold_quit->kind != NO_EVENT))
4767 Lisp_Object frame;
4769 if (f)
4770 XSETFRAME (frame, f);
4771 else
4772 frame = Qnil;
4774 if (do_help > 0)
4776 if (NILP (help_echo_string))
4778 help_echo_object = help_echo_window = Qnil;
4779 help_echo_pos = -1;
4782 any_help_event_p = 1;
4783 gen_help_event (help_echo_string, frame, help_echo_window,
4784 help_echo_object, help_echo_pos);
4786 else
4788 help_echo_string = Qnil;
4789 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4791 count++;
4795 /* If the focus was just given to an autoraising frame,
4796 raise it now. */
4797 /* ??? This ought to be able to handle more than one such frame. */
4798 if (pending_autoraise_frame)
4800 x_raise_frame (pending_autoraise_frame);
4801 pending_autoraise_frame = 0;
4804 /* Check which frames are still visisble, if we have enqueued any user
4805 events or been notified of events that may affect visibility. We
4806 do this here because there doesn't seem to be any direct
4807 notification from Windows that the visibility of a window has
4808 changed (at least, not in all cases). */
4809 if (count > 0 || check_visibility)
4811 Lisp_Object tail, frame;
4813 FOR_EACH_FRAME (tail, frame)
4815 FRAME_PTR f = XFRAME (frame);
4816 /* The tooltip has been drawn already. Avoid the
4817 SET_FRAME_GARBAGED below. */
4818 if (EQ (frame, tip_frame))
4819 continue;
4821 /* Check "visible" frames and mark each as obscured or not.
4822 Note that async_visible is nonzero for unobscured and
4823 obscured frames, but zero for hidden and iconified frames. */
4824 if (FRAME_W32_P (f) && f->async_visible)
4826 RECT clipbox;
4827 HDC hdc;
4829 enter_crit ();
4830 /* Query clipping rectangle for the entire window area
4831 (GetWindowDC), not just the client portion (GetDC).
4832 Otherwise, the scrollbars and menubar aren't counted as
4833 part of the visible area of the frame, and we may think
4834 the frame is obscured when really a scrollbar is still
4835 visible and gets WM_PAINT messages above. */
4836 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
4837 GetClipBox (hdc, &clipbox);
4838 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
4839 leave_crit ();
4841 if (clipbox.right == clipbox.left
4842 || clipbox.bottom == clipbox.top)
4844 /* Frame has become completely obscured so mark as
4845 such (we do this by setting async_visible to 2 so
4846 that FRAME_VISIBLE_P is still true, but redisplay
4847 will skip it). */
4848 f->async_visible = 2;
4850 if (!FRAME_OBSCURED_P (f))
4852 DebPrint (("frame %p (%s) obscured\n", f,
4853 SDATA (f->name)));
4856 else
4858 /* Frame is not obscured, so mark it as such. */
4859 f->async_visible = 1;
4861 if (FRAME_OBSCURED_P (f))
4863 SET_FRAME_GARBAGED (f);
4864 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
4865 SDATA (f->name)));
4867 /* Force a redisplay sooner or later. */
4868 record_asynch_buffer_change ();
4875 UNBLOCK_INPUT;
4876 return count;
4881 /***********************************************************************
4882 Text Cursor
4883 ***********************************************************************/
4885 /* Set clipping for output in glyph row ROW. W is the window in which
4886 we operate. GC is the graphics context to set clipping in.
4888 ROW may be a text row or, e.g., a mode line. Text rows must be
4889 clipped to the interior of the window dedicated to text display,
4890 mode lines must be clipped to the whole window. */
4892 static void
4893 w32_clip_to_row (w, row, area, hdc)
4894 struct window *w;
4895 struct glyph_row *row;
4896 int area;
4897 HDC hdc;
4899 struct frame *f = XFRAME (WINDOW_FRAME (w));
4900 RECT clip_rect;
4901 int window_x, window_y, window_width;
4903 window_box (w, area, &window_x, &window_y, &window_width, 0);
4905 clip_rect.left = window_x;
4906 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4907 clip_rect.top = max (clip_rect.top, window_y);
4908 clip_rect.right = clip_rect.left + window_width;
4909 clip_rect.bottom = clip_rect.top + row->visible_height;
4911 w32_set_clip_rectangle (hdc, &clip_rect);
4915 /* Draw a hollow box cursor on window W in glyph row ROW. */
4917 static void
4918 x_draw_hollow_cursor (w, row)
4919 struct window *w;
4920 struct glyph_row *row;
4922 struct frame *f = XFRAME (WINDOW_FRAME (w));
4923 HDC hdc;
4924 RECT rect;
4925 int left, top, h;
4926 struct glyph *cursor_glyph;
4927 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
4929 /* Get the glyph the cursor is on. If we can't tell because
4930 the current matrix is invalid or such, give up. */
4931 cursor_glyph = get_phys_cursor_glyph (w);
4932 if (cursor_glyph == NULL)
4933 return;
4935 /* Compute frame-relative coordinates for phys cursor. */
4936 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
4937 rect.left = left;
4938 rect.top = top;
4939 rect.bottom = rect.top + h;
4940 rect.right = rect.left + w->phys_cursor_width;
4942 hdc = get_frame_dc (f);
4943 /* Set clipping, draw the rectangle, and reset clipping again. */
4944 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4945 FrameRect (hdc, &rect, hb);
4946 DeleteObject (hb);
4947 w32_set_clip_rectangle (hdc, NULL);
4948 release_frame_dc (f, hdc);
4952 /* Draw a bar cursor on window W in glyph row ROW.
4954 Implementation note: One would like to draw a bar cursor with an
4955 angle equal to the one given by the font property XA_ITALIC_ANGLE.
4956 Unfortunately, I didn't find a font yet that has this property set.
4957 --gerd. */
4959 static void
4960 x_draw_bar_cursor (w, row, width, kind)
4961 struct window *w;
4962 struct glyph_row *row;
4963 int width;
4964 enum text_cursor_kinds kind;
4966 struct frame *f = XFRAME (w->frame);
4967 struct glyph *cursor_glyph;
4968 int x;
4969 HDC hdc;
4971 /* If cursor is out of bounds, don't draw garbage. This can happen
4972 in mini-buffer windows when switching between echo area glyphs
4973 and mini-buffer. */
4974 cursor_glyph = get_phys_cursor_glyph (w);
4975 if (cursor_glyph == NULL)
4976 return;
4978 /* If on an image, draw like a normal cursor. That's usually better
4979 visible than drawing a bar, esp. if the image is large so that
4980 the bar might not be in the window. */
4981 if (cursor_glyph->type == IMAGE_GLYPH)
4983 struct glyph_row *row;
4984 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
4985 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
4987 else
4989 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
4990 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
4992 /* If the glyph's background equals the color we normally draw
4993 the bar cursor in, the bar cursor in its normal color is
4994 invisible. Use the glyph's foreground color instead in this
4995 case, on the assumption that the glyph's colors are chosen so
4996 that the glyph is legible. */
4997 if (face->background == cursor_color)
4998 cursor_color = face->foreground;
5000 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5002 if (width < 0)
5003 width = FRAME_CURSOR_WIDTH (f);
5004 width = min (cursor_glyph->pixel_width, width);
5006 w->phys_cursor_width = width;
5009 hdc = get_frame_dc (f);
5010 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5012 if (kind == BAR_CURSOR)
5014 w32_fill_area (f, hdc, cursor_color, x,
5015 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5016 width, row->height);
5018 else
5020 w32_fill_area (f, hdc, cursor_color, x,
5021 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5022 row->height - width),
5023 min (FRAME_COLUMN_WIDTH (f), cursor_glyph->pixel_width),
5024 width);
5027 w32_set_clip_rectangle (hdc, NULL);
5028 release_frame_dc (f, hdc);
5033 /* RIF: Define cursor CURSOR on frame F. */
5035 static void
5036 w32_define_frame_cursor (f, cursor)
5037 struct frame *f;
5038 Cursor cursor;
5040 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5044 /* RIF: Clear area on frame F. */
5046 static void
5047 w32_clear_frame_area (f, x, y, width, height)
5048 struct frame *f;
5049 int x, y, width, height;
5051 HDC hdc;
5053 hdc = get_frame_dc (f);
5054 w32_clear_area (f, hdc, x, y, width, height);
5055 release_frame_dc (f, hdc);
5058 /* RIF: Draw or clear cursor on window W. */
5060 static void
5061 w32_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
5062 struct window *w;
5063 struct glyph_row *glyph_row;
5064 int x, y;
5065 int cursor_type, cursor_width;
5066 int on_p, active_p;
5068 if (on_p)
5070 /* If the user wants to use the system caret, make sure our own
5071 cursor remains invisible. */
5072 if (w32_use_visible_system_caret)
5074 /* Call to erase_phys_cursor here seems to use the
5075 wrong values of w->phys_cursor, as they have been
5076 overwritten before this function was called. */
5077 if (w->phys_cursor_type != NO_CURSOR)
5078 erase_phys_cursor (w);
5080 cursor_type = w->phys_cursor_type = NO_CURSOR;
5081 w->phys_cursor_width = -1;
5083 else
5085 w->phys_cursor_type = cursor_type;
5088 w->phys_cursor_on_p = 1;
5090 /* If this is the active cursor, we need to track it with the
5091 system caret, so third party software like screen magnifiers
5092 and speech synthesizers can follow the cursor. */
5093 if (active_p)
5095 struct frame *f = XFRAME (WINDOW_FRAME (w));
5096 HWND hwnd = FRAME_W32_WINDOW (f);
5098 w32_system_caret_x
5099 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5100 w32_system_caret_y
5101 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5102 + glyph_row->ascent - w->phys_cursor_ascent);
5104 /* If the size of the active cursor changed, destroy the old
5105 system caret. */
5106 if (w32_system_caret_hwnd
5107 && (w32_system_caret_height != w->phys_cursor_height))
5108 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5110 w32_system_caret_height = w->phys_cursor_height;
5112 /* Move the system caret. */
5113 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5116 if (glyph_row->exact_window_width_line_p
5117 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
5119 glyph_row->cursor_in_fringe_p = 1;
5120 draw_fringe_bitmap (w, glyph_row, 0);
5121 return;
5124 switch (cursor_type)
5126 case HOLLOW_BOX_CURSOR:
5127 x_draw_hollow_cursor (w, glyph_row);
5128 break;
5130 case FILLED_BOX_CURSOR:
5131 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5132 break;
5134 case BAR_CURSOR:
5135 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5136 break;
5138 case HBAR_CURSOR:
5139 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5140 break;
5142 case NO_CURSOR:
5143 w->phys_cursor_width = 0;
5144 break;
5146 default:
5147 abort ();
5154 /* Icons. */
5157 x_bitmap_icon (f, icon)
5158 struct frame *f;
5159 Lisp_Object icon;
5161 HANDLE main_icon;
5162 HANDLE small_icon = NULL;
5164 if (FRAME_W32_WINDOW (f) == 0)
5165 return 1;
5167 if (NILP (icon))
5168 main_icon = LoadIcon (hinst, EMACS_CLASS);
5169 else if (STRINGP (icon))
5171 /* Load the main icon from the named file. */
5172 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5173 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5174 /* Try to load a small icon to go with it. */
5175 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5176 GetSystemMetrics (SM_CXSMICON),
5177 GetSystemMetrics (SM_CYSMICON),
5178 LR_LOADFROMFILE);
5180 else if (SYMBOLP (icon))
5182 LPCTSTR name;
5184 if (EQ (icon, intern ("application")))
5185 name = (LPCTSTR) IDI_APPLICATION;
5186 else if (EQ (icon, intern ("hand")))
5187 name = (LPCTSTR) IDI_HAND;
5188 else if (EQ (icon, intern ("question")))
5189 name = (LPCTSTR) IDI_QUESTION;
5190 else if (EQ (icon, intern ("exclamation")))
5191 name = (LPCTSTR) IDI_EXCLAMATION;
5192 else if (EQ (icon, intern ("asterisk")))
5193 name = (LPCTSTR) IDI_ASTERISK;
5194 else if (EQ (icon, intern ("winlogo")))
5195 name = (LPCTSTR) IDI_WINLOGO;
5196 else
5197 return 1;
5199 main_icon = LoadIcon (NULL, name);
5201 else
5202 return 1;
5204 if (main_icon == NULL)
5205 return 1;
5207 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5208 (LPARAM) main_icon);
5210 /* If there is a small icon that goes with it, set that too. */
5211 if (small_icon)
5212 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5213 (LPARAM) small_icon);
5215 return 0;
5219 /************************************************************************
5220 Handling X errors
5221 ************************************************************************/
5223 /* Display Error Handling functions not used on W32. Listing them here
5224 helps diff stay in step when comparing w32term.c with xterm.c.
5226 x_error_catcher (display, error)
5227 x_catch_errors (dpy)
5228 x_catch_errors_unwind (old_val)
5229 x_check_errors (dpy, format)
5230 x_fully_uncatch_errors ()
5231 x_catching_errors ()
5232 x_had_errors_p (dpy)
5233 x_clear_errors (dpy)
5234 x_uncatch_errors (dpy, count)
5235 x_trace_wire ()
5236 x_connection_signal (signalnum)
5237 x_connection_closed (dpy, error_message)
5238 x_error_quitter (display, error)
5239 x_error_handler (display, error)
5240 x_io_error_quitter (display)
5245 /* Changing the font of the frame. */
5247 Lisp_Object
5248 x_new_font (f, font_object, fontset)
5249 struct frame *f;
5250 Lisp_Object font_object;
5251 int fontset;
5253 struct font *font = XFONT_OBJECT (font_object);
5255 if (fontset < 0)
5256 fontset = fontset_from_font (font_object);
5257 FRAME_FONTSET (f) = fontset;
5258 if (FRAME_FONT (f) == font)
5259 /* This font is already set in frame F. There's nothing more to
5260 do. */
5261 return font_object;
5263 FRAME_FONT (f) = font;
5264 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5265 FRAME_COLUMN_WIDTH (f) = font->average_width;
5266 FRAME_SPACE_WIDTH (f) = font->space_width;
5267 FRAME_LINE_HEIGHT (f) = font->height;
5269 compute_fringe_widths (f, 1);
5271 /* Compute the scroll bar width in character columns. */
5272 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5274 int wid = FRAME_COLUMN_WIDTH (f);
5275 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5276 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5278 else
5280 int wid = FRAME_COLUMN_WIDTH (f);
5281 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5284 /* Now make the frame display the given font. */
5285 if (FRAME_X_WINDOW (f) != 0)
5287 /* Don't change the size of a tip frame; there's no point in
5288 doing it because it's done in Fx_show_tip, and it leads to
5289 problems because the tip frame has no widget. */
5290 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5291 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5294 /* X version sets font of input methods here also. */
5296 return font_object;
5300 /***********************************************************************
5301 TODO: W32 Input Methods
5302 ***********************************************************************/
5303 /* Listing missing functions from xterm.c helps diff stay in step.
5305 xim_destroy_callback (xim, client_data, call_data)
5306 xim_open_dpy (dpyinfo, resource_name)
5307 struct xim_inst_t
5308 xim_instantiate_callback (display, client_data, call_data)
5309 xim_initialize (dpyinfo, resource_name)
5310 xim_close_dpy (dpyinfo)
5315 /* Calculate the absolute position in frame F
5316 from its current recorded position values and gravity. */
5318 void
5319 x_calc_absolute_position (f)
5320 struct frame *f;
5322 int flags = f->size_hint_flags;
5324 /* The sum of the widths of the frame's left and right borders, and
5325 the sum of the heights of the frame's top and bottom borders (in
5326 pixels) drawn by Windows. */
5327 unsigned int left_right_borders_width, top_bottom_borders_height;
5329 /* Try to get the actual values of these two variables. We compute
5330 the border width (height) by subtracting the width (height) of
5331 the frame's client area from the width (height) of the frame's
5332 entire window. */
5333 WINDOWPLACEMENT wp = { 0 };
5334 RECT client_rect = { 0 };
5336 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5337 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5339 left_right_borders_width =
5340 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5341 (client_rect.right - client_rect.left);
5343 top_bottom_borders_height =
5344 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5345 (client_rect.bottom - client_rect.top);
5347 else
5349 /* Use sensible default values. */
5350 left_right_borders_width = 8;
5351 top_bottom_borders_height = 32;
5354 /* Treat negative positions as relative to the rightmost bottommost
5355 position that fits on the screen. */
5356 if (flags & XNegative)
5357 f->left_pos = (x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f))
5358 - FRAME_PIXEL_WIDTH (f)
5359 + f->left_pos
5360 - (left_right_borders_width - 1));
5362 if (flags & YNegative)
5363 f->top_pos = (x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f))
5364 - FRAME_PIXEL_HEIGHT (f)
5365 + f->top_pos
5366 - (top_bottom_borders_height - 1));
5368 /* The left_pos and top_pos are now relative to the top and left
5369 screen edges, so the flags should correspond. */
5370 f->size_hint_flags &= ~ (XNegative | YNegative);
5373 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5374 to really change the position, and 0 when calling from
5375 x_make_frame_visible (in that case, XOFF and YOFF are the current
5376 position values). It is -1 when calling from x_set_frame_parameters,
5377 which means, do adjust for borders but don't change the gravity. */
5379 void
5380 x_set_offset (f, xoff, yoff, change_gravity)
5381 struct frame *f;
5382 register int xoff, yoff;
5383 int change_gravity;
5385 int modified_top, modified_left;
5387 if (change_gravity > 0)
5389 f->top_pos = yoff;
5390 f->left_pos = xoff;
5391 f->size_hint_flags &= ~ (XNegative | YNegative);
5392 if (xoff < 0)
5393 f->size_hint_flags |= XNegative;
5394 if (yoff < 0)
5395 f->size_hint_flags |= YNegative;
5396 f->win_gravity = NorthWestGravity;
5398 x_calc_absolute_position (f);
5400 BLOCK_INPUT;
5401 x_wm_set_size_hint (f, (long) 0, 0);
5403 modified_left = f->left_pos;
5404 modified_top = f->top_pos;
5406 my_set_window_pos (FRAME_W32_WINDOW (f),
5407 NULL,
5408 modified_left, modified_top,
5409 0, 0,
5410 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5411 UNBLOCK_INPUT;
5415 /* Check if we need to resize the frame due to a fullscreen request.
5416 If so needed, resize the frame. */
5417 static void
5418 x_check_fullscreen (f)
5419 struct frame *f;
5421 if (f->want_fullscreen & FULLSCREEN_BOTH)
5423 int width, height, ign;
5425 x_real_positions (f, &f->left_pos, &f->top_pos);
5427 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5429 /* We do not need to move the window, it shall be taken care of
5430 when setting WM manager hints. */
5431 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5433 change_frame_size (f, height, width, 0, 1, 0);
5434 SET_FRAME_GARBAGED (f);
5435 cancel_mouse_face (f);
5437 /* Wait for the change of frame size to occur */
5438 f->want_fullscreen |= FULLSCREEN_WAIT;
5443 /* Call this to change the size of frame F's x-window.
5444 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5445 for this size change and subsequent size changes.
5446 Otherwise we leave the window gravity unchanged. */
5448 void
5449 x_set_window_size (f, change_gravity, cols, rows)
5450 struct frame *f;
5451 int change_gravity;
5452 int cols, rows;
5454 int pixelwidth, pixelheight;
5456 BLOCK_INPUT;
5458 check_frame_size (f, &rows, &cols);
5459 f->scroll_bar_actual_width
5460 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5462 compute_fringe_widths (f, 0);
5464 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5465 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5467 f->win_gravity = NorthWestGravity;
5468 x_wm_set_size_hint (f, (long) 0, 0);
5471 RECT rect;
5473 rect.left = rect.top = 0;
5474 rect.right = pixelwidth;
5475 rect.bottom = pixelheight;
5477 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
5478 FRAME_EXTERNAL_MENU_BAR (f));
5480 my_set_window_pos (FRAME_W32_WINDOW (f),
5481 NULL,
5482 0, 0,
5483 rect.right - rect.left,
5484 rect.bottom - rect.top,
5485 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5488 #if 0
5489 /* The following mirrors what is done in xterm.c. It appears to be
5490 for informing lisp of the new size immediately, while the actual
5491 resize will happen asynchronously. But on Windows, the menu bar
5492 automatically wraps when the frame is too narrow to contain it,
5493 and that causes any calculations made here to come out wrong. The
5494 end is some nasty buggy behavior, including the potential loss
5495 of the minibuffer.
5497 Disabling this code is either not sufficient to fix the problems
5498 completely, or it causes fresh problems, but at least it removes
5499 the most problematic symptom of the minibuffer becoming unusable.
5501 -----------------------------------------------------------------
5503 Now, strictly speaking, we can't be sure that this is accurate,
5504 but the window manager will get around to dealing with the size
5505 change request eventually, and we'll hear how it went when the
5506 ConfigureNotify event gets here.
5508 We could just not bother storing any of this information here,
5509 and let the ConfigureNotify event set everything up, but that
5510 might be kind of confusing to the Lisp code, since size changes
5511 wouldn't be reported in the frame parameters until some random
5512 point in the future when the ConfigureNotify event arrives.
5514 We pass 1 for DELAY since we can't run Lisp code inside of
5515 a BLOCK_INPUT. */
5516 change_frame_size (f, rows, cols, 0, 1, 0);
5517 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5518 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5520 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5521 receive in the ConfigureNotify event; if we get what we asked
5522 for, then the event won't cause the screen to become garbaged, so
5523 we have to make sure to do it here. */
5524 SET_FRAME_GARBAGED (f);
5526 /* If cursor was outside the new size, mark it as off. */
5527 mark_window_cursors_off (XWINDOW (f->root_window));
5529 /* Clear out any recollection of where the mouse highlighting was,
5530 since it might be in a place that's outside the new frame size.
5531 Actually checking whether it is outside is a pain in the neck,
5532 so don't try--just let the highlighting be done afresh with new size. */
5533 cancel_mouse_face (f);
5534 #endif
5536 UNBLOCK_INPUT;
5539 /* Mouse warping. */
5541 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5543 void
5544 x_set_mouse_position (f, x, y)
5545 struct frame *f;
5546 int x, y;
5548 int pix_x, pix_y;
5550 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5551 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5553 if (pix_x < 0) pix_x = 0;
5554 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5556 if (pix_y < 0) pix_y = 0;
5557 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5559 x_set_mouse_pixel_position (f, pix_x, pix_y);
5562 void
5563 x_set_mouse_pixel_position (f, pix_x, pix_y)
5564 struct frame *f;
5565 int pix_x, pix_y;
5567 RECT rect;
5568 POINT pt;
5570 BLOCK_INPUT;
5572 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5573 pt.x = rect.left + pix_x;
5574 pt.y = rect.top + pix_y;
5575 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5577 SetCursorPos (pt.x, pt.y);
5579 UNBLOCK_INPUT;
5583 /* focus shifting, raising and lowering. */
5585 void
5586 x_focus_on_frame (f)
5587 struct frame *f;
5589 struct w32_display_info *dpyinfo = &one_w32_display_info;
5591 /* Give input focus to frame. */
5592 BLOCK_INPUT;
5593 #if 0
5594 /* Try not to change its Z-order if possible. */
5595 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5596 my_set_focus (f, FRAME_W32_WINDOW (f));
5597 else
5598 #endif
5599 my_set_foreground_window (FRAME_W32_WINDOW (f));
5600 UNBLOCK_INPUT;
5603 void
5604 x_unfocus_frame (f)
5605 struct frame *f;
5609 /* Raise frame F. */
5610 void
5611 x_raise_frame (f)
5612 struct frame *f;
5614 BLOCK_INPUT;
5616 /* Strictly speaking, raise-frame should only change the frame's Z
5617 order, leaving input focus unchanged. This is reasonable behavior
5618 on X where the usual policy is point-to-focus. However, this
5619 behavior would be very odd on Windows where the usual policy is
5620 click-to-focus.
5622 On X, if the mouse happens to be over the raised frame, it gets
5623 input focus anyway (so the window with focus will never be
5624 completely obscured) - if not, then just moving the mouse over it
5625 is sufficient to give it focus. On Windows, the user must actually
5626 click on the frame (preferrably the title bar so as not to move
5627 point), which is more awkward. Also, no other Windows program
5628 raises a window to the top but leaves another window (possibly now
5629 completely obscured) with input focus.
5631 Because there is a system setting on Windows that allows the user
5632 to choose the point to focus policy, we make the strict semantics
5633 optional, but by default we grab focus when raising. */
5635 if (NILP (Vw32_grab_focus_on_raise))
5637 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5638 not already the foreground application: the frame is raised
5639 above all other frames belonging to us, but not above the
5640 current top window. To achieve that, we have to resort to this
5641 more cumbersome method. */
5643 HDWP handle = BeginDeferWindowPos (2);
5644 if (handle)
5646 DeferWindowPos (handle,
5647 FRAME_W32_WINDOW (f),
5648 HWND_TOP,
5649 0, 0, 0, 0,
5650 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5652 DeferWindowPos (handle,
5653 GetForegroundWindow (),
5654 FRAME_W32_WINDOW (f),
5655 0, 0, 0, 0,
5656 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5658 EndDeferWindowPos (handle);
5661 else
5663 my_set_foreground_window (FRAME_W32_WINDOW (f));
5666 UNBLOCK_INPUT;
5669 /* Lower frame F. */
5670 void
5671 x_lower_frame (f)
5672 struct frame *f;
5674 BLOCK_INPUT;
5675 my_set_window_pos (FRAME_W32_WINDOW (f),
5676 HWND_BOTTOM,
5677 0, 0, 0, 0,
5678 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5679 UNBLOCK_INPUT;
5682 static void
5683 w32_frame_raise_lower (f, raise_flag)
5684 FRAME_PTR f;
5685 int raise_flag;
5687 if (! FRAME_W32_P (f))
5688 return;
5690 if (raise_flag)
5691 x_raise_frame (f);
5692 else
5693 x_lower_frame (f);
5696 /* Change of visibility. */
5698 /* This tries to wait until the frame is really visible.
5699 However, if the window manager asks the user where to position
5700 the frame, this will return before the user finishes doing that.
5701 The frame will not actually be visible at that time,
5702 but it will become visible later when the window manager
5703 finishes with it. */
5705 void
5706 x_make_frame_visible (f)
5707 struct frame *f;
5709 Lisp_Object type;
5711 BLOCK_INPUT;
5713 type = x_icon_type (f);
5714 if (!NILP (type))
5715 x_bitmap_icon (f, type);
5717 if (! FRAME_VISIBLE_P (f))
5719 /* We test FRAME_GARBAGED_P here to make sure we don't
5720 call x_set_offset a second time
5721 if we get to x_make_frame_visible a second time
5722 before the window gets really visible. */
5723 if (! FRAME_ICONIFIED_P (f)
5724 && ! f->output_data.w32->asked_for_visible)
5726 RECT workarea_rect;
5727 RECT window_rect;
5729 /* Adjust vertical window position in order to avoid being
5730 covered by a task bar placed at the bottom of the desktop. */
5731 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
5732 GetWindowRect(FRAME_W32_WINDOW(f), &window_rect);
5733 if (window_rect.bottom > workarea_rect.bottom
5734 && window_rect.top > workarea_rect.top)
5735 f->top_pos = max (window_rect.top
5736 - window_rect.bottom + workarea_rect.bottom,
5737 workarea_rect.top);
5739 x_set_offset (f, f->left_pos, f->top_pos, 0);
5742 f->output_data.w32->asked_for_visible = 1;
5744 /* The first of these seems to give more expected behavior, but
5745 was added as a commented out line in Sept 1997, with the
5746 second version remaining uncommented. There may have been
5747 some problem with it that led to it not being enabled,
5748 so the old version remains commented out below in case we
5749 decide we need to go back to it [23.0.60 2008-06-09]. */
5750 my_show_window (f, FRAME_W32_WINDOW (f),
5751 f->async_iconified ? SW_RESTORE : SW_SHOW);
5752 /* my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL); */
5755 /* Synchronize to ensure Emacs knows the frame is visible
5756 before we do anything else. We do this loop with input not blocked
5757 so that incoming events are handled. */
5759 Lisp_Object frame;
5760 int count;
5762 /* This must come after we set COUNT. */
5763 UNBLOCK_INPUT;
5765 XSETFRAME (frame, f);
5767 /* Wait until the frame is visible. Process X events until a
5768 MapNotify event has been seen, or until we think we won't get a
5769 MapNotify at all.. */
5770 for (count = input_signal_count + 10;
5771 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5773 /* Force processing of queued events. */
5774 /* TODO: x_sync equivalent? */
5776 /* Machines that do polling rather than SIGIO have been observed
5777 to go into a busy-wait here. So we'll fake an alarm signal
5778 to let the handler know that there's something to be read.
5779 We used to raise a real alarm, but it seems that the handler
5780 isn't always enabled here. This is probably a bug. */
5781 if (input_polling_used ())
5783 /* It could be confusing if a real alarm arrives while processing
5784 the fake one. Turn it off and let the handler reset it. */
5785 int old_poll_suppress_count = poll_suppress_count;
5786 poll_suppress_count = 1;
5787 poll_for_input_1 ();
5788 poll_suppress_count = old_poll_suppress_count;
5791 FRAME_SAMPLE_VISIBILITY (f);
5795 /* Change from mapped state to withdrawn state. */
5797 /* Make the frame visible (mapped and not iconified). */
5799 x_make_frame_invisible (f)
5800 struct frame *f;
5802 /* Don't keep the highlight on an invisible frame. */
5803 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5804 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5806 BLOCK_INPUT;
5808 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
5810 /* We can't distinguish this from iconification
5811 just by the event that we get from the server.
5812 So we can't win using the usual strategy of letting
5813 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5814 and synchronize with the server to make sure we agree. */
5815 f->visible = 0;
5816 FRAME_ICONIFIED_P (f) = 0;
5817 f->async_visible = 0;
5818 f->async_iconified = 0;
5820 UNBLOCK_INPUT;
5823 /* Change window state from mapped to iconified. */
5825 void
5826 x_iconify_frame (f)
5827 struct frame *f;
5829 Lisp_Object type;
5831 /* Don't keep the highlight on an invisible frame. */
5832 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5833 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5835 if (f->async_iconified)
5836 return;
5838 BLOCK_INPUT;
5840 type = x_icon_type (f);
5841 if (!NILP (type))
5842 x_bitmap_icon (f, type);
5844 /* Simulate the user minimizing the frame. */
5845 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
5847 UNBLOCK_INPUT;
5851 /* Free X resources of frame F. */
5853 void
5854 x_free_frame_resources (f)
5855 struct frame *f;
5857 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5859 BLOCK_INPUT;
5861 /* We must free faces before destroying windows because some
5862 font-driver (e.g. xft) access a window while finishing a
5863 face. */
5864 if (FRAME_FACE_CACHE (f))
5865 free_frame_faces (f);
5867 if (FRAME_W32_WINDOW (f))
5868 my_destroy_window (f, FRAME_W32_WINDOW (f));
5870 free_frame_menubar (f);
5872 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
5873 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
5874 unload_color (f, f->output_data.w32->cursor_pixel);
5875 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
5876 unload_color (f, f->output_data.w32->border_pixel);
5877 unload_color (f, f->output_data.w32->mouse_pixel);
5878 if (f->output_data.w32->white_relief.allocated_p)
5879 unload_color (f, f->output_data.w32->white_relief.pixel);
5880 if (f->output_data.w32->black_relief.allocated_p)
5881 unload_color (f, f->output_data.w32->black_relief.pixel);
5883 if (FRAME_FACE_CACHE (f))
5884 free_frame_faces (f);
5886 xfree (f->output_data.w32);
5887 f->output_data.w32 = NULL;
5889 if (f == dpyinfo->w32_focus_frame)
5890 dpyinfo->w32_focus_frame = 0;
5891 if (f == dpyinfo->w32_focus_event_frame)
5892 dpyinfo->w32_focus_event_frame = 0;
5893 if (f == dpyinfo->x_highlight_frame)
5894 dpyinfo->x_highlight_frame = 0;
5896 if (f == dpyinfo->mouse_face_mouse_frame)
5898 dpyinfo->mouse_face_beg_row
5899 = dpyinfo->mouse_face_beg_col = -1;
5900 dpyinfo->mouse_face_end_row
5901 = dpyinfo->mouse_face_end_col = -1;
5902 dpyinfo->mouse_face_window = Qnil;
5903 dpyinfo->mouse_face_deferred_gc = 0;
5904 dpyinfo->mouse_face_mouse_frame = 0;
5907 UNBLOCK_INPUT;
5911 /* Destroy the window of frame F. */
5912 void
5913 x_destroy_window (f)
5914 struct frame *f;
5916 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5918 x_free_frame_resources (f);
5919 dpyinfo->reference_count--;
5923 /* Setting window manager hints. */
5925 /* Set the normal size hints for the window manager, for frame F.
5926 FLAGS is the flags word to use--or 0 meaning preserve the flags
5927 that the window now has.
5928 If USER_POSITION is nonzero, we set the USPosition
5929 flag (this is useful when FLAGS is 0). */
5930 void
5931 x_wm_set_size_hint (f, flags, user_position)
5932 struct frame *f;
5933 long flags;
5934 int user_position;
5936 Window window = FRAME_W32_WINDOW (f);
5938 enter_crit ();
5940 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
5941 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
5942 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
5943 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
5945 leave_crit ();
5948 /* Window manager things */
5949 void
5950 x_wm_set_icon_position (f, icon_x, icon_y)
5951 struct frame *f;
5952 int icon_x, icon_y;
5954 #if 0
5955 Window window = FRAME_W32_WINDOW (f);
5957 f->display.x->wm_hints.flags |= IconPositionHint;
5958 f->display.x->wm_hints.icon_x = icon_x;
5959 f->display.x->wm_hints.icon_y = icon_y;
5961 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
5962 #endif
5966 /***********************************************************************
5967 Initialization
5968 ***********************************************************************/
5970 static int w32_initialized = 0;
5972 void
5973 w32_initialize_display_info (display_name)
5974 Lisp_Object display_name;
5976 struct w32_display_info *dpyinfo = &one_w32_display_info;
5978 bzero (dpyinfo, sizeof (*dpyinfo));
5980 /* Put it on w32_display_name_list. */
5981 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
5982 w32_display_name_list);
5983 dpyinfo->name_list_element = XCAR (w32_display_name_list);
5985 dpyinfo->w32_id_name
5986 = (char *) xmalloc (SCHARS (Vinvocation_name)
5987 + SCHARS (Vsystem_name)
5988 + 2);
5989 sprintf (dpyinfo->w32_id_name, "%s@%s",
5990 SDATA (Vinvocation_name), SDATA (Vsystem_name));
5992 /* Default Console mode values - overridden when running in GUI mode
5993 with values obtained from system metrics. */
5994 dpyinfo->resx = 1;
5995 dpyinfo->resy = 1;
5996 dpyinfo->n_planes = 1;
5997 dpyinfo->n_cbits = 4;
5998 dpyinfo->n_fonts = 0;
5999 dpyinfo->smallest_font_height = 1;
6000 dpyinfo->smallest_char_width = 1;
6002 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6003 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6004 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
6005 dpyinfo->mouse_face_window = Qnil;
6006 dpyinfo->mouse_face_overlay = Qnil;
6007 dpyinfo->mouse_face_hidden = 0;
6009 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6010 /* TODO: dpyinfo->gray */
6014 /* Create an xrdb-style database of resources to supercede registry settings.
6015 The database is just a concatenation of C strings, finished by an additional
6016 \0. The strings are submitted to some basic normalization, so
6018 [ *]option[ *]:[ *]value...
6020 becomes
6022 option:value...
6024 but any whitespace following value is not removed. */
6026 static char *
6027 w32_make_rdb (xrm_option)
6028 char *xrm_option;
6030 char *buffer = xmalloc (strlen (xrm_option) + 2);
6031 char *current = buffer;
6032 char ch;
6033 int in_option = 1;
6034 int before_value = 0;
6036 do {
6037 ch = *xrm_option++;
6039 if (ch == '\n')
6041 *current++ = '\0';
6042 in_option = 1;
6043 before_value = 0;
6045 else if (ch != ' ')
6047 *current++ = ch;
6048 if (in_option && (ch == ':'))
6050 in_option = 0;
6051 before_value = 1;
6053 else if (before_value)
6055 before_value = 0;
6058 else if (!(in_option || before_value))
6060 *current++ = ch;
6062 } while (ch);
6064 *current = '\0';
6066 return buffer;
6069 void
6070 x_flush (struct frame * f)
6071 { /* Nothing to do */ }
6074 extern frame_parm_handler w32_frame_parm_handlers[];
6076 static struct redisplay_interface w32_redisplay_interface =
6078 w32_frame_parm_handlers,
6079 x_produce_glyphs,
6080 x_write_glyphs,
6081 x_insert_glyphs,
6082 x_clear_end_of_line,
6083 x_scroll_run,
6084 x_after_update_window_line,
6085 x_update_window_begin,
6086 x_update_window_end,
6087 x_cursor_to,
6088 x_flush,
6089 0, /* flush_display_optional */
6090 x_clear_window_mouse_face,
6091 x_get_glyph_overhangs,
6092 x_fix_overlapping_area,
6093 w32_draw_fringe_bitmap,
6094 w32_define_fringe_bitmap,
6095 w32_destroy_fringe_bitmap,
6096 w32_compute_glyph_string_overhangs,
6097 x_draw_glyph_string,
6098 w32_define_frame_cursor,
6099 w32_clear_frame_area,
6100 w32_draw_window_cursor,
6101 w32_draw_vertical_window_border,
6102 w32_shift_glyphs_for_insert
6105 static void x_delete_terminal (struct terminal *term);
6107 static struct terminal *
6108 w32_create_terminal (struct w32_display_info *dpyinfo)
6110 struct terminal *terminal;
6112 terminal = create_terminal ();
6114 terminal->type = output_w32;
6115 terminal->display_info.w32 = dpyinfo;
6116 dpyinfo->terminal = terminal;
6118 /* MSVC does not type K&R functions with no arguments correctly, and
6119 so we must explicitly cast them. */
6120 terminal->clear_frame_hook = x_clear_frame;
6121 terminal->ins_del_lines_hook = x_ins_del_lines;
6122 terminal->delete_glyphs_hook = x_delete_glyphs;
6123 terminal->ring_bell_hook = w32_ring_bell;
6124 terminal->reset_terminal_modes_hook = w32_reset_terminal_modes;
6125 terminal->set_terminal_modes_hook = w32_set_terminal_modes;
6126 terminal->update_begin_hook = x_update_begin;
6127 terminal->update_end_hook = x_update_end;
6128 terminal->set_terminal_window_hook = w32_set_terminal_window;
6129 terminal->read_socket_hook = w32_read_socket;
6130 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6131 terminal->mouse_position_hook = w32_mouse_position;
6132 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6133 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6134 // terminal->fullscreen_hook = XTfullscreen_hook;
6135 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6136 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6137 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6138 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6140 terminal->delete_frame_hook = x_destroy_window;
6141 terminal->delete_terminal_hook = x_delete_terminal;
6143 terminal->rif = &w32_redisplay_interface;
6144 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
6145 terminal->char_ins_del_ok = 1;
6146 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
6147 terminal->fast_clear_end_of_line = 1; /* X does this well. */
6148 terminal->memory_below_frame = 0; /* We don't remember what scrolls
6149 off the bottom. */
6151 /* We don't yet support separate terminals on W32, so don't try to share
6152 keyboards between virtual terminals that are on the same physical
6153 terminal like X does. */
6154 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
6155 init_kboard (terminal->kboard);
6156 terminal->kboard->Vwindow_system = intern ("w32");
6157 terminal->kboard->next_kboard = all_kboards;
6158 all_kboards = terminal->kboard;
6159 /* Don't let the initial kboard remain current longer than necessary.
6160 That would cause problems if a file loaded on startup tries to
6161 prompt in the mini-buffer. */
6162 if (current_kboard == initial_kboard)
6163 current_kboard = terminal->kboard;
6164 terminal->kboard->reference_count++;
6166 return terminal;
6169 static void
6170 x_delete_terminal (struct terminal *terminal)
6172 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6173 int i;
6175 /* Protect against recursive calls. delete_frame in
6176 delete_terminal calls us back when it deletes our last frame. */
6177 if (!terminal->name)
6178 return;
6180 BLOCK_INPUT;
6182 x_delete_display (dpyinfo);
6183 UNBLOCK_INPUT;
6186 struct w32_display_info *
6187 w32_term_init (display_name, xrm_option, resource_name)
6188 Lisp_Object display_name;
6189 char *xrm_option;
6190 char *resource_name;
6192 struct w32_display_info *dpyinfo;
6193 struct terminal *terminal;
6194 HDC hdc;
6196 BLOCK_INPUT;
6198 if (!w32_initialized)
6200 w32_initialize ();
6201 w32_initialized = 1;
6204 w32_initialize_display_info (display_name);
6206 dpyinfo = &one_w32_display_info;
6207 terminal = w32_create_terminal (dpyinfo);
6209 /* Set the name of the terminal. */
6210 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
6211 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
6212 terminal->name[SBYTES (display_name)] = 0;
6214 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6216 /* Put this display on the chain. */
6217 dpyinfo->next = x_display_list;
6218 x_display_list = dpyinfo;
6220 hdc = GetDC (GetDesktopWindow ());
6222 dpyinfo->root_window = GetDesktopWindow ();
6223 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6224 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6225 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6226 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6227 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6228 ReleaseDC (GetDesktopWindow (), hdc);
6230 /* initialise palette with white and black */
6232 XColor color;
6233 w32_defined_color (0, "white", &color, 1);
6234 w32_defined_color (0, "black", &color, 1);
6237 /* Add the default keyboard. */
6238 add_keyboard_wait_descriptor (0);
6240 /* Create Fringe Bitmaps and store them for later use.
6242 On W32, bitmaps are all unsigned short, as Windows requires
6243 bitmap data to be Word aligned. For some reason they are
6244 horizontally reflected compared to how they appear on X, so we
6245 need to bitswap and convert to unsigned shorts before creating
6246 the bitmaps. */
6247 w32_init_fringe (terminal->rif);
6249 #ifdef F_SETOWN
6250 fcntl (connection, F_SETOWN, getpid ());
6251 #endif /* ! defined (F_SETOWN) */
6253 #ifdef SIGIO
6254 if (interrupt_input)
6255 init_sigio (connection);
6256 #endif /* ! defined (SIGIO) */
6258 UNBLOCK_INPUT;
6260 return dpyinfo;
6263 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6264 void
6265 x_delete_display (dpyinfo)
6266 struct w32_display_info *dpyinfo;
6268 /* Discard this display from w32_display_name_list and w32_display_list.
6269 We can't use Fdelq because that can quit. */
6270 if (! NILP (w32_display_name_list)
6271 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
6272 w32_display_name_list = XCDR (w32_display_name_list);
6273 else
6275 Lisp_Object tail;
6277 tail = w32_display_name_list;
6278 while (CONSP (tail) && CONSP (XCDR (tail)))
6280 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
6282 XSETCDR (tail, XCDR (XCDR (tail)));
6283 break;
6285 tail = XCDR (tail);
6289 /* free palette table */
6291 struct w32_palette_entry * plist;
6293 plist = dpyinfo->color_list;
6294 while (plist)
6296 struct w32_palette_entry * pentry = plist;
6297 plist = plist->next;
6298 xfree (pentry);
6300 dpyinfo->color_list = NULL;
6301 if (dpyinfo->palette)
6302 DeleteObject(dpyinfo->palette);
6304 xfree (dpyinfo->w32_id_name);
6306 w32_reset_fringes ();
6309 /* Set up use of W32. */
6311 DWORD WINAPI w32_msg_worker (void * arg);
6313 static void
6314 w32_initialize ()
6316 baud_rate = 19200;
6318 w32_system_caret_hwnd = NULL;
6319 w32_system_caret_height = 0;
6320 w32_system_caret_x = 0;
6321 w32_system_caret_y = 0;
6323 /* Initialize w32_use_visible_system_caret based on whether a screen
6324 reader is in use. */
6325 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6326 &w32_use_visible_system_caret, 0))
6327 w32_use_visible_system_caret = 0;
6329 last_tool_bar_item = -1;
6330 any_help_event_p = 0;
6332 /* Initialize input mode: interrupt_input off, no flow control, allow
6333 8 bit character input, standard quit char. */
6334 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6336 /* Create the window thread - it will terminate itself or when the app terminates */
6338 init_crit ();
6340 dwMainThreadId = GetCurrentThreadId ();
6341 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6342 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6344 /* Wait for thread to start */
6347 MSG msg;
6349 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6351 hWindowsThread = CreateThread (NULL, 0,
6352 w32_msg_worker,
6353 0, 0, &dwWindowsThreadId);
6355 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6358 /* It is desirable that mainThread should have the same notion of
6359 focus window and active window as windowsThread. Unfortunately, the
6360 following call to AttachThreadInput, which should do precisely what
6361 we need, causes major problems when Emacs is linked as a console
6362 program. Unfortunately, we have good reasons for doing that, so
6363 instead we need to send messages to windowsThread to make some API
6364 calls for us (ones that affect, or depend on, the active/focus
6365 window state.) */
6366 #ifdef ATTACH_THREADS
6367 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6368 #endif
6370 /* Dynamically link to optional system components. */
6372 HANDLE user_lib = LoadLibrary ("user32.dll");
6374 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6376 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6378 #undef LOAD_PROC
6380 FreeLibrary (user_lib);
6382 /* Ensure scrollbar handle is at least 5 pixels. */
6383 vertical_scroll_bar_min_handle = 5;
6385 /* For either kind of scroll bar, take account of the arrows; these
6386 effectively form the border of the main scroll bar range. */
6387 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6388 = GetSystemMetrics (SM_CYVSCROLL);
6392 void
6393 syms_of_w32term ()
6395 staticpro (&w32_display_name_list);
6396 w32_display_name_list = Qnil;
6398 staticpro (&last_mouse_scroll_bar);
6399 last_mouse_scroll_bar = Qnil;
6401 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6403 DEFVAR_INT ("w32-num-mouse-buttons",
6404 &w32_num_mouse_buttons,
6405 doc: /* Number of physical mouse buttons. */);
6406 w32_num_mouse_buttons = 2;
6408 DEFVAR_LISP ("w32-swap-mouse-buttons",
6409 &Vw32_swap_mouse_buttons,
6410 doc: /* Swap the mapping of middle and right mouse buttons.
6411 When nil, middle button is mouse-2 and right button is mouse-3. */);
6412 Vw32_swap_mouse_buttons = Qnil;
6414 DEFVAR_LISP ("w32-grab-focus-on-raise",
6415 &Vw32_grab_focus_on_raise,
6416 doc: /* Raised frame grabs input focus.
6417 When t, `raise-frame' grabs input focus as well. This fits well
6418 with the normal Windows click-to-focus policy, but might not be
6419 desirable when using a point-to-focus policy. */);
6420 Vw32_grab_focus_on_raise = Qt;
6422 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6423 &Vw32_capslock_is_shiftlock,
6424 doc: /* Apply CapsLock state to non character input keys.
6425 When nil, CapsLock only affects normal character input keys. */);
6426 Vw32_capslock_is_shiftlock = Qnil;
6428 DEFVAR_LISP ("w32-recognize-altgr",
6429 &Vw32_recognize_altgr,
6430 doc: /* Recognize right-alt and left-ctrl as AltGr.
6431 When nil, the right-alt and left-ctrl key combination is
6432 interpreted normally. */);
6433 Vw32_recognize_altgr = Qt;
6435 DEFVAR_BOOL ("w32-use-visible-system-caret",
6436 &w32_use_visible_system_caret,
6437 doc: /* Flag to make the system caret visible.
6438 When this is non-nil, Emacs will indicate the position of point by
6439 using the system caret instead of drawing its own cursor. Some screen
6440 reader software does not track the system cursor properly when it is
6441 invisible, and gets confused by Emacs drawing its own cursor, so this
6442 variable is initialized to t when Emacs detects that screen reader
6443 software is running as it starts up.
6445 When this variable is set, other variables affecting the appearance of
6446 the cursor have no effect. */);
6448 w32_use_visible_system_caret = 0;
6450 /* We don't yet support this, but defining this here avoids whining
6451 from cus-start.el and other places, like "M-x set-variable". */
6452 DEFVAR_BOOL ("x-use-underline-position-properties",
6453 &x_use_underline_position_properties,
6454 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
6455 A value of nil means ignore them. If you encounter fonts with bogus
6456 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6457 to 4.1, set this to nil.
6459 NOTE: Not supported on MS-Windows yet. */);
6460 x_use_underline_position_properties = 0;
6462 DEFVAR_BOOL ("x-underline-at-descent-line",
6463 &x_underline_at_descent_line,
6464 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
6465 A value of nil means to draw the underline according to the value of the
6466 variable `x-use-underline-position-properties', which is usually at the
6467 baseline level. The default value is nil. */);
6468 x_underline_at_descent_line = 0;
6470 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
6471 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6472 Vx_toolkit_scroll_bars = Qt;
6474 staticpro (&last_mouse_motion_frame);
6475 last_mouse_motion_frame = Qnil;
6478 /* arch-tag: 5fa70624-ab86-499c-8a85-473958ee4646
6479 (do not change this comment) */