X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkprogressbar.c;h=932980a33e5deb8599becbdabf73bb98e0968d8d;hb=d97861bd8b338c3d25d7ffb5496edee9eee9bfbb;hp=d97a81c0a0558eb9c49bfda1e25c9a948b0e222d;hpb=4a0aa41742a8830aae7e813f2e7b0386e394ada6;p=~andy%2Fgtk diff --git a/gtk/gtkprogressbar.c b/gtk/gtkprogressbar.c index d97a81c0a..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 . */ /* @@ -33,6 +31,8 @@ #include "gtkprivate.h" #include "gtkintl.h" +#include "a11y/gtkprogressbaraccessible.h" + /** * SECTION:gtkprogressbar * @Short_description: A widget which indicates progress visually @@ -70,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; @@ -180,12 +176,16 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class) GTK_PARAM_READWRITE)); /** - * GtkProgressBar:show_text: + * GtkProgressBar:show-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 */ @@ -200,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 */ @@ -287,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 @@ -299,14 +301,11 @@ 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; @@ -424,68 +423,23 @@ gtk_progress_bar_real_update (GtkProgressBar *pbar) if (priv->activity_mode) { - GtkAllocation allocation; - GtkStyleContext *context; - GtkStateFlags state; - GtkBorder padding; - guint size; - - 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); - /* 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 - padding.left) - { - priv->activity_pos = allocation.width - padding.left - size; - priv->activity_dir = 1; - } - } - else - { - priv->activity_pos -= priv->activity_step; - if (priv->activity_pos <= padding.left) - { - priv->activity_pos = padding.left; - 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->activity_step; - if (priv->activity_pos + size >= allocation.height - padding.top) - { - priv->activity_pos = allocation.height - padding.top - size; - priv->activity_dir = 1; - } - } - else + priv->activity_pos -= priv->pulse_fraction; + if (priv->activity_pos <= 0) { - priv->activity_pos -= priv->activity_step; - if (priv->activity_pos <= padding.top) - { - priv->activity_pos = padding.top; - priv->activity_dir = 0; - } + priv->activity_pos = 0; + priv->activity_dir = 0; } } } @@ -555,15 +509,15 @@ gtk_progress_bar_get_preferred_width (GtkWidget *widget, if (priv->ellipsize) { - const PangoFontDescription *font_desc; PangoContext *context; PangoFontMetrics *metrics; gint char_width; /* The minimum size for ellipsized text is ~ 3 chars */ context = pango_layout_get_context (layout); - font_desc = gtk_style_context_get_font (style_context, state); - metrics = pango_context_get_metrics (context, 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); @@ -650,7 +604,6 @@ static void gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) { GtkProgressBarPrivate *priv = pbar->priv; - GtkAllocation allocation; GtkStyleContext *context; GtkStateFlags state; GtkBorder padding; @@ -676,14 +629,12 @@ gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) { if (!inverted) { - priv->activity_pos = padding.left; + priv->activity_pos = 0.0; priv->activity_dir = 0; } else { - gtk_widget_get_allocation (widget, &allocation); - priv->activity_pos = allocation.width - padding.left - - (allocation.height - padding.top - padding.bottom); + priv->activity_pos = 1.0; priv->activity_dir = 1; } } @@ -691,14 +642,12 @@ gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) { if (!inverted) { - priv->activity_pos = padding.top; + priv->activity_pos = 0.0; priv->activity_dir = 0; } else { - gtk_widget_get_allocation (widget, &allocation); - priv->activity_pos = allocation.height - padding.top - - (allocation.width - padding.left - padding.right); + priv->activity_pos = 1.0; priv->activity_dir = 1; } } @@ -711,17 +660,25 @@ 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 @@ -757,6 +714,7 @@ gtk_progress_bar_paint_activity (GtkProgressBar *pbar, 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); @@ -970,6 +928,7 @@ gtk_progress_bar_draw (GtkWidget *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; @@ -987,8 +946,6 @@ gtk_progress_bar_draw (GtkWidget *widget, gtk_render_background (context, cr, 0, 0, width, height); gtk_render_frame (context, cr, 0, 0, width, height); - gtk_style_context_get_padding (context, state, &padding); - gtk_style_context_restore (context); if (priv->activity_mode) @@ -1045,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)); } } @@ -1058,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) @@ -1070,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); @@ -1081,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) { @@ -1102,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) @@ -1113,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"); } @@ -1127,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 */ @@ -1150,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"); } @@ -1161,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 @@ -1183,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) @@ -1210,8 +1179,7 @@ gtk_progress_bar_set_orientation (GtkProgressBar *pbar, 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)); } } @@ -1237,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"); } @@ -1255,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); @@ -1271,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) { @@ -1284,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) { @@ -1300,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 */ @@ -1321,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) @@ -1347,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) {