gtk: Allow hiding the trough/slider in GtkScale

When setting a GtkRange's upper and lower values to the same
value, the slider will not be drawn any more.

https://bugzilla.gnome.org/show_bug.cgi?id=549720
This commit is contained in:
Bastien Nocera 2010-11-22 17:46:25 +00:00
parent 926622e2dd
commit 959fc60c47
3 changed files with 48 additions and 1 deletions

View File

@ -1280,7 +1280,7 @@ gtk_range_set_range (GtkRange *range,
gdouble value; gdouble value;
g_return_if_fail (GTK_IS_RANGE (range)); g_return_if_fail (GTK_IS_RANGE (range));
g_return_if_fail (min < max); g_return_if_fail (min <= max);
priv = range->priv; priv = range->priv;
@ -2016,11 +2016,16 @@ gtk_range_draw (GtkWidget *widget,
gint focus_line_width = 0; gint focus_line_width = 0;
gint focus_padding = 0; gint focus_padding = 0;
gboolean touchscreen; gboolean touchscreen;
gboolean draw_trough = TRUE;
g_object_get (gtk_widget_get_settings (widget), g_object_get (gtk_widget_get_settings (widget),
"gtk-touchscreen-mode", &touchscreen, "gtk-touchscreen-mode", &touchscreen,
NULL); NULL);
if (GTK_IS_SCALE (widget) &&
priv->adjustment->upper == priv->adjustment->lower)
draw_trough = FALSE;
style = gtk_widget_get_style (widget); style = gtk_widget_get_style (widget);
if (gtk_widget_get_can_focus (GTK_WIDGET (range))) if (gtk_widget_get_can_focus (GTK_WIDGET (range)))
gtk_widget_style_get (GTK_WIDGET (range), gtk_widget_style_get (GTK_WIDGET (range),
@ -2112,6 +2117,7 @@ gtk_range_draw (GtkWidget *widget,
} }
} }
if (draw_trough)
{ {
gint trough_change_pos_x = width; gint trough_change_pos_x = width;
gint trough_change_pos_y = height; gint trough_change_pos_y = height;
@ -2147,6 +2153,17 @@ gtk_range_draw (GtkWidget *widget,
width - trough_change_pos_x, width - trough_change_pos_x,
height - trough_change_pos_y); height - trough_change_pos_y);
} }
else
{
gtk_paint_box (style, cr,
sensitive ? GTK_STATE_ACTIVE : GTK_STATE_INSENSITIVE,
GTK_SHADOW_IN,
GTK_WIDGET (range),
"trough-upper",
x, y,
width,
height);
}
if (priv->show_fill_level && if (priv->show_fill_level &&
priv->adjustment->upper - priv->adjustment->page_size - priv->adjustment->upper - priv->adjustment->page_size -
@ -2236,6 +2253,7 @@ gtk_range_draw (GtkWidget *widget,
gdk_cairo_rectangle (cr, &priv->slider); gdk_cairo_rectangle (cr, &priv->slider);
cairo_clip (cr); cairo_clip (cr);
if (draw_trough)
{ {
gtk_paint_slider (style, gtk_paint_slider (style,
cr, cr,

View File

@ -55,6 +55,11 @@
* To detect changes to the value, you would normally use the * To detect changes to the value, you would normally use the
* #GtkRange::value-changed signal. * #GtkRange::value-changed signal.
* *
* Note that using the same upper and lower bounds for the #GtkScale (through
* the #GtkRange methods) will hide the slider itself. This is useful for
* applications that want to show an undeterminate value on the scale, without
* changing the layout of the application (such as movie or music players).
*
* <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title> * <refsect2 id="GtkScale-BUILDER-UI"><title>GtkScale as GtkBuildable</title>
* GtkScale supports a custom &lt;marks&gt; element, which * GtkScale supports a custom &lt;marks&gt; element, which
* can contain multiple &lt;mark&gt; elements. The "value" and "position" * can contain multiple &lt;mark&gt; elements. The "value" and "position"

View File

@ -20,6 +20,16 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
static void
show_trough_toggled (GtkToggleButton *button,
GtkScale *scale)
{
gboolean value;
value = gtk_toggle_button_get_active (button);
gtk_range_set_range (GTK_RANGE (scale), 0., value ? 100.0 : 0.);
}
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
GtkWidget *window; GtkWidget *window;
@ -27,6 +37,7 @@ int main (int argc, char *argv[])
GtkWidget *box2; GtkWidget *box2;
GtkWidget *frame; GtkWidget *frame;
GtkWidget *scale; GtkWidget *scale;
GtkWidget *toggle;
gdouble marks[3] = { 0.0, 50.0, 100.0 }; gdouble marks[3] = { 0.0, 50.0, 100.0 };
const gchar *labels[3] = { const gchar *labels[3] = {
"<small>Left</small>", "<small>Left</small>",
@ -96,6 +107,19 @@ int main (int argc, char *argv[])
gtk_container_add (GTK_CONTAINER (frame), scale); gtk_container_add (GTK_CONTAINER (frame), scale);
gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Show/hide trough");
box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
0, 100, 1);
gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
toggle = gtk_toggle_button_new_with_label ("Show slider trough");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), TRUE);
g_signal_connect (G_OBJECT (toggle), "toggled",
G_CALLBACK (show_trough_toggled), scale);
gtk_box_pack_start (GTK_BOX (box2), toggle, TRUE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (frame), box2);
gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 0);
box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5); box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_pack_start (GTK_BOX (box), box2, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box), box2, TRUE, TRUE, 0);