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