|
Ok, Im willing to bet im having this issue due to my inexperience with the C language, however after quite alot of playing around, my attempt to completely modulate my code has come to a stop with instance errors.
My problem is that if I create a new GtkWidget *main_window, and instantiate it in the same method as I call gtk_main() from everything works great, I can pass the pointer to another function as a callback or just another function from another class whenever I want and make changes to it. However, if I instantiate, or to be more specific call main_window = gtk_window_new() from inside of another function I get runtime errors whenever I run my application. Here is an example of working / non working code to illustrate my issue.
WORKING::
//C Header File gtktools.h
struct gtktools{ void makeAndShowWindow(GtkWidget *window, GtkWidget *layout); };
//C Implementation File gtktools.cc
void gtktools::makeAndShowwindow(GtkWidget *window, GtkWidget *layout) { gtk_window_set_title(GTK_WINDOW(window), "MY WINDOW"); gtk_window_set_default_size(GTK_WINDOW(window), 500, 300); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_add(GTK_CONTAINER(window), layout); }
//C Application File gtkapp.cc
gtktools *gtool = new gtktools();
typedef struct { GtkWidget *main_win; GtkWidget *vbox; } WindowComponents;
int main(int argc, char *argv[]) { gtk_init(&argc, &argv); WindowComponents wcomps = g_slice_new(WindowComponents);
wcomps->main_win = gtk_window_new(); //These lines will cause errors if moved vbox = gtk_vbox_new(FALSE,0); //Into the makeAndShowwindow function gtool->makeAndShowWindow(wcomps->main_win, wcomps->vbox);
gtk_widget_show_all(wcomps->main_win); gtk_main(); delete gtool; return 0; }
The above works GREAT, however if I change one small thing it causes massive runtime errors, and I need to know why this is happening as I was under the impression that passing a pointer to an object allowed you to work directly with its location in memory, which is why my external function makeAndShowWindow can set the title of the window. However, if I call gtk_window_new inside of that function on the GtkWidget rather than before passing it, the errors happen.
THIS IS THE BROKEN VERSION, EVERYTHING IS THE SAME EXCEPT THE APP FILE A FEW LINES
gtkapp.cc
typedef struct { GtkWidget *main_win; GtkWidget *vbox; } WindowComponents;
int main(int argc, char *argv[]) { gtk_init(&argc,&argv); WindowComponents wcomps = g_slice_new(WindowComponents);
gtool->makeAndShowWindow(wcomps->main_win, wcomps->vbox); gtk_widget_show_all(wcomps->main_win); delete gtool; return 0; }
AND THEN IN gtktools.cc
void makeAndShowWindow(GtkWidget *window, GtkWidget *layout) { window = gtk_window_new(GTK_WINDOW_TOPLEVEL); layout = gtk_vbox_new(FALSE,0); /* ..set window attributes..*/ }
Everything compiles however about 7 runtime errors complaining of null instances and inability to cast to the GtkWidget type as there are null values involved
|