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