I'm trying to develope my application with Pike and GTK, but when I used GTK.DrawingArea to draw a line, it doesn't work.
I'm not sure if I misused GTK.DrawingArea, because I can't find example program that also use DrawingArea
Error message and my code listed below, any suggestion?
It says: Gdk-CRITICAL **: file gdkgc.c: line 51 (gdk_gc_new_with_values): assertion `window != NULL' failed.
Gdk-CRITICAL **: file gdkgc.c: line 456 (gdk_gc_set_foreground): assertion `gc != NULL' failed.
Gdk-CRITICAL **: file gdkdraw.c: line 65 (gdk_draw_line): assertion `drawable != NULL' failed.
Here is my code: #!/usr/bin/pike int main () { GTK.setup_gtk();
GTK.Window MainWin = 0;
GTK.Widget w = GTK.DrawingArea()->set_usize(100,100); GDK.GC gc = GDK.GC(w)->set_foreground( GDK.Color(255,0,0) ); w->draw_line ( gc , 10 , 10 , 100 , 100 );
MainWin = GTK.Window ( GTK.WINDOW_TOPLEVEL ); MainWin->set_title ( "Testing" );
MainWin->add ( w ); MainWin->show_all();
GTK.main(); return 0; }
A DrawingArea has no backing store, so you need to draw the line whenever the widget is exposed (for example when the window is first opened). So you need to connect a redraw function to the "expose_event" signal.
Also note that you can't create a GC from a widget before it's realized, so you need to defer that call. This code works, for example:
#!/usr/bin/pike
int main () { GTK.setup_gtk();
GTK.Window MainWin = 0;
GTK.Widget w = GTK.DrawingArea()->set_usize(100,100); GDK.GC gc; w->signal_connect("expose_event", lambda() { if(!gc) gc = GDK.GC(w)->set_foreground( GDK.Color(255,0,0) ); w->draw_line ( gc , 10 , 10 , 100 , 100 ); });
MainWin = GTK.Window ( GTK.WINDOW_TOPLEVEL ); MainWin->set_title ( "Testing" );
MainWin->add ( w ); MainWin->show_all();
GTK.main(); return 0; }
pike-devel@lists.lysator.liu.se