LCOV - code coverage report
Current view: top level - svtools/source/toolpanel - drawerlayouter.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 123 0.8 %
Date: 2014-11-03 Functions: 2 20 10.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10