1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "SidebarController.hxx"
25 #include "Deck.hxx"
26 #include "DeckTitleBar.hxx"
27 #include "Panel.hxx"
28 #include "PanelTitleBar.hxx"
29 #include "SidebarPanel.hxx"
30 #include "SidebarResource.hxx"
31 #include "TabBar.hxx"
32 #include "sfx2/sidebar/Theme.hxx"
33 #include "sfx2/sidebar/SidebarChildWindow.hxx"
34 #include "sfx2/sidebar/Tools.hxx"
35 #include "SidebarDockingWindow.hxx"
36 #include "Context.hxx"
37 
38 #include "sfxresid.hxx"
39 #include "sfx2/sfxsids.hrc"
40 #include "sfx2/titledockwin.hxx"
41 #include "sfxlocal.hrc"
42 #include <vcl/floatwin.hxx>
43 #include <vcl/fixed.hxx>
44 #include "splitwin.hxx"
45 #include <svl/smplhint.hxx>
46 #include <tools/link.hxx>
47 #include <toolkit/helper/vclunohelper.hxx>
48 
49 #include <comphelper/componentfactory.hxx>
50 #include <comphelper/processfactory.hxx>
51 #include <comphelper/componentcontext.hxx>
52 #include <comphelper/namedvaluecollection.hxx>
53 
54 #include <com/sun/star/frame/XDispatchProvider.hpp>
55 #include <com/sun/star/lang/XInitialization.hpp>
56 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
57 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
58 #include <com/sun/star/ui/XUIElementFactory.hpp>
59 #include <com/sun/star/util/XURLTransformer.hpp>
60 #include <com/sun/star/util/URL.hpp>
61 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
62 
63 #include <boost/bind.hpp>
64 #include <boost/function.hpp>
65 #include <boost/scoped_array.hpp>
66 
67 
68 using namespace css;
69 using namespace cssu;
70 using ::rtl::OUString;
71 
72 
73 #undef VERBOSE
74 
75 namespace
76 {
77     const static OUString gsReadOnlyCommandName (A2S(".uno:EditDoc"));
78     const static sal_Int32 gnMaximumSidebarWidth (400);
79     const static sal_Int32 gnWidthCloseThreshold (70);
80     const static sal_Int32 gnWidthOpenThreshold (40);
81 }
82 
83 
84 namespace sfx2 { namespace sidebar {
85 
86 namespace {
87     enum MenuId
88     {
89         MID_UNLOCK_TASK_PANEL = 1,
90         MID_LOCK_TASK_PANEL,
91         MID_CUSTOMIZATION,
92         MID_RESTORE_DEFAULT,
93         MID_FIRST_PANEL,
94         MID_FIRST_HIDE = 1000
95     };
96 
97     /** When in doubt, show this deck.
98     */
99     static const ::rtl::OUString gsDefaultDeckId(A2S("PropertyDeck"));
100 }
101 
102 
103 SidebarController::SidebarController (
104     SidebarDockingWindow* pParentWindow,
105     const cssu::Reference<css::frame::XFrame>& rxFrame)
106     : SidebarControllerInterfaceBase(m_aMutex),
107       mpCurrentDeck(),
108       mpParentWindow(pParentWindow),
109       mpTabBar(new TabBar(
110               mpParentWindow,
111               rxFrame,
112               ::boost::bind(&SidebarController::OpenThenSwitchToDeck, this, _1),
113               ::boost::bind(&SidebarController::ShowPopupMenu, this, _1,_2,_3))),
114       mxFrame(rxFrame),
115       maCurrentContext(OUString(), OUString()),
116       maRequestedContext(),
117       msCurrentDeckId(gsDefaultDeckId),
118       msCurrentDeckTitle(),
119       maPropertyChangeForwarder(::boost::bind(&SidebarController::BroadcastPropertyChange, this)),
120       maContextChangeUpdate(::boost::bind(&SidebarController::UpdateConfigurations, this)),
121       mbIsDeckRequestedOpen(),
122       mbIsDeckOpen(),
123       mbCanDeckBeOpened(true),
124       mnSavedSidebarWidth(pParentWindow->GetSizePixel().Width()),
125       maFocusManager(::boost::bind(&SidebarController::ShowPanel, this, _1)),
126       mxReadOnlyModeDispatch(),
127       mbIsDocumentReadOnly(false),
128       mpSplitWindow(NULL),
129       mnWidthOnSplitterButtonDown(0),
130       mpCloseIndicator()
131 {
132     if (pParentWindow == NULL)
133     {
134         OSL_ASSERT(pParentWindow!=NULL);
135             return;
136     }
137 
138     // Listen for context change events.
139     cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
140         css::ui::ContextChangeEventMultiplexer::get(
141             ::comphelper::getProcessComponentContext()));
142     if (xMultiplexer.is())
143         xMultiplexer->addContextChangeEventListener(
144             static_cast<css::ui::XContextChangeEventListener*>(this),
145             mxFrame->getController());
146 
147     // Listen for window events.
148     mpParentWindow->AddEventListener(LINK(this, SidebarController, WindowEventHandler));
149 
150     // Listen for theme property changes.
151     Theme::GetPropertySet()->addPropertyChangeListener(
152         A2S(""),
153         static_cast<css::beans::XPropertyChangeListener*>(this));
154 
155     // Get the dispatch object as preparation to listen for changes of
156     // the read-only state.
157     const util::URL aURL (Tools::GetURL(gsReadOnlyCommandName));
158     mxReadOnlyModeDispatch = Tools::GetDispatch(mxFrame, aURL);
159     if (mxReadOnlyModeDispatch.is())
160         mxReadOnlyModeDispatch->addStatusListener(this, aURL);
161 
162     SwitchToDeck(A2S("default"));
163 }
164 
165 
166 
167 
168 SidebarController::~SidebarController (void)
169 {
170 }
171 
172 
173 
174 
175 void SAL_CALL SidebarController::disposing (void)
176 {
177     maFocusManager.Clear();
178 
179     cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
180         css::ui::ContextChangeEventMultiplexer::get(
181             ::comphelper::getProcessComponentContext()));
182     if (xMultiplexer.is())
183         xMultiplexer->removeAllContextChangeEventListeners(
184             static_cast<css::ui::XContextChangeEventListener*>(this));
185 
186     if (mxReadOnlyModeDispatch.is())
187         mxReadOnlyModeDispatch->removeStatusListener(this, Tools::GetURL(gsReadOnlyCommandName));
188     if (mpSplitWindow != NULL)
189     {
190         mpSplitWindow->RemoveEventListener(LINK(this, SidebarController, WindowEventHandler));
191         mpSplitWindow = NULL;
192     }
193 
194     if (mpParentWindow != NULL)
195     {
196         mpParentWindow->RemoveEventListener(LINK(this, SidebarController, WindowEventHandler));
197         mpParentWindow = NULL;
198     }
199 
200     if (mpCurrentDeck)
201     {
202         mpCurrentDeck->Dispose();
203         mpCurrentDeck->PrintWindowTree();
204         mpCurrentDeck.reset();
205     }
206 
207     mpTabBar.reset();
208 
209     Theme::GetPropertySet()->removePropertyChangeListener(
210         A2S(""),
211         static_cast<css::beans::XPropertyChangeListener*>(this));
212 }
213 
214 
215 
216 
217 void SAL_CALL SidebarController::notifyContextChangeEvent (const css::ui::ContextChangeEventObject& rEvent)
218     throw(cssu::RuntimeException)
219 {
220     // Update to the requested new context asynchronously to avoid
221     // subtle errors caused by SFX2 which in rare cases can not
222     // properly handle a synchronous update.
223     maRequestedContext = Context(
224         rEvent.ApplicationName,
225         rEvent.ContextName);
226     if (maRequestedContext != maCurrentContext)
227         maContextChangeUpdate.RequestCall();
228 }
229 
230 
231 
232 
233 void SAL_CALL SidebarController::disposing (const css::lang::EventObject& rEventObject)
234     throw(cssu::RuntimeException)
235 {
236     (void)rEventObject;
237 
238     dispose();
239 }
240 
241 
242 
243 
244 void SAL_CALL SidebarController::propertyChange (const css::beans::PropertyChangeEvent& rEvent)
245     throw(cssu::RuntimeException)
246 {
247     (void)rEvent;
248 
249     maPropertyChangeForwarder.RequestCall();
250 }
251 
252 
253 
254 
255 void SAL_CALL SidebarController::statusChanged (const css::frame::FeatureStateEvent& rEvent)
256     throw(cssu::RuntimeException)
257 {
258     bool bIsReadWrite (true);
259     if (rEvent.IsEnabled)
260         rEvent.State >>= bIsReadWrite;
261 
262     if (mbIsDocumentReadOnly != !bIsReadWrite)
263     {
264         mbIsDocumentReadOnly = !bIsReadWrite;
265 
266         // Force the current deck to update its panel list.
267         if ( ! mbIsDocumentReadOnly)
268             msCurrentDeckId = gsDefaultDeckId;
269         maCurrentContext = Context();
270         maContextChangeUpdate.RequestCall();
271     }
272 }
273 
274 
275 
276 
277 void SAL_CALL SidebarController::requestLayout (void)
278     throw(cssu::RuntimeException)
279 {
280     if (mpCurrentDeck)
281         mpCurrentDeck->RequestLayout();
282     RestrictWidth();
283 }
284 
285 
286 
287 
288 void SidebarController::BroadcastPropertyChange (void)
289 {
290     DataChangedEvent aEvent (DATACHANGED_USER);
291     mpParentWindow->NotifyAllChilds(aEvent);
292     mpParentWindow->Invalidate(INVALIDATE_CHILDREN);
293 }
294 
295 
296 
297 
298 void SidebarController::NotifyResize (void)
299 {
300     if (mpTabBar == NULL)
301     {
302         OSL_ASSERT(mpTabBar!=NULL);
303         return;
304     }
305 
306     Window* pParentWindow = mpTabBar->GetParent();
307 
308     const sal_Int32 nWidth (pParentWindow->GetSizePixel().Width());
309     const sal_Int32 nHeight (pParentWindow->GetSizePixel().Height());
310 
311     mbIsDeckOpen = (nWidth > TabBar::GetDefaultWidth());
312 
313     if (mnSavedSidebarWidth <= 0)
314         mnSavedSidebarWidth = nWidth;
315 
316     bool bIsDeckVisible;
317     if (mbCanDeckBeOpened)
318     {
319         const bool bIsOpening (nWidth > mnWidthOnSplitterButtonDown);
320         if (bIsOpening)
321             bIsDeckVisible = nWidth >= TabBar::GetDefaultWidth() + gnWidthOpenThreshold;
322         else
323             bIsDeckVisible = nWidth >= TabBar::GetDefaultWidth() + gnWidthCloseThreshold;
324         mbIsDeckRequestedOpen = bIsDeckVisible;
325         UpdateCloseIndicator(!bIsDeckVisible);
326     }
327     else
328         bIsDeckVisible = false;
329 
330     // Place the deck.
331     if (mpCurrentDeck)
332     {
333         if (bIsDeckVisible)
334         {
335             mpCurrentDeck->SetPosSizePixel(0,0, nWidth-TabBar::GetDefaultWidth(), nHeight);
336             mpCurrentDeck->Show();
337             mpCurrentDeck->RequestLayout();
338         }
339         else
340             mpCurrentDeck->Hide();
341     }
342 
343     // Place the tab bar.
344     mpTabBar->SetPosSizePixel(nWidth-TabBar::GetDefaultWidth(),0,TabBar::GetDefaultWidth(),nHeight);
345     mpTabBar->Show();
346 
347     // Determine if the closer of the deck can be shown.
348     if (mpCurrentDeck)
349     {
350         DeckTitleBar* pTitleBar = mpCurrentDeck->GetTitleBar();
351         if (pTitleBar != NULL && pTitleBar->IsVisible())
352             pTitleBar->SetCloserVisible(CanModifyChildWindowWidth());
353     }
354 
355     RestrictWidth();
356 }
357 
358 
359 
360 
361 void SidebarController::ProcessNewWidth (const sal_Int32 nNewWidth)
362 {
363     if ( ! mbIsDeckRequestedOpen)
364         return;
365 
366     if (mbIsDeckRequestedOpen.get())
367      {
368         // Deck became large enough to be shown.  Show it.
369         mnSavedSidebarWidth = nNewWidth;
370         RequestOpenDeck();
371     }
372     else
373     {
374         // Deck became too small.  Close it completely.
375         // If window is wider than the tab bar then mark the deck as being visible, even when it its not.
376         // This is to trigger an adjustment of the width to the width of the tab bar.
377         mbIsDeckOpen = true;
378         RequestCloseDeck();
379 
380         if (mnWidthOnSplitterButtonDown > TabBar::GetDefaultWidth())
381             mnSavedSidebarWidth = mnWidthOnSplitterButtonDown;
382     }
383 }
384 
385 
386 
387 
388 void SidebarController::UpdateConfigurations (void)
389 {
390     if (maCurrentContext != maRequestedContext)
391     {
392         maCurrentContext = maRequestedContext;
393 
394         // Find the set of decks that could be displayed for the new context.
395         ResourceManager::DeckContextDescriptorContainer aDecks;
396         ResourceManager::Instance().GetMatchingDecks (
397             aDecks,
398             maCurrentContext,
399             mbIsDocumentReadOnly,
400             mxFrame);
401 
402         // Notify the tab bar about the updated set of decks.
403         mpTabBar->SetDecks(aDecks);
404 
405         // Find the new deck.  By default that is the same as the old
406         // one.  If that is not set or not enabled, then choose the
407         // first enabled deck.
408         OUString sNewDeckId;
409         for (ResourceManager::DeckContextDescriptorContainer::const_iterator
410                  iDeck(aDecks.begin()),
411                  iEnd(aDecks.end());
412              iDeck!=iEnd;
413              ++iDeck)
414         {
415             if (iDeck->mbIsEnabled)
416             {
417                 if (iDeck->msId.equals(msCurrentDeckId))
418                 {
419                     sNewDeckId = msCurrentDeckId;
420                     break;
421                 }
422                 else if (sNewDeckId.getLength() == 0)
423                     sNewDeckId = iDeck->msId;
424             }
425         }
426 
427         if (sNewDeckId.getLength() == 0)
428         {
429             // We did not find a valid deck.
430             RequestCloseDeck();
431             return;
432         }
433 
434         // Tell the tab bar to highlight the button associated
435         // with the deck.
436         mpTabBar->HighlightDeck(sNewDeckId);
437 
438         SwitchToDeck(
439             *ResourceManager::Instance().GetDeckDescriptor(sNewDeckId),
440             maCurrentContext);
441 
442 #ifdef DEBUG
443         // Show the context name in the deck title bar.
444         if (mpCurrentDeck)
445         {
446             DeckTitleBar* pTitleBar = mpCurrentDeck->GetTitleBar();
447             if (pTitleBar != NULL)
448                 pTitleBar->SetTitle(msCurrentDeckTitle+A2S(" (")+maCurrentContext.msContext+A2S(")"));
449         }
450 #endif
451     }
452 }
453 
454 
455 
456 
457 void SidebarController::OpenThenSwitchToDeck (
458     const ::rtl::OUString& rsDeckId)
459 {
460     RequestOpenDeck();
461     SwitchToDeck(rsDeckId);
462 }
463 
464 
465 
466 
467 void SidebarController::SwitchToDeck (
468     const ::rtl::OUString& rsDeckId)
469 {
470     if ( ! msCurrentDeckId.equals(rsDeckId) || ! mbIsDeckOpen)
471     {
472         const DeckDescriptor* pDeckDescriptor = ResourceManager::Instance().GetDeckDescriptor(rsDeckId);
473         if (pDeckDescriptor != NULL)
474             SwitchToDeck(*pDeckDescriptor, maCurrentContext);
475     }
476 }
477 
478 
479 
480 
481 void SidebarController::SwitchToDeck (
482     const DeckDescriptor& rDeckDescriptor,
483     const Context& rContext)
484 {
485     maFocusManager.Clear();
486 
487     if ( ! msCurrentDeckId.equals(rDeckDescriptor.msId))
488     {
489         // When the deck changes then destroy the deck and all panels
490         // and create everything new.
491         if (mpCurrentDeck)
492         {
493             mpCurrentDeck->Dispose();
494             mpCurrentDeck.reset();
495         }
496 
497         msCurrentDeckId = rDeckDescriptor.msId;
498     }
499     mpTabBar->HighlightDeck(msCurrentDeckId);
500 
501     // Determine the panels to display in the deck.
502     ResourceManager::PanelContextDescriptorContainer aPanelContextDescriptors;
503     ResourceManager::Instance().GetMatchingPanels(
504         aPanelContextDescriptors,
505         rContext,
506         rDeckDescriptor.msId,
507         mxFrame);
508 
509     if (aPanelContextDescriptors.empty())
510     {
511         // There are no panels to be displayed in the current context.
512         if (EnumContext::GetContextEnum(rContext.msContext) != EnumContext::Context_Empty)
513         {
514             // Switch to the "empty" context and try again.
515             SwitchToDeck(
516                 rDeckDescriptor,
517                 Context(
518                     rContext.msApplication,
519                     EnumContext::GetContextName(EnumContext::Context_Empty)));
520             return;
521         }
522         else
523         {
524             // This is already the "empty" context. Looks like we have
525             // to live with an empty deck.
526         }
527     }
528 
529     // Provide a configuration and Deck object.
530     if ( ! mpCurrentDeck)
531     {
532         mpCurrentDeck.reset(
533             new Deck(
534                 rDeckDescriptor,
535                 mpParentWindow,
536                 ::boost::bind(&SidebarController::RequestCloseDeck, this)));
537         msCurrentDeckTitle = rDeckDescriptor.msTitle;
538     }
539     if ( ! mpCurrentDeck)
540         return;
541 
542     // Update the panel list.
543     const sal_Int32 nNewPanelCount (aPanelContextDescriptors.size());
544     SharedPanelContainer aNewPanels;
545     const SharedPanelContainer& rCurrentPanels (mpCurrentDeck->GetPanels());
546     aNewPanels.resize(nNewPanelCount);
547     sal_Int32 nWriteIndex (0);
548     bool bHasPanelSetChanged (false);
549     for (sal_Int32 nReadIndex=0; nReadIndex<nNewPanelCount; ++nReadIndex)
550     {
551         const ResourceManager::PanelContextDescriptor& rPanelContexDescriptor (
552             aPanelContextDescriptors[nReadIndex]);
553 
554         // Determine if the panel can be displayed.
555         const bool bIsPanelVisible (!mbIsDocumentReadOnly || rPanelContexDescriptor.mbShowForReadOnlyDocuments);
556         if ( ! bIsPanelVisible)
557             continue;
558 
559         // Find the corresponding panel among the currently active
560         // panels.
561         SharedPanelContainer::const_iterator iPanel (::std::find_if(
562                 rCurrentPanels.begin(),
563                 rCurrentPanels.end(),
564                 ::boost::bind(&Panel::HasIdPredicate, _1, ::boost::cref(rPanelContexDescriptor.msId))));
565         if (iPanel != rCurrentPanels.end())
566         {
567             // Panel already exists in current deck.  Reuse it.
568             aNewPanels[nWriteIndex] = *iPanel;
569             aNewPanels[nWriteIndex]->SetExpanded(rPanelContexDescriptor.mbIsInitiallyVisible);
570         }
571         else
572         {
573             // Panel does not yet exist.  Create it.
574             aNewPanels[nWriteIndex] = CreatePanel(
575                 rPanelContexDescriptor.msId,
576                 mpCurrentDeck->GetPanelParentWindow(),
577                 rPanelContexDescriptor.mbIsInitiallyVisible,
578                 rContext);
579             bHasPanelSetChanged = true;
580         }
581         if (aNewPanels[nWriteIndex] != NULL)
582         {
583             // Depending on the context we have to change the command
584             // for the "more options" dialog.
585             PanelTitleBar* pTitleBar = aNewPanels[nWriteIndex]->GetTitleBar();
586             if (pTitleBar != NULL)
587             {
588                 pTitleBar->SetMoreOptionsCommand(
589                     rPanelContexDescriptor.msMenuCommand,
590                     mxFrame);
591             }
592 
593             ++nWriteIndex;
594         }
595 
596     }
597     aNewPanels.resize(nWriteIndex);
598 
599     // Activate the deck and the new set of panels.
600     mpCurrentDeck->SetPosSizePixel(
601         0,
602         0,
603         mpParentWindow->GetSizePixel().Width()-TabBar::GetDefaultWidth(),
604         mpParentWindow->GetSizePixel().Height());
605     mpCurrentDeck->SetPanels(aNewPanels);
606     mpCurrentDeck->Show();
607 
608     mpParentWindow->SetText(rDeckDescriptor.msTitle);
609 
610     if (bHasPanelSetChanged)
611         NotifyResize();
612 
613     // Tell the focus manager about the new panels and tab bar
614     // buttons.
615     maFocusManager.SetDeckTitle(mpCurrentDeck->GetTitleBar());
616     maFocusManager.SetPanels(aNewPanels);
617     mpTabBar->UpdateFocusManager(maFocusManager);
618     UpdateTitleBarIcons();
619 }
620 
621 
622 
623 
624 bool SidebarController::ArePanelSetsEqual (
625     const SharedPanelContainer& rCurrentPanels,
626     const ResourceManager::PanelContextDescriptorContainer& rRequestedPanels)
627 {
628     if (rCurrentPanels.size() != rRequestedPanels.size())
629         return false;
630     for (sal_Int32 nIndex=0,nCount=rCurrentPanels.size(); nIndex<nCount; ++nIndex)
631     {
632         if (rCurrentPanels[nIndex] == NULL)
633             return false;
634         if ( ! rCurrentPanels[nIndex]->GetId().equals(rRequestedPanels[nIndex].msId))
635             return false;
636 
637         // Check if the panels still can be displayed.  This may not be the case when
638         // the document just become rea-only.
639         if (mbIsDocumentReadOnly && ! rRequestedPanels[nIndex].mbShowForReadOnlyDocuments)
640             return false;
641     }
642     return true;
643 }
644 
645 
646 
647 
648 SharedPanel SidebarController::CreatePanel (
649     const OUString& rsPanelId,
650     ::Window* pParentWindow,
651     const bool bIsInitiallyExpanded,
652     const Context& rContext)
653 {
654     const PanelDescriptor* pPanelDescriptor = ResourceManager::Instance().GetPanelDescriptor(rsPanelId);
655     if (pPanelDescriptor == NULL)
656         return SharedPanel();
657 
658     // Create the panel which is the parent window of the UIElement.
659     SharedPanel pPanel (new Panel(
660         *pPanelDescriptor,
661         pParentWindow,
662         bIsInitiallyExpanded,
663         ::boost::bind(&Deck::RequestLayout, mpCurrentDeck.get()),
664         ::boost::bind(&SidebarController::GetCurrentContext, this)));
665 
666     // Create the XUIElement.
667     Reference<ui::XUIElement> xUIElement (CreateUIElement(
668             pPanel->GetComponentInterface(),
669             pPanelDescriptor->msImplementationURL,
670             pPanelDescriptor->mbWantsCanvas,
671             rContext));
672     if (xUIElement.is())
673     {
674         // Initialize the panel and add it to the active deck.
675         pPanel->SetUIElement(xUIElement);
676     }
677     else
678     {
679         pPanel.reset();
680     }
681 
682     return pPanel;
683 }
684 
685 
686 
687 
688 Reference<ui::XUIElement> SidebarController::CreateUIElement (
689     const Reference<awt::XWindowPeer>& rxWindow,
690     const ::rtl::OUString& rsImplementationURL,
691     const bool bWantsCanvas,
692     const Context& rContext)
693 {
694     try
695     {
696         const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
697         const Reference<ui::XUIElementFactory> xUIElementFactory (
698             aComponentContext.createComponent("com.sun.star.ui.UIElementFactoryManager"),
699             UNO_QUERY_THROW);
700 
701        // Create the XUIElement.
702         ::comphelper::NamedValueCollection aCreationArguments;
703         aCreationArguments.put("Frame", makeAny(mxFrame));
704         aCreationArguments.put("ParentWindow", makeAny(rxWindow));
705         SfxDockingWindow* pSfxDockingWindow = dynamic_cast<SfxDockingWindow*>(mpParentWindow);
706         if (pSfxDockingWindow != NULL)
707             aCreationArguments.put("SfxBindings", makeAny(sal_uInt64(&pSfxDockingWindow->GetBindings())));
708         aCreationArguments.put("Theme", Theme::GetPropertySet());
709         aCreationArguments.put("Sidebar", makeAny(Reference<ui::XSidebar>(static_cast<ui::XSidebar*>(this))));
710         if (bWantsCanvas)
711         {
712             Reference<rendering::XSpriteCanvas> xCanvas (VCLUnoHelper::GetWindow(rxWindow)->GetSpriteCanvas());
713             aCreationArguments.put("Canvas", makeAny(xCanvas));
714         }
715         aCreationArguments.put("ApplicationName", makeAny(rContext.msApplication));
716         aCreationArguments.put("ContextName", makeAny(rContext.msContext));
717 
718         Reference<ui::XUIElement> xUIElement(
719             xUIElementFactory->createUIElement(
720                 rsImplementationURL,
721                 Sequence<beans::PropertyValue>(aCreationArguments.getPropertyValues())),
722             UNO_QUERY_THROW);
723 
724         return xUIElement;
725     }
726     catch(Exception& rException)
727     {
728         OSL_TRACE("caught exception: %s",
729             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
730         // For some reason we can not create the actual panel.
731         // Probably because its factory was not properly registered.
732         // TODO: provide feedback to developer to better pinpoint the
733         // source of the error.
734 
735         return NULL;
736     }
737 }
738 
739 
740 
741 
742 IMPL_LINK(SidebarController, WindowEventHandler, VclWindowEvent*, pEvent)
743 {
744     if (pEvent==NULL)
745         return sal_False;
746 
747     if (pEvent->GetWindow() == mpParentWindow)
748     {
749         switch (pEvent->GetId())
750         {
751             case VCLEVENT_WINDOW_SHOW:
752             case VCLEVENT_WINDOW_RESIZE:
753                 NotifyResize();
754                 break;
755 
756             case VCLEVENT_WINDOW_DATACHANGED:
757                 // Force an update of deck and tab bar to reflect
758                 // changes in theme (high contrast mode).
759                 Theme::HandleDataChange();
760                 UpdateTitleBarIcons();
761                 mpParentWindow->Invalidate();
762                 break;
763 
764             case SFX_HINT_DYING:
765                 dispose();
766                 break;
767 
768             case VCLEVENT_WINDOW_PAINT:
769                 OSL_TRACE("Paint");
770                 break;
771 
772             default:
773                 break;
774         }
775     }
776     else if (pEvent->GetWindow()==mpSplitWindow && mpSplitWindow!=NULL)
777     {
778         switch (pEvent->GetId())
779         {
780             case VCLEVENT_WINDOW_MOUSEBUTTONDOWN:
781                 mnWidthOnSplitterButtonDown = mpParentWindow->GetSizePixel().Width();
782                 break;
783 
784             case VCLEVENT_WINDOW_MOUSEBUTTONUP:
785             {
786                 ProcessNewWidth(mpParentWindow->GetSizePixel().Width());
787                 mnWidthOnSplitterButtonDown = 0;
788                 break;
789             }
790 
791             case SFX_HINT_DYING:
792                 dispose();
793                 break;
794          }
795     }
796 
797     return sal_True;
798 }
799 
800 
801 
802 
803 void SidebarController::ShowPopupMenu (
804     const Rectangle& rButtonBox,
805     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
806     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
807 {
808     ::boost::shared_ptr<PopupMenu> pMenu = CreatePopupMenu(rDeckSelectionData, rDeckShowData);
809     pMenu->SetSelectHdl(LINK(this, SidebarController, OnMenuItemSelected));
810 
811     // pass toolbox button rect so the menu can stay open on button up
812     Rectangle aBox (rButtonBox);
813     aBox.Move(mpTabBar->GetPosPixel().X(), 0);
814     pMenu->Execute(mpParentWindow, aBox, POPUPMENU_EXECUTE_DOWN);
815 }
816 
817 
818 
819 
820 void SidebarController::ShowDetailMenu (const ::rtl::OUString& rsMenuCommand) const
821 {
822     try
823     {
824         const util::URL aURL (Tools::GetURL(rsMenuCommand));
825         Reference<frame::XDispatch> xDispatch (Tools::GetDispatch(mxFrame, aURL));
826         if (xDispatch.is())
827             xDispatch->dispatch(aURL, Sequence<beans::PropertyValue>());
828     }
829     catch(Exception& rException)
830     {
831         OSL_TRACE("caught exception: %s",
832             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
833     }
834 }
835 
836 
837 
838 
839 ::boost::shared_ptr<PopupMenu> SidebarController::CreatePopupMenu (
840     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
841     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
842 {
843     ::boost::shared_ptr<PopupMenu> pMenu (new PopupMenu());
844     FloatingWindow* pMenuWindow = dynamic_cast<FloatingWindow*>(pMenu->GetWindow());
845     if (pMenuWindow != NULL)
846     {
847         pMenuWindow->SetPopupModeFlags(pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE);
848     }
849 
850     SidebarResource aLocalResource;
851 
852     // Add one entry for every tool panel element to individually make
853     // them visible or hide them.
854     {
855         sal_Int32 nIndex (MID_FIRST_PANEL);
856         for(::std::vector<TabBar::DeckMenuData>::const_iterator
857                 iItem(rDeckSelectionData.begin()),
858                 iEnd(rDeckSelectionData.end());
859             iItem!=iEnd;
860             ++iItem)
861         {
862             pMenu->InsertItem(nIndex, iItem->get<0>(), MIB_RADIOCHECK);
863             pMenu->CheckItem(nIndex, iItem->get<2>());
864             ++nIndex;
865         }
866     }
867 
868     pMenu->InsertSeparator();
869 
870     // Add entry for docking or un-docking the tool panel.
871     if (mpParentWindow->IsFloatingMode())
872         pMenu->InsertItem(MID_LOCK_TASK_PANEL, String(SfxResId(STR_SFX_DOCK)));
873     else
874         pMenu->InsertItem(MID_UNLOCK_TASK_PANEL, String(SfxResId(STR_SFX_UNDOCK)));
875 
876     // Add sub menu for customization (hiding of deck tabs.)
877     PopupMenu* pCustomizationMenu = new PopupMenu();
878     {
879         sal_Int32 nIndex (MID_FIRST_HIDE);
880         for(::std::vector<TabBar::DeckMenuData>::const_iterator
881                 iItem(rDeckShowData.begin()),
882                 iEnd(rDeckShowData.end());
883             iItem!=iEnd;
884             ++iItem)
885         {
886             pCustomizationMenu->InsertItem(nIndex, iItem->get<0>(), MIB_CHECKABLE);
887             pCustomizationMenu->CheckItem(nIndex, iItem->get<2>());
888             ++nIndex;
889         }
890     }
891 
892     pCustomizationMenu->InsertSeparator();
893     pCustomizationMenu->InsertItem(MID_RESTORE_DEFAULT, String(SfxResId(STRING_RESTORE)));
894 
895     pMenu->InsertItem(MID_CUSTOMIZATION, String(SfxResId(STRING_CUSTOMIZATION)));
896     pMenu->SetPopupMenu(MID_CUSTOMIZATION, pCustomizationMenu);
897 
898     pMenu->RemoveDisabledEntries(sal_False, sal_False);
899 
900     return pMenu;
901 }
902 
903 
904 
905 
906 IMPL_LINK(SidebarController, OnMenuItemSelected, Menu*, pMenu)
907 {
908     if (pMenu == NULL)
909     {
910         OSL_ENSURE(pMenu!=NULL, "sfx2::sidebar::SidebarController::OnMenuItemSelected: illegal menu!");
911         return 0;
912     }
913 
914     pMenu->Deactivate();
915     const sal_Int32 nIndex (pMenu->GetCurItemId());
916     switch (nIndex)
917     {
918         case MID_UNLOCK_TASK_PANEL:
919             mpParentWindow->SetFloatingMode(sal_True);
920             break;
921 
922         case MID_LOCK_TASK_PANEL:
923             mpParentWindow->SetFloatingMode(sal_False);
924             break;
925 
926         case MID_RESTORE_DEFAULT:
927             mpTabBar->RestoreHideFlags();
928             break;
929 
930         default:
931         {
932             try
933             {
934                 if (nIndex >= MID_FIRST_PANEL && nIndex<MID_FIRST_HIDE)
935                     SwitchToDeck(mpTabBar->GetDeckIdForIndex(nIndex - MID_FIRST_PANEL));
936                 else if (nIndex >=MID_FIRST_HIDE)
937                     mpTabBar->ToggleHideFlag(nIndex-MID_FIRST_HIDE);
938             }
939             catch (RuntimeException&)
940             {
941             }
942         }
943         break;
944     }
945 
946     return 1;
947 }
948 
949 
950 
951 
952 void SidebarController::RequestCloseDeck (void)
953 {
954     mbIsDeckRequestedOpen = false;
955     UpdateDeckOpenState();
956 }
957 
958 
959 
960 
961 void SidebarController::RequestOpenDeck (void)
962 {
963     mbIsDeckRequestedOpen = true;
964     UpdateDeckOpenState();
965 }
966 
967 
968 
969 
970 void SidebarController::UpdateDeckOpenState (void)
971 {
972     if ( ! mbIsDeckRequestedOpen)
973         // No state requested.
974         return;
975 
976     // Update (change) the open state when it either has not yet been initialized
977     // or when its value differs from the requested state.
978     if ( ! mbIsDeckOpen
979         || mbIsDeckOpen.get() != mbIsDeckRequestedOpen.get())
980     {
981         if (mbIsDeckRequestedOpen.get())
982         {
983             if (mnSavedSidebarWidth <= TabBar::GetDefaultWidth())
984                 SetChildWindowWidth(SidebarChildWindow::GetDefaultWidth(mpParentWindow));
985             else
986                 SetChildWindowWidth(mnSavedSidebarWidth);
987         }
988         else
989         {
990             if ( ! mpParentWindow->IsFloatingMode())
991                 mnSavedSidebarWidth = SetChildWindowWidth(TabBar::GetDefaultWidth());
992             if (mnWidthOnSplitterButtonDown > TabBar::GetDefaultWidth())
993                 mnSavedSidebarWidth = mnWidthOnSplitterButtonDown;
994             mpParentWindow->SetStyle(mpParentWindow->GetStyle() & ~WB_SIZEABLE);
995         }
996 
997         mbIsDeckOpen = mbIsDeckRequestedOpen.get();
998         if (mbIsDeckOpen.get() && mpCurrentDeck)
999             mpCurrentDeck->Show(mbIsDeckOpen.get());
1000         NotifyResize();
1001     }
1002 }
1003 
1004 
1005 
1006 
1007 FocusManager& SidebarController::GetFocusManager (void)
1008 {
1009     return maFocusManager;
1010 }
1011 
1012 
1013 
1014 
1015 bool SidebarController::CanModifyChildWindowWidth (void)
1016 {
1017     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1018     if (pSplitWindow == NULL)
1019         return false;
1020 
1021     sal_uInt16 nRow (0xffff);
1022     sal_uInt16 nColumn (0xffff);
1023     if (pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow))
1024     {
1025         sal_uInt16 nRowCount (pSplitWindow->GetWindowCount(nColumn));
1026         return nRowCount==1;
1027     }
1028     else
1029         return false;
1030 }
1031 
1032 
1033 
1034 
1035 sal_Int32 SidebarController::SetChildWindowWidth (const sal_Int32 nNewWidth)
1036 {
1037     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1038     if (pSplitWindow == NULL)
1039         return 0;
1040 
1041     sal_uInt16 nRow (0xffff);
1042     sal_uInt16 nColumn (0xffff);
1043     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
1044     const long nColumnWidth (pSplitWindow->GetLineSize(nColumn));
1045 
1046     Window* pWindow = mpParentWindow;
1047     const Point aWindowPosition (pWindow->GetPosPixel());
1048     const Size aWindowSize (pWindow->GetSizePixel());
1049 
1050     pSplitWindow->MoveWindow(
1051         mpParentWindow,
1052         Size(nNewWidth, aWindowSize.Height()),
1053         nColumn,
1054         nRow);
1055     static_cast<SplitWindow*>(pSplitWindow)->Split();
1056 
1057     return static_cast<sal_Int32>(nColumnWidth);
1058 }
1059 
1060 
1061 
1062 
1063 void SidebarController::RestrictWidth (void)
1064 {
1065     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1066     if (pSplitWindow != NULL)
1067     {
1068         const sal_uInt16 nId (pSplitWindow->GetItemId(mpParentWindow));
1069         const sal_uInt16 nSetId (pSplitWindow->GetSet(nId));
1070         pSplitWindow->SetItemSizeRange(
1071             nSetId,
1072             Range(TabBar::GetDefaultWidth(), gnMaximumSidebarWidth));
1073     }
1074 }
1075 
1076 
1077 
1078 
1079 SfxSplitWindow* SidebarController::GetSplitWindow (void)
1080 {
1081     if (mpParentWindow != NULL)
1082     {
1083         SfxSplitWindow* pSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
1084         if (pSplitWindow != mpSplitWindow)
1085         {
1086             if (mpSplitWindow != NULL)
1087                 mpSplitWindow->RemoveEventListener(LINK(this, SidebarController, WindowEventHandler));
1088 
1089             mpSplitWindow = pSplitWindow;
1090 
1091             if (mpSplitWindow != NULL)
1092                 mpSplitWindow->AddEventListener(LINK(this, SidebarController, WindowEventHandler));
1093         }
1094         return mpSplitWindow;
1095     }
1096     else
1097         return NULL;
1098 }
1099 
1100 
1101 
1102 
1103 void SidebarController::UpdateCloseIndicator (const bool bCloseAfterDrag)
1104 {
1105     if (mpParentWindow == NULL)
1106         return;
1107 
1108     if (bCloseAfterDrag)
1109     {
1110         // Make sure that the indicator exists.
1111         if ( ! mpCloseIndicator)
1112         {
1113             mpCloseIndicator.reset(new FixedImage(mpParentWindow));
1114             FixedImage* pFixedImage = static_cast<FixedImage*>(mpCloseIndicator.get());
1115             const Image aImage (Theme::GetImage(Theme::Image_CloseIndicator));
1116             pFixedImage->SetImage(aImage);
1117             pFixedImage->SetSizePixel(aImage.GetSizePixel());
1118             pFixedImage->SetBackground(Theme::GetWallpaper(Theme::Paint_DeckBackground));
1119         }
1120 
1121         // Place and show the indicator.
1122         const Size aWindowSize (mpParentWindow->GetSizePixel());
1123         const Size aImageSize (mpCloseIndicator->GetSizePixel());
1124         mpCloseIndicator->SetPosPixel(
1125             Point(
1126                 aWindowSize.Width() - TabBar::GetDefaultWidth() - aImageSize.Width(),
1127                 (aWindowSize.Height() - aImageSize.Height())/2));
1128         mpCloseIndicator->Show();
1129     }
1130     else
1131     {
1132         // Hide but don't delete the indicator.
1133         if (mpCloseIndicator)
1134             mpCloseIndicator->Hide();
1135     }
1136 }
1137 
1138 
1139 
1140 
1141 void SidebarController::UpdateTitleBarIcons (void)
1142 {
1143     if ( ! mpCurrentDeck)
1144         return;
1145 
1146     const bool bIsHighContrastModeActive (Theme::IsHighContrastMode());
1147     const ResourceManager& rResourceManager (ResourceManager::Instance());
1148 
1149     // Update the deck icon.
1150     const DeckDescriptor* pDeckDescriptor = rResourceManager.GetDeckDescriptor(mpCurrentDeck->GetId());
1151     if (pDeckDescriptor != NULL && mpCurrentDeck->GetTitleBar())
1152     {
1153         const OUString sIconURL(
1154             bIsHighContrastModeActive
1155                 ? pDeckDescriptor->msHighContrastTitleBarIconURL
1156                 : pDeckDescriptor->msTitleBarIconURL);
1157         mpCurrentDeck->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1158     }
1159 
1160     // Update the panel icons.
1161     const SharedPanelContainer& rPanels (mpCurrentDeck->GetPanels());
1162     for (SharedPanelContainer::const_iterator
1163              iPanel(rPanels.begin()), iEnd(rPanels.end());
1164              iPanel!=iEnd;
1165              ++iPanel)
1166     {
1167         if ( ! *iPanel)
1168             continue;
1169         if ((*iPanel)->GetTitleBar() == NULL)
1170             continue;
1171         const PanelDescriptor* pPanelDescriptor = rResourceManager.GetPanelDescriptor((*iPanel)->GetId());
1172         if (pPanelDescriptor == NULL)
1173             continue;
1174         const OUString sIconURL (
1175             bIsHighContrastModeActive
1176                ? pPanelDescriptor->msHighContrastTitleBarIconURL
1177                : pPanelDescriptor->msTitleBarIconURL);
1178         (*iPanel)->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1179     }
1180 }
1181 
1182 
1183 
1184 
1185 void SidebarController::ShowPanel (const Panel& rPanel)
1186 {
1187     if (mpCurrentDeck)
1188         mpCurrentDeck->ShowPanel(rPanel);
1189 }
1190 
1191 
1192 
1193 
1194 Context SidebarController::GetCurrentContext (void) const
1195 {
1196     return maCurrentContext;
1197 }
1198 
1199 
1200 } } // end of namespace sfx2::sidebar
1201