X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkprogressbar.c;h=932980a33e5deb8599becbdabf73bb98e0968d8d;hb=0cc688aa92b8fe5452b63fb49c51204498a2988b;hp=154ed679c6c9cac9392c02a3584aa555df1a0d78;hpb=e8a6bad00bc9c544cece170f01dd220e5abc3254;p=~andy%2Fgtk diff --git a/gtk/gtkprogressbar.c b/gtk/gtkprogressbar.c index 154ed679c..932980a33 100644 --- a/gtk/gtkprogressbar.c +++ b/gtk/gtkprogressbar.c @@ -12,9 +12,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * License along with this library. If not, see . */ /* @@ -29,10 +27,40 @@ #include #include "gtkprogressbar.h" -#include "gtkorientable.h" +#include "gtkorientableprivate.h" #include "gtkprivate.h" #include "gtkintl.h" +#include "a11y/gtkprogressbaraccessible.h" + +/** + * SECTION:gtkprogressbar + * @Short_description: A widget which indicates progress visually + * @Title: GtkProgressBar + * + * The #GtkProgressBar is typically used to display the progress of a long + * running operation. It provides a visual clue that processing + * is underway. The #GtkProgressBar can be used in two different + * modes: percentage mode and activity mode. + * + * When an application can determine how much work needs to take place + * (e.g. read a fixed number of bytes from a file) and can monitor its + * progress, it can use the #GtkProgressBar in percentage mode and the user + * sees a growing bar indicating the percentage of the work that has + * been completed. In this mode, the application is required to call + * gtk_progress_bar_set_fraction() periodically to update the progress bar. + * + * When an application has no accurate way of knowing the amount of work + * to do, it can use the #GtkProgressBar in activity mode, which shows + * activity by a block moving back and forth within the progress area. In + * this mode, the application is required to call gtk_progress_bar_pulse() + * periodically to update the progress bar. + * + * There is quite a bit of flexibility provided to control the appearance + * of the #GtkProgressBar. Functions are provided to control the + * orientation of the bar, optional text can be displayed along with + * the bar, and the step size used in activity mode can be set. + */ #define MIN_HORIZONTAL_BAR_WIDTH 150 #define MIN_HORIZONTAL_BAR_HEIGHT 20 @@ -42,19 +70,15 @@ struct _GtkProgressBarPrivate { - GtkOrientation orientation; - gchar *text; gdouble fraction; gdouble pulse_fraction; - guint blocks; - gint in_block; - - gint activity_pos; + double activity_pos; guint activity_blocks; - guint activity_step; + + GtkOrientation orientation; guint activity_dir : 1; guint activity_mode : 1; @@ -74,27 +98,28 @@ enum { PROP_ELLIPSIZE }; -static void gtk_progress_bar_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); -static void gtk_progress_bar_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); -static gboolean gtk_progress_bar_expose (GtkWidget *widget, - GdkEventExpose *event); -static void gtk_progress_bar_size_request (GtkWidget *widget, - GtkRequisition *requisition); -static void gtk_progress_bar_size_allocate (GtkWidget *widget, - GtkAllocation *allocation); -static void gtk_progress_bar_real_update (GtkProgressBar *progress); -static void gtk_progress_bar_paint (GtkProgressBar *progress); -static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress); -static void gtk_progress_bar_realize (GtkWidget *widget); -static void gtk_progress_bar_finalize (GObject *object); -static void gtk_progress_bar_set_orientation (GtkProgressBar *progress, - GtkOrientation orientation); +static void gtk_progress_bar_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void gtk_progress_bar_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); +static void gtk_progress_bar_get_preferred_width (GtkWidget *widget, + gint *minimum, + gint *natural); +static void gtk_progress_bar_get_preferred_height (GtkWidget *widget, + gint *minimum, + gint *natural); + +static void gtk_progress_bar_real_update (GtkProgressBar *progress); +static gboolean gtk_progress_bar_draw (GtkWidget *widget, + cairo_t *cr); +static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress); +static void gtk_progress_bar_finalize (GObject *object); +static void gtk_progress_bar_set_orientation (GtkProgressBar *progress, + GtkOrientation orientation); G_DEFINE_TYPE_WITH_CODE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET, G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL)) @@ -112,14 +137,11 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class) gobject_class->get_property = gtk_progress_bar_get_property; gobject_class->finalize = gtk_progress_bar_finalize; - widget_class->realize = gtk_progress_bar_realize; - widget_class->expose_event = gtk_progress_bar_expose; - widget_class->size_request = gtk_progress_bar_size_request; - widget_class->size_allocate = gtk_progress_bar_size_allocate; + widget_class->draw = gtk_progress_bar_draw; + widget_class->get_preferred_width = gtk_progress_bar_get_preferred_width; + widget_class->get_preferred_height = gtk_progress_bar_get_preferred_height; - g_object_class_override_property (gobject_class, - PROP_ORIENTATION, - "orientation"); + g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation"); g_object_class_install_property (gobject_class, PROP_INVERTED, @@ -153,6 +175,20 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class) NULL, GTK_PARAM_READWRITE)); + /** + * GtkProgressBar:show-text: + * + * Sets whether the progress bar will show text superimposed + * over the bar. The shown text is either the value of + * the #GtkProgressBar:text property or, if that is %NULL, + * the #GtkProgressBar:fraction value, as a percentage. + * + * To make a progress bar that is styled and sized suitably for containing + * text (even if the actual text is blank), set #GtkProgressBar:show-text to + * %TRUE and #GtkProgressBar:text to the empty string (not %NULL). + * + * Since: 3.0 + */ g_object_class_install_property (gobject_class, PROP_SHOW_TEXT, g_param_spec_boolean ("show-text", @@ -164,14 +200,14 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class) /** * GtkProgressBar:ellipsize: * - * The preferred place to ellipsize the string, if the progressbar does + * The preferred place to ellipsize the string, if the progress bar does * not have enough room to display the entire string, specified as a - * #PangoEllisizeMode. + * #PangoEllipsizeMode. * * Note that setting this property to a value other than - * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests - * only enough space to display the ellipsis "...". Another means to set a - * progressbar's width is gtk_widget_set_size_request(). + * %PANGO_ELLIPSIZE_NONE has the side-effect that the progress bar requests + * only enough space to display the ellipsis ("..."). Another means to set a + * progress bar's width is gtk_widget_set_size_request(). * * Since: 2.6 */ @@ -251,6 +287,8 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class) G_PARAM_READWRITE)); g_type_class_add_private (class, sizeof (GtkProgressBarPrivate)); + + gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE); } static void @@ -263,53 +301,19 @@ gtk_progress_bar_init (GtkProgressBar *pbar) GtkProgressBarPrivate); priv = pbar->priv; - priv->blocks = 10; - priv->in_block = -1; priv->orientation = GTK_ORIENTATION_HORIZONTAL; priv->inverted = FALSE; priv->pulse_fraction = 0.1; priv->activity_pos = 0; priv->activity_dir = 1; - priv->activity_step = 3; priv->activity_blocks = 5; priv->ellipsize = PANGO_ELLIPSIZE_NONE; priv->show_text = FALSE; priv->text = NULL; priv->fraction = 0.0; -} -static void -gtk_progress_bar_realize (GtkWidget *widget) -{ - GtkAllocation allocation; - GdkWindow *window; - GdkWindowAttr attributes; - gint attributes_mask; - - gtk_widget_set_realized (widget, TRUE); - - gtk_widget_get_allocation (widget, &allocation); - - attributes.window_type = GDK_WINDOW_CHILD; - attributes.x = allocation.x; - attributes.y = allocation.y; - attributes.width = allocation.width; - attributes.height = allocation.height; - attributes.wclass = GDK_INPUT_OUTPUT; - attributes.visual = gtk_widget_get_visual (widget); - attributes.event_mask = gtk_widget_get_events (widget); - attributes.event_mask |= GDK_EXPOSURE_MASK; - - attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL; - window = gdk_window_new (gtk_widget_get_parent_window (widget), - &attributes, attributes_mask); - gtk_widget_set_window (widget, window); - gdk_window_set_user_data (window, widget); - - gtk_widget_style_attach (widget); - gtk_style_set_background (gtk_widget_get_style (widget), - window, GTK_STATE_ACTIVE); + gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE); } static void @@ -389,6 +393,13 @@ gtk_progress_bar_get_property (GObject *object, } } +/** + * gtk_progress_bar_new: + * + * Creates a new #GtkProgressBar. + * + * Returns: a #GtkProgressBar. + */ GtkWidget* gtk_progress_bar_new (void) { @@ -412,64 +423,23 @@ gtk_progress_bar_real_update (GtkProgressBar *pbar) if (priv->activity_mode) { - GtkAllocation allocation; - GtkStyle *style; - guint size; - - gtk_widget_get_allocation (widget, &allocation); - style = gtk_widget_get_style (widget); - /* advance the block */ - if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + if (priv->activity_dir == 0) { - /* Update our activity step. */ - priv->activity_step = allocation.width * priv->pulse_fraction; - - size = MAX (2, allocation.width / priv->activity_blocks); - - if (priv->activity_dir == 0) + priv->activity_pos += priv->pulse_fraction; + if (priv->activity_pos > 1.0) { - priv->activity_pos += priv->activity_step; - if (priv->activity_pos + size >= allocation.width - style->xthickness) - { - priv->activity_pos = allocation.width - style->xthickness - size; - priv->activity_dir = 1; - } - } - else - { - priv->activity_pos -= priv->activity_step; - if (priv->activity_pos <= style->xthickness) - { - priv->activity_pos = style->xthickness; - priv->activity_dir = 0; - } + priv->activity_pos = 1.0; + priv->activity_dir = 1; } } else { - /* Update our activity step. */ - priv->activity_step = allocation.height * priv->pulse_fraction; - - size = MAX (2, allocation.height / priv->activity_blocks); - - if (priv->activity_dir == 0) + priv->activity_pos -= priv->pulse_fraction; + if (priv->activity_pos <= 0) { - priv->activity_pos += priv->activity_step; - if (priv->activity_pos + size >= allocation.height - style->ythickness) - { - priv->activity_pos = allocation.height - style->ythickness - size; - priv->activity_dir = 1; - } - } - else - { - priv->activity_pos -= priv->activity_step; - if (priv->activity_pos <= style->ythickness) - { - priv->activity_pos = style->ythickness; - priv->activity_dir = 0; - } + priv->activity_pos = 0; + priv->activity_dir = 0; } } } @@ -487,18 +457,6 @@ gtk_progress_bar_finalize (GObject *object) G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object); } -static gboolean -gtk_progress_bar_expose (GtkWidget *widget, - GdkEventExpose *event) -{ - GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget); - - if (gtk_widget_is_drawable (widget)) - gtk_progress_bar_paint (pbar); - - return FALSE; -} - static gchar * get_current_text (GtkProgressBar *pbar) { @@ -511,33 +469,36 @@ get_current_text (GtkProgressBar *pbar) } static void -gtk_progress_bar_size_request (GtkWidget *widget, - GtkRequisition *requisition) +gtk_progress_bar_get_preferred_width (GtkWidget *widget, + gint *minimum, + gint *natural) { GtkProgressBar *pbar; GtkProgressBarPrivate *priv; - GtkStyle *style; + GtkStyleContext *style_context; + GtkStateFlags state; + GtkBorder padding; gchar *buf; PangoRectangle logical_rect; PangoLayout *layout; - gint width, height; - gint xspacing, yspacing; - gint min_width, min_height; + gint width; + gint xspacing; + gint min_width; g_return_if_fail (GTK_IS_PROGRESS_BAR (widget)); - g_return_if_fail (requisition != NULL); - style = gtk_widget_get_style (widget); + style_context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (style_context, state, &padding); + gtk_widget_style_get (widget, "xspacing", &xspacing, - "yspacing", &yspacing, NULL); pbar = GTK_PROGRESS_BAR (widget); priv = pbar->priv; - width = 2 * style->xthickness + xspacing; - height = 2 * style->ythickness + yspacing; + width = padding.left + padding.right + xspacing; if (priv->show_text) { @@ -554,7 +515,9 @@ gtk_progress_bar_size_request (GtkWidget *widget, /* The minimum size for ellipsized text is ~ 3 chars */ context = pango_layout_get_context (layout); - metrics = pango_context_get_metrics (context, style->font_desc, pango_context_get_language (context)); + metrics = pango_context_get_metrics (context, + pango_context_get_font_description (context), + pango_context_get_language (context)); char_width = pango_font_metrics_get_approximate_char_width (metrics); pango_font_metrics_unref (metrics); @@ -564,8 +527,6 @@ gtk_progress_bar_size_request (GtkWidget *widget, else width += logical_rect.width; - height += logical_rect.height; - g_object_unref (layout); g_free (buf); } @@ -573,41 +534,86 @@ gtk_progress_bar_size_request (GtkWidget *widget, if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) gtk_widget_style_get (widget, "min-horizontal-bar-width", &min_width, - "min-horizontal-bar-height", &min_height, NULL); else gtk_widget_style_get (widget, "min-vertical-bar-width", &min_width, - "min-vertical-bar-height", &min_height, NULL); - requisition->width = MAX (min_width, width); - requisition->height = MAX (min_height, height); + *minimum = *natural = MAX (min_width, width); } static void -gtk_progress_bar_size_allocate (GtkWidget *widget, - GtkAllocation *allocation) +gtk_progress_bar_get_preferred_height (GtkWidget *widget, + gint *minimum, + gint *natural) { - gtk_widget_set_allocation (widget, allocation); + GtkProgressBar *pbar; + GtkProgressBarPrivate *priv; + GtkStyleContext *context; + GtkStateFlags state; + GtkBorder padding; + gchar *buf; + PangoRectangle logical_rect; + PangoLayout *layout; + gint height; + gint yspacing; + gint min_height; + + g_return_if_fail (GTK_IS_PROGRESS_BAR (widget)); + + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (context, state, &padding); + + gtk_widget_style_get (widget, + "yspacing", &yspacing, + NULL); + + pbar = GTK_PROGRESS_BAR (widget); + priv = pbar->priv; + + height = padding.top + padding.bottom + yspacing; + + if (priv->show_text) + { + buf = get_current_text (pbar); + layout = gtk_widget_create_pango_layout (widget, buf); - if (gtk_widget_get_realized (widget)) - gdk_window_move_resize (gtk_widget_get_window (widget), - allocation->x, allocation->y, - allocation->width, allocation->height); + pango_layout_get_pixel_extents (layout, NULL, &logical_rect); + + height += logical_rect.height; + + g_object_unref (layout); + g_free (buf); + } + + if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) + gtk_widget_style_get (widget, + "min-horizontal-bar-height", &min_height, + NULL); + else + gtk_widget_style_get (widget, + "min-vertical-bar-height", &min_height, + NULL); + + *minimum = *natural = MAX (min_height, height); } static void gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) { GtkProgressBarPrivate *priv = pbar->priv; - GtkAllocation allocation; - GtkStyle *style; + GtkStyleContext *context; + GtkStateFlags state; + GtkBorder padding; GtkWidget *widget = GTK_WIDGET (pbar); GtkOrientation orientation; gboolean inverted; - style = gtk_widget_get_style (widget); + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (context, state, &padding); orientation = priv->orientation; inverted = priv->inverted; @@ -623,14 +629,12 @@ gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) { if (!inverted) { - priv->activity_pos = style->xthickness; + priv->activity_pos = 0.0; priv->activity_dir = 0; } else { - gtk_widget_get_allocation (widget, &allocation); - priv->activity_pos = allocation.width - style->xthickness - - (allocation.height - style->ythickness * 2); + priv->activity_pos = 1.0; priv->activity_dir = 1; } } @@ -638,14 +642,12 @@ gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) { if (!inverted) { - priv->activity_pos = style->ythickness; + priv->activity_pos = 0.0; priv->activity_dir = 0; } else { - gtk_widget_get_allocation (widget, &allocation); - priv->activity_pos = allocation.height - style->ythickness - - (allocation.width - style->xthickness * 2); + priv->activity_pos = 1.0; priv->activity_dir = 1; } } @@ -658,107 +660,134 @@ gtk_progress_bar_get_activity (GtkProgressBar *pbar, gint *amount) { GtkProgressBarPrivate *priv = pbar->priv; - GtkAllocation allocation; GtkWidget *widget = GTK_WIDGET (pbar); + GtkStyleContext *context; + GtkAllocation allocation; + GtkStateFlags state; + GtkBorder padding; + int size; - *offset = priv->activity_pos; - + context = gtk_widget_get_style_context (widget); + state = gtk_style_context_get_state (context); gtk_widget_get_allocation (widget, &allocation); + gtk_style_context_get_padding (context, state, &padding); if (orientation == GTK_ORIENTATION_HORIZONTAL) - *amount = MAX (2, allocation.width / priv->activity_blocks); + size = allocation.width - padding.left - padding.top; else - *amount = MAX (2, allocation.height / priv->activity_blocks); + size = allocation.height - padding.left - padding.top; + + *amount = MAX (2, size / priv->activity_blocks); + *offset = priv->activity_pos * (size - *amount); } static void gtk_progress_bar_paint_activity (GtkProgressBar *pbar, + cairo_t *cr, GtkOrientation orientation, - gboolean inverted) + gboolean inverted, + int width, + int height) { - GtkAllocation allocation; - GtkStyle *style; + GtkStyleContext *context; + GtkStateFlags state; + GtkBorder padding; GtkWidget *widget = GTK_WIDGET (pbar); GdkRectangle area; - style = gtk_widget_get_style (widget); - gtk_widget_get_allocation (widget, &allocation); + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (context, state, &padding); if (orientation == GTK_ORIENTATION_HORIZONTAL) { gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width); - area.y = style->ythickness; - area.height = allocation.height - 2 * style->ythickness; + area.y = padding.top; + area.height = height - padding.top - padding.bottom; } else { gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height); - area.x = style->xthickness; - area.width = allocation.width - 2 * style->xthickness; + area.x = padding.left; + area.width = width - padding.left - padding.right; } - gtk_paint_box (style, - gtk_widget_get_window (widget), - GTK_STATE_PRELIGHT, GTK_SHADOW_OUT, - &area, widget, "bar", - area.x, area.y, area.width, area.height); + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE); + + gtk_render_activity (context, cr, area.x, area.y, area.width, area.height); + + gtk_style_context_restore (context); } static void gtk_progress_bar_paint_continuous (GtkProgressBar *pbar, + cairo_t *cr, gint amount, GtkOrientation orientation, - gboolean inverted) + gboolean inverted, + int width, + int height) { - GtkAllocation allocation; - GtkStyle *style; + GtkStyleContext *context; + GtkStateFlags state; + GtkBorder padding; GtkWidget *widget = GTK_WIDGET (pbar); GdkRectangle area; if (amount <= 0) return; - style = gtk_widget_get_style (widget); - gtk_widget_get_allocation (widget, &allocation); + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (context, state, &padding); if (orientation == GTK_ORIENTATION_HORIZONTAL) { area.width = amount; - area.height = allocation.height - style->ythickness * 2; - area.y = style->ythickness; + area.height = height - padding.top - padding.bottom; + area.y = padding.top; - area.x = style->xthickness; - if (inverted) - area.x = allocation.width - amount - area.x; + if (!inverted) + area.x = padding.left; + else + area.x = width - amount - padding.right; } else { - area.width = allocation.width - style->xthickness * 2; + area.width = width - padding.left - padding.right; area.height = amount; - area.x = style->xthickness; + area.x = padding.left; - area.y = style->ythickness; - if (inverted) - area.y = allocation.height - amount - area.y; + if (!inverted) + area.y = padding.top; + else + area.y = height - amount - padding.bottom; } - gtk_paint_box (style, - gtk_widget_get_window (widget), - GTK_STATE_PRELIGHT, GTK_SHADOW_OUT, - &area, widget, "bar", - area.x, area.y, area.width, area.height); + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR); + + gtk_render_activity (context, cr, area.x, area.y, area.width, area.height); + + gtk_style_context_restore (context); } static void gtk_progress_bar_paint_text (GtkProgressBar *pbar, + cairo_t *cr, gint offset, gint amount, GtkOrientation orientation, - gboolean inverted) + gboolean inverted, + int width, + int height) { GtkProgressBarPrivate *priv = pbar->priv; - GtkAllocation allocation; - GtkStyle *style; + GtkStyleContext *context; + GtkStateFlags state; + GtkBorder padding; GtkWidget *widget = GTK_WIDGET (pbar); gint x; gint y; @@ -770,8 +799,9 @@ gtk_progress_bar_paint_text (GtkProgressBar *pbar, gfloat text_xalign = 0.5; gfloat text_yalign = 0.5; - style = gtk_widget_get_style (widget); - gtk_widget_get_allocation (widget, &allocation); + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (context, state, &padding); if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR) text_xalign = 1.0 - text_xalign; @@ -781,18 +811,17 @@ gtk_progress_bar_paint_text (GtkProgressBar *pbar, layout = gtk_widget_create_pango_layout (widget, buf); pango_layout_set_ellipsize (layout, priv->ellipsize); if (priv->ellipsize) - pango_layout_set_width (layout, allocation.width * PANGO_SCALE); + pango_layout_set_width (layout, width * PANGO_SCALE); pango_layout_get_pixel_extents (layout, NULL, &logical_rect); - x = style->xthickness + 1 + text_xalign * (allocation.width - 2 * style->xthickness - 2 - logical_rect.width); - - y = style->ythickness + 1 + text_yalign * (allocation.height - 2 * style->ythickness - 2 - logical_rect.height); + x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width); + y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height); - rect.x = style->xthickness; - rect.y = style->ythickness; - rect.width = allocation.width - 2 * style->xthickness; - rect.height = allocation.height - 2 * style->ythickness; + rect.x = padding.left; + rect.y = padding.top; + rect.width = width - padding.left - padding.right; + rect.height = height - padding.top - padding.bottom; prelight_clip = start_clip = end_clip = rect; @@ -843,54 +872,63 @@ gtk_progress_bar_paint_text (GtkProgressBar *pbar, } } + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH); + if (start_clip.width > 0 && start_clip.height > 0) - gtk_paint_layout (style, - gtk_widget_get_window (widget), - GTK_STATE_NORMAL, - FALSE, - &start_clip, - widget, - "progressbar", - x, y, - layout); + { + cairo_save (cr); + gdk_cairo_rectangle (cr, &start_clip); + cairo_clip (cr); + + gtk_render_layout (context, cr, x, y, layout); + cairo_restore (cr); + } if (end_clip.width > 0 && end_clip.height > 0) - gtk_paint_layout (style, - gtk_widget_get_window (widget), - GTK_STATE_NORMAL, - FALSE, - &end_clip, - widget, - "progressbar", - x, y, - layout); - - gtk_paint_layout (style, - gtk_widget_get_window (widget), - GTK_STATE_PRELIGHT, - FALSE, - &prelight_clip, - widget, - "progressbar", - x, y, - layout); + { + cairo_save (cr); + gdk_cairo_rectangle (cr, &end_clip); + cairo_clip (cr); + + gtk_render_layout (context, cr, x, y, layout); + cairo_restore (cr); + } + + gtk_style_context_restore (context); + + cairo_save (cr); + gdk_cairo_rectangle (cr, &prelight_clip); + cairo_clip (cr); + + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR); + + gtk_render_layout (context, cr, x, y, layout); + + gtk_style_context_restore (context); + cairo_restore (cr); g_object_unref (layout); g_free (buf); } -static void -gtk_progress_bar_paint (GtkProgressBar *pbar) +static gboolean +gtk_progress_bar_draw (GtkWidget *widget, + cairo_t *cr) { + GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget); GtkProgressBarPrivate *priv = pbar->priv; - GtkAllocation allocation; - GtkWidget *widget = GTK_WIDGET (pbar); GtkOrientation orientation; gboolean inverted; - GtkStyle *style; + GtkStyleContext *context; + GtkStateFlags state; + GtkBorder padding; + int width, height; - style = gtk_widget_get_style (widget); - gtk_widget_get_allocation (widget, &allocation); + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_padding (context, state, &padding); orientation = priv->orientation; inverted = priv->inverted; @@ -899,18 +937,22 @@ gtk_progress_bar_paint (GtkProgressBar *pbar) if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) inverted = !inverted; } + width = gtk_widget_get_allocated_width (widget); + height = gtk_widget_get_allocated_height (widget); + + gtk_style_context_save (context); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH); + + gtk_render_background (context, cr, 0, 0, width, height); + gtk_render_frame (context, cr, 0, 0, width, height); - gtk_paint_box (style, - gtk_widget_get_window (widget), - GTK_STATE_NORMAL, GTK_SHADOW_IN, - NULL, widget, "trough", - 0, 0, - allocation.width, - allocation.height); + gtk_style_context_restore (context); if (priv->activity_mode) { - gtk_progress_bar_paint_activity (pbar, orientation, inverted); + gtk_progress_bar_paint_activity (pbar, cr, + orientation, inverted, + width, height); if (priv->show_text) { @@ -918,7 +960,10 @@ gtk_progress_bar_paint (GtkProgressBar *pbar) gint amount; gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount); - gtk_progress_bar_paint_text (pbar, offset, amount, orientation, inverted); + gtk_progress_bar_paint_text (pbar, cr, + offset, amount, + orientation, inverted, + width, height); } } else @@ -927,17 +972,19 @@ gtk_progress_bar_paint (GtkProgressBar *pbar) gint space; if (orientation == GTK_ORIENTATION_HORIZONTAL) - space = allocation.width - 2 * style->xthickness; + space = width - padding.left - padding.right; else - space = allocation.height - 2 * style->ythickness; + space = height - padding.top - padding.bottom; amount = space * gtk_progress_bar_get_fraction (pbar); - gtk_progress_bar_paint_continuous (pbar, amount, orientation, inverted); + gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height); if (priv->show_text) - gtk_progress_bar_paint_text (pbar, -1, amount, orientation, inverted); + gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height); } + + return FALSE; } static void @@ -955,8 +1002,7 @@ gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar, if (priv->activity_mode) gtk_progress_bar_act_mode_enter (pbar); - if (gtk_widget_is_drawable (GTK_WIDGET (pbar))) - gtk_widget_queue_resize (GTK_WIDGET (pbar)); + gtk_widget_queue_resize (GTK_WIDGET (pbar)); } } @@ -968,8 +1014,7 @@ gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar, * Causes the progress bar to "fill in" the given fraction * of the bar. The fraction should be between 0.0 and 1.0, * inclusive. - * - **/ + */ void gtk_progress_bar_set_fraction (GtkProgressBar *pbar, gdouble fraction) @@ -980,7 +1025,7 @@ gtk_progress_bar_set_fraction (GtkProgressBar *pbar, priv = pbar->priv; - priv->fraction = fraction; + priv->fraction = CLAMP(fraction, 0.0, 1.0); gtk_progress_bar_set_activity_mode (pbar, FALSE); gtk_progress_bar_real_update (pbar); @@ -991,12 +1036,12 @@ gtk_progress_bar_set_fraction (GtkProgressBar *pbar, * gtk_progress_bar_pulse: * @pbar: a #GtkProgressBar * - * Indicates that some progress is made, but you don't know how much. + * Indicates that some progress has been made, but you don't know how much. * Causes the progress bar to enter "activity mode," where a block * bounces back and forth. Each call to gtk_progress_bar_pulse() * causes the block to move by a little bit (the amount of movement * per pulse is determined by gtk_progress_bar_set_pulse_step()). - **/ + */ void gtk_progress_bar_pulse (GtkProgressBar *pbar) { @@ -1012,7 +1057,15 @@ gtk_progress_bar_pulse (GtkProgressBar *pbar) * @text: (allow-none): a UTF-8 string, or %NULL * * Causes the given @text to appear superimposed on the progress bar. - **/ + * + * If @text is %NULL and #GtkProgressBar:show-text is %TRUE, the current + * value of #GtkProgressBar:fraction will be displayed as a percentage. + * + * If @text is non-%NULL and #GtkProgressBar:show-text is %TRUE, the text will + * be displayed. In this case, it will not display the progress percentage. + * If @text is the empty string, the progress bar will still be styled and sized + * suitably for containing text, as long as #GtkProgressBar:show-text is %TRUE. + */ void gtk_progress_bar_set_text (GtkProgressBar *pbar, const gchar *text) @@ -1023,11 +1076,14 @@ gtk_progress_bar_set_text (GtkProgressBar *pbar, priv = pbar->priv; + /* Don't notify again if nothing's changed. */ + if (g_strcmp0 (priv->text, text) == 0) + return; + g_free (priv->text); - priv->text = text && *text ? g_strdup (text) : NULL; + priv->text = g_strdup (text); - if (gtk_widget_is_drawable (GTK_WIDGET (pbar))) - gtk_widget_queue_resize (GTK_WIDGET (pbar)); + gtk_widget_queue_resize (GTK_WIDGET (pbar)); g_object_notify (G_OBJECT (pbar), "text"); } @@ -1037,10 +1093,14 @@ gtk_progress_bar_set_text (GtkProgressBar *pbar, * @pbar: a #GtkProgressBar * @show_text: whether to show superimposed text * - * Sets whether the progressbar will show text superimposed + * Sets whether the progress bar will show text superimposed * over the bar. The shown text is either the value of - * the #GtkProgressBar::text property or, if that is %NULL, - * the #GtkProgressBar::fraction value, as a percentage. + * the #GtkProgressBar:text property or, if that is %NULL, + * the #GtkProgressBar:fraction value, as a percentage. + * + * To make a progress bar that is styled and sized suitably for containing + * text (even if the actual text is blank), set #GtkProgressBar:show-text to + * %TRUE and #GtkProgressBar:text to the empty string (not %NULL). * * Since: 3.0 */ @@ -1060,8 +1120,7 @@ gtk_progress_bar_set_show_text (GtkProgressBar *pbar, { priv->show_text = show_text; - if (gtk_widget_is_drawable (GTK_WIDGET (pbar))) - gtk_widget_queue_resize (GTK_WIDGET (pbar)); + gtk_widget_queue_resize (GTK_WIDGET (pbar)); g_object_notify (G_OBJECT (pbar), "show-text"); } @@ -1071,7 +1130,7 @@ gtk_progress_bar_set_show_text (GtkProgressBar *pbar, * gtk_progress_bar_get_show_text: * @pbar: a #GtkProgressBar * - * Gets the value of the #GtkProgressBar::show-text property. + * Gets the value of the #GtkProgressBar:show-text property. * See gtk_progress_bar_set_show_text(). * * Returns: %TRUE if text is shown in the progress bar @@ -1093,7 +1152,7 @@ gtk_progress_bar_get_show_text (GtkProgressBar *pbar) * * Sets the fraction of total progress bar length to move the * bouncing block for each call to gtk_progress_bar_pulse(). - **/ + */ void gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar, gdouble fraction) @@ -1118,9 +1177,9 @@ gtk_progress_bar_set_orientation (GtkProgressBar *pbar, if (priv->orientation != orientation) { priv->orientation = orientation; + _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar)); - if (gtk_widget_is_drawable (GTK_WIDGET (pbar))) - gtk_widget_queue_resize (GTK_WIDGET (pbar)); + gtk_widget_queue_resize (GTK_WIDGET (pbar)); } } @@ -1146,8 +1205,7 @@ gtk_progress_bar_set_inverted (GtkProgressBar *pbar, { priv->inverted = inverted; - if (gtk_widget_is_drawable (GTK_WIDGET (pbar))) - gtk_widget_queue_resize (GTK_WIDGET (pbar)); + gtk_widget_queue_resize (GTK_WIDGET (pbar)); g_object_notify (G_OBJECT (pbar), "inverted"); } @@ -1164,8 +1222,8 @@ gtk_progress_bar_set_inverted (GtkProgressBar *pbar, * * Return value: text, or %NULL; this string is owned by the widget * and should not be modified or freed. - **/ -G_CONST_RETURN gchar* + */ +const gchar* gtk_progress_bar_get_text (GtkProgressBar *pbar) { g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL); @@ -1180,7 +1238,7 @@ gtk_progress_bar_get_text (GtkProgressBar *pbar) * Returns the current fraction of the task that's been completed. * * Return value: a fraction from 0.0 to 1.0 - **/ + */ gdouble gtk_progress_bar_get_fraction (GtkProgressBar *pbar) { @@ -1193,10 +1251,10 @@ gtk_progress_bar_get_fraction (GtkProgressBar *pbar) * gtk_progress_bar_get_pulse_step: * @pbar: a #GtkProgressBar * - * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step() + * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step(). * * Return value: a fraction from 0.0 to 1.0 - **/ + */ gdouble gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar) { @@ -1209,7 +1267,7 @@ gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar) * gtk_progress_bar_get_inverted: * @pbar: a #GtkProgressBar * - * Gets the value set by gtk_progress_bar_set_inverted() + * Gets the value set by gtk_progress_bar_set_inverted(). * * Return value: %TRUE if the progress bar is inverted */ @@ -1230,7 +1288,7 @@ gtk_progress_bar_get_inverted (GtkProgressBar *pbar) * if there is not enough space to render the entire string. * * Since: 2.6 - **/ + */ void gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar, PangoEllipsizeMode mode) @@ -1256,13 +1314,13 @@ gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar, * gtk_progress_bar_get_ellipsize: * @pbar: a #GtkProgressBar * - * Returns the ellipsizing position of the progressbar. + * Returns the ellipsizing position of the progress bar. * See gtk_progress_bar_set_ellipsize(). * * Return value: #PangoEllipsizeMode * * Since: 2.6 - **/ + */ PangoEllipsizeMode gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar) {