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