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"
35 static gboolean gtk_hscale_expose (GtkWidget *widget,
36 GdkEventExpose *event);
38 static void gtk_hscale_get_layout_offsets (GtkScale *scale,
42 G_DEFINE_TYPE (GtkHScale, gtk_hscale, GTK_TYPE_SCALE)
45 gtk_hscale_class_init (GtkHScaleClass *class)
47 GtkWidgetClass *widget_class;
48 GtkRangeClass *range_class;
49 GtkScaleClass *scale_class;
51 widget_class = GTK_WIDGET_CLASS (class);
52 range_class = GTK_RANGE_CLASS (class);
53 scale_class = GTK_SCALE_CLASS (class);
55 range_class->slider_detail = "hscale";
57 scale_class->get_layout_offsets = gtk_hscale_get_layout_offsets;
59 widget_class->expose_event = gtk_hscale_expose;
63 gtk_hscale_init (GtkHScale *hscale)
67 range = GTK_RANGE (hscale);
69 range->orientation = GTK_ORIENTATION_HORIZONTAL;
70 range->flippable = TRUE;
74 gtk_hscale_new (GtkAdjustment *adjustment)
76 return g_object_new (GTK_TYPE_HSCALE, "adjustment", adjustment, NULL);
80 * gtk_hscale_new_with_range:
83 * @step: step increment (tick size) used with keyboard shortcuts
85 * Creates a new horizontal scale widget that lets the user input a
86 * number between @min and @max (including @min and @max) with the
87 * increment @step. @step must be nonzero; it's the distance the
88 * slider moves when using the arrow keys to adjust the scale value.
90 * Note that the way in which the precision is derived works best if @step
91 * is a power of ten. If the resulting precision is not suitable for your
92 * needs, use gtk_scale_set_digits() to correct it.
94 * Return value: a new #GtkHScale
97 gtk_hscale_new_with_range (gdouble min,
105 g_return_val_if_fail (min < max, NULL);
106 g_return_val_if_fail (step != 0.0, NULL);
108 adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
110 scale = g_object_new (GTK_TYPE_HSCALE,
114 if (fabs (step) >= 1.0 || step == 0.0)
117 digits = abs ((gint) floor (log10 (fabs (step))));
122 gtk_scale_set_digits (scale, digits);
124 return GTK_WIDGET (scale);
128 gtk_hscale_expose (GtkWidget *widget,
129 GdkEventExpose *event)
133 scale = GTK_SCALE (widget);
135 /* We need to chain up _first_ so the various geometry members of
136 * GtkRange struct are updated.
138 if (GTK_WIDGET_CLASS (gtk_hscale_parent_class)->expose_event)
139 GTK_WIDGET_CLASS (gtk_hscale_parent_class)->expose_event (widget, event);
141 if (scale->draw_value)
145 GtkStateType state_type;
147 layout = gtk_scale_get_layout (scale);
148 gtk_scale_get_layout_offsets (scale, &x, &y);
150 state_type = GTK_STATE_NORMAL;
151 if (!GTK_WIDGET_IS_SENSITIVE (scale))
152 state_type = GTK_STATE_INSENSITIVE;
154 gtk_paint_layout (widget->style,
170 gtk_hscale_get_layout_offsets (GtkScale *scale,
177 PangoRectangle logical_rect;
180 widget = GTK_WIDGET (scale);
181 layout = gtk_scale_get_layout (scale);
191 gtk_widget_style_get (widget, "value-spacing", &value_spacing, NULL);
193 range = GTK_RANGE (widget);
194 scale = GTK_SCALE (widget);
196 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
198 switch (scale->value_pos)
201 *x = range->range_rect.x - value_spacing - logical_rect.width;
202 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
206 *x = range->range_rect.x + range->range_rect.width + value_spacing;
207 *y = range->range_rect.y + (range->range_rect.height - logical_rect.height) / 2;
211 *x = range->slider_start +
212 (range->slider_end - range->slider_start - logical_rect.width) / 2;
213 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
214 *y = range->range_rect.y - logical_rect.height - value_spacing;
218 *x = range->slider_start +
219 (range->slider_end - range->slider_start - logical_rect.width) / 2;
220 *x = CLAMP (*x, 0, widget->allocation.width - logical_rect.width);
221 *y = range->range_rect.y + range->range_rect.height + value_spacing;
225 g_return_if_reached ();
231 *x += widget->allocation.x;
232 *y += widget->allocation.y;
235 #define __GTK_HSCALE_C__
236 #include "gtkaliasdef.c"