Class XbpMenuBar() Foundation
Class function of the XbpMenuBar class.
The XbpMenubar class provides objects which display and manage horizontal menus (menu bars). A menu bar is displayed within a window and is the general access point into the menu system. The menu system can be used to control the program running within the window. The individual items of the menu bar are generally used to access submenus. These submenus are managed by objects of the XbpMenu class.
The pattern for using Xbase Parts normally includes the methods :new() and :create(). When creating menu bar objects there is an additional, easier way to generate XbpMenuBar objects. This is based on the fact that a menu bar must always be installed in a window. For this reason the XbpCrt and XbpDialog classes both include the method :menuBar() that returns a fully functional XbpMenuBar object that is already installed in the window.
After the XbpMenubar object is created, the items of the menu system must be added using the :addItem() method. If they are used, submenus (objects of the XbpMenu class) must be separately created.
The window area in which a menubar object is displayed is maintained automatically by the operating system. Consequently, most member variables and methods inherited from base class XbpWindow have no effect on menubar objects and should not be used.
// This example shows two different ways that a menu
// can be programmed using Xbase++. A menu bar is installed
// in an XbpCrt window and two submenus are added to it.
// The first submenu is programmed procedurally. Control
// occurs using the :itemSelected callback code block. This
// design is suitable for porting existing menu systems that
// are designed using DO..CASE control structures to a
// GUI application. This maintains as much existing code
// as possible. The second submenu uses the capabilities
// of the XbpMenu object for program control and uses code
// blocks assigned to each menu item.
#include "Appevent.ch"
PROCEDURE Main
LOCAL nEvent, mp1, mp2, oXbp
LOCAL oMenuBar, oSubMenu, aItem
Setcolor( "N/W")
CLS
// Allow use of shortcut keys
SetAppWindow():useShortCuts := .T.
// Install menu bar in the XbpCrt window
oMenuBar := SetAppWindow():MenuBar()
// Define submenu in procedural style.
// The numeric index of the selected menu item
// is passed to the Callback code block -> mp1
oSubMenu := XbpMenu():new(oMenuBar):create()
oSubMenu:title := "~Procedural"
oSubMenu:addItem( { "Procedure ~1", } )
oSubMenu:addItem( { "Procedure ~2", } )
oSubMenu:itemSelected := {|mp1| MyMenuProcedure( 100+mp1 ) }
oMenuBar:addItem( { oSubMenu, NIL } )
// Define submenu in the functional style:
// A menu item executes a code block that
// calls a function
oSubMenu := XbpMenu():new(oMenuBar):create()
oSubMenu:title := "~Functional"
oSubMenu:addItem( { "Function ~1", {|| MyFunction1() } } )
oSubMenu:addItem( { "Function ~2", {|| MyFunction2() } } )
oSubMenu:addItem( { "~End" , {|| MyFunction3() } } )
oMenuBar:addItem( { oSubMenu, NIL } )
// Event loop
nEvent := 0
DO WHILE nEvent <> xbeP_Close
nEvent := AppEvent( @mp1, @mp2, @oXbp )
oXbp:HandleEvent( nEvent, mp1, mp2 )
ENDDO
RETURN
// ************************************************************
// Example for integrating an existing menu system into
// event driven Xbase++
//
PROCEDURE MyMenuProcedure( nMenuSelection )
DO CASE // Process usual menu
CASE nMenuSelection == 101 // indexes with DO CASE
MyProcedure1()
CASE nMenuSelection == 102
MyProcedure2()
ENDCASE
RETURN
PROCEDURE MyProcedure1()
? "MyProcedure1() was called"
RETURN
PROCEDURE MyProcedure2()
? "MyProcedure2() was called"
RETURN
// ************************************************************
// These functions are called by the menu via code blocks
//
FUNCTION MyFunction1()
? "MyFunction1() was called"
RETURN NIL
FUNCTION MyFunction2()
? "MyFunction2() was called"
RETURN NIL
FUNCTION MyFunction3()
? "MyFunction3() was called -> End of program"
// Send message to program:
// The user will terminate
PostAppEvent( xbeP_Close )
RETURN NIL
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.