1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/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>
31 #include <comphelper/diagnose_ex.hxx>
33 namespace sd::colortoolpanel
36 using ::com::sun::star::uno::Reference
;
37 using ::com::sun::star::uno::UNO_QUERY
;
38 using ::com::sun::star::uno::UNO_QUERY_THROW
;
39 using ::com::sun::star::uno::UNO_SET_THROW
;
40 using ::com::sun::star::uno::Exception
;
41 using ::com::sun::star::uno::RuntimeException
;
42 using ::com::sun::star::uno::Type
;
43 using ::com::sun::star::drawing::framework::XConfigurationController
;
44 using ::com::sun::star::drawing::framework::XResourceId
;
45 using ::com::sun::star::uno::XComponentContext
;
46 using ::com::sun::star::drawing::framework::XPane
;
47 using ::com::sun::star::awt::XWindow
;
48 using ::com::sun::star::lang::DisposedException
;
49 using ::com::sun::star::awt::XWindowPeer
;
50 using ::com::sun::star::lang::XMultiComponentFactory
;
51 using ::com::sun::star::awt::WindowDescriptor
;
52 using ::com::sun::star::awt::WindowClass_SIMPLE
;
53 using ::com::sun::star::awt::Rectangle
;
54 using ::com::sun::star::awt::PaintEvent
;
55 using ::com::sun::star::lang::EventObject
;
56 using ::com::sun::star::awt::XDevice
;
57 using ::com::sun::star::awt::XGraphics
;
58 using ::com::sun::star::accessibility::XAccessible
;
60 namespace WindowAttribute
= ::com::sun::star::awt::WindowAttribute
;
61 namespace PosSize
= ::com::sun::star::awt::PosSize
;
67 Reference
< XWindow
> lcl_createPlainWindow_nothrow( const Reference
< XComponentContext
>& i_rContext
,
68 const Reference
< XWindowPeer
>& i_rParentWindow
)
72 ENSURE_OR_THROW( i_rContext
.is(), "illegal component context" );
73 Reference
< XMultiComponentFactory
> xFactory( i_rContext
->getServiceManager(), UNO_SET_THROW
);
74 Reference
< XToolkit2
> xToolkit
= Toolkit::create(i_rContext
);
76 WindowDescriptor aWindow
;
77 aWindow
.Type
= WindowClass_SIMPLE
;
78 aWindow
.WindowServiceName
= "window";
79 aWindow
.Parent
= i_rParentWindow
;
80 aWindow
.WindowAttributes
= WindowAttribute::BORDER
;
82 Reference
< XWindowPeer
> xWindow( xToolkit
->createWindow( aWindow
), UNO_SET_THROW
);
83 return Reference
< XWindow
>( xWindow
, UNO_QUERY_THROW
);
85 catch( const Exception
& )
87 DBG_UNHANDLED_EXCEPTION("sd");
93 //= class SingleColorPanel
95 SingleColorPanel::SingleColorPanel( const Reference
< XComponentContext
>& i_rContext
,
96 const Reference
< XConfigurationController
>& i_rConfigController
, const Reference
< XResourceId
>& i_rResourceId
)
97 :SingleColorPanel_Base( m_aMutex
)
98 ,m_xContext( i_rContext
)
99 ,m_xResourceId( i_rResourceId
)
102 ENSURE_OR_THROW( i_rConfigController
.is(), "invalid configuration controller" );
103 ENSURE_OR_THROW( m_xResourceId
.is(), "invalid resource id" );
105 // retrieve the parent window for our to-be-created pane window
106 Reference
< XWindow
> xParentWindow
;
107 Reference
< XWindowPeer
> xParentPeer
;
110 Reference
< XResource
> xAnchor( i_rConfigController
->getResource( m_xResourceId
->getAnchor() ), UNO_SET_THROW
);
111 Reference
< XPane
> xAnchorPane( xAnchor
, UNO_QUERY_THROW
);
112 xParentWindow
.set( xAnchorPane
->getWindow(), UNO_SET_THROW
);
113 xParentPeer
.set( xParentWindow
, UNO_QUERY_THROW
);
115 catch( const Exception
& )
117 DBG_UNHANDLED_EXCEPTION("sd");
119 osl_atomic_increment( &m_refCount
);
120 if ( xParentWindow
.is() )
122 m_xWindow
= lcl_createPlainWindow_nothrow( m_xContext
, xParentPeer
);
123 m_xWindow
->addPaintListener( this );
124 if ( m_xWindow
.is() )
126 const Rectangle
aPanelAnchorSize( xParentWindow
->getPosSize() );
127 m_xWindow
->setPosSize( 0, 0, aPanelAnchorSize
.Width
, aPanelAnchorSize
.Height
, PosSize::POSSIZE
);
128 m_xWindow
->setVisible( sal_True
);
131 osl_atomic_decrement( &m_refCount
);
134 SingleColorPanel::~SingleColorPanel()
138 Reference
< XWindow
> SAL_CALL
SingleColorPanel::getWindow( ) throw (RuntimeException
)
140 ::osl::MutexGuard
aGuard( m_aMutex
);
141 if ( !m_xWindow
.get() )
142 throw DisposedException( OUString(), *this );
146 Reference
< XAccessible
> SAL_CALL
SingleColorPanel::createAccessible( const Reference
< XAccessible
>& i_rParentAccessible
) throw (RuntimeException
)
148 (void)i_rParentAccessible
;
149 return Reference
< XAccessible
>( m_xWindow
, UNO_QUERY
);
150 // TODO: this is, strictly, not correct, as we ignore i_ParentAccessible here. If you are not doing a sample
151 // extension only, you'll want to do this correctly...
154 Reference
< XResourceId
> SAL_CALL
SingleColorPanel::getResourceId( ) throw (RuntimeException
)
156 ::osl::MutexGuard
aGuard( m_aMutex
);
157 if ( !m_xWindow
.is() )
158 throw DisposedException( OUString(), *this );
159 return m_xResourceId
;
162 sal_Bool SAL_CALL
SingleColorPanel::isAnchorOnly( ) throw (RuntimeException
)
164 ::osl::MutexGuard
aGuard( m_aMutex
);
165 if ( !m_xWindow
.is() )
166 throw DisposedException( OUString(), *this );
170 void SAL_CALL
SingleColorPanel::windowPaint( const PaintEvent
& i_rEvent
) throw (RuntimeException
)
174 const Reference
< XDevice
> xDevice( i_rEvent
.Source
, UNO_QUERY_THROW
);
175 const Reference
< XGraphics
> xGraphics( xDevice
->createGraphics(), UNO_SET_THROW
);
176 xGraphics
->setFillColor( 0x80 << 8 );
177 xGraphics
->setLineColor( 0x80 << 16 );
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 );
183 catch( const Exception
& )
185 DBG_UNHANDLED_EXCEPTION("sd");
189 void SAL_CALL
SingleColorPanel::disposing( const EventObject
& i_rSource
) throw (RuntimeException
)
194 void SAL_CALL
SingleColorPanel::disposing()
196 ::osl::MutexGuard
aGuard( m_aMutex
);
197 if ( !m_xWindow
.is() )
200 m_xWindow
->removePaintListener( this );
203 Reference
< XComponent
> xWindowComp( m_xWindow
, UNO_QUERY_THROW
);
204 xWindowComp
->dispose();
206 catch( const Exception
& )
208 DBG_UNHANDLED_EXCEPTION("sd");
213 } } // namespace sd::colortoolpanel
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */