It was time to add a simple menu to my Window. I had worked with menus before so I am somewhat familiar with it. To give you an idea on how a menu setup would look I have the below code for you to look at.

struct NewMenu mynewmenu[] =
{
{ NM_TITLE, (STRPTR)”Project”, NULL, 0, 0, NULL},
{ NM_ITEM, (STRPTR)”New”, (STRPTR)”N”, 0, 0, NULL},
{ NM_ITEM, (STRPTR)”Open”, (STRPTR)”O”, 0, 0, 0},
{ NM_ITEM, NM_BARLABEL, 0, 0, 0, 0},
{ NM_ITEM, (STRPTR)”Save”, (STRPTR)”S”, 0, 0, 0},
{ NM_ITEM, NM_BARLABEL, 0, 0, 0, 0},
{ NM_ITEM, (STRPTR)”About…”, (STRPTR)”?”, 0, 0, 0},
{ NM_ITEM, NM_BARLABEL, 0, 0, 0, 0},
{ NM_ITEM, (STRPTR)”Quit…”, (STRPTR)”Q”, 0, 0, 0},
{ NM_END, NULL, 0, 0, 0, 0},
};

enum
{
PROJECT_MENU,
};

enum
{
PROJECT_MENU_NEW,
PROJECT_MENU_OPEN,
PROJECT_MENU_SAVE,
PROJECT_MENU_BAR1,
PROJECT_MENU_ABOUT,
PROJECT_MENU_BAR2,
PROJECT_MENU_QUIT
};

The first part is defining our menu options. We need to number our menu options so that we can use them. In other words they need to have a value. We use “enum” to number them. In this example we have just one menu title called (PROJECT_MENU) Project. To check for menu selections we use the below code snippet.

case PROJECT_MENU:

switch (itemNum)
{

case PROJECT_MENU_NEW:
printf(“You selected NEW\n”);
break;

case PROJECT_MENU_OPEN:
printf(“You selected OPEN\n”);
break;

It is of course not a complete piece of code but it gives you the idea on how it would work. At my first attempt it did not do what it was suppose to do. For each selection I printed what you selected but while the first two did print the correct text the rest was all mixed up. Selecting “About” would print “Quit” for example. After some time I figured out that I forgot to add the Bar labels in the numbering. A Bar label separates menu option with a line.

Menu with no Bar labels

Menu with no Bar labels

Menu with Bar labels

Menu with Bar labels

With Bar labels it looks less cluttered. Once I added them to the numbering all my menu options did print the correct text.

PROJECT_MENU_BAR1,
PROJECT_MENU_BAR2,

The above defines will add the Bar labels to our “project” menu. With the menu bit added, while still being rather simplistic, it is time to have a more in depth look at loading and saving text. I will be talking about this next time.

Advertisements