xref: /AOO41X/main/odk/examples/cpp/custompanel/ctp_panel.cxx (revision abfe7a5a2afd40460bdbc5c1a51909ee2d9b5a04)
1760d135fSAriel Constenla-Haile /**************************************************************
2760d135fSAriel Constenla-Haile  *
3760d135fSAriel Constenla-Haile  * Licensed to the Apache Software Foundation (ASF) under one
4760d135fSAriel Constenla-Haile  * or more contributor license agreements.  See the NOTICE file
5760d135fSAriel Constenla-Haile  * distributed with this work for additional information
6760d135fSAriel Constenla-Haile  * regarding copyright ownership.  The ASF licenses this file
7760d135fSAriel Constenla-Haile  * to you under the Apache License, Version 2.0 (the
8760d135fSAriel Constenla-Haile  * "License"); you may not use this file except in compliance
9760d135fSAriel Constenla-Haile  * with the License.  You may obtain a copy of the License at
10760d135fSAriel Constenla-Haile  *
11760d135fSAriel Constenla-Haile  *   http://www.apache.org/licenses/LICENSE-2.0
12760d135fSAriel Constenla-Haile  *
13760d135fSAriel Constenla-Haile  * Unless required by applicable law or agreed to in writing,
14760d135fSAriel Constenla-Haile  * software distributed under the License is distributed on an
15760d135fSAriel Constenla-Haile  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16760d135fSAriel Constenla-Haile  * KIND, either express or implied.  See the License for the
17760d135fSAriel Constenla-Haile  * specific language governing permissions and limitations
18760d135fSAriel Constenla-Haile  * under the License.
19760d135fSAriel Constenla-Haile  *
20760d135fSAriel Constenla-Haile  *************************************************************/
21760d135fSAriel Constenla-Haile 
22760d135fSAriel Constenla-Haile 
23760d135fSAriel Constenla-Haile 
24760d135fSAriel Constenla-Haile 
25760d135fSAriel Constenla-Haile #include "ctp_panel.hxx"
26760d135fSAriel Constenla-Haile 
27760d135fSAriel Constenla-Haile /** === begin UNO includes === **/
28760d135fSAriel Constenla-Haile #include <com/sun/star/lang/DisposedException.hpp>
29760d135fSAriel Constenla-Haile #include <com/sun/star/awt/XWindowPeer.hpp>
30760d135fSAriel Constenla-Haile #include <com/sun/star/awt/XToolkit.hpp>
31760d135fSAriel Constenla-Haile #include <com/sun/star/awt/WindowClass.hpp>
32760d135fSAriel Constenla-Haile #include <com/sun/star/awt/WindowAttribute.hpp>
33760d135fSAriel Constenla-Haile #include <com/sun/star/awt/PosSize.hpp>
34760d135fSAriel Constenla-Haile #include <com/sun/star/awt/XDevice.hpp>
35760d135fSAriel Constenla-Haile #include <com/sun/star/awt/XGraphics.hpp>
36760d135fSAriel Constenla-Haile #include <com/sun/star/ui/UIElementType.hpp>
37760d135fSAriel Constenla-Haile /** === end UNO includes === **/
38760d135fSAriel Constenla-Haile 
39*abfe7a5aSAriel Constenla-Haile #include <osl/diagnose.h>
40760d135fSAriel Constenla-Haile 
41760d135fSAriel Constenla-Haile //......................................................................................................................
42760d135fSAriel Constenla-Haile namespace sd { namespace colortoolpanel
43760d135fSAriel Constenla-Haile {
44760d135fSAriel Constenla-Haile //......................................................................................................................
45760d135fSAriel Constenla-Haile 
46760d135fSAriel Constenla-Haile 	/** === begin UNO using === **/
47760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::Reference;
48760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::XInterface;
49760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::UNO_QUERY;
50760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::UNO_QUERY_THROW;
51760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::UNO_SET_THROW;
52760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::Exception;
53760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::RuntimeException;
54760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::Any;
55760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::makeAny;
56760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::Sequence;
57760d135fSAriel Constenla-Haile 	using ::com::sun::star::uno::Type;
58760d135fSAriel Constenla-Haile     using ::com::sun::star::uno::XComponentContext;
59760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::XWindow;
60760d135fSAriel Constenla-Haile     using ::com::sun::star::lang::DisposedException;
61760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::XWindowPeer;
62760d135fSAriel Constenla-Haile     using ::com::sun::star::lang::XMultiComponentFactory;
63760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::XToolkit;
64760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::WindowDescriptor;
65760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::WindowClass_SIMPLE;
66760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::Rectangle;
67760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::PaintEvent;
68760d135fSAriel Constenla-Haile     using ::com::sun::star::lang::EventObject;
69760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::XDevice;
70760d135fSAriel Constenla-Haile     using ::com::sun::star::awt::XGraphics;
71760d135fSAriel Constenla-Haile     using ::com::sun::star::accessibility::XAccessible;
72760d135fSAriel Constenla-Haile     using ::com::sun::star::frame::XFrame;
73760d135fSAriel Constenla-Haile 	/** === end UNO using === **/
74760d135fSAriel Constenla-Haile     namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
75760d135fSAriel Constenla-Haile     namespace PosSize = ::com::sun::star::awt::PosSize;
76760d135fSAriel Constenla-Haile     namespace UIElementType = ::com::sun::star::ui::UIElementType;
77760d135fSAriel Constenla-Haile 
78760d135fSAriel Constenla-Haile 	//==================================================================================================================
79760d135fSAriel Constenla-Haile 	//= helpers
80760d135fSAriel Constenla-Haile 	//==================================================================================================================
81760d135fSAriel Constenla-Haile     namespace
82760d135fSAriel Constenla-Haile     {
lcl_createPlainWindow_nothrow(const Reference<XComponentContext> & i_rContext,const Reference<XWindowPeer> & i_rParentWindow)83760d135fSAriel Constenla-Haile         Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
84760d135fSAriel Constenla-Haile             const Reference< XWindowPeer >& i_rParentWindow )
85760d135fSAriel Constenla-Haile         {
86760d135fSAriel Constenla-Haile             try
87760d135fSAriel Constenla-Haile             {
88*abfe7a5aSAriel Constenla-Haile                 OSL_ENSURE( i_rContext.is(), "illegal component context" );
89760d135fSAriel Constenla-Haile                 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
90760d135fSAriel Constenla-Haile                 Reference< XToolkit > xToolkit( xFactory->createInstanceWithContext(
91760d135fSAriel Constenla-Haile                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ),
92760d135fSAriel Constenla-Haile                     i_rContext
93760d135fSAriel Constenla-Haile                 ), UNO_QUERY_THROW );
94760d135fSAriel Constenla-Haile 
95760d135fSAriel Constenla-Haile                 WindowDescriptor aWindow;
96760d135fSAriel Constenla-Haile                 aWindow.Type = WindowClass_SIMPLE;
97760d135fSAriel Constenla-Haile                 aWindow.WindowServiceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "window" ) );
98760d135fSAriel Constenla-Haile                 aWindow.Parent = i_rParentWindow;
99760d135fSAriel Constenla-Haile                 aWindow.WindowAttributes = WindowAttribute::BORDER;
100760d135fSAriel Constenla-Haile 
101760d135fSAriel Constenla-Haile                 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
102760d135fSAriel Constenla-Haile                 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
103760d135fSAriel Constenla-Haile             }
104760d135fSAriel Constenla-Haile             catch( const Exception& )
105760d135fSAriel Constenla-Haile             {
106760d135fSAriel Constenla-Haile             }
107760d135fSAriel Constenla-Haile             return NULL;
108760d135fSAriel Constenla-Haile         }
109760d135fSAriel Constenla-Haile     }
110760d135fSAriel Constenla-Haile 	//==================================================================================================================
111760d135fSAriel Constenla-Haile 	//= class SingleColorPanel
112760d135fSAriel Constenla-Haile 	//==================================================================================================================
113760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
SingleColorPanel(const Reference<XComponentContext> & i_rContext,const Reference<XWindow> & i_rParentWindow,const::sal_Int32 i_nPanelColor)114760d135fSAriel Constenla-Haile     SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow, const ::sal_Int32 i_nPanelColor )
115760d135fSAriel Constenla-Haile         :SingleColorPanel_Base( m_aMutex )
116760d135fSAriel Constenla-Haile         ,m_xWindow()
117760d135fSAriel Constenla-Haile         ,m_nPanelColor( i_nPanelColor )
118760d135fSAriel Constenla-Haile     {
119760d135fSAriel Constenla-Haile         // retrieve the parent window for our to-be-created pane window
120760d135fSAriel Constenla-Haile         Reference< XWindowPeer > xParentPeer( i_rParentWindow, UNO_QUERY );
121760d135fSAriel Constenla-Haile 
122760d135fSAriel Constenla-Haile         osl_incrementInterlockedCount( &m_refCount );
123760d135fSAriel Constenla-Haile         if ( xParentPeer.is() )
124760d135fSAriel Constenla-Haile         {
125760d135fSAriel Constenla-Haile             m_xWindow = lcl_createPlainWindow_nothrow( i_rContext, xParentPeer );
126760d135fSAriel Constenla-Haile             m_xWindow->addPaintListener( this );
127760d135fSAriel Constenla-Haile             if ( m_xWindow.is() )
128760d135fSAriel Constenla-Haile             {
129760d135fSAriel Constenla-Haile                 const Rectangle aPanelAnchorSize( i_rParentWindow->getPosSize() );
130760d135fSAriel Constenla-Haile                 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
131760d135fSAriel Constenla-Haile                 m_xWindow->setVisible( sal_True );
132760d135fSAriel Constenla-Haile             }
133760d135fSAriel Constenla-Haile         }
134760d135fSAriel Constenla-Haile         osl_decrementInterlockedCount( &m_refCount );
135760d135fSAriel Constenla-Haile     }
136760d135fSAriel Constenla-Haile 
137760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
~SingleColorPanel()138760d135fSAriel Constenla-Haile     SingleColorPanel::~SingleColorPanel()
139760d135fSAriel Constenla-Haile     {
140760d135fSAriel Constenla-Haile     }
141760d135fSAriel Constenla-Haile 
142760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
getWindow()143760d135fSAriel Constenla-Haile     Reference< XWindow > SAL_CALL SingleColorPanel::getWindow() throw (RuntimeException)
144760d135fSAriel Constenla-Haile     {
145760d135fSAriel Constenla-Haile         ::osl::MutexGuard aGuard( m_aMutex );
146760d135fSAriel Constenla-Haile         if ( !m_xWindow.is() )
147760d135fSAriel Constenla-Haile             throw DisposedException( ::rtl::OUString(), *this );
148760d135fSAriel Constenla-Haile         return m_xWindow;
149760d135fSAriel Constenla-Haile     }
150760d135fSAriel Constenla-Haile 
151760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
createAccessible(const Reference<XAccessible> & i_rParentAccessible)152760d135fSAriel Constenla-Haile     Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
153760d135fSAriel Constenla-Haile     {
154760d135fSAriel Constenla-Haile         ::osl::MutexGuard aGuard( m_aMutex );
155760d135fSAriel Constenla-Haile         if ( !m_xWindow.is() )
156760d135fSAriel Constenla-Haile             throw DisposedException( ::rtl::OUString(), *this );
157760d135fSAriel Constenla-Haile 
158760d135fSAriel Constenla-Haile         // TODO: the following is wrong, since it doesn't respect i_rParentAccessible. In a real extension, you should
159760d135fSAriel Constenla-Haile         // implement this correctly :)
160760d135fSAriel Constenla-Haile         (void)i_rParentAccessible;
161760d135fSAriel Constenla-Haile         return Reference< XAccessible >( getWindow(), UNO_QUERY );
162760d135fSAriel Constenla-Haile     }
163760d135fSAriel Constenla-Haile 
164760d135fSAriel Constenla-Haile     //------------------------------------------------------------------------------------------------------------------
windowPaint(const PaintEvent & i_rEvent)165760d135fSAriel Constenla-Haile     void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
166760d135fSAriel Constenla-Haile     {
167760d135fSAriel Constenla-Haile         try
168760d135fSAriel Constenla-Haile         {
169760d135fSAriel Constenla-Haile             const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
170760d135fSAriel Constenla-Haile             const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
171760d135fSAriel Constenla-Haile             xGraphics->setFillColor( m_nPanelColor );
172760d135fSAriel Constenla-Haile             xGraphics->setLineColor( 0x00FFFFFF );
173760d135fSAriel Constenla-Haile 
174760d135fSAriel Constenla-Haile             const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
175760d135fSAriel Constenla-Haile             const Rectangle aWindowRect( xWindow->getPosSize() );
176760d135fSAriel Constenla-Haile             xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
177760d135fSAriel Constenla-Haile         }
178760d135fSAriel Constenla-Haile         catch( const Exception& )
179760d135fSAriel Constenla-Haile         {
180760d135fSAriel Constenla-Haile         }
181760d135fSAriel Constenla-Haile     }
182760d135fSAriel Constenla-Haile 
183760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
disposing(const EventObject & i_rSource)184760d135fSAriel Constenla-Haile     void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
185760d135fSAriel Constenla-Haile     {
186760d135fSAriel Constenla-Haile         (void)i_rSource;
187760d135fSAriel Constenla-Haile     }
188760d135fSAriel Constenla-Haile 
189760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
disposing()190760d135fSAriel Constenla-Haile     void SAL_CALL SingleColorPanel::disposing()
191760d135fSAriel Constenla-Haile     {
192760d135fSAriel Constenla-Haile         ::osl::MutexGuard aGuard( m_aMutex );
193760d135fSAriel Constenla-Haile         if ( !m_xWindow.is() )
194760d135fSAriel Constenla-Haile             // already disposed
195760d135fSAriel Constenla-Haile             return;
196760d135fSAriel Constenla-Haile         m_xWindow->removePaintListener( this );
197760d135fSAriel Constenla-Haile         try
198760d135fSAriel Constenla-Haile         {
199760d135fSAriel Constenla-Haile             Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
200760d135fSAriel Constenla-Haile             xWindowComp->dispose();
201760d135fSAriel Constenla-Haile         }
202760d135fSAriel Constenla-Haile         catch( const Exception& )
203760d135fSAriel Constenla-Haile         {
204760d135fSAriel Constenla-Haile         }
205760d135fSAriel Constenla-Haile         m_xWindow.clear();
206760d135fSAriel Constenla-Haile     }
207760d135fSAriel Constenla-Haile 
208760d135fSAriel Constenla-Haile 	//==================================================================================================================
209760d135fSAriel Constenla-Haile 	//= PanelUIElement
210760d135fSAriel Constenla-Haile 	//==================================================================================================================
211760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
PanelUIElement(const Reference<XComponentContext> & i_rContext,const Reference<XWindow> & i_rParentWindow,const::rtl::OUString & i_rResourceURL,const::sal_Int32 i_nPanelColor)212760d135fSAriel Constenla-Haile     PanelUIElement::PanelUIElement( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow,
213760d135fSAriel Constenla-Haile         const ::rtl::OUString& i_rResourceURL, const ::sal_Int32 i_nPanelColor )
214760d135fSAriel Constenla-Haile         :PanelUIElement_Base( m_aMutex )
215760d135fSAriel Constenla-Haile         ,m_sResourceURL( i_rResourceURL )
216760d135fSAriel Constenla-Haile         ,m_xToolPanel( new SingleColorPanel( i_rContext, i_rParentWindow, i_nPanelColor ) )
217760d135fSAriel Constenla-Haile     {
218760d135fSAriel Constenla-Haile     }
219760d135fSAriel Constenla-Haile 
220760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
~PanelUIElement()221760d135fSAriel Constenla-Haile     PanelUIElement::~PanelUIElement()
222760d135fSAriel Constenla-Haile     {
223760d135fSAriel Constenla-Haile     }
224760d135fSAriel Constenla-Haile 
225760d135fSAriel Constenla-Haile  	//------------------------------------------------------------------------------------------------------------------
getFrame()226760d135fSAriel Constenla-Haile     Reference< XFrame > SAL_CALL PanelUIElement::getFrame() throw (RuntimeException)
227760d135fSAriel Constenla-Haile     {
228760d135fSAriel Constenla-Haile         // TODO
229760d135fSAriel Constenla-Haile         return NULL;
230760d135fSAriel Constenla-Haile     }
231760d135fSAriel Constenla-Haile 
232760d135fSAriel Constenla-Haile     //------------------------------------------------------------------------------------------------------------------
getResourceURL()233760d135fSAriel Constenla-Haile     ::rtl::OUString SAL_CALL PanelUIElement::getResourceURL() throw (RuntimeException)
234760d135fSAriel Constenla-Haile     {
235760d135fSAriel Constenla-Haile         return m_sResourceURL;
236760d135fSAriel Constenla-Haile     }
237760d135fSAriel Constenla-Haile 
238760d135fSAriel Constenla-Haile     //------------------------------------------------------------------------------------------------------------------
getType()239760d135fSAriel Constenla-Haile     ::sal_Int16 SAL_CALL PanelUIElement::getType() throw (RuntimeException)
240760d135fSAriel Constenla-Haile     {
241760d135fSAriel Constenla-Haile         return UIElementType::TOOLPANEL;
242760d135fSAriel Constenla-Haile     }
243760d135fSAriel Constenla-Haile 
244760d135fSAriel Constenla-Haile  	//------------------------------------------------------------------------------------------------------------------
getRealInterface()245760d135fSAriel Constenla-Haile     Reference< XInterface > SAL_CALL PanelUIElement::getRealInterface(  ) throw (RuntimeException)
246760d135fSAriel Constenla-Haile     {
247760d135fSAriel Constenla-Haile         ::osl::MutexGuard aGuard( m_aMutex );
248760d135fSAriel Constenla-Haile         if ( !m_xToolPanel.is() )
249760d135fSAriel Constenla-Haile             throw DisposedException();
250760d135fSAriel Constenla-Haile         return m_xToolPanel;
251760d135fSAriel Constenla-Haile     }
252760d135fSAriel Constenla-Haile 
253760d135fSAriel Constenla-Haile 	//------------------------------------------------------------------------------------------------------------------
disposing()254760d135fSAriel Constenla-Haile     void SAL_CALL PanelUIElement::disposing()
255760d135fSAriel Constenla-Haile     {
256760d135fSAriel Constenla-Haile         Reference< XComponent > xPanelComponent( m_xToolPanel, UNO_QUERY_THROW );
257760d135fSAriel Constenla-Haile         m_xToolPanel.clear();
258760d135fSAriel Constenla-Haile         xPanelComponent->dispose();
259760d135fSAriel Constenla-Haile     }
260760d135fSAriel Constenla-Haile 
261760d135fSAriel Constenla-Haile //......................................................................................................................
262760d135fSAriel Constenla-Haile } } // namespace sd::colortoolpanel
263760d135fSAriel Constenla-Haile //......................................................................................................................
264