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.
21 * Modified by the GTK+ Team and others 1997-1999. 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 "gtkcontainer.h"
40 static void gtk_scale_class_init (GtkScaleClass *klass);
41 static void gtk_scale_init (GtkScale *scale);
42 static void gtk_scale_set_arg (GtkObject *object,
45 static void gtk_scale_get_arg (GtkObject *object,
48 static void gtk_scale_map (GtkWidget *widget);
49 static void gtk_scale_unmap (GtkWidget *widget);
51 static void gtk_scale_draw_background (GtkRange *range);
54 static GtkRangeClass *parent_class = NULL;
58 gtk_scale_get_type (void)
60 static GtkType scale_type = 0;
64 static const GtkTypeInfo scale_info =
68 sizeof (GtkScaleClass),
69 (GtkClassInitFunc) gtk_scale_class_init,
70 (GtkObjectInitFunc) gtk_scale_init,
71 /* reserved_1 */ NULL,
72 /* reserved_2 */ NULL,
73 (GtkClassInitFunc) NULL,
76 scale_type = gtk_type_unique (GTK_TYPE_RANGE, &scale_info);
83 gtk_scale_class_init (GtkScaleClass *class)
85 GtkObjectClass *object_class;
86 GtkWidgetClass *widget_class;
87 GtkRangeClass *range_class;
89 object_class = (GtkObjectClass*) class;
90 range_class = (GtkRangeClass*) class;
91 widget_class = (GtkWidgetClass*) class;
93 parent_class = gtk_type_class (GTK_TYPE_RANGE);
95 gtk_object_add_arg_type ("GtkScale::digits",
99 gtk_object_add_arg_type ("GtkScale::draw_value",
103 gtk_object_add_arg_type ("GtkScale::value_pos",
104 GTK_TYPE_POSITION_TYPE,
108 object_class->set_arg = gtk_scale_set_arg;
109 object_class->get_arg = gtk_scale_get_arg;
111 widget_class->map = gtk_scale_map;
112 widget_class->unmap = gtk_scale_unmap;
114 range_class->draw_background = gtk_scale_draw_background;
116 class->slider_length = 31;
117 class->value_spacing = 2;
118 class->draw_value = NULL;
122 gtk_scale_set_arg (GtkObject *object,
128 scale = GTK_SCALE (object);
133 gtk_scale_set_digits (scale, GTK_VALUE_INT (*arg));
136 gtk_scale_set_draw_value (scale, GTK_VALUE_BOOL (*arg));
139 gtk_scale_set_value_pos (scale, GTK_VALUE_ENUM (*arg));
147 gtk_scale_get_arg (GtkObject *object,
153 scale = GTK_SCALE (object);
158 GTK_VALUE_INT (*arg) = GTK_RANGE (scale)->digits;
161 GTK_VALUE_BOOL (*arg) = scale->draw_value;
164 GTK_VALUE_ENUM (*arg) = scale->value_pos;
167 arg->type = GTK_TYPE_INVALID;
173 gtk_scale_init (GtkScale *scale)
175 GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
176 GTK_WIDGET_SET_FLAGS (scale, GTK_NO_WINDOW);
177 GTK_RANGE (scale)->digits = 1;
178 scale->draw_value = TRUE;
179 scale->value_pos = GTK_POS_TOP;
183 gtk_scale_map (GtkWidget *widget)
187 g_return_if_fail (widget != NULL);
188 g_return_if_fail (GTK_IS_SCALE (widget));
190 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
191 range = GTK_RANGE (widget);
193 gdk_window_show (range->trough);
197 gtk_scale_unmap (GtkWidget *widget)
201 g_return_if_fail (widget != NULL);
202 g_return_if_fail (GTK_IS_SCALE (widget));
204 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
205 range = GTK_RANGE (widget);
207 if (GTK_WIDGET_NO_WINDOW (widget))
208 gtk_widget_queue_clear (widget);
210 gdk_window_hide (range->trough);
214 gtk_scale_set_digits (GtkScale *scale,
217 g_return_if_fail (scale != NULL);
218 g_return_if_fail (GTK_IS_SCALE (scale));
220 digits = CLAMP (digits, -1, 16);
222 if (GTK_RANGE (scale)->digits != digits)
224 GTK_RANGE (scale)->digits = digits;
226 gtk_widget_queue_resize (GTK_WIDGET (scale));
231 gtk_scale_set_draw_value (GtkScale *scale,
234 g_return_if_fail (scale != NULL);
235 g_return_if_fail (GTK_IS_SCALE (scale));
237 draw_value = draw_value != FALSE;
239 if (scale->draw_value != draw_value)
241 scale->draw_value = draw_value;
243 gtk_widget_queue_resize (GTK_WIDGET (scale));
248 gtk_scale_set_value_pos (GtkScale *scale,
251 g_return_if_fail (scale != NULL);
252 g_return_if_fail (GTK_IS_SCALE (scale));
254 if (scale->value_pos != pos)
256 scale->value_pos = pos;
258 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
259 gtk_widget_queue_resize (GTK_WIDGET (scale));
264 gtk_scale_get_value_width (GtkScale *scale)
274 g_return_val_if_fail (scale != NULL, 0);
275 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
278 if (scale->draw_value)
280 range = GTK_RANGE (scale);
282 value = ABS (range->adjustment->lower);
283 if (value == 0) value = 1;
284 digits = log10 (value) + 1;
289 if (range->adjustment->lower < 0)
291 for (j = 0; j < digits; j++)
293 if (GTK_RANGE (scale)->digits)
295 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
299 return_val = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
301 value = ABS (range->adjustment->upper);
302 if (value == 0) value = 1;
303 digits = log10 (value) + 1;
308 if (range->adjustment->upper < 0)
310 for (j = 0; j < digits; j++)
312 if (GTK_RANGE (scale)->digits)
314 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
318 temp = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
319 return_val = MAX (return_val, temp);
326 gtk_scale_draw_value (GtkScale *scale)
328 g_return_if_fail (scale != NULL);
329 g_return_if_fail (GTK_IS_SCALE (scale));
331 if (GTK_SCALE_GET_CLASS (scale)->draw_value)
332 GTK_SCALE_GET_CLASS (scale)->draw_value (scale);
337 gtk_scale_draw_background (GtkRange *range)
339 g_return_if_fail (range != NULL);
340 g_return_if_fail (GTK_IS_SCALE (range));
342 gtk_scale_draw_value (GTK_SCALE (range));