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"
31 #include "gtksignal.h"
34 #define VALUE_SPACING 2
36 static gpointer parent_class;
38 static void gtk_vscale_class_init (GtkVScaleClass *klass);
39 static void gtk_vscale_init (GtkVScale *vscale);
40 static gboolean gtk_vscale_expose (GtkWidget *widget,
41 GdkEventExpose *event);
44 gtk_vscale_get_type (void)
46 static GtkType vscale_type = 0;
50 static const GtkTypeInfo vscale_info =
54 sizeof (GtkVScaleClass),
55 (GtkClassInitFunc) gtk_vscale_class_init,
56 (GtkObjectInitFunc) gtk_vscale_init,
57 /* reserved_1 */ NULL,
58 /* reserved_2 */ NULL,
59 (GtkClassInitFunc) NULL,
62 vscale_type = gtk_type_unique (GTK_TYPE_SCALE, &vscale_info);
69 gtk_vscale_class_init (GtkVScaleClass *class)
71 GObjectClass *gobject_class;
72 GtkWidgetClass *widget_class;
73 GtkRangeClass *range_class;
75 gobject_class = G_OBJECT_CLASS (class);
76 widget_class = GTK_WIDGET_CLASS (class);
77 range_class = GTK_RANGE_CLASS (class);
79 parent_class = g_type_class_peek_parent (class);
81 range_class->slider_detail = "vscale";
83 widget_class->expose_event = gtk_vscale_expose;
87 gtk_vscale_init (GtkVScale *vscale)
91 range = GTK_RANGE (vscale);
93 range->orientation = GTK_ORIENTATION_VERTICAL;
97 gtk_vscale_new (GtkAdjustment *adjustment)
101 vscale = gtk_widget_new (GTK_TYPE_VSCALE,
102 "adjustment", adjustment,
110 * gtk_vscale_new_with_range:
111 * @min: minimum value
112 * @max: maximum value
113 * @step: step increment (tick size) used with keyboard shortcuts
115 * Creates a new vertical 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.
120 * Return value: a new #GtkVScale
123 gtk_vscale_new_with_range (gdouble min,
131 g_return_val_if_fail (min < max, NULL);
132 g_return_val_if_fail (step != 0.0, NULL);
134 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
136 scale = g_object_new (GTK_TYPE_VSCALE,
140 if (fabs (step) >= 1.0 || step == 0.0)
143 digits = abs ((gint) floor (log10 (fabs (step))));
148 gtk_scale_set_digits (scale, digits);
150 return GTK_WIDGET (scale);
154 gtk_vscale_expose (GtkWidget *widget,
155 GdkEventExpose *event)
161 range = GTK_RANGE (widget);
162 scale = GTK_SCALE (widget);
163 vscale = GTK_VSCALE (widget);
165 /* We need to chain up _first_ so the various geometry members of
166 * GtkRange struct are updated.
168 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
169 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
171 if (scale->draw_value)
174 PangoRectangle logical_rect;
177 GtkStateType state_type;
180 gtk_widget_style_get (widget, "value_spacing", &value_spacing, NULL);
182 txt = _gtk_scale_format_value (scale,
183 GTK_RANGE (scale)->adjustment->value);
185 layout = gtk_widget_create_pango_layout (widget, txt);
188 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
190 switch (scale->value_pos)
193 x = range->range_rect.x - logical_rect.width - value_spacing;
194 y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
195 y = CLAMP (y, 0, widget->allocation.height - logical_rect.height);
199 x = range->range_rect.x + range->range_rect.width + value_spacing;
200 y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
201 y = CLAMP (y, 0, widget->allocation.height - logical_rect.height);
205 x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
206 y = range->range_rect.y - logical_rect.height - value_spacing;
210 x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
211 y = range->range_rect.y + range->range_rect.height + value_spacing;
215 g_return_val_if_reached (FALSE);
221 x += widget->allocation.x;
222 y += widget->allocation.y;
224 state_type = GTK_STATE_NORMAL;
225 if (!GTK_WIDGET_IS_SENSITIVE (scale))
226 state_type = GTK_STATE_INSENSITIVE;
228 gtk_paint_layout (widget->style,
238 g_object_unref (G_OBJECT (layout));