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 
25 #include "ctp_panel.hxx"
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/awt/XWindowPeer.hpp>
30 #include <com/sun/star/awt/XToolkit.hpp>
31 #include <com/sun/star/awt/WindowClass.hpp>
32 #include <com/sun/star/awt/WindowAttribute.hpp>
33 #include <com/sun/star/awt/PosSize.hpp>
34 #include <com/sun/star/awt/XDevice.hpp>
35 #include <com/sun/star/awt/XGraphics.hpp>
36 #include <com/sun/star/ui/UIElementType.hpp>
37 /** === end UNO includes === **/
38 
39 #include <osl/diagnose.h>
40 
41 //......................................................................................................................
42 namespace sd { namespace colortoolpanel
43 {
44 //......................................................................................................................
45 
46 	/** === begin UNO using === **/
47 	using ::com::sun::star::uno::Reference;
48 	using ::com::sun::star::uno::XInterface;
49 	using ::com::sun::star::uno::UNO_QUERY;
50 	using ::com::sun::star::uno::UNO_QUERY_THROW;
51 	using ::com::sun::star::uno::UNO_SET_THROW;
52 	using ::com::sun::star::uno::Exception;
53 	using ::com::sun::star::uno::RuntimeException;
54 	using ::com::sun::star::uno::Any;
55 	using ::com::sun::star::uno::makeAny;
56 	using ::com::sun::star::uno::Sequence;
57 	using ::com::sun::star::uno::Type;
58     using ::com::sun::star::uno::XComponentContext;
59     using ::com::sun::star::awt::XWindow;
60     using ::com::sun::star::lang::DisposedException;
61     using ::com::sun::star::awt::XWindowPeer;
62     using ::com::sun::star::lang::XMultiComponentFactory;
63     using ::com::sun::star::awt::XToolkit;
64     using ::com::sun::star::awt::WindowDescriptor;
65     using ::com::sun::star::awt::WindowClass_SIMPLE;
66     using ::com::sun::star::awt::Rectangle;
67     using ::com::sun::star::awt::PaintEvent;
68     using ::com::sun::star::lang::EventObject;
69     using ::com::sun::star::awt::XDevice;
70     using ::com::sun::star::awt::XGraphics;
71     using ::com::sun::star::accessibility::XAccessible;
72     using ::com::sun::star::frame::XFrame;
73 	/** === end UNO using === **/
74     namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
75     namespace PosSize = ::com::sun::star::awt::PosSize;
76     namespace UIElementType = ::com::sun::star::ui::UIElementType;
77 
78 	//==================================================================================================================
79 	//= helpers
80 	//==================================================================================================================
81     namespace
82     {
lcl_createPlainWindow_nothrow(const Reference<XComponentContext> & i_rContext,const Reference<XWindowPeer> & i_rParentWindow)83         Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
84             const Reference< XWindowPeer >& i_rParentWindow )
85         {
86             try
87             {
88                 OSL_ENSURE( i_rContext.is(), "illegal component context" );
89                 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
90                 Reference< XToolkit > xToolkit( xFactory->createInstanceWithContext(
91                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ),
92                     i_rContext
93                 ), UNO_QUERY_THROW );
94 
95                 WindowDescriptor aWindow;
96                 aWindow.Type = WindowClass_SIMPLE;
97                 aWindow.WindowServiceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "window" ) );
98                 aWindow.Parent = i_rParentWindow;
99                 aWindow.WindowAttributes = WindowAttribute::BORDER;
100 
101                 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
102                 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
103             }
104             catch( const Exception& )
105             {
106             }
107             return NULL;
108         }
109     }
110 	//==================================================================================================================
111 	//= class SingleColorPanel
112 	//==================================================================================================================
113 	//------------------------------------------------------------------------------------------------------------------
SingleColorPanel(const Reference<XComponentContext> & i_rContext,const Reference<XWindow> & i_rParentWindow,const::sal_Int32 i_nPanelColor)114     SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow, const ::sal_Int32 i_nPanelColor )
115         :SingleColorPanel_Base( m_aMutex )
116         ,m_xWindow()
117         ,m_nPanelColor( i_nPanelColor )
118     {
119         // retrieve the parent window for our to-be-created pane window
120         Reference< XWindowPeer > xParentPeer( i_rParentWindow, UNO_QUERY );
121 
122         osl_incrementInterlockedCount( &m_refCount );
123         if ( xParentPeer.is() )
124         {
125             m_xWindow = lcl_createPlainWindow_nothrow( i_rContext, xParentPeer );
126             m_xWindow->addPaintListener( this );
127             if ( m_xWindow.is() )
128             {
129                 const Rectangle aPanelAnchorSize( i_rParentWindow->getPosSize() );
130                 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
131                 m_xWindow->setVisible( sal_True );
132             }
133         }
134         osl_decrementInterlockedCount( &m_refCount );
135     }
136 
137 	//------------------------------------------------------------------------------------------------------------------
~SingleColorPanel()138     SingleColorPanel::~SingleColorPanel()
139     {
140     }
141 
142 	//------------------------------------------------------------------------------------------------------------------
getWindow()143     Reference< XWindow > SAL_CALL SingleColorPanel::getWindow() throw (RuntimeException)
144     {
145         ::osl::MutexGuard aGuard( m_aMutex );
146         if ( !m_xWindow.is() )
147             throw DisposedException( ::rtl::OUString(), *this );
148         return m_xWindow;
149     }
150 
151 	//------------------------------------------------------------------------------------------------------------------
createAccessible(const Reference<XAccessible> & i_rParentAccessible)152     Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
153     {
154         ::osl::MutexGuard aGuard( m_aMutex );
155         if ( !m_xWindow.is() )
156             throw DisposedException( ::rtl::OUString(), *this );
157 
158         // TODO: the following is wrong, since it doesn't respect i_rParentAccessible. In a real extension, you should
159         // implement this correctly :)
160         (void)i_rParentAccessible;
161         return Reference< XAccessible >( getWindow(), UNO_QUERY );
162     }
163 
164     //------------------------------------------------------------------------------------------------------------------
windowPaint(const PaintEvent & i_rEvent)165     void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
166     {
167         try
168         {
169             const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
170             const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
171             xGraphics->setFillColor( m_nPanelColor );
172             xGraphics->setLineColor( 0x00FFFFFF );
173 
174             const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
175             const Rectangle aWindowRect( xWindow->getPosSize() );
176             xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
177         }
178         catch( const Exception& )
179         {
180         }
181     }
182 
183 	//------------------------------------------------------------------------------------------------------------------
disposing(const EventObject & i_rSource)184     void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
185     {
186         (void)i_rSource;
187     }
188 
189 	//------------------------------------------------------------------------------------------------------------------
disposing()190     void SAL_CALL SingleColorPanel::disposing()
191     {
192         ::osl::MutexGuard aGuard( m_aMutex );
193         if ( !m_xWindow.is() )
194             // already disposed
195             return;
196         m_xWindow->removePaintListener( this );
197         try
198         {
199             Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
200             xWindowComp->dispose();
201         }
202         catch( const Exception& )
203         {
204         }
205         m_xWindow.clear();
206     }
207 
208 	//==================================================================================================================
209 	//= PanelUIElement
210 	//==================================================================================================================
211 	//------------------------------------------------------------------------------------------------------------------
PanelUIElement(const Reference<XComponentContext> & i_rContext,const Reference<XWindow> & i_rParentWindow,const::rtl::OUString & i_rResourceURL,const::sal_Int32 i_nPanelColor)212     PanelUIElement::PanelUIElement( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow,
213         const ::rtl::OUString& i_rResourceURL, const ::sal_Int32 i_nPanelColor )
214         :PanelUIElement_Base( m_aMutex )
215         ,m_sResourceURL( i_rResourceURL )
216         ,m_xToolPanel( new SingleColorPanel( i_rContext, i_rParentWindow, i_nPanelColor ) )
217     {
218     }
219 
220 	//------------------------------------------------------------------------------------------------------------------
~PanelUIElement()221     PanelUIElement::~PanelUIElement()
222     {
223     }
224 
225  	//------------------------------------------------------------------------------------------------------------------
getFrame()226     Reference< XFrame > SAL_CALL PanelUIElement::getFrame() throw (RuntimeException)
227     {
228         // TODO
229         return NULL;
230     }
231 
232     //------------------------------------------------------------------------------------------------------------------
getResourceURL()233     ::rtl::OUString SAL_CALL PanelUIElement::getResourceURL() throw (RuntimeException)
234     {
235         return m_sResourceURL;
236     }
237 
238     //------------------------------------------------------------------------------------------------------------------
getType()239     ::sal_Int16 SAL_CALL PanelUIElement::getType() throw (RuntimeException)
240     {
241         return UIElementType::TOOLPANEL;
242     }
243 
244  	//------------------------------------------------------------------------------------------------------------------
getRealInterface()245     Reference< XInterface > SAL_CALL PanelUIElement::getRealInterface(  ) throw (RuntimeException)
246     {
247         ::osl::MutexGuard aGuard( m_aMutex );
248         if ( !m_xToolPanel.is() )
249             throw DisposedException();
250         return m_xToolPanel;
251     }
252 
253 	//------------------------------------------------------------------------------------------------------------------
disposing()254     void SAL_CALL PanelUIElement::disposing()
255     {
256         Reference< XComponent > xPanelComponent( m_xToolPanel, UNO_QUERY_THROW );
257         m_xToolPanel.clear();
258         xPanelComponent->dispose();
259     }
260 
261 //......................................................................................................................
262 } } // namespace sd::colortoolpanel
263 //......................................................................................................................
264