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