Fix EOLs broken with last commit.
[emacs.git] / src / w32term.c
blobc99604205972c14113f040ea9a874490caa05291
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 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;
1530 /* Set up the foreground color for drawing relief lines of glyph
1531 string S. RELIEF is a pointer to a struct relief containing the GC
1532 with which lines will be drawn. Use a color that is FACTOR or
1533 DELTA lighter or darker than the relief's background which is found
1534 in S->f->output_data.x->relief_background. If such a color cannot
1535 be allocated, use DEFAULT_PIXEL, instead. */
1537 static void
1538 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
1539 struct frame *f;
1540 struct relief *relief;
1541 double factor;
1542 int delta;
1543 COLORREF default_pixel;
1545 XGCValues xgcv;
1546 struct w32_output *di = f->output_data.w32;
1547 unsigned long mask = GCForeground;
1548 COLORREF pixel;
1549 COLORREF background = di->relief_background;
1550 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
1552 /* TODO: Free colors (if using palette)? */
1554 /* Allocate new color. */
1555 xgcv.foreground = default_pixel;
1556 pixel = background;
1557 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1559 relief->allocated_p = 1;
1560 xgcv.foreground = relief->pixel = pixel;
1563 if (relief->gc == 0)
1565 #if 0 /* TODO: stipple */
1566 xgcv.stipple = dpyinfo->gray;
1567 mask |= GCStipple;
1568 #endif
1569 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1571 else
1572 XChangeGC (NULL, relief->gc, mask, &xgcv);
1576 /* Set up colors for the relief lines around glyph string S. */
1578 static void
1579 x_setup_relief_colors (s)
1580 struct glyph_string *s;
1582 struct w32_output *di = s->f->output_data.w32;
1583 COLORREF color;
1585 if (s->face->use_box_color_for_shadows_p)
1586 color = s->face->box_color;
1587 else if (s->first_glyph->type == IMAGE_GLYPH
1588 && s->img->pixmap
1589 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1590 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1591 else
1592 color = s->gc->background;
1594 if (di->white_relief.gc == 0
1595 || color != di->relief_background)
1597 di->relief_background = color;
1598 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1599 WHITE_PIX_DEFAULT (s->f));
1600 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1601 BLACK_PIX_DEFAULT (s->f));
1606 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1607 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1608 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1609 relief. LEFT_P non-zero means draw a relief on the left side of
1610 the rectangle. RIGHT_P non-zero means draw a relief on the right
1611 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1612 when drawing. */
1614 static void
1615 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1616 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
1617 struct frame *f;
1618 int left_x, top_y, right_x, bottom_y, width;
1619 int top_p, bot_p, left_p, right_p, raised_p;
1620 RECT *clip_rect;
1622 int i;
1623 XGCValues gc;
1624 HDC hdc = get_frame_dc (f);
1626 if (raised_p)
1627 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1628 else
1629 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1631 w32_set_clip_rectangle (hdc, clip_rect);
1633 /* Top. */
1634 if (top_p)
1635 for (i = 0; i < width; ++i)
1636 w32_fill_area (f, hdc, gc.foreground,
1637 left_x + i * left_p, top_y + i,
1638 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1640 /* Left. */
1641 if (left_p)
1642 for (i = 0; i < width; ++i)
1643 w32_fill_area (f, hdc, gc.foreground,
1644 left_x + i, top_y + i, 1,
1645 bottom_y - top_y - 2 * i + 1);
1647 if (raised_p)
1648 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1649 else
1650 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1652 /* Bottom. */
1653 if (bot_p)
1654 for (i = 0; i < width; ++i)
1655 w32_fill_area (f, hdc, gc.foreground,
1656 left_x + i * left_p, bottom_y - i,
1657 right_x - left_x - i * (left_p + right_p) + 1, 1);
1659 /* Right. */
1660 if (right_p)
1661 for (i = 0; i < width; ++i)
1662 w32_fill_area (f, hdc, gc.foreground,
1663 right_x - i, top_y + i + 1, 1,
1664 bottom_y - top_y - 2 * i - 1);
1666 w32_set_clip_rectangle (hdc, NULL);
1668 release_frame_dc (f, hdc);
1672 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1673 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1674 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1675 left side of the rectangle. RIGHT_P non-zero means draw a line
1676 on the right side of the rectangle. CLIP_RECT is the clipping
1677 rectangle to use when drawing. */
1679 static void
1680 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1681 left_p, right_p, clip_rect)
1682 struct glyph_string *s;
1683 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
1684 RECT *clip_rect;
1686 w32_set_clip_rectangle (s->hdc, clip_rect);
1688 /* Top. */
1689 w32_fill_area (s->f, s->hdc, s->face->box_color,
1690 left_x, top_y, right_x - left_x + 1, width);
1692 /* Left. */
1693 if (left_p)
1695 w32_fill_area (s->f, s->hdc, s->face->box_color,
1696 left_x, top_y, width, bottom_y - top_y + 1);
1699 /* Bottom. */
1700 w32_fill_area (s->f, s->hdc, s->face->box_color,
1701 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1703 /* Right. */
1704 if (right_p)
1706 w32_fill_area (s->f, s->hdc, s->face->box_color,
1707 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1710 w32_set_clip_rectangle (s->hdc, NULL);
1714 /* Draw a box around glyph string S. */
1716 static void
1717 x_draw_glyph_string_box (s)
1718 struct glyph_string *s;
1720 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1721 int left_p, right_p;
1722 struct glyph *last_glyph;
1723 RECT clip_rect;
1725 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1726 ? WINDOW_RIGHT_EDGE_X (s->w)
1727 : window_box_right (s->w, s->area));
1729 /* The glyph that may have a right box line. */
1730 last_glyph = (s->cmp || s->img
1731 ? s->first_glyph
1732 : s->first_glyph + s->nchars - 1);
1734 width = eabs (s->face->box_line_width);
1735 raised_p = s->face->box == FACE_RAISED_BOX;
1736 left_x = s->x;
1737 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1738 ? last_x - 1
1739 : min (last_x, s->x + s->background_width) - 1));
1740 top_y = s->y;
1741 bottom_y = top_y + s->height - 1;
1743 left_p = (s->first_glyph->left_box_line_p
1744 || (s->hl == DRAW_MOUSE_FACE
1745 && (s->prev == NULL
1746 || s->prev->hl != s->hl)));
1747 right_p = (last_glyph->right_box_line_p
1748 || (s->hl == DRAW_MOUSE_FACE
1749 && (s->next == NULL
1750 || s->next->hl != s->hl)));
1752 get_glyph_string_clip_rect (s, &clip_rect);
1754 if (s->face->box == FACE_SIMPLE_BOX)
1755 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1756 left_p, right_p, &clip_rect);
1757 else
1759 x_setup_relief_colors (s);
1760 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1761 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1766 /* Draw foreground of image glyph string S. */
1768 static void
1769 x_draw_image_foreground (s)
1770 struct glyph_string *s;
1772 int x = s->x;
1773 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1775 /* If first glyph of S has a left box line, start drawing it to the
1776 right of that line. */
1777 if (s->face->box != FACE_NO_BOX
1778 && s->first_glyph->left_box_line_p
1779 && s->slice.x == 0)
1780 x += eabs (s->face->box_line_width);
1782 /* If there is a margin around the image, adjust x- and y-position
1783 by that margin. */
1784 if (s->slice.x == 0)
1785 x += s->img->hmargin;
1786 if (s->slice.y == 0)
1787 y += s->img->vmargin;
1789 SaveDC (s->hdc);
1791 if (s->img->pixmap)
1793 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1794 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1795 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1796 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1797 SetBkColor (compat_hdc, RGB (255, 255, 255));
1798 SetTextColor (s->hdc, RGB (0, 0, 0));
1799 x_set_glyph_string_clipping (s);
1801 if (s->img->mask)
1803 HDC mask_dc = CreateCompatibleDC (s->hdc);
1804 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1806 SetTextColor (s->hdc, RGB (255, 255, 255));
1807 SetBkColor (s->hdc, RGB (0, 0, 0));
1809 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1810 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1811 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1812 mask_dc, s->slice.x, s->slice.y, SRCAND);
1813 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1814 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1816 SelectObject (mask_dc, mask_orig_obj);
1817 DeleteDC (mask_dc);
1819 else
1821 SetTextColor (s->hdc, s->gc->foreground);
1822 SetBkColor (s->hdc, s->gc->background);
1824 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1825 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1827 /* When the image has a mask, we can expect that at
1828 least part of a mouse highlight or a block cursor will
1829 be visible. If the image doesn't have a mask, make
1830 a block cursor visible by drawing a rectangle around
1831 the image. I believe it's looking better if we do
1832 nothing here for mouse-face. */
1833 if (s->hl == DRAW_CURSOR)
1835 int r = s->img->relief;
1836 if (r < 0) r = -r;
1837 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1838 s->slice.width + r*2 - 1,
1839 s->slice.height + r*2 - 1);
1843 w32_set_clip_rectangle (s->hdc, NULL);
1844 SelectObject (s->hdc, orig_brush);
1845 DeleteObject (fg_brush);
1846 SelectObject (compat_hdc, orig_obj);
1847 DeleteDC (compat_hdc);
1849 else
1850 w32_draw_rectangle (s->hdc, s->gc, x, y,
1851 s->slice.width - 1, s->slice.height - 1);
1853 RestoreDC (s->hdc ,-1);
1857 /* Draw a relief around the image glyph string S. */
1859 static void
1860 x_draw_image_relief (s)
1861 struct glyph_string *s;
1863 int x0, y0, x1, y1, thick, raised_p;
1864 RECT r;
1865 int x = s->x;
1866 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1868 /* If first glyph of S has a left box line, start drawing it to the
1869 right of that line. */
1870 if (s->face->box != FACE_NO_BOX
1871 && s->first_glyph->left_box_line_p
1872 && s->slice.x == 0)
1873 x += eabs (s->face->box_line_width);
1875 /* If there is a margin around the image, adjust x- and y-position
1876 by that margin. */
1877 if (s->slice.x == 0)
1878 x += s->img->hmargin;
1879 if (s->slice.y == 0)
1880 y += s->img->vmargin;
1882 if (s->hl == DRAW_IMAGE_SUNKEN
1883 || s->hl == DRAW_IMAGE_RAISED)
1885 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1886 raised_p = s->hl == DRAW_IMAGE_RAISED;
1888 else
1890 thick = eabs (s->img->relief);
1891 raised_p = s->img->relief > 0;
1894 x0 = x - thick;
1895 y0 = y - thick;
1896 x1 = x + s->slice.width + thick - 1;
1897 y1 = y + s->slice.height + thick - 1;
1899 x_setup_relief_colors (s);
1900 get_glyph_string_clip_rect (s, &r);
1901 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
1902 s->slice.y == 0,
1903 s->slice.y + s->slice.height == s->img->height,
1904 s->slice.x == 0,
1905 s->slice.x + s->slice.width == s->img->width,
1906 &r);
1910 /* Draw the foreground of image glyph string S to PIXMAP. */
1912 static void
1913 w32_draw_image_foreground_1 (s, pixmap)
1914 struct glyph_string *s;
1915 HBITMAP pixmap;
1917 HDC hdc = CreateCompatibleDC (s->hdc);
1918 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1919 int x = 0;
1920 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1922 /* If first glyph of S has a left box line, start drawing it to the
1923 right of that line. */
1924 if (s->face->box != FACE_NO_BOX
1925 && s->first_glyph->left_box_line_p
1926 && s->slice.x == 0)
1927 x += eabs (s->face->box_line_width);
1929 /* If there is a margin around the image, adjust x- and y-position
1930 by that margin. */
1931 if (s->slice.x == 0)
1932 x += s->img->hmargin;
1933 if (s->slice.y == 0)
1934 y += s->img->vmargin;
1936 if (s->img->pixmap)
1938 HDC compat_hdc = CreateCompatibleDC (hdc);
1939 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1940 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1941 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1943 if (s->img->mask)
1945 HDC mask_dc = CreateCompatibleDC (hdc);
1946 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1948 SetTextColor (hdc, RGB (0, 0, 0));
1949 SetBkColor (hdc, RGB (255, 255, 255));
1950 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1951 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1952 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1953 mask_dc, s->slice.x, s->slice.y, SRCAND);
1954 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1955 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1957 SelectObject (mask_dc, mask_orig_obj);
1958 DeleteDC (mask_dc);
1960 else
1962 SetTextColor (hdc, s->gc->foreground);
1963 SetBkColor (hdc, s->gc->background);
1965 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1966 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1968 /* When the image has a mask, we can expect that at
1969 least part of a mouse highlight or a block cursor will
1970 be visible. If the image doesn't have a mask, make
1971 a block cursor visible by drawing a rectangle around
1972 the image. I believe it's looking better if we do
1973 nothing here for mouse-face. */
1974 if (s->hl == DRAW_CURSOR)
1976 int r = s->img->relief;
1977 if (r < 0) r = -r;
1978 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
1979 s->slice.width + r*2 - 1,
1980 s->slice.height + r*2 - 1);
1984 SelectObject (hdc, orig_brush);
1985 DeleteObject (fg_brush);
1986 SelectObject (compat_hdc, orig_obj);
1987 DeleteDC (compat_hdc);
1989 else
1990 w32_draw_rectangle (hdc, s->gc, x, y,
1991 s->slice.width - 1, s->slice.height - 1);
1993 SelectObject (hdc, orig_hdc_obj);
1994 DeleteDC (hdc);
1998 /* Draw part of the background of glyph string S. X, Y, W, and H
1999 give the rectangle to draw. */
2001 static void
2002 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2003 struct glyph_string *s;
2004 int x, y, w, h;
2006 #if 0 /* TODO: stipple */
2007 if (s->stippled_p)
2009 /* Fill background with a stipple pattern. */
2010 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2011 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2012 XSetFillStyle (s->display, s->gc, FillSolid);
2014 else
2015 #endif
2016 x_clear_glyph_string_rect (s, x, y, w, h);
2020 /* Draw image glyph string S.
2022 s->y
2023 s->x +-------------------------
2024 | s->face->box
2026 | +-------------------------
2027 | | s->img->vmargin
2029 | | +-------------------
2030 | | | the image
2034 static void
2035 x_draw_image_glyph_string (s)
2036 struct glyph_string *s;
2038 int x, y;
2039 int box_line_hwidth = eabs (s->face->box_line_width);
2040 int box_line_vwidth = max (s->face->box_line_width, 0);
2041 int height;
2042 HBITMAP pixmap = 0;
2044 height = s->height - 2 * box_line_vwidth;
2046 /* Fill background with face under the image. Do it only if row is
2047 taller than image or if image has a clip mask to reduce
2048 flickering. */
2049 s->stippled_p = s->face->stipple != 0;
2050 if (height > s->slice.height
2051 || s->img->hmargin
2052 || s->img->vmargin
2053 || s->img->mask
2054 || s->img->pixmap == 0
2055 || s->width != s->background_width)
2057 x = s->x;
2058 if (s->first_glyph->left_box_line_p
2059 && s->slice.x == 0)
2060 x += box_line_hwidth;
2062 y = s->y;
2063 if (s->slice.y == 0)
2064 y += box_line_vwidth;
2066 #if 0 /* TODO: figure out if we need to do this on Windows. */
2067 if (s->img->mask)
2069 /* Create a pixmap as large as the glyph string. Fill it
2070 with the background color. Copy the image to it, using
2071 its mask. Copy the temporary pixmap to the display. */
2072 Screen *screen = FRAME_X_SCREEN (s->f);
2073 int depth = DefaultDepthOfScreen (screen);
2075 /* Create a pixmap as large as the glyph string. */
2076 pixmap = XCreatePixmap (s->display, s->window,
2077 s->background_width,
2078 s->height, depth);
2080 /* Don't clip in the following because we're working on the
2081 pixmap. */
2082 XSetClipMask (s->display, s->gc, None);
2084 /* Fill the pixmap with the background color/stipple. */
2085 if (s->stippled_p)
2087 /* Fill background with a stipple pattern. */
2088 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2089 XFillRectangle (s->display, pixmap, s->gc,
2090 0, 0, s->background_width, s->height);
2091 XSetFillStyle (s->display, s->gc, FillSolid);
2093 else
2095 XGCValues xgcv;
2096 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2097 &xgcv);
2098 XSetForeground (s->display, s->gc, xgcv.background);
2099 XFillRectangle (s->display, pixmap, s->gc,
2100 0, 0, s->background_width, s->height);
2101 XSetForeground (s->display, s->gc, xgcv.foreground);
2104 else
2105 #endif
2106 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2108 s->background_filled_p = 1;
2111 /* Draw the foreground. */
2112 if (pixmap != 0)
2114 w32_draw_image_foreground_1 (s, pixmap);
2115 x_set_glyph_string_clipping (s);
2117 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2118 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2119 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2120 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2122 SetTextColor (s->hdc, s->gc->foreground);
2123 SetBkColor (s->hdc, s->gc->background);
2124 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2125 compat_hdc, 0, 0, SRCCOPY);
2127 SelectObject (s->hdc, orig_brush);
2128 DeleteObject (fg_brush);
2129 SelectObject (compat_hdc, orig_obj);
2130 DeleteDC (compat_hdc);
2132 DeleteObject (pixmap);
2133 pixmap = 0;
2135 else
2136 x_draw_image_foreground (s);
2138 /* If we must draw a relief around the image, do it. */
2139 if (s->img->relief
2140 || s->hl == DRAW_IMAGE_RAISED
2141 || s->hl == DRAW_IMAGE_SUNKEN)
2142 x_draw_image_relief (s);
2146 /* Draw stretch glyph string S. */
2148 static void
2149 x_draw_stretch_glyph_string (s)
2150 struct glyph_string *s;
2152 xassert (s->first_glyph->type == STRETCH_GLYPH);
2154 if (s->hl == DRAW_CURSOR
2155 && !x_stretch_cursor_p)
2157 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2158 as wide as the stretch glyph. */
2159 int width, background_width = s->background_width;
2160 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2162 if (x < left_x)
2164 background_width -= left_x - x;
2165 x = left_x;
2167 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2169 /* Draw cursor. */
2170 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2172 /* Clear rest using the GC of the original non-cursor face. */
2173 if (width < background_width)
2175 XGCValues *gc = s->face->gc;
2176 int y = s->y;
2177 int w = background_width - width, h = s->height;
2178 RECT r;
2179 HDC hdc = s->hdc;
2181 x += width;
2182 if (s->row->mouse_face_p
2183 && cursor_in_mouse_face_p (s->w))
2185 x_set_mouse_face_gc (s);
2186 gc = s->gc;
2188 else
2189 gc = s->face->gc;
2191 get_glyph_string_clip_rect (s, &r);
2192 w32_set_clip_rectangle (hdc, &r);
2194 #if 0 /* TODO: stipple */
2195 if (s->face->stipple)
2197 /* Fill background with a stipple pattern. */
2198 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2199 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2200 XSetFillStyle (s->display, gc, FillSolid);
2202 else
2203 #endif
2205 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2209 else if (!s->background_filled_p)
2211 int background_width = s->background_width;
2212 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2214 /* Don't draw into left margin, fringe or scrollbar area
2215 except for header line and mode line. */
2216 if (x < left_x && !s->row->mode_line_p)
2218 background_width -= left_x - x;
2219 x = left_x;
2221 if (background_width > 0)
2222 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2225 s->background_filled_p = 1;
2229 /* Draw glyph string S. */
2231 static void
2232 x_draw_glyph_string (s)
2233 struct glyph_string *s;
2235 int relief_drawn_p = 0;
2237 /* If S draws into the background of its successor, draw the
2238 background of the successor first so that S can draw into it.
2239 This makes S->next use XDrawString instead of XDrawImageString. */
2240 if (s->next && s->right_overhang && !s->for_overlaps)
2242 int width;
2243 struct glyph_string *next;
2244 for (width = 0, next = s->next;
2245 next && width < s->right_overhang;
2246 width += next->width, next = next->next)
2247 if (next->first_glyph->type != IMAGE_GLYPH)
2249 x_set_glyph_string_gc (next);
2250 x_set_glyph_string_clipping (next);
2251 if (next->first_glyph->type == STRETCH_GLYPH)
2252 x_draw_stretch_glyph_string (next);
2253 else
2254 x_draw_glyph_string_background (next, 1);
2255 next->num_clips = 0;
2259 /* Set up S->gc, set clipping and draw S. */
2260 x_set_glyph_string_gc (s);
2262 /* Draw relief (if any) in advance for char/composition so that the
2263 glyph string can be drawn over it. */
2264 if (!s->for_overlaps
2265 && s->face->box != FACE_NO_BOX
2266 && (s->first_glyph->type == CHAR_GLYPH
2267 || s->first_glyph->type == COMPOSITE_GLYPH))
2270 x_set_glyph_string_clipping (s);
2271 x_draw_glyph_string_background (s, 1);
2272 x_draw_glyph_string_box (s);
2273 x_set_glyph_string_clipping (s);
2274 relief_drawn_p = 1;
2276 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2277 && !s->clip_tail
2278 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2279 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2280 /* We must clip just this glyph. left_overhang part has already
2281 drawn when s->prev was drawn, and right_overhang part will be
2282 drawn later when s->next is drawn. */
2283 x_set_glyph_string_clipping_exactly (s, s);
2284 else
2285 x_set_glyph_string_clipping (s);
2287 switch (s->first_glyph->type)
2289 case IMAGE_GLYPH:
2290 x_draw_image_glyph_string (s);
2291 break;
2293 case STRETCH_GLYPH:
2294 x_draw_stretch_glyph_string (s);
2295 break;
2297 case CHAR_GLYPH:
2298 if (s->for_overlaps)
2299 s->background_filled_p = 1;
2300 else
2301 x_draw_glyph_string_background (s, 0);
2302 x_draw_glyph_string_foreground (s);
2303 break;
2305 case COMPOSITE_GLYPH:
2306 if (s->for_overlaps || (s->cmp_from > 0
2307 && ! s->first_glyph->u.cmp.automatic))
2308 s->background_filled_p = 1;
2309 else
2310 x_draw_glyph_string_background (s, 1);
2311 x_draw_composite_glyph_string_foreground (s);
2312 break;
2314 default:
2315 abort ();
2318 if (!s->for_overlaps)
2320 /* Draw underline. */
2321 if (s->face->underline_p)
2323 unsigned long thickness, position;
2324 int y;
2326 if (s->prev && s->prev->face->underline_p)
2328 /* We use the same underline style as the previous one. */
2329 thickness = s->prev->underline_thickness;
2330 position = s->prev->underline_position;
2332 else
2334 /* Get the underline thickness. Default is 1 pixel. */
2335 if (s->font && s->font->underline_thickness > 0)
2336 thickness = s->font->underline_thickness;
2337 else
2338 thickness = 1;
2339 if (x_underline_at_descent_line)
2340 position = (s->height - thickness) - (s->ybase - s->y);
2341 else
2343 /* Get the underline position. This is the recommended
2344 vertical offset in pixels from the baseline to the top of
2345 the underline. This is a signed value according to the
2346 specs, and its default is
2348 ROUND ((maximum_descent) / 2), with
2349 ROUND (x) = floor (x + 0.5) */
2351 if (x_use_underline_position_properties
2352 && s->font && s->font->underline_position >= 0)
2353 position = s->font->underline_position;
2354 else if (s->font)
2355 position = (s->font->descent + 1) / 2;
2357 position = max (position, underline_minimum_offset);
2359 /* Check the sanity of thickness and position. We should
2360 avoid drawing underline out of the current line area. */
2361 if (s->y + s->height <= s->ybase + position)
2362 position = (s->height - 1) - (s->ybase - s->y);
2363 if (s->y + s->height < s->ybase + position + thickness)
2364 thickness = (s->y + s->height) - (s->ybase + position);
2365 s->underline_thickness = thickness;
2366 s->underline_position =position;
2367 y = s->ybase + position;
2368 if (s->face->underline_defaulted_p)
2370 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2371 y, s->background_width, 1);
2373 else
2375 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2376 y, s->background_width, 1);
2379 /* Draw overline. */
2380 if (s->face->overline_p)
2382 unsigned long dy = 0, h = 1;
2384 if (s->face->overline_color_defaulted_p)
2386 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2387 s->y + dy, s->background_width, h);
2389 else
2391 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2392 s->y + dy, s->background_width, h);
2396 /* Draw strike-through. */
2397 if (s->face->strike_through_p
2398 && !FONT_TEXTMETRIC(s->font).tmStruckOut)
2400 unsigned long h = 1;
2401 unsigned long dy = (s->height - h) / 2;
2403 if (s->face->strike_through_color_defaulted_p)
2405 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2406 s->width, h);
2408 else
2410 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2411 s->y + dy, s->width, h);
2415 /* Draw relief if not yet drawn. */
2416 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2417 x_draw_glyph_string_box (s);
2419 if (s->prev)
2421 struct glyph_string *prev;
2423 for (prev = s->prev; prev; prev = prev->prev)
2424 if (prev->hl != s->hl
2425 && prev->x + prev->width + prev->right_overhang > s->x)
2427 /* As prev was drawn while clipped to its own area, we
2428 must draw the right_overhang part using s->hl now. */
2429 enum draw_glyphs_face save = prev->hl;
2431 prev->hl = s->hl;
2432 x_set_glyph_string_gc (prev);
2433 x_set_glyph_string_clipping_exactly (s, prev);
2434 if (prev->first_glyph->type == CHAR_GLYPH)
2435 x_draw_glyph_string_foreground (prev);
2436 else
2437 x_draw_composite_glyph_string_foreground (prev);
2438 w32_set_clip_rectangle (prev->hdc, NULL);
2439 prev->hl = save;
2440 prev->num_clips = 0;
2444 if (s->next)
2446 struct glyph_string *next;
2448 for (next = s->next; next; next = next->next)
2449 if (next->hl != s->hl
2450 && next->x - next->left_overhang < s->x + s->width)
2452 /* As next will be drawn while clipped to its own area,
2453 we must draw the left_overhang part using s->hl now. */
2454 enum draw_glyphs_face save = next->hl;
2456 next->hl = s->hl;
2457 x_set_glyph_string_gc (next);
2458 x_set_glyph_string_clipping_exactly (s, next);
2459 if (next->first_glyph->type == CHAR_GLYPH)
2460 x_draw_glyph_string_foreground (next);
2461 else
2462 x_draw_composite_glyph_string_foreground (next);
2463 w32_set_clip_rectangle (next->hdc, NULL);
2464 next->hl = save;
2465 next->num_clips = 0;
2470 /* Reset clipping. */
2471 w32_set_clip_rectangle (s->hdc, NULL);
2472 s->num_clips = 0;
2476 /* Shift display to make room for inserted glyphs. */
2478 void
2479 w32_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2480 struct frame *f;
2481 int x, y, width, height, shift_by;
2483 HDC hdc;
2485 hdc = get_frame_dc (f);
2486 BitBlt (hdc, x + shift_by, y, width, height,
2487 hdc, x, y, SRCCOPY);
2489 release_frame_dc (f, hdc);
2493 /* Delete N glyphs at the nominal cursor position. Not implemented
2494 for X frames. */
2496 static void
2497 x_delete_glyphs (f, n)
2498 struct frame *f;
2499 register int n;
2501 if (! FRAME_W32_P (f))
2502 return;
2504 abort ();
2508 /* Clear entire frame. If updating_frame is non-null, clear that
2509 frame. Otherwise clear the selected frame. */
2511 static void
2512 x_clear_frame (struct frame *f)
2514 if (! FRAME_W32_P (f))
2515 return;
2517 /* Clearing the frame will erase any cursor, so mark them all as no
2518 longer visible. */
2519 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2520 output_cursor.hpos = output_cursor.vpos = 0;
2521 output_cursor.x = -1;
2523 /* We don't set the output cursor here because there will always
2524 follow an explicit cursor_to. */
2525 BLOCK_INPUT;
2527 w32_clear_window (f);
2529 /* We have to clear the scroll bars, too. If we have changed
2530 colors or something like that, then they should be notified. */
2531 x_scroll_bar_clear (f);
2533 UNBLOCK_INPUT;
2537 /* Make audible bell. */
2539 static void
2540 w32_ring_bell (struct frame *f)
2542 BLOCK_INPUT;
2544 if (FRAME_W32_P (f) && visible_bell)
2546 int i;
2547 HWND hwnd = FRAME_W32_WINDOW (f);
2549 for (i = 0; i < 5; i++)
2551 FlashWindow (hwnd, TRUE);
2552 Sleep (10);
2554 FlashWindow (hwnd, FALSE);
2556 else
2557 w32_sys_ring_bell (f);
2559 UNBLOCK_INPUT;
2563 /* Specify how many text lines, from the top of the window,
2564 should be affected by insert-lines and delete-lines operations.
2565 This, and those operations, are used only within an update
2566 that is bounded by calls to x_update_begin and x_update_end. */
2568 static void
2569 w32_set_terminal_window (n)
2570 register int n;
2572 /* This function intentionally left blank. */
2576 /***********************************************************************
2577 Line Dance
2578 ***********************************************************************/
2580 /* Perform an insert-lines or delete-lines operation, inserting N
2581 lines or deleting -N lines at vertical position VPOS. */
2583 static void
2584 x_ins_del_lines (f, vpos, n)
2585 struct frame *f;
2586 int vpos, n;
2588 if (! FRAME_W32_P (f))
2589 return;
2591 abort ();
2595 /* Scroll part of the display as described by RUN. */
2597 static void
2598 x_scroll_run (w, run)
2599 struct window *w;
2600 struct run *run;
2602 struct frame *f = XFRAME (w->frame);
2603 int x, y, width, height, from_y, to_y, bottom_y;
2604 HWND hwnd = FRAME_W32_WINDOW (f);
2605 HRGN expect_dirty;
2607 /* Get frame-relative bounding box of the text display area of W,
2608 without mode lines. Include in this box the left and right
2609 fringes of W. */
2610 window_box (w, -1, &x, &y, &width, &height);
2612 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2613 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2614 bottom_y = y + height;
2616 if (to_y < from_y)
2618 /* Scrolling up. Make sure we don't copy part of the mode
2619 line at the bottom. */
2620 if (from_y + run->height > bottom_y)
2621 height = bottom_y - from_y;
2622 else
2623 height = run->height;
2624 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2626 else
2628 /* Scolling down. Make sure we don't copy over the mode line.
2629 at the bottom. */
2630 if (to_y + run->height > bottom_y)
2631 height = bottom_y - to_y;
2632 else
2633 height = run->height;
2634 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2637 BLOCK_INPUT;
2639 /* Cursor off. Will be switched on again in x_update_window_end. */
2640 updated_window = w;
2641 x_clear_cursor (w);
2644 RECT from;
2645 RECT to;
2646 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2647 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2649 from.left = to.left = x;
2650 from.right = to.right = x + width;
2651 from.top = from_y;
2652 from.bottom = from_y + height;
2653 to.top = y;
2654 to.bottom = bottom_y;
2656 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2657 NULL, SW_INVALIDATE);
2659 /* Combine this with what we expect to be dirty. This covers the
2660 case where not all of the region we expect is actually dirty. */
2661 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2663 /* If the dirty region is not what we expected, redraw the entire frame. */
2664 if (!EqualRgn (combined, expect_dirty))
2665 SET_FRAME_GARBAGED (f);
2667 DeleteObject (dirty);
2668 DeleteObject (combined);
2671 UNBLOCK_INPUT;
2672 DeleteObject (expect_dirty);
2677 /***********************************************************************
2678 Exposure Events
2679 ***********************************************************************/
2681 static void
2682 frame_highlight (f)
2683 struct frame *f;
2685 x_update_cursor (f, 1);
2686 x_set_frame_alpha (f);
2689 static void
2690 frame_unhighlight (f)
2691 struct frame *f;
2693 x_update_cursor (f, 1);
2694 x_set_frame_alpha (f);
2697 /* The focus has changed. Update the frames as necessary to reflect
2698 the new situation. Note that we can't change the selected frame
2699 here, because the Lisp code we are interrupting might become confused.
2700 Each event gets marked with the frame in which it occurred, so the
2701 Lisp code can tell when the switch took place by examining the events. */
2703 static void
2704 x_new_focus_frame (dpyinfo, frame)
2705 struct w32_display_info *dpyinfo;
2706 struct frame *frame;
2708 struct frame *old_focus = dpyinfo->w32_focus_frame;
2710 if (frame != dpyinfo->w32_focus_frame)
2712 /* Set this before calling other routines, so that they see
2713 the correct value of w32_focus_frame. */
2714 dpyinfo->w32_focus_frame = frame;
2716 if (old_focus && old_focus->auto_lower)
2717 x_lower_frame (old_focus);
2719 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2720 pending_autoraise_frame = dpyinfo->w32_focus_frame;
2721 else
2722 pending_autoraise_frame = 0;
2725 x_frame_rehighlight (dpyinfo);
2729 /* Handle FocusIn and FocusOut state changes for FRAME.
2730 If FRAME has focus and there exists more than one frame, puts
2731 a FOCUS_IN_EVENT into *BUFP. */
2733 static void
2734 x_focus_changed (type, state, dpyinfo, frame, bufp)
2735 int type;
2736 int state;
2737 struct w32_display_info *dpyinfo;
2738 struct frame *frame;
2739 struct input_event *bufp;
2741 if (type == WM_SETFOCUS)
2743 if (dpyinfo->w32_focus_event_frame != frame)
2745 x_new_focus_frame (dpyinfo, frame);
2746 dpyinfo->w32_focus_event_frame = frame;
2748 /* Don't stop displaying the initial startup message
2749 for a switch-frame event we don't need. */
2750 if (NILP (Vterminal_frame)
2751 && CONSP (Vframe_list)
2752 && !NILP (XCDR (Vframe_list)))
2754 bufp->kind = FOCUS_IN_EVENT;
2755 XSETFRAME (bufp->frame_or_window, frame);
2759 frame->output_data.x->focus_state |= state;
2761 /* TODO: IME focus? */
2763 else if (type == WM_KILLFOCUS)
2765 frame->output_data.x->focus_state &= ~state;
2767 if (dpyinfo->w32_focus_event_frame == frame)
2769 dpyinfo->w32_focus_event_frame = 0;
2770 x_new_focus_frame (dpyinfo, 0);
2773 /* TODO: IME focus? */
2778 /* The focus may have changed. Figure out if it is a real focus change,
2779 by checking both FocusIn/Out and Enter/LeaveNotify events.
2781 Returns FOCUS_IN_EVENT event in *BUFP. */
2783 static void
2784 w32_detect_focus_change (dpyinfo, event, bufp)
2785 struct w32_display_info *dpyinfo;
2786 W32Msg *event;
2787 struct input_event *bufp;
2789 struct frame *frame;
2791 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2792 if (! frame)
2793 return;
2795 /* On w32, this is only called from focus events, so no switch needed. */
2796 x_focus_changed (event->msg.message,
2797 (event->msg.message == WM_KILLFOCUS ?
2798 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2799 dpyinfo, frame, bufp);
2803 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2805 void
2806 x_mouse_leave (dpyinfo)
2807 struct w32_display_info *dpyinfo;
2809 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2812 /* The focus has changed, or we have redirected a frame's focus to
2813 another frame (this happens when a frame uses a surrogate
2814 mini-buffer frame). Shift the highlight as appropriate.
2816 The FRAME argument doesn't necessarily have anything to do with which
2817 frame is being highlighted or un-highlighted; we only use it to find
2818 the appropriate X display info. */
2820 static void
2821 w32_frame_rehighlight (frame)
2822 struct frame *frame;
2824 if (! FRAME_W32_P (frame))
2825 return;
2826 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
2829 static void
2830 x_frame_rehighlight (dpyinfo)
2831 struct w32_display_info *dpyinfo;
2833 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2835 if (dpyinfo->w32_focus_frame)
2837 dpyinfo->x_highlight_frame
2838 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2839 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2840 : dpyinfo->w32_focus_frame);
2841 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2843 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
2844 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2847 else
2848 dpyinfo->x_highlight_frame = 0;
2850 if (dpyinfo->x_highlight_frame != old_highlight)
2852 if (old_highlight)
2853 frame_unhighlight (old_highlight);
2854 if (dpyinfo->x_highlight_frame)
2855 frame_highlight (dpyinfo->x_highlight_frame);
2859 /* Keyboard processing - modifier keys, etc. */
2861 /* Convert a keysym to its name. */
2863 char *
2864 x_get_keysym_name (keysym)
2865 int keysym;
2867 /* Make static so we can always return it */
2868 static char value[100];
2870 BLOCK_INPUT;
2871 GetKeyNameText (keysym, value, 100);
2872 UNBLOCK_INPUT;
2874 return value;
2879 /* Mouse clicks and mouse movement. Rah. */
2881 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2882 the state in PUP. XBUTTON provides extra information for extended mouse
2883 button messages. Returns FALSE if unable to parse the message. */
2884 BOOL
2885 parse_button (message, xbutton, pbutton, pup)
2886 int message;
2887 int xbutton;
2888 int * pbutton;
2889 int * pup;
2891 int button = 0;
2892 int up = 0;
2894 switch (message)
2896 case WM_LBUTTONDOWN:
2897 button = 0;
2898 up = 0;
2899 break;
2900 case WM_LBUTTONUP:
2901 button = 0;
2902 up = 1;
2903 break;
2904 case WM_MBUTTONDOWN:
2905 if (NILP (Vw32_swap_mouse_buttons))
2906 button = 1;
2907 else
2908 button = 2;
2909 up = 0;
2910 break;
2911 case WM_MBUTTONUP:
2912 if (NILP (Vw32_swap_mouse_buttons))
2913 button = 1;
2914 else
2915 button = 2;
2916 up = 1;
2917 break;
2918 case WM_RBUTTONDOWN:
2919 if (NILP (Vw32_swap_mouse_buttons))
2920 button = 2;
2921 else
2922 button = 1;
2923 up = 0;
2924 break;
2925 case WM_RBUTTONUP:
2926 if (NILP (Vw32_swap_mouse_buttons))
2927 button = 2;
2928 else
2929 button = 1;
2930 up = 1;
2931 break;
2932 case WM_XBUTTONDOWN:
2933 button = xbutton + 2;
2934 up = 0;
2935 break;
2936 case WM_XBUTTONUP:
2937 button = xbutton + 2;
2938 up = 1;
2939 break;
2940 default:
2941 return (FALSE);
2944 if (pup) *pup = up;
2945 if (pbutton) *pbutton = button;
2947 return (TRUE);
2951 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2953 If the event is a button press, then note that we have grabbed
2954 the mouse. */
2956 static Lisp_Object
2957 construct_mouse_click (result, msg, f)
2958 struct input_event *result;
2959 W32Msg *msg;
2960 struct frame *f;
2962 int button;
2963 int up;
2965 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
2966 &button, &up);
2968 /* Make the event type NO_EVENT; we'll change that when we decide
2969 otherwise. */
2970 result->kind = MOUSE_CLICK_EVENT;
2971 result->code = button;
2972 result->timestamp = msg->msg.time;
2973 result->modifiers = (msg->dwModifiers
2974 | (up
2975 ? up_modifier
2976 : down_modifier));
2978 XSETINT (result->x, LOWORD (msg->msg.lParam));
2979 XSETINT (result->y, HIWORD (msg->msg.lParam));
2980 XSETFRAME (result->frame_or_window, f);
2981 result->arg = Qnil;
2982 return Qnil;
2985 static Lisp_Object
2986 construct_mouse_wheel (result, msg, f)
2987 struct input_event *result;
2988 W32Msg *msg;
2989 struct frame *f;
2991 POINT p;
2992 int delta;
2994 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
2995 : WHEEL_EVENT;
2996 result->code = 0;
2997 result->timestamp = msg->msg.time;
2999 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3000 forward, away from the user (up); a negative value indicates that
3001 the wheel was rotated backward, toward the user (down). */
3002 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3004 /* The up and down modifiers indicate if the wheel was rotated up or
3005 down based on WHEEL_DELTA value. */
3006 result->modifiers = (msg->dwModifiers
3007 | ((delta < 0 ) ? down_modifier : up_modifier));
3009 /* With multiple monitors, we can legitimately get negative
3010 coordinates, so cast to short to interpret them correctly. */
3011 p.x = (short) LOWORD (msg->msg.lParam);
3012 p.y = (short) HIWORD (msg->msg.lParam);
3013 ScreenToClient (msg->msg.hwnd, &p);
3014 XSETINT (result->x, p.x);
3015 XSETINT (result->y, p.y);
3016 XSETFRAME (result->frame_or_window, f);
3017 result->arg = Qnil;
3018 return Qnil;
3021 static Lisp_Object
3022 construct_drag_n_drop (result, msg, f)
3023 struct input_event *result;
3024 W32Msg *msg;
3025 struct frame *f;
3027 Lisp_Object files;
3028 Lisp_Object frame;
3029 HDROP hdrop;
3030 POINT p;
3031 WORD num_files;
3032 char *name;
3033 int i, len;
3035 result->kind = DRAG_N_DROP_EVENT;
3036 result->code = 0;
3037 result->timestamp = msg->msg.time;
3038 result->modifiers = msg->dwModifiers;
3040 hdrop = (HDROP) msg->msg.wParam;
3041 DragQueryPoint (hdrop, &p);
3043 #if 0
3044 p.x = LOWORD (msg->msg.lParam);
3045 p.y = HIWORD (msg->msg.lParam);
3046 ScreenToClient (msg->msg.hwnd, &p);
3047 #endif
3049 XSETINT (result->x, p.x);
3050 XSETINT (result->y, p.y);
3052 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3053 files = Qnil;
3055 for (i = 0; i < num_files; i++)
3057 len = DragQueryFile (hdrop, i, NULL, 0);
3058 if (len <= 0)
3059 continue;
3060 name = alloca (len + 1);
3061 DragQueryFile (hdrop, i, name, len + 1);
3062 files = Fcons (DECODE_FILE (build_string (name)), files);
3065 DragFinish (hdrop);
3067 XSETFRAME (frame, f);
3068 result->frame_or_window = frame;
3069 result->arg = files;
3070 return Qnil;
3074 /* Function to report a mouse movement to the mainstream Emacs code.
3075 The input handler calls this.
3077 We have received a mouse movement event, which is given in *event.
3078 If the mouse is over a different glyph than it was last time, tell
3079 the mainstream emacs code by setting mouse_moved. If not, ask for
3080 another motion event, so we can check again the next time it moves. */
3082 static MSG last_mouse_motion_event;
3083 static Lisp_Object last_mouse_motion_frame;
3085 static int
3086 note_mouse_movement (frame, msg)
3087 FRAME_PTR frame;
3088 MSG *msg;
3090 int mouse_x = LOWORD (msg->lParam);
3091 int mouse_y = HIWORD (msg->lParam);
3093 last_mouse_movement_time = msg->time;
3094 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
3095 XSETFRAME (last_mouse_motion_frame, frame);
3097 if (!FRAME_X_OUTPUT (frame))
3098 return 0;
3100 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3102 frame->mouse_moved = 1;
3103 last_mouse_scroll_bar = Qnil;
3104 note_mouse_highlight (frame, -1, -1);
3105 last_mouse_glyph_frame = 0;
3106 return 1;
3109 /* Has the mouse moved off the glyph it was on at the last sighting? */
3110 if (frame != last_mouse_glyph_frame
3111 || mouse_x < last_mouse_glyph.left
3112 || mouse_x >= last_mouse_glyph.right
3113 || mouse_y < last_mouse_glyph.top
3114 || mouse_y >= last_mouse_glyph.bottom)
3116 frame->mouse_moved = 1;
3117 last_mouse_scroll_bar = Qnil;
3118 note_mouse_highlight (frame, mouse_x, mouse_y);
3119 /* Remember the mouse position here, as w32_mouse_position only
3120 gets called when mouse tracking is enabled but we also need
3121 to keep track of the mouse for help_echo and highlighting at
3122 other times. */
3123 remember_mouse_glyph (frame, mouse_x, mouse_y, &last_mouse_glyph);
3124 last_mouse_glyph_frame = frame;
3125 return 1;
3128 return 0;
3132 /************************************************************************
3133 Mouse Face
3134 ************************************************************************/
3136 static struct scroll_bar *x_window_to_scroll_bar ();
3137 static void x_scroll_bar_report_motion ();
3138 static void x_check_fullscreen P_ ((struct frame *));
3140 static void
3141 redo_mouse_highlight ()
3143 if (!NILP (last_mouse_motion_frame)
3144 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3145 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3146 LOWORD (last_mouse_motion_event.lParam),
3147 HIWORD (last_mouse_motion_event.lParam));
3150 static void
3151 w32_define_cursor (window, cursor)
3152 Window window;
3153 Cursor cursor;
3155 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3157 /* Return the current position of the mouse.
3158 *fp should be a frame which indicates which display to ask about.
3160 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3161 and *part to the frame, window, and scroll bar part that the mouse
3162 is over. Set *x and *y to the portion and whole of the mouse's
3163 position on the scroll bar.
3165 If the mouse movement started elsewhere, set *fp to the frame the
3166 mouse is on, *bar_window to nil, and *x and *y to the character cell
3167 the mouse is over.
3169 Set *time to the server time-stamp for the time at which the mouse
3170 was at this position.
3172 Don't store anything if we don't have a valid set of values to report.
3174 This clears the mouse_moved flag, so we can wait for the next mouse
3175 movement. */
3177 static void
3178 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
3179 FRAME_PTR *fp;
3180 int insist;
3181 Lisp_Object *bar_window;
3182 enum scroll_bar_part *part;
3183 Lisp_Object *x, *y;
3184 unsigned long *time;
3186 FRAME_PTR f1;
3188 BLOCK_INPUT;
3190 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3191 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3192 else
3194 POINT pt;
3196 Lisp_Object frame, tail;
3198 /* Clear the mouse-moved flag for every frame on this display. */
3199 FOR_EACH_FRAME (tail, frame)
3200 XFRAME (frame)->mouse_moved = 0;
3202 last_mouse_scroll_bar = Qnil;
3204 GetCursorPos (&pt);
3206 /* Now we have a position on the root; find the innermost window
3207 containing the pointer. */
3209 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3210 && FRAME_LIVE_P (last_mouse_frame))
3212 /* If mouse was grabbed on a frame, give coords for that frame
3213 even if the mouse is now outside it. */
3214 f1 = last_mouse_frame;
3216 else
3218 /* Is window under mouse one of our frames? */
3219 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
3220 WindowFromPoint (pt));
3223 /* If not, is it one of our scroll bars? */
3224 if (! f1)
3226 struct scroll_bar *bar
3227 = x_window_to_scroll_bar (WindowFromPoint (pt));
3229 if (bar)
3231 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3235 if (f1 == 0 && insist > 0)
3236 f1 = SELECTED_FRAME ();
3238 if (f1)
3240 /* Ok, we found a frame. Store all the values.
3241 last_mouse_glyph is a rectangle used to reduce the
3242 generation of mouse events. To not miss any motion
3243 events, we must divide the frame into rectangles of the
3244 size of the smallest character that could be displayed
3245 on it, i.e. into the same rectangles that matrices on
3246 the frame are divided into. */
3248 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3249 remember_mouse_glyph (f1, pt.x, pt.y, &last_mouse_glyph);
3250 last_mouse_glyph_frame = f1;
3252 *bar_window = Qnil;
3253 *part = 0;
3254 *fp = f1;
3255 XSETINT (*x, pt.x);
3256 XSETINT (*y, pt.y);
3257 *time = last_mouse_movement_time;
3262 UNBLOCK_INPUT;
3266 /***********************************************************************
3267 Tool-bars
3268 ***********************************************************************/
3270 /* Handle mouse button event on the tool-bar of frame F, at
3271 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3272 or ButtonRelase. */
3274 static void
3275 w32_handle_tool_bar_click (f, button_event)
3276 struct frame *f;
3277 struct input_event *button_event;
3279 int x = XFASTINT (button_event->x);
3280 int y = XFASTINT (button_event->y);
3282 if (button_event->modifiers & down_modifier)
3283 handle_tool_bar_click (f, x, y, 1, 0);
3284 else
3285 handle_tool_bar_click (f, x, y, 0,
3286 button_event->modifiers & ~up_modifier);
3291 /***********************************************************************
3292 Scroll bars
3293 ***********************************************************************/
3295 /* Scroll bar support. */
3297 /* Given a window ID, find the struct scroll_bar which manages it.
3298 This can be called in GC, so we have to make sure to strip off mark
3299 bits. */
3301 static struct scroll_bar *
3302 x_window_to_scroll_bar (window_id)
3303 Window window_id;
3305 Lisp_Object tail;
3307 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3309 Lisp_Object frame, bar, condemned;
3311 frame = XCAR (tail);
3312 /* All elements of Vframe_list should be frames. */
3313 if (! FRAMEP (frame))
3314 abort ();
3316 /* Scan this frame's scroll bar list for a scroll bar with the
3317 right window ID. */
3318 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3319 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3320 /* This trick allows us to search both the ordinary and
3321 condemned scroll bar lists with one loop. */
3322 ! NILP (bar) || (bar = condemned,
3323 condemned = Qnil,
3324 ! NILP (bar));
3325 bar = XSCROLL_BAR (bar)->next)
3326 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3327 return XSCROLL_BAR (bar);
3330 return 0;
3335 /* Set the thumb size and position of scroll bar BAR. We are currently
3336 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3338 static void
3339 w32_set_scroll_bar_thumb (bar, portion, position, whole)
3340 struct scroll_bar *bar;
3341 int portion, position, whole;
3343 Window w = SCROLL_BAR_W32_WINDOW (bar);
3344 /* We use the whole scroll-bar height in the calculations below, to
3345 avoid strange effects like scrolling backwards when just clicking
3346 on the handle (without moving it). */
3347 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height))
3348 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3349 int sb_page, sb_pos;
3350 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3351 SCROLLINFO si;
3353 /* We used to change the nPage setting while dragging the handle,
3354 but that had very strange effects (such as scrolling backwards
3355 while dragging downwards).
3357 Now, we don't change the nPage setting while dragging unless we
3358 get near to the end of the buffer, in which case we often have to
3359 resize the handle to "go all the way". */
3361 if (draggingp)
3363 int near_bottom_p;
3364 BLOCK_INPUT;
3365 si.cbSize = sizeof (si);
3366 si.fMask = SIF_POS | SIF_PAGE;
3367 GetScrollInfo(w, SB_CTL, &si);
3368 near_bottom_p = si.nPos + si.nPage >= range;
3369 UNBLOCK_INPUT;
3370 if (!near_bottom_p)
3371 return;
3374 if (whole)
3376 /* Position scroll bar at rock bottom if the bottom of the
3377 buffer is visible. This avoids shinking the thumb away
3378 to nothing if it is held at the bottom of the buffer. */
3379 if (position + portion >= whole && !draggingp)
3381 sb_page = range * (whole - position) / whole;
3382 sb_pos = range;
3384 else
3386 sb_pos = position * range / whole;
3387 sb_page = (min (portion, (whole - position)) * range) / whole;
3390 else
3392 sb_page = range;
3393 sb_pos = 0;
3396 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3398 BLOCK_INPUT;
3400 si.cbSize = sizeof (si);
3401 si.fMask = SIF_PAGE | SIF_POS;
3402 si.nPage = sb_page;
3403 si.nPos = sb_pos;
3405 SetScrollInfo (w, SB_CTL, &si, TRUE);
3407 UNBLOCK_INPUT;
3411 /************************************************************************
3412 Scroll bars, general
3413 ************************************************************************/
3415 static HWND
3416 my_create_scrollbar (f, bar)
3417 struct frame * f;
3418 struct scroll_bar * bar;
3420 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3421 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3422 (LPARAM) bar);
3425 /*#define ATTACH_THREADS*/
3427 static BOOL
3428 my_show_window (FRAME_PTR f, HWND hwnd, int how)
3430 #ifndef ATTACH_THREADS
3431 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3432 (WPARAM) hwnd, (LPARAM) how);
3433 #else
3434 return ShowWindow (hwnd, how);
3435 #endif
3438 static void
3439 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3440 int x, int y, int cx, int cy, UINT flags)
3442 #ifndef ATTACH_THREADS
3443 WINDOWPOS pos;
3444 pos.hwndInsertAfter = hwndAfter;
3445 pos.x = x;
3446 pos.y = y;
3447 pos.cx = cx;
3448 pos.cy = cy;
3449 pos.flags = flags;
3450 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3451 #else
3452 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3453 #endif
3456 static void
3457 my_set_focus (f, hwnd)
3458 struct frame * f;
3459 HWND hwnd;
3461 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3462 (WPARAM) hwnd, 0);
3465 static void
3466 my_set_foreground_window (hwnd)
3467 HWND hwnd;
3469 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3473 static void
3474 my_destroy_window (f, hwnd)
3475 struct frame * f;
3476 HWND hwnd;
3478 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3479 (WPARAM) hwnd, 0);
3482 /* Create a scroll bar and return the scroll bar vector for it. W is
3483 the Emacs window on which to create the scroll bar. TOP, LEFT,
3484 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
3485 scroll bar. */
3487 static struct scroll_bar *
3488 x_scroll_bar_create (w, top, left, width, height)
3489 struct window *w;
3490 int top, left, width, height;
3492 struct frame *f = XFRAME (WINDOW_FRAME (w));
3493 HWND hwnd;
3494 SCROLLINFO si;
3495 struct scroll_bar *bar
3496 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3498 BLOCK_INPUT;
3500 XSETWINDOW (bar->window, w);
3501 XSETINT (bar->top, top);
3502 XSETINT (bar->left, left);
3503 XSETINT (bar->width, width);
3504 XSETINT (bar->height, height);
3505 XSETINT (bar->start, 0);
3506 XSETINT (bar->end, 0);
3507 bar->dragging = Qnil;
3508 bar->fringe_extended_p = Qnil;
3510 /* Requires geometry to be set before call to create the real window */
3512 hwnd = my_create_scrollbar (f, bar);
3514 si.cbSize = sizeof (si);
3515 si.fMask = SIF_ALL;
3516 si.nMin = 0;
3517 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3518 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3519 si.nPage = si.nMax;
3520 si.nPos = 0;
3522 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3524 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3526 /* Add bar to its frame's list of scroll bars. */
3527 bar->next = FRAME_SCROLL_BARS (f);
3528 bar->prev = Qnil;
3529 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3530 if (! NILP (bar->next))
3531 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3533 UNBLOCK_INPUT;
3535 return bar;
3539 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3540 nil. */
3542 static void
3543 x_scroll_bar_remove (bar)
3544 struct scroll_bar *bar;
3546 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3548 BLOCK_INPUT;
3550 /* Destroy the window. */
3551 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3553 /* Disassociate this scroll bar from its window. */
3554 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3556 UNBLOCK_INPUT;
3559 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3560 that we are displaying PORTION characters out of a total of WHOLE
3561 characters, starting at POSITION. If WINDOW has no scroll bar,
3562 create one. */
3563 static void
3564 w32_set_vertical_scroll_bar (w, portion, whole, position)
3565 struct window *w;
3566 int portion, whole, position;
3568 struct frame *f = XFRAME (w->frame);
3569 struct scroll_bar *bar;
3570 int top, height, left, sb_left, width, sb_width;
3571 int window_y, window_height;
3572 int fringe_extended_p;
3574 /* Get window dimensions. */
3575 window_box (w, -1, 0, &window_y, 0, &window_height);
3576 top = window_y;
3577 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3578 height = window_height;
3580 /* Compute the left edge of the scroll bar area. */
3581 left = WINDOW_SCROLL_BAR_AREA_X (w);
3583 /* Compute the width of the scroll bar which might be less than
3584 the width of the area reserved for the scroll bar. */
3585 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3586 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3587 else
3588 sb_width = width;
3590 /* Compute the left edge of the scroll bar. */
3591 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3592 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3593 else
3594 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3596 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3597 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
3598 && WINDOW_LEFT_FRINGE_WIDTH (w)
3599 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3600 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
3601 else
3602 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
3603 && WINDOW_RIGHT_FRINGE_WIDTH (w)
3604 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3605 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
3607 /* Does the scroll bar exist yet? */
3608 if (NILP (w->vertical_scroll_bar))
3610 HDC hdc;
3611 BLOCK_INPUT;
3612 if (width > 0 && height > 0)
3614 hdc = get_frame_dc (f);
3615 if (fringe_extended_p)
3616 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3617 else
3618 w32_clear_area (f, hdc, left, top, width, height);
3619 release_frame_dc (f, hdc);
3621 UNBLOCK_INPUT;
3623 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3625 else
3627 /* It may just need to be moved and resized. */
3628 HWND hwnd;
3630 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3631 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3633 /* If already correctly positioned, do nothing. */
3634 if ( XINT (bar->left) == sb_left
3635 && XINT (bar->top) == top
3636 && XINT (bar->width) == sb_width
3637 && XINT (bar->height) == height
3638 && !NILP (bar->fringe_extended_p) == fringe_extended_p )
3640 /* Redraw after clear_frame. */
3641 if (!my_show_window (f, hwnd, SW_NORMAL))
3642 InvalidateRect (hwnd, NULL, FALSE);
3644 else
3646 HDC hdc;
3647 SCROLLINFO si;
3649 BLOCK_INPUT;
3650 if (width && height)
3652 hdc = get_frame_dc (f);
3653 /* Since Windows scroll bars are smaller than the space reserved
3654 for them on the frame, we have to clear "under" them. */
3655 if (fringe_extended_p)
3656 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3657 else
3658 w32_clear_area (f, hdc, left, top, width, height);
3659 release_frame_dc (f, hdc);
3661 /* Make sure scroll bar is "visible" before moving, to ensure the
3662 area of the parent window now exposed will be refreshed. */
3663 my_show_window (f, hwnd, SW_HIDE);
3664 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3665 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3666 max (height, 1), TRUE);
3668 si.cbSize = sizeof (si);
3669 si.fMask = SIF_RANGE;
3670 si.nMin = 0;
3671 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3672 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3674 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3676 my_show_window (f, hwnd, SW_NORMAL);
3677 /* InvalidateRect (w, NULL, FALSE); */
3679 /* Remember new settings. */
3680 XSETINT (bar->left, sb_left);
3681 XSETINT (bar->top, top);
3682 XSETINT (bar->width, sb_width);
3683 XSETINT (bar->height, height);
3685 UNBLOCK_INPUT;
3688 bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
3690 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3692 XSETVECTOR (w->vertical_scroll_bar, bar);
3696 /* The following three hooks are used when we're doing a thorough
3697 redisplay of the frame. We don't explicitly know which scroll bars
3698 are going to be deleted, because keeping track of when windows go
3699 away is a real pain - "Can you say set-window-configuration, boys
3700 and girls?" Instead, we just assert at the beginning of redisplay
3701 that *all* scroll bars are to be removed, and then save a scroll bar
3702 from the fiery pit when we actually redisplay its window. */
3704 /* Arrange for all scroll bars on FRAME to be removed at the next call
3705 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3706 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3708 static void
3709 w32_condemn_scroll_bars (frame)
3710 FRAME_PTR frame;
3712 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3713 while (! NILP (FRAME_SCROLL_BARS (frame)))
3715 Lisp_Object bar;
3716 bar = FRAME_SCROLL_BARS (frame);
3717 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
3718 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3719 XSCROLL_BAR (bar)->prev = Qnil;
3720 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3721 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3722 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
3727 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3728 Note that WINDOW isn't necessarily condemned at all. */
3730 static void
3731 w32_redeem_scroll_bar (window)
3732 struct window *window;
3734 struct scroll_bar *bar;
3735 struct frame *f;
3737 /* We can't redeem this window's scroll bar if it doesn't have one. */
3738 if (NILP (window->vertical_scroll_bar))
3739 abort ();
3741 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3743 /* Unlink it from the condemned list. */
3744 f = XFRAME (WINDOW_FRAME (window));
3745 if (NILP (bar->prev))
3747 /* If the prev pointer is nil, it must be the first in one of
3748 the lists. */
3749 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3750 /* It's not condemned. Everything's fine. */
3751 return;
3752 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3753 window->vertical_scroll_bar))
3754 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
3755 else
3756 /* If its prev pointer is nil, it must be at the front of
3757 one or the other! */
3758 abort ();
3760 else
3761 XSCROLL_BAR (bar->prev)->next = bar->next;
3763 if (! NILP (bar->next))
3764 XSCROLL_BAR (bar->next)->prev = bar->prev;
3766 bar->next = FRAME_SCROLL_BARS (f);
3767 bar->prev = Qnil;
3768 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3769 if (! NILP (bar->next))
3770 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3773 /* Remove all scroll bars on FRAME that haven't been saved since the
3774 last call to `*condemn_scroll_bars_hook'. */
3776 static void
3777 w32_judge_scroll_bars (f)
3778 FRAME_PTR f;
3780 Lisp_Object bar, next;
3782 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3784 /* Clear out the condemned list now so we won't try to process any
3785 more events on the hapless scroll bars. */
3786 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3788 for (; ! NILP (bar); bar = next)
3790 struct scroll_bar *b = XSCROLL_BAR (bar);
3792 x_scroll_bar_remove (b);
3794 next = b->next;
3795 b->next = b->prev = Qnil;
3798 /* Now there should be no references to the condemned scroll bars,
3799 and they should get garbage-collected. */
3802 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3803 is set to something other than NO_EVENT, it is enqueued.
3805 This may be called from a signal handler, so we have to ignore GC
3806 mark bits. */
3808 static int
3809 w32_scroll_bar_handle_click (bar, msg, emacs_event)
3810 struct scroll_bar *bar;
3811 W32Msg *msg;
3812 struct input_event *emacs_event;
3814 if (! WINDOWP (bar->window))
3815 abort ();
3817 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
3818 emacs_event->code = 0;
3819 /* not really meaningful to distinguish up/down */
3820 emacs_event->modifiers = msg->dwModifiers;
3821 emacs_event->frame_or_window = bar->window;
3822 emacs_event->arg = Qnil;
3823 emacs_event->timestamp = msg->msg.time;
3826 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3827 int y;
3828 int dragging = !NILP (bar->dragging);
3829 SCROLLINFO si;
3831 si.cbSize = sizeof (si);
3832 si.fMask = SIF_POS;
3834 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
3835 y = si.nPos;
3837 bar->dragging = Qnil;
3840 last_mouse_scroll_bar_pos = msg->msg.wParam;
3842 switch (LOWORD (msg->msg.wParam))
3844 case SB_LINEDOWN:
3845 emacs_event->part = scroll_bar_down_arrow;
3846 break;
3847 case SB_LINEUP:
3848 emacs_event->part = scroll_bar_up_arrow;
3849 break;
3850 case SB_PAGEUP:
3851 emacs_event->part = scroll_bar_above_handle;
3852 break;
3853 case SB_PAGEDOWN:
3854 emacs_event->part = scroll_bar_below_handle;
3855 break;
3856 case SB_TOP:
3857 emacs_event->part = scroll_bar_handle;
3858 y = 0;
3859 break;
3860 case SB_BOTTOM:
3861 emacs_event->part = scroll_bar_handle;
3862 y = top_range;
3863 break;
3864 case SB_THUMBTRACK:
3865 case SB_THUMBPOSITION:
3866 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3867 y = HIWORD (msg->msg.wParam);
3868 bar->dragging = Qt;
3869 emacs_event->part = scroll_bar_handle;
3871 /* "Silently" update current position. */
3873 SCROLLINFO si;
3875 si.cbSize = sizeof (si);
3876 si.fMask = SIF_POS;
3877 si.nPos = y;
3878 /* Remember apparent position (we actually lag behind the real
3879 position, so don't set that directly. */
3880 last_scroll_bar_drag_pos = y;
3882 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
3884 break;
3885 case SB_ENDSCROLL:
3886 /* If this is the end of a drag sequence, then reset the scroll
3887 handle size to normal and do a final redraw. Otherwise do
3888 nothing. */
3889 if (dragging)
3891 SCROLLINFO si;
3892 int start = XINT (bar->start);
3893 int end = XINT (bar->end);
3895 si.cbSize = sizeof (si);
3896 si.fMask = SIF_PAGE | SIF_POS;
3897 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3898 si.nPos = last_scroll_bar_drag_pos;
3899 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
3901 /* fall through */
3902 default:
3903 emacs_event->kind = NO_EVENT;
3904 return FALSE;
3907 XSETINT (emacs_event->x, y);
3908 XSETINT (emacs_event->y, top_range);
3910 return TRUE;
3914 /* Return information to the user about the current position of the mouse
3915 on the scroll bar. */
3917 static void
3918 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
3919 FRAME_PTR *fp;
3920 Lisp_Object *bar_window;
3921 enum scroll_bar_part *part;
3922 Lisp_Object *x, *y;
3923 unsigned long *time;
3925 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
3926 Window w = SCROLL_BAR_W32_WINDOW (bar);
3927 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3928 int pos;
3929 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3930 SCROLLINFO si;
3932 BLOCK_INPUT;
3934 *fp = f;
3935 *bar_window = bar->window;
3937 si.cbSize = sizeof (si);
3938 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
3940 GetScrollInfo (w, SB_CTL, &si);
3941 pos = si.nPos;
3942 top_range = si.nMax - si.nPage + 1;
3944 switch (LOWORD (last_mouse_scroll_bar_pos))
3946 case SB_THUMBPOSITION:
3947 case SB_THUMBTRACK:
3948 *part = scroll_bar_handle;
3949 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3950 pos = HIWORD (last_mouse_scroll_bar_pos);
3951 break;
3952 case SB_LINEDOWN:
3953 *part = scroll_bar_handle;
3954 pos++;
3955 break;
3956 default:
3957 *part = scroll_bar_handle;
3958 break;
3961 XSETINT (*x, pos);
3962 XSETINT (*y, top_range);
3964 f->mouse_moved = 0;
3965 last_mouse_scroll_bar = Qnil;
3967 *time = last_mouse_movement_time;
3969 UNBLOCK_INPUT;
3973 /* The screen has been cleared so we may have changed foreground or
3974 background colors, and the scroll bars may need to be redrawn.
3975 Clear out the scroll bars, and ask for expose events, so we can
3976 redraw them. */
3978 void
3979 x_scroll_bar_clear (f)
3980 FRAME_PTR f;
3982 Lisp_Object bar;
3984 /* We can have scroll bars even if this is 0,
3985 if we just turned off scroll bar mode.
3986 But in that case we should not clear them. */
3987 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3988 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
3989 bar = XSCROLL_BAR (bar)->next)
3991 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
3992 HDC hdc = GetDC (window);
3993 RECT rect;
3995 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
3996 arranges to refresh the scroll bar if hidden. */
3997 my_show_window (f, window, SW_HIDE);
3999 GetClientRect (window, &rect);
4000 select_palette (f, hdc);
4001 w32_clear_rect (f, hdc, &rect);
4002 deselect_palette (f, hdc);
4004 ReleaseDC (window, hdc);
4009 /* The main W32 event-reading loop - w32_read_socket. */
4011 /* Record the last 100 characters stored
4012 to help debug the loss-of-chars-during-GC problem. */
4014 static int temp_index;
4015 static short temp_buffer[100];
4017 /* Temporarily store lead byte of DBCS input sequences. */
4018 static char dbcs_lead = 0;
4020 /* Read events coming from the W32 shell.
4021 This routine is called by the SIGIO handler.
4022 We return as soon as there are no more events to be read.
4024 We return the number of characters stored into the buffer,
4025 thus pretending to be `read'.
4027 EXPECTED is nonzero if the caller knows input is available.
4029 Some of these messages are reposted back to the message queue since the
4030 system calls the windows proc directly in a context where we cannot return
4031 the data nor can we guarantee the state we are in. So if we dispatch them
4032 we will get into an infinite loop. To prevent this from ever happening we
4033 will set a variable to indicate we are in the read_socket call and indicate
4034 which message we are processing since the windows proc gets called
4035 recursively with different messages by the system.
4039 w32_read_socket (sd, expected, hold_quit)
4040 register int sd;
4041 int expected;
4042 struct input_event *hold_quit;
4044 int count = 0;
4045 int check_visibility = 0;
4046 W32Msg msg;
4047 struct frame *f;
4048 struct w32_display_info *dpyinfo = &one_w32_display_info;
4050 if (interrupt_input_blocked)
4052 interrupt_input_pending = 1;
4053 return -1;
4056 interrupt_input_pending = 0;
4057 BLOCK_INPUT;
4059 /* So people can tell when we have read the available input. */
4060 input_signal_count++;
4062 /* TODO: ghostscript integration. */
4063 while (get_next_msg (&msg, FALSE))
4065 struct input_event inev;
4066 int do_help = 0;
4068 EVENT_INIT (inev);
4069 inev.kind = NO_EVENT;
4070 inev.arg = Qnil;
4072 switch (msg.msg.message)
4074 case WM_PAINT:
4075 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4077 if (f)
4079 if (msg.rect.right == msg.rect.left ||
4080 msg.rect.bottom == msg.rect.top)
4082 /* We may get paint messages even though the client
4083 area is clipped - these are not expose events. */
4084 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4085 SDATA (f->name)));
4087 else if (f->async_visible != 1)
4089 /* Definitely not obscured, so mark as visible. */
4090 f->async_visible = 1;
4091 f->async_iconified = 0;
4092 SET_FRAME_GARBAGED (f);
4093 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4094 SDATA (f->name)));
4096 /* WM_PAINT serves as MapNotify as well, so report
4097 visibility changes properly. */
4098 if (f->iconified)
4100 inev.kind = DEICONIFY_EVENT;
4101 XSETFRAME (inev.frame_or_window, f);
4103 else if (! NILP (Vframe_list)
4104 && ! NILP (XCDR (Vframe_list)))
4105 /* Force a redisplay sooner or later to update the
4106 frame titles in case this is the second frame. */
4107 record_asynch_buffer_change ();
4109 else
4111 HDC hdc = get_frame_dc (f);
4113 /* Erase background again for safety. */
4114 w32_clear_rect (f, hdc, &msg.rect);
4115 release_frame_dc (f, hdc);
4116 expose_frame (f,
4117 msg.rect.left,
4118 msg.rect.top,
4119 msg.rect.right - msg.rect.left,
4120 msg.rect.bottom - msg.rect.top);
4123 break;
4125 case WM_INPUTLANGCHANGE:
4126 /* Generate a language change event. */
4127 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4129 if (f)
4131 inev.kind = LANGUAGE_CHANGE_EVENT;
4132 XSETFRAME (inev.frame_or_window, f);
4133 inev.code = msg.msg.wParam;
4134 inev.modifiers = msg.msg.lParam & 0xffff;
4136 break;
4138 case WM_KEYDOWN:
4139 case WM_SYSKEYDOWN:
4140 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4142 if (f && !f->iconified)
4144 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4145 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4147 clear_mouse_face (dpyinfo);
4148 dpyinfo->mouse_face_hidden = 1;
4151 if (temp_index == sizeof temp_buffer / sizeof (short))
4152 temp_index = 0;
4153 temp_buffer[temp_index++] = msg.msg.wParam;
4154 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4155 inev.code = msg.msg.wParam;
4156 inev.modifiers = msg.dwModifiers;
4157 XSETFRAME (inev.frame_or_window, f);
4158 inev.timestamp = msg.msg.time;
4160 break;
4162 case WM_UNICHAR:
4163 case WM_SYSCHAR:
4164 case WM_CHAR:
4165 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4167 if (f && !f->iconified)
4169 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4170 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4172 clear_mouse_face (dpyinfo);
4173 dpyinfo->mouse_face_hidden = 1;
4176 if (temp_index == sizeof temp_buffer / sizeof (short))
4177 temp_index = 0;
4178 temp_buffer[temp_index++] = msg.msg.wParam;
4180 inev.modifiers = msg.dwModifiers;
4181 XSETFRAME (inev.frame_or_window, f);
4182 inev.timestamp = msg.msg.time;
4184 if (msg.msg.message == WM_UNICHAR)
4186 inev.code = msg.msg.wParam;
4188 else if (msg.msg.wParam < 256)
4190 wchar_t code;
4191 char dbcs[2];
4192 dbcs[0] = 0;
4193 dbcs[1] = (char) msg.msg.wParam;
4195 if (dbcs_lead)
4197 dbcs[0] = dbcs_lead;
4198 dbcs_lead = 0;
4199 if (!MultiByteToWideChar (CP_ACP, 0, dbcs, 2, &code, 1))
4201 /* Garbage */
4202 DebPrint (("Invalid DBCS sequence: %d %d\n",
4203 dbcs[0], dbcs[1]));
4204 inev.kind = NO_EVENT;
4205 break;
4208 else if (IsDBCSLeadByteEx (CP_ACP, (BYTE) msg.msg.wParam))
4210 dbcs_lead = (char) msg.msg.wParam;
4211 inev.kind = NO_EVENT;
4212 break;
4214 else
4216 if (!MultiByteToWideChar (CP_ACP, 0, &dbcs[1], 1,
4217 &code, 1))
4219 /* What to do with garbage? */
4220 DebPrint (("Invalid character: %d\n", dbcs[1]));
4221 inev.kind = NO_EVENT;
4222 break;
4225 inev.code = code;
4227 else
4229 /* Windows shouldn't generate WM_CHAR events above 0xFF
4230 in non-Unicode message handlers. */
4231 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4232 inev.kind = NO_EVENT;
4233 break;
4235 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4236 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4238 break;
4240 case WM_APPCOMMAND:
4241 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4243 if (f && !f->iconified)
4245 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4246 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4248 clear_mouse_face (dpyinfo);
4249 dpyinfo->mouse_face_hidden = 1;
4252 if (temp_index == sizeof temp_buffer / sizeof (short))
4253 temp_index = 0;
4254 temp_buffer[temp_index++] = msg.msg.wParam;
4255 inev.kind = MULTIMEDIA_KEY_EVENT;
4256 inev.code = GET_APPCOMMAND_LPARAM(msg.msg.lParam);
4257 inev.modifiers = msg.dwModifiers;
4258 XSETFRAME (inev.frame_or_window, f);
4259 inev.timestamp = msg.msg.time;
4261 break;
4263 case WM_MOUSEMOVE:
4264 /* Ignore non-movement. */
4266 int x = LOWORD (msg.msg.lParam);
4267 int y = HIWORD (msg.msg.lParam);
4268 if (x == last_mousemove_x && y == last_mousemove_y)
4269 break;
4270 last_mousemove_x = x;
4271 last_mousemove_y = y;
4274 previous_help_echo_string = help_echo_string;
4275 help_echo_string = Qnil;
4277 if (dpyinfo->grabbed && last_mouse_frame
4278 && FRAME_LIVE_P (last_mouse_frame))
4279 f = last_mouse_frame;
4280 else
4281 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4283 if (dpyinfo->mouse_face_hidden)
4285 dpyinfo->mouse_face_hidden = 0;
4286 clear_mouse_face (dpyinfo);
4289 if (f)
4291 /* Generate SELECT_WINDOW_EVENTs when needed. */
4292 if (!NILP (Vmouse_autoselect_window))
4294 Lisp_Object window;
4295 int x = LOWORD (msg.msg.lParam);
4296 int y = HIWORD (msg.msg.lParam);
4298 window = window_from_coordinates (f, x, y, 0, 0, 0, 0);
4300 /* Window will be selected only when it is not
4301 selected now and last mouse movement event was
4302 not in it. Minibuffer window will be selected
4303 only when it is active. */
4304 if (WINDOWP(window)
4305 && !EQ (window, last_window)
4306 && !EQ (window, selected_window)
4307 /* For click-to-focus window managers
4308 create event iff we don't leave the
4309 selected frame. */
4310 && (focus_follows_mouse
4311 || (EQ (XWINDOW (window)->frame,
4312 XWINDOW (selected_window)->frame))))
4314 inev.kind = SELECT_WINDOW_EVENT;
4315 inev.frame_or_window = window;
4318 last_window=window;
4320 if (!note_mouse_movement (f, &msg.msg))
4321 help_echo_string = previous_help_echo_string;
4323 else
4325 /* If we move outside the frame, then we're
4326 certainly no longer on any text in the frame. */
4327 clear_mouse_face (dpyinfo);
4330 /* If the contents of the global variable help_echo_string
4331 has changed, generate a HELP_EVENT. */
4332 #if 0 /* The below is an invalid comparison when USE_LISP_UNION_TYPE.
4333 But it was originally changed to this to fix a bug, so I have
4334 not removed it completely in case the bug is still there. */
4335 if (help_echo_string != previous_help_echo_string ||
4336 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4337 #else /* This is what xterm.c does. */
4338 if (!NILP (help_echo_string)
4339 || !NILP (previous_help_echo_string))
4340 do_help = 1;
4341 #endif
4342 break;
4344 case WM_LBUTTONDOWN:
4345 case WM_LBUTTONUP:
4346 case WM_MBUTTONDOWN:
4347 case WM_MBUTTONUP:
4348 case WM_RBUTTONDOWN:
4349 case WM_RBUTTONUP:
4350 case WM_XBUTTONDOWN:
4351 case WM_XBUTTONUP:
4353 /* If we decide we want to generate an event to be seen
4354 by the rest of Emacs, we put it here. */
4355 int tool_bar_p = 0;
4356 int button;
4357 int up;
4359 if (dpyinfo->grabbed && last_mouse_frame
4360 && FRAME_LIVE_P (last_mouse_frame))
4361 f = last_mouse_frame;
4362 else
4363 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4365 if (f)
4367 construct_mouse_click (&inev, &msg, f);
4369 /* Is this in the tool-bar? */
4370 if (WINDOWP (f->tool_bar_window)
4371 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4373 Lisp_Object window;
4374 int x = XFASTINT (inev.x);
4375 int y = XFASTINT (inev.y);
4377 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
4379 if (EQ (window, f->tool_bar_window))
4381 w32_handle_tool_bar_click (f, &inev);
4382 tool_bar_p = 1;
4386 if (tool_bar_p
4387 || (dpyinfo->w32_focus_frame
4388 && f != dpyinfo->w32_focus_frame))
4389 inev.kind = NO_EVENT;
4392 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4393 &button, &up);
4395 if (up)
4397 dpyinfo->grabbed &= ~ (1 << button);
4399 else
4401 dpyinfo->grabbed |= (1 << button);
4402 last_mouse_frame = f;
4403 /* Ignore any mouse motion that happened
4404 before this event; any subsequent mouse-movement
4405 Emacs events should reflect only motion after
4406 the ButtonPress. */
4407 if (f != 0)
4408 f->mouse_moved = 0;
4410 if (!tool_bar_p)
4411 last_tool_bar_item = -1;
4413 break;
4416 case WM_MOUSEWHEEL:
4417 case WM_MOUSEHWHEEL:
4419 if (dpyinfo->grabbed && last_mouse_frame
4420 && FRAME_LIVE_P (last_mouse_frame))
4421 f = last_mouse_frame;
4422 else
4423 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4425 if (f)
4428 if (!dpyinfo->w32_focus_frame
4429 || f == dpyinfo->w32_focus_frame)
4431 /* Emit an Emacs wheel-up/down event. */
4432 construct_mouse_wheel (&inev, &msg, f);
4434 /* Ignore any mouse motion that happened before this
4435 event; any subsequent mouse-movement Emacs events
4436 should reflect only motion after the
4437 ButtonPress. */
4438 f->mouse_moved = 0;
4440 last_mouse_frame = f;
4441 last_tool_bar_item = -1;
4443 break;
4445 case WM_DROPFILES:
4446 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4448 if (f)
4449 construct_drag_n_drop (&inev, &msg, f);
4450 break;
4452 case WM_VSCROLL:
4454 struct scroll_bar *bar =
4455 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4457 if (bar)
4458 w32_scroll_bar_handle_click (bar, &msg, &inev);
4459 break;
4462 case WM_WINDOWPOSCHANGED:
4463 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4464 if (f)
4466 if (f->want_fullscreen & FULLSCREEN_WAIT)
4467 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
4469 check_visibility = 1;
4470 break;
4472 case WM_ACTIVATE:
4473 case WM_ACTIVATEAPP:
4474 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4475 if (f)
4476 x_check_fullscreen (f);
4477 check_visibility = 1;
4478 break;
4480 case WM_MOVE:
4481 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4483 if (f && !f->async_iconified)
4485 int x, y;
4487 x_real_positions (f, &x, &y);
4488 f->left_pos = x;
4489 f->top_pos = y;
4492 check_visibility = 1;
4493 break;
4495 case WM_SHOWWINDOW:
4496 /* wParam non-zero means Window is about to be shown, 0 means
4497 about to be hidden. */
4498 /* Redo the mouse-highlight after the tooltip has gone. */
4499 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4501 tip_window = NULL;
4502 redo_mouse_highlight ();
4505 /* If window has been obscured or exposed by another window
4506 being maximised or minimised/restored, then recheck
4507 visibility of all frames. Direct changes to our own
4508 windows get handled by WM_SIZE. */
4509 #if 0
4510 if (msg.msg.lParam != 0)
4511 check_visibility = 1;
4512 else
4514 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4515 f->async_visible = msg.msg.wParam;
4517 #endif
4519 check_visibility = 1;
4520 break;
4522 case WM_SIZE:
4523 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4525 /* Inform lisp of whether frame has been iconified etc. */
4526 if (f)
4528 switch (msg.msg.wParam)
4530 case SIZE_MINIMIZED:
4531 f->async_visible = 0;
4532 f->async_iconified = 1;
4534 inev.kind = ICONIFY_EVENT;
4535 XSETFRAME (inev.frame_or_window, f);
4536 break;
4538 case SIZE_MAXIMIZED:
4539 case SIZE_RESTORED:
4540 f->async_visible = 1;
4541 f->async_iconified = 0;
4543 /* wait_reading_process_output will notice this and update
4544 the frame's display structures. */
4545 SET_FRAME_GARBAGED (f);
4547 if (f->iconified)
4549 int x, y;
4551 /* Reset top and left positions of the Window
4552 here since Windows sends a WM_MOVE message
4553 BEFORE telling us the Window is minimized
4554 when the Window is iconified, with 3000,3000
4555 as the co-ords. */
4556 x_real_positions (f, &x, &y);
4557 f->left_pos = x;
4558 f->top_pos = y;
4560 inev.kind = DEICONIFY_EVENT;
4561 XSETFRAME (inev.frame_or_window, f);
4563 else if (! NILP (Vframe_list)
4564 && ! NILP (XCDR (Vframe_list)))
4565 /* Force a redisplay sooner or later
4566 to update the frame titles
4567 in case this is the second frame. */
4568 record_asynch_buffer_change ();
4569 break;
4573 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
4575 RECT rect;
4576 int rows;
4577 int columns;
4578 int width;
4579 int height;
4581 GetClientRect (msg.msg.hwnd, &rect);
4583 height = rect.bottom - rect.top;
4584 width = rect.right - rect.left;
4586 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4587 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4589 /* TODO: Clip size to the screen dimensions. */
4591 /* Even if the number of character rows and columns has
4592 not changed, the font size may have changed, so we need
4593 to check the pixel dimensions as well. */
4595 if (columns != FRAME_COLS (f)
4596 || rows != FRAME_LINES (f)
4597 || width != FRAME_PIXEL_WIDTH (f)
4598 || height != FRAME_PIXEL_HEIGHT (f))
4600 change_frame_size (f, rows, columns, 0, 1, 0);
4601 SET_FRAME_GARBAGED (f);
4602 cancel_mouse_face (f);
4603 FRAME_PIXEL_WIDTH (f) = width;
4604 FRAME_PIXEL_HEIGHT (f) = height;
4605 f->win_gravity = NorthWestGravity;
4609 check_visibility = 1;
4610 break;
4612 case WM_MOUSELEAVE:
4613 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4614 if (f)
4616 if (f == dpyinfo->mouse_face_mouse_frame)
4618 /* If we move outside the frame, then we're
4619 certainly no longer on any text in the frame. */
4620 clear_mouse_face (dpyinfo);
4621 dpyinfo->mouse_face_mouse_frame = 0;
4624 /* Generate a nil HELP_EVENT to cancel a help-echo.
4625 Do it only if there's something to cancel.
4626 Otherwise, the startup message is cleared when
4627 the mouse leaves the frame. */
4628 if (any_help_event_p)
4629 do_help = -1;
4631 break;
4633 case WM_SETFOCUS:
4634 w32_detect_focus_change (dpyinfo, &msg, &inev);
4636 dpyinfo->grabbed = 0;
4637 check_visibility = 1;
4638 break;
4640 case WM_KILLFOCUS:
4641 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4643 if (f)
4645 if (f == dpyinfo->w32_focus_event_frame)
4646 dpyinfo->w32_focus_event_frame = 0;
4648 if (f == dpyinfo->w32_focus_frame)
4649 x_new_focus_frame (dpyinfo, 0);
4651 if (f == dpyinfo->mouse_face_mouse_frame)
4653 /* If we move outside the frame, then we're
4654 certainly no longer on any text in the frame. */
4655 clear_mouse_face (dpyinfo);
4656 dpyinfo->mouse_face_mouse_frame = 0;
4659 /* Generate a nil HELP_EVENT to cancel a help-echo.
4660 Do it only if there's something to cancel.
4661 Otherwise, the startup message is cleared when
4662 the mouse leaves the frame. */
4663 if (any_help_event_p)
4664 do_help = -1;
4667 dpyinfo->grabbed = 0;
4668 check_visibility = 1;
4669 break;
4671 case WM_CLOSE:
4672 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4674 if (f)
4676 inev.kind = DELETE_WINDOW_EVENT;
4677 XSETFRAME (inev.frame_or_window, f);
4679 break;
4681 case WM_INITMENU:
4682 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4684 if (f)
4686 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4687 XSETFRAME (inev.frame_or_window, f);
4689 break;
4691 case WM_COMMAND:
4692 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4694 if (f)
4696 extern void menubar_selection_callback
4697 (FRAME_PTR f, void * client_data);
4698 menubar_selection_callback (f, (void *)msg.msg.wParam);
4701 check_visibility = 1;
4702 break;
4704 case WM_DISPLAYCHANGE:
4705 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4707 if (f)
4709 dpyinfo->n_cbits = msg.msg.wParam;
4710 DebPrint (("display change: %d %d\n",
4711 (short) LOWORD (msg.msg.lParam),
4712 (short) HIWORD (msg.msg.lParam)));
4715 check_visibility = 1;
4716 break;
4718 default:
4719 /* Check for messages registered at runtime. */
4720 if (msg.msg.message == msh_mousewheel)
4722 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4723 msg.msg.message = WM_MOUSEWHEEL;
4724 prepend_msg (&msg);
4726 break;
4729 if (inev.kind != NO_EVENT)
4731 kbd_buffer_store_event_hold (&inev, hold_quit);
4732 count++;
4735 if (do_help
4736 && !(hold_quit && hold_quit->kind != NO_EVENT))
4738 Lisp_Object frame;
4740 if (f)
4741 XSETFRAME (frame, f);
4742 else
4743 frame = Qnil;
4745 if (do_help > 0)
4747 if (NILP (help_echo_string))
4749 help_echo_object = help_echo_window = Qnil;
4750 help_echo_pos = -1;
4753 any_help_event_p = 1;
4754 gen_help_event (help_echo_string, frame, help_echo_window,
4755 help_echo_object, help_echo_pos);
4757 else
4759 help_echo_string = Qnil;
4760 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4762 count++;
4766 /* If the focus was just given to an autoraising frame,
4767 raise it now. */
4768 /* ??? This ought to be able to handle more than one such frame. */
4769 if (pending_autoraise_frame)
4771 x_raise_frame (pending_autoraise_frame);
4772 pending_autoraise_frame = 0;
4775 /* Check which frames are still visisble, if we have enqueued any user
4776 events or been notified of events that may affect visibility. We
4777 do this here because there doesn't seem to be any direct
4778 notification from Windows that the visibility of a window has
4779 changed (at least, not in all cases). */
4780 if (count > 0 || check_visibility)
4782 Lisp_Object tail, frame;
4784 FOR_EACH_FRAME (tail, frame)
4786 FRAME_PTR f = XFRAME (frame);
4787 /* The tooltip has been drawn already. Avoid the
4788 SET_FRAME_GARBAGED below. */
4789 if (EQ (frame, tip_frame))
4790 continue;
4792 /* Check "visible" frames and mark each as obscured or not.
4793 Note that async_visible is nonzero for unobscured and
4794 obscured frames, but zero for hidden and iconified frames. */
4795 if (FRAME_W32_P (f) && f->async_visible)
4797 RECT clipbox;
4798 HDC hdc;
4800 enter_crit ();
4801 /* Query clipping rectangle for the entire window area
4802 (GetWindowDC), not just the client portion (GetDC).
4803 Otherwise, the scrollbars and menubar aren't counted as
4804 part of the visible area of the frame, and we may think
4805 the frame is obscured when really a scrollbar is still
4806 visible and gets WM_PAINT messages above. */
4807 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
4808 GetClipBox (hdc, &clipbox);
4809 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
4810 leave_crit ();
4812 if (clipbox.right == clipbox.left
4813 || clipbox.bottom == clipbox.top)
4815 /* Frame has become completely obscured so mark as
4816 such (we do this by setting async_visible to 2 so
4817 that FRAME_VISIBLE_P is still true, but redisplay
4818 will skip it). */
4819 f->async_visible = 2;
4821 if (!FRAME_OBSCURED_P (f))
4823 DebPrint (("frame %p (%s) obscured\n", f,
4824 SDATA (f->name)));
4827 else
4829 /* Frame is not obscured, so mark it as such. */
4830 f->async_visible = 1;
4832 if (FRAME_OBSCURED_P (f))
4834 SET_FRAME_GARBAGED (f);
4835 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
4836 SDATA (f->name)));
4838 /* Force a redisplay sooner or later. */
4839 record_asynch_buffer_change ();
4846 UNBLOCK_INPUT;
4847 return count;
4852 /***********************************************************************
4853 Text Cursor
4854 ***********************************************************************/
4856 /* Set clipping for output in glyph row ROW. W is the window in which
4857 we operate. GC is the graphics context to set clipping in.
4859 ROW may be a text row or, e.g., a mode line. Text rows must be
4860 clipped to the interior of the window dedicated to text display,
4861 mode lines must be clipped to the whole window. */
4863 static void
4864 w32_clip_to_row (w, row, area, hdc)
4865 struct window *w;
4866 struct glyph_row *row;
4867 int area;
4868 HDC hdc;
4870 struct frame *f = XFRAME (WINDOW_FRAME (w));
4871 RECT clip_rect;
4872 int window_x, window_y, window_width;
4874 window_box (w, area, &window_x, &window_y, &window_width, 0);
4876 clip_rect.left = window_x;
4877 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4878 clip_rect.top = max (clip_rect.top, window_y);
4879 clip_rect.right = clip_rect.left + window_width;
4880 clip_rect.bottom = clip_rect.top + row->visible_height;
4882 w32_set_clip_rectangle (hdc, &clip_rect);
4886 /* Draw a hollow box cursor on window W in glyph row ROW. */
4888 static void
4889 x_draw_hollow_cursor (w, row)
4890 struct window *w;
4891 struct glyph_row *row;
4893 struct frame *f = XFRAME (WINDOW_FRAME (w));
4894 HDC hdc;
4895 RECT rect;
4896 int left, top, h;
4897 struct glyph *cursor_glyph;
4898 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
4900 /* Get the glyph the cursor is on. If we can't tell because
4901 the current matrix is invalid or such, give up. */
4902 cursor_glyph = get_phys_cursor_glyph (w);
4903 if (cursor_glyph == NULL)
4904 return;
4906 /* Compute frame-relative coordinates for phys cursor. */
4907 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
4908 rect.left = left;
4909 rect.top = top;
4910 rect.bottom = rect.top + h;
4911 rect.right = rect.left + w->phys_cursor_width;
4913 hdc = get_frame_dc (f);
4914 /* Set clipping, draw the rectangle, and reset clipping again. */
4915 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4916 FrameRect (hdc, &rect, hb);
4917 DeleteObject (hb);
4918 w32_set_clip_rectangle (hdc, NULL);
4919 release_frame_dc (f, hdc);
4923 /* Draw a bar cursor on window W in glyph row ROW.
4925 Implementation note: One would like to draw a bar cursor with an
4926 angle equal to the one given by the font property XA_ITALIC_ANGLE.
4927 Unfortunately, I didn't find a font yet that has this property set.
4928 --gerd. */
4930 static void
4931 x_draw_bar_cursor (w, row, width, kind)
4932 struct window *w;
4933 struct glyph_row *row;
4934 int width;
4935 enum text_cursor_kinds kind;
4937 struct frame *f = XFRAME (w->frame);
4938 struct glyph *cursor_glyph;
4939 int x;
4940 HDC hdc;
4942 /* If cursor is out of bounds, don't draw garbage. This can happen
4943 in mini-buffer windows when switching between echo area glyphs
4944 and mini-buffer. */
4945 cursor_glyph = get_phys_cursor_glyph (w);
4946 if (cursor_glyph == NULL)
4947 return;
4949 /* If on an image, draw like a normal cursor. That's usually better
4950 visible than drawing a bar, esp. if the image is large so that
4951 the bar might not be in the window. */
4952 if (cursor_glyph->type == IMAGE_GLYPH)
4954 struct glyph_row *row;
4955 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
4956 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
4958 else
4960 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
4961 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
4963 /* If the glyph's background equals the color we normally draw
4964 the bar cursor in, the bar cursor in its normal color is
4965 invisible. Use the glyph's foreground color instead in this
4966 case, on the assumption that the glyph's colors are chosen so
4967 that the glyph is legible. */
4968 if (face->background == cursor_color)
4969 cursor_color = face->foreground;
4971 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
4973 if (width < 0)
4974 width = FRAME_CURSOR_WIDTH (f);
4975 width = min (cursor_glyph->pixel_width, width);
4977 w->phys_cursor_width = width;
4980 hdc = get_frame_dc (f);
4981 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4983 if (kind == BAR_CURSOR)
4985 w32_fill_area (f, hdc, cursor_color, x,
4986 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
4987 width, row->height);
4989 else
4991 w32_fill_area (f, hdc, cursor_color, x,
4992 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
4993 row->height - width),
4994 min (FRAME_COLUMN_WIDTH (f), cursor_glyph->pixel_width),
4995 width);
4998 w32_set_clip_rectangle (hdc, NULL);
4999 release_frame_dc (f, hdc);
5004 /* RIF: Define cursor CURSOR on frame F. */
5006 static void
5007 w32_define_frame_cursor (f, cursor)
5008 struct frame *f;
5009 Cursor cursor;
5011 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5015 /* RIF: Clear area on frame F. */
5017 static void
5018 w32_clear_frame_area (f, x, y, width, height)
5019 struct frame *f;
5020 int x, y, width, height;
5022 HDC hdc;
5024 hdc = get_frame_dc (f);
5025 w32_clear_area (f, hdc, x, y, width, height);
5026 release_frame_dc (f, hdc);
5029 /* RIF: Draw or clear cursor on window W. */
5031 static void
5032 w32_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
5033 struct window *w;
5034 struct glyph_row *glyph_row;
5035 int x, y;
5036 int cursor_type, cursor_width;
5037 int on_p, active_p;
5039 if (on_p)
5041 /* If the user wants to use the system caret, make sure our own
5042 cursor remains invisible. */
5043 if (w32_use_visible_system_caret)
5045 /* Call to erase_phys_cursor here seems to use the
5046 wrong values of w->phys_cursor, as they have been
5047 overwritten before this function was called. */
5048 if (w->phys_cursor_type != NO_CURSOR)
5049 erase_phys_cursor (w);
5051 cursor_type = w->phys_cursor_type = NO_CURSOR;
5052 w->phys_cursor_width = -1;
5054 else
5056 w->phys_cursor_type = cursor_type;
5059 w->phys_cursor_on_p = 1;
5061 /* If this is the active cursor, we need to track it with the
5062 system caret, so third party software like screen magnifiers
5063 and speech synthesizers can follow the cursor. */
5064 if (active_p)
5066 struct frame *f = XFRAME (WINDOW_FRAME (w));
5067 HWND hwnd = FRAME_W32_WINDOW (f);
5069 w32_system_caret_x
5070 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5071 w32_system_caret_y
5072 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5073 + glyph_row->ascent - w->phys_cursor_ascent);
5075 /* If the size of the active cursor changed, destroy the old
5076 system caret. */
5077 if (w32_system_caret_hwnd
5078 && (w32_system_caret_height != w->phys_cursor_height))
5079 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5081 w32_system_caret_height = w->phys_cursor_height;
5083 /* Move the system caret. */
5084 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5087 if (glyph_row->exact_window_width_line_p
5088 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
5090 glyph_row->cursor_in_fringe_p = 1;
5091 draw_fringe_bitmap (w, glyph_row, 0);
5092 return;
5095 switch (cursor_type)
5097 case HOLLOW_BOX_CURSOR:
5098 x_draw_hollow_cursor (w, glyph_row);
5099 break;
5101 case FILLED_BOX_CURSOR:
5102 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5103 break;
5105 case BAR_CURSOR:
5106 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5107 break;
5109 case HBAR_CURSOR:
5110 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5111 break;
5113 case NO_CURSOR:
5114 w->phys_cursor_width = 0;
5115 break;
5117 default:
5118 abort ();
5125 /* Icons. */
5128 x_bitmap_icon (f, icon)
5129 struct frame *f;
5130 Lisp_Object icon;
5132 HANDLE main_icon;
5133 HANDLE small_icon = NULL;
5135 if (FRAME_W32_WINDOW (f) == 0)
5136 return 1;
5138 if (NILP (icon))
5139 main_icon = LoadIcon (hinst, EMACS_CLASS);
5140 else if (STRINGP (icon))
5142 /* Load the main icon from the named file. */
5143 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5144 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5145 /* Try to load a small icon to go with it. */
5146 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5147 GetSystemMetrics (SM_CXSMICON),
5148 GetSystemMetrics (SM_CYSMICON),
5149 LR_LOADFROMFILE);
5151 else if (SYMBOLP (icon))
5153 LPCTSTR name;
5155 if (EQ (icon, intern ("application")))
5156 name = (LPCTSTR) IDI_APPLICATION;
5157 else if (EQ (icon, intern ("hand")))
5158 name = (LPCTSTR) IDI_HAND;
5159 else if (EQ (icon, intern ("question")))
5160 name = (LPCTSTR) IDI_QUESTION;
5161 else if (EQ (icon, intern ("exclamation")))
5162 name = (LPCTSTR) IDI_EXCLAMATION;
5163 else if (EQ (icon, intern ("asterisk")))
5164 name = (LPCTSTR) IDI_ASTERISK;
5165 else if (EQ (icon, intern ("winlogo")))
5166 name = (LPCTSTR) IDI_WINLOGO;
5167 else
5168 return 1;
5170 main_icon = LoadIcon (NULL, name);
5172 else
5173 return 1;
5175 if (main_icon == NULL)
5176 return 1;
5178 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5179 (LPARAM) main_icon);
5181 /* If there is a small icon that goes with it, set that too. */
5182 if (small_icon)
5183 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5184 (LPARAM) small_icon);
5186 return 0;
5190 /************************************************************************
5191 Handling X errors
5192 ************************************************************************/
5194 /* Display Error Handling functions not used on W32. Listing them here
5195 helps diff stay in step when comparing w32term.c with xterm.c.
5197 x_error_catcher (display, error)
5198 x_catch_errors (dpy)
5199 x_catch_errors_unwind (old_val)
5200 x_check_errors (dpy, format)
5201 x_fully_uncatch_errors ()
5202 x_catching_errors ()
5203 x_had_errors_p (dpy)
5204 x_clear_errors (dpy)
5205 x_uncatch_errors (dpy, count)
5206 x_trace_wire ()
5207 x_connection_signal (signalnum)
5208 x_connection_closed (dpy, error_message)
5209 x_error_quitter (display, error)
5210 x_error_handler (display, error)
5211 x_io_error_quitter (display)
5216 /* Changing the font of the frame. */
5218 Lisp_Object
5219 x_new_font (f, font_object, fontset)
5220 struct frame *f;
5221 Lisp_Object font_object;
5222 int fontset;
5224 struct font *font = XFONT_OBJECT (font_object);
5226 if (fontset < 0)
5227 fontset = fontset_from_font (font_object);
5228 FRAME_FONTSET (f) = fontset;
5229 if (FRAME_FONT (f) == font)
5230 /* This font is already set in frame F. There's nothing more to
5231 do. */
5232 return fontset_name (fontset);
5234 BLOCK_INPUT;
5236 FRAME_FONT (f) = font;
5237 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5238 FRAME_COLUMN_WIDTH (f) = font->average_width;
5239 FRAME_SPACE_WIDTH (f) = font->space_width;
5240 FRAME_LINE_HEIGHT (f) = font->height;
5242 compute_fringe_widths (f, 1);
5244 /* Compute the scroll bar width in character columns. */
5245 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5247 int wid = FRAME_COLUMN_WIDTH (f);
5248 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5249 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5251 else
5253 int wid = FRAME_COLUMN_WIDTH (f);
5254 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5257 /* Now make the frame display the given font. */
5258 if (FRAME_X_WINDOW (f) != 0)
5260 /* Don't change the size of a tip frame; there's no point in
5261 doing it because it's done in Fx_show_tip, and it leads to
5262 problems because the tip frame has no widget. */
5263 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5264 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5267 #ifdef HAVE_X_I18N
5268 if (FRAME_XIC (f)
5269 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
5270 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
5271 #endif
5273 UNBLOCK_INPUT;
5275 return fontset_name (fontset);
5279 /***********************************************************************
5280 TODO: W32 Input Methods
5281 ***********************************************************************/
5282 /* Listing missing functions from xterm.c helps diff stay in step.
5284 xim_destroy_callback (xim, client_data, call_data)
5285 xim_open_dpy (dpyinfo, resource_name)
5286 struct xim_inst_t
5287 xim_instantiate_callback (display, client_data, call_data)
5288 xim_initialize (dpyinfo, resource_name)
5289 xim_close_dpy (dpyinfo)
5294 /* Calculate the absolute position in frame F
5295 from its current recorded position values and gravity. */
5297 void
5298 x_calc_absolute_position (f)
5299 struct frame *f;
5301 int flags = f->size_hint_flags;
5303 /* The sum of the widths of the frame's left and right borders, and
5304 the sum of the heights of the frame's top and bottom borders (in
5305 pixels) drawn by Windows. */
5306 unsigned int left_right_borders_width, top_bottom_borders_height;
5308 /* Try to get the actual values of these two variables. We compute
5309 the border width (height) by subtracting the width (height) of
5310 the frame's client area from the width (height) of the frame's
5311 entire window. */
5312 WINDOWPLACEMENT wp = { 0 };
5313 RECT client_rect = { 0 };
5315 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5316 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5318 left_right_borders_width =
5319 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5320 (client_rect.right - client_rect.left);
5322 top_bottom_borders_height =
5323 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5324 (client_rect.bottom - client_rect.top);
5326 else
5328 /* Use sensible default values. */
5329 left_right_borders_width = 8;
5330 top_bottom_borders_height = 32;
5333 /* Treat negative positions as relative to the rightmost bottommost
5334 position that fits on the screen. */
5335 if (flags & XNegative)
5336 f->left_pos = (x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f))
5337 - FRAME_PIXEL_WIDTH (f)
5338 + f->left_pos
5339 - (left_right_borders_width - 1));
5341 if (flags & YNegative)
5342 f->top_pos = (x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f))
5343 - FRAME_PIXEL_HEIGHT (f)
5344 + f->top_pos
5345 - (top_bottom_borders_height - 1));
5347 /* The left_pos and top_pos are now relative to the top and left
5348 screen edges, so the flags should correspond. */
5349 f->size_hint_flags &= ~ (XNegative | YNegative);
5352 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5353 to really change the position, and 0 when calling from
5354 x_make_frame_visible (in that case, XOFF and YOFF are the current
5355 position values). It is -1 when calling from x_set_frame_parameters,
5356 which means, do adjust for borders but don't change the gravity. */
5358 void
5359 x_set_offset (f, xoff, yoff, change_gravity)
5360 struct frame *f;
5361 register int xoff, yoff;
5362 int change_gravity;
5364 int modified_top, modified_left;
5366 if (change_gravity > 0)
5368 f->top_pos = yoff;
5369 f->left_pos = xoff;
5370 f->size_hint_flags &= ~ (XNegative | YNegative);
5371 if (xoff < 0)
5372 f->size_hint_flags |= XNegative;
5373 if (yoff < 0)
5374 f->size_hint_flags |= YNegative;
5375 f->win_gravity = NorthWestGravity;
5377 x_calc_absolute_position (f);
5379 BLOCK_INPUT;
5380 x_wm_set_size_hint (f, (long) 0, 0);
5382 modified_left = f->left_pos;
5383 modified_top = f->top_pos;
5385 my_set_window_pos (FRAME_W32_WINDOW (f),
5386 NULL,
5387 modified_left, modified_top,
5388 0, 0,
5389 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5390 UNBLOCK_INPUT;
5394 /* Check if we need to resize the frame due to a fullscreen request.
5395 If so needed, resize the frame. */
5396 static void
5397 x_check_fullscreen (f)
5398 struct frame *f;
5400 if (f->want_fullscreen & FULLSCREEN_BOTH)
5402 int width, height, ign;
5404 x_real_positions (f, &f->left_pos, &f->top_pos);
5406 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5408 /* We do not need to move the window, it shall be taken care of
5409 when setting WM manager hints. */
5410 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5412 change_frame_size (f, height, width, 0, 1, 0);
5413 SET_FRAME_GARBAGED (f);
5414 cancel_mouse_face (f);
5416 /* Wait for the change of frame size to occur */
5417 f->want_fullscreen |= FULLSCREEN_WAIT;
5422 /* Call this to change the size of frame F's x-window.
5423 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5424 for this size change and subsequent size changes.
5425 Otherwise we leave the window gravity unchanged. */
5427 void
5428 x_set_window_size (f, change_gravity, cols, rows)
5429 struct frame *f;
5430 int change_gravity;
5431 int cols, rows;
5433 int pixelwidth, pixelheight;
5435 BLOCK_INPUT;
5437 check_frame_size (f, &rows, &cols);
5438 f->scroll_bar_actual_width
5439 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5441 compute_fringe_widths (f, 0);
5443 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5444 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5446 f->win_gravity = NorthWestGravity;
5447 x_wm_set_size_hint (f, (long) 0, 0);
5450 RECT rect;
5452 rect.left = rect.top = 0;
5453 rect.right = pixelwidth;
5454 rect.bottom = pixelheight;
5456 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
5457 FRAME_EXTERNAL_MENU_BAR (f));
5459 my_set_window_pos (FRAME_W32_WINDOW (f),
5460 NULL,
5461 0, 0,
5462 rect.right - rect.left,
5463 rect.bottom - rect.top,
5464 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5467 #if 0
5468 /* The following mirrors what is done in xterm.c. It appears to be
5469 for informing lisp of the new size immediately, while the actual
5470 resize will happen asynchronously. But on Windows, the menu bar
5471 automatically wraps when the frame is too narrow to contain it,
5472 and that causes any calculations made here to come out wrong. The
5473 end is some nasty buggy behavior, including the potential loss
5474 of the minibuffer.
5476 Disabling this code is either not sufficient to fix the problems
5477 completely, or it causes fresh problems, but at least it removes
5478 the most problematic symptom of the minibuffer becoming unusable.
5480 -----------------------------------------------------------------
5482 Now, strictly speaking, we can't be sure that this is accurate,
5483 but the window manager will get around to dealing with the size
5484 change request eventually, and we'll hear how it went when the
5485 ConfigureNotify event gets here.
5487 We could just not bother storing any of this information here,
5488 and let the ConfigureNotify event set everything up, but that
5489 might be kind of confusing to the Lisp code, since size changes
5490 wouldn't be reported in the frame parameters until some random
5491 point in the future when the ConfigureNotify event arrives.
5493 We pass 1 for DELAY since we can't run Lisp code inside of
5494 a BLOCK_INPUT. */
5495 change_frame_size (f, rows, cols, 0, 1, 0);
5496 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5497 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5499 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5500 receive in the ConfigureNotify event; if we get what we asked
5501 for, then the event won't cause the screen to become garbaged, so
5502 we have to make sure to do it here. */
5503 SET_FRAME_GARBAGED (f);
5505 /* If cursor was outside the new size, mark it as off. */
5506 mark_window_cursors_off (XWINDOW (f->root_window));
5508 /* Clear out any recollection of where the mouse highlighting was,
5509 since it might be in a place that's outside the new frame size.
5510 Actually checking whether it is outside is a pain in the neck,
5511 so don't try--just let the highlighting be done afresh with new size. */
5512 cancel_mouse_face (f);
5513 #endif
5515 UNBLOCK_INPUT;
5518 /* Mouse warping. */
5520 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5522 void
5523 x_set_mouse_position (f, x, y)
5524 struct frame *f;
5525 int x, y;
5527 int pix_x, pix_y;
5529 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5530 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5532 if (pix_x < 0) pix_x = 0;
5533 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5535 if (pix_y < 0) pix_y = 0;
5536 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5538 x_set_mouse_pixel_position (f, pix_x, pix_y);
5541 void
5542 x_set_mouse_pixel_position (f, pix_x, pix_y)
5543 struct frame *f;
5544 int pix_x, pix_y;
5546 RECT rect;
5547 POINT pt;
5549 BLOCK_INPUT;
5551 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5552 pt.x = rect.left + pix_x;
5553 pt.y = rect.top + pix_y;
5554 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5556 SetCursorPos (pt.x, pt.y);
5558 UNBLOCK_INPUT;
5562 /* focus shifting, raising and lowering. */
5564 void
5565 x_focus_on_frame (f)
5566 struct frame *f;
5568 struct w32_display_info *dpyinfo = &one_w32_display_info;
5570 /* Give input focus to frame. */
5571 BLOCK_INPUT;
5572 #if 0
5573 /* Try not to change its Z-order if possible. */
5574 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5575 my_set_focus (f, FRAME_W32_WINDOW (f));
5576 else
5577 #endif
5578 my_set_foreground_window (FRAME_W32_WINDOW (f));
5579 UNBLOCK_INPUT;
5582 void
5583 x_unfocus_frame (f)
5584 struct frame *f;
5588 /* Raise frame F. */
5589 void
5590 x_raise_frame (f)
5591 struct frame *f;
5593 BLOCK_INPUT;
5595 /* Strictly speaking, raise-frame should only change the frame's Z
5596 order, leaving input focus unchanged. This is reasonable behavior
5597 on X where the usual policy is point-to-focus. However, this
5598 behavior would be very odd on Windows where the usual policy is
5599 click-to-focus.
5601 On X, if the mouse happens to be over the raised frame, it gets
5602 input focus anyway (so the window with focus will never be
5603 completely obscured) - if not, then just moving the mouse over it
5604 is sufficient to give it focus. On Windows, the user must actually
5605 click on the frame (preferrably the title bar so as not to move
5606 point), which is more awkward. Also, no other Windows program
5607 raises a window to the top but leaves another window (possibly now
5608 completely obscured) with input focus.
5610 Because there is a system setting on Windows that allows the user
5611 to choose the point to focus policy, we make the strict semantics
5612 optional, but by default we grab focus when raising. */
5614 if (NILP (Vw32_grab_focus_on_raise))
5616 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5617 not already the foreground application: the frame is raised
5618 above all other frames belonging to us, but not above the
5619 current top window. To achieve that, we have to resort to this
5620 more cumbersome method. */
5622 HDWP handle = BeginDeferWindowPos (2);
5623 if (handle)
5625 DeferWindowPos (handle,
5626 FRAME_W32_WINDOW (f),
5627 HWND_TOP,
5628 0, 0, 0, 0,
5629 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5631 DeferWindowPos (handle,
5632 GetForegroundWindow (),
5633 FRAME_W32_WINDOW (f),
5634 0, 0, 0, 0,
5635 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5637 EndDeferWindowPos (handle);
5640 else
5642 my_set_foreground_window (FRAME_W32_WINDOW (f));
5645 UNBLOCK_INPUT;
5648 /* Lower frame F. */
5649 void
5650 x_lower_frame (f)
5651 struct frame *f;
5653 BLOCK_INPUT;
5654 my_set_window_pos (FRAME_W32_WINDOW (f),
5655 HWND_BOTTOM,
5656 0, 0, 0, 0,
5657 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5658 UNBLOCK_INPUT;
5661 static void
5662 w32_frame_raise_lower (f, raise_flag)
5663 FRAME_PTR f;
5664 int raise_flag;
5666 if (! FRAME_W32_P (f))
5667 return;
5669 if (raise_flag)
5670 x_raise_frame (f);
5671 else
5672 x_lower_frame (f);
5675 /* Change of visibility. */
5677 /* This tries to wait until the frame is really visible.
5678 However, if the window manager asks the user where to position
5679 the frame, this will return before the user finishes doing that.
5680 The frame will not actually be visible at that time,
5681 but it will become visible later when the window manager
5682 finishes with it. */
5684 void
5685 x_make_frame_visible (f)
5686 struct frame *f;
5688 Lisp_Object type;
5690 BLOCK_INPUT;
5692 type = x_icon_type (f);
5693 if (!NILP (type))
5694 x_bitmap_icon (f, type);
5696 if (! FRAME_VISIBLE_P (f))
5698 /* We test FRAME_GARBAGED_P here to make sure we don't
5699 call x_set_offset a second time
5700 if we get to x_make_frame_visible a second time
5701 before the window gets really visible. */
5702 if (! FRAME_ICONIFIED_P (f)
5703 && ! f->output_data.w32->asked_for_visible)
5705 RECT workarea_rect;
5706 RECT window_rect;
5708 /* Adjust vertical window position in order to avoid being
5709 covered by a task bar placed at the bottom of the desktop. */
5710 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
5711 GetWindowRect(FRAME_W32_WINDOW(f), &window_rect);
5712 if (window_rect.bottom > workarea_rect.bottom
5713 && window_rect.top > workarea_rect.top)
5714 f->top_pos = max (window_rect.top
5715 - window_rect.bottom + workarea_rect.bottom,
5716 workarea_rect.top);
5718 x_set_offset (f, f->left_pos, f->top_pos, 0);
5721 f->output_data.w32->asked_for_visible = 1;
5723 /* The first of these seems to give more expected behavior, but
5724 was added as a commented out line in Sept 1997, with the
5725 second version remaining uncommented. There may have been
5726 some problem with it that led to it not being enabled,
5727 so the old version remains commented out below in case we
5728 decide we need to go back to it [23.0.60 2008-06-09]. */
5729 my_show_window (f, FRAME_W32_WINDOW (f),
5730 f->async_iconified ? SW_RESTORE : SW_SHOW);
5731 /* my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL); */
5734 /* Synchronize to ensure Emacs knows the frame is visible
5735 before we do anything else. We do this loop with input not blocked
5736 so that incoming events are handled. */
5738 Lisp_Object frame;
5739 int count;
5741 /* This must come after we set COUNT. */
5742 UNBLOCK_INPUT;
5744 XSETFRAME (frame, f);
5746 /* Wait until the frame is visible. Process X events until a
5747 MapNotify event has been seen, or until we think we won't get a
5748 MapNotify at all.. */
5749 for (count = input_signal_count + 10;
5750 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5752 /* Force processing of queued events. */
5753 /* TODO: x_sync equivalent? */
5755 /* Machines that do polling rather than SIGIO have been observed
5756 to go into a busy-wait here. So we'll fake an alarm signal
5757 to let the handler know that there's something to be read.
5758 We used to raise a real alarm, but it seems that the handler
5759 isn't always enabled here. This is probably a bug. */
5760 if (input_polling_used ())
5762 /* It could be confusing if a real alarm arrives while processing
5763 the fake one. Turn it off and let the handler reset it. */
5764 int old_poll_suppress_count = poll_suppress_count;
5765 poll_suppress_count = 1;
5766 poll_for_input_1 ();
5767 poll_suppress_count = old_poll_suppress_count;
5770 FRAME_SAMPLE_VISIBILITY (f);
5774 /* Change from mapped state to withdrawn state. */
5776 /* Make the frame visible (mapped and not iconified). */
5778 x_make_frame_invisible (f)
5779 struct frame *f;
5781 /* Don't keep the highlight on an invisible frame. */
5782 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5783 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5785 BLOCK_INPUT;
5787 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
5789 /* We can't distinguish this from iconification
5790 just by the event that we get from the server.
5791 So we can't win using the usual strategy of letting
5792 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5793 and synchronize with the server to make sure we agree. */
5794 f->visible = 0;
5795 FRAME_ICONIFIED_P (f) = 0;
5796 f->async_visible = 0;
5797 f->async_iconified = 0;
5799 UNBLOCK_INPUT;
5802 /* Change window state from mapped to iconified. */
5804 void
5805 x_iconify_frame (f)
5806 struct frame *f;
5808 Lisp_Object type;
5810 /* Don't keep the highlight on an invisible frame. */
5811 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5812 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5814 if (f->async_iconified)
5815 return;
5817 BLOCK_INPUT;
5819 type = x_icon_type (f);
5820 if (!NILP (type))
5821 x_bitmap_icon (f, type);
5823 /* Simulate the user minimizing the frame. */
5824 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
5826 UNBLOCK_INPUT;
5830 /* Free X resources of frame F. */
5832 void
5833 x_free_frame_resources (f)
5834 struct frame *f;
5836 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5838 BLOCK_INPUT;
5840 /* We must free faces before destroying windows because some
5841 font-driver (e.g. xft) access a window while finishing a
5842 face. */
5843 if (FRAME_FACE_CACHE (f))
5844 free_frame_faces (f);
5846 if (FRAME_W32_WINDOW (f))
5847 my_destroy_window (f, FRAME_W32_WINDOW (f));
5849 free_frame_menubar (f);
5851 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
5852 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
5853 unload_color (f, f->output_data.w32->cursor_pixel);
5854 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
5855 unload_color (f, f->output_data.w32->border_pixel);
5856 unload_color (f, f->output_data.w32->mouse_pixel);
5857 if (f->output_data.w32->white_relief.allocated_p)
5858 unload_color (f, f->output_data.w32->white_relief.pixel);
5859 if (f->output_data.w32->black_relief.allocated_p)
5860 unload_color (f, f->output_data.w32->black_relief.pixel);
5862 if (FRAME_FACE_CACHE (f))
5863 free_frame_faces (f);
5865 xfree (f->output_data.w32);
5866 f->output_data.w32 = NULL;
5868 if (f == dpyinfo->w32_focus_frame)
5869 dpyinfo->w32_focus_frame = 0;
5870 if (f == dpyinfo->w32_focus_event_frame)
5871 dpyinfo->w32_focus_event_frame = 0;
5872 if (f == dpyinfo->x_highlight_frame)
5873 dpyinfo->x_highlight_frame = 0;
5875 if (f == dpyinfo->mouse_face_mouse_frame)
5877 dpyinfo->mouse_face_beg_row
5878 = dpyinfo->mouse_face_beg_col = -1;
5879 dpyinfo->mouse_face_end_row
5880 = dpyinfo->mouse_face_end_col = -1;
5881 dpyinfo->mouse_face_window = Qnil;
5882 dpyinfo->mouse_face_deferred_gc = 0;
5883 dpyinfo->mouse_face_mouse_frame = 0;
5886 UNBLOCK_INPUT;
5890 /* Destroy the window of frame F. */
5891 void
5892 x_destroy_window (f)
5893 struct frame *f;
5895 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5897 x_free_frame_resources (f);
5898 dpyinfo->reference_count--;
5902 /* Setting window manager hints. */
5904 /* Set the normal size hints for the window manager, for frame F.
5905 FLAGS is the flags word to use--or 0 meaning preserve the flags
5906 that the window now has.
5907 If USER_POSITION is nonzero, we set the USPosition
5908 flag (this is useful when FLAGS is 0). */
5909 void
5910 x_wm_set_size_hint (f, flags, user_position)
5911 struct frame *f;
5912 long flags;
5913 int user_position;
5915 Window window = FRAME_W32_WINDOW (f);
5917 enter_crit ();
5919 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
5920 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
5921 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
5922 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
5924 leave_crit ();
5927 /* Window manager things */
5928 void
5929 x_wm_set_icon_position (f, icon_x, icon_y)
5930 struct frame *f;
5931 int icon_x, icon_y;
5933 #if 0
5934 Window window = FRAME_W32_WINDOW (f);
5936 f->display.x->wm_hints.flags |= IconPositionHint;
5937 f->display.x->wm_hints.icon_x = icon_x;
5938 f->display.x->wm_hints.icon_y = icon_y;
5940 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
5941 #endif
5945 /***********************************************************************
5946 Initialization
5947 ***********************************************************************/
5949 static int w32_initialized = 0;
5951 void
5952 w32_initialize_display_info (display_name)
5953 Lisp_Object display_name;
5955 struct w32_display_info *dpyinfo = &one_w32_display_info;
5957 bzero (dpyinfo, sizeof (*dpyinfo));
5959 /* Put it on w32_display_name_list. */
5960 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
5961 w32_display_name_list);
5962 dpyinfo->name_list_element = XCAR (w32_display_name_list);
5964 dpyinfo->w32_id_name
5965 = (char *) xmalloc (SCHARS (Vinvocation_name)
5966 + SCHARS (Vsystem_name)
5967 + 2);
5968 sprintf (dpyinfo->w32_id_name, "%s@%s",
5969 SDATA (Vinvocation_name), SDATA (Vsystem_name));
5971 /* Default Console mode values - overridden when running in GUI mode
5972 with values obtained from system metrics. */
5973 dpyinfo->resx = 1;
5974 dpyinfo->resy = 1;
5975 dpyinfo->n_planes = 1;
5976 dpyinfo->n_cbits = 4;
5977 dpyinfo->n_fonts = 0;
5978 dpyinfo->smallest_font_height = 1;
5979 dpyinfo->smallest_char_width = 1;
5981 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
5982 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
5983 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
5984 dpyinfo->mouse_face_window = Qnil;
5985 dpyinfo->mouse_face_overlay = Qnil;
5986 dpyinfo->mouse_face_hidden = 0;
5988 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
5989 /* TODO: dpyinfo->gray */
5993 /* Create an xrdb-style database of resources to supercede registry settings.
5994 The database is just a concatenation of C strings, finished by an additional
5995 \0. The strings are submitted to some basic normalization, so
5997 [ *]option[ *]:[ *]value...
5999 becomes
6001 option:value...
6003 but any whitespace following value is not removed. */
6005 static char *
6006 w32_make_rdb (xrm_option)
6007 char *xrm_option;
6009 char *buffer = xmalloc (strlen (xrm_option) + 2);
6010 char *current = buffer;
6011 char ch;
6012 int in_option = 1;
6013 int before_value = 0;
6015 do {
6016 ch = *xrm_option++;
6018 if (ch == '\n')
6020 *current++ = '\0';
6021 in_option = 1;
6022 before_value = 0;
6024 else if (ch != ' ')
6026 *current++ = ch;
6027 if (in_option && (ch == ':'))
6029 in_option = 0;
6030 before_value = 1;
6032 else if (before_value)
6034 before_value = 0;
6037 else if (!(in_option || before_value))
6039 *current++ = ch;
6041 } while (ch);
6043 *current = '\0';
6045 return buffer;
6048 void
6049 x_flush (struct frame * f)
6050 { /* Nothing to do */ }
6053 extern frame_parm_handler w32_frame_parm_handlers[];
6055 static struct redisplay_interface w32_redisplay_interface =
6057 w32_frame_parm_handlers,
6058 x_produce_glyphs,
6059 x_write_glyphs,
6060 x_insert_glyphs,
6061 x_clear_end_of_line,
6062 x_scroll_run,
6063 x_after_update_window_line,
6064 x_update_window_begin,
6065 x_update_window_end,
6066 x_cursor_to,
6067 x_flush,
6068 0, /* flush_display_optional */
6069 x_clear_window_mouse_face,
6070 x_get_glyph_overhangs,
6071 x_fix_overlapping_area,
6072 w32_draw_fringe_bitmap,
6073 w32_define_fringe_bitmap,
6074 w32_destroy_fringe_bitmap,
6075 w32_compute_glyph_string_overhangs,
6076 x_draw_glyph_string,
6077 w32_define_frame_cursor,
6078 w32_clear_frame_area,
6079 w32_draw_window_cursor,
6080 w32_draw_vertical_window_border,
6081 w32_shift_glyphs_for_insert
6084 static void x_delete_terminal (struct terminal *term);
6086 static struct terminal *
6087 w32_create_terminal (struct w32_display_info *dpyinfo)
6089 struct terminal *terminal;
6091 terminal = create_terminal ();
6093 terminal->type = output_w32;
6094 terminal->display_info.w32 = dpyinfo;
6095 dpyinfo->terminal = terminal;
6097 /* MSVC does not type K&R functions with no arguments correctly, and
6098 so we must explicitly cast them. */
6099 terminal->clear_frame_hook = x_clear_frame;
6100 terminal->ins_del_lines_hook = x_ins_del_lines;
6101 terminal->delete_glyphs_hook = x_delete_glyphs;
6102 terminal->ring_bell_hook = w32_ring_bell;
6103 terminal->reset_terminal_modes_hook = w32_reset_terminal_modes;
6104 terminal->set_terminal_modes_hook = w32_set_terminal_modes;
6105 terminal->update_begin_hook = x_update_begin;
6106 terminal->update_end_hook = x_update_end;
6107 terminal->set_terminal_window_hook = w32_set_terminal_window;
6108 terminal->read_socket_hook = w32_read_socket;
6109 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6110 terminal->mouse_position_hook = w32_mouse_position;
6111 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6112 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6113 // terminal->fullscreen_hook = XTfullscreen_hook;
6114 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6115 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6116 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6117 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6119 terminal->delete_frame_hook = x_destroy_window;
6120 terminal->delete_terminal_hook = x_delete_terminal;
6122 terminal->rif = &w32_redisplay_interface;
6123 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
6124 terminal->char_ins_del_ok = 1;
6125 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
6126 terminal->fast_clear_end_of_line = 1; /* X does this well. */
6127 terminal->memory_below_frame = 0; /* We don't remember what scrolls
6128 off the bottom. */
6130 /* We don't yet support separate terminals on W32, so don't try to share
6131 keyboards between virtual terminals that are on the same physical
6132 terminal like X does. */
6133 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
6134 init_kboard (terminal->kboard);
6135 terminal->kboard->Vwindow_system = intern ("w32");
6136 terminal->kboard->next_kboard = all_kboards;
6137 all_kboards = terminal->kboard;
6138 /* Don't let the initial kboard remain current longer than necessary.
6139 That would cause problems if a file loaded on startup tries to
6140 prompt in the mini-buffer. */
6141 if (current_kboard == initial_kboard)
6142 current_kboard = terminal->kboard;
6143 terminal->kboard->reference_count++;
6145 return terminal;
6148 static void
6149 x_delete_terminal (struct terminal *terminal)
6151 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6152 int i;
6154 /* Protect against recursive calls. Fdelete_frame in
6155 delete_terminal calls us back when it deletes our last frame. */
6156 if (!terminal->name)
6157 return;
6159 BLOCK_INPUT;
6161 x_delete_display (dpyinfo);
6162 UNBLOCK_INPUT;
6165 struct w32_display_info *
6166 w32_term_init (display_name, xrm_option, resource_name)
6167 Lisp_Object display_name;
6168 char *xrm_option;
6169 char *resource_name;
6171 struct w32_display_info *dpyinfo;
6172 struct terminal *terminal;
6173 HDC hdc;
6175 BLOCK_INPUT;
6177 if (!w32_initialized)
6179 w32_initialize ();
6180 w32_initialized = 1;
6183 w32_initialize_display_info (display_name);
6185 dpyinfo = &one_w32_display_info;
6186 terminal = w32_create_terminal (dpyinfo);
6188 /* Set the name of the terminal. */
6189 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
6190 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
6191 terminal->name[SBYTES (display_name)] = 0;
6193 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6195 /* Put this display on the chain. */
6196 dpyinfo->next = x_display_list;
6197 x_display_list = dpyinfo;
6199 hdc = GetDC (GetDesktopWindow ());
6201 dpyinfo->root_window = GetDesktopWindow ();
6202 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6203 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6204 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6205 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6206 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6207 dpyinfo->terminal->image_cache = make_image_cache ();
6208 ReleaseDC (GetDesktopWindow (), hdc);
6210 /* initialise palette with white and black */
6212 XColor color;
6213 w32_defined_color (0, "white", &color, 1);
6214 w32_defined_color (0, "black", &color, 1);
6217 /* Add the default keyboard. */
6218 add_keyboard_wait_descriptor (0);
6220 /* Create Fringe Bitmaps and store them for later use.
6222 On W32, bitmaps are all unsigned short, as Windows requires
6223 bitmap data to be Word aligned. For some reason they are
6224 horizontally reflected compared to how they appear on X, so we
6225 need to bitswap and convert to unsigned shorts before creating
6226 the bitmaps. */
6227 w32_init_fringe (terminal->rif);
6229 #ifdef F_SETOWN
6230 fcntl (connection, F_SETOWN, getpid ());
6231 #endif /* ! defined (F_SETOWN) */
6233 #ifdef SIGIO
6234 if (interrupt_input)
6235 init_sigio (connection);
6236 #endif /* ! defined (SIGIO) */
6238 UNBLOCK_INPUT;
6240 return dpyinfo;
6243 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6244 void
6245 x_delete_display (dpyinfo)
6246 struct w32_display_info *dpyinfo;
6248 /* Discard this display from w32_display_name_list and w32_display_list.
6249 We can't use Fdelq because that can quit. */
6250 if (! NILP (w32_display_name_list)
6251 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
6252 w32_display_name_list = XCDR (w32_display_name_list);
6253 else
6255 Lisp_Object tail;
6257 tail = w32_display_name_list;
6258 while (CONSP (tail) && CONSP (XCDR (tail)))
6260 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
6262 XSETCDR (tail, XCDR (XCDR (tail)));
6263 break;
6265 tail = XCDR (tail);
6269 /* free palette table */
6271 struct w32_palette_entry * plist;
6273 plist = dpyinfo->color_list;
6274 while (plist)
6276 struct w32_palette_entry * pentry = plist;
6277 plist = plist->next;
6278 xfree (pentry);
6280 dpyinfo->color_list = NULL;
6281 if (dpyinfo->palette)
6282 DeleteObject(dpyinfo->palette);
6284 xfree (dpyinfo->w32_id_name);
6286 w32_reset_fringes ();
6289 /* Set up use of W32. */
6291 DWORD WINAPI w32_msg_worker (void * arg);
6293 static void
6294 w32_initialize ()
6296 baud_rate = 19200;
6298 w32_system_caret_hwnd = NULL;
6299 w32_system_caret_height = 0;
6300 w32_system_caret_x = 0;
6301 w32_system_caret_y = 0;
6303 /* Initialize w32_use_visible_system_caret based on whether a screen
6304 reader is in use. */
6305 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6306 &w32_use_visible_system_caret, 0))
6307 w32_use_visible_system_caret = 0;
6309 last_tool_bar_item = -1;
6310 any_help_event_p = 0;
6312 /* Initialize input mode: interrupt_input off, no flow control, allow
6313 8 bit character input, standard quit char. */
6314 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6316 /* Create the window thread - it will terminate itself or when the app terminates */
6318 init_crit ();
6320 dwMainThreadId = GetCurrentThreadId ();
6321 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6322 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6324 /* Wait for thread to start */
6327 MSG msg;
6329 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6331 hWindowsThread = CreateThread (NULL, 0,
6332 w32_msg_worker,
6333 0, 0, &dwWindowsThreadId);
6335 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6338 /* It is desirable that mainThread should have the same notion of
6339 focus window and active window as windowsThread. Unfortunately, the
6340 following call to AttachThreadInput, which should do precisely what
6341 we need, causes major problems when Emacs is linked as a console
6342 program. Unfortunately, we have good reasons for doing that, so
6343 instead we need to send messages to windowsThread to make some API
6344 calls for us (ones that affect, or depend on, the active/focus
6345 window state.) */
6346 #ifdef ATTACH_THREADS
6347 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6348 #endif
6350 /* Dynamically link to optional system components. */
6352 HANDLE user_lib = LoadLibrary ("user32.dll");
6354 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6356 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6358 #undef LOAD_PROC
6360 FreeLibrary (user_lib);
6362 /* Ensure scrollbar handle is at least 5 pixels. */
6363 vertical_scroll_bar_min_handle = 5;
6365 /* For either kind of scroll bar, take account of the arrows; these
6366 effectively form the border of the main scroll bar range. */
6367 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6368 = GetSystemMetrics (SM_CYVSCROLL);
6372 void
6373 syms_of_w32term ()
6375 staticpro (&w32_display_name_list);
6376 w32_display_name_list = Qnil;
6378 staticpro (&last_mouse_scroll_bar);
6379 last_mouse_scroll_bar = Qnil;
6381 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6383 DEFVAR_INT ("w32-num-mouse-buttons",
6384 &w32_num_mouse_buttons,
6385 doc: /* Number of physical mouse buttons. */);
6386 w32_num_mouse_buttons = 2;
6388 DEFVAR_LISP ("w32-swap-mouse-buttons",
6389 &Vw32_swap_mouse_buttons,
6390 doc: /* Swap the mapping of middle and right mouse buttons.
6391 When nil, middle button is mouse-2 and right button is mouse-3. */);
6392 Vw32_swap_mouse_buttons = Qnil;
6394 DEFVAR_LISP ("w32-grab-focus-on-raise",
6395 &Vw32_grab_focus_on_raise,
6396 doc: /* Raised frame grabs input focus.
6397 When t, `raise-frame' grabs input focus as well. This fits well
6398 with the normal Windows click-to-focus policy, but might not be
6399 desirable when using a point-to-focus policy. */);
6400 Vw32_grab_focus_on_raise = Qt;
6402 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6403 &Vw32_capslock_is_shiftlock,
6404 doc: /* Apply CapsLock state to non character input keys.
6405 When nil, CapsLock only affects normal character input keys. */);
6406 Vw32_capslock_is_shiftlock = Qnil;
6408 DEFVAR_LISP ("w32-recognize-altgr",
6409 &Vw32_recognize_altgr,
6410 doc: /* Recognize right-alt and left-ctrl as AltGr.
6411 When nil, the right-alt and left-ctrl key combination is
6412 interpreted normally. */);
6413 Vw32_recognize_altgr = Qt;
6415 DEFVAR_BOOL ("w32-use-visible-system-caret",
6416 &w32_use_visible_system_caret,
6417 doc: /* Flag to make the system caret visible.
6418 When this is non-nil, Emacs will indicate the position of point by
6419 using the system caret instead of drawing its own cursor. Some screen
6420 reader software does not track the system cursor properly when it is
6421 invisible, and gets confused by Emacs drawing its own cursor, so this
6422 variable is initialized to t when Emacs detects that screen reader
6423 software is running as it starts up.
6425 When this variable is set, other variables affecting the appearance of
6426 the cursor have no effect. */);
6428 w32_use_visible_system_caret = 0;
6430 /* We don't yet support this, but defining this here avoids whining
6431 from cus-start.el and other places, like "M-x set-variable". */
6432 DEFVAR_BOOL ("x-use-underline-position-properties",
6433 &x_use_underline_position_properties,
6434 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
6435 A value of nil means ignore them. If you encounter fonts with bogus
6436 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6437 to 4.1, set this to nil.
6439 NOTE: Not supported on MS-Windows yet. */);
6440 x_use_underline_position_properties = 0;
6442 DEFVAR_BOOL ("x-underline-at-descent-line",
6443 &x_underline_at_descent_line,
6444 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
6445 A value of nil means to draw the underline according to the value of the
6446 variable `x-use-underline-position-properties', which is usually at the
6447 baseline level. The default value is nil. */);
6448 x_underline_at_descent_line = 0;
6450 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
6451 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6452 Vx_toolkit_scroll_bars = Qt;
6454 staticpro (&last_mouse_motion_frame);
6455 last_mouse_motion_frame = Qnil;
6458 /* arch-tag: 5fa70624-ab86-499c-8a85-473958ee4646
6459 (do not change this comment) */