Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / sidebar / tools / ValueSetWithTextControl.cxx
blob17638b111f498ec75803cf5c9ed5b18b3dd7e663
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 .
19 #include "svx/sidebar/ValueSetWithTextControl.hxx"
20 #include <svx/dialogs.hrc>
21 #include <svx/dialmgr.hxx>
22 #include <sfx2/sidebar/Theme.hxx>
24 #include <limits.h>
25 #include <com/sun/star/uno/Reference.h>
26 #include <com/sun/star/uno/Sequence.h>
27 #include <com/sun/star/lang/Locale.hpp>
28 #include <com/sun/star/style/NumberingType.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/container/XIndexAccess.hpp>
31 #include <com/sun/star/text/XDefaultNumberingProvider.hpp>
32 #include <com/sun/star/text/XNumberingFormatter.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <com/sun/star/text/XNumberingTypeInfo.hpp>
36 #include <i18nlangtag/mslangid.hxx>
37 #include <svtools/valueset.hxx>
38 #include <editeng/brushitem.hxx>
39 #include <vcl/graph.hxx>
40 #include <vcl/settings.hxx>
41 #include <svx/nbdtmg.hxx>
42 #include <svx/nbdtmgfact.hxx>
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::beans;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::i18n;
48 using namespace com::sun::star::text;
49 using namespace com::sun::star::container;
50 using namespace com::sun::star::style;
52 namespace svx { namespace sidebar {
54 static Font& lcl_GetDefaultBulletFont()
56 static bool bInit = false;
57 static Font aDefBulletFont( OUString( "StarSymbol" ),
58 OUString(), Size( 0, 14 ) );
59 if(!bInit)
61 aDefBulletFont.SetCharSet( RTL_TEXTENCODING_SYMBOL );
62 aDefBulletFont.SetFamily( FAMILY_DONTKNOW );
63 aDefBulletFont.SetPitch( PITCH_DONTKNOW );
64 aDefBulletFont.SetWeight( WEIGHT_DONTKNOW );
65 aDefBulletFont.SetTransparent( true );
66 bInit = true;
68 return aDefBulletFont;
71 ValueSetWithTextControl::ValueSetWithTextControl(
72 const tControlType eControlType,
73 Window* pParent,
74 const ResId& rResId)
75 : ValueSet( pParent, rResId )
76 , meControlType( eControlType )
77 , maItems()
79 SetColCount( 1 );
83 ValueSetWithTextControl::~ValueSetWithTextControl(void)
88 void ValueSetWithTextControl::AddItem(
89 const Image& rItemImage,
90 const Image* pSelectedItemImage,
91 const OUString& rItemText,
92 const OUString* pItemHelpText )
94 if ( meControlType != IMAGE_TEXT )
96 return;
99 ValueSetWithTextItem aItem;
100 aItem.maItemImage = rItemImage;
101 aItem.maSelectedItemImage = (pSelectedItemImage != 0)
102 ? *pSelectedItemImage
103 : rItemImage;
105 if ( GetDPIScaleFactor() > 1 )
107 BitmapEx b = aItem.maItemImage.GetBitmapEx();
108 b.Scale(GetDPIScaleFactor(), GetDPIScaleFactor());
109 aItem.maItemImage = Image(b);
111 if ( pSelectedItemImage != 0 )
113 b = aItem.maSelectedItemImage.GetBitmapEx();
114 b.Scale(GetDPIScaleFactor(), GetDPIScaleFactor());
115 aItem.maSelectedItemImage = Image(b);
119 aItem.maItemText = rItemText;
121 maItems.push_back( aItem );
123 InsertItem( maItems.size() );
124 SetItemText( maItems.size(),
125 (pItemHelpText != 0) ? *pItemHelpText : rItemText );
129 void ValueSetWithTextControl::AddItem(
130 const OUString& rItemText,
131 const OUString& rItemText2,
132 const OUString* pItemHelpText )
134 if ( meControlType != TEXT_TEXT )
136 return;
139 ValueSetWithTextItem aItem;
140 aItem.maItemText = rItemText;
141 aItem.maItemText2 = rItemText2;
143 maItems.push_back( aItem );
145 InsertItem( maItems.size() );
146 SetItemText( maItems.size(),
147 (pItemHelpText != 0) ? *pItemHelpText : rItemText );
151 void ValueSetWithTextControl::ReplaceItemImages(
152 const sal_uInt16 nItemId,
153 const Image& rItemImage,
154 const Image* pSelectedItemImage )
156 if ( meControlType != IMAGE_TEXT )
158 return;
161 if ( nItemId == 0 ||
162 nItemId > maItems.size() )
164 return;
167 maItems[nItemId-1].maItemImage = rItemImage;
168 maItems[nItemId-1].maSelectedItemImage = (pSelectedItemImage != 0)
169 ? *pSelectedItemImage
170 : rItemImage;
172 //#ifndef MACOSX
173 if ( GetDPIScaleFactor() > 1 )
175 BitmapEx b = maItems[nItemId-1].maItemImage.GetBitmapEx();
176 b.Scale(GetDPIScaleFactor(), GetDPIScaleFactor());
177 maItems[nItemId-1].maItemImage = Image(b);
179 if ( pSelectedItemImage != 0 )
181 b = maItems[nItemId-1].maSelectedItemImage.GetBitmapEx();
182 b.Scale(GetDPIScaleFactor(), GetDPIScaleFactor());
183 maItems[nItemId-1].maSelectedItemImage = Image(b);
186 //#endif
190 void ValueSetWithTextControl::UserDraw( const UserDrawEvent& rUDEvt )
192 const Rectangle aRect = rUDEvt.GetRect();
193 OutputDevice* pDev = rUDEvt.GetDevice();
194 pDev->Push( PUSH_ALL );
195 const sal_uInt16 nItemId = rUDEvt.GetItemId();
197 const long nRectHeight = aRect.GetHeight();
198 const Point aBLPos = aRect.TopLeft();
200 Font aFont(OutputDevice::GetDefaultFont(DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
202 Size aSize = aFont.GetSize();
203 aSize.Height() = (nRectHeight*4)/9;
204 aFont.SetSize( aSize );
208 //draw backgroud
209 if ( GetSelectItemId() == nItemId )
211 Rectangle aBackRect = aRect;
212 aBackRect.Top() += 3;
213 aBackRect.Bottom() -= 2;
214 pDev->SetFillColor( sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Color_Highlight ) );
215 pDev->DrawRect(aBackRect);
217 else
219 pDev->SetFillColor( COL_TRANSPARENT );
220 pDev->DrawRect(aRect);
223 //draw image + text resp. text + text
224 Image* pImage = 0;
225 if ( GetSelectItemId() == nItemId )
227 aFont.SetColor( sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Color_HighlightText ) );
228 pImage = &maItems[nItemId-1].maSelectedItemImage;
230 else
232 aFont.SetColor( GetSettings().GetStyleSettings().GetFieldTextColor() );
233 pImage = &maItems[nItemId-1].maItemImage;
236 Rectangle aStrRect = aRect;
237 aStrRect.Top() += nRectHeight/4;
238 aStrRect.Bottom() -= nRectHeight/4;
240 switch ( meControlType )
242 case IMAGE_TEXT:
244 Point aImgStart(
245 aBLPos.X() + 4,
246 aBLPos.Y() + ( ( nRectHeight - pImage->GetSizePixel().Height() ) / 2 ) );
247 pDev->DrawImage( aImgStart, *pImage );
249 aStrRect.Left() += pImage->GetSizePixel().Width() + 12;
250 pDev->SetFont(aFont);
251 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText, TEXT_DRAW_ENDELLIPSIS);
253 break;
254 case TEXT_TEXT:
256 const long nRectWidth = aRect.GetWidth();
257 aStrRect.Left() += 8;
258 aStrRect.Right() -= (nRectWidth*2)/3;
259 pDev->SetFont(aFont);
260 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText, TEXT_DRAW_ENDELLIPSIS);
261 aStrRect.Left() += nRectWidth/3;
262 aStrRect.Right() += (nRectWidth*2)/3;
263 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText2, TEXT_DRAW_ENDELLIPSIS);
265 break;
269 Invalidate( aRect );
270 pDev->Pop();
273 SvxNumValueSet2::SvxNumValueSet2( Window* pParent, const ResId& rResId) :
274 ValueSet( pParent, rResId ),
275 aLineColor ( COL_LIGHTGRAY ),
276 pVDev ( NULL )
278 SetColCount( 3 );
279 SetLineCount( 3 );
280 SetStyle( GetStyle() | WB_ITEMBORDER );
283 SvxNumValueSet2::~SvxNumValueSet2()
285 delete pVDev;
288 void SvxNumValueSet2::SetNumberingSettings(
289 const Sequence<Sequence<PropertyValue> >& aNum,
290 Reference<XNumberingFormatter>& xFormat,
291 const Locale& rLocale )
293 aNumSettings = aNum;
294 xFormatter = xFormat;
295 aLocale = rLocale;
296 if(aNum.getLength() > 9)
297 SetStyle( GetStyle()|WB_VSCROLL);
298 InsertItem( DEFAULT_NONE, DEFAULT_NONE - 1 );
299 SetItemText( DEFAULT_NONE, SVX_RESSTR( RID_SVXSTR_NUMBULLET_NONE ));
301 for ( sal_Int32 i = 0; i < aNum.getLength(); i++ )
303 InsertItem( i + 1);
304 if( i < 8 )
306 NBOTypeMgrBase* pNumbering = NBOutlineTypeMgrFact::CreateInstance(eNBOType::NUMBERING);
307 if ( pNumbering )
309 SetItemText( i + 1, pNumbering->GetDescription(i));
315 void SvxNumValueSet2::UserDraw( const UserDrawEvent& rUDEvt )
317 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
318 const Color aBackColor = rStyleSettings.GetFieldColor();
319 const Color aTextColor = rStyleSettings.GetFieldTextColor();
321 OutputDevice* pDev = rUDEvt.GetDevice();
322 Rectangle aRect = rUDEvt.GetRect();
323 sal_uInt32 nItemId = rUDEvt.GetItemId();
324 long nRectWidth = aRect.GetWidth();
325 long nRectHeight = aRect.GetHeight();
326 Size aRectSize(nRectWidth, aRect.GetHeight());
327 Point aBLPos = aRect.TopLeft();
328 Font aOldFont = pDev->GetFont();
329 Color aOldColor = pDev->GetLineColor();
330 pDev->SetLineColor(aBackColor);
331 Font aFont(OutputDevice::GetDefaultFont(
332 DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
334 Size aSize = aFont.GetSize();
335 aSize.Height() = nRectHeight/5;
336 aFont.SetColor(aTextColor);
337 aFont.SetFillColor(aBackColor);
338 aFont.SetSize( aSize );
339 pDev->SetFont(aFont);
340 pDev->SetLineColor(aTextColor);
341 if(!pVDev)
343 // Die Linien werden nur einmalig in das VirtualDevice gepainted
344 // nur die Gliederungspage bekommt es aktuell
345 pVDev = new VirtualDevice(*pDev);
346 pVDev->SetMapMode(pDev->GetMapMode());
347 pVDev->EnableRTL( IsRTLEnabled() );
348 pVDev->SetOutputSize( aRectSize );
349 aOrgRect = aRect;
351 pVDev->SetLineColor( aBackColor );
352 pVDev->SetFillColor( aBackColor );
353 pVDev->DrawRect(aOrgRect);
355 if(aBackColor == aLineColor)
356 aLineColor.Invert();
357 if(GetSettings().GetStyleSettings().GetHighContrastMode())
358 pVDev->SetLineColor(aTextColor);
359 else
360 pVDev->SetLineColor(aLineColor);
361 // Linien nur einmalig Zeichnen
362 Point aStart(aBLPos.X() + nRectWidth *30 / 100,0);
363 Point aEnd(aBLPos.X() + nRectWidth * 9 / 10,0);
364 for( sal_uInt32 i = 11; i < 100; i += 33)
366 aStart.Y() = aEnd.Y() = aBLPos.Y() + nRectHeight * i / 100;
367 pVDev->DrawLine(aStart, aEnd);
368 aStart.Y() = aEnd.Y() = aBLPos.Y() + nRectHeight * (i + 11) / 100;
369 pVDev->DrawLine(aStart, aEnd);
372 if ( nItemId != DEFAULT_NONE)
373 pDev->DrawOutDev( aRect.TopLeft(), aRectSize,
374 aOrgRect.TopLeft(), aRectSize,
375 *pVDev );
377 Point aStart(aBLPos.X() + nRectWidth / 9,0);
378 if ( nItemId == DEFAULT_NONE)
380 OUString sText(SVX_RESSTR( RID_SVXSTR_NUMBULLET_NONE));
381 Font aLclFont = pDev->GetFont();
382 Size aLclSize = aLclFont.GetSize();
383 aLclSize.Height() = nRectHeight/4;
384 aLclFont.SetSize( aLclSize );
385 pDev->SetFont(aLclFont);
386 long nTextWidth = pDev->GetTextWidth(sText);
387 long nTextHeight = pDev->GetTextHeight();
388 //GVT refine
389 while (nTextWidth>nRectWidth && aLclSize.Height()>4) {
390 aLclSize.Height() = aLclSize.Height()*0.9;
391 aLclFont.SetSize( aLclSize );
392 pDev->SetFont(aLclFont);
393 nTextWidth = pDev->GetTextWidth(sText);
395 Point aSStart(aBLPos.X()+(nRectWidth-nTextWidth)/2, aBLPos.Y() +(nRectHeight-nTextHeight)/2);
396 pDev->DrawText(aSStart, sText);
397 pDev->SetFont(aOldFont);
399 else
401 NBOTypeMgrBase* pNumbering = NBOutlineTypeMgrFact::CreateInstance(eNBOType::NUMBERING);
402 if ( pNumbering && nItemId <= DEFAULT_BULLET_TYPES )
404 for( sal_uInt32 i = 0; i < 3; i++ )
406 sal_uInt32 nY = 11 + i * 33;
407 aStart.Y() = aBLPos.Y() + nRectHeight * nY / 100;
408 OUString sText;
409 sal_uInt16 nLvl = 0;
410 SvxNumRule aTempRule( 0, 10, false );
411 pNumbering->ApplyNumRule(aTempRule,nItemId -1,1<<nLvl);
412 SvxNumberFormat aNumFmt(aTempRule.GetLevel(nLvl));
413 sText = aNumFmt.GetPrefix() + aNumFmt.GetNumStr(i+1) + aNumFmt.GetSuffix();
414 aStart.X() = aBLPos.X() + 2;
415 aStart.Y() -= pDev->GetTextHeight()/2;
416 pDev->DrawText(aStart, sText);
419 pDev->SetFont(aOldFont);
420 pDev->SetLineColor(aOldColor);
422 //End
427 SvxNumValueSet3::SvxNumValueSet3( Window* pParent, const ResId& rResId) :
428 ValueSet( pParent, rResId )
430 SetColCount( 3 );
431 SetLineCount( 4 );
432 SetStyle( GetStyle() | WB_ITEMBORDER );
435 SvxNumValueSet3::~SvxNumValueSet3()
439 void SvxNumValueSet3::UserDraw( const UserDrawEvent& rUDEvt )
441 Rectangle aRect = rUDEvt.GetRect();
442 OutputDevice* pDev = rUDEvt.GetDevice();
443 sal_uInt32 nItemId = rUDEvt.GetItemId();
445 long nRectHeight = aRect.GetHeight();
446 long nRectWidth = aRect.GetWidth();
447 Point aBLPos = aRect.TopLeft();
448 NBOTypeMgrBase* pBullets = NBOutlineTypeMgrFact::CreateInstance(eNBOType::MIXBULLETS);
449 if ( pBullets )
451 if ( nItemId <= DEFAULT_BULLET_TYPES ) {
452 sal_uInt16 nLvl = 0;
453 SvxNumRule aTempRule( 0, 10, false );
454 pBullets->ApplyNumRule(aTempRule,nItemId -1,1<<nLvl);
455 SvxNumberFormat aFmt(aTempRule.GetLevel(nLvl));
456 sal_Int16 eNumType = aFmt.GetNumberingType();
457 if( eNumType == SVX_NUM_CHAR_SPECIAL)
459 sal_Unicode cChar = aFmt.GetBulletChar();
460 //End
461 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
462 const Color aBackColor = rStyleSettings.GetFieldColor();
463 const Color aTextColor = rStyleSettings.GetFieldTextColor();
465 Font aOldFont = pDev->GetFont();
466 Font aFont( lcl_GetDefaultBulletFont() );
468 Size aSize = aFont.GetSize();
469 aSize.Height() = nRectHeight*3/6;
470 aFont.SetColor(aTextColor);
471 aFont.SetFillColor(aBackColor);
472 aFont.SetSize( aSize );
473 pDev->SetFont(aFont);
474 pDev->SetFillColor( aBackColor ); //wj
476 OUString sText(cChar);
477 Font aOldBulletFont = pDev->GetFont();
478 Font aBulletFnt(aFmt.GetBulletFont() ? *aFmt.GetBulletFont() : aOldBulletFont);
479 Size aBulSize = aOldBulletFont.GetSize();
480 aBulletFnt.SetSize(aBulSize);
481 pDev->SetFont(aBulletFnt);
482 long nTextWidth = pDev->GetTextWidth(sText);
483 long nTextHeight = pDev->GetTextHeight();
484 Point aStart(aBLPos.X()+(nRectWidth-nTextWidth)/2, aBLPos.Y() +(nRectHeight-nTextHeight)/2);
485 pDev->DrawText(aStart, sText);
486 pDev->SetFont(aOldFont);
487 }else if ( eNumType == SVX_NUM_BITMAP )
489 const SvxBrushItem* pBrushItem = aFmt.GetBrush();
490 if(pBrushItem)
492 const Graphic* pGrf = pBrushItem->GetGraphic();
493 if(pGrf)
495 Size aSize(nRectHeight*6/20, nRectHeight*6/20);
496 Point aStart(aBLPos.X() + nRectWidth*7/20, aBLPos.Y() + nRectHeight*7/20);
498 pGrf->Draw( pDev, aStart, aSize );
502 }else if ( nItemId == DEFAULT_NONE)
504 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
505 const Color aBackColor = rStyleSettings.GetFieldColor();
506 const Color aTextColor = rStyleSettings.GetFieldTextColor();
508 Font aOldFont = pDev->GetFont();
509 Font aFont(OutputDevice::GetDefaultFont(DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
510 Size aSize = aFont.GetSize();
511 //aSize.Height() = nRectHeight/5;
512 aSize.Height() = nRectHeight/4;
513 aFont.SetColor(aTextColor);
514 aFont.SetFillColor(aBackColor);
515 aFont.SetSize( aSize );
516 pDev->SetFont(aFont);
517 pDev->SetFillColor( aBackColor );
519 OUString sText(SVX_RESSTR( RID_SVXSTR_NUMBULLET_NONE));
521 long nTextWidth = pDev->GetTextWidth(sText);
522 long nTextHeight = pDev->GetTextHeight();
523 //GVT refine
524 while (nTextWidth>nRectWidth && aSize.Height()>4) {
525 aSize.Height() = aSize.Height()*0.9;
526 aFont.SetSize( aSize );
527 pDev->SetFont(aFont);
528 nTextWidth = pDev->GetTextWidth(sText);
530 Point aStart(aBLPos.X()+(nRectWidth-nTextWidth)/2, aBLPos.Y() +(nRectHeight-nTextHeight)/2);
531 pDev->DrawText(aStart, sText);
533 pDev->SetFont(aOldFont);
539 } } // end of namespace svx::sidebar
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */