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