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_sd.hxx"
25 
26 #include "ctp_panel.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/drawing/framework/XPane.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/awt/XWindowPeer.hpp>
32 #include <com/sun/star/awt/XToolkit.hpp>
33 #include <com/sun/star/awt/WindowClass.hpp>
34 #include <com/sun/star/awt/WindowAttribute.hpp>
35 #include <com/sun/star/awt/PosSize.hpp>
36 #include <com/sun/star/awt/XDevice.hpp>
37 #include <com/sun/star/awt/XGraphics.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::drawing::framework::XConfigurationController;
60     using ::com::sun::star::drawing::framework::XResourceId;
61     using ::com::sun::star::uno::XComponentContext;
62     using ::com::sun::star::drawing::framework::XPane;
63     using ::com::sun::star::awt::XWindow;
64     using ::com::sun::star::rendering::XCanvas;
65     using ::com::sun::star::lang::DisposedException;
66     using ::com::sun::star::awt::XWindowPeer;
67     using ::com::sun::star::lang::XMultiComponentFactory;
68     using ::com::sun::star::awt::XToolkit;
69     using ::com::sun::star::awt::WindowDescriptor;
70     using ::com::sun::star::awt::WindowClass_SIMPLE;
71     using ::com::sun::star::awt::Rectangle;
72     using ::com::sun::star::awt::PaintEvent;
73     using ::com::sun::star::lang::EventObject;
74     using ::com::sun::star::awt::XDevice;
75     using ::com::sun::star::awt::XGraphics;
76     using ::com::sun::star::accessibility::XAccessible;
77 	/** === end UNO using === **/
78     namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
79     namespace PosSize = ::com::sun::star::awt::PosSize;
80 
81 	//==================================================================================================================
82 	//= helpers
83 	//==================================================================================================================
84     namespace
85     {
lcl_createPlainWindow_nothrow(const Reference<XComponentContext> & i_rContext,const Reference<XWindowPeer> & i_rParentWindow)86         Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
87             const Reference< XWindowPeer >& i_rParentWindow )
88         {
89             try
90             {
91                 ENSURE_OR_THROW( i_rContext.is(), "illegal component context" );
92                 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
93                 Reference< XToolkit > xToolkit( xFactory->createInstanceWithContext(
94                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ),
95                     i_rContext
96                 ), UNO_QUERY_THROW );
97 
98                 WindowDescriptor aWindow;
99                 aWindow.Type = WindowClass_SIMPLE;
100                 aWindow.WindowServiceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "window" ) );
101                 aWindow.Parent = i_rParentWindow;
102                 aWindow.WindowAttributes = WindowAttribute::BORDER;
103 
104                 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
105                 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
106             }
107             catch( const Exception& )
108             {
109             	DBG_UNHANDLED_EXCEPTION();
110             }
111             return NULL;
112         }
113     }
114 	//==================================================================================================================
115 	//= class SingleColorPanel
116 	//==================================================================================================================
117 	//------------------------------------------------------------------------------------------------------------------
SingleColorPanel(const Reference<XComponentContext> & i_rContext,const Reference<XConfigurationController> & i_rConfigController,const Reference<XResourceId> & i_rResourceId)118     SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext,
119             const Reference< XConfigurationController >& i_rConfigController, const Reference< XResourceId >& i_rResourceId )
120         :SingleColorPanel_Base( m_aMutex )
121         ,m_xContext( i_rContext )
122         ,m_xResourceId( i_rResourceId )
123         ,m_xWindow()
124     {
125         ENSURE_OR_THROW( i_rConfigController.is(), "invalid configuration controller" );
126         ENSURE_OR_THROW( m_xResourceId.is(), "invalid resource id" );
127 
128         // retrieve the parent window for our to-be-created pane window
129         Reference< XWindow > xParentWindow;
130         Reference< XWindowPeer > xParentPeer;
131         try
132         {
133             Reference< XResource > xAnchor( i_rConfigController->getResource( m_xResourceId->getAnchor() ), UNO_SET_THROW );
134             Reference< XPane > xAnchorPane( xAnchor, UNO_QUERY_THROW );
135             xParentWindow.set( xAnchorPane->getWindow(), UNO_SET_THROW );
136             xParentPeer.set( xParentWindow, UNO_QUERY_THROW );
137         }
138         catch( const Exception& )
139         {
140         	DBG_UNHANDLED_EXCEPTION();
141         }
142         osl_incrementInterlockedCount( &m_refCount );
143         if ( xParentWindow.is() )
144         {
145             m_xWindow = lcl_createPlainWindow_nothrow( m_xContext, xParentPeer );
146             m_xWindow->addPaintListener( this );
147             if ( m_xWindow.is() )
148             {
149                 const Rectangle aPanelAnchorSize( xParentWindow->getPosSize() );
150                 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
151                 m_xWindow->setVisible( sal_True );
152             }
153         }
154         osl_decrementInterlockedCount( &m_refCount );
155     }
156 
157 	//------------------------------------------------------------------------------------------------------------------
~SingleColorPanel()158     SingleColorPanel::~SingleColorPanel()
159     {
160     }
161 
162 	//------------------------------------------------------------------------------------------------------------------
getWindow()163     Reference< XWindow > SAL_CALL SingleColorPanel::getWindow(  ) throw (RuntimeException)
164     {
165         ::osl::MutexGuard aGuard( m_aMutex );
166         if ( !m_xWindow.get() )
167             throw DisposedException( ::rtl::OUString(), *this );
168         return m_xWindow;
169     }
170 
171 	//------------------------------------------------------------------------------------------------------------------
createAccessible(const Reference<XAccessible> & i_rParentAccessible)172     Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
173     {
174         (void)i_rParentAccessible;
175         return Reference< XAccessible >( m_xWindow, UNO_QUERY );
176         // TODO: this is, strictly, not correct, as we ignore i_ParentAccessible here. If you are not doing a sample
177         // extension only, you'll want to do this correctly ....
178     }
179 
180 	//------------------------------------------------------------------------------------------------------------------
getResourceId()181     Reference< XResourceId > SAL_CALL SingleColorPanel::getResourceId(  ) throw (RuntimeException)
182     {
183         ::osl::MutexGuard aGuard( m_aMutex );
184         if ( !m_xWindow.is() )
185             throw DisposedException( ::rtl::OUString(), *this );
186         return m_xResourceId;
187     }
188 
189 	//------------------------------------------------------------------------------------------------------------------
isAnchorOnly()190     ::sal_Bool SAL_CALL SingleColorPanel::isAnchorOnly(  ) throw (RuntimeException)
191     {
192         ::osl::MutexGuard aGuard( m_aMutex );
193         if ( !m_xWindow.is() )
194             throw DisposedException( ::rtl::OUString(), *this );
195         return sal_False;
196     }
197 
198 	//------------------------------------------------------------------------------------------------------------------
windowPaint(const PaintEvent & i_rEvent)199     void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
200     {
201         try
202         {
203             const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
204             const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
205             xGraphics->setFillColor( 0x80 << 8 );
206             xGraphics->setLineColor( 0x80 << 16 );
207 
208             const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
209             const Rectangle aWindowRect( xWindow->getPosSize() );
210             xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
211         }
212         catch( const Exception& )
213         {
214         	DBG_UNHANDLED_EXCEPTION();
215         }
216     }
217 
218 	//------------------------------------------------------------------------------------------------------------------
disposing(const EventObject & i_rSource)219     void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
220     {
221         (void)i_rSource;
222     }
223 
224 	//------------------------------------------------------------------------------------------------------------------
disposing()225     void SAL_CALL SingleColorPanel::disposing()
226     {
227         ::osl::MutexGuard aGuard( m_aMutex );
228         if ( !m_xWindow.is() )
229             // already disposed
230             return;
231         m_xWindow->removePaintListener( this );
232         try
233         {
234             Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
235             xWindowComp->dispose();
236         }
237         catch( const Exception& )
238         {
239         	DBG_UNHANDLED_EXCEPTION();
240         }
241         m_xWindow.clear();
242     }
243 
244 //......................................................................................................................
245 } } // namespace sd::colortoolpanel
246 //......................................................................................................................
247