Focus vs Sensitivity



/*
    Given 3 spin buttons, I want the second one to be enabled or disabled
based on input from the
first one.
    If in the first one the user puts 0 as value I want that when it
presses Tab the focus go to the
third spin button.
    If in the first one the user puts a value different than zero I want
that when it presses Tab
the focus go to the seconds spin button.
    What happens is that the Tab handler runs before the value-changed
handler so when the
value-changed handler sets the sensitivity of the second spin button Gtk+
complains of errors
internally or passes the focus to the third spin button instead of the
second.
    I imagine I could solve it by setting the focus by hand in my
value-changed handler but I am
looking for an easier way because I have a large application with lots of
these interactions and
also I don't know if that solves the Gtk+ error.
    I would love that Gtk+ emitted the value-changed signal before it
handled the Tab key...
    If you know another way to solve the problem then please email
ibaldo adinet com uy, thanks!!!
    Based on
http://developer.gnome.org/gnome-devel-demos/unstable/spinbutton.c.html.
    Compile with:
gcc focus_problem.c `pkg-config --cflags --libs gtk+-3.0` -o focus_problem
*/
#include <gtk/gtk.h>


static void spin_value_changed (GtkSpinButton *spin_button, gpointer
user_data)
{
    fprintf (stderr, "%s: before set_sensitive\n", __func__);
    GtkWidget * const spin_button2 = user_data;
    const gint value = gtk_spin_button_get_value_as_int (spin_button);
    gtk_widget_set_sensitive (spin_button2, value);
    fprintf (stderr, "%s: after set_sensitive\n", __func__);
}


static void activate (GtkApplication *app, gpointer user_data)
{
    GtkWidget * const window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Focus Problem");
    //gtk_window_set_default_size (GTK_WINDOW (window), 210, 70);
    gtk_container_set_border_width (GTK_CONTAINER (window), 5);

    GtkWidget * const label = gtk_label_new (
        "Enter 1 to enable middle spinbutton, 0 to disable it:");

    GtkAdjustment * const adjustment = gtk_adjustment_new (1, 0, 100, 1,
10, 0);
    GtkWidget * const spin_button = gtk_spin_button_new (adjustment, 0, 0);
    //gtk_widget_set_hexpand (spin_button, TRUE);

    GtkAdjustment * const adjustment2 = gtk_adjustment_new (0, 0, 100, 1,
10, 0);
    GtkWidget * const spin_button2 = gtk_spin_button_new (adjustment2, 0,
0);

    GtkAdjustment * const adjustment3 = gtk_adjustment_new (0, 0, 100, 1,
10, 0);
    GtkWidget * const spin_button3 = gtk_spin_button_new (adjustment3, 0,
0);

    g_signal_connect (spin_button, "value-changed", G_CALLBACK
(spin_value_changed), spin_button2);

    /* Create a grid and arrange everything accordingly */
    GtkWidget * const grid = gtk_grid_new ();
    gtk_grid_set_column_spacing (GTK_GRID (grid), 5);
    //gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
    gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (grid), spin_button, 1, 0, 1, 1);
    gtk_grid_attach (GTK_GRID (grid), spin_button2, 1, 1, 1, 1);
    gtk_grid_attach (GTK_GRID (grid), spin_button3, 1, 2, 1, 1);

    gtk_container_add (GTK_CONTAINER (window), grid);

    gtk_widget_show_all (window);
}


int main (int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return status;
}


-- 
Iván Baldo



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