Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: How to make a custom window dialog, in gtkmm.

  1. #1
    Join Date
    Nov 2007
    Beans
    21

    How to make a custom window dialog, in gtkmm.

    Hi, I do not understand how to make a dialog with gtkmm.
    I am only a beginner programmer.
    My exercise is this:
    I have a main window with some buttons. After clicking one of this, I would like to show a simple window dialog with 2 button (the classic "ok" button and "cancel" button).
    I have wrote this code:
    dialog.h
    Code:
    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include<gtkmm.h>
    
    class dialogo : public Gtk::Dialog
    {
       public:
          dialogo();
          virtual ~dialogo();
       protected:
          void show_dialog();
          Gtk::VButtonBox box;
          Gtk::Button ok,annulla;
          Gtk::HBox hbox;
    };         
    
    #endif
    dialog.cpp
    Code:
    #include"dialog.h"
    #include<gtkmm.h>
    
    dialogo::dialogo()
    : ok("OK"),
      annulla("ANNULLA")
    {
       set_title("primo dialgo");
          
       add(box);
          
       box.pack_start(ok);
       box.pack_start(annulla);
       
       show_all_children();
    }
    
    dialogo::~dialogo()
    {
    
    }
    
    void dialogo::show_dialog()
    {
       dialogo dialogo(*this, "questo è un esempio");
       //dialogo.set_secondary_test("prova testo secondario");
       dialogo.run();
    }
    main.cpp
    Code:
    #include "MainWindow.h"
    
    int main(int argc, char *argv[])
    {
       Gtk::Main kit(argc, argv);
       window win;
       Gtk::Main::run(win);  
       return 0;
    }
    Mainwindow.cpp
    Code:
    #include "MainWindow.h"
    #include "dialog.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    window::window()
    
    {
       //maximize(); //massimizza la finestra
       set_title("Finestra principale");
       set_resizable(false);
       set_border_width(13);
       set_size_request(250,250);
       set_position(Gtk::WIN_POS_CENTER); //avvia l'applicazione al centro dello schermo
       
       
       button.set_label("Ciao Mondo!");
       close.set_label("Close");
       summ.set_label("Somma");
       clear.set_label("Clear");
       dia.set_label("Dialog");
       entry.set_text("Clicca il pulsante!");
       label.set_text("Somma:");
       hbox.set_spacing(2);
       hbox1.set_spacing(2);
          
       button.signal_clicked().connect(sigc::mem_fun(*this,&window::click_button));
       close.signal_clicked().connect(sigc::mem_fun(*this, &window::close_button));
       summ.signal_clicked().connect(sigc::mem_fun(*this, &window::somma_button));
       clear.signal_clicked().connect(sigc::mem_fun(*this, &window::clear_button));
       dia.signal_clicked().connect(sigc::mem_fun(*this, &dialogo::show_dialog));
       
       hbox.pack_start(label);
       hbox.pack_start(entry3);
       hbox1.pack_start(close);
       hbox1.pack_start(dia);
       
       add(vbox);
       
       vbox.pack_start(button);
       vbox.pack_start(hbox1);
       vbox.pack_start(summ);
       vbox.pack_start(clear);
       vbox.pack_start(entry);
       vbox.pack_start(entry1);
       vbox.pack_start(entry2);
       vbox.pack_start(hbox);
       
       show_all();
    }
    
    window::~window()
    {
    }   
    
    void window::click_button()
    {
       stringa = button.get_label();
       if (stringa == "Ciao Mondo!")
       { 
          entry.set_text(stringa);
          button.set_label("Clicca ancora:)");
       }
       else
       {
          stringa = "Ciao Mondo!";   
          button.set_label(stringa);
          stringa = "Clicca ancora:)";
          entry.set_text(stringa);
       }     
    }
    
    void window::close_button()
    {
       hide(); //Chiude l'applicazione
    }
    
    void window::somma_button()
    {
       istringstream in, in2;//Uso sstream per convertire la stringa in numero 
       ostringstream out;//Uso sstream per convertire il numero in stringa
    
       stringa = entry1.get_text();//prendo la stringa iniziale
       in.str(stringa);//la inserisco nella variabile in e la converto in numero
       in>>a;//la posiziono in a
       
       s=entry2.get_text();
       in2.str(s);
       in2>>b;
       
       c=a+b;//eseguo la somma
       
       out<<c;//la posiziono in out per riconvertire in stringa il numero ottenuto
       o=out.str();//converto il numero in stringa e la metto in s
       entry3.set_text(o);//stampo il valore ottenuto
    }
    
    void window::clear_button()
    {
       entry.set_text("Clicca il pulsante!");
       entry1.set_text("");
       entry2.set_text("");
       entry3.set_text("");
    }
    MainWindow.h
    Code:
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <gtkmm.h>
    #include <glibmm.h>
    #include <string>
    
    using namespace std;
    using Glib::ustring;
    
    class window : public Gtk::Window
    {
       public:
           window(); //costruttore
           virtual ~window(); //distruttore
       protected:
           void click_button(); //funzione click button  
           void close_button(); //pulsante di chiusura
           void somma_button(); //pulsnte per la somma
           void clear_button(); //pulsante che cancella i 3 entry
           void dialog_button(); //pulsante che lancia una finestra di dialogo
           
           Gtk::Button button,close,summ,clear,dia;  // oggetti che compongono la finestra window
           Gtk::Entry entry,entry1,entry2,entry3;    // altri oggetti della finestra window
           Gtk::Label label;
           Gtk::VBox vbox;  //contenitore principale della finestra window
           Gtk::HBox hbox,hbox1; //contenitore secondario per un label ed un pulsante
           
           double a,b,c;
           string stringa,s,o;
    };  
           
    #endif
    but it do not run...
    Can you help me?

  2. #2
    Join Date
    Mar 2006
    Beans
    837

    Re: How to make a custom window dialog, in gtkmm.

    I didn't compile your code but I suggest this:

    1. Remove "show_dialog()" from dialog.h
    2. Remove the implementation of "show_dialog()" in dialog.cpp
    3. In MainWindow.h add another Gtk::Button named "open_dialog".
    4. In MainWindow.cpp add "open_dialog" in the appropriate container and connect to it's "clicked" signal.
    5. In open_dialog's signal handler do exactly what you do in "show_dialog()".

    I hope you understood me. You mentioned in another thread that you don't understand English very well. If something I said is confusing to you, please let me know and I will clarify.

    Some more tips:
    Since you want to use "OK/CANCEL" dialogs then you should use the StockID constructor of Gtk::Button. This will ensure that the correct text will be used as label according to the user's system-language. Eg in your system it will be "OK/ANNULLA", but in an english system it will be(automatically) "OK/CANCEL".

    So in the dialog.cpp in the constructor write this:
    PHP Code:
    dialogo::dialogo()
    ok(Gtk::Stock::OK),
      
    annulla(Gtk::Stock::CANCEL
    Link1: http://library.gnome.org/devel/gtkmm...7f7259626e67cc
    Link2: http://library.gnome.org/devel/gtkmm...d369836f1f3390

  3. #3
    Join Date
    Mar 2006
    Beans
    837

    Re: How to make a custom window dialog, in gtkmm.

    Oops sorry. I just noticed your problem.

    I think you wanted to impement "show_dialog()" in the MainWindow class and not in the dialogo class.

    In MainWindow class you tell gtk to connect dia's clicked-signal to "window::show_dialog" function, but you don't provide such function in MainWindow.h/MainWindow.cpp. I propose you cut/paste the relevant parts from dialog.h/dialog.cpp.

    This is the offending line:
    PHP Code:
    dia.signal_clicked().connect(sigc::mem_fun(*this, &dialogo::show_dialog)); 
    EDIT: I made a mess of things. Do what I propose above AND also change the above line to this:
    PHP Code:
    dia.signal_clicked().connect(sigc::mem_fun(*this, &window::show_dialog)); 
    Last edited by SledgeHammer_999; November 15th, 2010 at 09:25 PM.

  4. #4
    Join Date
    Mar 2006
    Beans
    837

    Re: How to make a custom window dialog, in gtkmm.

    Hmmm on further investigation your code seems to have multiple errors. Most of them involve wrong usage of the Gtkmm API. Since this is an exercise I don't know how to proceed and not spoil "the fun" for you.

    I will not give you working code for your whole project.
    I will try to push you in the right direction.
    But you HAVE TO try things yourself too. If something fails and you can't discover why, you can ask here reporting the error and being as SPECIFIC as you can. Don't come and say "this does not compile". Give more feedback.

    And to push you in the right direction. Gtk::Dialog is a special window. You don't need to create special containers to add buttons. It has it's own internal layout. Instead create a button using this method->Gtk::Dialog::add_button().

    I suggest you read how Gtk::Dialog works(aka the docs) here and here(the original C docs usually have more documentation).

  5. #5
    Join Date
    Nov 2007
    Beans
    21

    Re: How to make a custom window dialog, in gtkmm.

    Many thanks...Now, it run. But I do not know how to add the two buttons inside the dialog...
    Maybe I should to use Gtk::VBox widget?
    I have wrote this:
    Code:
    #include"dialog.h"
    #include<gtkmm.h>
    
    dialogo::dialogo()
    : ok(Gtk::Stock::OK),
      annulla(Gtk::Stock::CANCEL)
    {
       set_title("Primo dialgo");
       add(vbox);
       vbox.pack_start(ok);
       vbox.pack_start(annulla);
       
       show_all_children();
    }
    When the window dialog is starting, there are not the two buttons, and, so on, it show me this message:
    Code:
    (MainWindow:2210): Gtk-WARNING **: Attempting to add a widget with type gtkmm__GtkVBox to a gtkmm__GtkDialog, but as a GtkBin subclass a gtkmm__GtkDialog can only contain one widget at a time; it already contains a widget of type GtkVBox
    So, my dialog have three buttons: "close", "maximize", "minimize".
    It is strange for me...a window dialog should to have only two button ("close" and "minimize") or not?
    Thank you for your help! So, I am learning how to programm in c++ and gtkmm and to speak in english too.

  6. #6
    Join Date
    Mar 2006
    Beans
    837

    Re: How to make a custom window dialog, in gtkmm.

    Re-read my last post.

    Clue: you don't create the buttons beforehand. You create them when you call "add_button".

  7. #7
    Join Date
    Nov 2007
    Beans
    21

    Re: How to make a custom window dialog, in gtkmm.

    Sorry but I do not understand...maybe becouse it is 11 pm o' clock?

  8. #8
    Join Date
    Mar 2006
    Beans
    837

    Re: How to make a custom window dialog, in gtkmm.

    Well, here it is midnight
    I'll try to explain tomorrow if I have time.

  9. #9
    Join Date
    Nov 2007
    Beans
    21

    Re: How to make a custom window dialog, in gtkmm.

    Ok, I'm going to sleep! I will read you tomorrow! good night!

  10. #10
    Join Date
    Nov 2007
    Beans
    21

    Re: How to make a custom window dialog, in gtkmm.


Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •