XEmacs 21.2-b1
[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 Amdhal 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 /* This has really different semantics in Windows than in Motif.
38    There's no corresponding method; we just do not change slider
39    size while dragging. It makes the scrollbar look smother and
40    prevents some weird behavior when scrolled near the bottom */
41 static int inhibit_slider_size_change = 0;
42
43 static void
44 mswindows_create_scrollbar_instance (struct frame *f, int vertical,
45                                      struct scrollbar_instance *sb)
46 {
47   int orientation;
48   
49   sb->scrollbar_data = xnew_and_zero (struct mswindows_scrollbar_data);
50   
51   if (vertical)
52     orientation = SBS_VERT;
53   else
54     orientation = SBS_HORZ;
55   
56   SCROLLBAR_MSW_HANDLE (sb) =
57     CreateWindowEx(0, "SCROLLBAR", 0, orientation|WS_CHILD,
58                  CW_USEDEFAULT, CW_USEDEFAULT,
59                  CW_USEDEFAULT, CW_USEDEFAULT,
60                  FRAME_MSWINDOWS_HANDLE (f),
61                  NULL, NULL, NULL);
62   SCROLLBAR_MSW_INFO (sb).fMask = SIF_ALL;
63   GetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
64                 &SCROLLBAR_MSW_INFO (sb));
65   SetWindowLong (SCROLLBAR_MSW_HANDLE(sb), GWL_USERDATA, (LONG)sb);
66
67 #if 0
68   {
69           HWND h = SCROLLBAR_MSW_HANDLE (sb);
70           int x = SetWindowLong (SCROLLBAR_MSW_HANDLE(sb), GWL_USERDATA, (LONG)sb);
71           int y = GetLastError();
72           struct scrollbar_instance *z = (struct scrollbar_instance *)GetWindowLong (SCROLLBAR_MSW_HANDLE(sb),
73                   GWL_USERDATA);
74           *z = *z;
75   }
76 #endif
77 }
78
79 static void
80 mswindows_free_scrollbar_instance (struct scrollbar_instance *sb)
81 {
82   DestroyWindow (SCROLLBAR_MSW_HANDLE (sb));
83   if (sb->scrollbar_data) 
84     xfree (sb->scrollbar_data);
85 }
86
87 static void
88 mswindows_release_scrollbar_instance (struct scrollbar_instance *sb)
89 {
90   ShowScrollBar (SCROLLBAR_MSW_HANDLE (sb), SB_CTL, 0);
91   SCROLLBAR_MSW_SIZE (sb) = 0;
92 }
93
94 #define UPDATE_POS_FIELD(field)                                            \
95   if (new_##field >= 0 && SCROLLBAR_MSW_DATA (sb)->field != new_##field) { \
96     SCROLLBAR_MSW_DATA (sb)->field = new_##field;                          \
97     pos_changed = 1;                                                       \
98   }
99
100 static void
101 mswindows_update_scrollbar_instance_values (struct window *w,
102                                             struct scrollbar_instance *sb,
103                                             int new_line_increment,
104                                             int new_page_increment,
105                                             int new_minimum, int new_maximum,
106                                             int new_slider_size,
107                                             int new_slider_position,
108                                             int new_scrollbar_width,
109                                             int new_scrollbar_height,
110                                             int new_scrollbar_x,
111                                             int new_scrollbar_y)
112 {
113   struct frame *f;
114   int pos_changed = 0;
115
116   f = XFRAME (w->frame);
117
118 #if 0
119   stderr_out ("[%d, %d], page = %d, pos = %d, inhibit = %d\n", new_minimum, new_maximum,
120               new_slider_size, new_slider_position,inhibit_slider_size_change);
121 #endif
122
123   /* These might be optimized, but since at least one will change at each
124      call, it's probably not worth it. */
125   SCROLLBAR_MSW_INFO (sb).cbSize = sizeof(SCROLLINFO);
126   SCROLLBAR_MSW_INFO (sb).nMin = new_minimum;
127   SCROLLBAR_MSW_INFO (sb).nMax = new_maximum;
128   SCROLLBAR_MSW_INFO (sb).nPage = new_slider_size + 1; /* for DISABLENOSCROLL */
129   SCROLLBAR_MSW_INFO (sb).nPos = new_slider_position;
130   SCROLLBAR_MSW_INFO (sb).fMask = (inhibit_slider_size_change 
131                                    ? SIF_RANGE | SIF_POS
132                                    : SIF_ALL | SIF_DISABLENOSCROLL);
133   
134   SetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL, &SCROLLBAR_MSW_INFO (sb),
135                 !pos_changed);
136
137   UPDATE_POS_FIELD (scrollbar_x);
138   UPDATE_POS_FIELD (scrollbar_y);
139   UPDATE_POS_FIELD (scrollbar_width);
140   UPDATE_POS_FIELD (scrollbar_height);
141
142   if (pos_changed) 
143     {
144       MoveWindow(SCROLLBAR_MSW_HANDLE (sb),
145                  new_scrollbar_x, new_scrollbar_y,
146                  new_scrollbar_width, new_scrollbar_height,
147                  TRUE);
148     }
149 }
150
151 static void
152 mswindows_update_scrollbar_instance_status (struct window *w,
153                                             int active, int size,
154                                             struct scrollbar_instance *sb)
155 {
156   if (SCROLLBAR_MSW_SIZE (sb) != size)
157     {
158       SCROLLBAR_MSW_SIZE (sb) = size;
159       ShowScrollBar (SCROLLBAR_MSW_HANDLE (sb), SB_CTL,
160                      SCROLLBAR_MSW_SIZE (sb));
161       SCROLLBAR_MSW_INFO(sb).fMask |= SIF_DISABLENOSCROLL;
162       SetScrollInfo(SCROLLBAR_MSW_HANDLE (sb), SB_CTL, &SCROLLBAR_MSW_INFO (sb), TRUE);
163     }
164 }
165
166 void
167 mswindows_handle_scrollbar_event (HWND hwnd, int code, int pos)
168 {
169   struct frame *f;
170   Lisp_Object win, frame;
171   struct scrollbar_instance *sb;
172   SCROLLINFO scrollinfo;
173   int vert = GetWindowLong (hwnd, GWL_STYLE) & SBS_VERT;
174
175   sb = (struct scrollbar_instance *)GetWindowLong (hwnd, GWL_USERDATA);
176   win = real_window (sb->mirror, 1);
177   frame = XWINDOW (win)->frame;
178   f = XFRAME (frame);
179
180   inhibit_slider_size_change = code == SB_THUMBTRACK;
181
182   /* SB_LINEDOWN == SB_CHARLEFT etc. This is the way they will
183      always be - any Windows is binary compatible backward with 
184      old programs */
185
186   switch (code)
187     {
188     case SB_LINEDOWN:
189       mswindows_enqueue_misc_user_event
190         (frame, vert ? Qscrollbar_line_down : Qscrollbar_char_right, win);
191       break;
192           
193     case SB_LINEUP:
194       mswindows_enqueue_misc_user_event
195         (frame, vert ? Qscrollbar_line_up : Qscrollbar_char_left, win);
196       break;
197           
198     case SB_PAGEDOWN:
199       mswindows_enqueue_misc_user_event
200         (win, vert ? Qscrollbar_page_down : Qscrollbar_page_right,
201          vert ? Fcons (win, Qnil) : win);
202       break;
203
204     case SB_PAGEUP:
205       mswindows_enqueue_misc_user_event
206         (frame,
207          vert ? Qscrollbar_page_up : Qscrollbar_page_left,
208          vert ? Fcons (win, Qnil) : win);
209       break;
210           
211     case SB_BOTTOM:
212       mswindows_enqueue_misc_user_event
213         (frame, vert ? Qscrollbar_to_bottom : Qscrollbar_to_right, win);
214       break;
215
216     case SB_TOP:
217       mswindows_enqueue_misc_user_event
218         (frame, vert ? Qscrollbar_to_top : Qscrollbar_to_left, win);
219       break;
220
221     case SB_THUMBTRACK:
222     case SB_THUMBPOSITION:
223       scrollinfo.cbSize = sizeof(SCROLLINFO);
224       scrollinfo.fMask = SIF_TRACKPOS;
225       GetScrollInfo (hwnd, SB_CTL, &scrollinfo);
226       mswindows_enqueue_misc_user_event
227         (frame,
228          vert ? Qscrollbar_vertical_drag : Qscrollbar_horizontal_drag,
229          Fcons (win, make_int (scrollinfo.nTrackPos)));
230       break;
231     }
232 }
233
234 #ifdef MEMORY_USAGE_STATS
235
236 static int
237 mswindows_compute_scrollbar_instance_usage (struct device *d,
238                                     struct scrollbar_instance *inst,
239                                     struct overhead_stats *ovstats)
240 {
241   int total = 0;
242
243   while (inst)
244     {
245       struct mswindows_scrollbar_data *data =
246         (struct mswindows_scrollbar_data *) inst->scrollbar_data;
247
248       total += malloced_storage_size (data, sizeof (*data), ovstats);
249       inst = inst->next;
250     }
251
252   return total;
253 }
254
255 #endif /* MEMORY_USAGE_STATS */
256 \f
257 /************************************************************************/
258 /*          Device-specific ghost specifiers initialization             */
259 /************************************************************************/
260
261 DEFUN ("mswindows-init-scrollbar-metrics", Fmswindows_init_scrollbar_metrics, 1, 1, 0, /*
262 */
263        (locale))
264 {
265   if (DEVICEP (locale))
266     {
267       add_spec_to_ghost_specifier (Vscrollbar_width,
268                                    make_int (GetSystemMetrics (SM_CXVSCROLL)),
269                                    locale, Qmswindows, Qnil);
270       add_spec_to_ghost_specifier (Vscrollbar_height,
271                                    make_int (GetSystemMetrics (SM_CYHSCROLL)),
272                                    locale, Qmswindows, Qnil);
273     }
274   return Qnil;
275 }
276
277 \f
278 /************************************************************************/
279 /*                            initialization                            */
280 /************************************************************************/
281
282 void
283 console_type_create_scrollbar_mswindows (void)
284 {
285   CONSOLE_HAS_METHOD (mswindows, create_scrollbar_instance);
286   CONSOLE_HAS_METHOD (mswindows, free_scrollbar_instance);
287   CONSOLE_HAS_METHOD (mswindows, release_scrollbar_instance);
288   CONSOLE_HAS_METHOD (mswindows, update_scrollbar_instance_values);
289   CONSOLE_HAS_METHOD (mswindows, update_scrollbar_instance_status);
290 /*  CONSOLE_HAS_METHOD (mswindows, scrollbar_width_changed_in_frame); */
291 #ifdef MEMORY_USAGE_STATS
292   CONSOLE_HAS_METHOD (mswindows, compute_scrollbar_instance_usage);
293 #endif
294 }
295
296 void
297 syms_of_scrollbar_mswindows(void)
298 {
299   DEFSUBR (Fmswindows_init_scrollbar_metrics);
300 }
301
302 void
303 vars_of_scrollbar_mswindows(void)
304 {
305   Fprovide (intern ("mswindows-scrollbars"));
306 }
307