]> Pileus Git - ~andy/gtk/blob - gtk/gtkhscale.c
Support added for building using a GNU toolchain on Win32,
[~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
20 /*
21  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <stdio.h>
28 #include "gtkhscale.h"
29 #include "gtksignal.h"
30 #include "gdk/gdkkeysyms.h"
31
32
33 #define SCALE_CLASS(w)  GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
34 #define RANGE_CLASS(w)  GTK_RANGE_CLASS (GTK_OBJECT (w)->klass)
35
36 enum {
37   ARG_0,
38   ARG_ADJUSTMENT
39 };
40
41 static void gtk_hscale_class_init    (GtkHScaleClass *klass);
42 static void gtk_hscale_init          (GtkHScale      *hscale);
43 static void gtk_hscale_set_arg       (GtkObject      *object,
44                                       GtkArg         *arg,
45                                       guint           arg_id);
46 static void gtk_hscale_get_arg       (GtkObject      *object,
47                                       GtkArg         *arg,
48                                       guint           arg_id);
49 static void gtk_hscale_realize       (GtkWidget      *widget);
50 static void gtk_hscale_size_request  (GtkWidget      *widget,
51                                       GtkRequisition *requisition);
52 static void gtk_hscale_size_allocate (GtkWidget      *widget,
53                                       GtkAllocation  *allocation);
54 static void gtk_hscale_pos_trough    (GtkHScale      *hscale,
55                                       gint           *x,
56                                       gint           *y,
57                                       gint           *w,
58                                       gint           *h);
59 static void gtk_hscale_pos_background (GtkHScale     *hscale,
60                                        gint          *x,
61                                        gint          *y,
62                                        gint          *w,
63                                        gint          *h);
64 static void gtk_hscale_draw_slider   (GtkRange       *range);
65 static void gtk_hscale_draw_value    (GtkScale       *scale);
66 static void gtk_hscale_draw          (GtkWidget      *widget,
67                                       GdkRectangle   *area);
68 static gint gtk_hscale_trough_keys   (GtkRange *range,
69                                       GdkEventKey *key,
70                                       GtkScrollType *scroll,
71                                       GtkTroughType *pos);
72 static void gtk_hscale_clear_background (GtkRange    *range);
73
74 GtkType
75 gtk_hscale_get_type (void)
76 {
77   static GtkType hscale_type = 0;
78   
79   if (!hscale_type)
80     {
81       static const GtkTypeInfo hscale_info =
82       {
83         "GtkHScale",
84         sizeof (GtkHScale),
85         sizeof (GtkHScaleClass),
86         (GtkClassInitFunc) gtk_hscale_class_init,
87         (GtkObjectInitFunc) gtk_hscale_init,
88         /* reserved_1 */ NULL,
89         /* reserved_2 */ NULL,
90         (GtkClassInitFunc) NULL,
91       };
92       
93       hscale_type = gtk_type_unique (GTK_TYPE_SCALE, &hscale_info);
94     }
95   
96   return hscale_type;
97 }
98
99 static void
100 gtk_hscale_class_init (GtkHScaleClass *class)
101 {
102   GtkObjectClass *object_class;
103   GtkWidgetClass *widget_class;
104   GtkRangeClass *range_class;
105   GtkScaleClass *scale_class;
106   
107   object_class = (GtkObjectClass*) class;
108   widget_class = (GtkWidgetClass*) class;
109   range_class = (GtkRangeClass*) class;
110   scale_class = (GtkScaleClass*) class;
111   
112   gtk_object_add_arg_type ("GtkHScale::adjustment",
113                            GTK_TYPE_ADJUSTMENT,
114                            GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
115                            ARG_ADJUSTMENT);
116   
117   object_class->set_arg = gtk_hscale_set_arg;
118   object_class->get_arg = gtk_hscale_get_arg;
119   
120   widget_class->realize = gtk_hscale_realize;
121   widget_class->size_request = gtk_hscale_size_request;
122   widget_class->size_allocate = gtk_hscale_size_allocate;
123   widget_class->draw = gtk_hscale_draw;
124   
125   range_class->slider_update = gtk_range_default_hslider_update;
126   range_class->trough_click = gtk_range_default_htrough_click;
127   range_class->motion = gtk_range_default_hmotion;
128   range_class->draw_slider = gtk_hscale_draw_slider;
129   range_class->trough_keys = gtk_hscale_trough_keys;
130   range_class->clear_background = gtk_hscale_clear_background;
131   
132   scale_class->draw_value = gtk_hscale_draw_value;
133 }
134
135 static void
136 gtk_hscale_set_arg (GtkObject          *object,
137                     GtkArg             *arg,
138                     guint               arg_id)
139 {
140   GtkHScale *hscale;
141   
142   hscale = GTK_HSCALE (object);
143   
144   switch (arg_id)
145     {
146     case ARG_ADJUSTMENT:
147       gtk_range_set_adjustment (GTK_RANGE (hscale), GTK_VALUE_POINTER (*arg));
148       break;
149     default:
150       break;
151     }
152 }
153
154 static void
155 gtk_hscale_get_arg (GtkObject          *object,
156                     GtkArg             *arg,
157                     guint               arg_id)
158 {
159   GtkHScale *hscale;
160   
161   hscale = GTK_HSCALE (object);
162   
163   switch (arg_id)
164     {
165     case ARG_ADJUSTMENT:
166       GTK_VALUE_POINTER (*arg) = GTK_RANGE (hscale);
167       break;
168     default:
169       arg->type = GTK_TYPE_INVALID;
170       break;
171     }
172 }
173
174 static void
175 gtk_hscale_init (GtkHScale *hscale)
176 {
177   GTK_WIDGET_SET_FLAGS (hscale, GTK_NO_WINDOW);
178 }
179
180 GtkWidget*
181 gtk_hscale_new (GtkAdjustment *adjustment)
182 {
183   GtkWidget *hscale;
184   
185   hscale = gtk_widget_new (GTK_TYPE_HSCALE,
186                            "adjustment", adjustment,
187                            NULL);
188
189   return hscale;
190 }
191
192
193 static void
194 gtk_hscale_realize (GtkWidget *widget)
195 {
196   GtkRange *range;
197   GdkWindowAttr attributes;
198   gint attributes_mask;
199   gint x, y, w, h;
200   
201   g_return_if_fail (widget != NULL);
202   g_return_if_fail (GTK_IS_HSCALE (widget));
203   
204   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
205   range = GTK_RANGE (widget);
206   
207   widget->window = gtk_widget_get_parent_window (widget);
208   gdk_window_ref (widget->window);
209   
210   gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &w, &h);
211
212   attributes.x = x + widget->allocation.x;
213   attributes.y = y + widget->allocation.y;
214   attributes.width = w;
215   attributes.height = h;
216   attributes.wclass = GDK_INPUT_OUTPUT;
217   attributes.window_type = GDK_WINDOW_CHILD;
218   
219   attributes.event_mask = gtk_widget_get_events (widget) | 
220     (GDK_EXPOSURE_MASK |
221      GDK_BUTTON_PRESS_MASK |
222      GDK_BUTTON_RELEASE_MASK |
223      GDK_ENTER_NOTIFY_MASK |
224      GDK_LEAVE_NOTIFY_MASK);
225   attributes.visual = gtk_widget_get_visual (widget);
226   attributes.colormap = gtk_widget_get_colormap (widget);
227   
228   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
229   
230   range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
231   
232   attributes.width = SCALE_CLASS (range)->slider_length;
233   attributes.height = RANGE_CLASS (range)->slider_width;
234   attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
235                             GDK_POINTER_MOTION_HINT_MASK);
236   
237   range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
238   
239   widget->style = gtk_style_attach (widget->style, widget->window);
240   
241   gdk_window_set_user_data (range->trough, widget);
242   gdk_window_set_user_data (range->slider, widget);
243   
244   gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
245   gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
246   
247   gtk_range_slider_update (GTK_RANGE (widget));
248   
249   gdk_window_show (range->slider);
250 }
251
252 static void
253 gtk_hscale_draw (GtkWidget    *widget,
254                  GdkRectangle *area)
255 {
256   GtkRange *range;
257   GdkRectangle tmp_area;
258   GdkRectangle child_area;
259   gint x, y, width, height;
260   
261   g_return_if_fail (widget != NULL);
262   g_return_if_fail (GTK_IS_RANGE (widget));
263   g_return_if_fail (area != NULL);
264   
265   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
266     {
267       range = GTK_RANGE (widget);
268       
269       gtk_hscale_pos_background (GTK_HSCALE (widget), &x, &y, &width, &height);
270       
271       tmp_area.x = x;
272       tmp_area.y = y;
273       tmp_area.width = width;
274       tmp_area.height = height;
275       
276       if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
277         gtk_range_draw_background (range);
278       
279       gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
280       
281       tmp_area.x = x;
282       tmp_area.y = y;
283       tmp_area.width = width;
284       tmp_area.height = height;
285       
286       if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
287         {
288           gtk_range_draw_trough (range);
289           gtk_range_draw_slider (range);
290           gtk_range_draw_step_forw (range);
291           gtk_range_draw_step_back (range);
292         }
293     }
294 }
295
296 static void 
297 gtk_hscale_clear_background (GtkRange    *range)
298 {
299   GtkWidget *widget;
300   gint x, y, width, height;
301   
302   g_return_if_fail (range != NULL);
303   
304   widget = GTK_WIDGET (range);
305   
306   gtk_hscale_pos_background (GTK_HSCALE (range), &x, &y, &width, &height);
307   
308   gtk_widget_queue_clear_area (GTK_WIDGET (range),
309                                x, y, width, height);
310 }
311
312 static void
313 gtk_hscale_size_request (GtkWidget      *widget,
314                          GtkRequisition *requisition)
315 {
316   GtkScale *scale;
317   gint value_width;
318   
319   g_return_if_fail (widget != NULL);
320   g_return_if_fail (GTK_IS_HSCALE (widget));
321   g_return_if_fail (requisition != NULL);
322   
323   scale = GTK_SCALE (widget);
324   
325   requisition->width = (SCALE_CLASS (scale)->slider_length +
326                         widget->style->klass->xthickness) * 2;
327   requisition->height = (RANGE_CLASS (scale)->slider_width +
328                          widget->style->klass->ythickness * 2);
329   
330   if (scale->draw_value)
331     {
332       value_width = gtk_scale_get_value_width (scale);
333       
334       if ((scale->value_pos == GTK_POS_LEFT) ||
335           (scale->value_pos == GTK_POS_RIGHT))
336         {
337           requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
338           if (requisition->height < (widget->style->font->ascent + widget->style->font->descent))
339             requisition->height = widget->style->font->ascent + widget->style->font->descent;
340         }
341       else if ((scale->value_pos == GTK_POS_TOP) ||
342                (scale->value_pos == GTK_POS_BOTTOM))
343         {
344           if (requisition->width < value_width)
345             requisition->width = value_width;
346           requisition->height += widget->style->font->ascent + widget->style->font->descent;
347         }
348     }
349 }
350
351 static void
352 gtk_hscale_size_allocate (GtkWidget     *widget,
353                           GtkAllocation *allocation)
354 {
355   GtkRange *range;
356   GtkScale *scale;
357   gint width, height;
358   gint x, y;
359   
360   g_return_if_fail (widget != NULL);
361   g_return_if_fail (GTK_IS_HSCALE (widget));
362   g_return_if_fail (allocation != NULL);
363   
364   widget->allocation = *allocation;
365   if (GTK_WIDGET_REALIZED (widget))
366     {
367       range = GTK_RANGE (widget);
368       scale = GTK_SCALE (widget);
369       
370       gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
371       
372       gdk_window_move_resize (range->trough, 
373                               x + widget->allocation.x, 
374                               y + widget->allocation.y, width, height);
375       gtk_range_slider_update (GTK_RANGE (widget));
376     }
377 }
378
379 static void
380 gtk_hscale_pos_trough (GtkHScale *hscale,
381                        gint      *x,
382                        gint      *y,
383                        gint      *w,
384                        gint      *h)
385 {
386   GtkWidget *widget;
387   GtkScale *scale;
388   
389   g_return_if_fail (hscale != NULL);
390   g_return_if_fail (GTK_IS_HSCALE (hscale));
391   g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
392   
393   widget = GTK_WIDGET (hscale);
394   scale = GTK_SCALE (hscale);
395   
396   *w = widget->allocation.width;
397   *h = (RANGE_CLASS (scale)->slider_width +
398         widget->style->klass->ythickness * 2);
399   
400   if (scale->draw_value)
401     {
402       *x = 0;
403       *y = 0;
404       
405       switch (scale->value_pos)
406         {
407         case GTK_POS_LEFT:
408           *x += gtk_scale_get_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
409           *y = (widget->allocation.height - *h) / 2;
410           *w -= *x;
411           break;
412         case GTK_POS_RIGHT:
413           *w -= gtk_scale_get_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
414           *y = (widget->allocation.height - *h) / 2;
415           break;
416         case GTK_POS_TOP:
417           *y = (widget->style->font->ascent + widget->style->font->descent +
418                 (widget->allocation.height - widget->requisition.height) / 2);
419           break;
420         case GTK_POS_BOTTOM:
421           *y = (widget->allocation.height - widget->requisition.height) / 2;
422           break;
423         }
424     }
425   else
426     {
427       *x = 0;
428       *y = (widget->allocation.height - *h) / 2;
429     }
430   *x += 1;
431   *w -= 2;
432 }
433
434 static void
435 gtk_hscale_pos_background (GtkHScale *hscale,
436                            gint      *x,
437                            gint      *y,
438                            gint      *w,
439                            gint      *h)
440 {
441   GtkWidget *widget;
442   GtkScale *scale;
443   
444   gint tx, ty, twidth, theight;
445   
446   g_return_if_fail (hscale != NULL);
447   g_return_if_fail (GTK_IS_HSCALE (hscale));
448   g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
449   
450   gtk_hscale_pos_trough (hscale, &tx, &ty, &twidth, &theight);
451   
452   widget = GTK_WIDGET (hscale);
453   scale = GTK_SCALE (hscale);
454   
455   *x = widget->allocation.x;
456   *y = widget->allocation.y;
457   *w = widget->allocation.width;
458   *h = widget->allocation.height;
459   
460   switch (scale->value_pos)
461     {
462     case GTK_POS_LEFT:
463       *w -= twidth;
464       break;
465     case GTK_POS_RIGHT:
466       *x += twidth;
467       *w -= twidth;
468       break;
469     case GTK_POS_TOP:
470       *h -= theight;
471       break;
472     case GTK_POS_BOTTOM:
473       *y += theight;
474       *h -= theight;
475       break;
476     }
477 }
478
479 static void
480 gtk_hscale_draw_slider (GtkRange *range)
481 {
482   GtkStateType state_type;
483   
484   g_return_if_fail (range != NULL);
485   g_return_if_fail (GTK_IS_HSCALE (range));
486   
487   if (range->slider)
488     {
489       if ((range->in_child == RANGE_CLASS (range)->slider) ||
490           (range->click_child == RANGE_CLASS (range)->slider))
491         state_type = GTK_STATE_PRELIGHT;
492       else
493         state_type = GTK_STATE_NORMAL;
494       
495       gtk_paint_slider (GTK_WIDGET (range)->style, range->slider, state_type, 
496                         GTK_SHADOW_OUT,
497                         NULL, GTK_WIDGET (range), "hscale",
498                         0, 0, -1, -1, 
499                         GTK_ORIENTATION_HORIZONTAL); 
500     }
501 }
502
503 static void
504 gtk_hscale_draw_value (GtkScale *scale)
505 {
506   GtkStateType state_type;
507   GtkWidget *widget;
508   gchar buffer[32];
509   gint text_width;
510   gint width, height;
511   gint x, y;
512   
513   g_return_if_fail (scale != NULL);
514   g_return_if_fail (GTK_IS_HSCALE (scale));
515   
516   widget = GTK_WIDGET (scale);
517   
518   if (scale->draw_value)
519     {
520       sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
521       text_width = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
522       
523       switch (scale->value_pos)
524         {
525         case GTK_POS_LEFT:
526           gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
527           gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
528           
529           x -= SCALE_CLASS (scale)->value_spacing + text_width;
530           y += ((height -
531                  (GTK_WIDGET (scale)->style->font->ascent +
532                   GTK_WIDGET (scale)->style->font->descent)) / 2 +
533                 GTK_WIDGET (scale)->style->font->ascent);
534           break;
535         case GTK_POS_RIGHT:
536           gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
537           gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
538           
539           x += width + SCALE_CLASS (scale)->value_spacing;
540           y += ((height -
541                  (GTK_WIDGET (scale)->style->font->ascent +
542                   GTK_WIDGET (scale)->style->font->descent)) / 2 +
543                 GTK_WIDGET (scale)->style->font->ascent);
544           break;
545         case GTK_POS_TOP:
546           gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
547           gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
548           gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
549           gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
550           
551           x += widget->allocation.x + (width - text_width) / 2;
552           x = CLAMP (x, widget->allocation.x,
553                      widget->allocation.x + widget->allocation.width - text_width);
554           y -= GTK_WIDGET (scale)->style->font->descent;
555           break;
556         case GTK_POS_BOTTOM:
557           gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
558           gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
559           gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
560           gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
561           
562           x += widget->allocation.x + (width - text_width) / 2;
563           x = CLAMP (x, widget->allocation.x,
564                      widget->allocation.x + widget->allocation.width - text_width);
565           y += height + GTK_WIDGET (scale)->style->font->ascent;
566           break;
567         }
568       
569       state_type = GTK_STATE_NORMAL;
570       if (!GTK_WIDGET_IS_SENSITIVE (scale))
571         state_type = GTK_STATE_INSENSITIVE;
572       
573       gtk_paint_string (GTK_WIDGET (scale)->style,
574                         GTK_WIDGET (scale)->window,
575                         state_type, 
576                         NULL, GTK_WIDGET (scale), "hscale", 
577                         x, y, buffer);
578     }
579 }
580
581 static gint
582 gtk_hscale_trough_keys (GtkRange *range,
583                         GdkEventKey *key,
584                         GtkScrollType *scroll,
585                         GtkTroughType *pos)
586 {
587   gint return_val = FALSE;
588   switch (key->keyval)
589     {
590     case GDK_Left:
591       return_val = TRUE;
592       if (key->state & GDK_CONTROL_MASK)
593         *scroll = GTK_SCROLL_PAGE_BACKWARD;
594       else
595         *scroll = GTK_SCROLL_STEP_BACKWARD;
596       break;
597     case GDK_Right:
598       return_val = TRUE;
599       if (key->state & GDK_CONTROL_MASK)
600         *scroll = GTK_SCROLL_PAGE_FORWARD;
601       else
602         *scroll = GTK_SCROLL_STEP_FORWARD;
603       break;
604     case GDK_Home:
605       return_val = TRUE;
606       *pos = GTK_TROUGH_START;
607       break;
608     case GDK_End:
609       return_val = TRUE;
610       *pos = GTK_TROUGH_END;
611       break;
612     }
613   return return_val;
614 }