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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "gtkhscale.h"
20 #include "gtksignal.h"
21 #include "gdk/gdkkeysyms.h"
24 #define SCALE_CLASS(w) GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
25 #define RANGE_CLASS(w) GTK_RANGE_CLASS (GTK_OBJECT (w)->klass)
28 static void gtk_hscale_class_init (GtkHScaleClass *klass);
29 static void gtk_hscale_init (GtkHScale *hscale);
30 static void gtk_hscale_realize (GtkWidget *widget);
31 static void gtk_hscale_size_request (GtkWidget *widget,
32 GtkRequisition *requisition);
33 static void gtk_hscale_size_allocate (GtkWidget *widget,
34 GtkAllocation *allocation);
35 static void gtk_hscale_pos_trough (GtkHScale *hscale,
40 static void gtk_hscale_draw_slider (GtkRange *range);
41 static void gtk_hscale_draw_value (GtkScale *scale);
42 static gint gtk_hscale_trough_keys (GtkRange *range,
44 GtkScrollType *scroll,
48 gtk_hscale_get_type ()
50 static guint hscale_type = 0;
54 GtkTypeInfo hscale_info =
58 sizeof (GtkHScaleClass),
59 (GtkClassInitFunc) gtk_hscale_class_init,
60 (GtkObjectInitFunc) gtk_hscale_init,
64 hscale_type = gtk_type_unique (gtk_scale_get_type (), &hscale_info);
71 gtk_hscale_class_init (GtkHScaleClass *class)
73 GtkWidgetClass *widget_class;
74 GtkRangeClass *range_class;
75 GtkScaleClass *scale_class;
77 widget_class = (GtkWidgetClass*) class;
78 range_class = (GtkRangeClass*) class;
79 scale_class = (GtkScaleClass*) class;
81 widget_class->realize = gtk_hscale_realize;
82 widget_class->size_request = gtk_hscale_size_request;
83 widget_class->size_allocate = gtk_hscale_size_allocate;
85 range_class->slider_update = gtk_range_default_hslider_update;
86 range_class->trough_click = gtk_range_default_htrough_click;
87 range_class->motion = gtk_range_default_hmotion;
88 range_class->draw_slider = gtk_hscale_draw_slider;
89 range_class->trough_keys = gtk_hscale_trough_keys;
91 scale_class->draw_value = gtk_hscale_draw_value;
95 gtk_hscale_init (GtkHScale *hscale)
100 gtk_hscale_new (GtkAdjustment *adjustment)
104 hscale = gtk_type_new (gtk_hscale_get_type ());
107 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
109 gtk_range_set_adjustment (GTK_RANGE (hscale), adjustment);
111 return GTK_WIDGET (hscale);
116 gtk_hscale_realize (GtkWidget *widget)
119 GdkWindowAttr attributes;
120 gint attributes_mask;
123 g_return_if_fail (widget != NULL);
124 g_return_if_fail (GTK_IS_HSCALE (widget));
126 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
127 range = GTK_RANGE (widget);
129 attributes.x = widget->allocation.x;
130 attributes.y = widget->allocation.y;
131 attributes.width = widget->allocation.width;
132 attributes.height = widget->allocation.height;
133 attributes.wclass = GDK_INPUT_OUTPUT;
134 attributes.window_type = GDK_WINDOW_CHILD;
135 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
136 attributes.visual = gtk_widget_get_visual (widget);
137 attributes.colormap = gtk_widget_get_colormap (widget);
139 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
140 widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
142 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &w, &h);
145 attributes.width = w;
146 attributes.height = h;
147 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
148 GDK_BUTTON_RELEASE_MASK |
149 GDK_ENTER_NOTIFY_MASK |
150 GDK_LEAVE_NOTIFY_MASK);
152 range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
154 attributes.width = SCALE_CLASS (range)->slider_length;
155 attributes.height = RANGE_CLASS (range)->slider_width;
156 attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
157 GDK_POINTER_MOTION_HINT_MASK);
159 range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
161 widget->style = gtk_style_attach (widget->style, widget->window);
163 gdk_window_set_user_data (widget->window, widget);
164 gdk_window_set_user_data (range->trough, widget);
165 gdk_window_set_user_data (range->slider, widget);
167 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
168 gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
169 gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
171 gtk_range_slider_update (GTK_RANGE (widget));
173 gdk_window_show (range->slider);
174 gdk_window_show (range->trough);
178 gtk_hscale_size_request (GtkWidget *widget,
179 GtkRequisition *requisition)
184 g_return_if_fail (widget != NULL);
185 g_return_if_fail (GTK_IS_HSCALE (widget));
186 g_return_if_fail (requisition != NULL);
188 scale = GTK_SCALE (widget);
190 requisition->width = (SCALE_CLASS (scale)->slider_length +
191 widget->style->klass->xthickness) * 2;
192 requisition->height = (RANGE_CLASS (scale)->slider_width +
193 widget->style->klass->ythickness * 2);
195 if (scale->draw_value)
197 value_width = gtk_scale_value_width (scale);
199 if ((scale->value_pos == GTK_POS_LEFT) ||
200 (scale->value_pos == GTK_POS_RIGHT))
202 requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
203 if (requisition->height < (widget->style->font->ascent + widget->style->font->descent))
204 requisition->height = widget->style->font->ascent + widget->style->font->descent;
206 else if ((scale->value_pos == GTK_POS_TOP) ||
207 (scale->value_pos == GTK_POS_BOTTOM))
209 if (requisition->width < value_width)
210 requisition->width = value_width;
211 requisition->height += widget->style->font->ascent + widget->style->font->descent;
217 gtk_hscale_size_allocate (GtkWidget *widget,
218 GtkAllocation *allocation)
225 g_return_if_fail (widget != NULL);
226 g_return_if_fail (GTK_IS_HSCALE (widget));
227 g_return_if_fail (allocation != NULL);
229 widget->allocation = *allocation;
230 if (GTK_WIDGET_REALIZED (widget))
232 range = GTK_RANGE (widget);
233 scale = GTK_SCALE (widget);
235 gdk_window_move_resize (widget->window,
236 allocation->x, allocation->y,
237 allocation->width, allocation->height);
239 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
241 gdk_window_move_resize (range->trough, x, y, width, height);
242 gtk_range_slider_update (GTK_RANGE (widget));
247 gtk_hscale_pos_trough (GtkHScale *hscale,
256 g_return_if_fail (hscale != NULL);
257 g_return_if_fail (GTK_IS_HSCALE (hscale));
258 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
260 widget = GTK_WIDGET (hscale);
261 scale = GTK_SCALE (hscale);
263 *w = widget->allocation.width;
264 *h = (RANGE_CLASS (scale)->slider_width +
265 widget->style->klass->ythickness * 2);
267 if (scale->draw_value)
272 switch (scale->value_pos)
275 *x += gtk_scale_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
276 *y = (widget->allocation.height - *h) / 2;
280 *w -= gtk_scale_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
281 *y = (widget->allocation.height - *h) / 2;
284 *y = (widget->style->font->ascent + widget->style->font->descent +
285 (widget->allocation.height - widget->requisition.height) / 2);
288 *y = (widget->allocation.height - widget->requisition.height) / 2;
295 *y = (widget->allocation.height - *h) / 2;
302 gtk_hscale_draw_slider (GtkRange *range)
304 GtkStateType state_type;
307 g_return_if_fail (range != NULL);
308 g_return_if_fail (GTK_IS_HSCALE (range));
312 if ((range->in_child == RANGE_CLASS (range)->slider) ||
313 (range->click_child == RANGE_CLASS (range)->slider))
314 state_type = GTK_STATE_PRELIGHT;
316 state_type = GTK_STATE_NORMAL;
318 gtk_style_set_background (GTK_WIDGET (range)->style, range->slider, state_type);
319 gdk_window_clear (range->slider);
321 gdk_window_get_size (range->slider, &width, &height);
322 gtk_draw_vline (GTK_WIDGET (range)->style, range->slider,
323 state_type, 1, height - 2, width / 2);
325 gtk_draw_shadow (GTK_WIDGET (range)->style, range->slider,
326 state_type, GTK_SHADOW_OUT,
332 gtk_hscale_draw_value (GtkScale *scale)
334 GtkStateType state_type;
340 g_return_if_fail (scale != NULL);
341 g_return_if_fail (GTK_IS_HSCALE (scale));
343 if (scale->draw_value)
345 gdk_window_get_size (GTK_WIDGET (scale)->window, &width, &height);
346 gdk_window_clear_area (GTK_WIDGET (scale)->window, 1, 1, width - 2, height - 2);
348 sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
349 text_width = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
351 switch (scale->value_pos)
354 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
355 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
357 x -= SCALE_CLASS (scale)->value_spacing + text_width;
359 (GTK_WIDGET (scale)->style->font->ascent +
360 GTK_WIDGET (scale)->style->font->descent)) / 2 +
361 GTK_WIDGET (scale)->style->font->ascent);
364 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
365 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
367 x += width + SCALE_CLASS (scale)->value_spacing;
369 (GTK_WIDGET (scale)->style->font->ascent +
370 GTK_WIDGET (scale)->style->font->descent)) / 2 +
371 GTK_WIDGET (scale)->style->font->ascent);
374 gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
375 gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
376 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
377 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
379 x += (width - text_width) / 2;
380 y -= GTK_WIDGET (scale)->style->font->descent;
383 gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
384 gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
385 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
386 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
388 x += (width - text_width) / 2;
389 y += height + GTK_WIDGET (scale)->style->font->ascent;
393 state_type = GTK_STATE_NORMAL;
394 if (!GTK_WIDGET_IS_SENSITIVE (scale))
395 state_type = GTK_STATE_INSENSITIVE;
397 gtk_draw_string (GTK_WIDGET (scale)->style,
398 GTK_WIDGET (scale)->window,
399 state_type, x, y, buffer);
404 gtk_hscale_trough_keys(GtkRange *range,
406 GtkScrollType *scroll,
409 gint return_val = FALSE;
414 if (key->state & GDK_CONTROL_MASK)
415 *scroll = GTK_SCROLL_PAGE_BACKWARD;
417 *scroll = GTK_SCROLL_STEP_BACKWARD;
421 if (key->state & GDK_CONTROL_MASK)
422 *scroll = GTK_SCROLL_PAGE_FORWARD;
424 *scroll = GTK_SCROLL_STEP_FORWARD;
428 *pos = GTK_TROUGH_START;
432 *pos = GTK_TROUGH_END;