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/.
30 #include "gtkvscale.h"
33 #define VALUE_SPACING 2
35 static gpointer parent_class;
37 static void gtk_vscale_class_init (GtkVScaleClass *klass);
38 static void gtk_vscale_init (GtkVScale *vscale);
39 static gboolean gtk_vscale_expose (GtkWidget *widget,
40 GdkEventExpose *event);
43 gtk_vscale_get_type (void)
45 static GType vscale_type = 0;
49 static const GTypeInfo vscale_info =
51 sizeof (GtkVScaleClass),
53 NULL, /* base_finalize */
54 (GClassInitFunc) gtk_vscale_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
59 (GInstanceInitFunc) gtk_vscale_init,
62 vscale_type = g_type_register_static (GTK_TYPE_SCALE, "GtkVScale",
70 gtk_vscale_class_init (GtkVScaleClass *class)
72 GtkWidgetClass *widget_class;
73 GtkRangeClass *range_class;
75 widget_class = GTK_WIDGET_CLASS (class);
76 range_class = GTK_RANGE_CLASS (class);
78 parent_class = g_type_class_peek_parent (class);
80 range_class->slider_detail = "vscale";
82 widget_class->expose_event = gtk_vscale_expose;
86 gtk_vscale_init (GtkVScale *vscale)
90 range = GTK_RANGE (vscale);
92 range->orientation = GTK_ORIENTATION_VERTICAL;
96 gtk_vscale_new (GtkAdjustment *adjustment)
98 return g_object_new (GTK_TYPE_VSCALE, "adjustment", adjustment, NULL);
103 * gtk_vscale_new_with_range:
104 * @min: minimum value
105 * @max: maximum value
106 * @step: step increment (tick size) used with keyboard shortcuts
108 * Creates a new vertical scale widget that lets the user input a
109 * number between @min and @max (including @min and @max) with the
110 * increment @step. @step must be nonzero; it's the distance the
111 * slider moves when using the arrow keys to adjust the scale value.
113 * Return value: a new #GtkVScale
116 gtk_vscale_new_with_range (gdouble min,
124 g_return_val_if_fail (min < max, NULL);
125 g_return_val_if_fail (step != 0.0, NULL);
127 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
129 scale = g_object_new (GTK_TYPE_VSCALE,
133 if (fabs (step) >= 1.0 || step == 0.0)
136 digits = abs ((gint) floor (log10 (fabs (step))));
141 gtk_scale_set_digits (scale, digits);
143 return GTK_WIDGET (scale);
147 gtk_vscale_expose (GtkWidget *widget,
148 GdkEventExpose *event)
154 range = GTK_RANGE (widget);
155 scale = GTK_SCALE (widget);
156 vscale = GTK_VSCALE (widget);
158 /* We need to chain up _first_ so the various geometry members of
159 * GtkRange struct are updated.
161 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
162 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
164 if (scale->draw_value)
167 PangoRectangle logical_rect;
170 GtkStateType state_type;
173 gtk_widget_style_get (widget, "value_spacing", &value_spacing, NULL);
175 txt = _gtk_scale_format_value (scale,
176 GTK_RANGE (scale)->adjustment->value);
178 layout = gtk_widget_create_pango_layout (widget, txt);
181 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
183 switch (scale->value_pos)
186 x = range->range_rect.x - logical_rect.width - value_spacing;
187 y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
188 y = CLAMP (y, 0, widget->allocation.height - logical_rect.height);
192 x = range->range_rect.x + range->range_rect.width + value_spacing;
193 y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
194 y = CLAMP (y, 0, widget->allocation.height - logical_rect.height);
198 x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
199 y = range->range_rect.y - logical_rect.height - value_spacing;
203 x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
204 y = range->range_rect.y + range->range_rect.height + value_spacing;
208 g_return_val_if_reached (FALSE);
214 x += widget->allocation.x;
215 y += widget->allocation.y;
217 state_type = GTK_STATE_NORMAL;
218 if (!GTK_WIDGET_IS_SENSITIVE (scale))
219 state_type = GTK_STATE_INSENSITIVE;
221 gtk_paint_layout (widget->style,
231 g_object_unref (layout);