1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_SVTOOLS_CONTEXTMENUHELPER_HXX
29 #define INCLUDED_SVTOOLS_CONTEXTMENUHELPER_HXX
30 
31 #include <com/sun/star/frame/XFrame.hpp>
32 #include <com/sun/star/awt/XPopupMenu.hpp>
33 #include <com/sun/star/util/XURLTransformer.hpp>
34 #include <com/sun/star/ui/XImageManager.hpp>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 
37 #include <rtl/ustring.hxx>
38 #include <cppuhelper/weak.hxx>
39 #include <vcl/menu.hxx>
40 #include "svtools/svtdllapi.h"
41 
42 namespace svt
43 {
44 
45 /**
46     Context menu helper class.
47 
48     Fills images and labels for a provided popup menu or
49     com.sun.star.awt.XPopupMenu.
50 
51     PRECONDITION:
52     All commands must be set via SetItemCommand and are part
53     of the configuration files
54     (see org.openoffice.Office.UI.[Module]Commands.xcu)
55 */
56 struct ExecuteInfo;
57 class SVT_DLLPUBLIC ContextMenuHelper
58 {
59     public:
60         // create context menu helper
61         // ARGS: xFrame = frame defines the context of the context menu
62         //       bAutoRefresh = specifies that the context will be constant or not
63         ContextMenuHelper( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame, bool bAutoRefresh=true );
64         ~ContextMenuHelper();
65 
66         // methods to complete a popup menu (set images, labels, enable/disable states)
67         // ATTENTION: The item ID's must be unique for the whole popup (inclusive the sub menus!)
68         void completeAndExecute( const Point& aPos, PopupMenu& aPopupMenu );
69         void completeAndExecute( const Point& aPos, const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPopupMenu >& xPopupMenu );
70 
71         // methods to create a popup menu referenced by resource URL
72         // NOT IMPLEMENTED YET!
73         ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPopupMenu > create( const ::rtl::OUString& aPopupMenuResourceURL );
74 
75         // method to create and execute a popup menu referenced by a resource URL
76         // NOT IMPLEMENTED YET!
77         bool createAndExecute( const Point& aPos, const ::rtl::OUString& aPopupMenuResourceURL );
78 
79     private:
80         // asynchronous link to prevent destruction while on stack
81         DECL_STATIC_LINK( ContextMenuHelper, ExecuteHdl_Impl, ExecuteInfo* );
82 
83         // no copy-ctor and operator=
84         ContextMenuHelper( const ContextMenuHelper& );
85         const ContextMenuHelper& operator=( const ContextMenuHelper& );
86 
87         // show context menu and dispatch command automatically
88         void            executePopupMenu( const Point& aPos, PopupMenu* pMenu );
89 
90         // fill image and label for every menu item on the provided menu
91         void            completeMenuProperties( Menu* pMenu );
92 
93         // dispatch provided command
94         bool            dispatchCommand( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame, const ::rtl::OUString& aCommandURL );
95 
96 
97         // methods to retrieve a single command URL dependent value from a
98         // ui configuratin manager
99         Image           getImageFromCommandURL( const ::rtl::OUString& aCmdURL, bool bHiContrast ) const;
100         rtl::OUString   getLabelFromCommandURL( const ::rtl::OUString& aCmdURL ) const;
101 
102         // creates an association between current module/controller bound to the
103         // provided frame and their ui configuration managers.
104         bool            associateUIConfigurationManagers();
105 
106         // resets associations to create associations again on-demand.
107         // Usefull for implementations which recycle frames. Normal
108         // implementations can profit from caching and should set
109         // auto refresh on ctor to false (default).
110         void            resetAssociations()
111         {
112             if ( m_bAutoRefresh )
113                 m_bUICfgMgrAssociated = false;
114         }
115 
116         ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame >         m_xWeakFrame;
117         ::rtl::OUString                                                                 m_aModuleIdentifier;
118         ::rtl::OUString                                                                 m_aSelf;
119         ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >     m_xURLTransformer;
120         ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >       m_aDefaultArgs;
121         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XImageManager >         m_xDocImageMgr;
122         ::com::sun::star::uno::Reference< ::com::sun::star::ui::XImageManager >         m_xModuleImageMgr;
123         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >    m_xUICommandLabels;
124         bool                                                                            m_bAutoRefresh;
125         bool                                                                            m_bUICfgMgrAssociated;
126 };
127 
128 } // namespace svt
129 
130 #endif // INCLUDED_SVTOOLS_CONTEXTMENUHELPER_HXX
131