PreviousTutorial - Adding and Customizing a Status Bar Tutorial - Adding an ActiveX ControlNext

Chapter 21: Tutorial - Adding and Customizing a Menu Bar and Toolbar

This tutorial takes you through adding the menu bar and toolbar control program to the sample Customer screenset supplied with Dialog System. You will notice that many of the steps for adding the menu bar are almost exactly the same as those for adding a status bar, as described in the previous tutorial.

You use the same steps to add a menu bar and toolbar to any window in any screenset. The same principles apply to adding any other control program to a screenset. You will find out how to:

Before starting this tutorial you should have:

The user control objects and control programs are also documented in:

A separate demonstration of using a toolbar control program is provided in the screenset tbards.gs, and is documented in the file tbards.txt in your DialogSystem\demo\tbards subdirectory.

21.1 Setting Up

  1. You will be using the Customer screenset, and making many changes to it, so we advise you to make a backup of customer.gs before starting.

  2. Start Net Express.

  3. Open customer.app in your DialogSystem\demo\customer subdirectory.

  4. Right-click on customer.gs in the left-hand pane, and select Edit from the context menu.

    Dialog System starts up and the Customer Details window is displayed.

21.2 Adding a Menu Bar and Toolbar to the Screenset

One of the most important features to note when using class library menus and toolbars is that each toolbar button has an associated menu item. When a toolbar button is selected, a menu event is generated by the class library for its associated menu item. Similarly, when a menu item is enabled, disabled, checked or unchecked, its associated toolbar button is also.

Before using any of the control programs, a common data area must be defined in the screenset Data Block. This is used to pass information between a screenset and the control programs at run time.

21.2.1 Defining the Data Items

Each user control that you define in a screenset must have a Data Block item (master field) associated with it. The data item must be defined as OBJ-REF as it is used to hold the class library object reference of the created control. The data item must be defined before you add the user control. If you do not have any object references defined in your screenset, then Dialog System will not let you define a user control.

Include the following data item in the Data Block of the screenset, to hold the object reference of the menu bar and toolbar:

MYTOOLBAR          OBJ-REF

If you have completed the previous chapter, Tutorial - Adding and Customizing a Status Bar, you will not need to include the following data definition for FUNCTION-DATA in the screenset Data Block:

FUNCTION-DATA                           1
  WINDOW-HANDLE                  C5     4.0
  OBJECT-REFERENCE               OBJ-REF
  CALL-FUNCTION                  X     30.0
  NUMERIC-VALUE                  C5     4.0
  NUMERIC-VALUE2                 C5     4.0
  SIZE-WIDTH                     C5     4.0
  SIZE-HEIGHT                    C5     4.0
  POSITION-X                     C5     4.0
  POSITION-Y                     C5     4.0
  IO-TEXT-BUFFER                 X    256.0
  IO-TEXT-BUFFER2                X    256.0

because FUNCTION-DATA is common to all the control programs, so you need define it only once in each screenset that uses the control programs.

If you do need to include the FUNCTION-DATA definition, you can import it from the file funcdata.imp in the DialogSystem\source subdirectory. Select File, Import, Screenset and click OK to acknowledge the currently loaded screenset might be overwritten. Click the File button, double-click funcdata.imp, click the Import button and then OK and Close.

Next you need to import the following:

TBAR-PARMS                             1              
      MENU-INDEX                   9      2.0           
      CALLBACK-ENTRY-NAME          X     32.0           
      ACCEL-FLAGS                  9      3.0           
      ACCEL-KEY                    9      3.0           
      MENU-TEXT                    X    256.0           
      MENU-HINT-TEXT               X    256.0           
      RESOURCE-FILE                X    256.0           
      RESOURCE-ID                  9      5.0           
      TOOL-TIP-TEXT                X    256.0           
      INSERT-BUTTON-BEFORE         9      2.0
      MSG-BOX-TEXT                 X    256.0

from the file tbardata.imp in the DialogSystem\source subdirectory in the same way as described above.

MYTOOLBAR will be used to store the object reference of the created toolbar. This data item will be the masterfield for the toolbar user control defined in the steps below.
TBAR-PARMS is a general purpose group used to pass information about menu items and toolbar buttons to the control program when using the toolbar functions.

To enable the class library, define CONFIG-FLAG and CONFIG-VALUE as C5 4.0 items in your Data Block, and then add the following dialog to the beginning of the SCREENSET-INITIALIZED event in the global dialog of the screenset:

    CLEAR-CALLOUT-PARAMETERS $NULL
    CALLOUT-PARAMETER 1 CONFIG-FLAG $NULL
    CALLOUT-PARAMETER 2 CONFIG-VALUE $NULL
    MOVE 15 CONFIG-FLAG
    MOVE 1 CONFIG-VALUE
    CALLOUT "dsrtcfg" 3 $PARMLIST

This dialog must be placed before any other dialog in the SCREENSET-INITIALIZED event.

21.2.2 Defining the Menu Bar and Toolbar

When you have defined the required data, define the user control:

  1. Select the Customer Details window, MAIN-WINDOW, to which you want to add the menu bar and toolbar.

  2. Select User control on the Object menu.
    - or -
    Select User Control on the Objects toolbar.

  3. Position and size the user control along the top of the window, where you would expect to see the menu and toolbar.

  4. Complete the following items on the User Control Properties dialog box:

  5. Click OK and save the screenset.

  6. Select the Generate copyfile button on the toolbar, and confirm you want to overwrite customer.cpb.

  7. Compile the generated control program using Rebuild All on the Net Express Project menu to obtain an executable version of this program.

21.3 Running Your Screenset

Once you have completed all these steps with your screenset, you can run it to see the User Control working.

At this stage, the menu bar and toolbar will not do anything useful.

You need to add dialog for the menu bar and toolbar to work correctly.

TBAR2 is the name of our customized version of the toolbar control program. This will be our controlling program for our menu.

To enable the customized version of the toolbar control to be called, add the following dialog at the end of the screenset's global dialog:

    CALLOUT-TBAR
	CALLOUT "TBAR2" 0 $NULL    

If you have not completed the previous chapter, you will need to add a WINDOW-CREATED event in Object Dialog. If you have completed the previous chapter, add the following dialog to the WINDOW-CREATED event:

    INVOKE MYTOOLBAR "show" $NULL

Save the screenset.

21.4 Defining a Menu Structure

Select Edit, Menu Bar or the Edit menu bar icon from the toolbar. The Menu Bar Definition dialog box appears.

Because we are going to to use the menu created by the control program, and not the one created by DSGRUN, select Options, and check Use Class Library. This ensures that DSGRUN does not itself create the menu.

21.4.1 Adding New Menu Options

As an example, we will add two new menu options, Edit and Options, either side of the existing Orders menu option. Edit will have one menu item, Select all. Options will have a menu item, Customize, which will open a submenu containing Fonts.

  1. Click on Orders, and select Edit, Insert before.

  2. In the Menu Bar Choice Details dialog box, enter EDIT in the Choice name field, and Edit in the Choice text field. Leave all the other fields as they are, and click OK.

    You can see that a new menu item, Edit, has been added, with a submenu choice of Edit.

  3. Click on the Edit submenu choice, and select Edit, Change.

  4. In the Pulldown Choice Details dialog box, change the Choice text field to be Select all, and enter "select all" in the Class Library hint text field. Choose F6 from the Shortcut key pulldown. Click OK.

  5. Click on Orders again, as in step 1, and this time, select Edit, Insert after.

  6. In the Menu Bar Choice Details dialog box, enter OPTIONS in the Choice name field, and Options in the Choice text field. Click OK.

    You can see that a new menu item, Options, has been added, with a submenu choice of Options.

  7. Click on the Options submenu choice, and select Edit, Change.

  8. In the Pulldown Choice Details dialog box, change the Choice text field to be Customize, and enter "customize" in the Class Library hint text field. Click OK.

  9. Click on Customize, and select Edit, Subpulldown.

    You can see that Customize now has a submenu choice of Customize.

  10. Click on this submenu choice, and select Edit, Change.

  11. In the Pulldown Choice Details dialog box, change the Choice text field to be Fonts, and enter "fonts" in the Class Library hint text field. Choose F9 from the Shortcut key pulldown. Click OK.

  12. Close the Menu Bar Definition dialog box, and save the screenset.

  13. Double-click on the toolbar to open the User Control Properties dialog box. Select Toolbar from the Control type pulldown, and click Generate.

    A message box appears, asking you if you want to generate a class library menu structure. Click Yes.

    A second message box appears, asking you if you want to overwrite the entire toolbar program. Click No so that only the menu definitions are generated, leaving any toolbar changes intact.

    A message box appears, reminding you that you need to include the necessary entries in the Data Block for use by the generated control program. You have done this in the earlier steps, so click OK to continue.

    You can see the generated program being added to your Customer project in the background.

  14. Click Cancel.

  15. Select Rebuild All on the Project menu.

  16. Run the application using the Net Express Animate, Run menu choice.

    At this stage, you can see the menu options you have added, but they will not do anything if you select them.

21.5 Defining a Toolbar Structure

Look at the data structure defined in tbar2.cbl as an example of how to create your own toolbar. To do this, from within tbar2.cbl, select the Net Express Search, Locate Definition menu choice to locate the data item "bData".

21.5.1 The Existing Toolbar Structure

The bData table structure defines an ordered list of buttons to be added to the toolbar. Buttons are added to the toolbar in the order in which they appear in the list.

An example of a single button record is shown below:

  *>----------------------------------------------------------------
*  Button object reference.
   03 object reference. 

*  Menu item index to associate button with or zero if this
*  button is to be a separator. This index refers to an index
*  within the mData table.
   03 pic x comp-5 value 2. *> "Exit" menu item

*  Resource ID of button bitmap.
   03 pic x(4) comp-5 value IDB-EXIT.

*  Tool tip to be displayed when mouse is over button.
   03 pic x(bStringSize) value z"Quit this application".
*>---------------------------------------------------------------- 

Each element of the table defines a button on the toolbar and is made up of four parts:

  1. Object reference of the created toolbar button.
  2. The index (within mData table) of the menu item to associate the button with (or zero for a separator).
  3. Resource ID of a bitmap within a resource file to use for the button.
  4. Tool tip to be displayed when mouse is over the button.

The default toolbar structure that has been generated contains a separator and a button associated with menu index 2. However, as we have generated a menu structure from the existing Dialog System menu, the index of the File, Exit menu item is in fact 7. Therefore, to associate the second button with the correct menu index, change it from 2 to 7.

21.5.2 Adding New Toolbar Buttons

As an example, we will add two new toolbar buttons for Select All and Fonts.

To change the toolbar defined in tbar2.cbl, use the existing button records as an example to change the button structure.

  1. Duplicate the second element of the bData table structure that represents the Exit button, immediately below the existing one. We will now edit the new, duplicated, code.

  2. Change the index of the menu item from 7 to 9. Change the comment as appropriate.

  3. Change the resource ID of the button bitmap from IDB-EXIT to IDB-BLUE.

  4. Change the tool tip from "Quit" to "Select all".

  5. Duplicate the lines of code used for the second toolbar button, immediately below that one.

  6. Change the index of the menu item from 9 to 14. Change the comment as appropriate.

  7. Change the resource ID of the button bitmap from IDB-BLUE to IDB-RED.

  8. Change the tool tip from "Select all" to "Fonts".

  9. Select Rebuild All on the Project menu.

  10. Run the screenset.

    You can now see the two buttons you have just created displayed below the menu items in the toolbar.

    Again, the toolbar buttons will not do anything if you select them.

21.6 Customizing the Toolbar

In the previous section we added an existing bitmap button from the file tbresid.cpy. In this section, we will create a new button bitmap.

  1. Add a new resource file to the project and create bitmap resources the toolbar will use.

  2. Make copyfile changes to define the toolbar structure.

    We are going to review the toolbar structure in tbar2.cbl which provides a menu item and associated toolbar button. This file contains data structures that define how the toolbar will look and respond to events.

    We will look at how to define the toolbar.

    Ensure that the button definition data record contains a reference to an IDB_ resource identifier you defined in the resource file earlier. That is, IDB-Button1 and IDB-Button2.

    Save tbar2.cbl and close it.

  3. Add dialog to the screenset to respond to a menu selection as follows:

Save the screenset, switch to the Net Express IDE and rebuild the project.

You can now select Animate, Run, to test your screenset. When you select either Select All from the Edit menu, or the toolbar button, the dialog procedure that you coded will be executed. Try this for Fonts, from the Options, Customize menu too.


Copyright © 2000 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousTutorial - Adding and Customizing a Status Bar Tutorial - Adding an ActiveX ControlNext