Dynamic menu

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
coder89
Experienced Solver
Experienced Solver
Posts: 77
Joined: Fri May 23, 2008 9:01 am

Dynamic menu

Post by coder89 » Thu May 29, 2008 4:48 am

Hello.
I have a dynamical created menu with wxMenuRadioItems. Each position in menu is downloaded from MySQL database so I don't know before run the program which position in menu will be created.
And now is the question. How to run any c++ function when user click any RadioItem from this dynamical menu?? (I know how to do this in static menu - I use Connect() function and wxEvents to each RadioItems and it will works good but the dynamical creation of menu and then runing any function is a very big problem for me).

Thnx for help.
Best regards.

PS. Oo. I forgot.. How to get the name of this RadioItem (to use in function) when user click on them?

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu May 29, 2008 2:21 pm

You can check and uncheck item using the method Check() (see the docs for the check item)

You can catch the event using 'wxEVT_COMMAND_MENU_SELECTED'

You can find which was clicked by using the methods of wxCommandEvent : http://docs.wxwidgets.org/stable/wx_wxcommandevent.html, http://docs.wxwidgets.org/stable/wx_wxe ... ml#wxevent

A few look interesting:
GetString()
GetEventObject()
GetId()

coder89
Experienced Solver
Experienced Solver
Posts: 77
Joined: Fri May 23, 2008 9:01 am

Post by coder89 » Thu May 29, 2008 4:54 pm

OK Thank you. And I have last question about wxMenuRadioItems.
If I create this menu, the first element of list is always checked at start. How to uncheck all RadioItems?? ( RadioItem->wxMenu::Checked(false) don't work - First element is still checked :( )

JudyL
Experienced Solver
Experienced Solver
Posts: 66
Joined: Tue Jul 24, 2007 2:20 pm
Location: Florida, USA

Post by JudyL » Thu May 29, 2008 5:25 pm

coder89 wrote:How to uncheck all RadioItems??
You can't. The basic nature of a group of radio items requires that one and only one of the group must be selected. If you want none selected or multiple selected, you have to use a control for each item that is independent of every other item (except for whatever dependencies you implement in code), like a check box.

Judy

coder89
Experienced Solver
Experienced Solver
Posts: 77
Joined: Fri May 23, 2008 9:01 am

Post by coder89 » Thu May 29, 2008 7:41 pm

Ok I have add to each RadioItem this line "wxCommandEvent::wxCommandEvent(wxEVT_COMMAND_MENU_SELECTED, id_of_radio_item);"

But now how to set which function must be called after I click on any item??

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu May 29, 2008 11:30 pm

coder89 wrote: But now how to set which function must be called after I click on any item??
I'm not sure I understand the question - use event tables or Conenct() ?

coder89
Experienced Solver
Experienced Solver
Posts: 77
Joined: Fri May 23, 2008 9:01 am

Post by coder89 » Fri May 30, 2008 1:10 pm

Code: Select all

//This is Connect() function for wxMenuRadioItem
Connect(ids,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&CoderCatFrame::SelectDataBase);

void CoderCatFrame::SelectDataBase(wxCommandEvent& event)
{
    wxString msg = event.GetString();
    if(msg)
    {
        wxMessageBox(msg, _("Welcome to..."));
    }
}
Why in this code variable msg is empty in any time I click on wxMenu RadioItem?? It should have a name of Radio Item right??

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Fri May 30, 2008 2:42 pm

coder89 wrote: Why in this code variable msg is empty in any time I click on wxMenu RadioItem?? It should have a name of Radio Item right??
maybe, maybe not
try using the other functions I mentionned, what is set and what is not depends on the event type and it's not always what you'd expect

swapd0
I live to help wx-kind
I live to help wx-kind
Posts: 169
Joined: Mon May 14, 2007 11:16 am
Location: Spain

Post by swapd0 » Fri May 30, 2008 2:45 pm

Another option is to create each radio menu item with a different id, and catch a range event.

Code: Select all

//This is Connect() function for wxMenuRadioItem
Connect(ids, ids + num, wxEVT_COMMAND_RANGE,(wxObjectEventFunction)&CoderCatFrame::SelectDataBase);

void CoderCatFrame::SelectDataBase(wxCommandEvent& event)
{
    switch (event.Id())
    {
         case ids:
         ...
         break;
         case ids+1:
         break;
    }

}

coder89
Experienced Solver
Experienced Solver
Posts: 77
Joined: Fri May 23, 2008 9:01 am

Post by coder89 » Sat May 31, 2008 8:05 am

But I create each RadioItem with different id. I increment variable ids.

My code look like this:

Code: Select all

//Function which create all RarioItems from Mysql data base
void CoderCatFrame::InsertMySQLMenu()
{
    MYSQL_RES * result = mysql_list_dbs(mysql_handle, NULL);
    MYSQL_ROW row;
    int ids = 0;
	while(row = mysql_fetch_row(result))
    {
        ++ids;
        wxString nazwa = wxString::FromAscii(row[0]);
        CoderCatFrame::Mysql_select_db->wxMenu::AppendRadioItem(ids, nazwa);
        Connect(ids,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&CoderCatFrame::SelectDataBase);
        if(nazwa != wxString::FromAscii(mysql_db))
        {
            CoderCatFrame::Mysql_select_db->wxMenu::Check(ids, false);
        }
        else
        {
            CoderCatFrame::Mysql_select_db->wxMenu::Check(ids, true);
        }
    }
}

//Function to show name of RadioItem
void CoderCatFrame::SelectDataBase(wxCommandEvent& event)
{
    wxString msg = event.GetString();
    wxMessageBox(msg, _("Welcome to..."));
}
Pleas help me... :( I'm trying to get the name of each element since yesterday and I can't do this :(

tan
Moderator
Moderator
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan » Sat May 31, 2008 8:41 am

Hi,

Code: Select all

//Function to show name of RadioItem
void CoderCatFrame::SelectDataBase(wxCommandEvent& event)
{
    wxMenuItem* item = CoderCatFrame::Mysql_select_db->wxMenu::FindItem(event.GetId());
    if( item )
    {
        wxString msg = item->GetItemLabelText();
        wxMessageBox(msg, _("Welcome to..."));
    }
}
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10

lollisoft
Earned some good credits
Earned some good credits
Posts: 115
Joined: Sat Jul 23, 2005 3:52 pm
Location: Germany
Contact:

Post by lollisoft » Mon Jul 21, 2008 6:00 pm

I have very successfully implemented dynamic menu entries.

As of doing this really dynamic, you need a mapping between
the menunames and their ID's.

While you add a new menu entry, you increment the ID. You also
should track the ID in a list with a key of type string.

Then you could get back the ID from the name that comes with
the event.

Watch on uniqueness, that probably give you some problems.

Lothar
OS: Windows 7, Mac OS X (Panther/Leopard/Snow Leopard), SuSE Linux, Debian (PPC), OpenMoko FreeRunner
Compiler: OpenWatcom, GCC
wxWidgets 2.8.x
IDE Makefile based.
RAD My own brewed, Code generation with XSLT and DialogBlocks

blockn102
Experienced Solver
Experienced Solver
Posts: 70
Joined: Sat Oct 25, 2008 2:38 am

Post by blockn102 » Fri Aug 21, 2009 7:10 am

It's very hard problem :( An example:

Code: Select all

wxMenu menuPopup;
int i = 0;
menuPopup.Append(new wxMenuItem(&menuPopup, i, wxString::Format(wxT("Menu %d"), i)));
for(i = 1; i < 10; i++) {
    menuPopup.Append(new wxMenuItem(&menuPopup, i, wxString::Format(wxT("Menu %d"), i)));
}
The first menu append works good, but in "for" not works :( And another problems: events, menu icon...
??? year olds :)

Ridmix
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sat Dec 01, 2007 9:27 am

Post by Ridmix » Fri Aug 21, 2009 10:45 am

Code: Select all

// create menu on heap
wxMenu* menuPopup = new wxMenu;
for(int i = 0; i < 10; i++) {
    menuPopup->Append(i, wxString::Format(wxT("Menu %d"), i));
}
wxWidgets 2.8.10 & WX_TRUNK
Ubuntu 9.10
GCC 4.4

Post Reply