1 /* scrollbar implementation -- mswindows interface.
2 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
3 Copyright (C) 1994 Amdahl Corporation.
4 Copyright (C) 1995 Sun Microsystems, Inc.
5 Copyright (C) 1995 Darrell Kindred <dkindred+@cmu.edu>.
7 This file is part of XEmacs.
9 XEmacs is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with XEmacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Synched up with: Not in FSF. */
30 #include "console-msw.h"
33 #include "scrollbar-msw.h"
34 #include "scrollbar.h"
35 #include "specifier.h"
38 /* We use a similar sort of vertical scrollbar drag hack for mswindows
39 * scrollbars as is used for Motif or Lucid scrollbars under X.
40 * We do character-based instead of line-based scrolling, which can mean that
41 * without the hack it is impossible to drag to the end of a buffer. */
42 #define VERTICAL_SCROLLBAR_DRAG_HACK
44 static int vertical_drag_in_progress = 0;
47 mswindows_create_scrollbar_instance (struct frame *f, int vertical,
48 struct scrollbar_instance *sb)
52 sb->scrollbar_data = xnew_and_zero (struct mswindows_scrollbar_data);
55 orientation = SBS_VERT;
57 orientation = SBS_HORZ;
59 SCROLLBAR_MSW_HANDLE (sb) =
60 CreateWindowEx(0, "SCROLLBAR", 0, orientation|WS_CHILD,
61 CW_USEDEFAULT, CW_USEDEFAULT,
62 CW_USEDEFAULT, CW_USEDEFAULT,
63 FRAME_MSWINDOWS_HANDLE (f),
65 SCROLLBAR_MSW_INFO (sb).cbSize = sizeof(SCROLLINFO);
66 SCROLLBAR_MSW_INFO (sb).fMask = SIF_ALL;
67 GetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
68 &SCROLLBAR_MSW_INFO (sb));
69 SetWindowLong (SCROLLBAR_MSW_HANDLE(sb), GWL_USERDATA, (LONG)sb);
73 HWND h = SCROLLBAR_MSW_HANDLE (sb);
74 int x = SetWindowLong (SCROLLBAR_MSW_HANDLE(sb), GWL_USERDATA, (LONG)sb);
75 int y = GetLastError();
76 struct scrollbar_instance *z = (struct scrollbar_instance *)GetWindowLong (SCROLLBAR_MSW_HANDLE(sb),
84 mswindows_free_scrollbar_instance (struct scrollbar_instance *sb)
86 DestroyWindow (SCROLLBAR_MSW_HANDLE (sb));
87 if (sb->scrollbar_data)
88 xfree (sb->scrollbar_data);
92 mswindows_release_scrollbar_instance (struct scrollbar_instance *sb)
94 ShowScrollBar (SCROLLBAR_MSW_HANDLE (sb), SB_CTL, 0);
95 SCROLLBAR_MSW_SIZE (sb) = 0;
98 #define UPDATE_POS_FIELD(field) \
99 if (new_##field >= 0 && SCROLLBAR_MSW_DATA (sb)->field != new_##field) { \
100 SCROLLBAR_MSW_DATA (sb)->field = new_##field; \
105 mswindows_update_scrollbar_instance_values (struct window *w,
106 struct scrollbar_instance *sb,
107 int new_line_increment,
108 int new_page_increment,
109 int new_minimum, int new_maximum,
111 int new_slider_position,
112 int new_scrollbar_width,
113 int new_scrollbar_height,
118 int vert = GetWindowLong (SCROLLBAR_MSW_HANDLE (sb), GWL_STYLE) & SBS_VERT;
121 stderr_out ("[%d, %d], page = %d, pos = %d, inhibit = %d\n", new_minimum, new_maximum,
122 new_slider_size, new_slider_position,inhibit_slider_size_change);
125 /* These might be optimized, but since at least one will change at each
126 call, it's probably not worth it. */
127 SCROLLBAR_MSW_INFO (sb).nMin = new_minimum;
128 SCROLLBAR_MSW_INFO (sb).nMax = new_maximum;
129 SCROLLBAR_MSW_INFO (sb).nPage = new_slider_size + 1; /* +1 for DISABLENOSCROLL */
130 SCROLLBAR_MSW_INFO (sb).nPos = new_slider_position;
131 #ifndef VERTICAL_SCROLLBAR_DRAG_HACK
132 SCROLLBAR_MSW_INFO (sb).fMask = ((vert && vertical_drag_in_progress)
133 ? SIF_RANGE | SIF_POS
134 : SIF_ALL | SIF_DISABLENOSCROLL);
136 SCROLLBAR_MSW_INFO (sb).fMask = SIF_ALL | SIF_DISABLENOSCROLL;
138 /* Ignore XEmacs' requests to update the thumb position and size; they don't
139 * bear any relation to reality because we're reporting made-up positions */
140 if (!(vert && vertical_drag_in_progress))
142 SetScrollInfo (SCROLLBAR_MSW_HANDLE (sb), SB_CTL, &SCROLLBAR_MSW_INFO (sb),
145 UPDATE_POS_FIELD (scrollbar_x);
146 UPDATE_POS_FIELD (scrollbar_y);
147 UPDATE_POS_FIELD (scrollbar_width);
148 UPDATE_POS_FIELD (scrollbar_height);
152 MoveWindow(SCROLLBAR_MSW_HANDLE (sb),
153 new_scrollbar_x, new_scrollbar_y,
154 new_scrollbar_width, new_scrollbar_height,
160 mswindows_update_scrollbar_instance_status (struct window *w,
161 int active, int size,
162 struct scrollbar_instance *sb)
164 if (SCROLLBAR_MSW_SIZE (sb) != size)
166 SCROLLBAR_MSW_SIZE (sb) = size;
167 ShowScrollBar (SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
168 SCROLLBAR_MSW_SIZE (sb));
169 SCROLLBAR_MSW_INFO(sb).fMask |= SIF_DISABLENOSCROLL;
170 SetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL, &SCROLLBAR_MSW_INFO (sb), TRUE);
175 mswindows_handle_scrollbar_event (HWND hwnd, int code, int pos)
178 Lisp_Object win, frame;
179 struct scrollbar_instance *sb;
180 SCROLLINFO scrollinfo;
181 int vert = GetWindowLong (hwnd, GWL_STYLE) & SBS_VERT;
184 sb = (struct scrollbar_instance *)GetWindowLong (hwnd, GWL_USERDATA);
185 win = real_window (sb->mirror, 1);
186 frame = XWINDOW (win)->frame;
189 /* SB_LINEDOWN == SB_CHARLEFT etc. This is the way they will
190 always be - any Windows is binary compatible backward with
196 mswindows_enqueue_misc_user_event
197 (frame, vert ? Qscrollbar_line_down : Qscrollbar_char_right, win);
201 mswindows_enqueue_misc_user_event
202 (frame, vert ? Qscrollbar_line_up : Qscrollbar_char_left, win);
206 mswindows_enqueue_misc_user_event
207 (win, vert ? Qscrollbar_page_down : Qscrollbar_page_right,
208 vert ? Fcons (win, Qnil) : win);
212 mswindows_enqueue_misc_user_event
214 vert ? Qscrollbar_page_up : Qscrollbar_page_left,
215 vert ? Fcons (win, Qnil) : win);
219 mswindows_enqueue_misc_user_event
220 (frame, vert ? Qscrollbar_to_bottom : Qscrollbar_to_right, win);
224 mswindows_enqueue_misc_user_event
225 (frame, vert ? Qscrollbar_to_top : Qscrollbar_to_left, win);
229 case SB_THUMBPOSITION:
230 scrollinfo.cbSize = sizeof(SCROLLINFO);
231 scrollinfo.fMask = SIF_ALL;
232 GetScrollInfo (hwnd, SB_CTL, &scrollinfo);
233 vertical_drag_in_progress = vert;
234 #ifdef VERTICAL_SCROLLBAR_DRAG_HACK
235 if (vert && (scrollinfo.nTrackPos > scrollinfo.nPos))
236 /* new buffer position =
237 * buffer position at start of drag +
238 * ((text remaining in buffer at start of drag) *
239 * (amount that the thumb has been moved) /
240 * (space that remained past end of the thumb at start of drag)) */
244 (scrollinfo.nMax - scrollinfo.nPos)
245 * (scrollinfo.nTrackPos - scrollinfo.nPos))
246 / (scrollinfo.nMax - scrollinfo.nPage - scrollinfo.nPos)))
247 - 2; /* ensure that the last line doesn't disappear off screen */
250 value = scrollinfo.nTrackPos;
251 mswindows_enqueue_misc_user_event
253 vert ? Qscrollbar_vertical_drag : Qscrollbar_horizontal_drag,
254 Fcons (win, make_int (value)));
258 #ifdef VERTICAL_SCROLLBAR_DRAG_HACK
259 if (vertical_drag_in_progress)
260 /* User has just dropped the thumb - finally update it */
261 SetScrollInfo (SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
262 &SCROLLBAR_MSW_INFO (sb), TRUE);
264 vertical_drag_in_progress = 0;
270 can_scroll(struct scrollbar_instance* scrollbar)
272 return scrollbar != NULL
273 && IsWindowVisible (SCROLLBAR_MSW_HANDLE (scrollbar))
274 && IsWindowEnabled (SCROLLBAR_MSW_HANDLE (scrollbar));
278 mswindows_handle_mousewheel_event (Lisp_Object frame, int keys, int delta)
280 int hasVertBar, hasHorzBar; /* Indicates prescence of scroll bars */
281 unsigned wheelScrollLines = 0; /* Number of lines per wheel notch */
283 /* Find the currently selected window */
284 Lisp_Object win = FRAME_SELECTED_WINDOW (XFRAME (frame));
285 struct window* w = XWINDOW (win);
286 struct window_mirror* mirror = find_window_mirror (w);
288 /* Check that there is something to scroll */
289 hasVertBar = can_scroll (mirror->scrollbar_vertical_instance);
290 hasHorzBar = can_scroll (mirror->scrollbar_horizontal_instance);
291 if (!hasVertBar && !hasHorzBar)
294 /* No support for panning and zooming, so ignore */
295 if (keys & (MK_SHIFT | MK_CONTROL))
298 /* Get the number of lines per wheel delta */
299 SystemParametersInfo (SPI_GETWHEELSCROLLLINES, 0, &wheelScrollLines, 0);
301 /* Calculate the amount to scroll */
302 if (wheelScrollLines == WHEEL_PAGESCROLL)
304 /* Scroll by a page */
305 Lisp_Object function;
307 function = delta > 0 ? Qscrollbar_page_up : Qscrollbar_page_down;
309 function = delta > 0 ? Qscrollbar_page_left : Qscrollbar_page_right;
310 mswindows_enqueue_misc_user_event (frame, function, Fcons (win, Qnil));
312 else /* Scroll by a number of lines */
314 /* Calc the number of lines to scroll */
315 int toScroll = MulDiv (delta, wheelScrollLines, WHEEL_DELTA);
318 Lisp_Object function;
320 function = delta > 0 ? Qscrollbar_line_up : Qscrollbar_line_down;
322 function = delta > 0 ? Qscrollbar_char_left : Qscrollbar_char_right;
324 toScroll = -toScroll;
326 mswindows_enqueue_misc_user_event (frame, function, win);
332 #ifdef MEMORY_USAGE_STATS
335 mswindows_compute_scrollbar_instance_usage (struct device *d,
336 struct scrollbar_instance *inst,
337 struct overhead_stats *ovstats)
343 struct mswindows_scrollbar_data *data =
344 (struct mswindows_scrollbar_data *) inst->scrollbar_data;
346 total += malloced_storage_size (data, sizeof (*data), ovstats);
353 #endif /* MEMORY_USAGE_STATS */
355 /************************************************************************/
356 /* Device-specific ghost specifiers initialization */
357 /************************************************************************/
359 DEFUN ("mswindows-init-scrollbar-metrics", Fmswindows_init_scrollbar_metrics, 1, 1, 0, /*
363 if (DEVICEP (locale))
365 add_spec_to_ghost_specifier (Vscrollbar_width,
366 make_int (GetSystemMetrics (SM_CXVSCROLL)),
367 locale, Qmswindows, Qnil);
368 add_spec_to_ghost_specifier (Vscrollbar_height,
369 make_int (GetSystemMetrics (SM_CYHSCROLL)),
370 locale, Qmswindows, Qnil);
376 /************************************************************************/
378 /************************************************************************/
381 console_type_create_scrollbar_mswindows (void)
383 CONSOLE_HAS_METHOD (mswindows, create_scrollbar_instance);
384 CONSOLE_HAS_METHOD (mswindows, free_scrollbar_instance);
385 CONSOLE_HAS_METHOD (mswindows, release_scrollbar_instance);
386 CONSOLE_HAS_METHOD (mswindows, update_scrollbar_instance_values);
387 CONSOLE_HAS_METHOD (mswindows, update_scrollbar_instance_status);
388 /* CONSOLE_HAS_METHOD (mswindows, scrollbar_width_changed_in_frame); */
389 #ifdef MEMORY_USAGE_STATS
390 CONSOLE_HAS_METHOD (mswindows, compute_scrollbar_instance_usage);
395 syms_of_scrollbar_mswindows(void)
397 DEFSUBR (Fmswindows_init_scrollbar_metrics);
401 vars_of_scrollbar_mswindows(void)
403 Fprovide (intern ("mswindows-scrollbars"));