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 "gtkvscale.h"
21 #include "gtksignal.h"
22 #include "gdk/gdkkeysyms.h"
25 #define SCALE_CLASS(w) GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
26 #define RANGE_CLASS(w) GTK_RANGE_CLASS (GTK_OBJECT (w)->klass)
33 static void gtk_vscale_class_init (GtkVScaleClass *klass);
34 static void gtk_vscale_init (GtkVScale *vscale);
35 static void gtk_vscale_set_arg (GtkObject *object,
38 static void gtk_vscale_get_arg (GtkObject *object,
41 static void gtk_vscale_realize (GtkWidget *widget);
42 static void gtk_vscale_size_request (GtkWidget *widget,
43 GtkRequisition *requisition);
44 static void gtk_vscale_size_allocate (GtkWidget *widget,
45 GtkAllocation *allocation);
46 static void gtk_vscale_pos_trough (GtkVScale *vscale,
51 static void gtk_vscale_pos_background (GtkVScale *vscale,
56 static void gtk_vscale_draw_slider (GtkRange *range);
57 static void gtk_vscale_draw_value (GtkScale *scale);
58 static void gtk_vscale_draw (GtkWidget *widget,
60 static gint gtk_vscale_trough_keys (GtkRange *range,
62 GtkScrollType *scroll,
64 static void gtk_vscale_clear_background (GtkRange *range);
67 gtk_vscale_get_type (void)
69 static GtkType vscale_type = 0;
73 static const GtkTypeInfo vscale_info =
77 sizeof (GtkVScaleClass),
78 (GtkClassInitFunc) gtk_vscale_class_init,
79 (GtkObjectInitFunc) gtk_vscale_init,
80 /* reserved_1 */ NULL,
81 /* reserved_2 */ NULL,
82 (GtkClassInitFunc) NULL,
85 vscale_type = gtk_type_unique (GTK_TYPE_SCALE, &vscale_info);
92 gtk_vscale_class_init (GtkVScaleClass *class)
94 GtkObjectClass *object_class;
95 GtkWidgetClass *widget_class;
96 GtkRangeClass *range_class;
97 GtkScaleClass *scale_class;
99 object_class = (GtkObjectClass*) class;
100 widget_class = (GtkWidgetClass*) class;
101 range_class = (GtkRangeClass*) class;
102 scale_class = (GtkScaleClass*) class;
104 gtk_object_add_arg_type ("GtkVScale::adjustment",
106 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
109 object_class->set_arg = gtk_vscale_set_arg;
110 object_class->get_arg = gtk_vscale_get_arg;
112 widget_class->realize = gtk_vscale_realize;
113 widget_class->size_request = gtk_vscale_size_request;
114 widget_class->size_allocate = gtk_vscale_size_allocate;
115 widget_class->draw = gtk_vscale_draw;
117 range_class->slider_update = gtk_range_default_vslider_update;
118 range_class->trough_click = gtk_range_default_vtrough_click;
119 range_class->motion = gtk_range_default_vmotion;
120 range_class->draw_slider = gtk_vscale_draw_slider;
121 range_class->trough_keys = gtk_vscale_trough_keys;
122 range_class->clear_background = gtk_vscale_clear_background;
124 scale_class->draw_value = gtk_vscale_draw_value;
128 gtk_vscale_set_arg (GtkObject *object,
134 vscale = GTK_VSCALE (object);
139 gtk_range_set_adjustment (GTK_RANGE (vscale), GTK_VALUE_POINTER (*arg));
147 gtk_vscale_get_arg (GtkObject *object,
153 vscale = GTK_VSCALE (object);
158 GTK_VALUE_POINTER (*arg) = GTK_RANGE (vscale);
161 arg->type = GTK_TYPE_INVALID;
167 gtk_vscale_init (GtkVScale *vscale)
169 GTK_WIDGET_SET_FLAGS (vscale, GTK_NO_WINDOW);
173 gtk_vscale_new (GtkAdjustment *adjustment)
177 vscale = gtk_widget_new (GTK_TYPE_VSCALE,
178 "adjustment", adjustment,
186 gtk_vscale_realize (GtkWidget *widget)
189 GdkWindowAttr attributes;
190 gint attributes_mask;
193 g_return_if_fail (widget != NULL);
194 g_return_if_fail (GTK_IS_VSCALE (widget));
196 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
197 range = GTK_RANGE (widget);
199 widget->window = gtk_widget_get_parent_window (widget);
200 gdk_window_ref (widget->window);
202 gtk_vscale_pos_trough (GTK_VSCALE (widget), &x, &y, &w, &h);
206 attributes.width = w;
207 attributes.height = h;
208 attributes.wclass = GDK_INPUT_OUTPUT;
209 attributes.window_type = GDK_WINDOW_CHILD;
211 attributes.event_mask = gtk_widget_get_events (widget) |
213 GDK_BUTTON_PRESS_MASK |
214 GDK_BUTTON_RELEASE_MASK |
215 GDK_ENTER_NOTIFY_MASK |
216 GDK_LEAVE_NOTIFY_MASK);
217 attributes.visual = gtk_widget_get_visual (widget);
218 attributes.colormap = gtk_widget_get_colormap (widget);
220 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
222 range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
224 attributes.width = RANGE_CLASS (range)->slider_width;
225 attributes.height = SCALE_CLASS (range)->slider_length;
226 attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
227 GDK_POINTER_MOTION_HINT_MASK);
229 range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
231 widget->style = gtk_style_attach (widget->style, widget->window);
233 gdk_window_set_user_data (range->trough, widget);
234 gdk_window_set_user_data (range->slider, widget);
236 gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
237 gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
239 gtk_range_slider_update (GTK_RANGE (widget));
241 gdk_window_show (range->slider);
245 gtk_vscale_draw (GtkWidget *widget,
249 GdkRectangle tmp_area;
250 GdkRectangle child_area;
251 gint x, y, width, height;
253 g_return_if_fail (widget != NULL);
254 g_return_if_fail (GTK_IS_RANGE (widget));
255 g_return_if_fail (area != NULL);
257 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
259 range = GTK_RANGE (widget);
261 gtk_vscale_pos_background (GTK_VSCALE (widget), &x, &y, &width, &height);
265 tmp_area.width = width;
266 tmp_area.height = height;
268 if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
269 gtk_range_draw_background (range);
271 gtk_vscale_pos_trough (GTK_VSCALE (widget), &x, &y, &width, &height);
275 tmp_area.width = width;
276 tmp_area.height = height;
278 if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
280 gtk_range_draw_trough (range);
281 gtk_range_draw_slider (range);
282 gtk_range_draw_step_forw (range);
283 gtk_range_draw_step_back (range);
289 gtk_vscale_clear_background (GtkRange *range)
293 gint x, y, width, height;
295 g_return_if_fail (range != NULL);
296 g_return_if_fail (GTK_IS_SCALE (range));
298 widget = GTK_WIDGET (range);
299 scale = GTK_SCALE (range);
301 gtk_vscale_pos_background (GTK_VSCALE (widget), &x, &y, &width, &height);
303 gtk_widget_queue_clear_area (GTK_WIDGET (range),
304 x, y, width, height);
308 gtk_vscale_size_request (GtkWidget *widget,
309 GtkRequisition *requisition)
314 g_return_if_fail (widget != NULL);
315 g_return_if_fail (GTK_IS_VSCALE (widget));
316 g_return_if_fail (requisition != NULL);
318 scale = GTK_SCALE (widget);
320 requisition->width = (RANGE_CLASS (scale)->slider_width +
321 widget->style->klass->ythickness * 2);
322 requisition->height = (SCALE_CLASS (scale)->slider_length +
323 widget->style->klass->xthickness) * 2;
325 if (scale->draw_value)
327 value_width = gtk_scale_get_value_width (scale);
329 if ((scale->value_pos == GTK_POS_LEFT) ||
330 (scale->value_pos == GTK_POS_RIGHT))
332 requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
333 if (requisition->height < (widget->style->font->ascent + widget->style->font->descent))
334 requisition->height = widget->style->font->ascent + widget->style->font->descent;
336 else if ((scale->value_pos == GTK_POS_TOP) ||
337 (scale->value_pos == GTK_POS_BOTTOM))
339 if (requisition->width < value_width)
340 requisition->width = value_width;
341 requisition->height += widget->style->font->ascent + widget->style->font->descent;
347 gtk_vscale_size_allocate (GtkWidget *widget,
348 GtkAllocation *allocation)
355 g_return_if_fail (widget != NULL);
356 g_return_if_fail (GTK_IS_VSCALE (widget));
357 g_return_if_fail (allocation != NULL);
359 widget->allocation = *allocation;
360 if (GTK_WIDGET_REALIZED (widget))
362 range = GTK_RANGE (widget);
363 scale = GTK_SCALE (widget);
365 gtk_vscale_pos_trough (GTK_VSCALE (widget), &x, &y, &width, &height);
367 gdk_window_move_resize (range->trough, x, y, width, height);
368 gtk_range_slider_update (GTK_RANGE (widget));
373 gtk_vscale_pos_trough (GtkVScale *vscale,
382 g_return_if_fail (vscale != NULL);
383 g_return_if_fail (GTK_IS_VSCALE (vscale));
384 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
386 widget = GTK_WIDGET (vscale);
387 scale = GTK_SCALE (vscale);
389 *w = (RANGE_CLASS (scale)->slider_width +
390 widget->style->klass->xthickness * 2);
391 *h = widget->allocation.height;
393 if (scale->draw_value)
398 switch (scale->value_pos)
401 *x = (gtk_scale_get_value_width (scale) +
402 (widget->allocation.width - widget->requisition.width) / 2);
405 *x = (widget->allocation.width - widget->requisition.width) / 2;
408 *x = (widget->allocation.width - *w) / 2;
409 *y = widget->style->font->ascent + widget->style->font->descent;
413 *x = (widget->allocation.width - *w) / 2;
414 *h -= widget->style->font->ascent + widget->style->font->descent;
420 *x = (widget->allocation.width - *w) / 2;
426 *x += widget->allocation.x;
427 *y += widget->allocation.y;
431 gtk_vscale_pos_background (GtkVScale *vscale,
440 gint tx, ty, twidth, theight;
442 g_return_if_fail (vscale != NULL);
443 g_return_if_fail (GTK_IS_VSCALE (vscale));
444 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
446 gtk_vscale_pos_trough (vscale, &tx, &ty, &twidth, &theight);
448 widget = GTK_WIDGET (vscale);
449 scale = GTK_SCALE (vscale);
451 *x = widget->allocation.x;
452 *y = widget->allocation.y;
453 *w = widget->allocation.width;
454 *h = widget->allocation.height;
456 switch (scale->value_pos)
476 gtk_vscale_draw_slider (GtkRange *range)
478 GtkStateType state_type;
480 g_return_if_fail (range != NULL);
481 g_return_if_fail (GTK_IS_VSCALE (range));
485 if ((range->in_child == RANGE_CLASS (range)->slider) ||
486 (range->click_child == RANGE_CLASS (range)->slider))
487 state_type = GTK_STATE_PRELIGHT;
489 state_type = GTK_STATE_NORMAL;
491 gtk_paint_slider (GTK_WIDGET (range)->style, range->slider, state_type,
493 NULL, GTK_WIDGET (range), "vscale",
495 GTK_ORIENTATION_VERTICAL);
500 gtk_vscale_draw_value (GtkScale *scale)
502 GtkStateType state_type;
509 g_return_if_fail (scale != NULL);
510 g_return_if_fail (GTK_IS_VSCALE (scale));
512 widget = GTK_WIDGET (scale);
514 if (scale->draw_value)
516 sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
517 text_width = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
519 switch (scale->value_pos)
522 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, NULL);
523 gdk_window_get_position (GTK_RANGE (scale)->slider, NULL, &y);
524 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, NULL);
525 gdk_window_get_size (GTK_RANGE (scale)->slider, NULL, &height);
527 x -= SCALE_CLASS (scale)->value_spacing + text_width;
528 y += widget->allocation.y + ((height -
529 (GTK_WIDGET (scale)->style->font->ascent +
530 GTK_WIDGET (scale)->style->font->descent)) / 2 +
531 GTK_WIDGET (scale)->style->font->ascent);
534 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, NULL);
535 gdk_window_get_position (GTK_RANGE (scale)->slider, NULL, &y);
536 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, NULL);
537 gdk_window_get_size (GTK_RANGE (scale)->slider, NULL, &height);
539 x += width + SCALE_CLASS (scale)->value_spacing;
540 y += widget->allocation.y + ((height -
541 (GTK_WIDGET (scale)->style->font->ascent +
542 GTK_WIDGET (scale)->style->font->descent)) / 2 +
543 GTK_WIDGET (scale)->style->font->ascent);
546 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
547 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
548 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
550 x += (width - text_width) / 2;
551 y -= GTK_WIDGET (scale)->style->font->descent;
554 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
555 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
556 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
558 x += (width - text_width) / 2;
559 y += height + GTK_WIDGET (scale)->style->font->ascent;
563 state_type = GTK_STATE_NORMAL;
564 if (!GTK_WIDGET_IS_SENSITIVE (scale))
565 state_type = GTK_STATE_INSENSITIVE;
567 gtk_paint_string (GTK_WIDGET (scale)->style,
568 GTK_WIDGET (scale)->window,
570 NULL, GTK_WIDGET (scale), "vscale",
576 gtk_vscale_trough_keys (GtkRange *range,
578 GtkScrollType *scroll,
581 gint return_val = FALSE;
586 *scroll = GTK_SCROLL_STEP_BACKWARD;
590 *scroll = GTK_SCROLL_STEP_FORWARD;
594 *scroll = GTK_SCROLL_PAGE_BACKWARD;
598 *scroll = GTK_SCROLL_PAGE_FORWARD;
602 *pos = GTK_TROUGH_START;
606 *pos = GTK_TROUGH_END;