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         }
577         else
578         {
579             // Panel does not yet exist.  Create it.
580             aNewPanels[nWriteIndex] = CreatePanel(
581                 rPanelContexDescriptor.msId,
582                 mpCurrentDeck->GetPanelParentWindow());
583             bHasPanelSetChanged = true;
584         }
585         if (aNewPanels[nWriteIndex] != NULL)
586         {
587             // Depending on the context we have to collapse the panel.
588             aNewPanels[nWriteIndex]->SetExpanded(rPanelContexDescriptor.mbIsInitiallyVisible);
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 {
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         ::boost::bind(&Deck::RequestLayout, mpCurrentDeck.get()) ) );
663 
664     // Create the XUIElement.
665     Reference<ui::XUIElement> xUIElement (CreateUIElement(
666             pPanel->GetComponentInterface(),
667             pPanelDescriptor->msImplementationURL,
668             pPanelDescriptor->mbWantsCanvas));
669     if (xUIElement.is())
670     {
671         // Initialize the panel and add it to the active deck.
672         pPanel->SetUIElement(xUIElement);
673     }
674     else
675     {
676         pPanel.reset();
677     }
678 
679     return pPanel;
680 }
681 
682 
683 
684 
685 Reference<ui::XUIElement> SidebarController::CreateUIElement (
686     const Reference<awt::XWindowPeer>& rxWindow,
687     const ::rtl::OUString& rsImplementationURL,
688     const bool bWantsCanvas)
689 {
690     try
691     {
692         const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
693         const Reference<ui::XUIElementFactory> xUIElementFactory (
694             aComponentContext.createComponent("com.sun.star.ui.UIElementFactoryManager"),
695             UNO_QUERY_THROW);
696 
697        // Create the XUIElement.
698         ::comphelper::NamedValueCollection aCreationArguments;
699         aCreationArguments.put("Frame", makeAny(mxFrame));
700         aCreationArguments.put("ParentWindow", makeAny(rxWindow));
701         SfxDockingWindow* pSfxDockingWindow = dynamic_cast<SfxDockingWindow*>(mpParentWindow);
702         if (pSfxDockingWindow != NULL)
703             aCreationArguments.put("SfxBindings", makeAny(sal_uInt64(&pSfxDockingWindow->GetBindings())));
704         aCreationArguments.put("Theme", Theme::GetPropertySet());
705         aCreationArguments.put("Sidebar", makeAny(Reference<ui::XSidebar>(static_cast<ui::XSidebar*>(this))));
706         if (bWantsCanvas)
707         {
708             Reference<rendering::XSpriteCanvas> xCanvas (VCLUnoHelper::GetWindow(rxWindow)->GetSpriteCanvas());
709             aCreationArguments.put("Canvas", makeAny(xCanvas));
710         }
711 
712         Reference<ui::XUIElement> xUIElement(
713             xUIElementFactory->createUIElement(
714                 rsImplementationURL,
715                 Sequence<beans::PropertyValue>(aCreationArguments.getPropertyValues())),
716             UNO_QUERY_THROW);
717 
718         return xUIElement;
719     }
720     catch(Exception& rException)
721     {
722         OSL_TRACE("caught exception: %s",
723             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
724         // For some reason we can not create the actual panel.
725         // Probably because its factory was not properly registered.
726         // TODO: provide feedback to developer to better pinpoint the
727         // source of the error.
728 
729         return NULL;
730     }
731 }
732 
733 
734 
735 
736 IMPL_LINK(SidebarController, WindowEventHandler, VclWindowEvent*, pEvent)
737 {
738     if (pEvent==NULL)
739         return sal_False;
740 
741     if (pEvent->GetWindow() == mpParentWindow)
742     {
743         switch (pEvent->GetId())
744         {
745             case VCLEVENT_WINDOW_SHOW:
746             case VCLEVENT_WINDOW_RESIZE:
747                 NotifyResize();
748                 break;
749 
750             case VCLEVENT_WINDOW_DATACHANGED:
751                 // Force an update of deck and tab bar to reflect
752                 // changes in theme (high contrast mode).
753                 Theme::HandleDataChange();
754                 UpdateTitleBarIcons();
755                 mpParentWindow->Invalidate();
756                 break;
757 
758             case SFX_HINT_DYING:
759                 dispose();
760                 break;
761 
762             case VCLEVENT_WINDOW_PAINT:
763                 OSL_TRACE("Paint");
764                 break;
765 
766             default:
767                 break;
768         }
769     }
770     else if (pEvent->GetWindow()==mpSplitWindow && mpSplitWindow!=NULL)
771     {
772         switch (pEvent->GetId())
773         {
774             case VCLEVENT_WINDOW_MOUSEBUTTONDOWN:
775                 mnWidthOnSplitterButtonDown = mpParentWindow->GetSizePixel().Width();
776                 break;
777 
778             case VCLEVENT_WINDOW_MOUSEBUTTONUP:
779             {
780                 ProcessNewWidth(mpParentWindow->GetSizePixel().Width());
781                 mnWidthOnSplitterButtonDown = 0;
782                 break;
783             }
784 
785             case SFX_HINT_DYING:
786                 dispose();
787                 break;
788          }
789     }
790 
791     return sal_True;
792 }
793 
794 
795 
796 
797 void SidebarController::ShowPopupMenu (
798     const Rectangle& rButtonBox,
799     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
800     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
801 {
802     ::boost::shared_ptr<PopupMenu> pMenu = CreatePopupMenu(rDeckSelectionData, rDeckShowData);
803     pMenu->SetSelectHdl(LINK(this, SidebarController, OnMenuItemSelected));
804 
805     // pass toolbox button rect so the menu can stay open on button up
806     Rectangle aBox (rButtonBox);
807     aBox.Move(mpTabBar->GetPosPixel().X(), 0);
808     pMenu->Execute(mpParentWindow, aBox, POPUPMENU_EXECUTE_DOWN);
809 }
810 
811 
812 
813 
814 void SidebarController::ShowDetailMenu (const ::rtl::OUString& rsMenuCommand) const
815 {
816     try
817     {
818         const util::URL aURL (Tools::GetURL(rsMenuCommand));
819         Reference<frame::XDispatch> xDispatch (Tools::GetDispatch(mxFrame, aURL));
820         if (xDispatch.is())
821             xDispatch->dispatch(aURL, Sequence<beans::PropertyValue>());
822     }
823     catch(Exception& rException)
824     {
825         OSL_TRACE("caught exception: %s",
826             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
827     }
828 }
829 
830 
831 
832 
833 ::boost::shared_ptr<PopupMenu> SidebarController::CreatePopupMenu (
834     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
835     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
836 {
837     ::boost::shared_ptr<PopupMenu> pMenu (new PopupMenu());
838     FloatingWindow* pMenuWindow = dynamic_cast<FloatingWindow*>(pMenu->GetWindow());
839     if (pMenuWindow != NULL)
840     {
841         pMenuWindow->SetPopupModeFlags(pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE);
842     }
843 
844     SidebarResource aLocalResource;
845 
846     // Add one entry for every tool panel element to individually make
847     // them visible or hide them.
848     {
849         sal_Int32 nIndex (MID_FIRST_PANEL);
850         for(::std::vector<TabBar::DeckMenuData>::const_iterator
851                 iItem(rDeckSelectionData.begin()),
852                 iEnd(rDeckSelectionData.end());
853             iItem!=iEnd;
854             ++iItem)
855         {
856             pMenu->InsertItem(nIndex, iItem->get<0>(), MIB_RADIOCHECK);
857             pMenu->CheckItem(nIndex, iItem->get<2>());
858             ++nIndex;
859         }
860     }
861 
862     pMenu->InsertSeparator();
863 
864     // Add entry for docking or un-docking the tool panel.
865     if (mpParentWindow->IsFloatingMode())
866         pMenu->InsertItem(MID_LOCK_TASK_PANEL, String(SfxResId(STR_SFX_DOCK)));
867     else
868         pMenu->InsertItem(MID_UNLOCK_TASK_PANEL, String(SfxResId(STR_SFX_UNDOCK)));
869 
870     // Add sub menu for customization (hiding of deck tabs.)
871     PopupMenu* pCustomizationMenu = new PopupMenu();
872     {
873         sal_Int32 nIndex (MID_FIRST_HIDE);
874         for(::std::vector<TabBar::DeckMenuData>::const_iterator
875                 iItem(rDeckShowData.begin()),
876                 iEnd(rDeckShowData.end());
877             iItem!=iEnd;
878             ++iItem)
879         {
880             pCustomizationMenu->InsertItem(nIndex, iItem->get<0>(), MIB_CHECKABLE);
881             pCustomizationMenu->CheckItem(nIndex, iItem->get<2>());
882             ++nIndex;
883         }
884     }
885 
886     pCustomizationMenu->InsertSeparator();
887     pCustomizationMenu->InsertItem(MID_RESTORE_DEFAULT, String(SfxResId(STRING_RESTORE)));
888 
889     pMenu->InsertItem(MID_CUSTOMIZATION, String(SfxResId(STRING_CUSTOMIZATION)));
890     pMenu->SetPopupMenu(MID_CUSTOMIZATION, pCustomizationMenu);
891 
892     pMenu->RemoveDisabledEntries(sal_False, sal_False);
893 
894     return pMenu;
895 }
896 
897 
898 
899 
900 IMPL_LINK(SidebarController, OnMenuItemSelected, Menu*, pMenu)
901 {
902     if (pMenu == NULL)
903     {
904         OSL_ENSURE(pMenu!=NULL, "sfx2::sidebar::SidebarController::OnMenuItemSelected: illegal menu!");
905         return 0;
906     }
907 
908     pMenu->Deactivate();
909     const sal_Int32 nIndex (pMenu->GetCurItemId());
910     switch (nIndex)
911     {
912         case MID_UNLOCK_TASK_PANEL:
913             mpParentWindow->SetFloatingMode(sal_True);
914             break;
915 
916         case MID_LOCK_TASK_PANEL:
917             mpParentWindow->SetFloatingMode(sal_False);
918             break;
919 
920         case MID_RESTORE_DEFAULT:
921             mpTabBar->RestoreHideFlags();
922             break;
923 
924         default:
925         {
926             try
927             {
928                 if (nIndex >= MID_FIRST_PANEL && nIndex<MID_FIRST_HIDE)
929                     SwitchToDeck(mpTabBar->GetDeckIdForIndex(nIndex - MID_FIRST_PANEL));
930                 else if (nIndex >=MID_FIRST_HIDE)
931                     mpTabBar->ToggleHideFlag(nIndex-MID_FIRST_HIDE);
932             }
933             catch (RuntimeException&)
934             {
935             }
936         }
937         break;
938     }
939 
940     return 1;
941 }
942 
943 
944 
945 
946 void SidebarController::RequestCloseDeck (void)
947 {
948     mbIsDeckRequestedOpen = false;
949     UpdateDeckOpenState();
950 }
951 
952 
953 
954 
955 void SidebarController::RequestOpenDeck (void)
956 {
957     mbIsDeckRequestedOpen = true;
958     UpdateDeckOpenState();
959 }
960 
961 
962 
963 
964 void SidebarController::UpdateDeckOpenState (void)
965 {
966     if ( ! mbIsDeckRequestedOpen)
967         // No state requested.
968         return;
969 
970     // Update (change) the open state when it either has not yet been initialized
971     // or when its value differs from the requested state.
972     if ( ! mbIsDeckOpen
973         || mbIsDeckOpen.get() != mbIsDeckRequestedOpen.get())
974     {
975         if (mbIsDeckRequestedOpen.get())
976         {
977             if (mnSavedSidebarWidth <= TabBar::GetDefaultWidth())
978                 SetChildWindowWidth(SidebarChildWindow::GetDefaultWidth(mpParentWindow));
979             else
980                 SetChildWindowWidth(mnSavedSidebarWidth);
981         }
982         else
983         {
984             if ( ! mpParentWindow->IsFloatingMode())
985                 mnSavedSidebarWidth = SetChildWindowWidth(TabBar::GetDefaultWidth());
986             if (mnWidthOnSplitterButtonDown > TabBar::GetDefaultWidth())
987                 mnSavedSidebarWidth = mnWidthOnSplitterButtonDown;
988             mpParentWindow->SetStyle(mpParentWindow->GetStyle() & ~WB_SIZEABLE);
989         }
990 
991         mbIsDeckOpen = mbIsDeckRequestedOpen.get();
992         if (mbIsDeckOpen.get() && mpCurrentDeck)
993             mpCurrentDeck->Show(mbIsDeckOpen.get());
994         NotifyResize();
995     }
996 }
997 
998 
999 
1000 
1001 FocusManager& SidebarController::GetFocusManager (void)
1002 {
1003     return maFocusManager;
1004 }
1005 
1006 
1007 
1008 
1009 bool SidebarController::CanModifyChildWindowWidth (void)
1010 {
1011     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1012     if (pSplitWindow == NULL)
1013         return false;
1014 
1015     sal_uInt16 nRow (0xffff);
1016     sal_uInt16 nColumn (0xffff);
1017     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
1018 
1019     sal_uInt16 nRowCount (pSplitWindow->GetWindowCount(nColumn));
1020 
1021     return nRowCount==1;
1022 }
1023 
1024 
1025 
1026 
1027 sal_Int32 SidebarController::SetChildWindowWidth (const sal_Int32 nNewWidth)
1028 {
1029     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1030     if (pSplitWindow == NULL)
1031         return 0;
1032 
1033     sal_uInt16 nRow (0xffff);
1034     sal_uInt16 nColumn (0xffff);
1035     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
1036     const long nColumnWidth (pSplitWindow->GetLineSize(nColumn));
1037 
1038     Window* pWindow = mpParentWindow;
1039     const Point aWindowPosition (pWindow->GetPosPixel());
1040     const Size aWindowSize (pWindow->GetSizePixel());
1041 
1042     pSplitWindow->MoveWindow(
1043         mpParentWindow,
1044         Size(nNewWidth, aWindowSize.Height()),
1045         nColumn,
1046         nRow);
1047     static_cast<SplitWindow*>(pSplitWindow)->Split();
1048 
1049     return static_cast<sal_Int32>(nColumnWidth);
1050 }
1051 
1052 
1053 
1054 
1055 void SidebarController::RestrictWidth (void)
1056 {
1057     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1058     if (pSplitWindow != NULL)
1059     {
1060         const sal_uInt16 nId (pSplitWindow->GetItemId(mpParentWindow));
1061         const sal_uInt16 nSetId (pSplitWindow->GetSet(nId));
1062         pSplitWindow->SetItemSizeRange(
1063             nSetId,
1064             Range(TabBar::GetDefaultWidth(), gnMaximumSidebarWidth));
1065     }
1066 }
1067 
1068 
1069 
1070 
1071 SfxSplitWindow* SidebarController::GetSplitWindow (void)
1072 {
1073     if (mpSplitWindow == NULL)
1074     {
1075         if (mpParentWindow != NULL)
1076         {
1077             mpSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
1078             if (mpSplitWindow != NULL)
1079                 mpSplitWindow->AddEventListener(LINK(this, SidebarController, WindowEventHandler));
1080         }
1081     }
1082 
1083     return mpSplitWindow;
1084 }
1085 
1086 
1087 
1088 
1089 void SidebarController::UpdateCloseIndicator (const bool bCloseAfterDrag)
1090 {
1091     if (mpParentWindow == NULL)
1092         return;
1093 
1094     if (bCloseAfterDrag)
1095     {
1096         // Make sure that the indicator exists.
1097         if ( ! mpCloseIndicator)
1098         {
1099             mpCloseIndicator.reset(new FixedImage(mpParentWindow));
1100             FixedImage* pFixedImage = static_cast<FixedImage*>(mpCloseIndicator.get());
1101             const Image aImage (Theme::GetImage(Theme::Image_CloseIndicator));
1102             pFixedImage->SetImage(aImage);
1103             pFixedImage->SetSizePixel(aImage.GetSizePixel());
1104             pFixedImage->SetBackground(Theme::GetWallpaper(Theme::Paint_DeckBackground));
1105         }
1106 
1107         // Place and show the indicator.
1108         const Size aWindowSize (mpParentWindow->GetSizePixel());
1109         const Size aImageSize (mpCloseIndicator->GetSizePixel());
1110         mpCloseIndicator->SetPosPixel(
1111             Point(
1112                 aWindowSize.Width() - TabBar::GetDefaultWidth() - aImageSize.Width(),
1113                 (aWindowSize.Height() - aImageSize.Height())/2));
1114         mpCloseIndicator->Show();
1115     }
1116     else
1117     {
1118         // Hide but don't delete the indicator.
1119         if (mpCloseIndicator)
1120             mpCloseIndicator->Hide();
1121     }
1122 }
1123 
1124 
1125 
1126 
1127 void SidebarController::UpdateTitleBarIcons (void)
1128 {
1129     if ( ! mpCurrentDeck)
1130         return;
1131 
1132     const bool bIsHighContrastModeActive (Theme::IsHighContrastMode());
1133     const ResourceManager& rResourceManager (ResourceManager::Instance());
1134 
1135     // Update the deck icon.
1136     const DeckDescriptor* pDeckDescriptor = rResourceManager.GetDeckDescriptor(mpCurrentDeck->GetId());
1137     if (pDeckDescriptor != NULL && mpCurrentDeck->GetTitleBar())
1138     {
1139         const OUString sIconURL(
1140             bIsHighContrastModeActive
1141                 ? pDeckDescriptor->msHighContrastTitleBarIconURL
1142                 : pDeckDescriptor->msTitleBarIconURL);
1143         mpCurrentDeck->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1144     }
1145 
1146     // Update the panel icons.
1147     const SharedPanelContainer& rPanels (mpCurrentDeck->GetPanels());
1148     for (SharedPanelContainer::const_iterator
1149              iPanel(rPanels.begin()), iEnd(rPanels.end());
1150              iPanel!=iEnd;
1151              ++iPanel)
1152     {
1153         if ( ! *iPanel)
1154             continue;
1155         if ((*iPanel)->GetTitleBar() == NULL)
1156             continue;
1157         const PanelDescriptor* pPanelDescriptor = rResourceManager.GetPanelDescriptor((*iPanel)->GetId());
1158         if (pPanelDescriptor == NULL)
1159             continue;
1160         const OUString sIconURL (
1161             bIsHighContrastModeActive
1162                ? pPanelDescriptor->msHighContrastTitleBarIconURL
1163                : pPanelDescriptor->msTitleBarIconURL);
1164         (*iPanel)->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1165     }
1166 }
1167 
1168 
1169 
1170 
1171 void SidebarController::ShowPanel (const Panel& rPanel)
1172 {
1173     if (mpCurrentDeck)
1174         mpCurrentDeck->ShowPanel(rPanel);
1175 }
1176 
1177 
1178 } } // end of namespace sfx2::sidebar
1179