1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <vcl/builderfactory.hxx>
21 #include <vcl/layout.hxx>
22 #include <vcl/notebookbar/notebookbar.hxx>
23 #include <vcl/tabpage.hxx>
24 #include <sfx2/viewfrm.hxx>
25 #include <notebookbar/NotebookbarTabControl.hxx>
26 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
27 #include <com/sun/star/ui/ItemType.hpp>
28 #include <com/sun/star/frame/XModuleManager.hpp>
29 #include <com/sun/star/frame/ModuleManager.hpp>
30 #include <com/sun/star/frame/XFrame.hpp>
31 #include <com/sun/star/uno/Reference.h>
32 #include <com/sun/star/awt/PopupMenuDirection.hpp>
33 #include <com/sun/star/frame/XPopupMenuController.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <comphelper/propertyvalue.hxx>
36 #include <sidebar/SidebarToolBox.hxx>
37 #include <cppuhelper/implbase.hxx>
38 
39 #define ICON_SIZE 25
40 #define TOOLBAR_STR "private:resource/toolbar/notebookbarshortcuts"
41 
42 using namespace css::uno;
43 using namespace css::ui;
44 using namespace css::frame;
45 
46 class ChangedUIEventListener : public ::cppu::WeakImplHelper<XUIConfigurationListener>
47 {
48     VclPtr<NotebookbarTabControl> m_pParent;
49 
50 public:
ChangedUIEventListener(NotebookbarTabControl * p)51     explicit ChangedUIEventListener(NotebookbarTabControl *p)
52     : m_pParent(p)
53     {
54         try
55         {
56             if( SfxViewFrame::Current() )
57             {
58                 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
59                 const Reference<XModuleManager> xModuleManager  = ModuleManager::create( xContext );
60                 Reference<XFrame> xFrame = SfxViewFrame::Current()->GetFrame().GetFrameInterface();
61                 OUString aModuleName = xModuleManager->identify( xFrame );
62 
63                 Reference<XUIConfigurationManager> m_xConfigManager;
64                 Reference<XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier(
65                     theModuleUIConfigurationManagerSupplier::get( xContext ) );
66                 m_xConfigManager.set( xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleName ) );
67                 css::uno::Reference< css::ui::XUIConfiguration > xConfig( m_xConfigManager, css::uno::UNO_QUERY_THROW );
68                 xConfig->addConfigurationListener( this );
69             }
70         }
71         catch( const css::uno::RuntimeException& ) {}
72     }
73 
74     // XUIConfigurationListener
elementInserted(const ConfigurationEvent & rEvent)75     virtual void SAL_CALL elementInserted( const ConfigurationEvent& rEvent ) override
76     {
77         if( rEvent.ResourceURL == TOOLBAR_STR )
78         {
79             m_pParent->m_bInvalidate = true;
80             m_pParent->StateChanged(StateChangedType::UpdateMode);
81         }
82     }
83 
elementRemoved(const ConfigurationEvent & rEvent)84     virtual void SAL_CALL elementRemoved( const ConfigurationEvent& rEvent ) override
85     {
86         elementInserted( rEvent );
87     }
88 
elementReplaced(const ConfigurationEvent & rEvent)89     virtual void SAL_CALL elementReplaced( const ConfigurationEvent& rEvent ) override
90     {
91         elementInserted( rEvent );
92     }
93 
disposing(const::css::lang::EventObject &)94     virtual void SAL_CALL disposing(const ::css::lang::EventObject&) override
95     {
96         try
97         {
98             if( SfxViewFrame::Current() )
99             {
100                 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
101                 const Reference<XModuleManager> xModuleManager  = ModuleManager::create( xContext );
102                 Reference<XFrame> xFrame = SfxViewFrame::Current()->GetFrame().GetFrameInterface();
103                 OUString aModuleName = xModuleManager->identify( xFrame );
104 
105                 Reference<XUIConfigurationManager> m_xConfigManager;
106                 Reference<XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier(
107                     theModuleUIConfigurationManagerSupplier::get( xContext ) );
108                 m_xConfigManager.set( xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleName ) );
109                 css::uno::Reference< css::ui::XUIConfiguration > xConfig( m_xConfigManager, css::uno::UNO_QUERY_THROW );
110                 xConfig->removeConfigurationListener( this );
111             }
112         }
113         catch( const css::uno::RuntimeException& ) {}
114 
115         m_pParent.clear();
116     }
117 };
118 
119 namespace {
120 
121 class ShortcutsToolBox : public sfx2::sidebar::SidebarToolBox
122 {
123 public:
ShortcutsToolBox(Window * pParent)124     ShortcutsToolBox( Window* pParent )
125     : sfx2::sidebar::SidebarToolBox( pParent )
126     {
127         mbUseDefaultButtonSize = false;
128         mbSideBar = false;
129         SetToolboxButtonSize(ToolBoxButtonSize::Small);
130     }
131 
KeyInput(const KeyEvent & rKEvt)132     virtual void KeyInput( const KeyEvent& rKEvt ) override
133     {
134         if ( rKEvt.GetKeyCode().IsMod1() )
135         {
136             sal_uInt16 nCode( rKEvt.GetKeyCode().GetCode() );
137             if ( nCode == KEY_RIGHT || nCode == KEY_LEFT )
138             {
139                 GetParent()->KeyInput( rKEvt );
140                 return;
141             }
142         }
143         return sfx2::sidebar::SidebarToolBox::KeyInput( rKEvt );
144     }
145 };
146 
147 }
148 
NotebookbarTabControl(Window * pParent)149 NotebookbarTabControl::NotebookbarTabControl( Window* pParent )
150 : NotebookbarTabControlBase( pParent )
151 , m_bInitialized( false )
152 , m_bInvalidate( true )
153 {
154 }
155 
~NotebookbarTabControl()156 NotebookbarTabControl::~NotebookbarTabControl()
157 {
158 }
159 
ArrowStops(sal_uInt16 nCode)160 void NotebookbarTabControl::ArrowStops( sal_uInt16 nCode )
161 {
162     ToolBox* pToolBox( GetToolBox() );
163     Control* pOpenMenu( GetOpenMenu() );
164 
165     if ( nCode == KEY_LEFT )
166     {
167         if ( HasFocus() )
168         {
169             if ( pToolBox )
170                 pToolBox->GrabFocus();
171             else if ( pOpenMenu )
172                 pOpenMenu->GrabFocus();
173         }
174         else if ( pToolBox && pToolBox->HasFocus() )
175         {
176             if ( pOpenMenu )
177                 pOpenMenu->GrabFocus();
178             else
179                 GrabFocus();
180         }
181         else if ( pOpenMenu && pOpenMenu->HasFocus() )
182         {
183             GrabFocus();
184         }
185     }
186     else if ( nCode == KEY_RIGHT )
187     {
188         if ( HasFocus() )
189         {
190             if ( pOpenMenu )
191                 pOpenMenu->GrabFocus();
192             else if ( pToolBox )
193                 pToolBox->GrabFocus();
194         }
195         else if ( pToolBox && pToolBox->HasFocus() )
196         {
197             GrabFocus();
198         }
199         else if ( pOpenMenu && pOpenMenu->HasFocus() )
200         {
201             if ( pToolBox )
202                 pToolBox->GrabFocus();
203             else
204                 GrabFocus();
205         }
206     }
207 }
208 
KeyInput(const KeyEvent & rKEvt)209 void NotebookbarTabControl::KeyInput( const KeyEvent& rKEvt )
210 {
211     if ( rKEvt.GetKeyCode().IsMod1() )
212     {
213         sal_uInt16 nCode( rKEvt.GetKeyCode().GetCode() );
214         if ( nCode == KEY_RIGHT || nCode == KEY_LEFT )
215         {
216             ArrowStops( nCode );
217             return;
218         }
219     }
220     return NotebookbarTabControlBase::KeyInput( rKEvt );
221 }
222 
EventNotify(NotifyEvent & rNEvt)223 bool NotebookbarTabControl::EventNotify( NotifyEvent& rNEvt )
224 {
225     if ( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
226     {
227         const vcl::KeyCode& rKey = rNEvt.GetKeyEvent()->GetKeyCode();
228         sal_uInt16 nCode = rKey.GetCode();
229         if ( rKey.IsMod1() && ( nCode == KEY_RIGHT || nCode == KEY_LEFT ) )
230         {
231             ArrowStops( nCode );
232             return true;
233         }
234     }
235     return NotebookbarTabControlBase::EventNotify( rNEvt );
236 }
237 
StateChanged(StateChangedType nStateChange)238 void NotebookbarTabControl::StateChanged(StateChangedType nStateChange)
239 {
240     if( !m_bInitialized && SfxViewFrame::Current() )
241     {
242         VclPtr<ShortcutsToolBox> pShortcuts = VclPtr<ShortcutsToolBox>::Create( this );
243         pShortcuts->Show();
244 
245         SetToolBox( static_cast<ToolBox*>( pShortcuts.get() ) );
246         SetIconClickHdl( LINK( this, NotebookbarTabControl, OpenNotebookbarPopupMenu ) );
247 
248         m_pListener = new ChangedUIEventListener( this );
249 
250         m_bInitialized = true;
251     }
252     if( m_bInitialized && m_bInvalidate && SfxViewFrame::Current() )
253     {
254         ToolBox* pToolBox = GetToolBox();
255         if( !pToolBox )
256             return;
257 
258         pToolBox->Clear();
259 
260         Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
261         const Reference<XModuleManager> xModuleManager  = ModuleManager::create( xContext );
262         m_xFrame = SfxViewFrame::Current()->GetFrame().GetFrameInterface();
263         OUString aModuleName = xModuleManager->identify( m_xFrame );
264 
265         FillShortcutsToolBox( xContext, m_xFrame, aModuleName, pToolBox );
266 
267         Size aSize( pToolBox->GetOptimalSize() );
268         Point aPos( ICON_SIZE + 10, 0 );
269         pToolBox->SetPosSizePixel( aPos, aSize );
270         ImplPlaceTabs( GetSizePixel().getWidth() );
271 
272         m_bInvalidate = false;
273     }
274     NotebookbarTabControlBase::StateChanged( nStateChange );
275 }
276 
FillShortcutsToolBox(Reference<XComponentContext> const & xContext,const Reference<XFrame> & xFrame,const OUString & aModuleName,ToolBox * pShortcuts)277 void NotebookbarTabControl::FillShortcutsToolBox(Reference<XComponentContext> const & xContext,
278                                           const Reference<XFrame>& xFrame,
279                                           const OUString& aModuleName,
280                                           ToolBox* pShortcuts
281 )
282 {
283     Reference<::com::sun::star::container::XIndexAccess> xIndex;
284 
285     try
286     {
287         Reference<XUIConfigurationManager> m_xConfigManager;
288         Reference<XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier(
289             theModuleUIConfigurationManagerSupplier::get( xContext ) );
290         m_xConfigManager.set( xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleName ) );
291         xIndex = m_xConfigManager->getSettings( TOOLBAR_STR, false );
292     }
293     catch( const Exception& ) {}
294 
295     if ( !xIndex.is() )
296         return;
297 
298     Sequence< css::beans::PropertyValue > aPropSequence;
299     for ( sal_Int32 i = 0; i < xIndex->getCount(); ++i )
300     {
301         try
302         {
303             if ( xIndex->getByIndex( i ) >>= aPropSequence )
304             {
305                 OUString aCommandURL;
306                 sal_uInt16 nType = ItemType::DEFAULT;
307                 bool bVisible = true;
308 
309                 for ( const auto& aProp: std::as_const(aPropSequence) )
310                 {
311                     if ( aProp.Name == "CommandURL" )
312                         aProp.Value >>= aCommandURL;
313                     else if ( aProp.Name == "Type" )
314                         aProp.Value >>= nType;
315                     else if ( aProp.Name == "IsVisible" )
316                         aProp.Value >>= bVisible;
317                 }
318                 if ( bVisible && ( nType == ItemType::DEFAULT ) )
319                     pShortcuts->InsertItem( aCommandURL, xFrame, ToolBoxItemBits::ICON_ONLY, Size( ICON_SIZE, ICON_SIZE ) );
320             }
321         }
322         catch ( const Exception& )
323         {
324             break;
325         }
326     }
327 }
328 
IMPL_LINK(NotebookbarTabControl,OpenNotebookbarPopupMenu,NotebookBar *,pNotebookbar,void)329 IMPL_LINK(NotebookbarTabControl, OpenNotebookbarPopupMenu, NotebookBar*, pNotebookbar, void)
330 {
331     if (!pNotebookbar || !m_xFrame.is())
332         return;
333 
334     Sequence<Any> aArgs {
335         makeAny(comphelper::makePropertyValue("Value", OUString("notebookbar"))),
336         makeAny(comphelper::makePropertyValue("Frame", m_xFrame)) };
337 
338     Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
339     Reference<XPopupMenuController> xPopupController(
340         xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
341         "com.sun.star.comp.framework.ResourceMenuController", aArgs, xContext), UNO_QUERY);
342 
343     Reference<css::awt::XPopupMenu> xPopupMenu(xContext->getServiceManager()->createInstanceWithContext(
344         "com.sun.star.awt.PopupMenu", xContext), UNO_QUERY);
345 
346     if (!xPopupController.is() || !xPopupMenu.is())
347         return;
348 
349     xPopupController->setPopupMenu(xPopupMenu);
350     Point aPos(pNotebookbar->GetSizePixel().getWidth(), NotebookbarTabControl::GetHeaderHeight() - ICON_SIZE + 10);
351     xPopupMenu->execute(pNotebookbar->GetComponentInterface(),
352                         css::awt::Rectangle(aPos.X(), aPos.Y(), 1, 1),
353                         css::awt::PopupMenuDirection::EXECUTE_DOWN);
354 
355     Reference<css::lang::XComponent> xComponent(xPopupController, UNO_QUERY);
356     if (xComponent.is())
357         xComponent->dispose();
358 }
359 
calculateRequisition() const360 Size NotebookbarTabControl::calculateRequisition() const
361 {
362     Size aSize = NotebookbarTabControlBase::calculateRequisition();
363 
364     for (int i = 0; i < GetPageCount(); i++)
365     {
366         vcl::Window* pChild = GetTabPage(TabControl::GetPageId(i));
367 
368         if (pChild)
369         {
370             Size aChildSize = VclContainer::getLayoutRequisition(*pChild);
371 
372             if (aChildSize.getWidth() < aSize.getWidth())
373                 aSize.setWidth( aChildSize.Width() );
374         }
375     }
376 
377     if (aSize.Width() < 400)
378         aSize.setWidth( 400 );
379 
380     return aSize;
381 }
382 
383 VCL_BUILDER_FACTORY( NotebookbarTabControl )
384 
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
386