Re: [gtkmm] Access CheckMenuItem after inserted into menu?



I had this problem trying to implement a message in a status bar when the user moves
his/her mouse over the menu item (yeah, I reimplemented something from libgnomeuimm... oh
well), but I found a solution that I've adapted to your problem:

1. Make some pointers to Gtk::CheckMenuItem (the number depends on how many
CheckMenuElems you have and/or how many you want to access.  For our example, we'll use
m_abc, m_def, and m_ghi (remember, this is in your class):

Gtk::CheckMenuItem *m_abc, *m_def, *m_ghi;

2. After push_back'ing the CheckMenuElems into the menu, assign one of your pointers the
address (I think) of the back method of menulist. Here's an example using your code:

    ClassTest::ClassTest() : somelabel("fred") {
      /* Construct the popup menu */
      Menu::MenuList &menulist = popup.items();
          menulist.push_back(Menu_Helpers::CheckMenuElem("abc",
              SigC::slot(*this, &ClassTest::ChangeLabel)));
          m_abc = &menulist.back();
          menulist.push_back(Menu_Helpers::CheckMenuElem("def",
              SigC::slot(*this, &ClassTest::ChangeLabel)));
          m_def = &menulist.back();
          menulist.push_back(Menu_Helpers::CheckMenuElem("ghi",
              SigC::slot(*this, &ClassTest::ChangeLabel)));
          m_ghi = &menulist.back();
          /* ... could be any number of elements added ... */
      popup.accelerate(*this);
      ...

3. Now access the check menu items by dereferencing them (->)

m_abc->toggled();

Disclaimer: I've only used this to check if the menu item (Gtk::MenuItem) has the mouse
over it, so this should work (given my limited experience) with Gtk::CheckMenuItem. I
hope this helps.  If I'm wrong, someone please correct me :).

-Bryan



--- D Bently <dbently bigpond com> wrote:
> I am constructing the gui interface for several tools, using gtkmm 
> 2.0.2. Just quickly, I am trying to access the original elements 
> inserted into a popup menu. See code at the end. Explination of problem 
> below.
> 
> Okay, I have puzzled over this one for a few days now, and know there is 
> some easy answer... I have a popup menu that contains a whole set of 
> CheckMenuItems (the real number is about 50 items that are loaded from a 
> text file).
> 
> The problem is because I do not know what these items will be at compile 
> time, I can not construct a callback function for each item. Besides 
> which creating 50 callback functions would be a waste...
> 
> What I need to be able to do is construct a string containing the 
> contents of which items have been selected. The only way I figure I can 
> do this is to create a generic callback function, and from that extract 
> which check menu items have been selected. That is where I have the problem.
> 
> I have included an example below. Basically how can I access the 
> originally inserted checkmenuitem (or equiv.) to determin which items 
> are selected?
> 
> Thanks in advance,
> 
> D Bentley.
> 
> ----------
> 
> #include <gtkmm/box.h>
> #include <gtkmm/label.h>
> #include <gtkmm/combo.h>
> #include <gtkmm/menu.h>
> #include <gtkmm/eventbox.h>
> 
> using namespace Gtk;
> 
> class ClassTest : public HBox {
> public:
>      ClassTest();
> 
> private:
>      Label somelabel;
>      Menu popup;
> 
>      void ChangeLabel();
> 
> protected:
> 
> };
> 
> ClassTest::ClassTest() : somelabel("fred") {
>      /* Construct the popup menu */
>      Menu::MenuList &menulist = popup.items();
>          menulist.push_back(Menu_Helpers::CheckMenuElem("abc",
>              SigC::slot(*this, &ClassTest::ChangeLabel)));
>          menulist.push_back(Menu_Helpers::CheckMenuElem("def",
>              SigC::slot(*this, &ClassTest::ChangeLabel)));
>          menulist.push_back(Menu_Helpers::CheckMenuElem("ghi",
>              SigC::slot(*this, &ClassTest::ChangeLabel)));
>          /* ... could be any number of elements added ... */
>      popup.accelerate(*this);
> 
>      /* Insert the label */
>      pack_start(somelabel);
> }
> 
> void ClassTest::ChangeLabel() {
>      Glib::ustring str;
> 
>      /* Go through each of the menu items, and extract any selected */
>      const Menu_Helpers::MenuList &menulist = popup.items();
>      Menu_Helpers::MenuList::iterator iter = menulist.begin();
>      for ( ; iter != menulist.end(); iter++) {
>          MenuItem &item = *iter;
> 
>          /* HELP NEEDED HERE */
>          /* Probably something along the lines of:
>          if (item is a CheckMenuItem) {
>              CheckMenuItem *whatever = some sort of cast (item)
>              if (whatever->get_value() == true)
>                  str += soemthing else; */
>      }
> 
>      somelabel.set_text(str);
> }
> 
> 
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



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