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 
24 #include <sfx2/sidebar/Theme.hxx>
25 
26 #include <i18npool/mslangid.hxx>
27 
28 namespace svx { namespace sidebar {
29 
30 ValueSetWithTextControl::ValueSetWithTextControl(
31     const tControlType eControlType,
32     Window* pParent,
33     const ResId& rResId)
34     : ValueSet( pParent, rResId )
35     , meControlType( eControlType )
36     , maItems()
37 {
38     SetColCount( 1 );
39 }
40 
41 
42 ValueSetWithTextControl::~ValueSetWithTextControl(void)
43 {
44 }
45 
46 
47 void ValueSetWithTextControl::AddItem(
48     const Image& rItemImage,
49     const Image* pSelectedItemImage,
50     const XubString& rItemText,
51     const XubString* pItemHelpText )
52 {
53     if ( meControlType != IMAGE_TEXT )
54     {
55         return;
56     }
57 
58     ValueSetWithTextItem aItem;
59     aItem.maItemImage = rItemImage;
60     aItem.maSelectedItemImage = (pSelectedItemImage != 0)
61                                 ? *pSelectedItemImage
62                                 : rItemImage;
63     aItem.maItemText = rItemText;
64 
65     maItems.push_back( aItem );
66 
67     InsertItem( maItems.size() );
68     SetItemText( maItems.size(),
69                     (pItemHelpText != 0) ? *pItemHelpText : rItemText );
70 }
71 
72 
73 void ValueSetWithTextControl::AddItem(
74     const XubString& rItemText,
75     const XubString& rItemText2,
76     const XubString* pItemHelpText )
77 {
78     if ( meControlType != TEXT_TEXT )
79     {
80         return;
81     }
82 
83     ValueSetWithTextItem aItem;
84     aItem.maItemText = rItemText;
85     aItem.maItemText2 = rItemText2;
86 
87     maItems.push_back( aItem );
88 
89     InsertItem( maItems.size() );
90     SetItemText( maItems.size(),
91                     (pItemHelpText != 0) ? *pItemHelpText : rItemText );
92 }
93 
94 
95 void ValueSetWithTextControl::ReplaceItemImages(
96     const sal_uInt16 nItemId,
97     const Image& rItemImage,
98     const Image* pSelectedItemImage )
99 {
100     if ( meControlType != IMAGE_TEXT )
101     {
102         return;
103     }
104 
105     if ( nItemId == 0 ||
106          nItemId > maItems.size() )
107     {
108         return;
109     }
110 
111     maItems[nItemId-1].maItemImage = rItemImage;
112     maItems[nItemId-1].maSelectedItemImage = (pSelectedItemImage != 0)
113                                              ? *pSelectedItemImage
114                                              : rItemImage;
115 }
116 
117 
118 void ValueSetWithTextControl::UserDraw( const UserDrawEvent& rUDEvt )
119 {
120     const Rectangle aRect = rUDEvt.GetRect();
121     OutputDevice* pDev = rUDEvt.GetDevice();
122     pDev->Push( PUSH_ALL );
123     const sal_uInt16 nItemId = rUDEvt.GetItemId();
124 
125     const long nRectHeight = aRect.GetHeight();
126     const Point aBLPos = aRect.TopLeft();
127 
128     Font aFont(OutputDevice::GetDefaultFont(DEFAULTFONT_UI_SANS, MsLangId::getSystemLanguage(), DEFAULTFONT_FLAGS_ONLYONE));
129     {
130         Size aSize = aFont.GetSize();
131         aSize.Height() = (nRectHeight*4)/9;
132         aFont.SetSize( aSize );
133     }
134 
135     {
136         //draw backgroud
137         if ( GetSelectItemId() == nItemId )
138         {
139             Rectangle aBackRect = aRect;
140             aBackRect.Top() += 3;
141             aBackRect.Bottom() -= 2;
142             pDev->SetFillColor( sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Color_Highlight ) );
143             pDev->DrawRect(aBackRect);
144         }
145         else
146         {
147             pDev->SetFillColor( COL_TRANSPARENT );
148             pDev->DrawRect(aRect);
149         }
150 
151         //draw image + text resp. text + text
152         Image* pImage = 0;
153         if ( GetSelectItemId() == nItemId )
154         {
155             aFont.SetColor( sfx2::sidebar::Theme::GetColor( sfx2::sidebar::Theme::Color_HighlightText ) );
156             pImage = &maItems[nItemId-1].maSelectedItemImage;
157         }
158         else
159         {
160             aFont.SetColor( GetSettings().GetStyleSettings().GetFieldTextColor() );
161             pImage = &maItems[nItemId-1].maItemImage;
162         }
163 
164         Rectangle aStrRect = aRect;
165         aStrRect.Top() += nRectHeight/4;
166         aStrRect.Bottom() -= nRectHeight/4;
167 
168         switch ( meControlType )
169         {
170         case IMAGE_TEXT:
171             {
172                 Point aImgStart(
173                     aBLPos.X() + 4,
174                     aBLPos.Y() + ( ( nRectHeight - pImage->GetSizePixel().Height() ) / 2 ) );
175                 pDev->DrawImage( aImgStart, *pImage );
176 
177                 aStrRect.Left() += pImage->GetSizePixel().Width() + 12;
178                 pDev->SetFont(aFont);
179                 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText, TEXT_DRAW_ENDELLIPSIS);
180             }
181             break;
182         case TEXT_TEXT:
183             {
184                 const long nRectWidth = aRect.GetWidth();
185                 aStrRect.Left() += 8;
186                 aStrRect.Right() -= (nRectWidth*2)/3;
187                 pDev->SetFont(aFont);
188                 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText, TEXT_DRAW_ENDELLIPSIS);
189                 aStrRect.Left() += nRectWidth/3;
190                 aStrRect.Right() += (nRectWidth*2)/3;
191                 pDev->DrawText(aStrRect, maItems[nItemId-1].maItemText2, TEXT_DRAW_ENDELLIPSIS);
192             }
193             break;
194         }
195     }
196 
197     Invalidate( aRect );
198     pDev->Pop();
199 }
200 
201 } } // end of namespace svx::sidebar
202