Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / odk / examples / cpp / custompanel / ctp_panel.cxx
bloba6f03e247e75184dc744c7604e1dc7bad2154334
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/WindowClass.hpp>
26 #include <com/sun/star/awt/WindowAttribute.hpp>
27 #include <com/sun/star/awt/PosSize.hpp>
28 #include <com/sun/star/awt/XDevice.hpp>
29 #include <com/sun/star/awt/XGraphics.hpp>
30 #include <com/sun/star/ui/UIElementType.hpp>
32 #include <osl/diagnose.h>
35 namespace sd { namespace colortoolpanel
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::uno::XInterface;
41 using ::com::sun::star::uno::UNO_QUERY;
42 using ::com::sun::star::uno::UNO_QUERY_THROW;
43 using ::com::sun::star::uno::UNO_SET_THROW;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::uno::RuntimeException;
46 using ::com::sun::star::uno::Type;
47 using ::com::sun::star::uno::XComponentContext;
48 using ::com::sun::star::awt::XWindow;
49 using ::com::sun::star::lang::DisposedException;
50 using ::com::sun::star::awt::XWindowPeer;
51 using ::com::sun::star::lang::XMultiComponentFactory;
52 using ::com::sun::star::awt::WindowDescriptor;
53 using ::com::sun::star::awt::WindowClass_SIMPLE;
54 using ::com::sun::star::awt::Rectangle;
55 using ::com::sun::star::awt::PaintEvent;
56 using ::com::sun::star::lang::EventObject;
57 using ::com::sun::star::awt::XDevice;
58 using ::com::sun::star::awt::XGraphics;
59 using ::com::sun::star::accessibility::XAccessible;
60 using ::com::sun::star::frame::XFrame;
62 namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
63 namespace PosSize = ::com::sun::star::awt::PosSize;
64 namespace UIElementType = ::com::sun::star::ui::UIElementType;
67 //= helpers
69 namespace
71 Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
72 const Reference< XWindowPeer >& i_rParentWindow )
74 try
76 OSL_ENSURE( i_rContext.is(), "illegal component context" );
77 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
78 Reference< XToolkit2 > xToolkit = Toolkit::create(i_rContext);
80 WindowDescriptor aWindow;
81 aWindow.Type = WindowClass_SIMPLE;
82 aWindow.WindowServiceName = "window";
83 aWindow.Parent = i_rParentWindow;
84 aWindow.WindowAttributes = WindowAttribute::BORDER;
86 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
87 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
89 catch( const Exception& )
92 return NULL;
96 SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow, const ::sal_Int32 i_nPanelColor )
97 :SingleColorPanel_Base( m_aMutex )
98 ,m_xWindow()
99 ,m_nPanelColor( i_nPanelColor )
101 // retrieve the parent window for our to-be-created pane window
102 Reference< XWindowPeer > xParentPeer( i_rParentWindow, UNO_QUERY );
104 osl_atomic_increment( &m_refCount );
105 if ( xParentPeer.is() )
107 m_xWindow = lcl_createPlainWindow_nothrow( i_rContext, xParentPeer );
108 m_xWindow->addPaintListener( this );
109 if ( m_xWindow.is() )
111 const Rectangle aPanelAnchorSize( i_rParentWindow->getPosSize() );
112 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
113 m_xWindow->setVisible( sal_True );
116 osl_atomic_decrement( &m_refCount );
120 SingleColorPanel::~SingleColorPanel()
125 Reference< XWindow > SAL_CALL SingleColorPanel::getWindow() throw (RuntimeException)
127 ::osl::MutexGuard aGuard( m_aMutex );
128 if ( !m_xWindow.is() )
129 throw DisposedException( OUString(), *this );
130 return m_xWindow;
134 Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
136 ::osl::MutexGuard aGuard( m_aMutex );
137 if ( !m_xWindow.is() )
138 throw DisposedException( OUString(), *this );
140 // TODO: the following is wrong, since it doesn't respect i_rParentAccessible. In a real extension, you should
141 // implement this correctly :)
142 (void)i_rParentAccessible;
143 return Reference< XAccessible >( getWindow(), UNO_QUERY );
147 void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
151 const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
152 const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
153 xGraphics->setFillColor( m_nPanelColor );
154 xGraphics->setLineColor( 0x00FFFFFF );
156 const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
157 const Rectangle aWindowRect( xWindow->getPosSize() );
158 xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
160 catch( const Exception& )
166 void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
168 (void)i_rSource;
172 void SAL_CALL SingleColorPanel::disposing()
174 ::osl::MutexGuard aGuard( m_aMutex );
175 if ( !m_xWindow.is() )
176 // already disposed
177 return;
178 m_xWindow->removePaintListener( this );
181 Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
182 xWindowComp->dispose();
184 catch( const Exception& )
187 m_xWindow.clear();
190 PanelUIElement::PanelUIElement( const Reference< XComponentContext >& i_rContext, const Reference< XWindow >& i_rParentWindow,
191 const OUString& i_rResourceURL, const ::sal_Int32 i_nPanelColor )
192 :PanelUIElement_Base( m_aMutex )
193 ,m_sResourceURL( i_rResourceURL )
194 ,m_xToolPanel( new SingleColorPanel( i_rContext, i_rParentWindow, i_nPanelColor ) )
199 PanelUIElement::~PanelUIElement()
204 Reference< XFrame > SAL_CALL PanelUIElement::getFrame() throw (RuntimeException)
206 // TODO
207 return NULL;
211 OUString SAL_CALL PanelUIElement::getResourceURL() throw (RuntimeException)
213 return m_sResourceURL;
217 ::sal_Int16 SAL_CALL PanelUIElement::getType() throw (RuntimeException)
219 return UIElementType::TOOLPANEL;
223 Reference< XInterface > SAL_CALL PanelUIElement::getRealInterface( ) throw (RuntimeException)
225 ::osl::MutexGuard aGuard( m_aMutex );
226 if ( !m_xToolPanel.is() )
227 throw DisposedException();
228 return m_xToolPanel;
232 void SAL_CALL PanelUIElement::disposing()
234 Reference< XComponent > xPanelComponent( m_xToolPanel, UNO_QUERY_THROW );
235 m_xToolPanel.clear();
236 xPanelComponent->dispose();
240 } } // namespace sd::colortoolpanel
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */