small cleanuo
[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, gPixmap &pixmap)
218 {
219         if ((bs >= bsMax) || (bs < 0))
220                 return;
221         
222         int i = 0;
223         for (int b = 1; b < bpMax; b <<= 1, ++i)
224         {
225                 if (bp & b)
226                         m_border[bs].m_pixmap[i] = &pixmap;
227         }
228         
229                 /* recalc border sizes */
230         m_border[bs].m_border_top = 0;
231         m_border[bs].m_border_left = 0;
232         m_border[bs].m_border_bottom = 0;
233         m_border[bs].m_border_right = 0;
234         
235         for (int i = 0; i < 3; ++i)
236                 if (m_border[bs].m_pixmap[i])
237                         if (m_border[bs].m_border_top < m_border[bs].m_pixmap[i]->size().height())
238                                 m_border[bs].m_border_top = m_border[bs].m_pixmap[i]->size().height();
239         for (int i = 6; i < 9; ++i)
240                 if (m_border[bs].m_pixmap[i])
241                         if (m_border[bs].m_border_bottom < m_border[bs].m_pixmap[i]->size().height())
242                                 m_border[bs].m_border_bottom = m_border[bs].m_pixmap[i]->size().height();
243
244         if (m_border[bs].m_pixmap[3])
245                 m_border[bs].m_border_left = m_border[bs].m_pixmap[3]->size().width();
246         else
247                 m_border[bs].m_border_left = 0;
248         
249         if (m_border[bs].m_pixmap[5])
250                 m_border[bs].m_border_right = m_border[bs].m_pixmap[5]->size().width();
251         else
252                 m_border[bs].m_border_right = 0;
253
254         eDebug("recalced border size for %d: %d:%d %d:%d",
255                 bs, 
256                 m_border[bs].m_border_left, m_border[bs].m_border_top, 
257                 m_border[bs].m_border_right, m_border[bs].m_border_bottom); 
258 }
259
260 void eWindowStyleSkinned::setColor(int what, const gRGB &col)
261 {
262         if ((what < colMax) && (what >= 0))
263                 m_color[what] = col;
264 }
265
266 void eWindowStyleSkinned::setTitleOffset(const eSize &offset)
267 {
268         m_title_offset = offset;
269 }
270
271 void eWindowStyleSkinned::setTitleFont(gFont *fnt)
272 {
273         m_fnt = fnt;
274 }
275