]> Pileus Git - ~andy/gtk/blob - gtk/gtkhscale.c
call the base class init fucntions from all parent types upon class
[~andy/gtk] / gtk / gtkhscale.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <stdio.h>
20 #include "gtkhscale.h"
21 #include "gtksignal.h"
22 #include "gdk/gdkkeysyms.h"
23
24
25 #define SCALE_CLASS(w)  GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
26 #define RANGE_CLASS(w)  GTK_RANGE_CLASS (GTK_OBJECT (w)->klass)
27
28
29 static void gtk_hscale_class_init    (GtkHScaleClass *klass);
30 static void gtk_hscale_init          (GtkHScale      *hscale);
31 static void gtk_hscale_realize       (GtkWidget      *widget);
32 static void gtk_hscale_size_request  (GtkWidget      *widget,
33                                       GtkRequisition *requisition);
34 static void gtk_hscale_size_allocate (GtkWidget      *widget,
35                                       GtkAllocation  *allocation);
36 static void gtk_hscale_pos_trough    (GtkHScale      *hscale,
37                                       gint           *x,
38                                       gint           *y,
39                                       gint           *w,
40                                       gint           *h);
41 static void gtk_hscale_draw_slider   (GtkRange       *range);
42 static void gtk_hscale_draw_value    (GtkScale       *scale);
43 static gint gtk_hscale_trough_keys   (GtkRange *range,
44                                       GdkEventKey *key,
45                                       GtkScrollType *scroll,
46                                       GtkTroughType *pos);
47
48 guint
49 gtk_hscale_get_type (void)
50 {
51   static guint hscale_type = 0;
52
53   if (!hscale_type)
54     {
55       GtkTypeInfo hscale_info =
56       {
57         "GtkHScale",
58         sizeof (GtkHScale),
59         sizeof (GtkHScaleClass),
60         (GtkClassInitFunc) gtk_hscale_class_init,
61         (GtkObjectInitFunc) gtk_hscale_init,
62         /* reversed_1 */ NULL,
63         /* reversed_2 */ NULL,
64         (GtkClassInitFunc) NULL,
65       };
66
67       hscale_type = gtk_type_unique (gtk_scale_get_type (), &hscale_info);
68     }
69
70   return hscale_type;
71 }
72
73 static void
74 gtk_hscale_class_init (GtkHScaleClass *class)
75 {
76   GtkWidgetClass *widget_class;
77   GtkRangeClass *range_class;
78   GtkScaleClass *scale_class;
79
80   widget_class = (GtkWidgetClass*) class;
81   range_class = (GtkRangeClass*) class;
82   scale_class = (GtkScaleClass*) class;
83
84   widget_class->realize = gtk_hscale_realize;
85   widget_class->size_request = gtk_hscale_size_request;
86   widget_class->size_allocate = gtk_hscale_size_allocate;
87
88   range_class->slider_update = gtk_range_default_hslider_update;
89   range_class->trough_click = gtk_range_default_htrough_click;
90   range_class->motion = gtk_range_default_hmotion;
91   range_class->draw_slider = gtk_hscale_draw_slider;
92   range_class->trough_keys = gtk_hscale_trough_keys;
93
94   scale_class->draw_value = gtk_hscale_draw_value;
95 }
96
97 static void
98 gtk_hscale_init (GtkHScale *hscale)
99 {
100 }
101
102 GtkWidget*
103 gtk_hscale_new (GtkAdjustment *adjustment)
104 {
105   GtkHScale *hscale;
106
107   hscale = gtk_type_new (gtk_hscale_get_type ());
108
109   if (!adjustment)
110     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
111
112   gtk_range_set_adjustment (GTK_RANGE (hscale), adjustment);
113
114   return GTK_WIDGET (hscale);
115 }
116
117
118 static void
119 gtk_hscale_realize (GtkWidget *widget)
120 {
121   GtkRange *range;
122   GdkWindowAttr attributes;
123   gint attributes_mask;
124   gint x, y, w, h;
125
126   g_return_if_fail (widget != NULL);
127   g_return_if_fail (GTK_IS_HSCALE (widget));
128
129   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
130   range = GTK_RANGE (widget);
131
132   attributes.x = widget->allocation.x;
133   attributes.y = widget->allocation.y;
134   attributes.width = widget->allocation.width;
135   attributes.height = widget->allocation.height;
136   attributes.wclass = GDK_INPUT_OUTPUT;
137   attributes.window_type = GDK_WINDOW_CHILD;
138   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
139   attributes.visual = gtk_widget_get_visual (widget);
140   attributes.colormap = gtk_widget_get_colormap (widget);
141
142   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
143   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
144
145   gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &w, &h);
146   attributes.x = x;
147   attributes.y = y;
148   attributes.width = w;
149   attributes.height = h;
150   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
151                             GDK_BUTTON_RELEASE_MASK |
152                             GDK_ENTER_NOTIFY_MASK |
153                             GDK_LEAVE_NOTIFY_MASK);
154
155   range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
156
157   attributes.width = SCALE_CLASS (range)->slider_length;
158   attributes.height = RANGE_CLASS (range)->slider_width;
159   attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
160                             GDK_POINTER_MOTION_HINT_MASK);
161
162   range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
163
164   widget->style = gtk_style_attach (widget->style, widget->window);
165
166   gdk_window_set_user_data (widget->window, widget);
167   gdk_window_set_user_data (range->trough, widget);
168   gdk_window_set_user_data (range->slider, widget);
169
170   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
171   gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
172   gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
173
174   gtk_range_slider_update (GTK_RANGE (widget));
175
176   gdk_window_show (range->slider);
177   gdk_window_show (range->trough);
178 }
179
180 static void
181 gtk_hscale_size_request (GtkWidget      *widget,
182                          GtkRequisition *requisition)
183 {
184   GtkScale *scale;
185   gint value_width;
186
187   g_return_if_fail (widget != NULL);
188   g_return_if_fail (GTK_IS_HSCALE (widget));
189   g_return_if_fail (requisition != NULL);
190
191   scale = GTK_SCALE (widget);
192
193   requisition->width = (SCALE_CLASS (scale)->slider_length +
194                         widget->style->klass->xthickness) * 2;
195   requisition->height = (RANGE_CLASS (scale)->slider_width +
196                          widget->style->klass->ythickness * 2);
197
198   if (scale->draw_value)
199     {
200       value_width = gtk_scale_value_width (scale);
201
202       if ((scale->value_pos == GTK_POS_LEFT) ||
203           (scale->value_pos == GTK_POS_RIGHT))
204         {
205           requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
206           if (requisition->height < (widget->style->font->ascent + widget->style->font->descent))
207             requisition->height = widget->style->font->ascent + widget->style->font->descent;
208         }
209       else if ((scale->value_pos == GTK_POS_TOP) ||
210                (scale->value_pos == GTK_POS_BOTTOM))
211         {
212           if (requisition->width < value_width)
213             requisition->width = value_width;
214           requisition->height += widget->style->font->ascent + widget->style->font->descent;
215         }
216     }
217 }
218
219 static void
220 gtk_hscale_size_allocate (GtkWidget     *widget,
221                           GtkAllocation *allocation)
222 {
223   GtkRange *range;
224   GtkScale *scale;
225   gint width, height;
226   gint x, y;
227
228   g_return_if_fail (widget != NULL);
229   g_return_if_fail (GTK_IS_HSCALE (widget));
230   g_return_if_fail (allocation != NULL);
231
232   widget->allocation = *allocation;
233   if (GTK_WIDGET_REALIZED (widget))
234     {
235       range = GTK_RANGE (widget);
236       scale = GTK_SCALE (widget);
237
238       gdk_window_move_resize (widget->window,
239                               allocation->x, allocation->y,
240                               allocation->width, allocation->height);
241
242       gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
243
244       gdk_window_move_resize (range->trough, x, y, width, height);
245       gtk_range_slider_update (GTK_RANGE (widget));
246     }
247 }
248
249 static void
250 gtk_hscale_pos_trough (GtkHScale *hscale,
251                        gint      *x,
252                        gint      *y,
253                        gint      *w,
254                        gint      *h)
255 {
256   GtkWidget *widget;
257   GtkScale *scale;
258
259   g_return_if_fail (hscale != NULL);
260   g_return_if_fail (GTK_IS_HSCALE (hscale));
261   g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
262
263   widget = GTK_WIDGET (hscale);
264   scale = GTK_SCALE (hscale);
265
266   *w = widget->allocation.width;
267   *h = (RANGE_CLASS (scale)->slider_width +
268         widget->style->klass->ythickness * 2);
269
270   if (scale->draw_value)
271     {
272       *x = 0;
273       *y = 0;
274
275       switch (scale->value_pos)
276         {
277         case GTK_POS_LEFT:
278           *x += gtk_scale_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
279           *y = (widget->allocation.height - *h) / 2;
280           *w -= *x;
281           break;
282         case GTK_POS_RIGHT:
283           *w -= gtk_scale_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
284           *y = (widget->allocation.height - *h) / 2;
285           break;
286         case GTK_POS_TOP:
287           *y = (widget->style->font->ascent + widget->style->font->descent +
288                 (widget->allocation.height - widget->requisition.height) / 2);
289           break;
290         case GTK_POS_BOTTOM:
291           *y = (widget->allocation.height - widget->requisition.height) / 2;
292           break;
293         }
294     }
295   else
296     {
297       *x = 0;
298       *y = (widget->allocation.height - *h) / 2;
299     }
300   *x += 1;
301   *w -= 2;
302 }
303
304 static void
305 gtk_hscale_draw_slider (GtkRange *range)
306 {
307   GtkStateType state_type;
308   gint width, height;
309
310   g_return_if_fail (range != NULL);
311   g_return_if_fail (GTK_IS_HSCALE (range));
312
313   if (range->slider)
314     {
315       if ((range->in_child == RANGE_CLASS (range)->slider) ||
316           (range->click_child == RANGE_CLASS (range)->slider))
317         state_type = GTK_STATE_PRELIGHT;
318       else
319         state_type = GTK_STATE_NORMAL;
320
321       gtk_style_set_background (GTK_WIDGET (range)->style, range->slider, state_type);
322       gdk_window_clear (range->slider);
323
324       gdk_window_get_size (range->slider, &width, &height);
325       gtk_draw_vline (GTK_WIDGET (range)->style, range->slider,
326                       state_type, 1, height - 2, width / 2);
327
328       gtk_draw_shadow (GTK_WIDGET (range)->style, range->slider,
329                        state_type, GTK_SHADOW_OUT,
330                        0, 0, -1, -1);
331     }
332 }
333
334 static void
335 gtk_hscale_draw_value (GtkScale *scale)
336 {
337   GtkStateType state_type;
338   gchar buffer[32];
339   gint text_width;
340   gint width, height;
341   gint x, y;
342
343   g_return_if_fail (scale != NULL);
344   g_return_if_fail (GTK_IS_HSCALE (scale));
345
346   if (scale->draw_value)
347     {
348       gdk_window_get_size (GTK_WIDGET (scale)->window, &width, &height);
349       gdk_window_clear_area (GTK_WIDGET (scale)->window, 1, 1, width - 2, height - 2);
350
351       sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
352       text_width = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
353
354       switch (scale->value_pos)
355         {
356         case GTK_POS_LEFT:
357           gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
358           gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
359
360           x -= SCALE_CLASS (scale)->value_spacing + text_width;
361           y += ((height -
362                  (GTK_WIDGET (scale)->style->font->ascent +
363                   GTK_WIDGET (scale)->style->font->descent)) / 2 +
364                 GTK_WIDGET (scale)->style->font->ascent);
365           break;
366         case GTK_POS_RIGHT:
367           gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
368           gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
369
370           x += width + SCALE_CLASS (scale)->value_spacing;
371           y += ((height -
372                  (GTK_WIDGET (scale)->style->font->ascent +
373                   GTK_WIDGET (scale)->style->font->descent)) / 2 +
374                 GTK_WIDGET (scale)->style->font->ascent);
375           break;
376         case GTK_POS_TOP:
377           gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
378           gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
379           gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
380           gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
381
382           x += (width - text_width) / 2;
383           y -= GTK_WIDGET (scale)->style->font->descent;
384           break;
385         case GTK_POS_BOTTOM:
386           gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
387           gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
388           gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
389           gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
390
391           x += (width - text_width) / 2;
392           y += height + GTK_WIDGET (scale)->style->font->ascent;
393           break;
394         }
395
396       state_type = GTK_STATE_NORMAL;
397       if (!GTK_WIDGET_IS_SENSITIVE (scale))
398         state_type = GTK_STATE_INSENSITIVE;
399
400       gtk_draw_string (GTK_WIDGET (scale)->style,
401                        GTK_WIDGET (scale)->window,
402                        state_type, x, y, buffer);
403     }
404 }
405
406 static gint
407 gtk_hscale_trough_keys(GtkRange *range,
408                        GdkEventKey *key,
409                        GtkScrollType *scroll,
410                        GtkTroughType *pos)
411 {
412   gint return_val = FALSE;
413   switch (key->keyval)
414     {
415     case GDK_Left:
416       return_val = TRUE;
417       if (key->state & GDK_CONTROL_MASK)
418         *scroll = GTK_SCROLL_PAGE_BACKWARD;
419       else
420         *scroll = GTK_SCROLL_STEP_BACKWARD;
421       break;
422     case GDK_Right:
423       return_val = TRUE;
424       if (key->state & GDK_CONTROL_MASK)
425         *scroll = GTK_SCROLL_PAGE_FORWARD;
426       else
427         *scroll = GTK_SCROLL_STEP_FORWARD;
428       break;
429     case GDK_Home:
430       return_val = TRUE;
431       *pos = GTK_TROUGH_START;
432       break;
433     case GDK_End:
434       return_val = TRUE;
435       *pos = GTK_TROUGH_END;
436       break;
437     }
438   return return_val;
439 }