bump product version to 4.1.6.2
[LibreOffice.git] / framework / source / classes / fwktabwindow.cxx
blob5ca707a083b058867c52a1a7f3ac82302fd01343
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 .
20 // autogen include statement, do not remove
22 #include <classes/fwktabwindow.hxx>
23 #include "framework.hrc"
24 #include <classes/fwkresid.hxx>
26 #include <com/sun/star/awt/PosSize.hpp>
27 #include <com/sun/star/awt/XContainerWindowEventHandler.hpp>
28 #include <com/sun/star/awt/ContainerWindowProvider.hpp>
29 #include <com/sun/star/awt/XWindow.hpp>
30 #include <com/sun/star/awt/XWindowPeer.hpp>
31 #include <com/sun/star/awt/XControl.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <com/sun/star/graphic/XGraphic.hpp>
35 #include <comphelper/processfactory.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <tools/stream.hxx>
38 #include <tools/diagnose_ex.h>
39 #include <vcl/bitmap.hxx>
40 #include <vcl/image.hxx>
41 #include <vcl/msgbox.hxx>
43 const char EXTERNAL_EVENT[] = "external_event";
44 const char INITIALIZE_METHOD[] = "initialize";
46 using namespace ::com::sun::star;
48 namespace framework
51 // class FwkTabControl ---------------------------------------------------
52 FwkTabControl::FwkTabControl( Window* pParent, const ResId& rResId ) :
54 TabControl( pParent, rResId )
58 // -----------------------------------------------------------------------
60 void FwkTabControl::BroadcastEvent( sal_uLong nEvent )
62 if ( VCLEVENT_TABPAGE_ACTIVATE == nEvent || VCLEVENT_TABPAGE_DEACTIVATE == nEvent )
63 ImplCallEventListeners( nEvent, (void*)(sal_uIntPtr)GetCurPageId() );
64 else
66 SAL_WARN( "fwk", "FwkTabControl::BroadcastEvent(): illegal event" );
70 // class FwkTabPage ------------------------------------------------
72 FwkTabPage::FwkTabPage(
73 Window* pParent, const OUString& rPageURL,
74 const css::uno::Reference< css::awt::XContainerWindowEventHandler >& rEventHdl,
75 const css::uno::Reference< css::awt::XContainerWindowProvider >& rProvider ) :
77 TabPage( pParent, WB_DIALOGCONTROL | WB_TABSTOP | WB_CHILDDLGCTRL ),
79 m_sPageURL ( rPageURL ),
80 m_xEventHdl ( rEventHdl ),
81 m_xWinProvider ( rProvider )
86 // -----------------------------------------------------------------------
88 FwkTabPage::~FwkTabPage()
90 Hide();
91 DeactivatePage();
94 // -----------------------------------------------------------------------
96 void FwkTabPage::CreateDialog()
98 try
100 uno::Reference< uno::XInterface > xHandler;
101 if ( m_xEventHdl.is() )
102 xHandler = m_xEventHdl;
104 uno::Reference< awt::XWindowPeer > xParent( VCLUnoHelper::GetInterface( this ), uno::UNO_QUERY );
105 m_xPage = uno::Reference < awt::XWindow >(
106 m_xWinProvider->createContainerWindow(
107 m_sPageURL, OUString(), xParent, xHandler ), uno::UNO_QUERY );
109 uno::Reference< awt::XControl > xPageControl( m_xPage, uno::UNO_QUERY );
110 if ( xPageControl.is() )
112 uno::Reference< awt::XWindowPeer > xWinPeer( xPageControl->getPeer() );
113 if ( xWinPeer.is() )
115 Window* pWindow = VCLUnoHelper::GetWindow( xWinPeer );
116 if ( pWindow )
117 pWindow->SetStyle( pWindow->GetStyle() | WB_DIALOGCONTROL | WB_CHILDDLGCTRL );
121 CallMethod( OUString(INITIALIZE_METHOD) );
123 catch ( const lang::IllegalArgumentException& )
125 SAL_WARN( "fwk", "FwkTabPage::CreateDialog(): illegal argument" );
127 catch ( const uno::Exception& )
129 SAL_WARN( "fwk", "FwkTabPage::CreateDialog(): exception of XDialogProvider2::createContainerWindow()" );
133 // -----------------------------------------------------------------------
135 sal_Bool FwkTabPage::CallMethod( const OUString& rMethod )
137 sal_Bool bRet = sal_False;
138 if ( m_xEventHdl.is() )
142 bRet = m_xEventHdl->callHandlerMethod( m_xPage, uno::makeAny( rMethod ), OUString(EXTERNAL_EVENT) );
144 catch ( const uno::Exception& )
146 DBG_UNHANDLED_EXCEPTION();
149 return bRet;
152 // -----------------------------------------------------------------------
154 void FwkTabPage::ActivatePage()
156 TabPage::ActivatePage();
158 if ( !m_xPage.is() )
159 CreateDialog();
161 if ( m_xPage.is() )
163 Resize ();
164 m_xPage->setVisible( sal_True );
168 // -----------------------------------------------------------------------
170 void FwkTabPage::DeactivatePage()
172 TabPage::DeactivatePage();
174 if ( m_xPage.is() )
175 m_xPage->setVisible( sal_False );
178 // -----------------------------------------------------------------------
180 void FwkTabPage::Resize()
182 if ( m_xPage.is () )
184 Size aSize = GetSizePixel();
186 m_xPage->setPosSize( 0, 0, aSize.Width()-1 , aSize.Height()-1, awt::PosSize::POSSIZE );
190 // class FwkTabWindow ---------------------------------------------
192 FwkTabWindow::FwkTabWindow( Window* pParent ) :
194 Window( pParent, FwkResId( WIN_TABWINDOW ) ),
196 m_aTabCtrl ( this, FwkResId( TC_TABCONTROL ) )
198 m_xWinProvider = awt::ContainerWindowProvider::create( ::comphelper::getProcessComponentContext() );
200 SetPaintTransparent(true);
202 m_aTabCtrl.SetActivatePageHdl( LINK( this, FwkTabWindow, ActivatePageHdl ) );
203 m_aTabCtrl.SetDeactivatePageHdl( LINK( this, FwkTabWindow, DeactivatePageHdl ) );
204 m_aTabCtrl.Show();
207 // -----------------------------------------------------------------------
209 FwkTabWindow::~FwkTabWindow()
211 ClearEntryList();
214 // -----------------------------------------------------------------------
216 void FwkTabWindow::ClearEntryList()
218 TabEntryList::const_iterator pIt;
219 for ( pIt = m_TabList.begin();
220 pIt != m_TabList.end();
221 ++pIt )
223 delete *pIt;
226 m_TabList.clear();
229 // -----------------------------------------------------------------------
231 bool FwkTabWindow::RemoveEntry( sal_Int32 nIndex )
233 TabEntryList::iterator pIt;
234 for ( pIt = m_TabList.begin();
235 pIt != m_TabList.end();
236 ++pIt )
238 if ( (*pIt)->m_nIndex == nIndex )
239 break;
242 // remove entry from vector
243 if ( pIt != m_TabList.end())
245 m_TabList.erase(pIt);
246 return true;
248 else
249 return false;
252 // -----------------------------------------------------------------------
253 TabEntry* FwkTabWindow::FindEntry( sal_Int32 nIndex ) const
255 TabEntry* pEntry = NULL;
257 TabEntryList::const_iterator pIt;
258 for ( pIt = m_TabList.begin();
259 pIt != m_TabList.end();
260 ++pIt )
262 if ( (*pIt)->m_nIndex == nIndex )
264 pEntry = *pIt;
265 break;
269 return pEntry;
272 // -----------------------------------------------------------------------
274 IMPL_LINK_NOARG(FwkTabWindow, ActivatePageHdl)
276 const sal_uInt16 nId = m_aTabCtrl.GetCurPageId();
277 FwkTabPage* pTabPage = static_cast< FwkTabPage* >( m_aTabCtrl.GetTabPage( nId ) );
278 if ( !pTabPage )
280 TabEntry* pEntry = FindEntry( nId );
281 if ( pEntry )
283 pTabPage = new FwkTabPage( &m_aTabCtrl, pEntry->m_sPageURL, pEntry->m_xEventHdl, m_xWinProvider );
284 pEntry->m_pPage = pTabPage;
285 m_aTabCtrl.SetTabPage( nId, pTabPage );
286 pTabPage->Show();
287 pTabPage->ActivatePage();
289 } else {
290 pTabPage->ActivatePage();
292 m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_ACTIVATE );
293 return 1;
296 // -----------------------------------------------------------------------
298 IMPL_LINK_NOARG(FwkTabWindow, DeactivatePageHdl)
300 m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_DEACTIVATE );
301 return 1;
304 // -----------------------------------------------------------------------
306 void FwkTabWindow::AddEventListener( const Link& rEventListener )
308 m_aTabCtrl.AddEventListener( rEventListener );
311 void FwkTabWindow::RemoveEventListener( const Link& rEventListener )
313 m_aTabCtrl.RemoveEventListener( rEventListener );
316 // -----------------------------------------------------------------------
318 FwkTabPage* FwkTabWindow::AddTabPage( sal_Int32 nIndex, const uno::Sequence< beans::NamedValue >& rProperties )
320 OUString sTitle, sToolTip, sPageURL;
321 uno::Reference< css::awt::XContainerWindowEventHandler > xEventHdl;
322 uno::Reference< graphic::XGraphic > xImage;
323 bool bDisabled = false;
325 sal_Int32 i = 0, nLen = rProperties.getLength();
326 for ( i = 0; i < nLen; ++i )
328 beans::NamedValue aValue = rProperties[i];
329 OUString sName = aValue.Name;
331 if ( sName == "Title" )
332 aValue.Value >>= sTitle;
333 else if ( sName == "ToolTip" )
334 aValue.Value >>= sToolTip;
335 else if ( sName == "PageURL" )
336 aValue.Value >>= sPageURL;
337 else if ( sName == "EventHdl" )
338 aValue.Value >>= xEventHdl;
339 else if ( sName == "Image" )
340 aValue.Value >>= xImage;
341 else if ( sName == "Disabled" )
342 aValue.Value >>= bDisabled;
345 TabEntry* pEntry = new TabEntry( nIndex, sPageURL, xEventHdl );
346 m_TabList.push_back( pEntry );
347 sal_uInt16 nIdx = static_cast< sal_uInt16 >( nIndex );
348 m_aTabCtrl.InsertPage( nIdx, sTitle );
349 if ( !sToolTip.isEmpty() )
350 m_aTabCtrl.SetHelpText( nIdx, sToolTip );
351 if ( xImage.is() )
352 m_aTabCtrl.SetPageImage( nIdx, Image( xImage ) );
353 if ( bDisabled )
354 m_aTabCtrl.EnablePage( nIdx, false );
356 return pEntry->m_pPage;
359 // -----------------------------------------------------------------------
361 void FwkTabWindow::ActivatePage( sal_Int32 nIndex )
363 m_aTabCtrl.SetCurPageId( static_cast< sal_uInt16 >( nIndex ) );
364 ActivatePageHdl( &m_aTabCtrl );
367 // -----------------------------------------------------------------------
369 void FwkTabWindow::RemovePage( sal_Int32 nIndex )
371 TabEntry* pEntry = FindEntry(nIndex);
372 if ( pEntry )
374 m_aTabCtrl.RemovePage( static_cast< sal_uInt16 >( nIndex ) );
375 if (RemoveEntry(nIndex))
376 delete pEntry;
380 // -----------------------------------------------------------------------
381 void FwkTabWindow::Resize()
383 Size aPageSize = GetSizePixel();
384 m_aTabCtrl.SetTabPageSizePixel( aPageSize );
387 } // namespace framework
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */