merge the formfield patch from ooo-build
[ooovba.git] / framework / source / classes / fwktabwindow.cxx
blob8ffa00a4929aeaaefe27a6d387cf8c681db87086
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 // class FwkTabWindow ---------------------------------------------
210 FwkTabWindow::FwkTabWindow( Window* pParent ) :
212 Window( pParent, FwlResId( WIN_TABWINDOW ) ),
214 m_aTabCtrl ( this, FwlResId( TC_TABCONTROL ) )
216 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
217 m_xWinProvider = uno::Reference < awt::XContainerWindowProvider >(
218 xFactory->createInstance( SERVICENAME_WINPROVIDER ), uno::UNO_QUERY );
220 SetPaintTransparent(true);
222 m_aTabCtrl.SetActivatePageHdl( LINK( this, FwkTabWindow, ActivatePageHdl ) );
223 m_aTabCtrl.SetDeactivatePageHdl( LINK( this, FwkTabWindow, DeactivatePageHdl ) );
224 m_aTabCtrl.Show();
227 // -----------------------------------------------------------------------
229 FwkTabWindow::~FwkTabWindow()
231 ClearEntryList();
234 // -----------------------------------------------------------------------
236 void FwkTabWindow::ClearEntryList()
238 TabEntryList::const_iterator pIt;
239 for ( pIt = m_TabList.begin();
240 pIt != m_TabList.end();
241 ++pIt )
243 delete *pIt;
246 m_TabList.clear();
249 // -----------------------------------------------------------------------
251 bool FwkTabWindow::RemoveEntry( sal_Int32 nIndex )
253 TabEntryList::iterator pIt;
254 for ( pIt = m_TabList.begin();
255 pIt != m_TabList.end();
256 ++pIt )
258 if ( (*pIt)->m_nIndex == nIndex )
259 break;
262 // remove entry from vector
263 if ( pIt != m_TabList.end())
265 m_TabList.erase(pIt);
266 return true;
268 else
269 return false;
272 // -----------------------------------------------------------------------
273 TabEntry* FwkTabWindow::FindEntry( sal_Int32 nIndex ) const
275 TabEntry* pEntry = NULL;
277 TabEntryList::const_iterator pIt;
278 for ( pIt = m_TabList.begin();
279 pIt != m_TabList.end();
280 ++pIt )
282 if ( (*pIt)->m_nIndex == nIndex )
284 pEntry = *pIt;
285 break;
289 return pEntry;
292 // -----------------------------------------------------------------------
294 IMPL_LINK( FwkTabWindow, ActivatePageHdl, TabControl *, EMPTYARG )
296 const USHORT nId = m_aTabCtrl.GetCurPageId();
297 FwkTabPage* pTabPage = static_cast< FwkTabPage* >( m_aTabCtrl.GetTabPage( nId ) );
298 if ( !pTabPage )
300 TabEntry* pEntry = FindEntry( nId );
301 if ( pEntry )
303 pTabPage = new FwkTabPage( &m_aTabCtrl, pEntry->m_sPageURL, pEntry->m_xEventHdl, m_xWinProvider );
304 pEntry->m_pPage = pTabPage;
305 m_aTabCtrl.SetTabPage( nId, pTabPage );
306 pTabPage->Show();
307 pTabPage->ActivatePage();
309 } else {
310 pTabPage->ActivatePage();
312 m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_ACTIVATE );
313 return 1;
316 // -----------------------------------------------------------------------
318 IMPL_LINK( FwkTabWindow, DeactivatePageHdl, TabControl *, EMPTYARG )
320 m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_DEACTIVATE );
321 return 1;
324 // -----------------------------------------------------------------------
326 IMPL_LINK( FwkTabWindow, CloseHdl, PushButton *, EMPTYARG )
328 // Close();
329 return 0;
332 // -----------------------------------------------------------------------
334 void FwkTabWindow::AddEventListener( const Link& rEventListener )
336 m_aTabCtrl.AddEventListener( rEventListener );
339 // -----------------------------------------------------------------------
341 FwkTabPage* FwkTabWindow::AddTabPage( sal_Int32 nIndex, const uno::Sequence< beans::NamedValue >& rProperties )
343 ::rtl::OUString sTitle, sToolTip, sPageURL;
344 uno::Reference< css::awt::XContainerWindowEventHandler > xEventHdl;
345 uno::Reference< graphic::XGraphic > xImage;
346 bool bDisabled = false;
348 sal_Int32 i = 0, nLen = rProperties.getLength();
349 for ( i = 0; i < nLen; ++i )
351 beans::NamedValue aValue = rProperties[i];
352 ::rtl::OUString sName = aValue.Name;
354 if ( sName.equalsAscii("Title") )
355 aValue.Value >>= sTitle;
356 else if ( sName.equalsAscii("ToolTip") )
357 aValue.Value >>= sToolTip;
358 else if ( sName.equalsAscii("PageURL") )
359 aValue.Value >>= sPageURL;
360 else if ( sName.equalsAscii("EventHdl") )
361 aValue.Value >>= xEventHdl;
362 else if ( sName.equalsAscii("Image") )
363 aValue.Value >>= xImage;
364 else if ( sName.equalsAscii("Disabled") )
365 aValue.Value >>= bDisabled;
368 TabEntry* pEntry = new TabEntry( nIndex, sPageURL, xEventHdl );
369 m_TabList.push_back( pEntry );
370 USHORT nIdx = static_cast< USHORT >( nIndex );
371 m_aTabCtrl.InsertPage( nIdx, sTitle );
372 if ( sToolTip.getLength() > 0 )
373 m_aTabCtrl.SetHelpText( nIdx, sToolTip );
374 if ( xImage.is() )
375 m_aTabCtrl.SetPageImage( nIdx, Image( xImage ) );
376 if ( bDisabled )
377 m_aTabCtrl.EnablePage( nIdx, false );
379 return pEntry->m_pPage;
382 // -----------------------------------------------------------------------
384 void FwkTabWindow::ActivatePage( sal_Int32 nIndex )
386 m_aTabCtrl.SetCurPageId( static_cast< USHORT >( nIndex ) );
387 ActivatePageHdl( &m_aTabCtrl );
390 // -----------------------------------------------------------------------
392 void FwkTabWindow::RemovePage( sal_Int32 nIndex )
394 TabEntry* pEntry = FindEntry(nIndex);
395 if ( pEntry )
397 m_aTabCtrl.RemovePage( static_cast< USHORT >( nIndex ) );
398 if (RemoveEntry(nIndex))
399 delete pEntry;
403 // -----------------------------------------------------------------------
404 void FwkTabWindow::Resize()
406 Size aPageSize = GetSizePixel();
407 m_aTabCtrl.SetTabPageSizePixel( aPageSize );
410 } // namespace framework