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