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"
32 #define SCALE_CLASS(w) GTK_SCALE_GET_CLASS (w)
33 #define RANGE_CLASS(w) GTK_RANGE_GET_CLASS (w)
40 static void gtk_hscale_class_init (GtkHScaleClass *klass);
41 static void gtk_hscale_init (GtkHScale *hscale);
42 static void gtk_hscale_set_arg (GtkObject *object,
45 static void gtk_hscale_get_arg (GtkObject *object,
48 static void gtk_hscale_realize (GtkWidget *widget);
49 static void gtk_hscale_size_request (GtkWidget *widget,
50 GtkRequisition *requisition);
51 static void gtk_hscale_size_allocate (GtkWidget *widget,
52 GtkAllocation *allocation);
53 static void gtk_hscale_pos_trough (GtkHScale *hscale,
58 static void gtk_hscale_pos_background (GtkHScale *hscale,
63 static void gtk_hscale_draw_slider (GtkRange *range);
64 static void gtk_hscale_draw_value (GtkScale *scale);
65 static void gtk_hscale_draw (GtkWidget *widget,
67 static gint gtk_hscale_trough_keys (GtkRange *range,
69 GtkScrollType *scroll,
71 static void gtk_hscale_clear_background (GtkRange *range);
74 gtk_hscale_get_type (void)
76 static GtkType hscale_type = 0;
80 static const GtkTypeInfo hscale_info =
84 sizeof (GtkHScaleClass),
85 (GtkClassInitFunc) gtk_hscale_class_init,
86 (GtkObjectInitFunc) gtk_hscale_init,
87 /* reserved_1 */ NULL,
88 /* reserved_2 */ NULL,
89 (GtkClassInitFunc) NULL,
92 hscale_type = gtk_type_unique (GTK_TYPE_SCALE, &hscale_info);
99 gtk_hscale_class_init (GtkHScaleClass *class)
101 GtkObjectClass *object_class;
102 GtkWidgetClass *widget_class;
103 GtkRangeClass *range_class;
104 GtkScaleClass *scale_class;
106 object_class = (GtkObjectClass*) class;
107 widget_class = (GtkWidgetClass*) class;
108 range_class = (GtkRangeClass*) class;
109 scale_class = (GtkScaleClass*) class;
111 gtk_object_add_arg_type ("GtkHScale::adjustment",
116 object_class->set_arg = gtk_hscale_set_arg;
117 object_class->get_arg = gtk_hscale_get_arg;
119 widget_class->realize = gtk_hscale_realize;
120 widget_class->size_request = gtk_hscale_size_request;
121 widget_class->size_allocate = gtk_hscale_size_allocate;
122 widget_class->draw = gtk_hscale_draw;
124 range_class->slider_update = gtk_range_default_hslider_update;
125 range_class->trough_click = gtk_range_default_htrough_click;
126 range_class->motion = gtk_range_default_hmotion;
127 range_class->draw_slider = gtk_hscale_draw_slider;
128 range_class->trough_keys = gtk_hscale_trough_keys;
129 range_class->clear_background = gtk_hscale_clear_background;
131 scale_class->draw_value = gtk_hscale_draw_value;
135 gtk_hscale_set_arg (GtkObject *object,
141 hscale = GTK_HSCALE (object);
146 gtk_range_set_adjustment (GTK_RANGE (hscale), GTK_VALUE_POINTER (*arg));
154 gtk_hscale_get_arg (GtkObject *object,
160 hscale = GTK_HSCALE (object);
165 GTK_VALUE_POINTER (*arg) = GTK_RANGE (hscale);
168 arg->type = GTK_TYPE_INVALID;
174 gtk_hscale_init (GtkHScale *hscale)
176 GTK_WIDGET_SET_FLAGS (hscale, GTK_NO_WINDOW);
180 gtk_hscale_new (GtkAdjustment *adjustment)
184 hscale = gtk_widget_new (GTK_TYPE_HSCALE,
185 "adjustment", adjustment,
193 gtk_hscale_realize (GtkWidget *widget)
196 GdkWindowAttr attributes;
197 gint attributes_mask;
200 g_return_if_fail (widget != NULL);
201 g_return_if_fail (GTK_IS_HSCALE (widget));
203 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
204 range = GTK_RANGE (widget);
206 widget->window = gtk_widget_get_parent_window (widget);
207 gdk_window_ref (widget->window);
209 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &w, &h);
213 attributes.width = w;
214 attributes.height = h;
215 attributes.wclass = GDK_INPUT_OUTPUT;
216 attributes.window_type = GDK_WINDOW_CHILD;
218 attributes.event_mask = gtk_widget_get_events (widget) |
220 GDK_BUTTON_PRESS_MASK |
221 GDK_BUTTON_RELEASE_MASK |
222 GDK_ENTER_NOTIFY_MASK |
223 GDK_LEAVE_NOTIFY_MASK);
224 attributes.visual = gtk_widget_get_visual (widget);
225 attributes.colormap = gtk_widget_get_colormap (widget);
227 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
229 range->trough = gdk_window_new (widget->window, &attributes, attributes_mask);
231 attributes.width = SCALE_CLASS (range)->slider_length;
232 attributes.height = RANGE_CLASS (range)->slider_width;
233 attributes.event_mask |= (GDK_BUTTON_MOTION_MASK |
234 GDK_POINTER_MOTION_HINT_MASK);
236 range->slider = gdk_window_new (range->trough, &attributes, attributes_mask);
238 widget->style = gtk_style_attach (widget->style, widget->window);
240 gdk_window_set_user_data (range->trough, widget);
241 gdk_window_set_user_data (range->slider, widget);
243 gtk_style_set_background (widget->style, range->trough, GTK_STATE_ACTIVE);
244 gtk_style_set_background (widget->style, range->slider, GTK_STATE_NORMAL);
246 gtk_range_slider_update (GTK_RANGE (widget));
248 gdk_window_show (range->slider);
252 gtk_hscale_draw (GtkWidget *widget,
256 GdkRectangle tmp_area;
257 GdkRectangle child_area;
258 gint x, y, width, height;
260 g_return_if_fail (widget != NULL);
261 g_return_if_fail (GTK_IS_RANGE (widget));
262 g_return_if_fail (area != NULL);
264 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
266 range = GTK_RANGE (widget);
268 gtk_hscale_pos_background (GTK_HSCALE (widget), &x, &y, &width, &height);
272 tmp_area.width = width;
273 tmp_area.height = height;
275 if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
276 gtk_range_draw_background (range);
278 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
282 tmp_area.width = width;
283 tmp_area.height = height;
285 if (gdk_rectangle_intersect (area, &tmp_area, &child_area))
287 gtk_range_draw_trough (range);
288 gtk_range_draw_slider (range);
289 gtk_range_draw_step_forw (range);
290 gtk_range_draw_step_back (range);
296 gtk_hscale_clear_background (GtkRange *range)
299 gint x, y, width, height;
301 g_return_if_fail (range != NULL);
303 widget = GTK_WIDGET (range);
305 gtk_hscale_pos_background (GTK_HSCALE (range), &x, &y, &width, &height);
307 gtk_widget_queue_clear_area (GTK_WIDGET (range),
308 x, y, width, height);
312 gtk_hscale_size_request (GtkWidget *widget,
313 GtkRequisition *requisition)
317 g_return_if_fail (widget != NULL);
318 g_return_if_fail (GTK_IS_HSCALE (widget));
319 g_return_if_fail (requisition != NULL);
321 scale = GTK_SCALE (widget);
323 requisition->width = (SCALE_CLASS (scale)->slider_length +
324 widget->style->klass->xthickness) * 2;
325 requisition->height = (RANGE_CLASS (scale)->slider_width +
326 widget->style->klass->ythickness * 2);
328 if (scale->draw_value)
330 gint value_width, value_height;
331 gtk_scale_get_value_size (scale, &value_width, &value_height);
333 if ((scale->value_pos == GTK_POS_LEFT) ||
334 (scale->value_pos == GTK_POS_RIGHT))
336 requisition->width += value_width + SCALE_CLASS (scale)->value_spacing;
337 if (requisition->height < value_height)
338 requisition->height = value_height;
340 else if ((scale->value_pos == GTK_POS_TOP) ||
341 (scale->value_pos == GTK_POS_BOTTOM))
343 if (requisition->width < value_width)
344 requisition->width = value_width;
345 requisition->height += value_height;
351 gtk_hscale_size_allocate (GtkWidget *widget,
352 GtkAllocation *allocation)
359 g_return_if_fail (widget != NULL);
360 g_return_if_fail (GTK_IS_HSCALE (widget));
361 g_return_if_fail (allocation != NULL);
363 widget->allocation = *allocation;
364 if (GTK_WIDGET_REALIZED (widget))
366 range = GTK_RANGE (widget);
367 scale = GTK_SCALE (widget);
369 gtk_hscale_pos_trough (GTK_HSCALE (widget), &x, &y, &width, &height);
371 gdk_window_move_resize (range->trough,
372 x, y, width, height);
373 gtk_range_slider_update (GTK_RANGE (widget));
378 gtk_hscale_pos_trough (GtkHScale *hscale,
387 g_return_if_fail (hscale != NULL);
388 g_return_if_fail (GTK_IS_HSCALE (hscale));
389 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
391 widget = GTK_WIDGET (hscale);
392 scale = GTK_SCALE (hscale);
394 *w = widget->allocation.width;
395 *h = (RANGE_CLASS (scale)->slider_width +
396 widget->style->klass->ythickness * 2);
398 if (scale->draw_value)
400 gint value_width, value_height;
401 gtk_scale_get_value_size (scale, &value_width, &value_height);
406 switch (scale->value_pos)
409 *x += value_width + SCALE_CLASS (scale)->value_spacing;
410 *y = (widget->allocation.height - *h) / 2;
414 *w -= value_width + SCALE_CLASS (scale)->value_spacing;
415 *y = (widget->allocation.height - *h) / 2;
418 *y = (value_height + (widget->allocation.height - widget->requisition.height) / 2);
421 *y = (widget->allocation.height - widget->requisition.height) / 2;
428 *y = (widget->allocation.height - *h) / 2;
433 *x += widget->allocation.x;
434 *y += widget->allocation.y;
438 gtk_hscale_pos_background (GtkHScale *hscale,
447 gint tx, ty, twidth, theight;
449 g_return_if_fail (hscale != NULL);
450 g_return_if_fail (GTK_IS_HSCALE (hscale));
451 g_return_if_fail ((x != NULL) && (y != NULL) && (w != NULL) && (h != NULL));
453 gtk_hscale_pos_trough (hscale, &tx, &ty, &twidth, &theight);
455 widget = GTK_WIDGET (hscale);
456 scale = GTK_SCALE (hscale);
458 *x = widget->allocation.x;
459 *y = widget->allocation.y;
460 *w = widget->allocation.width;
461 *h = widget->allocation.height;
463 switch (scale->value_pos)
485 gtk_hscale_draw_slider (GtkRange *range)
487 GtkStateType state_type;
489 g_return_if_fail (range != NULL);
490 g_return_if_fail (GTK_IS_HSCALE (range));
494 if ((range->in_child == RANGE_CLASS (range)->slider) ||
495 (range->click_child == RANGE_CLASS (range)->slider))
496 state_type = GTK_STATE_PRELIGHT;
498 state_type = GTK_STATE_NORMAL;
500 gtk_paint_slider (GTK_WIDGET (range)->style, range->slider, state_type,
502 NULL, GTK_WIDGET (range), "hscale",
504 GTK_ORIENTATION_HORIZONTAL);
509 gtk_hscale_draw_value (GtkScale *scale)
511 GtkStateType state_type;
517 g_return_if_fail (scale != NULL);
518 g_return_if_fail (GTK_IS_HSCALE (scale));
520 widget = GTK_WIDGET (scale);
522 if (scale->draw_value)
525 PangoRectangle logical_rect;
527 sprintf (buffer, "%0.*f", GTK_RANGE (scale)->digits, GTK_RANGE (scale)->adjustment->value);
529 layout = gtk_widget_create_pango_layout (widget);
530 pango_layout_set_text (layout, buffer, -1);
531 pango_layout_get_extents (layout, NULL, &logical_rect);
533 switch (scale->value_pos)
536 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
537 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
539 x -= SCALE_CLASS (scale)->value_spacing + logical_rect.width / PANGO_SCALE;
540 y += (height - logical_rect.height / PANGO_SCALE) / 2;
543 gdk_window_get_position (GTK_RANGE (scale)->trough, &x, &y);
544 gdk_window_get_size (GTK_RANGE (scale)->trough, &width, &height);
546 x += width + SCALE_CLASS (scale)->value_spacing;
547 y += (height - logical_rect.height / PANGO_SCALE) / 2;
550 gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
551 gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
552 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
553 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
555 x += widget->allocation.x + (width - logical_rect.width / PANGO_SCALE) / 2;
556 x = CLAMP (x, widget->allocation.x,
557 widget->allocation.x + widget->allocation.width - logical_rect.width / PANGO_SCALE);
558 y -= logical_rect.height / PANGO_SCALE;
561 gdk_window_get_position (GTK_RANGE (scale)->slider, &x, NULL);
562 gdk_window_get_position (GTK_RANGE (scale)->trough, NULL, &y);
563 gdk_window_get_size (GTK_RANGE (scale)->slider, &width, NULL);
564 gdk_window_get_size (GTK_RANGE (scale)->trough, NULL, &height);
566 x += widget->allocation.x + (width - logical_rect.width / PANGO_SCALE) / 2;
567 x = CLAMP (x, widget->allocation.x,
568 widget->allocation.x + widget->allocation.width - logical_rect.width / PANGO_SCALE);
573 state_type = GTK_STATE_NORMAL;
574 if (!GTK_WIDGET_IS_SENSITIVE (scale))
575 state_type = GTK_STATE_INSENSITIVE;
578 gtk_paint_string (GTK_WIDGET (scale)->style,
579 GTK_WIDGET (scale)->window,
581 NULL, GTK_WIDGET (scale), "hscale",
585 gdk_draw_layout (GTK_WIDGET (scale)->window,
586 GTK_WIDGET (scale)->style->fg_gc [state_type],
589 pango_layout_unref (layout);
594 gtk_hscale_trough_keys (GtkRange *range,
596 GtkScrollType *scroll,
599 gint return_val = FALSE;
604 if (key->state & GDK_CONTROL_MASK)
605 *scroll = GTK_SCROLL_PAGE_BACKWARD;
607 *scroll = GTK_SCROLL_STEP_BACKWARD;
611 if (key->state & GDK_CONTROL_MASK)
612 *scroll = GTK_SCROLL_PAGE_FORWARD;
614 *scroll = GTK_SCROLL_STEP_FORWARD;
618 *pos = GTK_TROUGH_START;
622 *pos = GTK_TROUGH_END;