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/.
31 #undef GTK_DISABLE_DEPRECATED
32 #define __GTK_PROGRESS_C__
34 #include "gtkprogress.h"
35 #include "gtkprivate.h"
40 #define DEFAULT_FORMAT "%P %%"
51 static void gtk_progress_set_property (GObject *object,
55 static void gtk_progress_get_property (GObject *object,
59 static void gtk_progress_destroy (GtkObject *object);
60 static void gtk_progress_finalize (GObject *object);
61 static void gtk_progress_realize (GtkWidget *widget);
62 static gboolean gtk_progress_expose (GtkWidget *widget,
63 GdkEventExpose *event);
64 static void gtk_progress_size_allocate (GtkWidget *widget,
65 GtkAllocation *allocation);
66 static void gtk_progress_create_pixmap (GtkProgress *progress);
67 static void gtk_progress_value_changed (GtkAdjustment *adjustment,
68 GtkProgress *progress);
69 static void gtk_progress_changed (GtkAdjustment *adjustment,
70 GtkProgress *progress);
72 G_DEFINE_ABSTRACT_TYPE (GtkProgress, gtk_progress, GTK_TYPE_WIDGET)
75 gtk_progress_class_init (GtkProgressClass *class)
77 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
78 GtkObjectClass *object_class;
79 GtkWidgetClass *widget_class;
81 object_class = (GtkObjectClass *) class;
82 widget_class = (GtkWidgetClass *) class;
84 gobject_class->finalize = gtk_progress_finalize;
86 gobject_class->set_property = gtk_progress_set_property;
87 gobject_class->get_property = gtk_progress_get_property;
88 object_class->destroy = gtk_progress_destroy;
90 widget_class->realize = gtk_progress_realize;
91 widget_class->expose_event = gtk_progress_expose;
92 widget_class->size_allocate = gtk_progress_size_allocate;
94 /* to be overridden */
97 class->act_mode_enter = NULL;
99 g_object_class_install_property (gobject_class,
101 g_param_spec_boolean ("activity-mode",
103 P_("If TRUE, the GtkProgress is in activity mode, meaning that it signals "
104 "something is happening, but not how much of the activity is finished. "
105 "This is used when you're doing something but don't know how long it will take."),
107 GTK_PARAM_READWRITE));
108 g_object_class_install_property (gobject_class,
110 g_param_spec_boolean ("show-text",
112 P_("Whether the progress is shown as text."),
114 GTK_PARAM_READWRITE));
115 g_object_class_install_property (gobject_class,
117 g_param_spec_float ("text-xalign",
118 P_("Text x alignment"),
119 P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
121 GTK_PARAM_READWRITE));
122 g_object_class_install_property (gobject_class,
124 g_param_spec_float ("text-yalign",
125 P_("Text y alignment"),
126 P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
128 GTK_PARAM_READWRITE));
132 gtk_progress_set_property (GObject *object,
137 GtkProgress *progress;
139 progress = GTK_PROGRESS (object);
143 case PROP_ACTIVITY_MODE:
144 gtk_progress_set_activity_mode (progress, g_value_get_boolean (value));
147 gtk_progress_set_show_text (progress, g_value_get_boolean (value));
149 case PROP_TEXT_XALIGN:
150 gtk_progress_set_text_alignment (progress,
151 g_value_get_float (value),
154 case PROP_TEXT_YALIGN:
155 gtk_progress_set_text_alignment (progress,
157 g_value_get_float (value));
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 gtk_progress_get_property (GObject *object,
171 GtkProgress *progress;
173 progress = GTK_PROGRESS (object);
177 case PROP_ACTIVITY_MODE:
178 g_value_set_boolean (value, (progress->activity_mode != FALSE));
181 g_value_set_boolean (value, (progress->show_text != FALSE));
183 case PROP_TEXT_XALIGN:
184 g_value_set_float (value, progress->x_align);
186 case PROP_TEXT_YALIGN:
187 g_value_set_float (value, progress->y_align);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 gtk_progress_init (GtkProgress *progress)
198 progress->adjustment = NULL;
199 progress->offscreen_pixmap = NULL;
200 progress->format = g_strdup (DEFAULT_FORMAT);
201 progress->x_align = 0.5;
202 progress->y_align = 0.5;
203 progress->show_text = FALSE;
204 progress->activity_mode = FALSE;
205 progress->use_text_format = TRUE;
209 gtk_progress_realize (GtkWidget *widget)
211 GtkProgress *progress = GTK_PROGRESS (widget);
212 GdkWindowAttr attributes;
213 gint attributes_mask;
215 gtk_widget_set_realized (widget, TRUE);
217 attributes.window_type = GDK_WINDOW_CHILD;
218 attributes.x = widget->allocation.x;
219 attributes.y = widget->allocation.y;
220 attributes.width = widget->allocation.width;
221 attributes.height = widget->allocation.height;
222 attributes.wclass = GDK_INPUT_OUTPUT;
223 attributes.visual = gtk_widget_get_visual (widget);
224 attributes.colormap = gtk_widget_get_colormap (widget);
225 attributes.event_mask = gtk_widget_get_events (widget);
226 attributes.event_mask |= GDK_EXPOSURE_MASK;
228 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
230 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
231 &attributes, attributes_mask);
232 gdk_window_set_user_data (widget->window, progress);
234 widget->style = gtk_style_attach (widget->style, widget->window);
235 gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
237 gtk_progress_create_pixmap (progress);
241 gtk_progress_destroy (GtkObject *object)
243 GtkProgress *progress = GTK_PROGRESS (object);
245 if (progress->adjustment)
247 g_signal_handlers_disconnect_by_func (progress->adjustment,
248 gtk_progress_value_changed,
250 g_signal_handlers_disconnect_by_func (progress->adjustment,
251 gtk_progress_changed,
253 g_object_unref (progress->adjustment);
254 progress->adjustment = NULL;
257 GTK_OBJECT_CLASS (gtk_progress_parent_class)->destroy (object);
261 gtk_progress_finalize (GObject *object)
263 GtkProgress *progress = GTK_PROGRESS (object);
265 if (progress->offscreen_pixmap)
266 g_object_unref (progress->offscreen_pixmap);
268 g_free (progress->format);
270 G_OBJECT_CLASS (gtk_progress_parent_class)->finalize (object);
274 gtk_progress_expose (GtkWidget *widget,
275 GdkEventExpose *event)
277 if (GTK_WIDGET_DRAWABLE (widget))
278 gdk_draw_drawable (widget->window,
279 widget->style->black_gc,
280 GTK_PROGRESS (widget)->offscreen_pixmap,
281 event->area.x, event->area.y,
282 event->area.x, event->area.y,
290 gtk_progress_size_allocate (GtkWidget *widget,
291 GtkAllocation *allocation)
293 widget->allocation = *allocation;
295 if (gtk_widget_get_realized (widget))
297 gdk_window_move_resize (widget->window,
298 allocation->x, allocation->y,
299 allocation->width, allocation->height);
301 gtk_progress_create_pixmap (GTK_PROGRESS (widget));
306 gtk_progress_create_pixmap (GtkProgress *progress)
310 g_return_if_fail (GTK_IS_PROGRESS (progress));
312 widget = GTK_WIDGET (progress);
314 if (gtk_widget_get_realized (widget))
316 if (progress->offscreen_pixmap)
317 g_object_unref (progress->offscreen_pixmap);
319 progress->offscreen_pixmap = gdk_pixmap_new (widget->window,
320 widget->allocation.width,
321 widget->allocation.height,
324 /* clear the pixmap for transparent themes */
325 gtk_paint_flat_box (widget->style,
326 progress->offscreen_pixmap,
327 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
328 NULL, widget, "trough", 0, 0, -1, -1);
330 GTK_PROGRESS_GET_CLASS (progress)->paint (progress);
335 gtk_progress_changed (GtkAdjustment *adjustment,
336 GtkProgress *progress)
338 /* A change in the value of adjustment->upper can change
341 if (progress->use_text_format && progress->show_text)
342 gtk_widget_queue_resize (GTK_WIDGET (progress));
344 GTK_PROGRESS_GET_CLASS (progress)->update (progress);
348 gtk_progress_value_changed (GtkAdjustment *adjustment,
349 GtkProgress *progress)
351 GTK_PROGRESS_GET_CLASS (progress)->update (progress);
355 gtk_progress_build_string (GtkProgress *progress,
359 gchar buf[256] = { 0 };
360 gchar tmp[256] = { 0 };
365 src = progress->format;
367 /* This is the new supported version of this function */
368 if (!progress->use_text_format)
369 return g_strdup (src);
371 /* And here's all the deprecated goo. */
387 c = *(src + sizeof(gchar));
390 if (c >= '0' && c <= '2')
392 digits = (gint) (c - '0');
394 c = *(src + sizeof(gchar));
408 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
409 g_snprintf (tmp, sizeof (tmp), fmt, 100 * percentage);
412 g_snprintf (tmp, sizeof (tmp), "%.0f", 100 * percentage);
414 dest = &(buf[strlen (buf)]);
421 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
422 g_snprintf (tmp, sizeof (tmp), fmt, value);
425 g_snprintf (tmp, sizeof (tmp), "%.0f", value);
427 dest = &(buf[strlen (buf)]);
434 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
435 g_snprintf (tmp, sizeof (tmp), fmt, progress->adjustment->lower);
438 g_snprintf (tmp, sizeof (tmp), "%.0f", progress->adjustment->lower);
440 dest = &(buf[strlen (buf)]);
447 g_snprintf (fmt, sizeof (fmt), "%%.%df", digits);
448 g_snprintf (tmp, sizeof (tmp), fmt, progress->adjustment->upper);
451 g_snprintf (tmp, sizeof (tmp), "%.0f", progress->adjustment->upper);
453 dest = &(buf[strlen (buf)]);
463 return g_strdup (buf);
466 /***************************************************************/
469 gtk_progress_set_adjustment (GtkProgress *progress,
470 GtkAdjustment *adjustment)
472 g_return_if_fail (GTK_IS_PROGRESS (progress));
474 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
476 adjustment = (GtkAdjustment*) gtk_adjustment_new (0, 0, 100, 0, 0, 0);
478 if (progress->adjustment != adjustment)
480 if (progress->adjustment)
482 g_signal_handlers_disconnect_by_func (progress->adjustment,
483 gtk_progress_changed,
485 g_signal_handlers_disconnect_by_func (progress->adjustment,
486 gtk_progress_value_changed,
488 g_object_unref (progress->adjustment);
490 progress->adjustment = adjustment;
493 g_object_ref_sink (adjustment);
494 g_signal_connect (adjustment, "changed",
495 G_CALLBACK (gtk_progress_changed),
497 g_signal_connect (adjustment, "value-changed",
498 G_CALLBACK (gtk_progress_value_changed),
502 gtk_progress_changed (adjustment, progress);
507 gtk_progress_configure (GtkProgress *progress,
513 gboolean changed = FALSE;
515 g_return_if_fail (GTK_IS_PROGRESS (progress));
516 g_return_if_fail (min <= max);
517 g_return_if_fail (value >= min && value <= max);
519 if (!progress->adjustment)
520 gtk_progress_set_adjustment (progress, NULL);
521 adj = progress->adjustment;
523 if (fabs (adj->lower - min) > EPSILON || fabs (adj->upper - max) > EPSILON)
530 gtk_adjustment_value_changed (adj);
532 gtk_adjustment_changed (adj);
536 gtk_progress_set_percentage (GtkProgress *progress,
539 g_return_if_fail (GTK_IS_PROGRESS (progress));
540 g_return_if_fail (percentage >= 0 && percentage <= 1.0);
542 if (!progress->adjustment)
543 gtk_progress_set_adjustment (progress, NULL);
544 gtk_progress_set_value (progress, progress->adjustment->lower + percentage *
545 (progress->adjustment->upper - progress->adjustment->lower));
549 gtk_progress_get_current_percentage (GtkProgress *progress)
551 g_return_val_if_fail (GTK_IS_PROGRESS (progress), 0);
553 if (!progress->adjustment)
554 gtk_progress_set_adjustment (progress, NULL);
556 return gtk_progress_get_percentage_from_value (progress, progress->adjustment->value);
560 gtk_progress_get_percentage_from_value (GtkProgress *progress,
563 g_return_val_if_fail (GTK_IS_PROGRESS (progress), 0);
565 if (!progress->adjustment)
566 gtk_progress_set_adjustment (progress, NULL);
568 if (progress->adjustment->lower < progress->adjustment->upper &&
569 value >= progress->adjustment->lower &&
570 value <= progress->adjustment->upper)
571 return (value - progress->adjustment->lower) /
572 (progress->adjustment->upper - progress->adjustment->lower);
578 gtk_progress_set_value (GtkProgress *progress,
581 g_return_if_fail (GTK_IS_PROGRESS (progress));
583 if (!progress->adjustment)
584 gtk_progress_set_adjustment (progress, NULL);
586 if (fabs (progress->adjustment->value - value) > EPSILON)
587 gtk_adjustment_set_value (progress->adjustment, value);
591 gtk_progress_get_value (GtkProgress *progress)
593 g_return_val_if_fail (GTK_IS_PROGRESS (progress), 0);
595 return progress->adjustment ? progress->adjustment->value : 0;
599 gtk_progress_set_show_text (GtkProgress *progress,
602 g_return_if_fail (GTK_IS_PROGRESS (progress));
604 if (progress->show_text != show_text)
606 progress->show_text = show_text;
608 gtk_widget_queue_resize (GTK_WIDGET (progress));
610 g_object_notify (G_OBJECT (progress), "show-text");
615 gtk_progress_set_text_alignment (GtkProgress *progress,
619 g_return_if_fail (GTK_IS_PROGRESS (progress));
620 g_return_if_fail (x_align >= 0.0 && x_align <= 1.0);
621 g_return_if_fail (y_align >= 0.0 && y_align <= 1.0);
623 if (progress->x_align != x_align || progress->y_align != y_align)
625 g_object_freeze_notify (G_OBJECT (progress));
626 if (progress->x_align != x_align)
628 progress->x_align = x_align;
629 g_object_notify (G_OBJECT (progress), "text-xalign");
632 if (progress->y_align != y_align)
634 progress->y_align = y_align;
635 g_object_notify (G_OBJECT (progress), "text-yalign");
637 g_object_thaw_notify (G_OBJECT (progress));
639 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (progress)))
640 gtk_widget_queue_resize (GTK_WIDGET (progress));
645 gtk_progress_set_format_string (GtkProgress *progress,
650 g_return_if_fail (GTK_IS_PROGRESS (progress));
652 /* Turn on format, in case someone called
653 * gtk_progress_bar_set_text() and turned it off.
655 progress->use_text_format = TRUE;
657 old_format = progress->format;
660 format = DEFAULT_FORMAT;
662 progress->format = g_strdup (format);
665 gtk_widget_queue_resize (GTK_WIDGET (progress));
669 gtk_progress_get_current_text (GtkProgress *progress)
671 g_return_val_if_fail (GTK_IS_PROGRESS (progress), NULL);
673 if (!progress->adjustment)
674 gtk_progress_set_adjustment (progress, NULL);
676 return gtk_progress_build_string (progress, progress->adjustment->value,
677 gtk_progress_get_current_percentage (progress));
681 gtk_progress_get_text_from_value (GtkProgress *progress,
684 g_return_val_if_fail (GTK_IS_PROGRESS (progress), NULL);
686 if (!progress->adjustment)
687 gtk_progress_set_adjustment (progress, NULL);
689 return gtk_progress_build_string (progress, value,
690 gtk_progress_get_percentage_from_value (progress, value));
694 gtk_progress_set_activity_mode (GtkProgress *progress,
695 gboolean activity_mode)
697 g_return_if_fail (GTK_IS_PROGRESS (progress));
699 if (progress->activity_mode != (activity_mode != FALSE))
701 progress->activity_mode = (activity_mode != FALSE);
703 if (progress->activity_mode)
704 GTK_PROGRESS_GET_CLASS (progress)->act_mode_enter (progress);
706 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (progress)))
707 gtk_widget_queue_resize (GTK_WIDGET (progress));
709 g_object_notify (G_OBJECT (progress), "activity-mode");
713 #include "gtkaliasdef.c"