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/.
28 #include "gtkvscale.h"
29 #include "gtksignal.h"
32 #define VALUE_SPACING 2
34 static gpointer parent_class;
36 static void gtk_vscale_class_init (GtkVScaleClass *klass);
37 static void gtk_vscale_init (GtkVScale *vscale);
38 static gboolean gtk_vscale_expose (GtkWidget *widget,
39 GdkEventExpose *event);
42 gtk_vscale_get_type (void)
44 static GtkType vscale_type = 0;
48 static const GtkTypeInfo vscale_info =
52 sizeof (GtkVScaleClass),
53 (GtkClassInitFunc) gtk_vscale_class_init,
54 (GtkObjectInitFunc) gtk_vscale_init,
55 /* reserved_1 */ NULL,
56 /* reserved_2 */ NULL,
57 (GtkClassInitFunc) NULL,
60 vscale_type = gtk_type_unique (GTK_TYPE_SCALE, &vscale_info);
67 gtk_vscale_class_init (GtkVScaleClass *class)
69 GObjectClass *gobject_class;
70 GtkWidgetClass *widget_class;
71 GtkRangeClass *range_class;
73 gobject_class = G_OBJECT_CLASS (class);
74 widget_class = GTK_WIDGET_CLASS (class);
75 range_class = GTK_RANGE_CLASS (class);
77 parent_class = g_type_class_peek_parent (class);
79 range_class->slider_detail = "vscale";
81 widget_class->expose_event = gtk_vscale_expose;
85 gtk_vscale_init (GtkVScale *vscale)
89 range = GTK_RANGE (vscale);
91 range->orientation = GTK_ORIENTATION_VERTICAL;
95 gtk_vscale_new (GtkAdjustment *adjustment)
99 vscale = gtk_widget_new (GTK_TYPE_VSCALE,
100 "adjustment", adjustment,
108 * gtk_vscale_new_with_range:
109 * @min: minimum value
110 * @max: maximum value
111 * @step: step increment (tick size) used with keyboard shortcuts
113 * Creates a new vertical scale widget that lets the user
114 * input a number between @min and @max with the increment @step.
115 * @step must be nonzero; it's the distance the slider moves when
116 * using the arrow keys to adjust the scale value.
118 * Return value: a new #GtkVScale
121 gtk_vscale_new_with_range (gdouble min,
129 g_return_val_if_fail (min < max, NULL);
130 g_return_val_if_fail (step != 0.0, NULL);
132 adj = gtk_adjustment_new (min, min, max, step, 10 * step, step);
134 scale = g_object_new (GTK_TYPE_VSCALE,
138 if (fabs (step) >= 1.0 || step == 0.0)
141 digits = abs ((gint) floor (log10 (fabs (step))));
146 gtk_scale_set_digits (scale, digits);
148 return GTK_WIDGET (scale);
152 gtk_vscale_expose (GtkWidget *widget,
153 GdkEventExpose *event)
159 range = GTK_RANGE (widget);
160 scale = GTK_SCALE (widget);
161 vscale = GTK_VSCALE (widget);
163 if (scale->draw_value)
166 PangoRectangle logical_rect;
169 GtkStateType state_type;
172 gtk_widget_style_get (widget, "value_spacing", &value_spacing, NULL);
174 txt = _gtk_scale_format_value (scale,
175 GTK_RANGE (scale)->adjustment->value);
177 layout = gtk_widget_create_pango_layout (widget, txt);
180 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
182 switch (scale->value_pos)
185 x = range->range_rect.x - logical_rect.width - value_spacing;
186 y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
187 y = CLAMP (y, 0, widget->allocation.height - logical_rect.height);
191 x = range->range_rect.x + range->range_rect.width + value_spacing;
192 y = range->slider_start + (range->slider_end - range->slider_start - logical_rect.height) / 2;
193 y = CLAMP (y, 0, widget->allocation.height - logical_rect.height);
197 x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
198 y = range->range_rect.y - logical_rect.height - value_spacing;
202 x = range->range_rect.x + (range->range_rect.width - logical_rect.width) / 2;
203 y = range->range_rect.y + range->range_rect.height + value_spacing;
207 g_return_val_if_reached (FALSE);
213 state_type = GTK_STATE_NORMAL;
214 if (!GTK_WIDGET_IS_SENSITIVE (scale))
215 state_type = GTK_STATE_INSENSITIVE;
217 gtk_paint_layout (widget->style,
227 g_object_unref (G_OBJECT (layout));
230 return (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);