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 "SidebarDockingWindow.hxx"
33 #include "Context.hxx"
34 #include "Tools.hxx"
35 
36 #include "sfxresid.hxx"
37 #include "sfx2/sfxsids.hrc"
38 #include "sfx2/titledockwin.hxx"
39 #include "sfxlocal.hrc"
40 #include <vcl/floatwin.hxx>
41 #include "splitwin.hxx"
42 #include <svl/smplhint.hxx>
43 #include <tools/link.hxx>
44 #include <toolkit/helper/vclunohelper.hxx>
45 
46 #include <comphelper/componentfactory.hxx>
47 #include <comphelper/processfactory.hxx>
48 #include <comphelper/componentcontext.hxx>
49 #include <comphelper/namedvaluecollection.hxx>
50 
51 #include <com/sun/star/frame/XDispatchProvider.hpp>
52 #include <com/sun/star/lang/XInitialization.hpp>
53 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
54 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
55 #include <com/sun/star/ui/XUIElementFactory.hpp>
56 #include <com/sun/star/util/XURLTransformer.hpp>
57 #include <com/sun/star/util/URL.hpp>
58 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
59 
60 #include <boost/bind.hpp>
61 #include <boost/function.hpp>
62 #include <boost/scoped_array.hpp>
63 
64 
65 using namespace css;
66 using namespace cssu;
67 using ::rtl::OUString;
68 
69 
70 #undef VERBOSE
71 
72 namespace sfx2 { namespace sidebar {
73 
74 namespace {
75     enum MenuId
76     {
77         MID_UNLOCK_TASK_PANEL = 1,
78         MID_LOCK_TASK_PANEL,
79         MID_CUSTOMIZATION,
80         MID_RESTORE_DEFAULT,
81         MID_FIRST_PANEL,
82         MID_FIRST_HIDE = 1000
83     };
84 }
85 
86 
87 SidebarController::SidebarController (
88     SidebarDockingWindow* pParentWindow,
89     const cssu::Reference<css::frame::XFrame>& rxFrame)
90     : SidebarControllerInterfaceBase(m_aMutex),
91       mpCurrentDeck(),
92       mpParentWindow(pParentWindow),
93       mpTabBar(new TabBar(
94               mpParentWindow,
95               rxFrame,
96               ::boost::bind(&SidebarController::SwitchToDeck, this, _1),
97               ::boost::bind(&SidebarController::ShowPopupMenu, this, _1,_2,_3))),
98       mxFrame(rxFrame),
99       maCurrentContext(OUString(), OUString()),
100       msCurrentDeckId(A2S("PropertyDeck")),
101       maPropertyChangeForwarder(::boost::bind(&SidebarController::BroadcastPropertyChange, this)),
102       maContextChangeUpdate(::boost::bind(&SidebarController::UpdateConfigurations, this)),
103       mbIsDeckClosed(false),
104       mnSavedSidebarWidth(pParentWindow->GetSizePixel().Width())
105 {
106     if (pParentWindow == NULL)
107     {
108         OSL_ASSERT(pParentWindow!=NULL);
109             return;
110     }
111 
112     // Listen for context change events.
113     cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
114         css::ui::ContextChangeEventMultiplexer::get(
115             ::comphelper::getProcessComponentContext()));
116     if (xMultiplexer.is())
117         xMultiplexer->addContextChangeEventListener(
118             static_cast<css::ui::XContextChangeEventListener*>(this),
119             mxFrame->getController());
120 
121     // Listen for window events.
122     mpParentWindow->AddEventListener(LINK(this, SidebarController, WindowEventHandler));
123 
124     // Listen for theme property changes.
125     Theme::GetPropertySet()->addPropertyChangeListener(
126         A2S(""),
127         static_cast<css::beans::XPropertyChangeListener*>(this));
128 
129     SwitchToDeck(A2S("default"));
130 }
131 
132 
133 
134 
135 SidebarController::~SidebarController (void)
136 {
137 }
138 
139 
140 
141 
142 void SAL_CALL SidebarController::disposing (void)
143 {
144     maFocusManager.Clear();
145 
146     cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
147         css::ui::ContextChangeEventMultiplexer::get(
148             ::comphelper::getProcessComponentContext()));
149     if (xMultiplexer.is())
150         xMultiplexer->removeAllContextChangeEventListeners(
151             static_cast<css::ui::XContextChangeEventListener*>(this));
152 
153     if (mpParentWindow != NULL)
154     {
155         mpParentWindow->RemoveEventListener(LINK(this, SidebarController, WindowEventHandler));
156         mpParentWindow = NULL;
157     }
158 
159     if (mpCurrentDeck)
160     {
161         mpCurrentDeck->Dispose();
162         mpCurrentDeck->PrintWindowTree();
163         mpCurrentDeck.reset();
164     }
165 
166     mpTabBar.reset();
167 
168     Theme::GetPropertySet()->removePropertyChangeListener(
169         A2S(""),
170         static_cast<css::beans::XPropertyChangeListener*>(this));
171 }
172 
173 
174 
175 
176 void SAL_CALL SidebarController::notifyContextChangeEvent (const css::ui::ContextChangeEventObject& rEvent)
177     throw(cssu::RuntimeException)
178 {
179     // Update to the requested new context asynchronously to avoid
180     // subtle errors caused by SFX2 which in rare cases can not
181     // properly handle a synchronous update.
182     maRequestedContext = Context(
183         rEvent.ApplicationName,
184         rEvent.ContextName);
185     if (maRequestedContext != maCurrentContext)
186         maContextChangeUpdate.RequestCall();
187 }
188 
189 
190 
191 
192 void SAL_CALL SidebarController::disposing (const css::lang::EventObject& rEventObject)
193     throw(cssu::RuntimeException)
194 {
195     (void)rEventObject;
196 
197     dispose();
198 }
199 
200 
201 
202 
203 void SAL_CALL SidebarController::propertyChange (const css::beans::PropertyChangeEvent& rEvent)
204     throw(cssu::RuntimeException)
205 {
206     (void)rEvent;
207 
208     maPropertyChangeForwarder.RequestCall();
209 }
210 
211 
212 
213 
214 void SAL_CALL SidebarController::requestLayout (void)
215     throw(cssu::RuntimeException)
216 {
217     if (mpCurrentDeck)
218         mpCurrentDeck->RequestLayout();
219     RestrictWidth();
220 }
221 
222 
223 
224 
225 void SidebarController::BroadcastPropertyChange (void)
226 {
227     DataChangedEvent aEvent (DATACHANGED_USER);
228     mpParentWindow->NotifyAllChilds(aEvent);
229     mpParentWindow->Invalidate(INVALIDATE_CHILDREN);
230 }
231 
232 
233 
234 
235 void SidebarController::NotifyResize (void)
236 {
237     if (mpTabBar == NULL)
238     {
239         OSL_ASSERT(mpTabBar!=NULL);
240         return;
241     }
242 
243     Window* pParentWindow = mpTabBar->GetParent();
244 
245     const sal_Int32 nWidth (pParentWindow->GetSizePixel().Width());
246     const sal_Int32 nHeight (pParentWindow->GetSizePixel().Height());
247 
248     // Place the deck.
249     if (mpCurrentDeck)
250     {
251         mpCurrentDeck->SetPosSizePixel(0,0, nWidth-TabBar::GetDefaultWidth(), nHeight);
252         mpCurrentDeck->Show();
253         mpCurrentDeck->RequestLayout();
254     }
255 
256     // Place the tab bar.
257     mpTabBar->SetPosSizePixel(nWidth-TabBar::GetDefaultWidth(),0,TabBar::GetDefaultWidth(),nHeight);
258     mpTabBar->Show();
259 
260     // Determine if the closer of the deck can be shown.
261     if (mpCurrentDeck)
262     {
263         DeckTitleBar* pTitleBar = mpCurrentDeck->GetTitleBar();
264         if (pTitleBar != NULL && pTitleBar->IsVisible())
265             pTitleBar->SetCloserVisible(CanModifyChildWindowWidth());
266     }
267 
268     if (nWidth > TabBar::GetDefaultWidth())
269         mnSavedSidebarWidth = nWidth;
270 
271     RestrictWidth();
272 #ifdef VERBOSE
273     if (mpCurrentDeck)
274     {
275         mpCurrentDeck->PrintWindowTree();
276         sal_Int32 nPanelIndex (0);
277         for (SharedPanelContainer::const_iterator
278                  iPanel(mpCurrentDeck->GetPanels().begin()),
279                  iEnd(mpCurrentDeck->GetPanels().end());
280              iPanel!=iEnd;
281              ++iPanel,++nPanelIndex)
282         {
283             OSL_TRACE("panel %d:", nPanelIndex);
284             (*iPanel)->PrintWindowTree();
285         }
286     }
287 #endif
288 }
289 
290 
291 
292 
293 void SidebarController::UpdateConfigurations (void)
294 {
295     if (maCurrentContext != maRequestedContext)
296     {
297         maCurrentContext = maRequestedContext;
298 
299         // Notify the tab bar about the updated set of decks.
300         ResourceManager::IdContainer aDeckIds;
301         ResourceManager::Instance().GetMatchingDecks (
302             aDeckIds,
303             maCurrentContext,
304             mxFrame);
305         mpTabBar->SetDecks(aDeckIds);
306 
307         // Check if the current deck is among the matching decks.
308         bool bCurrentDeckMatches (false);
309         for (ResourceManager::IdContainer::const_iterator
310                  iDeck(aDeckIds.begin()),
311                  iEnd(aDeckIds.end());
312              iDeck!=iEnd;
313              ++iDeck)
314         {
315             if (iDeck->equals(msCurrentDeckId))
316             {
317                 bCurrentDeckMatches = true;
318                 break;
319             }
320         }
321 
322         DeckDescriptor const* pDeckDescriptor = NULL;
323         if ( ! bCurrentDeckMatches)
324             pDeckDescriptor = ResourceManager::Instance().GetBestMatchingDeck(maCurrentContext, mxFrame);
325         else
326             pDeckDescriptor = ResourceManager::Instance().GetDeckDescriptor(msCurrentDeckId);
327         if (pDeckDescriptor != NULL)
328         {
329             msCurrentDeckId = pDeckDescriptor->msId;
330             SwitchToDeck(*pDeckDescriptor, maCurrentContext);
331 
332             // Tell the tab bar to highlight the button associated
333             // with the deck.
334             mpTabBar->HighlightDeck(msCurrentDeckId);
335         }
336 
337 #ifdef DEBUG
338         // Show the context name in the deck title bar.
339         if (mpCurrentDeck)
340         {
341             DeckTitleBar* pTitleBar = mpCurrentDeck->GetTitleBar();
342             if (pTitleBar != NULL)
343                 pTitleBar->SetTitle(msCurrentDeckTitle+A2S(" (")+maCurrentContext.msContext+A2S(")"));
344         }
345 #endif
346     }
347 }
348 
349 
350 
351 
352 void SidebarController::SwitchToDeck (
353     const ::rtl::OUString& rsDeckId)
354 {
355     if ( ! msCurrentDeckId.equals(rsDeckId) || mbIsDeckClosed)
356     {
357         const DeckDescriptor* pDeckDescriptor = ResourceManager::Instance().GetDeckDescriptor(rsDeckId);
358         if (pDeckDescriptor != NULL)
359             SwitchToDeck(*pDeckDescriptor, maCurrentContext);
360     }
361 }
362 
363 
364 
365 
366 void SidebarController::SwitchToDeck (
367     const DeckDescriptor& rDeckDescriptor,
368     const Context& rContext)
369 {
370     maFocusManager.Clear();
371 
372     if ( ! msCurrentDeckId.equals(rDeckDescriptor.msId))
373     {
374         // When the deck changes then destroy the deck and all panels
375         // and create everything new.
376         if (mpCurrentDeck)
377         {
378             mpCurrentDeck->Dispose();
379             mpCurrentDeck.reset();
380         }
381 
382         msCurrentDeckId = rDeckDescriptor.msId;
383     }
384 
385     // Reopen the deck when necessary.
386     OpenDeck();
387 
388     // Determine the panels to display in the deck.
389     ResourceManager::PanelContextDescriptorContainer aPanelContextDescriptors;
390     ResourceManager::Instance().GetMatchingPanels(
391         aPanelContextDescriptors,
392         rContext,
393         rDeckDescriptor.msId,
394         mxFrame);
395 
396     if (aPanelContextDescriptors.empty())
397     {
398         // There are no panels to be displayed in the current context.
399         if (EnumContext::GetContextEnum(rContext.msContext) != EnumContext::Context_Empty)
400         {
401             // Switch to the "empty" context and try again.
402             SwitchToDeck(
403                 rDeckDescriptor,
404                 Context(
405                     rContext.msApplication,
406                     EnumContext::GetContextName(EnumContext::Context_Empty)));
407             return;
408         }
409         else
410         {
411             // This is already the "empty" context. Looks like we have
412             // to live with an empty deck.
413         }
414     }
415 
416     if (mpCurrentDeck
417         && ArePanelSetsEqual(mpCurrentDeck->GetPanels(), aPanelContextDescriptors))
418     {
419         // Requested set of panels is identical to the current set of
420         // panels => Nothing to do.
421         return;
422     }
423 
424     // Provide a configuration and Deck object.
425     if ( ! mpCurrentDeck)
426     {
427         mpCurrentDeck.reset(
428             new Deck(
429                 rDeckDescriptor,
430                 mpParentWindow,
431                 ::boost::bind(&SidebarController::CloseDeck, this)));
432         msCurrentDeckTitle = rDeckDescriptor.msTitle;
433     }
434     if ( ! mpCurrentDeck)
435         return;
436 
437     // Update the panel list.
438     const sal_Int32 nNewPanelCount (aPanelContextDescriptors.size());
439     SharedPanelContainer aNewPanels;
440     const SharedPanelContainer& rCurrentPanels (mpCurrentDeck->GetPanels());
441     aNewPanels.resize(nNewPanelCount);
442     sal_Int32 nWriteIndex (0);
443     bool bHasPanelSetChanged (false);
444     for (sal_Int32 nReadIndex=0; nReadIndex<nNewPanelCount; ++nReadIndex)
445     {
446         const ResourceManager::PanelContextDescriptor& rPanelContexDescriptor (
447             aPanelContextDescriptors[nReadIndex]);
448 
449         // Find the corresponding panel among the currently active
450         // panels.
451         SharedPanelContainer::const_iterator iPanel (::std::find_if(
452                 rCurrentPanels.begin(),
453                 rCurrentPanels.end(),
454                 ::boost::bind(&Panel::HasIdPredicate, _1, ::boost::cref(rPanelContexDescriptor.msId))));
455         if (iPanel != rCurrentPanels.end())
456         {
457             // Panel already exists in current deck.  Reuse it.
458             aNewPanels[nWriteIndex] = *iPanel;
459         }
460         else
461         {
462             // Panel does not yet exist.  Create it.
463             aNewPanels[nWriteIndex] = CreatePanel(
464                 rPanelContexDescriptor.msId,
465                 mpCurrentDeck->GetPanelParentWindow(),
466                 rPanelContexDescriptor.msMenuCommand);
467             bHasPanelSetChanged = true;
468         }
469         if (aNewPanels[nWriteIndex] != NULL)
470         {
471             // Depending on the context we have to collapse the panel.
472             aNewPanels[nWriteIndex]->SetExpanded(rPanelContexDescriptor.mbIsInitiallyVisible);
473 
474             ++nWriteIndex;
475         }
476 
477     }
478     aNewPanels.resize(nWriteIndex);
479 
480     // Activate the deck and the new set of panels.
481     mpCurrentDeck->SetPosSizePixel(
482         0,
483         0,
484         mpParentWindow->GetSizePixel().Width()-TabBar::GetDefaultWidth(),
485         mpParentWindow->GetSizePixel().Height());
486     mpCurrentDeck->SetPanels(aNewPanels);
487     mpCurrentDeck->Show();
488 
489     mpParentWindow->SetText(rDeckDescriptor.msTitle);
490 
491     if (bHasPanelSetChanged)
492         NotifyResize();
493 
494     // Tell the focus manager about the new panels and tab bar
495     // buttons.
496     maFocusManager.SetPanels(aNewPanels);
497     mpTabBar->UpdateFocusManager(maFocusManager);
498 }
499 
500 
501 
502 
503 bool SidebarController::ArePanelSetsEqual (
504     const SharedPanelContainer& rCurrentPanels,
505     const ResourceManager::PanelContextDescriptorContainer& rRequestedPanels)
506 {
507 #ifdef VERBOSE
508     OSL_TRACE("current panel list:");
509     for (SharedPanelContainer::const_iterator
510              iPanel(rCurrentPanels.begin()),
511              iEnd(rCurrentPanels.end());
512          iPanel!=iEnd;
513          ++iPanel)
514     {
515         OSL_TRACE("    panel %s", S2A((*iPanel)->GetId()));
516     }
517 
518     OSL_TRACE("requested panels: ");
519     for (ResourceManager::PanelContextDescriptorContainer::const_iterator
520              iId(rRequestedPanels.begin()),
521              iEnd(rRequestedPanels.end());
522          iId!=iEnd;
523          ++iId)
524     {
525         OSL_TRACE("    panel %s", S2A(iId->msId));
526     }
527 #endif
528 
529     if (rCurrentPanels.size() != rRequestedPanels.size())
530         return false;
531     for (sal_Int32 nIndex=0,nCount=rCurrentPanels.size(); nIndex<nCount; ++nIndex)
532     {
533         if (rCurrentPanels[nIndex] == NULL)
534             return false;
535         if ( ! rCurrentPanels[nIndex]->GetId().equals(rRequestedPanels[nIndex].msId))
536             return false;
537     }
538     return true;
539 }
540 
541 
542 
543 
544 SharedPanel SidebarController::CreatePanel (
545     const OUString& rsPanelId,
546     ::Window* pParentWindow,
547     const OUString& rsMenuCommand)
548 {
549     const PanelDescriptor* pPanelDescriptor = ResourceManager::Instance().GetPanelDescriptor(rsPanelId);
550     if (pPanelDescriptor == NULL)
551         return SharedPanel();
552 
553     // Create the panel which is the parent window of the UIElement.
554     SharedPanel pPanel (new Panel(
555         *pPanelDescriptor,
556         pParentWindow,
557         ::boost::bind(&Deck::RequestLayout, mpCurrentDeck.get()),
558         rsMenuCommand.getLength()>0
559             ? ::boost::bind(&SidebarController::ShowDetailMenu,this,rsMenuCommand)
560             : ::boost::function<void(void)>()));
561 
562     // Create the XUIElement.
563     Reference<ui::XUIElement> xUIElement (CreateUIElement(
564             pPanel->GetComponentInterface(),
565             pPanelDescriptor->msImplementationURL,
566             pPanelDescriptor->mbWantsCanvas));
567     if (xUIElement.is())
568     {
569         // Initialize the panel and add it to the active deck.
570         pPanel->SetUIElement(xUIElement);
571     }
572     else
573     {
574         pPanel.reset();
575     }
576 
577     return pPanel;
578 }
579 
580 
581 
582 
583 Reference<ui::XUIElement> SidebarController::CreateUIElement (
584     const Reference<awt::XWindowPeer>& rxWindow,
585     const ::rtl::OUString& rsImplementationURL,
586     const bool bWantsCanvas)
587 {
588     try
589     {
590         const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
591         const Reference<ui::XUIElementFactory> xUIElementFactory (
592             aComponentContext.createComponent("com.sun.star.ui.UIElementFactoryManager"),
593             UNO_QUERY_THROW);
594 
595        // Create the XUIElement.
596         ::comphelper::NamedValueCollection aCreationArguments;
597         aCreationArguments.put("Frame", makeAny(mxFrame));
598         aCreationArguments.put("ParentWindow", makeAny(rxWindow));
599         SfxDockingWindow* pSfxDockingWindow = dynamic_cast<SfxDockingWindow*>(mpParentWindow);
600         if (pSfxDockingWindow != NULL)
601             aCreationArguments.put("SfxBindings", makeAny(sal_uInt64(&pSfxDockingWindow->GetBindings())));
602         aCreationArguments.put("Theme", Theme::GetPropertySet());
603         aCreationArguments.put("Sidebar", makeAny(Reference<ui::XSidebar>(static_cast<ui::XSidebar*>(this))));
604         if (bWantsCanvas)
605         {
606             Reference<rendering::XSpriteCanvas> xCanvas (VCLUnoHelper::GetWindow(rxWindow)->GetSpriteCanvas());
607             aCreationArguments.put("Canvas", makeAny(xCanvas));
608         }
609 
610         Reference<ui::XUIElement> xUIElement(
611             xUIElementFactory->createUIElement(
612                 rsImplementationURL,
613                 Sequence<beans::PropertyValue>(aCreationArguments.getPropertyValues())),
614             UNO_QUERY_THROW);
615 
616         return xUIElement;
617     }
618     catch(Exception& rException)
619     {
620         OSL_TRACE("caught exception: %s",
621             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
622         // For some reason we can not create the actual panel.
623         // Probably because its factory was not properly registered.
624         // TODO: provide feedback to developer to better pinpoint the
625         // source of the error.
626 
627         return NULL;
628     }
629 }
630 
631 
632 
633 
634 IMPL_LINK(SidebarController, WindowEventHandler, VclWindowEvent*, pEvent)
635 {
636     if (pEvent != NULL)
637     {
638         switch (pEvent->GetId())
639         {
640             case VCLEVENT_WINDOW_GETFOCUS:
641             case VCLEVENT_WINDOW_LOSEFOCUS:
642                 break;
643 
644             case VCLEVENT_WINDOW_SHOW:
645             case VCLEVENT_WINDOW_RESIZE:
646                 NotifyResize();
647                 break;
648 
649             case VCLEVENT_WINDOW_DATACHANGED:
650                 // Force an update of deck and tab bar to reflect
651                 // changes in theme (high contrast mode).
652                 Theme::HandleDataChange();
653                 mpParentWindow->Invalidate();
654                 break;
655 
656             case SFX_HINT_DYING:
657                 dispose();
658                 break;
659 
660             default:
661                 break;
662         }
663     }
664 
665     return sal_True;
666 }
667 
668 
669 
670 
671 void SidebarController::ShowPopupMenu (
672     const Rectangle& rButtonBox,
673     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
674     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
675 {
676     ::boost::shared_ptr<PopupMenu> pMenu = CreatePopupMenu(rDeckSelectionData, rDeckShowData);
677     pMenu->SetSelectHdl(LINK(this, SidebarController, OnMenuItemSelected));
678 
679     // pass toolbox button rect so the menu can stay open on button up
680     Rectangle aBox (rButtonBox);
681     aBox.Move(mpTabBar->GetPosPixel().X(), 0);
682     pMenu->Execute(mpParentWindow, aBox, POPUPMENU_EXECUTE_DOWN);
683 }
684 
685 
686 
687 
688 void SidebarController::ShowDetailMenu (const ::rtl::OUString& rsMenuCommand) const
689 {
690     try
691     {
692         util::URL aURL;
693         aURL.Complete = rsMenuCommand;
694 
695         const ::comphelper::ComponentContext aComponentContext (::comphelper::getProcessServiceFactory());
696         const Reference<util::XURLTransformer> xParser (
697             aComponentContext.createComponent("com.sun.star.util.URLTransformer"),
698             UNO_QUERY_THROW);
699         xParser->parseStrict(aURL);
700         Reference<frame::XDispatchProvider> xProvider (mxFrame, UNO_QUERY_THROW);
701         Reference<frame::XDispatch> xDispatch (xProvider->queryDispatch(aURL, OUString(), 0));
702         if (xDispatch.is())
703             xDispatch->dispatch(aURL, Sequence<beans::PropertyValue>());
704     }
705     catch(Exception& rException)
706     {
707         OSL_TRACE("caught exception: %s",
708             OUStringToOString(rException.Message, RTL_TEXTENCODING_ASCII_US).getStr());
709     }
710 }
711 
712 
713 
714 
715 ::boost::shared_ptr<PopupMenu> SidebarController::CreatePopupMenu (
716     const ::std::vector<TabBar::DeckMenuData>& rDeckSelectionData,
717     const ::std::vector<TabBar::DeckMenuData>& rDeckShowData) const
718 {
719     ::boost::shared_ptr<PopupMenu> pMenu (new PopupMenu());
720     FloatingWindow* pMenuWindow = dynamic_cast<FloatingWindow*>(pMenu->GetWindow());
721     if (pMenuWindow != NULL)
722     {
723         pMenuWindow->SetPopupModeFlags(pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE);
724     }
725 
726     SidebarResource aLocalResource;
727 
728     // Add one entry for every tool panel element to individually make
729     // them visible or hide them.
730     {
731         sal_Int32 nIndex (MID_FIRST_PANEL);
732         for(::std::vector<TabBar::DeckMenuData>::const_iterator
733                 iItem(rDeckSelectionData.begin()),
734                 iEnd(rDeckSelectionData.end());
735             iItem!=iEnd;
736             ++iItem)
737         {
738             pMenu->InsertItem(nIndex, iItem->get<0>(), MIB_RADIOCHECK);
739             pMenu->CheckItem(nIndex, iItem->get<2>());
740             ++nIndex;
741         }
742     }
743 
744     pMenu->InsertSeparator();
745 
746     // Add entry for docking or un-docking the tool panel.
747     if (mpParentWindow->IsFloatingMode())
748         pMenu->InsertItem(MID_LOCK_TASK_PANEL, String(SfxResId(STR_SFX_DOCK)));
749     else
750         pMenu->InsertItem(MID_UNLOCK_TASK_PANEL, String(SfxResId(STR_SFX_UNDOCK)));
751 
752     // Add sub menu for customization (hiding of deck tabs.)
753     PopupMenu* pCustomizationMenu = new PopupMenu();
754     {
755         sal_Int32 nIndex (MID_FIRST_HIDE);
756         for(::std::vector<TabBar::DeckMenuData>::const_iterator
757                 iItem(rDeckShowData.begin()),
758                 iEnd(rDeckShowData.end());
759             iItem!=iEnd;
760             ++iItem)
761         {
762             pCustomizationMenu->InsertItem(nIndex, iItem->get<0>(), MIB_CHECKABLE);
763             pCustomizationMenu->CheckItem(nIndex, iItem->get<2>());
764             ++nIndex;
765         }
766     }
767 
768     pCustomizationMenu->InsertSeparator();
769     pCustomizationMenu->InsertItem(MID_RESTORE_DEFAULT, String(SfxResId(STRING_RESTORE)));
770 
771     pMenu->InsertItem(MID_CUSTOMIZATION, String(SfxResId(STRING_CUSTOMIZATION)));
772     pMenu->SetPopupMenu(MID_CUSTOMIZATION, pCustomizationMenu);
773 
774     pMenu->RemoveDisabledEntries(sal_False, sal_False);
775 
776     return pMenu;
777 }
778 
779 
780 
781 
782 IMPL_LINK(SidebarController, OnMenuItemSelected, Menu*, pMenu)
783 {
784     if (pMenu == NULL)
785     {
786         OSL_ENSURE(pMenu!=NULL, "sfx2::sidebar::SidebarController::OnMenuItemSelected: illegal menu!");
787         return 0;
788     }
789 
790     pMenu->Deactivate();
791     const sal_Int32 nIndex (pMenu->GetCurItemId());
792     switch (nIndex)
793     {
794         case MID_UNLOCK_TASK_PANEL:
795             mpParentWindow->SetFloatingMode(sal_True);
796             break;
797 
798         case MID_LOCK_TASK_PANEL:
799             mpParentWindow->SetFloatingMode(sal_False);
800             break;
801 
802         case MID_RESTORE_DEFAULT:
803             mpTabBar->RestoreHideFlags();
804             break;
805 
806         default:
807         {
808             try
809             {
810                 if (nIndex >= MID_FIRST_PANEL && nIndex<MID_FIRST_HIDE)
811                     SwitchToDeck(mpTabBar->GetDeckIdForIndex(nIndex - MID_FIRST_PANEL));
812                 else if (nIndex >=MID_FIRST_HIDE)
813                     mpTabBar->ToggleHideFlag(nIndex-MID_FIRST_HIDE);
814             }
815             catch (RuntimeException&)
816             {
817             }
818         }
819         break;
820     }
821 
822     return 1;
823 }
824 
825 
826 
827 
828 void SidebarController::CloseDeck (void)
829 {
830     if ( ! mbIsDeckClosed)
831     {
832         mbIsDeckClosed = true;
833         if ( ! mpParentWindow->IsFloatingMode())
834             mnSavedSidebarWidth = SetChildWindowWidth(TabBar::GetDefaultWidth());
835         mpParentWindow->SetStyle(mpParentWindow->GetStyle() & ~WB_SIZEABLE);
836 
837         if (mpCurrentDeck)
838             mpCurrentDeck->Hide();
839 
840         NotifyResize();
841     }
842 }
843 
844 
845 
846 
847 void SidebarController::OpenDeck (void)
848 {
849     if (mbIsDeckClosed)
850     {
851         mbIsDeckClosed = false;
852         SetChildWindowWidth(mnSavedSidebarWidth);
853 
854         if (mpCurrentDeck)
855             mpCurrentDeck->Show();
856 
857         NotifyResize();
858     }
859 }
860 
861 
862 
863 
864 FocusManager& SidebarController::GetFocusManager (void)
865 {
866     return maFocusManager;
867 }
868 
869 
870 
871 
872 bool SidebarController::CanModifyChildWindowWidth (void) const
873 {
874     SfxSplitWindow* pSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
875     if (pSplitWindow == NULL)
876     {
877         return 0;
878     }
879 
880     sal_uInt16 nRow (0xffff);
881     sal_uInt16 nColumn (0xffff);
882     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
883 
884     sal_uInt16 nRowCount (pSplitWindow->GetWindowCount(nColumn));
885 
886     return nRowCount == 1;
887 }
888 
889 
890 
891 
892 sal_Int32 SidebarController::SetChildWindowWidth (const sal_Int32 nNewWidth)
893 {
894     SfxSplitWindow* pSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
895     if (pSplitWindow == NULL)
896         return 0;
897 
898     sal_uInt16 nRow (0xffff);
899     sal_uInt16 nColumn (0xffff);
900     pSplitWindow->GetWindowPos(mpParentWindow, nColumn, nRow);
901     const long nColumnWidth (pSplitWindow->GetLineSize(nColumn));
902 
903     Window* pWindow = mpParentWindow;
904     const Point aWindowPosition (pWindow->GetPosPixel());
905     const Size aWindowSize (pWindow->GetSizePixel());
906 
907     pSplitWindow->MoveWindow(
908         mpParentWindow,
909         Size(nNewWidth, aWindowSize.Height()),
910         nColumn,
911         nRow);
912 
913     return static_cast<sal_Int32>(nColumnWidth);
914 }
915 
916 
917 
918 
919 void SidebarController::RestrictWidth (void)
920 {
921     SfxSplitWindow* pSplitWindow = dynamic_cast<SfxSplitWindow*>(mpParentWindow->GetParent());
922     if (pSplitWindow != NULL)
923     {
924         const sal_uInt16 nId (pSplitWindow->GetItemId(mpParentWindow));
925         const sal_uInt16 nSetId (pSplitWindow->GetSet(nId));
926         // Minimum width is always that of the tabbar.
927         const sal_Int32 nMinimumWidth (TabBar::GetDefaultWidth());
928         // Maximum width depends on whether the deck is open or closed.
929         const sal_Int32 nMaximumWidth (
930             mbIsDeckClosed
931                 ? TabBar::GetDefaultWidth()
932                 : 400);
933         pSplitWindow->SetItemSizeRange(
934             nSetId,
935             Range(nMinimumWidth, nMaximumWidth));
936         if (nMinimumWidth == nMaximumWidth)
937             pSplitWindow->SetItemSize(nSetId, nMinimumWidth);
938     }
939 }
940 
941 
942 } } // end of namespace sfx2::sidebar
943