1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 #ifndef INCLUDED_SFX2_TBXCTRL_HXX
20 #define INCLUDED_SFX2_TBXCTRL_HXX
21 
22 #include <memory>
23 #include <sal/config.h>
24 #include <sfx2/dllapi.h>
25 #include <sal/types.h>
26 #include <svl/poolitem.hxx>
27 #include <vcl/floatwin.hxx>
28 #include <svtools/toolboxcontroller.hxx>
29 #include <rtl/ref.hxx>
30 
31 namespace com::sun::star::frame { class XDispatchProvider; }
32 namespace com::sun::star::frame { class XFrame; }
33 
34 class SfxToolBoxControl;
35 class SfxModule;
36 
37 svt::ToolboxController* SfxToolBoxControllerFactory( const css::uno::Reference< css::frame::XFrame >& rFrame, ToolBox* pToolbox, unsigned short nID, const OUString& aCommandURL );
38 
39 typedef SfxToolBoxControl* (*SfxToolBoxControlCtor)( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rBox );
40 
41 struct SfxTbxCtrlFactory
42 {
43     SfxToolBoxControlCtor const   pCtor;
44     const std::type_info&         nTypeId;
45     sal_uInt16 const              nSlotId;
46 
SfxTbxCtrlFactorySfxTbxCtrlFactory47     SfxTbxCtrlFactory( SfxToolBoxControlCtor pTheCtor,
48             const std::type_info& nTheTypeId, sal_uInt16 nTheSlotId ):
49         pCtor(pTheCtor),
50         nTypeId(nTheTypeId),
51         nSlotId(nTheSlotId)
52     {}
53 };
54 
55 
56 /* Floating windows that can be torn from tool boxes should be derived from
57    this class. Since it is also derived from SfxControllerItem, its instances
58    will also receive the StateChanged calls.
59 */
60 class SfxFrameStatusListener;
61 class SFX2_DLLPUBLIC SfxPopupWindow: public FloatingWindow
62 {
63 friend class SfxFrameStatusListener;
64     bool                                                   m_bFloating;
65     bool                                                   m_bCascading;
66     Link<SfxPopupWindow*,void>                             m_aDeleteLink;
67     sal_uInt16 const                                       m_nId;
68     css::uno::Reference< css::frame::XFrame > const        m_xFrame;
69     rtl::Reference<SfxFrameStatusListener>                 m_xStatusListener;
70 
71 private:
72     SfxPopupWindow(SfxPopupWindow const &) = delete;
73     SfxPopupWindow& operator =(SfxPopupWindow const &) = delete;
74     void Delete();
75 
76 protected:
77     virtual void            PopupModeEnd() override;
78     virtual bool            Close() override;
79 
GetId() const80     sal_uInt16              GetId() const { return m_nId; }
GetFrame() const81     const css::uno::Reference< css::frame::XFrame >& GetFrame() const { return m_xFrame; }
82 
83     void                    AddStatusListener( const OUString& rCommandURL );
84 
85     virtual void            statusChanged( const css::frame::FeatureStateEvent& rEvent );
86 
87 public:
88                             SfxPopupWindow(sal_uInt16 nId, vcl::Window *pParent,
89                                             const css::uno::Reference< css::frame::XFrame >& rFrame,
90                                             WinBits nBits );
91                             SfxPopupWindow(sal_uInt16 nId, vcl::Window *pParent,
92                                            const OString& rID, const OUString& rUIXMLDescription,
93                                            const css::uno::Reference<css::frame::XFrame> &rFrame =
94                                                css::uno::Reference<css::frame::XFrame>());
95                             virtual ~SfxPopupWindow() override;
96     virtual void            dispose() override;
97 
98     virtual void            MouseMove( const MouseEvent& rMEvt ) override;
99 
100     void                    StartCascading();
SetDeleteLink_Impl(const Link<SfxPopupWindow *,void> & rLink)101     SAL_DLLPRIVATE void SetDeleteLink_Impl( const Link<SfxPopupWindow*,void>& rLink )
102                             {
103                                 m_aDeleteLink = rLink;
104                             }
105 };
106 
107 
108 #define SFX_DECL_TOOLBOX_CONTROL() \
109         static SfxToolBoxControl* CreateImpl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox &rTbx ); \
110         static void RegisterControl(sal_uInt16 nSlotId = 0, SfxModule *pMod=nullptr)
111 
112 /*  For special ToolBox controls, such as a font selection box or toolbox
113     tear-off floating windows, an appropriate Item-Subclass of SfxTooBoxControl
114     has to be implemented.
115 
116     This class has to be registered in SfxApplication:Init() with the static
117     control method RegisterControl(). The SFx then automatically creates these
118     controls in the toolbox, if the associated slots are of the specific type.
119  */
120 
121 struct SfxToolBoxControl_Impl;
122 class SFX2_DLLPUBLIC SfxToolBoxControl: public svt::ToolboxController
123 {
124 friend class SfxPopupWindow;
125 friend struct SfxTbxCtrlFactory;
126 
127     std::unique_ptr< SfxToolBoxControl_Impl>    pImpl;
128 
129 protected:
130     DECL_LINK( PopupModeEndHdl, FloatingWindow*, void );
131     DECL_LINK( ClosePopupWindow, SfxPopupWindow *, void );
132 
133     // old SfxToolBoxControl methods
134     virtual void               StateChanged( sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState );
135     virtual void               Select( sal_uInt16 nSelectModifier );
136 
137     virtual void               DoubleClick();
138     virtual void               Click();
139     virtual VclPtr<SfxPopupWindow> CreatePopupWindow();
140     virtual VclPtr<vcl::Window> CreateItemWindow( vcl::Window *pParent );
141 
142     // Must be called by subclass to set a new popup window instance
143     void                       SetPopupWindow( SfxPopupWindow* pWindow );
144 
145 public:
146     // XComponent
147     virtual void SAL_CALL dispose() override;
148 
149     // new controller API
150     // XStatusListener
151     virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override;
152 
153     // XToolbarController
154     virtual void SAL_CALL execute( sal_Int16 KeyModifier ) override;
155     virtual void SAL_CALL click() override;
156     virtual void SAL_CALL doubleClick() override;
157     virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override;
158     virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createItemWindow( const css::uno::Reference< css::awt::XWindow >& rParent ) override;
159 
160 public:
161                                SFX_DECL_TOOLBOX_CONTROL();
162 
163                                SfxToolBoxControl( sal_uInt16 nSlotID, sal_uInt16 nId, ToolBox& rBox, bool bShowStrings = false );
164     virtual                    ~SfxToolBoxControl() override;
165 
166     ToolBox&                   GetToolBox() const;
167     unsigned short             GetId() const;
168     unsigned short             GetSlotId() const;
169 
170     void                       Dispatch( const OUString& aCommand,
171                                          css::uno::Sequence< css::beans::PropertyValue > const & aArgs );
172     static void                Dispatch( const css::uno::Reference< css::frame::XDispatchProvider >& rDispatchProvider,
173                                          const OUString& rCommand,
174                                          css::uno::Sequence< css::beans::PropertyValue > const & aArgs );
175 
176     static SfxItemState        GetItemState( const SfxPoolItem* pState );
177     static SfxToolBoxControl*  CreateControl( sal_uInt16 nSlotId, sal_uInt16 nTbxId, ToolBox *pBox, SfxModule const *pMod );
178     static void                RegisterToolBoxControl( SfxModule*, const SfxTbxCtrlFactory&);
179 };
180 
181 #define SFX_IMPL_TOOLBOX_CONTROL(Class, nItemClass) \
182         SfxToolBoxControl* Class::CreateImpl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox &rTbx ) \
183                { return new Class( nSlotId, nId, rTbx ); } \
184         void Class::RegisterControl(sal_uInt16 nSlotId, SfxModule *pMod) \
185                { SfxToolBoxControl::RegisterToolBoxControl( pMod, SfxTbxCtrlFactory( \
186                     Class::CreateImpl, typeid(nItemClass), nSlotId ) ); }
187 
188 #define SFX_IMPL_TOOLBOX_CONTROL_ARG(Class, nItemClass, Arg) \
189         SfxToolBoxControl* Class::CreateImpl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox &rTbx ) \
190                { return new Class( nSlotId, nId, rTbx, Arg); } \
191         void Class::RegisterControl(sal_uInt16 nSlotId, SfxModule *pMod) \
192                { SfxToolBoxControl::RegisterToolBoxControl( pMod, SfxTbxCtrlFactory( \
193                     Class::CreateImpl, typeid(nItemClass), nSlotId ) ); }
194 
195 
196 #endif
197 
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
199