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"
32 #define SCALE_CLASS(w) GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
42 static void gtk_scale_class_init (GtkScaleClass *klass);
43 static void gtk_scale_init (GtkScale *scale);
44 static void gtk_scale_set_arg (GtkObject *object,
47 static void gtk_scale_get_arg (GtkObject *object,
50 static void gtk_scale_map (GtkWidget *widget);
51 static void gtk_scale_unmap (GtkWidget *widget);
53 static void gtk_scale_draw_background (GtkRange *range);
56 static GtkRangeClass *parent_class = NULL;
60 gtk_scale_get_type (void)
62 static GtkType scale_type = 0;
66 static const GtkTypeInfo scale_info =
70 sizeof (GtkScaleClass),
71 (GtkClassInitFunc) gtk_scale_class_init,
72 (GtkObjectInitFunc) gtk_scale_init,
73 /* reserved_1 */ NULL,
74 /* reserved_2 */ NULL,
75 (GtkClassInitFunc) NULL,
78 scale_type = gtk_type_unique (GTK_TYPE_RANGE, &scale_info);
85 gtk_scale_class_init (GtkScaleClass *class)
87 GtkObjectClass *object_class;
88 GtkWidgetClass *widget_class;
89 GtkRangeClass *range_class;
91 object_class = (GtkObjectClass*) class;
92 range_class = (GtkRangeClass*) class;
93 widget_class = (GtkWidgetClass*) class;
95 parent_class = gtk_type_class (GTK_TYPE_RANGE);
97 gtk_object_add_arg_type ("GtkScale::digits",
101 gtk_object_add_arg_type ("GtkScale::draw_value",
105 gtk_object_add_arg_type ("GtkScale::value_pos",
106 GTK_TYPE_POSITION_TYPE,
110 object_class->set_arg = gtk_scale_set_arg;
111 object_class->get_arg = gtk_scale_get_arg;
113 widget_class->map = gtk_scale_map;
114 widget_class->unmap = gtk_scale_unmap;
116 range_class->draw_background = gtk_scale_draw_background;
118 class->slider_length = 31;
119 class->value_spacing = 2;
120 class->draw_value = NULL;
124 gtk_scale_set_arg (GtkObject *object,
130 scale = GTK_SCALE (object);
135 gtk_scale_set_digits (scale, GTK_VALUE_INT (*arg));
138 gtk_scale_set_draw_value (scale, GTK_VALUE_BOOL (*arg));
141 gtk_scale_set_value_pos (scale, GTK_VALUE_ENUM (*arg));
149 gtk_scale_get_arg (GtkObject *object,
155 scale = GTK_SCALE (object);
160 GTK_VALUE_INT (*arg) = GTK_RANGE (scale)->digits;
163 GTK_VALUE_BOOL (*arg) = scale->draw_value;
166 GTK_VALUE_ENUM (*arg) = scale->value_pos;
169 arg->type = GTK_TYPE_INVALID;
175 gtk_scale_init (GtkScale *scale)
177 GTK_WIDGET_SET_FLAGS (scale, GTK_CAN_FOCUS);
178 GTK_WIDGET_SET_FLAGS (scale, GTK_NO_WINDOW);
179 GTK_RANGE (scale)->digits = 1;
180 scale->draw_value = TRUE;
181 scale->value_pos = GTK_POS_TOP;
185 gtk_scale_map (GtkWidget *widget)
189 g_return_if_fail (widget != NULL);
190 g_return_if_fail (GTK_IS_SCALE (widget));
192 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
193 range = GTK_RANGE (widget);
195 gdk_window_show (range->trough);
199 gtk_scale_unmap (GtkWidget *widget)
203 g_return_if_fail (widget != NULL);
204 g_return_if_fail (GTK_IS_SCALE (widget));
206 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
207 range = GTK_RANGE (widget);
209 if (GTK_WIDGET_NO_WINDOW (widget))
210 gtk_widget_queue_clear (widget);
212 gdk_window_hide (range->trough);
216 gtk_scale_set_digits (GtkScale *scale,
219 g_return_if_fail (scale != NULL);
220 g_return_if_fail (GTK_IS_SCALE (scale));
222 digits = CLAMP (digits, -1, 16);
224 if (GTK_RANGE (scale)->digits != digits)
226 GTK_RANGE (scale)->digits = digits;
228 gtk_widget_queue_resize (GTK_WIDGET (scale));
233 gtk_scale_set_draw_value (GtkScale *scale,
236 g_return_if_fail (scale != NULL);
237 g_return_if_fail (GTK_IS_SCALE (scale));
239 draw_value = draw_value != FALSE;
241 if (scale->draw_value != draw_value)
243 scale->draw_value = draw_value;
245 gtk_widget_queue_resize (GTK_WIDGET (scale));
250 gtk_scale_set_value_pos (GtkScale *scale,
253 g_return_if_fail (scale != NULL);
254 g_return_if_fail (GTK_IS_SCALE (scale));
256 if (scale->value_pos != pos)
258 scale->value_pos = pos;
260 if (GTK_WIDGET_VISIBLE (scale) && GTK_WIDGET_MAPPED (scale))
261 gtk_widget_queue_resize (GTK_WIDGET (scale));
266 gtk_scale_get_value_width (GtkScale *scale)
276 g_return_val_if_fail (scale != NULL, 0);
277 g_return_val_if_fail (GTK_IS_SCALE (scale), 0);
280 if (scale->draw_value)
282 range = GTK_RANGE (scale);
284 value = ABS (range->adjustment->lower);
285 if (value == 0) value = 1;
286 digits = log10 (value) + 1;
291 if (range->adjustment->lower < 0)
293 for (j = 0; j < digits; j++)
295 if (GTK_RANGE (scale)->digits)
297 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
301 return_val = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
303 value = ABS (range->adjustment->upper);
304 if (value == 0) value = 1;
305 digits = log10 (value) + 1;
310 if (range->adjustment->lower < 0)
312 for (j = 0; j < digits; j++)
314 if (GTK_RANGE (scale)->digits)
316 for (j = 0; j < GTK_RANGE (scale)->digits; j++)
320 temp = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
321 return_val = MAX (return_val, temp);
328 gtk_scale_draw_value (GtkScale *scale)
330 g_return_if_fail (scale != NULL);
331 g_return_if_fail (GTK_IS_SCALE (scale));
333 if (SCALE_CLASS (scale)->draw_value)
334 (* SCALE_CLASS (scale)->draw_value) (scale);
339 gtk_scale_draw_background (GtkRange *range)
341 g_return_if_fail (range != NULL);
342 g_return_if_fail (GTK_IS_SCALE (range));
344 gtk_scale_draw_value (GTK_SCALE (range));