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