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 "gtkhscale.h"
34 static gpointer parent_class;
36 static void gtk_hscale_class_init (GtkHScaleClass *klass);
37 static void gtk_hscale_init (GtkHScale *hscale);
38 static gboolean gtk_hscale_expose (GtkWidget *widget,
39 GdkEventExpose *event);
41 static void gtk_hscale_get_layout_offsets (GtkScale *scale,
46 gtk_hscale_get_type (void)
48 static GType hscale_type = 0;
52 static const GTypeInfo hscale_info =
54 sizeof (GtkHScaleClass),
56 NULL, /* base_finalize */
57 (GClassInitFunc) gtk_hscale_class_init,
58 NULL, /* class_finalize */
59 NULL, /* class_data */
62 (GInstanceInitFunc) gtk_hscale_init,
65 hscale_type = g_type_register_static (GTK_TYPE_SCALE, "GtkHScale",
73 gtk_hscale_class_init (GtkHScaleClass *class)
75 GtkWidgetClass *widget_class;
76 GtkRangeClass *range_class;
77 GtkScaleClass *scale_class;
79 widget_class = GTK_WIDGET_CLASS (class);
80 range_class = GTK_RANGE_CLASS (class);
81 scale_class = GTK_SCALE_CLASS (class);
83 parent_class = g_type_class_peek_parent (class);
85 range_class->slider_detail = "hscale";
87 scale_class->get_layout_offsets = gtk_hscale_get_layout_offsets;
89 widget_class->expose_event = gtk_hscale_expose;
93 gtk_hscale_init (GtkHScale *hscale)
97 range = GTK_RANGE (hscale);
99 range->orientation = GTK_ORIENTATION_HORIZONTAL;
100 range->flippable = TRUE;
104 gtk_hscale_new (GtkAdjustment *adjustment)
106 return g_object_new (GTK_TYPE_HSCALE, "adjustment", adjustment, NULL);
110 * gtk_hscale_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 horizontal 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 #GtkHScale
123 gtk_hscale_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_HSCALE,
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_hscale_expose (GtkWidget *widget,
155 GdkEventExpose *event)
159 scale = GTK_SCALE (widget);
161 /* We need to chain up _first_ so the various geometry members of
162 * GtkRange struct are updated.
164 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
165 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
167 if (scale->draw_value)
171 GtkStateType state_type;
173 layout = gtk_scale_get_layout (scale);
174 gtk_scale_get_layout_offsets (scale, &x, &y);
176 state_type = GTK_STATE_NORMAL;
177 if (!GTK_WIDGET_IS_SENSITIVE (scale))
178 state_type = GTK_STATE_INSENSITIVE;
180 gtk_paint_layout (widget->style,
196 gtk_hscale_get_layout_offsets (GtkScale *scale,
203 PangoRectangle logical_rect;
206 widget = GTK_WIDGET (scale);
207 layout = gtk_scale_get_layout (scale);
217 gtk_widget_style_get (widget, "value_spacing", &value_spacing, NULL);
219 range = GTK_RANGE (widget);
220 scale = GTK_SCALE (widget);
222 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
224 switch (scale->value_pos)
227 *x = range->range_rect.x - value_spacing - logical_rect.width;
228 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
232 *x = range->range_rect.x + range->range_rect.width + value_spacing;
233 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
237 *x = range->slider_start +
238 (range->slider_end - range->slider_start - logical_rect.width) / 2;
239 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
240 *y = range->range_rect.y - logical_rect.height - value_spacing;
244 *x = range->slider_start +
245 (range->slider_end - range->slider_start - logical_rect.width) / 2;
246 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
247 *y = range->range_rect.y + range->range_rect.height + value_spacing;
251 g_return_if_reached ();
257 *x += widget->allocation.x;
258 *y += widget->allocation.y;