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 #pragma once
20 
21 #include <memory>
22 #include <sal/config.h>
23 
24 #include <sfx2/sidebar/AsynchronousCall.hxx>
25 #include <sfx2/sidebar/Context.hxx>
26 #include <sfx2/sidebar/Deck.hxx>
27 #include <sfx2/sidebar/FocusManager.hxx>
28 #include <sfx2/sidebar/ResourceManager.hxx>
29 #include <sfx2/sidebar/TabBar.hxx>
30 #include <sfx2/viewfrm.hxx>
31 
32 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
33 #include <com/sun/star/frame/XStatusListener.hpp>
34 #include <com/sun/star/frame/XFrameActionListener.hpp>
35 #include <com/sun/star/ui/XContextChangeEventListener.hpp>
36 #include <com/sun/star/ui/XSidebar.hpp>
37 
38 #include <optional>
39 #include <cppuhelper/compbase.hxx>
40 #include <cppuhelper/basemutex.hxx>
41 
42 namespace com::sun::star::awt { class XWindow; }
43 namespace com::sun::star::frame { class XDispatch; }
44 namespace com::sun::star::ui { class XUIElement; }
45 
46 typedef cppu::WeakComponentImplHelper <
47     css::ui::XContextChangeEventListener,
48     css::beans::XPropertyChangeListener,
49     css::ui::XSidebar,
50     css::frame::XStatusListener,
51     css::frame::XFrameActionListener
52     > SidebarControllerInterfaceBase;
53 
54 class SfxSplitWindow;
55 class SfxViewShell;
56 
57 namespace sfx2::sidebar {
58 
59 class DeckDescriptor;
60 class SidebarDockingWindow;
61 
62 class SFX2_DLLPUBLIC SidebarController
63     : private ::cppu::BaseMutex,
64       public SidebarControllerInterfaceBase
65 {
66 public:
67     static rtl::Reference<SidebarController> create(SidebarDockingWindow* pParentWindow,
68                                                     const SfxViewFrame* pViewFrame);
69     virtual ~SidebarController() override;
70     SidebarController(const SidebarController&) = delete;
71     SidebarController& operator=( const SidebarController& ) = delete;
72 
73     /** Return the SidebarController object that is associated with
74         the given XFrame.
75         @return
76             When there is no SidebarController object for the given
77             XFrame then <NULL/> is returned.
78     */
79     static SidebarController* GetSidebarControllerForFrame (
80         const css::uno::Reference<css::frame::XFrame>& rxFrame);
81 
82     static void registerSidebarForFrame(SidebarController* pController, const css::uno::Reference<css::frame::XController>& xFrame);
83 
84     static void unregisterSidebarForFrame(SidebarController* pController, const css::uno::Reference<css::frame::XController>& xFrame);
85 
86     // ui::XContextChangeEventListener
87     virtual void SAL_CALL notifyContextChangeEvent (const css::ui::ContextChangeEventObject& rEvent) override;
88 
89     // XEventListener
90     virtual void SAL_CALL disposing (const css::lang::EventObject& rEventObject) override;
91 
92     // beans::XPropertyChangeListener
93     virtual void SAL_CALL propertyChange (const css::beans::PropertyChangeEvent& rEvent) override;
94 
95     // frame::XStatusListener
96     virtual void SAL_CALL statusChanged (const css::frame::FeatureStateEvent& rEvent) override;
97 
98     // frame::XFrameActionListener
99     virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent) override;
100 
101     // ui::XSidebar
102     virtual void SAL_CALL requestLayout() override;
103 
104     void NotifyResize();
105 
106     /** In some situations it is necessary to force an update of the
107         current deck and its panels.  One reason is a change of the
108         view scale.  Some panels can handle this only when
109         constructed.  In this case we have to a context change and
110         also force that all panels are destroyed and created new.
111     */
112     const static sal_Int32 SwitchFlag_NoForce = 0x00;
113     const static sal_Int32 SwitchFlag_ForceSwitch = 0x01;
114     const static sal_Int32 SwitchFlag_ForceNewDeck = 0x02;
115     const static sal_Int32 SwitchFlag_ForceNewPanels = 0x02;
116 
117     void OpenThenSwitchToDeck(std::u16string_view rsDeckId);
118     void OpenThenToggleDeck(const OUString& rsDeckId);
119 
120     /** Show only the tab bar, not the deck.
121     */
122     void RequestCloseDeck();
123 
124     /** Open the deck area and restore the parent window to its old width.
125     */
126     void RequestOpenDeck();
127 
128     /** Returns true when the given deck is the currently visible deck
129      */
130     bool IsDeckVisible(std::u16string_view rsDeckId);
131 
132     bool IsDeckOpen(const sal_Int32 nIndex = -1);
133 
GetFocusManager()134     FocusManager& GetFocusManager() { return maFocusManager;}
135 
GetResourceManager()136     ResourceManager* GetResourceManager() { return mpResourceManager.get();}
GetCurrentDeckId() const137     auto& GetCurrentDeckId() const { return msCurrentDeckId; }
138 
139    // std::unique_ptr<ResourceManager> GetResourceManager() { return mpResourceManager;}
140 
GetCurrentContext() const141     const Context& GetCurrentContext() const { return maCurrentContext;}
IsDocumentReadOnly(void) const142     bool IsDocumentReadOnly (void) const { return mbIsDocumentReadOnly;}
143 
144     void SwitchToDeck(std::u16string_view rsDeckId);
145     void SwitchToDefaultDeck();
WasFloatingDeckClosed() const146     bool WasFloatingDeckClosed() const { return mbFloatingDeckClosed; }
SetFloatingDeckClosed(bool bWasClosed)147     void SetFloatingDeckClosed(bool bWasClosed) { mbFloatingDeckClosed = bWasClosed; }
148 
149     void CreateDeck(std::u16string_view rDeckId);
150     void CreateDeck(std::u16string_view rDeckId, const Context& rContext, bool bForceCreate = false);
151 
152     ResourceManager::DeckContextDescriptorContainer GetMatchingDecks();
153     ResourceManager::PanelContextDescriptorContainer GetMatchingPanels(std::u16string_view rDeckId);
154 
155     void notifyDeckTitle(std::u16string_view targetDeckId);
156 
157     void updateModel(const css::uno::Reference<css::frame::XModel>& xModel);
158 
159     void disposeDecks();
160 
161     void FadeIn();
162     void FadeOut();
163 
164     tools::Rectangle GetDeckDragArea() const;
165 
getXFrame() const166     css::uno::Reference<css::frame::XFrame> const & getXFrame() const {return mxFrame;}
167 
getMaximumWidth() const168     sal_Int32 getMaximumWidth() const { return mnMaximumSidebarWidth; }
setMaximumWidth(sal_Int32 nMaximumWidth)169     void setMaximumWidth(sal_Int32 nMaximumWidth) { mnMaximumSidebarWidth = nMaximumWidth; }
170 
171     void saveDeckState();
172 
173     void SyncUpdate();
174 
175     bool hasChartContextCurrently() const;
176 
177     static SidebarController* GetSidebarControllerForView(SfxViewShell* pViewShell);
178 
179 private:
180     SidebarController(SidebarDockingWindow* pParentWindow, const SfxViewFrame* pViewFrame);
181 
182     VclPtr<Deck> mpCurrentDeck;
183     VclPtr<SidebarDockingWindow> mpParentWindow;
184     const SfxViewFrame* mpViewFrame;
185     css::uno::Reference<css::frame::XFrame> mxFrame;
186     VclPtr<TabBar> mpTabBar;
187     Context maCurrentContext;
188     Context maRequestedContext;
189     css::uno::Reference<css::frame::XController> mxCurrentController;
190     /// Use a combination of SwitchFlag_* as value.
191     sal_Int32 mnRequestedForceFlags;
192     sal_Int32 mnMaximumSidebarWidth;
193     bool mbMinimumSidebarWidth;
194     OUString msCurrentDeckId;
195     AsynchronousCall maPropertyChangeForwarder;
196     AsynchronousCall maContextChangeUpdate;
197 
198     /** Two flags control whether the deck is displayed or if only the
199         tab bar remains visible.
200         The mbIsDeckOpen flag stores the current state while
201         mbIsDeckRequestedOpen stores how this state should be.  User
202         actions like clicking on the deck closer affect the
203         mbIsDeckRequestedOpen.  Normally both flags have the same
204         value.  A document being read-only can prevent the deck from opening.
205     */
206     ::std::optional<bool> mbIsDeckRequestedOpen;
207     ::std::optional<bool> mbIsDeckOpen;
208 
209     bool mbFloatingDeckClosed;
210 
211     /** Before the deck is closed the sidebar width is saved into this variable,
212         so that it can be restored when the deck is reopened.
213     */
214     sal_Int32 mnSavedSidebarWidth;
215     FocusManager maFocusManager;
216     css::uno::Reference<css::frame::XDispatch> mxReadOnlyModeDispatch;
217     bool mbIsDocumentReadOnly;
218     VclPtr<SfxSplitWindow> mpSplitWindow;
219     /** When the user moves the splitter then we remember the
220         width at that time.
221     */
222     sal_Int32 mnWidthOnSplitterButtonDown;
223     /** Control that is temporarily used as replacement for the deck
224         to indicate that when the current mouse drag operation ends, the
225         sidebar will only show the tab bar.
226     */
227     VclPtr<vcl::Window> mpCloseIndicator;
228 
229     DECL_LINK(WindowEventHandler, VclWindowEvent&, void);
230     /** Make maRequestedContext the current context.
231     */
232     void UpdateConfigurations();
233 
234     css::uno::Reference<css::ui::XUIElement> CreateUIElement (
235         const css::uno::Reference<css::awt::XWindow>& rxWindow,
236         const OUString& rsImplementationURL,
237         const bool bWantsCanvas,
238         const Context& rContext);
239 
240     void CreatePanels(
241         std::u16string_view rDeckId,
242         const Context& rContext);
243     std::shared_ptr<Panel> CreatePanel (
244         std::u16string_view rsPanelId,
245         weld::Widget* pParentWindow,
246         const bool bIsInitiallyExpanded,
247         const Context& rContext,
248         const VclPtr<Deck>& pDeck);
249 
250     void SwitchToDeck (
251         const DeckDescriptor& rDeckDescriptor,
252         const Context& rContext);
253 
254     void ShowPopupMenu (
255         weld::Menu& rMainMenu,
256         weld::Menu& rSubMenu,
257         const ::std::vector<TabBar::DeckMenuData>& rMenuData) const;
258     void PopulatePopupMenus(
259         weld::Menu& rMainButton,
260         weld::Menu& rSubMenu,
261         const ::std::vector<TabBar::DeckMenuData>& rMenuData) const;
262     DECL_LINK(OnMenuItemSelected, const OString&, void);
263     DECL_LINK(OnSubMenuItemSelected, const OString&, void);
264     void BroadcastPropertyChange();
265 
266     /** The close of the deck changes the width of the child window.
267         That is only possible if there is no other docking window docked above or below the sidebar.
268         Return whether the width of the child window can be modified.
269     */
270     bool CanModifyChildWindowWidth();
271 
272     /** Set the child window container to a new width.
273         Return the old width.
274     */
275     sal_Int32 SetChildWindowWidth (const sal_Int32 nNewWidth);
276 
277     /** Update the icons displayed in the title bars of the deck and
278         the panels.  This is called once when a deck is created and
279         every time when a data change event is processed.
280     */
281     void UpdateTitleBarIcons();
282 
283     void UpdateDeckOpenState();
284     void RestrictWidth (sal_Int32 nWidth);
285     SfxSplitWindow* GetSplitWindow();
286     void ProcessNewWidth (const sal_Int32 nNewWidth);
287     void UpdateCloseIndicator (const bool bIsIndicatorVisible);
288 
289     /** Typically called when a panel is focused via keyboard.
290         Tries to scroll the deck up or down to make the given panel
291         completely visible.
292     */
293     void ShowPanel (const Panel& rPanel);
294 
295     virtual void SAL_CALL disposing() override;
296 
297     std::unique_ptr<ResourceManager> mpResourceManager;
298 
299 };
300 
301 } // end of namespace sfx2::sidebar
302 
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
304