1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25 ************************************************************************/
26 
27 #include "precompiled_sd.hxx"
28 
29 #include "ctp_panel.hxx"
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/drawing/framework/XPane.hpp>
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <com/sun/star/awt/XWindowPeer.hpp>
35 #include <com/sun/star/awt/XToolkit.hpp>
36 #include <com/sun/star/awt/WindowClass.hpp>
37 #include <com/sun/star/awt/WindowAttribute.hpp>
38 #include <com/sun/star/awt/PosSize.hpp>
39 #include <com/sun/star/awt/XDevice.hpp>
40 #include <com/sun/star/awt/XGraphics.hpp>
41 /** === end UNO includes === **/
42 
43 #include <tools/diagnose_ex.h>
44 
45 //......................................................................................................................
46 namespace sd { namespace colortoolpanel
47 {
48 //......................................................................................................................
49 
50 	/** === begin UNO using === **/
51 	using ::com::sun::star::uno::Reference;
52 	using ::com::sun::star::uno::XInterface;
53 	using ::com::sun::star::uno::UNO_QUERY;
54 	using ::com::sun::star::uno::UNO_QUERY_THROW;
55 	using ::com::sun::star::uno::UNO_SET_THROW;
56 	using ::com::sun::star::uno::Exception;
57 	using ::com::sun::star::uno::RuntimeException;
58 	using ::com::sun::star::uno::Any;
59 	using ::com::sun::star::uno::makeAny;
60 	using ::com::sun::star::uno::Sequence;
61 	using ::com::sun::star::uno::Type;
62     using ::com::sun::star::drawing::framework::XConfigurationController;
63     using ::com::sun::star::drawing::framework::XResourceId;
64     using ::com::sun::star::uno::XComponentContext;
65     using ::com::sun::star::drawing::framework::XPane;
66     using ::com::sun::star::awt::XWindow;
67     using ::com::sun::star::rendering::XCanvas;
68     using ::com::sun::star::lang::DisposedException;
69     using ::com::sun::star::awt::XWindowPeer;
70     using ::com::sun::star::lang::XMultiComponentFactory;
71     using ::com::sun::star::awt::XToolkit;
72     using ::com::sun::star::awt::WindowDescriptor;
73     using ::com::sun::star::awt::WindowClass_SIMPLE;
74     using ::com::sun::star::awt::Rectangle;
75     using ::com::sun::star::awt::PaintEvent;
76     using ::com::sun::star::lang::EventObject;
77     using ::com::sun::star::awt::XDevice;
78     using ::com::sun::star::awt::XGraphics;
79     using ::com::sun::star::accessibility::XAccessible;
80 	/** === end UNO using === **/
81     namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
82     namespace PosSize = ::com::sun::star::awt::PosSize;
83 
84 	//==================================================================================================================
85 	//= helpers
86 	//==================================================================================================================
87     namespace
88     {
89         Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
90             const Reference< XWindowPeer >& i_rParentWindow )
91         {
92             try
93             {
94                 ENSURE_OR_THROW( i_rContext.is(), "illegal component context" );
95                 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
96                 Reference< XToolkit > xToolkit( xFactory->createInstanceWithContext(
97                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ),
98                     i_rContext
99                 ), UNO_QUERY_THROW );
100 
101                 WindowDescriptor aWindow;
102                 aWindow.Type = WindowClass_SIMPLE;
103                 aWindow.WindowServiceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "window" ) );
104                 aWindow.Parent = i_rParentWindow;
105                 aWindow.WindowAttributes = WindowAttribute::BORDER;
106 
107                 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
108                 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
109             }
110             catch( const Exception& )
111             {
112             	DBG_UNHANDLED_EXCEPTION();
113             }
114             return NULL;
115         }
116     }
117 	//==================================================================================================================
118 	//= class SingleColorPanel
119 	//==================================================================================================================
120 	//------------------------------------------------------------------------------------------------------------------
121     SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext,
122             const Reference< XConfigurationController >& i_rConfigController, const Reference< XResourceId >& i_rResourceId )
123         :SingleColorPanel_Base( m_aMutex )
124         ,m_xContext( i_rContext )
125         ,m_xResourceId( i_rResourceId )
126         ,m_xWindow()
127     {
128         ENSURE_OR_THROW( i_rConfigController.is(), "invalid configuration controller" );
129         ENSURE_OR_THROW( m_xResourceId.is(), "invalid resource id" );
130 
131         // retrieve the parent window for our to-be-created pane window
132         Reference< XWindow > xParentWindow;
133         Reference< XWindowPeer > xParentPeer;
134         try
135         {
136             Reference< XResource > xAnchor( i_rConfigController->getResource( m_xResourceId->getAnchor() ), UNO_SET_THROW );
137             Reference< XPane > xAnchorPane( xAnchor, UNO_QUERY_THROW );
138             xParentWindow.set( xAnchorPane->getWindow(), UNO_SET_THROW );
139             xParentPeer.set( xParentWindow, UNO_QUERY_THROW );
140         }
141         catch( const Exception& )
142         {
143         	DBG_UNHANDLED_EXCEPTION();
144         }
145         osl_incrementInterlockedCount( &m_refCount );
146         if ( xParentWindow.is() )
147         {
148             m_xWindow = lcl_createPlainWindow_nothrow( m_xContext, xParentPeer );
149             m_xWindow->addPaintListener( this );
150             if ( m_xWindow.is() )
151             {
152                 const Rectangle aPanelAnchorSize( xParentWindow->getPosSize() );
153                 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
154                 m_xWindow->setVisible( sal_True );
155             }
156         }
157         osl_decrementInterlockedCount( &m_refCount );
158     }
159 
160 	//------------------------------------------------------------------------------------------------------------------
161     SingleColorPanel::~SingleColorPanel()
162     {
163     }
164 
165 	//------------------------------------------------------------------------------------------------------------------
166     Reference< XWindow > SAL_CALL SingleColorPanel::getWindow(  ) throw (RuntimeException)
167     {
168         ::osl::MutexGuard aGuard( m_aMutex );
169         if ( !m_xWindow.get() )
170             throw DisposedException( ::rtl::OUString(), *this );
171         return m_xWindow;
172     }
173 
174 	//------------------------------------------------------------------------------------------------------------------
175     Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
176     {
177         (void)i_rParentAccessible;
178         return Reference< XAccessible >( m_xWindow, UNO_QUERY );
179         // TODO: this is, strictly, not correct, as we ignore i_ParentAccessible here. If you are not doing a sample
180         // extension only, you'll want to do this correctly ....
181     }
182 
183 	//------------------------------------------------------------------------------------------------------------------
184     Reference< XResourceId > SAL_CALL SingleColorPanel::getResourceId(  ) throw (RuntimeException)
185     {
186         ::osl::MutexGuard aGuard( m_aMutex );
187         if ( !m_xWindow.is() )
188             throw DisposedException( ::rtl::OUString(), *this );
189         return m_xResourceId;
190     }
191 
192 	//------------------------------------------------------------------------------------------------------------------
193     ::sal_Bool SAL_CALL SingleColorPanel::isAnchorOnly(  ) throw (RuntimeException)
194     {
195         ::osl::MutexGuard aGuard( m_aMutex );
196         if ( !m_xWindow.is() )
197             throw DisposedException( ::rtl::OUString(), *this );
198         return sal_False;
199     }
200 
201 	//------------------------------------------------------------------------------------------------------------------
202     void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
203     {
204         try
205         {
206             const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
207             const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
208             xGraphics->setFillColor( 0x80 << 8 );
209             xGraphics->setLineColor( 0x80 << 16 );
210 
211             const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
212             const Rectangle aWindowRect( xWindow->getPosSize() );
213             xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
214         }
215         catch( const Exception& )
216         {
217         	DBG_UNHANDLED_EXCEPTION();
218         }
219     }
220 
221 	//------------------------------------------------------------------------------------------------------------------
222     void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
223     {
224         (void)i_rSource;
225     }
226 
227 	//------------------------------------------------------------------------------------------------------------------
228     void SAL_CALL SingleColorPanel::disposing()
229     {
230         ::osl::MutexGuard aGuard( m_aMutex );
231         if ( !m_xWindow.is() )
232             // already disposed
233             return;
234         m_xWindow->removePaintListener( this );
235         try
236         {
237             Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
238             xWindowComp->dispose();
239         }
240         catch( const Exception& )
241         {
242         	DBG_UNHANDLED_EXCEPTION();
243         }
244         m_xWindow.clear();
245     }
246 
247 //......................................................................................................................
248 } } // namespace sd::colortoolpanel
249 //......................................................................................................................
250