GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Jul 28, 2014 4:15 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: GtkUIManager, Spin Button, Radio Button in one Callback ?
PostPosted: Sun May 05, 2013 5:40 pm 
Offline

Joined: Sun May 05, 2013 9:46 am
Posts: 1
Hello. I use GTK 3.6.4 on Fedora 18
Firstly I want you to know that I'm French. So sorry if I make some mistakes.
I'm creating a program in which I create A menu and a toolbar thanks to GtkUIManager.
I also create a spin button, a GtkRadioButton, and a single button (GTk_STOCK_OK)
in the main window.


What I want is when I click on my single button (OK button) I enter in a Callback function
which tells me what GtkRadioButton I checked, what number I enter in my Spin button AND
what agorithm I choose in my GtkRadioActionEntry created by using GtkUIManager.


I manage to get the infos from my spinButton and my RadioButton from my interface
but I don't manage to know what algorithm is checked from my menu in the same
callback
. I manage to know what algorithm is selected in a different callback but
I need to know this piece of information in the same callback which tells me the infos
about my spinButton,...

Note : I put the important part in bold.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <gtk/gtk.h>

/*
#include "heuristic.h"
#include "constantes.h"
#include "testsUnitaires.h" */

#define _PPV  0
#define _PPA  1
#define _IMC  2
#define _IPL  3

[b]// I may have to change the strucure ?
typedef struct
{
  GtkWidget *boutonP;
  GtkWidget *nbSommet;
}  mesArguments;[/b]


gboolean on_draw (GtkWidget *widget, cairo_t *cr, gpointer data);
void whatAlgo(GtkRadioAction *courant, gpointer param);
void onValider(GtkWidget *pBtn, gpointer data);

static void menuAjouterWidget (GtkUIManager *, GtkWidget *, GtkContainer *);


static void menuOuvrir (GtkWidget *pWidget, gpointer donneesUser);
static void ouvrirFichier (const gchar *file_name, GtkTextView *p_text_view);

static void menuQuitter (void);
static void menuAide (void);
static void menuAPropos (GtkWidget *pWidget, gpointer donneesUser);

void print_warning (char *format, ...);

/*
static void menuPPV (void);
static void menuPPA (void);
static void menuIMC (void);
static void menuIPL (void); */

/* Variable globale */
Graphe* graphe;
Liste* listeSommets = NULL;


int main (int argc, char* argv[])
{
  GtkWidget *pFenetre = NULL;
  GtkWidget *pBoite = NULL;
  GtkUIManager *puiManager = NULL;
  GtkActionGroup *pActionGroupe = NULL;
  GError *error;
  GtkWidget *menubar = NULL;
  GtkWidget *toolbar = NULL;

  /* Widget pour sauvegarde icones en GTK_STOCK */
  GtkWidget *image;
  GtkIconFactory *factory;
  GtkIconSource *source;
  GtkIconSet *set;
  GtkStockItem item;
 
  char chemin[256];
  /* Fin des Widget utiles pour les icones */

  GtkWidget * area;
  GtkWidget * table;

  /* Widget nb de sommets */
  GtkWidget * frame;
  //GtkWidget *nbSommet;

  /* Widget style de sommets */
  GtkWidget * styleSom;
  //GtkWidget * boutonP;
  GtkWidget * boutonD;
  GtkWidget * boxSom;

  /* Widget GO */
  GtkWidget * Go;

  /* Widget 2-opt */
  GtkWidget * deuxOpt;

  /* séparateurs */
  GtkWidget * separateur1;
  GtkWidget * separateur2;
  GtkWidget * separateur3; 

  /* bouton Reset */
  GtkWidget * reset;

  /* mesArguments */
  mesArguments mesArgs;

  /* Création du menu */
static  GtkActionEntry entries[] =
    {
      { "FichierMenuAction",        NULL,              "Fichier",                    NULL,           NULL,           NULL                        },
      { "OuvrirAction",             GTK_STOCK_OPEN,    "_Ouvrir",                    "<control>O",   "Ouvrir",       G_CALLBACK (menuOuvrir)     },
      { "EnregistrerAction",        GTK_STOCK_SAVE_AS,  "Enregistrer sous",          "<control>S",   "Enregistrer",  NULL /* G_CALLBACK (menuNouveau)*/    },
      { "QuitterAction",            GTK_STOCK_QUIT,    "_Quitter",                   "<control>Q",   "Quitter",      G_CALLBACK (menuQuitter)    },
      { "AlgorithmesMenuAction",    NULL,              "Algorithmes",                NULL,           NULL,           NULL              },
      { "AideMenuAction",           NULL,              "Aide",                       NULL,           NULL,           NULL                        },
      { "AideAction",               GTK_STOCK_HELP,    "Aide",                       "<release>F1",  "Aide",         G_CALLBACK(menuAide)        },
      { "AProposAction",            GTK_STOCK_ABOUT,   "_A propos",                  "<control>A" ,  "A Propos",     G_CALLBACK(menuAPropos)    }
    };

   
[b] static GtkRadioActionEntry radio_entries[] =
   {
     { "PlusProcheVoisinAction",  "TSP_PROCHE",            "Plus Proche Voisin",         NULL,           "Plus Proche Voisin",  _PPV},
     { "PlusProcheAdditionAction", "TSP_ADDITION",            "Plus Proche Addition",       NULL,          "Plus Proche Addition" , _PPA },
     { "InsertionMoindreCoutAction", "TSP_MOINDRE_COUT",            "Insertion Moindre Cout",     NULL,          "Insertion Moindre Cout" , _IMC },
     { "InsertionPlusLointaineAction", "TSP_PLUS_LOIN",            "Insertion Plus Lointaine",   NULL,           "Insertion Plus Lointaine", _IPL },
   };[/b]


  /* Initialisation de GTK+ */
  gtk_init(&argc, &argv);

  /* Création de la fenetre */
  pFenetre = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(pFenetre), GTK_WIN_POS_CENTER);
  gtk_window_set_title(GTK_WINDOW(pFenetre), "Traveling Salesman Problem");
  gtk_window_set_default_size(GTK_WINDOW(pFenetre), 800, 600);
  gtk_window_set_resizable (GTK_WINDOW(pFenetre), TRUE);
  gtk_window_iconify (GTK_WINDOW(pFenetre));
  gtk_window_maximize (GTK_WINDOW(pFenetre));
  g_signal_connect(G_OBJECT(pFenetre), "destroy", G_CALLBACK(gtk_main_quit), NULL);


  /* création d'une structure pour les images */
  static struct
  {
     gchar *nomFichier;
     gchar *stockId;
  } stockIcones[] =
      {
    { "plusProche.png", "TSP_PROCHE" },
    { "plusAddition.png", "TSP_ADDITION" },
    { "moindreCout.png", "TSP_MOINDRE_COUT" },
    { "plusLoin.png", "TSP_PLUS_LOIN" }
      };
  static gint nbrIcones = G_N_ELEMENTS (stockIcones);

  /* chargement des images */
  factory = gtk_icon_factory_new();
  int i;
  for (i = 0 ; i < nbrIcones ; i++)
    {
      set = gtk_icon_set_new();
      source = gtk_icon_source_new();
           
      sprintf(chemin, "%s/", getcwd(chemin, sizeof(chemin)));
      strcat(chemin, stockIcones[i].nomFichier);
      gtk_icon_source_set_filename( source, chemin);
      gtk_icon_set_add_source( set, source );
      gtk_icon_source_free (source);
      gtk_icon_factory_add( factory, stockIcones[i].stockId , set );
      gtk_icon_set_unref (set);
    }
   gtk_icon_factory_add_default ( factory );
   g_object_unref (factory);

   /* Création d'une grille de subdivision de la fenetre */
   table = gtk_grid_new ();
   gtk_grid_set_column_homogeneous(GTK_GRID(table), TRUE);
   gtk_grid_set_row_homogeneous(GTK_GRID(table), TRUE);
   gtk_container_add(GTK_CONTAINER(pFenetre), GTK_WIDGET(table));
   
  /* Creation d'une vBox */
  pBoite = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  //gtk_container_add (GTK_CONTAINER (table), pBoite);

  /* Creation du menu */
  pActionGroupe = gtk_action_group_new ("menuActionGroupe");
  gtk_action_group_add_actions (pActionGroupe, entries, G_N_ELEMENTS (entries), NULL);
[b]  gtk_action_group_add_radio_actions (pActionGroupe, radio_entries, G_N_ELEMENTS (radio_entries), -1, (GCallback)whatAlgo, NULL);         // I have to call onValider instead of whatAlgo
//because I want to know what algorithm is selected in onValider Callback and not in whatAlgo...[/b]

  puiManager = gtk_ui_manager_new ();
  gtk_ui_manager_insert_action_group (puiManager, pActionGroupe, 0);
  gtk_ui_manager_add_ui_from_file (puiManager, "menu.xml", NULL);
  g_signal_connect (puiManager, "add_widget", G_CALLBACK (menuAjouterWidget), pBoite);

  error = NULL;
  gtk_ui_manager_add_ui_from_file (puiManager, "menu.xml", &error);
   
  if (error)
    {
        g_message ("La construction du menu a merdé: %s", error->message);
        g_error_free (error);
    }

  /********************** DEBUT DE L'INTERFACE ********************/

  /* Widget nombre de sommet */
  frame = gtk_frame_new("Nombre de sommets");
  mesArgs.nbSommet = gtk_spin_button_new_with_range(0,5000,1);
  gtk_container_add(GTK_CONTAINER(frame), mesArgs.nbSommet);


  /* Widget style de points */
  styleSom = gtk_frame_new("Style des sommets");

  boxSom = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_container_add(GTK_CONTAINER(styleSom), boxSom);

  mesArgs.boutonP = gtk_radio_button_new_with_label(NULL, "Points");
  gtk_box_pack_start(GTK_BOX(boxSom), mesArgs.boutonP, TRUE, FALSE, 0);
  boutonD = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(mesArgs.boutonP), "Disques");
  gtk_box_pack_start(GTK_BOX(boxSom), boutonD, TRUE, FALSE, 0);

  /* Bouton pour valider */
  Go = gtk_button_new_from_stock(GTK_STOCK_OK);

  /* Séparateur */
  separateur1 =  gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
  separateur2 =  gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
  separateur3 =  gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);

  /* 2-opt bouton */
  deuxOpt = gtk_button_new_with_mnemonic("_2-opt");

  /* reset bouton */
  reset = gtk_button_new_with_mnemonic("_Reset");

  /* DrawingArea */
  area = gtk_drawing_area_new ();

  /* Mis en place des widgets dans la fenetre */
  gtk_grid_attach(GTK_GRID (table), pBoite, 0, 0 , 18 ,1 );
  gtk_grid_attach(GTK_GRID (table), frame, 0, 1 ,3 ,1 );
  gtk_grid_attach(GTK_GRID (table), area, 3, 1, 15, 10);
  gtk_grid_attach_next_to(GTK_GRID (table), styleSom, frame, GTK_POS_BOTTOM, 3, 1);
  gtk_grid_attach(GTK_GRID (table), separateur1, 0, 3, 3, 1);
  gtk_grid_attach(GTK_GRID (table), Go, 0, 4, 3, 1);
  gtk_grid_attach(GTK_GRID (table), separateur2, 0, 5, 3, 1);
  gtk_grid_attach(GTK_GRID (table), deuxOpt, 0, 6, 3, 1);
  gtk_grid_attach(GTK_GRID (table), separateur3, 0, 7, 3, 1);
  gtk_grid_attach(GTK_GRID (table), reset, 0, 8, 3, 1);

  /* bordure autour de la DrawingArea */
  g_object_set (frame, "margin-left", 20, NULL);
  g_object_set (styleSom, "margin-left", 20, NULL);
  g_object_set (separateur1, "margin-left", 20, NULL);
  g_object_set (separateur2, "margin-left", 20, NULL);
  g_object_set (separateur3, "margin-left", 20, NULL);
  g_object_set (Go, "margin-left", 20, NULL);
  g_object_set (deuxOpt, "margin-left", 20, NULL);
  g_object_set (reset, "margin-left", 20, NULL);
  g_object_set (area, "margin", 20, NULL);


  g_signal_connect (G_OBJECT (area), "draw", G_CALLBACK (on_draw), NULL);

  /************************ FIN DE L'INTERFACE *****************************/

  /********** L'appui sur valider détermine l'algo
         choisi et la représentation des sommets ********/
[b] g_signal_connect(G_OBJECT(Go), "clicked", G_CALLBACK(onValider), &mesArgs);[/b]

  /* Autorise l'utilisation des raccourcis. */
  gtk_window_add_accel_group (GTK_WINDOW (pFenetre), gtk_ui_manager_get_accel_group (puiManager));

  /* Boucle principale */
  gtk_widget_show_all(pFenetre);
  gtk_main();
  gtk_widget_destroy(pFenetre);

  return EXIT_SUCCESS;
}


gboolean on_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
{

  /* affiche dans la console les dimensions du widget */
  printf("width = %d, height = %d\n",
        gtk_widget_get_allocated_width(widget),
        gtk_widget_get_allocated_height(widget));


  cairo_set_source_rgb (cr, 0.0 , 0.0 , 0.0); //fond noir
  cairo_paint (cr);

  //cairo_destroy (cr);
  return FALSE;
}



static void menuAjouterWidget (GtkUIManager * puiManager, GtkWidget * pWidget, GtkContainer * pBoite)
{
   gtk_box_pack_start(GTK_BOX (pBoite), pWidget, FALSE, FALSE, 0);
   gtk_widget_show (pWidget);
   return;
}

/****************************************************************/
/****************** OUVRIR UN FICHIER ***************************/
/****************************************************************/
static void menuOuvrir (GtkWidget *pWidget, gpointer donneesUser)
{
  /* Création de la boite de dialogue */
  GtkWidget *pDialogue = NULL;
  pDialogue = gtk_file_chooser_dialog_new ("Ouvrir un fichier", NULL,
                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                          NULL);
  /* Si l'utilisateur souhaite ouvrir un fichier */
  if (gtk_dialog_run (GTK_DIALOG (pDialogue)) == GTK_RESPONSE_ACCEPT)
  {
    gchar *nomFichier = NULL;

    nomFichier = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (pDialogue));
    ouvrirFichier (nomFichier, GTK_TEXT_VIEW (donneesUser));
    g_free (nomFichier), nomFichier = NULL;
  }
  gtk_widget_destroy (pDialogue);

}

static void ouvrirFichier (const gchar *nomFichier, GtkTextView *pTextView)
{
  g_return_if_fail (nomFichier && pTextView);
  {
    gchar *contents = NULL;

    if (g_file_get_contents (nomFichier, &contents, NULL, NULL))
    {
      /* Copie de contents dans le GtkTextView */
      gchar *utf8 = NULL;
      GtkTextIter iter;
      GtkTextBuffer *pTextBuffer = NULL;

      pTextBuffer = gtk_text_view_get_buffer (pTextView);
      gtk_text_buffer_get_iter_at_line (pTextBuffer, &iter, 0);
      utf8 = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL);
      g_free (contents), contents = NULL;
      gtk_text_buffer_insert (pTextBuffer, &iter, utf8, -1);
      g_free (utf8), utf8 = NULL;
    }
    else
    {
       print_warning ("Impossible d'ouvrir le fichier %s\n", nomFichier);
    }
  }
}

void print_warning (char *format, ...)
{
  va_list va;

  va_start (va, format);
  fprintf (stderr, "Erreur : ");
  vfprintf (stderr, format, va);
  fprintf (stderr, "\n");
}


/****************************************************************/
/******************** QUITTER L'APPLI ***************************/
/****************************************************************/
static void menuQuitter (void)
{
   gtk_main_quit ();
   return;
}


/****************************************************************/
/************************** MENU AIDE ***************************/
/****************************************************************/
static void menuAide (void)
{
   GtkWidget *pDialogue = NULL;

   pDialogue = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Aide");
   gtk_dialog_run (GTK_DIALOG (pDialogue));
   gtk_widget_destroy (pDialogue);
   return;
}


/****************************************************************/
/************************ MENU A PROPOS *************************/
/****************************************************************/
static void menuAPropos (GtkWidget *pWidget, gpointer donneesUser)
{
  GtkWidget *AProposDialogue = NULL;

  AProposDialogue = gtk_about_dialog_new ();
  gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (AProposDialogue), "1.1");
  gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG (AProposDialogue), "Voyageur de Commerce");
 
  {
    const gchar *auteurs[5] = {"Victor BUSA", "Nawfal TACHFINE", "Benjamin NEMPONT", "Kevin JEGOUIC", NULL};

    gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (AProposDialogue), auteurs);
  }
  {
    gchar *contenu = NULL;

    if (g_file_get_contents ("COPYING", &contenu, NULL, NULL))
    {
      gchar *utf8 = NULL;

      utf8 = g_locale_to_utf8 (contenu, -1, NULL, NULL, NULL);
      g_free (contenu), contenu = NULL;
      gtk_about_dialog_set_license (GTK_ABOUT_DIALOG (AProposDialogue), utf8);
      g_free (utf8), utf8 = NULL;
    }
  }
  gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (AProposDialogue), "http://www.telecom-sudparis.eu/fr_accueil.html");
  {
    GdkPixbuf *logo = NULL;

    logo = gdk_pixbuf_new_from_file ("logo.png", NULL);
    gtk_about_dialog_set_logo (GTK_ABOUT_DIALOG (AProposDialogue), logo);
  }
  gtk_dialog_run (GTK_DIALOG (AProposDialogue));
  gtk_widget_destroy (AProposDialogue);
}

void whatAlgo(GtkRadioAction *courant, gpointer param)
{
  switch(gtk_radio_action_get_current_value (courant));
 
 
}


[b]void onValider(GtkWidget *pBtn, gpointer data)[/b]
{
    GtkWidget *pInfo;
    GtkWidget *pWindow;
    GSList *pList;
    const gchar *sLabel;

    mesArguments * mesArgs = (mesArguments *) data;

    /* Récupération de la liste des boutons */
    pList = gtk_radio_button_get_group(GTK_RADIO_BUTTON(mesArgs->boutonP));

    /* Parcours de la liste */
    while(pList)
    {
        /* Le bouton est-il sélectionné */
        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));
            /* 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);
        }
    }   

    /* On récupére la fenêtre principale */
    /*
    /* A partir d'un widget, gtk_widget_get_toplevel
    /* remonte jusqu'à la fenêtre mère qui nous est
    /* utile pour l'affichage de la boite de dialogue
    */
    pWindow = gtk_widget_get_toplevel(GTK_WIDGET(data));

    pInfo = gtk_message_dialog_new (GTK_WINDOW(pWindow),
        GTK_DIALOG_MODAL,
        GTK_MESSAGE_INFO,
        GTK_BUTTONS_OK,
                "Vous avez choisi : %s et %d", sLabel,
                gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(mesArgs->nbSommet))
                );

    gtk_dialog_run(GTK_DIALOG(pInfo));

    gtk_widget_destroy(pInfo);
}


Image :
Note : The algorithms are the four icone with green stroke and red points in the toolbar.

Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group