]> Pileus Git - ~andy/gtk/blob - gtk/gtkhscale.c
Intern some more strings.
[~andy/gtk] / gtk / gtkhscale.c
1 /* GTK - The GIMP 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 gpointer parent_class;
36
37 static void     gtk_hscale_class_init       (GtkHScaleClass *klass);
38 static void     gtk_hscale_init             (GtkHScale      *hscale);
39 static gboolean gtk_hscale_expose           (GtkWidget      *widget,
40                                              GdkEventExpose *event);
41
42 static void     gtk_hscale_get_layout_offsets (GtkScale       *scale,
43                                                gint           *x,
44                                                gint           *y);
45
46 GType
47 gtk_hscale_get_type (void)
48 {
49   static GType hscale_type = 0;
50   
51   if (!hscale_type)
52     {
53       static const GTypeInfo hscale_info =
54       {
55         sizeof (GtkHScaleClass),
56         NULL,           /* base_init */
57         NULL,           /* base_finalize */
58         (GClassInitFunc) gtk_hscale_class_init,
59         NULL,           /* class_finalize */
60         NULL,           /* class_data */
61         sizeof (GtkHScale),
62         0,              /* n_preallocs */
63         (GInstanceInitFunc) gtk_hscale_init,
64       };
65       
66       hscale_type = g_type_register_static (GTK_TYPE_SCALE, I_("GtkHScale"),
67                                             &hscale_info, 0);
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 = GTK_WIDGET_CLASS (class);
81   range_class = GTK_RANGE_CLASS (class);
82   scale_class = GTK_SCALE_CLASS (class);
83
84   parent_class = g_type_class_peek_parent (class);
85
86   range_class->slider_detail = "hscale";
87
88   scale_class->get_layout_offsets = gtk_hscale_get_layout_offsets;
89   
90   widget_class->expose_event = gtk_hscale_expose;
91 }
92
93 static void
94 gtk_hscale_init (GtkHScale *hscale)
95 {
96   GtkRange *range;
97
98   range = GTK_RANGE (hscale);
99
100   range->orientation = GTK_ORIENTATION_HORIZONTAL;
101   range->flippable = TRUE;
102 }
103
104 GtkWidget*
105 gtk_hscale_new (GtkAdjustment *adjustment)
106 {
107   return g_object_new (GTK_TYPE_HSCALE, "adjustment", adjustment, NULL);
108 }
109
110 /**
111  * gtk_hscale_new_with_range:
112  * @min: minimum value
113  * @max: maximum value
114  * @step: step increment (tick size) used with keyboard shortcuts
115  * 
116  * Creates a new horizontal scale widget that lets the user input a
117  * number between @min and @max (including @min and @max) with the
118  * increment @step.  @step must be nonzero; it's the distance the
119  * slider moves when using the arrow keys to adjust the scale value.
120  * 
121  * Note that the way in which the precision is derived works best if @step 
122  * is a power of ten. If the resulting precision is not suitable for your 
123  * needs, use gtk_scale_set_digits() to correct it.
124  * 
125  * Return value: a new #GtkHScale
126  **/
127 GtkWidget*
128 gtk_hscale_new_with_range (gdouble min,
129                            gdouble max,
130                            gdouble step)
131 {
132   GtkObject *adj;
133   GtkScale *scale;
134   gint digits;
135
136   g_return_val_if_fail (min < max, NULL);
137   g_return_val_if_fail (step != 0.0, NULL);
138
139   adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
140   
141   scale = g_object_new (GTK_TYPE_HSCALE,
142                         "adjustment", adj,
143                         NULL);
144
145   if (fabs (step) >= 1.0 || step == 0.0)
146     digits = 0;
147   else {
148     digits = abs ((gint) floor (log10 (fabs (step))));
149     if (digits > 5)
150       digits = 5;
151   }
152
153   gtk_scale_set_digits (scale, digits);
154   
155   return GTK_WIDGET (scale);
156 }
157
158 static gboolean
159 gtk_hscale_expose (GtkWidget      *widget,
160                    GdkEventExpose *event)
161 {
162   GtkScale *scale;
163   
164   scale = GTK_SCALE (widget);
165
166   /* We need to chain up _first_ so the various geometry members of
167    * GtkRange struct are updated.
168    */
169   if (GTK_WIDGET_CLASS (parent_class)->expose_event)
170     GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
171
172   if (scale->draw_value)
173     {
174       PangoLayout *layout;
175       gint x, y;
176       GtkStateType state_type;
177
178       layout = gtk_scale_get_layout (scale);
179       gtk_scale_get_layout_offsets (scale, &x, &y);
180
181       state_type = GTK_STATE_NORMAL;
182       if (!GTK_WIDGET_IS_SENSITIVE (scale))
183         state_type = GTK_STATE_INSENSITIVE;
184
185       gtk_paint_layout (widget->style,
186                         widget->window,
187                         state_type,
188                         FALSE,
189                         NULL,
190                         widget,
191                         "hscale",
192                         x, y,
193                         layout);
194
195     }
196   
197   return FALSE;
198 }
199
200 static void     
201 gtk_hscale_get_layout_offsets (GtkScale *scale,
202                                gint     *x,
203                                gint     *y)
204 {
205   GtkWidget *widget;
206   GtkRange *range;
207   PangoLayout *layout;
208   PangoRectangle logical_rect;
209   gint value_spacing;
210
211   widget = GTK_WIDGET (scale);
212   layout = gtk_scale_get_layout (scale);
213       
214   if (!layout)    
215     {
216       *x = 0;
217       *y = 0;
218
219       return;
220     }
221
222   gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
223
224   range = GTK_RANGE (widget);
225   scale = GTK_SCALE (widget);
226
227   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
228       
229   switch (scale->value_pos)
230     {
231      case GTK_POS_LEFT:
232        *x = range->range_rect.x - value_spacing - logical_rect.width;
233        *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
234        break;
235           
236     case GTK_POS_RIGHT:
237       *x = range->range_rect.x + range->range_rect.width + value_spacing;
238       *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
239       break;
240           
241     case GTK_POS_TOP:
242       *x = range->slider_start +
243         (range->slider_end - range->slider_start - logical_rect.width) / 2;
244       *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
245       *y = range->range_rect.y - logical_rect.height - value_spacing;
246       break;
247           
248     case GTK_POS_BOTTOM:
249       *x = range->slider_start +
250         (range->slider_end - range->slider_start - logical_rect.width) / 2;
251       *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
252       *y = range->range_rect.y + range->range_rect.height + value_spacing;
253       break;
254
255     default:
256       g_return_if_reached ();
257       *x = 0;
258       *y = 0;
259       break;
260     }
261
262   *x += widget->allocation.x;
263   *y += widget->allocation.y;
264 }
265
266 #define __GTK_HSCALE_C__
267 #include "gtkaliasdef.c"