Line data Source code
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 :
21 : #include "svtools/toolpanel/drawerlayouter.hxx"
22 : #include "toolpaneldrawer.hxx"
23 :
24 : #include <com/sun/star/accessibility/XAccessible.hpp>
25 :
26 : #include <comphelper/accimplaccess.hxx>
27 : #include <tools/diagnose_ex.h>
28 :
29 : //......................................................................................................................
30 : namespace svt
31 : {
32 : //......................................................................................................................
33 :
34 : /** === begin UNO using === **/
35 : using ::com::sun::star::uno::Reference;
36 : using ::com::sun::star::accessibility::XAccessible;
37 : /** === end UNO using === **/
38 :
39 : //==================================================================================================================
40 : //= DrawerDeckLayouter
41 : //==================================================================================================================
42 : //------------------------------------------------------------------------------------------------------------------
43 0 : DrawerDeckLayouter::DrawerDeckLayouter( ::Window& i_rParentWindow, IToolPanelDeck& i_rPanels )
44 : :m_rParentWindow( i_rParentWindow )
45 : ,m_rPanelDeck( i_rPanels )
46 : ,m_aDrawers()
47 0 : ,m_aLastKnownActivePanel()
48 : {
49 0 : m_rPanelDeck.AddListener( *this );
50 :
51 : // simulate PanelInserted events for the panels which are already there
52 0 : for ( size_t i=0; i<m_rPanelDeck.GetPanelCount(); ++i )
53 0 : PanelInserted( m_rPanelDeck.GetPanel( i ), i );
54 0 : }
55 :
56 : //------------------------------------------------------------------------------------------------------------------
57 0 : DrawerDeckLayouter::~DrawerDeckLayouter()
58 : {
59 0 : }
60 :
61 : //------------------------------------------------------------------------------------------------------------------
62 0 : IMPLEMENT_IREFERENCE( DrawerDeckLayouter )
63 :
64 : //------------------------------------------------------------------------------------------------------------------
65 0 : Rectangle DrawerDeckLayouter::Layout( const Rectangle& i_rDeckPlayground )
66 : {
67 0 : const size_t nPanelCount( m_rPanelDeck.GetPanelCount() );
68 0 : if ( nPanelCount == 0 )
69 0 : return i_rDeckPlayground;
70 :
71 0 : const int nWidth( i_rDeckPlayground.GetWidth() );
72 0 : ::boost::optional< size_t > aActivePanel( m_rPanelDeck.GetActivePanel() );
73 0 : if ( !aActivePanel )
74 0 : aActivePanel = m_aLastKnownActivePanel;
75 :
76 : // arrange the title bars which are *above* the active panel (or *all* if there is no active panel), plus
77 : // the title bar of the active panel itself
78 0 : Point aUpperDrawerPos( i_rDeckPlayground.TopLeft() );
79 0 : const size_t nUpperBound = !!aActivePanel ? *aActivePanel : nPanelCount - 1;
80 0 : for ( size_t i=0; i<=nUpperBound; ++i )
81 : {
82 0 : long const nDrawerHeight = m_aDrawers[i]->GetPreferredHeightPixel();
83 0 : m_aDrawers[i]->SetPosSizePixel(
84 0 : aUpperDrawerPos, Size( nWidth, nDrawerHeight ) );
85 0 : aUpperDrawerPos.Move( 0, nDrawerHeight );
86 : }
87 :
88 : // arrange title bars which are below the active panel (or *none* if there is no active panel)
89 0 : Point aLowerDrawerPos( i_rDeckPlayground.BottomLeft() );
90 0 : for ( size_t j = nPanelCount - 1; j > nUpperBound; --j )
91 : {
92 0 : long const nDrawerHeight = m_aDrawers[j]->GetPreferredHeightPixel();
93 0 : m_aDrawers[j]->SetPosSizePixel(
94 0 : Point( aLowerDrawerPos.X(), aLowerDrawerPos.Y() - nDrawerHeight + 1 ),
95 : Size( nWidth, nDrawerHeight )
96 0 : );
97 0 : aLowerDrawerPos.Move( 0, -nDrawerHeight );
98 : }
99 :
100 : // fincally calculate the rectangle for the active panel
101 : return Rectangle(
102 : aUpperDrawerPos,
103 0 : Size( nWidth, aLowerDrawerPos.Y() - aUpperDrawerPos.Y() + 1 )
104 0 : );
105 : }
106 :
107 : //------------------------------------------------------------------------------------------------------------------
108 0 : void DrawerDeckLayouter::Destroy()
109 : {
110 0 : while ( !m_aDrawers.empty() )
111 0 : impl_removeDrawer( 0 );
112 0 : m_rPanelDeck.RemoveListener( *this );
113 0 : }
114 :
115 : //------------------------------------------------------------------------------------------------------------------
116 0 : void DrawerDeckLayouter::SetFocusToPanelSelector()
117 : {
118 0 : const size_t nPanelCount( m_rPanelDeck.GetPanelCount() );
119 0 : if ( !nPanelCount )
120 : // nothing to focus
121 : return;
122 0 : ::boost::optional< size_t > aActivePanel( m_rPanelDeck.GetActivePanel() );
123 0 : if ( !aActivePanel )
124 0 : aActivePanel = 0;
125 0 : ENSURE_OR_RETURN_VOID( *aActivePanel < m_aDrawers.size(), "DrawerDeckLayouter::SetFocusToPanelSelector: invalid active panel, or inconsistent drawers!" );
126 0 : m_aDrawers[ *aActivePanel ]->GrabFocus();
127 : }
128 :
129 : //------------------------------------------------------------------------------------------------------------------
130 0 : size_t DrawerDeckLayouter::GetAccessibleChildCount() const
131 : {
132 0 : return m_aDrawers.size();
133 : }
134 :
135 : //------------------------------------------------------------------------------------------------------------------
136 0 : Reference< XAccessible > DrawerDeckLayouter::GetAccessibleChild( const size_t i_nChildIndex, const Reference< XAccessible >& i_rParentAccessible )
137 : {
138 0 : ENSURE_OR_RETURN( i_nChildIndex < m_aDrawers.size(), "illegal index", NULL );
139 :
140 0 : const PToolPanelDrawer pDrawer( m_aDrawers[ i_nChildIndex ] );
141 :
142 0 : Reference< XAccessible > xItemAccessible = pDrawer->GetAccessible( sal_False );
143 0 : if ( !xItemAccessible.is() )
144 : {
145 0 : xItemAccessible = pDrawer->GetAccessible( sal_True );
146 0 : ENSURE_OR_RETURN( xItemAccessible.is(), "illegal accessible provided by the drawer implementation!", NULL );
147 0 : OSL_VERIFY( ::comphelper::OAccessibleImplementationAccess::setAccessibleParent( xItemAccessible->getAccessibleContext(),
148 : i_rParentAccessible ) );
149 : }
150 :
151 0 : return xItemAccessible;
152 : }
153 :
154 : //------------------------------------------------------------------------------------------------------------------
155 0 : void DrawerDeckLayouter::PanelInserted( const PToolPanel& i_pPanel, const size_t i_nPosition )
156 : {
157 : OSL_PRECOND( i_nPosition <= m_aDrawers.size(), "DrawerDeckLayouter::PanelInserted: inconsistency!" );
158 :
159 0 : PToolPanelDrawer pDrawer( new ToolPanelDrawer( m_rParentWindow, i_pPanel->GetDisplayName() ) );
160 0 : pDrawer->SetHelpId( i_pPanel->GetHelpID() );
161 : // proper Z-Order
162 0 : if ( i_nPosition == 0 )
163 : {
164 0 : pDrawer->SetZOrder( NULL, WINDOW_ZORDER_FIRST );
165 : }
166 : else
167 : {
168 0 : const PToolPanelDrawer pFirstDrawer( m_aDrawers[ i_nPosition - 1 ] );
169 0 : pDrawer->SetZOrder( pFirstDrawer.get(), WINDOW_ZORDER_BEHIND );
170 : }
171 :
172 0 : pDrawer->Show();
173 0 : pDrawer->AddEventListener( LINK( this, DrawerDeckLayouter, OnWindowEvent ) );
174 0 : m_aDrawers.insert( m_aDrawers.begin() + i_nPosition, pDrawer );
175 0 : impl_triggerRearrange();
176 0 : }
177 :
178 : //------------------------------------------------------------------------------------------------------------------
179 0 : void DrawerDeckLayouter::PanelRemoved( const size_t i_nPosition )
180 : {
181 0 : impl_removeDrawer( i_nPosition );
182 0 : impl_triggerRearrange();
183 0 : }
184 :
185 : //------------------------------------------------------------------------------------------------------------------
186 0 : void DrawerDeckLayouter::impl_triggerRearrange() const
187 : {
188 : // this is somewhat hacky, it assumes that the parent of our panels is a tool panel deck, which, in its
189 : // Resize implementation, rearrances all elements.
190 0 : m_rParentWindow.Resize();
191 0 : }
192 :
193 : //------------------------------------------------------------------------------------------------------------------
194 0 : void DrawerDeckLayouter::ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive )
195 : {
196 0 : if ( !!i_rOldActive )
197 : {
198 : OSL_ENSURE( *i_rOldActive < m_aDrawers.size(), "DrawerDeckLayouter::ActivePanelChanged: illegal old index!" );
199 0 : m_aDrawers[ *i_rOldActive ]->SetExpanded( false );
200 : }
201 :
202 0 : if ( !!i_rNewActive )
203 : {
204 : OSL_ENSURE( *i_rNewActive < m_aDrawers.size(), "DrawerDeckLayouter::ActivePanelChanged: illegal new index!" );
205 0 : m_aDrawers[ *i_rNewActive ]->SetExpanded( true );
206 : }
207 :
208 0 : impl_triggerRearrange();
209 0 : }
210 :
211 : //------------------------------------------------------------------------------------------------------------------
212 0 : void DrawerDeckLayouter::LayouterChanged( const PDeckLayouter& i_rNewLayouter )
213 : {
214 : // not interested in
215 : (void)i_rNewLayouter;
216 0 : }
217 :
218 : //------------------------------------------------------------------------------------------------------------------
219 0 : size_t DrawerDeckLayouter::impl_getPanelPositionFromWindow( const Window* i_pDrawerWindow ) const
220 : {
221 0 : for ( ::std::vector< PToolPanelDrawer >::const_iterator drawerPos = m_aDrawers.begin();
222 0 : drawerPos != m_aDrawers.end();
223 : ++drawerPos
224 : )
225 : {
226 0 : if ( drawerPos->get() == i_pDrawerWindow )
227 0 : return drawerPos - m_aDrawers.begin();
228 : }
229 0 : return m_aDrawers.size();
230 : }
231 :
232 : //------------------------------------------------------------------------------------------------------------------
233 0 : void DrawerDeckLayouter::impl_removeDrawer( const size_t i_nPosition )
234 : {
235 : OSL_PRECOND( i_nPosition < m_aDrawers.size(), "DrawerDeckLayouter::impl_removeDrawer: invalid panel position!" );
236 0 : m_aDrawers[ i_nPosition ]->RemoveEventListener( LINK( this, DrawerDeckLayouter, OnWindowEvent ) );
237 : OSL_ENSURE( m_aDrawers[ i_nPosition ].unique(), "DrawerDeckLayouter::impl_removeDrawer: somebody else is still holding a reference!" );
238 0 : m_aDrawers.erase( m_aDrawers.begin() + i_nPosition );
239 0 : }
240 :
241 : //------------------------------------------------------------------------------------------------------------------
242 0 : IMPL_LINK( DrawerDeckLayouter, OnWindowEvent, VclSimpleEvent*, i_pEvent )
243 : {
244 0 : const VclWindowEvent* pWindowEvent = PTR_CAST( VclWindowEvent, i_pEvent );
245 0 : ENSURE_OR_RETURN( pWindowEvent, "no WindowEvent", 0L );
246 :
247 0 : bool bActivatePanel = false;
248 0 : switch ( pWindowEvent->GetId() )
249 : {
250 : case VCLEVENT_WINDOW_MOUSEBUTTONUP:
251 : {
252 0 : const MouseEvent* pMouseEvent = static_cast< const MouseEvent* >( pWindowEvent->GetData() );
253 0 : ENSURE_OR_RETURN( pMouseEvent, "no mouse event with MouseButtonUp", 0L );
254 0 : if ( pMouseEvent->GetButtons() == MOUSE_LEFT )
255 : {
256 0 : bActivatePanel = true;
257 : }
258 : }
259 0 : break;
260 : case VCLEVENT_WINDOW_KEYINPUT:
261 : {
262 0 : const KeyEvent* pKeyEvent = static_cast< const KeyEvent* >( pWindowEvent->GetData() );
263 0 : ENSURE_OR_RETURN( pKeyEvent, "no key event with KeyInput", 0L );
264 0 : const KeyCode& rKeyCode( pKeyEvent->GetKeyCode() );
265 0 : if ( ( rKeyCode.GetModifier() == 0 ) && ( rKeyCode.GetCode() == KEY_RETURN ) )
266 : {
267 0 : bActivatePanel = true;
268 : }
269 : }
270 0 : break;
271 : }
272 0 : if ( bActivatePanel )
273 : {
274 0 : const size_t nPanelPos = impl_getPanelPositionFromWindow( pWindowEvent->GetWindow() );
275 0 : if ( nPanelPos != m_rPanelDeck.GetActivePanel() )
276 : {
277 0 : m_rPanelDeck.ActivatePanel( nPanelPos );
278 : }
279 : else
280 : {
281 0 : PToolPanel pPanel( m_rPanelDeck.GetPanel( nPanelPos ) );
282 0 : pPanel->GrabFocus();
283 : }
284 0 : return 1L;
285 : }
286 0 : return 0L;
287 : }
288 :
289 : //------------------------------------------------------------------------------------------------------------------
290 0 : void DrawerDeckLayouter::Dying()
291 : {
292 0 : Destroy();
293 0 : }
294 :
295 : //......................................................................................................................
296 : } // namespace svt
297 : //......................................................................................................................
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|