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