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 .
21 #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/util/Color.hpp>
43 #include <com/sun/star/i18n/ScriptType.hpp>
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::lang
;
50 namespace sd
{ namespace presenter
{
52 //===== Service ===============================================================
54 Reference
<XInterface
> SAL_CALL
PresenterTextViewService_createInstance (
55 const Reference
<XComponentContext
>& rxContext
)
57 return Reference
<XInterface
>(static_cast<XWeak
*>(new PresenterTextView(rxContext
)));
63 OUString
PresenterTextViewService_getImplementationName (void) throw(RuntimeException
)
65 return OUString("com.sun.star.comp.Draw.PresenterTextView");
71 Sequence
<OUString
> SAL_CALL
PresenterTextViewService_getSupportedServiceNames (void)
72 throw (RuntimeException
)
74 static const OUString
sServiceName("com.sun.star.drawing.PresenterTextView");
75 return Sequence
<OUString
>(&sServiceName
, 1);
80 //===== PresenterTextView::Implementation =====================================
82 class PresenterTextView::Implementation
85 const OUString msTextPropertyName
;
86 const OUString msBitmapPropertyName
;
87 const OUString msSizePropertyName
;
88 const OUString msBackgroundColorPropertyName
;
89 const OUString msTextColorPropertyName
;
90 const OUString msFontDescriptorPropertyName
;
91 const OUString msTopPropertyName
;
92 const OUString msTopRelativePropertyName
;
93 const OUString msTotalHeightPropertyName
;
95 Implementation (void);
96 ~Implementation (void);
98 void SetCanvas (const cppcanvas::CanvasSharedPtr
& rCanvas
);
99 void SetSize (const Size aSize
);
100 void SetBackgroundColor (const Color aColor
);
101 void SetTextColor (const Color aColor
);
102 void SetFontDescriptor (const awt::FontDescriptor
& rFontDescriptor
);
103 sal_Int32
GetTop (void) const;
104 void SetTop (const sal_Int32 nTop
);
105 void SetText (const OUString
& Text
);
106 sal_Int32
ParseDistance (const OUString
& rsDistance
) const;
107 Reference
<rendering::XBitmap
> GetBitmap (void);
108 sal_Int32
GetTotalHeight (void);
111 Reference
<rendering::XBitmap
> mxBitmap
;
112 cppcanvas::CanvasSharedPtr mpCanvas
;
113 VirtualDevice
* mpOutputDevice
;
114 EditEngine
* mpEditEngine
;
115 SfxItemPool
* mpEditEngineItemPool
;
117 Color maBackgroundColor
;
121 sal_Int32 mnTotalHeight
;
123 EditEngine
* GetEditEngine (void);
124 EditEngine
* CreateEditEngine (void);
125 void CheckTop (void);
131 //===== PresenterTextView =====================================================
133 PresenterTextView::PresenterTextView (const Reference
<XComponentContext
>& rxContext
)
134 : PresenterTextViewInterfaceBase(),
135 mpImplementation(new Implementation())
143 PresenterTextView::~PresenterTextView (void)
150 void SAL_CALL
PresenterTextView::disposing (void)
152 mpImplementation
.reset();
158 //----- XInitialization -------------------------------------------------------
160 void SAL_CALL
PresenterTextView::initialize (const Sequence
<Any
>& rArguments
)
161 throw (Exception
, RuntimeException
)
165 if (rArguments
.getLength() == 1)
169 Reference
<rendering::XBitmapCanvas
> xCanvas (rArguments
[0], UNO_QUERY_THROW
);
172 mpImplementation
->SetCanvas(
173 cppcanvas::VCLFactory::getInstance().createCanvas(xCanvas
));
176 catch (RuntimeException
&)
183 throw RuntimeException("PresenterTextView: invalid number of arguments",
184 static_cast<XWeak
*>(this));
191 //-----------------------------------------------------------------------------
193 Any
PresenterTextView::GetPropertyValue (const OUString
& rsPropertyName
)
197 if (rsPropertyName
== mpImplementation
->msBitmapPropertyName
)
199 return Any(mpImplementation
->GetBitmap());
201 else if (rsPropertyName
== mpImplementation
->msTopPropertyName
)
203 return Any(mpImplementation
->GetTop());
205 else if (rsPropertyName
== mpImplementation
->msTotalHeightPropertyName
)
207 return Any(mpImplementation
->GetTotalHeight());
216 Any
PresenterTextView::SetPropertyValue (
217 const OUString
& rsPropertyName
,
218 const css::uno::Any
& rValue
)
223 if (rsPropertyName
== mpImplementation
->msTextPropertyName
)
226 if (rValue
>>= sText
)
227 mpImplementation
->SetText(sText
);
229 else if (rsPropertyName
== mpImplementation
->msSizePropertyName
)
232 if (rValue
>>= aSize
)
233 mpImplementation
->SetSize(Size(aSize
.Width
,aSize
.Height
));
235 else if (rsPropertyName
== mpImplementation
->msBackgroundColorPropertyName
)
237 util::Color aColor
= util::Color();
238 if (rValue
>>= aColor
)
239 mpImplementation
->SetBackgroundColor(Color(aColor
));
241 else if (rsPropertyName
== mpImplementation
->msTextColorPropertyName
)
243 util::Color aColor
= util::Color();
244 if (rValue
>>= aColor
)
245 mpImplementation
->SetTextColor(Color(aColor
));
247 else if (rsPropertyName
== mpImplementation
->msFontDescriptorPropertyName
)
249 awt::FontDescriptor aFontDescriptor
;
250 if (rValue
>>= aFontDescriptor
)
251 mpImplementation
->SetFontDescriptor(aFontDescriptor
);
253 else if (rsPropertyName
== mpImplementation
->msTopPropertyName
)
257 mpImplementation
->SetTop(nTop
);
259 else if (rsPropertyName
== mpImplementation
->msTopRelativePropertyName
)
262 if (rValue
>>= sDistance
)
263 mpImplementation
->SetTop(
264 mpImplementation
->GetTop()
265 + mpImplementation
->ParseDistance(sDistance
));
273 void PresenterTextView::ThrowIfDisposed (void)
274 throw (::com::sun::star::lang::DisposedException
)
276 if (PresenterTextViewInterfaceBase::rBHelper
.bDisposed
277 || PresenterTextViewInterfaceBase::rBHelper
.bInDispose
278 || mpImplementation
.get()==NULL
)
280 throw lang::DisposedException ("PresenterTextView object has already been disposed",
281 static_cast<uno::XWeak
*>(this));
288 //===== PresenterTextView::Implementation =====================================
290 PresenterTextView::Implementation::Implementation (void)
291 : msTextPropertyName("Text"),
292 msBitmapPropertyName("Bitmap"),
293 msSizePropertyName("Size"),
294 msBackgroundColorPropertyName("BackgroundColor"),
295 msTextColorPropertyName("TextColor"),
296 msFontDescriptorPropertyName("FontDescriptor"),
297 msTopPropertyName("Top"),
298 msTopRelativePropertyName("RelativeTop"),
299 msTotalHeightPropertyName("TotalHeight"),
302 mpOutputDevice(new VirtualDevice(*Application::GetDefaultDevice(), 0, 0)),
304 mpEditEngineItemPool(EditEngine::CreatePool()),
306 maBackgroundColor(0xffffffff),
307 maTextColor(0x00000000),
312 mpOutputDevice
->SetMapMode(MAP_PIXEL
);
320 PresenterTextView::Implementation::~Implementation (void)
323 SfxItemPool::Free(mpEditEngineItemPool
);
324 delete mpOutputDevice
;
330 EditEngine
* PresenterTextView::Implementation::GetEditEngine (void)
332 if (mpEditEngine
== NULL
)
333 mpEditEngine
= CreateEditEngine ();
340 EditEngine
* PresenterTextView::Implementation::CreateEditEngine (void)
342 EditEngine
* pEditEngine
= mpEditEngine
;
343 if (pEditEngine
== NULL
)
346 // set fonts to be used
348 SvtLinguOptions aOpt
;
349 SvtLinguConfig().GetOptions( aOpt
);
352 sal_Int16 nFallbackLang
;
354 sal_uInt16 nFontType
;
355 sal_uInt16 nFontInfoId
;
358 // info to get western font to be used
359 { LANGUAGE_ENGLISH_US
, LANGUAGE_NONE
,
360 DEFAULTFONT_SERIF
, EE_CHAR_FONTINFO
},
361 // info to get CJK font to be used
362 { LANGUAGE_JAPANESE
, LANGUAGE_NONE
,
363 DEFAULTFONT_CJK_TEXT
, EE_CHAR_FONTINFO_CJK
},
364 // info to get CTL font to be used
365 { LANGUAGE_ARABIC_SAUDI_ARABIA
, LANGUAGE_NONE
,
366 DEFAULTFONT_CTL_TEXT
, EE_CHAR_FONTINFO_CTL
}
368 aTable
[0].nLang
= MsLangId::resolveSystemLanguageByScriptType(aOpt
.nDefaultLanguage
, ::com::sun::star::i18n::ScriptType::LATIN
);
369 aTable
[1].nLang
= MsLangId::resolveSystemLanguageByScriptType(aOpt
.nDefaultLanguage_CJK
, ::com::sun::star::i18n::ScriptType::ASIAN
);
370 aTable
[2].nLang
= MsLangId::resolveSystemLanguageByScriptType(aOpt
.nDefaultLanguage_CTL
, ::com::sun::star::i18n::ScriptType::COMPLEX
);
372 for (int i
= 0; i
< 3; ++i
)
374 const FontDta
&rFntDta
= aTable
[i
];
375 LanguageType nLang
= (LANGUAGE_NONE
== rFntDta
.nLang
) ?
376 rFntDta
.nFallbackLang
: rFntDta
.nLang
;
377 Font aFont
= Application::GetDefaultDevice()->GetDefaultFont(
378 rFntDta
.nFontType
, nLang
, DEFAULTFONT_FLAGS_ONLYONE
);
379 mpEditEngineItemPool
->SetPoolDefaultItem(
383 aFont
.GetStyleName(),
386 rFntDta
.nFontInfoId
));
390 pEditEngine
= new EditEngine (mpEditEngineItemPool
);
392 pEditEngine
->EnableUndo (sal_True
);
393 pEditEngine
->SetDefTab (sal_uInt16(
394 Application::GetDefaultDevice()->GetTextWidth(OUString("XXXX"))));
396 pEditEngine
->SetControlWord(
397 (pEditEngine
->GetControlWord()
398 | EE_CNTRL_AUTOINDENTING
) &
399 (~EE_CNTRL_UNDOATTRIBS
) &
400 (~EE_CNTRL_PASTESPECIAL
));
402 pEditEngine
->SetWordDelimiters (OUString(" .=+-*/(){}[];\""));
403 pEditEngine
->SetRefMapMode (MAP_PIXEL
);
404 pEditEngine
->SetPaperSize (Size(800, 0));
405 pEditEngine
->EraseVirtualDevice();
406 pEditEngine
->ClearModifyFlag();
415 void PresenterTextView::Implementation::SetCanvas (const cppcanvas::CanvasSharedPtr
& rpCanvas
)
424 void PresenterTextView::Implementation::SetSize (const Size aSize
)
426 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
429 mpEditEngine
->SetPaperSize(maSize
);
437 void PresenterTextView::Implementation::SetBackgroundColor (const Color aColor
)
439 maBackgroundColor
= aColor
;
442 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
443 DBG_ASSERT(mpEditEngineItemPool
!=NULL
, "EditEngineItemPool missing");
444 mpEditEngine
->SetBackgroundColor(aColor
);
445 mpEditEngine
->EnableAutoColor(sal_False
);
446 mpEditEngine
->ForceAutoColor(sal_False
);
452 void PresenterTextView::Implementation::SetTextColor (const Color aColor
)
454 maTextColor
= aColor
;
457 DBG_ASSERT(mpEditEngineItemPool
!=NULL
, "EditEngineItemPool missing");
458 mpEditEngineItemPool
->SetPoolDefaultItem(SvxColorItem(aColor
, EE_CHAR_COLOR
));
464 void PresenterTextView::Implementation::SetFontDescriptor (
465 const awt::FontDescriptor
& rFontDescriptor
)
469 DBG_ASSERT(mpEditEngineItemPool
!=NULL
, "EditEngineItemPool missing");
471 const sal_Int32 nFontHeight
= rFontDescriptor
.Height
;
473 SvxFontHeightItem
aFontHeight(
474 Application::GetDefaultDevice()->LogicToPixel(
475 Size(0, nFontHeight
), MapMode (MAP_POINT
)).Height(),
478 mpEditEngineItemPool
->SetPoolDefaultItem( aFontHeight
);
479 aFontHeight
.SetWhich (EE_CHAR_FONTHEIGHT_CJK
);
480 mpEditEngineItemPool
->SetPoolDefaultItem( aFontHeight
);
481 aFontHeight
.SetWhich (EE_CHAR_FONTHEIGHT_CTL
);
482 mpEditEngineItemPool
->SetPoolDefaultItem( aFontHeight
);
484 SvxFontItem
aSvxFontItem (EE_CHAR_FONTINFO
);
485 aSvxFontItem
.SetFamilyName( rFontDescriptor
.Name
);
486 mpEditEngineItemPool
->SetPoolDefaultItem(aSvxFontItem
);
498 sal_Int32
PresenterTextView::Implementation::GetTop (void) const
506 void PresenterTextView::Implementation::SetTop (const sal_Int32 nTop
)
519 void PresenterTextView::Implementation::SetText (const OUString
& rText
)
521 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
523 mpEditEngine
->SetPaperSize(maSize
);
531 sal_Int32
PresenterTextView::Implementation::ParseDistance (const OUString
& rsDistance
) const
533 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
534 sal_Int32
nDistance (0);
535 if (rsDistance
.endsWithAsciiL("px", 2))
537 nDistance
= rsDistance
.copy(0,rsDistance
.getLength()-2).toInt32();
539 else if (rsDistance
.endsWithAsciiL("l", 1))
541 const sal_Int32
nLines (rsDistance
.copy(0,rsDistance
.getLength()-1).toInt32());
542 // Take the height of the first line as the height of every line.
543 const sal_uInt32
nFirstLineHeight (mpEditEngine
->GetLineHeight(0,0));
544 nDistance
= nFirstLineHeight
* nLines
;
553 Reference
<rendering::XBitmap
> PresenterTextView::Implementation::GetBitmap (void)
555 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
557 if ( ! mxBitmap
.is())
559 if (mpOutputDevice
!= NULL
)
560 delete mpOutputDevice
;
561 mpOutputDevice
= new VirtualDevice(*Application::GetDefaultDevice(), 0, 0);
562 mpOutputDevice
->SetMapMode(MAP_PIXEL
);
563 mpOutputDevice
->SetOutputSizePixel(maSize
, sal_True
);
564 mpOutputDevice
->SetLineColor();
565 mpOutputDevice
->SetFillColor();
566 mpOutputDevice
->SetBackground(Wallpaper());
567 mpOutputDevice
->Erase();
569 MapMode
aMapMode (mpOutputDevice
->GetMapMode());
570 aMapMode
.SetOrigin(Point(0,0));
571 mpOutputDevice
->SetMapMode(aMapMode
);
572 const Rectangle
aWindowBox (Point(0,0), maSize
);
573 mpOutputDevice
->DrawRect(aWindowBox
);
575 mpEditEngine
->Clear();
576 mpEditEngine
->SetText(msText
);
577 mpEditEngine
->SetPaperSize(maSize
);
579 mpEditEngine
->Draw(mpOutputDevice
, aWindowBox
, Point(0,mnTop
));
581 const BitmapEx
aBitmap (mpOutputDevice
->GetBitmapEx(Point(0,0), maSize
));
582 mxBitmap
= cppcanvas::VCLFactory::getInstance().createBitmap(
593 sal_Int32
PresenterTextView::Implementation::GetTotalHeight (void)
595 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
597 if (mnTotalHeight
< 0)
599 if ( ! mxBitmap
.is())
601 mnTotalHeight
= mpEditEngine
->GetTextHeight();
603 return mnTotalHeight
;
609 void PresenterTextView::Implementation::CheckTop (void)
611 DBG_ASSERT(mpEditEngine
!=NULL
, "EditEngine missing");
613 if (mnTotalHeight
< 0)
614 mnTotalHeight
= mpEditEngine
->GetTextHeight();
615 if (mpEditEngine
!=NULL
&& mnTop
>= mnTotalHeight
)
616 mnTop
= mnTotalHeight
- mpEditEngine
->GetLineHeight(0,0);
618 if (mnTotalHeight
< maSize
.Height())
621 if (mnTotalHeight
- mnTop
< maSize
.Height())
622 mnTop
= mnTotalHeight
- maSize
.Height();
629 } } // end of namespace ::sd::presenter
631 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */