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