Standard App Menu

An automated menubar class that simplifies creating and modifying application menu's. Contributed by Francesco Danti. Features include automatically adding whole nested structures in one method call, adding event handlers on the fly, locating menu's on the left and right sides of the menu bar and modifying menu's and their contents by re-adding substructures under the same name. See the forum thread and the blogpost on oracoltech.com for documentation.




    <?php
    
//============================================================+
    // Author: Francesco "Abbadon1334" Danti
    //
    // (c) Copyright:
    //               Francesco Danti
    //               S.C. Oracol Tech s.r.l.
    //               www.oracoltech.com
    //               info@oracoltech.com
    //============================================================+

    
function authorizeLogin$args$user$pass ) {
       
// We'll just hardcode these credentials here, normally you'd want to do something
       // more sophisticated here...
       
return ($user == 'user' && $pass == 'password');
    }

    function 
getApplication$args ) {
       
// Return the name of the application class that should be started.
       
return 'testTopMenu';
    }


    class 
testTopMenu extends Application
    
{
       public function 
init$event ) {
          
          
// Set the window title and size
          
$this->window->title'Standard App Menu Demo' );
          
$this->window->size1024740 );
          
          
$this->window->addChild(
             
$top = new XULStdAppMenu()
          );
          
          
$top->addMenuRoot('File',NULL);
          
$top->addMenu(
             
'File'
             
,'File'
             
,array(
                array(
'New'$this'noEventYet'),
                array(
'Open'$this,'noEventYet'),
                array(
NULL,NULL,NULL),
                array(
'Exit'$this'noEventYet')      
             )
          );
          
          
$top->addMenuRoot('Modules',NULL);
          
$top->addMenu(
             
'Modules'
             
,'Modules A'
             
,array(
                array(
'Module A'$this'noEventYet'),            
                array(
NULL,NULL,NULL),
                array(
'Module B'$this,'noEventYet'),
                array(
'Module C'$this'noEventYet')      
             )
          );
          
          
$top->addMenu(
             
'Modules'
             
,'Single modules A'
             
,array(
                array(
'Module A A'$this'noEventYet'),            
                array(
NULL,NULL,NULL),
                array(
'Module A B'$this,'noEventYet'),
                array(
'Module A C'$this'noEventYet')      
             )
          );
          
          
$top->addMenu(
             
'Single modules A'
             
,'Single modules B'
             
,array(
                array(
'Module B A'$this'noEventYet'),            
                array(
NULL,NULL,NULL),
                array(
'Module B B'$this,'noEventYet'),
                array(
'Module B C'$this'noEventYet')      
             )
          );
          
          
$top->addMenuRoot('Tools',NULL);
          
$top->addMenu(
             
'Tools'
             
,'Tools'
             
,array(
                array(
'Tool A'$this'noEventYet'),            
                array(
NULL,NULL,NULL),
                array(
'Tool B'$this'noEventYet'),            
                array(
NULL,NULL,NULL),
                array(
'Tool C'$this'noEventYet')
             )
          );
          
          
$top->addMenuRoot('TestDeepRef',NULL);
          
$top->addMenuRoot('TestDeepRef','TestDeepRef2');
          
$top->addMenuRoot('TestDeepRef2','TestDeepRef3');
          
$top->addMenuRoot('TestDeepRef3','TestDeepRef4');
          
          
$top->addMenu(
             
'TestDeepRef4'
             
,'TestDeepRef4'
             
,array(
                array(
'openAbout'$this'openAbout'),
                array(
'openAboutMemory'$this,'openAboutMemory'),
                array(
'openExtensions'$this'openExtensions'),
                array(
'openErrorConsole',$this,'openErrorConsole')            
             )
          );
          
          
          
$top->addMenuRoot('HelpMenu',NULL,FALSE);
          
          
$top->addMenu(
             
'HelpMenu'
             
,'HelpMenu'
             
,array(
                array(
'openAbout'$this'openAbout'),
                array(
'openAboutMemory'$this,'openAboutMemory'),
                array(
'openExtensions'$this'openExtensions'),
                array(
'openErrorConsole',$this,'openErrorConsole')            
             )
          );
          
          
$top->addMenu(
             
'HelpMenu'
             
,'HelpMenu'
             
,array(
                array(
'singleMenu test'$this'openAbout')
             )
          );
          
          
$top->addMenu(
             
'HelpMenu'
             
,'SubMenu'
             
,array(
                array(
'singleMenu test 1'$this'openAbout')
             )
          );
          
          
$top->addMenu(
             
'HelpMenu'
             
,'SubMenu'
             
,array(
                array(
'singleMenu test 2'$this'openAbout')
             )
          );
          
          
$top->addMenu(
             
'HelpMenu'
             
,'SubMenu'
             
,array(
                array(
'singleMenu test 3'$this'openAbout')
             )
          );
          
          
$top->addMenu(
             
'SubMenu'
             
,'SubSubMenu'      
             
,array(
                array(
'openAbout'$this'openAbout'),
                array(
'openAboutMemory'$this,'openAboutMemory'),
                array(
'openExtensions'$this'openExtensions'),
                array(
'openErrorConsole',$this,'openErrorConsole')            
             )
          );
          
          
$top->addMenu(
             
'SubSubMenu'
             
,'SubSubSubMenu'      
             
,array(
                array(
'openAbout'$this'openAbout'),
                array(
'openAboutMemory'$this,'openAboutMemory'),
                array(
'openExtensions'$this'openExtensions'),
                array(
'openErrorConsole',$this,'openErrorConsole')            
             )
          );
          
          
$top->addMenu(
             
'TestDeepRef4'
             
,'TestDeepRef5'
             
,array(
                array(
'openAbout'$this'openAbout'),
                array(
'openAboutMemory'$this,'openAboutMemory'),
                array(
'openExtensions'$this'openExtensions'),
                array(
'openErrorConsole',$this,'openErrorConsole')            
             )
          );
          
       }
       
       
// EVENTS   
       
public function noEventYet($e) {
          
$this->window->alert("evento ancora non linkato");
       }
    }




    class 
XULStdAppMenu extends XULToolBox {
       
       public 
$bar;
       public 
$menus = array();
       private 
$_separator;
          
       public function 
__construct() {
          
          
$this->bar = new XULMenuBar();
          
          
$this->addChild$this->bar );
          
          
$this->_spacer = new XULSpacer(1);
          
          
$this->bar->addChild$this->_spacer );
       }   
       
       public function 
addMenuRoot($rootLabel,$groupLabel,$left TRUE) {
          
          if(
$groupLabel == NULL) {
             
$menu $this->menus[$rootLabel.$rootLabel] = new XULStdAppTopMenu($rootLabel);
          } else {
             
$rootMenu $this->menus[$rootLabel.$rootLabel];
             
$this->menus[$groupLabel.$groupLabel] = $rootMenu->getGroup($groupLabel);
             
$this->menus[$rootLabel.$groupLabel] = $rootMenu->getGroup($groupLabel);
          }
          
          if(!
$groupLabel) {
             if(
$left)
                
$this->bar->addChildBefore($menu,$this->_spacer );
             else
                
$this->bar->addChildAfter($menu,$this->_spacer );
          }
       }
       
       public function 
getMenu($rootLabel,$groupLabel) {
          
          if(!
array_key_exists($rootLabel.$groupLabel,$this->menus)) {
             
$this->addMenuRoot($rootLabel,$groupLabel);
          }
          
          return 
$this->menus[$rootLabel.$groupLabel];
       }
       
       public function 
addMenu($rootLabel,$groupLabel,$aMenu) {
          
          
$refMenu $this->getMenu($rootLabel,$groupLabel);
             
          foreach(
$aMenu as $menu) {
             
$label $menu[0];
             
$callMethodFrom $menu[1];
             
$methodToCall $menu[2];
             
             if(empty(
$label) && empty($callMethodFrom) && empty($methodToCall))
                
$refMenu->addSeparator($groupLabel);
             else
                
$refMenu->addMenuItem($groupLabel,$label,$callMethodFrom,$methodToCall);
          }
       }
       
       public function 
addMenuSeparator($rootLabel,$groupLabel) {
          
$this->getMenu($rootLabel,$groupLabel)->addSeparator($groupLabel);
       }
    }

    class 
XULStdAppTopMenu extends XULMenu {
       
       private 
$_popup NULL;
       private 
$_groupMenus = array();
       
       private function 
getPopUp() {
          
          if(!
$this->_popup) {         
             
$this->_popup = new XULMenuPopup();         
             
$this->addChild$this->_popup );
             
$this->_groupMenus[$this->label] = $this;
          }
          
          return 
$this->_popup;
       }
       
       public function 
getGroup($groupLabel) {
          
          
$p $this->getPopUp();
          
          if(!
array_key_exists($groupLabel,$this->_groupMenus)) {
             
$this->_groupMenus[$groupLabel] = new XULStdAppTopMenu($groupLabel);
             
$p->addChild($this->_groupMenus[$groupLabel]);
          }
          
          return 
$this->_groupMenus[$groupLabel];
       }
       
       public function 
addMenuItem($groupLabel,$label,$callMethodFrom,$methodToCall) {
          
          
$menu = new XULStdAppTopMenuItem($label);
          
          
$this->getGroup($groupLabel)->getPopUp()->addChild$menu );
          
          
$menu->setEventType('command'MSG_SEND )
             ->
setEventHandler('command'$callMethodFrom$methodToCall)
          ;
          
          return 
$menu;
       }
       
       public function 
addSeparator($groupLabel) {
          if(
$groupLabel || $groupLabel == $this->label)
             
$this->getPopUp()->addChild( new XULMenuSeparator());
          else
             
$this->_groupMenus[$groupLabel]->getPopUp()->addChild( new XULMenuSeparator());
       }
       
    }

    class 
XULStdAppTopMenuItem extends XULMenuItem {   
    }
    
?>