Bonjour à tous,

Pour un projet, j'ai besoin d'ajouter une petite GUI (une simple window) à partir d'un logiciel source existant.

Puisque gtk_main() est bloquant, j'ai décidé (et c'est surement là une partie de mon erreur), de mettre toutes les instructions GTK (sauf le init()), dans un thread.

Voici mon code :

main / programme source
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
....
gtk_init(&argc, &argv);
//To be implemented : detached thread    
if(pthread_create(&thread_window_gtk, NULL, create_window, (void *)NULL) != 0)
	perror("Error during GTK window thread creation\n!");
...
GTK Window thread
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
#include <stdlib.h>
#include <gtk/gtk.h>
#include <pthread.h>
 
#include "ui_gtk.h"
 
#define MAIN_WINDOW_WIDTH 600
#define MAIN_WINDOW_HEIGHT 90
 
void * create_window(void * data)
{
    GtkWidget * MainWindow = NULL;
    GtkWidget * Main_Vertical_Box = NULL;
    GtkWidget * first_Horizontal_Box = NULL;
    GtkWidget * second_Horizontal_Box = NULL;
    GtkWidget * OK_Button = NULL;
    GdkScreen * MainScreen= NULL;
 
    gchar * user_information = NULL;
    gint MainWindow_width = MAIN_WINDOW_WIDTH;    
    gint MainWindow_height = MAIN_WINDOW_HEIGHT;
    gint Screen_width, Screen_height;
    gint adjusted_main_window_x_position, adjusted_main_window_y_position;
 
    printf("******* GTK Window thread created ***********\n");
 
    //Main window creation
    MainWindow = gtk_window_new(GTK_WINDOW_POPUP);
    g_signal_connect(G_OBJECT(MainWindow), "delete-event", G_CALLBACK(gtk_main_quit), NULL);  
 
    //Main box containers creation & initialization
    Main_Vertical_Box = gtk_vbox_new(FALSE, 0);
    first_Horizontal_Box = gtk_hbox_new(FALSE, 0);
    second_Horizontal_Box = gtk_hbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(MainWindow), Main_Vertical_Box);
    gtk_box_pack_start(GTK_BOX(Main_Vertical_Box), first_Horizontal_Box, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(Main_Vertical_Box), second_Horizontal_Box, TRUE, FALSE, 5);
 
    //OK Button creation
    OK_Button = gtk_button_new_with_label("OK");
 
    //Window personalization
    gtk_window_set_default_size(GTK_WINDOW(MainWindow), MainWindow_width, MainWindow_height);
    gtk_window_set_position(GTK_WINDOW(MainWindow), GTK_WIN_POS_CENTER);
 
    //Retrieve screen resolution and adjust popup position (NOTE : Gravity center is upper left corner of windows by default)    
    MainScreen = gtk_window_get_screen(GTK_WINDOW(MainWindow)); 
    Screen_width = gdk_screen_get_width(MainScreen);
    Screen_height = gdk_screen_get_height(MainScreen);
    printf("Main screen resolution is %d*%d \n", Screen_width, Screen_height);
    adjusted_main_window_x_position = (Screen_width/2) - (MainWindow_width/2);
    adjusted_main_window_y_position = Screen_height - MainWindow_height - 10; 
    gtk_window_move(GTK_WINDOW(MainWindow), adjusted_main_window_x_position, adjusted_main_window_y_position);
 
      //Add widget
     ...
    ///
 
       //Add Button & connect "clicked" signal to quit UI interface
    gtk_box_pack_start(GTK_BOX(second_Horizontal_Box), OK_Button, TRUE, FALSE, 5);
    gtk_widget_set_size_request(OK_Button, 70, 30);
    g_signal_connect(G_OBJECT(OK_Button), "clicked", G_CALLBACK(OnDestroy), MainWindow);
 
    //Show main window and loop
    gtk_widget_show_all(MainWindow);
    gtk_main();
 
    printf("Test \n");
 
    return NULL;
}
 
void OnDestroy(GtkWidget * pWidget, gpointer pData)
{
    gtk_widget_destroy(pData);
    gtk_main_quit();
}
Mon problème est que si je ne rajoute pas l'instruction :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
 gtk_widget_destroy(pData);
J'ai un comportement étrange puisque la fonction gtk_main retourne bien (le printf s'affiche) mais j'ai le droit à un message d'erreur système (ou plutot la fenêtre qui se colore en gris) me disant "Window not responding". L'application tourne néanmoins toujours en fond.

Est-ce le fait d'utiliser GTK dans un thread qui ne lui convient pas à la terminaison ?
Ou alors un problème dans mon code ?

Dans tous les cas, pourquoi rajouter le gtk_widget_destroy semble (du moins en surface) résoudre mon problème ?

Merci d'avance,

CRC