Re: measuring time between two buttonclicks
- From: Olexiy Avramchenko <olexiy irtech cn ua>
- To: Thomas Deschepper <thomas deschepper ecol sintjozefscollege be>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: measuring time between two buttonclicks
- Date: Fri, 09 Apr 2004 13:01:08 +0300
Thomas Deschepper wrote:
I'm expermimenting a bit with GTK and I wanted to measure the time
interval between two clicks on a button. I figured out that I had to use
a GTimer provided by GLib.
The first time the button is clicked, it should initialize and start the
timer. The second time however, the GTimer should be stopped and the
interval should be calculated with the appropriate function...
How can I construct my callback so that I can execute this piece of code
on the first click and that piece of code on the second click??
You can use static boolean flag in callback, like this:
c-source start
----------------------------------------------------------------------------------------------------
#include <gtk/gtk.h>
static void clicked(GtkWidget *button, GTimer *timer)
{
GtkWidget *label;
static gboolean first_click=FALSE;
if ( !first_click ) {
g_timer_start(timer);
gtk_label_set_text(GTK_LABEL(label), "Press button one more");
}
else {
gchar *s;
g_timer_stop(timer);
s = g_strdup_printf(
"Time between clicks: %.0fs %.0fms %.0fus",
g_timer_elapsed(timer, NULL),
g_timer_elapsed(timer, NULL)*1000,
g_timer_elapsed(timer, NULL)*1000*1000
);
gtk_label_set_text(GTK_LABEL(label), s);
g_free(s);
}
first_click = !first_click;
}
int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *button;
GTimer *timer;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(window), 4);
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), 0);
button = gtk_button_new_with_label("Press button");
gtk_container_add(GTK_CONTAINER(window), button);
timer = g_timer_new();
g_signal_connect(button, "clicked", G_CALLBACK(clicked), timer);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
c-source
end------------------------------------------------------------------------------------------------------
Olexiy
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]