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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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-2000. 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/.
30 #undef GTK_DISABLE_DEPRECATED
31 #include "gtkprogress.h"
32 #include "gtkprivate.h"
37 #define DEFAULT_FORMAT "%P %%"
48 static void gtk_progress_set_property (GObject *object,
52 static void gtk_progress_get_property (GObject *object,
56 static void gtk_progress_destroy (GtkObject *object);
57 static void gtk_progress_finalize (GObject *object);
58 static void gtk_progress_realize (GtkWidget *widget);
59 static gboolean gtk_progress_expose (GtkWidget *widget,
60 GdkEventExpose *event);
61 static void gtk_progress_size_allocate (GtkWidget *widget,
62 GtkAllocation *allocation);
63 static void gtk_progress_create_pixmap (GtkProgress *progress);
64 static void gtk_progress_value_changed (GtkAdjustment *adjustment,
65 GtkProgress *progress);
66 static void gtk_progress_changed (GtkAdjustment *adjustment,
67 GtkProgress *progress);
69 G_DEFINE_TYPE (GtkProgress, gtk_progress, GTK_TYPE_WIDGET)
72 gtk_progress_class_init (GtkProgressClass *class)
74 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
75 GtkObjectClass *object_class;
76 GtkWidgetClass *widget_class;
78 object_class = (GtkObjectClass *) class;
79 widget_class = (GtkWidgetClass *) class;
81 gobject_class->finalize = gtk_progress_finalize;
83 gobject_class->set_property = gtk_progress_set_property;
84 gobject_class->get_property = gtk_progress_get_property;
85 object_class->destroy = gtk_progress_destroy;
87 widget_class->realize = gtk_progress_realize;
88 widget_class->expose_event = gtk_progress_expose;
89 widget_class->size_allocate = gtk_progress_size_allocate;
91 /* to be overridden */
94 class->act_mode_enter = NULL;
96 g_object_class_install_property (gobject_class,
98 g_param_spec_boolean ("activity-mode",
100 P_("If TRUE, the GtkProgress is in activity mode, meaning that it signals "
101 "something is happening, but not how much of the activity is finished. "
102 "This is used when you're doing something but don't know how long it will take."),
104 GTK_PARAM_READWRITE));
105 g_object_class_install_property (gobject_class,
107 g_param_spec_boolean ("show-text",
109 P_("Whether the progress is shown as text."),
111 GTK_PARAM_READWRITE));
112 g_object_class_install_property (gobject_class,
114 g_param_spec_float ("text-xalign",
115 P_("Text x alignment"),
116 P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
118 GTK_PARAM_READWRITE));
119 g_object_class_install_property (gobject_class,
121 g_param_spec_float ("text-yalign",
122 P_("Text y alignment"),
123 P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
125 GTK_PARAM_READWRITE));
129 gtk_progress_set_property (GObject *object,
134 GtkProgress *progress;
136 progress = GTK_PROGRESS (object);
140 case PROP_ACTIVITY_MODE:
141 gtk_progress_set_activity_mode (progress, g_value_get_boolean (value));
144 gtk_progress_set_show_text (progress, g_value_get_boolean (value));
146 case PROP_TEXT_XALIGN:
147 gtk_progress_set_text_alignment (progress,
148 g_value_get_float (value),
151 case PROP_TEXT_YALIGN:
152 gtk_progress_set_text_alignment (progress,
154 g_value_get_float (value));
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 gtk_progress_get_property (GObject *object,
168 GtkProgress *progress;
170 progress = GTK_PROGRESS (object);
174 case PROP_ACTIVITY_MODE:
175 g_value_set_boolean (value, (progress->activity_mode != FALSE));
178 g_value_set_boolean (value, (progress->show_text != FALSE));
180 case PROP_TEXT_XALIGN:
181 g_value_set_float (value, progress->x_align);
183 case PROP_TEXT_YALIGN:
184 g_value_set_float (value, progress->y_align);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 gtk_progress_init (GtkProgress *progress)
195 progress->adjustment = NULL;
196 progress->offscreen_pixmap = NULL;
197 progress->format = g_strdup (DEFAULT_FORMAT);
198 progress->x_align = 0.5;
199 progress->y_align = 0.5;
200 progress->show_text = FALSE;
201 progress->activity_mode = FALSE;
202 progress->use_text_format = TRUE;
206 gtk_progress_realize (GtkWidget *widget)
208 GtkProgress *progress = GTK_PROGRESS (widget);
209 GdkWindowAttr attributes;
210 gint attributes_mask;
212 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
214 attributes.window_type = GDK_WINDOW_CHILD;
215 attributes.x = widget->allocation.x;
216 attributes.y = widget->allocation.y;
217 attributes.width = widget->allocation.width;
218 attributes.height = widget->allocation.height;
219 attributes.wclass = GDK_INPUT_OUTPUT;
220 attributes.visual = gtk_widget_get_visual (widget);
221 attributes.colormap = gtk_widget_get_colormap (widget);
222 attributes.event_mask = gtk_widget_get_events (widget);
223 attributes.event_mask |= GDK_EXPOSURE_MASK;
225 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
227 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
228 &attributes, attributes_mask);
229 gdk_window_set_user_data (widget->window, progress);
231 widget->style = gtk_style_attach (widget->style, widget->window);
232 gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
234 gtk_progress_create_pixmap (progress);
238 gtk_progress_destroy (GtkObject *object)
240 GtkProgress *progress = GTK_PROGRESS (object);
242 if (progress->adjustment)
244 g_signal_handlers_disconnect_by_func (progress->adjustment,
245 gtk_progress_value_changed,
247 g_signal_handlers_disconnect_by_func (progress->adjustment,
248 gtk_progress_changed,
250 g_object_unref (progress->adjustment);
251 progress->adjustment = NULL;
254 GTK_OBJECT_CLASS (gtk_progress_parent_class)->destroy (object);
258 gtk_progress_finalize (GObject *object)
260 GtkProgress *progress = GTK_PROGRESS (object);
262 if (progress->offscreen_pixmap)
263 g_object_unref (progress->offscreen_pixmap);
265 g_free (progress->format);
267 G_OBJECT_CLASS (gtk_progress_parent_class)->finalize (object);
271 gtk_progress_expose (GtkWidget *widget,
272 GdkEventExpose *event)
274 if (GTK_WIDGET_DRAWABLE (widget))
275 gdk_draw_drawable (widget->window,
276 widget->style->black_gc,
277 GTK_PROGRESS (widget)->offscreen_pixmap,
278 event->area.x, event->area.y,
279 event->area.x, event->area.y,
287 gtk_progress_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation)
290 widget->allocation = *allocation;
292 if (GTK_WIDGET_REALIZED (widget))
294 gdk_window_move_resize (widget->window,
295 allocation->x, allocation->y,
296 allocation->width, allocation->height);
298 gtk_progress_create_pixmap (GTK_PROGRESS (widget));
303 gtk_progress_create_pixmap (GtkProgress *progress)
307 g_return_if_fail (GTK_IS_PROGRESS (progress));
309 if (GTK_WIDGET_REALIZED (progress))
311 widget = GTK_WIDGET (progress);
313 if (progress->offscreen_pixmap)
314 g_object_unref (progress->offscreen_pixmap);
316 progress->offscreen_pixmap = gdk_pixmap_new (widget->window,
317 widget->allocation.width,
318 widget->allocation.height,
321 /* clear the pixmap for transparent themes */
322 gtk_paint_flat_box (widget->style,
323 progress->offscreen_pixmap,
324 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
325 NULL, widget, "trough", 0, 0, -1, -1);
327 GTK_PROGRESS_GET_CLASS (progress)->paint (progress);
332 gtk_progress_changed (GtkAdjustment *adjustment,
333 GtkProgress *progress)
335 /* A change in the value of adjustment->upper can change
338 if (progress->use_text_format && progress->show_text)
339 gtk_widget_queue_resize (GTK_WIDGET (progress));
341 GTK_PROGRESS_GET_CLASS (progress)->update (progress);
345 gtk_progress_value_changed (GtkAdjustment *adjustment,
346 GtkProgress *progress)
348 GTK_PROGRESS_GET_CLASS (progress)->update (progress);
352 gtk_progress_build_string (GtkProgress *progress,
356 gchar buf[256] = { 0 };
357 gchar tmp[256] = { 0 };
362 src = progress->format;
364 /* This is the new supported version of this function */
365 if (!progress->use_text_format)
366 return g_strdup (src);
368 /* And here's all the deprecated goo. */
384 c = *(src + sizeof(gchar));
387 if (c >= '0' && c <= '2')
389 digits = (gint) (c - '0');
391 c = *(src + sizeof(gchar));
405 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
406 g_snprintf (tmp, sizeof (tmp), fmt, 100 * percentage);
409 g_snprintf (tmp, sizeof (tmp), "%.0f", 100 * percentage);
411 dest = &(buf[strlen (buf)]);
418 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
419 g_snprintf (tmp, sizeof (tmp), fmt, value);
422 g_snprintf (tmp, sizeof (tmp), "%.0f", value);
424 dest = &(buf[strlen (buf)]);
431 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
432 g_snprintf (tmp, sizeof (tmp), fmt, progress->adjustment->lower);
435 g_snprintf (tmp, sizeof (tmp), "%.0f", progress->adjustment->lower);
437 dest = &(buf[strlen (buf)]);
444 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
445 g_snprintf (tmp, sizeof (tmp), fmt, progress->adjustment->upper);
448 g_snprintf (tmp, sizeof (tmp), "%.0f", progress->adjustment->upper);
450 dest = &(buf[strlen (buf)]);
460 return g_strdup (buf);
463 /***************************************************************/
466 gtk_progress_set_adjustment (GtkProgress *progress,
467 GtkAdjustment *adjustment)
469 g_return_if_fail (GTK_IS_PROGRESS (progress));
471 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
473 adjustment = (GtkAdjustment*) gtk_adjustment_new (0, 0, 100, 0, 0, 0);
475 if (progress->adjustment != adjustment)
477 if (progress->adjustment)
479 g_signal_handlers_disconnect_by_func (progress->adjustment,
480 gtk_progress_changed,
482 g_signal_handlers_disconnect_by_func (progress->adjustment,
483 gtk_progress_value_changed,
485 g_object_unref (progress->adjustment);
487 progress->adjustment = adjustment;
490 g_object_ref_sink (adjustment);
491 g_signal_connect (adjustment, "changed",
492 G_CALLBACK (gtk_progress_changed),
494 g_signal_connect (adjustment, "value_changed",
495 G_CALLBACK (gtk_progress_value_changed),
499 gtk_progress_changed (adjustment, progress);
504 gtk_progress_configure (GtkProgress *progress,
510 gboolean changed = FALSE;
512 g_return_if_fail (GTK_IS_PROGRESS (progress));
513 g_return_if_fail (min <= max);
514 g_return_if_fail (value >= min && value <= max);
516 if (!progress->adjustment)
517 gtk_progress_set_adjustment (progress, NULL);
518 adj = progress->adjustment;
520 if (fabs (adj->lower - min) > EPSILON || fabs (adj->upper - max) > EPSILON)
527 gtk_adjustment_value_changed (adj);
529 gtk_adjustment_changed (adj);
533 gtk_progress_set_percentage (GtkProgress *progress,
536 g_return_if_fail (GTK_IS_PROGRESS (progress));
537 g_return_if_fail (percentage >= 0 && percentage <= 1.0);
539 if (!progress->adjustment)
540 gtk_progress_set_adjustment (progress, NULL);
541 gtk_progress_set_value (progress, progress->adjustment->lower + percentage *
542 (progress->adjustment->upper - progress->adjustment->lower));
546 gtk_progress_get_current_percentage (GtkProgress *progress)
548 g_return_val_if_fail (GTK_IS_PROGRESS (progress), 0);
550 if (!progress->adjustment)
551 gtk_progress_set_adjustment (progress, NULL);
553 return gtk_progress_get_percentage_from_value (progress, progress->adjustment->value);
557 gtk_progress_get_percentage_from_value (GtkProgress *progress,
560 g_return_val_if_fail (GTK_IS_PROGRESS (progress), 0);
562 if (!progress->adjustment)
563 gtk_progress_set_adjustment (progress, NULL);
565 if (progress->adjustment->lower < progress->adjustment->upper &&
566 value >= progress->adjustment->lower &&
567 value <= progress->adjustment->upper)
568 return (value - progress->adjustment->lower) /
569 (progress->adjustment->upper - progress->adjustment->lower);
575 gtk_progress_set_value (GtkProgress *progress,
578 g_return_if_fail (GTK_IS_PROGRESS (progress));
580 if (!progress->adjustment)
581 gtk_progress_set_adjustment (progress, NULL);
583 if (fabs (progress->adjustment->value - value) > EPSILON)
584 gtk_adjustment_set_value (progress->adjustment, value);
588 gtk_progress_get_value (GtkProgress *progress)
590 g_return_val_if_fail (GTK_IS_PROGRESS (progress), 0);
592 return progress->adjustment ? progress->adjustment->value : 0;
596 gtk_progress_set_show_text (GtkProgress *progress,
599 g_return_if_fail (GTK_IS_PROGRESS (progress));
601 if (progress->show_text != show_text)
603 progress->show_text = show_text;
605 gtk_widget_queue_resize (GTK_WIDGET (progress));
607 g_object_notify (G_OBJECT (progress), "show-text");
612 gtk_progress_set_text_alignment (GtkProgress *progress,
616 g_return_if_fail (GTK_IS_PROGRESS (progress));
617 g_return_if_fail (x_align >= 0.0 && x_align <= 1.0);
618 g_return_if_fail (y_align >= 0.0 && y_align <= 1.0);
620 if (progress->x_align != x_align || progress->y_align != y_align)
622 g_object_freeze_notify (G_OBJECT (progress));
623 if (progress->x_align != x_align)
625 progress->x_align = x_align;
626 g_object_notify (G_OBJECT (progress), "text-xalign");
629 if (progress->y_align != y_align)
631 progress->y_align = y_align;
632 g_object_notify (G_OBJECT (progress), "text-yalign");
634 g_object_thaw_notify (G_OBJECT (progress));
636 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (progress)))
637 gtk_widget_queue_resize (GTK_WIDGET (progress));
642 gtk_progress_set_format_string (GtkProgress *progress,
647 g_return_if_fail (GTK_IS_PROGRESS (progress));
649 /* Turn on format, in case someone called
650 * gtk_progress_bar_set_text() and turned it off.
652 progress->use_text_format = TRUE;
654 old_format = progress->format;
657 format = DEFAULT_FORMAT;
659 progress->format = g_strdup (format);
662 gtk_widget_queue_resize (GTK_WIDGET (progress));
666 gtk_progress_get_current_text (GtkProgress *progress)
668 g_return_val_if_fail (GTK_IS_PROGRESS (progress), NULL);
670 if (!progress->adjustment)
671 gtk_progress_set_adjustment (progress, NULL);
673 return gtk_progress_build_string (progress, progress->adjustment->value,
674 gtk_progress_get_current_percentage (progress));
678 gtk_progress_get_text_from_value (GtkProgress *progress,
681 g_return_val_if_fail (GTK_IS_PROGRESS (progress), NULL);
683 if (!progress->adjustment)
684 gtk_progress_set_adjustment (progress, NULL);
686 return gtk_progress_build_string (progress, value,
687 gtk_progress_get_percentage_from_value (progress, value));
691 gtk_progress_set_activity_mode (GtkProgress *progress,
692 gboolean activity_mode)
694 g_return_if_fail (GTK_IS_PROGRESS (progress));
696 if (progress->activity_mode != (activity_mode != FALSE))
698 progress->activity_mode = (activity_mode != FALSE);
700 if (progress->activity_mode)
701 GTK_PROGRESS_GET_CLASS (progress)->act_mode_enter (progress);
703 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (progress)))
704 gtk_widget_queue_resize (GTK_WIDGET (progress));
706 g_object_notify (G_OBJECT (progress), "activity-mode");
710 #define __GTK_PROGRESS_C__
711 #include "gtkaliasdef.c"