Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <classes/fwktabwindow.hxx>
21 : #include <services.h>
22 : #include <properties.h>
23 :
24 : #include <com/sun/star/awt/PosSize.hpp>
25 : #include <com/sun/star/awt/XSimpleTabController.hpp>
26 : #include <com/sun/star/awt/XWindow.hpp>
27 : #include <com/sun/star/beans/PropertyAttribute.hpp>
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 :
30 : #include <cppuhelper/basemutex.hxx>
31 : #include <cppuhelper/supportsservice.hxx>
32 : #include <cppuhelper/weak.hxx>
33 : #include <toolkit/helper/vclunohelper.hxx>
34 : #include <rtl/ref.hxx>
35 : #include <rtl/ustrbuf.hxx>
36 : #include <vcl/svapp.hxx>
37 : #include <vcl/window.hxx>
38 : #include <classes/propertysethelper.hxx>
39 : #include <macros/generic.hxx>
40 : #include <macros/xinterface.hxx>
41 : #include <macros/xtypeprovider.hxx>
42 : #include <macros/xserviceinfo.hxx>
43 : #include <general.h>
44 : #include <stdtypes.h>
45 :
46 : using namespace framework;
47 :
48 : namespace {
49 :
50 0 : struct TTabPageInfo
51 : {
52 : public:
53 :
54 0 : TTabPageInfo()
55 : : m_nIndex ( -1 )
56 : , m_bCreated (false)
57 : , m_pPage ( NULL )
58 0 : , m_lProperties ( )
59 0 : {}
60 :
61 0 : TTabPageInfo(::sal_Int32 nID)
62 : : m_nIndex ( nID )
63 : , m_bCreated (false)
64 : , m_pPage ( NULL )
65 0 : , m_lProperties ( )
66 0 : {}
67 :
68 : public:
69 :
70 : ::sal_Int32 m_nIndex;
71 : bool m_bCreated;
72 : FwkTabPage* m_pPage;
73 : css::uno::Sequence< css::beans::NamedValue > m_lProperties;
74 : };
75 :
76 : typedef ::boost::unordered_map< ::sal_Int32 ,
77 : TTabPageInfo ,
78 : Int32HashCode ,
79 : ::std::equal_to< ::sal_Int32 > > TTabPageInfoHash;
80 :
81 : /*-************************************************************************************************************
82 : @short implements a helper service providing a dockable tab control window
83 : *//*-*************************************************************************************************************/
84 :
85 : class TabWindowService : public css::lang::XTypeProvider
86 : , public css::lang::XServiceInfo
87 : , public css::awt::XSimpleTabController
88 : , public css::lang::XComponent
89 : , public TransactionBase
90 : , private cppu::BaseMutex
91 : , public PropertySetHelper
92 : , public ::cppu::OWeakObject
93 : {
94 : public:
95 : TabWindowService();
96 : virtual ~TabWindowService();
97 :
98 : /// Initialization function after having acquire()'d.
99 : void initProperties();
100 :
101 : FWK_DECLARE_XINTERFACE
102 : FWK_DECLARE_XTYPEPROVIDER
103 :
104 0 : virtual OUString SAL_CALL getImplementationName()
105 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
106 : {
107 0 : return OUString("com.sun.star.comp.framework.TabWindowService");
108 : }
109 :
110 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
111 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
112 : {
113 0 : return cppu::supportsService(this, ServiceName);
114 : }
115 :
116 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
117 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
118 : {
119 0 : css::uno::Sequence< OUString > aSeq(1);
120 0 : aSeq[0] = OUString("com.sun.star.ui.dialogs.TabContainerWindow");
121 0 : return aSeq;
122 : }
123 :
124 : // XSimpleTabController
125 :
126 : virtual sal_Int32 SAL_CALL insertTab() throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
127 : virtual void SAL_CALL removeTab( sal_Int32 nID ) throw ( css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
128 : virtual void SAL_CALL setTabProps( sal_Int32 nID, const css::uno::Sequence< css::beans::NamedValue >& aProperties ) throw ( css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
129 : virtual css::uno::Sequence< css::beans::NamedValue > SAL_CALL getTabProps( sal_Int32 nID ) throw ( css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
130 : virtual void SAL_CALL activateTab( sal_Int32 nID ) throw ( css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
131 : virtual sal_Int32 SAL_CALL getActiveTabID() throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
132 : virtual void SAL_CALL addTabListener( const css::uno::Reference< css::awt::XTabListener >& Listener ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
133 : virtual void SAL_CALL removeTabListener( const css::uno::Reference< css::awt::XTabListener >& Listener ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
134 :
135 : // XComponent
136 :
137 : virtual void SAL_CALL dispose() throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
138 : virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
139 : virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) throw ( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
140 :
141 : private:
142 :
143 : void impl_initializePropInfo();
144 : virtual void SAL_CALL impl_setPropertyValue(const OUString& sProperty,
145 : sal_Int32 nHandle ,
146 : const css::uno::Any& aValue ) SAL_OVERRIDE;
147 : virtual css::uno::Any SAL_CALL impl_getPropertyValue(const OUString& sProperty,
148 : sal_Int32 nHandle ) SAL_OVERRIDE;
149 :
150 : DECL_DLLPRIVATE_LINK( EventListener, VclSimpleEvent * );
151 :
152 : void impl_checkTabIndex (::sal_Int32 nID) throw (css::lang::IndexOutOfBoundsException);
153 : TTabPageInfoHash::iterator impl_getTabPageInfo(::sal_Int32 nID) throw (css::lang::IndexOutOfBoundsException);
154 : FwkTabWindow* mem_TabWin ();
155 :
156 : private:
157 :
158 : /// the tab window as XWindow ( to hold window* alive !)
159 : css::uno::Reference< css::awt::XWindow > m_xTabWin;
160 :
161 : /// the VCL tab window
162 : FwkTabWindow* m_pTabWin;
163 :
164 : /// container of inserted tab pages
165 : TTabPageInfoHash m_lTabPageInfos;
166 :
167 : /// container of the added TabListener
168 : ::cppu::OMultiTypeInterfaceContainerHelper m_lListener;
169 :
170 : /// counter of the tabpage indexes
171 : ::sal_Int32 m_nPageIndexCounter;
172 :
173 : /// index of the current active page
174 : ::sal_Int32 m_nCurrentPageIndex;
175 : };
176 :
177 0 : DEFINE_XINTERFACE_6 ( TabWindowService ,
178 : OWeakObject ,
179 : DIRECT_INTERFACE(css::lang::XTypeProvider ),
180 : DIRECT_INTERFACE(css::lang::XServiceInfo ),
181 : DIRECT_INTERFACE(css::lang::XComponent),
182 : DIRECT_INTERFACE(css::awt::XSimpleTabController),
183 : DIRECT_INTERFACE(css::beans::XPropertySet ),
184 : DIRECT_INTERFACE(css::beans::XPropertySetInfo )
185 : )
186 :
187 0 : DEFINE_XTYPEPROVIDER_6 ( TabWindowService ,
188 : css::lang::XTypeProvider ,
189 : css::lang::XServiceInfo ,
190 : css::lang::XComponent ,
191 : css::awt::XSimpleTabController ,
192 : css::beans::XPropertySet ,
193 : css::beans::XPropertySetInfo
194 : )
195 :
196 : // constructor
197 :
198 0 : TabWindowService::TabWindowService()
199 : : TransactionBase ( )
200 : , PropertySetHelper ( m_aMutex,
201 : &m_aTransactionManager ,
202 : false ) // sal_False => dont release shared mutex on calling us!
203 : , OWeakObject ( )
204 :
205 : // Init member
206 : , m_xTabWin ( )
207 : , m_pTabWin ( NULL )
208 : , m_lTabPageInfos ( )
209 : , m_lListener ( m_aMutex )
210 : , m_nPageIndexCounter ( 1 )
211 0 : , m_nCurrentPageIndex ( 0 )
212 : {
213 0 : }
214 :
215 0 : void TabWindowService::initProperties()
216 : {
217 0 : impl_initializePropInfo();
218 0 : m_aTransactionManager.setWorkingMode( E_WORK );
219 0 : }
220 :
221 : // destructor
222 :
223 0 : TabWindowService::~TabWindowService()
224 : {
225 0 : SolarMutexGuard g;
226 0 : if (m_pTabWin)
227 0 : m_pTabWin->RemoveEventListener( LINK( this, TabWindowService, EventListener ) );
228 0 : }
229 :
230 : // XSimpleTabController
231 :
232 0 : ::sal_Int32 SAL_CALL TabWindowService::insertTab()
233 : throw ( css::uno::RuntimeException, std::exception )
234 : {
235 0 : SolarMutexGuard g;
236 :
237 0 : ::sal_Int32 nID = m_nPageIndexCounter++;
238 0 : TTabPageInfo aInfo(nID);
239 :
240 0 : m_lTabPageInfos[nID] = aInfo;
241 :
242 0 : return nID;
243 : }
244 :
245 : // XSimpleTabController
246 :
247 0 : void SAL_CALL TabWindowService::removeTab(::sal_Int32 nID)
248 : throw (css::lang::IndexOutOfBoundsException,
249 : css::uno::RuntimeException, std::exception )
250 : {
251 0 : SolarMutexGuard g;
252 :
253 : // throws suitable IndexOutOfBoundsException .-)
254 0 : TTabPageInfoHash::iterator pIt = impl_getTabPageInfo (nID);
255 0 : m_lTabPageInfos.erase(pIt);
256 :
257 0 : FwkTabWindow* pTabWin = mem_TabWin ();
258 0 : if (pTabWin)
259 0 : pTabWin->RemovePage(nID);
260 0 : }
261 :
262 : // XSimpleTabController
263 :
264 0 : void SAL_CALL TabWindowService::setTabProps( ::sal_Int32 nID ,
265 : const css::uno::Sequence< css::beans::NamedValue >& lProperties)
266 : throw (css::lang::IndexOutOfBoundsException,
267 : css::uno::RuntimeException, std::exception )
268 : {
269 0 : SolarMutexGuard g;
270 :
271 : // throws suitable IndexOutOfBoundsException .-)
272 0 : TTabPageInfoHash::iterator pIt = impl_getTabPageInfo (nID);
273 0 : TTabPageInfo& rInfo = pIt->second;
274 0 : rInfo.m_lProperties = lProperties;
275 :
276 0 : if ( ! rInfo.m_bCreated)
277 : {
278 0 : FwkTabWindow* pTabWin = mem_TabWin ();
279 0 : if (pTabWin)
280 : {
281 0 : pTabWin->AddTabPage(rInfo.m_nIndex, rInfo.m_lProperties);
282 0 : rInfo.m_bCreated = true;
283 : }
284 0 : }
285 0 : }
286 :
287 : // XSimpleTabController
288 :
289 0 : css::uno::Sequence< css::beans::NamedValue > SAL_CALL TabWindowService::getTabProps(::sal_Int32 nID)
290 : throw (css::lang::IndexOutOfBoundsException,
291 : css::uno::RuntimeException, std::exception )
292 : {
293 0 : SolarMutexGuard g;
294 :
295 : // throws suitable IndexOutOfBoundsException .-)
296 0 : TTabPageInfoHash::const_iterator pIt = impl_getTabPageInfo (nID);
297 0 : const TTabPageInfo& rInfo = pIt->second;
298 :
299 0 : return rInfo.m_lProperties;
300 : }
301 :
302 : // XSimpleTabController
303 :
304 0 : void SAL_CALL TabWindowService::activateTab(::sal_Int32 nID)
305 : throw (css::lang::IndexOutOfBoundsException,
306 : css::uno::RuntimeException, std::exception )
307 : {
308 0 : SolarMutexGuard g;
309 :
310 : // throws suitable IndexOutOfBoundsException .-)
311 0 : impl_checkTabIndex (nID);
312 0 : m_nCurrentPageIndex = nID;
313 :
314 0 : FwkTabWindow* pTabWin = mem_TabWin ();
315 0 : if (pTabWin)
316 0 : pTabWin->ActivatePage(nID);
317 0 : }
318 :
319 : // XSimpleTabController
320 :
321 0 : ::sal_Int32 SAL_CALL TabWindowService::getActiveTabID()
322 : throw (css::uno::RuntimeException, std::exception)
323 : {
324 0 : SolarMutexGuard g;
325 0 : return m_nCurrentPageIndex;
326 : }
327 :
328 : // XSimpleTabController
329 :
330 0 : void SAL_CALL TabWindowService::addTabListener(const css::uno::Reference< css::awt::XTabListener >& xListener)
331 : throw (css::uno::RuntimeException, std::exception)
332 : {
333 0 : m_lListener.addInterface(cppu::UnoType<css::awt::XTabListener>::get(), xListener);
334 0 : }
335 :
336 : // XSimpleTabController
337 :
338 0 : void SAL_CALL TabWindowService::removeTabListener(const css::uno::Reference< css::awt::XTabListener >& xListener)
339 : throw (css::uno::RuntimeException, std::exception)
340 : {
341 0 : m_lListener.removeInterface(cppu::UnoType<css::awt::XTabListener>::get(), xListener);
342 0 : }
343 :
344 : // XComponent
345 :
346 0 : void SAL_CALL TabWindowService::dispose()
347 : throw (css::uno::RuntimeException, std::exception)
348 : {
349 0 : SolarMutexGuard g;
350 :
351 0 : css::uno::Reference< css::uno::XInterface > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
352 0 : css::lang::EventObject aEvent(xThis);
353 :
354 0 : m_lListener.disposeAndClear (aEvent);
355 :
356 0 : if (m_pTabWin)
357 0 : m_pTabWin->RemoveEventListener( LINK( this, TabWindowService, EventListener ) );
358 :
359 0 : m_pTabWin = NULL;
360 0 : m_xTabWin.clear();
361 0 : }
362 :
363 : // XComponent
364 :
365 0 : void SAL_CALL TabWindowService::addEventListener(const css::uno::Reference< css::lang::XEventListener >& xListener)
366 : throw (css::uno::RuntimeException, std::exception)
367 : {
368 0 : m_lListener.addInterface(cppu::UnoType<css::lang::XEventListener>::get(), xListener);
369 0 : }
370 :
371 : // XComponent
372 :
373 0 : void SAL_CALL TabWindowService::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener)
374 : throw (css::uno::RuntimeException, std::exception)
375 : {
376 0 : m_lListener.removeInterface(cppu::UnoType<css::lang::XEventListener>::get(), xListener);
377 0 : }
378 :
379 0 : void TabWindowService::impl_initializePropInfo()
380 : {
381 0 : impl_setPropertyChangeBroadcaster(static_cast< css::awt::XSimpleTabController* >(this));
382 :
383 : impl_addPropertyInfo(
384 : css::beans::Property(
385 : OUString("Window"),
386 : TABWINDOWSERVICE_PROPHANDLE_WINDOW,
387 0 : cppu::UnoType<css::awt::XWindow>::get(),
388 0 : css::beans::PropertyAttribute::TRANSIENT));
389 0 : }
390 :
391 0 : void SAL_CALL TabWindowService::impl_setPropertyValue(const OUString& /*sProperty*/,
392 : sal_Int32 /*nHandle */,
393 : const css::uno::Any& /*aValue */)
394 :
395 : {
396 0 : }
397 :
398 0 : css::uno::Any SAL_CALL TabWindowService::impl_getPropertyValue(const OUString& /*sProperty*/,
399 : sal_Int32 nHandle )
400 : {
401 : /* There is no need to lock any mutex here. Because we share the
402 : solar mutex with our base class. And we said to our base class: "dont release it on calling us" .-)
403 : see ctor of PropertySetHelper for further information.
404 : */
405 0 : css::uno::Any aValue;
406 :
407 0 : switch (nHandle)
408 : {
409 : case TABWINDOWSERVICE_PROPHANDLE_WINDOW:
410 : {
411 0 : mem_TabWin (); // force "creation on demand" of m_xTabWin :-)
412 0 : aValue <<= m_xTabWin;
413 : }
414 0 : break;
415 : }
416 :
417 0 : return aValue;
418 : }
419 :
420 : // TabWindowService
421 0 : IMPL_LINK( TabWindowService, EventListener, VclSimpleEvent*, pEvent )
422 : {
423 0 : if (!pEvent || !pEvent->ISA(VclWindowEvent))
424 0 : return 0;
425 :
426 0 : sal_uLong nEventId = pEvent->GetId();
427 0 : VclWindowEvent* pWinEvt = static_cast< VclWindowEvent* >(pEvent);
428 :
429 0 : css::uno::Reference< css::uno::XInterface > xThis ( static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY );
430 0 : css::lang::EventObject aEvent( xThis );
431 :
432 0 : if (nEventId == VCLEVENT_OBJECT_DYING)
433 : {
434 0 : m_lListener.disposeAndClear (aEvent);
435 :
436 0 : m_pTabWin->RemoveEventListener( LINK( this, TabWindowService, EventListener ) );
437 0 : m_pTabWin = NULL;
438 0 : m_xTabWin.clear();
439 :
440 0 : return 0;
441 : }
442 :
443 0 : ::cppu::OInterfaceContainerHelper* pContainer = m_lListener.getContainer(cppu::UnoType<css::awt::XTabListener>::get());
444 0 : if ( ! pContainer)
445 0 : return 0;
446 :
447 0 : ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
448 0 : while (pIterator.hasMoreElements())
449 : {
450 : try
451 : {
452 0 : css::awt::XTabListener* pListener = static_cast<css::awt::XTabListener*>(pIterator.next());
453 :
454 0 : switch (nEventId)
455 : {
456 : case VCLEVENT_TABPAGE_ACTIVATE :
457 0 : pListener->activated( (sal_Int32)reinterpret_cast<sal_uLong>(pWinEvt->GetData()) );
458 0 : break;
459 :
460 : case VCLEVENT_TABPAGE_DEACTIVATE :
461 0 : pListener->deactivated( (sal_Int32)reinterpret_cast<sal_uLong>(pWinEvt->GetData()) );
462 0 : break;
463 :
464 : case VCLEVENT_TABPAGE_INSERTED :
465 0 : pListener->inserted( (sal_Int32)reinterpret_cast<sal_uLong>(pWinEvt->GetData()) );
466 0 : break;
467 :
468 : case VCLEVENT_TABPAGE_REMOVED :
469 0 : pListener->removed( (sal_Int32)reinterpret_cast<sal_uLong>(pWinEvt->GetData()) );
470 0 : break;
471 :
472 : case VCLEVENT_TABPAGE_PAGETEXTCHANGED :
473 : case VCLEVENT_TABPAGE_REMOVEDALL :
474 0 : break;
475 : }
476 : }
477 0 : catch(const css::uno::RuntimeException&)
478 : {
479 0 : pIterator.remove();
480 : }
481 : }
482 :
483 0 : return 0;
484 : }
485 :
486 : // TabWindowService
487 :
488 0 : void TabWindowService::impl_checkTabIndex (::sal_Int32 nID)
489 : throw (css::lang::IndexOutOfBoundsException)
490 : {
491 0 : if (
492 0 : (nID <= 0 ) ||
493 0 : (nID > m_nPageIndexCounter)
494 : )
495 : {
496 : throw css::lang::IndexOutOfBoundsException(
497 : "Tab index out of bounds.",
498 0 : static_cast< ::cppu::OWeakObject* >(this) );
499 : }
500 0 : }
501 :
502 : // TabWindowService
503 :
504 0 : TTabPageInfoHash::iterator TabWindowService::impl_getTabPageInfo(::sal_Int32 nID)
505 : throw (css::lang::IndexOutOfBoundsException)
506 : {
507 0 : TTabPageInfoHash::iterator pIt = m_lTabPageInfos.find(nID);
508 0 : if (pIt == m_lTabPageInfos.end ())
509 : throw css::lang::IndexOutOfBoundsException(
510 : "Tab index out of bounds.",
511 0 : static_cast< ::cppu::OWeakObject* >(this) );
512 0 : return pIt;
513 : }
514 :
515 : // TabWindowService
516 0 : FwkTabWindow* TabWindowService::mem_TabWin ()
517 : {
518 0 : FwkTabWindow* pWin = NULL;
519 :
520 0 : if ( ! m_xTabWin.is ())
521 : {
522 0 : vcl::Window* pFakeParent = dynamic_cast< vcl::Window* >(Application::GetDefaultDevice ());
523 :
524 0 : m_pTabWin = new FwkTabWindow (pFakeParent);
525 0 : m_xTabWin = VCLUnoHelper::GetInterface (m_pTabWin);
526 :
527 0 : m_pTabWin->AddEventListener( LINK( this, TabWindowService, EventListener ) );
528 : }
529 :
530 0 : if (m_xTabWin.is ())
531 0 : pWin = m_pTabWin;
532 :
533 0 : return pWin;
534 : }
535 :
536 : }
537 :
538 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
539 0 : com_sun_star_comp_framework_TabWindowService_get_implementation(
540 : css::uno::XComponentContext *,
541 : css::uno::Sequence<css::uno::Any> const &)
542 : {
543 0 : TabWindowService *inst = new TabWindowService;
544 0 : css::uno::XInterface *acquired_inst = cppu::acquire(inst);
545 :
546 0 : inst->initProperties();
547 :
548 0 : return acquired_inst;
549 951 : }
550 :
551 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|