コード例 #1
0
    Glib::RefPtr<Gtk::UIManager> SmartChessWindow::createUIManager(
            Glib::RefPtr<Gtk::ActionGroup> &action_group) {
        auto ui_manager = Gtk::UIManager::create();

        ui_manager->insert_action_group(action_group);
        add_accel_group(ui_manager->get_accel_group());

        Glib::ustring ui_info =
                "<ui>"
                "  <menubar name='MenuBar'>"
                "    <menu action='MenuFile'>"
                "      <separator/>"
                "      <menuitem action='Quit'/>"
                "    </menu>"
                "       <menu action='MenuView'>"
                "           <menuitem action='ShowOptionsArea' />"
                "           <menuitem action='ShowLogArea' />"
                "       </menu>"
                "       <menu action='MenuHelp'>"
                "           <menuitem action='About' />"
                "       </menu>"
                "  </menubar>"
                "  <toolbar  name='ToolBar'>"
                "    <toolitem action='Quit'/>"
                "  </toolbar>"
                "</ui>";

        ui_manager->add_ui_from_string(ui_info);
        return ui_manager;
    }
コード例 #2
0
ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
: m_Box(Gtk::ORIENTATION_VERTICAL)
{
	set_title("main_menu example");
	set_default_size(200,200);

	add(m_Box); //We can put a MenuBar at the top of the box and other stuff below it.

	// Define the actions:
	m_refActionGroup = Gio::SimpleActionGroup::create();

	m_refActionGroup->add_action("new",
		sigc::mem_fun(*this, &ExampleWindow::on_action_file_new) );
	m_refActionGroup->add_action("open",
		sigc::mem_fun(*this, &ExampleWindow::on_action_others) );


	m_refActionRain = m_refActionGroup->add_action_bool("rain",
		sigc::mem_fun(*this, &ExampleWindow::on_action_toggle),
		false);

	m_refActionGroup->add_action("quit",
		sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit) );

	m_refActionGroup->add_action("cut",
		sigc::mem_fun(*this, &ExampleWindow::on_action_others) );
	m_refActionGroup->add_action("copy",
		sigc::mem_fun(*this, &ExampleWindow::on_action_others) );
	m_refActionGroup->add_action("paste",
		sigc::mem_fun(*this, &ExampleWindow::on_action_others) );

	insert_action_group("example", m_refActionGroup);

	// Define how the actions are presented in the menus and toolbars:
	m_refBuilder = Gtk::Builder::create();

	// Layout the actions in a menubar and toolbar:
	const char* ui_info =
		"<interface>"
		"  <menu id='menubar'>"
		"    <submenu>"
		"      <attribute name='label' translatable='yes'>_File</attribute>"
		"      <section>"
		"        <item>"
		"          <attribute name='label' tranlatable='yes'>_New</attribute>"
		"          <attribute name='action'>example.new</attribute>"
		"          <attribute name='accel'>&lt;Primary&gt;n</attribute>"
		"        </item>"
		"        <item>"
		"          <attribute name='label' translatable='yes'>_Open</attribute>"
		"          <attribute name='action'>example.open</attribute>"
		"          <attribute name='accel'>&lt;Primary&gt;o</attribute>"
		"        </item>"
		"      </section>"
		"      <section>"
		"        <item>"
		"          <attribute name='label' translatable='yes'>Rain</attribute>"
		"          <attribute name='action'>example.rain</attribute>"
		"        </item>"
		"      </section>"
		"      <section>"
		"        <item>"
		"          <attribute name='label' translatable='yes'>_Quit</attribute>"
		"          <attribute name='action'>example.quit</attribute>"
		"          <attribute name='accel'>&lt;Primary&gt;q</attribute>"
		"        </item>"
		"      </section>"
		"    </submenu>"
		"    <submenu>"
		"      <attribute name='lable' translatable='yes'>_Edit</attribute>"
		"      <item>"
		"        <attribute name='label' tlanslatable='yes'>_Cut</attribute>"
		"        <attribute name='action'>example.cut</attribute>"
		"        <attribute name='accel'>&lt;Primary&gt;x</attribute>"
		"      </item>"
		"      <item>"
		"        <attribute name='label' translatable='yes'>_Copy</attribute>"
		"        <attribute name='action'>example.copy</attribute>"
		"        <attribute name='accel'>&lt;Primary&gt;c</attribute>"
		"      </item>"
		"      <item>"
		"        <attribute name='label' translatable='yes'>_Paste</attribute>"
		"        <attribute name='action'>example.paste</attribute>"
		"        <attribute name='accel'>&lt;Primary&gt;v</attribute>"
		"      </item>"
		"    </submenu>"
		"  </menu>"
		"</interface>";

	// When the manubar is a child of a Gtk::Window, keyboard accelerators are not
	// automatically fetched from the Gio::Menu.
	// See the examples/book/menus/main_menu example for an alternative way of
	// adding the menubar when using Gtk::ApplicationWindow.
	// Gtk::Application::set_accel_for_action() is new in gtkmm 3.11.9.
	app->set_accel_for_action("example.new", "<Primary>n");
	app->set_accel_for_action("example.open", "<Primary>o");
	app->set_accel_for_action("example.quit", "<Primary>q");
	app->set_accel_for_action("example.cut", "<Primary>x");
	app->set_accel_for_action("example.copy", "<Primary>c");
	app->set_accel_for_action("example.paste", "<Primary>v");
	
	try
	{
		m_refBuilder->add_from_string(ui_info);
		m_refBuilder->add_from_resource("/toolbar/toolbar.glade");
	}
	catch(const Glib::Error& ex)
	{
		std::cerr << "Building menus and toolbar failed: " << ex.what();
	}
	
	// Get the menubat:
	auto object = m_refBuilder->get_object("menubar");
	auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object);
	if (!gmenu)
		g_warning("GMenu not found");
	else
	{
		auto pMenuBar = Gtk::manage(new Gtk::MenuBar(gmenu));
		
		// Add the MenuBar to the window:
		m_Box.pack_start(*pMenuBar, Gtk::PACK_SHRINK);
	}
	
	// Get the toolbar and add it to a container widget:
	Gtk::Toolbar* toolbar = nullptr;
	m_refBuilder->get_widget("toolbar", toolbar);
	if (!toolbar)
		g_warning("GtkToolbar not found");
	else
		m_Box.pack_start(*toolbar, Gtk::PACK_SHRINK);

	show_all_children();
}
コード例 #3
0
ファイル: gsteg.cpp プロジェクト: elken/GSteg
GSteg::GSteg() : gsteg_box(Gtk::ORIENTATION_VERTICAL)
{
    //Default gubbins:
    set_title("GSteg");
    set_default_size(500, 500);
    add(gsteg_box); 
  
    //Define the actions:
    gsteg_ag = Gio::SimpleActionGroup::create();
  
    gsteg_ag->add_action("open",    sigc::mem_fun(*this, &GSteg::on_action_file_open));
    gsteg_ag->add_action("quit",    sigc::mem_fun(*this, &GSteg::on_action_file_quit));
    gsteg_ag->add_action("encode",  sigc::mem_fun(*this, &GSteg::on_action_encode));
    gsteg_ag->add_action("decode",  sigc::mem_fun(*this, &GSteg::on_action_decode));
    gsteg_ag->add_action("about",   sigc::mem_fun(*this, &GSteg::on_action_help_about));
  
    insert_action_group("gsteg", gsteg_ag);

    this->add_events(Gdk::KEY_PRESS_MASK);
    this->signal_key_release_event().connect(sigc::mem_fun(*this, &GSteg::on_key_release));

    //Create and allocate the builder:
    Glib::RefPtr<Gtk::Builder> gsteg_ui = Gtk::Builder::create();
    Glib::ustring ui_info = 
    "<interface>"
        "<menu id='gsteg_menu'>"
            "<submenu>"
                "<attribute name='label' translatable='yes'>_File</attribute>"
                    "<section>"
                        "<item>"
                            "<attribute name='label' translatable='yes'>_Open</attribute>"
                            "<attribute name='action'>gsteg.open</attribute>"
                            "<attribute name='accel'>&lt;Primary&gt;o</attribute>"
                        "</item>"
                   "</section>"
                   "<section>"
                        "<item>"
                            "<attribute name='label' translatable='yes'>_Quit</attribute>"
                            "<attribute name='action'>gsteg.quit</attribute>"
                            "<attribute name='accel'>&lt;Primary&gt;q</attribute>"
                        "</item>"
                    "</section>"
            "</submenu>"
            "<submenu>"
                "<attribute name='label' translatable='yes'>_Encode</attribute>"
                    "<section>"
                        "<item>"
                            "<attribute name='label' translatable='yes'>_Encode</attribute>"
                            "<attribute name='action'>gsteg.encode</attribute>"
                            "<attribute name='accel'>&lt;Primary&gt;e</attribute>"
                        "</item>"
                    "</section>"
            "</submenu>"
            "<submenu>"
                "<attribute name='label' translatable='yes'>_Decode</attribute>"
                    "<section>"
                        "<item>"
                            "<attribute name='label' translatable='yes'>_Decode</attribute>"
                            "<attribute name='action'>gsteg.decode</attribute>"
                            "<attribute name='accel'>&lt;Primary&gt;d</attribute>"
                        "</item>"
                    "</section>"
            "</submenu>"
            "<submenu>"
                "<attribute name='label' translatable='yes'>_Help</attribute>"
                    "<item>"
                        "<attribute name='label' translatable='yes'>_About</attribute>"
                        "<attribute name='action'>gsteg.about</attribute>"
                        "<attribute name='accel'>F1</attribute>"
                    "</item>"
            "</submenu>"
    "</menu>";
    try
    {
        gsteg_ui->add_from_string(ui_info);
    }
    catch(const Glib::Error& ex)
    {
        msgBox("Unable to load UI", "The UI string is invalid. Correct and try again.", "UI Error", Gtk::MESSAGE_ERROR);
    }

    //Get the menubar:
    Glib::RefPtr<Gio::Menu> gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(gsteg_ui->get_object("gsteg_menu"));
  
    if(!gmenu)
    {
        msgBox("Unable to load menu from UI", "Unable to read the menu, or the menu failed to initialize.", "UI Error", Gtk::MESSAGE_ERROR);
    } 

    gsteg_menu = new Gtk::MenuBar(gmenu);
  
    //Create and add empty image to window:
    gsteg_image = Gtk::manage(new Gtk::Image());

    //Create and allocate txt_in:
    gsteg_txt_in = Gtk::manage(new Gtk::TextView());
    gsteg_txt_in->set_wrap_mode(Gtk::WRAP_CHAR);
    gsteg_txt_in->set_buffer(gsteg_txt_buf);

    //Create the ScrolledWindow to stop TextView auto-resizing:
    gsteg_txt_no_scroll = Gtk::manage(new Gtk::ScrolledWindow());
    gsteg_txt_no_scroll->add(*gsteg_txt_in);

    //Create the ScrolledWindow to stop Image auto-resizing:
    gsteg_img_no_scroll = Gtk::manage(new Gtk::ScrolledWindow());
    gsteg_img_no_scroll->add(*gsteg_image);

    //AboutDialog gubbins:
    gsteg_about.set_logo_icon_name("help-about");
    gsteg_about.set_program_name("GSteg");
    gsteg_about.set_version("1.0");
    gsteg_about.set_copyright("Ellis Kenyo");
    gsteg_about.set_comments("GSteg is an application for embedding text within an image.");
    gsteg_about.set_license_type(Gtk::LICENSE_BSD);
    gsteg_about.set_website("http://www.elken.github.io/GSteg");

    //Add widgets to the window:
    gsteg_box.pack_start(*gsteg_menu, Gtk::PACK_SHRINK);
    gsteg_box.pack_start(*gsteg_img_no_scroll, true, true);
    gsteg_box.pack_start(*gsteg_txt_no_scroll, true, true);

    show_all_children();
}