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,
53 scale_type = gtk_type_unique (gtk_range_get_type (), &scale_info);
60 gtk_scale_class_init (GtkScaleClass *class)
62 GtkObjectClass *object_class;
63 GtkRangeClass *range_class;
65 object_class = (GtkObjectClass*) class;
66 range_class = (GtkRangeClass*) class;
68 parent_class = gtk_type_class (gtk_range_get_type ());
70 range_class->draw_background = gtk_scale_draw_background;
72 class->slider_length = 31;
73 class->value_spacing = 2;
74 class->draw_value = NULL;
78 gtk_scale_init (GtkScale *scale)
80 GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
81 GTK_RANGE (scale)->digits = 1;
82 scale->draw_value = TRUE;
83 scale->value_pos = GTK_POS_TOP;
87 gtk_scale_set_digits (GtkScale *scale,
90 g_return_if_fail (scale != NULL);
91 g_return_if_fail (GTK_IS_SCALE (scale));
93 if (GTK_RANGE (scale)->digits != digits)
95 GTK_RANGE (scale)->digits = digits;
97 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
98 gtk_widget_queue_resize (GTK_WIDGET (scale));
103 gtk_scale_set_draw_value (GtkScale *scale,
106 g_return_if_fail (scale != NULL);
107 g_return_if_fail (GTK_IS_SCALE (scale));
109 if (scale->draw_value != draw_value)
111 scale->draw_value = (draw_value != 0);
113 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
114 gtk_widget_queue_resize (GTK_WIDGET (scale));
119 gtk_scale_set_value_pos (GtkScale *scale,
122 g_return_if_fail (scale != NULL);
123 g_return_if_fail (GTK_IS_SCALE (scale));
125 if (scale->value_pos != pos)
127 scale->value_pos = pos;
129 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
130 gtk_widget_queue_resize (GTK_WIDGET (scale));
135 gtk_scale_value_width (GtkScale *scale)
145 g_return_val_if_fail (scale != NULL, 0);
146 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
149 if (scale->draw_value)
151 range = GTK_RANGE (scale);
153 value = ABS (range->adjustment->lower);
154 if (value == 0) value = 1;
155 digits = log10 (value) + 1;
160 if (range->adjustment->lower < 0)
162 for (j = 0; j < digits; j++)
164 if (GTK_RANGE (scale)->digits)
166 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
170 return_val = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
172 value = ABS (range->adjustment->upper);
173 if (value == 0) value = 1;
174 digits = log10 (value) + 1;
179 if (range->adjustment->lower < 0)
181 for (j = 0; j < digits; j++)
183 if (GTK_RANGE (scale)->digits)
185 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
189 temp = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
190 return_val = MAX (return_val, temp);
197 gtk_scale_draw_value (GtkScale *scale)
199 g_return_if_fail (scale != NULL);
200 g_return_if_fail (GTK_IS_SCALE (scale));
202 if (SCALE_CLASS (scale)->draw_value)
203 (* SCALE_CLASS (scale)->draw_value) (scale);
208 gtk_scale_draw_background (GtkRange *range)
210 g_return_if_fail (range != NULL);
211 g_return_if_fail (GTK_IS_SCALE (range));
213 gtk_scale_draw_value (GTK_SCALE (range));