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