update dev300-m58
[ooovba.git] / sd / source / ui / presenter / PresenterTextView.cxx
blobef898549cf772da04e94478d7fc0a1446ff7b872
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 <cppcanvas/vclfactory.hxx>
37 #include <svtools/itempool.hxx>
38 #include <svtools/itemset.hxx>
39 #include <svtools/linguprops.hxx>
40 #include <svtools/lingucfg.hxx>
41 #include <svx/colritem.hxx>
42 #include <svx/editeng.hxx>
43 #include <svx/editstat.hxx>
44 #include <svx/eeitem.hxx>
45 #include <svx/fhgtitem.hxx>
46 #include <svx/fontitem.hxx>
47 #include <svx/xflclit.hxx>
48 #include <vcl/bitmapex.hxx>
49 #include <vcl/svapp.hxx>
50 #include <vcl/virdev.hxx>
51 #include <com/sun/star/awt/FontDescriptor.hpp>
52 #include <com/sun/star/awt/Size.hpp>
53 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
54 #include <com/sun/star/util/Color.hpp>
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::lang;
59 using ::rtl::OUString;
61 namespace sd { namespace presenter {
63 //===== Service ===============================================================
65 Reference<XInterface> SAL_CALL PresenterTextViewService_createInstance (
66 const Reference<XComponentContext>& rxContext)
68 return Reference<XInterface>(static_cast<XWeak*>(new PresenterTextView(rxContext)));
74 ::rtl::OUString PresenterTextViewService_getImplementationName (void) throw(RuntimeException)
76 return OUString::createFromAscii("com.sun.star.comp.Draw.PresenterTextView");
82 Sequence<rtl::OUString> SAL_CALL PresenterTextViewService_getSupportedServiceNames (void)
83 throw (RuntimeException)
85 static const ::rtl::OUString sServiceName(
86 ::rtl::OUString::createFromAscii("com.sun.star.drawing.PresenterTextView"));
87 return Sequence<rtl::OUString>(&sServiceName, 1);
92 //===== PresenterTextView::Implementation =====================================
94 class PresenterTextView::Implementation
96 public:
97 const OUString msTextPropertyName;
98 const OUString msBitmapPropertyName;
99 const OUString msSizePropertyName;
100 const OUString msBackgroundColorPropertyName;
101 const OUString msTextColorPropertyName;
102 const OUString msFontDescriptorPropertyName;
103 const OUString msTopPropertyName;
104 const OUString msTopRelativePropertyName;
105 const OUString msTotalHeightPropertyName;
107 Implementation (void);
108 ~Implementation (void);
110 void SetCanvas (const cppcanvas::CanvasSharedPtr& rCanvas);
111 void SetSize (const Size aSize);
112 void SetBackgroundColor (const Color aColor);
113 void SetTextColor (const Color aColor);
114 void SetFontDescriptor (const awt::FontDescriptor& rFontDescriptor);
115 sal_Int32 GetTop (void) const;
116 void SetTop (const sal_Int32 nTop);
117 void ClearText (void);
118 void SetText (const OUString& Text);
119 sal_Int32 ParseDistance (const OUString& rsDistance) const;
120 Reference<rendering::XBitmap> GetBitmap (void);
121 sal_Int32 GetTotalHeight (void);
123 private:
124 Reference<rendering::XBitmap> mxBitmap;
125 cppcanvas::CanvasSharedPtr mpCanvas;
126 VirtualDevice* mpOutputDevice;
127 EditEngine* mpEditEngine;
128 SfxItemPool* mpEditEngineItemPool;
129 Size maSize;
130 Color maBackgroundColor;
131 Color maTextColor;
132 String msText;
133 sal_Int32 mnTop;
134 sal_Int32 mnTotalHeight;
136 EditEngine * GetEditEngine (void);
137 EditEngine* CreateEditEngine (void);
138 void CheckTop (void);
144 //===== PresenterTextView =====================================================
146 PresenterTextView::PresenterTextView (const Reference<XComponentContext>& rxContext)
147 : PresenterTextViewInterfaceBase(),
148 mpImplementation(new Implementation())
150 (void)rxContext;
156 PresenterTextView::~PresenterTextView (void)
163 void SAL_CALL PresenterTextView::disposing (void)
165 mpImplementation.reset();
171 //----- XInitialization -------------------------------------------------------
173 void SAL_CALL PresenterTextView::initialize (const Sequence<Any>& rArguments)
174 throw (Exception, RuntimeException)
176 ThrowIfDisposed();
178 if (rArguments.getLength() == 1)
182 Reference<rendering::XBitmapCanvas> xCanvas (rArguments[0], UNO_QUERY_THROW);
183 if (xCanvas.is())
185 mpImplementation->SetCanvas(
186 cppcanvas::VCLFactory::getInstance().createCanvas(xCanvas));
189 catch (RuntimeException&)
191 throw;
194 else
196 throw RuntimeException(
197 OUString::createFromAscii("PresenterTextView: invalid number of arguments"),
198 static_cast<XWeak*>(this));
205 //-----------------------------------------------------------------------------
207 Any PresenterTextView::GetPropertyValue (const OUString& rsPropertyName)
209 ThrowIfDisposed();
211 if (rsPropertyName == mpImplementation->msBitmapPropertyName)
213 return Any(mpImplementation->GetBitmap());
215 else if (rsPropertyName == mpImplementation->msTopPropertyName)
217 return Any(mpImplementation->GetTop());
219 else if (rsPropertyName == mpImplementation->msTotalHeightPropertyName)
221 return Any(mpImplementation->GetTotalHeight());
224 return Any();
230 Any PresenterTextView::SetPropertyValue (
231 const ::rtl::OUString& rsPropertyName,
232 const css::uno::Any& rValue)
234 ThrowIfDisposed();
236 Any aOldValue;
237 if (rsPropertyName == mpImplementation->msTextPropertyName)
239 OUString sText;
240 if (rValue >>= sText)
241 mpImplementation->SetText(sText);
243 else if (rsPropertyName == mpImplementation->msSizePropertyName)
245 awt::Size aSize;
246 if (rValue >>= aSize)
247 mpImplementation->SetSize(Size(aSize.Width,aSize.Height));
249 else if (rsPropertyName == mpImplementation->msBackgroundColorPropertyName)
251 util::Color aColor = util::Color();
252 if (rValue >>= aColor)
253 mpImplementation->SetBackgroundColor(Color(aColor));
255 else if (rsPropertyName == mpImplementation->msTextColorPropertyName)
257 util::Color aColor = util::Color();
258 if (rValue >>= aColor)
259 mpImplementation->SetTextColor(Color(aColor));
261 else if (rsPropertyName == mpImplementation->msFontDescriptorPropertyName)
263 awt::FontDescriptor aFontDescriptor;
264 if (rValue >>= aFontDescriptor)
265 mpImplementation->SetFontDescriptor(aFontDescriptor);
267 else if (rsPropertyName == mpImplementation->msTopPropertyName)
269 sal_Int32 nTop = 0;
270 if (rValue >>= nTop)
271 mpImplementation->SetTop(nTop);
273 else if (rsPropertyName == mpImplementation->msTopRelativePropertyName)
275 OUString sDistance;
276 if (rValue >>= sDistance)
277 mpImplementation->SetTop(
278 mpImplementation->GetTop()
279 + mpImplementation->ParseDistance(sDistance));
281 return aOldValue;
287 void PresenterTextView::ThrowIfDisposed (void)
288 throw (::com::sun::star::lang::DisposedException)
290 if (PresenterTextViewInterfaceBase::rBHelper.bDisposed
291 || PresenterTextViewInterfaceBase::rBHelper.bInDispose
292 || mpImplementation.get()==NULL)
294 throw lang::DisposedException (
295 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
296 "PresenterTextView object has already been disposed")),
297 static_cast<uno::XWeak*>(this));
304 //===== PresenterTextView::Implementation =====================================
306 PresenterTextView::Implementation::Implementation (void)
307 : msTextPropertyName(OUString::createFromAscii("Text")),
308 msBitmapPropertyName(OUString::createFromAscii("Bitmap")),
309 msSizePropertyName(OUString::createFromAscii("Size")),
310 msBackgroundColorPropertyName(OUString::createFromAscii("BackgroundColor")),
311 msTextColorPropertyName(OUString::createFromAscii("TextColor")),
312 msFontDescriptorPropertyName(OUString::createFromAscii("FontDescriptor")),
313 msTopPropertyName(OUString::createFromAscii("Top")),
314 msTopRelativePropertyName(OUString::createFromAscii("RelativeTop")),
315 msTotalHeightPropertyName(OUString::createFromAscii("TotalHeight")),
316 mxBitmap(),
317 mpCanvas(),
318 mpOutputDevice(new VirtualDevice(*Application::GetDefaultDevice(), 0, 0)),
319 mpEditEngine(NULL),
320 mpEditEngineItemPool(EditEngine::CreatePool()),
321 maSize(100,100),
322 maBackgroundColor(0xffffffff),
323 maTextColor(0x00000000),
324 msText(),
325 mnTop(0),
326 mnTotalHeight(-1)
328 mpOutputDevice->SetMapMode(MAP_PIXEL);
330 GetEditEngine();
336 PresenterTextView::Implementation::~Implementation (void)
338 delete mpEditEngine;
339 SfxItemPool::Free(mpEditEngineItemPool);
340 delete mpOutputDevice;
346 EditEngine * PresenterTextView::Implementation::GetEditEngine (void)
348 if (mpEditEngine == NULL)
349 mpEditEngine = CreateEditEngine ();
350 return mpEditEngine;
356 EditEngine* PresenterTextView::Implementation::CreateEditEngine (void)
358 EditEngine* pEditEngine = mpEditEngine;
359 if (pEditEngine == NULL)
362 // set fonts to be used
364 SvtLinguOptions aOpt;
365 SvtLinguConfig().GetOptions( aOpt );
367 struct FontDta {
368 INT16 nFallbackLang;
369 INT16 nLang;
370 USHORT nFontType;
371 USHORT nFontInfoId;
372 } aTable[3] =
374 // info to get western font to be used
375 { LANGUAGE_ENGLISH_US, LANGUAGE_NONE,
376 DEFAULTFONT_SERIF, EE_CHAR_FONTINFO },
377 // info to get CJK font to be used
378 { LANGUAGE_JAPANESE, LANGUAGE_NONE,
379 DEFAULTFONT_CJK_TEXT, EE_CHAR_FONTINFO_CJK },
380 // info to get CTL font to be used
381 { LANGUAGE_ARABIC_SAUDI_ARABIA, LANGUAGE_NONE,
382 DEFAULTFONT_CTL_TEXT, EE_CHAR_FONTINFO_CTL }
384 aTable[0].nLang = aOpt.nDefaultLanguage;
385 aTable[1].nLang = aOpt.nDefaultLanguage_CJK;
386 aTable[2].nLang = aOpt.nDefaultLanguage_CTL;
388 for (int i = 0; i < 3; ++i)
390 const FontDta &rFntDta = aTable[i];
391 LanguageType nLang = (LANGUAGE_NONE == rFntDta.nLang) ?
392 rFntDta.nFallbackLang : rFntDta.nLang;
393 Font aFont = Application::GetDefaultDevice()->GetDefaultFont(
394 rFntDta.nFontType, nLang, DEFAULTFONT_FLAGS_ONLYONE);
395 mpEditEngineItemPool->SetPoolDefaultItem(
396 SvxFontItem(
397 aFont.GetFamily(),
398 aFont.GetName(),
399 aFont.GetStyleName(),
400 aFont.GetPitch(),
401 aFont.GetCharSet(),
402 rFntDta.nFontInfoId));
406 pEditEngine = new EditEngine (mpEditEngineItemPool);
408 pEditEngine->EnableUndo (TRUE);
409 pEditEngine->SetDefTab (USHORT(
410 Application::GetDefaultDevice()->GetTextWidth(
411 UniString::CreateFromAscii("XXXX"))));
413 pEditEngine->SetControlWord(
414 (pEditEngine->GetControlWord()
415 | EE_CNTRL_AUTOINDENTING) &
416 (~EE_CNTRL_UNDOATTRIBS) &
417 (~EE_CNTRL_PASTESPECIAL));
419 pEditEngine->SetWordDelimiters (
420 UniString::CreateFromAscii(" .=+-*/(){}[];\""));
421 pEditEngine->SetRefMapMode (MAP_PIXEL);
422 pEditEngine->SetPaperSize (Size(800, 0));
423 pEditEngine->EraseVirtualDevice();
424 pEditEngine->ClearModifyFlag();
427 return pEditEngine;
433 void PresenterTextView::Implementation::SetCanvas (const cppcanvas::CanvasSharedPtr& rpCanvas)
435 mpCanvas = rpCanvas;
436 mxBitmap = NULL;
442 void PresenterTextView::Implementation::SetSize (const Size aSize)
444 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
446 maSize = aSize;
447 mpEditEngine->SetPaperSize(maSize);
448 mnTotalHeight = -1;
449 mxBitmap = NULL;
455 void PresenterTextView::Implementation::SetBackgroundColor (const Color aColor)
457 maBackgroundColor = aColor;
458 mxBitmap = NULL;
460 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
461 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
462 mpEditEngine->SetBackgroundColor(aColor);
463 mpEditEngine->EnableAutoColor(FALSE);
464 mpEditEngine->ForceAutoColor(FALSE);
470 void PresenterTextView::Implementation::SetTextColor (const Color aColor)
472 maTextColor = aColor;
473 mxBitmap = NULL;
475 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
476 mpEditEngineItemPool->SetPoolDefaultItem(SvxColorItem(aColor, EE_CHAR_COLOR));
482 void PresenterTextView::Implementation::SetFontDescriptor (
483 const awt::FontDescriptor& rFontDescriptor)
485 mxBitmap = NULL;
487 DBG_ASSERT(mpEditEngineItemPool!=NULL, "EditEngineItemPool missing");
489 const sal_Int32 nFontHeight = rFontDescriptor.Height;
491 SvxFontHeightItem aFontHeight(
492 Application::GetDefaultDevice()->LogicToPixel(
493 Size(0, nFontHeight), MapMode (MAP_POINT)).Height(),
494 100,
495 EE_CHAR_FONTHEIGHT);
496 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
497 aFontHeight.SetWhich (EE_CHAR_FONTHEIGHT_CJK);
498 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
499 aFontHeight.SetWhich (EE_CHAR_FONTHEIGHT_CTL);
500 mpEditEngineItemPool->SetPoolDefaultItem( aFontHeight);
502 SvxFontItem aSvxFontItem (EE_CHAR_FONTINFO);
503 aSvxFontItem.GetFamilyName() = rFontDescriptor.Name;
504 mpEditEngineItemPool->SetPoolDefaultItem(aSvxFontItem);
506 mnTotalHeight = -1;
507 mxBitmap = NULL;
509 CheckTop();
510 mnTotalHeight = -1;
516 sal_Int32 PresenterTextView::Implementation::GetTop (void) const
518 return mnTop;
524 void PresenterTextView::Implementation::SetTop (const sal_Int32 nTop)
526 if (nTop == mnTop)
527 return;
529 mnTop = nTop;
530 mxBitmap = NULL;
531 CheckTop();
537 void PresenterTextView::Implementation::ClearText (void)
539 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
540 msText = OUString();
541 mnTotalHeight = 0;
542 mxBitmap = NULL;
548 void PresenterTextView::Implementation::SetText (const OUString& rText)
550 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
551 msText = rText;
552 mpEditEngine->SetPaperSize(maSize);
553 mnTotalHeight = -1;
554 mxBitmap = NULL;
560 sal_Int32 PresenterTextView::Implementation::ParseDistance (const OUString& rsDistance) const
562 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
563 sal_Int32 nDistance (0);
564 if (rsDistance.endsWithAsciiL("px", 2))
566 nDistance = rsDistance.copy(0,rsDistance.getLength()-2).toInt32();
568 else if (rsDistance.endsWithAsciiL("l", 1))
570 const sal_Int32 nLines (rsDistance.copy(0,rsDistance.getLength()-1).toInt32());
571 // Take the height of the first line as the height of every line.
572 const sal_uInt32 nFirstLineHeight (mpEditEngine->GetLineHeight(0,0));
573 nDistance = nFirstLineHeight * nLines;
576 return nDistance;
582 Reference<rendering::XBitmap> PresenterTextView::Implementation::GetBitmap (void)
584 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
586 if ( ! mxBitmap.is())
588 if (mpOutputDevice != NULL)
589 delete mpOutputDevice;
590 mpOutputDevice = new VirtualDevice(*Application::GetDefaultDevice(), 0, 0);
591 mpOutputDevice->SetMapMode(MAP_PIXEL);
592 mpOutputDevice->SetOutputSizePixel(maSize, TRUE);
593 mpOutputDevice->SetLineColor();
594 mpOutputDevice->SetFillColor();
595 mpOutputDevice->SetBackground(Wallpaper());
596 mpOutputDevice->Erase();
598 MapMode aMapMode (mpOutputDevice->GetMapMode());
599 aMapMode.SetOrigin(Point(0,0));
600 mpOutputDevice->SetMapMode(aMapMode);
601 const Rectangle aWindowBox (Point(0,0), maSize);
602 mpOutputDevice->DrawRect(aWindowBox);
604 mpEditEngine->Clear();
605 mpEditEngine->SetText(msText);
606 mpEditEngine->SetPaperSize(maSize);
608 mpEditEngine->Draw(mpOutputDevice, aWindowBox, Point(0,mnTop));
610 const BitmapEx aBitmap (mpOutputDevice->GetBitmapEx(Point(0,0), maSize));
611 mxBitmap = cppcanvas::VCLFactory::getInstance().createBitmap(
612 mpCanvas,
613 aBitmap
614 )->getUNOBitmap();
616 return mxBitmap;
622 sal_Int32 PresenterTextView::Implementation::GetTotalHeight (void)
624 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
626 if (mnTotalHeight < 0)
628 if ( ! mxBitmap.is())
629 GetBitmap();
630 mnTotalHeight = mpEditEngine->GetTextHeight();
632 return mnTotalHeight;
638 void PresenterTextView::Implementation::CheckTop (void)
640 DBG_ASSERT(mpEditEngine!=NULL, "EditEngine missing");
642 if (mnTotalHeight < 0)
643 mnTotalHeight = mpEditEngine->GetTextHeight();
644 if (mpEditEngine!=NULL && mnTop >= mnTotalHeight)
645 mnTop = mnTotalHeight - mpEditEngine->GetLineHeight(0,0);
647 if (mnTotalHeight < maSize.Height())
648 mnTop = 0;
650 if (mnTotalHeight - mnTop < maSize.Height())
651 mnTop = mnTotalHeight - maSize.Height();
653 if (mnTop < 0)
654 mnTop = 0;
658 } } // end of namespace ::sd::presenter