RE: [gtkmm] Two beginners questions to signal handling



>1. Is there a way to connect the signal handler
>    B::something_in_B_selected() to A::update_values_in_A()?
>    class A : public Gtk::VBox
>    class B : public Gtk::VBox

Here's an example (someone can step in and tell me if this is the preferred way
of doing things):

Assume two classes.. A Main Window for your application, and a Main Menu
(menubar) to be displayed on the Main Window.  When you select File->Quit from
the menubar, you want the application to quit by calling the on_quit() signal
handler in the MainWindow object.

1) [mainmenu.h]
class MainMenu : public Gtk::MenuBar
{
public:
   MainMenu(MainWindow &object);
};

2) [mainwindow.h]
class MainWindow : public Gtk::Window
{
friend class MainMenu;
public:
  MainWindow();
  virtual ~MainWindow();
protected:
   // signal handlers
   virtual void on_quit();
};

3) [mainmenu.cpp]
#include "mainmenu.h"

MainMenu::MainMenu(MainWindow &object)
{
   // fill menus
   
   // File|New sub menu:
   {
      Gtk::Menu::MenuList& menulist = m_menu_file_new.items();
      
   }

   // File menu:
   {
  	   Gtk::Menu::MenuList& menulist = m_menu_file.items();

  	   menulist.push_back( Gtk::Menu_Helpers::MenuElem("_New", m_menu_file_new)
); //Add sub menu.
  	   menulist.push_back( Gtk::Menu_Helpers::MenuElem("_Quit",
Gtk::Menu::AccelKey("<control>q"),
      SigC::slot(object, &MainWindow::on_quit) ) );
   }
    
   //Add the menus to the MenuBar:
   items().push_back( Gtk::Menu_Helpers::MenuElem("_File", m_menu_file) );
   
   Gtk::MenuItem* pMenuItem = &items().back();
   pMenuItem->set_right_justified();   
   
   show_all_children();
}

The key is to pass a MainWindow pointer to the MainMenu constructor.  You can
then use this pointer to MainWindow to connect your signals to any signal
handler in MainWindow.  I've declared MainMenu to be a friend of MainWindow in
order to allow access to the protected signal handlers.

If anyone has a better way of doing this, please speak up. =)

-- 
Gene Ruebsamen


-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]