As you might expect, creating a menu hierarchy in a frame definition is a matter of defining a series of panes for the frame. At the top-most level in the menu hierarchy is the menu bar itself. The menu bar contains each menu defined for the application and each menu contains the menu commands that themselves perform operations. Once the panes have been defined, the menu bar needs to be included in the frame using the menu-bar clause.
First of all, you can create a pane that defines the menu bar itself as follows:
pane task-menu-bar (frame)
make(<menu-bar>,
children: vector(frame.file-menu,
frame.edit-menu,
frame.task-menu,
frame.help-menu));
Next, define the File and Tasks menus themselves:
pane file-menu (frame)
make(<menu>, label: "File",
children: vector(frame.open-menu-button,
frame.save-menu-button,
frame.save-as-menu-button,
frame.exit-menu-button));
pane edit-menu (frame)
make(<menu>, label: "Edit",
children: vector(frame.cut-menu-button,
frame.copy-menu-button,
frame.paste-menu-button));
pane task-menu (frame)
make(<menu>, label: "Task",
children: vector(frame.add-menu-button,
frame.remove-menu-button));
pane help-menu (frame)
make(<menu>, label: "Help",
children: vector(frame.about-menu-button));
Finally, you need to define the menu commands themselves. A command that appears on a menu is defined as an instance of <menu-button>, and so there is a strong similarity between these buttons and some of the buttons already defined. DUIM also generates mnemonics for each menu item; thus, the items appear as File and Edit, and so forth. (Note that the make-keyboard-gesture function that appears below is defined in Section 4.2.2, "Keyboard accelerators".)
// Commands in the File menu
pane open-menu-button (frame)
make(<menu-button>, label: "Open...",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"o", #"control"),
documentation: "Opens an existing file.");
pane save-menu-button (frame)
make(<menu-button>, label: "Save",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"s", #"control"),
documentation: "Saves the current file to disk.");
pane save-as-menu-button (frame)
make(<menu-button>, label: "Save As...",
activate-callback: save-as-file,
documentation: "Saves the current file with a new name.");
pane exit-menu-button (frame)
make(<menu-button>, label: "Exit",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"f4", #"alt"),
documentation: "Exits the application.");
//Commands in the Edit menu
pane cut-menu-button (frame)
make(<menu-button>, label: "Cut",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"x", #"control"),
documentation: "Cut the selection to the clipboard.");
pane copy-menu-button (frame)
make(<menu-button>, label: "Copy",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"c", #"control"),
documentation: "Copy the selection to the clipboard.");
pane paste-menu-button (frame)
make(<menu-button>, label: "Paste",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"v", #"control"),
documentation: "Paste the selection in the clipboard
at the current position.");
//Commands in the Task menu
pane add-menu-button (frame)
make(<menu-button>, label: "Add...",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture
(#"a", #"control", #"shift"),
documentation: "Add a new task.");
pane remove-menu-button (frame)
make(<menu-button>, label: "Remove",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture
(#"d", #"control", #"shift"),
documentation: "Remove the selected task from the list.");
//Commands in the Help menu
pane about-menu-button (frame)
make(<menu-button>, label: "About",
activate-callback: not-yet-implemented,
accelerator: make-keyboard-gesture(#"f1"),
documentation:
"Display information about the application.");
Once you have defined the menu bar and all the children that it is to contain, you need to activate the menu bar in the frame by including the following towards the end of the frame definition.
menu-bar (frame) frame.task-menu-bar;
The definitions of these menu buttons demonstrate two interesting new features: the use of keyboard accelerators, and the use of documentation strings.