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 "gtkvscale.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_vscale_class_init (GtkVScaleClass *klass);
29 static void gtk_vscale_init (GtkVScale *vscale);
30 static void gtk_vscale_realize (GtkWidget *widget);
31 static void gtk_vscale_size_request (GtkWidget *widget,
32 GtkRequisition *requisition);
33 static void gtk_vscale_size_allocate (GtkWidget *widget,
34 GtkAllocation *allocation);
35 static void gtk_vscale_pos_trough (GtkVScale *vscale,
40 static void gtk_vscale_draw_slider (GtkRange *range);
41 static void gtk_vscale_draw_value (GtkScale *scale);
42 static gint gtk_vscale_trough_keys (GtkRange *range,
44 GtkScrollType *scroll,
49 gtk_vscale_get_type ()
51 static guint vscale_type = 0;
55 GtkTypeInfo vscale_info =
59 sizeof (GtkVScaleClass),
60 (GtkClassInitFunc) gtk_vscale_class_init,
61 (GtkObjectInitFunc) gtk_vscale_init,
66 vscale_type = gtk_type_unique (gtk_scale_get_type (), &vscale_info);
73 gtk_vscale_class_init (GtkVScaleClass *class)
75 GtkWidgetClass *widget_class;
76 GtkRangeClass *range_class;
77 GtkScaleClass *scale_class;
79 widget_class = (GtkWidgetClass*) class;
80 range_class = (GtkRangeClass*) class;
81 scale_class = (GtkScaleClass*) class;
83 widget_class->realize = gtk_vscale_realize;
84 widget_class->size_request = gtk_vscale_size_request;
85 widget_class->size_allocate = gtk_vscale_size_allocate;
87 range_class->slider_update = gtk_range_default_vslider_update;
88 range_class->trough_click = gtk_range_default_vtrough_click;
89 range_class->motion = gtk_range_default_vmotion;
90 range_class->draw_slider = gtk_vscale_draw_slider;
91 range_class->trough_keys = gtk_vscale_trough_keys;
93 scale_class->draw_value = gtk_vscale_draw_value;
97 gtk_vscale_init (GtkVScale *vscale)
102 gtk_vscale_new (GtkAdjustment *adjustment)
106 vscale = gtk_type_new (gtk_vscale_get_type ());
109 adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
111 gtk_range_set_adjustment (GTK_RANGE (vscale), adjustment);
113 return GTK_WIDGET (vscale);
118 gtk_vscale_realize (GtkWidget *widget)
121 GdkWindowAttr attributes;
122 gint attributes_mask;
125 g_return_if_fail (widget != NULL);
126 g_return_if_fail (GTK_IS_VSCALE (widget));
128 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
129 range = GTK_RANGE (widget);
131 attributes.x = widget->allocation.x;
132 attributes.y = widget->allocation.y;
133 attributes.width = widget->allocation.width;
134 attributes.height = widget->allocation.height;
135 attributes.wclass = GDK_INPUT_OUTPUT;
136 attributes.window_type = GDK_WINDOW_CHILD;
137 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
138 attributes.visual = gtk_widget_get_visual (widget);
139 attributes.colormap = gtk_widget_get_colormap (widget);
141 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
142 widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
144 gtk_vscale_pos_trough (GTK_VSCALE (widget), &x, &y, &w, &h);
147 attributes.width = w;
148 attributes.height = h;
149 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
150 GDK_BUTTON_RELEASE_MASK |
151 GDK_ENTER_NOTIFY_MASK |
152 GDK_LEAVE_NOTIFY_MASK);
154 range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
156 attributes.width = RANGE_CLASS (range)->slider_width;
157 attributes.height = SCALE_CLASS (range)->slider_length;
158 attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
159 GDK_POINTER_MOTION_HINT_MASK);
161 range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
163 widget->style = gtk_style_attach (widget->style, widget->window);
165 gdk_window_set_user_data (widget->window, widget);
166 gdk_window_set_user_data (range->trough, widget);
167 gdk_window_set_user_data (range->slider, widget);
169 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
170 gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
171 gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
173 gtk_range_slider_update (GTK_RANGE (widget));
175 gdk_window_show (range->slider);
176 gdk_window_show (range->trough);
180 gtk_vscale_size_request (GtkWidget *widget,
181 GtkRequisition *requisition)
186 g_return_if_fail (widget != NULL);
187 g_return_if_fail (GTK_IS_VSCALE (widget));
188 g_return_if_fail (requisition != NULL);
190 scale = GTK_SCALE (widget);
192 requisition->width = (RANGE_CLASS (scale)->slider_width +
193 widget->style->klass->ythickness * 2);
194 requisition->height = (SCALE_CLASS (scale)->slider_length +
195 widget->style->klass->xthickness) * 2;
197 if (scale->draw_value)
199 value_width = gtk_scale_value_width (scale);
201 if ((scale->value_pos == GTK_POS_LEFT) ||
202 (scale->value_pos == GTK_POS_RIGHT))
204 requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
205 if (requisition->height < (widget->style->font->ascent + widget->style->font->descent))
206 requisition->height = widget->style->font->ascent + widget->style->font->descent;
208 else if ((scale->value_pos == GTK_POS_TOP) ||
209 (scale->value_pos == GTK_POS_BOTTOM))
211 if (requisition->width < value_width)
212 requisition->width = value_width;
213 requisition->height += widget->style->font->ascent + widget->style->font->descent;
219 gtk_vscale_size_allocate (GtkWidget *widget,
220 GtkAllocation *allocation)
227 g_return_if_fail (widget != NULL);
228 g_return_if_fail (GTK_IS_VSCALE (widget));
229 g_return_if_fail (allocation != NULL);
231 widget->allocation = *allocation;
232 if (GTK_WIDGET_REALIZED (widget))
234 range = GTK_RANGE (widget);
235 scale = GTK_SCALE (widget);
237 gdk_window_move_resize (widget->window,
238 allocation->x, allocation->y,
239 allocation->width, allocation->height);
241 gtk_vscale_pos_trough (GTK_VSCALE (widget), &x, &y, &width, &height);
243 gdk_window_move_resize (range->trough, x, y, width, height);
244 gtk_range_slider_update (GTK_RANGE (widget));
249 gtk_vscale_pos_trough (GtkVScale *vscale,
258 g_return_if_fail (vscale != NULL);
259 g_return_if_fail (GTK_IS_VSCALE (vscale));
260 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
262 widget = GTK_WIDGET (vscale);
263 scale = GTK_SCALE (vscale);
265 *w = (RANGE_CLASS (scale)->slider_width +
266 widget->style->klass->xthickness * 2);
267 *h = widget->allocation.height;
269 if (scale->draw_value)
274 switch (scale->value_pos)
277 *x = (gtk_scale_value_width (scale) +
278 (widget->allocation.width - widget->requisition.width) / 2);
281 *x = (widget->allocation.width - widget->requisition.width) / 2;
284 *x = (widget->allocation.width - *w) / 2;
285 *y = widget->style->font->ascent + widget->style->font->descent;
289 *x = (widget->allocation.width - *w) / 2;
290 *h -= widget->style->font->ascent + widget->style->font->descent;
296 *x = (widget->allocation.width - *w) / 2;
304 gtk_vscale_draw_slider (GtkRange *range)
306 GtkStateType state_type;
309 g_return_if_fail (range != NULL);
310 g_return_if_fail (GTK_IS_VSCALE (range));
314 if ((range->in_child == RANGE_CLASS (range)->slider) ||
315 (range->click_child == RANGE_CLASS (range)->slider))
316 state_type = GTK_STATE_PRELIGHT;
318 state_type = GTK_STATE_NORMAL;
320 gtk_style_set_background (GTK_WIDGET (range)->style, range->slider, state_type);
321 gdk_window_clear (range->slider);
323 gdk_window_get_size (range->slider, &width, &height);
324 gtk_draw_hline (GTK_WIDGET (range)->style, range->slider,
325 state_type, 1, width - 2, height / 2);
327 gtk_draw_shadow (GTK_WIDGET (range)->style, range->slider,
328 state_type, GTK_SHADOW_OUT,
334 gtk_vscale_draw_value (GtkScale *scale)
336 GtkStateType state_type;
342 g_return_if_fail (scale != NULL);
343 g_return_if_fail (GTK_IS_VSCALE (scale));
345 if (scale->draw_value)
347 gdk_window_get_size (GTK_WIDGET (scale)->window, &width, &height);
348 gdk_window_clear_area (GTK_WIDGET (scale)->window, 1, 1, width - 3, height - 3);
350 sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
351 text_width = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
353 switch (scale->value_pos)
356 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, NULL);
357 gdk_window_get_position (GTK_RANGE (scale)->slider, NULL, &y);
358 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, NULL);
359 gdk_window_get_size (GTK_RANGE (scale)->slider, NULL, &height);
361 x -= SCALE_CLASS (scale)->value_spacing + text_width;
363 (GTK_WIDGET (scale)->style->font->ascent +
364 GTK_WIDGET (scale)->style->font->descent)) / 2 +
365 GTK_WIDGET (scale)->style->font->ascent);
368 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, NULL);
369 gdk_window_get_position (GTK_RANGE (scale)->slider, NULL, &y);
370 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, NULL);
371 gdk_window_get_size (GTK_RANGE (scale)->slider, NULL, &height);
373 x += width + SCALE_CLASS (scale)->value_spacing;
375 (GTK_WIDGET (scale)->style->font->ascent +
376 GTK_WIDGET (scale)->style->font->descent)) / 2 +
377 GTK_WIDGET (scale)->style->font->ascent);
380 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
381 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
382 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
384 x += (width - text_width) / 2;
385 y -= GTK_WIDGET (scale)->style->font->descent;
388 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
389 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
390 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
392 x += (width - text_width) / 2;
393 y += height + GTK_WIDGET (scale)->style->font->ascent;
397 state_type = GTK_STATE_NORMAL;
398 if (!GTK_WIDGET_IS_SENSITIVE (scale))
399 state_type = GTK_STATE_INSENSITIVE;
401 gtk_draw_string (GTK_WIDGET (scale)->style,
402 GTK_WIDGET (scale)->window,
403 state_type, x, y, buffer);
408 gtk_vscale_trough_keys(GtkRange *range,
410 GtkScrollType *scroll,
413 gint return_val = FALSE;
418 *scroll = GTK_SCROLL_STEP_BACKWARD;
422 *scroll = GTK_SCROLL_STEP_FORWARD;
426 *scroll = GTK_SCROLL_PAGE_BACKWARD;
430 *scroll = GTK_SCROLL_PAGE_FORWARD;
434 *pos = GTK_TROUGH_START;
438 *pos = GTK_TROUGH_END;