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))),
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>& rMenuData) const
806 {
807     ::boost::shared_ptr<PopupMenu> pMenu = CreatePopupMenu(rMenuData);
808     pMenu->SetSelectHdl(LINK(this, SidebarController, OnMenuItemSelected));
809 
810     // pass toolbox button rect so the menu can stay open on button up
811     Rectangle aBox (rButtonBox);
812     aBox.Move(mpTabBar->GetPosPixel().X(), 0);
813     pMenu->Execute(mpParentWindow, aBox, POPUPMENU_EXECUTE_DOWN);
814 }
815 
816 
817 
818 
819 void SidebarController::ShowDetailMenu (const ::rtl::OUString& rsMenuCommand) const
820 {
821     try
822     {
823         const util::URL aURL (Tools::GetURL(rsMenuCommand));
824         Reference<frame::XDispatch> xDispatch (Tools::GetDispatch(mxFrame, aURL));
825         if (xDispatch.is())
826             xDispatch->dispatch(aURL, Sequence<beans::PropertyValue>());
827     }
828     catch(Exception& rException)
829     {
830         OSL_TRACE("caught exception: %s",
831             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
832     }
833 }
834 
835 
836 
837 
838 ::boost::shared_ptr<PopupMenu> SidebarController::CreatePopupMenu (
839     const ::std::vector<TabBar::DeckMenuData>& rMenuData) const
840 {
841     // Create the top level popup menu.
842     ::boost::shared_ptr<PopupMenu> pMenu (new PopupMenu());
843     FloatingWindow* pMenuWindow = dynamic_cast<FloatingWindow*>(pMenu->GetWindow());
844     if (pMenuWindow != NULL)
845     {
846         pMenuWindow->SetPopupModeFlags(pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE);
847     }
848 
849     // Create sub menu for customization (hiding of deck tabs.)
850     PopupMenu* pCustomizationMenu = new PopupMenu();
851 
852     SidebarResource aLocalResource;
853 
854     // Add one entry for every tool panel element to individually make
855     // them visible or hide them.
856     sal_Int32 nIndex (0);
857     for(::std::vector<TabBar::DeckMenuData>::const_iterator
858             iItem(rMenuData.begin()),
859             iEnd(rMenuData.end());
860         iItem!=iEnd;
861         ++iItem,++nIndex)
862     {
863         const sal_Int32 nMenuIndex (nIndex+MID_FIRST_PANEL);
864         pMenu->InsertItem(nMenuIndex, iItem->msDisplayName, MIB_RADIOCHECK);
865         pMenu->CheckItem(nMenuIndex, iItem->mbIsCurrentDeck ? sal_True : sal_False);
866         pMenu->EnableItem(nMenuIndex, (iItem->mbIsEnabled&&iItem->mbIsActive) ? sal_True : sal_False);
867 
868         const sal_Int32 nSubMenuIndex (nIndex+MID_FIRST_HIDE);
869         if (iItem->mbIsCurrentDeck)
870         {
871             // Don't allow the currently visible deck to be disabled.
872             pCustomizationMenu->InsertItem(nSubMenuIndex, iItem->msDisplayName, MIB_RADIOCHECK);
873             pCustomizationMenu->CheckItem(nSubMenuIndex, sal_True);
874         }
875         else
876         {
877             pCustomizationMenu->InsertItem(nSubMenuIndex, iItem->msDisplayName, MIB_CHECKABLE);
878             pCustomizationMenu->CheckItem(nSubMenuIndex, iItem->mbIsActive ? sal_True : sal_False);
879         }
880     }
881 
882     pMenu->InsertSeparator();
883 
884     // Add entry for docking or un-docking the tool panel.
885     if (mpParentWindow->IsFloatingMode())
886         pMenu->InsertItem(MID_LOCK_TASK_PANEL, String(SfxResId(STR_SFX_DOCK)));
887     else
888         pMenu->InsertItem(MID_UNLOCK_TASK_PANEL, String(SfxResId(STR_SFX_UNDOCK)));
889 
890     pCustomizationMenu->InsertSeparator();
891     pCustomizationMenu->InsertItem(MID_RESTORE_DEFAULT, String(SfxResId(STRING_RESTORE)));
892 
893     pMenu->InsertItem(MID_CUSTOMIZATION, String(SfxResId(STRING_CUSTOMIZATION)));
894     pMenu->SetPopupMenu(MID_CUSTOMIZATION, pCustomizationMenu);
895 
896     pMenu->RemoveDisabledEntries(sal_False, sal_False);
897 
898     return pMenu;
899 }
900 
901 
902 
903 
904 IMPL_LINK(SidebarController, OnMenuItemSelected, Menu*, pMenu)
905 {
906     if (pMenu == NULL)
907     {
908         OSL_ENSURE(pMenu!=NULL, "sfx2::sidebar::SidebarController::OnMenuItemSelected: illegal menu!");
909         return 0;
910     }
911 
912     pMenu->Deactivate();
913     const sal_Int32 nIndex (pMenu->GetCurItemId());
914     switch (nIndex)
915     {
916         case MID_UNLOCK_TASK_PANEL:
917             mpParentWindow->SetFloatingMode(sal_True);
918             break;
919 
920         case MID_LOCK_TASK_PANEL:
921             mpParentWindow->SetFloatingMode(sal_False);
922             break;
923 
924         case MID_RESTORE_DEFAULT:
925             mpTabBar->RestoreHideFlags();
926             break;
927 
928         default:
929         {
930             try
931             {
932                 if (nIndex >= MID_FIRST_PANEL && nIndex<MID_FIRST_HIDE)
933                     SwitchToDeck(mpTabBar->GetDeckIdForIndex(nIndex - MID_FIRST_PANEL));
934                 else if (nIndex >=MID_FIRST_HIDE)
935                     if (pMenu->GetItemBits(nIndex) == MIB_CHECKABLE)
936                         mpTabBar->ToggleHideFlag(nIndex-MID_FIRST_HIDE);
937             }
938             catch (RuntimeException&)
939             {
940             }
941         }
942         break;
943     }
944 
945     return 1;
946 }
947 
948 
949 
950 
951 void SidebarController::RequestCloseDeck (void)
952 {
953     mbIsDeckRequestedOpen = false;
954     UpdateDeckOpenState();
955 }
956 
957 
958 
959 
960 void SidebarController::RequestOpenDeck (void)
961 {
962     mbIsDeckRequestedOpen = true;
963     UpdateDeckOpenState();
964 }
965 
966 
967 
968 
969 void SidebarController::UpdateDeckOpenState (void)
970 {
971     if ( ! mbIsDeckRequestedOpen)
972         // No state requested.
973         return;
974 
975     // Update (change) the open state when it either has not yet been initialized
976     // or when its value differs from the requested state.
977     if ( ! mbIsDeckOpen
978         || mbIsDeckOpen.get() != mbIsDeckRequestedOpen.get())
979     {
980         if (mbIsDeckRequestedOpen.get())
981         {
982             if (mnSavedSidebarWidth <= TabBar::GetDefaultWidth())
983                 SetChildWindowWidth(SidebarChildWindow::GetDefaultWidth(mpParentWindow));
984             else
985                 SetChildWindowWidth(mnSavedSidebarWidth);
986         }
987         else
988         {
989             if ( ! mpParentWindow->IsFloatingMode())
990                 mnSavedSidebarWidth = SetChildWindowWidth(TabBar::GetDefaultWidth());
991             if (mnWidthOnSplitterButtonDown > TabBar::GetDefaultWidth())
992                 mnSavedSidebarWidth = mnWidthOnSplitterButtonDown;
993             mpParentWindow->SetStyle(mpParentWindow->GetStyle() & ~WB_SIZEABLE);
994         }
995 
996         mbIsDeckOpen = mbIsDeckRequestedOpen.get();
997         if (mbIsDeckOpen.get() && mpCurrentDeck)
998             mpCurrentDeck->Show(mbIsDeckOpen.get());
999         NotifyResize();
1000     }
1001 }
1002 
1003 
1004 
1005 
1006 FocusManager& SidebarController::GetFocusManager (void)
1007 {
1008     return maFocusManager;
1009 }
1010 
1011 
1012 
1013 
1014 bool SidebarController::CanModifyChildWindowWidth (void)
1015 {
1016     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1017     if (pSplitWindow == NULL)
1018         return false;
1019 
1020     sal_uInt16 nRow (0xffff);
1021     sal_uInt16 nColumn (0xffff);
1022     if (pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow))
1023     {
1024         sal_uInt16 nRowCount (pSplitWindow->GetWindowCount(nColumn));
1025         return nRowCount==1;
1026     }
1027     else
1028         return false;
1029 }
1030 
1031 
1032 
1033 
1034 sal_Int32 SidebarController::SetChildWindowWidth (const sal_Int32 nNewWidth)
1035 {
1036     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1037     if (pSplitWindow == NULL)
1038         return 0;
1039 
1040     sal_uInt16 nRow (0xffff);
1041     sal_uInt16 nColumn (0xffff);
1042     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
1043     const long nColumnWidth (pSplitWindow->GetLineSize(nColumn));
1044 
1045     Window* pWindow = mpParentWindow;
1046     const Point aWindowPosition (pWindow->GetPosPixel());
1047     const Size aWindowSize (pWindow->GetSizePixel());
1048 
1049     pSplitWindow->MoveWindow(
1050         mpParentWindow,
1051         Size(nNewWidth, aWindowSize.Height()),
1052         nColumn,
1053         nRow);
1054     static_cast<SplitWindow*>(pSplitWindow)->Split();
1055 
1056     return static_cast<sal_Int32>(nColumnWidth);
1057 }
1058 
1059 
1060 
1061 
1062 void SidebarController::RestrictWidth (void)
1063 {
1064     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1065     if (pSplitWindow != NULL)
1066     {
1067         const sal_uInt16 nId (pSplitWindow->GetItemId(mpParentWindow));
1068         const sal_uInt16 nSetId (pSplitWindow->GetSet(nId));
1069         pSplitWindow->SetItemSizeRange(
1070             nSetId,
1071             Range(TabBar::GetDefaultWidth(), gnMaximumSidebarWidth));
1072     }
1073 }
1074 
1075 
1076 
1077 
1078 SfxSplitWindow* SidebarController::GetSplitWindow (void)
1079 {
1080     if (mpParentWindow != NULL)
1081     {
1082         SfxSplitWindow* pSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
1083         if (pSplitWindow != mpSplitWindow)
1084         {
1085             if (mpSplitWindow != NULL)
1086                 mpSplitWindow->RemoveEventListener(LINK(this, SidebarController, WindowEventHandler));
1087 
1088             mpSplitWindow = pSplitWindow;
1089 
1090             if (mpSplitWindow != NULL)
1091                 mpSplitWindow->AddEventListener(LINK(this, SidebarController, WindowEventHandler));
1092         }
1093         return mpSplitWindow;
1094     }
1095     else
1096         return NULL;
1097 }
1098 
1099 
1100 
1101 
1102 void SidebarController::UpdateCloseIndicator (const bool bCloseAfterDrag)
1103 {
1104     if (mpParentWindow == NULL)
1105         return;
1106 
1107     if (bCloseAfterDrag)
1108     {
1109         // Make sure that the indicator exists.
1110         if ( ! mpCloseIndicator)
1111         {
1112             mpCloseIndicator.reset(new FixedImage(mpParentWindow));
1113             FixedImage* pFixedImage = static_cast<FixedImage*>(mpCloseIndicator.get());
1114             const Image aImage (Theme::GetImage(Theme::Image_CloseIndicator));
1115             pFixedImage->SetImage(aImage);
1116             pFixedImage->SetSizePixel(aImage.GetSizePixel());
1117             pFixedImage->SetBackground(Theme::GetWallpaper(Theme::Paint_DeckBackground));
1118         }
1119 
1120         // Place and show the indicator.
1121         const Size aWindowSize (mpParentWindow->GetSizePixel());
1122         const Size aImageSize (mpCloseIndicator->GetSizePixel());
1123         mpCloseIndicator->SetPosPixel(
1124             Point(
1125                 aWindowSize.Width() - TabBar::GetDefaultWidth() - aImageSize.Width(),
1126                 (aWindowSize.Height() - aImageSize.Height())/2));
1127         mpCloseIndicator->Show();
1128     }
1129     else
1130     {
1131         // Hide but don't delete the indicator.
1132         if (mpCloseIndicator)
1133             mpCloseIndicator->Hide();
1134     }
1135 }
1136 
1137 
1138 
1139 
1140 void SidebarController::UpdateTitleBarIcons (void)
1141 {
1142     if ( ! mpCurrentDeck)
1143         return;
1144 
1145     const bool bIsHighContrastModeActive (Theme::IsHighContrastMode());
1146     const ResourceManager& rResourceManager (ResourceManager::Instance());
1147 
1148     // Update the deck icon.
1149     const DeckDescriptor* pDeckDescriptor = rResourceManager.GetDeckDescriptor(mpCurrentDeck->GetId());
1150     if (pDeckDescriptor != NULL && mpCurrentDeck->GetTitleBar())
1151     {
1152         const OUString sIconURL(
1153             bIsHighContrastModeActive
1154                 ? pDeckDescriptor->msHighContrastTitleBarIconURL
1155                 : pDeckDescriptor->msTitleBarIconURL);
1156         mpCurrentDeck->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1157     }
1158 
1159     // Update the panel icons.
1160     const SharedPanelContainer& rPanels (mpCurrentDeck->GetPanels());
1161     for (SharedPanelContainer::const_iterator
1162              iPanel(rPanels.begin()), iEnd(rPanels.end());
1163              iPanel!=iEnd;
1164              ++iPanel)
1165     {
1166         if ( ! *iPanel)
1167             continue;
1168         if ((*iPanel)->GetTitleBar() == NULL)
1169             continue;
1170         const PanelDescriptor* pPanelDescriptor = rResourceManager.GetPanelDescriptor((*iPanel)->GetId());
1171         if (pPanelDescriptor == NULL)
1172             continue;
1173         const OUString sIconURL (
1174             bIsHighContrastModeActive
1175                ? pPanelDescriptor->msHighContrastTitleBarIconURL
1176                : pPanelDescriptor->msTitleBarIconURL);
1177         (*iPanel)->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1178     }
1179 }
1180 
1181 
1182 
1183 
1184 void SidebarController::ShowPanel (const Panel& rPanel)
1185 {
1186     if (mpCurrentDeck)
1187         mpCurrentDeck->ShowPanel(rPanel);
1188 }
1189 
1190 
1191 
1192 
1193 Context SidebarController::GetCurrentContext (void) const
1194 {
1195     return maCurrentContext;
1196 }
1197 
1198 
1199 } } // end of namespace sfx2::sidebar
1200