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