]> Pileus Git - ~andy/gtk/blob - gtk/gtkhscale.c
3f99e5a95ca02b112a402b4e9b3e584502ae3bbf
[~andy/gtk] / gtk / gtkhscale.c
1 /* GTK - The GTK+ Toolkit
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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-2000.  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 "config.h"
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "gtkhscale.h"
32 #include "gtkintl.h"
33 #include "gtkalias.h"
34
35 static gboolean gtk_hscale_expose           (GtkWidget      *widget,
36                                              GdkEventExpose *event);
37
38 static void     gtk_hscale_get_layout_offsets (GtkScale       *scale,
39                                                gint           *x,
40                                                gint           *y);
41
42 G_DEFINE_TYPE (GtkHScale, gtk_hscale, GTK_TYPE_SCALE)
43
44 static void
45 gtk_hscale_class_init (GtkHScaleClass *class)
46 {
47   GtkWidgetClass *widget_class;
48   GtkRangeClass *range_class;
49   GtkScaleClass *scale_class;
50   
51   widget_class = GTK_WIDGET_CLASS (class);
52   range_class = GTK_RANGE_CLASS (class);
53   scale_class = GTK_SCALE_CLASS (class);
54
55   range_class->slider_detail = "hscale";
56
57   scale_class->get_layout_offsets = gtk_hscale_get_layout_offsets;
58   
59   widget_class->expose_event = gtk_hscale_expose;
60 }
61
62 static void
63 gtk_hscale_init (GtkHScale *hscale)
64 {
65   GtkRange *range;
66
67   range = GTK_RANGE (hscale);
68
69   range->orientation = GTK_ORIENTATION_HORIZONTAL;
70   range->flippable = TRUE;
71 }
72
73 GtkWidget*
74 gtk_hscale_new (GtkAdjustment *adjustment)
75 {
76   return g_object_new (GTK_TYPE_HSCALE, "adjustment", adjustment, NULL);
77 }
78
79 /**
80  * gtk_hscale_new_with_range:
81  * @min: minimum value
82  * @max: maximum value
83  * @step: step increment (tick size) used with keyboard shortcuts
84  * 
85  * Creates a new horizontal scale widget that lets the user input a
86  * number between @min and @max (including @min and @max) with the
87  * increment @step.  @step must be nonzero; it's the distance the
88  * slider moves when using the arrow keys to adjust the scale value.
89  * 
90  * Note that the way in which the precision is derived works best if @step 
91  * is a power of ten. If the resulting precision is not suitable for your 
92  * needs, use gtk_scale_set_digits() to correct it.
93  * 
94  * Return value: a new #GtkHScale
95  **/
96 GtkWidget*
97 gtk_hscale_new_with_range (gdouble min,
98                            gdouble max,
99                            gdouble step)
100 {
101   GtkObject *adj;
102   GtkScale *scale;
103   gint digits;
104
105   g_return_val_if_fail (min < max, NULL);
106   g_return_val_if_fail (step != 0.0, NULL);
107
108   adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
109
110   if (fabs (step) >= 1.0 || step == 0.0)
111     digits = 0;
112   else {
113     digits = abs ((gint) floor (log10 (fabs (step))));
114     if (digits > 5)
115       digits = 5;
116   }
117   
118   scale = g_object_new (GTK_TYPE_HSCALE,
119                         "adjustment", adj,
120                         "digits", digits,
121                         NULL);
122   
123   return GTK_WIDGET (scale);
124 }
125
126 static gboolean
127 gtk_hscale_expose (GtkWidget      *widget,
128                    GdkEventExpose *event)
129 {
130   GtkScale *scale;
131   
132   scale = GTK_SCALE (widget);
133
134   /* We need to chain up _first_ so the various geometry members of
135    * GtkRange struct are updated.
136    */
137   if (GTK_WIDGET_CLASS (gtk_hscale_parent_class)->expose_event)
138     GTK_WIDGET_CLASS (gtk_hscale_parent_class)->expose_event (widget, event);
139
140   if (scale->draw_value)
141     {
142       PangoLayout *layout;
143       gint x, y;
144       GtkStateType state_type;
145
146       layout = gtk_scale_get_layout (scale);
147       gtk_scale_get_layout_offsets (scale, &x, &y);
148
149       state_type = GTK_STATE_NORMAL;
150       if (!GTK_WIDGET_IS_SENSITIVE (scale))
151         state_type = GTK_STATE_INSENSITIVE;
152
153       gtk_paint_layout (widget->style,
154                         widget->window,
155                         state_type,
156                         FALSE,
157                         NULL,
158                         widget,
159                         "hscale",
160                         x, y,
161                         layout);
162
163     }
164   
165   return FALSE;
166 }
167
168 static void     
169 gtk_hscale_get_layout_offsets (GtkScale *scale,
170                                gint     *x,
171                                gint     *y)
172 {
173   GtkWidget *widget;
174   GtkRange *range;
175   PangoLayout *layout;
176   PangoRectangle logical_rect;
177   gint value_spacing;
178
179   widget = GTK_WIDGET (scale);
180   layout = gtk_scale_get_layout (scale);
181       
182   if (!layout)    
183     {
184       *x = 0;
185       *y = 0;
186
187       return;
188     }
189
190   gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
191
192   range = GTK_RANGE (widget);
193   scale = GTK_SCALE (widget);
194
195   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
196       
197   switch (scale->value_pos)
198     {
199      case GTK_POS_LEFT:
200        *x = range->range_rect.x - value_spacing - logical_rect.width;
201        *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
202        break;
203           
204     case GTK_POS_RIGHT:
205       *x = range->range_rect.x + range->range_rect.width + value_spacing;
206       *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
207       break;
208           
209     case GTK_POS_TOP:
210       *x = range->slider_start +
211         (range->slider_end - range->slider_start - logical_rect.width) / 2;
212       *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
213       *y = range->range_rect.y - logical_rect.height - value_spacing;
214       break;
215           
216     case GTK_POS_BOTTOM:
217       *x = range->slider_start +
218         (range->slider_end - range->slider_start - logical_rect.width) / 2;
219       *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
220       *y = range->range_rect.y + range->range_rect.height + value_spacing;
221       break;
222
223     default:
224       g_return_if_reached ();
225       *x = 0;
226       *y = 0;
227       break;
228     }
229
230   *x += widget->allocation.x;
231   *y += widget->allocation.y;
232 }
233
234 #define __GTK_HSCALE_C__
235 #include "gtkaliasdef.c"