merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / presenter / PresenterTextView.cxx
blob1ec20a11f9183b775d6704ee67c0c8a56c96f391
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterTextView.cxx,v $
11 * $Revision: 1.5.76.1 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "precompiled_sd.hxx"
34 #include "PresenterTextView.hxx"
36 #include <i18npool/mslangid.hxx>
37 #include <cppcanvas/vclfactory.hxx>
38 #include <svtools/itempool.hxx>
39 #include <svtools/itemset.hxx>
40 #include <svtools/linguprops.hxx>
41 #include <svtools/lingucfg.hxx>
42 #include <svx/colritem.hxx>
43 #include <svx/editeng.hxx>
44 #include <svx/editstat.hxx>
45 #include <svx/eeitem.hxx>
46 #include <svx/fhgtitem.hxx>
47 #include <svx/fontitem.hxx>
48 #include <svx/xflclit.hxx>
49 #include <vcl/bitmapex.hxx>
50 #include <vcl/svapp.hxx>
51 #include <vcl/virdev.hxx>
52 #include <com/sun/star/awt/FontDescriptor.hpp>
53 #include <com/sun/star/awt/Size.hpp>
54 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
55 #include <com/sun/star/util/Color.hpp>
56 #include <com/sun/star/i18n/ScriptType.hpp>
59 using namespace ::com::sun::star;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::lang;
62 using ::rtl::OUString;
64 namespace sd { namespace presenter {
66 //===== Service ===============================================================
68 Reference<XInterface> SAL_CALL PresenterTextViewService_createInstance (
69 const Reference<XComponentContext>& rxContext)
71 return Reference<XInterface>(static_cast<XWeak*>(new PresenterTextView(rxContext)));
77 ::rtl::OUString PresenterTextViewService_getImplementationName (void) throw(RuntimeException)
79 return OUString::createFromAscii("com.sun.star.comp.Draw.PresenterTextView");
85 Sequence<rtl::OUString> SAL_CALL PresenterTextViewService_getSupportedServiceNames (void)
86 throw (RuntimeException)
88 static const ::rtl::OUString sServiceName(
89 ::rtl::OUString::createFromAscii("com.sun.star.drawing.PresenterTextView"));
90 return Sequence<rtl::OUString>(&sServiceName, 1);
95 //===== PresenterTextView::Implementation =====================================
97 class PresenterTextView::Implementation
99 public:
100 const OUString msTextPropertyName;
101 const OUString msBitmapPropertyName;
102 const OUString msSizePropertyName;
103 const OUString msBackgroundColorPropertyName;
104 const OUString msTextColorPropertyName;
105 const OUString msFontDescriptorPropertyName;
106 const OUString msTopPropertyName;
107 const OUString msTopRelativePropertyName;
108 const OUString msTotalHeightPropertyName;
110 Implementation (void);
111 ~Implementation (void);
113 void SetCanvas (const cppcanvas::CanvasSharedPtr& rCanvas);
114 void SetSize (const Size aSize);
115 void SetBackgroundColor (const Color aColor);
116 void SetTextColor (const Color aColor);
117 void SetFontDescriptor (const awt::FontDescriptor& rFontDescriptor);
118 sal_Int32 GetTop (void) const;
119 void SetTop (const sal_Int32 nTop);
120 void ClearText (void);
121 void SetText (const OUString& Text);
122 sal_Int32 ParseDistance (const OUString& rsDistance) const;
123 Reference<rendering::XBitmap> GetBitmap (void);
124 sal_Int32 GetTotalHeight (void);
126 private:
127 Reference<rendering::XBitmap> mxBitmap;
128 cppcanvas::CanvasSharedPtr mpCanvas;
129 VirtualDevice* mpOutputDevice;
130 EditEngine* mpEditEngine;
131 SfxItemPool* mpEditEngineItemPool;
132 Size maSize;
133 Color maBackgroundColor;
134 Color maTextColor;
135 String msText;
136 sal_Int32 mnTop;
137 sal_Int32 mnTotalHeight;
139 EditEngine * GetEditEngine (void);
140 EditEngine* CreateEditEngine (void);
141 void CheckTop (void);
147 //===== PresenterTextView =====================================================
149 PresenterTextView::PresenterTextView (const Reference<XComponentContext>& rxContext)
150 : PresenterTextViewInterfaceBase(),
151 mpImplementation(new Implementation())
153 (void)rxContext;
159 PresenterTextView::~PresenterTextView (void)
166 void SAL_CALL PresenterTextView::disposing (void)
168 mpImplementation.reset();
174 //----- XInitialization -------------------------------------------------------
176 void SAL_CALL PresenterTextView::initialize (const Sequence<Any>& rArguments)
177 throw (Exception, RuntimeException)
179 ThrowIfDisposed();
181 if (rArguments.getLength() == 1)
185 Reference<rendering::XBitmapCanvas> xCanvas (rArguments[0], UNO_QUERY_THROW);
186 if (xCanvas.is())
188 mpImplementation->SetCanvas(
189 cppcanvas::VCLFactory::getInstance().createCanvas(xCanvas));
192 catch (RuntimeException&)
194 throw;
197 else
199 throw RuntimeException(
200 OUString::createFromAscii("PresenterTextView: invalid number of arguments"),
201 static_cast<XWeak*>(this));
208 //-----------------------------------------------------------------------------
210 Any PresenterTextView::GetPropertyValue (const OUString& rsPropertyName)
212 ThrowIfDisposed();
214 if (rsPropertyName == mpImplementation->msBitmapPropertyName)
216 return Any(mpImplementation->GetBitmap());
218 else if (rsPropertyName == mpImplementation->msTopPropertyName)
220 return Any(mpImplementation->GetTop());
222 else if (rsPropertyName == mpImplementation->msTotalHeightPropertyName)
224 return Any(mpImplementation->GetTotalHeight());
227 return Any();
233 Any PresenterTextView::SetPropertyValue (
234 const ::rtl::OUString& rsPropertyName,
235 const css::uno::Any& rValue)
237 ThrowIfDisposed();
239 Any aOldValue;
240 if (rsPropertyName == mpImplementation->msTextPropertyName)
242 OUString sText;
243 if (rValue >>= sText)
244 mpImplementation->SetText(sText);
246 else if (rsPropertyName == mpImplementation->msSizePropertyName)
248 awt::Size aSize;
249 if (rValue >>= aSize)
250 mpImplementation->SetSize(Size(aSize.Width,aSize.Height));
252 else if (rsPropertyName == mpImplementation->msBackgroundColorPropertyName)
254 util::Color aColor = util::Color();
255 if (rValue >>= aColor)
256 mpImplementation->SetBackgroundColor(Color(aColor));
258 else if (rsPropertyName == mpImplementation->msTextColorPropertyName)
260 util::Color aColor = util::Color();
261 if (rValue >>= aColor)
262 mpImplementation->SetTextColor(Color(aColor));
264 else if (rsPropertyName == mpImplementation->msFontDescriptorPropertyName)
266 awt::FontDescriptor aFontDescriptor;
267 if (rValue >>= aFontDescriptor)
268 mpImplementation->SetFontDescriptor(aFontDescriptor);
270 else if (rsPropertyName == mpImplementation->msTopPropertyName)
272 sal_Int32 nTop = 0;
273 if (rValue >>= nTop)
274 mpImplementation->SetTop(nTop);
276 else if (rsPropertyName == mpImplementation->msTopRelativePropertyName)
278 OUString sDistance;
279 if (rValue >>= sDistance)
280 mpImplementation->SetTop(
281 mpImplementation->GetTop()
282 + mpImplementation->ParseDistance(sDistance));
284 return aOldValue;
290 void PresenterTextView::ThrowIfDisposed (void)
291 throw (::com::sun::star::lang::DisposedException)
293 if (PresenterTextViewInterfaceBase::rBHelper.bDisposed
294 || PresenterTextViewInterfaceBase::rBHelper.bInDispose
295 || mpImplementation.get()==NULL)
297 throw lang::DisposedException (
298 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
299 "PresenterTextView object has already been disposed")),
300 static_cast<uno::XWeak*>(this));
307 //===== PresenterTextView::Implementation =====================================
309 PresenterTextView::Implementation::Implementation (void)
310 : msTextPropertyName(OUString::createFromAscii("Text")),
311 msBitmapPropertyName(OUString::createFromAscii("Bitmap")),
312 msSizePropertyName(OUString::createFromAscii("Size")),
313 msBackgroundColorPropertyName(OUString::createFromAscii("BackgroundColor")),
314 msTextColorPropertyName(OUString::createFromAscii("TextColor")),
315 msFontDescriptorPropertyName(OUString::createFromAscii("FontDescriptor")),
316 msTopPropertyName(OUString::createFromAscii("Top")),
317 msTopRelativePropertyName(OUString::createFromAscii("RelativeTop")),
318 msTotalHeightPropertyName(OUString::createFromAscii("TotalHeight")),
319 mxBitmap(),
320 mpCanvas(),
321 mpOutputDevice(new VirtualDevice(*Application::GetDefaultDevice(), 0, 0)),
322 mpEditEngine(NULL),
323 mpEditEngineItemPool(EditEngine::CreatePool()),
324 maSize(100,100),
325 maBackgroundColor(0xffffffff),
326 maTextColor(0x00000000),
327 msText(),
328 mnTop(0),
329 mnTotalHeight(-1)
331 mpOutputDevice->SetMapMode(MAP_PIXEL);
333 GetEditEngine();
339 PresenterTextView::Implementation::~Implementation (void)
341 delete mpEditEngine;
342 SfxItemPool::Free(mpEditEngineItemPool);
343 delete mpOutputDevice;
349 EditEngine * PresenterTextView::Implementation::GetEditEngine (void)
351 if (mpEditEngine == NULL)
352 mpEditEngine = CreateEditEngine ();
353 return mpEditEngine;
359 EditEngine* PresenterTextView::Implementation::CreateEditEngine (void)
361 EditEngine* pEditEngine = mpEditEngine;
362 if (pEditEngine == NULL)
365 // set fonts to be used
367 SvtLinguOptions aOpt;
368 SvtLinguConfig().GetOptions( aOpt );
370 struct FontDta {
371 INT16 nFallbackLang;
372 INT16 nLang;
373 USHORT nFontType;
374 USHORT nFontInfoId;
375 } aTable[3] =
377 // info to get western font to be used
378 { LANGUAGE_ENGLISH_US, LANGUAGE_NONE,
379 DEFAULTFONT_SERIF, EE_CHAR_FONTINFO },
380 // info to get CJK font to be used
381 { LANGUAGE_JAPANESE, LANGUAGE_NONE,
382 DEFAULTFONT_CJK_TEXT, EE_CHAR_FONTINFO_CJK },
383 // info to get CTL font to be used
384 { LANGUAGE_ARABIC_SAUDI_ARABIA, LANGUAGE_NONE,
385 DEFAULTFONT_CTL_TEXT, EE_CHAR_FONTINFO_CTL }
387 aTable[0].nLang = MsLangId::resolveSystemLanguageByScriptType(aOpt.nDefaultLanguage, ::com::sun::star::i18n::ScriptType::LATIN);
388 aTable[1].nLang = MsLangId::resolveSystemLanguageByScriptType(aOpt.nDefaultLanguage_CJK, ::com::sun::star::i18n::ScriptType::ASIAN);
389 aTable[2].nLang = MsLangId::resolveSystemLanguageByScriptType(aOpt.nDefaultLanguage_CTL, ::com::sun::star::i18n::ScriptType::COMPLEX);
391 for (int i = 0; i < 3; ++i)
393 const FontDta &rFntDta = aTable[i];
394 LanguageType nLang = (LANGUAGE_NONE == rFntDta.nLang) ?
395 rFntDta.nFallbackLang : rFntDta.nLang;
396 Font aFont = Application::GetDefaultDevice()->GetDefaultFont(
397 rFntDta.nFontType, nLang, DEFAULTFONT_FLAGS_ONLYONE);
398 mpEditEngineItemPool->SetPoolDefaultItem(
399 SvxFontItem(
400 aFont.GetFamily(),
401 aFont.GetName(),
402 aFont.GetStyleName(),
403 aFont.GetPitch(),
404 aFont.GetCharSet(),
405 rFntDta.nFontInfoId));
409 pEditEngine = new EditEngine (mpEditEngineItemPool);
411 pEditEngine->EnableUndo (TRUE);
412 pEditEngine->SetDefTab (USHORT(
413 Application::GetDefaultDevice()->GetTextWidth(
414 UniString::CreateFromAscii("XXXX"))));
416 pEditEngine->SetControlWord(
417 (pEditEngine->GetControlWord()
418 | EE_CNTRL_AUTOINDENTING) &
419 (~EE_CNTRL_UNDOATTRIBS) &
420 (~EE_CNTRL_PASTESPECIAL));
422 pEditEngine->SetWordDelimiters (
423 UniString::CreateFromAscii(" .=+-*/(){}[];\""));
424 pEditEngine->SetRefMapMode (MAP_PIXEL);
425 pEditEngine->SetPaperSize (Size(800, 0));
426 pEditEngine->EraseVirtualDevice();
427 pEditEngine->ClearModifyFlag();
430 return pEditEngine;
436 void PresenterTextView::Implementation::SetCanvas (const cppcanvas::CanvasSharedPtr& rpCanvas)
438 mpCanvas = rpCanvas;
439 mxBitmap = NULL;
445 void PresenterTextView::Implementation::SetSize (const Size aSize)
447 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
449 maSize = aSize;
450 mpEditEngine->SetPaperSize(maSize);
451 mnTotalHeight = -1;
452 mxBitmap = NULL;
458 void PresenterTextView::Implementation::SetBackgroundColor (const Color aColor)
460 maBackgroundColor = aColor;
461 mxBitmap = NULL;
463 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
464 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
465 mpEditEngine->SetBackgroundColor(aColor);
466 mpEditEngine->EnableAutoColor(FALSE);
467 mpEditEngine->ForceAutoColor(FALSE);
473 void PresenterTextView::Implementation::SetTextColor (const Color aColor)
475 maTextColor = aColor;
476 mxBitmap = NULL;
478 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
479 mpEditEngineItemPool->SetPoolDefaultItem(SvxColorItem(aColor, EE_CHAR_COLOR));
485 void PresenterTextView::Implementation::SetFontDescriptor (
486 const awt::FontDescriptor& rFontDescriptor)
488 mxBitmap = NULL;
490 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
492 const sal_Int32 nFontHeight = rFontDescriptor.Height;
494 SvxFontHeightItem aFontHeight(
495 Application::GetDefaultDevice()->LogicToPixel(
496 Size(0, nFontHeight), MapMode (MAP_POINT)).Height(),
497 100,
498 EE_CHAR_FONTHEIGHT);
499 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
500 aFontHeight.SetWhich (EE_CHAR_FONTHEIGHT_CJK);
501 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
502 aFontHeight.SetWhich (EE_CHAR_FONTHEIGHT_CTL);
503 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
505 SvxFontItem aSvxFontItem (EE_CHAR_FONTINFO);
506 aSvxFontItem.GetFamilyName() = rFontDescriptor.Name;
507 mpEditEngineItemPool->SetPoolDefaultItem(aSvxFontItem);
509 mnTotalHeight = -1;
510 mxBitmap = NULL;
512 CheckTop();
513 mnTotalHeight = -1;
519 sal_Int32 PresenterTextView::Implementation::GetTop (void) const
521 return mnTop;
527 void PresenterTextView::Implementation::SetTop (const sal_Int32 nTop)
529 if (nTop == mnTop)
530 return;
532 mnTop = nTop;
533 mxBitmap = NULL;
534 CheckTop();
540 void PresenterTextView::Implementation::ClearText (void)
542 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
543 msText = OUString();
544 mnTotalHeight = 0;
545 mxBitmap = NULL;
551 void PresenterTextView::Implementation::SetText (const OUString& rText)
553 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
554 msText = rText;
555 mpEditEngine->SetPaperSize(maSize);
556 mnTotalHeight = -1;
557 mxBitmap = NULL;
563 sal_Int32 PresenterTextView::Implementation::ParseDistance (const OUString& rsDistance) const
565 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
566 sal_Int32 nDistance (0);
567 if (rsDistance.endsWithAsciiL("px", 2))
569 nDistance = rsDistance.copy(0,rsDistance.getLength()-2).toInt32();
571 else if (rsDistance.endsWithAsciiL("l", 1))
573 const sal_Int32 nLines (rsDistance.copy(0,rsDistance.getLength()-1).toInt32());
574 // Take the height of the first line as the height of every line.
575 const sal_uInt32 nFirstLineHeight (mpEditEngine->GetLineHeight(0,0));
576 nDistance = nFirstLineHeight * nLines;
579 return nDistance;
585 Reference<rendering::XBitmap> PresenterTextView::Implementation::GetBitmap (void)
587 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
589 if ( ! mxBitmap.is())
591 if (mpOutputDevice != NULL)
592 delete mpOutputDevice;
593 mpOutputDevice = new VirtualDevice(*Application::GetDefaultDevice(), 0, 0);
594 mpOutputDevice->SetMapMode(MAP_PIXEL);
595 mpOutputDevice->SetOutputSizePixel(maSize, TRUE);
596 mpOutputDevice->SetLineColor();
597 mpOutputDevice->SetFillColor();
598 mpOutputDevice->SetBackground(Wallpaper());
599 mpOutputDevice->Erase();
601 MapMode aMapMode (mpOutputDevice->GetMapMode());
602 aMapMode.SetOrigin(Point(0,0));
603 mpOutputDevice->SetMapMode(aMapMode);
604 const Rectangle aWindowBox (Point(0,0), maSize);
605 mpOutputDevice->DrawRect(aWindowBox);
607 mpEditEngine->Clear();
608 mpEditEngine->SetText(msText);
609 mpEditEngine->SetPaperSize(maSize);
611 mpEditEngine->Draw(mpOutputDevice, aWindowBox, Point(0,mnTop));
613 const BitmapEx aBitmap (mpOutputDevice->GetBitmapEx(Point(0,0), maSize));
614 mxBitmap = cppcanvas::VCLFactory::getInstance().createBitmap(
615 mpCanvas,
616 aBitmap
617 )->getUNOBitmap();
619 return mxBitmap;
625 sal_Int32 PresenterTextView::Implementation::GetTotalHeight (void)
627 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
629 if (mnTotalHeight < 0)
631 if ( ! mxBitmap.is())
632 GetBitmap();
633 mnTotalHeight = mpEditEngine->GetTextHeight();
635 return mnTotalHeight;
641 void PresenterTextView::Implementation::CheckTop (void)
643 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
645 if (mnTotalHeight < 0)
646 mnTotalHeight = mpEditEngine->GetTextHeight();
647 if (mpEditEngine!=NULL && mnTop >= mnTotalHeight)
648 mnTop = mnTotalHeight - mpEditEngine->GetLineHeight(0,0);
650 if (mnTotalHeight < maSize.Height())
651 mnTop = 0;
653 if (mnTotalHeight - mnTop < maSize.Height())
654 mnTop = mnTotalHeight - maSize.Height();
656 if (mnTop < 0)
657 mnTop = 0;
661 } } // end of namespace ::sd::presenter