Re: Displaying intermediate button images.



Rich Burridge wrote:
[...]
Can I ask a favour please? Could you adjust the reve_sleep() routine in the
attached small program to use a g_timeout_add() to do the pausing?

   The problem with your program is not with reve_sleep(), the problem
is that you have a reve_sleep() function at all, in GTK+ your functions
must "return", not hijack the main loop.

Here is a functional modified version of your program that returns
to the main loop, I hope this helps some people understand better
the nature of event based programming :)

Enjoy,
                               -Tristan

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


/* 1.5 second flickers */
#define FLICKER_MILLIES 1500

typedef struct {
    GtkWidget *button;
    gint       times;
} Flicker;

GtkWidget *window;

gboolean
flicker_timeout (Flicker *flicker)
{
    gchar *label = (flicker->times % 2) ? "Off" : "On";

    gtk_button_set_label(GTK_BUTTON (flicker->button), label);

    /* return false to cancel timeout
     * and free flicker struct as a consequence
     */
    if (flicker->times-- <= 0) {
        /* reenable button sensitivity */
        gtk_widget_set_sensitive (flicker->button, TRUE);
        return FALSE;
    }
    return TRUE;
}

void
on_click(GtkWidget *button, gpointer user_data)
{
    Flicker *flicker = (Flicker *)g_new (gint, 1);

    /* flicker 5 times */
    flicker->times  = 10;
    flicker->button = button;

    g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
                        FLICKER_MILLIES, (GSourceFunc)flicker_timeout,
                        flicker, g_free);

    /* disable button during flicker */
    gtk_widget_set_sensitive (button, FALSE);
}


void
create_window(void)
{
    GtkWidget *button;

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_mnemonic("Off");
    gtk_widget_show(button);
    gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect((gpointer) button, "clicked", G_CALLBACK(on_click), NULL);

    gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);
}


int
main(int argc, char *argv[])
{
    gtk_set_locale();
    gtk_init(&argc, &argv);
    create_window();
    gtk_widget_show(window);

    gtk_main();

    return(0);
}




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