(change-log-font-lock-keywords): Make the regexp for
[emacs.git] / src / w32term.c
blob1caa1313df40da7b650cbd3941a65f1707aaaf30
1 /* Implementation of GUI terminal on the Microsoft W32 API.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "lisp.h"
27 #include "charset.h"
28 #include "blockinput.h"
30 #include "w32heap.h"
31 #include "w32term.h"
32 #include "w32bdf.h"
33 #include <shellapi.h>
35 #include "systty.h"
36 #include "systime.h"
37 #include "atimer.h"
38 #include "keymap.h"
40 #include <ctype.h>
41 #include <errno.h>
42 #include <setjmp.h>
43 #include <sys/stat.h>
45 #include "keyboard.h"
46 #include "frame.h"
47 #include "dispextern.h"
48 #include "fontset.h"
49 #include "termhooks.h"
50 #include "termopts.h"
51 #include "termchar.h"
52 #include "gnu.h"
53 #include "disptab.h"
54 #include "buffer.h"
55 #include "window.h"
56 #include "intervals.h"
57 #include "composite.h"
58 #include "coding.h"
60 #define abs(x) ((x) < 0 ? -(x) : (x))
63 /* Fringe bitmaps. */
65 static int max_fringe_bmp = 0;
66 static HBITMAP *fringe_bmp = 0;
68 /* Non-nil means Emacs uses toolkit scroll bars. */
70 Lisp_Object Vx_toolkit_scroll_bars;
72 /* Temporary variables for w32_read_socket. */
74 static int last_mousemove_x = 0;
75 static int last_mousemove_y = 0;
77 /* Define GET_WHEEL_DELTA_WPARAM macro if system headers don't. */
78 #ifndef GET_WHEEL_DELTA_WPARAM
79 #define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD (wparam))
80 #endif
82 /* Non-zero means that a HELP_EVENT has been generated since Emacs
83 start. */
85 static int any_help_event_p;
87 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
88 static Lisp_Object last_window;
90 /* Non-zero means make use of UNDERLINE_POSITION font properties.
91 (Not yet supported, see TODO in x_draw_glyph_string.) */
92 int x_use_underline_position_properties;
94 extern unsigned int msh_mousewheel;
96 extern void free_frame_menubar ();
98 extern int w32_codepage_for_font (char *fontname);
99 extern Cursor w32_load_cursor (LPCTSTR name);
101 extern glyph_metric *w32_BDF_TextMetric(bdffont *fontp,
102 unsigned char *text, int dim);
103 extern Lisp_Object Vwindow_system;
105 #define x_any_window_to_frame x_window_to_frame
106 #define x_top_window_to_frame x_window_to_frame
109 /* This is display since w32 does not support multiple ones. */
110 struct w32_display_info one_w32_display_info;
111 struct w32_display_info *x_display_list;
113 /* This is a list of cons cells, each of the form (NAME . FONT-LIST-CACHE),
114 one for each element of w32_display_list and in the same order.
115 NAME is the name of the frame.
116 FONT-LIST-CACHE records previous values returned by x-list-fonts. */
117 Lisp_Object w32_display_name_list;
119 /* Frame being updated by update_frame. This is declared in term.c.
120 This is set by update_begin and looked at by all the
121 w32 functions. It is zero while not inside an update.
122 In that case, the w32 functions assume that `SELECTED_FRAME ()'
123 is the frame to apply to. */
124 extern struct frame *updating_frame;
126 /* This is a frame waiting to be autoraised, within w32_read_socket. */
127 struct frame *pending_autoraise_frame;
129 /* The handle of the frame that currently owns the system caret. */
130 HWND w32_system_caret_hwnd;
131 int w32_system_caret_height;
132 int w32_system_caret_x;
133 int w32_system_caret_y;
134 int w32_use_visible_system_caret;
136 /* Flag to enable Unicode output in case users wish to use programs
137 like Twinbridge on '95 rather than installed system level support
138 for Far East languages. */
139 int w32_enable_unicode_output;
141 DWORD dwWindowsThreadId = 0;
142 HANDLE hWindowsThread = NULL;
143 DWORD dwMainThreadId = 0;
144 HANDLE hMainThread = NULL;
146 #ifndef SIF_ALL
147 /* These definitions are new with Windows 95. */
148 #define SIF_RANGE 0x0001
149 #define SIF_PAGE 0x0002
150 #define SIF_POS 0x0004
151 #define SIF_DISABLENOSCROLL 0x0008
152 #define SIF_TRACKPOS 0x0010
153 #define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
155 typedef struct tagSCROLLINFO
157 UINT cbSize;
158 UINT fMask;
159 int nMin;
160 int nMax;
161 UINT nPage;
162 int nPos;
163 int nTrackPos;
164 } SCROLLINFO, FAR *LPSCROLLINFO;
165 typedef SCROLLINFO CONST FAR *LPCSCROLLINFO;
166 #endif /* SIF_ALL */
168 /* Dynamic linking to new proportional scroll bar functions. */
169 int (PASCAL *pfnSetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi, BOOL fRedraw);
170 BOOL (PASCAL *pfnGetScrollInfo) (HWND hwnd, int fnBar, LPSCROLLINFO lpsi);
172 int vertical_scroll_bar_min_handle;
173 int vertical_scroll_bar_top_border;
174 int vertical_scroll_bar_bottom_border;
176 int last_scroll_bar_drag_pos;
178 /* Mouse movement. */
180 /* Where the mouse was last time we reported a mouse event. */
182 static RECT last_mouse_glyph;
183 static Lisp_Object last_mouse_press_frame;
185 int w32_num_mouse_buttons;
187 Lisp_Object Vw32_swap_mouse_buttons;
189 /* Control whether x_raise_frame also sets input focus. */
190 Lisp_Object Vw32_grab_focus_on_raise;
192 /* Control whether Caps Lock affects non-ascii characters. */
193 Lisp_Object Vw32_capslock_is_shiftlock;
195 /* Control whether right-alt and left-ctrl should be recognized as AltGr. */
196 Lisp_Object Vw32_recognize_altgr;
198 /* The scroll bar in which the last motion event occurred.
200 If the last motion event occurred in a scroll bar, we set this
201 so w32_mouse_position can know whether to report a scroll bar motion or
202 an ordinary motion.
204 If the last motion event didn't occur in a scroll bar, we set this
205 to Qnil, to tell w32_mouse_position to return an ordinary motion event. */
206 static Lisp_Object last_mouse_scroll_bar;
207 static int last_mouse_scroll_bar_pos;
209 /* This is a hack. We would really prefer that w32_mouse_position would
210 return the time associated with the position it returns, but there
211 doesn't seem to be any way to wrest the time-stamp from the server
212 along with the position query. So, we just keep track of the time
213 of the last movement we received, and return that in hopes that
214 it's somewhat accurate. */
216 static Time last_mouse_movement_time;
218 /* Incremented by w32_read_socket whenever it really tries to read
219 events. */
221 #ifdef __STDC__
222 static int volatile input_signal_count;
223 #else
224 static int input_signal_count;
225 #endif
227 extern Lisp_Object Vcommand_line_args, Vsystem_name;
229 #ifndef USE_CRT_DLL
230 extern int errno;
231 #endif
233 /* A mask of extra modifier bits to put into every keyboard char. */
235 extern EMACS_INT extra_keyboard_modifiers;
237 static void x_update_window_end P_ ((struct window *, int, int));
238 void w32_delete_display P_ ((struct w32_display_info *));
239 static void w32_handle_tool_bar_click P_ ((struct frame *,
240 struct input_event *));
241 void w32_define_cursor P_ ((Window, Cursor));
243 void x_lower_frame P_ ((struct frame *));
244 void x_scroll_bar_clear P_ ((struct frame *));
245 void x_wm_set_size_hint P_ ((struct frame *, long, int));
246 void x_raise_frame P_ ((struct frame *));
247 void x_set_window_size P_ ((struct frame *, int, int, int));
248 void x_wm_set_window_state P_ ((struct frame *, int));
249 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
250 void w32_initialize P_ ((void));
251 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
252 int x_compute_min_glyph_bounds P_ ((struct frame *));
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_ ((void));
256 static void w32_reset_terminal_modes P_ ((void));
257 static void x_clear_frame P_ ((void));
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));
273 static Lisp_Object Qvendor_specific_keysyms;
276 /***********************************************************************
277 Debugging
278 ***********************************************************************/
280 #if 0
282 /* This is a function useful for recording debugging information about
283 the sequence of occurrences in this file. */
285 struct record
287 char *locus;
288 int type;
291 struct record event_record[100];
293 int event_record_index;
295 record_event (locus, type)
296 char *locus;
297 int type;
299 if (event_record_index == sizeof (event_record) / sizeof (struct record))
300 event_record_index = 0;
302 event_record[event_record_index].locus = locus;
303 event_record[event_record_index].type = type;
304 event_record_index++;
307 #endif /* 0 */
310 void
311 XChangeGC (void * ignore, XGCValues* gc, unsigned long mask,
312 XGCValues *xgcv)
314 if (mask & GCForeground)
315 gc->foreground = xgcv->foreground;
316 if (mask & GCBackground)
317 gc->background = xgcv->background;
318 if (mask & GCFont)
319 gc->font = xgcv->font;
322 XGCValues *XCreateGC (void * ignore, Window window, unsigned long mask,
323 XGCValues *xgcv)
325 XGCValues *gc = (XGCValues *) xmalloc (sizeof (XGCValues));
326 bzero (gc, sizeof (XGCValues));
328 XChangeGC (ignore, gc, mask, xgcv);
330 return gc;
333 void
334 XGetGCValues (void* ignore, XGCValues *gc,
335 unsigned long mask, XGCValues *xgcv)
337 XChangeGC (ignore, xgcv, mask, gc);
340 static void
341 w32_set_clip_rectangle (HDC hdc, RECT *rect)
343 if (rect)
345 HRGN clip_region = CreateRectRgnIndirect (rect);
346 SelectClipRgn (hdc, clip_region);
347 DeleteObject (clip_region);
349 else
350 SelectClipRgn (hdc, NULL);
354 /* Draw a hollow rectangle at the specified position. */
355 void
356 w32_draw_rectangle (HDC hdc, XGCValues *gc, int x, int y,
357 int width, int height)
359 HBRUSH hb, oldhb;
360 HPEN hp, oldhp;
362 hb = CreateSolidBrush (gc->background);
363 hp = CreatePen (PS_SOLID, 0, gc->foreground);
364 oldhb = SelectObject (hdc, hb);
365 oldhp = SelectObject (hdc, hp);
367 Rectangle (hdc, x, y, x + width, y + height);
369 SelectObject (hdc, oldhb);
370 SelectObject (hdc, oldhp);
371 DeleteObject (hb);
372 DeleteObject (hp);
375 /* Draw a filled rectangle at the specified position. */
376 void
377 w32_fill_rect (f, hdc, pix, lprect)
378 FRAME_PTR f;
379 HDC hdc;
380 COLORREF pix;
381 RECT * lprect;
383 HBRUSH hb;
385 hb = CreateSolidBrush (pix);
386 FillRect (hdc, lprect, hb);
387 DeleteObject (hb);
390 void
391 w32_clear_window (f)
392 FRAME_PTR f;
394 RECT rect;
395 HDC hdc = get_frame_dc (f);
397 /* Under certain conditions, this can be called at startup with
398 a console frame pointer before the GUI frame is created. An HDC
399 of 0 indicates this. */
400 if (hdc)
402 GetClientRect (FRAME_W32_WINDOW (f), &rect);
403 w32_clear_rect (f, hdc, &rect);
406 release_frame_dc (f, hdc);
410 /***********************************************************************
411 Starting and ending an update
412 ***********************************************************************/
414 /* Start an update of frame F. This function is installed as a hook
415 for update_begin, i.e. it is called when update_begin is called.
416 This function is called prior to calls to x_update_window_begin for
417 each window being updated. */
419 static void
420 x_update_begin (f)
421 struct frame *f;
423 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
425 if (! FRAME_W32_P (f))
426 return;
428 /* Regenerate display palette before drawing if list of requested
429 colors has changed. */
430 if (display_info->regen_palette)
432 w32_regenerate_palette (f);
433 display_info->regen_palette = FALSE;
438 /* Start update of window W. Set the global variable updated_window
439 to the window being updated and set output_cursor to the cursor
440 position of W. */
442 static void
443 x_update_window_begin (w)
444 struct window *w;
446 struct frame *f = XFRAME (WINDOW_FRAME (w));
447 struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
449 /* Hide the system caret during an update. */
450 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
452 SendMessage (w32_system_caret_hwnd, WM_EMACS_HIDE_CARET, 0, 0);
455 updated_window = w;
456 set_output_cursor (&w->cursor);
458 BLOCK_INPUT;
460 if (f == display_info->mouse_face_mouse_frame)
462 /* Don't do highlighting for mouse motion during the update. */
463 display_info->mouse_face_defer = 1;
465 /* If F needs to be redrawn, simply forget about any prior mouse
466 highlighting. */
467 if (FRAME_GARBAGED_P (f))
468 display_info->mouse_face_window = Qnil;
470 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
471 their mouse_face_p flag set, which means that they are always
472 unequal to rows in a desired matrix which never have that
473 flag set. So, rows containing mouse-face glyphs are never
474 scrolled, and we don't have to switch the mouse highlight off
475 here to prevent it from being scrolled. */
477 /* Can we tell that this update does not affect the window
478 where the mouse highlight is? If so, no need to turn off.
479 Likewise, don't do anything if the frame is garbaged;
480 in that case, the frame's current matrix that we would use
481 is all wrong, and we will redisplay that line anyway. */
482 if (!NILP (display_info->mouse_face_window)
483 && w == XWINDOW (display_info->mouse_face_window))
485 int i;
487 for (i = 0; i < w->desired_matrix->nrows; ++i)
488 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
489 break;
491 if (i < w->desired_matrix->nrows)
492 clear_mouse_face (display_info);
494 #endif /* 0 */
497 UNBLOCK_INPUT;
500 /* Draw a vertical window border from (x,y0) to (x,y1) */
502 static void
503 w32_draw_vertical_window_border (w, x, y0, y1)
504 struct window *w;
505 int x, y0, y1;
507 struct frame *f = XFRAME (WINDOW_FRAME (w));
508 RECT r;
509 HDC hdc;
511 r.left = x;
512 r.right = x + 1;
513 r.top = y0;
514 r.bottom = y1;
516 hdc = get_frame_dc (f);
517 w32_fill_rect (f, hdc, FRAME_FOREGROUND_PIXEL (f), &r);
518 release_frame_dc (f, hdc);
522 /* End update of window W (which is equal to updated_window).
524 Draw vertical borders between horizontally adjacent windows, and
525 display W's cursor if CURSOR_ON_P is non-zero.
527 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
528 glyphs in mouse-face were overwritten. In that case we have to
529 make sure that the mouse-highlight is properly redrawn.
531 W may be a menu bar pseudo-window in case we don't have X toolkit
532 support. Such windows don't have a cursor, so don't display it
533 here. */
535 static void
536 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
537 struct window *w;
538 int cursor_on_p, mouse_face_overwritten_p;
540 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (XFRAME (w->frame));
542 if (!w->pseudo_window_p)
544 BLOCK_INPUT;
546 if (cursor_on_p)
547 display_and_set_cursor (w, 1, output_cursor.hpos,
548 output_cursor.vpos,
549 output_cursor.x, output_cursor.y);
551 if (draw_window_fringes (w, 1))
552 x_draw_vertical_border (w);
554 UNBLOCK_INPUT;
557 /* If a row with mouse-face was overwritten, arrange for
558 XTframe_up_to_date to redisplay the mouse highlight. */
559 if (mouse_face_overwritten_p)
561 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
562 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
563 dpyinfo->mouse_face_window = Qnil;
566 /* Unhide the caret. This won't actually show the cursor, unless it
567 was visible before the corresponding call to HideCaret in
568 x_update_window_begin. */
569 if (w32_use_visible_system_caret && w32_system_caret_hwnd)
571 SendMessage (w32_system_caret_hwnd, WM_EMACS_SHOW_CARET, 0, 0);
574 updated_window = NULL;
578 /* End update of frame F. This function is installed as a hook in
579 update_end. */
581 static void
582 x_update_end (f)
583 struct frame *f;
585 if (! FRAME_W32_P (f))
586 return;
588 /* Mouse highlight may be displayed again. */
589 FRAME_W32_DISPLAY_INFO (f)->mouse_face_defer = 0;
593 /* This function is called from various places in xdisp.c whenever a
594 complete update has been performed. The global variable
595 updated_window is not available here. */
597 static void
598 w32_frame_up_to_date (f)
599 struct frame *f;
601 if (FRAME_W32_P (f))
603 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
605 if (dpyinfo->mouse_face_deferred_gc
606 || f == dpyinfo->mouse_face_mouse_frame)
608 BLOCK_INPUT;
609 if (dpyinfo->mouse_face_mouse_frame)
610 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
611 dpyinfo->mouse_face_mouse_x,
612 dpyinfo->mouse_face_mouse_y);
613 dpyinfo->mouse_face_deferred_gc = 0;
614 UNBLOCK_INPUT;
620 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
621 arrow bitmaps, or clear the fringes if no bitmaps are required
622 before DESIRED_ROW is made current. The window being updated is
623 found in updated_window. This function is called from
624 update_window_line only if it is known that there are differences
625 between bitmaps to be drawn between current row and DESIRED_ROW. */
627 static void
628 x_after_update_window_line (desired_row)
629 struct glyph_row *desired_row;
631 struct window *w = updated_window;
632 struct frame *f;
633 int width, height;
635 xassert (w);
637 if (!desired_row->mode_line_p && !w->pseudo_window_p)
638 desired_row->redraw_fringe_bitmaps_p = 1;
640 /* When a window has disappeared, make sure that no rest of
641 full-width rows stays visible in the internal border. Could
642 check here if updated_window is the leftmost/rightmost window,
643 but I guess it's not worth doing since vertically split windows
644 are almost never used, internal border is rarely set, and the
645 overhead is very small. */
646 if (windows_or_buffers_changed
647 && desired_row->full_width_p
648 && (f = XFRAME (w->frame),
649 width = FRAME_INTERNAL_BORDER_WIDTH (f),
650 width != 0)
651 && (height = desired_row->visible_height,
652 height > 0))
654 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
656 /* Internal border is drawn below the tool bar. */
657 if (WINDOWP (f->tool_bar_window)
658 && w == XWINDOW (f->tool_bar_window))
659 y -= width;
661 BLOCK_INPUT;
663 HDC hdc = get_frame_dc (f);
664 w32_clear_area (f, hdc, 0, y, width, height);
665 w32_clear_area (f, hdc, FRAME_PIXEL_WIDTH (f) - width,
666 y, width, height);
667 release_frame_dc (f, hdc);
669 UNBLOCK_INPUT;
674 /* Draw the bitmap WHICH in one of the left or right fringes of
675 window W. ROW is the glyph row for which to display the bitmap; it
676 determines the vertical position at which the bitmap has to be
677 drawn. */
679 static void
680 w32_draw_fringe_bitmap (w, row, p)
681 struct window *w;
682 struct glyph_row *row;
683 struct draw_fringe_bitmap_params *p;
685 struct frame *f = XFRAME (WINDOW_FRAME (w));
686 HDC hdc;
687 struct face *face = p->face;
688 int rowY;
690 hdc = get_frame_dc (f);
692 /* Must clip because of partially visible lines. */
693 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
694 if (p->y < rowY)
696 /* Adjust position of "bottom aligned" bitmap on partially
697 visible last row. */
698 int oldY = row->y;
699 int oldVH = row->visible_height;
700 row->visible_height = p->h;
701 row->y -= rowY - p->y;
702 w32_clip_to_row (w, row, -1, hdc);
703 row->y = oldY;
704 row->visible_height = oldVH;
706 else
707 w32_clip_to_row (w, row, -1, hdc);
709 if (p->bx >= 0 && !p->overlay_p)
711 w32_fill_area (f, hdc, face->background,
712 p->bx, p->by, p->nx, p->ny);
715 if (p->which && p->which < max_fringe_bmp)
717 HBITMAP pixmap = fringe_bmp[p->which];
718 HDC compat_hdc;
719 HANDLE horig_obj;
721 compat_hdc = CreateCompatibleDC (hdc);
723 SaveDC (hdc);
725 horig_obj = SelectObject (compat_hdc, pixmap);
727 /* Paint overlays transparently. */
728 if (p->overlay_p)
730 HBRUSH h_brush, h_orig_brush;
732 SetTextColor (hdc, BLACK_PIX_DEFAULT (f));
733 SetBkColor (hdc, WHITE_PIX_DEFAULT (f));
734 h_brush = CreateSolidBrush (face->foreground);
735 h_orig_brush = SelectObject (hdc, h_brush);
737 BitBlt (hdc, p->x, p->y, p->wd, p->h,
738 compat_hdc, 0, p->dh,
739 DSTINVERT);
740 BitBlt (hdc, p->x, p->y, p->wd, p->h,
741 compat_hdc, 0, p->dh,
742 0x2E064A);
743 BitBlt (hdc, p->x, p->y, p->wd, p->h,
744 compat_hdc, 0, p->dh,
745 DSTINVERT);
747 SelectObject (hdc, h_orig_brush);
748 DeleteObject (h_brush);
750 else
752 SetTextColor (hdc, face->background);
753 SetBkColor (hdc, (p->cursor_p
754 ? f->output_data.w32->cursor_pixel
755 : face->foreground));
757 BitBlt (hdc, p->x, p->y, p->wd, p->h,
758 compat_hdc, 0, p->dh,
759 SRCCOPY);
762 SelectObject (compat_hdc, horig_obj);
763 DeleteDC (compat_hdc);
764 RestoreDC (hdc, -1);
767 w32_set_clip_rectangle (hdc, NULL);
769 release_frame_dc (f, hdc);
772 static void
773 w32_define_fringe_bitmap (which, bits, h, wd)
774 int which;
775 unsigned short *bits;
776 int h, wd;
778 if (which >= max_fringe_bmp)
780 int i = max_fringe_bmp;
781 max_fringe_bmp = which + 20;
782 fringe_bmp = (HBITMAP *) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (HBITMAP));
783 while (i < max_fringe_bmp)
784 fringe_bmp[i++] = 0;
787 fringe_bmp[which] = CreateBitmap (wd, h, 1, 1, bits);
790 static void
791 w32_destroy_fringe_bitmap (which)
792 int which;
794 if (which >= max_fringe_bmp)
795 return;
797 if (fringe_bmp[which])
798 DeleteObject (fringe_bmp[which]);
799 fringe_bmp[which] = 0;
804 /* This is called when starting Emacs and when restarting after
805 suspend. When starting Emacs, no window is mapped. And nothing
806 must be done to Emacs's own window if it is suspended (though that
807 rarely happens). */
809 static void
810 w32_set_terminal_modes (void)
814 /* This is called when exiting or suspending Emacs. Exiting will make
815 the W32 windows go away, and suspending requires no action. */
817 static void
818 w32_reset_terminal_modes (void)
824 /***********************************************************************
825 Display Iterator
826 ***********************************************************************/
828 /* Function prototypes of this page. */
830 XCharStruct *w32_per_char_metric P_ ((XFontStruct *, wchar_t *, int));
831 static int w32_encode_char P_ ((int, wchar_t *, struct font_info *, int *));
834 /* Get metrics of character CHAR2B in FONT. Value is always non-null.
835 If CHAR2B is not contained in FONT, the font's default character
836 metric is returned. */
838 static int
839 w32_bdf_per_char_metric (font, char2b, dim, pcm)
840 XFontStruct *font;
841 wchar_t *char2b;
842 int dim;
843 XCharStruct * pcm;
845 glyph_metric * bdf_metric;
846 char buf[2];
848 if (dim == 1)
849 buf[0] = (char)(*char2b);
850 else
852 buf[0] = XCHAR2B_BYTE1 (char2b);
853 buf[1] = XCHAR2B_BYTE2 (char2b);
856 bdf_metric = w32_BDF_TextMetric (font->bdf, buf, dim);
858 if (bdf_metric)
860 pcm->width = bdf_metric->dwidth;
861 pcm->lbearing = bdf_metric->bbox;
862 pcm->rbearing = bdf_metric->dwidth
863 - (bdf_metric->bbox + bdf_metric->bbw);
864 pcm->ascent = bdf_metric->bboy + bdf_metric->bbh;
865 pcm->descent = -bdf_metric->bboy;
867 return 1;
869 return 0;
873 static int
874 w32_native_per_char_metric (font, char2b, font_type, pcm)
875 XFontStruct *font;
876 wchar_t *char2b;
877 enum w32_char_font_type font_type;
878 XCharStruct * pcm;
880 HDC hdc = GetDC (NULL);
881 HFONT old_font;
882 BOOL retval = FALSE;
884 xassert (font && char2b);
885 xassert (font->hfont);
886 xassert (font_type == UNICODE_FONT || font_type == ANSI_FONT);
888 old_font = SelectObject (hdc, font->hfont);
890 if ((font->tm.tmPitchAndFamily & TMPF_TRUETYPE) != 0)
892 ABC char_widths;
894 if (font_type == UNICODE_FONT)
895 retval = GetCharABCWidthsW (hdc, *char2b, *char2b, &char_widths);
896 else
897 retval = GetCharABCWidthsA (hdc, *char2b, *char2b, &char_widths);
899 if (retval)
901 #if 0
902 /* Disabled until we can find a way to get the right results
903 on all versions of Windows. */
905 /* Don't trust the ABC widths. For synthesized fonts they are
906 wrong, and so is the result of GetCharWidth()! */
907 int real_width;
908 GetCharWidth (hdc, *char2b, *char2b, &real_width);
909 #endif
910 pcm->width = char_widths.abcA + char_widths.abcB + char_widths.abcC;
911 #if 0
912 /* As far as I can tell, this is the best way to determine what
913 ExtTextOut will do with the broken font. */
914 if (pcm->width != real_width)
915 pcm->width = (pcm->width + real_width) / 2;
916 #endif
917 pcm->lbearing = char_widths.abcA;
918 pcm->rbearing = char_widths.abcA + char_widths.abcB;
919 pcm->ascent = FONT_BASE (font);
920 pcm->descent = FONT_DESCENT (font);
924 if (!retval)
926 /* Either font is not a True-type font, or GetCharABCWidthsW
927 failed (it is not supported on Windows 9x for instance), so we
928 can't determine the full info we would like. All is not lost
929 though - we can call GetTextExtentPoint32 to get rbearing and
930 deduce width based on the font's per-string overhang. lbearing
931 is assumed to be zero. */
933 /* TODO: Some Thai characters (and other composites if Windows
934 supports them) do have lbearing, and report their total width
935 as zero. Need some way of handling them when
936 GetCharABCWidthsW fails. */
937 SIZE sz;
939 if (font_type == UNICODE_FONT)
940 retval = GetTextExtentPoint32W (hdc, char2b, 1, &sz);
941 else
942 retval = GetTextExtentPoint32A (hdc, (char*)char2b, 1, &sz);
944 if (retval)
946 pcm->width = sz.cx - font->tm.tmOverhang;
947 pcm->rbearing = sz.cx;
948 pcm->lbearing = 0;
949 pcm->ascent = FONT_BASE (font);
950 pcm->descent = FONT_DESCENT (font);
955 if (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
957 retval = FALSE;
960 SelectObject (hdc, old_font);
961 ReleaseDC (NULL, hdc);
963 return retval;
967 XCharStruct *
968 w32_per_char_metric (font, char2b, font_type)
969 XFontStruct *font;
970 wchar_t *char2b;
971 int /* enum w32_char_font_type */ font_type;
973 /* The result metric information. */
974 XCharStruct *pcm;
975 BOOL retval;
977 xassert (font && char2b);
979 /* TODO: This function is currently called through the RIF, and in
980 some cases font_type is UNKNOWN_FONT. We currently allow the
981 cached metrics to be used, which seems to work, but in cases
982 where font_type is UNKNOWN_FONT, we probably haven't encoded
983 char2b appropriately. All callers need checking to see what they
984 are passing. This is most likely to affect variable width fonts
985 outside the Latin-1 range, particularly in languages like Thai
986 that rely on rbearing and lbearing to provide composition. I
987 don't think that is working currently anyway, but we don't seem
988 to have anyone testing such languages on Windows. */
990 /* Handle the common cases quickly. */
991 if (!font->bdf && font->per_char == NULL)
992 /* TODO: determine whether char2b exists in font? */
993 return &font->max_bounds;
994 else if (!font->bdf && *char2b < 128)
995 return &font->per_char[*char2b];
997 xassert (font_type != UNKNOWN_FONT);
999 pcm = &font->scratch;
1001 if (font_type == BDF_1D_FONT)
1002 retval = w32_bdf_per_char_metric (font, char2b, 1, pcm);
1003 else if (font_type == BDF_2D_FONT)
1004 retval = w32_bdf_per_char_metric (font, char2b, 2, pcm);
1005 else
1006 retval = w32_native_per_char_metric (font, char2b, font_type, pcm);
1008 if (retval)
1009 return pcm;
1011 return NULL;
1014 void
1015 w32_cache_char_metrics (font)
1016 XFontStruct *font;
1018 wchar_t char2b = L'x';
1020 /* Cache char metrics for the common cases. */
1021 if (font->bdf)
1023 /* TODO: determine whether font is fixed-pitch. */
1024 if (!w32_bdf_per_char_metric (font, &char2b, 1, &font->max_bounds))
1026 /* Use the font width and height as max bounds, as not all BDF
1027 fonts contain the letter 'x'. */
1028 font->max_bounds.width = FONT_MAX_WIDTH (font);
1029 font->max_bounds.lbearing = -font->bdf->llx;
1030 font->max_bounds.rbearing = FONT_MAX_WIDTH (font) - font->bdf->urx;
1031 font->max_bounds.ascent = FONT_BASE (font);
1032 font->max_bounds.descent = FONT_DESCENT (font);
1035 else
1037 if (((font->tm.tmPitchAndFamily & TMPF_FIXED_PITCH) != 0)
1038 /* Some fonts (eg DBCS fonts) are marked as fixed width even
1039 though they contain characters of different widths. */
1040 || (font->tm.tmMaxCharWidth != font->tm.tmAveCharWidth))
1042 /* Font is not fixed pitch, so cache per_char info for the
1043 ASCII characters. It would be much more work, and probably
1044 not worth it, to cache other chars, since we may change
1045 between using Unicode and ANSI text drawing functions at
1046 run-time. */
1047 int i;
1049 font->per_char = xmalloc (128 * sizeof(XCharStruct));
1050 for (i = 0; i < 128; i++)
1052 char2b = i;
1053 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1054 &font->per_char[i]);
1057 else
1058 w32_native_per_char_metric (font, &char2b, ANSI_FONT,
1059 &font->max_bounds);
1064 /* Determine if a font is double byte. */
1065 int w32_font_is_double_byte (XFontStruct *font)
1067 return font->double_byte_p;
1071 static BOOL
1072 w32_use_unicode_for_codepage (codepage)
1073 int codepage;
1075 /* If the current codepage is supported, use Unicode for output. */
1076 return (w32_enable_unicode_output
1077 && codepage != CP_8BIT
1078 && (codepage == CP_UNICODE || IsValidCodePage (codepage)));
1081 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1082 the two-byte form of C. Encoding is returned in *CHAR2B. */
1084 static int /* enum w32_char_font_type */
1085 w32_encode_char (c, char2b, font_info, two_byte_p)
1086 int c;
1087 wchar_t *char2b;
1088 struct font_info *font_info;
1089 int * two_byte_p;
1091 int charset = CHAR_CHARSET (c);
1092 int codepage;
1093 int unicode_p = 0;
1094 int internal_two_byte_p = 0;
1096 XFontStruct *font = font_info->font;
1098 internal_two_byte_p = w32_font_is_double_byte (font);
1100 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1101 This may be either a program in a special encoder language or a
1102 fixed encoding. */
1103 if (font_info->font_encoder)
1105 /* It's a program. */
1106 struct ccl_program *ccl = font_info->font_encoder;
1108 if (CHARSET_DIMENSION (charset) == 1)
1110 ccl->reg[0] = charset;
1111 ccl->reg[1] = XCHAR2B_BYTE2 (char2b);
1112 ccl->reg[2] = -1;
1114 else
1116 ccl->reg[0] = charset;
1117 ccl->reg[1] = XCHAR2B_BYTE1 (char2b);
1118 ccl->reg[2] = XCHAR2B_BYTE2 (char2b);
1121 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1123 /* We assume that MSBs are appropriately set/reset by CCL
1124 program. */
1125 if (!internal_two_byte_p) /* 1-byte font */
1126 STORE_XCHAR2B (char2b, 0, ccl->reg[1]);
1127 else
1128 STORE_XCHAR2B (char2b, ccl->reg[1], ccl->reg[2]);
1130 else if (font_info->encoding[charset])
1132 /* Fixed encoding scheme. See fontset.h for the meaning of the
1133 encoding numbers. */
1134 int enc = font_info->encoding[charset];
1136 if ((enc == 1 || enc == 2)
1137 && CHARSET_DIMENSION (charset) == 2)
1138 STORE_XCHAR2B (char2b, XCHAR2B_BYTE1 (char2b) | 0x80, XCHAR2B_BYTE2 (char2b));
1140 if (enc == 1 || enc == 3
1141 || (enc == 4 && CHARSET_DIMENSION (charset) == 1))
1142 STORE_XCHAR2B (char2b, XCHAR2B_BYTE1 (char2b), XCHAR2B_BYTE2 (char2b) | 0x80);
1143 else if (enc == 4)
1145 int sjis1, sjis2;
1147 ENCODE_SJIS (XCHAR2B_BYTE1 (char2b), XCHAR2B_BYTE2 (char2b),
1148 sjis1, sjis2);
1149 STORE_XCHAR2B (char2b, sjis1, sjis2);
1152 codepage = font_info->codepage;
1154 /* If charset is not ASCII or Latin-1, may need to move it into
1155 Unicode space. */
1156 if ( font && !font->bdf && w32_use_unicode_for_codepage (codepage)
1157 && charset != CHARSET_ASCII && charset != charset_latin_iso8859_1
1158 && charset != CHARSET_8_BIT_CONTROL && charset != CHARSET_8_BIT_GRAPHIC)
1160 char temp[3];
1161 temp[0] = XCHAR2B_BYTE1 (char2b);
1162 temp[1] = XCHAR2B_BYTE2 (char2b);
1163 temp[2] = '\0';
1164 if (codepage != CP_UNICODE)
1166 if (temp[0])
1167 MultiByteToWideChar (codepage, 0, temp, 2, char2b, 1);
1168 else
1169 MultiByteToWideChar (codepage, 0, temp+1, 1, char2b, 1);
1171 unicode_p = 1;
1172 internal_two_byte_p = 1;
1175 if (two_byte_p)
1176 *two_byte_p = internal_two_byte_p;
1178 if (!font)
1179 return UNKNOWN_FONT;
1180 else if (font->bdf && CHARSET_DIMENSION (charset) == 1)
1181 return BDF_1D_FONT;
1182 else if (font->bdf)
1183 return BDF_2D_FONT;
1184 else if (unicode_p)
1185 return UNICODE_FONT;
1186 else
1187 return ANSI_FONT;
1192 /***********************************************************************
1193 Glyph display
1194 ***********************************************************************/
1197 /* Encapsulate the different ways of displaying text under W32. */
1199 static void
1200 w32_text_out (s, x, y,chars,nchars)
1201 struct glyph_string * s;
1202 int x, y;
1203 wchar_t * chars;
1204 int nchars;
1206 int charset_dim = w32_font_is_double_byte (s->font) ? 2 : 1;
1207 if (s->font->bdf)
1208 w32_BDF_TextOut (s->font->bdf, s->hdc,
1209 x, y, (char *) chars, charset_dim,
1210 nchars * charset_dim, 0);
1211 else if (s->first_glyph->font_type == UNICODE_FONT)
1212 ExtTextOutW (s->hdc, x, y, 0, NULL, chars, nchars, NULL);
1213 else
1214 ExtTextOutA (s->hdc, x, y, 0, NULL, (char *) chars,
1215 nchars * charset_dim, NULL);
1219 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1220 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1221 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1222 int));
1223 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
1224 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
1225 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1226 static void x_draw_glyph_string P_ ((struct glyph_string *));
1227 static void x_set_cursor_gc P_ ((struct glyph_string *));
1228 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1229 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
1230 static int w32_alloc_lighter_color (struct frame *, COLORREF *, double, int);
1231 static void w32_setup_relief_color P_ ((struct frame *, struct relief *,
1232 double, int, COLORREF));
1233 static void x_setup_relief_colors P_ ((struct glyph_string *));
1234 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1235 static void x_draw_image_relief P_ ((struct glyph_string *));
1236 static void x_draw_image_foreground P_ ((struct glyph_string *));
1237 static void w32_draw_image_foreground_1 P_ ((struct glyph_string *, HBITMAP));
1238 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1239 int, int, int));
1240 static void w32_draw_relief_rect P_ ((struct frame *, int, int, int, int,
1241 int, int, int, int, int, int,
1242 RECT *));
1243 static void w32_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1244 int, int, int, RECT *));
1246 #if GLYPH_DEBUG
1247 static void x_check_font P_ ((struct frame *, XFontStruct *));
1248 #endif
1251 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1252 face. */
1254 static void
1255 x_set_cursor_gc (s)
1256 struct glyph_string *s;
1258 if (s->font == FRAME_FONT (s->f)
1259 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1260 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1261 && !s->cmp)
1262 s->gc = s->f->output_data.w32->cursor_gc;
1263 else
1265 /* Cursor on non-default face: must merge. */
1266 XGCValues xgcv;
1267 unsigned long mask;
1269 xgcv.background = s->f->output_data.w32->cursor_pixel;
1270 xgcv.foreground = s->face->background;
1272 /* If the glyph would be invisible, try a different foreground. */
1273 if (xgcv.foreground == xgcv.background)
1274 xgcv.foreground = s->face->foreground;
1275 if (xgcv.foreground == xgcv.background)
1276 xgcv.foreground = s->f->output_data.w32->cursor_foreground_pixel;
1277 if (xgcv.foreground == xgcv.background)
1278 xgcv.foreground = s->face->foreground;
1280 /* Make sure the cursor is distinct from text in this face. */
1281 if (xgcv.background == s->face->background
1282 && xgcv.foreground == s->face->foreground)
1284 xgcv.background = s->face->foreground;
1285 xgcv.foreground = s->face->background;
1288 IF_DEBUG (x_check_font (s->f, s->font));
1289 xgcv.font = s->font;
1290 mask = GCForeground | GCBackground | GCFont;
1292 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1293 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1294 mask, &xgcv);
1295 else
1296 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1297 = XCreateGC (NULL, s->window, mask, &xgcv);
1299 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1304 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1306 static void
1307 x_set_mouse_face_gc (s)
1308 struct glyph_string *s;
1310 int face_id;
1311 struct face *face;
1313 /* What face has to be used last for the mouse face? */
1314 face_id = FRAME_W32_DISPLAY_INFO (s->f)->mouse_face_face_id;
1315 face = FACE_FROM_ID (s->f, face_id);
1316 if (face == NULL)
1317 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1319 if (s->first_glyph->type == CHAR_GLYPH)
1320 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1321 else
1322 face_id = FACE_FOR_CHAR (s->f, face, 0);
1323 s->face = FACE_FROM_ID (s->f, face_id);
1324 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1326 /* If font in this face is same as S->font, use it. */
1327 if (s->font == s->face->font)
1328 s->gc = s->face->gc;
1329 else
1331 /* Otherwise construct scratch_cursor_gc with values from FACE
1332 but font FONT. */
1333 XGCValues xgcv;
1334 unsigned long mask;
1336 xgcv.background = s->face->background;
1337 xgcv.foreground = s->face->foreground;
1338 IF_DEBUG (x_check_font (s->f, s->font));
1339 xgcv.font = s->font;
1340 mask = GCForeground | GCBackground | GCFont;
1342 if (FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1343 XChangeGC (NULL, FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1344 mask, &xgcv);
1345 else
1346 FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc
1347 = XCreateGC (NULL, s->window, mask, &xgcv);
1349 s->gc = FRAME_W32_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1352 xassert (s->gc != 0);
1356 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1357 Faces to use in the mode line have already been computed when the
1358 matrix was built, so there isn't much to do, here. */
1360 static INLINE void
1361 x_set_mode_line_face_gc (s)
1362 struct glyph_string *s;
1364 s->gc = s->face->gc;
1368 /* Set S->gc of glyph string S for drawing that glyph string. Set
1369 S->stippled_p to a non-zero value if the face of S has a stipple
1370 pattern. */
1372 static INLINE void
1373 x_set_glyph_string_gc (s)
1374 struct glyph_string *s;
1376 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1378 if (s->hl == DRAW_NORMAL_TEXT)
1380 s->gc = s->face->gc;
1381 s->stippled_p = s->face->stipple != 0;
1383 else if (s->hl == DRAW_INVERSE_VIDEO)
1385 x_set_mode_line_face_gc (s);
1386 s->stippled_p = s->face->stipple != 0;
1388 else if (s->hl == DRAW_CURSOR)
1390 x_set_cursor_gc (s);
1391 s->stippled_p = 0;
1393 else if (s->hl == DRAW_MOUSE_FACE)
1395 x_set_mouse_face_gc (s);
1396 s->stippled_p = s->face->stipple != 0;
1398 else if (s->hl == DRAW_IMAGE_RAISED
1399 || s->hl == DRAW_IMAGE_SUNKEN)
1401 s->gc = s->face->gc;
1402 s->stippled_p = s->face->stipple != 0;
1404 else
1406 s->gc = s->face->gc;
1407 s->stippled_p = s->face->stipple != 0;
1410 /* GC must have been set. */
1411 xassert (s->gc != 0);
1415 /* Set clipping for output of glyph string S. S may be part of a mode
1416 line or menu if we don't have X toolkit support. */
1418 static INLINE void
1419 x_set_glyph_string_clipping (s)
1420 struct glyph_string *s;
1422 RECT r;
1423 get_glyph_string_clip_rect (s, &r);
1424 w32_set_clip_rectangle (s->hdc, &r);
1428 /* RIF:
1429 Compute left and right overhang of glyph string S. If S is a glyph
1430 string for a composition, assume overhangs don't exist. */
1432 static void
1433 w32_compute_glyph_string_overhangs (s)
1434 struct glyph_string *s;
1436 /* TODO: Windows does not appear to have a method for
1437 getting this info without getting the ABC widths for each
1438 individual character and working it out manually. */
1442 static void
1443 w32_get_glyph_overhangs (glyph, f, left, right)
1444 struct glyph *glyph;
1445 struct frame *f;
1446 int *left, *right;
1448 HDC hdc = get_frame_dc (f);
1449 /* Convert to unicode! */
1450 x_get_glyph_overhangs (glyph, f, left, right);
1451 release_frame_dc (f, hdc);
1455 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1457 static INLINE void
1458 x_clear_glyph_string_rect (s, x, y, w, h)
1459 struct glyph_string *s;
1460 int x, y, w, h;
1462 int real_x = x;
1463 int real_y = y;
1464 int real_w = w;
1465 int real_h = h;
1466 #if 0
1467 /* Take clipping into account. */
1468 if (s->gc->clip_mask == Rect)
1470 real_x = max (real_x, s->gc->clip_rectangle.left);
1471 real_y = max (real_y, s->gc->clip_rectangle.top);
1472 real_w = min (real_w, s->gc->clip_rectangle.right
1473 - s->gc->clip_rectangle.left);
1474 real_h = min (real_h, s->gc->clip_rectangle.bottom
1475 - s->gc->clip_rectangle.top);
1477 #endif
1478 w32_fill_area (s->f, s->hdc, s->gc->background, real_x, real_y,
1479 real_w, real_h);
1483 /* Draw the background of glyph_string S. If S->background_filled_p
1484 is non-zero don't draw it. FORCE_P non-zero means draw the
1485 background even if it wouldn't be drawn normally. This is used
1486 when a string preceding S draws into the background of S, or S
1487 contains the first component of a composition. */
1489 static void
1490 x_draw_glyph_string_background (s, force_p)
1491 struct glyph_string *s;
1492 int force_p;
1494 /* Nothing to do if background has already been drawn or if it
1495 shouldn't be drawn in the first place. */
1496 if (!s->background_filled_p)
1498 int box_line_width = max (s->face->box_line_width, 0);
1500 #if 0 /* TODO: stipple */
1501 if (s->stippled_p)
1503 /* Fill background with a stipple pattern. */
1504 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1505 XFillRectangle (s->display, s->window, s->gc, s->x,
1506 s->y + box_line_width,
1507 s->background_width,
1508 s->height - 2 * box_line_width);
1509 XSetFillStyle (s->display, s->gc, FillSolid);
1510 s->background_filled_p = 1;
1512 else
1513 #endif
1514 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1515 || s->font_not_found_p
1516 || s->extends_to_end_of_line_p
1517 || s->font->bdf
1518 || force_p)
1520 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1521 s->background_width,
1522 s->height - 2 * box_line_width);
1523 s->background_filled_p = 1;
1529 /* Draw the foreground of glyph string S. */
1531 static void
1532 x_draw_glyph_string_foreground (s)
1533 struct glyph_string *s;
1535 int i, x;
1536 HFONT old_font;
1538 /* If first glyph of S has a left box line, start drawing the text
1539 of S to the right of that box line. */
1540 if (s->face->box != FACE_NO_BOX
1541 && s->first_glyph->left_box_line_p)
1542 x = s->x + abs (s->face->box_line_width);
1543 else
1544 x = s->x;
1546 if (s->for_overlaps_p || (s->background_filled_p && s->hl != DRAW_CURSOR))
1547 SetBkMode (s->hdc, TRANSPARENT);
1548 else
1549 SetBkMode (s->hdc, OPAQUE);
1551 SetTextColor (s->hdc, s->gc->foreground);
1552 SetBkColor (s->hdc, s->gc->background);
1553 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1555 if (s->font && s->font->hfont)
1556 old_font = SelectObject (s->hdc, s->font->hfont);
1558 /* Draw characters of S as rectangles if S's font could not be
1559 loaded. */
1560 if (s->font_not_found_p)
1562 for (i = 0; i < s->nchars; ++i)
1564 struct glyph *g = s->first_glyph + i;
1566 w32_draw_rectangle (s->hdc, s->gc, x, s->y, g->pixel_width - 1,
1567 s->height - 1);
1568 x += g->pixel_width;
1571 else
1573 char *char1b = (char *) s->char2b;
1574 int boff = s->font_info->baseline_offset;
1576 if (s->font_info->vertical_centering)
1577 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1579 /* If we can use 8-bit functions, condense S->char2b. */
1580 if (!s->two_byte_p)
1581 for (i = 0; i < s->nchars; ++i)
1582 char1b[i] = XCHAR2B_BYTE2 (&s->char2b[i]);
1584 /* Draw text with TextOut and friends. */
1585 w32_text_out (s, x, s->ybase - boff, s->char2b, s->nchars);
1587 if (s->face->overstrike)
1589 /* For overstriking (to simulate bold-face), draw the
1590 characters again shifted to the right by one pixel. */
1591 w32_text_out (s, x + 1, s->ybase - boff, s->char2b, s->nchars);
1594 if (s->font && s->font->hfont)
1595 SelectObject (s->hdc, old_font);
1598 /* Draw the foreground of composite glyph string S. */
1600 static void
1601 x_draw_composite_glyph_string_foreground (s)
1602 struct glyph_string *s;
1604 int i, x;
1605 HFONT old_font;
1607 /* If first glyph of S has a left box line, start drawing the text
1608 of S to the right of that box line. */
1609 if (s->face->box != FACE_NO_BOX
1610 && s->first_glyph->left_box_line_p)
1611 x = s->x + abs (s->face->box_line_width);
1612 else
1613 x = s->x;
1615 /* S is a glyph string for a composition. S->gidx is the index of
1616 the first character drawn for glyphs of this composition.
1617 S->gidx == 0 means we are drawing the very first character of
1618 this composition. */
1620 SetTextColor (s->hdc, s->gc->foreground);
1621 SetBkColor (s->hdc, s->gc->background);
1622 SetBkMode (s->hdc, TRANSPARENT);
1623 SetTextAlign (s->hdc, TA_BASELINE | TA_LEFT);
1625 if (s->font && s->font->hfont)
1626 old_font = SelectObject (s->hdc, s->font->hfont);
1628 /* Draw a rectangle for the composition if the font for the very
1629 first character of the composition could not be loaded. */
1630 if (s->font_not_found_p)
1632 if (s->gidx == 0)
1633 w32_draw_rectangle (s->hdc, s->gc, x, s->y, s->width - 1,
1634 s->height - 1);
1636 else
1638 for (i = 0; i < s->nchars; i++, ++s->gidx)
1640 w32_text_out (s, x + s->cmp->offsets[s->gidx * 2],
1641 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1642 s->char2b + i, 1);
1643 if (s->face->overstrike)
1644 w32_text_out (s, x + s->cmp->offsets[s->gidx * 2] + 1,
1645 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1646 s->char2b + i, 1);
1650 if (s->font && s->font->hfont)
1651 SelectObject (s->hdc, old_font);
1655 /* Brightness beyond which a color won't have its highlight brightness
1656 boosted.
1658 Nominally, highlight colors for `3d' faces are calculated by
1659 brightening an object's color by a constant scale factor, but this
1660 doesn't yield good results for dark colors, so for colors who's
1661 brightness is less than this value (on a scale of 0-255) have to
1662 use an additional additive factor.
1664 The value here is set so that the default menu-bar/mode-line color
1665 (grey75) will not have its highlights changed at all. */
1666 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 187
1669 /* Allocate a color which is lighter or darker than *COLOR by FACTOR
1670 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1671 If this produces the same color as COLOR, try a color where all RGB
1672 values have DELTA added. Return the allocated color in *COLOR.
1673 DISPLAY is the X display, CMAP is the colormap to operate on.
1674 Value is non-zero if successful. */
1676 static int
1677 w32_alloc_lighter_color (f, color, factor, delta)
1678 struct frame *f;
1679 COLORREF *color;
1680 double factor;
1681 int delta;
1683 COLORREF new;
1684 long bright;
1686 /* On Windows, RGB values are 0-255, not 0-65535, so scale delta. */
1687 delta /= 256;
1689 /* Change RGB values by specified FACTOR. Avoid overflow! */
1690 xassert (factor >= 0);
1691 new = PALETTERGB (min (0xff, factor * GetRValue (*color)),
1692 min (0xff, factor * GetGValue (*color)),
1693 min (0xff, factor * GetBValue (*color)));
1695 /* Calculate brightness of COLOR. */
1696 bright = (2 * GetRValue (*color) + 3 * GetGValue (*color)
1697 + GetBValue (*color)) / 6;
1699 /* We only boost colors that are darker than
1700 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1701 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1702 /* Make an additive adjustment to NEW, because it's dark enough so
1703 that scaling by FACTOR alone isn't enough. */
1705 /* How far below the limit this color is (0 - 1, 1 being darker). */
1706 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1707 /* The additive adjustment. */
1708 int min_delta = delta * dimness * factor / 2;
1710 if (factor < 1)
1711 new = PALETTERGB (max (0, min (0xff, min_delta - GetRValue (*color))),
1712 max (0, min (0xff, min_delta - GetGValue (*color))),
1713 max (0, min (0xff, min_delta - GetBValue (*color))));
1714 else
1715 new = PALETTERGB (max (0, min (0xff, min_delta + GetRValue (*color))),
1716 max (0, min (0xff, min_delta + GetGValue (*color))),
1717 max (0, min (0xff, min_delta + GetBValue (*color))));
1720 if (new == *color)
1721 new = PALETTERGB (max (0, min (0xff, delta + GetRValue (*color))),
1722 max (0, min (0xff, delta + GetGValue (*color))),
1723 max (0, min (0xff, delta + GetBValue (*color))));
1725 /* TODO: Map to palette and retry with delta if same? */
1726 /* TODO: Free colors (if using palette)? */
1728 if (new == *color)
1729 return 0;
1731 *color = new;
1733 return 1;
1737 /* Set up the foreground color for drawing relief lines of glyph
1738 string S. RELIEF is a pointer to a struct relief containing the GC
1739 with which lines will be drawn. Use a color that is FACTOR or
1740 DELTA lighter or darker than the relief's background which is found
1741 in S->f->output_data.x->relief_background. If such a color cannot
1742 be allocated, use DEFAULT_PIXEL, instead. */
1744 static void
1745 w32_setup_relief_color (f, relief, factor, delta, default_pixel)
1746 struct frame *f;
1747 struct relief *relief;
1748 double factor;
1749 int delta;
1750 COLORREF default_pixel;
1752 XGCValues xgcv;
1753 struct w32_output *di = f->output_data.w32;
1754 unsigned long mask = GCForeground;
1755 COLORREF pixel;
1756 COLORREF background = di->relief_background;
1757 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
1759 /* TODO: Free colors (if using palette)? */
1761 /* Allocate new color. */
1762 xgcv.foreground = default_pixel;
1763 pixel = background;
1764 if (w32_alloc_lighter_color (f, &pixel, factor, delta))
1766 relief->allocated_p = 1;
1767 xgcv.foreground = relief->pixel = pixel;
1770 if (relief->gc == 0)
1772 #if 0 /* TODO: stipple */
1773 xgcv.stipple = dpyinfo->gray;
1774 mask |= GCStipple;
1775 #endif
1776 relief->gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, &xgcv);
1778 else
1779 XChangeGC (NULL, relief->gc, mask, &xgcv);
1783 /* Set up colors for the relief lines around glyph string S. */
1785 static void
1786 x_setup_relief_colors (s)
1787 struct glyph_string *s;
1789 struct w32_output *di = s->f->output_data.w32;
1790 COLORREF color;
1792 if (s->face->use_box_color_for_shadows_p)
1793 color = s->face->box_color;
1794 else if (s->first_glyph->type == IMAGE_GLYPH
1795 && s->img->pixmap
1796 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1797 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1798 else
1799 color = s->gc->background;
1801 if (di->white_relief.gc == 0
1802 || color != di->relief_background)
1804 di->relief_background = color;
1805 w32_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1806 WHITE_PIX_DEFAULT (s->f));
1807 w32_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1808 BLACK_PIX_DEFAULT (s->f));
1813 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1814 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1815 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1816 relief. LEFT_P non-zero means draw a relief on the left side of
1817 the rectangle. RIGHT_P non-zero means draw a relief on the right
1818 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1819 when drawing. */
1821 static void
1822 w32_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1823 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
1824 struct frame *f;
1825 int left_x, top_y, right_x, bottom_y, width;
1826 int top_p, bot_p, left_p, right_p, raised_p;
1827 RECT *clip_rect;
1829 int i;
1830 XGCValues gc;
1831 HDC hdc = get_frame_dc (f);
1833 if (raised_p)
1834 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1835 else
1836 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1838 w32_set_clip_rectangle (hdc, clip_rect);
1840 /* Top. */
1841 if (top_p)
1842 for (i = 0; i < width; ++i)
1843 w32_fill_area (f, hdc, gc.foreground,
1844 left_x + i * left_p, top_y + i,
1845 right_x - left_x - i * (left_p + right_p ) + 1, 1);
1847 /* Left. */
1848 if (left_p)
1849 for (i = 0; i < width; ++i)
1850 w32_fill_area (f, hdc, gc.foreground,
1851 left_x + i, top_y + i, 1,
1852 bottom_y - top_y - 2 * i + 1);
1854 if (raised_p)
1855 gc.foreground = f->output_data.w32->black_relief.gc->foreground;
1856 else
1857 gc.foreground = f->output_data.w32->white_relief.gc->foreground;
1859 /* Bottom. */
1860 if (bot_p)
1861 for (i = 0; i < width; ++i)
1862 w32_fill_area (f, hdc, gc.foreground,
1863 left_x + i * left_p, bottom_y - i,
1864 right_x - left_x - i * (left_p + right_p) + 1, 1);
1866 /* Right. */
1867 if (right_p)
1868 for (i = 0; i < width; ++i)
1869 w32_fill_area (f, hdc, gc.foreground,
1870 right_x - i, top_y + i + 1, 1,
1871 bottom_y - top_y - 2 * i - 1);
1873 w32_set_clip_rectangle (hdc, NULL);
1875 release_frame_dc (f, hdc);
1879 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1880 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1881 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1882 left side of the rectangle. RIGHT_P non-zero means draw a line
1883 on the right side of the rectangle. CLIP_RECT is the clipping
1884 rectangle to use when drawing. */
1886 static void
1887 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1888 left_p, right_p, clip_rect)
1889 struct glyph_string *s;
1890 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
1891 RECT *clip_rect;
1893 w32_set_clip_rectangle (s->hdc, clip_rect);
1895 /* Top. */
1896 w32_fill_area (s->f, s->hdc, s->face->box_color,
1897 left_x, top_y, right_x - left_x + 1, width);
1899 /* Left. */
1900 if (left_p)
1902 w32_fill_area (s->f, s->hdc, s->face->box_color,
1903 left_x, top_y, width, bottom_y - top_y + 1);
1906 /* Bottom. */
1907 w32_fill_area (s->f, s->hdc, s->face->box_color,
1908 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1910 /* Right. */
1911 if (right_p)
1913 w32_fill_area (s->f, s->hdc, s->face->box_color,
1914 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1917 w32_set_clip_rectangle (s->hdc, NULL);
1921 /* Draw a box around glyph string S. */
1923 static void
1924 x_draw_glyph_string_box (s)
1925 struct glyph_string *s;
1927 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1928 int left_p, right_p;
1929 struct glyph *last_glyph;
1930 RECT clip_rect;
1932 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1933 ? WINDOW_RIGHT_EDGE_X (s->w)
1934 : window_box_right (s->w, s->area));
1936 /* The glyph that may have a right box line. */
1937 last_glyph = (s->cmp || s->img
1938 ? s->first_glyph
1939 : s->first_glyph + s->nchars - 1);
1941 width = abs (s->face->box_line_width);
1942 raised_p = s->face->box == FACE_RAISED_BOX;
1943 left_x = s->x;
1944 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
1945 ? last_x - 1
1946 : min (last_x, s->x + s->background_width) - 1));
1947 top_y = s->y;
1948 bottom_y = top_y + s->height - 1;
1950 left_p = (s->first_glyph->left_box_line_p
1951 || (s->hl == DRAW_MOUSE_FACE
1952 && (s->prev == NULL
1953 || s->prev->hl != s->hl)));
1954 right_p = (last_glyph->right_box_line_p
1955 || (s->hl == DRAW_MOUSE_FACE
1956 && (s->next == NULL
1957 || s->next->hl != s->hl)));
1959 get_glyph_string_clip_rect (s, &clip_rect);
1961 if (s->face->box == FACE_SIMPLE_BOX)
1962 w32_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1963 left_p, right_p, &clip_rect);
1964 else
1966 x_setup_relief_colors (s);
1967 w32_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1968 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
1973 /* Draw foreground of image glyph string S. */
1975 static void
1976 x_draw_image_foreground (s)
1977 struct glyph_string *s;
1979 int x = s->x;
1980 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
1982 /* If first glyph of S has a left box line, start drawing it to the
1983 right of that line. */
1984 if (s->face->box != FACE_NO_BOX
1985 && s->first_glyph->left_box_line_p
1986 && s->slice.x == 0)
1987 x += abs (s->face->box_line_width);
1989 /* If there is a margin around the image, adjust x- and y-position
1990 by that margin. */
1991 if (s->slice.x == 0)
1992 x += s->img->hmargin;
1993 if (s->slice.y == 0)
1994 y += s->img->vmargin;
1996 SaveDC (s->hdc);
1998 if (s->img->pixmap)
2000 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2001 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2002 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2003 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
2004 SetBkColor (compat_hdc, RGB (255, 255, 255));
2005 SetTextColor (s->hdc, RGB (0, 0, 0));
2006 x_set_glyph_string_clipping (s);
2008 if (s->img->mask)
2010 HDC mask_dc = CreateCompatibleDC (s->hdc);
2011 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
2013 SetTextColor (s->hdc, RGB (255, 255, 255));
2014 SetBkColor (s->hdc, RGB (0, 0, 0));
2016 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
2017 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2018 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
2019 mask_dc, s->slice.x, s->slice.y, SRCAND);
2020 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
2021 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2023 SelectObject (mask_dc, mask_orig_obj);
2024 DeleteDC (mask_dc);
2026 else
2028 SetTextColor (s->hdc, s->gc->foreground);
2029 SetBkColor (s->hdc, s->gc->background);
2031 BitBlt (s->hdc, x, y, s->slice.width, s->slice.height,
2032 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2034 /* When the image has a mask, we can expect that at
2035 least part of a mouse highlight or a block cursor will
2036 be visible. If the image doesn't have a mask, make
2037 a block cursor visible by drawing a rectangle around
2038 the image. I believe it's looking better if we do
2039 nothing here for mouse-face. */
2040 if (s->hl == DRAW_CURSOR)
2042 int r = s->img->relief;
2043 if (r < 0) r = -r;
2044 w32_draw_rectangle (s->hdc, s->gc, x - r, y - r ,
2045 s->slice.width + r*2 - 1,
2046 s->slice.height + r*2 - 1);
2050 w32_set_clip_rectangle (s->hdc, NULL);
2051 SelectObject (s->hdc, orig_brush);
2052 DeleteObject (fg_brush);
2053 SelectObject (compat_hdc, orig_obj);
2054 DeleteDC (compat_hdc);
2056 else
2057 w32_draw_rectangle (s->hdc, s->gc, x, y,
2058 s->slice.width - 1, s->slice.height - 1);
2060 RestoreDC (s->hdc ,-1);
2064 /* Draw a relief around the image glyph string S. */
2066 static void
2067 x_draw_image_relief (s)
2068 struct glyph_string *s;
2070 int x0, y0, x1, y1, thick, raised_p;
2071 RECT r;
2072 int x = s->x;
2073 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2075 /* If first glyph of S has a left box line, start drawing it to the
2076 right of that line. */
2077 if (s->face->box != FACE_NO_BOX
2078 && s->first_glyph->left_box_line_p
2079 && s->slice.x == 0)
2080 x += abs (s->face->box_line_width);
2082 /* If there is a margin around the image, adjust x- and y-position
2083 by that margin. */
2084 if (s->slice.x == 0)
2085 x += s->img->hmargin;
2086 if (s->slice.y == 0)
2087 y += s->img->vmargin;
2089 if (s->hl == DRAW_IMAGE_SUNKEN
2090 || s->hl == DRAW_IMAGE_RAISED)
2092 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2093 raised_p = s->hl == DRAW_IMAGE_RAISED;
2095 else
2097 thick = abs (s->img->relief);
2098 raised_p = s->img->relief > 0;
2101 x0 = x - thick;
2102 y0 = y - thick;
2103 x1 = x + s->slice.width + thick - 1;
2104 y1 = y + s->slice.height + thick - 1;
2106 x_setup_relief_colors (s);
2107 get_glyph_string_clip_rect (s, &r);
2108 w32_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2109 s->slice.y == 0,
2110 s->slice.y + s->slice.height == s->img->height,
2111 s->slice.x == 0,
2112 s->slice.x + s->slice.width == s->img->width,
2113 &r);
2117 /* Draw the foreground of image glyph string S to PIXMAP. */
2119 static void
2120 w32_draw_image_foreground_1 (s, pixmap)
2121 struct glyph_string *s;
2122 HBITMAP pixmap;
2124 HDC hdc = CreateCompatibleDC (s->hdc);
2125 HGDIOBJ orig_hdc_obj = SelectObject (hdc, pixmap);
2126 int x = 0;
2127 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2129 /* If first glyph of S has a left box line, start drawing it to the
2130 right of that line. */
2131 if (s->face->box != FACE_NO_BOX
2132 && s->first_glyph->left_box_line_p
2133 && s->slice.x == 0)
2134 x += abs (s->face->box_line_width);
2136 /* If there is a margin around the image, adjust x- and y-position
2137 by that margin. */
2138 if (s->slice.x == 0)
2139 x += s->img->hmargin;
2140 if (s->slice.y == 0)
2141 y += s->img->vmargin;
2143 if (s->img->pixmap)
2145 HDC compat_hdc = CreateCompatibleDC (hdc);
2146 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2147 HBRUSH orig_brush = SelectObject (hdc, fg_brush);
2148 HGDIOBJ orig_obj = SelectObject (compat_hdc, s->img->pixmap);
2150 if (s->img->mask)
2152 HDC mask_dc = CreateCompatibleDC (hdc);
2153 HGDIOBJ mask_orig_obj = SelectObject (mask_dc, s->img->mask);
2155 SetTextColor (hdc, RGB (0, 0, 0));
2156 SetBkColor (hdc, RGB (255, 255, 255));
2157 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2158 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2159 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2160 mask_dc, s->slice.x, s->slice.y, SRCAND);
2161 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2162 compat_hdc, s->slice.x, s->slice.y, SRCINVERT);
2164 SelectObject (mask_dc, mask_orig_obj);
2165 DeleteDC (mask_dc);
2167 else
2169 SetTextColor (hdc, s->gc->foreground);
2170 SetBkColor (hdc, s->gc->background);
2172 BitBlt (hdc, x, y, s->slice.width, s->slice.height,
2173 compat_hdc, s->slice.x, s->slice.y, SRCCOPY);
2175 /* When the image has a mask, we can expect that at
2176 least part of a mouse highlight or a block cursor will
2177 be visible. If the image doesn't have a mask, make
2178 a block cursor visible by drawing a rectangle around
2179 the image. I believe it's looking better if we do
2180 nothing here for mouse-face. */
2181 if (s->hl == DRAW_CURSOR)
2183 int r = s->img->relief;
2184 if (r < 0) r = -r;
2185 w32_draw_rectangle (hdc, s->gc, x - r, y - r,
2186 s->slice.width + r*2 - 1,
2187 s->slice.height + r*2 - 1);
2191 SelectObject (hdc, orig_brush);
2192 DeleteObject (fg_brush);
2193 SelectObject (compat_hdc, orig_obj);
2194 DeleteDC (compat_hdc);
2196 else
2197 w32_draw_rectangle (hdc, s->gc, x, y,
2198 s->slice.width - 1, s->slice.height - 1);
2200 SelectObject (hdc, orig_hdc_obj);
2201 DeleteDC (hdc);
2205 /* Draw part of the background of glyph string S. X, Y, W, and H
2206 give the rectangle to draw. */
2208 static void
2209 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2210 struct glyph_string *s;
2211 int x, y, w, h;
2213 #if 0 /* TODO: stipple */
2214 if (s->stippled_p)
2216 /* Fill background with a stipple pattern. */
2217 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2218 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2219 XSetFillStyle (s->display, s->gc, FillSolid);
2221 else
2222 #endif
2223 x_clear_glyph_string_rect (s, x, y, w, h);
2227 /* Draw image glyph string S.
2229 s->y
2230 s->x +-------------------------
2231 | s->face->box
2233 | +-------------------------
2234 | | s->img->vmargin
2236 | | +-------------------
2237 | | | the image
2241 static void
2242 x_draw_image_glyph_string (s)
2243 struct glyph_string *s;
2245 int x, y;
2246 int box_line_hwidth = abs (s->face->box_line_width);
2247 int box_line_vwidth = max (s->face->box_line_width, 0);
2248 int height;
2249 HBITMAP pixmap = 0;
2251 height = s->height - 2 * box_line_vwidth;
2253 /* Fill background with face under the image. Do it only if row is
2254 taller than image or if image has a clip mask to reduce
2255 flickering. */
2256 s->stippled_p = s->face->stipple != 0;
2257 if (height > s->slice.height
2258 || s->img->hmargin
2259 || s->img->vmargin
2260 || s->img->mask
2261 || s->img->pixmap == 0
2262 || s->width != s->background_width)
2264 x = s->x;
2265 if (s->first_glyph->left_box_line_p
2266 && s->slice.x == 0)
2267 x += box_line_hwidth;
2269 y = s->y;
2270 if (s->slice.y == 0)
2271 y += box_line_vwidth;
2273 #if 0 /* TODO: figure out if we need to do this on Windows. */
2274 if (s->img->mask)
2276 /* Create a pixmap as large as the glyph string. Fill it
2277 with the background color. Copy the image to it, using
2278 its mask. Copy the temporary pixmap to the display. */
2279 Screen *screen = FRAME_X_SCREEN (s->f);
2280 int depth = DefaultDepthOfScreen (screen);
2282 /* Create a pixmap as large as the glyph string. */
2283 pixmap = XCreatePixmap (s->display, s->window,
2284 s->background_width,
2285 s->height, depth);
2287 /* Don't clip in the following because we're working on the
2288 pixmap. */
2289 XSetClipMask (s->display, s->gc, None);
2291 /* Fill the pixmap with the background color/stipple. */
2292 if (s->stippled_p)
2294 /* Fill background with a stipple pattern. */
2295 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2296 XFillRectangle (s->display, pixmap, s->gc,
2297 0, 0, s->background_width, s->height);
2298 XSetFillStyle (s->display, s->gc, FillSolid);
2300 else
2302 XGCValues xgcv;
2303 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2304 &xgcv);
2305 XSetForeground (s->display, s->gc, xgcv.background);
2306 XFillRectangle (s->display, pixmap, s->gc,
2307 0, 0, s->background_width, s->height);
2308 XSetForeground (s->display, s->gc, xgcv.foreground);
2311 else
2312 #endif
2313 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2315 s->background_filled_p = 1;
2318 /* Draw the foreground. */
2319 if (pixmap != 0)
2321 w32_draw_image_foreground_1 (s, pixmap);
2322 x_set_glyph_string_clipping (s);
2324 HDC compat_hdc = CreateCompatibleDC (s->hdc);
2325 HBRUSH fg_brush = CreateSolidBrush (s->gc->foreground);
2326 HBRUSH orig_brush = SelectObject (s->hdc, fg_brush);
2327 HGDIOBJ orig_obj = SelectObject (compat_hdc, pixmap);
2329 SetTextColor (s->hdc, s->gc->foreground);
2330 SetBkColor (s->hdc, s->gc->background);
2331 BitBlt (s->hdc, s->x, s->y, s->background_width, s->height,
2332 compat_hdc, 0, 0, SRCCOPY);
2334 SelectObject (s->hdc, orig_brush);
2335 DeleteObject (fg_brush);
2336 SelectObject (compat_hdc, orig_obj);
2337 DeleteDC (compat_hdc);
2339 DeleteObject (pixmap);
2340 pixmap = 0;
2342 else
2343 x_draw_image_foreground (s);
2345 /* If we must draw a relief around the image, do it. */
2346 if (s->img->relief
2347 || s->hl == DRAW_IMAGE_RAISED
2348 || s->hl == DRAW_IMAGE_SUNKEN)
2349 x_draw_image_relief (s);
2353 /* Draw stretch glyph string S. */
2355 static void
2356 x_draw_stretch_glyph_string (s)
2357 struct glyph_string *s;
2359 xassert (s->first_glyph->type == STRETCH_GLYPH);
2360 s->stippled_p = s->face->stipple != 0;
2362 if (s->hl == DRAW_CURSOR
2363 && !x_stretch_cursor_p)
2365 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2366 as wide as the stretch glyph. */
2367 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
2369 /* Draw cursor. */
2370 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2372 /* Clear rest using the GC of the original non-cursor face. */
2373 if (width < s->background_width)
2375 XGCValues *gc = s->face->gc;
2376 int x = s->x + width, y = s->y;
2377 int w = s->background_width - width, h = s->height;
2378 RECT r;
2379 HDC hdc = s->hdc;
2381 if (s->row->mouse_face_p
2382 && cursor_in_mouse_face_p (s->w))
2384 x_set_mouse_face_gc (s);
2385 gc = s->gc;
2387 else
2388 gc = s->face->gc;
2390 get_glyph_string_clip_rect (s, &r);
2391 w32_set_clip_rectangle (hdc, &r);
2393 #if 0 /* TODO: stipple */
2394 if (s->face->stipple)
2396 /* Fill background with a stipple pattern. */
2397 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2398 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2399 XSetFillStyle (s->display, gc, FillSolid);
2401 else
2402 #endif
2404 w32_fill_area (s->f, s->hdc, gc->background, x, y, w, h);
2408 else if (!s->background_filled_p)
2409 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2410 s->height);
2412 s->background_filled_p = 1;
2416 /* Draw glyph string S. */
2418 static void
2419 x_draw_glyph_string (s)
2420 struct glyph_string *s;
2422 int relief_drawn_p = 0;
2424 /* If S draws into the background of its successor, draw the
2425 background of the successor first so that S can draw into it.
2426 This makes S->next use XDrawString instead of XDrawImageString. */
2427 if (s->next && s->right_overhang && !s->for_overlaps_p)
2429 xassert (s->next->img == NULL);
2430 x_set_glyph_string_gc (s->next);
2431 x_set_glyph_string_clipping (s->next);
2432 x_draw_glyph_string_background (s->next, 1);
2435 /* Set up S->gc, set clipping and draw S. */
2436 x_set_glyph_string_gc (s);
2438 /* Draw relief (if any) in advance for char/composition so that the
2439 glyph string can be drawn over it. */
2440 if (!s->for_overlaps_p
2441 && s->face->box != FACE_NO_BOX
2442 && (s->first_glyph->type == CHAR_GLYPH
2443 || s->first_glyph->type == COMPOSITE_GLYPH))
2446 x_set_glyph_string_clipping (s);
2447 x_draw_glyph_string_background (s, 1);
2448 x_draw_glyph_string_box (s);
2449 x_set_glyph_string_clipping (s);
2450 relief_drawn_p = 1;
2452 else
2453 x_set_glyph_string_clipping (s);
2455 switch (s->first_glyph->type)
2457 case IMAGE_GLYPH:
2458 x_draw_image_glyph_string (s);
2459 break;
2461 case STRETCH_GLYPH:
2462 x_draw_stretch_glyph_string (s);
2463 break;
2465 case CHAR_GLYPH:
2466 if (s->for_overlaps_p)
2467 s->background_filled_p = 1;
2468 else
2469 x_draw_glyph_string_background (s, 0);
2470 x_draw_glyph_string_foreground (s);
2471 break;
2473 case COMPOSITE_GLYPH:
2474 if (s->for_overlaps_p || s->gidx > 0)
2475 s->background_filled_p = 1;
2476 else
2477 x_draw_glyph_string_background (s, 1);
2478 x_draw_composite_glyph_string_foreground (s);
2479 break;
2481 default:
2482 abort ();
2485 if (!s->for_overlaps_p)
2487 /* Draw underline. */
2488 if (s->face->underline_p
2489 && (s->font->bdf || !s->font->tm.tmUnderlined))
2491 unsigned long h = 1;
2492 unsigned long dy = s->height - h;
2494 /* TODO: Use font information for positioning and thickness
2495 of underline. See OUTLINETEXTMETRIC, and xterm.c.
2496 Note: If you make this work, don't forget to change the
2497 doc string of x-use-underline-position-properties below. */
2498 if (s->face->underline_defaulted_p)
2500 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2501 s->y + dy, s->width, 1);
2503 else
2505 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2506 s->y + dy, s->width, 1);
2510 /* Draw overline. */
2511 if (s->face->overline_p)
2513 unsigned long dy = 0, h = 1;
2515 if (s->face->overline_color_defaulted_p)
2517 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x,
2518 s->y + dy, s->width, h);
2520 else
2522 w32_fill_area (s->f, s->hdc, s->face->overline_color, s->x,
2523 s->y + dy, s->width, h);
2527 /* Draw strike-through. */
2528 if (s->face->strike_through_p
2529 && (s->font->bdf || !s->font->tm.tmStruckOut))
2531 unsigned long h = 1;
2532 unsigned long dy = (s->height - h) / 2;
2534 if (s->face->strike_through_color_defaulted_p)
2536 w32_fill_area (s->f, s->hdc, s->gc->foreground, s->x, s->y + dy,
2537 s->width, h);
2539 else
2541 w32_fill_area (s->f, s->hdc, s->face->underline_color, s->x,
2542 s->y + dy, s->width, h);
2546 /* Draw relief. */
2547 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2548 x_draw_glyph_string_box (s);
2551 /* Reset clipping. */
2552 w32_set_clip_rectangle (s->hdc, NULL);
2556 /* Shift display to make room for inserted glyphs. */
2558 void
2559 w32_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2560 struct frame *f;
2561 int x, y, width, height, shift_by;
2563 HDC hdc;
2565 hdc = get_frame_dc (f);
2566 BitBlt (hdc, x + shift_by, y, width, height,
2567 hdc, x, y, SRCCOPY);
2569 release_frame_dc (f, hdc);
2573 /* Delete N glyphs at the nominal cursor position. Not implemented
2574 for X frames. */
2576 static void
2577 x_delete_glyphs (n)
2578 register int n;
2580 struct frame *f;
2582 if (updating_frame)
2583 f = updating_frame;
2584 else
2585 f = SELECTED_FRAME ();
2587 if (! FRAME_W32_P (f))
2588 return;
2590 abort ();
2594 /* Clear entire frame. If updating_frame is non-null, clear that
2595 frame. Otherwise clear the selected frame. */
2597 static void
2598 x_clear_frame ()
2600 struct frame *f;
2602 if (updating_frame)
2603 f = updating_frame;
2604 else
2605 f = SELECTED_FRAME ();
2607 if (! FRAME_W32_P (f))
2608 return;
2610 /* Clearing the frame will erase any cursor, so mark them all as no
2611 longer visible. */
2612 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2613 output_cursor.hpos = output_cursor.vpos = 0;
2614 output_cursor.x = -1;
2616 /* We don't set the output cursor here because there will always
2617 follow an explicit cursor_to. */
2618 BLOCK_INPUT;
2620 w32_clear_window (f);
2622 /* We have to clear the scroll bars, too. If we have changed
2623 colors or something like that, then they should be notified. */
2624 x_scroll_bar_clear (f);
2626 UNBLOCK_INPUT;
2630 /* Make audible bell. */
2632 static void
2633 w32_ring_bell (void)
2635 struct frame *f;
2637 f = SELECTED_FRAME ();
2639 BLOCK_INPUT;
2641 if (FRAME_W32_P (f) && visible_bell)
2643 int i;
2644 HWND hwnd = FRAME_W32_WINDOW (SELECTED_FRAME ());
2646 for (i = 0; i < 5; i++)
2648 FlashWindow (hwnd, TRUE);
2649 Sleep (10);
2651 FlashWindow (hwnd, FALSE);
2653 else
2654 w32_sys_ring_bell ();
2656 UNBLOCK_INPUT;
2660 /* Specify how many text lines, from the top of the window,
2661 should be affected by insert-lines and delete-lines operations.
2662 This, and those operations, are used only within an update
2663 that is bounded by calls to x_update_begin and x_update_end. */
2665 static void
2666 w32_set_terminal_window (n)
2667 register int n;
2669 /* This function intentionally left blank. */
2673 /***********************************************************************
2674 Line Dance
2675 ***********************************************************************/
2677 /* Perform an insert-lines or delete-lines operation, inserting N
2678 lines or deleting -N lines at vertical position VPOS. */
2680 static void
2681 x_ins_del_lines (vpos, n)
2682 int vpos, n;
2684 struct frame *f;
2686 if (updating_frame)
2687 f = updating_frame;
2688 else
2689 f = SELECTED_FRAME ();
2691 if (! FRAME_W32_P (f))
2692 return;
2694 abort ();
2698 /* Scroll part of the display as described by RUN. */
2700 static void
2701 x_scroll_run (w, run)
2702 struct window *w;
2703 struct run *run;
2705 struct frame *f = XFRAME (w->frame);
2706 int x, y, width, height, from_y, to_y, bottom_y;
2707 HWND hwnd = FRAME_W32_WINDOW (f);
2708 HRGN expect_dirty;
2710 /* Get frame-relative bounding box of the text display area of W,
2711 without mode lines. Include in this box the left and right
2712 fringes of W. */
2713 window_box (w, -1, &x, &y, &width, &height);
2715 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2716 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2717 bottom_y = y + height;
2719 if (to_y < from_y)
2721 /* Scrolling up. Make sure we don't copy part of the mode
2722 line at the bottom. */
2723 if (from_y + run->height > bottom_y)
2724 height = bottom_y - from_y;
2725 else
2726 height = run->height;
2727 expect_dirty = CreateRectRgn (x, y + height, x + width, bottom_y);
2729 else
2731 /* Scolling down. Make sure we don't copy over the mode line.
2732 at the bottom. */
2733 if (to_y + run->height > bottom_y)
2734 height = bottom_y - to_y;
2735 else
2736 height = run->height;
2737 expect_dirty = CreateRectRgn (x, y, x + width, to_y);
2740 BLOCK_INPUT;
2742 /* Cursor off. Will be switched on again in x_update_window_end. */
2743 updated_window = w;
2744 x_clear_cursor (w);
2747 RECT from;
2748 RECT to;
2749 HRGN dirty = CreateRectRgn (0, 0, 0, 0);
2750 HRGN combined = CreateRectRgn (0, 0, 0, 0);
2752 from.left = to.left = x;
2753 from.right = to.right = x + width;
2754 from.top = from_y;
2755 from.bottom = from_y + height;
2756 to.top = y;
2757 to.bottom = bottom_y;
2759 ScrollWindowEx (hwnd, 0, to_y - from_y, &from, &to, dirty,
2760 NULL, SW_INVALIDATE);
2762 /* Combine this with what we expect to be dirty. This covers the
2763 case where not all of the region we expect is actually dirty. */
2764 CombineRgn (combined, dirty, expect_dirty, RGN_OR);
2766 /* If the dirty region is not what we expected, redraw the entire frame. */
2767 if (!EqualRgn (combined, expect_dirty))
2768 SET_FRAME_GARBAGED (f);
2770 DeleteObject (dirty);
2771 DeleteObject (combined);
2774 UNBLOCK_INPUT;
2775 DeleteObject (expect_dirty);
2780 /***********************************************************************
2781 Exposure Events
2782 ***********************************************************************/
2784 static void
2785 frame_highlight (f)
2786 struct frame *f;
2788 x_update_cursor (f, 1);
2791 static void
2792 frame_unhighlight (f)
2793 struct frame *f;
2795 x_update_cursor (f, 1);
2798 /* The focus has changed. Update the frames as necessary to reflect
2799 the new situation. Note that we can't change the selected frame
2800 here, because the Lisp code we are interrupting might become confused.
2801 Each event gets marked with the frame in which it occurred, so the
2802 Lisp code can tell when the switch took place by examining the events. */
2804 static void
2805 x_new_focus_frame (dpyinfo, frame)
2806 struct w32_display_info *dpyinfo;
2807 struct frame *frame;
2809 struct frame *old_focus = dpyinfo->w32_focus_frame;
2811 if (frame != dpyinfo->w32_focus_frame)
2813 /* Set this before calling other routines, so that they see
2814 the correct value of w32_focus_frame. */
2815 dpyinfo->w32_focus_frame = frame;
2817 if (old_focus && old_focus->auto_lower)
2818 x_lower_frame (old_focus);
2820 if (dpyinfo->w32_focus_frame && dpyinfo->w32_focus_frame->auto_raise)
2821 pending_autoraise_frame = dpyinfo->w32_focus_frame;
2822 else
2823 pending_autoraise_frame = 0;
2826 x_frame_rehighlight (dpyinfo);
2830 /* Handle FocusIn and FocusOut state changes for FRAME.
2831 If FRAME has focus and there exists more than one frame, puts
2832 a FOCUS_IN_EVENT into *BUFP. */
2834 static void
2835 x_focus_changed (type, state, dpyinfo, frame, bufp)
2836 int type;
2837 int state;
2838 struct w32_display_info *dpyinfo;
2839 struct frame *frame;
2840 struct input_event *bufp;
2842 if (type == WM_SETFOCUS)
2844 if (dpyinfo->w32_focus_event_frame != frame)
2846 x_new_focus_frame (dpyinfo, frame);
2847 dpyinfo->w32_focus_event_frame = frame;
2849 /* Don't stop displaying the initial startup message
2850 for a switch-frame event we don't need. */
2851 if (GC_NILP (Vterminal_frame)
2852 && GC_CONSP (Vframe_list)
2853 && !GC_NILP (XCDR (Vframe_list)))
2855 bufp->kind = FOCUS_IN_EVENT;
2856 XSETFRAME (bufp->frame_or_window, frame);
2860 frame->output_data.x->focus_state |= state;
2862 /* TODO: IME focus? */
2864 else if (type == WM_KILLFOCUS)
2866 frame->output_data.x->focus_state &= ~state;
2868 if (dpyinfo->w32_focus_event_frame == frame)
2870 dpyinfo->w32_focus_event_frame = 0;
2871 x_new_focus_frame (dpyinfo, 0);
2874 /* TODO: IME focus? */
2879 /* The focus may have changed. Figure out if it is a real focus change,
2880 by checking both FocusIn/Out and Enter/LeaveNotify events.
2882 Returns FOCUS_IN_EVENT event in *BUFP. */
2884 static void
2885 w32_detect_focus_change (dpyinfo, event, bufp)
2886 struct w32_display_info *dpyinfo;
2887 W32Msg *event;
2888 struct input_event *bufp;
2890 struct frame *frame;
2892 frame = x_any_window_to_frame (dpyinfo, event->msg.hwnd);
2893 if (! frame)
2894 return;
2896 /* On w32, this is only called from focus events, so no switch needed. */
2897 x_focus_changed (event->msg.message,
2898 (event->msg.message == WM_KILLFOCUS ?
2899 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
2900 dpyinfo, frame, bufp);
2904 /* Handle an event saying the mouse has moved out of an Emacs frame. */
2906 void
2907 x_mouse_leave (dpyinfo)
2908 struct w32_display_info *dpyinfo;
2910 x_new_focus_frame (dpyinfo, dpyinfo->w32_focus_event_frame);
2913 /* The focus has changed, or we have redirected a frame's focus to
2914 another frame (this happens when a frame uses a surrogate
2915 mini-buffer frame). Shift the highlight as appropriate.
2917 The FRAME argument doesn't necessarily have anything to do with which
2918 frame is being highlighted or un-highlighted; we only use it to find
2919 the appropriate X display info. */
2921 static void
2922 w32_frame_rehighlight (frame)
2923 struct frame *frame;
2925 if (! FRAME_W32_P (frame))
2926 return;
2927 x_frame_rehighlight (FRAME_W32_DISPLAY_INFO (frame));
2930 static void
2931 x_frame_rehighlight (dpyinfo)
2932 struct w32_display_info *dpyinfo;
2934 struct frame *old_highlight = dpyinfo->x_highlight_frame;
2936 if (dpyinfo->w32_focus_frame)
2938 dpyinfo->x_highlight_frame
2939 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame)))
2940 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame))
2941 : dpyinfo->w32_focus_frame);
2942 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
2944 FRAME_FOCUS_FRAME (dpyinfo->w32_focus_frame) = Qnil;
2945 dpyinfo->x_highlight_frame = dpyinfo->w32_focus_frame;
2948 else
2949 dpyinfo->x_highlight_frame = 0;
2951 if (dpyinfo->x_highlight_frame != old_highlight)
2953 if (old_highlight)
2954 frame_unhighlight (old_highlight);
2955 if (dpyinfo->x_highlight_frame)
2956 frame_highlight (dpyinfo->x_highlight_frame);
2960 /* Keyboard processing - modifier keys, etc. */
2962 /* Convert a keysym to its name. */
2964 char *
2965 x_get_keysym_name (keysym)
2966 int keysym;
2968 /* Make static so we can always return it */
2969 static char value[100];
2971 BLOCK_INPUT;
2972 GetKeyNameText (keysym, value, 100);
2973 UNBLOCK_INPUT;
2975 return value;
2980 /* Mouse clicks and mouse movement. Rah. */
2982 /* Parse a button MESSAGE. The button index is returned in PBUTTON, and
2983 the state in PUP. XBUTTON provides extra information for extended mouse
2984 button messages. Returns FALSE if unable to parse the message. */
2985 BOOL
2986 parse_button (message, xbutton, pbutton, pup)
2987 int message;
2988 int xbutton;
2989 int * pbutton;
2990 int * pup;
2992 int button = 0;
2993 int up = 0;
2995 switch (message)
2997 case WM_LBUTTONDOWN:
2998 button = 0;
2999 up = 0;
3000 break;
3001 case WM_LBUTTONUP:
3002 button = 0;
3003 up = 1;
3004 break;
3005 case WM_MBUTTONDOWN:
3006 if (NILP (Vw32_swap_mouse_buttons))
3007 button = 1;
3008 else
3009 button = 2;
3010 up = 0;
3011 break;
3012 case WM_MBUTTONUP:
3013 if (NILP (Vw32_swap_mouse_buttons))
3014 button = 1;
3015 else
3016 button = 2;
3017 up = 1;
3018 break;
3019 case WM_RBUTTONDOWN:
3020 if (NILP (Vw32_swap_mouse_buttons))
3021 button = 2;
3022 else
3023 button = 1;
3024 up = 0;
3025 break;
3026 case WM_RBUTTONUP:
3027 if (NILP (Vw32_swap_mouse_buttons))
3028 button = 2;
3029 else
3030 button = 1;
3031 up = 1;
3032 break;
3033 case WM_XBUTTONDOWN:
3034 button = xbutton + 2;
3035 up = 0;
3036 break;
3037 case WM_XBUTTONUP:
3038 button = xbutton + 2;
3039 up = 1;
3040 break;
3041 default:
3042 return (FALSE);
3045 if (pup) *pup = up;
3046 if (pbutton) *pbutton = button;
3048 return (TRUE);
3052 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3054 If the event is a button press, then note that we have grabbed
3055 the mouse. */
3057 static Lisp_Object
3058 construct_mouse_click (result, msg, f)
3059 struct input_event *result;
3060 W32Msg *msg;
3061 struct frame *f;
3063 int button;
3064 int up;
3066 parse_button (msg->msg.message, HIWORD (msg->msg.wParam),
3067 &button, &up);
3069 /* Make the event type NO_EVENT; we'll change that when we decide
3070 otherwise. */
3071 result->kind = MOUSE_CLICK_EVENT;
3072 result->code = button;
3073 result->timestamp = msg->msg.time;
3074 result->modifiers = (msg->dwModifiers
3075 | (up
3076 ? up_modifier
3077 : down_modifier));
3079 XSETINT (result->x, LOWORD (msg->msg.lParam));
3080 XSETINT (result->y, HIWORD (msg->msg.lParam));
3081 XSETFRAME (result->frame_or_window, f);
3082 result->arg = Qnil;
3083 return Qnil;
3086 static Lisp_Object
3087 construct_mouse_wheel (result, msg, f)
3088 struct input_event *result;
3089 W32Msg *msg;
3090 struct frame *f;
3092 POINT p;
3093 int delta;
3095 result->kind = WHEEL_EVENT;
3096 result->code = 0;
3097 result->timestamp = msg->msg.time;
3099 /* A WHEEL_DELTA positive value indicates that the wheel was rotated
3100 forward, away from the user (up); a negative value indicates that
3101 the wheel was rotated backward, toward the user (down). */
3102 delta = GET_WHEEL_DELTA_WPARAM (msg->msg.wParam);
3104 /* The up and down modifiers indicate if the wheel was rotated up or
3105 down based on WHEEL_DELTA value. */
3106 result->modifiers = (msg->dwModifiers
3107 | ((delta < 0 ) ? down_modifier : up_modifier));
3109 p.x = LOWORD (msg->msg.lParam);
3110 p.y = HIWORD (msg->msg.lParam);
3111 ScreenToClient (msg->msg.hwnd, &p);
3112 XSETINT (result->x, p.x);
3113 XSETINT (result->y, p.y);
3114 XSETFRAME (result->frame_or_window, f);
3115 result->arg = Qnil;
3116 return Qnil;
3119 static Lisp_Object
3120 construct_drag_n_drop (result, msg, f)
3121 struct input_event *result;
3122 W32Msg *msg;
3123 struct frame *f;
3125 Lisp_Object files;
3126 Lisp_Object frame;
3127 HDROP hdrop;
3128 POINT p;
3129 WORD num_files;
3130 char *name;
3131 int i, len;
3133 result->kind = DRAG_N_DROP_EVENT;
3134 result->code = 0;
3135 result->timestamp = msg->msg.time;
3136 result->modifiers = msg->dwModifiers;
3138 hdrop = (HDROP) msg->msg.wParam;
3139 DragQueryPoint (hdrop, &p);
3141 #if 0
3142 p.x = LOWORD (msg->msg.lParam);
3143 p.y = HIWORD (msg->msg.lParam);
3144 ScreenToClient (msg->msg.hwnd, &p);
3145 #endif
3147 XSETINT (result->x, p.x);
3148 XSETINT (result->y, p.y);
3150 num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
3151 files = Qnil;
3153 for (i = 0; i < num_files; i++)
3155 len = DragQueryFile (hdrop, i, NULL, 0);
3156 if (len <= 0)
3157 continue;
3158 name = alloca (len + 1);
3159 DragQueryFile (hdrop, i, name, len + 1);
3160 files = Fcons (DECODE_FILE (build_string (name)), files);
3163 DragFinish (hdrop);
3165 XSETFRAME (frame, f);
3166 result->frame_or_window = Fcons (frame, files);
3167 result->arg = Qnil;
3168 return Qnil;
3172 /* Function to report a mouse movement to the mainstream Emacs code.
3173 The input handler calls this.
3175 We have received a mouse movement event, which is given in *event.
3176 If the mouse is over a different glyph than it was last time, tell
3177 the mainstream emacs code by setting mouse_moved. If not, ask for
3178 another motion event, so we can check again the next time it moves. */
3180 static MSG last_mouse_motion_event;
3181 static Lisp_Object last_mouse_motion_frame;
3183 static void remember_mouse_glyph P_ ((struct frame *, int, int));
3185 static void
3186 note_mouse_movement (frame, msg)
3187 FRAME_PTR frame;
3188 MSG *msg;
3190 int mouse_x = LOWORD (msg->lParam);
3191 int mouse_y = HIWORD (msg->lParam);
3193 last_mouse_movement_time = msg->time;
3194 memcpy (&last_mouse_motion_event, msg, sizeof (last_mouse_motion_event));
3195 XSETFRAME (last_mouse_motion_frame, frame);
3197 if (msg->hwnd != FRAME_W32_WINDOW (frame))
3199 frame->mouse_moved = 1;
3200 last_mouse_scroll_bar = Qnil;
3201 note_mouse_highlight (frame, -1, -1);
3204 /* Has the mouse moved off the glyph it was on at the last sighting? */
3205 else if (mouse_x < last_mouse_glyph.left
3206 || mouse_x > last_mouse_glyph.right
3207 || mouse_y < last_mouse_glyph.top
3208 || mouse_y > last_mouse_glyph.bottom)
3210 frame->mouse_moved = 1;
3211 last_mouse_scroll_bar = Qnil;
3212 note_mouse_highlight (frame, mouse_x, mouse_y);
3213 /* Remember the mouse position here, as w32_mouse_position only
3214 gets called when mouse tracking is enabled but we also need
3215 to keep track of the mouse for help_echo and highlighting at
3216 other times. */
3217 remember_mouse_glyph (frame, mouse_x, mouse_y);
3222 /************************************************************************
3223 Mouse Face
3224 ************************************************************************/
3226 static struct scroll_bar *x_window_to_scroll_bar ();
3227 static void x_scroll_bar_report_motion ();
3228 static void x_check_fullscreen P_ ((struct frame *));
3229 static int glyph_rect P_ ((struct frame *f, int, int, RECT *));
3232 static void
3233 redo_mouse_highlight ()
3235 if (!NILP (last_mouse_motion_frame)
3236 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3237 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3238 LOWORD (last_mouse_motion_event.lParam),
3239 HIWORD (last_mouse_motion_event.lParam));
3242 void
3243 w32_define_cursor (window, cursor)
3244 Window window;
3245 Cursor cursor;
3247 PostMessage (window, WM_EMACS_SETCURSOR, (WPARAM) cursor, 0);
3250 /* Try to determine frame pixel position and size of the glyph under
3251 frame pixel coordinates X/Y on frame F . Return the position and
3252 size in *RECT. Value is non-zero if we could compute these
3253 values. */
3255 static int
3256 glyph_rect (f, x, y, rect)
3257 struct frame *f;
3258 int x, y;
3259 RECT *rect;
3261 Lisp_Object window;
3263 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
3265 if (!NILP (window))
3267 struct window *w = XWINDOW (window);
3268 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3269 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3271 for (; r < end && r->enabled_p; ++r)
3272 if (r->y <= y && r->y + r->height > y)
3274 /* Found the row at y. */
3275 struct glyph *g = r->glyphs[TEXT_AREA];
3276 struct glyph *end = g + r->used[TEXT_AREA];
3277 int gx;
3279 rect->top = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3280 rect->bottom = rect->top + r->height;
3282 if (x < r->x)
3284 /* x is to the left of the first glyph in the row. */
3285 /* Shouldn't this be a pixel value?
3286 WINDOW_LEFT_EDGE_X (w) seems to be the right value.
3287 ++KFS */
3288 rect->left = WINDOW_LEFT_EDGE_COL (w);
3289 rect->right = WINDOW_TO_FRAME_PIXEL_X (w, r->x);
3290 return 1;
3293 for (gx = r->x; g < end; gx += g->pixel_width, ++g)
3294 if (gx <= x && gx + g->pixel_width > x)
3296 /* x is on a glyph. */
3297 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3298 rect->right = rect->left + g->pixel_width;
3299 return 1;
3302 /* x is to the right of the last glyph in the row. */
3303 rect->left = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3304 /* Shouldn't this be a pixel value?
3305 WINDOW_RIGHT_EDGE_X (w) seems to be the right value.
3306 ++KFS */
3307 rect->right = WINDOW_RIGHT_EDGE_COL (w);
3308 return 1;
3312 /* The y is not on any row. */
3313 return 0;
3316 /* Record the position of the mouse in last_mouse_glyph. */
3317 static void
3318 remember_mouse_glyph (f1, gx, gy)
3319 struct frame * f1;
3320 int gx, gy;
3322 if (!glyph_rect (f1, gx, gy, &last_mouse_glyph))
3324 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3325 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3327 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
3328 round down even for negative values. */
3329 if (gx < 0)
3330 gx -= width - 1;
3331 if (gy < 0)
3332 gy -= height - 1;
3333 #if 0
3334 /* This was the original code from XTmouse_position, but it seems
3335 to give the position of the glyph diagonally next to the one
3336 the mouse is over. */
3337 gx = (gx + width - 1) / width * width;
3338 gy = (gy + height - 1) / height * height;
3339 #else
3340 gx = gx / width * width;
3341 gy = gy / height * height;
3342 #endif
3344 last_mouse_glyph.left = gx;
3345 last_mouse_glyph.top = gy;
3346 last_mouse_glyph.right = gx + width;
3347 last_mouse_glyph.bottom = gy + height;
3351 /* Return the current position of the mouse.
3352 *fp should be a frame which indicates which display to ask about.
3354 If the mouse movement started in a scroll bar, set *fp, *bar_window,
3355 and *part to the frame, window, and scroll bar part that the mouse
3356 is over. Set *x and *y to the portion and whole of the mouse's
3357 position on the scroll bar.
3359 If the mouse movement started elsewhere, set *fp to the frame the
3360 mouse is on, *bar_window to nil, and *x and *y to the character cell
3361 the mouse is over.
3363 Set *time to the server time-stamp for the time at which the mouse
3364 was at this position.
3366 Don't store anything if we don't have a valid set of values to report.
3368 This clears the mouse_moved flag, so we can wait for the next mouse
3369 movement. */
3371 static void
3372 w32_mouse_position (fp, insist, bar_window, part, x, y, time)
3373 FRAME_PTR *fp;
3374 int insist;
3375 Lisp_Object *bar_window;
3376 enum scroll_bar_part *part;
3377 Lisp_Object *x, *y;
3378 unsigned long *time;
3380 FRAME_PTR f1;
3382 BLOCK_INPUT;
3384 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3385 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3386 else
3388 POINT pt;
3390 Lisp_Object frame, tail;
3392 /* Clear the mouse-moved flag for every frame on this display. */
3393 FOR_EACH_FRAME (tail, frame)
3394 XFRAME (frame)->mouse_moved = 0;
3396 last_mouse_scroll_bar = Qnil;
3398 GetCursorPos (&pt);
3400 /* Now we have a position on the root; find the innermost window
3401 containing the pointer. */
3403 if (FRAME_W32_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3404 && FRAME_LIVE_P (last_mouse_frame))
3406 /* If mouse was grabbed on a frame, give coords for that frame
3407 even if the mouse is now outside it. */
3408 f1 = last_mouse_frame;
3410 else
3412 /* Is window under mouse one of our frames? */
3413 f1 = x_any_window_to_frame (FRAME_W32_DISPLAY_INFO (*fp),
3414 WindowFromPoint (pt));
3417 /* If not, is it one of our scroll bars? */
3418 if (! f1)
3420 struct scroll_bar *bar
3421 = x_window_to_scroll_bar (WindowFromPoint (pt));
3423 if (bar)
3425 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3429 if (f1 == 0 && insist > 0)
3430 f1 = SELECTED_FRAME ();
3432 if (f1)
3434 /* Ok, we found a frame. Store all the values.
3435 last_mouse_glyph is a rectangle used to reduce the
3436 generation of mouse events. To not miss any motion
3437 events, we must divide the frame into rectangles of the
3438 size of the smallest character that could be displayed
3439 on it, i.e. into the same rectangles that matrices on
3440 the frame are divided into. */
3442 #if OLD_REDISPLAY_CODE
3443 int ignore1, ignore2;
3445 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3447 pixel_to_glyph_coords (f1, pt.x, pt.y, &ignore1, &ignore2,
3448 &last_mouse_glyph,
3449 FRAME_W32_DISPLAY_INFO (f1)->grabbed
3450 || insist);
3451 #else
3452 ScreenToClient (FRAME_W32_WINDOW (f1), &pt);
3453 remember_mouse_glyph (f1, pt.x, pt.y);
3454 #endif
3456 *bar_window = Qnil;
3457 *part = 0;
3458 *fp = f1;
3459 XSETINT (*x, pt.x);
3460 XSETINT (*y, pt.y);
3461 *time = last_mouse_movement_time;
3466 UNBLOCK_INPUT;
3470 /***********************************************************************
3471 Tool-bars
3472 ***********************************************************************/
3474 /* Handle mouse button event on the tool-bar of frame F, at
3475 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
3476 or ButtonRelase. */
3478 static void
3479 w32_handle_tool_bar_click (f, button_event)
3480 struct frame *f;
3481 struct input_event *button_event;
3483 int x = XFASTINT (button_event->x);
3484 int y = XFASTINT (button_event->y);
3486 if (button_event->modifiers & down_modifier)
3487 handle_tool_bar_click (f, x, y, 1, 0);
3488 else
3489 handle_tool_bar_click (f, x, y, 0,
3490 button_event->modifiers & ~up_modifier);
3495 /***********************************************************************
3496 Scroll bars
3497 ***********************************************************************/
3499 /* Scroll bar support. */
3501 /* Given a window ID, find the struct scroll_bar which manages it.
3502 This can be called in GC, so we have to make sure to strip off mark
3503 bits. */
3505 static struct scroll_bar *
3506 x_window_to_scroll_bar (window_id)
3507 Window window_id;
3509 Lisp_Object tail;
3511 for (tail = Vframe_list;
3512 XGCTYPE (tail) == Lisp_Cons;
3513 tail = XCDR (tail))
3515 Lisp_Object frame, bar, condemned;
3517 frame = XCAR (tail);
3518 /* All elements of Vframe_list should be frames. */
3519 if (! GC_FRAMEP (frame))
3520 abort ();
3522 /* Scan this frame's scroll bar list for a scroll bar with the
3523 right window ID. */
3524 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3525 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3526 /* This trick allows us to search both the ordinary and
3527 condemned scroll bar lists with one loop. */
3528 ! GC_NILP (bar) || (bar = condemned,
3529 condemned = Qnil,
3530 ! GC_NILP (bar));
3531 bar = XSCROLL_BAR (bar)->next)
3532 if (SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar)) == window_id)
3533 return XSCROLL_BAR (bar);
3536 return 0;
3541 /* Set the thumb size and position of scroll bar BAR. We are currently
3542 displaying PORTION out of a whole WHOLE, and our position POSITION. */
3544 static void
3545 w32_set_scroll_bar_thumb (bar, portion, position, whole)
3546 struct scroll_bar *bar;
3547 int portion, position, whole;
3549 Window w = SCROLL_BAR_W32_WINDOW (bar);
3550 double range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
3551 int sb_page, sb_pos;
3552 BOOL draggingp = !NILP (bar->dragging) ? TRUE : FALSE;
3554 if (whole)
3556 /* Position scroll bar at rock bottom if the bottom of the
3557 buffer is visible. This avoids shinking the thumb away
3558 to nothing if it is held at the bottom of the buffer. */
3559 if (position + portion >= whole)
3561 sb_page = range * (whole - position) / whole
3562 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3563 sb_pos = range;
3566 sb_page = portion * range / whole + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3567 sb_pos = position * range / whole;
3569 else
3571 sb_page = range;
3572 sb_pos = 0;
3575 BLOCK_INPUT;
3577 if (pfnSetScrollInfo)
3579 SCROLLINFO si;
3581 si.cbSize = sizeof (si);
3582 /* Only update page size if currently dragging, to reduce
3583 flicker effects. */
3584 if (draggingp)
3585 si.fMask = SIF_PAGE;
3586 else
3587 si.fMask = SIF_PAGE | SIF_POS;
3588 si.nPage = sb_page;
3589 si.nPos = sb_pos;
3591 pfnSetScrollInfo (w, SB_CTL, &si, !draggingp);
3593 else
3594 SetScrollPos (w, SB_CTL, sb_pos, !draggingp);
3596 UNBLOCK_INPUT;
3600 /************************************************************************
3601 Scroll bars, general
3602 ************************************************************************/
3604 HWND
3605 my_create_scrollbar (f, bar)
3606 struct frame * f;
3607 struct scroll_bar * bar;
3609 return (HWND) SendMessage (FRAME_W32_WINDOW (f),
3610 WM_EMACS_CREATESCROLLBAR, (WPARAM) f,
3611 (LPARAM) bar);
3614 /*#define ATTACH_THREADS*/
3616 BOOL
3617 my_show_window (FRAME_PTR f, HWND hwnd, int how)
3619 #ifndef ATTACH_THREADS
3620 return SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SHOWWINDOW,
3621 (WPARAM) hwnd, (LPARAM) how);
3622 #else
3623 return ShowWindow (hwnd, how);
3624 #endif
3627 void
3628 my_set_window_pos (HWND hwnd, HWND hwndAfter,
3629 int x, int y, int cx, int cy, UINT flags)
3631 #ifndef ATTACH_THREADS
3632 WINDOWPOS pos;
3633 pos.hwndInsertAfter = hwndAfter;
3634 pos.x = x;
3635 pos.y = y;
3636 pos.cx = cx;
3637 pos.cy = cy;
3638 pos.flags = flags;
3639 SendMessage (hwnd, WM_EMACS_SETWINDOWPOS, (WPARAM) &pos, 0);
3640 #else
3641 SetWindowPos (hwnd, hwndAfter, x, y, cx, cy, flags);
3642 #endif
3645 void
3646 my_set_focus (f, hwnd)
3647 struct frame * f;
3648 HWND hwnd;
3650 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_SETFOCUS,
3651 (WPARAM) hwnd, 0);
3654 void
3655 my_set_foreground_window (hwnd)
3656 HWND hwnd;
3658 SendMessage (hwnd, WM_EMACS_SETFOREGROUND, (WPARAM) hwnd, 0);
3661 void
3662 my_destroy_window (f, hwnd)
3663 struct frame * f;
3664 HWND hwnd;
3666 SendMessage (FRAME_W32_WINDOW (f), WM_EMACS_DESTROYWINDOW,
3667 (WPARAM) hwnd, 0);
3670 /* Create a scroll bar and return the scroll bar vector for it. W is
3671 the Emacs window on which to create the scroll bar. TOP, LEFT,
3672 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
3673 scroll bar. */
3675 static struct scroll_bar *
3676 x_scroll_bar_create (w, top, left, width, height)
3677 struct window *w;
3678 int top, left, width, height;
3680 struct frame *f = XFRAME (WINDOW_FRAME (w));
3681 HWND hwnd;
3682 struct scroll_bar *bar
3683 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
3685 BLOCK_INPUT;
3687 XSETWINDOW (bar->window, w);
3688 XSETINT (bar->top, top);
3689 XSETINT (bar->left, left);
3690 XSETINT (bar->width, width);
3691 XSETINT (bar->height, height);
3692 XSETINT (bar->start, 0);
3693 XSETINT (bar->end, 0);
3694 bar->dragging = Qnil;
3696 /* Requires geometry to be set before call to create the real window */
3698 hwnd = my_create_scrollbar (f, bar);
3700 if (pfnSetScrollInfo)
3702 SCROLLINFO si;
3704 si.cbSize = sizeof (si);
3705 si.fMask = SIF_ALL;
3706 si.nMin = 0;
3707 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3708 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3709 si.nPage = si.nMax;
3710 si.nPos = 0;
3712 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3714 else
3716 SetScrollRange (hwnd, SB_CTL, 0,
3717 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
3718 SetScrollPos (hwnd, SB_CTL, 0, FALSE);
3721 SET_SCROLL_BAR_W32_WINDOW (bar, hwnd);
3723 /* Add bar to its frame's list of scroll bars. */
3724 bar->next = FRAME_SCROLL_BARS (f);
3725 bar->prev = Qnil;
3726 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3727 if (! NILP (bar->next))
3728 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3730 UNBLOCK_INPUT;
3732 return bar;
3736 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
3737 nil. */
3739 static void
3740 x_scroll_bar_remove (bar)
3741 struct scroll_bar *bar;
3743 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3745 BLOCK_INPUT;
3747 /* Destroy the window. */
3748 my_destroy_window (f, SCROLL_BAR_W32_WINDOW (bar));
3750 /* Disassociate this scroll bar from its window. */
3751 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
3753 UNBLOCK_INPUT;
3756 /* Set the handle of the vertical scroll bar for WINDOW to indicate
3757 that we are displaying PORTION characters out of a total of WHOLE
3758 characters, starting at POSITION. If WINDOW has no scroll bar,
3759 create one. */
3760 static void
3761 w32_set_vertical_scroll_bar (w, portion, whole, position)
3762 struct window *w;
3763 int portion, whole, position;
3765 struct frame *f = XFRAME (w->frame);
3766 struct scroll_bar *bar;
3767 int top, height, left, sb_left, width, sb_width;
3768 int window_y, window_height;
3770 /* Get window dimensions. */
3771 window_box (w, -1, 0, &window_y, 0, &window_height);
3772 top = window_y;
3773 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
3774 height = window_height;
3776 /* Compute the left edge of the scroll bar area. */
3777 left = WINDOW_SCROLL_BAR_AREA_X (w);
3779 /* Compute the width of the scroll bar which might be less than
3780 the width of the area reserved for the scroll bar. */
3781 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
3782 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3783 else
3784 sb_width = width;
3786 /* Compute the left edge of the scroll bar. */
3787 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
3788 sb_left = left + width - sb_width - (width - sb_width) / 2;
3789 else
3790 sb_left = left + (width - sb_width) / 2;
3792 /* Does the scroll bar exist yet? */
3793 if (NILP (w->vertical_scroll_bar))
3795 HDC hdc;
3796 BLOCK_INPUT;
3797 if (width > 0 && height > 0)
3799 hdc = get_frame_dc (f);
3800 w32_clear_area (f, hdc, left, top, width, height);
3801 release_frame_dc (f, hdc);
3803 UNBLOCK_INPUT;
3805 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
3807 else
3809 /* It may just need to be moved and resized. */
3810 HWND hwnd;
3812 bar = XSCROLL_BAR (w->vertical_scroll_bar);
3813 hwnd = SCROLL_BAR_W32_WINDOW (bar);
3815 /* If already correctly positioned, do nothing. */
3816 if ( XINT (bar->left) == sb_left
3817 && XINT (bar->top) == top
3818 && XINT (bar->width) == sb_width
3819 && XINT (bar->height) == height )
3821 /* Redraw after clear_frame. */
3822 if (!my_show_window (f, hwnd, SW_NORMAL))
3823 InvalidateRect (hwnd, NULL, FALSE);
3825 else
3827 HDC hdc;
3828 BLOCK_INPUT;
3829 if (width && height)
3831 hdc = get_frame_dc (f);
3832 /* Since Windows scroll bars are smaller than the space reserved
3833 for them on the frame, we have to clear "under" them. */
3834 w32_clear_area (f, hdc,
3835 left,
3836 top,
3837 width,
3838 height);
3839 release_frame_dc (f, hdc);
3841 /* Make sure scroll bar is "visible" before moving, to ensure the
3842 area of the parent window now exposed will be refreshed. */
3843 my_show_window (f, hwnd, SW_HIDE);
3844 MoveWindow (hwnd, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
3845 top, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
3846 max (height, 1), TRUE);
3847 if (pfnSetScrollInfo)
3849 SCROLLINFO si;
3851 si.cbSize = sizeof (si);
3852 si.fMask = SIF_RANGE;
3853 si.nMin = 0;
3854 si.nMax = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height)
3855 + VERTICAL_SCROLL_BAR_MIN_HANDLE;
3857 pfnSetScrollInfo (hwnd, SB_CTL, &si, FALSE);
3859 else
3860 SetScrollRange (hwnd, SB_CTL, 0,
3861 VERTICAL_SCROLL_BAR_TOP_RANGE (f, height), FALSE);
3862 my_show_window (f, hwnd, SW_NORMAL);
3863 /* InvalidateRect (w, NULL, FALSE); */
3865 /* Remember new settings. */
3866 XSETINT (bar->left, sb_left);
3867 XSETINT (bar->top, top);
3868 XSETINT (bar->width, sb_width);
3869 XSETINT (bar->height, height);
3871 UNBLOCK_INPUT;
3874 w32_set_scroll_bar_thumb (bar, portion, position, whole);
3876 XSETVECTOR (w->vertical_scroll_bar, bar);
3880 /* The following three hooks are used when we're doing a thorough
3881 redisplay of the frame. We don't explicitly know which scroll bars
3882 are going to be deleted, because keeping track of when windows go
3883 away is a real pain - "Can you say set-window-configuration, boys
3884 and girls?" Instead, we just assert at the beginning of redisplay
3885 that *all* scroll bars are to be removed, and then save a scroll bar
3886 from the fiery pit when we actually redisplay its window. */
3888 /* Arrange for all scroll bars on FRAME to be removed at the next call
3889 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
3890 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
3892 static void
3893 w32_condemn_scroll_bars (frame)
3894 FRAME_PTR frame;
3896 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
3897 while (! NILP (FRAME_SCROLL_BARS (frame)))
3899 Lisp_Object bar;
3900 bar = FRAME_SCROLL_BARS (frame);
3901 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
3902 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
3903 XSCROLL_BAR (bar)->prev = Qnil;
3904 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
3905 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
3906 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
3911 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
3912 Note that WINDOW isn't necessarily condemned at all. */
3914 static void
3915 w32_redeem_scroll_bar (window)
3916 struct window *window;
3918 struct scroll_bar *bar;
3919 struct frame *f;
3921 /* We can't redeem this window's scroll bar if it doesn't have one. */
3922 if (NILP (window->vertical_scroll_bar))
3923 abort ();
3925 bar = XSCROLL_BAR (window->vertical_scroll_bar);
3927 /* Unlink it from the condemned list. */
3928 f = XFRAME (WINDOW_FRAME (window));
3929 if (NILP (bar->prev))
3931 /* If the prev pointer is nil, it must be the first in one of
3932 the lists. */
3933 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
3934 /* It's not condemned. Everything's fine. */
3935 return;
3936 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
3937 window->vertical_scroll_bar))
3938 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
3939 else
3940 /* If its prev pointer is nil, it must be at the front of
3941 one or the other! */
3942 abort ();
3944 else
3945 XSCROLL_BAR (bar->prev)->next = bar->next;
3947 if (! NILP (bar->next))
3948 XSCROLL_BAR (bar->next)->prev = bar->prev;
3950 bar->next = FRAME_SCROLL_BARS (f);
3951 bar->prev = Qnil;
3952 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
3953 if (! NILP (bar->next))
3954 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
3957 /* Remove all scroll bars on FRAME that haven't been saved since the
3958 last call to `*condemn_scroll_bars_hook'. */
3960 static void
3961 w32_judge_scroll_bars (f)
3962 FRAME_PTR f;
3964 Lisp_Object bar, next;
3966 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
3968 /* Clear out the condemned list now so we won't try to process any
3969 more events on the hapless scroll bars. */
3970 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
3972 for (; ! NILP (bar); bar = next)
3974 struct scroll_bar *b = XSCROLL_BAR (bar);
3976 x_scroll_bar_remove (b);
3978 next = b->next;
3979 b->next = b->prev = Qnil;
3982 /* Now there should be no references to the condemned scroll bars,
3983 and they should get garbage-collected. */
3986 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
3987 is set to something other than NO_EVENT, it is enqueued.
3989 This may be called from a signal handler, so we have to ignore GC
3990 mark bits. */
3992 static int
3993 w32_scroll_bar_handle_click (bar, msg, emacs_event)
3994 struct scroll_bar *bar;
3995 W32Msg *msg;
3996 struct input_event *emacs_event;
3998 if (! GC_WINDOWP (bar->window))
3999 abort ();
4001 emacs_event->kind = W32_SCROLL_BAR_CLICK_EVENT;
4002 emacs_event->code = 0;
4003 /* not really meaningful to distinguish up/down */
4004 emacs_event->modifiers = msg->dwModifiers;
4005 emacs_event->frame_or_window = bar->window;
4006 emacs_event->arg = Qnil;
4007 emacs_event->timestamp = msg->msg.time;
4010 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4011 int y;
4012 int dragging = !NILP (bar->dragging);
4014 if (pfnGetScrollInfo)
4016 SCROLLINFO si;
4018 si.cbSize = sizeof (si);
4019 si.fMask = SIF_POS;
4021 pfnGetScrollInfo ((HWND) msg->msg.lParam, SB_CTL, &si);
4022 y = si.nPos;
4024 else
4025 y = GetScrollPos ((HWND) msg->msg.lParam, SB_CTL);
4027 bar->dragging = Qnil;
4030 last_mouse_scroll_bar_pos = msg->msg.wParam;
4032 switch (LOWORD (msg->msg.wParam))
4034 case SB_LINEDOWN:
4035 emacs_event->part = scroll_bar_down_arrow;
4036 break;
4037 case SB_LINEUP:
4038 emacs_event->part = scroll_bar_up_arrow;
4039 break;
4040 case SB_PAGEUP:
4041 emacs_event->part = scroll_bar_above_handle;
4042 break;
4043 case SB_PAGEDOWN:
4044 emacs_event->part = scroll_bar_below_handle;
4045 break;
4046 case SB_TOP:
4047 emacs_event->part = scroll_bar_handle;
4048 y = 0;
4049 break;
4050 case SB_BOTTOM:
4051 emacs_event->part = scroll_bar_handle;
4052 y = top_range;
4053 break;
4054 case SB_THUMBTRACK:
4055 case SB_THUMBPOSITION:
4056 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
4057 y = HIWORD (msg->msg.wParam);
4058 bar->dragging = Qt;
4059 emacs_event->part = scroll_bar_handle;
4061 /* "Silently" update current position. */
4062 if (pfnSetScrollInfo)
4064 SCROLLINFO si;
4066 si.cbSize = sizeof (si);
4067 si.fMask = SIF_POS;
4068 si.nPos = y;
4069 /* Remember apparent position (we actually lag behind the real
4070 position, so don't set that directly. */
4071 last_scroll_bar_drag_pos = y;
4073 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, FALSE);
4075 else
4076 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, FALSE);
4077 break;
4078 case SB_ENDSCROLL:
4079 /* If this is the end of a drag sequence, then reset the scroll
4080 handle size to normal and do a final redraw. Otherwise do
4081 nothing. */
4082 if (dragging)
4084 if (pfnSetScrollInfo)
4086 SCROLLINFO si;
4087 int start = XINT (bar->start);
4088 int end = XINT (bar->end);
4090 si.cbSize = sizeof (si);
4091 si.fMask = SIF_PAGE | SIF_POS;
4092 si.nPage = end - start + VERTICAL_SCROLL_BAR_MIN_HANDLE;
4093 si.nPos = last_scroll_bar_drag_pos;
4094 pfnSetScrollInfo (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, &si, TRUE);
4096 else
4097 SetScrollPos (SCROLL_BAR_W32_WINDOW (bar), SB_CTL, y, TRUE);
4099 /* fall through */
4100 default:
4101 emacs_event->kind = NO_EVENT;
4102 return FALSE;
4105 XSETINT (emacs_event->x, y);
4106 XSETINT (emacs_event->y, top_range);
4108 return TRUE;
4112 /* Return information to the user about the current position of the mouse
4113 on the scroll bar. */
4115 static void
4116 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
4117 FRAME_PTR *fp;
4118 Lisp_Object *bar_window;
4119 enum scroll_bar_part *part;
4120 Lisp_Object *x, *y;
4121 unsigned long *time;
4123 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
4124 Window w = SCROLL_BAR_W32_WINDOW (bar);
4125 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4126 int pos;
4127 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4129 BLOCK_INPUT;
4131 *fp = f;
4132 *bar_window = bar->window;
4134 if (pfnGetScrollInfo)
4136 SCROLLINFO si;
4138 si.cbSize = sizeof (si);
4139 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE;
4141 pfnGetScrollInfo (w, SB_CTL, &si);
4142 pos = si.nPos;
4143 top_range = si.nMax - si.nPage + 1;
4145 else
4146 pos = GetScrollPos (w, SB_CTL);
4148 switch (LOWORD (last_mouse_scroll_bar_pos))
4150 case SB_THUMBPOSITION:
4151 case SB_THUMBTRACK:
4152 *part = scroll_bar_handle;
4153 if (VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)) <= 0xffff)
4154 pos = HIWORD (last_mouse_scroll_bar_pos);
4155 break;
4156 case SB_LINEDOWN:
4157 *part = scroll_bar_handle;
4158 pos++;
4159 break;
4160 default:
4161 *part = scroll_bar_handle;
4162 break;
4165 XSETINT (*x, pos);
4166 XSETINT (*y, top_range);
4168 f->mouse_moved = 0;
4169 last_mouse_scroll_bar = Qnil;
4171 *time = last_mouse_movement_time;
4173 UNBLOCK_INPUT;
4177 /* The screen has been cleared so we may have changed foreground or
4178 background colors, and the scroll bars may need to be redrawn.
4179 Clear out the scroll bars, and ask for expose events, so we can
4180 redraw them. */
4182 void
4183 x_scroll_bar_clear (f)
4184 FRAME_PTR f;
4186 Lisp_Object bar;
4188 /* We can have scroll bars even if this is 0,
4189 if we just turned off scroll bar mode.
4190 But in that case we should not clear them. */
4191 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4192 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
4193 bar = XSCROLL_BAR (bar)->next)
4195 HWND window = SCROLL_BAR_W32_WINDOW (XSCROLL_BAR (bar));
4196 HDC hdc = GetDC (window);
4197 RECT rect;
4199 /* Hide scroll bar until ready to repaint. x_scroll_bar_move
4200 arranges to refresh the scroll bar if hidden. */
4201 my_show_window (f, window, SW_HIDE);
4203 GetClientRect (window, &rect);
4204 select_palette (f, hdc);
4205 w32_clear_rect (f, hdc, &rect);
4206 deselect_palette (f, hdc);
4208 ReleaseDC (window, hdc);
4213 /* The main W32 event-reading loop - w32_read_socket. */
4215 /* Record the last 100 characters stored
4216 to help debug the loss-of-chars-during-GC problem. */
4218 static int temp_index;
4219 static short temp_buffer[100];
4222 /* Read events coming from the W32 shell.
4223 This routine is called by the SIGIO handler.
4224 We return as soon as there are no more events to be read.
4226 We return the number of characters stored into the buffer,
4227 thus pretending to be `read'.
4229 EXPECTED is nonzero if the caller knows input is available.
4231 Some of these messages are reposted back to the message queue since the
4232 system calls the windows proc directly in a context where we cannot return
4233 the data nor can we guarantee the state we are in. So if we dispatch them
4234 we will get into an infinite loop. To prevent this from ever happening we
4235 will set a variable to indicate we are in the read_socket call and indicate
4236 which message we are processing since the windows proc gets called
4237 recursively with different messages by the system.
4241 w32_read_socket (sd, expected, hold_quit)
4242 register int sd;
4243 int expected;
4244 struct input_event *hold_quit;
4246 int count = 0;
4247 int check_visibility = 0;
4248 W32Msg msg;
4249 struct frame *f;
4250 struct w32_display_info *dpyinfo = &one_w32_display_info;
4252 if (interrupt_input_blocked)
4254 interrupt_input_pending = 1;
4255 return -1;
4258 interrupt_input_pending = 0;
4259 BLOCK_INPUT;
4261 /* So people can tell when we have read the available input. */
4262 input_signal_count++;
4264 /* TODO: ghostscript integration. */
4265 while (get_next_msg (&msg, FALSE))
4267 struct input_event inev;
4268 int do_help = 0;
4270 EVENT_INIT (inev);
4271 inev.kind = NO_EVENT;
4272 inev.arg = Qnil;
4274 switch (msg.msg.message)
4276 case WM_PAINT:
4277 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4279 if (f)
4281 if (msg.rect.right == msg.rect.left ||
4282 msg.rect.bottom == msg.rect.top)
4284 /* We may get paint messages even though the client
4285 area is clipped - these are not expose events. */
4286 DebPrint (("clipped frame %p (%s) got WM_PAINT - ignored\n", f,
4287 SDATA (f->name)));
4289 else if (f->async_visible != 1)
4291 /* Definitely not obscured, so mark as visible. */
4292 f->async_visible = 1;
4293 f->async_iconified = 0;
4294 SET_FRAME_GARBAGED (f);
4295 DebPrint (("frame %p (%s) reexposed by WM_PAINT\n", f,
4296 SDATA (f->name)));
4298 /* WM_PAINT serves as MapNotify as well, so report
4299 visibility changes properly. */
4300 if (f->iconified)
4302 inev.kind = DEICONIFY_EVENT;
4303 XSETFRAME (inev.frame_or_window, f);
4305 else if (! NILP (Vframe_list)
4306 && ! NILP (XCDR (Vframe_list)))
4307 /* Force a redisplay sooner or later to update the
4308 frame titles in case this is the second frame. */
4309 record_asynch_buffer_change ();
4311 else
4313 HDC hdc = get_frame_dc (f);
4315 /* Erase background again for safety. */
4316 w32_clear_rect (f, hdc, &msg.rect);
4317 release_frame_dc (f, hdc);
4318 expose_frame (f,
4319 msg.rect.left,
4320 msg.rect.top,
4321 msg.rect.right - msg.rect.left,
4322 msg.rect.bottom - msg.rect.top);
4325 break;
4327 case WM_INPUTLANGCHANGE:
4328 /* Generate a language change event. */
4329 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4331 if (f)
4333 inev.kind = LANGUAGE_CHANGE_EVENT;
4334 XSETFRAME (inev.frame_or_window, f);
4335 inev.code = msg.msg.wParam;
4336 inev.modifiers = msg.msg.lParam & 0xffff;
4338 break;
4340 case WM_KEYDOWN:
4341 case WM_SYSKEYDOWN:
4342 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4344 if (f && !f->iconified)
4346 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
4348 clear_mouse_face (dpyinfo);
4349 dpyinfo->mouse_face_hidden = 1;
4352 if (temp_index == sizeof temp_buffer / sizeof (short))
4353 temp_index = 0;
4354 temp_buffer[temp_index++] = msg.msg.wParam;
4355 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
4356 inev.code = msg.msg.wParam;
4357 inev.modifiers = msg.dwModifiers;
4358 XSETFRAME (inev.frame_or_window, f);
4359 inev.timestamp = msg.msg.time;
4361 break;
4363 case WM_SYSCHAR:
4364 case WM_CHAR:
4365 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4367 if (f && !f->iconified)
4369 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
4371 clear_mouse_face (dpyinfo);
4372 dpyinfo->mouse_face_hidden = 1;
4375 if (temp_index == sizeof temp_buffer / sizeof (short))
4376 temp_index = 0;
4377 temp_buffer[temp_index++] = msg.msg.wParam;
4378 inev.kind = ASCII_KEYSTROKE_EVENT;
4379 inev.code = msg.msg.wParam;
4380 inev.modifiers = msg.dwModifiers;
4381 XSETFRAME (inev.frame_or_window, f);
4382 inev.timestamp = msg.msg.time;
4384 break;
4386 case WM_MOUSEMOVE:
4387 /* Ignore non-movement. */
4389 int x = LOWORD (msg.msg.lParam);
4390 int y = HIWORD (msg.msg.lParam);
4391 if (x == last_mousemove_x && y == last_mousemove_y)
4392 break;
4393 last_mousemove_x = x;
4394 last_mousemove_y = y;
4397 previous_help_echo_string = help_echo_string;
4399 if (dpyinfo->grabbed && last_mouse_frame
4400 && FRAME_LIVE_P (last_mouse_frame))
4401 f = last_mouse_frame;
4402 else
4403 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4405 if (dpyinfo->mouse_face_hidden)
4407 dpyinfo->mouse_face_hidden = 0;
4408 clear_mouse_face (dpyinfo);
4411 if (f)
4413 /* Generate SELECT_WINDOW_EVENTs when needed. */
4414 if (mouse_autoselect_window)
4416 Lisp_Object window;
4417 int x = LOWORD (msg.msg.lParam);
4418 int y = HIWORD (msg.msg.lParam);
4420 window = window_from_coordinates (f, x, y, 0, 0, 0, 0);
4422 /* Window will be selected only when it is not
4423 selected now and last mouse movement event was
4424 not in it. Minibuffer window will be selected
4425 iff it is active. */
4426 if (WINDOWP(window)
4427 && !EQ (window, last_window)
4428 && !EQ (window, selected_window))
4430 inev.kind = SELECT_WINDOW_EVENT;
4431 inev.frame_or_window = window;
4434 last_window=window;
4436 note_mouse_movement (f, &msg.msg);
4438 else
4440 /* If we move outside the frame, then we're
4441 certainly no longer on any text in the frame. */
4442 clear_mouse_face (dpyinfo);
4445 /* If the contents of the global variable help_echo_string
4446 has changed, generate a HELP_EVENT. */
4447 #if 0 /* The below is an invalid comparison when USE_LISP_UNION_TYPE.
4448 But it was originally changed to this to fix a bug, so I have
4449 not removed it completely in case the bug is still there. */
4450 if (help_echo_string != previous_help_echo_string ||
4451 (!NILP (help_echo_string) && !STRINGP (help_echo_string) && f->mouse_moved))
4452 #else /* This is what xterm.c does. */
4453 if (!NILP (help_echo_string)
4454 || !NILP (previous_help_echo_string))
4455 do_help = 1;
4456 #endif
4457 break;
4459 case WM_LBUTTONDOWN:
4460 case WM_LBUTTONUP:
4461 case WM_MBUTTONDOWN:
4462 case WM_MBUTTONUP:
4463 case WM_RBUTTONDOWN:
4464 case WM_RBUTTONUP:
4465 case WM_XBUTTONDOWN:
4466 case WM_XBUTTONUP:
4468 /* If we decide we want to generate an event to be seen
4469 by the rest of Emacs, we put it here. */
4470 int tool_bar_p = 0;
4471 int button;
4472 int up;
4474 if (dpyinfo->grabbed && last_mouse_frame
4475 && FRAME_LIVE_P (last_mouse_frame))
4476 f = last_mouse_frame;
4477 else
4478 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4480 if (f)
4482 construct_mouse_click (&inev, &msg, f);
4484 /* Is this in the tool-bar? */
4485 if (WINDOWP (f->tool_bar_window)
4486 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
4488 Lisp_Object window;
4489 int x = XFASTINT (inev.x);
4490 int y = XFASTINT (inev.y);
4492 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
4494 if (EQ (window, f->tool_bar_window))
4496 w32_handle_tool_bar_click (f, &inev);
4497 tool_bar_p = 1;
4501 if (tool_bar_p
4502 || (dpyinfo->w32_focus_frame
4503 && f != dpyinfo->w32_focus_frame))
4504 inev.kind = NO_EVENT;
4507 parse_button (msg.msg.message, HIWORD (msg.msg.wParam),
4508 &button, &up);
4510 if (up)
4512 dpyinfo->grabbed &= ~ (1 << button);
4514 else
4516 dpyinfo->grabbed |= (1 << button);
4517 last_mouse_frame = f;
4518 /* Ignore any mouse motion that happened
4519 before this event; any subsequent mouse-movement
4520 Emacs events should reflect only motion after
4521 the ButtonPress. */
4522 if (f != 0)
4523 f->mouse_moved = 0;
4525 if (!tool_bar_p)
4526 last_tool_bar_item = -1;
4528 break;
4531 case WM_MOUSEWHEEL:
4533 if (dpyinfo->grabbed && last_mouse_frame
4534 && FRAME_LIVE_P (last_mouse_frame))
4535 f = last_mouse_frame;
4536 else
4537 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4539 if (f)
4542 if (!dpyinfo->w32_focus_frame
4543 || f == dpyinfo->w32_focus_frame)
4545 /* Emit an Emacs wheel-up/down event. */
4546 construct_mouse_wheel (&inev, &msg, f);
4548 /* Ignore any mouse motion that happened before this
4549 event; any subsequent mouse-movement Emacs events
4550 should reflect only motion after the
4551 ButtonPress. */
4552 f->mouse_moved = 0;
4554 last_mouse_frame = f;
4555 last_tool_bar_item = -1;
4557 break;
4559 case WM_DROPFILES:
4560 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4562 if (f)
4563 construct_drag_n_drop (&inev, &msg, f);
4564 break;
4566 case WM_VSCROLL:
4568 struct scroll_bar *bar =
4569 x_window_to_scroll_bar ((HWND)msg.msg.lParam);
4571 if (bar)
4572 w32_scroll_bar_handle_click (bar, &msg, &inev);
4573 break;
4576 case WM_WINDOWPOSCHANGED:
4577 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4578 if (f)
4580 if (f->want_fullscreen & FULLSCREEN_WAIT)
4581 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
4583 check_visibility = 1;
4584 break;
4586 case WM_ACTIVATE:
4587 case WM_ACTIVATEAPP:
4588 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4589 if (f)
4590 x_check_fullscreen (f);
4591 check_visibility = 1;
4592 break;
4594 case WM_MOVE:
4595 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4597 if (f && !f->async_iconified)
4599 int x, y;
4601 x_real_positions (f, &x, &y);
4602 f->left_pos = x;
4603 f->top_pos = y;
4606 check_visibility = 1;
4607 break;
4609 case WM_SHOWWINDOW:
4610 /* wParam non-zero means Window is about to be shown, 0 means
4611 about to be hidden. */
4612 /* Redo the mouse-highlight after the tooltip has gone. */
4613 if (!msg.msg.wParam && msg.msg.hwnd == tip_window)
4615 tip_window = NULL;
4616 redo_mouse_highlight ();
4619 /* If window has been obscured or exposed by another window
4620 being maximised or minimised/restored, then recheck
4621 visibility of all frames. Direct changes to our own
4622 windows get handled by WM_SIZE. */
4623 #if 0
4624 if (msg.msg.lParam != 0)
4625 check_visibility = 1;
4626 else
4628 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4629 f->async_visible = msg.msg.wParam;
4631 #endif
4633 check_visibility = 1;
4634 break;
4636 case WM_SIZE:
4637 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4639 /* Inform lisp of whether frame has been iconified etc. */
4640 if (f)
4642 switch (msg.msg.wParam)
4644 case SIZE_MINIMIZED:
4645 f->async_visible = 0;
4646 f->async_iconified = 1;
4648 inev.kind = ICONIFY_EVENT;
4649 XSETFRAME (inev.frame_or_window, f);
4650 break;
4652 case SIZE_MAXIMIZED:
4653 case SIZE_RESTORED:
4654 f->async_visible = 1;
4655 f->async_iconified = 0;
4657 /* wait_reading_process_output will notice this and update
4658 the frame's display structures. */
4659 SET_FRAME_GARBAGED (f);
4661 if (f->iconified)
4663 int x, y;
4665 /* Reset top and left positions of the Window
4666 here since Windows sends a WM_MOVE message
4667 BEFORE telling us the Window is minimized
4668 when the Window is iconified, with 3000,3000
4669 as the co-ords. */
4670 x_real_positions (f, &x, &y);
4671 f->left_pos = x;
4672 f->top_pos = y;
4674 inev.kind = DEICONIFY_EVENT;
4675 XSETFRAME (inev.frame_or_window, f);
4677 else if (! NILP (Vframe_list)
4678 && ! NILP (XCDR (Vframe_list)))
4679 /* Force a redisplay sooner or later
4680 to update the frame titles
4681 in case this is the second frame. */
4682 record_asynch_buffer_change ();
4683 break;
4687 if (f && !f->async_iconified && msg.msg.wParam != SIZE_MINIMIZED)
4689 RECT rect;
4690 int rows;
4691 int columns;
4692 int width;
4693 int height;
4695 GetClientRect (msg.msg.hwnd, &rect);
4697 height = rect.bottom - rect.top;
4698 width = rect.right - rect.left;
4700 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height);
4701 columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width);
4703 /* TODO: Clip size to the screen dimensions. */
4705 /* Even if the number of character rows and columns has
4706 not changed, the font size may have changed, so we need
4707 to check the pixel dimensions as well. */
4709 if (columns != FRAME_COLS (f)
4710 || rows != FRAME_LINES (f)
4711 || width != FRAME_PIXEL_WIDTH (f)
4712 || height != FRAME_PIXEL_HEIGHT (f))
4714 change_frame_size (f, rows, columns, 0, 1, 0);
4715 SET_FRAME_GARBAGED (f);
4716 cancel_mouse_face (f);
4717 FRAME_PIXEL_WIDTH (f) = width;
4718 FRAME_PIXEL_HEIGHT (f) = height;
4719 f->win_gravity = NorthWestGravity;
4723 check_visibility = 1;
4724 break;
4726 case WM_MOUSELEAVE:
4727 f = x_any_window_to_frame (dpyinfo, msg.msg.hwnd);
4728 if (f)
4730 if (f == dpyinfo->mouse_face_mouse_frame)
4732 /* If we move outside the frame, then we're
4733 certainly no longer on any text in the frame. */
4734 clear_mouse_face (dpyinfo);
4735 dpyinfo->mouse_face_mouse_frame = 0;
4738 /* Generate a nil HELP_EVENT to cancel a help-echo.
4739 Do it only if there's something to cancel.
4740 Otherwise, the startup message is cleared when
4741 the mouse leaves the frame. */
4742 if (any_help_event_p)
4743 do_help = -1;
4745 break;
4747 case WM_SETFOCUS:
4748 w32_detect_focus_change (dpyinfo, &msg, &inev);
4750 dpyinfo->grabbed = 0;
4751 check_visibility = 1;
4752 break;
4754 case WM_KILLFOCUS:
4755 f = x_top_window_to_frame (dpyinfo, msg.msg.hwnd);
4757 if (f)
4759 if (f == dpyinfo->w32_focus_event_frame)
4760 dpyinfo->w32_focus_event_frame = 0;
4762 if (f == dpyinfo->w32_focus_frame)
4763 x_new_focus_frame (dpyinfo, 0);
4765 if (f == dpyinfo->mouse_face_mouse_frame)
4767 /* If we move outside the frame, then we're
4768 certainly no longer on any text in the frame. */
4769 clear_mouse_face (dpyinfo);
4770 dpyinfo->mouse_face_mouse_frame = 0;
4773 /* Generate a nil HELP_EVENT to cancel a help-echo.
4774 Do it only if there's something to cancel.
4775 Otherwise, the startup message is cleared when
4776 the mouse leaves the frame. */
4777 if (any_help_event_p)
4778 do_help = -1;
4781 dpyinfo->grabbed = 0;
4782 check_visibility = 1;
4783 break;
4785 case WM_CLOSE:
4786 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4788 if (f)
4790 inev.kind = DELETE_WINDOW_EVENT;
4791 XSETFRAME (inev.frame_or_window, f);
4793 break;
4795 case WM_INITMENU:
4796 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4798 if (f)
4800 inev.kind = MENU_BAR_ACTIVATE_EVENT;
4801 XSETFRAME (inev.frame_or_window, f);
4803 break;
4805 case WM_COMMAND:
4806 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4808 if (f)
4810 extern void menubar_selection_callback
4811 (FRAME_PTR f, void * client_data);
4812 menubar_selection_callback (f, (void *)msg.msg.wParam);
4815 check_visibility = 1;
4816 break;
4818 case WM_DISPLAYCHANGE:
4819 f = x_window_to_frame (dpyinfo, msg.msg.hwnd);
4821 if (f)
4823 dpyinfo->width = (short) LOWORD (msg.msg.lParam);
4824 dpyinfo->height = (short) HIWORD (msg.msg.lParam);
4825 dpyinfo->n_cbits = msg.msg.wParam;
4826 DebPrint (("display change: %d %d\n", dpyinfo->width,
4827 dpyinfo->height));
4830 check_visibility = 1;
4831 break;
4833 default:
4834 /* Check for messages registered at runtime. */
4835 if (msg.msg.message == msh_mousewheel)
4837 /* Forward MSH_MOUSEWHEEL as WM_MOUSEWHEEL. */
4838 msg.msg.message = WM_MOUSEWHEEL;
4839 prepend_msg (&msg);
4841 break;
4844 if (inev.kind != NO_EVENT)
4846 kbd_buffer_store_event_hold (&inev, hold_quit);
4847 count++;
4850 if (do_help
4851 && !(hold_quit && hold_quit->kind != NO_EVENT))
4853 Lisp_Object frame;
4855 if (f)
4856 XSETFRAME (frame, f);
4857 else
4858 frame = Qnil;
4860 if (do_help > 0)
4862 if (NILP (help_echo_string))
4864 help_echo_object = help_echo_window = Qnil;
4865 help_echo_pos = -1;
4868 any_help_event_p = 1;
4869 gen_help_event (help_echo_string, frame, help_echo_window,
4870 help_echo_object, help_echo_pos);
4872 else
4874 help_echo_string = Qnil;
4875 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
4877 count++;
4881 /* If the focus was just given to an autoraising frame,
4882 raise it now. */
4883 /* ??? This ought to be able to handle more than one such frame. */
4884 if (pending_autoraise_frame)
4886 x_raise_frame (pending_autoraise_frame);
4887 pending_autoraise_frame = 0;
4890 /* Check which frames are still visisble, if we have enqueued any user
4891 events or been notified of events that may affect visibility. We
4892 do this here because there doesn't seem to be any direct
4893 notification from Windows that the visibility of a window has
4894 changed (at least, not in all cases). */
4895 if (count > 0 || check_visibility)
4897 Lisp_Object tail, frame;
4899 FOR_EACH_FRAME (tail, frame)
4901 FRAME_PTR f = XFRAME (frame);
4902 /* The tooltip has been drawn already. Avoid the
4903 SET_FRAME_GARBAGED below. */
4904 if (EQ (frame, tip_frame))
4905 continue;
4907 /* Check "visible" frames and mark each as obscured or not.
4908 Note that async_visible is nonzero for unobscured and
4909 obscured frames, but zero for hidden and iconified frames. */
4910 if (FRAME_W32_P (f) && f->async_visible)
4912 RECT clipbox;
4913 HDC hdc;
4915 enter_crit ();
4916 /* Query clipping rectangle for the entire window area
4917 (GetWindowDC), not just the client portion (GetDC).
4918 Otherwise, the scrollbars and menubar aren't counted as
4919 part of the visible area of the frame, and we may think
4920 the frame is obscured when really a scrollbar is still
4921 visible and gets WM_PAINT messages above. */
4922 hdc = GetWindowDC (FRAME_W32_WINDOW (f));
4923 GetClipBox (hdc, &clipbox);
4924 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
4925 leave_crit ();
4927 if (clipbox.right == clipbox.left
4928 || clipbox.bottom == clipbox.top)
4930 /* Frame has become completely obscured so mark as
4931 such (we do this by setting async_visible to 2 so
4932 that FRAME_VISIBLE_P is still true, but redisplay
4933 will skip it). */
4934 f->async_visible = 2;
4936 if (!FRAME_OBSCURED_P (f))
4938 DebPrint (("frame %p (%s) obscured\n", f,
4939 SDATA (f->name)));
4942 else
4944 /* Frame is not obscured, so mark it as such. */
4945 f->async_visible = 1;
4947 if (FRAME_OBSCURED_P (f))
4949 SET_FRAME_GARBAGED (f);
4950 DebPrint (("obscured frame %p (%s) found to be visible\n", f,
4951 SDATA (f->name)));
4953 /* Force a redisplay sooner or later. */
4954 record_asynch_buffer_change ();
4961 UNBLOCK_INPUT;
4962 return count;
4967 /***********************************************************************
4968 Text Cursor
4969 ***********************************************************************/
4971 /* Set clipping for output in glyph row ROW. W is the window in which
4972 we operate. GC is the graphics context to set clipping in.
4974 ROW may be a text row or, e.g., a mode line. Text rows must be
4975 clipped to the interior of the window dedicated to text display,
4976 mode lines must be clipped to the whole window. */
4978 static void
4979 w32_clip_to_row (w, row, area, hdc)
4980 struct window *w;
4981 struct glyph_row *row;
4982 int area;
4983 HDC hdc;
4985 struct frame *f = XFRAME (WINDOW_FRAME (w));
4986 RECT clip_rect;
4987 int window_x, window_y, window_width;
4989 window_box (w, area, &window_x, &window_y, &window_width, 0);
4991 clip_rect.left = window_x;
4992 clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4993 clip_rect.top = max (clip_rect.top, window_y);
4994 clip_rect.right = clip_rect.left + window_width;
4995 clip_rect.bottom = clip_rect.top + row->visible_height;
4997 w32_set_clip_rectangle (hdc, &clip_rect);
5001 /* Draw a hollow box cursor on window W in glyph row ROW. */
5003 static void
5004 x_draw_hollow_cursor (w, row)
5005 struct window *w;
5006 struct glyph_row *row;
5008 struct frame *f = XFRAME (WINDOW_FRAME (w));
5009 HDC hdc;
5010 RECT rect;
5011 int h;
5012 struct glyph *cursor_glyph;
5013 HBRUSH hb = CreateSolidBrush (f->output_data.w32->cursor_pixel);
5015 /* Get the glyph the cursor is on. If we can't tell because
5016 the current matrix is invalid or such, give up. */
5017 cursor_glyph = get_phys_cursor_glyph (w);
5018 if (cursor_glyph == NULL)
5019 return;
5021 /* Compute frame-relative coordinates for phys cursor. */
5022 rect.left = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5023 rect.top = get_phys_cursor_geometry (w, row, cursor_glyph, &h);
5024 rect.bottom = rect.top + h;
5025 rect.right = rect.left + w->phys_cursor_width;
5027 hdc = get_frame_dc (f);
5028 /* Set clipping, draw the rectangle, and reset clipping again. */
5029 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5030 FrameRect (hdc, &rect, hb);
5031 DeleteObject (hb);
5032 w32_set_clip_rectangle (hdc, NULL);
5033 release_frame_dc (f, hdc);
5037 /* Draw a bar cursor on window W in glyph row ROW.
5039 Implementation note: One would like to draw a bar cursor with an
5040 angle equal to the one given by the font property XA_ITALIC_ANGLE.
5041 Unfortunately, I didn't find a font yet that has this property set.
5042 --gerd. */
5044 static void
5045 x_draw_bar_cursor (w, row, width, kind)
5046 struct window *w;
5047 struct glyph_row *row;
5048 int width;
5049 enum text_cursor_kinds kind;
5051 struct frame *f = XFRAME (w->frame);
5052 struct glyph *cursor_glyph;
5053 int x;
5054 HDC hdc;
5056 /* If cursor is out of bounds, don't draw garbage. This can happen
5057 in mini-buffer windows when switching between echo area glyphs
5058 and mini-buffer. */
5059 cursor_glyph = get_phys_cursor_glyph (w);
5060 if (cursor_glyph == NULL)
5061 return;
5063 /* If on an image, draw like a normal cursor. That's usually better
5064 visible than drawing a bar, esp. if the image is large so that
5065 the bar might not be in the window. */
5066 if (cursor_glyph->type == IMAGE_GLYPH)
5068 struct glyph_row *row;
5069 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5070 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
5072 else
5074 COLORREF cursor_color = f->output_data.w32->cursor_pixel;
5075 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
5077 /* If the glyph's background equals the color we normally draw
5078 the bar cursor in, the bar cursor in its normal color is
5079 invisible. Use the glyph's foreground color instead in this
5080 case, on the assumption that the glyph's colors are chosen so
5081 that the glyph is legible. */
5082 if (face->background == cursor_color)
5083 cursor_color = face->foreground;
5085 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5087 if (width < 0)
5088 width = FRAME_CURSOR_WIDTH (f);
5089 width = min (cursor_glyph->pixel_width, width);
5091 w->phys_cursor_width = width;
5094 hdc = get_frame_dc (f);
5095 w32_clip_to_row (w, row, TEXT_AREA, hdc);
5097 if (kind == BAR_CURSOR)
5099 w32_fill_area (f, hdc, cursor_color, x,
5100 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
5101 width, row->height);
5103 else
5105 w32_fill_area (f, hdc, cursor_color, x,
5106 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
5107 row->height - width),
5108 cursor_glyph->pixel_width, width);
5111 w32_set_clip_rectangle (hdc, NULL);
5112 release_frame_dc (f, hdc);
5117 /* RIF: Define cursor CURSOR on frame F. */
5119 static void
5120 w32_define_frame_cursor (f, cursor)
5121 struct frame *f;
5122 Cursor cursor;
5124 w32_define_cursor (FRAME_W32_WINDOW (f), cursor);
5128 /* RIF: Clear area on frame F. */
5130 static void
5131 w32_clear_frame_area (f, x, y, width, height)
5132 struct frame *f;
5133 int x, y, width, height;
5135 HDC hdc;
5137 hdc = get_frame_dc (f);
5138 w32_clear_area (f, hdc, x, y, width, height);
5139 release_frame_dc (f, hdc);
5142 /* RIF: Draw or clear cursor on window W. */
5144 static void
5145 w32_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
5146 struct window *w;
5147 struct glyph_row *glyph_row;
5148 int x, y;
5149 int cursor_type, cursor_width;
5150 int on_p, active_p;
5152 if (on_p)
5154 /* If the user wants to use the system caret, make sure our own
5155 cursor remains invisible. */
5156 if (w32_use_visible_system_caret)
5158 /* Call to erase_phys_cursor here seems to use the
5159 wrong values of w->phys_cursor, as they have been
5160 overwritten before this function was called. */
5161 if (w->phys_cursor_type != NO_CURSOR)
5162 erase_phys_cursor (w);
5164 cursor_type = w->phys_cursor_type = NO_CURSOR;
5165 w->phys_cursor_width = -1;
5167 else
5169 w->phys_cursor_type = cursor_type;
5172 w->phys_cursor_on_p = 1;
5174 /* If this is the active cursor, we need to track it with the
5175 system caret, so third party software like screen magnifiers
5176 and speech synthesizers can follow the cursor. */
5177 if (active_p)
5179 struct frame *f = XFRAME (WINDOW_FRAME (w));
5180 HWND hwnd = FRAME_W32_WINDOW (f);
5182 w32_system_caret_x
5183 = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
5184 w32_system_caret_y
5185 = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
5186 + glyph_row->ascent - w->phys_cursor_ascent);
5188 /* If the size of the active cursor changed, destroy the old
5189 system caret. */
5190 if (w32_system_caret_hwnd
5191 && (w32_system_caret_height != w->phys_cursor_height))
5192 PostMessage (hwnd, WM_EMACS_DESTROY_CARET, 0, 0);
5194 w32_system_caret_height = w->phys_cursor_height;
5196 /* Move the system caret. */
5197 PostMessage (hwnd, WM_EMACS_TRACK_CARET, 0, 0);
5200 if (glyph_row->exact_window_width_line_p
5201 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
5203 glyph_row->cursor_in_fringe_p = 1;
5204 draw_fringe_bitmap (w, glyph_row, 0);
5205 return;
5208 switch (cursor_type)
5210 case HOLLOW_BOX_CURSOR:
5211 x_draw_hollow_cursor (w, glyph_row);
5212 break;
5214 case FILLED_BOX_CURSOR:
5215 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
5216 break;
5218 case BAR_CURSOR:
5219 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
5220 break;
5222 case HBAR_CURSOR:
5223 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
5224 break;
5226 case NO_CURSOR:
5227 w->phys_cursor_width = 0;
5228 break;
5230 default:
5231 abort ();
5238 /* Icons. */
5241 x_bitmap_icon (f, icon)
5242 struct frame *f;
5243 Lisp_Object icon;
5245 HANDLE hicon;
5247 if (FRAME_W32_WINDOW (f) == 0)
5248 return 1;
5250 if (NILP (icon))
5251 hicon = LoadIcon (hinst, EMACS_CLASS);
5252 else if (STRINGP (icon))
5253 hicon = LoadImage (NULL, (LPCTSTR) SDATA (icon), IMAGE_ICON, 0, 0,
5254 LR_DEFAULTSIZE | LR_LOADFROMFILE);
5255 else if (SYMBOLP (icon))
5257 LPCTSTR name;
5259 if (EQ (icon, intern ("application")))
5260 name = (LPCTSTR) IDI_APPLICATION;
5261 else if (EQ (icon, intern ("hand")))
5262 name = (LPCTSTR) IDI_HAND;
5263 else if (EQ (icon, intern ("question")))
5264 name = (LPCTSTR) IDI_QUESTION;
5265 else if (EQ (icon, intern ("exclamation")))
5266 name = (LPCTSTR) IDI_EXCLAMATION;
5267 else if (EQ (icon, intern ("asterisk")))
5268 name = (LPCTSTR) IDI_ASTERISK;
5269 else if (EQ (icon, intern ("winlogo")))
5270 name = (LPCTSTR) IDI_WINLOGO;
5271 else
5272 return 1;
5274 hicon = LoadIcon (NULL, name);
5276 else
5277 return 1;
5279 if (hicon == NULL)
5280 return 1;
5282 PostMessage (FRAME_W32_WINDOW (f), WM_SETICON, (WPARAM) ICON_BIG,
5283 (LPARAM) hicon);
5285 return 0;
5289 /************************************************************************
5290 Handling X errors
5291 ************************************************************************/
5293 /* Display Error Handling functions not used on W32. Listing them here
5294 helps diff stay in step when comparing w32term.c with xterm.c.
5296 x_error_catcher (display, error)
5297 x_catch_errors (dpy)
5298 x_catch_errors_unwind (old_val)
5299 x_check_errors (dpy, format)
5300 x_had_errors_p (dpy)
5301 x_clear_errors (dpy)
5302 x_uncatch_errors (dpy, count)
5303 x_trace_wire ()
5304 x_connection_signal (signalnum)
5305 x_connection_closed (dpy, error_message)
5306 x_error_quitter (display, error)
5307 x_error_handler (display, error)
5308 x_io_error_quitter (display)
5313 /* Changing the font of the frame. */
5315 /* Give frame F the font named FONTNAME as its default font, and
5316 return the full name of that font. FONTNAME may be a wildcard
5317 pattern; in that case, we choose some font that fits the pattern.
5318 The return value shows which font we chose. */
5320 Lisp_Object
5321 x_new_font (f, fontname)
5322 struct frame *f;
5323 register char *fontname;
5325 struct font_info *fontp
5326 = FS_LOAD_FONT (f, 0, fontname, -1);
5328 if (!fontp)
5329 return Qnil;
5331 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
5332 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
5333 FRAME_FONTSET (f) = -1;
5335 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
5336 FRAME_SPACE_WIDTH (f) = fontp->space_width;
5337 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
5339 compute_fringe_widths (f, 1);
5341 /* Compute the scroll bar width in character columns. */
5342 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
5344 int wid = FRAME_COLUMN_WIDTH (f);
5345 FRAME_CONFIG_SCROLL_BAR_COLS (f)
5346 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
5348 else
5350 int wid = FRAME_COLUMN_WIDTH (f);
5351 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
5354 /* Now make the frame display the given font. */
5355 if (FRAME_W32_WINDOW (f) != 0)
5357 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
5358 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
5361 return build_string (fontp->full_name);
5364 /* Give frame F the fontset named FONTSETNAME as its default font, and
5365 return the full name of that fontset. FONTSETNAME may be a wildcard
5366 pattern; in that case, we choose some fontset that fits the pattern.
5367 The return value shows which fontset we chose. */
5369 Lisp_Object
5370 x_new_fontset (f, fontsetname)
5371 struct frame *f;
5372 char *fontsetname;
5374 int fontset = fs_query_fontset (build_string (fontsetname), 0);
5375 Lisp_Object result;
5377 if (fontset < 0)
5378 return Qnil;
5380 if (FRAME_FONTSET (f) == fontset)
5381 /* This fontset is already set in frame F. There's nothing more
5382 to do. */
5383 return fontset_name (fontset);
5385 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
5387 if (!STRINGP (result))
5388 /* Can't load ASCII font. */
5389 return Qnil;
5391 /* Since x_new_font doesn't update any fontset information, do it now. */
5392 FRAME_FONTSET(f) = fontset;
5394 return build_string (fontsetname);
5398 /***********************************************************************
5399 TODO: W32 Input Methods
5400 ***********************************************************************/
5401 /* Listing missing functions from xterm.c helps diff stay in step.
5403 xim_destroy_callback (xim, client_data, call_data)
5404 xim_open_dpy (dpyinfo, resource_name)
5405 struct xim_inst_t
5406 xim_instantiate_callback (display, client_data, call_data)
5407 xim_initialize (dpyinfo, resource_name)
5408 xim_close_dpy (dpyinfo)
5413 /* Calculate the absolute position in frame F
5414 from its current recorded position values and gravity. */
5416 void
5417 x_calc_absolute_position (f)
5418 struct frame *f;
5420 int flags = f->size_hint_flags;
5422 /* Treat negative positions as relative to the leftmost bottommost
5423 position that fits on the screen. */
5424 if (flags & XNegative)
5425 f->left_pos = (FRAME_W32_DISPLAY_INFO (f)->width
5426 - FRAME_PIXEL_WIDTH (f)
5427 + f->left_pos);
5429 if (flags & YNegative)
5430 f->top_pos = (FRAME_W32_DISPLAY_INFO (f)->height
5431 - FRAME_PIXEL_HEIGHT (f)
5432 + f->top_pos);
5433 /* The left_pos and top_pos
5434 are now relative to the top and left screen edges,
5435 so the flags should correspond. */
5436 f->size_hint_flags &= ~ (XNegative | YNegative);
5439 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
5440 to really change the position, and 0 when calling from
5441 x_make_frame_visible (in that case, XOFF and YOFF are the current
5442 position values). It is -1 when calling from x_set_frame_parameters,
5443 which means, do adjust for borders but don't change the gravity. */
5445 void
5446 x_set_offset (f, xoff, yoff, change_gravity)
5447 struct frame *f;
5448 register int xoff, yoff;
5449 int change_gravity;
5451 int modified_top, modified_left;
5453 if (change_gravity > 0)
5455 f->top_pos = yoff;
5456 f->left_pos = xoff;
5457 f->size_hint_flags &= ~ (XNegative | YNegative);
5458 if (xoff < 0)
5459 f->size_hint_flags |= XNegative;
5460 if (yoff < 0)
5461 f->size_hint_flags |= YNegative;
5462 f->win_gravity = NorthWestGravity;
5464 x_calc_absolute_position (f);
5466 BLOCK_INPUT;
5467 x_wm_set_size_hint (f, (long) 0, 0);
5469 modified_left = f->left_pos;
5470 modified_top = f->top_pos;
5472 my_set_window_pos (FRAME_W32_WINDOW (f),
5473 NULL,
5474 modified_left, modified_top,
5475 0, 0,
5476 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
5477 UNBLOCK_INPUT;
5481 /* Check if we need to resize the frame due to a fullscreen request.
5482 If so needed, resize the frame. */
5483 static void
5484 x_check_fullscreen (f)
5485 struct frame *f;
5487 if (f->want_fullscreen & FULLSCREEN_BOTH)
5489 int width, height, ign;
5491 x_real_positions (f, &f->left_pos, &f->top_pos);
5493 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
5495 /* We do not need to move the window, it shall be taken care of
5496 when setting WM manager hints. */
5497 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
5499 change_frame_size (f, height, width, 0, 1, 0);
5500 SET_FRAME_GARBAGED (f);
5501 cancel_mouse_face (f);
5503 /* Wait for the change of frame size to occur */
5504 f->want_fullscreen |= FULLSCREEN_WAIT;
5509 /* Call this to change the size of frame F's x-window.
5510 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
5511 for this size change and subsequent size changes.
5512 Otherwise we leave the window gravity unchanged. */
5514 void
5515 x_set_window_size (f, change_gravity, cols, rows)
5516 struct frame *f;
5517 int change_gravity;
5518 int cols, rows;
5520 int pixelwidth, pixelheight;
5522 BLOCK_INPUT;
5524 check_frame_size (f, &rows, &cols);
5525 f->scroll_bar_actual_width
5526 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
5528 compute_fringe_widths (f, 0);
5530 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
5531 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
5533 f->win_gravity = NorthWestGravity;
5534 x_wm_set_size_hint (f, (long) 0, 0);
5537 RECT rect;
5539 rect.left = rect.top = 0;
5540 rect.right = pixelwidth;
5541 rect.bottom = pixelheight;
5543 AdjustWindowRect(&rect, f->output_data.w32->dwStyle,
5544 FRAME_EXTERNAL_MENU_BAR (f));
5546 my_set_window_pos (FRAME_W32_WINDOW (f),
5547 NULL,
5548 0, 0,
5549 rect.right - rect.left,
5550 rect.bottom - rect.top,
5551 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
5554 /* Now, strictly speaking, we can't be sure that this is accurate,
5555 but the window manager will get around to dealing with the size
5556 change request eventually, and we'll hear how it went when the
5557 ConfigureNotify event gets here.
5559 We could just not bother storing any of this information here,
5560 and let the ConfigureNotify event set everything up, but that
5561 might be kind of confusing to the Lisp code, since size changes
5562 wouldn't be reported in the frame parameters until some random
5563 point in the future when the ConfigureNotify event arrives.
5565 We pass 1 for DELAY since we can't run Lisp code inside of
5566 a BLOCK_INPUT. */
5567 change_frame_size (f, rows, cols, 0, 1, 0);
5568 FRAME_PIXEL_WIDTH (f) = pixelwidth;
5569 FRAME_PIXEL_HEIGHT (f) = pixelheight;
5571 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
5572 receive in the ConfigureNotify event; if we get what we asked
5573 for, then the event won't cause the screen to become garbaged, so
5574 we have to make sure to do it here. */
5575 SET_FRAME_GARBAGED (f);
5577 /* If cursor was outside the new size, mark it as off. */
5578 mark_window_cursors_off (XWINDOW (f->root_window));
5580 /* Clear out any recollection of where the mouse highlighting was,
5581 since it might be in a place that's outside the new frame size.
5582 Actually checking whether it is outside is a pain in the neck,
5583 so don't try--just let the highlighting be done afresh with new size. */
5584 cancel_mouse_face (f);
5586 UNBLOCK_INPUT;
5589 /* Mouse warping. */
5591 void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
5593 void
5594 x_set_mouse_position (f, x, y)
5595 struct frame *f;
5596 int x, y;
5598 int pix_x, pix_y;
5600 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
5601 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
5603 if (pix_x < 0) pix_x = 0;
5604 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
5606 if (pix_y < 0) pix_y = 0;
5607 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
5609 x_set_mouse_pixel_position (f, pix_x, pix_y);
5612 void
5613 x_set_mouse_pixel_position (f, pix_x, pix_y)
5614 struct frame *f;
5615 int pix_x, pix_y;
5617 RECT rect;
5618 POINT pt;
5620 BLOCK_INPUT;
5622 GetClientRect (FRAME_W32_WINDOW (f), &rect);
5623 pt.x = rect.left + pix_x;
5624 pt.y = rect.top + pix_y;
5625 ClientToScreen (FRAME_W32_WINDOW (f), &pt);
5627 SetCursorPos (pt.x, pt.y);
5629 UNBLOCK_INPUT;
5633 /* focus shifting, raising and lowering. */
5635 void
5636 x_focus_on_frame (f)
5637 struct frame *f;
5639 struct w32_display_info *dpyinfo = &one_w32_display_info;
5641 /* Give input focus to frame. */
5642 BLOCK_INPUT;
5643 #if 0
5644 /* Try not to change its Z-order if possible. */
5645 if (x_window_to_frame (dpyinfo, GetForegroundWindow ()))
5646 my_set_focus (f, FRAME_W32_WINDOW (f));
5647 else
5648 #endif
5649 my_set_foreground_window (FRAME_W32_WINDOW (f));
5650 UNBLOCK_INPUT;
5653 void
5654 x_unfocus_frame (f)
5655 struct frame *f;
5659 /* Raise frame F. */
5660 void
5661 x_raise_frame (f)
5662 struct frame *f;
5664 BLOCK_INPUT;
5666 /* Strictly speaking, raise-frame should only change the frame's Z
5667 order, leaving input focus unchanged. This is reasonable behaviour
5668 on X where the usual policy is point-to-focus. However, this
5669 behaviour would be very odd on Windows where the usual policy is
5670 click-to-focus.
5672 On X, if the mouse happens to be over the raised frame, it gets
5673 input focus anyway (so the window with focus will never be
5674 completely obscured) - if not, then just moving the mouse over it
5675 is sufficient to give it focus. On Windows, the user must actually
5676 click on the frame (preferrably the title bar so as not to move
5677 point), which is more awkward. Also, no other Windows program
5678 raises a window to the top but leaves another window (possibly now
5679 completely obscured) with input focus.
5681 Because there is a system setting on Windows that allows the user
5682 to choose the point to focus policy, we make the strict semantics
5683 optional, but by default we grab focus when raising. */
5685 if (NILP (Vw32_grab_focus_on_raise))
5687 /* The obvious call to my_set_window_pos doesn't work if Emacs is
5688 not already the foreground application: the frame is raised
5689 above all other frames belonging to us, but not above the
5690 current top window. To achieve that, we have to resort to this
5691 more cumbersome method. */
5693 HDWP handle = BeginDeferWindowPos (2);
5694 if (handle)
5696 DeferWindowPos (handle,
5697 FRAME_W32_WINDOW (f),
5698 HWND_TOP,
5699 0, 0, 0, 0,
5700 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5702 DeferWindowPos (handle,
5703 GetForegroundWindow (),
5704 FRAME_W32_WINDOW (f),
5705 0, 0, 0, 0,
5706 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5708 EndDeferWindowPos (handle);
5711 else
5713 my_set_foreground_window (FRAME_W32_WINDOW (f));
5716 UNBLOCK_INPUT;
5719 /* Lower frame F. */
5720 void
5721 x_lower_frame (f)
5722 struct frame *f;
5724 BLOCK_INPUT;
5725 my_set_window_pos (FRAME_W32_WINDOW (f),
5726 HWND_BOTTOM,
5727 0, 0, 0, 0,
5728 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
5729 UNBLOCK_INPUT;
5732 static void
5733 w32_frame_raise_lower (f, raise_flag)
5734 FRAME_PTR f;
5735 int raise_flag;
5737 if (! FRAME_W32_P (f))
5738 return;
5740 if (raise_flag)
5741 x_raise_frame (f);
5742 else
5743 x_lower_frame (f);
5746 /* Change of visibility. */
5748 /* This tries to wait until the frame is really visible.
5749 However, if the window manager asks the user where to position
5750 the frame, this will return before the user finishes doing that.
5751 The frame will not actually be visible at that time,
5752 but it will become visible later when the window manager
5753 finishes with it. */
5755 void
5756 x_make_frame_visible (f)
5757 struct frame *f;
5759 Lisp_Object type;
5761 BLOCK_INPUT;
5763 type = x_icon_type (f);
5764 if (!NILP (type))
5765 x_bitmap_icon (f, type);
5767 if (! FRAME_VISIBLE_P (f))
5769 /* We test FRAME_GARBAGED_P here to make sure we don't
5770 call x_set_offset a second time
5771 if we get to x_make_frame_visible a second time
5772 before the window gets really visible. */
5773 if (! FRAME_ICONIFIED_P (f)
5774 && ! f->output_data.w32->asked_for_visible)
5775 x_set_offset (f, f->left_pos, f->top_pos, 0);
5777 f->output_data.w32->asked_for_visible = 1;
5779 /* my_show_window (f, FRAME_W32_WINDOW (f), f->async_iconified ? SW_RESTORE : SW_SHOW); */
5780 my_show_window (f, FRAME_W32_WINDOW (f), SW_SHOWNORMAL);
5783 /* Synchronize to ensure Emacs knows the frame is visible
5784 before we do anything else. We do this loop with input not blocked
5785 so that incoming events are handled. */
5787 Lisp_Object frame;
5788 int count;
5790 /* This must come after we set COUNT. */
5791 UNBLOCK_INPUT;
5793 XSETFRAME (frame, f);
5795 /* Wait until the frame is visible. Process X events until a
5796 MapNotify event has been seen, or until we think we won't get a
5797 MapNotify at all.. */
5798 for (count = input_signal_count + 10;
5799 input_signal_count < count && !FRAME_VISIBLE_P (f);)
5801 /* Force processing of queued events. */
5802 /* TODO: x_sync equivalent? */
5804 /* Machines that do polling rather than SIGIO have been observed
5805 to go into a busy-wait here. So we'll fake an alarm signal
5806 to let the handler know that there's something to be read.
5807 We used to raise a real alarm, but it seems that the handler
5808 isn't always enabled here. This is probably a bug. */
5809 if (input_polling_used ())
5811 /* It could be confusing if a real alarm arrives while processing
5812 the fake one. Turn it off and let the handler reset it. */
5813 int old_poll_suppress_count = poll_suppress_count;
5814 poll_suppress_count = 1;
5815 poll_for_input_1 ();
5816 poll_suppress_count = old_poll_suppress_count;
5819 FRAME_SAMPLE_VISIBILITY (f);
5823 /* Change from mapped state to withdrawn state. */
5825 /* Make the frame visible (mapped and not iconified). */
5827 x_make_frame_invisible (f)
5828 struct frame *f;
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 BLOCK_INPUT;
5836 my_show_window (f, FRAME_W32_WINDOW (f), SW_HIDE);
5838 /* We can't distinguish this from iconification
5839 just by the event that we get from the server.
5840 So we can't win using the usual strategy of letting
5841 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
5842 and synchronize with the server to make sure we agree. */
5843 f->visible = 0;
5844 FRAME_ICONIFIED_P (f) = 0;
5845 f->async_visible = 0;
5846 f->async_iconified = 0;
5848 UNBLOCK_INPUT;
5851 /* Change window state from mapped to iconified. */
5853 void
5854 x_iconify_frame (f)
5855 struct frame *f;
5857 Lisp_Object type;
5859 /* Don't keep the highlight on an invisible frame. */
5860 if (FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame == f)
5861 FRAME_W32_DISPLAY_INFO (f)->x_highlight_frame = 0;
5863 if (f->async_iconified)
5864 return;
5866 BLOCK_INPUT;
5868 type = x_icon_type (f);
5869 if (!NILP (type))
5870 x_bitmap_icon (f, type);
5872 /* Simulate the user minimizing the frame. */
5873 SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MINIMIZE, 0);
5875 UNBLOCK_INPUT;
5879 /* Free X resources of frame F. */
5881 void
5882 x_free_frame_resources (f)
5883 struct frame *f;
5885 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5887 BLOCK_INPUT;
5889 if (FRAME_W32_WINDOW (f))
5890 my_destroy_window (f, FRAME_W32_WINDOW (f));
5892 free_frame_menubar (f);
5894 unload_color (f, f->output_data.x->foreground_pixel);
5895 unload_color (f, f->output_data.x->background_pixel);
5896 unload_color (f, f->output_data.w32->cursor_pixel);
5897 unload_color (f, f->output_data.w32->cursor_foreground_pixel);
5898 unload_color (f, f->output_data.w32->border_pixel);
5899 unload_color (f, f->output_data.w32->mouse_pixel);
5900 if (f->output_data.w32->white_relief.allocated_p)
5901 unload_color (f, f->output_data.w32->white_relief.pixel);
5902 if (f->output_data.w32->black_relief.allocated_p)
5903 unload_color (f, f->output_data.w32->black_relief.pixel);
5905 if (FRAME_FACE_CACHE (f))
5906 free_frame_faces (f);
5908 xfree (f->output_data.w32);
5909 f->output_data.w32 = NULL;
5911 if (f == dpyinfo->w32_focus_frame)
5912 dpyinfo->w32_focus_frame = 0;
5913 if (f == dpyinfo->w32_focus_event_frame)
5914 dpyinfo->w32_focus_event_frame = 0;
5915 if (f == dpyinfo->x_highlight_frame)
5916 dpyinfo->x_highlight_frame = 0;
5918 if (f == dpyinfo->mouse_face_mouse_frame)
5920 dpyinfo->mouse_face_beg_row
5921 = dpyinfo->mouse_face_beg_col = -1;
5922 dpyinfo->mouse_face_end_row
5923 = dpyinfo->mouse_face_end_col = -1;
5924 dpyinfo->mouse_face_window = Qnil;
5925 dpyinfo->mouse_face_deferred_gc = 0;
5926 dpyinfo->mouse_face_mouse_frame = 0;
5929 UNBLOCK_INPUT;
5933 /* Destroy the window of frame F. */
5935 x_destroy_window (f)
5936 struct frame *f;
5938 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
5940 x_free_frame_resources (f);
5942 dpyinfo->reference_count--;
5946 /* Setting window manager hints. */
5948 /* Set the normal size hints for the window manager, for frame F.
5949 FLAGS is the flags word to use--or 0 meaning preserve the flags
5950 that the window now has.
5951 If USER_POSITION is nonzero, we set the USPosition
5952 flag (this is useful when FLAGS is 0). */
5953 void
5954 x_wm_set_size_hint (f, flags, user_position)
5955 struct frame *f;
5956 long flags;
5957 int user_position;
5959 Window window = FRAME_W32_WINDOW (f);
5961 enter_crit ();
5963 SetWindowLong (window, WND_FONTWIDTH_INDEX, FRAME_COLUMN_WIDTH (f));
5964 SetWindowLong (window, WND_LINEHEIGHT_INDEX, FRAME_LINE_HEIGHT (f));
5965 SetWindowLong (window, WND_BORDER_INDEX, FRAME_INTERNAL_BORDER_WIDTH (f));
5966 SetWindowLong (window, WND_SCROLLBAR_INDEX, f->scroll_bar_actual_width);
5968 leave_crit ();
5971 /* Window manager things */
5972 void
5973 x_wm_set_icon_position (f, icon_x, icon_y)
5974 struct frame *f;
5975 int icon_x, icon_y;
5977 #if 0
5978 Window window = FRAME_W32_WINDOW (f);
5980 f->display.x->wm_hints.flags |= IconPositionHint;
5981 f->display.x->wm_hints.icon_x = icon_x;
5982 f->display.x->wm_hints.icon_y = icon_y;
5984 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->display.x->wm_hints);
5985 #endif
5989 /***********************************************************************
5990 Fonts
5991 ***********************************************************************/
5993 /* The following functions are listed here to help diff stay in step
5994 with xterm.c. See w32fns.c for definitions.
5996 x_get_font_info (f, font_idx)
5997 x_list_fonts (f, pattern, size, maxnames)
6001 #if GLYPH_DEBUG
6003 /* Check that FONT is valid on frame F. It is if it can be found in F's
6004 font table. */
6006 static void
6007 x_check_font (f, font)
6008 struct frame *f;
6009 XFontStruct *font;
6011 int i;
6012 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6014 xassert (font != NULL);
6016 for (i = 0; i < dpyinfo->n_fonts; i++)
6017 if (dpyinfo->font_table[i].name
6018 && font == dpyinfo->font_table[i].font)
6019 break;
6021 xassert (i < dpyinfo->n_fonts);
6024 #endif /* GLYPH_DEBUG != 0 */
6026 /* Set *W to the minimum width, *H to the minimum font height of FONT.
6027 Note: There are (broken) X fonts out there with invalid XFontStruct
6028 min_bounds contents. For example, handa@etl.go.jp reports that
6029 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
6030 have font->min_bounds.width == 0. */
6032 static INLINE void
6033 x_font_min_bounds (font, w, h)
6034 XFontStruct *font;
6035 int *w, *h;
6038 * TODO: Windows does not appear to offer min bound, only
6039 * average and maximum width, and maximum height.
6041 *h = FONT_HEIGHT (font);
6042 *w = FONT_WIDTH (font);
6046 /* Compute the smallest character width and smallest font height over
6047 all fonts available on frame F. Set the members smallest_char_width
6048 and smallest_font_height in F's x_display_info structure to
6049 the values computed. Value is non-zero if smallest_font_height or
6050 smallest_char_width become smaller than they were before. */
6053 x_compute_min_glyph_bounds (f)
6054 struct frame *f;
6056 int i;
6057 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
6058 XFontStruct *font;
6059 int old_width = dpyinfo->smallest_char_width;
6060 int old_height = dpyinfo->smallest_font_height;
6062 dpyinfo->smallest_font_height = 100000;
6063 dpyinfo->smallest_char_width = 100000;
6065 for (i = 0; i < dpyinfo->n_fonts; ++i)
6066 if (dpyinfo->font_table[i].name)
6068 struct font_info *fontp = dpyinfo->font_table + i;
6069 int w, h;
6071 font = (XFontStruct *) fontp->font;
6072 xassert (font != (XFontStruct *) ~0);
6073 x_font_min_bounds (font, &w, &h);
6075 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
6076 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
6079 xassert (dpyinfo->smallest_char_width > 0
6080 && dpyinfo->smallest_font_height > 0);
6082 return (dpyinfo->n_fonts == 1
6083 || dpyinfo->smallest_char_width < old_width
6084 || dpyinfo->smallest_font_height < old_height);
6087 /* The following functions are listed here to help diff stay in step
6088 with xterm.c. See w32fns.c for definitions.
6090 x_load_font (f, fontname, size)
6091 x_query_font (f, fontname)
6092 x_find_ccl_program (fontp)
6096 /***********************************************************************
6097 Initialization
6098 ***********************************************************************/
6100 static int w32_initialized = 0;
6102 void
6103 w32_initialize_display_info (display_name)
6104 Lisp_Object display_name;
6106 struct w32_display_info *dpyinfo = &one_w32_display_info;
6108 bzero (dpyinfo, sizeof (*dpyinfo));
6110 /* Put it on w32_display_name_list. */
6111 w32_display_name_list = Fcons (Fcons (display_name, Qnil),
6112 w32_display_name_list);
6113 dpyinfo->name_list_element = XCAR (w32_display_name_list);
6115 dpyinfo->w32_id_name
6116 = (char *) xmalloc (SCHARS (Vinvocation_name)
6117 + SCHARS (Vsystem_name)
6118 + 2);
6119 sprintf (dpyinfo->w32_id_name, "%s@%s",
6120 SDATA (Vinvocation_name), SDATA (Vsystem_name));
6122 /* Default Console mode values - overridden when running in GUI mode
6123 with values obtained from system metrics. */
6124 dpyinfo->resx = 1;
6125 dpyinfo->resy = 1;
6126 dpyinfo->height_in = 1;
6127 dpyinfo->width_in = 1;
6128 dpyinfo->n_planes = 1;
6129 dpyinfo->n_cbits = 4;
6130 dpyinfo->n_fonts = 0;
6131 dpyinfo->smallest_font_height = 1;
6132 dpyinfo->smallest_char_width = 1;
6134 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6135 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6136 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
6137 dpyinfo->mouse_face_window = Qnil;
6138 dpyinfo->mouse_face_overlay = Qnil;
6139 dpyinfo->mouse_face_hidden = 0;
6141 dpyinfo->vertical_scroll_bar_cursor = w32_load_cursor (IDC_ARROW);
6142 /* TODO: dpyinfo->gray */
6146 /* Create an xrdb-style database of resources to supercede registry settings.
6147 The database is just a concatenation of C strings, finished by an additional
6148 \0. The string are submitted to some basic normalization, so
6150 [ *]option[ *]:[ *]value...
6152 becomes
6154 option:value...
6156 but any whitespace following value is not removed. */
6158 static char *
6159 w32_make_rdb (xrm_option)
6160 char *xrm_option;
6162 char *buffer = xmalloc (strlen (xrm_option) + 2);
6163 char *current = buffer;
6164 char ch;
6165 int in_option = 1;
6166 int before_value = 0;
6168 do {
6169 ch = *xrm_option++;
6171 if (ch == '\n')
6173 *current++ = '\0';
6174 in_option = 1;
6175 before_value = 0;
6177 else if (ch != ' ')
6179 *current++ = ch;
6180 if (in_option && (ch == ':'))
6182 in_option = 0;
6183 before_value = 1;
6185 else if (before_value)
6187 before_value = 0;
6190 else if (!(in_option || before_value))
6192 *current++ = ch;
6194 } while (ch);
6196 *current = '\0';
6198 return buffer;
6201 struct w32_display_info *
6202 w32_term_init (display_name, xrm_option, resource_name)
6203 Lisp_Object display_name;
6204 char *xrm_option;
6205 char *resource_name;
6207 struct w32_display_info *dpyinfo;
6208 HDC hdc;
6210 BLOCK_INPUT;
6212 if (!w32_initialized)
6214 w32_initialize ();
6215 w32_initialized = 1;
6218 w32_initialize_display_info (display_name);
6220 dpyinfo = &one_w32_display_info;
6222 dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL;
6224 /* Put this display on the chain. */
6225 dpyinfo->next = x_display_list;
6226 x_display_list = dpyinfo;
6228 hdc = GetDC (GetDesktopWindow ());
6230 dpyinfo->height = GetDeviceCaps (hdc, VERTRES);
6231 dpyinfo->width = GetDeviceCaps (hdc, HORZRES);
6232 dpyinfo->root_window = GetDesktopWindow ();
6233 dpyinfo->n_planes = GetDeviceCaps (hdc, PLANES);
6234 dpyinfo->n_cbits = GetDeviceCaps (hdc, BITSPIXEL);
6235 dpyinfo->resx = GetDeviceCaps (hdc, LOGPIXELSX);
6236 dpyinfo->resy = GetDeviceCaps (hdc, LOGPIXELSY);
6237 dpyinfo->has_palette = GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE;
6238 dpyinfo->image_cache = make_image_cache ();
6239 dpyinfo->height_in = dpyinfo->height / dpyinfo->resx;
6240 dpyinfo->width_in = dpyinfo->width / dpyinfo->resy;
6241 ReleaseDC (GetDesktopWindow (), hdc);
6243 /* initialise palette with white and black */
6245 XColor color;
6246 w32_defined_color (0, "white", &color, 1);
6247 w32_defined_color (0, "black", &color, 1);
6250 /* Create Fringe Bitmaps and store them for later use.
6252 On W32, bitmaps are all unsigned short, as Windows requires
6253 bitmap data to be Word aligned. For some reason they are
6254 horizontally reflected compared to how they appear on X, so we
6255 need to bitswap and convert to unsigned shorts before creating
6256 the bitmaps. */
6257 w32_init_fringe ();
6259 #ifndef F_SETOWN_BUG
6260 #ifdef F_SETOWN
6261 #ifdef F_SETOWN_SOCK_NEG
6262 /* stdin is a socket here */
6263 fcntl (connection, F_SETOWN, -getpid ());
6264 #else /* ! defined (F_SETOWN_SOCK_NEG) */
6265 fcntl (connection, F_SETOWN, getpid ());
6266 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
6267 #endif /* ! defined (F_SETOWN) */
6268 #endif /* F_SETOWN_BUG */
6270 #ifdef SIGIO
6271 if (interrupt_input)
6272 init_sigio (connection);
6273 #endif /* ! defined (SIGIO) */
6275 UNBLOCK_INPUT;
6277 return dpyinfo;
6280 /* Get rid of display DPYINFO, assuming all frames are already gone. */
6282 void
6283 x_delete_display (dpyinfo)
6284 struct w32_display_info *dpyinfo;
6286 /* Discard this display from w32_display_name_list and w32_display_list.
6287 We can't use Fdelq because that can quit. */
6288 if (! NILP (w32_display_name_list)
6289 && EQ (XCAR (w32_display_name_list), dpyinfo->name_list_element))
6290 w32_display_name_list = XCDR (w32_display_name_list);
6291 else
6293 Lisp_Object tail;
6295 tail = w32_display_name_list;
6296 while (CONSP (tail) && CONSP (XCDR (tail)))
6298 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
6300 XSETCDR (tail, XCDR (XCDR (tail)));
6301 break;
6303 tail = XCDR (tail);
6307 /* free palette table */
6309 struct w32_palette_entry * plist;
6311 plist = dpyinfo->color_list;
6312 while (plist)
6314 struct w32_palette_entry * pentry = plist;
6315 plist = plist->next;
6316 xfree (pentry);
6318 dpyinfo->color_list = NULL;
6319 if (dpyinfo->palette)
6320 DeleteObject(dpyinfo->palette);
6322 xfree (dpyinfo->font_table);
6323 xfree (dpyinfo->w32_id_name);
6325 w32_reset_fringes ();
6328 /* Set up use of W32. */
6330 DWORD w32_msg_worker ();
6332 void
6333 x_flush (struct frame * f)
6334 { /* Nothing to do */ }
6336 extern frame_parm_handler w32_frame_parm_handlers[];
6338 static struct redisplay_interface w32_redisplay_interface =
6340 w32_frame_parm_handlers,
6341 x_produce_glyphs,
6342 x_write_glyphs,
6343 x_insert_glyphs,
6344 x_clear_end_of_line,
6345 x_scroll_run,
6346 x_after_update_window_line,
6347 x_update_window_begin,
6348 x_update_window_end,
6349 x_cursor_to,
6350 x_flush,
6351 0, /* flush_display_optional */
6352 x_clear_window_mouse_face,
6353 w32_get_glyph_overhangs,
6354 x_fix_overlapping_area,
6355 w32_draw_fringe_bitmap,
6356 w32_define_fringe_bitmap,
6357 w32_destroy_fringe_bitmap,
6358 w32_per_char_metric,
6359 w32_encode_char,
6360 NULL, /* w32_compute_glyph_string_overhangs */
6361 x_draw_glyph_string,
6362 w32_define_frame_cursor,
6363 w32_clear_frame_area,
6364 w32_draw_window_cursor,
6365 w32_draw_vertical_window_border,
6366 w32_shift_glyphs_for_insert
6369 void
6370 w32_initialize ()
6372 rif = &w32_redisplay_interface;
6374 /* MSVC does not type K&R functions with no arguments correctly, and
6375 so we must explicitly cast them. */
6376 clear_frame_hook = (void (*)(void)) x_clear_frame;
6377 ring_bell_hook = (void (*)(void)) w32_ring_bell;
6378 update_begin_hook = x_update_begin;
6379 update_end_hook = x_update_end;
6381 read_socket_hook = w32_read_socket;
6383 frame_up_to_date_hook = w32_frame_up_to_date;
6385 mouse_position_hook = w32_mouse_position;
6386 frame_rehighlight_hook = w32_frame_rehighlight;
6387 frame_raise_lower_hook = w32_frame_raise_lower;
6388 set_vertical_scroll_bar_hook = w32_set_vertical_scroll_bar;
6389 condemn_scroll_bars_hook = w32_condemn_scroll_bars;
6390 redeem_scroll_bar_hook = w32_redeem_scroll_bar;
6391 judge_scroll_bars_hook = w32_judge_scroll_bars;
6393 scroll_region_ok = 1; /* we'll scroll partial frames */
6394 char_ins_del_ok = 1;
6395 line_ins_del_ok = 1; /* we'll just blt 'em */
6396 fast_clear_end_of_line = 1; /* X does this well */
6397 memory_below_frame = 0; /* we don't remember what scrolls
6398 off the bottom */
6399 baud_rate = 19200;
6401 w32_system_caret_hwnd = NULL;
6402 w32_system_caret_height = 0;
6403 w32_system_caret_x = 0;
6404 w32_system_caret_y = 0;
6406 last_tool_bar_item = -1;
6407 any_help_event_p = 0;
6409 /* Initialize input mode: interrupt_input off, no flow control, allow
6410 8 bit character input, standard quit char. */
6411 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6413 /* Create the window thread - it will terminate itself or when the app terminates */
6415 init_crit ();
6417 dwMainThreadId = GetCurrentThreadId ();
6418 DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
6419 GetCurrentProcess (), &hMainThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
6421 /* Wait for thread to start */
6424 MSG msg;
6426 PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE);
6428 hWindowsThread = CreateThread (NULL, 0,
6429 (LPTHREAD_START_ROUTINE) w32_msg_worker,
6430 0, 0, &dwWindowsThreadId);
6432 GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
6435 /* It is desirable that mainThread should have the same notion of
6436 focus window and active window as windowsThread. Unfortunately, the
6437 following call to AttachThreadInput, which should do precisely what
6438 we need, causes major problems when Emacs is linked as a console
6439 program. Unfortunately, we have good reasons for doing that, so
6440 instead we need to send messages to windowsThread to make some API
6441 calls for us (ones that affect, or depend on, the active/focus
6442 window state. */
6443 #ifdef ATTACH_THREADS
6444 AttachThreadInput (dwMainThreadId, dwWindowsThreadId, TRUE);
6445 #endif
6447 /* Dynamically link to optional system components. */
6449 HANDLE user_lib = LoadLibrary ("user32.dll");
6451 #define LOAD_PROC(fn) pfn##fn = (void *) GetProcAddress (user_lib, #fn)
6453 /* New proportional scroll bar functions. */
6454 LOAD_PROC (SetScrollInfo);
6455 LOAD_PROC (GetScrollInfo);
6457 #undef LOAD_PROC
6459 FreeLibrary (user_lib);
6461 /* If using proportional scroll bars, ensure handle is at least 5 pixels;
6462 otherwise use the fixed height. */
6463 vertical_scroll_bar_min_handle = (pfnSetScrollInfo != NULL) ? 5 :
6464 GetSystemMetrics (SM_CYVTHUMB);
6466 /* For either kind of scroll bar, take account of the arrows; these
6467 effectively form the border of the main scroll bar range. */
6468 vertical_scroll_bar_top_border = vertical_scroll_bar_bottom_border
6469 = GetSystemMetrics (SM_CYVSCROLL);
6473 void
6474 syms_of_w32term ()
6476 staticpro (&w32_display_name_list);
6477 w32_display_name_list = Qnil;
6479 staticpro (&last_mouse_scroll_bar);
6480 last_mouse_scroll_bar = Qnil;
6482 staticpro (&Qvendor_specific_keysyms);
6483 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
6485 DEFVAR_INT ("w32-num-mouse-buttons",
6486 &w32_num_mouse_buttons,
6487 doc: /* Number of physical mouse buttons. */);
6488 w32_num_mouse_buttons = 2;
6490 DEFVAR_LISP ("w32-swap-mouse-buttons",
6491 &Vw32_swap_mouse_buttons,
6492 doc: /* Swap the mapping of middle and right mouse buttons.
6493 When nil, middle button is mouse-2 and right button is mouse-3. */);
6494 Vw32_swap_mouse_buttons = Qnil;
6496 DEFVAR_LISP ("w32-grab-focus-on-raise",
6497 &Vw32_grab_focus_on_raise,
6498 doc: /* Raised frame grabs input focus.
6499 When t, `raise-frame' grabs input focus as well. This fits well
6500 with the normal Windows click-to-focus policy, but might not be
6501 desirable when using a point-to-focus policy. */);
6502 Vw32_grab_focus_on_raise = Qt;
6504 DEFVAR_LISP ("w32-capslock-is-shiftlock",
6505 &Vw32_capslock_is_shiftlock,
6506 doc: /* Apply CapsLock state to non character input keys.
6507 When nil, CapsLock only affects normal character input keys. */);
6508 Vw32_capslock_is_shiftlock = Qnil;
6510 DEFVAR_LISP ("w32-recognize-altgr",
6511 &Vw32_recognize_altgr,
6512 doc: /* Recognize right-alt and left-ctrl as AltGr.
6513 When nil, the right-alt and left-ctrl key combination is
6514 interpreted normally. */);
6515 Vw32_recognize_altgr = Qt;
6517 DEFVAR_BOOL ("w32-enable-unicode-output",
6518 &w32_enable_unicode_output,
6519 doc: /* Enable the use of Unicode for text output if non-nil.
6520 Unicode output may prevent some third party applications for displaying
6521 Far-East Languages on Windows 95/98 from working properly.
6522 NT uses Unicode internally anyway, so this flag will probably have no
6523 affect on NT machines. */);
6524 w32_enable_unicode_output = 1;
6526 DEFVAR_BOOL ("w32-use-visible-system-caret",
6527 &w32_use_visible_system_caret,
6528 doc: /* Flag to make the system caret visible.
6529 When this is non-nil, Emacs will indicate the position of point by
6530 using the system caret instead of drawing its own cursor. Some screen
6531 reader software does not track the system cursor properly when it is
6532 invisible, and gets confused by Emacs drawing its own cursor, so this
6533 variable is initialized to t when Emacs detects that screen reader
6534 software is running as it starts up.
6536 When this variable is set, other variables affecting the appearance of
6537 the cursor have no effect. */);
6539 /* Initialize w32_use_visible_system_caret based on whether a screen
6540 reader is in use. */
6541 if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
6542 &w32_use_visible_system_caret, 0))
6543 w32_use_visible_system_caret = 0;
6545 /* We don't yet support this, but defining this here avoids whining
6546 from cus-start.el and other places, like "M-x set-variable". */
6547 DEFVAR_BOOL ("x-use-underline-position-properties",
6548 &x_use_underline_position_properties,
6549 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
6550 nil means ignore them. If you encounter fonts with bogus
6551 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6552 to 4.1, set this to nil.
6554 NOTE: Not supported on MS-Windows yet. */);
6555 x_use_underline_position_properties = 0;
6557 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
6558 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6559 Vx_toolkit_scroll_bars = Qt;
6561 staticpro (&last_mouse_motion_frame);
6562 last_mouse_motion_frame = Qnil;
6565 /* arch-tag: 5fa70624-ab86-499c-8a85-473958ee4646
6566 (do not change this comment) */