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 "gtkhscale.h"
29 #include "gtksignal.h"
30 #include "gdk/gdkkeysyms.h"
33 #define SCALE_CLASS(w) GTK_SCALE_CLASS (GTK_OBJECT (w)->klass)
34 #define RANGE_CLASS(w) GTK_RANGE_CLASS (GTK_OBJECT (w)->klass)
41 static void gtk_hscale_class_init (GtkHScaleClass *klass);
42 static void gtk_hscale_init (GtkHScale *hscale);
43 static void gtk_hscale_set_arg (GtkObject *object,
46 static void gtk_hscale_get_arg (GtkObject *object,
49 static void gtk_hscale_realize (GtkWidget *widget);
50 static void gtk_hscale_size_request (GtkWidget *widget,
51 GtkRequisition *requisition);
52 static void gtk_hscale_size_allocate (GtkWidget *widget,
53 GtkAllocation *allocation);
54 static void gtk_hscale_pos_trough (GtkHScale *hscale,
59 static void gtk_hscale_pos_background (GtkHScale *hscale,
64 static void gtk_hscale_draw_slider (GtkRange *range);
65 static void gtk_hscale_draw_value (GtkScale *scale);
66 static void gtk_hscale_draw (GtkWidget *widget,
68 static gint gtk_hscale_trough_keys (GtkRange *range,
70 GtkScrollType *scroll,
72 static void gtk_hscale_clear_background (GtkRange *range);
75 gtk_hscale_get_type (void)
77 static GtkType hscale_type = 0;
81 static const GtkTypeInfo hscale_info =
85 sizeof (GtkHScaleClass),
86 (GtkClassInitFunc) gtk_hscale_class_init,
87 (GtkObjectInitFunc) gtk_hscale_init,
88 /* reserved_1 */ NULL,
89 /* reserved_2 */ NULL,
90 (GtkClassInitFunc) NULL,
93 hscale_type = gtk_type_unique (GTK_TYPE_SCALE, &hscale_info);
100 gtk_hscale_class_init (GtkHScaleClass *class)
102 GtkObjectClass *object_class;
103 GtkWidgetClass *widget_class;
104 GtkRangeClass *range_class;
105 GtkScaleClass *scale_class;
107 object_class = (GtkObjectClass*) class;
108 widget_class = (GtkWidgetClass*) class;
109 range_class = (GtkRangeClass*) class;
110 scale_class = (GtkScaleClass*) class;
112 gtk_object_add_arg_type ("GtkHScale::adjustment",
114 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
117 object_class->set_arg = gtk_hscale_set_arg;
118 object_class->get_arg = gtk_hscale_get_arg;
120 widget_class->realize = gtk_hscale_realize;
121 widget_class->size_request = gtk_hscale_size_request;
122 widget_class->size_allocate = gtk_hscale_size_allocate;
123 widget_class->draw = gtk_hscale_draw;
125 range_class->slider_update = gtk_range_default_hslider_update;
126 range_class->trough_click = gtk_range_default_htrough_click;
127 range_class->motion = gtk_range_default_hmotion;
128 range_class->draw_slider = gtk_hscale_draw_slider;
129 range_class->trough_keys = gtk_hscale_trough_keys;
130 range_class->clear_background = gtk_hscale_clear_background;
132 scale_class->draw_value = gtk_hscale_draw_value;
136 gtk_hscale_set_arg (GtkObject *object,
142 hscale = GTK_HSCALE (object);
147 gtk_range_set_adjustment (GTK_RANGE (hscale), GTK_VALUE_POINTER (*arg));
155 gtk_hscale_get_arg (GtkObject *object,
161 hscale = GTK_HSCALE (object);
166 GTK_VALUE_POINTER (*arg) = GTK_RANGE (hscale);
169 arg->type = GTK_TYPE_INVALID;
175 gtk_hscale_init (GtkHScale *hscale)
177 GTK_WIDGET_SET_FLAGS (hscale, GTK_NO_WINDOW);
181 gtk_hscale_new (GtkAdjustment *adjustment)
185 hscale = gtk_widget_new (GTK_TYPE_HSCALE,
186 "adjustment", adjustment,
194 gtk_hscale_realize (GtkWidget *widget)
197 GdkWindowAttr attributes;
198 gint attributes_mask;
201 g_return_if_fail (widget != NULL);
202 g_return_if_fail (GTK_IS_HSCALE (widget));
204 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
205 range = GTK_RANGE (widget);
207 widget->window = gtk_widget_get_parent_window (widget);
208 gdk_window_ref (widget->window);
210 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &w, &h);
214 attributes.width = w;
215 attributes.height = h;
216 attributes.wclass = GDK_INPUT_OUTPUT;
217 attributes.window_type = GDK_WINDOW_CHILD;
219 attributes.event_mask = gtk_widget_get_events (widget) |
221 GDK_BUTTON_PRESS_MASK |
222 GDK_BUTTON_RELEASE_MASK |
223 GDK_ENTER_NOTIFY_MASK |
224 GDK_LEAVE_NOTIFY_MASK);
225 attributes.visual = gtk_widget_get_visual (widget);
226 attributes.colormap = gtk_widget_get_colormap (widget);
228 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
230 range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
232 attributes.width = SCALE_CLASS (range)->slider_length;
233 attributes.height = RANGE_CLASS (range)->slider_width;
234 attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
235 GDK_POINTER_MOTION_HINT_MASK);
237 range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
239 widget->style = gtk_style_attach (widget->style, widget->window);
241 gdk_window_set_user_data (range->trough, widget);
242 gdk_window_set_user_data (range->slider, widget);
244 gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
245 gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
247 gtk_range_slider_update (GTK_RANGE (widget));
249 gdk_window_show (range->slider);
253 gtk_hscale_draw (GtkWidget *widget,
257 GdkRectangle tmp_area;
258 GdkRectangle child_area;
259 gint x, y, width, height;
261 g_return_if_fail (widget != NULL);
262 g_return_if_fail (GTK_IS_RANGE (widget));
263 g_return_if_fail (area != NULL);
265 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
267 range = GTK_RANGE (widget);
269 gtk_hscale_pos_background (GTK_HSCALE (widget), &x, &y, &width, &height);
273 tmp_area.width = width;
274 tmp_area.height = height;
276 if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
277 gtk_range_draw_background (range);
279 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
283 tmp_area.width = width;
284 tmp_area.height = height;
286 if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
288 gtk_range_draw_trough (range);
289 gtk_range_draw_slider (range);
290 gtk_range_draw_step_forw (range);
291 gtk_range_draw_step_back (range);
297 gtk_hscale_clear_background (GtkRange *range)
300 gint x, y, width, height;
302 g_return_if_fail (range != NULL);
304 widget = GTK_WIDGET (range);
306 gtk_hscale_pos_background (GTK_HSCALE (range), &x, &y, &width, &height);
308 gtk_widget_queue_clear_area (GTK_WIDGET (range),
309 x, y, width, height);
313 gtk_hscale_size_request (GtkWidget *widget,
314 GtkRequisition *requisition)
319 g_return_if_fail (widget != NULL);
320 g_return_if_fail (GTK_IS_HSCALE (widget));
321 g_return_if_fail (requisition != NULL);
323 scale = GTK_SCALE (widget);
325 requisition->width = (SCALE_CLASS (scale)->slider_length +
326 widget->style->klass->xthickness) * 2;
327 requisition->height = (RANGE_CLASS (scale)->slider_width +
328 widget->style->klass->ythickness * 2);
330 if (scale->draw_value)
332 value_width = gtk_scale_get_value_width (scale);
334 if ((scale->value_pos == GTK_POS_LEFT) ||
335 (scale->value_pos == GTK_POS_RIGHT))
337 requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
338 if (requisition->height < (widget->style->font->ascent + widget->style->font->descent))
339 requisition->height = widget->style->font->ascent + widget->style->font->descent;
341 else if ((scale->value_pos == GTK_POS_TOP) ||
342 (scale->value_pos == GTK_POS_BOTTOM))
344 if (requisition->width < value_width)
345 requisition->width = value_width;
346 requisition->height += widget->style->font->ascent + widget->style->font->descent;
352 gtk_hscale_size_allocate (GtkWidget *widget,
353 GtkAllocation *allocation)
360 g_return_if_fail (widget != NULL);
361 g_return_if_fail (GTK_IS_HSCALE (widget));
362 g_return_if_fail (allocation != NULL);
364 widget->allocation = *allocation;
365 if (GTK_WIDGET_REALIZED (widget))
367 range = GTK_RANGE (widget);
368 scale = GTK_SCALE (widget);
370 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
372 gdk_window_move_resize (range->trough,
373 x, y, width, height);
374 gtk_range_slider_update (GTK_RANGE (widget));
379 gtk_hscale_pos_trough (GtkHScale *hscale,
388 g_return_if_fail (hscale != NULL);
389 g_return_if_fail (GTK_IS_HSCALE (hscale));
390 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
392 widget = GTK_WIDGET (hscale);
393 scale = GTK_SCALE (hscale);
395 *w = widget->allocation.width;
396 *h = (RANGE_CLASS (scale)->slider_width +
397 widget->style->klass->ythickness * 2);
399 if (scale->draw_value)
404 switch (scale->value_pos)
407 *x += gtk_scale_get_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
408 *y = (widget->allocation.height - *h) / 2;
412 *w -= gtk_scale_get_value_width (scale) + SCALE_CLASS (scale)->value_spacing;
413 *y = (widget->allocation.height - *h) / 2;
416 *y = (widget->style->font->ascent + widget->style->font->descent +
417 (widget->allocation.height - widget->requisition.height) / 2);
420 *y = (widget->allocation.height - widget->requisition.height) / 2;
427 *y = (widget->allocation.height - *h) / 2;
432 *x += widget->allocation.x;
433 *y += widget->allocation.y;
437 gtk_hscale_pos_background (GtkHScale *hscale,
446 gint tx, ty, twidth, theight;
448 g_return_if_fail (hscale != NULL);
449 g_return_if_fail (GTK_IS_HSCALE (hscale));
450 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
452 gtk_hscale_pos_trough (hscale, &tx, &ty, &twidth, &theight);
454 widget = GTK_WIDGET (hscale);
455 scale = GTK_SCALE (hscale);
457 *x = widget->allocation.x;
458 *y = widget->allocation.y;
459 *w = widget->allocation.width;
460 *h = widget->allocation.height;
462 switch (scale->value_pos)
482 gtk_hscale_draw_slider (GtkRange *range)
484 GtkStateType state_type;
486 g_return_if_fail (range != NULL);
487 g_return_if_fail (GTK_IS_HSCALE (range));
491 if ((range->in_child == RANGE_CLASS (range)->slider) ||
492 (range->click_child == RANGE_CLASS (range)->slider))
493 state_type = GTK_STATE_PRELIGHT;
495 state_type = GTK_STATE_NORMAL;
497 gtk_paint_slider (GTK_WIDGET (range)->style, range->slider, state_type,
499 NULL, GTK_WIDGET (range), "hscale",
501 GTK_ORIENTATION_HORIZONTAL);
506 gtk_hscale_draw_value (GtkScale *scale)
508 GtkStateType state_type;
515 g_return_if_fail (scale != NULL);
516 g_return_if_fail (GTK_IS_HSCALE (scale));
518 widget = GTK_WIDGET (scale);
520 if (scale->draw_value)
522 sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
523 text_width = gdk_string_measure (GTK_WIDGET (scale)->style->font, buffer);
525 switch (scale->value_pos)
528 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
529 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
531 x -= SCALE_CLASS (scale)->value_spacing + text_width;
533 (GTK_WIDGET (scale)->style->font->ascent +
534 GTK_WIDGET (scale)->style->font->descent)) / 2 +
535 GTK_WIDGET (scale)->style->font->ascent);
538 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
539 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
541 x += width + SCALE_CLASS (scale)->value_spacing;
543 (GTK_WIDGET (scale)->style->font->ascent +
544 GTK_WIDGET (scale)->style->font->descent)) / 2 +
545 GTK_WIDGET (scale)->style->font->ascent);
548 gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
549 gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
550 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
551 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
553 x += widget->allocation.x + (width - text_width) / 2;
554 x = CLAMP (x, widget->allocation.x,
555 widget->allocation.x + widget->allocation.width - text_width);
556 y -= GTK_WIDGET (scale)->style->font->descent;
559 gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
560 gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
561 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
562 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
564 x += widget->allocation.x + (width - text_width) / 2;
565 x = CLAMP (x, widget->allocation.x,
566 widget->allocation.x + widget->allocation.width - text_width);
567 y += height + GTK_WIDGET (scale)->style->font->ascent;
571 state_type = GTK_STATE_NORMAL;
572 if (!GTK_WIDGET_IS_SENSITIVE (scale))
573 state_type = GTK_STATE_INSENSITIVE;
575 gtk_paint_string (GTK_WIDGET (scale)->style,
576 GTK_WIDGET (scale)->window,
578 NULL, GTK_WIDGET (scale), "hscale",
584 gtk_hscale_trough_keys (GtkRange *range,
586 GtkScrollType *scroll,
589 gint return_val = FALSE;
594 if (key->state & GDK_CONTROL_MASK)
595 *scroll = GTK_SCROLL_PAGE_BACKWARD;
597 *scroll = GTK_SCROLL_STEP_BACKWARD;
601 if (key->state & GDK_CONTROL_MASK)
602 *scroll = GTK_SCROLL_PAGE_FORWARD;
604 *scroll = GTK_SCROLL_STEP_FORWARD;
608 *pos = GTK_TROUGH_START;
612 *pos = GTK_TROUGH_END;