Re: How to create scales for range widgets?



On Tue, 22 Aug 2000 20:17:58 -0400, "David J. Topper" <topper@virginia.edu> wrote:
> Vlad Harchev wrote:
> 
> >  Could you post your stripped-down source (probably with main function) to
> > help us helping you?
> 
> It's a bit hard to strip down but I'll try.  The following compiles with:
> 
> gcc -g slider_stuff.c `gtk-config --cflags` `gtk-config --libs`
> 
> Just start moving the sliders and you'll see what I mean about scales,
> values, and special type scales.
> 
> ---- slider_stuff.c -----
[snip]


You can't simply place a table next to the scale an match things up.
You have to use a table that's the same size as the area covered by
the slider, not the entire size of the vscale. 
i.e., you where trying to map

this   with respect to this
  /        /------------^
 \/       \/
table   vscale
+-+      +-+
| |      | |
| |      | |
| |      | |
| |      | |
| |      | |
| |      | |              
| |      | |
| |      | |
| |      | |
| |      | |
| |      | |
| |      | |
+-+      +-+

but it should be more like this:

table             vscale
                   +-+
                   | |
+-+       ---      | |
| |        |       | |
| |        |       | |
| |  area covered  | |
| |  by the slider | |
| |        |       | |
| |        |       | |
| |        |       | |
| |        |       | |
+-+       ---      | |
                   | |
                   +-+


Here's an example. It uses a max_range of 15 (the vscale goes from 1 to
15 with marks at 1.0, 2.0, 3.0, etc....), but you can change it
from the command line, in order to see if everything is lining up
correctly:  
bash$ ./a.out <max_range>

#include <gtk/gtk.h>
#include <stdlib.h>

static void adjustment_value_changed_cb (GtkAdjustment *adj, gpointer data)
{
        g_print ("adj->value == %f\n", adj->value);
}

static void add_ypadding (GtkWidget *box, guint size)
{
        GtkWidget *fixed;

        fixed = gtk_fixed_new ();
        gtk_widget_set_usize (fixed, 10, size);
        gtk_box_pack_start (GTK_BOX (box), fixed, FALSE, FALSE, 0);
        gtk_widget_show (fixed);
}

gchar *rcstring = "
style 'green' {
        bg[NORMAL] = { 0.0, 0.6, 0.0 }
}

widget '*green_label' style 'green'
";


int main (int argc, char *argv[])
{
        GtkWidget *win, *hbox, *vscale, *label_vbox;
        GtkObject *adjustment;
        guint max_range;
        GtkWidget *table;
        GtkWidget *fixed, *label;
        guint i;
        guint ypadding, yoffset;
        gint slider_length, vscale_ythickness, stepper_slider_spacing;
        gchar *s;

        gtk_init (&argc, &argv);
        gtk_rc_parse_string (rcstring);

        if (argc < 2) 
                max_range = 15;
        else 
                max_range = atoi (argv[1]);

        win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_signal_connect (GTK_OBJECT (win), "destroy",
                        GTK_SIGNAL_FUNC (gtk_main_quit), NULL);

        hbox = gtk_hbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (win), hbox);

        label = gtk_label_new ("test\nvscale");
        gtk_widget_set_usize (label, 40, 400);
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

        adjustment = gtk_adjustment_new (0.0, 0.0, (gfloat) max_range, 
                                        0.1, 1.0, 0.0);
        gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
                        GTK_SIGNAL_FUNC (adjustment_value_changed_cb), NULL);
        vscale = gtk_vscale_new (GTK_ADJUSTMENT (adjustment));
        gtk_scale_set_draw_value (GTK_SCALE (vscale), FALSE);
        gtk_scale_set_digits (GTK_SCALE (vscale), 1);
        gtk_box_pack_start (GTK_BOX (hbox), vscale, TRUE, TRUE, 0);

#define GET_CLASS(widget) GTK_OBJECT(widget)->klass
        slider_length = GTK_SCALE_CLASS(GET_CLASS(vscale))->slider_length;
        vscale_ythickness = GTK_SCALE_CLASS(GET_CLASS(vscale))->value_spacing;
        stepper_slider_spacing =
 		GTK_RANGE_CLASS(GET_CLASS(vscale))->stepper_slider_spacing;
        /*FIXME is this right??? */
        ypadding = slider_length + vscale_ythickness * 2 +
		stepper_slider_spacing * 2;

        g_print ("slength:%d  ythick:%d  stepslspac:%d\n",
                slider_length, vscale_ythickness, stepper_slider_spacing);

        label_vbox = gtk_vbox_new (FALSE, 0);
        gtk_box_pack_start (GTK_BOX (hbox), label_vbox, FALSE, FALSE, 0);
        /* Add padding so that that the labels with the numbers line up
           with the center mark of the slider.  I guess if you wanted to
           be "correct" you would base the offset amount on the height
           of whatever font is currently being used.  To make it easy I
           just assume that the default gtk font is being used and use '10'
           as the offset amount.
        */
        yoffset = 10;
        add_ypadding (label_vbox, yoffset);

        table = gtk_table_new (1, max_range, FALSE);

        gtk_box_pack_start (GTK_BOX (label_vbox), table, TRUE, TRUE, 0);

        for (i = 0;  i < max_range ;  i++) {
                fixed = gtk_fixed_new ();
                /* Just colorizing it so that I can see the size of the area
                   which is covered by the vscale slider.
                */
                gtk_widget_set_name (fixed, "green_label");
                s = g_strdup_printf ("%2.2f", (gfloat) i);
                label = gtk_label_new (s);
                g_free (s);
                gtk_fixed_put (GTK_FIXED (fixed), label, 0, 0);
                gtk_table_attach_defaults (GTK_TABLE (table), fixed, 0, 1,
                                        i, i + 1);
        }

        /* The last index marker.  I didn't add it in the above loop
           because it's not really part of the space covered by the
           vscale slider, it's right at the end.  It's also used to
           complete all of the ypadding.
        */
        fixed = gtk_fixed_new ();
        s = g_strdup_printf ("%2.2f", (gfloat) i);
        label = gtk_label_new (s);
        g_free (s);
        gtk_fixed_put (GTK_FIXED (fixed), label, 0, 0);
        gtk_widget_set_usize (fixed, 10, ypadding - yoffset);
        gtk_box_pack_start (GTK_BOX (label_vbox), fixed, FALSE, FALSE, 0);

        gtk_widget_show_all (win);
	/* just so you can move slider with keyboard right away */
        gtk_widget_grab_focus (vscale); 
        gtk_main ();
        return 0;
}
        






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]