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 "PresenterTextView.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
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
;
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();
83 Reference
<rendering::XBitmap
> mxBitmap
;
84 cppcanvas::CanvasSharedPtr mpCanvas
;
85 VclPtr
<VirtualDevice
> mpOutputDevice
;
86 EditEngine
* mpEditEngine
;
87 SfxItemPool
* mpEditEngineItemPool
;
89 Color maBackgroundColor
;
93 sal_Int32 mnTotalHeight
;
95 EditEngine
* GetEditEngine();
96 EditEngine
* CreateEditEngine();
101 PresenterTextView::PresenterTextView (const Reference
<XComponentContext
>& rxContext
)
102 : PresenterTextViewInterfaceBase(),
103 mpImplementation(new Implementation())
108 PresenterTextView::~PresenterTextView()
112 void SAL_CALL
PresenterTextView::disposing()
114 mpImplementation
.reset();
118 void SAL_CALL
PresenterTextView::initialize (const Sequence
<Any
>& rArguments
)
119 throw (Exception
, RuntimeException
, std::exception
)
123 if (rArguments
.getLength() == 1)
127 Reference
<rendering::XCanvas
> xCanvas (rArguments
[0], UNO_QUERY_THROW
);
130 mpImplementation
->SetCanvas(
131 cppcanvas::VCLFactory::createCanvas(xCanvas
));
134 catch (RuntimeException
&)
141 throw RuntimeException("PresenterTextView: invalid number of arguments",
142 static_cast<XWeak
*>(this));
146 Any
PresenterTextView::GetPropertyValue (const OUString
& rsPropertyName
)
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());
166 Any
PresenterTextView::SetPropertyValue (
167 const OUString
& rsPropertyName
,
168 const css::uno::Any
& rValue
)
173 if (rsPropertyName
== mpImplementation
->msTextPropertyName
)
176 if (rValue
>>= sText
)
177 mpImplementation
->SetText(sText
);
179 else if (rsPropertyName
== mpImplementation
->msSizePropertyName
)
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
)
207 mpImplementation
->SetTop(nTop
);
209 else if (rsPropertyName
== mpImplementation
->msTopRelativePropertyName
)
212 if (rValue
>>= sDistance
)
213 mpImplementation
->SetTop(
214 mpImplementation
->GetTop()
215 + mpImplementation
->ParseDistance(sDistance
));
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"),
245 mpOutputDevice(VclPtr
<VirtualDevice
>::Create(*Application::GetDefaultDevice(), 0, 0)),
247 mpEditEngineItemPool(EditEngine::CreatePool()),
249 maBackgroundColor(0xffffffff),
250 maTextColor(0x00000000),
255 mpOutputDevice
->SetMapMode(MAP_PIXEL
);
260 PresenterTextView::Implementation::~Implementation()
263 SfxItemPool::Free(mpEditEngineItemPool
);
264 mpOutputDevice
.disposeAndClear();
267 EditEngine
* PresenterTextView::Implementation::GetEditEngine()
269 if (mpEditEngine
== NULL
)
270 mpEditEngine
= CreateEditEngine ();
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
);
286 sal_Int16 nFallbackLang
;
288 DefaultFontType nFontType
;
289 sal_uInt16 nFontInfoId
;
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(
317 aFont
.GetStyleName(),
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();
344 void PresenterTextView::Implementation::SetCanvas (const cppcanvas::CanvasSharedPtr
& rpCanvas
)
350 void PresenterTextView::Implementation::SetSize (const Size aSize
)
352 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
355 mpEditEngine
->SetPaperSize(maSize
);
360 void PresenterTextView::Implementation::SetBackgroundColor (const Color aColor
)
362 maBackgroundColor
= aColor
;
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
;
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
)
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(),
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
);
412 void PresenterTextView::Implementation::SetTop (const sal_Int32 nTop
)
422 void PresenterTextView::Implementation::SetText (const OUString
& rText
)
424 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
426 mpEditEngine
->SetPaperSize(maSize
);
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
;
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(
486 sal_Int32
PresenterTextView::Implementation::GetTotalHeight()
488 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
490 if (mnTotalHeight
< 0)
492 if ( ! mxBitmap
.is())
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())
511 if (mnTotalHeight
- mnTop
< maSize
.Height())
512 mnTop
= mnTotalHeight
- maSize
.Height();
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: */