[gtkmm] Problems with UIManager, Action, and Accelerators



I have this small test case that I cannot get the accelerators to work.
This also shows that the Action API is quite strange in C++ and I think
we need some solution to it.  Any suggestions would be awesome.  I may
have some ideas tonite when I get back, but for now, I've attached my
example.

-Bryan
#include <gtkmm.h>
#include <iostream>

class MyWindow : public Gtk::Window
{
public:
  MyWindow();
  ~MyWindow();
private:
  Glib::RefPtr<Gtk::ActionGroup> m_rpActionGroup;
  Glib::RefPtr<Gtk::UIManager> m_rpUIManager;
  Glib::RefPtr<Gtk::AccelGroup> m_rpAccelGroup;
  Gtk::VBox m_VBox;
  Gtk::RadioButtonGroup m_RBG_HNL;

  void on_open();
  void on_quit();
  void on_zoom_in();
  void on_zoom_out();
  void on_full_screen();
  void on_quality();
};

void MyWindow::on_open()
{
  std::cout << "Open" << std::endl;
}

void MyWindow::on_quit()
{
  hide();
}

void MyWindow::on_zoom_in()
{
  std::cout << "Zoom In" << std::endl;
}

void MyWindow::on_zoom_out()
{
  std::cout << "Zoom Out" << std::endl;
}

void MyWindow::on_full_screen()
{
  std::cout << "Full Screen" << std::endl;
}

void MyWindow::on_quality()
{
  std::cout << "Quality" << std::endl;
}

MyWindow::MyWindow()
{
  static const Glib::ustring ui_description =
    "<ui>"
    "  <menubar name='MainMenu'>"
    "    <menu action='FileMenu'>"
    "      <menuitem action='Open'/>"
    "      <menuitem action='Exit'/>"
    "    </menu>"
    "    <menu action='ViewMenu'>"
    "      <menuitem action='ZoomIn'/>"
    "      <menuitem action='ZoomOut'/>"
    "      <separator/>"
    "      <menuitem action='FullScreen'/>"
    "      <separator/>"
    "      <menuitem action='HighQuality'/>"
    "      <menuitem action='NormalQuality'/>"
    "      <menuitem action='LowQuality'/>"
    "    </menu>"
    "  </menubar>"
    "</ui>";
  using namespace Gtk;
  add(m_VBox);

  m_rpAccelGroup = AccelGroup::create();
  m_rpActionGroup = ActionGroup::create("MenuActions");

  Glib::RefPtr<Action> action;
  
  action = Action::create("FileMenu", "_File");
  m_rpActionGroup->add(action);

  action = Action::create("ViewMenu","_View");
  m_rpActionGroup->add(action);

  action = Action::create("Open", Gtk::Stock::OPEN, "_Open", "Open a file");
  m_rpActionGroup->add(action, AccelKey("<control>O"), sigc::mem_fun(*this, &MyWindow::on_open));
  action->set_accel_group(m_rpAccelGroup);

  action = Action::create("Exit", Gtk::Stock::QUIT, "_Exit", "Exit the program");
  m_rpActionGroup->add(action, AccelKey("<control>Q"), sigc::mem_fun(*this, &MyWindow::on_quit));
  action->set_accel_group(m_rpAccelGroup);

  action = Action::create("ZoomIn", Gtk::Stock::ZOOM_IN, "Zoom _In", "Zoom into the image");
  m_rpActionGroup->add(action, AccelKey("plus"), sigc::mem_fun(*this, &MyWindow::on_zoom_in));
  action->set_accel_group(m_rpAccelGroup);

  action = Action::create("ZoomOut", Gtk::Stock::ZOOM_OUT, "Zoom _Out", "Zoom away from the image");
  m_rpActionGroup->add(action, AccelKey("minus"), sigc::mem_fun(*this, &MyWindow::on_zoom_out));
  action->set_accel_group(m_rpAccelGroup);
  

  Glib::RefPtr<ToggleAction> toggleaction;
  toggleaction = ToggleAction::create("FullScreen", "_Full Screen", "Switch between full screen and windowed mode");
  m_rpActionGroup->add(toggleaction, AccelKey("F11"), sigc::mem_fun(*this, &MyWindow::on_full_screen));
  action->set_accel_group(m_rpAccelGroup);
  

  Glib::RefPtr<RadioAction> radioaction;
  radioaction = RadioAction::create(m_RBG_HNL, "HighQuality", "_High Quality", "Display images in high quality, slow mode");
  m_rpActionGroup->add(radioaction, sigc::mem_fun(*this, &MyWindow::on_quality));

  radioaction = RadioAction::create(m_RBG_HNL, "NormalQuality","_Normal Quality", "Display images in normal quality");
  m_rpActionGroup->add(radioaction, sigc::mem_fun(*this, &MyWindow::on_quality));

  radioaction = RadioAction::create(m_RBG_HNL, "LowQuality","_Low Quality", "Display images in low quality, fast mode");
  m_rpActionGroup->add(radioaction, sigc::mem_fun(*this, &MyWindow::on_quality));


  m_rpUIManager = UIManager::create();
  m_rpUIManager->insert_action_group(m_rpActionGroup, 0);

  add_accel_group(m_rpAccelGroup);

  if(!m_rpUIManager->add_ui_from_string(ui_description))
  {
    std::cout << "Building menus failed" << std::endl;
    exit(EXIT_FAILURE);
  }

  Widget* menubar = m_rpUIManager->get_widget("/MainMenu");
  m_VBox.pack_start(*manage(menubar), false, false);

  show_all();
}

MyWindow::~MyWindow()
{}

int main(int argc, char* argv[])
{
  Gtk::Main myMain(argc, argv);

  MyWindow myWindow;
  Gtk::Main::run(myWindow);

  return 0;
}

Attachment: signature.asc
Description: This is a digitally signed message part



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