< Lesson 6: Simple Requester  |  Index  |  Lesson 8: File Requesters >


Lesson 7:  Simple Menu


This lesson will explain how to make a simple menu.

001   #include <exec/types.h>
002   #include <intuition/intuition.h>
003   #include <libraries/gadtools.h>
004
005   /* Prototypes for system functions. */
006   #include <proto/exec.h>
007   #include <proto/intuition.h>
008   #include <proto/gadtools.h>
009
010   /* ANSI C includes. */
011   #include <stdio.h>
012
013   struct IntuitionBase *IntuitionBase = NULL;
014   struct Library *GadToolsBase = NULL;
015
016   struct NewMenu window_menu[] =
017   {
018      { NM_TITLE, "Project",     0 , 0, 0, 0, },
019      {  NM_ITEM, "Open...",    "O", 0, 0, 0, },
020      {  NM_ITEM, "Save",       "s", 0, 0, 0, },
021      {  NM_ITEM, "Save As...", "S", 0, 0, 0, },
022      {  NM_ITEM, NM_BARLABEL,   0,  0, 0, 0, },
023      {  NM_ITEM, "Quit",       "Q", 0, 0, 0, },
024      {   NM_END, NULL,          0,  0, 0, 0, },
025   };
026
027   #define PROJECT_MENU          0
028   #define PROJECT_MENU_OPEN     0
029   #define PROJECT_MENU_SAVE     1
030   #define PROJECT_MANU_SAVE_AS  2
031   #define PROJECT_MENU_QUIT     4
032
033   /* Macro Definitions. */
034   #define Get_Window_Signal(x) (1L << (x)->UserPort->mp_SigBit)
035
036
037   int main( void )
038   {
039      struct Window *window;
040
041      /* Open the intuition.library. */
042      IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 0L );
043
044      if ( IntuitionBase )
045      {
046         /* Open version 37 or greater of the gadtools.library. */
047         GadToolsBase = OpenLibrary( "gadtools.library", 37L );
048
049         if ( GadToolsBase )
050         {
051            /* Open the window. */
052            window = OpenWindowTags( NULL,
053                              WA_Width, 200,
054                              WA_Height, 100,
055                              WA_MinWidth, 100,
056                              WA_MinHeight, 50,
057                              WA_MaxWidth, 600,
058                              WA_MaxHeight, 300,
059                              WA_Title, (ULONG)"Window",
060                              WA_ScreenTitle, (ULONG)"Menu Example",
061                              WA_DepthGadget, TRUE,
062                              WA_CloseGadget, TRUE,
063                              WA_SizeGadget, TRUE,
064                              WA_DragBar, TRUE,
065                              WA_Activate, TRUE,
066                              WA_NewLookMenus, TRUE,
067                              WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
068                              TAG_END);
069
070            if ( window )
071            {
072               BOOL window_opened = TRUE;
073               ULONG window_signal = 0L;
074               APTR *visual_info;
075
076               /* Get the window's signal bit. */
077               window_signal = Get_Window_Signal( window );
078
079               /* Get the visual info structure for the window's screen. */
080               visual_info = GetVisualInfo( window->WScreen, TAG_END );
081
082               if ( visual_info )
083               {
084                  struct Menu *menu;
085
086                  /* Create the menu. */
087                  menu = CreateMenus( window_menu, GTMN_FrontPen, 1, TAG_END );
088
089                  if ( menu )
090                  {
091                     BOOL menu_layout;
092
093                     /* Layout the menu. */
094                     menu_layout = LayoutMenus( menu, visual_info, TAG_END );
095
096                     if ( menu_layout )
097                     {
098                        BOOL menu_attached;
099
100                        /* Attach the menu to the window. */
101                        menu_attached = SetMenuStrip( window, menu );
102
103                        if ( menu_attached )
104                        {
105                           UWORD menu_number;
106                           UWORD selected_menu;
107                           UWORD selected_item;
108                           UWORD selected_sub_item;
109                           struct MenuItem *menu_item;
110
111                           /* Since the menu was attached successfully we can begin
112                            * processing the window's messages.
113                            */
114
115                           while ( window_opened )
116                           {
117                              struct IntuiMessage *message;
118                              UWORD msg_code;
119                              UWORD msg_class;
120
121                              /* Wait for a signal from the window. */
122                              Wait( window_signal );
123
124                              /* There may be more than one message waiting. */
125                              while ( message = (struct IntuiMessage *)GetMsg( window->UserPort ) )
126                              {
127                                 /* Copy the necessary information from the message. */
128                                 msg_code = message->Code;
129                                 msg_class = message->Class;
130
131                                 /* Reply as soon as possible. */
132                                 ReplyMsg( (struct Message *)message );
133
134                                 /* Take the proper action in response to the message. */
135                                 switch ( msg_class )
136                                 {
137                                    case IDCMP_CLOSEWINDOW: /* User pressed the close gadget. */
138                                       window_opened = FALSE;
139                                       break;
140                                    case IDCMP_MENUPICK:
141                                       /* The message code contain the menu number. */
142                                       menu_number = msg_code;
143
144                                       /* This is the menu handler. */
145                                       while ( menu_number != MENUNULL )
146                                       {
147                                          /* Get the info structure for the selected menu item. */
148                                          menu_item = ItemAddress( menu, menu_number );
149
150                                          /* Get the details on the current menu item. */
151                                          selected_menu = MENUNUM( menu_number );
152                                          selected_item = ITEMNUM( menu_number );
153                                          selected_sub_item = SUBNUM( menu_number );
154
155                                          /* Close the window if the Quit menu option is selected. */
156                                          if ( ( selected_menu == PROJECT_MENU ) && ( selected_item == PROJECT_MENU_QUIT ) )
157                                          {
158                                             window_opened = FALSE;
159                                          }
160
161                                          menu_number = menu_item->NextSelect;
162                                       }
163                                       break;
164                                    default:
165                                       break;
166                                 }
167                              }
168                           }
169
170                           /* Detach the menu from the window. */
171                           ClearMenuStrip( window );
172                        }
173                        else
174                        {
175                           printf( "Unable to attach menu to window.\n" );
176                        }
177
178                        /* Free the menu structures. */
179                        FreeMenus( menu );
180                     }
181                     else
182                     {
183                        printf( "Unable to layout menus.\n" );
184                     }
185                  }
186                  else
187                  {
188                     printf( "Unable to create menu.\n" );
189                  }
190
191                  /* Free the visual info structure. */
192                  FreeVisualInfo( visual_info );
193               }
194               else
195               {
196                  printf( "Unable to obtain visual info structure.\n" );
197               }
198
199               /* Close the window. */
200               CloseWindow( window );
201            }
202            else
203            {
204               printf( "Unable to open window.\n" );
205            }
206
207            /* Close the gadtools.library. */
208            CloseLibrary( GadToolsBase );
209         }
210         else
211         {
212            printf( "Unable to open version 37 or later of the gadtools.library.\n" );
213         }
214
215         /* Close the intuition.library. */
216         CloseLibrary( (struct Library *)IntuitionBase );
217      }
218      else
219      {
220         printf( "Unable to open intuition.library.\n" );
221      }
222

223      return 0;
224  }


 

< Lesson 6: Simple Requester  |  Index  |  Lesson 8: File Requesters >

If you find any errors or have any questions or comments please e-mail me.

Last updated on Monday, January 23, 2006 18:41