Exploring zetcode wxwidget tutorial on Menus and Toolbars:Simple Menu Example

#include “menu.h”

SimpleMenu::SimpleMenu(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

menubar = new wxMenuBar;
file = new wxMenu;
file1 = new wxMenu;
file->Append(wxID_EXIT,wxT(“&Hasta La Vista Baby”));
menubar->Append(file, wxT(“&File”));

menubar->Append(file1 , wxT(“&Something new”));
file1->Append(ID_JT_TEST,wxT(“&Test”));

//const int ID_JT_TEST = 101;
//const int ID_JT_HELLO = 102;
//const int ID_JT_BLAH_BLAH = 103;

SetMenuBar(menubar);

Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(SimpleMenu::OnQuit));
Centre();

}

void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}It seemed like I was in zetcode tutorial chapter First Programs for a while.  There was a lot of interesting stuff in there.  Next on the list is Menus and Toolbars. I was browsing through hard copy yesterday and I think this should go fairly quickly….

I really like this zetcode/wxwidgets tutorial insofar as everything works.
I want to work my way through this tutorial. So I’m going to attempt to speed things up here.. We’ll see what happens.
Main point.

To implement a menubar in wxWidgets we need to have three things. A wxMenuBar, a wxMenu and a wxMenuItem.

Got that….
I’m looking at menu.cpp and just looking at some of the documentation real quick to see if I want to try some experiments:

#include “menu.h”

SimpleMenu::SimpleMenu(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

menubar = new wxMenuBar;
file = new wxMenu;
file->Append(wxID_EXIT, wxT(“&Quit”));
menubar->Append(file, wxT(“&File”));
SetMenuBar(menubar);

Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(SimpleMenu::OnQuit));
Centre();

}

void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}

Menu items gets explored in the next tutorial, so I’m not going to mess with that..  I suppose I should add a things to the menu to make sure that the way I thinks this works actually is the way it works.
Hear’s a couple of things I experimented with:

//file->Append(wxID_EXIT, wxT(“&Quit”));
file->Append(wxID_EXIT);

In Linux anyway, there is no change in behavior.. You done need to add the wxT(“Quit”)
Now lets try this.

//file->Append(wxID_EXIT, wxT(“&Quit”));
file->Append(wxID_EXIT,wxT(“&Hasta La Vista Baby”));

Ok… Now this is interesting(I’m easily entertained:)). This behavior is not consistent with the behavior or wxbuttons. Actually I like this better.
With the buttons, if you tried this you would loose the icon. Here you don’t. The Hot Key &H worked, but there is a short apparent coming from the wxID_EXIT for the CTRL + Q shortcut..
Ok… the next logical experiment is to figure out how to change the short cut and see if we loose the icon.
But… Looking at the documentation I saw this:

NB: Please note that wxID_ABOUT and wxID_EXIT are predefined by wxWidgets and have a special meaning since entries using these IDs will be taken out of the normal menus under MacOS X and will be inserted into the system menu (following the appropriate MacOS X interface guideline). On PalmOS wxID_EXIT is disabled according to Palm OS Companion guidelines.

I’m not sure if what I’m seeing here is generalized behavior or something that’s special case. So while this is interestings it’s not that interestings so I’m moving on.

(On a side note. I gave myself a todo that I don’t want to deal with at the moment. Apparently, if a pulldown menu is active I can’t do a screen print. Obviously, there’s a trick I just don’t know it at the moment).

My next experiment was to add a item to the menubar and item under the sub-item….

The following will not give the desired result:
#include “menu.h”

SimpleMenu::SimpleMenu(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

menubar = new wxMenuBar;
file = new wxMenu;
file->Append(wxID_EXIT,wxT(“&Hasta La Vista Baby”));
menubar->Append(file, wxT(“&File”));

menubar->Append(file, wxT(“&Something new”));
file->Append(ID_JT_TEST,wxT(“&Test”));

//const int ID_JT_TEST = 101;
//const int ID_JT_HELLO = 102;
//const int ID_JT_BLAH_BLAH = 103;

SetMenuBar(menubar);

Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(SimpleMenu::OnQuit));
Centre();

}

void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}

What happens is that “Hasta La Vista Baby” and “Test” are duplicated under “File” and “Something new”.
Hmm… That’s sort of weird.. I guess need to jump ahead to the next tutorial to figure out what to do…
Back in a minute?
Ok… This gets me the desired result:

#include “menu.h”

SimpleMenu::SimpleMenu(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{

menubar = new wxMenuBar;
file = new wxMenu;
file1 = new wxMenu;
file->Append(wxID_EXIT,wxT(“&Hasta La Vista Baby”));
menubar->Append(file, wxT(“&File”));

menubar->Append(file1 , wxT(“&Something new”));
file1->Append(ID_JT_TEST,wxT(“&Test”));

//const int ID_JT_TEST = 101;
//const int ID_JT_HELLO = 102;
//const int ID_JT_BLAH_BLAH = 103;

SetMenuBar(menubar);

Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(SimpleMenu::OnQuit));
Centre();

}

void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}

Well, I sucked out as much as I’m going to in this sample… Onwards to submenus

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *