progressbar: Store activity position as percentage

... instead of in absolute pixels.
This commit is contained in:
Benjamin Otte 2012-12-23 19:13:11 +01:00
parent 02b440b466
commit b39bb4400e

View File

@ -75,7 +75,7 @@ struct _GtkProgressBarPrivate
gdouble fraction; gdouble fraction;
gdouble pulse_fraction; gdouble pulse_fraction;
gint activity_pos; double activity_pos;
guint activity_blocks; guint activity_blocks;
GtkOrientation orientation; GtkOrientation orientation;
@ -415,7 +415,6 @@ gtk_progress_bar_real_update (GtkProgressBar *pbar)
{ {
GtkProgressBarPrivate *priv; GtkProgressBarPrivate *priv;
GtkWidget *widget; GtkWidget *widget;
int activity_step;
g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar)); g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
@ -424,68 +423,23 @@ gtk_progress_bar_real_update (GtkProgressBar *pbar)
if (priv->activity_mode) if (priv->activity_mode)
{ {
GtkAllocation allocation;
GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding;
gint 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 */ /* advance the block */
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) if (priv->activity_dir == 0)
{ {
/* Update our activity step. */ priv->activity_pos += priv->pulse_fraction;
activity_step = allocation.width * priv->pulse_fraction; if (priv->activity_pos > 1.0)
size = MAX (2, allocation.width / priv->activity_blocks);
if (priv->activity_dir == 0)
{ {
priv->activity_pos += activity_step; priv->activity_pos = 1.0;
if (priv->activity_pos + size >= allocation.width - padding.left) priv->activity_dir = 1;
{
priv->activity_pos = allocation.width - padding.left - size;
priv->activity_dir = 1;
}
}
else
{
priv->activity_pos -= activity_step;
if (priv->activity_pos <= padding.left)
{
priv->activity_pos = padding.left;
priv->activity_dir = 0;
}
} }
} }
else else
{ {
/* Update our activity step. */ priv->activity_pos -= priv->pulse_fraction;
activity_step = allocation.height * priv->pulse_fraction; if (priv->activity_pos <= 0)
size = MAX (2, allocation.height / priv->activity_blocks);
if (priv->activity_dir == 0)
{ {
priv->activity_pos += activity_step; priv->activity_pos = 0;
if (priv->activity_pos + size >= allocation.height - padding.top) priv->activity_dir = 0;
{
priv->activity_pos = allocation.height - padding.top - size;
priv->activity_dir = 1;
}
}
else
{
priv->activity_pos -= activity_step;
if (priv->activity_pos <= padding.top)
{
priv->activity_pos = padding.top;
priv->activity_dir = 0;
}
} }
} }
} }
@ -650,7 +604,6 @@ static void
gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar) gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
{ {
GtkProgressBarPrivate *priv = pbar->priv; GtkProgressBarPrivate *priv = pbar->priv;
GtkAllocation allocation;
GtkStyleContext *context; GtkStyleContext *context;
GtkStateFlags state; GtkStateFlags state;
GtkBorder padding; GtkBorder padding;
@ -676,14 +629,12 @@ gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
{ {
if (!inverted) if (!inverted)
{ {
priv->activity_pos = padding.left; priv->activity_pos = 0.0;
priv->activity_dir = 0; priv->activity_dir = 0;
} }
else else
{ {
gtk_widget_get_allocation (widget, &allocation); priv->activity_pos = 1.0;
priv->activity_pos = allocation.width - padding.left -
(allocation.height - padding.top - padding.bottom);
priv->activity_dir = 1; priv->activity_dir = 1;
} }
} }
@ -691,14 +642,12 @@ gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
{ {
if (!inverted) if (!inverted)
{ {
priv->activity_pos = padding.top; priv->activity_pos = 0.0;
priv->activity_dir = 0; priv->activity_dir = 0;
} }
else else
{ {
gtk_widget_get_allocation (widget, &allocation); priv->activity_pos = 1.0;
priv->activity_pos = allocation.height - padding.top -
(allocation.width - padding.left - padding.right);
priv->activity_dir = 1; priv->activity_dir = 1;
} }
} }
@ -711,17 +660,25 @@ gtk_progress_bar_get_activity (GtkProgressBar *pbar,
gint *amount) gint *amount)
{ {
GtkProgressBarPrivate *priv = pbar->priv; GtkProgressBarPrivate *priv = pbar->priv;
GtkAllocation allocation;
GtkWidget *widget = GTK_WIDGET (pbar); 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_widget_get_allocation (widget, &allocation);
gtk_style_context_get_padding (context, state, &padding);
if (orientation == GTK_ORIENTATION_HORIZONTAL) if (orientation == GTK_ORIENTATION_HORIZONTAL)
*amount = MAX (2, allocation.width / priv->activity_blocks); size = allocation.width - padding.left - padding.top;
else 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 static void