Why the Timer doesn't work? (about g_timeout_add())
- From: "Lazy Fox" <lazy fox wu gmail com>
- To: gtk-app-devel-list gnome org
- Cc: gtk-list gnome org
- Subject: Why the Timer doesn't work? (about g_timeout_add())
- Date: Wed, 20 Aug 2008 15:48:49 +0800
I copy my source files "CurveWindow.h" and "testCurveWindow.cpp" below.
And attach them in this mail. You can compile them by:
$ g++ testCurveWindow.cpp -o testCurveWindow `pkg-config --cflags
--libs gtk+-2.0`
In this application, I create an 1 second timer to draw a moving line.
But in the function timer_1_test(), the timer is run only once, and
then stopped.
See the comment in this function.
The following is my two source files:
/***************************************************************************
* Filename : CurveWindow.h
* Begin : 2008-08-14 08:46:43
* Project : CurveWindow
* Author : Wu Yin
***************************************************************************/
#include <iostream>
#include <gtk/gtk.h>
using namespace std;
class CurveWindow
{
public:
CurveWindow(){};
GtkWidget *create_drawing_win();
GtkWidget *get_root_widget();
int draw();
GdkPixmap *pixmap;
private:
GtkWidget *drawing;
};
//========================================================
/* Create a new backing pixmap of the appropriate size */
static gboolean configure_event( GtkWidget *widget, GdkEvent *event,
gpointer data)
{
CurveWindow *self = (CurveWindow *)data;
GdkColor color;
GdkGC *gc = gdk_gc_new(widget->window);
self->pixmap = gdk_pixmap_new(widget->window,
widget->allocation.width,
widget->allocation.height,
-1);
// Draw the background
gdk_draw_rectangle (self->pixmap,
widget->style->white_gc,
TRUE,
0, 0,
widget->allocation.width,
widget->allocation.height);
return TRUE;
}
/***************************************************************************
* add by Wu Yin, 2008-08-14 08:55:09
***************************************************************************/
static gboolean redraw(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
printf("expose-event \n");
CurveWindow *self = (CurveWindow *)data;
gdk_draw_drawable (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
self->pixmap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
return TRUE;
}
/***************************************************************************
* add by Wu Yin, 2008-08-19
***************************************************************************/
int CurveWindow::draw()
{
GtkWidget *widget = this->get_root_widget();
static int y = 0;
y += 10;
gdk_draw_line(pixmap,
widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
0, 0, 120, y);
gtk_widget_draw(widget, NULL);
return 0;
}
/***************************************************************************
* add by Wu Yin, 2008-08-14 08:55:09
***************************************************************************/
GtkWidget *CurveWindow::get_root_widget()
{
return drawing;
}
/***************************************************************************
* add by Wu Yin, 2008-08-14 08:55:09
***************************************************************************/
GtkWidget *CurveWindow::create_drawing_win()
{
drawing = gtk_drawing_area_new();
g_signal_connect(G_OBJECT(drawing), "expose-event",
G_CALLBACK(redraw), this);
g_signal_connect (G_OBJECT (drawing),"configure_event",
G_CALLBACK (configure_event), this);
return drawing;
}
/***************************************************************************
* Filename : testCurveWindow.cpp
* Begin : 2008-08-14 08:46:43
* Project : CurveWindow
* Author : Wu Yin
***************************************************************************/
#include <gtk/gtk.h>
#include <iostream>
#include "CurveWindow.h"
using namespace std;
/***************************************************************************
* add by Wu Yin, 2008-08-14 08:55:09
***************************************************************************/
static void timer_1_test(gpointer data)
{
CurveWindow *cw = (CurveWindow *)data;
cw->draw();
/* !!! Look Here !!!
* Uncomment each statement of the following, the timer will run
correctly, WHY?
*/
// printf("timer_1_test() \n");
// gtk_widget_draw(cw->get_root_widget(), NULL);
}
/***************************************************************************
* add by Wu Yin, 2008-08-14 08:55:09
***************************************************************************/
static void create_gui()
{
GtkWidget *drawing_win;
GtkWidget *root_win;
GtkWidget *root_table;
CurveWindow *cw;
GdkColor bg, fg;
// Create root_window
root_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(root_win), 800, 600);
g_signal_connect(GTK_OBJECT(root_win), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
// Create root table container
root_table = gtk_table_new(10, 10, TRUE);
gtk_container_add(GTK_CONTAINER(root_win), root_table);
// Create Drawing place 1, and add to the root table
cw = new CurveWindow();
drawing_win = cw->create_drawing_win();
g_timeout_add(1000, (GSourceFunc)timer_1_test, cw);
gtk_table_attach_defaults(GTK_TABLE(root_table), drawing_win, 0, 2, 0, 2);
gtk_widget_show_all(root_win);
}
/***************************************************************************
* add by Wu Yin, 2008-08-14 08:55:09
***************************************************************************/
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
create_gui();
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]