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