I’m making a C Application using GtkApplication, trying to use standard way to do so, but randomly my application doesn’t show and needs to be killed, then immediately of after several kills shows and runs ok.
It’s a simple project with only one c file, main.c
int main (int argc, char ** argv) {
GtkApplication * app;
donneesDep * monDepliage;
int status;
monDepliage = g_new (donneesDep, 1);
donneesDepliageInit (monDepliage);
app = gtk_application_new (APPLICATION_ID, G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (initUI), monDepliage);
status = g_application_run (G_APPLICATION (app), argc, argv);
donneesDepliageFree (monDepliage);
g_free (monDepliage);
g_object_unref (app);
return status;
}
a function to respond to app.activate signal and where I create the application window then populate it with UI elements, setup callbacks nd finally show the window. It’s from that point where from time to time the app doesn’t work.
I added an explicite gtk_window_destroy before the g_application_quit on the callback function called by the menu quit option.
Any idea is welcome, I tried a gdb session, but I am not experienced enough with it.
I tried to free any resource that I used. What is odd is that even if I don’t do anything sometimes after I quit, next run hangs instead of showing the window. Maybe is this behaviour linked to the way I setup my UI, here it is
// Initialise l'interface utilisateur et l'active
static void initUI (GApplication * app, gpointer data) {
donneesDep * donnees = (donneesDep *)data;
GtkWidget * header, * button, * box;
lmenu lm[] = {
{0, "zout", "Dézoomer", "win.zout", actionZoomOut},
{0, "znor", "Zoom 100%", "win.znor", actionZoomNormal},
{0, "zin", "Zoomer", "win.zin", actionZoomIn},
{1, "new", "Nouveau dépliage", "win.new", actionNouveau},
{1, "open", "Ouvrir dépliage", "win.open", actionOuvre},
{2, "save", "Sauver", "win.save", actionSauve},
{3, "expg", "Exporter gabarit", "win.expg", actionExporteGabarit},
{4, "help", "Aide", "app.help", actionAide},
{4, "quit", "Quitter", "app.quit", actionQuitte}
};
win = gtk_application_window_new (GTK_APPLICATION(app));
gtk_window_set_title (GTK_WINDOW (win), "Deplieur Base");
gtk_window_set_default_size (GTK_WINDOW (win), 600, 400);
//g_signal_connect (win, "destroy", G_CALLBACK (close_window), NULL);
header = gtk_header_bar_new ();
button = gtk_menu_button_new ();
gtk_menu_button_set_icon_name (GTK_MENU_BUTTON (button), "open-menu-symbolic");
gtk_widget_set_tooltip_text (button, "Menu");
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), G_MENU_MODEL (creeMenu(app, win, lm, 9, donnees)));
gtk_header_bar_pack_end (GTK_HEADER_BAR (header), button);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_add_css_class (box, "linked");
button = gtk_button_new_from_icon_name ("document-new");
gtk_box_append (GTK_BOX (box), button);
gtk_header_bar_pack_start (GTK_HEADER_BAR (header), box);
gtk_window_set_titlebar (GTK_WINDOW (win), header);
GtkWidget * cadre = gtk_scrolled_window_new();
rendu = gtk_drawing_area_new();
gtk_widget_set_cursor(rendu, gdk_cursor_new_from_name("crosshair", NULL));
gtk_drawing_area_set_content_width(GTK_DRAWING_AREA(rendu), (int)formats[donnees->formatPage].x);
gtk_drawing_area_set_content_height(GTK_DRAWING_AREA(rendu), (int)formats[donnees->formatPage].y);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(cadre), rendu);
gtk_widget_set_vexpand(cadre, TRUE);
gtk_widget_set_hexpand(cadre, TRUE);
donnees->surface = cairo_pdf_surface_create("work.pdf",
formats[donnees->formatPage].x, formats[donnees->formatPage].y);
gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(rendu), dessinePage, donnees, NULL);
GtkEventController * hover = gtk_event_controller_motion_new ();
gtk_widget_add_controller (rendu, hover);
g_signal_connect (hover, "motion", G_CALLBACK (gereHover), donnees );
GtkWidget *bv = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_window_set_child(GTK_WINDOW(win), bv);
gtk_box_append(GTK_BOX(bv), cadre);
statut = gtk_statusbar_new();
gtk_box_append (GTK_BOX (bv), statut);
g_print("status bar OK\n");
gtk_widget_show (win); // active l'application
g_print("apres show win\n");
}