1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
20 #include "gtkcontainer.h"
24 #define SCALE_CLASS(w) GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
27 static void gtk_scale_class_init (GtkScaleClass *klass);
28 static void gtk_scale_init (GtkScale *scale);
29 static void gtk_scale_draw_background (GtkRange *range);
32 static GtkRangeClass *parent_class = NULL;
36 gtk_scale_get_type (void)
38 static guint scale_type = 0;
42 GtkTypeInfo scale_info =
46 sizeof (GtkScaleClass),
47 (GtkClassInitFunc) gtk_scale_class_init,
48 (GtkObjectInitFunc) gtk_scale_init,
49 /* reserved_1 */ NULL,
50 /* reserved_2 */ NULL,
51 (GtkClassInitFunc) NULL,
54 scale_type = gtk_type_unique (gtk_range_get_type (), &scale_info);
61 gtk_scale_class_init (GtkScaleClass *class)
63 GtkObjectClass *object_class;
64 GtkRangeClass *range_class;
66 object_class = (GtkObjectClass*) class;
67 range_class = (GtkRangeClass*) class;
69 parent_class = gtk_type_class (gtk_range_get_type ());
71 range_class->draw_background = gtk_scale_draw_background;
73 class->slider_length = 31;
74 class->value_spacing = 2;
75 class->draw_value = NULL;
79 gtk_scale_init (GtkScale *scale)
81 GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
82 GTK_RANGE (scale)->digits = 1;
83 scale->draw_value = TRUE;
84 scale->value_pos = GTK_POS_TOP;
88 gtk_scale_set_digits (GtkScale *scale,
91 g_return_if_fail (scale != NULL);
92 g_return_if_fail (GTK_IS_SCALE (scale));
94 if (GTK_RANGE (scale)->digits != digits)
96 GTK_RANGE (scale)->digits = digits;
98 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
99 gtk_widget_queue_resize (GTK_WIDGET (scale));
104 gtk_scale_set_draw_value (GtkScale *scale,
107 g_return_if_fail (scale != NULL);
108 g_return_if_fail (GTK_IS_SCALE (scale));
110 if (scale->draw_value != draw_value)
112 scale->draw_value = (draw_value != 0);
114 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
115 gtk_widget_queue_resize (GTK_WIDGET (scale));
120 gtk_scale_set_value_pos (GtkScale *scale,
123 g_return_if_fail (scale != NULL);
124 g_return_if_fail (GTK_IS_SCALE (scale));
126 if (scale->value_pos != pos)
128 scale->value_pos = pos;
130 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
131 gtk_widget_queue_resize (GTK_WIDGET (scale));
136 gtk_scale_value_width (GtkScale *scale)
146 g_return_val_if_fail (scale != NULL, 0);
147 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
150 if (scale->draw_value)
152 range = GTK_RANGE (scale);
154 value = ABS (range->adjustment->lower);
155 if (value == 0) value = 1;
156 digits = log10 (value) + 1;
161 if (range->adjustment->lower < 0)
163 for (j = 0; j < digits; j++)
165 if (GTK_RANGE (scale)->digits)
167 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
171 return_val = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
173 value = ABS (range->adjustment->upper);
174 if (value == 0) value = 1;
175 digits = log10 (value) + 1;
180 if (range->adjustment->lower < 0)
182 for (j = 0; j < digits; j++)
184 if (GTK_RANGE (scale)->digits)
186 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
190 temp = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
191 return_val = MAX (return_val, temp);
198 gtk_scale_draw_value (GtkScale *scale)
200 g_return_if_fail (scale != NULL);
201 g_return_if_fail (GTK_IS_SCALE (scale));
203 if (SCALE_CLASS (scale)->draw_value)
204 (* SCALE_CLASS (scale)->draw_value) (scale);
209 gtk_scale_draw_background (GtkRange *range)
211 g_return_if_fail (range != NULL);
212 g_return_if_fail (GTK_IS_SCALE (range));
214 gtk_scale_draw_value (GTK_SCALE (range));