Comment (python-mode is now separate from Python).
[emacs.git] / src / w32term.c
blob7ad7778feaaed4de882b14f2ac107c235ed727e2
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "w32term.h"
29 #include "systty.h"
30 #include "systime.h"
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/stat.h>
36 #include "charset.h"
37 #include "character.h"
38 #include "coding.h"
39 #include "ccl.h"
40 #include "frame.h"
41 #include "dispextern.h"
42 #include "fontset.h"
43 #include "termhooks.h"
44 #include "termopts.h"
45 #include "termchar.h"
46 #include "disptab.h"
47 #include "buffer.h"
48 #include "window.h"
49 #include "keyboard.h"
50 #include "intervals.h"
51 #include "process.h"
52 #include "atimer.h"
53 #include "keymap.h"
55 #include "w32heap.h"
56 #include <shellapi.h>
58 #include "font.h"
59 #include "w32font.h"
61 /* Fringe bitmaps. */
63 static int max_fringe_bmp = 0;
64 static HBITMAP *fringe_bmp = 0;
66 /* Non-nil means Emacs uses toolkit scroll bars. */
68 Lisp_Object Vx_toolkit_scroll_bars;
70 /* Temporary variables for w32_read_socket. */
72 static int last_mousemove_x = 0;
73 static int last_mousemove_y = 0;
75 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
76 #ifndef GET_WHEEL_DELTA_WPARAM
77 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
78 #endif
80 /* Non-zero means that a HELP_EVENT has been generated since Emacs
81 start. */
83 static int any_help_event_p;
85 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
86 static Lisp_Object last_window;
88 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
89 int x_use_underline_position_properties;
91 /* Non-zero means to draw the underline at the same place as the descent line. */
93 int x_underline_at_descent_line;
95 extern unsigned int msh_mousewheel;
97 extern void free_frame_menubar ();
99 extern int w32_codepage_for_font (char *fontname);
100 extern Cursor w32_load_cursor (LPCTSTR name);
102 extern Lisp_Object Vwindow_system;
104 #define x_any_window_to_frame x_window_to_frame
105 #define x_top_window_to_frame x_window_to_frame
108 /* This is display since w32 does not support multiple ones. */
109 struct w32_display_info one_w32_display_info;
110 struct w32_display_info *x_display_list;
112 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
113 one for each element of w32_display_list and in the same order.
114 NAME is the name of the frame.
115 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
116 Lisp_Object w32_display_name_list;
119 #ifndef GLYPHSET
120 /* Pre Windows 2000, this was not available, but define it here so
121 that Emacs compiled on such a platform will run on newer versions. */
123 typedef struct tagWCRANGE
125 WCHAR wcLow;
126 USHORT cGlyphs;
127 } WCRANGE;
129 typedef struct tagGLYPHSET
131 DWORD cbThis;
132 DWORD flAccel;
133 DWORD cGlyphsSupported;
134 DWORD cRanges;
135 WCRANGE ranges[1];
136 } GLYPHSET;
138 #endif
140 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000). */
141 BOOL (PASCAL *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
143 #ifndef LWA_ALPHA
144 #define LWA_ALPHA 0x02
145 #endif
146 /* WS_EX_LAYERED is defined unconditionally by MingW, but only for W2K and
147 later targets by MSVC headers. */
148 #ifndef WS_EX_LAYERED
149 #define WS_EX_LAYERED 0x80000
150 #endif
152 /* Frame being updated by update_frame. This is declared in term.c.
153 This is set by update_begin and looked at by all the
154 w32 functions. It is zero while not inside an update.
155 In that case, the w32 functions assume that `SELECTED_FRAME ()'
156 is the frame to apply to. */
157 extern struct frame *updating_frame;
159 /* This is a frame waiting to be autoraised, within w32_read_socket. */
160 struct frame *pending_autoraise_frame;
162 /* The handle of the frame that currently owns the system caret. */
163 HWND w32_system_caret_hwnd;
164 int w32_system_caret_height;
165 int w32_system_caret_x;
166 int w32_system_caret_y;
167 int w32_use_visible_system_caret;
169 DWORD dwWindowsThreadId = 0;
170 HANDLE hWindowsThread = NULL;
171 DWORD dwMainThreadId = 0;
172 HANDLE hMainThread = NULL;
174 int vertical_scroll_bar_min_handle;
175 int vertical_scroll_bar_top_border;
176 int vertical_scroll_bar_bottom_border;
178 int last_scroll_bar_drag_pos;
180 /* Mouse movement. */
182 /* Where the mouse was last time we reported a mouse event. */
184 static RECT last_mouse_glyph;
185 static FRAME_PTR last_mouse_glyph_frame;
186 static Lisp_Object last_mouse_press_frame;
188 int w32_num_mouse_buttons;
190 Lisp_Object Vw32_swap_mouse_buttons;
192 /* Control whether x_raise_frame also sets input focus. */
193 Lisp_Object Vw32_grab_focus_on_raise;
195 /* Control whether Caps Lock affects non-ascii characters. */
196 Lisp_Object Vw32_capslock_is_shiftlock;
198 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
199 Lisp_Object Vw32_recognize_altgr;
201 /* The scroll bar in which the last motion event occurred.
203 If the last motion event occurred in a scroll bar, we set this
204 so w32_mouse_position can know whether to report a scroll bar motion or
205 an ordinary motion.
207 If the last motion event didn't occur in a scroll bar, we set this
208 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
209 static Lisp_Object last_mouse_scroll_bar;
210 static int last_mouse_scroll_bar_pos;
212 /* This is a hack. We would really prefer that w32_mouse_position would
213 return the time associated with the position it returns, but there
214 doesn't seem to be any way to wrest the time-stamp from the server
215 along with the position query. So, we just keep track of the time
216 of the last movement we received, and return that in hopes that
217 it's somewhat accurate. */
219 static Time last_mouse_movement_time;
221 /* Incremented by w32_read_socket whenever it really tries to read
222 events. */
224 #ifdef __STDC__
225 static int volatile input_signal_count;
226 #else
227 static int input_signal_count;
228 #endif
230 extern Lisp_Object Vcommand_line_args, Vsystem_name;
232 #ifndef USE_CRT_DLL
233 extern int errno;
234 #endif
236 /* A mask of extra modifier bits to put into every keyboard char. */
238 extern EMACS_INT extra_keyboard_modifiers;
240 static void x_update_window_end P_ ((struct window *, int, int));
241 static void w32_handle_tool_bar_click P_ ((struct frame *,
242 struct input_event *));
243 static void w32_define_cursor P_ ((Window, Cursor));
245 void x_lower_frame P_ ((struct frame *));
246 void x_scroll_bar_clear P_ ((struct frame *));
247 void x_wm_set_size_hint P_ ((struct frame *, long, int));
248 void x_raise_frame P_ ((struct frame *));
249 void x_set_window_size P_ ((struct frame *, int, int, int));
250 void x_wm_set_window_state P_ ((struct frame *, int));
251 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
252 static void w32_initialize P_ ((void));
253 static void x_update_end P_ ((struct frame *));
254 static void w32_frame_up_to_date P_ ((struct frame *));
255 static void w32_set_terminal_modes P_ ((struct terminal *));
256 static void w32_reset_terminal_modes P_ ((struct terminal *));
257 static void x_clear_frame P_ ((struct frame *));
258 static void frame_highlight P_ ((struct frame *));
259 static void frame_unhighlight P_ ((struct frame *));
260 static void x_new_focus_frame P_ ((struct w32_display_info *,
261 struct frame *));
262 static void x_focus_changed P_ ((int, int, struct w32_display_info *,
263 struct frame *, struct input_event *));
264 static void w32_detect_focus_change P_ ((struct w32_display_info *,
265 W32Msg *, struct input_event *));
266 static void w32_frame_rehighlight P_ ((struct frame *));
267 static void x_frame_rehighlight P_ ((struct w32_display_info *));
268 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
269 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
270 enum text_cursor_kinds));
271 static void w32_clip_to_row P_ ((struct window *, struct glyph_row *, int, HDC));
272 static BOOL my_show_window P_ ((struct frame *, HWND, int));
273 static void my_set_window_pos P_ ((HWND, HWND, int, int, int, int, UINT));
274 static void my_set_focus P_ ((struct frame *, HWND));
275 static void my_set_foreground_window P_ ((HWND));
276 static void my_destroy_window P_ ((struct frame *, HWND));
278 static Lisp_Object Qvendor_specific_keysyms;
281 /***********************************************************************
282 Debugging
283 ***********************************************************************/
285 #if 0
287 /* This is a function useful for recording debugging information about
288 the sequence of occurrences in this file. */
290 struct record
292 char *locus;
293 int type;
296 struct record event_record[100];
298 int event_record_index;
300 record_event (locus, type)
301 char *locus;
302 int type;
304 if (event_record_index == sizeof (event_record) / sizeof (struct record))
305 event_record_index = 0;
307 event_record[event_record_index].locus = locus;
308 event_record[event_record_index].type = type;
309 event_record_index++;
312 #endif /* 0 */
315 void
316 XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
317 XGCValues *xgcv)
319 if (mask & GCForeground)
320 gc->foreground = xgcv->foreground;
321 if (mask & GCBackground)
322 gc->background = xgcv->background;
323 if (mask & GCFont)
324 gc->font = xgcv->font;
327 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
328 XGCValues *xgcv)
330 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
331 bzero (gc, sizeof (XGCValues));
333 XChangeGC (ignore, gc, mask, xgcv);
335 return gc;
338 void
339 XGetGCValues (void* ignore, XGCValues *gc,
340 unsigned long mask, XGCValues *xgcv)
342 XChangeGC (ignore, xgcv, mask, gc);
345 static void
346 w32_set_clip_rectangle (HDC hdc, RECT *rect)
348 if (rect)
350 HRGN clip_region = CreateRectRgnIndirect (rect);
351 SelectClipRgn (hdc, clip_region);
352 DeleteObject (clip_region);
354 else
355 SelectClipRgn (hdc, NULL);
359 /* Draw a hollow rectangle at the specified position. */
360 void
361 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
362 int width, int height)
364 HBRUSH hb, oldhb;
365 HPEN hp, oldhp;
367 hb = CreateSolidBrush (gc->background);
368 hp = CreatePen (PS_SOLID, 0, gc->foreground);
369 oldhb = SelectObject (hdc, hb);
370 oldhp = SelectObject (hdc, hp);
372 Rectangle (hdc, x, y, x + width, y + height);
374 SelectObject (hdc, oldhb);
375 SelectObject (hdc, oldhp);
376 DeleteObject (hb);
377 DeleteObject (hp);
380 /* Draw a filled rectangle at the specified position. */
381 void
382 w32_fill_rect (f, hdc, pix, lprect)
383 FRAME_PTR f;
384 HDC hdc;
385 COLORREF pix;
386 RECT * lprect;
388 HBRUSH hb;
390 hb = CreateSolidBrush (pix);
391 FillRect (hdc, lprect, hb);
392 DeleteObject (hb);
395 void
396 w32_clear_window (f)
397 FRAME_PTR f;
399 RECT rect;
400 HDC hdc = get_frame_dc (f);
402 /* Under certain conditions, this can be called at startup with
403 a console frame pointer before the GUI frame is created. An HDC
404 of 0 indicates this. */
405 if (hdc)
407 GetClientRect (FRAME_W32_WINDOW (f), &rect);
408 w32_clear_rect (f, hdc, &rect);
411 release_frame_dc (f, hdc);
414 #define OPAQUE_FRAME 255
416 void
417 x_set_frame_alpha (f)
418 struct frame *f;
420 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
421 double alpha = 1.0;
422 double alpha_min = 1.0;
423 BYTE opac;
424 LONG ex_style;
425 HWND window = FRAME_W32_WINDOW (f);
427 /* Older versions of Windows do not support transparency. */
428 if (!pfnSetLayeredWindowAttributes)
429 return;
431 if (dpyinfo->x_highlight_frame == f)
432 alpha = f->alpha[0];
433 else
434 alpha = f->alpha[1];
436 if (FLOATP (Vframe_alpha_lower_limit))
437 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
438 else if (INTEGERP (Vframe_alpha_lower_limit))
439 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
441 if (alpha < 0.0)
442 return;
443 else if (alpha > 1.0)
444 alpha = 1.0;
445 else if (alpha < alpha_min && alpha_min <= 1.0)
446 alpha = alpha_min;
448 opac = alpha * OPAQUE_FRAME;
450 ex_style = GetWindowLong (window, GWL_EXSTYLE);
452 if (opac == OPAQUE_FRAME)
453 ex_style &= ~WS_EX_LAYERED;
454 else
455 ex_style |= WS_EX_LAYERED;
457 SetWindowLong (window, GWL_EXSTYLE, ex_style);
459 if (opac != OPAQUE_FRAME)
460 pfnSetLayeredWindowAttributes (window, 0, opac, LWA_ALPHA);
464 x_display_pixel_height (dpyinfo)
465 struct w32_display_info *dpyinfo;
467 return GetDeviceCaps (GetDC (GetDesktopWindow ()), VERTRES);
471 x_display_pixel_width (dpyinfo)
472 struct w32_display_info *dpyinfo;
474 return GetDeviceCaps (GetDC (GetDesktopWindow ()), HORZRES);
478 /***********************************************************************
479 Starting and ending an update
480 ***********************************************************************/
482 /* Start an update of frame F. This function is installed as a hook
483 for update_begin, i.e. it is called when update_begin is called.
484 This function is called prior to calls to x_update_window_begin for
485 each window being updated. */
487 static void
488 x_update_begin (f)
489 struct frame *f;
491 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
493 if (! FRAME_W32_P (f))
494 return;
496 /* Regenerate display palette before drawing if list of requested
497 colors has changed. */
498 if (display_info->regen_palette)
500 w32_regenerate_palette (f);
501 display_info->regen_palette = FALSE;
506 /* Start update of window W. Set the global variable updated_window
507 to the window being updated and set output_cursor to the cursor
508 position of W. */
510 static void
511 x_update_window_begin (w)
512 struct window *w;
514 struct frame *f = XFRAME (WINDOW_FRAME (w));
515 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
517 /* Hide the system caret during an update. */
518 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
520 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
523 updated_window = w;
524 set_output_cursor (&w->cursor);
526 BLOCK_INPUT;
528 if (f == display_info->mouse_face_mouse_frame)
530 /* Don't do highlighting for mouse motion during the update. */
531 display_info->mouse_face_defer = 1;
533 /* If F needs to be redrawn, simply forget about any prior mouse
534 highlighting. */
535 if (FRAME_GARBAGED_P (f))
536 display_info->mouse_face_window = Qnil;
538 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
539 their mouse_face_p flag set, which means that they are always
540 unequal to rows in a desired matrix which never have that
541 flag set. So, rows containing mouse-face glyphs are never
542 scrolled, and we don't have to switch the mouse highlight off
543 here to prevent it from being scrolled. */
545 /* Can we tell that this update does not affect the window
546 where the mouse highlight is? If so, no need to turn off.
547 Likewise, don't do anything if the frame is garbaged;
548 in that case, the frame's current matrix that we would use
549 is all wrong, and we will redisplay that line anyway. */
550 if (!NILP (display_info->mouse_face_window)
551 && w == XWINDOW (display_info->mouse_face_window))
553 int i;
555 for (i = 0; i < w->desired_matrix->nrows; ++i)
556 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
557 break;
559 if (i < w->desired_matrix->nrows)
560 clear_mouse_face (display_info);
562 #endif /* 0 */
565 UNBLOCK_INPUT;
568 /* Draw a vertical window border from (x,y0) to (x,y1) */
570 static void
571 w32_draw_vertical_window_border (w, x, y0, y1)
572 struct window *w;
573 int x, y0, y1;
575 struct frame *f = XFRAME (WINDOW_FRAME (w));
576 RECT r;
577 HDC hdc;
578 struct face *face;
580 r.left = x;
581 r.right = x + 1;
582 r.top = y0;
583 r.bottom = y1;
585 hdc = get_frame_dc (f);
586 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
587 if (face)
588 w32_fill_rect (f, hdc, face->foreground, &r);
589 else
590 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
592 release_frame_dc (f, hdc);
596 /* End update of window W (which is equal to updated_window).
598 Draw vertical borders between horizontally adjacent windows, and
599 display W's cursor if CURSOR_ON_P is non-zero.
601 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
602 glyphs in mouse-face were overwritten. In that case we have to
603 make sure that the mouse-highlight is properly redrawn.
605 W may be a menu bar pseudo-window in case we don't have X toolkit
606 support. Such windows don't have a cursor, so don't display it
607 here. */
609 static void
610 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
611 struct window *w;
612 int cursor_on_p, mouse_face_overwritten_p;
614 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
616 if (!w->pseudo_window_p)
618 BLOCK_INPUT;
620 if (cursor_on_p)
621 display_and_set_cursor (w, 1, output_cursor.hpos,
622 output_cursor.vpos,
623 output_cursor.x, output_cursor.y);
625 if (draw_window_fringes (w, 1))
626 x_draw_vertical_border (w);
628 UNBLOCK_INPUT;
631 /* If a row with mouse-face was overwritten, arrange for
632 XTframe_up_to_date to redisplay the mouse highlight. */
633 if (mouse_face_overwritten_p)
635 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
636 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
637 dpyinfo->mouse_face_window = Qnil;
640 /* Unhide the caret. This won't actually show the cursor, unless it
641 was visible before the corresponding call to HideCaret in
642 x_update_window_begin. */
643 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
645 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
648 updated_window = NULL;
652 /* End update of frame F. This function is installed as a hook in
653 update_end. */
655 static void
656 x_update_end (f)
657 struct frame *f;
659 if (! FRAME_W32_P (f))
660 return;
662 /* Mouse highlight may be displayed again. */
663 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
667 /* This function is called from various places in xdisp.c whenever a
668 complete update has been performed. The global variable
669 updated_window is not available here. */
671 static void
672 w32_frame_up_to_date (f)
673 struct frame *f;
675 if (FRAME_W32_P (f))
677 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
679 if (dpyinfo->mouse_face_deferred_gc
680 || f == dpyinfo->mouse_face_mouse_frame)
682 BLOCK_INPUT;
683 if (dpyinfo->mouse_face_mouse_frame)
684 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
685 dpyinfo->mouse_face_mouse_x,
686 dpyinfo->mouse_face_mouse_y);
687 dpyinfo->mouse_face_deferred_gc = 0;
688 UNBLOCK_INPUT;
694 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
695 arrow bitmaps, or clear the fringes if no bitmaps are required
696 before DESIRED_ROW is made current. The window being updated is
697 found in updated_window. This function is called from
698 update_window_line only if it is known that there are differences
699 between bitmaps to be drawn between current row and DESIRED_ROW. */
701 static void
702 x_after_update_window_line (desired_row)
703 struct glyph_row *desired_row;
705 struct window *w = updated_window;
706 struct frame *f;
707 int width, height;
709 xassert (w);
711 if (!desired_row->mode_line_p && !w->pseudo_window_p)
712 desired_row->redraw_fringe_bitmaps_p = 1;
714 /* When a window has disappeared, make sure that no rest of
715 full-width rows stays visible in the internal border. Could
716 check here if updated_window is the leftmost/rightmost window,
717 but I guess it's not worth doing since vertically split windows
718 are almost never used, internal border is rarely set, and the
719 overhead is very small. */
720 if (windows_or_buffers_changed
721 && desired_row->full_width_p
722 && (f = XFRAME (w->frame),
723 width = FRAME_INTERNAL_BORDER_WIDTH (f),
724 width != 0)
725 && (height = desired_row->visible_height,
726 height > 0))
728 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
730 /* Internal border is drawn below the tool bar. */
731 if (WINDOWP (f->tool_bar_window)
732 && w == XWINDOW (f->tool_bar_window))
733 y -= width;
735 BLOCK_INPUT;
737 HDC hdc = get_frame_dc (f);
738 w32_clear_area (f, hdc, 0, y, width, height);
739 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
740 y, width, height);
741 release_frame_dc (f, hdc);
743 UNBLOCK_INPUT;
748 /* Draw the bitmap WHICH in one of the left or right fringes of
749 window W. ROW is the glyph row for which to display the bitmap; it
750 determines the vertical position at which the bitmap has to be
751 drawn. */
753 static void
754 w32_draw_fringe_bitmap (w, row, p)
755 struct window *w;
756 struct glyph_row *row;
757 struct draw_fringe_bitmap_params *p;
759 struct frame *f = XFRAME (WINDOW_FRAME (w));
760 HDC hdc;
761 struct face *face = p->face;
762 int rowY;
764 hdc = get_frame_dc (f);
766 if (!p->overlay_p)
768 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
770 /* If the fringe is adjacent to the left (right) scroll bar of a
771 leftmost (rightmost, respectively) window, then extend its
772 background to the gap between the fringe and the bar. */
773 if ((WINDOW_LEFTMOST_P (w)
774 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
775 || (WINDOW_RIGHTMOST_P (w)
776 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
778 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
780 if (sb_width > 0)
782 int left = WINDOW_SCROLL_BAR_AREA_X (w);
783 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
784 * FRAME_COLUMN_WIDTH (f));
786 if (bx < 0)
788 /* Bitmap fills the fringe. */
789 if (left + width == p->x)
790 bx = left + sb_width;
791 else if (p->x + p->wd == left)
792 bx = left;
793 if (bx >= 0)
795 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
797 nx = width - sb_width;
798 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
799 row->y));
800 ny = row->visible_height;
803 else
805 if (left + width == bx)
807 bx = left + sb_width;
808 nx += width - sb_width;
810 else if (bx + nx == left)
811 nx += width - sb_width;
816 if (bx >= 0 && nx > 0)
817 w32_fill_area (f, hdc, face->background, bx, by, nx, ny);
820 /* Must clip because of partially visible lines. */
821 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
822 if (p->y < rowY)
824 /* Adjust position of "bottom aligned" bitmap on partially
825 visible last row. */
826 int oldY = row->y;
827 int oldVH = row->visible_height;
828 row->visible_height = p->h;
829 row->y -= rowY - p->y;
830 w32_clip_to_row (w, row, -1, hdc);
831 row->y = oldY;
832 row->visible_height = oldVH;
834 else
835 w32_clip_to_row (w, row, -1, hdc);
837 if (p->which && p->which < max_fringe_bmp)
839 HBITMAP pixmap = fringe_bmp[p->which];
840 HDC compat_hdc;
841 HANDLE horig_obj;
843 compat_hdc = CreateCompatibleDC (hdc);
845 SaveDC (hdc);
847 horig_obj = SelectObject (compat_hdc, pixmap);
849 /* Paint overlays transparently. */
850 if (p->overlay_p)
852 HBRUSH h_brush, h_orig_brush;
854 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
855 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
856 h_brush = CreateSolidBrush (face->foreground);
857 h_orig_brush = SelectObject (hdc, h_brush);
859 BitBlt (hdc, p->x, p->y, p->wd, p->h,
860 compat_hdc, 0, p->dh,
861 DSTINVERT);
862 BitBlt (hdc, p->x, p->y, p->wd, p->h,
863 compat_hdc, 0, p->dh,
864 0x2E064A);
865 BitBlt (hdc, p->x, p->y, p->wd, p->h,
866 compat_hdc, 0, p->dh,
867 DSTINVERT);
869 SelectObject (hdc, h_orig_brush);
870 DeleteObject (h_brush);
872 else
874 SetTextColor (hdc, face->background);
875 SetBkColor (hdc, (p->cursor_p
876 ? f->output_data.w32->cursor_pixel
877 : face->foreground));
879 BitBlt (hdc, p->x, p->y, p->wd, p->h,
880 compat_hdc, 0, p->dh,
881 SRCCOPY);
884 SelectObject (compat_hdc, horig_obj);
885 DeleteDC (compat_hdc);
886 RestoreDC (hdc, -1);
889 w32_set_clip_rectangle (hdc, NULL);
891 release_frame_dc (f, hdc);
894 static void
895 w32_define_fringe_bitmap (which, bits, h, wd)
896 int which;
897 unsigned short *bits;
898 int h, wd;
900 if (which >= max_fringe_bmp)
902 int i = max_fringe_bmp;
903 max_fringe_bmp = which + 20;
904 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
905 while (i < max_fringe_bmp)
906 fringe_bmp[i++] = 0;
909 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
912 static void
913 w32_destroy_fringe_bitmap (which)
914 int which;
916 if (which >= max_fringe_bmp)
917 return;
919 if (fringe_bmp[which])
920 DeleteObject (fringe_bmp[which]);
921 fringe_bmp[which] = 0;
926 /* This is called when starting Emacs and when restarting after
927 suspend. When starting Emacs, no window is mapped. And nothing
928 must be done to Emacs's own window if it is suspended (though that
929 rarely happens). */
931 static void
932 w32_set_terminal_modes (struct terminal *term)
936 /* This is called when exiting or suspending Emacs. Exiting will make
937 the W32 windows go away, and suspending requires no action. */
939 static void
940 w32_reset_terminal_modes (struct terminal *term)
946 /***********************************************************************
947 Display Iterator
948 ***********************************************************************/
950 /* Function prototypes of this page. */
952 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
953 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
954 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
955 int));
956 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
957 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
958 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
959 static void x_draw_glyph_string P_ ((struct glyph_string *));
960 static void x_set_cursor_gc P_ ((struct glyph_string *));
961 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
962 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
963 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
964 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
965 double, int, COLORREF));
966 static void x_setup_relief_colors P_ ((struct glyph_string *));
967 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
968 static void x_draw_image_relief P_ ((struct glyph_string *));
969 static void x_draw_image_foreground P_ ((struct glyph_string *));
970 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
971 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
972 int, int, int));
973 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
974 int, int, int, int, int, int,
975 RECT *));
976 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
977 int, int, int, RECT *));
980 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
981 face. */
983 static void
984 x_set_cursor_gc (s)
985 struct glyph_string *s;
987 if (s->font == FRAME_FONT (s->f)
988 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
989 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
990 && !s->cmp)
991 s->gc = s->f->output_data.w32->cursor_gc;
992 else
994 /* Cursor on non-default face: must merge. */
995 XGCValues xgcv;
996 unsigned long mask;
998 xgcv.background = s->f->output_data.w32->cursor_pixel;
999 xgcv.foreground = s->face->background;
1001 /* If the glyph would be invisible, try a different foreground. */
1002 if (xgcv.foreground == xgcv.background)
1003 xgcv.foreground = s->face->foreground;
1004 if (xgcv.foreground == xgcv.background)
1005 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
1006 if (xgcv.foreground == xgcv.background)
1007 xgcv.foreground = s->face->foreground;
1009 /* Make sure the cursor is distinct from text in this face. */
1010 if (xgcv.background == s->face->background
1011 && xgcv.foreground == s->face->foreground)
1013 xgcv.background = s->face->foreground;
1014 xgcv.foreground = s->face->background;
1017 IF_DEBUG (x_check_font (s->f, s->font));
1018 xgcv.font = s->font;
1019 mask = GCForeground | GCBackground | GCFont;
1021 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1022 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1023 mask, &xgcv);
1024 else
1025 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1026 = XCreateGC (NULL, s->window, mask, &xgcv);
1028 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1033 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1035 static void
1036 x_set_mouse_face_gc (s)
1037 struct glyph_string *s;
1039 int face_id;
1040 struct face *face;
1042 /* What face has to be used last for the mouse face? */
1043 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
1044 face = FACE_FROM_ID (s->f, face_id);
1045 if (face == NULL)
1046 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1048 if (s->first_glyph->type == CHAR_GLYPH)
1049 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1050 else
1051 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1052 s->face = FACE_FROM_ID (s->f, face_id);
1053 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1055 /* If font in this face is same as S->font, use it. */
1056 if (s->font == s->face->font)
1057 s->gc = s->face->gc;
1058 else
1060 /* Otherwise construct scratch_cursor_gc with values from FACE
1061 but font FONT. */
1062 XGCValues xgcv;
1063 unsigned long mask;
1065 xgcv.background = s->face->background;
1066 xgcv.foreground = s->face->foreground;
1067 IF_DEBUG (x_check_font (s->f, s->font));
1068 xgcv.font = s->font;
1069 mask = GCForeground | GCBackground | GCFont;
1071 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1072 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1073 mask, &xgcv);
1074 else
1075 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1076 = XCreateGC (NULL, s->window, mask, &xgcv);
1078 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1081 xassert (s->gc != 0);
1085 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1086 Faces to use in the mode line have already been computed when the
1087 matrix was built, so there isn't much to do, here. */
1089 static INLINE void
1090 x_set_mode_line_face_gc (s)
1091 struct glyph_string *s;
1093 s->gc = s->face->gc;
1097 /* Set S->gc of glyph string S for drawing that glyph string. Set
1098 S->stippled_p to a non-zero value if the face of S has a stipple
1099 pattern. */
1101 static INLINE void
1102 x_set_glyph_string_gc (s)
1103 struct glyph_string *s;
1105 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1107 if (s->hl == DRAW_NORMAL_TEXT)
1109 s->gc = s->face->gc;
1110 s->stippled_p = s->face->stipple != 0;
1112 else if (s->hl == DRAW_INVERSE_VIDEO)
1114 x_set_mode_line_face_gc (s);
1115 s->stippled_p = s->face->stipple != 0;
1117 else if (s->hl == DRAW_CURSOR)
1119 x_set_cursor_gc (s);
1120 s->stippled_p = 0;
1122 else if (s->hl == DRAW_MOUSE_FACE)
1124 x_set_mouse_face_gc (s);
1125 s->stippled_p = s->face->stipple != 0;
1127 else if (s->hl == DRAW_IMAGE_RAISED
1128 || s->hl == DRAW_IMAGE_SUNKEN)
1130 s->gc = s->face->gc;
1131 s->stippled_p = s->face->stipple != 0;
1133 else
1135 s->gc = s->face->gc;
1136 s->stippled_p = s->face->stipple != 0;
1139 /* GC must have been set. */
1140 xassert (s->gc != 0);
1144 /* Set clipping for output of glyph string S. S may be part of a mode
1145 line or menu if we don't have X toolkit support. */
1147 static INLINE void
1148 x_set_glyph_string_clipping (s)
1149 struct glyph_string *s;
1151 RECT *r = s->clip;
1152 int n = get_glyph_string_clip_rects (s, r, 2);
1154 if (n == 1)
1155 w32_set_clip_rectangle (s->hdc, r);
1156 else if (n > 1)
1158 HRGN full_clip, clip1, clip2;
1159 clip1 = CreateRectRgnIndirect (r);
1160 clip2 = CreateRectRgnIndirect (r + 1);
1161 if (CombineRgn (full_clip, clip1, clip2, RGN_OR) != ERROR)
1163 SelectClipRgn (s->hdc, full_clip);
1165 DeleteObject (clip1);
1166 DeleteObject (clip2);
1167 DeleteObject (full_clip);
1169 s->num_clips = n;
1172 /* Set SRC's clipping for output of glyph string DST. This is called
1173 when we are drawing DST's left_overhang or right_overhang only in
1174 the area of SRC. */
1176 static void
1177 x_set_glyph_string_clipping_exactly (src, dst)
1178 struct glyph_string *src, *dst;
1180 RECT r;
1182 r.left = src->x;
1183 r.right = r.left + src->width;
1184 r.top = src->y;
1185 r.bottom = r.top + src->height;
1186 dst->clip[0] = r;
1187 dst->num_clips = 1;
1188 w32_set_clip_rectangle (dst->hdc, &r);
1191 /* RIF:
1192 Compute left and right overhang of glyph string S. */
1194 static void
1195 w32_compute_glyph_string_overhangs (s)
1196 struct glyph_string *s;
1198 if (s->cmp == NULL
1199 && s->first_glyph->type == CHAR_GLYPH
1200 && !s->font_not_found_p)
1202 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1203 struct font *font = s->font;
1204 struct font_metrics metrics;
1205 int i;
1207 for (i = 0; i < s->nchars; i++)
1208 code[i] = s->char2b[i];
1209 font->driver->text_extents (font, code, s->nchars, &metrics);
1210 s->right_overhang = (metrics.rbearing > metrics.width
1211 ? metrics.rbearing - metrics.width : 0);
1212 s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
1214 else if (s->cmp)
1216 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1217 s->left_overhang = -s->cmp->lbearing;
1221 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1223 static INLINE void
1224 x_clear_glyph_string_rect (s, x, y, w, h)
1225 struct glyph_string *s;
1226 int x, y, w, h;
1228 int real_x = x;
1229 int real_y = y;
1230 int real_w = w;
1231 int real_h = h;
1232 #if 0
1233 /* Take clipping into account. */
1234 if (s->gc->clip_mask == Rect)
1236 real_x = max (real_x, s->gc->clip_rectangle.left);
1237 real_y = max (real_y, s->gc->clip_rectangle.top);
1238 real_w = min (real_w, s->gc->clip_rectangle.right
1239 - s->gc->clip_rectangle.left);
1240 real_h = min (real_h, s->gc->clip_rectangle.bottom
1241 - s->gc->clip_rectangle.top);
1243 #endif
1244 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1245 real_w, real_h);
1249 /* Draw the background of glyph_string S. If S->background_filled_p
1250 is non-zero don't draw it. FORCE_P non-zero means draw the
1251 background even if it wouldn't be drawn normally. This is used
1252 when a string preceding S draws into the background of S, or S
1253 contains the first component of a composition. */
1255 static void
1256 x_draw_glyph_string_background (s, force_p)
1257 struct glyph_string *s;
1258 int force_p;
1260 /* Nothing to do if background has already been drawn or if it
1261 shouldn't be drawn in the first place. */
1262 if (!s->background_filled_p)
1264 int box_line_width = max (s->face->box_line_width, 0);
1266 #if 0 /* TODO: stipple */
1267 if (s->stippled_p)
1269 /* Fill background with a stipple pattern. */
1270 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1271 XFillRectangle (s->display, s->window, s->gc, s->x,
1272 s->y + box_line_width,
1273 s->background_width,
1274 s->height - 2 * box_line_width);
1275 XSetFillStyle (s->display, s->gc, FillSolid);
1276 s->background_filled_p = 1;
1278 else
1279 #endif
1280 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1281 || s->font_not_found_p
1282 || s->extends_to_end_of_line_p
1283 || force_p)
1285 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1286 s->background_width,
1287 s->height - 2 * box_line_width);
1288 s->background_filled_p = 1;
1294 /* Draw the foreground of glyph string S. */
1296 static void
1297 x_draw_glyph_string_foreground (s)
1298 struct glyph_string *s;
1300 int i, x;
1302 /* If first glyph of S has a left box line, start drawing the text
1303 of S to the right of that box line. */
1304 if (s->face->box != FACE_NO_BOX
1305 && s->first_glyph->left_box_line_p)
1306 x = s->x + eabs (s->face->box_line_width);
1307 else
1308 x = s->x;
1310 SetTextColor (s->hdc, s->gc->foreground);
1311 SetBkColor (s->hdc, s->gc->background);
1312 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1314 /* Draw characters of S as rectangles if S's font could not be
1315 loaded. */
1316 if (s->font_not_found_p)
1318 for (i = 0; i < s->nchars; ++i)
1320 struct glyph *g = s->first_glyph + i;
1322 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1323 s->height - 1);
1324 x += g->pixel_width;
1327 else
1329 struct font *font = s->font;
1330 int boff = font->baseline_offset;
1331 int y;
1332 HFONT old_font;
1334 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1336 if (font->vertical_centering)
1337 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1339 y = s->ybase - boff;
1340 if (s->for_overlaps
1341 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1342 font->driver->draw (s, 0, s->nchars, x, y, 0);
1343 else
1344 font->driver->draw (s, 0, s->nchars, x, y, 1);
1345 if (s->face->overstrike)
1346 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1348 SelectObject (s->hdc, old_font);
1352 /* Draw the foreground of composite glyph string S. */
1354 static void
1355 x_draw_composite_glyph_string_foreground (s)
1356 struct glyph_string *s;
1358 int i, j, x;
1359 struct font *font = s->font;
1361 /* If first glyph of S has a left box line, start drawing the text
1362 of S to the right of that box line. */
1363 if (s->face && s->face->box != FACE_NO_BOX
1364 && s->first_glyph->left_box_line_p)
1365 x = s->x + eabs (s->face->box_line_width);
1366 else
1367 x = s->x;
1369 /* S is a glyph string for a composition. S->cmp_from is the index
1370 of the first character drawn for glyphs of this composition.
1371 S->cmp_from == 0 means we are drawing the very first character of
1372 this composition. */
1374 SetTextColor (s->hdc, s->gc->foreground);
1375 SetBkColor (s->hdc, s->gc->background);
1376 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1378 /* Draw a rectangle for the composition if the font for the very
1379 first character of the composition could not be loaded. */
1380 if (s->font_not_found_p)
1382 if (s->cmp_from == 0)
1383 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1384 s->height - 1);
1386 else if (! s->first_glyph->u.cmp.automatic)
1388 int y = s->ybase;
1389 int width = 0;
1390 HFONT old_font;
1392 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1394 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1395 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1397 int xx = x + s->cmp->offsets[j * 2];
1398 int yy = y - s->cmp->offsets[j * 2 + 1];
1400 font->driver->draw (s, j, j + 1, xx, yy, 0);
1401 if (s->face->overstrike)
1402 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1404 SelectObject (s->hdc, old_font);
1406 else
1408 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1409 Lisp_Object glyph;
1410 int y = s->ybase;
1411 int width = 0;
1412 HFONT old_font;
1414 old_font = SelectObject (s->hdc, FONT_HANDLE (font));
1416 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1418 glyph = LGSTRING_GLYPH (gstring, i);
1419 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1420 width += LGLYPH_WIDTH (glyph);
1421 else
1423 int xoff, yoff, wadjust;
1425 if (j < i)
1427 font->driver->draw (s, j, i, x, y, 0);
1428 x += width;
1430 xoff = LGLYPH_XOFF (glyph);
1431 yoff = LGLYPH_YOFF (glyph);
1432 wadjust = LGLYPH_WADJUST (glyph);
1433 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1434 x += wadjust;
1435 j = i + 1;
1436 width = 0;
1439 if (j < i)
1440 font->driver->draw (s, j, i, x, y, 0);
1442 SelectObject (s->hdc, old_font);
1447 /* Brightness beyond which a color won't have its highlight brightness
1448 boosted.
1450 Nominally, highlight colors for `3d' faces are calculated by
1451 brightening an object's color by a constant scale factor, but this
1452 doesn't yield good results for dark colors, so for colors who's
1453 brightness is less than this value (on a scale of 0-255) have to
1454 use an additional additive factor.
1456 The value here is set so that the default menu-bar/mode-line color
1457 (grey75) will not have its highlights changed at all. */
1458 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1461 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1462 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1463 If this produces the same color as COLOR, try a color where all RGB
1464 values have DELTA added. Return the allocated color in *COLOR.
1465 DISPLAY is the X display, CMAP is the colormap to operate on.
1466 Value is non-zero if successful. */
1468 static int
1469 w32_alloc_lighter_color (f, color, factor, delta)
1470 struct frame *f;
1471 COLORREF *color;
1472 double factor;
1473 int delta;
1475 COLORREF new;
1476 long bright;
1478 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1479 delta /= 256;
1481 /* Change RGB values by specified FACTOR. Avoid overflow! */
1482 xassert (factor >= 0);
1483 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1484 min (0xff, factor * GetGValue (*color)),
1485 min (0xff, factor * GetBValue (*color)));
1487 /* Calculate brightness of COLOR. */
1488 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1489 + GetBValue (*color)) / 6;
1491 /* We only boost colors that are darker than
1492 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1493 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1494 /* Make an additive adjustment to NEW, because it's dark enough so
1495 that scaling by FACTOR alone isn't enough. */
1497 /* How far below the limit this color is (0 - 1, 1 being darker). */
1498 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1499 /* The additive adjustment. */
1500 int min_delta = delta * dimness * factor / 2;
1502 if (factor < 1)
1503 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1504 max (0, min (0xff, min_delta - GetGValue (*color))),
1505 max (0, min (0xff, min_delta - GetBValue (*color))));
1506 else
1507 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1508 max (0, min (0xff, min_delta + GetGValue (*color))),
1509 max (0, min (0xff, min_delta + GetBValue (*color))));
1512 if (new == *color)
1513 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1514 max (0, min (0xff, delta + GetGValue (*color))),
1515 max (0, min (0xff, delta + GetBValue (*color))));
1517 /* TODO: Map to palette and retry with delta if same? */
1518 /* TODO: Free colors (if using palette)? */
1520 if (new == *color)
1521 return 0;
1523 *color = new;
1525 return 1;
1528 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1529 colors in COLORS. On W32, we no longer try to map colors to
1530 a palette. */
1531 void
1532 x_query_colors (f, colors, ncolors)
1533 struct frame *f;
1534 XColor *colors;
1535 int ncolors;
1537 int i;
1539 for (i = 0; i < ncolors; i++)
1541 DWORD pixel = colors[i].pixel;
1542 /* Convert to a 16 bit value in range 0 - 0xffff. */
1543 colors[i].red = GetRValue (pixel) * 257;
1544 colors[i].green = GetGValue (pixel) * 257;
1545 colors[i].blue = GetBValue (pixel) * 257;
1549 void
1550 x_query_color (f, color)
1551 struct frame *f;
1552 XColor *color;
1554 x_query_colors (f, color, 1);
1558 /* Set up the foreground color for drawing relief lines of glyph
1559 string S. RELIEF is a pointer to a struct relief containing the GC
1560 with which lines will be drawn. Use a color that is FACTOR or
1561 DELTA lighter or darker than the relief's background which is found
1562 in S->f->output_data.x->relief_background. If such a color cannot
1563 be allocated, use DEFAULT_PIXEL, instead. */
1565 static void
1566 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
1567 struct frame *f;
1568 struct relief *relief;
1569 double factor;
1570 int delta;
1571 COLORREF default_pixel;
1573 XGCValues xgcv;
1574 struct w32_output *di = f->output_data.w32;
1575 unsigned long mask = GCForeground;
1576 COLORREF pixel;
1577 COLORREF background = di->relief_background;
1578 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
1580 /* TODO: Free colors (if using palette)? */
1582 /* Allocate new color. */
1583 xgcv.foreground = default_pixel;
1584 pixel = background;
1585 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1587 relief->allocated_p = 1;
1588 xgcv.foreground = relief->pixel = pixel;
1591 if (relief->gc == 0)
1593 #if 0 /* TODO: stipple */
1594 xgcv.stipple = dpyinfo->gray;
1595 mask |= GCStipple;
1596 #endif
1597 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1599 else
1600 XChangeGC (NULL, relief->gc, mask, &xgcv);
1604 /* Set up colors for the relief lines around glyph string S. */
1606 static void
1607 x_setup_relief_colors (s)
1608 struct glyph_string *s;
1610 struct w32_output *di = s->f->output_data.w32;
1611 COLORREF color;
1613 if (s->face->use_box_color_for_shadows_p)
1614 color = s->face->box_color;
1615 else if (s->first_glyph->type == IMAGE_GLYPH
1616 && s->img->pixmap
1617 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1618 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1619 else
1620 color = s->gc->background;
1622 if (di->white_relief.gc == 0
1623 || color != di->relief_background)
1625 di->relief_background = color;
1626 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1627 WHITE_PIX_DEFAULT (s->f));
1628 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1629 BLACK_PIX_DEFAULT (s->f));
1634 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1635 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1636 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1637 relief. LEFT_P non-zero means draw a relief on the left side of
1638 the rectangle. RIGHT_P non-zero means draw a relief on the right
1639 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1640 when drawing. */
1642 static void
1643 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1644 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
1645 struct frame *f;
1646 int left_x, top_y, right_x, bottom_y, width;
1647 int top_p, bot_p, left_p, right_p, raised_p;
1648 RECT *clip_rect;
1650 int i;
1651 XGCValues gc;
1652 HDC hdc = get_frame_dc (f);
1654 if (raised_p)
1655 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1656 else
1657 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1659 w32_set_clip_rectangle (hdc, clip_rect);
1661 /* Top. */
1662 if (top_p)
1663 for (i = 0; i < width; ++i)
1664 w32_fill_area (f, hdc, gc.foreground,
1665 left_x + i * left_p, top_y + i,
1666 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1668 /* Left. */
1669 if (left_p)
1670 for (i = 0; i < width; ++i)
1671 w32_fill_area (f, hdc, gc.foreground,
1672 left_x + i, top_y + i, 1,
1673 bottom_y - top_y - 2 * i + 1);
1675 if (raised_p)
1676 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1677 else
1678 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1680 /* Bottom. */
1681 if (bot_p)
1682 for (i = 0; i < width; ++i)
1683 w32_fill_area (f, hdc, gc.foreground,
1684 left_x + i * left_p, bottom_y - i,
1685 right_x - left_x - i * (left_p + right_p) + 1, 1);
1687 /* Right. */
1688 if (right_p)
1689 for (i = 0; i < width; ++i)
1690 w32_fill_area (f, hdc, gc.foreground,
1691 right_x - i, top_y + i + 1, 1,
1692 bottom_y - top_y - 2 * i - 1);
1694 w32_set_clip_rectangle (hdc, NULL);
1696 release_frame_dc (f, hdc);
1700 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1701 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1702 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1703 left side of the rectangle. RIGHT_P non-zero means draw a line
1704 on the right side of the rectangle. CLIP_RECT is the clipping
1705 rectangle to use when drawing. */
1707 static void
1708 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1709 left_p, right_p, clip_rect)
1710 struct glyph_string *s;
1711 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
1712 RECT *clip_rect;
1714 w32_set_clip_rectangle (s->hdc, clip_rect);
1716 /* Top. */
1717 w32_fill_area (s->f, s->hdc, s->face->box_color,
1718 left_x, top_y, right_x - left_x + 1, width);
1720 /* Left. */
1721 if (left_p)
1723 w32_fill_area (s->f, s->hdc, s->face->box_color,
1724 left_x, top_y, width, bottom_y - top_y + 1);
1727 /* Bottom. */
1728 w32_fill_area (s->f, s->hdc, s->face->box_color,
1729 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1731 /* Right. */
1732 if (right_p)
1734 w32_fill_area (s->f, s->hdc, s->face->box_color,
1735 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1738 w32_set_clip_rectangle (s->hdc, NULL);
1742 /* Draw a box around glyph string S. */
1744 static void
1745 x_draw_glyph_string_box (s)
1746 struct glyph_string *s;
1748 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1749 int left_p, right_p;
1750 struct glyph *last_glyph;
1751 RECT clip_rect;
1753 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1754 ? WINDOW_RIGHT_EDGE_X (s->w)
1755 : window_box_right (s->w, s->area));
1757 /* The glyph that may have a right box line. */
1758 last_glyph = (s->cmp || s->img
1759 ? s->first_glyph
1760 : s->first_glyph + s->nchars - 1);
1762 width = eabs (s->face->box_line_width);
1763 raised_p = s->face->box == FACE_RAISED_BOX;
1764 left_x = s->x;
1765 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1766 ? last_x - 1
1767 : min (last_x, s->x + s->background_width) - 1));
1768 top_y = s->y;
1769 bottom_y = top_y + s->height - 1;
1771 left_p = (s->first_glyph->left_box_line_p
1772 || (s->hl == DRAW_MOUSE_FACE
1773 && (s->prev == NULL
1774 || s->prev->hl != s->hl)));
1775 right_p = (last_glyph->right_box_line_p
1776 || (s->hl == DRAW_MOUSE_FACE
1777 && (s->next == NULL
1778 || s->next->hl != s->hl)));
1780 get_glyph_string_clip_rect (s, &clip_rect);
1782 if (s->face->box == FACE_SIMPLE_BOX)
1783 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1784 left_p, right_p, &clip_rect);
1785 else
1787 x_setup_relief_colors (s);
1788 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1789 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1794 /* Draw foreground of image glyph string S. */
1796 static void
1797 x_draw_image_foreground (s)
1798 struct glyph_string *s;
1800 int x = s->x;
1801 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1803 /* If first glyph of S has a left box line, start drawing it to the
1804 right of that line. */
1805 if (s->face->box != FACE_NO_BOX
1806 && s->first_glyph->left_box_line_p
1807 && s->slice.x == 0)
1808 x += eabs (s->face->box_line_width);
1810 /* If there is a margin around the image, adjust x- and y-position
1811 by that margin. */
1812 if (s->slice.x == 0)
1813 x += s->img->hmargin;
1814 if (s->slice.y == 0)
1815 y += s->img->vmargin;
1817 SaveDC (s->hdc);
1819 if (s->img->pixmap)
1821 HDC compat_hdc = CreateCompatibleDC (s->hdc);
1822 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1823 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
1824 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1825 SetBkColor (compat_hdc, RGB (255, 255, 255));
1826 SetTextColor (s->hdc, RGB (0, 0, 0));
1827 x_set_glyph_string_clipping (s);
1829 if (s->img->mask)
1831 HDC mask_dc = CreateCompatibleDC (s->hdc);
1832 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1834 SetTextColor (s->hdc, RGB (255, 255, 255));
1835 SetBkColor (s->hdc, RGB (0, 0, 0));
1837 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1838 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1839 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1840 mask_dc, s->slice.x, s->slice.y, SRCAND);
1841 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1842 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1844 SelectObject (mask_dc, mask_orig_obj);
1845 DeleteDC (mask_dc);
1847 else
1849 SetTextColor (s->hdc, s->gc->foreground);
1850 SetBkColor (s->hdc, s->gc->background);
1852 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
1853 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1855 /* When the image has a mask, we can expect that at
1856 least part of a mouse highlight or a block cursor will
1857 be visible. If the image doesn't have a mask, make
1858 a block cursor visible by drawing a rectangle around
1859 the image. I believe it's looking better if we do
1860 nothing here for mouse-face. */
1861 if (s->hl == DRAW_CURSOR)
1863 int r = s->img->relief;
1864 if (r < 0) r = -r;
1865 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
1866 s->slice.width + r*2 - 1,
1867 s->slice.height + r*2 - 1);
1871 w32_set_clip_rectangle (s->hdc, NULL);
1872 SelectObject (s->hdc, orig_brush);
1873 DeleteObject (fg_brush);
1874 SelectObject (compat_hdc, orig_obj);
1875 DeleteDC (compat_hdc);
1877 else
1878 w32_draw_rectangle (s->hdc, s->gc, x, y,
1879 s->slice.width - 1, s->slice.height - 1);
1881 RestoreDC (s->hdc ,-1);
1885 /* Draw a relief around the image glyph string S. */
1887 static void
1888 x_draw_image_relief (s)
1889 struct glyph_string *s;
1891 int x0, y0, x1, y1, thick, raised_p;
1892 RECT r;
1893 int x = s->x;
1894 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1896 /* If first glyph of S has a left box line, start drawing it to the
1897 right of that line. */
1898 if (s->face->box != FACE_NO_BOX
1899 && s->first_glyph->left_box_line_p
1900 && s->slice.x == 0)
1901 x += eabs (s->face->box_line_width);
1903 /* If there is a margin around the image, adjust x- and y-position
1904 by that margin. */
1905 if (s->slice.x == 0)
1906 x += s->img->hmargin;
1907 if (s->slice.y == 0)
1908 y += s->img->vmargin;
1910 if (s->hl == DRAW_IMAGE_SUNKEN
1911 || s->hl == DRAW_IMAGE_RAISED)
1913 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
1914 raised_p = s->hl == DRAW_IMAGE_RAISED;
1916 else
1918 thick = eabs (s->img->relief);
1919 raised_p = s->img->relief > 0;
1922 x0 = x - thick;
1923 y0 = y - thick;
1924 x1 = x + s->slice.width + thick - 1;
1925 y1 = y + s->slice.height + thick - 1;
1927 x_setup_relief_colors (s);
1928 get_glyph_string_clip_rect (s, &r);
1929 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
1930 s->slice.y == 0,
1931 s->slice.y + s->slice.height == s->img->height,
1932 s->slice.x == 0,
1933 s->slice.x + s->slice.width == s->img->width,
1934 &r);
1938 /* Draw the foreground of image glyph string S to PIXMAP. */
1940 static void
1941 w32_draw_image_foreground_1 (s, pixmap)
1942 struct glyph_string *s;
1943 HBITMAP pixmap;
1945 HDC hdc = CreateCompatibleDC (s->hdc);
1946 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
1947 int x = 0;
1948 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
1950 /* If first glyph of S has a left box line, start drawing it to the
1951 right of that line. */
1952 if (s->face->box != FACE_NO_BOX
1953 && s->first_glyph->left_box_line_p
1954 && s->slice.x == 0)
1955 x += eabs (s->face->box_line_width);
1957 /* If there is a margin around the image, adjust x- and y-position
1958 by that margin. */
1959 if (s->slice.x == 0)
1960 x += s->img->hmargin;
1961 if (s->slice.y == 0)
1962 y += s->img->vmargin;
1964 if (s->img->pixmap)
1966 HDC compat_hdc = CreateCompatibleDC (hdc);
1967 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
1968 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
1969 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
1971 if (s->img->mask)
1973 HDC mask_dc = CreateCompatibleDC (hdc);
1974 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
1976 SetTextColor (hdc, RGB (0, 0, 0));
1977 SetBkColor (hdc, RGB (255, 255, 255));
1978 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1979 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1980 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1981 mask_dc, s->slice.x, s->slice.y, SRCAND);
1982 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1983 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
1985 SelectObject (mask_dc, mask_orig_obj);
1986 DeleteDC (mask_dc);
1988 else
1990 SetTextColor (hdc, s->gc->foreground);
1991 SetBkColor (hdc, s->gc->background);
1993 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
1994 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
1996 /* When the image has a mask, we can expect that at
1997 least part of a mouse highlight or a block cursor will
1998 be visible. If the image doesn't have a mask, make
1999 a block cursor visible by drawing a rectangle around
2000 the image. I believe it's looking better if we do
2001 nothing here for mouse-face. */
2002 if (s->hl == DRAW_CURSOR)
2004 int r = s->img->relief;
2005 if (r < 0) r = -r;
2006 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2007 s->slice.width + r*2 - 1,
2008 s->slice.height + r*2 - 1);
2012 SelectObject (hdc, orig_brush);
2013 DeleteObject (fg_brush);
2014 SelectObject (compat_hdc, orig_obj);
2015 DeleteDC (compat_hdc);
2017 else
2018 w32_draw_rectangle (hdc, s->gc, x, y,
2019 s->slice.width - 1, s->slice.height - 1);
2021 SelectObject (hdc, orig_hdc_obj);
2022 DeleteDC (hdc);
2026 /* Draw part of the background of glyph string S. X, Y, W, and H
2027 give the rectangle to draw. */
2029 static void
2030 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2031 struct glyph_string *s;
2032 int x, y, w, h;
2034 #if 0 /* TODO: stipple */
2035 if (s->stippled_p)
2037 /* Fill background with a stipple pattern. */
2038 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2039 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2040 XSetFillStyle (s->display, s->gc, FillSolid);
2042 else
2043 #endif
2044 x_clear_glyph_string_rect (s, x, y, w, h);
2048 /* Draw image glyph string S.
2050 s->y
2051 s->x +-------------------------
2052 | s->face->box
2054 | +-------------------------
2055 | | s->img->vmargin
2057 | | +-------------------
2058 | | | the image
2062 static void
2063 x_draw_image_glyph_string (s)
2064 struct glyph_string *s;
2066 int x, y;
2067 int box_line_hwidth = eabs (s->face->box_line_width);
2068 int box_line_vwidth = max (s->face->box_line_width, 0);
2069 int height;
2070 HBITMAP pixmap = 0;
2072 height = s->height - 2 * box_line_vwidth;
2074 /* Fill background with face under the image. Do it only if row is
2075 taller than image or if image has a clip mask to reduce
2076 flickering. */
2077 s->stippled_p = s->face->stipple != 0;
2078 if (height > s->slice.height
2079 || s->img->hmargin
2080 || s->img->vmargin
2081 || s->img->mask
2082 || s->img->pixmap == 0
2083 || s->width != s->background_width)
2085 x = s->x;
2086 if (s->first_glyph->left_box_line_p
2087 && s->slice.x == 0)
2088 x += box_line_hwidth;
2090 y = s->y;
2091 if (s->slice.y == 0)
2092 y += box_line_vwidth;
2094 #if 0 /* TODO: figure out if we need to do this on Windows. */
2095 if (s->img->mask)
2097 /* Create a pixmap as large as the glyph string. Fill it
2098 with the background color. Copy the image to it, using
2099 its mask. Copy the temporary pixmap to the display. */
2100 Screen *screen = FRAME_X_SCREEN (s->f);
2101 int depth = DefaultDepthOfScreen (screen);
2103 /* Create a pixmap as large as the glyph string. */
2104 pixmap = XCreatePixmap (s->display, s->window,
2105 s->background_width,
2106 s->height, depth);
2108 /* Don't clip in the following because we're working on the
2109 pixmap. */
2110 XSetClipMask (s->display, s->gc, None);
2112 /* Fill the pixmap with the background color/stipple. */
2113 if (s->stippled_p)
2115 /* Fill background with a stipple pattern. */
2116 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2117 XFillRectangle (s->display, pixmap, s->gc,
2118 0, 0, s->background_width, s->height);
2119 XSetFillStyle (s->display, s->gc, FillSolid);
2121 else
2123 XGCValues xgcv;
2124 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2125 &xgcv);
2126 XSetForeground (s->display, s->gc, xgcv.background);
2127 XFillRectangle (s->display, pixmap, s->gc,
2128 0, 0, s->background_width, s->height);
2129 XSetForeground (s->display, s->gc, xgcv.foreground);
2132 else
2133 #endif
2134 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2136 s->background_filled_p = 1;
2139 /* Draw the foreground. */
2140 if (pixmap != 0)
2142 w32_draw_image_foreground_1 (s, pixmap);
2143 x_set_glyph_string_clipping (s);
2145 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2146 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2147 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2148 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2150 SetTextColor (s->hdc, s->gc->foreground);
2151 SetBkColor (s->hdc, s->gc->background);
2152 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2153 compat_hdc, 0, 0, SRCCOPY);
2155 SelectObject (s->hdc, orig_brush);
2156 DeleteObject (fg_brush);
2157 SelectObject (compat_hdc, orig_obj);
2158 DeleteDC (compat_hdc);
2160 DeleteObject (pixmap);
2161 pixmap = 0;
2163 else
2164 x_draw_image_foreground (s);
2166 /* If we must draw a relief around the image, do it. */
2167 if (s->img->relief
2168 || s->hl == DRAW_IMAGE_RAISED
2169 || s->hl == DRAW_IMAGE_SUNKEN)
2170 x_draw_image_relief (s);
2174 /* Draw stretch glyph string S. */
2176 static void
2177 x_draw_stretch_glyph_string (s)
2178 struct glyph_string *s;
2180 xassert (s->first_glyph->type == STRETCH_GLYPH);
2182 if (s->hl == DRAW_CURSOR
2183 && !x_stretch_cursor_p)
2185 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2186 as wide as the stretch glyph. */
2187 int width, background_width = s->background_width;
2188 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2190 if (x < left_x)
2192 background_width -= left_x - x;
2193 x = left_x;
2195 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2197 /* Draw cursor. */
2198 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2200 /* Clear rest using the GC of the original non-cursor face. */
2201 if (width < background_width)
2203 XGCValues *gc = s->face->gc;
2204 int y = s->y;
2205 int w = background_width - width, h = s->height;
2206 RECT r;
2207 HDC hdc = s->hdc;
2209 x += width;
2210 if (s->row->mouse_face_p
2211 && cursor_in_mouse_face_p (s->w))
2213 x_set_mouse_face_gc (s);
2214 gc = s->gc;
2216 else
2217 gc = s->face->gc;
2219 get_glyph_string_clip_rect (s, &r);
2220 w32_set_clip_rectangle (hdc, &r);
2222 #if 0 /* TODO: stipple */
2223 if (s->face->stipple)
2225 /* Fill background with a stipple pattern. */
2226 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2227 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2228 XSetFillStyle (s->display, gc, FillSolid);
2230 else
2231 #endif
2233 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2237 else if (!s->background_filled_p)
2239 int background_width = s->background_width;
2240 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2242 /* Don't draw into left margin, fringe or scrollbar area
2243 except for header line and mode line. */
2244 if (x < left_x && !s->row->mode_line_p)
2246 background_width -= left_x - x;
2247 x = left_x;
2249 if (background_width > 0)
2250 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2253 s->background_filled_p = 1;
2257 /* Draw glyph string S. */
2259 static void
2260 x_draw_glyph_string (s)
2261 struct glyph_string *s;
2263 int relief_drawn_p = 0;
2265 /* If S draws into the background of its successor, draw the
2266 background of the successor first so that S can draw into it.
2267 This makes S->next use XDrawString instead of XDrawImageString. */
2268 if (s->next && s->right_overhang && !s->for_overlaps)
2270 int width;
2271 struct glyph_string *next;
2272 for (width = 0, next = s->next;
2273 next && width < s->right_overhang;
2274 width += next->width, next = next->next)
2275 if (next->first_glyph->type != IMAGE_GLYPH)
2277 x_set_glyph_string_gc (next);
2278 x_set_glyph_string_clipping (next);
2279 if (next->first_glyph->type == STRETCH_GLYPH)
2280 x_draw_stretch_glyph_string (next);
2281 else
2282 x_draw_glyph_string_background (next, 1);
2283 next->num_clips = 0;
2287 /* Set up S->gc, set clipping and draw S. */
2288 x_set_glyph_string_gc (s);
2290 /* Draw relief (if any) in advance for char/composition so that the
2291 glyph string can be drawn over it. */
2292 if (!s->for_overlaps
2293 && s->face->box != FACE_NO_BOX
2294 && (s->first_glyph->type == CHAR_GLYPH
2295 || s->first_glyph->type == COMPOSITE_GLYPH))
2298 x_set_glyph_string_clipping (s);
2299 x_draw_glyph_string_background (s, 1);
2300 x_draw_glyph_string_box (s);
2301 x_set_glyph_string_clipping (s);
2302 relief_drawn_p = 1;
2304 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2305 && !s->clip_tail
2306 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2307 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2308 /* We must clip just this glyph. left_overhang part has already
2309 drawn when s->prev was drawn, and right_overhang part will be
2310 drawn later when s->next is drawn. */
2311 x_set_glyph_string_clipping_exactly (s, s);
2312 else
2313 x_set_glyph_string_clipping (s);
2315 switch (s->first_glyph->type)
2317 case IMAGE_GLYPH:
2318 x_draw_image_glyph_string (s);
2319 break;
2321 case STRETCH_GLYPH:
2322 x_draw_stretch_glyph_string (s);
2323 break;
2325 case CHAR_GLYPH:
2326 if (s->for_overlaps)
2327 s->background_filled_p = 1;
2328 else
2329 x_draw_glyph_string_background (s, 0);
2330 x_draw_glyph_string_foreground (s);
2331 break;
2333 case COMPOSITE_GLYPH:
2334 if (s->for_overlaps || (s->cmp_from > 0
2335 && ! s->first_glyph->u.cmp.automatic))
2336 s->background_filled_p = 1;
2337 else
2338 x_draw_glyph_string_background (s, 1);
2339 x_draw_composite_glyph_string_foreground (s);
2340 break;
2342 default:
2343 abort ();
2346 if (!s->for_overlaps)
2348 /* Draw underline. */
2349 if (s->face->underline_p)
2351 unsigned long thickness, position;
2352 int y;
2354 if (s->prev && s->prev->face->underline_p)
2356 /* We use the same underline style as the previous one. */
2357 thickness = s->prev->underline_thickness;
2358 position = s->prev->underline_position;
2360 else
2362 /* Get the underline thickness. Default is 1 pixel. */
2363 if (s->font && s->font->underline_thickness > 0)
2364 thickness = s->font->underline_thickness;
2365 else
2366 thickness = 1;
2367 if (x_underline_at_descent_line)
2368 position = (s->height - thickness) - (s->ybase - s->y);
2369 else
2371 /* Get the underline position. This is the recommended
2372 vertical offset in pixels from the baseline to the top of
2373 the underline. This is a signed value according to the
2374 specs, and its default is
2376 ROUND ((maximum_descent) / 2), with
2377 ROUND (x) = floor (x + 0.5) */
2379 if (x_use_underline_position_properties
2380 && s->font && s->font->underline_position >= 0)
2381 position = s->font->underline_position;
2382 else if (s->font)
2383 position = (s->font->descent + 1) / 2;
2385 position = max (position, underline_minimum_offset);
2387 /* Check the sanity of thickness and position. We should
2388 avoid drawing underline out of the current line area. */
2389 if (s->y + s->height <= s->ybase + position)
2390 position = (s->height - 1) - (s->ybase - s->y);
2391 if (s->y + s->height < s->ybase + position + thickness)
2392 thickness = (s->y + s->height) - (s->ybase + position);
2393 s->underline_thickness = thickness;
2394 s->underline_position =position;
2395 y = s->ybase + position;
2396 if (s->face->underline_defaulted_p)
2398 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2399 y, s->background_width, 1);
2401 else
2403 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2404 y, s->background_width, 1);
2407 /* Draw overline. */
2408 if (s->face->overline_p)
2410 unsigned long dy = 0, h = 1;
2412 if (s->face->overline_color_defaulted_p)
2414 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2415 s->y + dy, s->background_width, h);
2417 else
2419 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2420 s->y + dy, s->background_width, h);
2424 /* Draw strike-through. */
2425 if (s->face->strike_through_p
2426 && !FONT_TEXTMETRIC(s->font).tmStruckOut)
2428 unsigned long h = 1;
2429 unsigned long dy = (s->height - h) / 2;
2431 if (s->face->strike_through_color_defaulted_p)
2433 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2434 s->width, h);
2436 else
2438 w32_fill_area (s->f, s->hdc, s->face->strike_through_color, s->x,
2439 s->y + dy, s->width, h);
2443 /* Draw relief if not yet drawn. */
2444 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2445 x_draw_glyph_string_box (s);
2447 if (s->prev)
2449 struct glyph_string *prev;
2451 for (prev = s->prev; prev; prev = prev->prev)
2452 if (prev->hl != s->hl
2453 && prev->x + prev->width + prev->right_overhang > s->x)
2455 /* As prev was drawn while clipped to its own area, we
2456 must draw the right_overhang part using s->hl now. */
2457 enum draw_glyphs_face save = prev->hl;
2459 prev->hl = s->hl;
2460 x_set_glyph_string_gc (prev);
2461 x_set_glyph_string_clipping_exactly (s, prev);
2462 if (prev->first_glyph->type == CHAR_GLYPH)
2463 x_draw_glyph_string_foreground (prev);
2464 else
2465 x_draw_composite_glyph_string_foreground (prev);
2466 w32_set_clip_rectangle (prev->hdc, NULL);
2467 prev->hl = save;
2468 prev->num_clips = 0;
2472 if (s->next)
2474 struct glyph_string *next;
2476 for (next = s->next; next; next = next->next)
2477 if (next->hl != s->hl
2478 && next->x - next->left_overhang < s->x + s->width)
2480 /* As next will be drawn while clipped to its own area,
2481 we must draw the left_overhang part using s->hl now. */
2482 enum draw_glyphs_face save = next->hl;
2484 next->hl = s->hl;
2485 x_set_glyph_string_gc (next);
2486 x_set_glyph_string_clipping_exactly (s, next);
2487 if (next->first_glyph->type == CHAR_GLYPH)
2488 x_draw_glyph_string_foreground (next);
2489 else
2490 x_draw_composite_glyph_string_foreground (next);
2491 w32_set_clip_rectangle (next->hdc, NULL);
2492 next->hl = save;
2493 next->num_clips = 0;
2498 /* Reset clipping. */
2499 w32_set_clip_rectangle (s->hdc, NULL);
2500 s->num_clips = 0;
2504 /* Shift display to make room for inserted glyphs. */
2506 void
2507 w32_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2508 struct frame *f;
2509 int x, y, width, height, shift_by;
2511 HDC hdc;
2513 hdc = get_frame_dc (f);
2514 BitBlt (hdc, x + shift_by, y, width, height,
2515 hdc, x, y, SRCCOPY);
2517 release_frame_dc (f, hdc);
2521 /* Delete N glyphs at the nominal cursor position. Not implemented
2522 for X frames. */
2524 static void
2525 x_delete_glyphs (f, n)
2526 struct frame *f;
2527 register int n;
2529 if (! FRAME_W32_P (f))
2530 return;
2532 abort ();
2536 /* Clear entire frame. If updating_frame is non-null, clear that
2537 frame. Otherwise clear the selected frame. */
2539 static void
2540 x_clear_frame (struct frame *f)
2542 if (! FRAME_W32_P (f))
2543 return;
2545 /* Clearing the frame will erase any cursor, so mark them all as no
2546 longer visible. */
2547 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2548 output_cursor.hpos = output_cursor.vpos = 0;
2549 output_cursor.x = -1;
2551 /* We don't set the output cursor here because there will always
2552 follow an explicit cursor_to. */
2553 BLOCK_INPUT;
2555 w32_clear_window (f);
2557 /* We have to clear the scroll bars, too. If we have changed
2558 colors or something like that, then they should be notified. */
2559 x_scroll_bar_clear (f);
2561 UNBLOCK_INPUT;
2565 /* Make audible bell. */
2567 static void
2568 w32_ring_bell (struct frame *f)
2570 BLOCK_INPUT;
2572 if (FRAME_W32_P (f) && visible_bell)
2574 int i;
2575 HWND hwnd = FRAME_W32_WINDOW (f);
2577 for (i = 0; i < 5; i++)
2579 FlashWindow (hwnd, TRUE);
2580 Sleep (10);
2582 FlashWindow (hwnd, FALSE);
2584 else
2585 w32_sys_ring_bell (f);
2587 UNBLOCK_INPUT;
2591 /* Specify how many text lines, from the top of the window,
2592 should be affected by insert-lines and delete-lines operations.
2593 This, and those operations, are used only within an update
2594 that is bounded by calls to x_update_begin and x_update_end. */
2596 static void
2597 w32_set_terminal_window (n)
2598 register int n;
2600 /* This function intentionally left blank. */
2604 /***********************************************************************
2605 Line Dance
2606 ***********************************************************************/
2608 /* Perform an insert-lines or delete-lines operation, inserting N
2609 lines or deleting -N lines at vertical position VPOS. */
2611 static void
2612 x_ins_del_lines (f, vpos, n)
2613 struct frame *f;
2614 int vpos, n;
2616 if (! FRAME_W32_P (f))
2617 return;
2619 abort ();
2623 /* Scroll part of the display as described by RUN. */
2625 static void
2626 x_scroll_run (w, run)
2627 struct window *w;
2628 struct run *run;
2630 struct frame *f = XFRAME (w->frame);
2631 int x, y, width, height, from_y, to_y, bottom_y;
2632 HWND hwnd = FRAME_W32_WINDOW (f);
2633 HRGN expect_dirty;
2635 /* Get frame-relative bounding box of the text display area of W,
2636 without mode lines. Include in this box the left and right
2637 fringes of W. */
2638 window_box (w, -1, &x, &y, &width, &height);
2640 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2641 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2642 bottom_y = y + height;
2644 if (to_y < from_y)
2646 /* Scrolling up. Make sure we don't copy part of the mode
2647 line at the bottom. */
2648 if (from_y + run->height > bottom_y)
2649 height = bottom_y - from_y;
2650 else
2651 height = run->height;
2652 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2654 else
2656 /* Scolling down. Make sure we don't copy over the mode line.
2657 at the bottom. */
2658 if (to_y + run->height > bottom_y)
2659 height = bottom_y - to_y;
2660 else
2661 height = run->height;
2662 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2665 BLOCK_INPUT;
2667 /* Cursor off. Will be switched on again in x_update_window_end. */
2668 updated_window = w;
2669 x_clear_cursor (w);
2672 RECT from;
2673 RECT to;
2674 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2675 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2677 from.left = to.left = x;
2678 from.right = to.right = x + width;
2679 from.top = from_y;
2680 from.bottom = from_y + height;
2681 to.top = y;
2682 to.bottom = bottom_y;
2684 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2685 NULL, SW_INVALIDATE);
2687 /* Combine this with what we expect to be dirty. This covers the
2688 case where not all of the region we expect is actually dirty. */
2689 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2691 /* If the dirty region is not what we expected, redraw the entire frame. */
2692 if (!EqualRgn (combined, expect_dirty))
2693 SET_FRAME_GARBAGED (f);
2695 DeleteObject (dirty);
2696 DeleteObject (combined);
2699 UNBLOCK_INPUT;
2700 DeleteObject (expect_dirty);
2705 /***********************************************************************
2706 Exposure Events
2707 ***********************************************************************/
2709 static void
2710 frame_highlight (f)
2711 struct frame *f;
2713 x_update_cursor (f, 1);
2714 x_set_frame_alpha (f);
2717 static void
2718 frame_unhighlight (f)
2719 struct frame *f;
2721 x_update_cursor (f, 1);
2722 x_set_frame_alpha (f);
2725 /* The focus has changed. Update the frames as necessary to reflect
2726 the new situation. Note that we can't change the selected frame
2727 here, because the Lisp code we are interrupting might become confused.
2728 Each event gets marked with the frame in which it occurred, so the
2729 Lisp code can tell when the switch took place by examining the events. */
2731 static void
2732 x_new_focus_frame (dpyinfo, frame)
2733 struct w32_display_info *dpyinfo;
2734 struct frame *frame;
2736 struct frame *old_focus = dpyinfo->w32_focus_frame;
2738 if (frame != dpyinfo->w32_focus_frame)
2740 /* Set this before calling other routines, so that they see
2741 the correct value of w32_focus_frame. */
2742 dpyinfo->w32_focus_frame = frame;
2744 if (old_focus && old_focus->auto_lower)
2745 x_lower_frame (old_focus);
2747 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2748 pending_autoraise_frame = dpyinfo->w32_focus_frame;
2749 else
2750 pending_autoraise_frame = 0;
2753 x_frame_rehighlight (dpyinfo);
2757 /* Handle FocusIn and FocusOut state changes for FRAME.
2758 If FRAME has focus and there exists more than one frame, puts
2759 a FOCUS_IN_EVENT into *BUFP. */
2761 static void
2762 x_focus_changed (type, state, dpyinfo, frame, bufp)
2763 int type;
2764 int state;
2765 struct w32_display_info *dpyinfo;
2766 struct frame *frame;
2767 struct input_event *bufp;
2769 if (type == WM_SETFOCUS)
2771 if (dpyinfo->w32_focus_event_frame != frame)
2773 x_new_focus_frame (dpyinfo, frame);
2774 dpyinfo->w32_focus_event_frame = frame;
2776 /* Don't stop displaying the initial startup message
2777 for a switch-frame event we don't need. */
2778 if (NILP (Vterminal_frame)
2779 && CONSP (Vframe_list)
2780 && !NILP (XCDR (Vframe_list)))
2782 bufp->kind = FOCUS_IN_EVENT;
2783 XSETFRAME (bufp->frame_or_window, frame);
2787 frame->output_data.x->focus_state |= state;
2789 /* TODO: IME focus? */
2791 else if (type == WM_KILLFOCUS)
2793 frame->output_data.x->focus_state &= ~state;
2795 if (dpyinfo->w32_focus_event_frame == frame)
2797 dpyinfo->w32_focus_event_frame = 0;
2798 x_new_focus_frame (dpyinfo, 0);
2801 /* TODO: IME focus? */
2806 /* The focus may have changed. Figure out if it is a real focus change,
2807 by checking both FocusIn/Out and Enter/LeaveNotify events.
2809 Returns FOCUS_IN_EVENT event in *BUFP. */
2811 static void
2812 w32_detect_focus_change (dpyinfo, event, bufp)
2813 struct w32_display_info *dpyinfo;
2814 W32Msg *event;
2815 struct input_event *bufp;
2817 struct frame *frame;
2819 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2820 if (! frame)
2821 return;
2823 /* On w32, this is only called from focus events, so no switch needed. */
2824 x_focus_changed (event->msg.message,
2825 (event->msg.message == WM_KILLFOCUS ?
2826 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2827 dpyinfo, frame, bufp);
2831 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2833 void
2834 x_mouse_leave (dpyinfo)
2835 struct w32_display_info *dpyinfo;
2837 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2840 /* The focus has changed, or we have redirected a frame's focus to
2841 another frame (this happens when a frame uses a surrogate
2842 mini-buffer frame). Shift the highlight as appropriate.
2844 The FRAME argument doesn't necessarily have anything to do with which
2845 frame is being highlighted or un-highlighted; we only use it to find
2846 the appropriate X display info. */
2848 static void
2849 w32_frame_rehighlight (frame)
2850 struct frame *frame;
2852 if (! FRAME_W32_P (frame))
2853 return;
2854 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
2857 static void
2858 x_frame_rehighlight (dpyinfo)
2859 struct w32_display_info *dpyinfo;
2861 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2863 if (dpyinfo->w32_focus_frame)
2865 dpyinfo->x_highlight_frame
2866 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2867 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2868 : dpyinfo->w32_focus_frame);
2869 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2871 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
2872 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2875 else
2876 dpyinfo->x_highlight_frame = 0;
2878 if (dpyinfo->x_highlight_frame != old_highlight)
2880 if (old_highlight)
2881 frame_unhighlight (old_highlight);
2882 if (dpyinfo->x_highlight_frame)
2883 frame_highlight (dpyinfo->x_highlight_frame);
2887 /* Keyboard processing - modifier keys, etc. */
2889 /* Convert a keysym to its name. */
2891 char *
2892 x_get_keysym_name (keysym)
2893 int keysym;
2895 /* Make static so we can always return it */
2896 static char value[100];
2898 BLOCK_INPUT;
2899 GetKeyNameText (keysym, value, 100);
2900 UNBLOCK_INPUT;
2902 return value;
2907 /* Mouse clicks and mouse movement. Rah. */
2909 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2910 the state in PUP. XBUTTON provides extra information for extended mouse
2911 button messages. Returns FALSE if unable to parse the message. */
2912 BOOL
2913 parse_button (message, xbutton, pbutton, pup)
2914 int message;
2915 int xbutton;
2916 int * pbutton;
2917 int * pup;
2919 int button = 0;
2920 int up = 0;
2922 switch (message)
2924 case WM_LBUTTONDOWN:
2925 button = 0;
2926 up = 0;
2927 break;
2928 case WM_LBUTTONUP:
2929 button = 0;
2930 up = 1;
2931 break;
2932 case WM_MBUTTONDOWN:
2933 if (NILP (Vw32_swap_mouse_buttons))
2934 button = 1;
2935 else
2936 button = 2;
2937 up = 0;
2938 break;
2939 case WM_MBUTTONUP:
2940 if (NILP (Vw32_swap_mouse_buttons))
2941 button = 1;
2942 else
2943 button = 2;
2944 up = 1;
2945 break;
2946 case WM_RBUTTONDOWN:
2947 if (NILP (Vw32_swap_mouse_buttons))
2948 button = 2;
2949 else
2950 button = 1;
2951 up = 0;
2952 break;
2953 case WM_RBUTTONUP:
2954 if (NILP (Vw32_swap_mouse_buttons))
2955 button = 2;
2956 else
2957 button = 1;
2958 up = 1;
2959 break;
2960 case WM_XBUTTONDOWN:
2961 button = xbutton + 2;
2962 up = 0;
2963 break;
2964 case WM_XBUTTONUP:
2965 button = xbutton + 2;
2966 up = 1;
2967 break;
2968 default:
2969 return (FALSE);
2972 if (pup) *pup = up;
2973 if (pbutton) *pbutton = button;
2975 return (TRUE);
2979 /* Prepare a mouse-event in *RESULT for placement in the input queue.
2981 If the event is a button press, then note that we have grabbed
2982 the mouse. */
2984 static Lisp_Object
2985 construct_mouse_click (result, msg, f)
2986 struct input_event *result;
2987 W32Msg *msg;
2988 struct frame *f;
2990 int button;
2991 int up;
2993 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
2994 &button, &up);
2996 /* Make the event type NO_EVENT; we'll change that when we decide
2997 otherwise. */
2998 result->kind = MOUSE_CLICK_EVENT;
2999 result->code = button;
3000 result->timestamp = msg->msg.time;
3001 result->modifiers = (msg->dwModifiers
3002 | (up
3003 ? up_modifier
3004 : down_modifier));
3006 XSETINT (result->x, LOWORD (msg->msg.lParam));
3007 XSETINT (result->y, HIWORD (msg->msg.lParam));
3008 XSETFRAME (result->frame_or_window, f);
3009 result->arg = Qnil;
3010 return Qnil;
3013 static Lisp_Object
3014 construct_mouse_wheel (result, msg, f)
3015 struct input_event *result;
3016 W32Msg *msg;
3017 struct frame *f;
3019 POINT p;
3020 int delta;
3022 result->kind = msg->msg.message == WM_MOUSEHWHEEL ? HORIZ_WHEEL_EVENT
3023 : WHEEL_EVENT;
3024 result->code = 0;
3025 result->timestamp = msg->msg.time;
3027 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3028 forward, away from the user (up); a negative value indicates that
3029 the wheel was rotated backward, toward the user (down). */
3030 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3032 /* The up and down modifiers indicate if the wheel was rotated up or
3033 down based on WHEEL_DELTA value. */
3034 result->modifiers = (msg->dwModifiers
3035 | ((delta < 0 ) ? down_modifier : up_modifier));
3037 /* With multiple monitors, we can legitimately get negative
3038 coordinates, so cast to short to interpret them correctly. */
3039 p.x = (short) LOWORD (msg->msg.lParam);
3040 p.y = (short) HIWORD (msg->msg.lParam);
3041 ScreenToClient (msg->msg.hwnd, &p);
3042 XSETINT (result->x, p.x);
3043 XSETINT (result->y, p.y);
3044 XSETFRAME (result->frame_or_window, f);
3045 result->arg = Qnil;
3046 return Qnil;
3049 static Lisp_Object
3050 construct_drag_n_drop (result, msg, f)
3051 struct input_event *result;
3052 W32Msg *msg;
3053 struct frame *f;
3055 Lisp_Object files;
3056 Lisp_Object frame;
3057 HDROP hdrop;
3058 POINT p;
3059 WORD num_files;
3060 char *name;
3061 int i, len;
3063 result->kind = DRAG_N_DROP_EVENT;
3064 result->code = 0;
3065 result->timestamp = msg->msg.time;
3066 result->modifiers = msg->dwModifiers;
3068 hdrop = (HDROP) msg->msg.wParam;
3069 DragQueryPoint (hdrop, &p);
3071 #if 0
3072 p.x = LOWORD (msg->msg.lParam);
3073 p.y = HIWORD (msg->msg.lParam);
3074 ScreenToClient (msg->msg.hwnd, &p);
3075 #endif
3077 XSETINT (result->x, p.x);
3078 XSETINT (result->y, p.y);
3080 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3081 files = Qnil;
3083 for (i = 0; i < num_files; i++)
3085 len = DragQueryFile (hdrop, i, NULL, 0);
3086 if (len <= 0)
3087 continue;
3088 name = alloca (len + 1);
3089 DragQueryFile (hdrop, i, name, len + 1);
3090 files = Fcons (DECODE_FILE (build_string (name)), files);
3093 DragFinish (hdrop);
3095 XSETFRAME (frame, f);
3096 result->frame_or_window = frame;
3097 result->arg = files;
3098 return Qnil;
3102 /* Function to report a mouse movement to the mainstream Emacs code.
3103 The input handler calls this.
3105 We have received a mouse movement event, which is given in *event.
3106 If the mouse is over a different glyph than it was last time, tell
3107 the mainstream emacs code by setting mouse_moved. If not, ask for
3108 another motion event, so we can check again the next time it moves. */
3110 static MSG last_mouse_motion_event;
3111 static Lisp_Object last_mouse_motion_frame;
3113 static int
3114 note_mouse_movement (frame, msg)
3115 FRAME_PTR frame;
3116 MSG *msg;
3118 int mouse_x = LOWORD (msg->lParam);
3119 int mouse_y = HIWORD (msg->lParam);
3121 last_mouse_movement_time = msg->time;
3122 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
3123 XSETFRAME (last_mouse_motion_frame, frame);
3125 if (!FRAME_X_OUTPUT (frame))
3126 return 0;
3128 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3130 frame->mouse_moved = 1;
3131 last_mouse_scroll_bar = Qnil;
3132 note_mouse_highlight (frame, -1, -1);
3133 last_mouse_glyph_frame = 0;
3134 return 1;
3137 /* Has the mouse moved off the glyph it was on at the last sighting? */
3138 if (frame != last_mouse_glyph_frame
3139 || mouse_x < last_mouse_glyph.left
3140 || mouse_x >= last_mouse_glyph.right
3141 || mouse_y < last_mouse_glyph.top
3142 || mouse_y >= last_mouse_glyph.bottom)
3144 frame->mouse_moved = 1;
3145 last_mouse_scroll_bar = Qnil;
3146 note_mouse_highlight (frame, mouse_x, mouse_y);
3147 /* Remember the mouse position here, as w32_mouse_position only
3148 gets called when mouse tracking is enabled but we also need
3149 to keep track of the mouse for help_echo and highlighting at
3150 other times. */
3151 remember_mouse_glyph (frame, mouse_x, mouse_y, &last_mouse_glyph);
3152 last_mouse_glyph_frame = frame;
3153 return 1;
3156 return 0;
3160 /************************************************************************
3161 Mouse Face
3162 ************************************************************************/
3164 static struct scroll_bar *x_window_to_scroll_bar ();
3165 static void x_scroll_bar_report_motion ();
3166 static void x_check_fullscreen P_ ((struct frame *));
3168 static void
3169 redo_mouse_highlight ()
3171 if (!NILP (last_mouse_motion_frame)
3172 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3173 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3174 LOWORD (last_mouse_motion_event.lParam),
3175 HIWORD (last_mouse_motion_event.lParam));
3178 static void
3179 w32_define_cursor (window, cursor)
3180 Window window;
3181 Cursor cursor;
3183 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3185 /* Return the current position of the mouse.
3186 *fp should be a frame which indicates which display to ask about.
3188 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3189 and *part to the frame, window, and scroll bar part that the mouse
3190 is over. Set *x and *y to the portion and whole of the mouse's
3191 position on the scroll bar.
3193 If the mouse movement started elsewhere, set *fp to the frame the
3194 mouse is on, *bar_window to nil, and *x and *y to the character cell
3195 the mouse is over.
3197 Set *time to the server time-stamp for the time at which the mouse
3198 was at this position.
3200 Don't store anything if we don't have a valid set of values to report.
3202 This clears the mouse_moved flag, so we can wait for the next mouse
3203 movement. */
3205 static void
3206 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
3207 FRAME_PTR *fp;
3208 int insist;
3209 Lisp_Object *bar_window;
3210 enum scroll_bar_part *part;
3211 Lisp_Object *x, *y;
3212 unsigned long *time;
3214 FRAME_PTR f1;
3216 BLOCK_INPUT;
3218 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3219 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3220 else
3222 POINT pt;
3224 Lisp_Object frame, tail;
3226 /* Clear the mouse-moved flag for every frame on this display. */
3227 FOR_EACH_FRAME (tail, frame)
3228 XFRAME (frame)->mouse_moved = 0;
3230 last_mouse_scroll_bar = Qnil;
3232 GetCursorPos (&pt);
3234 /* Now we have a position on the root; find the innermost window
3235 containing the pointer. */
3237 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3238 && FRAME_LIVE_P (last_mouse_frame))
3240 /* If mouse was grabbed on a frame, give coords for that frame
3241 even if the mouse is now outside it. */
3242 f1 = last_mouse_frame;
3244 else
3246 /* Is window under mouse one of our frames? */
3247 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
3248 WindowFromPoint (pt));
3251 /* If not, is it one of our scroll bars? */
3252 if (! f1)
3254 struct scroll_bar *bar
3255 = x_window_to_scroll_bar (WindowFromPoint (pt));
3257 if (bar)
3259 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3263 if (f1 == 0 && insist > 0)
3264 f1 = SELECTED_FRAME ();
3266 if (f1)
3268 /* Ok, we found a frame. Store all the values.
3269 last_mouse_glyph is a rectangle used to reduce the
3270 generation of mouse events. To not miss any motion
3271 events, we must divide the frame into rectangles of the
3272 size of the smallest character that could be displayed
3273 on it, i.e. into the same rectangles that matrices on
3274 the frame are divided into. */
3276 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3277 remember_mouse_glyph (f1, pt.x, pt.y, &last_mouse_glyph);
3278 last_mouse_glyph_frame = f1;
3280 *bar_window = Qnil;
3281 *part = 0;
3282 *fp = f1;
3283 XSETINT (*x, pt.x);
3284 XSETINT (*y, pt.y);
3285 *time = last_mouse_movement_time;
3290 UNBLOCK_INPUT;
3294 /***********************************************************************
3295 Tool-bars
3296 ***********************************************************************/
3298 /* Handle mouse button event on the tool-bar of frame F, at
3299 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3300 or ButtonRelase. */
3302 static void
3303 w32_handle_tool_bar_click (f, button_event)
3304 struct frame *f;
3305 struct input_event *button_event;
3307 int x = XFASTINT (button_event->x);
3308 int y = XFASTINT (button_event->y);
3310 if (button_event->modifiers & down_modifier)
3311 handle_tool_bar_click (f, x, y, 1, 0);
3312 else
3313 handle_tool_bar_click (f, x, y, 0,
3314 button_event->modifiers & ~up_modifier);
3319 /***********************************************************************
3320 Scroll bars
3321 ***********************************************************************/
3323 /* Scroll bar support. */
3325 /* Given a window ID, find the struct scroll_bar which manages it.
3326 This can be called in GC, so we have to make sure to strip off mark
3327 bits. */
3329 static struct scroll_bar *
3330 x_window_to_scroll_bar (window_id)
3331 Window window_id;
3333 Lisp_Object tail;
3335 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3337 Lisp_Object frame, bar, condemned;
3339 frame = XCAR (tail);
3340 /* All elements of Vframe_list should be frames. */
3341 if (! FRAMEP (frame))
3342 abort ();
3344 /* Scan this frame's scroll bar list for a scroll bar with the
3345 right window ID. */
3346 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3347 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3348 /* This trick allows us to search both the ordinary and
3349 condemned scroll bar lists with one loop. */
3350 ! NILP (bar) || (bar = condemned,
3351 condemned = Qnil,
3352 ! NILP (bar));
3353 bar = XSCROLL_BAR (bar)->next)
3354 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3355 return XSCROLL_BAR (bar);
3358 return 0;
3363 /* Set the thumb size and position of scroll bar BAR. We are currently
3364 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3366 static void
3367 w32_set_scroll_bar_thumb (bar, portion, position, whole)
3368 struct scroll_bar *bar;
3369 int portion, position, whole;
3371 Window w = SCROLL_BAR_W32_WINDOW (bar);
3372 /* We use the whole scroll-bar height in the calculations below, to
3373 avoid strange effects like scrolling backwards when just clicking
3374 on the handle (without moving it). */
3375 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height))
3376 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3377 int sb_page, sb_pos;
3378 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3379 SCROLLINFO si;
3381 /* We used to change the nPage setting while dragging the handle,
3382 but that had very strange effects (such as scrolling backwards
3383 while dragging downwards).
3385 Now, we don't change the nPage setting while dragging unless we
3386 get near to the end of the buffer, in which case we often have to
3387 resize the handle to "go all the way". */
3389 if (draggingp)
3391 int near_bottom_p;
3392 BLOCK_INPUT;
3393 si.cbSize = sizeof (si);
3394 si.fMask = SIF_POS | SIF_PAGE;
3395 GetScrollInfo(w, SB_CTL, &si);
3396 near_bottom_p = si.nPos + si.nPage >= range;
3397 UNBLOCK_INPUT;
3398 if (!near_bottom_p)
3399 return;
3402 if (whole)
3404 /* Position scroll bar at rock bottom if the bottom of the
3405 buffer is visible. This avoids shinking the thumb away
3406 to nothing if it is held at the bottom of the buffer. */
3407 if (position + portion >= whole && !draggingp)
3409 sb_page = range * (whole - position) / whole;
3410 sb_pos = range;
3412 else
3414 sb_pos = position * range / whole;
3415 sb_page = (min (portion, (whole - position)) * range) / whole;
3418 else
3420 sb_page = range;
3421 sb_pos = 0;
3424 sb_page = max (sb_page, VERTICAL_SCROLL_BAR_MIN_HANDLE);
3426 BLOCK_INPUT;
3428 si.cbSize = sizeof (si);
3429 si.fMask = SIF_PAGE | SIF_POS;
3430 si.nPage = sb_page;
3431 si.nPos = sb_pos;
3433 SetScrollInfo (w, SB_CTL, &si, TRUE);
3435 UNBLOCK_INPUT;
3439 /************************************************************************
3440 Scroll bars, general
3441 ************************************************************************/
3443 static HWND
3444 my_create_scrollbar (f, bar)
3445 struct frame * f;
3446 struct scroll_bar * bar;
3448 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3449 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3450 (LPARAM) bar);
3453 /*#define ATTACH_THREADS*/
3455 static BOOL
3456 my_show_window (FRAME_PTR f, HWND hwnd, int how)
3458 #ifndef ATTACH_THREADS
3459 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3460 (WPARAM) hwnd, (LPARAM) how);
3461 #else
3462 return ShowWindow (hwnd, how);
3463 #endif
3466 static void
3467 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3468 int x, int y, int cx, int cy, UINT flags)
3470 #ifndef ATTACH_THREADS
3471 WINDOWPOS pos;
3472 pos.hwndInsertAfter = hwndAfter;
3473 pos.x = x;
3474 pos.y = y;
3475 pos.cx = cx;
3476 pos.cy = cy;
3477 pos.flags = flags;
3478 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3479 #else
3480 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3481 #endif
3484 static void
3485 my_set_focus (f, hwnd)
3486 struct frame * f;
3487 HWND hwnd;
3489 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3490 (WPARAM) hwnd, 0);
3493 static void
3494 my_set_foreground_window (hwnd)
3495 HWND hwnd;
3497 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3501 static void
3502 my_destroy_window (f, hwnd)
3503 struct frame * f;
3504 HWND hwnd;
3506 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3507 (WPARAM) hwnd, 0);
3510 /* Create a scroll bar and return the scroll bar vector for it. W is
3511 the Emacs window on which to create the scroll bar. TOP, LEFT,
3512 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
3513 scroll bar. */
3515 static struct scroll_bar *
3516 x_scroll_bar_create (w, top, left, width, height)
3517 struct window *w;
3518 int top, left, width, height;
3520 struct frame *f = XFRAME (WINDOW_FRAME (w));
3521 HWND hwnd;
3522 SCROLLINFO si;
3523 struct scroll_bar *bar
3524 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3526 BLOCK_INPUT;
3528 XSETWINDOW (bar->window, w);
3529 XSETINT (bar->top, top);
3530 XSETINT (bar->left, left);
3531 XSETINT (bar->width, width);
3532 XSETINT (bar->height, height);
3533 XSETINT (bar->start, 0);
3534 XSETINT (bar->end, 0);
3535 bar->dragging = Qnil;
3536 bar->fringe_extended_p = Qnil;
3538 /* Requires geometry to be set before call to create the real window */
3540 hwnd = my_create_scrollbar (f, bar);
3542 si.cbSize = sizeof (si);
3543 si.fMask = SIF_ALL;
3544 si.nMin = 0;
3545 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3546 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3547 si.nPage = si.nMax;
3548 si.nPos = 0;
3550 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3552 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3554 /* Add bar to its frame's list of scroll bars. */
3555 bar->next = FRAME_SCROLL_BARS (f);
3556 bar->prev = Qnil;
3557 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3558 if (! NILP (bar->next))
3559 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3561 UNBLOCK_INPUT;
3563 return bar;
3567 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3568 nil. */
3570 static void
3571 x_scroll_bar_remove (bar)
3572 struct scroll_bar *bar;
3574 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3576 BLOCK_INPUT;
3578 /* Destroy the window. */
3579 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3581 /* Disassociate this scroll bar from its window. */
3582 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3584 UNBLOCK_INPUT;
3587 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3588 that we are displaying PORTION characters out of a total of WHOLE
3589 characters, starting at POSITION. If WINDOW has no scroll bar,
3590 create one. */
3591 static void
3592 w32_set_vertical_scroll_bar (w, portion, whole, position)
3593 struct window *w;
3594 int portion, whole, position;
3596 struct frame *f = XFRAME (w->frame);
3597 struct scroll_bar *bar;
3598 int top, height, left, sb_left, width, sb_width;
3599 int window_y, window_height;
3600 int fringe_extended_p;
3602 /* Get window dimensions. */
3603 window_box (w, -1, 0, &window_y, 0, &window_height);
3604 top = window_y;
3605 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3606 height = window_height;
3608 /* Compute the left edge of the scroll bar area. */
3609 left = WINDOW_SCROLL_BAR_AREA_X (w);
3611 /* Compute the width of the scroll bar which might be less than
3612 the width of the area reserved for the scroll bar. */
3613 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3614 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3615 else
3616 sb_width = width;
3618 /* Compute the left edge of the scroll bar. */
3619 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3620 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
3621 else
3622 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
3624 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3625 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
3626 && WINDOW_LEFT_FRINGE_WIDTH (w)
3627 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3628 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
3629 else
3630 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
3631 && WINDOW_RIGHT_FRINGE_WIDTH (w)
3632 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
3633 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
3635 /* Does the scroll bar exist yet? */
3636 if (NILP (w->vertical_scroll_bar))
3638 HDC hdc;
3639 BLOCK_INPUT;
3640 if (width > 0 && height > 0)
3642 hdc = get_frame_dc (f);
3643 if (fringe_extended_p)
3644 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3645 else
3646 w32_clear_area (f, hdc, left, top, width, height);
3647 release_frame_dc (f, hdc);
3649 UNBLOCK_INPUT;
3651 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3653 else
3655 /* It may just need to be moved and resized. */
3656 HWND hwnd;
3658 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3659 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3661 /* If already correctly positioned, do nothing. */
3662 if ( XINT (bar->left) == sb_left
3663 && XINT (bar->top) == top
3664 && XINT (bar->width) == sb_width
3665 && XINT (bar->height) == height
3666 && !NILP (bar->fringe_extended_p) == fringe_extended_p )
3668 /* Redraw after clear_frame. */
3669 if (!my_show_window (f, hwnd, SW_NORMAL))
3670 InvalidateRect (hwnd, NULL, FALSE);
3672 else
3674 HDC hdc;
3675 SCROLLINFO si;
3677 BLOCK_INPUT;
3678 if (width && height)
3680 hdc = get_frame_dc (f);
3681 /* Since Windows scroll bars are smaller than the space reserved
3682 for them on the frame, we have to clear "under" them. */
3683 if (fringe_extended_p)
3684 w32_clear_area (f, hdc, sb_left, top, sb_width, height);
3685 else
3686 w32_clear_area (f, hdc, left, top, width, height);
3687 release_frame_dc (f, hdc);
3689 /* Make sure scroll bar is "visible" before moving, to ensure the
3690 area of the parent window now exposed will be refreshed. */
3691 my_show_window (f, hwnd, SW_HIDE);
3692 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3693 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3694 max (height, 1), TRUE);
3696 si.cbSize = sizeof (si);
3697 si.fMask = SIF_RANGE;
3698 si.nMin = 0;
3699 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3700 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3702 SetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3704 my_show_window (f, hwnd, SW_NORMAL);
3705 /* InvalidateRect (w, NULL, FALSE); */
3707 /* Remember new settings. */
3708 XSETINT (bar->left, sb_left);
3709 XSETINT (bar->top, top);
3710 XSETINT (bar->width, sb_width);
3711 XSETINT (bar->height, height);
3713 UNBLOCK_INPUT;
3716 bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
3718 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3720 XSETVECTOR (w->vertical_scroll_bar, bar);
3724 /* The following three hooks are used when we're doing a thorough
3725 redisplay of the frame. We don't explicitly know which scroll bars
3726 are going to be deleted, because keeping track of when windows go
3727 away is a real pain - "Can you say set-window-configuration, boys
3728 and girls?" Instead, we just assert at the beginning of redisplay
3729 that *all* scroll bars are to be removed, and then save a scroll bar
3730 from the fiery pit when we actually redisplay its window. */
3732 /* Arrange for all scroll bars on FRAME to be removed at the next call
3733 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3734 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3736 static void
3737 w32_condemn_scroll_bars (frame)
3738 FRAME_PTR frame;
3740 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3741 while (! NILP (FRAME_SCROLL_BARS (frame)))
3743 Lisp_Object bar;
3744 bar = FRAME_SCROLL_BARS (frame);
3745 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
3746 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3747 XSCROLL_BAR (bar)->prev = Qnil;
3748 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3749 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3750 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
3755 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3756 Note that WINDOW isn't necessarily condemned at all. */
3758 static void
3759 w32_redeem_scroll_bar (window)
3760 struct window *window;
3762 struct scroll_bar *bar;
3763 struct frame *f;
3765 /* We can't redeem this window's scroll bar if it doesn't have one. */
3766 if (NILP (window->vertical_scroll_bar))
3767 abort ();
3769 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3771 /* Unlink it from the condemned list. */
3772 f = XFRAME (WINDOW_FRAME (window));
3773 if (NILP (bar->prev))
3775 /* If the prev pointer is nil, it must be the first in one of
3776 the lists. */
3777 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3778 /* It's not condemned. Everything's fine. */
3779 return;
3780 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3781 window->vertical_scroll_bar))
3782 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
3783 else
3784 /* If its prev pointer is nil, it must be at the front of
3785 one or the other! */
3786 abort ();
3788 else
3789 XSCROLL_BAR (bar->prev)->next = bar->next;
3791 if (! NILP (bar->next))
3792 XSCROLL_BAR (bar->next)->prev = bar->prev;
3794 bar->next = FRAME_SCROLL_BARS (f);
3795 bar->prev = Qnil;
3796 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3797 if (! NILP (bar->next))
3798 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3801 /* Remove all scroll bars on FRAME that haven't been saved since the
3802 last call to `*condemn_scroll_bars_hook'. */
3804 static void
3805 w32_judge_scroll_bars (f)
3806 FRAME_PTR f;
3808 Lisp_Object bar, next;
3810 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3812 /* Clear out the condemned list now so we won't try to process any
3813 more events on the hapless scroll bars. */
3814 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3816 for (; ! NILP (bar); bar = next)
3818 struct scroll_bar *b = XSCROLL_BAR (bar);
3820 x_scroll_bar_remove (b);
3822 next = b->next;
3823 b->next = b->prev = Qnil;
3826 /* Now there should be no references to the condemned scroll bars,
3827 and they should get garbage-collected. */
3830 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3831 is set to something other than NO_EVENT, it is enqueued.
3833 This may be called from a signal handler, so we have to ignore GC
3834 mark bits. */
3836 static int
3837 w32_scroll_bar_handle_click (bar, msg, emacs_event)
3838 struct scroll_bar *bar;
3839 W32Msg *msg;
3840 struct input_event *emacs_event;
3842 if (! WINDOWP (bar->window))
3843 abort ();
3845 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
3846 emacs_event->code = 0;
3847 /* not really meaningful to distinguish up/down */
3848 emacs_event->modifiers = msg->dwModifiers;
3849 emacs_event->frame_or_window = bar->window;
3850 emacs_event->arg = Qnil;
3851 emacs_event->timestamp = msg->msg.time;
3854 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3855 int y;
3856 int dragging = !NILP (bar->dragging);
3857 SCROLLINFO si;
3859 si.cbSize = sizeof (si);
3860 si.fMask = SIF_POS;
3862 GetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
3863 y = si.nPos;
3865 bar->dragging = Qnil;
3868 last_mouse_scroll_bar_pos = msg->msg.wParam;
3870 switch (LOWORD (msg->msg.wParam))
3872 case SB_LINEDOWN:
3873 emacs_event->part = scroll_bar_down_arrow;
3874 break;
3875 case SB_LINEUP:
3876 emacs_event->part = scroll_bar_up_arrow;
3877 break;
3878 case SB_PAGEUP:
3879 emacs_event->part = scroll_bar_above_handle;
3880 break;
3881 case SB_PAGEDOWN:
3882 emacs_event->part = scroll_bar_below_handle;
3883 break;
3884 case SB_TOP:
3885 emacs_event->part = scroll_bar_handle;
3886 y = 0;
3887 break;
3888 case SB_BOTTOM:
3889 emacs_event->part = scroll_bar_handle;
3890 y = top_range;
3891 break;
3892 case SB_THUMBTRACK:
3893 case SB_THUMBPOSITION:
3894 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3895 y = HIWORD (msg->msg.wParam);
3896 bar->dragging = Qt;
3897 emacs_event->part = scroll_bar_handle;
3899 /* "Silently" update current position. */
3901 SCROLLINFO si;
3903 si.cbSize = sizeof (si);
3904 si.fMask = SIF_POS;
3905 si.nPos = y;
3906 /* Remember apparent position (we actually lag behind the real
3907 position, so don't set that directly. */
3908 last_scroll_bar_drag_pos = y;
3910 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
3912 break;
3913 case SB_ENDSCROLL:
3914 /* If this is the end of a drag sequence, then reset the scroll
3915 handle size to normal and do a final redraw. Otherwise do
3916 nothing. */
3917 if (dragging)
3919 SCROLLINFO si;
3920 int start = XINT (bar->start);
3921 int end = XINT (bar->end);
3923 si.cbSize = sizeof (si);
3924 si.fMask = SIF_PAGE | SIF_POS;
3925 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3926 si.nPos = last_scroll_bar_drag_pos;
3927 SetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
3929 /* fall through */
3930 default:
3931 emacs_event->kind = NO_EVENT;
3932 return FALSE;
3935 XSETINT (emacs_event->x, y);
3936 XSETINT (emacs_event->y, top_range);
3938 return TRUE;
3942 /* Return information to the user about the current position of the mouse
3943 on the scroll bar. */
3945 static void
3946 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
3947 FRAME_PTR *fp;
3948 Lisp_Object *bar_window;
3949 enum scroll_bar_part *part;
3950 Lisp_Object *x, *y;
3951 unsigned long *time;
3953 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
3954 Window w = SCROLL_BAR_W32_WINDOW (bar);
3955 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3956 int pos;
3957 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3958 SCROLLINFO si;
3960 BLOCK_INPUT;
3962 *fp = f;
3963 *bar_window = bar->window;
3965 si.cbSize = sizeof (si);
3966 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
3968 GetScrollInfo (w, SB_CTL, &si);
3969 pos = si.nPos;
3970 top_range = si.nMax - si.nPage + 1;
3972 switch (LOWORD (last_mouse_scroll_bar_pos))
3974 case SB_THUMBPOSITION:
3975 case SB_THUMBTRACK:
3976 *part = scroll_bar_handle;
3977 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
3978 pos = HIWORD (last_mouse_scroll_bar_pos);
3979 break;
3980 case SB_LINEDOWN:
3981 *part = scroll_bar_handle;
3982 pos++;
3983 break;
3984 default:
3985 *part = scroll_bar_handle;
3986 break;
3989 XSETINT (*x, pos);
3990 XSETINT (*y, top_range);
3992 f->mouse_moved = 0;
3993 last_mouse_scroll_bar = Qnil;
3995 *time = last_mouse_movement_time;
3997 UNBLOCK_INPUT;
4001 /* The screen has been cleared so we may have changed foreground or
4002 background colors, and the scroll bars may need to be redrawn.
4003 Clear out the scroll bars, and ask for expose events, so we can
4004 redraw them. */
4006 void
4007 x_scroll_bar_clear (f)
4008 FRAME_PTR f;
4010 Lisp_Object bar;
4012 /* We can have scroll bars even if this is 0,
4013 if we just turned off scroll bar mode.
4014 But in that case we should not clear them. */
4015 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4016 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4017 bar = XSCROLL_BAR (bar)->next)
4019 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4020 HDC hdc = GetDC (window);
4021 RECT rect;
4023 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4024 arranges to refresh the scroll bar if hidden. */
4025 my_show_window (f, window, SW_HIDE);
4027 GetClientRect (window, &rect);
4028 select_palette (f, hdc);
4029 w32_clear_rect (f, hdc, &rect);
4030 deselect_palette (f, hdc);
4032 ReleaseDC (window, hdc);
4037 /* The main W32 event-reading loop - w32_read_socket. */
4039 /* Record the last 100 characters stored
4040 to help debug the loss-of-chars-during-GC problem. */
4042 static int temp_index;
4043 static short temp_buffer[100];
4045 /* Temporarily store lead byte of DBCS input sequences. */
4046 static char dbcs_lead = 0;
4048 /* Read events coming from the W32 shell.
4049 This routine is called by the SIGIO handler.
4050 We return as soon as there are no more events to be read.
4052 We return the number of characters stored into the buffer,
4053 thus pretending to be `read'.
4055 EXPECTED is nonzero if the caller knows input is available.
4057 Some of these messages are reposted back to the message queue since the
4058 system calls the windows proc directly in a context where we cannot return
4059 the data nor can we guarantee the state we are in. So if we dispatch them
4060 we will get into an infinite loop. To prevent this from ever happening we
4061 will set a variable to indicate we are in the read_socket call and indicate
4062 which message we are processing since the windows proc gets called
4063 recursively with different messages by the system.
4067 w32_read_socket (sd, expected, hold_quit)
4068 register int sd;
4069 int expected;
4070 struct input_event *hold_quit;
4072 int count = 0;
4073 int check_visibility = 0;
4074 W32Msg msg;
4075 struct frame *f;
4076 struct w32_display_info *dpyinfo = &one_w32_display_info;
4078 if (interrupt_input_blocked)
4080 interrupt_input_pending = 1;
4081 return -1;
4084 interrupt_input_pending = 0;
4085 BLOCK_INPUT;
4087 /* So people can tell when we have read the available input. */
4088 input_signal_count++;
4090 /* TODO: ghostscript integration. */
4091 while (get_next_msg (&msg, FALSE))
4093 struct input_event inev;
4094 int do_help = 0;
4096 EVENT_INIT (inev);
4097 inev.kind = NO_EVENT;
4098 inev.arg = Qnil;
4100 switch (msg.msg.message)
4102 case WM_PAINT:
4103 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4105 if (f)
4107 if (msg.rect.right == msg.rect.left ||
4108 msg.rect.bottom == msg.rect.top)
4110 /* We may get paint messages even though the client
4111 area is clipped - these are not expose events. */
4112 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4113 SDATA (f->name)));
4115 else if (f->async_visible != 1)
4117 /* Definitely not obscured, so mark as visible. */
4118 f->async_visible = 1;
4119 f->async_iconified = 0;
4120 SET_FRAME_GARBAGED (f);
4121 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4122 SDATA (f->name)));
4124 /* WM_PAINT serves as MapNotify as well, so report
4125 visibility changes properly. */
4126 if (f->iconified)
4128 inev.kind = DEICONIFY_EVENT;
4129 XSETFRAME (inev.frame_or_window, f);
4131 else if (! NILP (Vframe_list)
4132 && ! NILP (XCDR (Vframe_list)))
4133 /* Force a redisplay sooner or later to update the
4134 frame titles in case this is the second frame. */
4135 record_asynch_buffer_change ();
4137 else
4139 HDC hdc = get_frame_dc (f);
4141 /* Erase background again for safety. */
4142 w32_clear_rect (f, hdc, &msg.rect);
4143 release_frame_dc (f, hdc);
4144 expose_frame (f,
4145 msg.rect.left,
4146 msg.rect.top,
4147 msg.rect.right - msg.rect.left,
4148 msg.rect.bottom - msg.rect.top);
4151 break;
4153 case WM_INPUTLANGCHANGE:
4154 /* Generate a language change event. */
4155 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4157 if (f)
4159 inev.kind = LANGUAGE_CHANGE_EVENT;
4160 XSETFRAME (inev.frame_or_window, f);
4161 inev.code = msg.msg.wParam;
4162 inev.modifiers = msg.msg.lParam & 0xffff;
4164 break;
4166 case WM_KEYDOWN:
4167 case WM_SYSKEYDOWN:
4168 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4170 if (f && !f->iconified)
4172 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4173 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4175 clear_mouse_face (dpyinfo);
4176 dpyinfo->mouse_face_hidden = 1;
4179 if (temp_index == sizeof temp_buffer / sizeof (short))
4180 temp_index = 0;
4181 temp_buffer[temp_index++] = msg.msg.wParam;
4182 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4183 inev.code = msg.msg.wParam;
4184 inev.modifiers = msg.dwModifiers;
4185 XSETFRAME (inev.frame_or_window, f);
4186 inev.timestamp = msg.msg.time;
4188 break;
4190 case WM_UNICHAR:
4191 case WM_SYSCHAR:
4192 case WM_CHAR:
4193 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4195 if (f && !f->iconified)
4197 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4198 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4200 clear_mouse_face (dpyinfo);
4201 dpyinfo->mouse_face_hidden = 1;
4204 if (temp_index == sizeof temp_buffer / sizeof (short))
4205 temp_index = 0;
4206 temp_buffer[temp_index++] = msg.msg.wParam;
4208 inev.modifiers = msg.dwModifiers;
4209 XSETFRAME (inev.frame_or_window, f);
4210 inev.timestamp = msg.msg.time;
4212 if (msg.msg.message == WM_UNICHAR)
4214 inev.code = msg.msg.wParam;
4216 else if (msg.msg.wParam < 256)
4218 wchar_t code;
4219 char dbcs[2];
4220 dbcs[0] = 0;
4221 dbcs[1] = (char) msg.msg.wParam;
4223 if (dbcs_lead)
4225 dbcs[0] = dbcs_lead;
4226 dbcs_lead = 0;
4227 if (!MultiByteToWideChar (CP_ACP, 0, dbcs, 2, &code, 1))
4229 /* Garbage */
4230 DebPrint (("Invalid DBCS sequence: %d %d\n",
4231 dbcs[0], dbcs[1]));
4232 inev.kind = NO_EVENT;
4233 break;
4236 else if (IsDBCSLeadByteEx (CP_ACP, (BYTE) msg.msg.wParam))
4238 dbcs_lead = (char) msg.msg.wParam;
4239 inev.kind = NO_EVENT;
4240 break;
4242 else
4244 if (!MultiByteToWideChar (CP_ACP, 0, &dbcs[1], 1,
4245 &code, 1))
4247 /* What to do with garbage? */
4248 DebPrint (("Invalid character: %d\n", dbcs[1]));
4249 inev.kind = NO_EVENT;
4250 break;
4253 inev.code = code;
4255 else
4257 /* Windows shouldn't generate WM_CHAR events above 0xFF
4258 in non-Unicode message handlers. */
4259 DebPrint (("Non-byte WM_CHAR: %d\n", msg.msg.wParam));
4260 inev.kind = NO_EVENT;
4261 break;
4263 inev.kind = inev.code < 128 ? ASCII_KEYSTROKE_EVENT
4264 : MULTIBYTE_CHAR_KEYSTROKE_EVENT;
4266 break;
4268 case WM_APPCOMMAND:
4269 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4271 if (f && !f->iconified)
4273 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
4274 && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window))
4276 clear_mouse_face (dpyinfo);
4277 dpyinfo->mouse_face_hidden = 1;
4280 if (temp_index == sizeof temp_buffer / sizeof (short))
4281 temp_index = 0;
4282 temp_buffer[temp_index++] = msg.msg.wParam;
4283 inev.kind = MULTIMEDIA_KEY_EVENT;
4284 inev.code = GET_APPCOMMAND_LPARAM(msg.msg.lParam);
4285 inev.modifiers = msg.dwModifiers;
4286 XSETFRAME (inev.frame_or_window, f);
4287 inev.timestamp = msg.msg.time;
4289 break;
4291 case WM_MOUSEMOVE:
4292 /* Ignore non-movement. */
4294 int x = LOWORD (msg.msg.lParam);
4295 int y = HIWORD (msg.msg.lParam);
4296 if (x == last_mousemove_x && y == last_mousemove_y)
4297 break;
4298 last_mousemove_x = x;
4299 last_mousemove_y = y;
4302 previous_help_echo_string = help_echo_string;
4303 help_echo_string = Qnil;
4305 if (dpyinfo->grabbed && last_mouse_frame
4306 && FRAME_LIVE_P (last_mouse_frame))
4307 f = last_mouse_frame;
4308 else
4309 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4311 if (dpyinfo->mouse_face_hidden)
4313 dpyinfo->mouse_face_hidden = 0;
4314 clear_mouse_face (dpyinfo);
4317 if (f)
4319 /* Generate SELECT_WINDOW_EVENTs when needed. */
4320 if (!NILP (Vmouse_autoselect_window))
4322 Lisp_Object window;
4323 int x = LOWORD (msg.msg.lParam);
4324 int y = HIWORD (msg.msg.lParam);
4326 window = window_from_coordinates (f, x, y, 0, 0, 0, 0);
4328 /* Window will be selected only when it is not
4329 selected now and last mouse movement event was
4330 not in it. Minibuffer window will be selected
4331 only when it is active. */
4332 if (WINDOWP(window)
4333 && !EQ (window, last_window)
4334 && !EQ (window, selected_window)
4335 /* For click-to-focus window managers
4336 create event iff we don't leave the
4337 selected frame. */
4338 && (focus_follows_mouse
4339 || (EQ (XWINDOW (window)->frame,
4340 XWINDOW (selected_window)->frame))))
4342 inev.kind = SELECT_WINDOW_EVENT;
4343 inev.frame_or_window = window;
4346 last_window=window;
4348 if (!note_mouse_movement (f, &msg.msg))
4349 help_echo_string = previous_help_echo_string;
4351 else
4353 /* If we move outside the frame, then we're
4354 certainly no longer on any text in the frame. */
4355 clear_mouse_face (dpyinfo);
4358 /* If the contents of the global variable help_echo_string
4359 has changed, generate a HELP_EVENT. */
4360 #if 0 /* The below is an invalid comparison when USE_LISP_UNION_TYPE.
4361 But it was originally changed to this to fix a bug, so I have
4362 not removed it completely in case the bug is still there. */
4363 if (help_echo_string != previous_help_echo_string ||
4364 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4365 #else /* This is what xterm.c does. */
4366 if (!NILP (help_echo_string)
4367 || !NILP (previous_help_echo_string))
4368 do_help = 1;
4369 #endif
4370 break;
4372 case WM_LBUTTONDOWN:
4373 case WM_LBUTTONUP:
4374 case WM_MBUTTONDOWN:
4375 case WM_MBUTTONUP:
4376 case WM_RBUTTONDOWN:
4377 case WM_RBUTTONUP:
4378 case WM_XBUTTONDOWN:
4379 case WM_XBUTTONUP:
4381 /* If we decide we want to generate an event to be seen
4382 by the rest of Emacs, we put it here. */
4383 int tool_bar_p = 0;
4384 int button;
4385 int up;
4387 if (dpyinfo->grabbed && last_mouse_frame
4388 && FRAME_LIVE_P (last_mouse_frame))
4389 f = last_mouse_frame;
4390 else
4391 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4393 if (f)
4395 construct_mouse_click (&inev, &msg, f);
4397 /* Is this in the tool-bar? */
4398 if (WINDOWP (f->tool_bar_window)
4399 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4401 Lisp_Object window;
4402 int x = XFASTINT (inev.x);
4403 int y = XFASTINT (inev.y);
4405 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
4407 if (EQ (window, f->tool_bar_window))
4409 w32_handle_tool_bar_click (f, &inev);
4410 tool_bar_p = 1;
4414 if (tool_bar_p
4415 || (dpyinfo->w32_focus_frame
4416 && f != dpyinfo->w32_focus_frame))
4417 inev.kind = NO_EVENT;
4420 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4421 &button, &up);
4423 if (up)
4425 dpyinfo->grabbed &= ~ (1 << button);
4427 else
4429 dpyinfo->grabbed |= (1 << button);
4430 last_mouse_frame = f;
4431 /* Ignore any mouse motion that happened
4432 before this event; any subsequent mouse-movement
4433 Emacs events should reflect only motion after
4434 the ButtonPress. */
4435 if (f != 0)
4436 f->mouse_moved = 0;
4438 if (!tool_bar_p)
4439 last_tool_bar_item = -1;
4441 break;
4444 case WM_MOUSEWHEEL:
4445 case WM_MOUSEHWHEEL:
4447 if (dpyinfo->grabbed && last_mouse_frame
4448 && FRAME_LIVE_P (last_mouse_frame))
4449 f = last_mouse_frame;
4450 else
4451 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4453 if (f)
4456 if (!dpyinfo->w32_focus_frame
4457 || f == dpyinfo->w32_focus_frame)
4459 /* Emit an Emacs wheel-up/down event. */
4460 construct_mouse_wheel (&inev, &msg, f);
4462 /* Ignore any mouse motion that happened before this
4463 event; any subsequent mouse-movement Emacs events
4464 should reflect only motion after the
4465 ButtonPress. */
4466 f->mouse_moved = 0;
4468 last_mouse_frame = f;
4469 last_tool_bar_item = -1;
4471 break;
4473 case WM_DROPFILES:
4474 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4476 if (f)
4477 construct_drag_n_drop (&inev, &msg, f);
4478 break;
4480 case WM_VSCROLL:
4482 struct scroll_bar *bar =
4483 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4485 if (bar)
4486 w32_scroll_bar_handle_click (bar, &msg, &inev);
4487 break;
4490 case WM_WINDOWPOSCHANGED:
4491 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4492 if (f)
4494 if (f->want_fullscreen & FULLSCREEN_WAIT)
4495 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
4497 check_visibility = 1;
4498 break;
4500 case WM_ACTIVATE:
4501 case WM_ACTIVATEAPP:
4502 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4503 if (f)
4504 x_check_fullscreen (f);
4505 check_visibility = 1;
4506 break;
4508 case WM_MOVE:
4509 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4511 if (f && !f->async_iconified)
4513 int x, y;
4515 x_real_positions (f, &x, &y);
4516 f->left_pos = x;
4517 f->top_pos = y;
4520 check_visibility = 1;
4521 break;
4523 case WM_SHOWWINDOW:
4524 /* wParam non-zero means Window is about to be shown, 0 means
4525 about to be hidden. */
4526 /* Redo the mouse-highlight after the tooltip has gone. */
4527 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4529 tip_window = NULL;
4530 redo_mouse_highlight ();
4533 /* If window has been obscured or exposed by another window
4534 being maximised or minimised/restored, then recheck
4535 visibility of all frames. Direct changes to our own
4536 windows get handled by WM_SIZE. */
4537 #if 0
4538 if (msg.msg.lParam != 0)
4539 check_visibility = 1;
4540 else
4542 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4543 f->async_visible = msg.msg.wParam;
4545 #endif
4547 check_visibility = 1;
4548 break;
4550 case WM_SIZE:
4551 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4553 /* Inform lisp of whether frame has been iconified etc. */
4554 if (f)
4556 switch (msg.msg.wParam)
4558 case SIZE_MINIMIZED:
4559 f->async_visible = 0;
4560 f->async_iconified = 1;
4562 inev.kind = ICONIFY_EVENT;
4563 XSETFRAME (inev.frame_or_window, f);
4564 break;
4566 case SIZE_MAXIMIZED:
4567 case SIZE_RESTORED:
4568 f->async_visible = 1;
4569 f->async_iconified = 0;
4571 /* wait_reading_process_output will notice this and update
4572 the frame's display structures. */
4573 SET_FRAME_GARBAGED (f);
4575 if (f->iconified)
4577 int x, y;
4579 /* Reset top and left positions of the Window
4580 here since Windows sends a WM_MOVE message
4581 BEFORE telling us the Window is minimized
4582 when the Window is iconified, with 3000,3000
4583 as the co-ords. */
4584 x_real_positions (f, &x, &y);
4585 f->left_pos = x;
4586 f->top_pos = y;
4588 inev.kind = DEICONIFY_EVENT;
4589 XSETFRAME (inev.frame_or_window, f);
4591 else if (! NILP (Vframe_list)
4592 && ! NILP (XCDR (Vframe_list)))
4593 /* Force a redisplay sooner or later
4594 to update the frame titles
4595 in case this is the second frame. */
4596 record_asynch_buffer_change ();
4597 break;
4601 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
4603 RECT rect;
4604 int rows;
4605 int columns;
4606 int width;
4607 int height;
4609 GetClientRect (msg.msg.hwnd, &rect);
4611 height = rect.bottom - rect.top;
4612 width = rect.right - rect.left;
4614 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4615 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4617 /* TODO: Clip size to the screen dimensions. */
4619 /* Even if the number of character rows and columns has
4620 not changed, the font size may have changed, so we need
4621 to check the pixel dimensions as well. */
4623 if (columns != FRAME_COLS (f)
4624 || rows != FRAME_LINES (f)
4625 || width != FRAME_PIXEL_WIDTH (f)
4626 || height != FRAME_PIXEL_HEIGHT (f))
4628 change_frame_size (f, rows, columns, 0, 1, 0);
4629 SET_FRAME_GARBAGED (f);
4630 cancel_mouse_face (f);
4631 FRAME_PIXEL_WIDTH (f) = width;
4632 FRAME_PIXEL_HEIGHT (f) = height;
4633 f->win_gravity = NorthWestGravity;
4637 check_visibility = 1;
4638 break;
4640 case WM_MOUSELEAVE:
4641 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4642 if (f)
4644 if (f == dpyinfo->mouse_face_mouse_frame)
4646 /* If we move outside the frame, then we're
4647 certainly no longer on any text in the frame. */
4648 clear_mouse_face (dpyinfo);
4649 dpyinfo->mouse_face_mouse_frame = 0;
4652 /* Generate a nil HELP_EVENT to cancel a help-echo.
4653 Do it only if there's something to cancel.
4654 Otherwise, the startup message is cleared when
4655 the mouse leaves the frame. */
4656 if (any_help_event_p)
4657 do_help = -1;
4659 break;
4661 case WM_SETFOCUS:
4662 w32_detect_focus_change (dpyinfo, &msg, &inev);
4664 dpyinfo->grabbed = 0;
4665 check_visibility = 1;
4666 break;
4668 case WM_KILLFOCUS:
4669 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4671 if (f)
4673 if (f == dpyinfo->w32_focus_event_frame)
4674 dpyinfo->w32_focus_event_frame = 0;
4676 if (f == dpyinfo->w32_focus_frame)
4677 x_new_focus_frame (dpyinfo, 0);
4679 if (f == dpyinfo->mouse_face_mouse_frame)
4681 /* If we move outside the frame, then we're
4682 certainly no longer on any text in the frame. */
4683 clear_mouse_face (dpyinfo);
4684 dpyinfo->mouse_face_mouse_frame = 0;
4687 /* Generate a nil HELP_EVENT to cancel a help-echo.
4688 Do it only if there's something to cancel.
4689 Otherwise, the startup message is cleared when
4690 the mouse leaves the frame. */
4691 if (any_help_event_p)
4692 do_help = -1;
4695 dpyinfo->grabbed = 0;
4696 check_visibility = 1;
4697 break;
4699 case WM_CLOSE:
4700 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4702 if (f)
4704 inev.kind = DELETE_WINDOW_EVENT;
4705 XSETFRAME (inev.frame_or_window, f);
4707 break;
4709 case WM_INITMENU:
4710 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4712 if (f)
4714 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4715 XSETFRAME (inev.frame_or_window, f);
4717 break;
4719 case WM_COMMAND:
4720 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4722 if (f)
4724 extern void menubar_selection_callback
4725 (FRAME_PTR f, void * client_data);
4726 menubar_selection_callback (f, (void *)msg.msg.wParam);
4729 check_visibility = 1;
4730 break;
4732 case WM_DISPLAYCHANGE:
4733 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4735 if (f)
4737 dpyinfo->n_cbits = msg.msg.wParam;
4738 DebPrint (("display change: %d %d\n",
4739 (short) LOWORD (msg.msg.lParam),
4740 (short) HIWORD (msg.msg.lParam)));
4743 check_visibility = 1;
4744 break;
4746 default:
4747 /* Check for messages registered at runtime. */
4748 if (msg.msg.message == msh_mousewheel)
4750 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4751 msg.msg.message = WM_MOUSEWHEEL;
4752 prepend_msg (&msg);
4754 break;
4757 if (inev.kind != NO_EVENT)
4759 kbd_buffer_store_event_hold (&inev, hold_quit);
4760 count++;
4763 if (do_help
4764 && !(hold_quit && hold_quit->kind != NO_EVENT))
4766 Lisp_Object frame;
4768 if (f)
4769 XSETFRAME (frame, f);
4770 else
4771 frame = Qnil;
4773 if (do_help > 0)
4775 if (NILP (help_echo_string))
4777 help_echo_object = help_echo_window = Qnil;
4778 help_echo_pos = -1;
4781 any_help_event_p = 1;
4782 gen_help_event (help_echo_string, frame, help_echo_window,
4783 help_echo_object, help_echo_pos);
4785 else
4787 help_echo_string = Qnil;
4788 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4790 count++;
4794 /* If the focus was just given to an autoraising frame,
4795 raise it now. */
4796 /* ??? This ought to be able to handle more than one such frame. */
4797 if (pending_autoraise_frame)
4799 x_raise_frame (pending_autoraise_frame);
4800 pending_autoraise_frame = 0;
4803 /* Check which frames are still visisble, if we have enqueued any user
4804 events or been notified of events that may affect visibility. We
4805 do this here because there doesn't seem to be any direct
4806 notification from Windows that the visibility of a window has
4807 changed (at least, not in all cases). */
4808 if (count > 0 || check_visibility)
4810 Lisp_Object tail, frame;
4812 FOR_EACH_FRAME (tail, frame)
4814 FRAME_PTR f = XFRAME (frame);
4815 /* The tooltip has been drawn already. Avoid the
4816 SET_FRAME_GARBAGED below. */
4817 if (EQ (frame, tip_frame))
4818 continue;
4820 /* Check "visible" frames and mark each as obscured or not.
4821 Note that async_visible is nonzero for unobscured and
4822 obscured frames, but zero for hidden and iconified frames. */
4823 if (FRAME_W32_P (f) && f->async_visible)
4825 RECT clipbox;
4826 HDC hdc;
4828 enter_crit ();
4829 /* Query clipping rectangle for the entire window area
4830 (GetWindowDC), not just the client portion (GetDC).
4831 Otherwise, the scrollbars and menubar aren't counted as
4832 part of the visible area of the frame, and we may think
4833 the frame is obscured when really a scrollbar is still
4834 visible and gets WM_PAINT messages above. */
4835 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
4836 GetClipBox (hdc, &clipbox);
4837 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
4838 leave_crit ();
4840 if (clipbox.right == clipbox.left
4841 || clipbox.bottom == clipbox.top)
4843 /* Frame has become completely obscured so mark as
4844 such (we do this by setting async_visible to 2 so
4845 that FRAME_VISIBLE_P is still true, but redisplay
4846 will skip it). */
4847 f->async_visible = 2;
4849 if (!FRAME_OBSCURED_P (f))
4851 DebPrint (("frame %p (%s) obscured\n", f,
4852 SDATA (f->name)));
4855 else
4857 /* Frame is not obscured, so mark it as such. */
4858 f->async_visible = 1;
4860 if (FRAME_OBSCURED_P (f))
4862 SET_FRAME_GARBAGED (f);
4863 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
4864 SDATA (f->name)));
4866 /* Force a redisplay sooner or later. */
4867 record_asynch_buffer_change ();
4874 UNBLOCK_INPUT;
4875 return count;
4880 /***********************************************************************
4881 Text Cursor
4882 ***********************************************************************/
4884 /* Set clipping for output in glyph row ROW. W is the window in which
4885 we operate. GC is the graphics context to set clipping in.
4887 ROW may be a text row or, e.g., a mode line. Text rows must be
4888 clipped to the interior of the window dedicated to text display,
4889 mode lines must be clipped to the whole window. */
4891 static void
4892 w32_clip_to_row (w, row, area, hdc)
4893 struct window *w;
4894 struct glyph_row *row;
4895 int area;
4896 HDC hdc;
4898 struct frame *f = XFRAME (WINDOW_FRAME (w));
4899 RECT clip_rect;
4900 int window_x, window_y, window_width;
4902 window_box (w, area, &window_x, &window_y, &window_width, 0);
4904 clip_rect.left = window_x;
4905 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4906 clip_rect.top = max (clip_rect.top, window_y);
4907 clip_rect.right = clip_rect.left + window_width;
4908 clip_rect.bottom = clip_rect.top + row->visible_height;
4910 w32_set_clip_rectangle (hdc, &clip_rect);
4914 /* Draw a hollow box cursor on window W in glyph row ROW. */
4916 static void
4917 x_draw_hollow_cursor (w, row)
4918 struct window *w;
4919 struct glyph_row *row;
4921 struct frame *f = XFRAME (WINDOW_FRAME (w));
4922 HDC hdc;
4923 RECT rect;
4924 int left, top, h;
4925 struct glyph *cursor_glyph;
4926 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
4928 /* Get the glyph the cursor is on. If we can't tell because
4929 the current matrix is invalid or such, give up. */
4930 cursor_glyph = get_phys_cursor_glyph (w);
4931 if (cursor_glyph == NULL)
4932 return;
4934 /* Compute frame-relative coordinates for phys cursor. */
4935 get_phys_cursor_geometry (w, row, cursor_glyph, &left, &top, &h);
4936 rect.left = left;
4937 rect.top = top;
4938 rect.bottom = rect.top + h;
4939 rect.right = rect.left + w->phys_cursor_width;
4941 hdc = get_frame_dc (f);
4942 /* Set clipping, draw the rectangle, and reset clipping again. */
4943 w32_clip_to_row (w, row, TEXT_AREA, hdc);
4944 FrameRect (hdc, &rect, hb);
4945 DeleteObject (hb);
4946 w32_set_clip_rectangle (hdc, NULL);
4947 release_frame_dc (f, hdc);
4951 /* Draw a bar cursor on window W in glyph row ROW.
4953 Implementation note: One would like to draw a bar cursor with an
4954 angle equal to the one given by the font property XA_ITALIC_ANGLE.
4955 Unfortunately, I didn't find a font yet that has this property set.
4956 --gerd. */
4958 static void
4959 x_draw_bar_cursor (w, row, width, kind)
4960 struct window *w;
4961 struct glyph_row *row;
4962 int width;
4963 enum text_cursor_kinds kind;
4965 struct frame *f = XFRAME (w->frame);
4966 struct glyph *cursor_glyph;
4967 int x;
4968 HDC hdc;
4970 /* If cursor is out of bounds, don't draw garbage. This can happen
4971 in mini-buffer windows when switching between echo area glyphs
4972 and mini-buffer. */
4973 cursor_glyph = get_phys_cursor_glyph (w);
4974 if (cursor_glyph == NULL)
4975 return;
4977 /* If on an image, draw like a normal cursor. That's usually better
4978 visible than drawing a bar, esp. if the image is large so that
4979 the bar might not be in the window. */
4980 if (cursor_glyph->type == IMAGE_GLYPH)
4982 struct glyph_row *row;
4983 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
4984 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
4986 else
4988 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
4989 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
4991 /* If the glyph's background equals the color we normally draw
4992 the bar cursor in, the bar cursor in its normal color is
4993 invisible. Use the glyph's foreground color instead in this
4994 case, on the assumption that the glyph's colors are chosen so
4995 that the glyph is legible. */
4996 if (face->background == cursor_color)
4997 cursor_color = face->foreground;
4999 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5001 if (width < 0)
5002 width = FRAME_CURSOR_WIDTH (f);
5003 width = min (cursor_glyph->pixel_width, width);
5005 w->phys_cursor_width = width;
5008 hdc = get_frame_dc (f);
5009 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5011 if (kind == BAR_CURSOR)
5013 w32_fill_area (f, hdc, cursor_color, x,
5014 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5015 width, row->height);
5017 else
5019 w32_fill_area (f, hdc, cursor_color, x,
5020 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5021 row->height - width),
5022 min (FRAME_COLUMN_WIDTH (f), cursor_glyph->pixel_width),
5023 width);
5026 w32_set_clip_rectangle (hdc, NULL);
5027 release_frame_dc (f, hdc);
5032 /* RIF: Define cursor CURSOR on frame F. */
5034 static void
5035 w32_define_frame_cursor (f, cursor)
5036 struct frame *f;
5037 Cursor cursor;
5039 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5043 /* RIF: Clear area on frame F. */
5045 static void
5046 w32_clear_frame_area (f, x, y, width, height)
5047 struct frame *f;
5048 int x, y, width, height;
5050 HDC hdc;
5052 hdc = get_frame_dc (f);
5053 w32_clear_area (f, hdc, x, y, width, height);
5054 release_frame_dc (f, hdc);
5057 /* RIF: Draw or clear cursor on window W. */
5059 static void
5060 w32_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
5061 struct window *w;
5062 struct glyph_row *glyph_row;
5063 int x, y;
5064 int cursor_type, cursor_width;
5065 int on_p, active_p;
5067 if (on_p)
5069 /* If the user wants to use the system caret, make sure our own
5070 cursor remains invisible. */
5071 if (w32_use_visible_system_caret)
5073 /* Call to erase_phys_cursor here seems to use the
5074 wrong values of w->phys_cursor, as they have been
5075 overwritten before this function was called. */
5076 if (w->phys_cursor_type != NO_CURSOR)
5077 erase_phys_cursor (w);
5079 cursor_type = w->phys_cursor_type = NO_CURSOR;
5080 w->phys_cursor_width = -1;
5082 else
5084 w->phys_cursor_type = cursor_type;
5087 w->phys_cursor_on_p = 1;
5089 /* If this is the active cursor, we need to track it with the
5090 system caret, so third party software like screen magnifiers
5091 and speech synthesizers can follow the cursor. */
5092 if (active_p)
5094 struct frame *f = XFRAME (WINDOW_FRAME (w));
5095 HWND hwnd = FRAME_W32_WINDOW (f);
5097 w32_system_caret_x
5098 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5099 w32_system_caret_y
5100 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5101 + glyph_row->ascent - w->phys_cursor_ascent);
5103 /* If the size of the active cursor changed, destroy the old
5104 system caret. */
5105 if (w32_system_caret_hwnd
5106 && (w32_system_caret_height != w->phys_cursor_height))
5107 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5109 w32_system_caret_height = w->phys_cursor_height;
5111 /* Move the system caret. */
5112 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5115 if (glyph_row->exact_window_width_line_p
5116 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
5118 glyph_row->cursor_in_fringe_p = 1;
5119 draw_fringe_bitmap (w, glyph_row, 0);
5120 return;
5123 switch (cursor_type)
5125 case HOLLOW_BOX_CURSOR:
5126 x_draw_hollow_cursor (w, glyph_row);
5127 break;
5129 case FILLED_BOX_CURSOR:
5130 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5131 break;
5133 case BAR_CURSOR:
5134 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5135 break;
5137 case HBAR_CURSOR:
5138 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5139 break;
5141 case NO_CURSOR:
5142 w->phys_cursor_width = 0;
5143 break;
5145 default:
5146 abort ();
5153 /* Icons. */
5156 x_bitmap_icon (f, icon)
5157 struct frame *f;
5158 Lisp_Object icon;
5160 HANDLE main_icon;
5161 HANDLE small_icon = NULL;
5163 if (FRAME_W32_WINDOW (f) == 0)
5164 return 1;
5166 if (NILP (icon))
5167 main_icon = LoadIcon (hinst, EMACS_CLASS);
5168 else if (STRINGP (icon))
5170 /* Load the main icon from the named file. */
5171 main_icon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5172 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5173 /* Try to load a small icon to go with it. */
5174 small_icon = LoadImage (NULL, (LPCSTR) SDATA (icon), IMAGE_ICON,
5175 GetSystemMetrics (SM_CXSMICON),
5176 GetSystemMetrics (SM_CYSMICON),
5177 LR_LOADFROMFILE);
5179 else if (SYMBOLP (icon))
5181 LPCTSTR name;
5183 if (EQ (icon, intern ("application")))
5184 name = (LPCTSTR) IDI_APPLICATION;
5185 else if (EQ (icon, intern ("hand")))
5186 name = (LPCTSTR) IDI_HAND;
5187 else if (EQ (icon, intern ("question")))
5188 name = (LPCTSTR) IDI_QUESTION;
5189 else if (EQ (icon, intern ("exclamation")))
5190 name = (LPCTSTR) IDI_EXCLAMATION;
5191 else if (EQ (icon, intern ("asterisk")))
5192 name = (LPCTSTR) IDI_ASTERISK;
5193 else if (EQ (icon, intern ("winlogo")))
5194 name = (LPCTSTR) IDI_WINLOGO;
5195 else
5196 return 1;
5198 main_icon = LoadIcon (NULL, name);
5200 else
5201 return 1;
5203 if (main_icon == NULL)
5204 return 1;
5206 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5207 (LPARAM) main_icon);
5209 /* If there is a small icon that goes with it, set that too. */
5210 if (small_icon)
5211 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_SMALL,
5212 (LPARAM) small_icon);
5214 return 0;
5218 /************************************************************************
5219 Handling X errors
5220 ************************************************************************/
5222 /* Display Error Handling functions not used on W32. Listing them here
5223 helps diff stay in step when comparing w32term.c with xterm.c.
5225 x_error_catcher (display, error)
5226 x_catch_errors (dpy)
5227 x_catch_errors_unwind (old_val)
5228 x_check_errors (dpy, format)
5229 x_fully_uncatch_errors ()
5230 x_catching_errors ()
5231 x_had_errors_p (dpy)
5232 x_clear_errors (dpy)
5233 x_uncatch_errors (dpy, count)
5234 x_trace_wire ()
5235 x_connection_signal (signalnum)
5236 x_connection_closed (dpy, error_message)
5237 x_error_quitter (display, error)
5238 x_error_handler (display, error)
5239 x_io_error_quitter (display)
5244 /* Changing the font of the frame. */
5246 Lisp_Object
5247 x_new_font (f, font_object, fontset)
5248 struct frame *f;
5249 Lisp_Object font_object;
5250 int fontset;
5252 struct font *font = XFONT_OBJECT (font_object);
5254 if (fontset < 0)
5255 fontset = fontset_from_font (font_object);
5256 FRAME_FONTSET (f) = fontset;
5257 if (FRAME_FONT (f) == font)
5258 /* This font is already set in frame F. There's nothing more to
5259 do. */
5260 return font_object;
5262 FRAME_FONT (f) = font;
5263 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
5264 FRAME_COLUMN_WIDTH (f) = font->average_width;
5265 FRAME_SPACE_WIDTH (f) = font->space_width;
5266 FRAME_LINE_HEIGHT (f) = font->height;
5268 compute_fringe_widths (f, 1);
5270 /* Compute the scroll bar width in character columns. */
5271 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5273 int wid = FRAME_COLUMN_WIDTH (f);
5274 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5275 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
5277 else
5279 int wid = FRAME_COLUMN_WIDTH (f);
5280 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5283 /* Now make the frame display the given font. */
5284 if (FRAME_X_WINDOW (f) != 0)
5286 /* Don't change the size of a tip frame; there's no point in
5287 doing it because it's done in Fx_show_tip, and it leads to
5288 problems because the tip frame has no widget. */
5289 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5290 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5293 /* X version sets font of input methods here also. */
5295 return font_object;
5299 /***********************************************************************
5300 TODO: W32 Input Methods
5301 ***********************************************************************/
5302 /* Listing missing functions from xterm.c helps diff stay in step.
5304 xim_destroy_callback (xim, client_data, call_data)
5305 xim_open_dpy (dpyinfo, resource_name)
5306 struct xim_inst_t
5307 xim_instantiate_callback (display, client_data, call_data)
5308 xim_initialize (dpyinfo, resource_name)
5309 xim_close_dpy (dpyinfo)
5314 /* Calculate the absolute position in frame F
5315 from its current recorded position values and gravity. */
5317 void
5318 x_calc_absolute_position (f)
5319 struct frame *f;
5321 int flags = f->size_hint_flags;
5323 /* The sum of the widths of the frame's left and right borders, and
5324 the sum of the heights of the frame's top and bottom borders (in
5325 pixels) drawn by Windows. */
5326 unsigned int left_right_borders_width, top_bottom_borders_height;
5328 /* Try to get the actual values of these two variables. We compute
5329 the border width (height) by subtracting the width (height) of
5330 the frame's client area from the width (height) of the frame's
5331 entire window. */
5332 WINDOWPLACEMENT wp = { 0 };
5333 RECT client_rect = { 0 };
5335 if (GetWindowPlacement (FRAME_W32_WINDOW (f), &wp)
5336 && GetClientRect (FRAME_W32_WINDOW (f), &client_rect))
5338 left_right_borders_width =
5339 (wp.rcNormalPosition.right - wp.rcNormalPosition.left) -
5340 (client_rect.right - client_rect.left);
5342 top_bottom_borders_height =
5343 (wp.rcNormalPosition.bottom - wp.rcNormalPosition.top) -
5344 (client_rect.bottom - client_rect.top);
5346 else
5348 /* Use sensible default values. */
5349 left_right_borders_width = 8;
5350 top_bottom_borders_height = 32;
5353 /* Treat negative positions as relative to the rightmost bottommost
5354 position that fits on the screen. */
5355 if (flags & XNegative)
5356 f->left_pos = (x_display_pixel_width (FRAME_W32_DISPLAY_INFO (f))
5357 - FRAME_PIXEL_WIDTH (f)
5358 + f->left_pos
5359 - (left_right_borders_width - 1));
5361 if (flags & YNegative)
5362 f->top_pos = (x_display_pixel_height (FRAME_W32_DISPLAY_INFO (f))
5363 - FRAME_PIXEL_HEIGHT (f)
5364 + f->top_pos
5365 - (top_bottom_borders_height - 1));
5367 /* The left_pos and top_pos are now relative to the top and left
5368 screen edges, so the flags should correspond. */
5369 f->size_hint_flags &= ~ (XNegative | YNegative);
5372 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5373 to really change the position, and 0 when calling from
5374 x_make_frame_visible (in that case, XOFF and YOFF are the current
5375 position values). It is -1 when calling from x_set_frame_parameters,
5376 which means, do adjust for borders but don't change the gravity. */
5378 void
5379 x_set_offset (f, xoff, yoff, change_gravity)
5380 struct frame *f;
5381 register int xoff, yoff;
5382 int change_gravity;
5384 int modified_top, modified_left;
5386 if (change_gravity > 0)
5388 f->top_pos = yoff;
5389 f->left_pos = xoff;
5390 f->size_hint_flags &= ~ (XNegative | YNegative);
5391 if (xoff < 0)
5392 f->size_hint_flags |= XNegative;
5393 if (yoff < 0)
5394 f->size_hint_flags |= YNegative;
5395 f->win_gravity = NorthWestGravity;
5397 x_calc_absolute_position (f);
5399 BLOCK_INPUT;
5400 x_wm_set_size_hint (f, (long) 0, 0);
5402 modified_left = f->left_pos;
5403 modified_top = f->top_pos;
5405 my_set_window_pos (FRAME_W32_WINDOW (f),
5406 NULL,
5407 modified_left, modified_top,
5408 0, 0,
5409 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5410 UNBLOCK_INPUT;
5414 /* Check if we need to resize the frame due to a fullscreen request.
5415 If so needed, resize the frame. */
5416 static void
5417 x_check_fullscreen (f)
5418 struct frame *f;
5420 if (f->want_fullscreen & FULLSCREEN_BOTH)
5422 int width, height, ign;
5424 x_real_positions (f, &f->left_pos, &f->top_pos);
5426 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5428 /* We do not need to move the window, it shall be taken care of
5429 when setting WM manager hints. */
5430 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5432 change_frame_size (f, height, width, 0, 1, 0);
5433 SET_FRAME_GARBAGED (f);
5434 cancel_mouse_face (f);
5436 /* Wait for the change of frame size to occur */
5437 f->want_fullscreen |= FULLSCREEN_WAIT;
5442 /* Call this to change the size of frame F's x-window.
5443 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5444 for this size change and subsequent size changes.
5445 Otherwise we leave the window gravity unchanged. */
5447 void
5448 x_set_window_size (f, change_gravity, cols, rows)
5449 struct frame *f;
5450 int change_gravity;
5451 int cols, rows;
5453 int pixelwidth, pixelheight;
5455 BLOCK_INPUT;
5457 check_frame_size (f, &rows, &cols);
5458 f->scroll_bar_actual_width
5459 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5461 compute_fringe_widths (f, 0);
5463 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5464 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5466 f->win_gravity = NorthWestGravity;
5467 x_wm_set_size_hint (f, (long) 0, 0);
5470 RECT rect;
5472 rect.left = rect.top = 0;
5473 rect.right = pixelwidth;
5474 rect.bottom = pixelheight;
5476 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
5477 FRAME_EXTERNAL_MENU_BAR (f));
5479 my_set_window_pos (FRAME_W32_WINDOW (f),
5480 NULL,
5481 0, 0,
5482 rect.right - rect.left,
5483 rect.bottom - rect.top,
5484 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5487 #if 0
5488 /* The following mirrors what is done in xterm.c. It appears to be
5489 for informing lisp of the new size immediately, while the actual
5490 resize will happen asynchronously. But on Windows, the menu bar
5491 automatically wraps when the frame is too narrow to contain it,
5492 and that causes any calculations made here to come out wrong. The
5493 end is some nasty buggy behavior, including the potential loss
5494 of the minibuffer.
5496 Disabling this code is either not sufficient to fix the problems
5497 completely, or it causes fresh problems, but at least it removes
5498 the most problematic symptom of the minibuffer becoming unusable.
5500 -----------------------------------------------------------------
5502 Now, strictly speaking, we can't be sure that this is accurate,
5503 but the window manager will get around to dealing with the size
5504 change request eventually, and we'll hear how it went when the
5505 ConfigureNotify event gets here.
5507 We could just not bother storing any of this information here,
5508 and let the ConfigureNotify event set everything up, but that
5509 might be kind of confusing to the Lisp code, since size changes
5510 wouldn't be reported in the frame parameters until some random
5511 point in the future when the ConfigureNotify event arrives.
5513 We pass 1 for DELAY since we can't run Lisp code inside of
5514 a BLOCK_INPUT. */
5515 change_frame_size (f, rows, cols, 0, 1, 0);
5516 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5517 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5519 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5520 receive in the ConfigureNotify event; if we get what we asked
5521 for, then the event won't cause the screen to become garbaged, so
5522 we have to make sure to do it here. */
5523 SET_FRAME_GARBAGED (f);
5525 /* If cursor was outside the new size, mark it as off. */
5526 mark_window_cursors_off (XWINDOW (f->root_window));
5528 /* Clear out any recollection of where the mouse highlighting was,
5529 since it might be in a place that's outside the new frame size.
5530 Actually checking whether it is outside is a pain in the neck,
5531 so don't try--just let the highlighting be done afresh with new size. */
5532 cancel_mouse_face (f);
5533 #endif
5535 UNBLOCK_INPUT;
5538 /* Mouse warping. */
5540 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5542 void
5543 x_set_mouse_position (f, x, y)
5544 struct frame *f;
5545 int x, y;
5547 int pix_x, pix_y;
5549 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5550 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5552 if (pix_x < 0) pix_x = 0;
5553 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5555 if (pix_y < 0) pix_y = 0;
5556 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5558 x_set_mouse_pixel_position (f, pix_x, pix_y);
5561 void
5562 x_set_mouse_pixel_position (f, pix_x, pix_y)
5563 struct frame *f;
5564 int pix_x, pix_y;
5566 RECT rect;
5567 POINT pt;
5569 BLOCK_INPUT;
5571 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5572 pt.x = rect.left + pix_x;
5573 pt.y = rect.top + pix_y;
5574 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5576 SetCursorPos (pt.x, pt.y);
5578 UNBLOCK_INPUT;
5582 /* focus shifting, raising and lowering. */
5584 void
5585 x_focus_on_frame (f)
5586 struct frame *f;
5588 struct w32_display_info *dpyinfo = &one_w32_display_info;
5590 /* Give input focus to frame. */
5591 BLOCK_INPUT;
5592 #if 0
5593 /* Try not to change its Z-order if possible. */
5594 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5595 my_set_focus (f, FRAME_W32_WINDOW (f));
5596 else
5597 #endif
5598 my_set_foreground_window (FRAME_W32_WINDOW (f));
5599 UNBLOCK_INPUT;
5602 void
5603 x_unfocus_frame (f)
5604 struct frame *f;
5608 /* Raise frame F. */
5609 void
5610 x_raise_frame (f)
5611 struct frame *f;
5613 BLOCK_INPUT;
5615 /* Strictly speaking, raise-frame should only change the frame's Z
5616 order, leaving input focus unchanged. This is reasonable behavior
5617 on X where the usual policy is point-to-focus. However, this
5618 behavior would be very odd on Windows where the usual policy is
5619 click-to-focus.
5621 On X, if the mouse happens to be over the raised frame, it gets
5622 input focus anyway (so the window with focus will never be
5623 completely obscured) - if not, then just moving the mouse over it
5624 is sufficient to give it focus. On Windows, the user must actually
5625 click on the frame (preferrably the title bar so as not to move
5626 point), which is more awkward. Also, no other Windows program
5627 raises a window to the top but leaves another window (possibly now
5628 completely obscured) with input focus.
5630 Because there is a system setting on Windows that allows the user
5631 to choose the point to focus policy, we make the strict semantics
5632 optional, but by default we grab focus when raising. */
5634 if (NILP (Vw32_grab_focus_on_raise))
5636 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5637 not already the foreground application: the frame is raised
5638 above all other frames belonging to us, but not above the
5639 current top window. To achieve that, we have to resort to this
5640 more cumbersome method. */
5642 HDWP handle = BeginDeferWindowPos (2);
5643 if (handle)
5645 DeferWindowPos (handle,
5646 FRAME_W32_WINDOW (f),
5647 HWND_TOP,
5648 0, 0, 0, 0,
5649 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5651 DeferWindowPos (handle,
5652 GetForegroundWindow (),
5653 FRAME_W32_WINDOW (f),
5654 0, 0, 0, 0,
5655 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5657 EndDeferWindowPos (handle);
5660 else
5662 my_set_foreground_window (FRAME_W32_WINDOW (f));
5665 UNBLOCK_INPUT;
5668 /* Lower frame F. */
5669 void
5670 x_lower_frame (f)
5671 struct frame *f;
5673 BLOCK_INPUT;
5674 my_set_window_pos (FRAME_W32_WINDOW (f),
5675 HWND_BOTTOM,
5676 0, 0, 0, 0,
5677 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5678 UNBLOCK_INPUT;
5681 static void
5682 w32_frame_raise_lower (f, raise_flag)
5683 FRAME_PTR f;
5684 int raise_flag;
5686 if (! FRAME_W32_P (f))
5687 return;
5689 if (raise_flag)
5690 x_raise_frame (f);
5691 else
5692 x_lower_frame (f);
5695 /* Change of visibility. */
5697 /* This tries to wait until the frame is really visible.
5698 However, if the window manager asks the user where to position
5699 the frame, this will return before the user finishes doing that.
5700 The frame will not actually be visible at that time,
5701 but it will become visible later when the window manager
5702 finishes with it. */
5704 void
5705 x_make_frame_visible (f)
5706 struct frame *f;
5708 Lisp_Object type;
5710 BLOCK_INPUT;
5712 type = x_icon_type (f);
5713 if (!NILP (type))
5714 x_bitmap_icon (f, type);
5716 if (! FRAME_VISIBLE_P (f))
5718 /* We test FRAME_GARBAGED_P here to make sure we don't
5719 call x_set_offset a second time
5720 if we get to x_make_frame_visible a second time
5721 before the window gets really visible. */
5722 if (! FRAME_ICONIFIED_P (f)
5723 && ! f->output_data.w32->asked_for_visible)
5725 RECT workarea_rect;
5726 RECT window_rect;
5728 /* Adjust vertical window position in order to avoid being
5729 covered by a task bar placed at the bottom of the desktop. */
5730 SystemParametersInfo(SPI_GETWORKAREA, 0, &workarea_rect, 0);
5731 GetWindowRect(FRAME_W32_WINDOW(f), &window_rect);
5732 if (window_rect.bottom > workarea_rect.bottom
5733 && window_rect.top > workarea_rect.top)
5734 f->top_pos = max (window_rect.top
5735 - window_rect.bottom + workarea_rect.bottom,
5736 workarea_rect.top);
5738 x_set_offset (f, f->left_pos, f->top_pos, 0);
5741 f->output_data.w32->asked_for_visible = 1;
5743 /* The first of these seems to give more expected behavior, but
5744 was added as a commented out line in Sept 1997, with the
5745 second version remaining uncommented. There may have been
5746 some problem with it that led to it not being enabled,
5747 so the old version remains commented out below in case we
5748 decide we need to go back to it [23.0.60 2008-06-09]. */
5749 my_show_window (f, FRAME_W32_WINDOW (f),
5750 f->async_iconified ? SW_RESTORE : SW_SHOW);
5751 /* my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL); */
5754 /* Synchronize to ensure Emacs knows the frame is visible
5755 before we do anything else. We do this loop with input not blocked
5756 so that incoming events are handled. */
5758 Lisp_Object frame;
5759 int count;
5761 /* This must come after we set COUNT. */
5762 UNBLOCK_INPUT;
5764 XSETFRAME (frame, f);
5766 /* Wait until the frame is visible. Process X events until a
5767 MapNotify event has been seen, or until we think we won't get a
5768 MapNotify at all.. */
5769 for (count = input_signal_count + 10;
5770 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5772 /* Force processing of queued events. */
5773 /* TODO: x_sync equivalent? */
5775 /* Machines that do polling rather than SIGIO have been observed
5776 to go into a busy-wait here. So we'll fake an alarm signal
5777 to let the handler know that there's something to be read.
5778 We used to raise a real alarm, but it seems that the handler
5779 isn't always enabled here. This is probably a bug. */
5780 if (input_polling_used ())
5782 /* It could be confusing if a real alarm arrives while processing
5783 the fake one. Turn it off and let the handler reset it. */
5784 int old_poll_suppress_count = poll_suppress_count;
5785 poll_suppress_count = 1;
5786 poll_for_input_1 ();
5787 poll_suppress_count = old_poll_suppress_count;
5790 FRAME_SAMPLE_VISIBILITY (f);
5794 /* Change from mapped state to withdrawn state. */
5796 /* Make the frame visible (mapped and not iconified). */
5798 x_make_frame_invisible (f)
5799 struct frame *f;
5801 /* Don't keep the highlight on an invisible frame. */
5802 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5803 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5805 BLOCK_INPUT;
5807 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
5809 /* We can't distinguish this from iconification
5810 just by the event that we get from the server.
5811 So we can't win using the usual strategy of letting
5812 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5813 and synchronize with the server to make sure we agree. */
5814 f->visible = 0;
5815 FRAME_ICONIFIED_P (f) = 0;
5816 f->async_visible = 0;
5817 f->async_iconified = 0;
5819 UNBLOCK_INPUT;
5822 /* Change window state from mapped to iconified. */
5824 void
5825 x_iconify_frame (f)
5826 struct frame *f;
5828 Lisp_Object type;
5830 /* Don't keep the highlight on an invisible frame. */
5831 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5832 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5834 if (f->async_iconified)
5835 return;
5837 BLOCK_INPUT;
5839 type = x_icon_type (f);
5840 if (!NILP (type))
5841 x_bitmap_icon (f, type);
5843 /* Simulate the user minimizing the frame. */
5844 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
5846 UNBLOCK_INPUT;
5850 /* Free X resources of frame F. */
5852 void
5853 x_free_frame_resources (f)
5854 struct frame *f;
5856 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5858 BLOCK_INPUT;
5860 /* We must free faces before destroying windows because some
5861 font-driver (e.g. xft) access a window while finishing a
5862 face. */
5863 if (FRAME_FACE_CACHE (f))
5864 free_frame_faces (f);
5866 if (FRAME_W32_WINDOW (f))
5867 my_destroy_window (f, FRAME_W32_WINDOW (f));
5869 free_frame_menubar (f);
5871 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
5872 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
5873 unload_color (f, f->output_data.w32->cursor_pixel);
5874 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
5875 unload_color (f, f->output_data.w32->border_pixel);
5876 unload_color (f, f->output_data.w32->mouse_pixel);
5877 if (f->output_data.w32->white_relief.allocated_p)
5878 unload_color (f, f->output_data.w32->white_relief.pixel);
5879 if (f->output_data.w32->black_relief.allocated_p)
5880 unload_color (f, f->output_data.w32->black_relief.pixel);
5882 if (FRAME_FACE_CACHE (f))
5883 free_frame_faces (f);
5885 xfree (f->output_data.w32);
5886 f->output_data.w32 = NULL;
5888 if (f == dpyinfo->w32_focus_frame)
5889 dpyinfo->w32_focus_frame = 0;
5890 if (f == dpyinfo->w32_focus_event_frame)
5891 dpyinfo->w32_focus_event_frame = 0;
5892 if (f == dpyinfo->x_highlight_frame)
5893 dpyinfo->x_highlight_frame = 0;
5895 if (f == dpyinfo->mouse_face_mouse_frame)
5897 dpyinfo->mouse_face_beg_row
5898 = dpyinfo->mouse_face_beg_col = -1;
5899 dpyinfo->mouse_face_end_row
5900 = dpyinfo->mouse_face_end_col = -1;
5901 dpyinfo->mouse_face_window = Qnil;
5902 dpyinfo->mouse_face_deferred_gc = 0;
5903 dpyinfo->mouse_face_mouse_frame = 0;
5906 UNBLOCK_INPUT;
5910 /* Destroy the window of frame F. */
5911 void
5912 x_destroy_window (f)
5913 struct frame *f;
5915 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5917 x_free_frame_resources (f);
5918 dpyinfo->reference_count--;
5922 /* Setting window manager hints. */
5924 /* Set the normal size hints for the window manager, for frame F.
5925 FLAGS is the flags word to use--or 0 meaning preserve the flags
5926 that the window now has.
5927 If USER_POSITION is nonzero, we set the USPosition
5928 flag (this is useful when FLAGS is 0). */
5929 void
5930 x_wm_set_size_hint (f, flags, user_position)
5931 struct frame *f;
5932 long flags;
5933 int user_position;
5935 Window window = FRAME_W32_WINDOW (f);
5937 enter_crit ();
5939 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
5940 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
5941 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
5942 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
5944 leave_crit ();
5947 /* Window manager things */
5948 void
5949 x_wm_set_icon_position (f, icon_x, icon_y)
5950 struct frame *f;
5951 int icon_x, icon_y;
5953 #if 0
5954 Window window = FRAME_W32_WINDOW (f);
5956 f->display.x->wm_hints.flags |= IconPositionHint;
5957 f->display.x->wm_hints.icon_x = icon_x;
5958 f->display.x->wm_hints.icon_y = icon_y;
5960 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
5961 #endif
5965 /***********************************************************************
5966 Initialization
5967 ***********************************************************************/
5969 static int w32_initialized = 0;
5971 void
5972 w32_initialize_display_info (display_name)
5973 Lisp_Object display_name;
5975 struct w32_display_info *dpyinfo = &one_w32_display_info;
5977 bzero (dpyinfo, sizeof (*dpyinfo));
5979 /* Put it on w32_display_name_list. */
5980 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
5981 w32_display_name_list);
5982 dpyinfo->name_list_element = XCAR (w32_display_name_list);
5984 dpyinfo->w32_id_name
5985 = (char *) xmalloc (SCHARS (Vinvocation_name)
5986 + SCHARS (Vsystem_name)
5987 + 2);
5988 sprintf (dpyinfo->w32_id_name, "%s@%s",
5989 SDATA (Vinvocation_name), SDATA (Vsystem_name));
5991 /* Default Console mode values - overridden when running in GUI mode
5992 with values obtained from system metrics. */
5993 dpyinfo->resx = 1;
5994 dpyinfo->resy = 1;
5995 dpyinfo->n_planes = 1;
5996 dpyinfo->n_cbits = 4;
5997 dpyinfo->n_fonts = 0;
5998 dpyinfo->smallest_font_height = 1;
5999 dpyinfo->smallest_char_width = 1;
6001 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6002 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6003 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
6004 dpyinfo->mouse_face_window = Qnil;
6005 dpyinfo->mouse_face_overlay = Qnil;
6006 dpyinfo->mouse_face_hidden = 0;
6008 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6009 /* TODO: dpyinfo->gray */
6013 /* Create an xrdb-style database of resources to supercede registry settings.
6014 The database is just a concatenation of C strings, finished by an additional
6015 \0. The strings are submitted to some basic normalization, so
6017 [ *]option[ *]:[ *]value...
6019 becomes
6021 option:value...
6023 but any whitespace following value is not removed. */
6025 static char *
6026 w32_make_rdb (xrm_option)
6027 char *xrm_option;
6029 char *buffer = xmalloc (strlen (xrm_option) + 2);
6030 char *current = buffer;
6031 char ch;
6032 int in_option = 1;
6033 int before_value = 0;
6035 do {
6036 ch = *xrm_option++;
6038 if (ch == '\n')
6040 *current++ = '\0';
6041 in_option = 1;
6042 before_value = 0;
6044 else if (ch != ' ')
6046 *current++ = ch;
6047 if (in_option && (ch == ':'))
6049 in_option = 0;
6050 before_value = 1;
6052 else if (before_value)
6054 before_value = 0;
6057 else if (!(in_option || before_value))
6059 *current++ = ch;
6061 } while (ch);
6063 *current = '\0';
6065 return buffer;
6068 void
6069 x_flush (struct frame * f)
6070 { /* Nothing to do */ }
6073 extern frame_parm_handler w32_frame_parm_handlers[];
6075 static struct redisplay_interface w32_redisplay_interface =
6077 w32_frame_parm_handlers,
6078 x_produce_glyphs,
6079 x_write_glyphs,
6080 x_insert_glyphs,
6081 x_clear_end_of_line,
6082 x_scroll_run,
6083 x_after_update_window_line,
6084 x_update_window_begin,
6085 x_update_window_end,
6086 x_cursor_to,
6087 x_flush,
6088 0, /* flush_display_optional */
6089 x_clear_window_mouse_face,
6090 x_get_glyph_overhangs,
6091 x_fix_overlapping_area,
6092 w32_draw_fringe_bitmap,
6093 w32_define_fringe_bitmap,
6094 w32_destroy_fringe_bitmap,
6095 w32_compute_glyph_string_overhangs,
6096 x_draw_glyph_string,
6097 w32_define_frame_cursor,
6098 w32_clear_frame_area,
6099 w32_draw_window_cursor,
6100 w32_draw_vertical_window_border,
6101 w32_shift_glyphs_for_insert
6104 static void x_delete_terminal (struct terminal *term);
6106 static struct terminal *
6107 w32_create_terminal (struct w32_display_info *dpyinfo)
6109 struct terminal *terminal;
6111 terminal = create_terminal ();
6113 terminal->type = output_w32;
6114 terminal->display_info.w32 = dpyinfo;
6115 dpyinfo->terminal = terminal;
6117 /* MSVC does not type K&R functions with no arguments correctly, and
6118 so we must explicitly cast them. */
6119 terminal->clear_frame_hook = x_clear_frame;
6120 terminal->ins_del_lines_hook = x_ins_del_lines;
6121 terminal->delete_glyphs_hook = x_delete_glyphs;
6122 terminal->ring_bell_hook = w32_ring_bell;
6123 terminal->reset_terminal_modes_hook = w32_reset_terminal_modes;
6124 terminal->set_terminal_modes_hook = w32_set_terminal_modes;
6125 terminal->update_begin_hook = x_update_begin;
6126 terminal->update_end_hook = x_update_end;
6127 terminal->set_terminal_window_hook = w32_set_terminal_window;
6128 terminal->read_socket_hook = w32_read_socket;
6129 terminal->frame_up_to_date_hook = w32_frame_up_to_date;
6130 terminal->mouse_position_hook = w32_mouse_position;
6131 terminal->frame_rehighlight_hook = w32_frame_rehighlight;
6132 terminal->frame_raise_lower_hook = w32_frame_raise_lower;
6133 // terminal->fullscreen_hook = XTfullscreen_hook;
6134 terminal->set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6135 terminal->condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6136 terminal->redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6137 terminal->judge_scroll_bars_hook = w32_judge_scroll_bars;
6139 terminal->delete_frame_hook = x_destroy_window;
6140 terminal->delete_terminal_hook = x_delete_terminal;
6142 terminal->rif = &w32_redisplay_interface;
6143 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
6144 terminal->char_ins_del_ok = 1;
6145 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
6146 terminal->fast_clear_end_of_line = 1; /* X does this well. */
6147 terminal->memory_below_frame = 0; /* We don't remember what scrolls
6148 off the bottom. */
6150 /* We don't yet support separate terminals on W32, so don't try to share
6151 keyboards between virtual terminals that are on the same physical
6152 terminal like X does. */
6153 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
6154 init_kboard (terminal->kboard);
6155 terminal->kboard->Vwindow_system = intern ("w32");
6156 terminal->kboard->next_kboard = all_kboards;
6157 all_kboards = terminal->kboard;
6158 /* Don't let the initial kboard remain current longer than necessary.
6159 That would cause problems if a file loaded on startup tries to
6160 prompt in the mini-buffer. */
6161 if (current_kboard == initial_kboard)
6162 current_kboard = terminal->kboard;
6163 terminal->kboard->reference_count++;
6165 return terminal;
6168 static void
6169 x_delete_terminal (struct terminal *terminal)
6171 struct w32_display_info *dpyinfo = terminal->display_info.w32;
6172 int i;
6174 /* Protect against recursive calls. delete_frame in
6175 delete_terminal calls us back when it deletes our last frame. */
6176 if (!terminal->name)
6177 return;
6179 BLOCK_INPUT;
6181 x_delete_display (dpyinfo);
6182 UNBLOCK_INPUT;
6185 struct w32_display_info *
6186 w32_term_init (display_name, xrm_option, resource_name)
6187 Lisp_Object display_name;
6188 char *xrm_option;
6189 char *resource_name;
6191 struct w32_display_info *dpyinfo;
6192 struct terminal *terminal;
6193 HDC hdc;
6195 BLOCK_INPUT;
6197 if (!w32_initialized)
6199 w32_initialize ();
6200 w32_initialized = 1;
6203 w32_initialize_display_info (display_name);
6205 dpyinfo = &one_w32_display_info;
6206 terminal = w32_create_terminal (dpyinfo);
6208 /* Set the name of the terminal. */
6209 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
6210 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
6211 terminal->name[SBYTES (display_name)] = 0;
6213 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6215 /* Put this display on the chain. */
6216 dpyinfo->next = x_display_list;
6217 x_display_list = dpyinfo;
6219 hdc = GetDC (GetDesktopWindow ());
6221 dpyinfo->root_window = GetDesktopWindow ();
6222 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6223 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6224 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6225 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6226 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6227 ReleaseDC (GetDesktopWindow (), hdc);
6229 /* initialise palette with white and black */
6231 XColor color;
6232 w32_defined_color (0, "white", &color, 1);
6233 w32_defined_color (0, "black", &color, 1);
6236 /* Add the default keyboard. */
6237 add_keyboard_wait_descriptor (0);
6239 /* Create Fringe Bitmaps and store them for later use.
6241 On W32, bitmaps are all unsigned short, as Windows requires
6242 bitmap data to be Word aligned. For some reason they are
6243 horizontally reflected compared to how they appear on X, so we
6244 need to bitswap and convert to unsigned shorts before creating
6245 the bitmaps. */
6246 w32_init_fringe (terminal->rif);
6248 #ifdef F_SETOWN
6249 fcntl (connection, F_SETOWN, getpid ());
6250 #endif /* ! defined (F_SETOWN) */
6252 #ifdef SIGIO
6253 if (interrupt_input)
6254 init_sigio (connection);
6255 #endif /* ! defined (SIGIO) */
6257 UNBLOCK_INPUT;
6259 return dpyinfo;
6262 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6263 void
6264 x_delete_display (dpyinfo)
6265 struct w32_display_info *dpyinfo;
6267 /* Discard this display from w32_display_name_list and w32_display_list.
6268 We can't use Fdelq because that can quit. */
6269 if (! NILP (w32_display_name_list)
6270 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
6271 w32_display_name_list = XCDR (w32_display_name_list);
6272 else
6274 Lisp_Object tail;
6276 tail = w32_display_name_list;
6277 while (CONSP (tail) && CONSP (XCDR (tail)))
6279 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
6281 XSETCDR (tail, XCDR (XCDR (tail)));
6282 break;
6284 tail = XCDR (tail);
6288 /* free palette table */
6290 struct w32_palette_entry * plist;
6292 plist = dpyinfo->color_list;
6293 while (plist)
6295 struct w32_palette_entry * pentry = plist;
6296 plist = plist->next;
6297 xfree (pentry);
6299 dpyinfo->color_list = NULL;
6300 if (dpyinfo->palette)
6301 DeleteObject(dpyinfo->palette);
6303 xfree (dpyinfo->w32_id_name);
6305 w32_reset_fringes ();
6308 /* Set up use of W32. */
6310 DWORD WINAPI w32_msg_worker (void * arg);
6312 static void
6313 w32_initialize ()
6315 baud_rate = 19200;
6317 w32_system_caret_hwnd = NULL;
6318 w32_system_caret_height = 0;
6319 w32_system_caret_x = 0;
6320 w32_system_caret_y = 0;
6322 /* Initialize w32_use_visible_system_caret based on whether a screen
6323 reader is in use. */
6324 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6325 &w32_use_visible_system_caret, 0))
6326 w32_use_visible_system_caret = 0;
6328 last_tool_bar_item = -1;
6329 any_help_event_p = 0;
6331 /* Initialize input mode: interrupt_input off, no flow control, allow
6332 8 bit character input, standard quit char. */
6333 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6335 /* Create the window thread - it will terminate itself or when the app terminates */
6337 init_crit ();
6339 dwMainThreadId = GetCurrentThreadId ();
6340 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6341 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6343 /* Wait for thread to start */
6346 MSG msg;
6348 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6350 hWindowsThread = CreateThread (NULL, 0,
6351 w32_msg_worker,
6352 0, 0, &dwWindowsThreadId);
6354 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6357 /* It is desirable that mainThread should have the same notion of
6358 focus window and active window as windowsThread. Unfortunately, the
6359 following call to AttachThreadInput, which should do precisely what
6360 we need, causes major problems when Emacs is linked as a console
6361 program. Unfortunately, we have good reasons for doing that, so
6362 instead we need to send messages to windowsThread to make some API
6363 calls for us (ones that affect, or depend on, the active/focus
6364 window state.) */
6365 #ifdef ATTACH_THREADS
6366 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6367 #endif
6369 /* Dynamically link to optional system components. */
6371 HANDLE user_lib = LoadLibrary ("user32.dll");
6373 #define LOAD_PROC(lib, fn) pfn##fn = (void *) GetProcAddress (lib, #fn)
6375 LOAD_PROC (user_lib, SetLayeredWindowAttributes);
6377 #undef LOAD_PROC
6379 FreeLibrary (user_lib);
6381 /* Ensure scrollbar handle is at least 5 pixels. */
6382 vertical_scroll_bar_min_handle = 5;
6384 /* For either kind of scroll bar, take account of the arrows; these
6385 effectively form the border of the main scroll bar range. */
6386 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6387 = GetSystemMetrics (SM_CYVSCROLL);
6391 void
6392 syms_of_w32term ()
6394 staticpro (&w32_display_name_list);
6395 w32_display_name_list = Qnil;
6397 staticpro (&last_mouse_scroll_bar);
6398 last_mouse_scroll_bar = Qnil;
6400 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
6402 DEFVAR_INT ("w32-num-mouse-buttons",
6403 &w32_num_mouse_buttons,
6404 doc: /* Number of physical mouse buttons. */);
6405 w32_num_mouse_buttons = 2;
6407 DEFVAR_LISP ("w32-swap-mouse-buttons",
6408 &Vw32_swap_mouse_buttons,
6409 doc: /* Swap the mapping of middle and right mouse buttons.
6410 When nil, middle button is mouse-2 and right button is mouse-3. */);
6411 Vw32_swap_mouse_buttons = Qnil;
6413 DEFVAR_LISP ("w32-grab-focus-on-raise",
6414 &Vw32_grab_focus_on_raise,
6415 doc: /* Raised frame grabs input focus.
6416 When t, `raise-frame' grabs input focus as well. This fits well
6417 with the normal Windows click-to-focus policy, but might not be
6418 desirable when using a point-to-focus policy. */);
6419 Vw32_grab_focus_on_raise = Qt;
6421 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6422 &Vw32_capslock_is_shiftlock,
6423 doc: /* Apply CapsLock state to non character input keys.
6424 When nil, CapsLock only affects normal character input keys. */);
6425 Vw32_capslock_is_shiftlock = Qnil;
6427 DEFVAR_LISP ("w32-recognize-altgr",
6428 &Vw32_recognize_altgr,
6429 doc: /* Recognize right-alt and left-ctrl as AltGr.
6430 When nil, the right-alt and left-ctrl key combination is
6431 interpreted normally. */);
6432 Vw32_recognize_altgr = Qt;
6434 DEFVAR_BOOL ("w32-use-visible-system-caret",
6435 &w32_use_visible_system_caret,
6436 doc: /* Flag to make the system caret visible.
6437 When this is non-nil, Emacs will indicate the position of point by
6438 using the system caret instead of drawing its own cursor. Some screen
6439 reader software does not track the system cursor properly when it is
6440 invisible, and gets confused by Emacs drawing its own cursor, so this
6441 variable is initialized to t when Emacs detects that screen reader
6442 software is running as it starts up.
6444 When this variable is set, other variables affecting the appearance of
6445 the cursor have no effect. */);
6447 w32_use_visible_system_caret = 0;
6449 /* We don't yet support this, but defining this here avoids whining
6450 from cus-start.el and other places, like "M-x set-variable". */
6451 DEFVAR_BOOL ("x-use-underline-position-properties",
6452 &x_use_underline_position_properties,
6453 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
6454 A value of nil means ignore them. If you encounter fonts with bogus
6455 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6456 to 4.1, set this to nil.
6458 NOTE: Not supported on MS-Windows yet. */);
6459 x_use_underline_position_properties = 0;
6461 DEFVAR_BOOL ("x-underline-at-descent-line",
6462 &x_underline_at_descent_line,
6463 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
6464 A value of nil means to draw the underline according to the value of the
6465 variable `x-use-underline-position-properties', which is usually at the
6466 baseline level. The default value is nil. */);
6467 x_underline_at_descent_line = 0;
6469 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
6470 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6471 Vx_toolkit_scroll_bars = Qt;
6473 staticpro (&last_mouse_motion_frame);
6474 last_mouse_motion_frame = Qnil;
6477 /* arch-tag: 5fa70624-ab86-499c-8a85-473958ee4646
6478 (do not change this comment) */