xref: /AOO41X/main/svx/source/sidebar/tools/ValueSetWithTextControl.cxx (revision 5443dcac4da55ae8863c5c80e8907938642a7f1b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "svx/sidebar/ValueSetWithTextControl.hxx"
23 #include <svx/dialogs.hrc>
24 #include <svx/dialmgr.hxx>
25 #include <sfx2/sidebar/Theme.hxx>
26 
27 #include <limits.h>
28 #include <com/sun/star/uno/Reference.h>
29 #include <com/sun/star/uno/Sequence.h>
30 #include <com/sun/star/lang/Locale.hpp>
31 #include <com/sun/star/style/NumberingType.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/container/XIndexAccess.hpp>
34 #include <com/sun/star/text/XDefaultNumberingProvider.hpp>
35 #include <com/sun/star/text/XNumberingFormatter.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <comphelper/processfactory.hxx>
38 #include <com/sun/star/text/XNumberingTypeInfo.hpp>
39 #include <i18npool/mslangid.hxx>
40 #include <svtools/valueset.hxx>
41 #include <editeng/brshitem.hxx>
42 #include <vcl/graph.hxx>
43 #include <svx/nbdtmg.hxx>
44 #include <svx/nbdtmgfact.hxx>
45 
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::beans;
48 using namespace com::sun::star::lang;
49 using namespace com::sun::star::i18n;
50 using namespace com::sun::star::text;
51 using namespace com::sun::star::container;
52 using namespace com::sun::star::style;
53 using rtl::OUString;
54 
55 #define C2U(cChar) OUString::createFromAscii(cChar)
56 
57 namespace svx { namespace sidebar {
58 static const sal_Char cValue[] = "Value";
59 
60 static Font& lcl_GetDefaultBulletFont()
61 {
62     static sal_Bool bInit = 0;
63     static Font aDefBulletFont( UniString::CreateFromAscii(
64                                 RTL_CONSTASCII_STRINGPARAM( "StarSymbol" ) ),
65                                 String(), Size( 0, 14 ) );
66     if(!bInit)
67     {
68         aDefBulletFont.SetCharSet( RTL_TEXTENCODING_SYMBOL );
69         aDefBulletFont.SetFamily( FAMILY_DONTKNOW );
70         aDefBulletFont.SetPitch( PITCH_DONTKNOW );
71         aDefBulletFont.SetWeight( WEIGHT_DONTKNOW );
72         aDefBulletFont.SetTransparent( sal_True );
73         bInit = sal_True;
74     }
75     return aDefBulletFont;
76 }
77 
78 ValueSetWithTextControl::ValueSetWithTextControl(
79     const tControlType eControlType,
80     Window* pParent,
81     const ResId& rResId)
82     : ValueSet( pParent, rResId )
83     , meControlType( eControlType )
84     , maItems()
85 {
86     SetColCount( 1 );
87 }
88 
89 
90 ValueSetWithTextControl::~ValueSetWithTextControl(void)
91 {
92 }
93 
94 
95 void ValueSetWithTextControl::AddItem(
96     const Image& rItemImage,
97     const Image* pSelectedItemImage,
98     const XubString& rItemText,
99     const XubString* pItemHelpText )
100 {
101     if ( meControlType != IMAGE_TEXT )
102     {
103         return;
104     }
105 
106     ValueSetWithTextItem aItem;
107     aItem.maItemImage = rItemImage;
108     aItem.maSelectedItemImage = (pSelectedItemImage != 0)
109                                 ? *pSelectedItemImage
110                                 : rItemImage;
111     aItem.maItemText = rItemText;
112 
113     maItems.push_back( aItem );
114 
115     InsertItem( maItems.size() );
116     SetItemText( maItems.size(),
117                     (pItemHelpText != 0) ? *pItemHelpText : rItemText );
118 }
119 
120 
121 void ValueSetWithTextControl::AddItem(
122     const XubString& rItemText,
123     const XubString& rItemText2,
124     const XubString* pItemHelpText )
125 {
126     if ( meControlType != TEXT_TEXT )
127     {
128         return;
129     }
130 
131     ValueSetWithTextItem aItem;
132     aItem.maItemText = rItemText;
133     aItem.maItemText2 = rItemText2;
134 
135     maItems.push_back( aItem );
136 
137     InsertItem( maItems.size() );
138     SetItemText( maItems.size(),
139                     (pItemHelpText != 0) ? *pItemHelpText : rItemText );
140 }
141 
142 
143 void ValueSetWithTextControl::ReplaceItemImages(
144     const sal_uInt16 nItemId,
145     const Image& rItemImage,
146     const Image* pSelectedItemImage )
147 {
148     if ( meControlType != IMAGE_TEXT )
149     {
150         return;
151     }
152 
153     if ( nItemId == 0 ||
154          nItemId > maItems.size() )
155     {
156         return;
157     }
158 
159     maItems[nItemId-1].maItemImage = rItemImage;
160     maItems[nItemId-1].maSelectedItemImage = (pSelectedItemImage != 0)
161                                              ? *pSelectedItemImage
162                                              : rItemImage;
163 }
164 
165 
166 void ValueSetWithTextControl::UserDraw( const UserDrawEvent& rUDEvt )
167 {
168     const Rectangle aRect = rUDEvt.GetRect();
169     OutputDevice* pDev = rUDEvt.GetDevice();
170     pDev->Push( PUSH_ALL );
171     const sal_uInt16 nItemId = rUDEvt.GetItemId();
172 
173     const long nRectHeight = aRect.GetHeight();
174     const Point aBLPos = aRect.TopLeft();
175 
176     Font aFont(OutputDevice::GetDefaultFont(DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
177     {
178         Size aSize = aFont.GetSize();
179         aSize.Height() = (nRectHeight*4)/9;
180         aFont.SetSize( aSize );
181     }
182 
183     {
184         //draw backgroud
185         if ( GetSelectItemId() == nItemId )
186         {
187             Rectangle aBackRect = aRect;
188             aBackRect.Top() += 3;
189             aBackRect.Bottom() -= 2;
190             pDev->SetFillColor( sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Color_Highlight ) );
191             pDev->DrawRect(aBackRect);
192         }
193         else
194         {
195             pDev->SetFillColor( COL_TRANSPARENT );
196             pDev->DrawRect(aRect);
197         }
198 
199         //draw image + text resp. text + text
200         Image* pImage = 0;
201         if ( GetSelectItemId() == nItemId )
202         {
203             aFont.SetColor( sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Color_HighlightText ) );
204             pImage = &maItems[nItemId-1].maSelectedItemImage;
205         }
206         else
207         {
208             aFont.SetColor( GetSettings().GetStyleSettings().GetFieldTextColor() );
209             pImage = &maItems[nItemId-1].maItemImage;
210         }
211 
212         Rectangle aStrRect = aRect;
213         aStrRect.Top() += nRectHeight/4;
214         aStrRect.Bottom() -= nRectHeight/4;
215 
216         switch ( meControlType )
217         {
218         case IMAGE_TEXT:
219             {
220                 Point aImgStart(
221                     aBLPos.X() + 4,
222                     aBLPos.Y() + ( ( nRectHeight - pImage->GetSizePixel().Height() ) / 2 ) );
223                 pDev->DrawImage( aImgStart, *pImage );
224 
225                 aStrRect.Left() += pImage->GetSizePixel().Width() + 12;
226                 pDev->SetFont(aFont);
227                 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText, TEXT_DRAW_ENDELLIPSIS);
228             }
229             break;
230         case TEXT_TEXT:
231             {
232                 const long nRectWidth = aRect.GetWidth();
233                 aStrRect.Left() += 8;
234                 aStrRect.Right() -= (nRectWidth*2)/3;
235                 pDev->SetFont(aFont);
236                 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText, TEXT_DRAW_ENDELLIPSIS);
237                 aStrRect.Left() += nRectWidth/3;
238                 aStrRect.Right() += (nRectWidth*2)/3;
239                 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText2, TEXT_DRAW_ENDELLIPSIS);
240             }
241             break;
242         }
243     }
244 
245     Invalidate( aRect );
246     pDev->Pop();
247 }
248 
249 SvxNumValueSet2::SvxNumValueSet2( Window* pParent, const ResId& rResId) :
250     ValueSet( pParent, rResId ),
251     aLineColor  ( COL_LIGHTGRAY ),
252     pVDev       ( NULL )
253 {
254     SetColCount( 3 );
255     SetLineCount( 3 );
256     SetStyle( GetStyle() | WB_ITEMBORDER );
257 }
258 
259  SvxNumValueSet2::~SvxNumValueSet2()
260 {
261     delete pVDev;
262 }
263 
264 void SvxNumValueSet2::SetNumberingSettings(
265     const Sequence<Sequence<PropertyValue> >& aNum,
266     Reference<XNumberingFormatter>& xFormat,
267     const Locale& rLocale   )
268 {
269     aNumSettings = aNum;
270     xFormatter = xFormat;
271     aLocale = rLocale;
272         if(aNum.getLength() > 9)
273         SetStyle( GetStyle()|WB_VSCROLL);
274     InsertItem( DEFAULT_NONE, DEFAULT_NONE - 1 );
275     SetItemText( DEFAULT_NONE, SVX_RESSTR( RID_SVXSTR_NUMBULLET_NONE ));
276 
277         for ( sal_Int32 i = 0; i < aNum.getLength(); i++ )
278         {
279             InsertItem( i + 1);
280                 if( i < 8 )
281                 {
282             NBOTypeMgrBase* pNumbering = NBOutlineTypeMgrFact::CreateInstance(eNBOType::NUMBERING);
283             if ( pNumbering )
284             {
285                 SetItemText( i + 1, pNumbering->GetDescription(i));
286             }
287                 }
288     }
289 }
290 
291 void  SvxNumValueSet2::UserDraw( const UserDrawEvent& rUDEvt )
292 {
293     const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
294     const Color aBackColor = rStyleSettings.GetFieldColor();
295     const Color aTextColor = rStyleSettings.GetFieldTextColor();
296 
297     OutputDevice*  pDev = rUDEvt.GetDevice();
298     Rectangle aRect = rUDEvt.GetRect();
299     sal_uInt32  nItemId = rUDEvt.GetItemId();
300     long nRectWidth = aRect.GetWidth();
301     long nRectHeight = aRect.GetHeight();
302     Size aRectSize(nRectWidth, aRect.GetHeight());
303     Point aBLPos = aRect.TopLeft();
304     Font aOldFont = pDev->GetFont();
305     Color aOldColor = pDev->GetLineColor();
306     pDev->SetLineColor(aBackColor);
307     Font aFont(OutputDevice::GetDefaultFont(
308                 DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
309 
310     Size aSize = aFont.GetSize();
311     aSize.Height() = nRectHeight/5;
312     aFont.SetColor(aTextColor);
313     aFont.SetFillColor(aBackColor);
314     aFont.SetSize( aSize );
315     pDev->SetFont(aFont);
316     pDev->SetLineColor(aTextColor);
317     if(!pVDev)
318     {
319         // Die Linien werden nur einmalig in das VirtualDevice gepainted
320         // nur die Gliederungspage bekommt es aktuell
321         pVDev = new VirtualDevice(*pDev);
322         pVDev->SetMapMode(pDev->GetMapMode());
323         pVDev->EnableRTL( IsRTLEnabled() );
324         pVDev->SetOutputSize( aRectSize );
325         aOrgRect = aRect;
326 
327         pVDev->SetLineColor( aBackColor );
328         pVDev->SetFillColor( aBackColor );
329         pVDev->DrawRect(aOrgRect);
330 
331         if(aBackColor == aLineColor)
332             aLineColor.Invert();
333         if(GetSettings().GetStyleSettings().GetHighContrastMode())
334             pVDev->SetLineColor(aTextColor);
335         else
336             pVDev->SetLineColor(aLineColor);
337         // Linien nur einmalig Zeichnen
338         Point aStart(aBLPos.X() + nRectWidth *30 / 100,0);
339         Point aEnd(aBLPos.X() + nRectWidth * 9 / 10,0);
340         for( sal_uInt32 i = 11; i < 100; i += 33)
341         {
342             aStart.Y() = aEnd.Y() = aBLPos.Y() + nRectHeight  * i / 100;
343             pVDev->DrawLine(aStart, aEnd);
344             aStart.Y() = aEnd.Y() = aBLPos.Y() + nRectHeight  * (i + 11) / 100;
345             pVDev->DrawLine(aStart, aEnd);
346         }
347     }
348     if ( nItemId != DEFAULT_NONE)
349         pDev->DrawOutDev(   aRect.TopLeft(), aRectSize,
350                             aOrgRect.TopLeft(), aRectSize,
351                             *pVDev );
352     const OUString sValue(C2U(cValue));
353 
354     Point aStart(aBLPos.X() + nRectWidth / 9,0);
355     if ( nItemId == DEFAULT_NONE)
356     {
357         String sText(SVX_RESSTR( RID_SVXSTR_NUMBULLET_NONE));
358         Font aFont = pDev->GetFont();
359         Size aSize = aFont.GetSize();
360         aSize.Height() = nRectHeight/4;
361         aFont.SetSize( aSize );
362         pDev->SetFont(aFont);
363         long nTextWidth = pDev->GetTextWidth(sText);
364         long nTextHeight = pDev->GetTextHeight();
365         //GVT refine
366         while (nTextWidth>nRectWidth && aSize.Height()>4) {
367             aSize.Height() = aSize.Height()*0.9;
368             aFont.SetSize( aSize );
369             pDev->SetFont(aFont);
370             nTextWidth = pDev->GetTextWidth(sText);
371         }
372         Point aSStart(aBLPos.X()+(nRectWidth-nTextWidth)/2, aBLPos.Y() +(nRectHeight-nTextHeight)/2);
373         pDev->DrawText(aSStart, sText);
374         pDev->SetFont(aOldFont);
375     }
376     else
377     {
378         NBOTypeMgrBase* pNumbering = NBOutlineTypeMgrFact::CreateInstance(eNBOType::NUMBERING);
379         if ( pNumbering && nItemId <= DEFAULT_BULLET_TYPES )
380         {
381             for( sal_uInt32 i = 0; i < 3; i++ )
382             {
383                 sal_uInt32 nY = 11 + i * 33;
384                 aStart.Y() = aBLPos.Y() + nRectHeight  * nY / 100;
385                 String sText;
386                 sal_uInt16 nLvl = 0;
387                 SvxNumRule aTempRule( 0, 10, false );
388                 pNumbering->ApplyNumRule(aTempRule,nItemId -1,1<<nLvl);
389                 SvxNumberFormat aNumFmt(aTempRule.GetLevel(nLvl));
390                 sText=aNumFmt.GetNumStr(i+1);
391                 sText.Insert( aNumFmt.GetPrefix(), 0 );
392                 sText += aNumFmt.GetSuffix();
393                 aStart.X() = aBLPos.X() + 2;
394                 aStart.Y() -= pDev->GetTextHeight()/2;
395                 pDev->DrawText(aStart, sText);
396             }
397         }
398         pDev->SetFont(aOldFont);
399         pDev->SetLineColor(aOldColor);
400     }
401     //End
402 }
403 
404 //===============================================================================================
405 
406 SvxNumValueSet3::SvxNumValueSet3( Window* pParent, const ResId& rResId) :
407     ValueSet( pParent, rResId )
408 {
409     SetColCount( 3 );
410     SetLineCount( 4 );
411     SetStyle( GetStyle() | WB_ITEMBORDER );
412 }
413 
414  SvxNumValueSet3::~SvxNumValueSet3()
415 {
416 }
417 
418 void  SvxNumValueSet3::UserDraw( const UserDrawEvent& rUDEvt )
419 {
420     Rectangle aRect = rUDEvt.GetRect();
421     OutputDevice*  pDev = rUDEvt.GetDevice();
422     sal_uInt32  nItemId = rUDEvt.GetItemId();
423 
424     long nRectHeight = aRect.GetHeight();
425     long nRectWidth = aRect.GetWidth();
426     Point aBLPos = aRect.TopLeft();
427     NBOTypeMgrBase* pBullets = NBOutlineTypeMgrFact::CreateInstance(eNBOType::MIXBULLETS);
428     if ( pBullets )
429     {
430         if ( nItemId <= DEFAULT_BULLET_TYPES ) {
431             sal_uInt16 nLvl = 0;
432             SvxNumRule aTempRule( 0, 10, false );
433             pBullets->ApplyNumRule(aTempRule,nItemId -1,1<<nLvl);
434             SvxNumberFormat aFmt(aTempRule.GetLevel(nLvl));
435             sal_Int16 eNumType = aFmt.GetNumberingType();
436             if( eNumType == SVX_NUM_CHAR_SPECIAL)
437             {
438                 sal_Unicode cChar = aFmt.GetBulletChar();
439             //End
440                 const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
441                 const Color aBackColor = rStyleSettings.GetFieldColor();
442                 const Color aTextColor = rStyleSettings.GetFieldTextColor();
443 
444                 Font aOldFont = pDev->GetFont();
445                 Font aFont( lcl_GetDefaultBulletFont() );
446 
447                 Size aSize = aFont.GetSize();
448                 aSize.Height() = nRectHeight*3/6;
449                 aFont.SetColor(aTextColor);
450                 aFont.SetFillColor(aBackColor);
451                 aFont.SetSize( aSize );
452                 pDev->SetFont(aFont);
453                 pDev->SetFillColor( aBackColor ); //wj
454 
455                 String sText;
456                 sText = cChar;
457                 Font aOldBulletFont = pDev->GetFont();
458                 Font aBulletFnt(aFmt.GetBulletFont() ? *aFmt.GetBulletFont() : aOldBulletFont);
459                 Size aBulSize = aOldBulletFont.GetSize();
460                 aBulletFnt.SetSize(aBulSize);
461                 pDev->SetFont(aBulletFnt);
462                 long nTextWidth = pDev->GetTextWidth(sText);
463                 long nTextHeight = pDev->GetTextHeight();
464                 Point aStart(aBLPos.X()+(nRectWidth-nTextWidth)/2, aBLPos.Y() +(nRectHeight-nTextHeight)/2);
465                 pDev->DrawText(aStart, sText);
466                 pDev->SetFont(aOldFont);
467             }else if ( eNumType == SVX_NUM_BITMAP )
468             {
469                 const SvxBrushItem* pBrushItem = aFmt.GetBrush();
470                 if(pBrushItem)
471                 {
472                     const Graphic* pGrf = pBrushItem->GetGraphic();
473                     if(pGrf)
474                     {
475                         Size aSize(nRectHeight*6/20, nRectHeight*6/20);
476                         Point aStart(aBLPos.X() + nRectWidth*7/20, aBLPos.Y() + nRectHeight*7/20);
477 
478                         pGrf->Draw( pDev, aStart, aSize );
479                     }
480                 }
481             }
482         }else if ( nItemId == DEFAULT_NONE)
483         {
484             const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
485             const Color aBackColor = rStyleSettings.GetFieldColor();
486             const Color aTextColor = rStyleSettings.GetFieldTextColor();
487 
488             Font aOldFont = pDev->GetFont();
489             Font aFont(OutputDevice::GetDefaultFont(DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
490             Size aSize = aFont.GetSize();
491             //aSize.Height() = nRectHeight/5;
492             aSize.Height() = nRectHeight/4;
493             aFont.SetColor(aTextColor);
494             aFont.SetFillColor(aBackColor);
495             aFont.SetSize( aSize );
496             pDev->SetFont(aFont);
497             pDev->SetFillColor( aBackColor );
498 
499             String sText(SVX_RESSTR( RID_SVXSTR_NUMBULLET_NONE));
500 
501             long nTextWidth = pDev->GetTextWidth(sText);
502             long nTextHeight = pDev->GetTextHeight();
503             //GVT refine
504             while (nTextWidth>nRectWidth && aSize.Height()>4) {
505                 aSize.Height() = aSize.Height()*0.9;
506                 aFont.SetSize( aSize );
507                 pDev->SetFont(aFont);
508                 nTextWidth = pDev->GetTextWidth(sText);
509             }
510             Point aStart(aBLPos.X()+(nRectWidth-nTextWidth)/2, aBLPos.Y() +(nRectHeight-nTextHeight)/2);
511             pDev->DrawText(aStart, sText);
512 
513             pDev->SetFont(aOldFont);
514         }
515     }
516 
517 }
518 
519 } } // end of namespace svx::sidebar
520