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