Creating Dialog Boxes

This chapter contains the following topics:

Dialog Editor Summary

To edit a dialog box that is being run, press Ctrl+Shift+Space or right-click on the top of a form and select Edit. If you press Ctrl+Shift+Space while the Properties dialog box is active, you edit the Properties dialog box. Double-click the system menu to close the edited Properties form. Some UNIX window managers do not close windows when you double-click on the system menu.

Adding and Deleting Controls

The bitmaps on the left of the Properties dialog box are used to create controls. Hover over a bitmap to display the function of a bitmap. There are two methods for creating a control. The first method is to double-click the left mouse button on the bitmap of the control that you want to create. This places a new control in the middle of the selected form.

The Picture Box and Frame controls enable you to place controls inside of them. To do so, select Window → Properties or, use the show_properties command.

To use the other method for creating a control, complete the following steps:

  1. Single-click on the Text Box bitmap.

  2. Move your mouse so that it appears on top of the form that you are editing. If you cannot see the form that you are editing, display it by selecting Window → Selected Form.

  3. To create the text box control, click the left mouse button, and, while holding it down, move the mouse pointer to the right to create a dotted rectangle. When you release the mouse, the text box control is displayed within the rectangle.

To delete a control, select the control(s) to remove, then press Backspace or Delete.

Setting Properties

To set properties, complete the following steps:

  1. Select the control. Left-click the mouse button on the property in Properties list box.

  2. Type the new value in Properties combo box. Press Enter when the Properties list box is active to set the property.

  3. Select the control. Double-click the left mouse button on the property in the Properties list box to go to the next value of the property. For color and picture properties, a dialog box is displayed.

Aligning Controls

Select the control with which you want to align the other controls. Select the other controls with Shift+ LButton. Double-click the left mouse button on one of the properties x or y to align the controls in the x or y direction. Press Enter on the value in the Properties combo box.

Sizing Controls

To size controls, use one of the following methods:

  • To size a single control, select the control and click and drag one of the selection handles with the left mouse button.

  • To size multiple controls, select the controls and set the width or height property.

  • To size multiple controls, select the controls and press Shift+Left, Shift+Right, Shift+Up, or Shift+Down to move the lower right corner of the selected controls by one pixel.

Moving Controls

To move controls, use one of the following methods:

  • Select the control(s), then click and drag with the left mouse button.

  • Select the control(s), then set the x or y property.

  • Select the control(s), then press the Left, Right, Up, or Down arrow key to move the selected controls by one pixel.

Miscellaneous Assignments When the Form is Active

The table below shows a list of miscellaneous button and key assignments that can be used when the form is active.

Assignment

Action

Right mouse click

Displays menu with various dialog editor commands.

Ctrl+Shift+Space

Loads form and Slick-C® code. Runs dialog box. If you accidentally press Ctrl+Shift+Space when in the Properties dialog box, you will be editing the Properties dialog box. Double-click on the system menu to close the edited Properties form. Some UNIX window managers do not close windows when you double-click on the system menu.

Ctrl+S

Loads form and saves into state file. Under UNIX, this may just list source for the form that can be executed.

Ctrl+L

Loads form.

Ctrl+C

Copies selected controls.

Ctrl+V

Pastes controls from the clipboard.

Ctrl+X

Cuts selected controls.

Ctrl+A

Selects all controls with same parent as the already selected control(s).

Tab

Deselects all controls and selects next control in tab order (p_tab_index).

Shift+Tab

Deselects all controls and selects previous control in tab order.

Left mouse click

Double-click (on control) displays Select an Event Function dialog box for adding or modifying event handlers.

Miscellaneous Menu Items

The table below shows the miscellaneous menu items.

Menu Item

Description

System Box of form, Show Properties

Display Properties dialog box.

Window → Properties

Display Properties dialog box.

Window → Selected Form

Display selected form (form being edited).

Macro → New Form

Creates a new dialog box with a default name.

Macro → Open Form

Open existing dialog box or create new dialog box.

Macro → Grid

Sets the distances between the dots on edited form.

Creating a Form

A form is the outer window of a dialog box. The objects within the dialog box are called controls. The form also refers to the entire dialog box. A new form can be created by using one of the following methods:

  • Use the New Form menu item (Macro → New Form).

OR

  • Use the Open Form menu item (Macro → Open Form) and specify the name of a new form.

Saving a Form

Click on the form being edited and press Ctrl+S.

Adding Event Handlers

Set the form name and the control names (name property in Properties list box) before adding code to the dialog box because these names are referenced in the code. Prefix your control names using the letters ctl so that they are easily recognizable. To add an event handler, complete the following steps:

  1. Double-click on the control in the dialog box for which you want to add code (not the bitmap in the Properties dialog box). The Select An Event dialog box is displayed.

  2. Select an event and click OK. If this is the first event handler for this dialog box, you will be prompted with an Open dialog box for a new file to contain the source code for this dialog box.

  3. Type a unique file name. Usually this file name is derived from the name of the dialog box you are creating, such as form1.e.

After performing the above steps, the dialog editor inserts an event function definition into your source file and places your cursor in the function.

Inherited Code Found Dialog Box

This dialog box is displayed when there is no code for the event you have chosen and the control is using an inherited event table. You will see this dialog box if you copy a control with existing code, paste elsewhere and then double-click on the new instance of the control.

The following options are available on the dialog:

  • Inherit code - When this option is selected, a statement which links a new event event table to an inherited event table (event table not belonging to the control and possible copied through the clipboard). This afffects user level 1 inheritance code (p_eventtab) only.

  • Go to inherited code - When this option is selected, no code is inserted. The cursor is placed on the existing inherited event handling code.

  • Don't inherit code - Select this option when you do not want to inherit the existing user level 1 inheritance code (p_eventtab). Sometimes when you copy a control with existing code to the clipboard, you will not want to inherit the existing event handlers.

Loading and Running the Form

To run the current dialog box that you are editing, click Macro → Load and Run Form or use the run_selected command. This loads the code, loads the dialog box, and runs the dialog box. To close the dialog box, double-click on the system menu (some UNIX window managers do not close windows when you double-click on the system menu) or press Ctrl+Shift+Space (in the running version of your dialog box and not the edited copy). Press Ctrl+Shift+Space when any dialog box is running to edit it (this includes the Properties dialog box).

Display the dialog box from the command line by typing show <FormName>. To display the dialog box modally enter show -modal <FormName> on the command line. For more information about this command, see Displaying Dialog Boxes. Dialog box templates and compiled macros are stored in the state file vslick.sta (UNIX: vslick.stu).

The example code below shows how to write a command that displays a dialog box. This is used when binding a command to a key that displays a dialog box.

#include "slick.sh"
_command void run_form1()
{
    // The -modal option displays other windows while the dialog box
    // is displayed.
    show("-modal form1");
}

Adding a Cancel Button

To add a Cancel button, complete the following steps:

  1. Double-click Insert Button Control.

  2. Set the caption property to Cancel.

  3. Set the cancel property to TRUE by double-clicking the left mouse button on the cancel property in the Properties list box.

  4. Set the name property of a control (never the form) to "" if you are not going to reference the control by name.

  5. Clicking Cancel when your dialog box is running will close the dialog box even though you have not written any code. If you do add code to your Cancel button, you must close the dialog box by typing the following in the command line: p_active_form._delete_window();

Adding an OK Button and Closing a Dialog Box

To add an OK button and close a dialog box, complete the following steps:

  1. Create a command button control by double-clicking Insert Button Control in the Properties dialog box.

  2. Set the caption property to OK, set the default property to TRUE, and set the name property to ctlok.

  3. Double-click on the command button control in the dialog box for which you want to add code (not the bitmap in the Properties dialog box). The Select An Event dialog box is displayed.

  4. Choose the lbutton_up event and click OK.

  5. If this is the first event handler for this dialog box, the Open dialog box for a new file to contain the source code for this dialog box is displayed. Type a unique file name. Usually this file name is derived from the name of the dialog box that you are creating, such as form1.e.

After completing the previous steps, the dialog editor inserts an event function definition into your source file and places your cursor in the function. Add the code as shown in the following example:

#include "slick.sh"
defeventtab form1
// Code for OK button.
ctlok.lbutton_up()
{
    // Close the dialog box and return a value. The _delete_window 
    // function allows modal dialog boxes to return a value. For
    // more information, see "Displaying Dialog Boxes"  below. Each object 
    // in the dialog box will receive an on_destroy event.
    // NOTE: If "" is a valid return value. Return 1 here and store
    // your results in the global _param1 variable.
    p_active_form._delete_window("return value");
    // Statements after closing a dialog box are executed.
}

Before closing a dialog box, review the following information:

  • First, if a modal dialog box returns a value, the value "" (zero length string) MUST be returned to indicate that the dialog box has been canceled. This convention is used so that when running a dialog box, you can press Ctrl+Shift+Space to safely cancel and edit the dialog box.

  • Use the global container variables_param1.._param10 to return multiple strings. Alternately, you can make an array or structure and place it in _param1. If you do place your string results in the global variables _param1.._param10, make sure your dialog box returns 1 (or any value other than "") to indicate that the dialog box was not canceled.

Displaying Dialog Boxes

The show command is called in function-style syntax from within a macro. It can also be invoked from the command line or a menu item.

The command line call syntax is:

        
    show cmdline 
      
      

The function call syntax is:

        
    show (cmdline [,arg1 [,arg2 ... [argN]]])
      
      

cmdline is a string in the format:

        
    [option] form_name
      
      

option can be one of the options in the table below.

Option

Description

-mdi

Keep the form on top of the MDI window.

-app

Keep the form on top of the SlickEdit® application window. This allows the MDI window to be displayed on top of the form.

-xy

Restore the previous x,y position of the dialog box. If the old position cannot be found, the dialog box is centered. When the dialog box is closed, the x,y position is automatically saved (the dialog manager calls _save_form_xy).

-hidden

Do not make the form visible. Run the form modally. All other forms are disabled. Control returns to the caller when the form window is deleted with _delete_window.

-nocenter

Do not center the form.

-new

Normally, when a form is already displayed, the existing form is given focus. This option allows for multiple instances of a form to be displayed.

-reinit

(UNIX only) This option causes the _delete_window function to make the form invisible instead of deleting the form. The destroy events are dispatched even though no windows are actually destroyed. Next time show is called for the same dialog box, the invisible dialog box is made visible, some properties are reinitialized, and the create events are sent. Be careful when using this option. Not all dialog boxes can use this option without minor modifications. The form_parent() function does not work because the next time the form is used, the parent is not changed to the new parent specified.

-hideondel

(UNIX only) This option is the same as the -reinit option except no properties are reinitialized when the invisible dialog box is shown again.

form_name specifies a form or menu resource. If it is an integer, it must be a valid index into the names table of a form or menu. Otherwise, it should be the name of an existing form or menu that can be found in the names table.

on_create and on_load Events

The array of args (arg1...argN) is passed to on_create.When a dialog box and all its objects are created, each object receives an on_create event. The on_create event receives the arg1, arg2,...,argN arguments given to the show function. After the on_create events are sent, the form receives an on_load event. You CANNOT set the final focus in an on_create event. Use the _set_focus function during the on_load event to set the initial focus to a control other than the control with lowest tab index (p_tab_index) that is enabled and visible.

Return Value of show

If the -modal option is given, the return value given to _delete_window is returned. "" is returned if the dialog box is edited or destroyed during an on_create event. Use the global variables _param1..._param10 to return more than one string value. Alternately, you can make an array or structure and place it in _param1 for non-string return types.

If the -modaloption is not given, the form window id is returned if successful. Otherwise, a negative error code is returned.

Example:

// This example requires that you create a form called form1 with a 
// command button and load this file.
#include "slick.sh"
_command mytest()
{
    result=show("-modal form1");
    if (result=="") {
       return(COMMAND_CANCELLED_RC);
    }
    message("_param1="_param1" _param2="_param2);
}
 
defeventtab form1
ctlcommand1.on_create()
{
    // Global variable _param1.._param10 are defined in "slick.sh" to
    // allow for multiple strings to be returned in separate variables.
    // Alternatively, if the return strings do not contain spaces, you
    // could concatenate them together with a space and use the parse 
    // built-in to easily separate them.
    _param1="string1";
    _param2="string2";
    // Close the dialog box and indicate that the dialog box was not canceled.
    // Each object in the dialog box will receive an on_destroy event.
    p_active_form._delete_window(1);
}

Example:

// This example requires that you create a form called form1 with a
// command button and load this file.
#include "slick.sh"
_command void mytest()
{
    show("-modal form1","param1 to on_create", "param2 to on_create");
}
 
defeventtab form1
ctlcommand1.on_create(_str arg1="", _str arg2="")
{
    _str tmpArg1 , tmpArg2;
   tmpArg1=arg(1);
   tmpArg2=arg(2);
   _message_box("arg1="arg1" arg2="arg2);
   _message_box("tmpArg1="tmpArg1" tmpArg2="tmpArg2);
}

Example:

#include "slick.sh"
defmain()
{
    index=find_index("form1",oi2type(OI_FORM));
    if (!index) {
       messageNwait("form1 not found");
       return(1);
    }
    // Can specify name table index instead of name. When show is called
    // without the "-modal" option, the positive window id (instance handle)
    // of the form created is returned.
    form_wid=show("-hidden -nocenter "index);
    if (form_wid<0) {
       return(1);
    }
    // Place the form at the top left corner of the display.
    form_wid.p_x=form_wid.p_y=0;
    // Make the form visible.
    form_wid.p_visible=1;
    return(0);
}

Modal and Modeless Dialog Boxes

If you do not want the MDI window or any other form to get focus when your dialog box is displayed, specify the -modaloption to the show command (see Displaying Dialog Boxes). When the -modal option is given, other forms, including the MDI window, are disabled (p_enabled=0) until the form is closed. In addition, the _delete_window function can be used to return a value (see the previous example).

Modeless example:

#include "slick.sh"
defmain()
{
    // When show is called without the "-modal" option, the positive
    // window id (instance handle) of the form created is returned.
    form_wid=show("-hidden -nocenter form1");
    if (form_wid<0) {
       return(1);
    }
    // Place the form at the top left corner of the display.
    form_wid.p_x=form_wid.p_y=0;
    // Make the form visible.
    form_wid.p_visible=1;
    return(0);
}

If you need to display a status dialog box during processing, you might require a modeless dialog box so control is returned to you. However, it is a best practice to disable all other dialog boxes including the MDI window during processing.

Advanced modeless example:

#include "slick.sh"
static typeless gcancel;
_command void test()
{
    // Show the form modeless so there is no modal wait.
    form1_wid=show("form1");
    // Disable all forms by the one with p_window_id==form_wid. A space-
    // delimited string of disabled form window ids is returned.
    disabled_wid_list=_enable_non_modal_forms(0,form_wid);
    gcancel=0;
    for (;;) {
       // Read mouse, key, and all other events until none are left
       // or until the variable gcancel becomes true.
       process_events(gcancel);
       if (gcancel) break;
       // Do your processing here.
    }
    // Enable the forms that were disabled.
    enable_non_modal_forms(1,0,disabled_wid_list);
    form1_wid._delete_window();
}
defeventtab form1;
ctlcancel.lbutton_up()
{
    gcancel=1;
}

Dialog Box Parent Window

The parent window of a dialog box form has two uses. First, the dialog box remains on top of the parent window. Use the show command and specify the -app option if you want to allow a modeless dialog box be displayed behind the MDI window. The -mdi option of the show command can be used to make sure a dialog box stays on top of the MDI window.

Command line examples:

      
    show -app _calc_form
    show -mdi -new _calc_form
    
    

Second, the parent window is used by some dialog boxes (such as the Print and Spelling dialog boxes) to determine on which buffer to operate. This permits the dialog boxes to support the editor control. To do this, they call the _form_parent function during an on_create event to get the window id of the window which contains the buffer to be operated on. These dialog boxes only support certain parent windows. For example, the Print dialog box will not run correctly if the -app option of the show command is used.

Remembering a Dialog Box's Previous Position

The show command centers the dialog box to the current form or MDI window. Usually this is fine, but sometimes it is helpful for a dialog box to reappear in the same position that it was in when the user closed the dialog box. To do this, specify the -xy option to the show command. This adds the IS_SAVE_XY flag to the p_init_style property. When the dialog box is closed, the x and y position of the dialog box is stored and later saved in the auto restore file (vrestore.slk by default) when you exit the editor. The form is centered if the old x,y position information cannot be found.