1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "PresenterButton.hxx"
#include "PresenterCanvasHelper.hxx"
#include "PresenterController.hxx"
#include "PresenterGeometryHelper.hxx"
#include "PresenterPaintManager.hxx"
#include "PresenterUIPainter.hxx"
#include <com/sun/star/awt/PosSize.hpp>
#include <com/sun/star/awt/XWindowPeer.hpp>
#include <com/sun/star/drawing/XPresenterHelper.hpp>
#include <com/sun/star/rendering/CompositeOperation.hpp>
#include <com/sun/star/rendering/TextDirection.hpp>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
namespace sdext::presenter {
const static double gnHorizontalBorder (15);
const static double gnVerticalBorder (5);
::rtl::Reference<PresenterButton> PresenterButton::Create (
const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
const ::rtl::Reference<PresenterController>& rpPresenterController,
const std::shared_ptr<PresenterTheme>& rpTheme,
const css::uno::Reference<css::awt::XWindow>& rxParentWindow,
const css::uno::Reference<css::rendering::XCanvas>& rxParentCanvas,
const OUString& rsConfigurationName)
{
Reference<beans::XPropertySet> xProperties (GetConfigurationProperties(
rxComponentContext,
rsConfigurationName));
if (xProperties.is())
{
OUString sText;
OUString sAction;
PresenterConfigurationAccess::GetProperty(xProperties, "Text") >>= sText;
PresenterConfigurationAccess::GetProperty(xProperties, "Action") >>= sAction;
PresenterTheme::SharedFontDescriptor pFont;
if (rpTheme != nullptr)
pFont = rpTheme->GetFont("ButtonFont");
PresenterTheme::SharedFontDescriptor pMouseOverFont;
if (rpTheme != nullptr)
pMouseOverFont = rpTheme->GetFont("ButtonMouseOverFont");
rtl::Reference<PresenterButton> pButton (
new PresenterButton(
rxComponentContext,
rpPresenterController,
rpTheme,
rxParentWindow,
pFont,
pMouseOverFont,
sText,
sAction));
pButton->SetCanvas(rxParentCanvas, rxParentWindow);
return pButton;
}
else
return nullptr;
}
PresenterButton::PresenterButton (
const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
const ::rtl::Reference<PresenterController>& rpPresenterController,
const std::shared_ptr<PresenterTheme>& rpTheme,
const css::uno::Reference<css::awt::XWindow>& rxParentWindow,
const PresenterTheme::SharedFontDescriptor& rpFont,
const PresenterTheme::SharedFontDescriptor& rpMouseOverFont,
const OUString& rsText,
const OUString& rsAction)
: PresenterButtonInterfaceBase(m_aMutex),
mpPresenterController(rpPresenterController),
mpTheme(rpTheme),
mxWindow(),
mxCanvas(),
mxPresenterHelper(),
msText(rsText),
mpFont(rpFont),
mpMouseOverFont(rpMouseOverFont),
msAction(rsAction),
maCenter(),
maButtonSize(-1,-1),
meState(PresenterBitmapDescriptor::Normal),
mxNormalBitmap(),
mxMouseOverBitmap()
{
try
{
Reference<lang::XMultiComponentFactory> xFactory (rxComponentContext->getServiceManager());
if ( ! xFactory.is())
throw RuntimeException();
mxPresenterHelper.set(
xFactory->createInstanceWithContext(
"com.sun.star.comp.Draw.PresenterHelper",
rxComponentContext),
UNO_QUERY_THROW);
if (mxPresenterHelper.is())
mxWindow = mxPresenterHelper->createWindow(rxParentWindow,
false,
false,
false,
false);
// Make the background transparent.
Reference<awt::XWindowPeer> xPeer (mxWindow, UNO_QUERY_THROW);
xPeer->setBackground(0xff000000);
mxWindow->setVisible(true);
mxWindow->addPaintListener(this);
mxWindow->addMouseListener(this);
}
catch (RuntimeException&)
{
}
}
PresenterButton::~PresenterButton()
{
}
void SAL_CALL PresenterButton::disposing()
{
if (mxCanvas.is())
{
Reference<lang::XComponent> xComponent (mxCanvas, UNO_QUERY);
mxCanvas = nullptr;
if (xComponent.is())
xComponent->dispose();
}
if (mxWindow.is())
{
mxWindow->removePaintListener(this);
mxWindow->removeMouseListener(this);
Reference<lang::XComponent> xComponent = mxWindow;
mxWindow = nullptr;
if (xComponent.is())
xComponent->dispose();
}
}
void PresenterButton::SetCenter (const css::geometry::RealPoint2D& rLocation)
{
if (mxCanvas.is())
{
Invalidate();
maCenter = rLocation;
mxWindow->setPosSize(
sal_Int32(0.5 + maCenter.X - maButtonSize.Width/2),
sal_Int32(0.5 + maCenter.Y - maButtonSize.Height/2),
maButtonSize.Width,
maButtonSize.Height,
awt::PosSize::POSSIZE);
Invalidate();
}
else
{
// The button can not be painted but we can at least store the new center.
maCenter = rLocation;
}
}
void PresenterButton::SetCanvas (
const css::uno::Reference<css::rendering::XCanvas>& rxParentCanvas,
const css::uno::Reference<css::awt::XWindow>& rxParentWindow)
{
if (mxCanvas.is())
{
Reference<lang::XComponent> xComponent (mxCanvas, UNO_QUERY);
mxCanvas = nullptr;
if (xComponent.is())
xComponent->dispose();
}
if (!(mxPresenterHelper.is() && rxParentCanvas.is() && rxParentWindow.is()))
return;
mxCanvas = mxPresenterHelper->createSharedCanvas (
Reference<rendering::XSpriteCanvas>(rxParentCanvas, UNO_QUERY),
rxParentWindow,
rxParentCanvas,
rxParentWindow,
mxWindow);
if (mxCanvas.is())
{
SetupButtonBitmaps();
SetCenter(maCenter);
}
}
css::geometry::IntegerSize2D const & PresenterButton::GetSize()
{
if (maButtonSize.Width < 0)
CalculateButtonSize();
return maButtonSize;
}
//----- XPaintListener --------------------------------------------------------
void SAL_CALL PresenterButton::windowPaint (const css::awt::PaintEvent& rEvent)
{
ThrowIfDisposed();
if (!(mxWindow.is() && mxCanvas.is()))
return;
Reference<rendering::XBitmap> xBitmap;
if (meState == PresenterBitmapDescriptor::MouseOver)
xBitmap = mxMouseOverBitmap;
else
xBitmap = mxNormalBitmap;
if ( ! xBitmap.is())
return;
rendering::ViewState aViewState(
geometry::AffineMatrix2D(1,0,0, 0,1,0),
nullptr);
rendering::RenderState aRenderState(
geometry::AffineMatrix2D(1,0,0, 0,1,0),
PresenterGeometryHelper::CreatePolygon(rEvent.UpdateRect, mxCanvas->getDevice()),
Sequence<double>(4),
rendering::CompositeOperation::SOURCE);
mxCanvas->drawBitmap(xBitmap, aViewState, aRenderState);
Reference<rendering::XSpriteCanvas> xSpriteCanvas (mxCanvas, UNO_QUERY);
if (xSpriteCanvas.is())
xSpriteCanvas->updateScreen(false);
}
//----- XMouseListener --------------------------------------------------------
void SAL_CALL PresenterButton::mousePressed (const css::awt::MouseEvent&)
{
ThrowIfDisposed();
meState = PresenterBitmapDescriptor::ButtonDown;
}
void SAL_CALL PresenterButton::mouseReleased (const css::awt::MouseEvent&)
{
ThrowIfDisposed();
if (meState == PresenterBitmapDescriptor::ButtonDown)
{
OSL_ASSERT(mpPresenterController.get()!=nullptr);
mpPresenterController->DispatchUnoCommand(msAction);
meState = PresenterBitmapDescriptor::Normal;
Invalidate();
}
}
void SAL_CALL PresenterButton::mouseEntered (const css::awt::MouseEvent&)
{
ThrowIfDisposed();
meState = PresenterBitmapDescriptor::MouseOver;
Invalidate();
}
void SAL_CALL PresenterButton::mouseExited (const css::awt::MouseEvent&)
{
ThrowIfDisposed();
meState = PresenterBitmapDescriptor::Normal;
Invalidate();
}
//----- lang::XEventListener --------------------------------------------------
void SAL_CALL PresenterButton::disposing (const css::lang::EventObject& rEvent)
{
if (rEvent.Source == mxWindow)
mxWindow = nullptr;
}
css::geometry::IntegerSize2D PresenterButton::CalculateButtonSize()
{
if (mpFont.get()!=nullptr && !mpFont->mxFont.is() && mxCanvas.is())
mpFont->PrepareFont(mxCanvas);
if (mpFont.get()==nullptr || !mpFont->mxFont.is())
return geometry::IntegerSize2D(-1,-1);
geometry::RealSize2D aTextSize (PresenterCanvasHelper::GetTextSize(mpFont->mxFont,msText));
return geometry::IntegerSize2D (
sal_Int32(0.5 + aTextSize.Width + 2*gnHorizontalBorder),
sal_Int32(0.5 + aTextSize.Height + 2*gnVerticalBorder));
}
void PresenterButton::RenderButton (
const Reference<rendering::XCanvas>& rxCanvas,
const geometry::IntegerSize2D& rSize,
const PresenterTheme::SharedFontDescriptor& rpFont,
const PresenterBitmapDescriptor::Mode eMode,
const SharedBitmapDescriptor& rpLeft,
const SharedBitmapDescriptor& rpCenter,
const SharedBitmapDescriptor& rpRight)
{
if ( ! rxCanvas.is())
return;
const awt::Rectangle aBox(0,0, rSize.Width, rSize.Height);
PresenterUIPainter::PaintHorizontalBitmapComposite (
rxCanvas,
aBox,
aBox,
GetBitmap(rpLeft, eMode),
GetBitmap(rpCenter, eMode),
GetBitmap(rpRight, eMode));
if (rpFont.get()==nullptr || ! rpFont->mxFont.is())
return;
const rendering::StringContext aContext (msText, 0, msText.getLength());
const Reference<rendering::XTextLayout> xLayout (
rpFont->mxFont->createTextLayout(aContext,rendering::TextDirection::WEAK_LEFT_TO_RIGHT,0));
const geometry::RealRectangle2D aTextBBox (xLayout->queryTextBounds());
rendering::RenderState aRenderState (geometry::AffineMatrix2D(1,0,0, 0,1,0), nullptr,
Sequence<double>(4), rendering::CompositeOperation::SOURCE);
PresenterCanvasHelper::SetDeviceColor(aRenderState, rpFont->mnColor);
aRenderState.AffineTransform.m02 = (rSize.Width - aTextBBox.X2 + aTextBBox.X1)/2;
aRenderState.AffineTransform.m12 = (rSize.Height - aTextBBox.Y2 + aTextBBox.Y1)/2 - aTextBBox.Y1;
/// this is responsible of the close button
rxCanvas->drawTextLayout(
xLayout,
rendering::ViewState(geometry::AffineMatrix2D(1,0,0, 0,1,0), nullptr),
aRenderState);
}
void PresenterButton::Invalidate()
{
mpPresenterController->GetPaintManager()->Invalidate(mxWindow);
}
Reference<rendering::XBitmap> PresenterButton::GetBitmap (
const SharedBitmapDescriptor& mpIcon,
const PresenterBitmapDescriptor::Mode eMode)
{
if (mpIcon.get() != nullptr)
return mpIcon->GetBitmap(eMode);
else
{
OSL_ASSERT(mpIcon.get()!=nullptr);
return nullptr;
}
}
void PresenterButton::SetupButtonBitmaps()
{
if ( ! mxCanvas.is())
return;
if ( ! mxCanvas->getDevice().is())
return;
// Get the bitmaps for the button border.
SharedBitmapDescriptor pLeftBitmap (mpTheme->GetBitmap("ButtonFrameLeft"));
SharedBitmapDescriptor pCenterBitmap(mpTheme->GetBitmap("ButtonFrameCenter"));
SharedBitmapDescriptor pRightBitmap(mpTheme->GetBitmap("ButtonFrameRight"));
maButtonSize = CalculateButtonSize();
if (maButtonSize.Height<=0 && maButtonSize.Width<= 0)
return;
mxNormalBitmap = mxCanvas->getDevice()->createCompatibleAlphaBitmap(maButtonSize);
Reference<rendering::XCanvas> xCanvas (mxNormalBitmap, UNO_QUERY);
if (xCanvas.is())
RenderButton(
xCanvas,
maButtonSize,
mpFont,
PresenterBitmapDescriptor::Normal,
pLeftBitmap,
pCenterBitmap,
pRightBitmap);
mxMouseOverBitmap = mxCanvas->getDevice()->createCompatibleAlphaBitmap(maButtonSize);
xCanvas.set(mxMouseOverBitmap, UNO_QUERY);
if (mpMouseOverFont.get()!=nullptr && !mpMouseOverFont->mxFont.is() && mxCanvas.is())
mpMouseOverFont->PrepareFont(mxCanvas);
if (xCanvas.is())
RenderButton(
xCanvas,
maButtonSize,
mpMouseOverFont,
PresenterBitmapDescriptor::MouseOver,
pLeftBitmap,
pCenterBitmap,
pRightBitmap);
}
Reference<beans::XPropertySet> PresenterButton::GetConfigurationProperties (
const css::uno::Reference<css::uno::XComponentContext>& rxComponentContext,
const OUString& rsConfgurationName)
{
PresenterConfigurationAccess aConfiguration (
rxComponentContext,
PresenterConfigurationAccess::msPresenterScreenRootName,
PresenterConfigurationAccess::READ_ONLY);
return Reference<beans::XPropertySet>(
PresenterConfigurationAccess::Find (
Reference<container::XNameAccess>(
aConfiguration.GetConfigurationNode("PresenterScreenSettings/Buttons"),
UNO_QUERY),
[&rsConfgurationName](OUString const&, uno::Reference<beans::XPropertySet> const& xProps) -> bool
{
return PresenterConfigurationAccess::IsStringPropertyEqual(
rsConfgurationName, "Name", xProps);
}),
UNO_QUERY);
}
void PresenterButton::ThrowIfDisposed() const
{
if (rBHelper.bDisposed || rBHelper.bInDispose)
{
throw lang::DisposedException (
"PresenterButton object has already been disposed",
const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
}
}
} // end of namespace sdext::presenter
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|