Tutorial |
||
|
||
|
MenuBar(); |
Menu(); Menu(const Gtk::AccelGroup& accel_group); |
Gtk::AccelGroup* get_accel_group() const; |
MenuItem(); explicit MenuItem(const String& label, bool use_underline = false); MenuItem(const String& label, Gtk::Menu& submenu, bool use_underline = false); |
Gtk::Menu *file_menu = new Gtk::Menu; // Create menu items Gtk::MenuItem *item = new Gtk::Menu("Open"); file_menu->append(item, slot(this, &MyClass::on_file_open)); item = new Gtk::Menu("Save"); file_menu->append(item, slot(this, &MyClass::on_file_save)); item = new Gtk::Menu("Quit"); file_menu->append(item, slot(this, &MyClass::on_file_quit)); // Show all the menu items file_menu->show_all(); |
Gtk::MenuBar *menubar = new Gtk::MenuBar window->add(*menubar); Gtk::MenuItem *file_item = new Gtk::MenuItem("File", *file_menu); menubar->append(*file_item); file_item->show(); menubar->show(); |
void
set_right_justified(bool right_justified); |
bool MyClass::event_handler(GdkEventButton *event); |
bool MyClass::event_handler(GdkEventButton *event, Gtk::Menu *menu); |
bind(slot(this, &MyClass:event_handler), menu); |
bool MyClass::event_handler(GdkEventButton *event, Gtk::Menu *menu) { menu->popup(event->button, event->time); // return true to indicate event has been handled return true; } |
#include<inti/main.h> #include <inti/core.h> #include <inti/gtk/menu.h> using namespace Inti; class MenuWindow : public Gtk::Window { MenuWindow(const MenuWindow&); MenuWindow& operator=(const MenuWindow&); protected: bool on_button_press(GdkEventButton *event, Gtk::Menu *menu); public: MenuWindow(); virtual ~MenuWindow(); void menu_item_selected(const char *parent, const char *item); void on_file_new(); void on_file_open(); void on_file_save(); void on_file_save_as(); void on_file_quit(); void on_options_preferences(); void on_help_about(); }; |
#include"menu.h" #include <inti/gtk/accelgroup.h> #include <inti/gtk/eventbox.h> #include <inti/gtk/label.h> #include <inti/gtk/menubar.h> #include <inti/gtk/menuitem.h> #include <inti/gtk/separatormenuitem.h> #include <inti/bind.h> MenuWindow::MenuWindow() { set_title("Menu Window"); set_size_request(300, 200); // Boxes don't receive button events so use an eventbox. The eventbox is added first // and then all the other widgets added to it. Gtk::EventBox *eventbox = new Gtk::EventBox; add(*eventbox); // Set the events the eventbox is to receive. These can be any number of or'd (|) values // from the Gdk::EventMask enumeration. eventbox->set_events(Gdk::BUTTON_PRESS_MASK); // Add the packing box to eventbox Gtk::VBox *vbox = new Gtk::VBox(false, 1); vbox->set_border_width(1); eventbox->add(*vbox); // Create the menubar. Menus can be created by using the append, prepend or insert methods // in menushell.h to add menu items. Gtk::MenuBar *menubar = new Gtk::MenuBar; Gtk::AccelGroup *accel_group = add_accel_group(); // Create the File menu Gtk::Menu *menu = new Gtk::Menu(*accel_group); menu->append(*(new Gtk::MenuItem("_New", true)), "<control>N", slot(this, &MenuWindow::on_file_new)); menu->append(*(new Gtk::MenuItem("_Open", true)), "<control>O", slot(this, &MenuWindow::on_file_open)); menu->append(*(new Gtk::MenuItem("_Save", true)), "<control>S", slot(this, &MenuWindow::on_file_save)); menu->append(*(new Gtk::MenuItem("Save _As", true)), slot(this, &MenuWindow::on_file_save_as)); menu->append(*(new Gtk::SeparatorMenuItem)); menu->append(*(new Gtk::MenuItem("_Quit", true)), "<control>Q", slot(this, &MenuWindow::on_file_quit)); menubar->append(*(new Gtk::MenuItem("_File", *menu, true))); // Bind the file menu to the button_press event and use it as the popup menu. eventbox->sig_button_press_event().connect(bind(slot(this, &MenuWindow::on_button_press), menu)); // Create Options menu menu = new Gtk::Menu; menu->append(*(new Gtk::MenuItem("_Preferences", true)), slot(this, &MenuWindow::on_options_preferences)); menubar->append(*(new Gtk::MenuItem("_Options", *menu, true))); // Create Help menu menu = new Gtk::Menu; menu->append(*(new Gtk::MenuItem("About")), slot(this, &MenuWindow::on_help_about)); Gtk::MenuItem *menu_item = new Gtk::MenuItem("_Help", *menu, true); menu_item->set_right_justified(true); menubar->append(*menu_item); // Pack the menubar into the vbox vbox->pack_start(*menubar, false); // Add a label that tells the user to click the mouse button inside the client area. Gtk::Label *label = new Gtk::Label("Click mouse button here..."); vbox->pack_start(*label); // Being lazy, just show everything with one call. show_all(); } MenuWindow::~MenuWindow() { } bool MenuWindow::on_button_press(GdkEventButton *event, Gtk::Menu *menu) { menu->popup(event->button, event->time); return true; } void MenuWindow::menu_item_selected(const char *parent, const char *item) { g_message("Menu: activated the \"%s\" menu item: \"%s\"", parent, item); } void MenuWindow::on_file_new() { menu_item_selected("File", "New"); } void MenuWindow::on_file_open() { menu_item_selected("File", "Open"); } void MenuWindow::on_file_save() { menu_item_selected("File", "Save"); } void MenuWindow::on_file_save_as() { menu_item_selected("File", "Save As"); } void MenuWindow::on_file_quit() { dispose(); } void MenuWindow::on_options_preferences() { menu_item_selected("Options", "Preferences"); } void MenuWindow::on_help_about() { menu_item_selected("Help", "About"); } INTI_MAIN(MenuWindow) |
#define
INTI_MAIN(MainWidget)\ int main (int argc, char *argv[])\ {\ Inti::Main::init(&argc, &argv);\ MainWidget main_widget;\ main_widget.sig_destroy().connect(slot(&Inti::Main::quit));\ main_widget.show();\ Inti::Main::run();\ return 0;\ } |
|
|||
|
|||