Re: Accelerator



Accelerators can be confusing. There are 2 ways to do them, one
reasonably well documented, the other not documented at all (AFAIK).
Here is a sample of the undocumented (and really more complicated) way.
HTH
I have removed some redundant code, but this should show you the basic
steps.

Chuck Crisler

class HelloWorld : public Gtk::Window
{
public:
        HelloWorld();
        virtual ~HelloWorld();

protected:
        //Signal Handlers
        virtual void on_button_clicked();
        virtual void on_fileopen_activated();
        virtual void on_fileclose_activated();
...
        //Member widgets.
        Gtk::Button m_button;
        Gtk::VBox m_VBox;
        Gtk::MenuBar m_MainMenu;
        Gtk::Menu m_FileMenu;
        Gtk::Menu m_EditMenu;
        Gtk::MenuItem m_RootMenu1;
        Gtk::MenuItem m_RootMenu2;
        Gtk::MenuItem m_FileOpenItem;
        Gtk::MenuItem m_FileCloseItem;
...
        Glib::RefPtr<Gtk::AccelGroup>   m_oAccelGroup;
};
HelloWorld::HelloWorld() : m_button(Gtk::Stock::OK)
{
        //Set border width for window
        set_border_width( 10 );
        set_title( "Menu Test Window" );

        //      Create the accelerator group for the window.
        m_oAccelGroup = Gtk::AccelGroup::create( );
        //      Add that group to the window so that accelerators can be added.
        add_accel_group( m_oAccelGroup );

        Glib::ustring  strAccelSignal = "activate";
...
        //      Setup the file menu options.
        m_FileOpenItem.add_label("Open", false, Gtk::ALIGN_LEFT,
Gtk::ALIGN_TOP);
        m_FileCloseItem.add_label("Close", false, Gtk::ALIGN_LEFT,
Gtk::ALIGN_TOP);
...
        m_FileExit.signal_activate().connect(sigc::mem_fun(*this,
&HelloWorld::on_fileexit_activated));

        //      Setup the accelerators
        m_FileOpenItem.add_accelerator( strAccelSignal, m_oAccelGroup, GDK_O,
                Gdk::CONTROL_MASK, Gtk::ACCEL_MASK );
        m_FileCloseItem.add_accelerator( strAccelSignal, m_oAccelGroup, GDK_C,
                Gdk::CONTROL_MASK, Gtk::ACCEL_MASK );
...
        m_FileMenu.append(m_FileOpenItem);
        m_FileMenu.append(m_FileCloseItem);
...
        m_RootMenu1.add_label("File", false, Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP);
        m_RootMenu2.add_label("Edit", false, Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP);
        m_RootMenu1.set_submenu(m_FileMenu);
        m_RootMenu2.set_submenu(m_EditMenu);

        m_RootMenu1.show();
        m_RootMenu2.show();
        m_MainMenu.append(m_RootMenu1);
        m_MainMenu.append(m_RootMenu2);
        m_MainMenu.show();
        m_VBox.add(m_MainMenu);
...
        m_Window.set_size_request( 780, 550 );
        m_Window.show();
        m_VBox.add(m_Window);
        m_VBox.show();
        add(m_VBox);

        set_default_size( 800, 600 );

        show_all_children();
        show();
}
...

void HelloWorld::on_fileopen_activated()
{
        std::cout << "Hello World! FileOpen activated." << std::endl;
}

void HelloWorld::on_fileclose_activated()
{
        std::cout << "Hello World! FileClose activated." << std::endl;
}

void HelloWorld::on_fileexit_activated()
{
        std::cout << "Hello World! FileExit activated." << std::endl;
        //      Terminate the application.
        Gtk::Main::quit();
}
...

int main(int argc, char *argv[])
{
        Gtk::Main kit(argc, argv);
        HelloWorld helloworld;
        //Shows the window and returns when it is closed.
        Gtk::Main::run();

        return (0);
}


On Mon, 2009-05-25 at 10:41 -0400, dhk wrote:
Are accelerators only for menu items?  I've seen a lot of examples with 
accelerators and the all seem to connect to a menu item.  I've been 
having a difficult time getting accelerators to work even with the 
examples.  I think I'm missing something.

One thing I would like is to just have an accelerator execute a function 
or a callback.  Maybe if I could just print "Hello World" to the 
terminal when pressing Alt-F4 would help prove the concept.

Can someone help?

Thanks,

dhk
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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