Branch libreoffice-5-0-4
[LibreOffice.git] / sd / workben / custompanel / ctp_panel.cxx
blob9cc951de9c61bf57ca5897d6c05c34a86ea018c2
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 .
20 #include "ctp_panel.hxx"
22 #include <com/sun/star/drawing/framework/XPane.hpp>
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>
32 #include <tools/diagnose_ex.h>
34 namespace sd { namespace colortoolpanel
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::XInterface;
39 using ::com::sun::star::uno::UNO_QUERY;
40 using ::com::sun::star::uno::UNO_QUERY_THROW;
41 using ::com::sun::star::uno::UNO_SET_THROW;
42 using ::com::sun::star::uno::Exception;
43 using ::com::sun::star::uno::RuntimeException;
44 using ::com::sun::star::uno::Any;
45 using ::com::sun::star::uno::makeAny;
46 using ::com::sun::star::uno::Sequence;
47 using ::com::sun::star::uno::Type;
48 using ::com::sun::star::drawing::framework::XConfigurationController;
49 using ::com::sun::star::drawing::framework::XResourceId;
50 using ::com::sun::star::uno::XComponentContext;
51 using ::com::sun::star::drawing::framework::XPane;
52 using ::com::sun::star::awt::XWindow;
53 using ::com::sun::star::rendering::XCanvas;
54 using ::com::sun::star::lang::DisposedException;
55 using ::com::sun::star::awt::XWindowPeer;
56 using ::com::sun::star::lang::XMultiComponentFactory;
57 using ::com::sun::star::awt::XToolkit;
58 using ::com::sun::star::awt::WindowDescriptor;
59 using ::com::sun::star::awt::WindowClass_SIMPLE;
60 using ::com::sun::star::awt::Rectangle;
61 using ::com::sun::star::awt::PaintEvent;
62 using ::com::sun::star::lang::EventObject;
63 using ::com::sun::star::awt::XDevice;
64 using ::com::sun::star::awt::XGraphics;
65 using ::com::sun::star::accessibility::XAccessible;
67 namespace WindowAttribute = ::com::sun::star::awt::WindowAttribute;
68 namespace PosSize = ::com::sun::star::awt::PosSize;
70 //= helpers
72 namespace
74 Reference< XWindow > lcl_createPlainWindow_nothrow( const Reference< XComponentContext >& i_rContext,
75 const Reference< XWindowPeer >& i_rParentWindow )
77 try
79 ENSURE_OR_THROW( i_rContext.is(), "illegal component context" );
80 Reference< XMultiComponentFactory > xFactory( i_rContext->getServiceManager(), UNO_SET_THROW );
81 Reference< XToolkit2 > xToolkit = Toolkit::create(i_rContext);
83 WindowDescriptor aWindow;
84 aWindow.Type = WindowClass_SIMPLE;
85 aWindow.WindowServiceName = "window";
86 aWindow.Parent = i_rParentWindow;
87 aWindow.WindowAttributes = WindowAttribute::BORDER;
89 Reference< XWindowPeer > xWindow( xToolkit->createWindow( aWindow ), UNO_SET_THROW );
90 return Reference< XWindow >( xWindow, UNO_QUERY_THROW );
92 catch( const Exception& )
94 DBG_UNHANDLED_EXCEPTION();
96 return NULL;
100 //= class SingleColorPanel
102 SingleColorPanel::SingleColorPanel( const Reference< XComponentContext >& i_rContext,
103 const Reference< XConfigurationController >& i_rConfigController, const Reference< XResourceId >& i_rResourceId )
104 :SingleColorPanel_Base( m_aMutex )
105 ,m_xContext( i_rContext )
106 ,m_xResourceId( i_rResourceId )
107 ,m_xWindow()
109 ENSURE_OR_THROW( i_rConfigController.is(), "invalid configuration controller" );
110 ENSURE_OR_THROW( m_xResourceId.is(), "invalid resource id" );
112 // retrieve the parent window for our to-be-created pane window
113 Reference< XWindow > xParentWindow;
114 Reference< XWindowPeer > xParentPeer;
117 Reference< XResource > xAnchor( i_rConfigController->getResource( m_xResourceId->getAnchor() ), UNO_SET_THROW );
118 Reference< XPane > xAnchorPane( xAnchor, UNO_QUERY_THROW );
119 xParentWindow.set( xAnchorPane->getWindow(), UNO_SET_THROW );
120 xParentPeer.set( xParentWindow, UNO_QUERY_THROW );
122 catch( const Exception& )
124 DBG_UNHANDLED_EXCEPTION();
126 osl_atomic_increment( &m_refCount );
127 if ( xParentWindow.is() )
129 m_xWindow = lcl_createPlainWindow_nothrow( m_xContext, xParentPeer );
130 m_xWindow->addPaintListener( this );
131 if ( m_xWindow.is() )
133 const Rectangle aPanelAnchorSize( xParentWindow->getPosSize() );
134 m_xWindow->setPosSize( 0, 0, aPanelAnchorSize.Width, aPanelAnchorSize.Height, PosSize::POSSIZE );
135 m_xWindow->setVisible( sal_True );
138 osl_atomic_decrement( &m_refCount );
141 SingleColorPanel::~SingleColorPanel()
145 Reference< XWindow > SAL_CALL SingleColorPanel::getWindow( ) throw (RuntimeException)
147 ::osl::MutexGuard aGuard( m_aMutex );
148 if ( !m_xWindow.get() )
149 throw DisposedException( OUString(), *this );
150 return m_xWindow;
153 Reference< XAccessible > SAL_CALL SingleColorPanel::createAccessible( const Reference< XAccessible >& i_rParentAccessible ) throw (RuntimeException)
155 (void)i_rParentAccessible;
156 return Reference< XAccessible >( m_xWindow, UNO_QUERY );
157 // TODO: this is, strictly, not correct, as we ignore i_ParentAccessible here. If you are not doing a sample
158 // extension only, you'll want to do this correctly ....
161 Reference< XResourceId > SAL_CALL SingleColorPanel::getResourceId( ) throw (RuntimeException)
163 ::osl::MutexGuard aGuard( m_aMutex );
164 if ( !m_xWindow.is() )
165 throw DisposedException( OUString(), *this );
166 return m_xResourceId;
169 sal_Bool SAL_CALL SingleColorPanel::isAnchorOnly( ) throw (RuntimeException)
171 ::osl::MutexGuard aGuard( m_aMutex );
172 if ( !m_xWindow.is() )
173 throw DisposedException( OUString(), *this );
174 return sal_False;
177 void SAL_CALL SingleColorPanel::windowPaint( const PaintEvent& i_rEvent ) throw (RuntimeException)
181 const Reference< XDevice > xDevice( i_rEvent.Source, UNO_QUERY_THROW );
182 const Reference< XGraphics > xGraphics( xDevice->createGraphics(), UNO_SET_THROW );
183 xGraphics->setFillColor( 0x80 << 8 );
184 xGraphics->setLineColor( 0x80 << 16 );
186 const Reference< XWindow > xWindow( i_rEvent.Source, UNO_QUERY_THROW );
187 const Rectangle aWindowRect( xWindow->getPosSize() );
188 xGraphics->drawRect( 0, 0, aWindowRect.Width - 1, aWindowRect.Height - 1 );
190 catch( const Exception& )
192 DBG_UNHANDLED_EXCEPTION();
196 void SAL_CALL SingleColorPanel::disposing( const EventObject& i_rSource ) throw (RuntimeException)
198 (void)i_rSource;
201 void SAL_CALL SingleColorPanel::disposing()
203 ::osl::MutexGuard aGuard( m_aMutex );
204 if ( !m_xWindow.is() )
205 // already disposed
206 return;
207 m_xWindow->removePaintListener( this );
210 Reference< XComponent > xWindowComp( m_xWindow, UNO_QUERY_THROW );
211 xWindowComp->dispose();
213 catch( const Exception& )
215 DBG_UNHANDLED_EXCEPTION();
217 m_xWindow.clear();
220 } } // namespace sd::colortoolpanel
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */