Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / cpp / custompanel / ctp_panel.cxx
blobaa1b4ef403a657eb65cab86a4a72cadc12fb7d2a
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>
36 namespace sd { namespace colortoolpanel
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;
72 //= helpers
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 = "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;
101 SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow, const ::sal_Int32 i_nPanelColor )
102 :SingleColorPanel_Base( m_aMutex )
103 ,m_xWindow()
104 ,m_nPanelColor( i_nPanelColor )
106 // retrieve the parent window for our to-be-created pane window
107 Reference< XWindowPeer > xParentPeer( i_rParentWindow, UNO_QUERY );
109 osl_atomic_increment( &m_refCount );
110 if ( xParentPeer.is() )
112 m_xWindow = lcl_createPlainWindow_nothrow( i_rContext, xParentPeer );
113 m_xWindow->addPaintListener( this );
114 if ( m_xWindow.is() )
116 const Rectangle aPanelAnchorSize( i_rParentWindow->getPosSize() );
117 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
118 m_xWindow->setVisible( sal_True );
121 osl_atomic_decrement( &m_refCount );
125 SingleColorPanel::~SingleColorPanel()
130 Reference< XWindow > SAL_CALL SingleColorPanel::getWindow() throw (RuntimeException)
132 ::osl::MutexGuard aGuard( m_aMutex );
133 if ( !m_xWindow.is() )
134 throw DisposedException( OUString(), *this );
135 return m_xWindow;
139 Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
141 ::osl::MutexGuard aGuard( m_aMutex );
142 if ( !m_xWindow.is() )
143 throw DisposedException( OUString(), *this );
145 // TODO: the following is wrong, since it doesn't respect i_rParentAccessible. In a real extension, you should
146 // implement this correctly :)
147 (void)i_rParentAccessible;
148 return Reference< XAccessible >( getWindow(), UNO_QUERY );
152 void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
156 const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
157 const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
158 xGraphics->setFillColor( m_nPanelColor );
159 xGraphics->setLineColor( 0x00FFFFFF );
161 const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
162 const Rectangle aWindowRect( xWindow->getPosSize() );
163 xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
165 catch( const Exception& )
171 void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
173 (void)i_rSource;
177 void SAL_CALL SingleColorPanel::disposing()
179 ::osl::MutexGuard aGuard( m_aMutex );
180 if ( !m_xWindow.is() )
181 // already disposed
182 return;
183 m_xWindow->removePaintListener( this );
186 Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
187 xWindowComp->dispose();
189 catch( const Exception& )
192 m_xWindow.clear();
195 PanelUIElement::PanelUIElement( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow,
196 const OUString& i_rResourceURL, const ::sal_Int32 i_nPanelColor )
197 :PanelUIElement_Base( m_aMutex )
198 ,m_sResourceURL( i_rResourceURL )
199 ,m_xToolPanel( new SingleColorPanel( i_rContext, i_rParentWindow, i_nPanelColor ) )
204 PanelUIElement::~PanelUIElement()
209 Reference< XFrame > SAL_CALL PanelUIElement::getFrame() throw (RuntimeException)
211 // TODO
212 return NULL;
216 OUString SAL_CALL PanelUIElement::getResourceURL() throw (RuntimeException)
218 return m_sResourceURL;
222 ::sal_Int16 SAL_CALL PanelUIElement::getType() throw (RuntimeException)
224 return UIElementType::TOOLPANEL;
228 Reference< XInterface > SAL_CALL PanelUIElement::getRealInterface( ) throw (RuntimeException)
230 ::osl::MutexGuard aGuard( m_aMutex );
231 if ( !m_xToolPanel.is() )
232 throw DisposedException();
233 return m_xToolPanel;
237 void SAL_CALL PanelUIElement::disposing()
239 Reference< XComponent > xPanelComponent( m_xToolPanel, UNO_QUERY_THROW );
240 m_xToolPanel.clear();
241 xPanelComponent->dispose();
245 } } // namespace sd::colortoolpanel
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */