Re: [gtkmm] Problems with UIManager, Action, and Accelerators



Sweet, that works.  Looking at my program (which I modified), the API
isn't that bad.  I've attached it as well.  I'm getting a warning on the
command line now when I quit this program:

(test:28463): GLib-GObject-CRITICAL **: file gobject.c: line 1579
(g_object_unref): assertion `G_IS_OBJECT (object)' failed

No clue what this means.

-Bryan



On Fri, 2004-03-19 at 10:25 +0100, Murray Cumming wrote:

> On Fri, 2004-03-19 at 00:56, Bryan Forbes wrote:
> > I have this small test case that I cannot get the accelerators to work.
> 
> 
> I don't know where you got the AccelGroup stuff from, but you don't need
> it and it doesn't seem to work. Here is a patch to your test that makes
> it work for me. You do need to call
>   add_accel_group(m_refUIManager->get_accel_group());
> on the parent window, and that seems a bit obscure.
> 
> > This also shows that the Action API is quite strange in C++ and I think
> > we need some solution to it.
> 
> Can you be more precise? Where exactly is the problem? I don't think
> it's very different from the old gtkmm 2.2 menu/toolbar API or the
> libgnomeuimm UIInfo API.
> 
> You can make it more compact by doing the Action::create() inside the
> add() call. You didn't do that this time because you thought you had to
> do the extra accelgroup stuff.
> 
> Had you seen demos/gtk-demo/example_uimanager.cc?
>  
> >   Any suggestions would be awesome.  I may
> > have some ideas tonite when I get back, but for now, I've attached my
> > example.
> 
> -- 
> Murray Cumming
> www.murrayc.com
> murrayc murrayc com
#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_rpActionGroup = ActionGroup::create("MenuActions");

  Glib::RefPtr<Action> action;
  
  m_rpActionGroup->add(Action::create("FileMenu", "_File"));
  m_rpActionGroup->add(Action::create("ViewMenu","_View"));
  m_rpActionGroup->add(Action::create("Open", Gtk::Stock::OPEN, "_Open", "Open a file"), 
                        sigc::mem_fun(*this, &MyWindow::on_open));
  m_rpActionGroup->add(Action::create("Exit", Gtk::Stock::QUIT, "_Exit", "Exit the program"), 
                        AccelKey("<control>Q"), sigc::mem_fun(*this, &MyWindow::on_quit));
  m_rpActionGroup->add(Action::create("ZoomIn", Gtk::Stock::ZOOM_IN, 
                                      "Zoom _In", "Zoom into the image"), 
                        AccelKey("plus"), sigc::mem_fun(*this, &MyWindow::on_zoom_in));
  m_rpActionGroup->add(Action::create("ZoomOut", Gtk::Stock::ZOOM_OUT,
                                      "Zoom _Out", "Zoom away from the image"),
                        AccelKey("minus"), sigc::mem_fun(*this, &MyWindow::on_zoom_out));

  m_rpActionGroup->add(ToggleAction::create("FullScreen", "_Full Screen", 
                                            "Switch between full screen and windowed mode"),
                        AccelKey("F11"), sigc::mem_fun(*this, &MyWindow::on_full_screen));
  

  m_rpActionGroup->add(RadioAction::create(m_RBG_HNL, "HighQuality", "_High Quality", 
                                                      "Display images in high quality, slow mode"),
                        sigc::mem_fun(*this, &MyWindow::on_quality));
  m_rpActionGroup->add(RadioAction::create(m_RBG_HNL, "NormalQuality","_Normal Quality",
                                                      "Display images in normal quality"),
                        sigc::mem_fun(*this, &MyWindow::on_quality));
  m_rpActionGroup->add(RadioAction::create(m_RBG_HNL, "LowQuality","_Low Quality",
                                                      "Display images in low quality, fast mode"),
                        sigc::mem_fun(*this, &MyWindow::on_quality));


  m_rpUIManager = UIManager::create();
  m_rpUIManager->insert_action_group(m_rpActionGroup);
  add_accel_group(m_rpUIManager->get_accel_group());


  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]