[gtkmm] Access CheckMenuItem after inserted into menu?



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);
}





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