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 
24 // autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #include <classes/fwktabwindow.hxx>
28 #include "framework.hrc"
29 #include <classes/fwkresid.hxx>
30 
31 #include <com/sun/star/awt/PosSize.hpp>
32 #include <com/sun/star/awt/XContainerWindowEventHandler.hpp>
33 #include <com/sun/star/awt/XContainerWindowProvider.hpp>
34 #include <com/sun/star/awt/XWindow.hpp>
35 #include <com/sun/star/awt/XWindowPeer.hpp>
36 #include <com/sun/star/awt/XControl.hpp>
37 #include <com/sun/star/beans/NamedValue.hpp>
38 #include <com/sun/star/graphic/XGraphic.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 
41 #include <comphelper/processfactory.hxx>
42 #include <toolkit/helper/vclunohelper.hxx>
43 #include <tools/stream.hxx>
44 #include <tools/diagnose_ex.h>
45 #include <vcl/bitmap.hxx>
46 #include <vcl/image.hxx>
47 #include <vcl/msgbox.hxx>
48 
49 const ::rtl::OUString SERVICENAME_WINPROVIDER
50                                         = ::rtl::OUString::createFromAscii("com.sun.star.awt.ContainerWindowProvider");
51 const ::rtl::OUString EXTERNAL_EVENT    = ::rtl::OUString::createFromAscii("external_event");
52 const ::rtl::OUString BACK_METHOD       = ::rtl::OUString::createFromAscii("back");
53 const ::rtl::OUString INITIALIZE_METHOD = ::rtl::OUString::createFromAscii("initialize");
54 const ::rtl::OUString OK_METHOD         = ::rtl::OUString::createFromAscii("ok");
55 
56 using namespace ::com::sun::star;
57 
58 namespace framework
59 {
60 
61 // class FwkTabControl ---------------------------------------------------
FwkTabControl(Window * pParent,const ResId & rResId)62 FwkTabControl::FwkTabControl( Window* pParent, const ResId& rResId ) :
63 
64     TabControl( pParent, rResId )
65 {
66 }
67 
68 // -----------------------------------------------------------------------
69 
BroadcastEvent(sal_uLong nEvent)70 void FwkTabControl::BroadcastEvent( sal_uLong nEvent )
71 {
72     if ( VCLEVENT_TABPAGE_ACTIVATE == nEvent || VCLEVENT_TABPAGE_DEACTIVATE == nEvent )
73         ImplCallEventListeners( nEvent, (void*)(sal_uIntPtr)GetCurPageId() );
74     else
75     {
76         DBG_ERRORFILE( "FwkTabControl::BroadcastEvent(): illegal event" );
77     }
78 }
79 
80 // class FwkTabPage ------------------------------------------------
81 
FwkTabPage(Window * pParent,const rtl::OUString & rPageURL,const css::uno::Reference<css::awt::XContainerWindowEventHandler> & rEventHdl,const css::uno::Reference<css::awt::XContainerWindowProvider> & rProvider)82 FwkTabPage::FwkTabPage(
83 		       Window* pParent, const rtl::OUString& rPageURL,
84 		       const css::uno::Reference< css::awt::XContainerWindowEventHandler >& rEventHdl,
85 		       const css::uno::Reference< css::awt::XContainerWindowProvider >& rProvider ) :
86 
87     TabPage( pParent, WB_DIALOGCONTROL | WB_TABSTOP | WB_CHILDDLGCTRL ),
88 
89     m_sPageURL          ( rPageURL ),
90     m_xEventHdl         ( rEventHdl ),
91     m_xWinProvider      ( rProvider )
92 
93 {
94 }
95 
96 // -----------------------------------------------------------------------
97 
~FwkTabPage()98 FwkTabPage::~FwkTabPage()
99 {
100     Hide();
101     DeactivatePage();
102 }
103 
104 // -----------------------------------------------------------------------
105 
CreateDialog()106 void FwkTabPage::CreateDialog()
107 {
108     try
109     {
110         uno::Reference< uno::XInterface > xHandler;
111         if ( m_xEventHdl.is() )
112 	  xHandler = m_xEventHdl;
113 
114         uno::Reference< awt::XWindowPeer > xParent( VCLUnoHelper::GetInterface( this ), uno::UNO_QUERY );
115         m_xPage = uno::Reference < awt::XWindow >(
116             m_xWinProvider->createContainerWindow(
117                 m_sPageURL, rtl::OUString(), xParent, xHandler ), uno::UNO_QUERY );
118 
119         uno::Reference< awt::XControl > xPageControl( m_xPage, uno::UNO_QUERY );
120         if ( xPageControl.is() )
121         {
122             uno::Reference< awt::XWindowPeer > xWinPeer( xPageControl->getPeer() );
123             if ( xWinPeer.is() )
124             {
125                 Window* pWindow = VCLUnoHelper::GetWindow( xWinPeer );
126                 if ( pWindow )
127                     pWindow->SetStyle( pWindow->GetStyle() | WB_DIALOGCONTROL | WB_CHILDDLGCTRL );
128             }
129         }
130 
131         CallMethod( INITIALIZE_METHOD );
132     }
133     catch ( lang::IllegalArgumentException& )
134     {
135         DBG_ERRORFILE( "FwkTabPage::CreateDialog(): illegal argument" );
136     }
137     catch ( uno::Exception& )
138     {
139         DBG_ERRORFILE( "FwkTabPage::CreateDialog(): exception of XDialogProvider2::createContainerWindow()" );
140     }
141 }
142 
143 // -----------------------------------------------------------------------
144 
CallMethod(const rtl::OUString & rMethod)145 sal_Bool FwkTabPage::CallMethod( const rtl::OUString& rMethod )
146 {
147     sal_Bool bRet = sal_False;
148     if ( m_xEventHdl.is() )
149     {
150         try
151         {
152             bRet = m_xEventHdl->callHandlerMethod( m_xPage, uno::makeAny( rMethod ), EXTERNAL_EVENT );
153         }
154         catch ( uno::Exception& )
155         {
156             DBG_UNHANDLED_EXCEPTION();
157         }
158     }
159     return bRet;
160 }
161 
162 // -----------------------------------------------------------------------
163 
ActivatePage()164 void FwkTabPage::ActivatePage()
165 {
166     TabPage::ActivatePage();
167 
168     if ( !m_xPage.is() )
169         CreateDialog();
170 
171     if ( m_xPage.is() )
172     {
173         Resize ();
174         m_xPage->setVisible( sal_True );
175     }
176 }
177 
178 // -----------------------------------------------------------------------
179 
DeactivatePage()180 void FwkTabPage::DeactivatePage()
181 {
182     TabPage::DeactivatePage();
183 
184     if ( m_xPage.is() )
185         m_xPage->setVisible( sal_False );
186 }
187 
188 // -----------------------------------------------------------------------
189 
Resize()190 void FwkTabPage::Resize()
191 {
192 	if ( m_xPage.is () )
193 	{
194 		Size  aSize = GetSizePixel ();
195         Point aPos  = GetPosPixel  ();
196 
197         m_xPage->setPosSize( 0, 0, aSize.Width()-1 , aSize.Height()-1, awt::PosSize::POSSIZE );
198 	}
199 }
200 
201 // class FwkTabWindow ---------------------------------------------
202 
FwkTabWindow(Window * pParent)203 FwkTabWindow::FwkTabWindow( Window* pParent ) :
204 
205     Window( pParent, FwkResId( WIN_TABWINDOW ) ),
206 
207     m_aTabCtrl  ( this, FwkResId( TC_TABCONTROL ) )
208 {
209     uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
210     m_xWinProvider = uno::Reference < awt::XContainerWindowProvider >(
211         xFactory->createInstance( SERVICENAME_WINPROVIDER ), uno::UNO_QUERY );
212 
213     SetPaintTransparent(true);
214 
215     m_aTabCtrl.SetActivatePageHdl( LINK( this, FwkTabWindow, ActivatePageHdl ) );
216     m_aTabCtrl.SetDeactivatePageHdl( LINK( this, FwkTabWindow, DeactivatePageHdl ) );
217     m_aTabCtrl.Show();
218 }
219 
220 // -----------------------------------------------------------------------
221 
~FwkTabWindow()222 FwkTabWindow::~FwkTabWindow()
223 {
224     ClearEntryList();
225 }
226 
227 // -----------------------------------------------------------------------
228 
ClearEntryList()229 void FwkTabWindow::ClearEntryList()
230 {
231     TabEntryList::const_iterator pIt;
232     for (  pIt  = m_TabList.begin();
233            pIt != m_TabList.end();
234          ++pIt )
235     {
236         delete *pIt;
237     }
238 
239     m_TabList.clear();
240 }
241 
242 // -----------------------------------------------------------------------
243 
RemoveEntry(sal_Int32 nIndex)244 bool FwkTabWindow::RemoveEntry( sal_Int32 nIndex )
245 {
246     TabEntryList::iterator pIt;
247     for (  pIt  = m_TabList.begin();
248            pIt != m_TabList.end();
249          ++pIt )
250     {
251         if ( (*pIt)->m_nIndex == nIndex )
252             break;
253     }
254 
255     // remove entry from vector
256     if ( pIt != m_TabList.end())
257     {
258         m_TabList.erase(pIt);
259         return true;
260     }
261     else
262         return false;
263 }
264 
265 // -----------------------------------------------------------------------
FindEntry(sal_Int32 nIndex) const266 TabEntry* FwkTabWindow::FindEntry( sal_Int32 nIndex ) const
267 {
268     TabEntry* pEntry = NULL;
269 
270     TabEntryList::const_iterator pIt;
271     for (  pIt  = m_TabList.begin();
272            pIt != m_TabList.end();
273          ++pIt )
274     {
275         if ( (*pIt)->m_nIndex == nIndex )
276         {
277             pEntry = *pIt;
278             break;
279         }
280     }
281 
282     return pEntry;
283 }
284 
285 // -----------------------------------------------------------------------
286 
IMPL_LINK(FwkTabWindow,ActivatePageHdl,TabControl *,EMPTYARG)287 IMPL_LINK( FwkTabWindow, ActivatePageHdl, TabControl *, EMPTYARG )
288 {
289     const sal_uInt16 nId = m_aTabCtrl.GetCurPageId();
290     FwkTabPage* pTabPage = static_cast< FwkTabPage* >( m_aTabCtrl.GetTabPage( nId ) );
291     if ( !pTabPage )
292     {
293         TabEntry* pEntry = FindEntry( nId );
294         if ( pEntry )
295         {
296             pTabPage = new FwkTabPage( &m_aTabCtrl, pEntry->m_sPageURL, pEntry->m_xEventHdl, m_xWinProvider );
297             pEntry->m_pPage = pTabPage;
298             m_aTabCtrl.SetTabPage( nId, pTabPage );
299             pTabPage->Show();
300             pTabPage->ActivatePage();
301         }
302     } else {
303         pTabPage->ActivatePage();
304     }
305     m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_ACTIVATE );
306     return 1;
307 }
308 
309 // -----------------------------------------------------------------------
310 
IMPL_LINK(FwkTabWindow,DeactivatePageHdl,TabControl *,EMPTYARG)311 IMPL_LINK( FwkTabWindow, DeactivatePageHdl, TabControl *, EMPTYARG )
312 {
313     m_aTabCtrl.BroadcastEvent( VCLEVENT_TABPAGE_DEACTIVATE );
314     return 1;
315 }
316 
317 // -----------------------------------------------------------------------
318 
IMPL_LINK(FwkTabWindow,CloseHdl,PushButton *,EMPTYARG)319 IMPL_LINK( FwkTabWindow, CloseHdl, PushButton *, EMPTYARG )
320 {
321 //    Close();
322     return 0;
323 }
324 
325 // -----------------------------------------------------------------------
326 
AddEventListener(const Link & rEventListener)327 void FwkTabWindow::AddEventListener( const Link& rEventListener )
328 {
329     m_aTabCtrl.AddEventListener( rEventListener );
330 }
331 
RemoveEventListener(const Link & rEventListener)332 void FwkTabWindow::RemoveEventListener( const Link& rEventListener )
333 {
334     m_aTabCtrl.RemoveEventListener( rEventListener );
335 }
336 
337 // -----------------------------------------------------------------------
338 
AddTabPage(sal_Int32 nIndex,const uno::Sequence<beans::NamedValue> & rProperties)339 FwkTabPage* FwkTabWindow::AddTabPage( sal_Int32 nIndex, const uno::Sequence< beans::NamedValue >& rProperties )
340 {
341     ::rtl::OUString sTitle, sToolTip, sPageURL;
342     uno::Reference< css::awt::XContainerWindowEventHandler > xEventHdl;
343     uno::Reference< graphic::XGraphic > xImage;
344     bool bDisabled = false;
345 
346     sal_Int32 i = 0, nLen = rProperties.getLength();
347     for ( i = 0; i < nLen; ++i )
348     {
349         beans::NamedValue aValue = rProperties[i];
350         ::rtl::OUString sName = aValue.Name;
351 
352         if ( sName.equalsAscii("Title") )
353             aValue.Value >>= sTitle;
354         else if ( sName.equalsAscii("ToolTip") )
355             aValue.Value >>= sToolTip;
356         else if ( sName.equalsAscii("PageURL") )
357             aValue.Value >>= sPageURL;
358         else if ( sName.equalsAscii("EventHdl") )
359             aValue.Value >>= xEventHdl;
360         else if ( sName.equalsAscii("Image") )
361             aValue.Value >>= xImage;
362         else if ( sName.equalsAscii("Disabled") )
363             aValue.Value >>= bDisabled;
364     }
365 
366     TabEntry* pEntry = new TabEntry( nIndex, sPageURL, xEventHdl );
367     m_TabList.push_back( pEntry );
368     sal_uInt16 nIdx = static_cast< sal_uInt16 >( nIndex );
369     m_aTabCtrl.InsertPage( nIdx, sTitle );
370     if ( sToolTip.getLength() > 0 )
371         m_aTabCtrl.SetHelpText( nIdx, sToolTip );
372     if ( xImage.is() )
373         m_aTabCtrl.SetPageImage( nIdx, Image( xImage ) );
374     if ( bDisabled )
375         m_aTabCtrl.EnablePage( nIdx, false );
376 
377     return pEntry->m_pPage;
378 }
379 
380 // -----------------------------------------------------------------------
381 
ActivatePage(sal_Int32 nIndex)382 void FwkTabWindow::ActivatePage( sal_Int32 nIndex )
383 {
384     m_aTabCtrl.SetCurPageId( static_cast< sal_uInt16 >( nIndex ) );
385     ActivatePageHdl( &m_aTabCtrl );
386 }
387 
388 // -----------------------------------------------------------------------
389 
RemovePage(sal_Int32 nIndex)390 void FwkTabWindow::RemovePage( sal_Int32 nIndex )
391 {
392     TabEntry* pEntry = FindEntry(nIndex);
393     if ( pEntry )
394     {
395         m_aTabCtrl.RemovePage( static_cast< sal_uInt16 >( nIndex ) );
396         if (RemoveEntry(nIndex))
397             delete pEntry;
398     }
399 }
400 
401 // -----------------------------------------------------------------------
Resize()402 void FwkTabWindow::Resize()
403 {
404     Size aPageSize = GetSizePixel();
405     m_aTabCtrl.SetTabPageSizePixel( aPageSize );
406 }
407 
408 } // namespace framework
409 
410