1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "precompiled_toolkit.hxx"
24 
25 #include <toolkit/awt/vclxtabpagecontainer.hxx>
26 #include <com/sun/star/awt/tab/XTabPageModel.hpp>
27 #include <com/sun/star/awt/XControl.hpp>
28 #include <vcl/tabpage.hxx>
29 #include <vcl/tabctrl.hxx>
30 #include <toolkit/helper/property.hxx>
31 #include <toolkit/helper/vclunohelper.hxx>
32 #include <toolkit/helper/tkresmgr.hxx>
33 #include <cppuhelper/typeprovider.hxx>
34 
35 using ::rtl::OUString;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::view;
42 //	----------------------------------------------------
43 //	class VCLXTabPageContainer
44 //	----------------------------------------------------
45 void VCLXTabPageContainer::ImplGetPropertyIds( std::list< sal_uInt16 > &rIds )
46 {
47     VCLXWindow::ImplGetPropertyIds( rIds );
48 }
49 
50 VCLXTabPageContainer::VCLXTabPageContainer() :
51 	m_aTabPageListeners( *this )
52 {
53 }
54 
55 VCLXTabPageContainer::~VCLXTabPageContainer()
56 {
57 #ifndef __SUNPRO_CC
58     OSL_TRACE ("%s", __FUNCTION__);
59 #endif
60 }
61 
62 void SAL_CALL VCLXTabPageContainer::draw( sal_Int32 nX, sal_Int32 nY ) throw(RuntimeException)
63 {
64 	::vos::OGuard aGuard( GetMutex() );
65 	TabControl* pTabControl = (TabControl*)GetWindow();
66     if ( pTabControl )
67     {
68         TabPage *pTabPage = pTabControl->GetTabPage( sal::static_int_cast< sal_uInt16 >(  pTabControl->GetCurPageId( ) ) );
69         if ( pTabPage )
70         {
71             ::Point aPos( nX, nY );
72             ::Size  aSize = pTabPage->GetSizePixel();
73 
74 		    OutputDevice* pDev = VCLUnoHelper::GetOutputDevice( getGraphics() );
75             aPos  = pDev->PixelToLogic( aPos );
76             aSize = pDev->PixelToLogic( aSize );
77 
78             pTabPage->Draw( pDev, aPos, aSize, 0 );
79         }
80     }
81 
82     VCLXWindow::draw( nX, nY );
83 /*
84 	if ( pWindow )
85 	{
86 		OutputDevice* pDev = VCLUnoHelper::GetOutputDevice( getGraphics() );
87 		if ( !pDev )
88 			pDev = pWindow->GetParent();
89 
90 		Size aSize = pDev->PixelToLogic( pWindow->GetSizePixel() );
91 		Point aPos = pDev->PixelToLogic( Point( nX, nY ) );
92 
93 		pWindow->Draw( pDev, aPos, aSize, WINDOW_DRAW_NOCONTROLS );
94     }
95 */
96 }
97 
98 ::com::sun::star::awt::DeviceInfo VCLXTabPageContainer::getInfo() throw(RuntimeException)
99 {
100 	::com::sun::star::awt::DeviceInfo aInfo = VCLXDevice::getInfo();
101 	return aInfo;
102 }
103 
104 void SAL_CALL VCLXTabPageContainer::setProperty(const ::rtl::OUString& PropertyName,   const Any& Value ) throw(RuntimeException)
105 {
106 	::vos::OGuard aGuard( GetMutex() );
107 
108 	TabControl* pTabPage = (TabControl*)GetWindow();
109 	if ( pTabPage )
110 	{
111 		VCLXWindow::setProperty( PropertyName, Value );
112     }
113 }
114 ::sal_Int16 SAL_CALL VCLXTabPageContainer::getActiveTabPageID() throw (RuntimeException)
115 {
116     TabControl* pTabCtrl = (TabControl*)GetWindow();
117     return pTabCtrl != NULL ? pTabCtrl->GetCurPageId( ) : 0;
118 }
119 void SAL_CALL VCLXTabPageContainer::setActiveTabPageID( ::sal_Int16 _activetabpageid ) throw (RuntimeException)
120 {
121     TabControl* pTabCtrl = (TabControl*)GetWindow();
122 	if ( pTabCtrl )
123         pTabCtrl->SelectTabPage(_activetabpageid);
124 }
125 ::sal_Int16 SAL_CALL VCLXTabPageContainer::getTabPageCount(  ) throw (RuntimeException)
126 {
127 	TabControl* pTabCtrl = (TabControl*)GetWindow();
128     return pTabCtrl != NULL ? pTabCtrl->GetPageCount() : 0;
129 }
130 ::sal_Bool SAL_CALL VCLXTabPageContainer::isTabPageActive( ::sal_Int16 tabPageIndex ) throw (RuntimeException)
131 {
132 	return (getActiveTabPageID() == tabPageIndex);
133 }
134 Reference< ::com::sun::star::awt::tab::XTabPage > SAL_CALL VCLXTabPageContainer::getTabPage( ::sal_Int16 tabPageIndex ) throw (RuntimeException)
135 {
136     return (tabPageIndex >= 0 && tabPageIndex < static_cast<sal_Int16>(m_aTabPages.size())) ? m_aTabPages[tabPageIndex] : NULL;
137 }
138 Reference< ::com::sun::star::awt::tab::XTabPage > SAL_CALL VCLXTabPageContainer::getTabPageByID( ::sal_Int16 tabPageID ) throw (RuntimeException)
139 {
140     ::vos::OClearableGuard aGuard( GetMutex() );
141     Reference< ::com::sun::star::awt::tab::XTabPage > xTabPage;
142     ::std::vector< Reference< ::com::sun::star::awt::tab::XTabPage > >::iterator aIter = m_aTabPages.begin();
143     ::std::vector< Reference< ::com::sun::star::awt::tab::XTabPage > >::iterator aEnd = m_aTabPages.end();
144 	for(;aIter != aEnd;++aIter)
145     {
146         Reference< awt::XControl > xControl(*aIter,UNO_QUERY );
147         Reference< awt::tab::XTabPageModel > xP( xControl->getModel(), UNO_QUERY );
148         if ( tabPageID == xP->getTabPageID() )
149         {
150             xTabPage = *aIter;
151             break;
152         }
153     }
154     return xTabPage;
155 }
156 void SAL_CALL VCLXTabPageContainer::addTabPageContainerListener( const Reference< ::com::sun::star::awt::tab::XTabPageContainerListener >& listener ) throw (RuntimeException)
157 {
158 	m_aTabPageListeners.addInterface( listener );
159 }
160 void SAL_CALL VCLXTabPageContainer::removeTabPageContainerListener( const Reference< ::com::sun::star::awt::tab::XTabPageContainerListener >& listener ) throw (RuntimeException)
161 {
162 	m_aTabPageListeners.removeInterface( listener );
163 }
164 
165 void VCLXTabPageContainer::ProcessWindowEvent( const VclWindowEvent& _rVclWindowEvent )
166 {
167     ::vos::OClearableGuard aGuard( GetMutex() );
168     TabControl* pTabControl = static_cast< TabControl* >( GetWindow() );
169     if ( pTabControl )
170     {
171         switch ( _rVclWindowEvent.GetId() )
172         {
173             case VCLEVENT_TABPAGE_ACTIVATE:
174             {
175 //                allocateArea( maAllocation );
176                 sal_uLong page = (sal_uLong)_rVclWindowEvent.GetData();
177                 awt::tab::TabPageActivatedEvent aEvent(NULL,page);
178                 m_aTabPageListeners.tabPageActivated(aEvent);
179                 break;
180             }
181             default:
182                 aGuard.clear();
183                 VCLXWindow::ProcessWindowEvent( _rVclWindowEvent );
184                 break;
185         }
186     }
187 }
188 void SAL_CALL VCLXTabPageContainer::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (::com::sun::star::uno::RuntimeException)
189 {
190 }
191 void SAL_CALL VCLXTabPageContainer::elementInserted( const ::com::sun::star::container::ContainerEvent& Event ) throw (::com::sun::star::uno::RuntimeException)
192 {
193     ::vos::OGuard aGuard( GetMutex() );
194 	TabControl* pTabCtrl = (TabControl*)GetWindow();
195     Reference< ::com::sun::star::awt::tab::XTabPage > xTabPage(Event.Element,uno::UNO_QUERY);
196 	if ( pTabCtrl && xTabPage.is() )
197 	{
198         Reference< awt::XControl > xControl(xTabPage,UNO_QUERY );
199         Reference< awt::tab::XTabPageModel > xP( xControl->getModel(), UNO_QUERY );
200         sal_Int16 nPageID = xP->getTabPageID();
201 
202         Window* pWindow = VCLUnoHelper::GetWindow(xControl->getPeer());
203         TabPage* pPage = (TabPage*)pWindow;
204         pTabCtrl->InsertPage(nPageID,pPage->GetText());
205 
206         pPage->Hide();
207         pTabCtrl->SetTabPage(nPageID,pPage);
208         pTabCtrl->SetHelpText(nPageID,xP->getToolTip());
209         pTabCtrl->SetPageImage(nPageID,TkResMgr::getImageFromURL(xP->getImageURL()));
210         pTabCtrl->SelectTabPage(nPageID);
211         m_aTabPages.push_back(xTabPage);
212     }
213 }
214 void SAL_CALL VCLXTabPageContainer::elementRemoved( const ::com::sun::star::container::ContainerEvent& Event ) throw (::com::sun::star::uno::RuntimeException)
215 {
216     ::vos::OGuard aGuard( GetMutex() );
217 	TabControl* pTabCtrl = (TabControl*)GetWindow();
218     Reference< ::com::sun::star::awt::tab::XTabPage > xTabPage(Event.Element,uno::UNO_QUERY);
219 	if ( pTabCtrl && xTabPage.is() )
220 	{
221         Reference< awt::XControl > xControl(xTabPage,UNO_QUERY );
222         Reference< awt::tab::XTabPageModel > xP( xControl->getModel(), UNO_QUERY );
223         pTabCtrl->RemovePage(xP->getTabPageID());
224         m_aTabPages.erase(::std::remove(m_aTabPages.begin(),m_aTabPages.end(),xTabPage));
225     }
226 }
227 void SAL_CALL VCLXTabPageContainer::elementReplaced( const ::com::sun::star::container::ContainerEvent& /*Event*/ ) throw (::com::sun::star::uno::RuntimeException)
228 {
229 }
230