update dev300-m58
[ooovba.git] / framework / source / classes / fwktabwindow.cxx
blob973d3cbdc02e237463bf5d224780d16569df943f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: backingwindow.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 #include <classes/fwktabwindow.hxx>
35 #include "framework.hrc"
36 #include <classes/fwlresid.hxx>
38 #include <com/sun/star/awt/PosSize.hpp>
39 #include <com/sun/star/awt/XContainerWindowEventHandler.hpp>
40 #include <com/sun/star/awt/XContainerWindowProvider.hpp>
41 #include <com/sun/star/awt/XWindow.hpp>
42 #include <com/sun/star/awt/XWindowPeer.hpp>
43 #include <com/sun/star/awt/XControl.hpp>
44 #include <com/sun/star/beans/NamedValue.hpp>
45 #include <com/sun/star/graphic/XGraphic.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #include <comphelper/processfactory.hxx>
49 #include <toolkit/helper/vclunohelper.hxx>
50 #include <tools/stream.hxx>
51 #include <vcl/bitmap.hxx>
52 #include <vcl/image.hxx>
53 #include <vcl/msgbox.hxx>
55 const ::rtl::OUString SERVICENAME_WINPROVIDER
56 = ::rtl::OUString::createFromAscii("com.sun.star.awt.ContainerWindowProvider");
57 const ::rtl::OUString EXTERNAL_EVENT = ::rtl::OUString::createFromAscii("external_event");
58 const ::rtl::OUString BACK_METHOD = ::rtl::OUString::createFromAscii("back");
59 const ::rtl::OUString INITIALIZE_METHOD = ::rtl::OUString::createFromAscii("initialize");
60 const ::rtl::OUString OK_METHOD = ::rtl::OUString::createFromAscii("ok");
62 using namespace ::com::sun::star;
64 namespace framework
67 // class FwkTabControl ---------------------------------------------------
69 FwkTabControl::FwkTabControl( Window* pParent, const ResId& rResId ) :
71 TabControl( pParent, rResId )
75 // -----------------------------------------------------------------------
77 void FwkTabControl::BroadcastEvent( ULONG nEvent )
79 if ( VCLEVENT_TABPAGE_ACTIVATE == nEvent || VCLEVENT_TABPAGE_DEACTIVATE == nEvent )
80 ImplCallEventListeners( nEvent, (void*)(ULONG)GetCurPageId() );
81 else
83 DBG_ERRORFILE( "FwkTabControl::BroadcastEvent(): illegal event" );
87 // class FwkTabPage ------------------------------------------------
89 FwkTabPage::FwkTabPage(
90 Window* pParent, const rtl::OUString& rPageURL,
91 const css::uno::Reference< css::awt::XContainerWindowEventHandler >& rEventHdl,
92 const css::uno::Reference< css::awt::XContainerWindowProvider >& rProvider ) :
94 TabPage( pParent, WB_DIALOGCONTROL | WB_TABSTOP | WB_CHILDDLGCTRL ),
96 m_sPageURL ( rPageURL ),
97 m_xEventHdl ( rEventHdl ),
98 m_xWinProvider ( rProvider )
103 // -----------------------------------------------------------------------
105 FwkTabPage::~FwkTabPage()
107 Hide();
108 DeactivatePage();
111 // -----------------------------------------------------------------------
113 void FwkTabPage::CreateDialog()
117 uno::Reference< uno::XInterface > xHandler;
118 if ( m_xEventHdl.is() )
119 xHandler = m_xEventHdl;
121 uno::Reference< awt::XWindowPeer > xParent( VCLUnoHelper::GetInterface( this ), uno::UNO_QUERY );
122 m_xPage = uno::Reference < awt::XWindow >(
123 m_xWinProvider->createContainerWindow(
124 m_sPageURL, rtl::OUString(), xParent, xHandler ), uno::UNO_QUERY );
126 uno::Reference< awt::XControl > xPageControl( m_xPage, uno::UNO_QUERY );
127 if ( xPageControl.is() )
129 uno::Reference< awt::XWindowPeer > xWinPeer( xPageControl->getPeer() );
130 if ( xWinPeer.is() )
132 Window* pWindow = VCLUnoHelper::GetWindow( xWinPeer );
133 if ( pWindow )
134 pWindow->SetStyle( pWindow->GetStyle() | WB_DIALOGCONTROL | WB_CHILDDLGCTRL );
138 CallMethod( INITIALIZE_METHOD );
140 catch ( lang::IllegalArgumentException& )
142 DBG_ERRORFILE( "FwkTabPage::CreateDialog(): illegal argument" );
144 catch ( uno::Exception& )
146 DBG_ERRORFILE( "FwkTabPage::CreateDialog(): exception of XDialogProvider2::createContainerWindow()" );
150 // -----------------------------------------------------------------------
152 sal_Bool FwkTabPage::CallMethod( const rtl::OUString& rMethod )
154 sal_Bool bRet = sal_False;
155 if ( m_xEventHdl.is() )
159 bRet = m_xEventHdl->callHandlerMethod( m_xPage, uno::makeAny( rMethod ), EXTERNAL_EVENT );
161 catch ( uno::Exception& )
163 DBG_ERRORFILE( "FwkTabPage::CallMethod(): exception of XDialogEventHandler::callHandlerMethod()" );
166 return bRet;
169 // -----------------------------------------------------------------------
171 void FwkTabPage::ActivatePage()
173 TabPage::ActivatePage();
175 if ( !m_xPage.is() )
176 CreateDialog();
178 if ( m_xPage.is() )
180 Resize ();
181 m_xPage->setVisible( sal_True );
185 // -----------------------------------------------------------------------
187 void FwkTabPage::DeactivatePage()
189 TabPage::DeactivatePage();
191 if ( m_xPage.is() )
192 m_xPage->setVisible( sal_False );
195 // -----------------------------------------------------------------------
197 void FwkTabPage::Resize()
199 if ( m_xPage.is () )
201 Size aSize = GetSizePixel ();
202 Point aPos = GetPosPixel ();
204 m_xPage->setPosSize( 0, 0, aSize.Width()-1 , aSize.Height()-1, awt::PosSize::POSSIZE );
208 // -----------------------------------------------------------------------
210 void FwkTabPage::Reset()
212 CallMethod( BACK_METHOD );
213 ActivatePage();
216 // -----------------------------------------------------------------------
218 void FwkTabPage::Save()
220 CallMethod( OK_METHOD );
223 // class FwkTabWindow ---------------------------------------------
225 FwkTabWindow::FwkTabWindow( Window* pParent ) :
227 Window( pParent, FwlResId( WIN_TABWINDOW ) ),
229 m_aTabCtrl ( this, FwlResId( TC_TABCONTROL ) )
231 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
232 m_xWinProvider = uno::Reference < awt::XContainerWindowProvider >(
233 xFactory->createInstance( SERVICENAME_WINPROVIDER ), uno::UNO_QUERY );
235 SetPaintTransparent(true);
237 m_aTabCtrl.SetActivatePageHdl( LINK( this, FwkTabWindow, ActivatePageHdl ) );
238 m_aTabCtrl.SetDeactivatePageHdl( LINK( this, FwkTabWindow, DeactivatePageHdl ) );
239 m_aTabCtrl.Show();
242 // -----------------------------------------------------------------------
244 FwkTabWindow::~FwkTabWindow()
246 ClearEntryList();
249 // -----------------------------------------------------------------------
251 void FwkTabWindow::ClearEntryList()
253 TabEntryList::const_iterator pIt;
254 for ( pIt = m_TabList.begin();
255 pIt != m_TabList.end();
256 ++pIt )
258 delete *pIt;
261 m_TabList.clear();
264 // -----------------------------------------------------------------------
266 bool FwkTabWindow::RemoveEntry( sal_Int32 nIndex )
268 TabEntryList::iterator pIt;
269 for ( pIt = m_TabList.begin();
270 pIt != m_TabList.end();
271 ++pIt )
273 if ( (*pIt)->m_nIndex == nIndex )
274 break;
277 // remove entry from vector
278 if ( pIt != m_TabList.end())
280 m_TabList.erase(pIt);
281 return true;
283 else
284 return false;
287 // -----------------------------------------------------------------------
288 TabEntry* FwkTabWindow::FindEntry( sal_Int32 nIndex ) const
290 TabEntry* pEntry = NULL;
292 TabEntryList::const_iterator pIt;
293 for ( pIt = m_TabList.begin();
294 pIt != m_TabList.end();
295 ++pIt )
297 if ( (*pIt)->m_nIndex == nIndex )
299 pEntry = *pIt;
300 break;
304 return pEntry;
307 // -----------------------------------------------------------------------
309 IMPL_LINK( FwkTabWindow, ActivatePageHdl, TabControl *, EMPTYARG )
311 const USHORT nId = m_aTabCtrl.GetCurPageId();
312 FwkTabPage* pTabPage = static_cast< FwkTabPage* >( m_aTabCtrl.GetTabPage( nId ) );
313 if ( !pTabPage )
315 TabEntry* pEntry = FindEntry( nId );
316 if ( pEntry )
318 pTabPage = new FwkTabPage( &m_aTabCtrl, pEntry->m_sPageURL, pEntry->m_xEventHdl, m_xWinProvider );
319 pEntry->m_pPage = pTabPage;
320 m_aTabCtrl.SetTabPage( nId, pTabPage );
321 pTabPage->Show();
322 pTabPage->ActivatePage();
324 } else {
325 pTabPage->ActivatePage();
327 m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_ACTIVATE );
328 return 1;
331 // -----------------------------------------------------------------------
333 IMPL_LINK( FwkTabWindow, DeactivatePageHdl, TabControl *, EMPTYARG )
335 m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_DEACTIVATE );
336 return 1;
339 // -----------------------------------------------------------------------
341 IMPL_LINK( FwkTabWindow, CloseHdl, PushButton *, EMPTYARG )
343 // Close();
344 return 0;
347 // -----------------------------------------------------------------------
349 void FwkTabWindow::AddEventListener( const Link& rEventListener )
351 m_aTabCtrl.AddEventListener( rEventListener );
354 // -----------------------------------------------------------------------
356 FwkTabPage* FwkTabWindow::AddTabPage( sal_Int32 nIndex, const uno::Sequence< beans::NamedValue >& rProperties )
358 ::rtl::OUString sTitle, sToolTip, sPageURL;
359 uno::Reference< css::awt::XContainerWindowEventHandler > xEventHdl;
360 uno::Reference< graphic::XGraphic > xImage;
361 bool bDisabled = false;
363 sal_Int32 i = 0, nLen = rProperties.getLength();
364 for ( i = 0; i < nLen; ++i )
366 beans::NamedValue aValue = rProperties[i];
367 ::rtl::OUString sName = aValue.Name;
369 if ( sName.equalsAscii("Title") )
370 aValue.Value >>= sTitle;
371 else if ( sName.equalsAscii("ToolTip") )
372 aValue.Value >>= sToolTip;
373 else if ( sName.equalsAscii("PageURL") )
374 aValue.Value >>= sPageURL;
375 else if ( sName.equalsAscii("EventHdl") )
376 aValue.Value >>= xEventHdl;
377 else if ( sName.equalsAscii("Image") )
378 aValue.Value >>= xImage;
379 else if ( sName.equalsAscii("Disabled") )
380 aValue.Value >>= bDisabled;
383 TabEntry* pEntry = new TabEntry( nIndex, sPageURL, xEventHdl );
384 m_TabList.push_back( pEntry );
385 USHORT nIdx = static_cast< USHORT >( nIndex );
386 m_aTabCtrl.InsertPage( nIdx, sTitle );
387 if ( sToolTip.getLength() > 0 )
388 m_aTabCtrl.SetHelpText( nIdx, sToolTip );
389 if ( xImage.is() )
390 m_aTabCtrl.SetPageImage( nIdx, Image( xImage ) );
391 if ( bDisabled )
392 m_aTabCtrl.EnablePage( nIdx, false );
394 return pEntry->m_pPage;
397 // -----------------------------------------------------------------------
399 void FwkTabWindow::ActivatePage( sal_Int32 nIndex )
401 m_aTabCtrl.SetCurPageId( static_cast< USHORT >( nIndex ) );
402 ActivatePageHdl( &m_aTabCtrl );
405 // -----------------------------------------------------------------------
407 void FwkTabWindow::RemovePage( sal_Int32 nIndex )
409 TabEntry* pEntry = FindEntry(nIndex);
410 if ( pEntry )
412 m_aTabCtrl.RemovePage( static_cast< USHORT >( nIndex ) );
413 if (RemoveEntry(nIndex))
414 delete pEntry;
418 // -----------------------------------------------------------------------
420 sal_Int32 FwkTabWindow::GetActivePageId() const
422 return m_aTabCtrl.GetCurPageId();
425 // -----------------------------------------------------------------------
426 void FwkTabWindow::Resize()
428 Size aPageSize = GetSizePixel();
429 m_aTabCtrl.SetTabPageSizePixel( aPageSize );
432 } // namespace framework