Initiation C++/GTK

From Tuxunix
Jump to: navigation, search

Exemple d'exécution


$ export DISPLAY=:0
$ ./mediateckx

Mediateckx.jpg

  • mediateckx.cpp :
   1./*
   2. * mediateckx.cpp for (self)
   3. *
   4. * Made by tuxedo
   5. *
   6. * Started on Sun 16 Nov 2008 17:22:03 tuxedo
   7. * Last update Tue 18 Nov 2008 22:10:59 tuxedo
   8.*/
   9.       
  10.#include <iostream>
  11.#include <string>
  12.#include "mediateckx.h"
  13.#include <gtk/gtk.h>
  14.       
  15.using std::cerr;
  16.using std::cin;
  17.using std::cout;
  18.using std::endl;
  19.using std::string;
  20.       
  21.void destroyWindow(GtkWidget* widget, gpointer data){
  22.       
  23.    GtkWidget *pQuestion;
  24.    /* Creation de la boite de message */
  25.    pQuestion = gtk_message_dialog_new (GTK_WINDOW(data), GTK_DIALOG_MODAL,\
  26.    GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, \
  27.    "Voulez vous vraiment\nquitter ce programme?");
  28.       
  29.    /* Affichage et attente d une reponse */
  30.    switch(gtk_dialog_run(GTK_DIALOG(pQuestion)))
  31.    {
  32.        case GTK_RESPONSE_YES:
  33.            gtk_main_quit();
  34.            break;
  35.        case GTK_RESPONSE_NO:
  36.            gtk_widget_destroy(pQuestion);
  37.            break;
  38.    }
  39.       
  40.}/*destroyWindow*/
  41.       
  42.void Mediateckx::createWindow(GtkWidget *pWindow, gchar const *title){
  43.    gtk_window_set_title(GTK_WINDOW(pWindow), title);
  44.    gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
  45.    gtk_window_set_default_size(GTK_WINDOW(pWindow), 620, 400);
  46.}/*createWindow*/
  47.       
  48.void Mediateckx::showWindow(GtkWidget *pWindow){
  49.    gtk_signal_connect(GTK_OBJECT(pWindow), "destroy", \
  50.      (GtkSignalFunc)destroyWindow, NULL);
  51.      gtk_widget_show_all(pWindow);
  52.    gtk_main();
  53.}/*showWindow*/
  54.       
  55.void Mediateckx::addTable(GtkWidget *pWindow){
  56.    GtkWidget *pTable;
  57.    GtkWidget *pButton[2];
  58.    pTable = gtk_table_new(2, 2, TRUE);
  59.    gtk_container_add(GTK_CONTAINER(pWindow), GTK_WIDGET(pTable));
  60.    pButton[0]= gtk_button_new_with_label("Rechercher");
  61.    pButton[1]= gtk_button_new_with_label("Quitter");
  62.    g_signal_connect(G_OBJECT(pButton[1]), "clicked", \
  63.      G_CALLBACK(destroyWindow), NULL);
  64.    /* Insertion des boutons */
  65.    gtk_table_attach(GTK_TABLE(pTable), pButton[0], 0, 1, 0, 1, GTK_FILL, \
  66.      GTK_FILL, 0, 0);
  67.    gtk_table_attach(GTK_TABLE(pTable), pButton[1], 0, 5, 0, 2, GTK_FILL, \
  68.      GTK_FILL, 90, 10);
  69.    //gtk_table_attach_defaults(GTK_TABLE(pTable), pButton[1], 0, 1, 0, 2);
  70.}/*addTable*/
  71.       
  72.int main(int argc, char *argv[]){
  73.    GtkWidget *pWindow;
  74.      
  75.    gtk_init(&argc, &argv);
  76.    pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  77.    /*instance*/
  78.    Mediateckx mainWin;
  79.    mainWin.createWindow(pWindow, "Menu principale");
  80.    mainWin.addTable(pWindow);
  81.    //mainWin.submitQuit(pWindow);
  82.    mainWin.showWindow(pWindow);
  83.    return 0;
  84.} /*main*/


  • mediateckx.h :
   1./*
   2. * mediateckx.h for (self)
   3. *
   4. * Made by tuxedo
   5. *
   6. * Started on Sun 16 Nov 2008 17:22:08 tuxedo
   7. * Last update Tue 18 Nov 2008 01:26:32 tuxedo
   8.*/
   9.       
  10.#ifndef _MEDIATECKX_H
  11.# define _MEDIATECKX_H
  12.       
  13.#include <string>
  14.#include <gtk/gtk.h>
  15.       
  16.class Mediateckx
  17.{
  18.    private:
  19.        gchar const *title;
  20.    public:
  21.        void createWindow(GtkWidget *pWindow, gchar const *title);
  22.        void showWindow(GtkWidget *pWindow);
  23.        void addTable(GtkWidget *pWindow);
  24.};
  25.       
  26.#endif /* !_MEDIATECKX_H */