How to add your own items to the default Codejock Context menu

From DataFlex Wiki
Jump to navigationJump to search

Question

Each DEO control has its own popup menu. In the old days (when FormFloatingPopupMenu was used), we could add our own items to the default menu.

How can we do that now the cCJContextMenu class is being used?

So when a user right clicks a dbForm, I'd like to show the default context menu but add my own item(s) to the bottom of the menu.

Answer

by John Tuohy

We didn't make these menus classes because we wanted to show an example of how you can build context menus with objects and how they can be visually modeled. Load oDEOEditContextMenu.pkg and you can visually model it.

Rather than augmenting our menus I might suggest that you create your own, usingoDEOEditContextMenu.pkg as a model. You could make this with objects (which are visually modeled) or you can do it with a sub-class. Then change the handle of Default_dbFloating_Menu_ID to point to your own custom object. This really doesn't add much code to your application and it gives you complete control of your default menu.

For example, you could add your custom menu in the middle as follows:

 Use Windows.pkg
 Use cCJStandardMenuItemClasses.pkg
 Use cCJDeoMenuItemClasses.pkg
 
 Class cMenuTest is a cCJMenuItem
     Procedure Construct_Object
         Forward Send Construct_Object
         Set psCaption to "Testing"
     End_Procedure
      
     Procedure OnExecute Variant vCommandBarControl
         Showln "Hi"
     End_Procedure
 End_Class
  
   
 
 Object oMyCoolDEOEditContextMenu is a cCJContextMenu
     
    Move Self to Default_dbFloating_Menu_ID
     
    Object oUndoMenuItem is a cCJUndoMenuItem
    End_Object
    
    Object oCutMenuItem is a cCJCutMenuItem
        Set pbControlBeginGroup to True
    End_Object
    
    Object oCopyMenuItem is a cCJCopyMenuItem
    End_Object
 
    Object oPasteMenuItem is a cCJPasteMenuItem
    End_Object
 
    Object oDeleteItem is a cCJDeleteEditMenuItem
    End_Object
 
    Object oSelectAllMenuItem is a cCJSelectAllMenuItem
        Set pbControlBeginGroup to True
    End_Object
 
    Object oMyCoolMenu a cMenuTest 
        Set pbControlBeginGroup to True
    End_Object
 
    Object oPromptMenuItem is a cCJPromptMenuItem
        Set pbControlBeginGroup to True
    End_Object
 
    Object oFindNextMenu is a cCJFindNextMenuItem
        Set pbControlBeginGroup to True
    End_Object
 
    Object oFindPreviousMenu is a cCJFindPreviousMenuItem
    End_Object
 
    Object oClearMenuItem is a cCJClearMenuItem
        Set pbControlBeginGroup to True
    End_Object
 
    Object oClearAllMenu is a cCJClearAllMenuItem
    End_Object
 
    Object oSaveMenu is a cCJSaveMenuItem
    End_Object
    
    Object oDeleteMenu is a cCJDeleteMenuItem
    End_Object
 
 End_Object

If you've built your own menu you can do a similar thing inside of Popup. You can do whatever you want before you forward Popup (like create new items) and you can do whatever you want after you forward Popup (like remove some items). It's actually a lot easier than regular menus. I thought this was documented somewhere, but I can't find it.

And, finally, if you really just want to change an existing context menu, it's actually pretty easy. It's a lot easier than creating dynamic menus because the objects are not activated so all you have to do is to add the objects. You can do something like this:

  Class cMenuTest is a cCJMenuItem
    Procedure Construct_Object
        Forward Send Construct_Object
        Set psCaption to "Testing"
    End_Procedure
    
    Procedure OnExecute Variant vCommandBarControl
        Showln "Hi"
    End_Procedure
  End_Class
  
  Procedure  DoAddItemsToContextMenu
    Handle hoMenu hoMenu1
  
    If (Default_dbFloating_Menu_ID <> 0) Begin
      Move Default_dbFloating_Menu_ID to hoMenu
      Get Create of hoMenu (RefClass(cMenuTest)) to hoMenu1 
      Set pbControlBeginGroup of hoMenu1 to True
    End
  End_Procedure
  
  Send DoAddItemsToContextMenu

External Links

How to add your own items to the default cCJContextMenu