speedup resolveFilename a bit
[enigma2.git] / lib / gui / ewindowstyleskinned.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/gdi/esize.h>
3 #include <lib/gui/ewindow.h>
4 #include <lib/gui/ewindowstyle.h>
5 #include <lib/gui/ewindowstyleskinned.h>
6
7 DEFINE_REF(eWindowStyleSkinned);
8
9 eWindowStyleSkinned::eWindowStyleSkinned()
10 {
11         // m_background_color = gRGB(0x808080);
12         
13         // TODO: initialize colors!!
14 }
15
16 void eWindowStyleSkinned::handleNewSize(eWindow *wnd, eSize &size, eSize &offset)
17 {
18 //      eDebug("handle new size: %d x %d", size.width(), size.height());
19         
20         size = eSize(
21                         size.width() + m_border[bsWindow].m_border_left + m_border[bsWindow].m_border_right,
22                         size.height() + m_border[bsWindow].m_border_top + m_border[bsWindow].m_border_bottom
23                 );
24         
25         offset = eSize(-m_border[bsWindow].m_border_left, -m_border[bsWindow].m_border_top);
26         
27         eWidget *child = wnd->child();
28         
29         wnd->m_clip_region = eRect(ePoint(0, 0), size);
30         
31         child->move(ePoint(m_border[bsWindow].m_border_left, m_border[bsWindow].m_border_top));
32         child->resize(eSize(size.width() - m_border[bsWindow].m_border_left - m_border[bsWindow].m_border_right, size.height() - m_border[bsWindow].m_border_top - m_border[bsWindow].m_border_bottom));
33 }
34
35 void eWindowStyleSkinned::paintWindowDecoration(eWindow *wnd, gPainter &painter, const std::string &title)
36 {
37         drawBorder(painter, eRect(ePoint(0, 0), wnd->size()), m_border[bsWindow], bpAll);
38         
39         if (m_fnt)
40         {
41                 painter.setBackgroundColor(m_color[colWindowTitleBackground]);
42                 painter.setForegroundColor(m_color[colWindowTitleForeground]);
43                 painter.setFont(m_fnt);
44                 painter.renderText(eRect(m_title_offset.width(), m_title_offset.height(), wnd->size().width() - m_title_offset.width(), m_border[bsWindow].m_border_top - m_title_offset.height()), title);
45         }
46 }
47
48 void eWindowStyleSkinned::paintBackground(gPainter &painter, const ePoint &offset, const eSize &size)
49 {
50         painter.setBackgroundColor(m_color[colBackground]);
51         painter.clear();
52 }
53
54 void eWindowStyleSkinned::setStyle(gPainter &painter, int what)
55 {
56         switch (what)
57         {
58         case styleLabel:
59                 painter.setForegroundColor(m_color[colLabelForeground]);
60                 break;
61         case styleListboxSelected:
62                 painter.setForegroundColor(m_color[colListboxSelectedForeground]);
63                 painter.setBackgroundColor(m_color[colListboxSelectedBackground]);
64                 break;
65         case styleListboxNormal:
66                 painter.setForegroundColor(m_color[colListboxForeground]);
67                 painter.setBackgroundColor(m_color[colListboxBackground]);
68                 break;
69         case styleListboxMarked:
70                 painter.setForegroundColor(m_color[colListboxMarkedForeground]);
71                 painter.setBackgroundColor(m_color[colListboxMarkedBackground]);
72                 break;
73         case styleListboxMarkedAndSelected:
74                 painter.setForegroundColor(m_color[colListboxMarkedAndSelectedForeground]);
75                 painter.setBackgroundColor(m_color[colListboxMarkedAndSelectedBackground]);
76                 break;
77         }
78 }
79
80 void eWindowStyleSkinned::drawFrame(gPainter &painter, const eRect &frame, int what)
81 {
82         int bs;
83         switch (what)
84         {
85         case frameButton:
86                 bs = bsButton;
87                 break;
88         case frameListboxEntry:
89                 bs = bsListboxEntry;
90                 break;
91         default:
92                 eWarning("invalid frame style %d", what);
93                 return;
94         }
95         drawBorder(painter, frame, m_border[bs], bpAll);
96 }
97
98 void eWindowStyleSkinned::drawBorder(gPainter &painter, const eRect &pos, struct borderSet &border, int what)
99 {
100         int x = pos.left(), xm = pos.right();
101         
102         ePtr<gPixmap> 
103                 &tl = border.m_pixmap[bpiTopLeft],
104                 &t  = border.m_pixmap[bpiTop],
105                 &tr = border.m_pixmap[bpiTopRight],
106                 &l  = border.m_pixmap[bpiLeft],
107                 &r  = border.m_pixmap[bpiRight],
108                 &bl = border.m_pixmap[bpiBottomLeft],
109                 &b  = border.m_pixmap[bpiBottom],
110                 &br = border.m_pixmap[bpiBottomRight];
111         
112         if (tl)
113         {
114                 painter.blit(tl, ePoint(x, pos.top()));
115                 x += tl->size().width();
116         }
117         
118         if (tr)
119         {
120                 xm -= tr->size().width();
121                 painter.blit(tr, ePoint(xm, pos.top()), pos);
122         }
123
124         if (t)
125         {
126                 while (x < xm)
127                 {
128                         painter.blit(t, ePoint(x, pos.top()), eRect(x, pos.top(), xm - x, pos.height()));
129                         x += t->size().width();
130                 }
131         }
132         
133         x = pos.left();
134         xm = pos.right();
135         
136         if (bl)
137         {
138                 painter.blit(bl, ePoint(pos.left(), pos.bottom()-bl->size().height()));
139                 x += bl->size().width();
140         }
141         
142         if (br)
143         {
144                 xm -= br->size().width();
145                 painter.blit(br, ePoint(xm, pos.bottom()-br->size().height()), eRect(x, pos.bottom()-br->size().height(), pos.width() - x, bl->size().height()));
146         }
147         
148         if (b)
149         {
150                 while (x < xm)
151                 {
152                         painter.blit(b, ePoint(x, pos.bottom()-b->size().height()), eRect(x, pos.bottom()-b->size().height(), xm - x, pos.height()));
153                         x += b->size().width();
154                 }
155         }
156         
157         int y = 0;
158         if (tl)
159                 y = tl->size().height();
160         
161         y += pos.top();
162         
163         int ym = pos.bottom();
164         if (bl)
165                 ym -= bl->size().height();
166         
167         if (l)
168         {
169                 while (y < ym)
170                 {
171                         painter.blit(l, ePoint(pos.left(), y), eRect(pos.left(), y, pos.width(), ym - y));
172                         y += l->size().height();
173                 }
174         }
175         
176         y = 0;
177         
178         if (tr)
179                 y = tr->size().height();
180         
181         y += pos.top();
182         
183         ym = pos.bottom();
184         if (br)
185                 ym -= br->size().height();
186         
187         if (r)
188         {
189                 while (y < ym)
190                 {
191                         painter.blit(r, ePoint(pos.right() - r->size().width(), y), eRect(pos.right()-r->size().width(), y, r->size().width(), ym - y));
192                         y += r->size().height();
193                 }
194         }
195 }
196
197 RESULT eWindowStyleSkinned::getFont(int what, ePtr<gFont> &fnt)
198 {
199         fnt = 0;
200         switch (what)
201         {
202         case fontStatic:
203                 fnt = new gFont("Regular", 12);
204                 break;
205         case fontButton:
206                 fnt = new gFont("Regular", 20);
207                 break;
208         case fontTitlebar:
209                 fnt = new gFont("Regular", 25);
210                 break;
211         default:
212                 return -1;
213         }
214         return 0;
215 }
216
217 void eWindowStyleSkinned::setPixmap(int bs, int bp, ePtr<gPixmap> &ptr)
218 {
219         setPixmap(bs, bp, *(ptr.operator->()));
220 }
221
222 void eWindowStyleSkinned::setPixmap(int bs, int bp, gPixmap &pixmap)
223 {
224         if ((bs >= bsMax) || (bs < 0))
225                 return;
226         
227         int i = 0;
228         for (int b = 1; b < bpMax; b <<= 1, ++i)
229         {
230                 if (bp & b)
231                         m_border[bs].m_pixmap[i] = &pixmap;
232         }
233         
234                 /* recalc border sizes */
235         m_border[bs].m_border_top = 0;
236         m_border[bs].m_border_left = 0;
237         m_border[bs].m_border_bottom = 0;
238         m_border[bs].m_border_right = 0;
239         
240         for (int i = 0; i < 3; ++i)
241                 if (m_border[bs].m_pixmap[i])
242                         if (m_border[bs].m_border_top < m_border[bs].m_pixmap[i]->size().height())
243                                 m_border[bs].m_border_top = m_border[bs].m_pixmap[i]->size().height();
244         for (int i = 6; i < 9; ++i)
245                 if (m_border[bs].m_pixmap[i])
246                         if (m_border[bs].m_border_bottom < m_border[bs].m_pixmap[i]->size().height())
247                                 m_border[bs].m_border_bottom = m_border[bs].m_pixmap[i]->size().height();
248
249         if (m_border[bs].m_pixmap[3])
250                 m_border[bs].m_border_left = m_border[bs].m_pixmap[3]->size().width();
251         else
252                 m_border[bs].m_border_left = 0;
253         
254         if (m_border[bs].m_pixmap[5])
255                 m_border[bs].m_border_right = m_border[bs].m_pixmap[5]->size().width();
256         else
257                 m_border[bs].m_border_right = 0;
258
259 /*      eDebug("recalced border size for %d: %d:%d %d:%d",
260                 bs, 
261                 m_border[bs].m_border_left, m_border[bs].m_border_top, 
262                 m_border[bs].m_border_right, m_border[bs].m_border_bottom);  */
263 }
264
265 void eWindowStyleSkinned::setColor(int what, const gRGB &col)
266 {
267         if ((what < colMax) && (what >= 0))
268                 m_color[what] = col;
269 }
270
271 void eWindowStyleSkinned::setTitleOffset(const eSize &offset)
272 {
273         m_title_offset = offset;
274 }
275
276 void eWindowStyleSkinned::setTitleFont(gFont *fnt)
277 {
278         m_fnt = fnt;
279 }
280