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 
394         // Notify the tab bar about the updated set of decks.
395         mpTabBar->SetDecks(aDecks);
396 
397         // Find the new deck.  By default that is the same as the old
398         // one.  If that is not set or not enabled, then choose the
399         // first enabled deck.
400         OUString sNewDeckId;
401         for (ResourceManager::DeckContextDescriptorContainer::const_iterator
402                  iDeck(aDecks.begin()),
403                  iEnd(aDecks.end());
404              iDeck!=iEnd;
405              ++iDeck)
406         {
407             if (iDeck->mbIsEnabled)
408             {
409                 if (iDeck->msId.equals(msCurrentDeckId))
410                 {
411                     sNewDeckId = msCurrentDeckId;
412                     break;
413                 }
414                 else if (sNewDeckId.getLength() == 0)
415                     sNewDeckId = iDeck->msId;
416             }
417         }
418 
419         if (sNewDeckId.getLength() == 0)
420         {
421             // We did not find a valid deck.
422             RequestCloseDeck();
423             return;
424         }
425 
426         // Tell the tab bar to highlight the button associated
427         // with the deck.
428         mpTabBar->HighlightDeck(sNewDeckId);
429 
430         SwitchToDeck(
431             *ResourceManager::Instance().GetDeckDescriptor(sNewDeckId),
432             maCurrentContext);
433 
434 #ifdef DEBUG
435         // Show the context name in the deck title bar.
436         if (mpCurrentDeck)
437         {
438             DeckTitleBar* pTitleBar = mpCurrentDeck->GetTitleBar();
439             if (pTitleBar != NULL)
440                 pTitleBar->SetTitle(msCurrentDeckTitle+A2S(" (")+maCurrentContext.msContext+A2S(")"));
441         }
442 #endif
443     }
444 }
445 
446 
447 
448 
449 void SidebarController::OpenThenSwitchToDeck (
450     const ::rtl::OUString& rsDeckId)
451 {
452     RequestOpenDeck();
453     SwitchToDeck(rsDeckId);
454 }
455 
456 
457 
458 
459 void SidebarController::SwitchToDeck (
460     const ::rtl::OUString& rsDeckId)
461 {
462     if ( ! msCurrentDeckId.equals(rsDeckId) || ! mbIsDeckOpen)
463     {
464         const DeckDescriptor* pDeckDescriptor = ResourceManager::Instance().GetDeckDescriptor(rsDeckId);
465         if (pDeckDescriptor != NULL)
466             SwitchToDeck(*pDeckDescriptor, maCurrentContext);
467     }
468 }
469 
470 
471 
472 
473 void SidebarController::SwitchToDeck (
474     const DeckDescriptor& rDeckDescriptor,
475     const Context& rContext)
476 {
477     maFocusManager.Clear();
478 
479     if ( ! msCurrentDeckId.equals(rDeckDescriptor.msId))
480     {
481         // When the deck changes then destroy the deck and all panels
482         // and create everything new.
483         if (mpCurrentDeck)
484         {
485             mpCurrentDeck->Dispose();
486             mpCurrentDeck.reset();
487         }
488 
489         msCurrentDeckId = rDeckDescriptor.msId;
490     }
491 
492     // Determine the panels to display in the deck.
493     ResourceManager::PanelContextDescriptorContainer aPanelContextDescriptors;
494     ResourceManager::Instance().GetMatchingPanels(
495         aPanelContextDescriptors,
496         rContext,
497         rDeckDescriptor.msId,
498         mxFrame);
499 
500     if (aPanelContextDescriptors.empty())
501     {
502         // There are no panels to be displayed in the current context.
503         if (EnumContext::GetContextEnum(rContext.msContext) != EnumContext::Context_Empty)
504         {
505             // Switch to the "empty" context and try again.
506             SwitchToDeck(
507                 rDeckDescriptor,
508                 Context(
509                     rContext.msApplication,
510                     EnumContext::GetContextName(EnumContext::Context_Empty)));
511             return;
512         }
513         else
514         {
515             // This is already the "empty" context. Looks like we have
516             // to live with an empty deck.
517         }
518     }
519 
520     if (mpCurrentDeck
521         && ArePanelSetsEqual(mpCurrentDeck->GetPanels(), aPanelContextDescriptors))
522     {
523         // Requested set of panels is identical to the current set of
524         // panels => Nothing to do.
525         return;
526     }
527 
528         // When the document is read-only, check if there are any panels that can still be displayed.
529     if (mbIsDocumentReadOnly)
530     {
531     }
532 
533 
534     // Provide a configuration and Deck object.
535     if ( ! mpCurrentDeck)
536     {
537         mpCurrentDeck.reset(
538             new Deck(
539                 rDeckDescriptor,
540                 mpParentWindow,
541                 ::boost::bind(&SidebarController::RequestCloseDeck, this)));
542         msCurrentDeckTitle = rDeckDescriptor.msTitle;
543     }
544     if ( ! mpCurrentDeck)
545         return;
546 
547     // Update the panel list.
548     const sal_Int32 nNewPanelCount (aPanelContextDescriptors.size());
549     SharedPanelContainer aNewPanels;
550     const SharedPanelContainer& rCurrentPanels (mpCurrentDeck->GetPanels());
551     aNewPanels.resize(nNewPanelCount);
552     sal_Int32 nWriteIndex (0);
553     bool bHasPanelSetChanged (false);
554     for (sal_Int32 nReadIndex=0; nReadIndex<nNewPanelCount; ++nReadIndex)
555     {
556         const ResourceManager::PanelContextDescriptor& rPanelContexDescriptor (
557             aPanelContextDescriptors[nReadIndex]);
558 
559         // Determine if the panel can be displayed.
560         const bool bIsPanelVisible (!mbIsDocumentReadOnly || rPanelContexDescriptor.mbShowForReadOnlyDocuments);
561         if ( ! bIsPanelVisible)
562             continue;
563 
564         // Find the corresponding panel among the currently active
565         // panels.
566         SharedPanelContainer::const_iterator iPanel (::std::find_if(
567                 rCurrentPanels.begin(),
568                 rCurrentPanels.end(),
569                 ::boost::bind(&Panel::HasIdPredicate, _1, ::boost::cref(rPanelContexDescriptor.msId))));
570         if (iPanel != rCurrentPanels.end())
571         {
572             // Panel already exists in current deck.  Reuse it.
573             aNewPanels[nWriteIndex] = *iPanel;
574             aNewPanels[nWriteIndex]->SetExpanded(rPanelContexDescriptor.mbIsInitiallyVisible);
575         }
576         else
577         {
578             // Panel does not yet exist.  Create it.
579             aNewPanels[nWriteIndex] = CreatePanel(
580                 rPanelContexDescriptor.msId,
581                 mpCurrentDeck->GetPanelParentWindow(),
582                 rPanelContexDescriptor.mbIsInitiallyVisible);
583             bHasPanelSetChanged = true;
584         }
585         if (aNewPanels[nWriteIndex] != NULL)
586         {
587             // Depending on the context we have to apply the show menu functor.
588             aNewPanels[nWriteIndex]->SetShowMenuFunctor(
589                 rPanelContexDescriptor.msMenuCommand.getLength()>0
590                 ? ::boost::bind(&SidebarController::ShowDetailMenu,this,rPanelContexDescriptor.msMenuCommand)
591                 : ::boost::function<void(void)>() );
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 {
653     const PanelDescriptor* pPanelDescriptor = ResourceManager::Instance().GetPanelDescriptor(rsPanelId);
654     if (pPanelDescriptor == NULL)
655         return SharedPanel();
656 
657     // Create the panel which is the parent window of the UIElement.
658     SharedPanel pPanel (new Panel(
659         *pPanelDescriptor,
660         pParentWindow,
661         bIsInitiallyExpanded,
662         ::boost::bind(&Deck::RequestLayout, mpCurrentDeck.get()),
663         ::boost::bind(&SidebarController::GetCurrentContext, this)));
664 
665     // Create the XUIElement.
666     Reference<ui::XUIElement> xUIElement (CreateUIElement(
667             pPanel->GetComponentInterface(),
668             pPanelDescriptor->msImplementationURL,
669             pPanelDescriptor->mbWantsCanvas));
670     if (xUIElement.is())
671     {
672         // Initialize the panel and add it to the active deck.
673         pPanel->SetUIElement(xUIElement);
674     }
675     else
676     {
677         pPanel.reset();
678     }
679 
680     return pPanel;
681 }
682 
683 
684 
685 
686 Reference<ui::XUIElement> SidebarController::CreateUIElement (
687     const Reference<awt::XWindowPeer>& rxWindow,
688     const ::rtl::OUString& rsImplementationURL,
689     const bool bWantsCanvas)
690 {
691     try
692     {
693         const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
694         const Reference<ui::XUIElementFactory> xUIElementFactory (
695             aComponentContext.createComponent("com.sun.star.ui.UIElementFactoryManager"),
696             UNO_QUERY_THROW);
697 
698        // Create the XUIElement.
699         ::comphelper::NamedValueCollection aCreationArguments;
700         aCreationArguments.put("Frame", makeAny(mxFrame));
701         aCreationArguments.put("ParentWindow", makeAny(rxWindow));
702         SfxDockingWindow* pSfxDockingWindow = dynamic_cast<SfxDockingWindow*>(mpParentWindow);
703         if (pSfxDockingWindow != NULL)
704             aCreationArguments.put("SfxBindings", makeAny(sal_uInt64(&pSfxDockingWindow->GetBindings())));
705         aCreationArguments.put("Theme", Theme::GetPropertySet());
706         aCreationArguments.put("Sidebar", makeAny(Reference<ui::XSidebar>(static_cast<ui::XSidebar*>(this))));
707         if (bWantsCanvas)
708         {
709             Reference<rendering::XSpriteCanvas> xCanvas (VCLUnoHelper::GetWindow(rxWindow)->GetSpriteCanvas());
710             aCreationArguments.put("Canvas", makeAny(xCanvas));
711         }
712 
713         Reference<ui::XUIElement> xUIElement(
714             xUIElementFactory->createUIElement(
715                 rsImplementationURL,
716                 Sequence<beans::PropertyValue>(aCreationArguments.getPropertyValues())),
717             UNO_QUERY_THROW);
718 
719         return xUIElement;
720     }
721     catch(Exception& rException)
722     {
723         OSL_TRACE("caught exception: %s",
724             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
725         // For some reason we can not create the actual panel.
726         // Probably because its factory was not properly registered.
727         // TODO: provide feedback to developer to better pinpoint the
728         // source of the error.
729 
730         return NULL;
731     }
732 }
733 
734 
735 
736 
737 IMPL_LINK(SidebarController, WindowEventHandler, VclWindowEvent*, pEvent)
738 {
739     if (pEvent==NULL)
740         return sal_False;
741 
742     if (pEvent->GetWindow() == mpParentWindow)
743     {
744         switch (pEvent->GetId())
745         {
746             case VCLEVENT_WINDOW_SHOW:
747             case VCLEVENT_WINDOW_RESIZE:
748                 NotifyResize();
749                 break;
750 
751             case VCLEVENT_WINDOW_DATACHANGED:
752                 // Force an update of deck and tab bar to reflect
753                 // changes in theme (high contrast mode).
754                 Theme::HandleDataChange();
755                 UpdateTitleBarIcons();
756                 mpParentWindow->Invalidate();
757                 break;
758 
759             case SFX_HINT_DYING:
760                 dispose();
761                 break;
762 
763             case VCLEVENT_WINDOW_PAINT:
764                 OSL_TRACE("Paint");
765                 break;
766 
767             default:
768                 break;
769         }
770     }
771     else if (pEvent->GetWindow()==mpSplitWindow && mpSplitWindow!=NULL)
772     {
773         switch (pEvent->GetId())
774         {
775             case VCLEVENT_WINDOW_MOUSEBUTTONDOWN:
776                 mnWidthOnSplitterButtonDown = mpParentWindow->GetSizePixel().Width();
777                 break;
778 
779             case VCLEVENT_WINDOW_MOUSEBUTTONUP:
780             {
781                 ProcessNewWidth(mpParentWindow->GetSizePixel().Width());
782                 mnWidthOnSplitterButtonDown = 0;
783                 break;
784             }
785 
786             case SFX_HINT_DYING:
787                 dispose();
788                 break;
789          }
790     }
791 
792     return sal_True;
793 }
794 
795 
796 
797 
798 void SidebarController::ShowPopupMenu (
799     const Rectangle& rButtonBox,
800     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
801     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
802 {
803     ::boost::shared_ptr<PopupMenu> pMenu = CreatePopupMenu(rDeckSelectionData, rDeckShowData);
804     pMenu->SetSelectHdl(LINK(this, SidebarController, OnMenuItemSelected));
805 
806     // pass toolbox button rect so the menu can stay open on button up
807     Rectangle aBox (rButtonBox);
808     aBox.Move(mpTabBar->GetPosPixel().X(), 0);
809     pMenu->Execute(mpParentWindow, aBox, POPUPMENU_EXECUTE_DOWN);
810 }
811 
812 
813 
814 
815 void SidebarController::ShowDetailMenu (const ::rtl::OUString& rsMenuCommand) const
816 {
817     try
818     {
819         const util::URL aURL (Tools::GetURL(rsMenuCommand));
820         Reference<frame::XDispatch> xDispatch (Tools::GetDispatch(mxFrame, aURL));
821         if (xDispatch.is())
822             xDispatch->dispatch(aURL, Sequence<beans::PropertyValue>());
823     }
824     catch(Exception& rException)
825     {
826         OSL_TRACE("caught exception: %s",
827             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
828     }
829 }
830 
831 
832 
833 
834 ::boost::shared_ptr<PopupMenu> SidebarController::CreatePopupMenu (
835     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
836     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
837 {
838     ::boost::shared_ptr<PopupMenu> pMenu (new PopupMenu());
839     FloatingWindow* pMenuWindow = dynamic_cast<FloatingWindow*>(pMenu->GetWindow());
840     if (pMenuWindow != NULL)
841     {
842         pMenuWindow->SetPopupModeFlags(pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE);
843     }
844 
845     SidebarResource aLocalResource;
846 
847     // Add one entry for every tool panel element to individually make
848     // them visible or hide them.
849     {
850         sal_Int32 nIndex (MID_FIRST_PANEL);
851         for(::std::vector<TabBar::DeckMenuData>::const_iterator
852                 iItem(rDeckSelectionData.begin()),
853                 iEnd(rDeckSelectionData.end());
854             iItem!=iEnd;
855             ++iItem)
856         {
857             pMenu->InsertItem(nIndex, iItem->get<0>(), MIB_RADIOCHECK);
858             pMenu->CheckItem(nIndex, iItem->get<2>());
859             ++nIndex;
860         }
861     }
862 
863     pMenu->InsertSeparator();
864 
865     // Add entry for docking or un-docking the tool panel.
866     if (mpParentWindow->IsFloatingMode())
867         pMenu->InsertItem(MID_LOCK_TASK_PANEL, String(SfxResId(STR_SFX_DOCK)));
868     else
869         pMenu->InsertItem(MID_UNLOCK_TASK_PANEL, String(SfxResId(STR_SFX_UNDOCK)));
870 
871     // Add sub menu for customization (hiding of deck tabs.)
872     PopupMenu* pCustomizationMenu = new PopupMenu();
873     {
874         sal_Int32 nIndex (MID_FIRST_HIDE);
875         for(::std::vector<TabBar::DeckMenuData>::const_iterator
876                 iItem(rDeckShowData.begin()),
877                 iEnd(rDeckShowData.end());
878             iItem!=iEnd;
879             ++iItem)
880         {
881             pCustomizationMenu->InsertItem(nIndex, iItem->get<0>(), MIB_CHECKABLE);
882             pCustomizationMenu->CheckItem(nIndex, iItem->get<2>());
883             ++nIndex;
884         }
885     }
886 
887     pCustomizationMenu->InsertSeparator();
888     pCustomizationMenu->InsertItem(MID_RESTORE_DEFAULT, String(SfxResId(STRING_RESTORE)));
889 
890     pMenu->InsertItem(MID_CUSTOMIZATION, String(SfxResId(STRING_CUSTOMIZATION)));
891     pMenu->SetPopupMenu(MID_CUSTOMIZATION, pCustomizationMenu);
892 
893     pMenu->RemoveDisabledEntries(sal_False, sal_False);
894 
895     return pMenu;
896 }
897 
898 
899 
900 
901 IMPL_LINK(SidebarController, OnMenuItemSelected, Menu*, pMenu)
902 {
903     if (pMenu == NULL)
904     {
905         OSL_ENSURE(pMenu!=NULL, "sfx2::sidebar::SidebarController::OnMenuItemSelected: illegal menu!");
906         return 0;
907     }
908 
909     pMenu->Deactivate();
910     const sal_Int32 nIndex (pMenu->GetCurItemId());
911     switch (nIndex)
912     {
913         case MID_UNLOCK_TASK_PANEL:
914             mpParentWindow->SetFloatingMode(sal_True);
915             break;
916 
917         case MID_LOCK_TASK_PANEL:
918             mpParentWindow->SetFloatingMode(sal_False);
919             break;
920 
921         case MID_RESTORE_DEFAULT:
922             mpTabBar->RestoreHideFlags();
923             break;
924 
925         default:
926         {
927             try
928             {
929                 if (nIndex >= MID_FIRST_PANEL && nIndex<MID_FIRST_HIDE)
930                     SwitchToDeck(mpTabBar->GetDeckIdForIndex(nIndex - MID_FIRST_PANEL));
931                 else if (nIndex >=MID_FIRST_HIDE)
932                     mpTabBar->ToggleHideFlag(nIndex-MID_FIRST_HIDE);
933             }
934             catch (RuntimeException&)
935             {
936             }
937         }
938         break;
939     }
940 
941     return 1;
942 }
943 
944 
945 
946 
947 void SidebarController::RequestCloseDeck (void)
948 {
949     mbIsDeckRequestedOpen = false;
950     UpdateDeckOpenState();
951 }
952 
953 
954 
955 
956 void SidebarController::RequestOpenDeck (void)
957 {
958     mbIsDeckRequestedOpen = true;
959     UpdateDeckOpenState();
960 }
961 
962 
963 
964 
965 void SidebarController::UpdateDeckOpenState (void)
966 {
967     if ( ! mbIsDeckRequestedOpen)
968         // No state requested.
969         return;
970 
971     // Update (change) the open state when it either has not yet been initialized
972     // or when its value differs from the requested state.
973     if ( ! mbIsDeckOpen
974         || mbIsDeckOpen.get() != mbIsDeckRequestedOpen.get())
975     {
976         if (mbIsDeckRequestedOpen.get())
977         {
978             if (mnSavedSidebarWidth <= TabBar::GetDefaultWidth())
979                 SetChildWindowWidth(SidebarChildWindow::GetDefaultWidth(mpParentWindow));
980             else
981                 SetChildWindowWidth(mnSavedSidebarWidth);
982         }
983         else
984         {
985             if ( ! mpParentWindow->IsFloatingMode())
986                 mnSavedSidebarWidth = SetChildWindowWidth(TabBar::GetDefaultWidth());
987             if (mnWidthOnSplitterButtonDown > TabBar::GetDefaultWidth())
988                 mnSavedSidebarWidth = mnWidthOnSplitterButtonDown;
989             mpParentWindow->SetStyle(mpParentWindow->GetStyle() & ~WB_SIZEABLE);
990         }
991 
992         mbIsDeckOpen = mbIsDeckRequestedOpen.get();
993         if (mbIsDeckOpen.get() && mpCurrentDeck)
994             mpCurrentDeck->Show(mbIsDeckOpen.get());
995         NotifyResize();
996     }
997 }
998 
999 
1000 
1001 
1002 FocusManager& SidebarController::GetFocusManager (void)
1003 {
1004     return maFocusManager;
1005 }
1006 
1007 
1008 
1009 
1010 bool SidebarController::CanModifyChildWindowWidth (void)
1011 {
1012     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1013     if (pSplitWindow == NULL)
1014         return false;
1015 
1016     sal_uInt16 nRow (0xffff);
1017     sal_uInt16 nColumn (0xffff);
1018     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
1019 
1020     sal_uInt16 nRowCount (pSplitWindow->GetWindowCount(nColumn));
1021 
1022     return nRowCount==1;
1023 }
1024 
1025 
1026 
1027 
1028 sal_Int32 SidebarController::SetChildWindowWidth (const sal_Int32 nNewWidth)
1029 {
1030     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1031     if (pSplitWindow == NULL)
1032         return 0;
1033 
1034     sal_uInt16 nRow (0xffff);
1035     sal_uInt16 nColumn (0xffff);
1036     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
1037     const long nColumnWidth (pSplitWindow->GetLineSize(nColumn));
1038 
1039     Window* pWindow = mpParentWindow;
1040     const Point aWindowPosition (pWindow->GetPosPixel());
1041     const Size aWindowSize (pWindow->GetSizePixel());
1042 
1043     pSplitWindow->MoveWindow(
1044         mpParentWindow,
1045         Size(nNewWidth, aWindowSize.Height()),
1046         nColumn,
1047         nRow);
1048     static_cast<SplitWindow*>(pSplitWindow)->Split();
1049 
1050     return static_cast<sal_Int32>(nColumnWidth);
1051 }
1052 
1053 
1054 
1055 
1056 void SidebarController::RestrictWidth (void)
1057 {
1058     SfxSplitWindow* pSplitWindow = GetSplitWindow();
1059     if (pSplitWindow != NULL)
1060     {
1061         const sal_uInt16 nId (pSplitWindow->GetItemId(mpParentWindow));
1062         const sal_uInt16 nSetId (pSplitWindow->GetSet(nId));
1063         pSplitWindow->SetItemSizeRange(
1064             nSetId,
1065             Range(TabBar::GetDefaultWidth(), gnMaximumSidebarWidth));
1066     }
1067 }
1068 
1069 
1070 
1071 
1072 SfxSplitWindow* SidebarController::GetSplitWindow (void)
1073 {
1074     if (mpSplitWindow == NULL)
1075     {
1076         if (mpParentWindow != NULL)
1077         {
1078             mpSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
1079             if (mpSplitWindow != NULL)
1080                 mpSplitWindow->AddEventListener(LINK(this, SidebarController, WindowEventHandler));
1081         }
1082     }
1083 
1084     return mpSplitWindow;
1085 }
1086 
1087 
1088 
1089 
1090 void SidebarController::UpdateCloseIndicator (const bool bCloseAfterDrag)
1091 {
1092     if (mpParentWindow == NULL)
1093         return;
1094 
1095     if (bCloseAfterDrag)
1096     {
1097         // Make sure that the indicator exists.
1098         if ( ! mpCloseIndicator)
1099         {
1100             mpCloseIndicator.reset(new FixedImage(mpParentWindow));
1101             FixedImage* pFixedImage = static_cast<FixedImage*>(mpCloseIndicator.get());
1102             const Image aImage (Theme::GetImage(Theme::Image_CloseIndicator));
1103             pFixedImage->SetImage(aImage);
1104             pFixedImage->SetSizePixel(aImage.GetSizePixel());
1105             pFixedImage->SetBackground(Theme::GetWallpaper(Theme::Paint_DeckBackground));
1106         }
1107 
1108         // Place and show the indicator.
1109         const Size aWindowSize (mpParentWindow->GetSizePixel());
1110         const Size aImageSize (mpCloseIndicator->GetSizePixel());
1111         mpCloseIndicator->SetPosPixel(
1112             Point(
1113                 aWindowSize.Width() - TabBar::GetDefaultWidth() - aImageSize.Width(),
1114                 (aWindowSize.Height() - aImageSize.Height())/2));
1115         mpCloseIndicator->Show();
1116     }
1117     else
1118     {
1119         // Hide but don't delete the indicator.
1120         if (mpCloseIndicator)
1121             mpCloseIndicator->Hide();
1122     }
1123 }
1124 
1125 
1126 
1127 
1128 void SidebarController::UpdateTitleBarIcons (void)
1129 {
1130     if ( ! mpCurrentDeck)
1131         return;
1132 
1133     const bool bIsHighContrastModeActive (Theme::IsHighContrastMode());
1134     const ResourceManager& rResourceManager (ResourceManager::Instance());
1135 
1136     // Update the deck icon.
1137     const DeckDescriptor* pDeckDescriptor = rResourceManager.GetDeckDescriptor(mpCurrentDeck->GetId());
1138     if (pDeckDescriptor != NULL && mpCurrentDeck->GetTitleBar())
1139     {
1140         const OUString sIconURL(
1141             bIsHighContrastModeActive
1142                 ? pDeckDescriptor->msHighContrastTitleBarIconURL
1143                 : pDeckDescriptor->msTitleBarIconURL);
1144         mpCurrentDeck->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1145     }
1146 
1147     // Update the panel icons.
1148     const SharedPanelContainer& rPanels (mpCurrentDeck->GetPanels());
1149     for (SharedPanelContainer::const_iterator
1150              iPanel(rPanels.begin()), iEnd(rPanels.end());
1151              iPanel!=iEnd;
1152              ++iPanel)
1153     {
1154         if ( ! *iPanel)
1155             continue;
1156         if ((*iPanel)->GetTitleBar() == NULL)
1157             continue;
1158         const PanelDescriptor* pPanelDescriptor = rResourceManager.GetPanelDescriptor((*iPanel)->GetId());
1159         if (pPanelDescriptor == NULL)
1160             continue;
1161         const OUString sIconURL (
1162             bIsHighContrastModeActive
1163                ? pPanelDescriptor->msHighContrastTitleBarIconURL
1164                : pPanelDescriptor->msTitleBarIconURL);
1165         (*iPanel)->GetTitleBar()->SetIcon(Tools::GetImage(sIconURL, mxFrame));
1166     }
1167 }
1168 
1169 
1170 
1171 
1172 void SidebarController::ShowPanel (const Panel& rPanel)
1173 {
1174     if (mpCurrentDeck)
1175         mpCurrentDeck->ShowPanel(rPanel);
1176 }
1177 
1178 
1179 
1180 
1181 Context SidebarController::GetCurrentContext (void) const
1182 {
1183     return maCurrentContext;
1184 }
1185 
1186 
1187 } } // end of namespace sfx2::sidebar
1188