1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2001 Red Hat, Inc.
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.
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.
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.
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/.
31 #include "gtkvscale.h"
35 #define VALUE_SPACING 2
37 static gpointer parent_class;
39 static void gtk_vscale_class_init (GtkVScaleClass *klass);
40 static void gtk_vscale_init (GtkVScale *vscale);
41 static gboolean gtk_vscale_expose (GtkWidget *widget,
42 GdkEventExpose *event);
44 static void gtk_vscale_get_layout_offsets (GtkScale *scale,
49 gtk_vscale_get_type (void)
51 static GType vscale_type = 0;
55 static const GTypeInfo vscale_info =
57 sizeof (GtkVScaleClass),
59 NULL, /* base_finalize */
60 (GClassInitFunc) gtk_vscale_class_init,
61 NULL, /* class_finalize */
62 NULL, /* class_data */
65 (GInstanceInitFunc) gtk_vscale_init,
68 vscale_type = g_type_register_static (GTK_TYPE_SCALE, I_("GtkVScale"),
76 gtk_vscale_class_init (GtkVScaleClass *class)
78 GtkWidgetClass *widget_class;
79 GtkRangeClass *range_class;
80 GtkScaleClass *scale_class;
82 widget_class = GTK_WIDGET_CLASS (class);
83 range_class = GTK_RANGE_CLASS (class);
84 scale_class = GTK_SCALE_CLASS (class);
86 parent_class = g_type_class_peek_parent (class);
88 range_class->slider_detail = "vscale";
90 scale_class->get_layout_offsets = gtk_vscale_get_layout_offsets;
92 widget_class->expose_event = gtk_vscale_expose;
96 gtk_vscale_init (GtkVScale *vscale)
100 range = GTK_RANGE (vscale);
102 range->orientation = GTK_ORIENTATION_VERTICAL;
106 gtk_vscale_new (GtkAdjustment *adjustment)
108 return g_object_new (GTK_TYPE_VSCALE, "adjustment", adjustment, NULL);
113 * gtk_vscale_new_with_range:
114 * @min: minimum value
115 * @max: maximum value
116 * @step: step increment (tick size) used with keyboard shortcuts
118 * Creates a new vertical scale widget that lets the user input a
119 * number between @min and @max (including @min and @max) with the
120 * increment @step. @step must be nonzero; it's the distance the
121 * slider moves when using the arrow keys to adjust the scale value.
123 * Note that the way in which the precision is derived works best if @step
124 * is a power of ten. If the resulting precision is not suitable for your
125 * needs, use gtk_scale_set_digits() to correct it.
127 * Return value: a new #GtkVScale
130 gtk_vscale_new_with_range (gdouble min,
138 g_return_val_if_fail (min < max, NULL);
139 g_return_val_if_fail (step != 0.0, NULL);
141 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
143 scale = g_object_new (GTK_TYPE_VSCALE,
147 if (fabs (step) >= 1.0 || step == 0.0)
150 digits = abs ((gint) floor (log10 (fabs (step))));
155 gtk_scale_set_digits (scale, digits);
157 return GTK_WIDGET (scale);
161 gtk_vscale_expose (GtkWidget *widget,
162 GdkEventExpose *event)
166 scale = GTK_SCALE (widget);
168 /* We need to chain up _first_ so the various geometry members of
169 * GtkRange struct are updated.
171 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
172 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
174 if (scale->draw_value)
178 GtkStateType state_type;
180 layout = gtk_scale_get_layout (scale);
181 gtk_scale_get_layout_offsets (scale, &x, &y);
183 state_type = GTK_STATE_NORMAL;
184 if (!GTK_WIDGET_IS_SENSITIVE (scale))
185 state_type = GTK_STATE_INSENSITIVE;
187 gtk_paint_layout (widget->style,
203 gtk_vscale_get_layout_offsets (GtkScale *scale,
210 PangoRectangle logical_rect;
213 widget = GTK_WIDGET (scale);
214 layout = gtk_scale_get_layout (scale);
224 range = GTK_RANGE (widget);
225 scale = GTK_SCALE (widget);
227 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
229 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
231 switch (scale->value_pos)
234 *x = range->range_rect.x - logical_rect.width - value_spacing;
235 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
236 *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
240 *x = range->range_rect.x + range->range_rect.width + value_spacing;
241 *y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
242 *y = CLAMP (*y, 0, widget->allocation.height - logical_rect.height);
246 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
247 *y = range->range_rect.y - logical_rect.height - value_spacing;
251 *x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
252 *y = range->range_rect.y + range->range_rect.height + value_spacing;
256 g_return_if_reached ();
262 *x += widget->allocation.x;
263 *y += widget->allocation.y;
266 #define __GTK_VSCALE_C__
267 #include "gtkaliasdef.c"