You can create a new menu and change or add menu items by using the Menu Editor dialog box (Macro → Menus, select a menu to edit or click New). Or, to create a new menu, use the Open Menu dialog box (Macro → Menus) and click New. A quick way to bind a pop-up menu to a mouse click is to use the Show button on the Open Menu dialog box while recording a macro. When you are finished recording the macro, the Key Bindings option screen (Window → SlickEdit Preferences → Keyboard → Key Bindings) is displayed which enables the binding of the new macro to a mouse click.
This section describes macro programming details about menus for advanced menu item enabling and for writing macros that manage menus. Topics are:
The Menu Editor dialog is used for editing menu resources. Use the Menu Editor to modify the SlickEdit MDI menu bar or an existing menu resource which can be displayed as a pop-up or menu bar.
To access this dialog, from the main SlickEdit® menu, select Macro → Menus, then click Open to open a menu for editing, or New to create a new menu. The New button on the Open Menu dialog box creates a new menu resource and places you in the Menu Editor so you can add menu items. After creating a menu, you can use the Show button on the Open Menu dialog box while macro recording to create a command which runs a menu by displaying it as a pop-up. If you bind the recorded command to a left or right button mouse event, the menu will be displayed at the cursor position.
You DO NOT need to specify key bindings for menu items because our Menu Editor automatically determines the key bindings for you. Use the Advanced Appearance option screen (Window → SlickEdit Preferences → Appearance → Advanced) to choose between short and long key names.
For information about each field and option on the Menu Editor dialog, see "Menu Editor dialog" in the SlickEdit Help → Index. See Creating and Editing Menu Resources for information on creating forms with menu bars or advanced information.
When you click the Alias button on the Menu Editor Dialog Box, the Menu Item Alias dialog is displayed. This dialog box allows you to define aliases (similar commands) for the command that is being executed. Enter each alias command on a separate line. If one of the alias commands is bound to a key, that key name will be displayed to the right of the menu item. For example, the e and edit commands are absolutely identically in function except that the e command requires fewer characters to type. The gui_open command is identical to the edit command except that it prompts the user with a dialog box, whereas the edit command prompts for files on the command line. These two examples illustrate the best reasons for using aliases. See also "aliases" in the SlickEdit Help → Index for more information.
For convenience, SlickEdit® has some predefined enable/disable attributes which you can specify for any command. When these predefined auto-enabling attributes are not enough, then you need to implement a callback which determines the enable/disable state of the command. See Creating and Editing Menu Resources for information on enabling and disabling menu items with your own callback. For information about each field and option on the dialog, see "Auto Enable Properties dialog" in the SlickEdit Help → Index.
Modified menus are stored in the state file vslick.sta (UNIX: vslick.stu) file. The easiest way to create or change a menu is to use the Open Menu dialog box (Macro → Menus). After you select the menu, the Menu Editor Dialog Box is displayed and you can edit the menu resource. After the menu is created, use the show, mou_show_menu, or _menu_show function to run the menu by displaying it as a pop-up window. The _menu_set method may be used to create a menu bar on a form. Another way to create or change a menu is to define or modify a menu resource. See the following topics:
Use the insert_object command to insert macro source code for a menu into the current buffer. Edit the resource properties and then run the macro to apply the resource changes. Ignore the message No main entry point if it is displayed. Changing a menu resource does not change any menu bars. Menu bars represent menu resources that have been loaded. A menu definition has the following format:
_menu menu_name {
submenu menu_item, help_command, help_message, categories {
menu_item, command, categories, help_command, help_message
}
submenu
}endsubmenu
}
The table below contains the menu items and their definitions:
|
Menu item |
Definition |
|
menu_item |
Menu item name in double quotes. Use & to choose selection character. |
|
command |
Any editor command. Places the cursor on the command line and press ? to list all editor commands. |
|
help_command |
Command to be executed when F1 is pressed. Usually it is a help or popup_imessage command. |
|
categories |
Specifies zero or more help categories in double quotes. Multiple help categories are separated with | (pipe). |
|
help_message |
A single line message in double quotes displayed on message line. |
Example of a menu definition:
_menu mymenu {
submenu "&File", "Help file menu", "Displays File drop-down menu", "ncw" {
"&New", "new", "ncw" , "help new", "Creates a new file to edit";
"&Open\tCtrl+O", "gui_open", "help gui_open", "Open a file";
}
submenu "&Edit", "Help edit menu", "Displays Edit drop-down menu","ncw" {
"Cu&t", "cut", "sel|nrdonly", "help cut", "Deletes the selection and copies it
to the clipboard";
}
}Predefined enabling or disabling attributes can be specified for any command. Specify these attributes in the name_info of a command definition. Auto-enabling attributes affects the enable/disable state for a command placed in a menu or in a toolbar. The following command is disabled when there is no editor control on which to operate:
#include slick.sh
_command void top_of_buffer()
name_info(',' VSARG2_READ_ONLY| VSARG2_REQUIRES_EDITORCTL)
{
}If the auto-enable attributes do not provide the features that you want, you can define the enable and disable callback for the command. The name of the callback function you define is based on the name of the command as shown in the following example:
#include "slick.sh"
static boolean gSomeOtherState;
/*
This function gets called if your command is used in a menu or toolbar.
You must return a combination of the MF_ flags ORed together.
BEWARE: If an _OnUpdate callback causes a Slick-C run-time error, you
may not see the error. In addition, the timer used for toolbars,
Context Tagging(R), AutoSave, and some other features may be
automatically terminated. Exit and restart the editor to restart
this timer. Use the "say" function to debug your _OnUpdate
callback.
*/
int _OnUpdate_mycommand(CMDUI &cmdui,int target_wid,_str command)
{
//say('h1');
// Lets assume this command requires the target to be an editor control
// with a selection.
// IF the target is not an editor control:
if ( !target_wid || !target_wid._isEditorCtl()) {
//say('disabled at h2');
return(MF_GRAYED);
}
//say('h3');
// IF the editor control does not have a selection:
if (!target_wid.select_active2()) {
//say('disabled at h4');
return(MF_GRAYED);
}
//say('h5');
if (gSomeOtherState) {
//say('disabled at h6');
return(MF_GRAYED);
}
//say('enabled at h7')
return(MF_ENABLED);
}
_command void mycommand() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
// Some code here...
}
// This command affects the enable/disable of mycommand.
_command void mycommand2(_str argument="0")
{
gSomeOtherState=(argument)?1:0;
// Indicate that the enable state of the toolbar buttons must be updated.
// The _tbSetRefreshBy function is very fast. Toolbars will be updated
// after the macro terminates and the user stops typing fast.
_tbSetRefreshBy(VSTBREFRESHBY_USER);
}The following sample code shows how to add a menu on a form as a menu bar:
#include slick.sh
// Create a form called form1 and set the border style to anything BUT
// BDS_DIALOG BOX. Windows does not allow forms with a dialog box style
// border to have menu bars.
defeventtab form1;
form1.on_load()
{
// Find index of MDI menu resource.
index=find_index(def_mdi_menu,oi2type(OI_MENU));
// Load this menu resource.
menu_handle=p_active_form._menu_load(index);
// _set_menu will fail if the form has a dialog box style border.
// Put a menu bar on this form.
_menu_set(menu_handle);
// You DO NOT need to call _menu_destroy. This menu is destroyed when
// the form window is deleted.
}
form1.on_init_menu()
{
// Gray out all menu items that are not allowed when there are no child windows.
_menu_set_state(p_menu_handle,!ncw,MF_GRAYED,C);
}If the show or mou_show_menu function meets your needs, use one of them. The following sample code shows how to display a menu as a pop-up:
#include slick.sh
defmain()
{
// Low-level code to display menu bar as pop-up.
// Could just use show or mou_show_menu function.
index=find_index(_mdi_menu,oi2type(OI_MENU))
if (!index) {
message(Can't find _mdi_menu);
}
menu_handle=_menu_load(index,P);
// Display this menu in the menu of the screen.
x=_screen_width()/2;y=_screen_height()/2;
flags=VPM_CENTERALIGN|VPM_LEFTBUTTON;
_menu_show(menu_handle,flags,x,y);
_menu_destroy(menu_handle);
}