Page 1 of 1

Complete custom menu system

Posted: Tue Aug 10, 2010 6:14 pm
by zabolyx
OK... my knowledgeable comrades...

I need to have all of the default and extension installed menus and toolbars hidden from my spreadsheet upon opening it.

So I'd link the macro to one of the startup events... I understand that. I saw in Andrews online documentation how to list the standard toolbars (but what about the installed extensions).

I then need to create a custom menu (cascading) that then will be shown to the user instead of the default menus.

My menu layout I'd like to have. All non-bold are the dropdown menus under the bold headers


File Edit Import Report Archive Print Email Sheet Sync

Save Update New Listings MAP Backup Process Generate Process Company
Close Delete Archive Statistics Import Statistics Mail Merge Options Agent
Combine Local History History
MAP
Statistics

I'd rather have the process explained rather than just done for me. I've found bits and pieces of code but nothing that really has done much for my feeble mind. :)

The FILE menu would work just like the SAVE and CLOSE do in Open Office. So if there was a way to directly interface those bits of code so I don't have to rewrite that would be great. Or if the current menubar can have things selectively removed from it.

I did see some good info in Andrew's material on XML menu distribution, but think that using macros to build them would work better for this as I have several users using the project and I usually have tons of versions released to fix the plethora of bugs I like to build into my projects.

Re: Complete custom menu system

Posted: Mon Aug 16, 2010 7:57 pm
by zabolyx
Found this nugget of goodness on the old forum....

Code: Select all

Sub hideallBars_inOOo2( )
    doc = Stardesktop.getCurrentComponent()
    frame = doc.CurrentController.Frame
    lmgr = frame.LayoutManager
    lmgr.setVisible(False)
    End Sub
I had to edit it... as getCurrentComponent seems to cause issues with the IDE

Code: Select all

Sub hideallBars_inOOo2( )
    doc = ThisComponent
    frame = doc.CurrentController.Frame
    lmgr = frame.LayoutManager
    lmgr.setVisible(False)
    End Sub
and this works for removing the toolbars but not the menubar. Looks tons better but my quest it for only a custom menubar to remain.

how do I remove the menubar and replace it with my own? Let alone make that custom menubar. I've seen a little on this but nothing that makes much sense to me. Hoping for some good tutelage from the community.

Re: Complete custom menu system

Posted: Mon Aug 16, 2010 7:59 pm
by zabolyx
And of course that code there should be able to compress it down to one line, right?

ThisComponent.CurrentController.Frame.LayoutManager.setVisible(False)

Yes?

Re: Complete custom menu system

Posted: Fri Aug 20, 2010 9:09 pm
by zabolyx
That does seem to work just fine...

Now that doesn't seem to hide the menubar... just the toolbars

Now I did find some info that I have decoded from Andrew's online book...

oThisComponent.CurrentController.getFrame().LayoutManager.hideElement("private:resource/menubar/menubar")

Giving this a test on hiding the menu bar... but I need to create my own

Should I just name it "private:resource/menubar/custom_menubar" ? Would that be considered proper and would it work?

Re: Complete custom menu system

Posted: Sat Aug 21, 2010 3:12 pm
by hanya
Just an example written in Python:

Code: Select all

import unohelper

from com.sun.star.awt import XMenuListener
from com.sun.star.lang import XEventListener

from com.sun.star.awt import Rectangle

# multiple task should be kept
global event


class EventListener(unohelper.Base, XEventListener):
    """ to close window safely. """
    def __init__(self, menubar, window, listener):
        self.menubar = menubar
        self.window = window
        self.listener = listener
    
    def disposing(self, ev):
        self.window.setMenuBar(None) # have to be set to None
        self.menubar = None
        self.window = None
        self.listener = None
        global event
        event = None


class MainClass(unohelper.Base, XEventListener, XMenuListener):
    def __init__(self, ctx, doc):
        self.ctx = ctx
        self.doc = doc
    
    # XMenuListener
    def hightlight(self, ev):
        pass
    
    def select(self, ev):
        self.message(ev.Source.getCommand(ev.MenuId))
    
    def activate(self, ev):
        pass
    
    def deactivate(self, ev):
        pass
    
    def dispose(self, ev):
        pass
    
    def message(self, message, title=""):
        win = self.doc.getCurrentController().getFrame().getContainerWindow()
        toolkit = win.getToolkit()
        dlg = toolkit.createMessageBox(win, Rectangle(), "messbox", 1, title, message)
        return dlg.execute()
    

def set_menubar():
    ctx = XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.getServiceManager()
    frame = XSCRIPTCONTEXT.getDocument().getCurrentController().getFrame()
    
    # window to set custom menubar
    window = frame.getContainerWindow()
    
    # custom menubar
    menubar = smgr.createInstanceWithContext(
        "com.sun.star.awt.MenuBar", ctx)
    menubar.insertItem(1, "~File", 0, 0)
    menubar.insertItem(2, "~Edit", 0, 1)
    
    # create submenus and set to the main menu
    file_menu = create_popupmenu(smgr, ctx, 
        ((1, "~New", 0, 0, "new"),))
    edit_menu = create_popupmenu(smgr, ctx, 
        ((2, "~Edit", 0, 0, "edit"),))
    
    menubar.setPopupMenu(1, file_menu)
    menubar.setPopupMenu(2, edit_menu)
    
    # set listeners
    menu_listener = MainClass(ctx, XSCRIPTCONTEXT.getDocument())
    file_menu.addMenuListener(menu_listener)
    edit_menu.addMenuListener(menu_listener)
    
    # set custom one
    window.setMenuBar(menubar)
    global event
    event = EventListener(menubar, window, menu_listener)
    

# items [id,text,type,pos,command], if separator [-1,pos]
def create_popupmenu(smgr,ctx,items):
    menu = smgr.createInstanceWithContext('com.sun.star.awt.PopupMenu',ctx)
    for item in items:
        if -1 < item[0]:
            menu.insertItem(item[0],item[1],item[2],item[3])
            menu.setCommand(item[0],item[4])
        else:
            menu.insertSeparator(item[1])
        #if len(item) >= 6 and item[5] is not None:
        #   menu.setAcceleratorKeyEvent(item[0], item[5])
    return menu

Re: Complete custom menu system

Posted: Mon Aug 23, 2010 3:14 pm
by zabolyx
Hanya... question is.. how do I convert that to OOoBASIC?

I'm thinking along the lines of creating objects from the com.sun.star items. And from there... well I'm pretty lost after that.

Re: Complete custom menu system

Posted: Mon Aug 23, 2010 5:22 pm
by hanya
zabolyx wrote:Hanya... question is.. how do I convert that to OOoBASIC?
First I tried to write it in the basic but unfortunately it was not stable. In the case of my above code, the office is going to be crashed without "global event" declaration at the time to close the window. So, I can not help you to convert the code from python to basic.

Re: Complete custom menu system

Posted: Mon Aug 23, 2010 5:31 pm
by zabolyx
well that sucks... so this causes stability issues within OOoBASIC. Great. I've got to keep this code in BASIC only as I sometimes run this in the OOo Portable versions and do not have the Python support files installed for those. Kind of between a rock and a hard place at the moment.

Thanks for the help Hanya. Keeping my fingers crossed for some more enlightenment from the community.