API/Samples/StarBasic/Office/Favorites-Menu

    From The Document Foundation Wiki
    < API‎ | Samples‎ | StarBasic
    PROGRAM:              FAVORITES.BAS
    Description:          Create a favorites menu in AOO, based on a blog entry from Malte Timmermann, for easy accessing often used documents.
                          See comments for the location where to make your customizations.
    Programming Language: StarBasic
    Author:               Malte Timmermann, 2006
    License:              None - Treat this as sample code, feel free to reuse
    

    BookmarksMenu is an extension to add a menu like a favorits menu on the OpenOffice menubar. This extension is based on the idea of Malte Timmermann's Favorites Menu. It was developed after the blog entry from Malte.

    '''REM ====== To remove menu: delete macro, then delete menu in Tools > Customize… ======'''
     
     '''REM ====== YOUR ROOT BELOW ======'''
     
     const aDataRootFolder="file:///home/Malte/Documents/"
     
     '''REM ====== YOUR ROOT ABOVE ======'''
     
     Sub InsertFavoritesMenu
        ImplInsertFavoritesMenu( "com.sun.star.frame.StartModule" )
        ImplInsertFavoritesMenu( "com.sun.star.text.TextDocument" )
        ImplInsertFavoritesMenu( "com.sun.star.sheet.SpreadsheetDocument" )
        ImplInsertFavoritesMenu( "com.sun.star.drawing.DrawingDocument" )
        ImplInsertFavoritesMenu( "com.sun.star.presentation.PresentationDocument" )
     End Sub 
     
     Sub ImplInsertFavoritesMenu( ModuleName as string )
        sMenuBar = "private:resource/menubar/menubar"
        sMyPopupMenuCmdId = "vnd.openoffice.org:FavoritesMenu"
       
        oModuleCfgMgrSupplier = createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
        oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager( ModuleName )
        oMenuBarSettings = oModuleCfgMgr.getSettings( sMenuBar, true )
       
        bHasAlreadyPopupMenu = false
        nCount = oMenuBarSettings.getCount()
        for i = 0 to nCount-1
            oPopupMenu() = oMenuBarSettings.getByIndex( i )
            nPopupMenuCount = ubound(oPopupMenu())
            for j = 0 to nPopupMenuCount
                if oPopupMenu(j).Name = "CommandURL" then
                    if oPopupMenu(j).Value = sMyPopupMenuCmdId then
                        bHasAlreadyPopupMenu = true
                    end if
                endif
            next j
        next i
       
        if not bHasAlreadyPopupMenu then
            sString = "Favorites"
            oPopupMenu = ImplCreatePopupMenu( sMyPopupMenuCmdId, sString, oMenuBarSettings )
            oPopupMenuContainer = oPopupMenu(3).Value
     
            '''REM ====== YOUR CUSTOMIZATIONS BELOW ======'''
     
            oMenuItem = ImplCreateMenuItem( aDataRootFolder+"ToDo.ods", "ToDo's" )
            oPopupMenuContainer.insertByIndex( oPopupMenuContainer.Count(), oMenuItem )
            oMenuItem = ImplCreateMenuItem( aDataRootFolder+"ODF-Plugin ToDo's.ods", "ToDo's ODF Plugin" )
            oPopupMenuContainer.insertByIndex( oPopupMenuContainer.Count(), oMenuItem )
            oMenuItem = ImplCreateMenuItem( aDataRootFolder+"cws-tools-doc-short.odt", "CWS Tools" )
            oPopupMenuContainer.insertByIndex( oPopupMenuContainer.Count(), oMenuItem )
           
            oMenuItem = ImplCreateMenuSeparator()
            oPopupMenuContainer.insertByIndex( oPopupMenuContainer.Count(), oMenuItem )
           
            oMenuItem = ImplCreateMenuItem( aDataRootFolder+"Addresses.ods", "Addresses" )
            oPopupMenuContainer.insertByIndex( oPopupMenuContainer.Count(), oMenuItem )
            oMenuItem = ImplCreateMenuItem( aDataRootFolder+"Data.ods", "Data" )
            oPopupMenuContainer.insertByIndex( oPopupMenuContainer.Count(), oMenuItem )
     
            '''REM ====== YOUR CUSTOMIZATIONS ABOVE ======'''
     
            oMenuBarSettings.insertByIndex( nCount, oPopupMenu )
            oModuleCfgMgr.replaceSettings( sMenuBar, oMenuBarSettings )
        end if
     End Sub
     
     Function ImplCreatePopupMenu( CommandId, Label, Factory ) as Variant
        Dim aPopupMenu(3) as new com.sun.star.beans.PropertyValue
        aPopupMenu(0).Name = "CommandURL"
        aPopupMenu(0).Value = CommandId
        aPopupMenu(1).Name = "Label"
        aPopupMenu(1).Value = Label
        aPopupMenu(2).Name = "Type"
        aPopupMenu(2).Value = 0
        aPopupMenu(3).Name = "ItemDescriptorContainer"
        aPopupMenu(3).Value = Factory.createInstanceWithContext( GetDefaultContext() )
        ImplCreatePopupMenu = aPopupMenu()
     End Function
     
     Function ImplCreateMenuItem( Command as String, Label as String ) as Variant
        Dim aMenuItem(2) as new com.sun.star.beans.PropertyValue
        aMenuItem(0).Name = "CommandURL"
        aMenuItem(0).Value = ".uno:Open?URL:string=" + Command + "&FrameName:string=_default"
        aMenuItem(1).Name = "Label"
        aMenuItem(1).Value = Label
        aMenuItem(2).Name = "Type"
        aMenuItem(2).Value = 0
          ImplCreateMenuItem = aMenuItem()
     End Function
     
     Function ImplCreateMenuSeparator() as Variant
        Dim aMenuItem(0) as new com.sun.star.beans.PropertyValue
        aMenuItem(0).Name = "Type"
        aMenuItem(0).Value = 1
        ImplCreateMenuSeparator = aMenuItem()
     End Function

    Apache Feather Logo.svg

    This work is licensed under the Apache License version 2.0