Bonjour,

Je désirerai créer des onglets "à la Firefox", c'est-à-dire avoir la possibilité de fermer chaque onglet indépendamment même ceux non actifs avec un bouton de fermeture sur chacun d'eux.

J'ai réussi à coder sur un code test cette fonctionnalité mais j'obtiens un comportement étrange lors de la fermeture des onglets. Fermé dans l'ordre de 5 à 0, il n'y a aucun problème mais de 0 à 5 ou aléatoirement, l'onglet 3 ferme l'onglet 5, et etc...

D'où pourrait provenir ce problème? et comment pourrait-on le résoudre?

constante.h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
GList *list = NULL;
 
typedef struct _pages {
    gint page_num;
    GtkWidget *icon;
    GtkWidget *label;
    GtkWidget *onglet_box;
    GtkWidget *box;
    GtkWidget *button;
} t_pages;
main.c :
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
int main (int argc, char **argv)
{
    gint i = 0;
 
    gchar *string           = NULL;
    GtkWidget *win          = NULL;
    GtkWidget *notebook     = NULL;
 
    t_pages *nouvelle = (t_pages *)malloc(6 * sizeof(t_pages));
 
    /* Initialize GTK+ */
    gtk_init (&argc, &argv);
 
    /* Create the main window */
    win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(win), 320, 200);
    gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
    g_signal_connect (win, "destroy", gtk_main_quit, NULL);
 
    notebook = gtk_notebook_new();
    gtk_container_add(GTK_CONTAINER(win), notebook);
 
    /* Create 6 pages with close button */
    for(i = 0; i < 6; i++)
    {
        fprintf(stderr,"adresse p>>%p\n",&nouvelle[i]);
 
        nouvelle[i].page_num = i;
        string = g_strdup_printf("page %d", i);
        nouvelle[i].label       = gtk_label_new(string);
        nouvelle[i].box         = gtk_vbox_new(FALSE, 0);
        nouvelle[i].onglet_box  = gtk_hbox_new(FALSE, 0);
 
        gtk_box_pack_start (GTK_BOX(nouvelle[i].onglet_box), nouvelle[i].label, TRUE, TRUE, 0);
 
        nouvelle[i].button  = gtk_button_new ();
        nouvelle[i].icon    = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
        gtk_button_set_image (GTK_BUTTON(nouvelle[i].button), nouvelle[i].icon);
        gtk_button_set_relief (GTK_BUTTON(nouvelle[i].button), GTK_RELIEF_NONE);
 
        /* Associate button with the "num page" */
        g_object_set_data (G_OBJECT(nouvelle[i].button), "str", (gpointer)i);
 
        gtk_box_pack_start (GTK_BOX(nouvelle[i].onglet_box), nouvelle[i].button, TRUE, TRUE, 0);
 
        /* Add the structure _pages in Glist */
        list = g_list_append (list, &nouvelle[i]);
        g_free(string);
 
        gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nouvelle[i].box, nouvelle[i].onglet_box);
        g_signal_connect (G_OBJECT(nouvelle[i].button), "clicked", G_CALLBACK(DeleteOnglet), notebook);
 
        gtk_widget_show_all(nouvelle[i].onglet_box);
    }
 
    g_free(string);
 
    gtk_widget_show_all (win);
    gtk_main ();
 
    free(nouvelle);
 
    return EXIT_SUCCESS;
}
La fonction de suppression :
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
void DeleteOnglet (GtkWidget *widget, gpointer data)
{
    gint n      = 0;
    gint i      = 0;
    gint num    = 0;
    gint nb     = 0;
 
    gpointer p  = NULL;
    t_pages *element;
 
    n = gtk_notebook_get_n_pages(GTK_NOTEBOOK(data));
 
    while(n>0)
    {
        /* Recovers structure _pages in position i in the Glist *
         * then the num of the page associate                   */
        element = (t_pages *)g_list_nth_data(list, i);
        num = (gint)element->page_num;
 
        /* Recovers the num of clicked button */
        p   = g_object_get_data(G_OBJECT(widget), "str");
        nb  = (gint) p;
 
        fprintf(stderr,"nb button >> %d >> %d\n",nb,i);
        fprintf(stderr,"num onglet >> %d\n",num);
 
        if (num == nb)
        {
            gtk_notebook_remove_page (GTK_NOTEBOOK(data), num);
            list = g_list_remove(list, element);
            break;
        }
        i++;
    }
}
Merci d'avance de votre aide !