IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GTK+ avec C & C++ Discussion :

[GTK+] Plusieurs données pour un CALLBACK


Sujet :

GTK+ avec C & C++

  1. #1
    Membre habitué
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Par défaut [GTK+] Plusieurs données pour un CALLBACK
    Bonjour à tous,

    Je débute dans le programmation avec GTK+.

    J'ai créé un notebook où dans chaque onglet, il y a du texte + bouton radio.

    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
     
     
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <gtk/gtk.h>
     
     
    void OnButton(GtkWidget *pButton, gpointer data)
    {
        GtkWidget *pDialog;
        GtkWidget *pChild;
        gint iPageNum;
        const gchar *sLabel;
        const gchar *sTabLabel;
        gchar *sDialogText;
     
        const gchar *sRep;
        GSList *pList;
     
       /* Recuperation de la liste des boutons */
        pList = gtk_radio_button_get_group(GTK_RADIO_BUTTON(data));
     
     /* Parcours de la liste */
        while(pList)
        {
            /* Le bouton est il selectionne */
            if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pList->data)))
            {
                /* OUI -> on copie le label du bouton */
                sLabel = gtk_button_get_label(GTK_BUTTON(pList->data));
     
     
    	    /* reponse - oui ou non */
    	    if( strcmp("Oui",sLabel) == 0 )
    	      sRep = "Oui";
    	    else
    	      sRep = "Non";
     
     
                /* On met la liste a NULL pour sortir de la boucle */
                pList = NULL;
            }
            else
            {
                /* NON -> on passe au bouton suivant */
                pList = g_slist_next(pList);
            }
        }
     
     
        /* Recuperation de la page active */
        iPageNum = gtk_notebook_get_current_page(GTK_NOTEBOOK(data));
     
        /* Creation du label de la boite de dialogue */
        sDialogText = g_strdup_printf("C'est la page %d\n"
    				  "La réponse est \"%s\"\n",
    				  iPageNum,
    				  sRep);
     
        sDialogText = g_locale_to_utf8(sDialogText, strlen(sDialogText), NULL, NULL, NULL);
     
        pDialog = gtk_message_dialog_new (NULL,
            GTK_DIALOG_MODAL,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            sDialogText);
     
        gtk_dialog_run(GTK_DIALOG(pDialog));
     
        gtk_widget_destroy(pDialog);
     
        g_free(sDialogText);
    }
     
    int main(int argc, char **argv)
    {
        GtkWidget *pWindow;
        GtkWidget *pVBox;
        GtkWidget *pNotebook;
        GtkWidget *pButton;
        gint i;
     
        gtk_init(&argc,&argv);
     
        pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(pWindow), "Onglet");
        gtk_window_set_default_size(GTK_WINDOW(pWindow), 500, 400);
        g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
     
        pVBox = gtk_vbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(pWindow), pVBox);
     
     
        /* Creation du GtkNotebook */
        pNotebook = gtk_notebook_new();
        gtk_box_pack_start(GTK_BOX(pVBox), pNotebook, TRUE, TRUE, 0);
        /* Position des onglets : en bas */
        gtk_notebook_set_tab_pos(GTK_NOTEBOOK(pNotebook), GTK_POS_BOTTOM);
        /* Ajout des boutons de navigation */
        gtk_notebook_set_scrollable(GTK_NOTEBOOK(pNotebook), TRUE);
     
        for(i = 0 ; i < 2 ; ++i)
        {
            GtkWidget *pLabel;
            GtkWidget *pTabLabel;
            gchar *sTabLabel;   
     
     
    	GtkWidget *pRadio1;
    	GtkWidget *pRadio2;
    	GtkWidget *pValider;
    	GtkWidget *pVBox2;
    	gchar *texte;
     
     
            sTabLabel = g_strdup_printf("Page %d", i);
     
            /* Creation des differents GtkLabel */
            pTabLabel = gtk_label_new(sTabLabel);
     
     
    	if(i == 0){
    	/*  Creation de la GtkBox verticale  */
    	pVBox2 = gtk_vbox_new(TRUE, 0);
     
    	/* Question a poser */
    	texte="As tu bien mangé ?";
    	texte = g_locale_to_utf8(texte, strlen(texte), NULL, NULL, NULL);
    	pLabel = gtk_label_new(texte);
     
    	gtk_box_pack_start(GTK_BOX(pVBox2), pLabel, FALSE, FALSE, 0);
     
    	/* Creation du premier bouton radio */
    	pRadio1 = gtk_radio_button_new_with_label(NULL, "Non");
    	gtk_box_pack_start(GTK_BOX (pVBox2), pRadio1, FALSE, FALSE, 0);
    	/* Ajout du deuxieme */
    	pRadio2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON (pRadio1), "Oui");
    	gtk_box_pack_start(GTK_BOX (pVBox2), pRadio2, FALSE, FALSE, 0);
     
    	/* Bouton valider*/
    	pValider = gtk_button_new_from_stock(GTK_STOCK_OK);
    	gtk_box_pack_start(GTK_BOX (pVBox2), pValider, FALSE, FALSE, 0);
     
    	g_signal_connect(G_OBJECT(pValider), "clicked", G_CALLBACK(OnButton), pRadio1); // + DATA pNotebook
     
     
    	}
     
    	if(i == 1){
    	/*  Creation de la GtkBox verticale  */
    	pVBox2 = gtk_vbox_new(TRUE, 0);
     
    	/* Question a poser */
    	texte="Es tu en forme ?";
    	texte = g_locale_to_utf8(texte, strlen(texte), NULL, NULL, NULL);
    	pLabel = gtk_label_new(texte);
     
    	gtk_box_pack_start(GTK_BOX(pVBox2), pLabel, FALSE, FALSE, 0);
     
    	/* Creation du premier bouton radio */
    	pRadio1 = gtk_radio_button_new_with_label(NULL, "Non");
    	gtk_box_pack_start(GTK_BOX (pVBox2), pRadio1, FALSE, FALSE, 0);
    	/* Ajout du deuxieme */
    	pRadio2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON (pRadio1), "Oui");
    	gtk_box_pack_start(GTK_BOX (pVBox2), pRadio2, FALSE, FALSE, 0);
     
    	/* Bouton valider*/
    	pValider = gtk_button_new_from_stock(GTK_STOCK_OK);
    	gtk_box_pack_start(GTK_BOX (pVBox2), pValider, FALSE, FALSE, 0);
     
    	g_signal_connect(G_OBJECT(pValider), "clicked", G_CALLBACK(OnButton), pRadio1); // + DATA pNotebook
     
     
     
    	}
     
            /* Insertion de la page */
            gtk_notebook_append_page(GTK_NOTEBOOK(pNotebook), pVBox2, pTabLabel);
     
     
            g_free(sTabLabel);
     
        }
     
        /* Activation du menu popup */
        gtk_notebook_popup_enable(GTK_NOTEBOOK(pNotebook));
     
     
        gtk_widget_show_all(pWindow);
     
        gtk_main();
     
        return EXIT_SUCCESS;
    }


    Et ma question est au niveau des signaux. En fait, je voudrai les données de 2 widgets différents dans un même CALLBACK.
    Dans mon code ici, quand je clique sur le bouton OK, je souhaite la réponse de mon petit questionnaire + le numéro de page du notebook où j'ai validé la réponse.

    Possible ou pas !?

    Merci =)

  2. #2
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    Tu peux modifier ton post en utilisant la balise CODE plutôt que QUOTE stp ? Parce que là c'est difficile à lire.

  3. #3
    Membre habitué
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Par défaut
    Balise Code - Fait !

    Problème résolu avec une strucutre ... =)

  4. #4
    Membre confirmé
    Homme Profil pro
    Chercheur
    Inscrit en
    Mars 2009
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France

    Informations professionnelles :
    Activité : Chercheur

    Informations forums :
    Inscription : Mars 2009
    Messages : 61
    Par défaut Re
    Bonjour,
    en fait un callback ne prend qu'une seule option,
    donc pour passer 2 widgets il faut déclarer une structure
    de 2 widgdets et passer cette structure en argument.
    Un exemple ici avec des eniters::

    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
     
    ! Déclaration de structure avec 2 entiers
    typedef struct {
      int a;
      int b;
    } axisdata;
     
    void plotaxis (int * x, int * y)
    {
     
    }
     
    G_MODULE_EXPORT void show_axis (GtkToggleButton * axis, gpointer data)
    {
      axisdata * adata = (axisdata *)data;
     
      if (gtk_toggle_button_get_active (axis));
      {
         plotaxis(& adata -> a, & adata -> b);
      }
    }
     
    GtkWdiget * createbuttton(int * axis)
    {
      GtkWidget * showaxis
      axisdata dataxa;
     
      if (* axis == 1)
      {
        dataxa.a = 1;
        dataxa.b = 0;
      }
      else
      {
        dataxa.a = 0;
        dataxa.b = 1;
      }
     
      showaxis = gtk_check_button_new_with_label ("Show axis");
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(showaxis), 1);
     
      g_signal_connect ((gpointer)showaxis, "toggled", G_CALLBACK(set_axis), & dataxa);
     
      gtk_widget_show(showaxis);
      return(showaxis);
    }

Discussions similaires

  1. 1 DataGridView pour plusieurs données
    Par Jinroh77 dans le forum Windows Forms
    Réponses: 14
    Dernier message: 14/02/2007, 18h56
  2. Réponses: 2
    Dernier message: 21/10/2006, 17h27
  3. Plusieurs devices de données pour une seule base
    Par The Wretched dans le forum Sybase
    Réponses: 4
    Dernier message: 12/10/2006, 09h27
  4. Réponses: 12
    Dernier message: 30/03/2006, 20h55
  5. Réponses: 2
    Dernier message: 24/05/2005, 16h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo