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