Bump version to 5.0-14
[LibreOffice.git] / svtools / source / toolpanel / drawerlayouter.cxx
blobc044105d38e1e339b4b35eff91f14f71f549d281
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <svtools/toolpanel/drawerlayouter.hxx>
22 #include "toolpaneldrawer.hxx"
24 #include <com/sun/star/accessibility/XAccessible.hpp>
26 #include <comphelper/accimplaccess.hxx>
27 #include <tools/diagnose_ex.h>
30 namespace svt
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::accessibility::XAccessible;
38 //= DrawerDeckLayouter
41 DrawerDeckLayouter::DrawerDeckLayouter( vcl::Window& i_rParentWindow, IToolPanelDeck& i_rPanels )
42 :m_rParentWindow( i_rParentWindow )
43 ,m_rPanelDeck( i_rPanels )
44 ,m_aDrawers()
45 ,m_aLastKnownActivePanel()
47 m_rPanelDeck.AddListener( *this );
49 // simulate PanelInserted events for the panels which are already there
50 for ( size_t i=0; i<m_rPanelDeck.GetPanelCount(); ++i )
51 PanelInserted( m_rPanelDeck.GetPanel( i ), i );
55 DrawerDeckLayouter::~DrawerDeckLayouter()
60 Rectangle DrawerDeckLayouter::Layout( const Rectangle& i_rDeckPlayground )
62 const size_t nPanelCount( m_rPanelDeck.GetPanelCount() );
63 if ( nPanelCount == 0 )
64 return i_rDeckPlayground;
66 const int nWidth( i_rDeckPlayground.GetWidth() );
67 ::boost::optional< size_t > aActivePanel( m_rPanelDeck.GetActivePanel() );
68 if ( !aActivePanel )
69 aActivePanel = m_aLastKnownActivePanel;
71 // arrange the title bars which are *above* the active panel (or *all* if there is no active panel), plus
72 // the title bar of the active panel itself
73 Point aUpperDrawerPos( i_rDeckPlayground.TopLeft() );
74 const size_t nUpperBound = !!aActivePanel ? *aActivePanel : nPanelCount - 1;
75 for ( size_t i=0; i<=nUpperBound; ++i )
77 long const nDrawerHeight = m_aDrawers[i]->GetPreferredHeightPixel();
78 m_aDrawers[i]->SetPosSizePixel(
79 aUpperDrawerPos, Size( nWidth, nDrawerHeight ) );
80 aUpperDrawerPos.Move( 0, nDrawerHeight );
83 // arrange title bars which are below the active panel (or *none* if there is no active panel)
84 Point aLowerDrawerPos( i_rDeckPlayground.BottomLeft() );
85 for ( size_t j = nPanelCount - 1; j > nUpperBound; --j )
87 long const nDrawerHeight = m_aDrawers[j]->GetPreferredHeightPixel();
88 m_aDrawers[j]->SetPosSizePixel(
89 Point( aLowerDrawerPos.X(), aLowerDrawerPos.Y() - nDrawerHeight + 1 ),
90 Size( nWidth, nDrawerHeight )
92 aLowerDrawerPos.Move( 0, -nDrawerHeight );
95 // fincally calculate the rectangle for the active panel
96 return Rectangle(
97 aUpperDrawerPos,
98 Size( nWidth, aLowerDrawerPos.Y() - aUpperDrawerPos.Y() + 1 )
103 void DrawerDeckLayouter::Destroy()
105 while ( !m_aDrawers.empty() )
106 impl_removeDrawer( 0 );
107 m_rPanelDeck.RemoveListener( *this );
111 void DrawerDeckLayouter::SetFocusToPanelSelector()
113 const size_t nPanelCount( m_rPanelDeck.GetPanelCount() );
114 if ( !nPanelCount )
115 // nothing to focus
116 return;
117 ::boost::optional< size_t > aActivePanel( m_rPanelDeck.GetActivePanel() );
118 if ( !aActivePanel )
119 aActivePanel = 0;
120 ENSURE_OR_RETURN_VOID( *aActivePanel < m_aDrawers.size(), "DrawerDeckLayouter::SetFocusToPanelSelector: invalid active panel, or inconsistent drawers!" );
121 m_aDrawers[ *aActivePanel ]->GrabFocus();
125 size_t DrawerDeckLayouter::GetAccessibleChildCount() const
127 return m_aDrawers.size();
131 Reference< XAccessible > DrawerDeckLayouter::GetAccessibleChild( const size_t i_nChildIndex, const Reference< XAccessible >& i_rParentAccessible )
133 ENSURE_OR_RETURN( i_nChildIndex < m_aDrawers.size(), "illegal index", NULL );
135 VclPtr<ToolPanelDrawer> pDrawer( m_aDrawers[ i_nChildIndex ] );
137 Reference< XAccessible > xItemAccessible = pDrawer->GetAccessible( false );
138 if ( !xItemAccessible.is() )
140 xItemAccessible = pDrawer->GetAccessible( true );
141 ENSURE_OR_RETURN( xItemAccessible.is(), "illegal accessible provided by the drawer implementation!", NULL );
142 OSL_VERIFY( ::comphelper::OAccessibleImplementationAccess::setAccessibleParent( xItemAccessible->getAccessibleContext(),
143 i_rParentAccessible ) );
146 return xItemAccessible;
150 void DrawerDeckLayouter::PanelInserted( const PToolPanel& i_pPanel, const size_t i_nPosition )
152 OSL_PRECOND( i_nPosition <= m_aDrawers.size(), "DrawerDeckLayouter::PanelInserted: inconsistency!" );
154 VclPtrInstance<ToolPanelDrawer> pDrawer( m_rParentWindow, i_pPanel->GetDisplayName() );
155 pDrawer->SetHelpId( i_pPanel->GetHelpID() );
156 // proper Z-Order
157 if ( i_nPosition == 0 )
159 pDrawer->SetZOrder( NULL, WINDOW_ZORDER_FIRST );
161 else
163 ToolPanelDrawer* pFirstDrawer( m_aDrawers[ i_nPosition - 1 ] );
164 pDrawer->SetZOrder( pFirstDrawer, WINDOW_ZORDER_BEHIND );
167 pDrawer->Show();
168 pDrawer->AddEventListener( LINK( this, DrawerDeckLayouter, OnWindowEvent ) );
169 m_aDrawers.insert( m_aDrawers.begin() + i_nPosition, pDrawer );
170 impl_triggerRearrange();
174 void DrawerDeckLayouter::PanelRemoved( const size_t i_nPosition )
176 impl_removeDrawer( i_nPosition );
177 impl_triggerRearrange();
181 void DrawerDeckLayouter::impl_triggerRearrange() const
183 // this is somewhat hacky, it assumes that the parent of our panels is a tool panel deck, which, in its
184 // Resize implementation, rearrances all elements.
185 m_rParentWindow.Resize();
189 void DrawerDeckLayouter::ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive )
191 if ( !!i_rOldActive )
193 OSL_ENSURE( *i_rOldActive < m_aDrawers.size(), "DrawerDeckLayouter::ActivePanelChanged: illegal old index!" );
194 m_aDrawers[ *i_rOldActive ]->SetExpanded( false );
197 if ( !!i_rNewActive )
199 OSL_ENSURE( *i_rNewActive < m_aDrawers.size(), "DrawerDeckLayouter::ActivePanelChanged: illegal new index!" );
200 m_aDrawers[ *i_rNewActive ]->SetExpanded( true );
203 impl_triggerRearrange();
207 void DrawerDeckLayouter::LayouterChanged( const PDeckLayouter& i_rNewLayouter )
209 // not interested in
210 (void)i_rNewLayouter;
214 size_t DrawerDeckLayouter::impl_getPanelPositionFromWindow( const vcl::Window* i_pDrawerWindow ) const
216 for ( auto drawerPos = m_aDrawers.begin(); drawerPos != m_aDrawers.end(); ++drawerPos )
218 if ( drawerPos->get() == i_pDrawerWindow )
219 return drawerPos - m_aDrawers.begin();
221 return m_aDrawers.size();
225 void DrawerDeckLayouter::impl_removeDrawer( const size_t i_nPosition )
227 OSL_PRECOND( i_nPosition < m_aDrawers.size(), "DrawerDeckLayouter::impl_removeDrawer: invalid panel position!" );
228 m_aDrawers[ i_nPosition ]->RemoveEventListener( LINK( this, DrawerDeckLayouter, OnWindowEvent ) );
229 m_aDrawers.erase( m_aDrawers.begin() + i_nPosition );
233 IMPL_LINK( DrawerDeckLayouter, OnWindowEvent, VclSimpleEvent*, i_pEvent )
235 const VclWindowEvent* pWindowEvent = PTR_CAST( VclWindowEvent, i_pEvent );
236 ENSURE_OR_RETURN( pWindowEvent, "no WindowEvent", 0L );
238 bool bActivatePanel = false;
239 switch ( pWindowEvent->GetId() )
241 case VCLEVENT_WINDOW_MOUSEBUTTONUP:
243 const MouseEvent* pMouseEvent = static_cast< const MouseEvent* >( pWindowEvent->GetData() );
244 ENSURE_OR_RETURN( pMouseEvent, "no mouse event with MouseButtonUp", 0L );
245 if ( pMouseEvent->GetButtons() == MOUSE_LEFT )
247 bActivatePanel = true;
250 break;
251 case VCLEVENT_WINDOW_KEYINPUT:
253 const KeyEvent* pKeyEvent = static_cast< const KeyEvent* >( pWindowEvent->GetData() );
254 ENSURE_OR_RETURN( pKeyEvent, "no key event with KeyInput", 0L );
255 const vcl::KeyCode& rKeyCode( pKeyEvent->GetKeyCode() );
256 if ( ( rKeyCode.GetModifier() == 0 ) && ( rKeyCode.GetCode() == KEY_RETURN ) )
258 bActivatePanel = true;
261 break;
263 if ( bActivatePanel )
265 const size_t nPanelPos = impl_getPanelPositionFromWindow( pWindowEvent->GetWindow() );
266 if ( nPanelPos != m_rPanelDeck.GetActivePanel() )
268 m_rPanelDeck.ActivatePanel( nPanelPos );
270 else
272 PToolPanel pPanel( m_rPanelDeck.GetPanel( nPanelPos ) );
273 pPanel->GrabFocus();
275 return 1L;
277 return 0L;
281 void DrawerDeckLayouter::Dying()
283 Destroy();
287 } // namespace svt
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */