Hi. I have some problem with my
codes.
Because I want to use C++ in
GTK+, not gtk--. But I have a little
trouble.
I write one class and have three files. They're
bellow:
/* myFrame.cc */
#include <gtk/gtk.h> #include "main_window.h" void main( gint argc , char * argv[]
)
{
gtk_init( &argc , &argv ); main_window
window;
window.create_main_frame();
gtk_main(); }
/* main_window.h
*/
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H #include <gtk/gtk.h>
#include <math.h> class
main_window
{ public: main_window() {} ~main_window() {} void
test1();
void test2(); //callback
function;
void do_exit( GtkWidget * widget , GtkWidget * window ); void create_main_frame(); };
#endif
/*main_window.cc */
#ifndef MAIN_WINDOW_CC
#define MAIN_WINDOW_CC #include "main_window.h"
void main_window::test1() {}
void main_window::test2() {} void main_window::do_exit( GtkWidget * widget , GtkWidget *
window )
{ gtk_widget_destroy( window ); gtk_main_quit(); } void
main_window::create_main_frame()
{ void (main_window::*exit)( GtkWidget * widget , GtkWidget * window ) = &main_window::do_exit; struct
{ char * label; void (main_window::*func)(); } buttons[] = { {"TEST Button1" , &main_window::test1} , {"TEST Button2" , &main_window::test2 } , };
int no_buttons = sizeof(buttons)/ sizeof (buttons[0]); GtkWidget *
window;
GtkWidget * close_button; GtkWidget * button; GtkWidget * up_box; GtkWidget * down_box; window = gtk_window_new(
GTK_WINDOW_TOPLEVEL );
//set window's
size;
gtk_widget_set_usize( window , 200 , 250 ); gtk_widget_set_uposition( window , 10 , 10 ); //signal
process;
gtk_signal_connect( GTK_OBJECT(window) , "destroy" , GTK_SIGNAL_FUNC(gtk_main_quit) , NULL ); //create a new vertical box; up_box = gtk_vbox_new( FALSE , 0 ); gtk_container_add( GTK_CONTAINER(window) , up_box ); gtk_container_border_width( GTK_CONTAINER(up_box) , 10 ); for( int i=0 ; i < no_buttons ;
i++ )
{ //create a new button; button = gtk_toggle_button_new_with_label(buttons[i].label);
gtk_signal_connect( GTK_OBJECT(button) , "clicked" ,
GTK_SIGNAL_FUNC(&buttons[i].func) , NULL );
gtk_box_pack_start( GTK_BOX(up_box) , button , TRUE , TRUE , 0 ); } //create a new vertical
box;
down_box = gtk_vbox_new( FALSE , 10 ); gtk_container_border_width( GTK_CONTAINER(down_box) , 10 ); gtk_box_pack_start( GTK_BOX(up_box) ,down_box , FALSE , TRUE , 0 ); GtkWidget * separator =
gtk_hseparator_new();
gtk_box_pack_start( GTK_BOX(down_box) , separator , FALSE , TRUE , 3 ); //create a new button about close window; close_button = gtk_button_new_with_label( "Close" ); gtk_signal_connect( GTK_OBJECT(close_button) , "clicked" , GTK_SIGNAL_FUNC(&exit) , window ); gtk_box_pack_start( GTK_BOX(down_box) , close_button , TRUE , TRUE , 0 ); GTK_WIDGET_SET_FLAGS( close_button , GTK_CAN_DEFAULT ); gtk_widget_grab_default( close_button ); gtk_widget_show_all( window
);
}
#endif
And my makefile is like this.
/*makefile*/
myFrame: myFrame.cc main_window.o
g++ -O3 `gtk-config --cflags` myFrame.cc main_window.o -o myFrame `gtk-config --libs` main_window.o: main_window.cc main_window.h
g++ -O3 -c main_window.cc //........................... my codes can be compile(didn't have any error).
But, if executing the program, it'll crash and close. And tell me
this:
Illegal
instruction ( core dumped)
Please help me. Thanks a lot.
Cooper.
|