Re: "Linking" two adjustment widgets?



On Wed, Feb 02, 2005 at 08:23:12PM +0100, Daniel Haude wrote:
suppose I have a GtkHScrollbar and a GtkEntry both of which control the 
same numerical quantity. Whenever the user changes the position of the 
slider I want this change to be reflected in the GtkEntry and vice versa.

Obviously it won't do to connect the "changed" signals of each widget to 
callbacks that change the setting of the other widget because the other 
one in turn would emit a "changed" signal as well, creating an infinite 
loop.

Actually I find it strange that this isn't even in the FAQ, considering 
that many applications have such functionality.

IOW you can find the answer in their source code ;-)

In my case there may or may not be added complications because the entry 
setting and the slider setting don't have 1:1 correspondence but an 
exponential / loigarithmic one.

Block the signals.  If you have two adjustments, you can
define generic functions like

static void
update_log_slave(GtkAdjustment *adj, GtkAdjustment *slave)
{
    gulong id;

    id = g_signal_handler_find(slave,
                               G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
                               0, 0, 0, update_exp_slave, adj);
    g_signal_handler_block(slave, id);
    gtk_adjustment_set_value(slave, log(adj->value));
    g_signal_handler_unblock(slave, id);
}

static void
update_exp_slave(GtkAdjustment *adj, GtkAdjustment *slave)
{
    gulong id;

    id = g_signal_handler_find(slave,
                               G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
                               0, 0, 0, update_log_slave, adj);
    g_signal_handler_block(slave, id);
    gtk_adjustment_set_value(slave, exp(adj->value));
    g_signal_handler_unblock(slave, id);
}

and connect one to one adjustment's "value_changed" with the
other adjustment as data, and vice versa.  Or you can define
specialized functions, remember handler id's so that they
don't have to be looked up each time, abstract it in various
ways, or whatever... the idea remains the same.

Yeti


--
Dynamic IP address is not a crime.



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