more questions on struct, variables and objects



so I have

typedef struct _FooApp
{
  gchar *app_name;

  GtkWidget *main_window;
  .
  .
} FooApp;

which I use like this:

FooApp *
create_app (void)
{
  FooApp *app;

  app = g_new0 (FooApp, 1);

  app->app_name = g_strdup ("My Foo App");
  app->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  .....
}

now somewhere in my callbacks I want to create a messagebox:

void game_over(void *data)
{

    FooApp *app = (FooApp*)data;
    GtkWidget *msgBox;
    
    gchar* message;
    message = g_strdup_printf("GAME OVER") ;

    msgBox = gtk_message_dialog_new(GTK_WINDOW(app->main_window),
GTK_DIALOG_DESTROY_WITH_PARENT,
    GTK_MESSAGE_WARNING,GTK_BUTTONS_CLOSE,message);

g_signal_connect_swapped(msgBox,"response",G_CALLBACK(destroy_game_over_cb),msgBox);
    
    gtk_widget_show_all(msgBox);
    g_free (message);
}


but calling this messagebox I also want to modify some elements of app.
so should I now create a second struct like

typedef struct _FooApp2
{
  GtkWidget *msgBox;
  FooApp *app;
} FooApp2;

and then pass the new struct to g_signal_connect?

void game_over(void *data)
{

    FooApp *app = (FooApp*)data;
    FooApp2 *app2;

    app2 = g_new0 (FooApp2, 1);
    app2->app=app;

    gchar* message;
    message = g_strdup_printf("GAME OVER") ;

    app2->msgBox = gtk_message_dialog_new(GTK_WINDOW(app->main_window),
GTK_DIALOG_DESTROY_WITH_PARENT,
    GTK_MESSAGE_WARNING,GTK_BUTTONS_CLOSE,message);


g_signal_connect_swapped(app2->msgBox,"response",G_CALLBACK(destroy_game_over_cb),app2);
    
    gtk_widget_show_all(app2->msgBox);
    g_free (message);
}


or is there a better way to do this?



BTW, is there any difference in memory usage if I use

    gchar* message;
    message = g_strdup_printf("GAME OVER") ;

    msgBox = gtk_message_dialog_new(GTK_WINDOW(app->main_window),
GTK_DIALOG_DESTROY_WITH_PARENT,
    GTK_MESSAGE_WARNING,GTK_BUTTONS_CLOSE,message);
    g_free (message);


or 


    msgBox = gtk_message_dialog_new(GTK_WINDOW(app->main_window),
GTK_DIALOG_DESTROY_WITH_PARENT,
    GTK_MESSAGE_WARNING,GTK_BUTTONS_CLOSE,"Game OVER");

?


cheers,
Andreas




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]