bump product version to 4.1.6.2
[LibreOffice.git] / odk / examples / cpp / custompanel / ctp_panel.cxx
blob6848f35573590f679b9fd9361bfe22ccc3237437
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "ctp_panel.hxx"
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/awt/XWindowPeer.hpp>
25 #include <com/sun/star/awt/Toolkit.hpp>
26 #include <com/sun/star/awt/WindowClass.hpp>
27 #include <com/sun/star/awt/WindowAttribute.hpp>
28 #include <com/sun/star/awt/PosSize.hpp>
29 #include <com/sun/star/awt/XDevice.hpp>
30 #include <com/sun/star/awt/XGraphics.hpp>
31 #include <com/sun/star/ui/UIElementType.hpp>
33 #include <osl/diagnose.h>
35 //......................................................................................................................
36 namespace sd { namespace colortoolpanel
38 //......................................................................................................................
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::XInterface;
42 using ::com::sun::star::uno::UNO_QUERY;
43 using ::com::sun::star::uno::UNO_QUERY_THROW;
44 using ::com::sun::star::uno::UNO_SET_THROW;
45 using ::com::sun::star::uno::Exception;
46 using ::com::sun::star::uno::RuntimeException;
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::uno::makeAny;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::Type;
51 using ::com::sun::star::uno::XComponentContext;
52 using ::com::sun::star::awt::XWindow;
53 using ::com::sun::star::lang::DisposedException;
54 using ::com::sun::star::awt::XWindowPeer;
55 using ::com::sun::star::lang::XMultiComponentFactory;
56 using ::com::sun::star::awt::XToolkit;
57 using ::com::sun::star::awt::WindowDescriptor;
58 using ::com::sun::star::awt::WindowClass_SIMPLE;
59 using ::com::sun::star::awt::Rectangle;
60 using ::com::sun::star::awt::PaintEvent;
61 using ::com::sun::star::lang::EventObject;
62 using ::com::sun::star::awt::XDevice;
63 using ::com::sun::star::awt::XGraphics;
64 using ::com::sun::star::accessibility::XAccessible;
65 using ::com::sun::star::frame::XFrame;
67 namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
68 namespace PosSize = ::com::sun::star::awt::PosSize;
69 namespace UIElementType = ::com::sun::star::ui::UIElementType;
71 //==================================================================================================================
72 //= helpers
73 //==================================================================================================================
74 namespace
76 Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
77 const Reference< XWindowPeer >& i_rParentWindow )
79 try
81 OSL_ENSURE( i_rContext.is(), "illegal component context" );
82 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
83 Reference< XToolkit2 > xToolkit = Toolkit::create(i_rContext);
85 WindowDescriptor aWindow;
86 aWindow.Type = WindowClass_SIMPLE;
87 aWindow.WindowServiceName = OUString( "window" );
88 aWindow.Parent = i_rParentWindow;
89 aWindow.WindowAttributes = WindowAttribute::BORDER;
91 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
92 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
94 catch( const Exception& )
97 return NULL;
100 //==================================================================================================================
101 //= class SingleColorPanel
102 //==================================================================================================================
103 //------------------------------------------------------------------------------------------------------------------
104 SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow, const ::sal_Int32 i_nPanelColor )
105 :SingleColorPanel_Base( m_aMutex )
106 ,m_xWindow()
107 ,m_nPanelColor( i_nPanelColor )
109 // retrieve the parent window for our to-be-created pane window
110 Reference< XWindowPeer > xParentPeer( i_rParentWindow, UNO_QUERY );
112 osl_atomic_increment( &m_refCount );
113 if ( xParentPeer.is() )
115 m_xWindow = lcl_createPlainWindow_nothrow( i_rContext, xParentPeer );
116 m_xWindow->addPaintListener( this );
117 if ( m_xWindow.is() )
119 const Rectangle aPanelAnchorSize( i_rParentWindow->getPosSize() );
120 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
121 m_xWindow->setVisible( sal_True );
124 osl_atomic_decrement( &m_refCount );
127 //------------------------------------------------------------------------------------------------------------------
128 SingleColorPanel::~SingleColorPanel()
132 //------------------------------------------------------------------------------------------------------------------
133 Reference< XWindow > SAL_CALL SingleColorPanel::getWindow() throw (RuntimeException)
135 ::osl::MutexGuard aGuard( m_aMutex );
136 if ( !m_xWindow.is() )
137 throw DisposedException( OUString(), *this );
138 return m_xWindow;
141 //------------------------------------------------------------------------------------------------------------------
142 Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
144 ::osl::MutexGuard aGuard( m_aMutex );
145 if ( !m_xWindow.is() )
146 throw DisposedException( OUString(), *this );
148 // TODO: the following is wrong, since it doesn't respect i_rParentAccessible. In a real extension, you should
149 // implement this correctly :)
150 (void)i_rParentAccessible;
151 return Reference< XAccessible >( getWindow(), UNO_QUERY );
154 //------------------------------------------------------------------------------------------------------------------
155 void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
159 const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
160 const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
161 xGraphics->setFillColor( m_nPanelColor );
162 xGraphics->setLineColor( 0x00FFFFFF );
164 const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
165 const Rectangle aWindowRect( xWindow->getPosSize() );
166 xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
168 catch( const Exception& )
173 //------------------------------------------------------------------------------------------------------------------
174 void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
176 (void)i_rSource;
179 //------------------------------------------------------------------------------------------------------------------
180 void SAL_CALL SingleColorPanel::disposing()
182 ::osl::MutexGuard aGuard( m_aMutex );
183 if ( !m_xWindow.is() )
184 // already disposed
185 return;
186 m_xWindow->removePaintListener( this );
189 Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
190 xWindowComp->dispose();
192 catch( const Exception& )
195 m_xWindow.clear();
198 //==================================================================================================================
199 //= PanelUIElement
200 //==================================================================================================================
201 //------------------------------------------------------------------------------------------------------------------
202 PanelUIElement::PanelUIElement( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow,
203 const OUString& i_rResourceURL, const ::sal_Int32 i_nPanelColor )
204 :PanelUIElement_Base( m_aMutex )
205 ,m_sResourceURL( i_rResourceURL )
206 ,m_xToolPanel( new SingleColorPanel( i_rContext, i_rParentWindow, i_nPanelColor ) )
210 //------------------------------------------------------------------------------------------------------------------
211 PanelUIElement::~PanelUIElement()
215 //------------------------------------------------------------------------------------------------------------------
216 Reference< XFrame > SAL_CALL PanelUIElement::getFrame() throw (RuntimeException)
218 // TODO
219 return NULL;
222 //------------------------------------------------------------------------------------------------------------------
223 OUString SAL_CALL PanelUIElement::getResourceURL() throw (RuntimeException)
225 return m_sResourceURL;
228 //------------------------------------------------------------------------------------------------------------------
229 ::sal_Int16 SAL_CALL PanelUIElement::getType() throw (RuntimeException)
231 return UIElementType::TOOLPANEL;
234 //------------------------------------------------------------------------------------------------------------------
235 Reference< XInterface > SAL_CALL PanelUIElement::getRealInterface( ) throw (RuntimeException)
237 ::osl::MutexGuard aGuard( m_aMutex );
238 if ( !m_xToolPanel.is() )
239 throw DisposedException();
240 return m_xToolPanel;
243 //------------------------------------------------------------------------------------------------------------------
244 void SAL_CALL PanelUIElement::disposing()
246 Reference< XComponent > xPanelComponent( m_xToolPanel, UNO_QUERY_THROW );
247 m_xToolPanel.clear();
248 xPanelComponent->dispose();
251 //......................................................................................................................
252 } } // namespace sd::colortoolpanel
253 //......................................................................................................................
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */