bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / presenter / PresenterTextView.cxx
blobc8d9c364fd054ae2ce7c441d8a6b52adf409a2c4
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 "PresenterTextView.hxx"
21 #include "facreg.hxx"
23 #include <i18nlangtag/mslangid.hxx>
24 #include <cppcanvas/vclfactory.hxx>
25 #include <svl/itempool.hxx>
26 #include <svl/itemset.hxx>
27 #include <unotools/linguprops.hxx>
28 #include <unotools/lingucfg.hxx>
29 #include <editeng/colritem.hxx>
30 #include <editeng/editeng.hxx>
31 #include <editeng/editstat.hxx>
32 #include <editeng/eeitem.hxx>
33 #include <editeng/fhgtitem.hxx>
34 #include <editeng/fontitem.hxx>
35 #include <svx/xflclit.hxx>
36 #include <vcl/bitmapex.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/virdev.hxx>
39 #include <com/sun/star/awt/FontDescriptor.hpp>
40 #include <com/sun/star/awt/Size.hpp>
41 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
42 #include <com/sun/star/rendering/XBitmapCanvas.hpp>
43 #include <com/sun/star/uno/XComponentContext.hpp>
44 #include <com/sun/star/util/Color.hpp>
45 #include <com/sun/star/i18n/ScriptType.hpp>
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
51 namespace sd { namespace presenter {
53 // PresenterTextView::Implementation
54 class PresenterTextView::Implementation
56 public:
57 const OUString msTextPropertyName;
58 const OUString msBitmapPropertyName;
59 const OUString msSizePropertyName;
60 const OUString msBackgroundColorPropertyName;
61 const OUString msTextColorPropertyName;
62 const OUString msFontDescriptorPropertyName;
63 const OUString msTopPropertyName;
64 const OUString msTopRelativePropertyName;
65 const OUString msTotalHeightPropertyName;
67 Implementation();
68 ~Implementation();
70 void SetCanvas (const cppcanvas::CanvasSharedPtr& rCanvas);
71 void SetSize (const Size aSize);
72 void SetBackgroundColor (const Color aColor);
73 void SetTextColor (const Color aColor);
74 void SetFontDescriptor (const awt::FontDescriptor& rFontDescriptor);
75 sal_Int32 GetTop() const { return mnTop;}
76 void SetTop (const sal_Int32 nTop);
77 void SetText (const OUString& Text);
78 sal_Int32 ParseDistance (const OUString& rsDistance) const;
79 Reference<rendering::XBitmap> GetBitmap();
80 sal_Int32 GetTotalHeight();
82 private:
83 Reference<rendering::XBitmap> mxBitmap;
84 cppcanvas::CanvasSharedPtr mpCanvas;
85 VclPtr<VirtualDevice> mpOutputDevice;
86 EditEngine* mpEditEngine;
87 SfxItemPool* mpEditEngineItemPool;
88 Size maSize;
89 Color maBackgroundColor;
90 Color maTextColor;
91 OUString msText;
92 sal_Int32 mnTop;
93 sal_Int32 mnTotalHeight;
95 EditEngine * GetEditEngine();
96 EditEngine* CreateEditEngine();
97 void CheckTop();
100 // PresenterTextView
101 PresenterTextView::PresenterTextView (const Reference<XComponentContext>& rxContext)
102 : PresenterTextViewInterfaceBase(),
103 mpImplementation(new Implementation())
105 (void)rxContext;
108 PresenterTextView::~PresenterTextView()
112 void SAL_CALL PresenterTextView::disposing()
114 mpImplementation.reset();
117 // XInitialization
118 void SAL_CALL PresenterTextView::initialize (const Sequence<Any>& rArguments)
119 throw (Exception, RuntimeException, std::exception)
121 ThrowIfDisposed();
123 if (rArguments.getLength() == 1)
127 Reference<rendering::XCanvas> xCanvas (rArguments[0], UNO_QUERY_THROW);
128 if (xCanvas.is())
130 mpImplementation->SetCanvas(
131 cppcanvas::VCLFactory::createCanvas(xCanvas));
134 catch (RuntimeException&)
136 throw;
139 else
141 throw RuntimeException("PresenterTextView: invalid number of arguments",
142 static_cast<XWeak*>(this));
146 Any PresenterTextView::GetPropertyValue (const OUString& rsPropertyName)
148 ThrowIfDisposed();
150 if (rsPropertyName == mpImplementation->msBitmapPropertyName)
152 return Any(mpImplementation->GetBitmap());
154 else if (rsPropertyName == mpImplementation->msTopPropertyName)
156 return Any(mpImplementation->GetTop());
158 else if (rsPropertyName == mpImplementation->msTotalHeightPropertyName)
160 return Any(mpImplementation->GetTotalHeight());
163 return Any();
166 Any PresenterTextView::SetPropertyValue (
167 const OUString& rsPropertyName,
168 const css::uno::Any& rValue)
170 ThrowIfDisposed();
172 Any aOldValue;
173 if (rsPropertyName == mpImplementation->msTextPropertyName)
175 OUString sText;
176 if (rValue >>= sText)
177 mpImplementation->SetText(sText);
179 else if (rsPropertyName == mpImplementation->msSizePropertyName)
181 awt::Size aSize;
182 if (rValue >>= aSize)
183 mpImplementation->SetSize(Size(aSize.Width,aSize.Height));
185 else if (rsPropertyName == mpImplementation->msBackgroundColorPropertyName)
187 util::Color aColor = util::Color();
188 if (rValue >>= aColor)
189 mpImplementation->SetBackgroundColor(Color(aColor));
191 else if (rsPropertyName == mpImplementation->msTextColorPropertyName)
193 util::Color aColor = util::Color();
194 if (rValue >>= aColor)
195 mpImplementation->SetTextColor(Color(aColor));
197 else if (rsPropertyName == mpImplementation->msFontDescriptorPropertyName)
199 awt::FontDescriptor aFontDescriptor;
200 if (rValue >>= aFontDescriptor)
201 mpImplementation->SetFontDescriptor(aFontDescriptor);
203 else if (rsPropertyName == mpImplementation->msTopPropertyName)
205 sal_Int32 nTop = 0;
206 if (rValue >>= nTop)
207 mpImplementation->SetTop(nTop);
209 else if (rsPropertyName == mpImplementation->msTopRelativePropertyName)
211 OUString sDistance;
212 if (rValue >>= sDistance)
213 mpImplementation->SetTop(
214 mpImplementation->GetTop()
215 + mpImplementation->ParseDistance(sDistance));
217 return aOldValue;
220 void PresenterTextView::ThrowIfDisposed()
221 throw (::com::sun::star::lang::DisposedException)
223 if (PresenterTextViewInterfaceBase::rBHelper.bDisposed
224 || PresenterTextViewInterfaceBase::rBHelper.bInDispose
225 || mpImplementation.get()==NULL)
227 throw lang::DisposedException ("PresenterTextView object has already been disposed",
228 static_cast<uno::XWeak*>(this));
232 // PresenterTextView::Implementation
233 PresenterTextView::Implementation::Implementation()
234 : msTextPropertyName("Text"),
235 msBitmapPropertyName("Bitmap"),
236 msSizePropertyName("Size"),
237 msBackgroundColorPropertyName("BackgroundColor"),
238 msTextColorPropertyName("TextColor"),
239 msFontDescriptorPropertyName("FontDescriptor"),
240 msTopPropertyName("Top"),
241 msTopRelativePropertyName("RelativeTop"),
242 msTotalHeightPropertyName("TotalHeight"),
243 mxBitmap(),
244 mpCanvas(),
245 mpOutputDevice(VclPtr<VirtualDevice>::Create(*Application::GetDefaultDevice(), 0, 0)),
246 mpEditEngine(NULL),
247 mpEditEngineItemPool(EditEngine::CreatePool()),
248 maSize(100,100),
249 maBackgroundColor(0xffffffff),
250 maTextColor(0x00000000),
251 msText(),
252 mnTop(0),
253 mnTotalHeight(-1)
255 mpOutputDevice->SetMapMode(MAP_PIXEL);
257 GetEditEngine();
260 PresenterTextView::Implementation::~Implementation()
262 delete mpEditEngine;
263 SfxItemPool::Free(mpEditEngineItemPool);
264 mpOutputDevice.disposeAndClear();
267 EditEngine * PresenterTextView::Implementation::GetEditEngine()
269 if (mpEditEngine == NULL)
270 mpEditEngine = CreateEditEngine ();
271 return mpEditEngine;
274 EditEngine* PresenterTextView::Implementation::CreateEditEngine()
276 EditEngine* pEditEngine = mpEditEngine;
277 if (pEditEngine == NULL)
280 // set fonts to be used
282 SvtLinguOptions aOpt;
283 SvtLinguConfig().GetOptions( aOpt );
285 struct FontDta {
286 sal_Int16 nFallbackLang;
287 sal_Int16 nLang;
288 DefaultFontType nFontType;
289 sal_uInt16 nFontInfoId;
290 } aTable[3] =
292 // info to get western font to be used
293 { LANGUAGE_ENGLISH_US, LANGUAGE_NONE,
294 DefaultFontType::SERIF, EE_CHAR_FONTINFO },
295 // info to get CJK font to be used
296 { LANGUAGE_JAPANESE, LANGUAGE_NONE,
297 DefaultFontType::CJK_TEXT, EE_CHAR_FONTINFO_CJK },
298 // info to get CTL font to be used
299 { LANGUAGE_ARABIC_SAUDI_ARABIA, LANGUAGE_NONE,
300 DefaultFontType::CTL_TEXT, EE_CHAR_FONTINFO_CTL }
302 aTable[0].nLang = MsLangId::resolveSystemLanguageByScriptType(aOpt.nDefaultLanguage, ::com::sun::star::i18n::ScriptType::LATIN);
303 aTable[1].nLang = MsLangId::resolveSystemLanguageByScriptType(aOpt.nDefaultLanguage_CJK, ::com::sun::star::i18n::ScriptType::ASIAN);
304 aTable[2].nLang = MsLangId::resolveSystemLanguageByScriptType(aOpt.nDefaultLanguage_CTL, ::com::sun::star::i18n::ScriptType::COMPLEX);
306 for (int i = 0; i < 3; ++i)
308 const FontDta &rFntDta = aTable[i];
309 LanguageType nLang = (LANGUAGE_NONE == rFntDta.nLang) ?
310 rFntDta.nFallbackLang : rFntDta.nLang;
311 vcl::Font aFont = OutputDevice::GetDefaultFont(
312 rFntDta.nFontType, nLang, GetDefaultFontFlags::OnlyOne);
313 mpEditEngineItemPool->SetPoolDefaultItem(
314 SvxFontItem(
315 aFont.GetFamily(),
316 aFont.GetName(),
317 aFont.GetStyleName(),
318 aFont.GetPitch(),
319 aFont.GetCharSet(),
320 rFntDta.nFontInfoId));
323 pEditEngine = new EditEngine (mpEditEngineItemPool);
325 pEditEngine->EnableUndo (true);
326 pEditEngine->SetDefTab (sal_uInt16(
327 Application::GetDefaultDevice()->GetTextWidth(OUString("XXXX"))));
329 pEditEngine->SetControlWord(
330 EEControlBits(pEditEngine->GetControlWord() | EEControlBits::AUTOINDENTING) &
331 EEControlBits(~EEControlBits::UNDOATTRIBS) &
332 EEControlBits(~EEControlBits::PASTESPECIAL) );
334 pEditEngine->SetWordDelimiters (" .=+-*/(){}[];\"");
335 pEditEngine->SetRefMapMode (MAP_PIXEL);
336 pEditEngine->SetPaperSize (Size(800, 0));
337 pEditEngine->EraseVirtualDevice();
338 pEditEngine->ClearModifyFlag();
341 return pEditEngine;
344 void PresenterTextView::Implementation::SetCanvas (const cppcanvas::CanvasSharedPtr& rpCanvas)
346 mpCanvas = rpCanvas;
347 mxBitmap = NULL;
350 void PresenterTextView::Implementation::SetSize (const Size aSize)
352 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
354 maSize = aSize;
355 mpEditEngine->SetPaperSize(maSize);
356 mnTotalHeight = -1;
357 mxBitmap = NULL;
360 void PresenterTextView::Implementation::SetBackgroundColor (const Color aColor)
362 maBackgroundColor = aColor;
363 mxBitmap = NULL;
365 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
366 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
367 mpEditEngine->SetBackgroundColor(aColor);
368 mpEditEngine->EnableAutoColor(false);
369 mpEditEngine->ForceAutoColor(false);
372 void PresenterTextView::Implementation::SetTextColor (const Color aColor)
374 maTextColor = aColor;
375 mxBitmap = NULL;
377 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
378 mpEditEngineItemPool->SetPoolDefaultItem(SvxColorItem(aColor, EE_CHAR_COLOR));
381 void PresenterTextView::Implementation::SetFontDescriptor (
382 const awt::FontDescriptor& rFontDescriptor)
384 mxBitmap = NULL;
386 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
388 const sal_Int32 nFontHeight = rFontDescriptor.Height;
390 SvxFontHeightItem aFontHeight(
391 Application::GetDefaultDevice()->LogicToPixel(
392 Size(0, nFontHeight), MapMode (MAP_POINT)).Height(),
393 100,
394 EE_CHAR_FONTHEIGHT);
395 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
396 aFontHeight.SetWhich (EE_CHAR_FONTHEIGHT_CJK);
397 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
398 aFontHeight.SetWhich (EE_CHAR_FONTHEIGHT_CTL);
399 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
401 SvxFontItem aSvxFontItem (EE_CHAR_FONTINFO);
402 aSvxFontItem.SetFamilyName( rFontDescriptor.Name );
403 mpEditEngineItemPool->SetPoolDefaultItem(aSvxFontItem);
405 mnTotalHeight = -1;
406 mxBitmap = NULL;
408 CheckTop();
409 mnTotalHeight = -1;
412 void PresenterTextView::Implementation::SetTop (const sal_Int32 nTop)
414 if (nTop == mnTop)
415 return;
417 mnTop = nTop;
418 mxBitmap = NULL;
419 CheckTop();
422 void PresenterTextView::Implementation::SetText (const OUString& rText)
424 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
425 msText = rText;
426 mpEditEngine->SetPaperSize(maSize);
427 mnTotalHeight = -1;
428 mxBitmap = NULL;
431 sal_Int32 PresenterTextView::Implementation::ParseDistance (const OUString& rsDistance) const
433 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
434 sal_Int32 nDistance (0);
435 if (rsDistance.endsWith("px"))
437 nDistance = rsDistance.copy(0,rsDistance.getLength()-2).toInt32();
439 else if (rsDistance.endsWith("l"))
441 const sal_Int32 nLines (rsDistance.copy(0,rsDistance.getLength()-1).toInt32());
442 // Take the height of the first line as the height of every line.
443 const sal_uInt32 nFirstLineHeight (mpEditEngine->GetLineHeight(0,0));
444 nDistance = nFirstLineHeight * nLines;
447 return nDistance;
450 Reference<rendering::XBitmap> PresenterTextView::Implementation::GetBitmap()
452 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
454 if ( ! mxBitmap.is())
456 mpOutputDevice.disposeAndClear();
457 mpOutputDevice = VclPtr<VirtualDevice>::Create(*Application::GetDefaultDevice(), 0, 0);
458 mpOutputDevice->SetMapMode(MAP_PIXEL);
459 mpOutputDevice->SetOutputSizePixel(maSize, true);
460 mpOutputDevice->SetLineColor();
461 mpOutputDevice->SetFillColor();
462 mpOutputDevice->SetBackground(Wallpaper());
463 mpOutputDevice->Erase();
465 MapMode aMapMode (mpOutputDevice->GetMapMode());
466 aMapMode.SetOrigin(Point(0,0));
467 mpOutputDevice->SetMapMode(aMapMode);
468 const Rectangle aWindowBox (Point(0,0), maSize);
469 mpOutputDevice->DrawRect(aWindowBox);
471 mpEditEngine->Clear();
472 mpEditEngine->SetText(msText);
473 mpEditEngine->SetPaperSize(maSize);
475 mpEditEngine->Draw(mpOutputDevice, aWindowBox, Point(0,mnTop));
477 const BitmapEx aBitmap (mpOutputDevice->GetBitmapEx(Point(0,0), maSize));
478 mxBitmap = cppcanvas::VCLFactory::createBitmap(
479 mpCanvas,
480 aBitmap
481 )->getUNOBitmap();
483 return mxBitmap;
486 sal_Int32 PresenterTextView::Implementation::GetTotalHeight()
488 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
490 if (mnTotalHeight < 0)
492 if ( ! mxBitmap.is())
493 GetBitmap();
494 mnTotalHeight = mpEditEngine->GetTextHeight();
496 return mnTotalHeight;
499 void PresenterTextView::Implementation::CheckTop()
501 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
503 if (mpEditEngine!=NULL && mnTotalHeight < 0)
504 mnTotalHeight = mpEditEngine->GetTextHeight();
505 if (mpEditEngine!=NULL && mnTop >= mnTotalHeight)
506 mnTop = mnTotalHeight - mpEditEngine->GetLineHeight(0,0);
508 if (mnTotalHeight < maSize.Height())
509 mnTop = 0;
511 if (mnTotalHeight - mnTop < maSize.Height())
512 mnTop = mnTotalHeight - maSize.Height();
514 if (mnTop < 0)
515 mnTop = 0;
518 } } // end of namespace ::sd::presenter
521 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
522 com_sun_star_comp_Draw_PresenterTextView_get_implementation(::com::sun::star::uno::XComponentContext* context,
523 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
525 return cppu::acquire(new sd::presenter::PresenterTextView(context));
530 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */