XEmacs 21.4.5 "Civil Service".
[chise/xemacs-chise.git.1] / src / scrollbar-msw.c
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>.
6
7 This file is part of XEmacs.
8
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
12 later version.
13
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
17 for more details.
18
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.  */
23
24 /* Synched up with: Not in FSF. */
25
26 #include <config.h>
27 #include "lisp.h"
28
29 #include "console-msw.h"
30 #include "events.h"
31 #include "frame.h"
32 #include "scrollbar-msw.h"
33 #include "scrollbar.h"
34 #include "specifier.h"
35 #include "window.h"
36
37 /* We use a similar sort of vertical scrollbar drag hack for mswindows
38  * scrollbars as is used for Motif or Lucid scrollbars under X.
39  * We do character-based instead of line-based scrolling, which can mean that
40  * without the hack it is impossible to drag to the end of a buffer. */
41 #define VERTICAL_SCROLLBAR_DRAG_HACK
42
43 static int vertical_drag_in_progress = 0;
44
45 static void
46 mswindows_create_scrollbar_instance (struct frame *f, int vertical,
47                                      struct scrollbar_instance *sb)
48 {
49   int orientation;
50
51   sb->scrollbar_data = xnew_and_zero (struct mswindows_scrollbar_data);
52
53   if (vertical)
54     orientation = SBS_VERT;
55   else
56     orientation = SBS_HORZ;
57
58   SCROLLBAR_MSW_HANDLE (sb) =
59     CreateWindowEx(0, "SCROLLBAR", 0, orientation|WS_CHILD,
60                  CW_USEDEFAULT, CW_USEDEFAULT,
61                  CW_USEDEFAULT, CW_USEDEFAULT,
62                  FRAME_MSWINDOWS_HANDLE (f),
63                  NULL, NULL, NULL);
64   SCROLLBAR_MSW_INFO (sb).cbSize = sizeof(SCROLLINFO);
65   SCROLLBAR_MSW_INFO (sb).fMask = SIF_ALL;
66   GetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
67                 &SCROLLBAR_MSW_INFO (sb));
68   SetWindowLong (SCROLLBAR_MSW_HANDLE(sb), GWL_USERDATA, (LONG)sb);
69
70 #if 0
71   {
72           HWND h = SCROLLBAR_MSW_HANDLE (sb);
73           int x = SetWindowLong (SCROLLBAR_MSW_HANDLE(sb), GWL_USERDATA, (LONG)sb);
74           int y = GetLastError();
75           struct scrollbar_instance *z = (struct scrollbar_instance *)GetWindowLong (SCROLLBAR_MSW_HANDLE(sb),
76                   GWL_USERDATA);
77           *z = *z;
78   }
79 #endif
80 }
81
82 static void
83 mswindows_free_scrollbar_instance (struct scrollbar_instance *sb)
84 {
85   DestroyWindow (SCROLLBAR_MSW_HANDLE (sb));
86   if (sb->scrollbar_data)
87     xfree (sb->scrollbar_data);
88 }
89
90 static void
91 unshow_that_mofo (void *handle)
92 {
93   ShowScrollBar ((HWND) handle, SB_CTL, 0);
94 }
95
96 static void
97 mswindows_release_scrollbar_instance (struct scrollbar_instance *sb)
98 {
99   if (gc_in_progress)
100     /* #### way bogus!  need to remove the offending call.
101        see mark_redisplay(). */
102     register_post_gc_action (unshow_that_mofo,
103                              (void *) SCROLLBAR_MSW_HANDLE (sb));
104   else
105     ShowScrollBar (SCROLLBAR_MSW_HANDLE (sb), SB_CTL, 0);
106   SCROLLBAR_MSW_SIZE (sb) = 0;
107 }
108
109 #define UPDATE_POS_FIELD(field)                                            \
110   if (new_##field >= 0 && SCROLLBAR_MSW_DATA (sb)->field != new_##field) { \
111     SCROLLBAR_MSW_DATA (sb)->field = new_##field;                          \
112     pos_changed = 1;                                                       \
113   }
114
115 static void
116 mswindows_update_scrollbar_instance_values (struct window *w,
117                                             struct scrollbar_instance *sb,
118                                             int new_line_increment,
119                                             int new_page_increment,
120                                             int new_minimum, int new_maximum,
121                                             int new_slider_size,
122                                             int new_slider_position,
123                                             int new_scrollbar_width,
124                                             int new_scrollbar_height,
125                                             int new_scrollbar_x,
126                                             int new_scrollbar_y)
127 {
128   int pos_changed = 0;
129   long styles = GetWindowLong (SCROLLBAR_MSW_HANDLE (sb), GWL_STYLE);
130   int vert = styles & SBS_VERT;
131
132   if (styles == 0) {
133     mswindows_output_last_error("GetWindowLong");
134     return;
135   }
136
137 #if 0
138   stderr_out ("[%d, %d], page = %d, pos = %d, inhibit = %d\n", new_minimum, new_maximum,
139               new_slider_size, new_slider_position,inhibit_slider_size_change);
140 #endif
141
142   /* These might be optimized, but since at least one will change at each
143      call, it's probably not worth it. */
144   SCROLLBAR_MSW_INFO (sb).nMin = new_minimum;
145   SCROLLBAR_MSW_INFO (sb).nMax = new_maximum;
146   SCROLLBAR_MSW_INFO (sb).nPage = new_slider_size + 1; /* +1 for DISABLENOSCROLL */
147   SCROLLBAR_MSW_INFO (sb).nPos = new_slider_position;
148 #ifndef VERTICAL_SCROLLBAR_DRAG_HACK
149   SCROLLBAR_MSW_INFO (sb).fMask = ((vert && vertical_drag_in_progress)
150                                    ? SIF_RANGE | SIF_POS
151                                    : SIF_ALL | SIF_DISABLENOSCROLL);
152 #else
153   SCROLLBAR_MSW_INFO (sb).fMask = SIF_ALL | SIF_DISABLENOSCROLL;
154
155   /* Ignore XEmacs' requests to update the thumb position and size; they don't
156    * bear any relation to reality because we're reporting made-up positions */
157   if (!(vert && vertical_drag_in_progress))
158 #endif
159     SetScrollInfo (SCROLLBAR_MSW_HANDLE (sb), SB_CTL, &SCROLLBAR_MSW_INFO (sb),
160                    TRUE);
161
162   UPDATE_POS_FIELD (scrollbar_x);
163   UPDATE_POS_FIELD (scrollbar_y);
164   UPDATE_POS_FIELD (scrollbar_width);
165   UPDATE_POS_FIELD (scrollbar_height);
166
167   if (pos_changed)
168     {
169       MoveWindow(SCROLLBAR_MSW_HANDLE (sb),
170                  new_scrollbar_x, new_scrollbar_y,
171                  new_scrollbar_width, new_scrollbar_height,
172                  TRUE);
173     }
174 }
175
176 static void
177 mswindows_update_scrollbar_instance_status (struct window *w,
178                                             int active, int size,
179                                             struct scrollbar_instance *sb)
180 {
181   if (SCROLLBAR_MSW_SIZE (sb) != size)
182     {
183       SCROLLBAR_MSW_SIZE (sb) = size;
184       ShowScrollBar (SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
185                      SCROLLBAR_MSW_SIZE (sb));
186       SCROLLBAR_MSW_INFO(sb).fMask |= SIF_DISABLENOSCROLL;
187       SetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL, &SCROLLBAR_MSW_INFO (sb), TRUE);
188     }
189 }
190
191 void
192 mswindows_handle_scrollbar_event (HWND hwnd, int code, int pos)
193 {
194   struct frame *f;
195   Lisp_Object win, frame;
196   struct scrollbar_instance *sb;
197   long styles = GetWindowLong (hwnd, GWL_STYLE);
198   int vert = styles & SBS_VERT;
199
200   if (styles == 0) {
201     mswindows_output_last_error("GetWindowLong");
202     return;
203   }
204
205   sb = (struct scrollbar_instance *)GetWindowLong (hwnd, GWL_USERDATA);
206   win = real_window ((sb==NULL) ? GetFocus() : sb->mirror, 1);
207   /* "0 as the second parameter" refers to the call to real_window above.
208      This comment was taken from Ben's 21.5 code that differs somewhat
209      from this, I don't think the 21.4 code ever had a 0 there.
210      #### we're still hitting an abort here with 0 as the second
211      parameter, although only occasionally.  It seems that sometimes we
212      receive events for scrollbars that don't exist anymore.  I assume
213      it must happen like this: The user does something that causes a
214      scrollbar to disappear (e.g. Alt-TAB, causing recomputation of
215      everything in the new frame) and then immediately uses the mouse
216      wheel, generating scrollbar events.  Both events get posted before
217      we have a chance to process them, and in processing the first, the
218      scrollbar mentioned in the second disappears. */
219   if (NILP (win))
220     return;
221   frame = XWINDOW (win)->frame;
222   f = XFRAME (frame);
223
224   /* SB_LINEDOWN == SB_CHARLEFT etc. This is the way they will
225      always be - any Windows is binary compatible backward with
226      old programs */
227
228   switch (code)
229     {
230     case SB_LINEDOWN:
231       mswindows_enqueue_misc_user_event
232         (frame, vert ? Qscrollbar_line_down : Qscrollbar_char_right, win);
233       break;
234
235     case SB_LINEUP:
236       mswindows_enqueue_misc_user_event
237         (frame, vert ? Qscrollbar_line_up : Qscrollbar_char_left, win);
238       break;
239
240     case SB_PAGEDOWN:
241       mswindows_enqueue_misc_user_event
242         (win, vert ? Qscrollbar_page_down : Qscrollbar_page_right,
243          vert ? Fcons (win, Qnil) : win);
244       break;
245
246     case SB_PAGEUP:
247       mswindows_enqueue_misc_user_event
248         (frame,
249          vert ? Qscrollbar_page_up : Qscrollbar_page_left,
250          vert ? Fcons (win, Qnil) : win);
251       break;
252
253     case SB_BOTTOM:
254       mswindows_enqueue_misc_user_event
255         (frame, vert ? Qscrollbar_to_bottom : Qscrollbar_to_right, win);
256       break;
257
258     case SB_TOP:
259       mswindows_enqueue_misc_user_event
260         (frame, vert ? Qscrollbar_to_top : Qscrollbar_to_left, win);
261       break;
262
263     case SB_THUMBTRACK:
264     case SB_THUMBPOSITION:
265       {
266         int pos;
267         SCROLLINFO scrollinfo;
268         scrollinfo.cbSize = sizeof(SCROLLINFO);
269         scrollinfo.fMask = SIF_ALL;
270         GetScrollInfo (hwnd, SB_CTL, &scrollinfo);
271         vertical_drag_in_progress = vert;
272 #ifdef VERTICAL_SCROLLBAR_DRAG_HACK
273       if (vert && (scrollinfo.nTrackPos > scrollinfo.nPos))
274         /* new buffer position =
275          *  buffer position at start of drag +
276          *   ((text remaining in buffer at start of drag) *
277          *    (amount that the thumb has been moved) /
278          *    (space that remained past end of the thumb at start of drag)) */
279         pos = (int)
280           (scrollinfo.nPos
281            + (((double)
282               (scrollinfo.nMax - scrollinfo.nPos)
283                * (scrollinfo.nTrackPos - scrollinfo.nPos))
284               / (scrollinfo.nMax - scrollinfo.nPage - scrollinfo.nPos)))
285           - 2;  /* ensure that the last line doesn't disappear off screen */
286       else
287 #endif
288         pos = scrollinfo.nTrackPos;
289       mswindows_enqueue_misc_user_event
290         (frame,
291          vert ? Qscrollbar_vertical_drag : Qscrollbar_horizontal_drag,
292          Fcons (win, make_int (pos)));
293       }
294       break;
295
296     case SB_ENDSCROLL:
297 #ifdef VERTICAL_SCROLLBAR_DRAG_HACK
298       if (vertical_drag_in_progress && sb)
299         /* User has just dropped the thumb - finally update it */
300         SetScrollInfo (SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
301                        &SCROLLBAR_MSW_INFO (sb), TRUE);
302 #endif
303       vertical_drag_in_progress = 0;
304       break;
305     }
306 }
307
308 static int
309 can_scroll (struct scrollbar_instance* scrollbar)
310 {
311   return scrollbar != NULL
312         && IsWindowVisible (SCROLLBAR_MSW_HANDLE (scrollbar))
313         && IsWindowEnabled (SCROLLBAR_MSW_HANDLE (scrollbar));
314 }
315
316 int
317 mswindows_handle_mousewheel_event (Lisp_Object frame, int keys, int delta,
318                                    POINTS where)
319 {
320   int hasVertBar, hasHorzBar;   /* Indicates presence of scroll bars */
321   unsigned wheelScrollLines = 0; /* Number of lines per wheel notch */
322   Lisp_Object win;
323   struct window_mirror *mirror;
324   POINT donde_esta;
325
326   donde_esta.x = where.x;
327   donde_esta.y = where.y;
328
329   ScreenToClient (FRAME_MSWINDOWS_HANDLE (XFRAME (frame)), &donde_esta);
330
331   /* Find the window to scroll */
332   {
333     int mene, _mene, tekel, upharsin;
334     Bufpos mens, sana;
335     Charcount in;
336     Lisp_Object corpore, sano;
337     struct window *needle_in_haystack;
338
339     // stderr_out ("donde_esta: %d %d\n", donde_esta.x, donde_esta.y);
340     pixel_to_glyph_translation (XFRAME (frame), donde_esta.x, donde_esta.y,
341                                 &mene, &_mene, &tekel, &upharsin,
342                                 &needle_in_haystack,
343                                 &mens, &sana, &in, &corpore, &sano);
344
345     if (needle_in_haystack)
346       {
347         XSETWINDOW (win, needle_in_haystack);
348         // stderr_out ("found needle\n");
349         // debug_print (win);
350       }
351     else
352       {
353         win = FRAME_SELECTED_WINDOW (XFRAME (frame));
354         needle_in_haystack = XWINDOW (win);
355       }
356
357     mirror = find_window_mirror (needle_in_haystack);
358   }
359
360   /* Check that there is something to scroll */
361   hasVertBar = can_scroll (mirror->scrollbar_vertical_instance);
362   hasHorzBar = can_scroll (mirror->scrollbar_horizontal_instance);
363   if (!hasVertBar && !hasHorzBar)
364     return FALSE;
365
366   /* No support for panning and zooming, so ignore */
367   if (keys & (MK_SHIFT | MK_CONTROL))
368     return FALSE;
369
370   /* Get the number of lines per wheel delta */
371   SystemParametersInfo (SPI_GETWHEELSCROLLLINES, 0, &wheelScrollLines, 0);
372
373   /* Calculate the amount to scroll */
374   if (wheelScrollLines == WHEEL_PAGESCROLL)
375     {
376       /* Scroll by a page */
377       Lisp_Object function;
378       if (hasVertBar)
379         function = delta > 0 ? Qscrollbar_page_up : Qscrollbar_page_down;
380       else
381         function = delta > 0 ? Qscrollbar_page_left : Qscrollbar_page_right;
382       mswindows_enqueue_misc_user_event (frame, function, Fcons (win, Qnil));
383     }
384   else /* Scroll by a number of lines */
385     {
386       /* Calc the number of lines to scroll */
387       int toScroll = MulDiv (delta, wheelScrollLines, WHEEL_DELTA);
388
389       /* Do the scroll */
390       Lisp_Object function;
391       if (hasVertBar)
392         function = delta > 0 ? Qscrollbar_line_up : Qscrollbar_line_down;
393       else
394         function = delta > 0 ? Qscrollbar_char_left : Qscrollbar_char_right;
395       if (toScroll < 0)
396         toScroll = -toScroll;
397       while (toScroll--)
398         mswindows_enqueue_misc_user_event (frame, function, win);
399     }
400
401   return TRUE;
402 }
403
404 #ifdef MEMORY_USAGE_STATS
405
406 static int
407 mswindows_compute_scrollbar_instance_usage (struct device *d,
408                                     struct scrollbar_instance *inst,
409                                     struct overhead_stats *ovstats)
410 {
411   int total = 0;
412
413   while (inst)
414     {
415       struct mswindows_scrollbar_data *data =
416         (struct mswindows_scrollbar_data *) inst->scrollbar_data;
417
418       total += malloced_storage_size (data, sizeof (*data), ovstats);
419       inst = inst->next;
420     }
421
422   return total;
423 }
424
425 #endif /* MEMORY_USAGE_STATS */
426 \f
427 /************************************************************************/
428 /*          Device-specific ghost specifiers initialization             */
429 /************************************************************************/
430
431 DEFUN ("mswindows-init-scrollbar-metrics", Fmswindows_init_scrollbar_metrics, 1, 1, 0, /*
432 */
433        (locale))
434 {
435   if (DEVICEP (locale))
436     {
437       add_spec_to_ghost_specifier (Vscrollbar_width,
438                                    make_int (GetSystemMetrics (SM_CXVSCROLL)),
439                                    locale, Qmswindows, Qnil);
440       add_spec_to_ghost_specifier (Vscrollbar_height,
441                                    make_int (GetSystemMetrics (SM_CYHSCROLL)),
442                                    locale, Qmswindows, Qnil);
443     }
444   return Qnil;
445 }
446
447 \f
448 /************************************************************************/
449 /*                            initialization                            */
450 /************************************************************************/
451
452 void
453 console_type_create_scrollbar_mswindows (void)
454 {
455   CONSOLE_HAS_METHOD (mswindows, create_scrollbar_instance);
456   CONSOLE_HAS_METHOD (mswindows, free_scrollbar_instance);
457   CONSOLE_HAS_METHOD (mswindows, release_scrollbar_instance);
458   CONSOLE_HAS_METHOD (mswindows, update_scrollbar_instance_values);
459   CONSOLE_HAS_METHOD (mswindows, update_scrollbar_instance_status);
460 /*  CONSOLE_HAS_METHOD (mswindows, scrollbar_width_changed_in_frame); */
461 #ifdef MEMORY_USAGE_STATS
462   CONSOLE_HAS_METHOD (mswindows, compute_scrollbar_instance_usage);
463 #endif
464 }
465
466 void
467 syms_of_scrollbar_mswindows(void)
468 {
469   DEFSUBR (Fmswindows_init_scrollbar_metrics);
470 }
471
472 void
473 vars_of_scrollbar_mswindows(void)
474 {
475   Fprovide (intern ("mswindows-scrollbars"));
476 }
477