isCompatible with can now handle all frontend types
[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         }
74 }
75
76 void eWindowStyleSkinned::drawFrame(gPainter &painter, const eRect &frame, int what)
77 {
78         int bs;
79         switch (what)
80         {
81         case frameButton:
82                 bs = bsButton;
83                 break;
84         case frameListboxEntry:
85                 bs = bsListboxEntry;
86                 break;
87         default:
88                 eWarning("invalid frame style %d", what);
89                 return;
90         }
91         drawBorder(painter, frame, m_border[bs], bpAll);
92 }
93
94 void eWindowStyleSkinned::drawBorder(gPainter &painter, const eRect &pos, struct borderSet &border, int what)
95 {
96         int x = pos.left(), xm = pos.right();
97         
98         ePtr<gPixmap> 
99                 &tl = border.m_pixmap[bpiTopLeft],
100                 &t  = border.m_pixmap[bpiTop],
101                 &tr = border.m_pixmap[bpiTopRight],
102                 &l  = border.m_pixmap[bpiLeft],
103                 &r  = border.m_pixmap[bpiRight],
104                 &bl = border.m_pixmap[bpiBottomLeft],
105                 &b  = border.m_pixmap[bpiBottom],
106                 &br = border.m_pixmap[bpiBottomRight];
107         
108         if (tl)
109         {
110                 painter.blit(tl, ePoint(x, pos.top()));
111                 x += tl->size().width();
112         }
113         
114         if (tr)
115         {
116                 xm -= tr->size().width();
117                 painter.blit(tr, ePoint(xm, pos.top()), pos);
118         }
119
120         if (t)
121         {
122                 while (x < xm)
123                 {
124                         painter.blit(t, ePoint(x, pos.top()), eRect(x, pos.top(), xm - x, pos.height()));
125                         x += t->size().width();
126                 }
127         }
128         
129         x = pos.left();
130         xm = pos.right();
131         
132         if (bl)
133         {
134                 painter.blit(bl, ePoint(pos.left(), pos.bottom()-bl->size().height()));
135                 x += bl->size().width();
136         }
137         
138         if (br)
139         {
140                 xm -= br->size().width();
141                 painter.blit(br, ePoint(xm, pos.bottom()-br->size().height()), eRect(x, pos.bottom()-br->size().height(), pos.width() - x, bl->size().height()));
142         }
143         
144         if (b)
145         {
146                 while (x < xm)
147                 {
148                         painter.blit(b, ePoint(x, pos.bottom()-b->size().height()), eRect(x, pos.bottom()-b->size().height(), xm - x, pos.height()));
149                         x += b->size().width();
150                 }
151         }
152         
153         int y = 0;
154         if (tl)
155                 y = tl->size().height();
156         
157         y += pos.top();
158         
159         int ym = pos.bottom();
160         if (bl)
161                 ym -= bl->size().height();
162         
163         if (l)
164         {
165                 while (y < ym)
166                 {
167                         painter.blit(l, ePoint(pos.left(), y), eRect(pos.left(), y, pos.width(), ym - y));
168                         y += l->size().height();
169                 }
170         }
171         
172         y = 0;
173         
174         if (tr)
175                 y = tr->size().height();
176         
177         y += pos.top();
178         
179         ym = pos.bottom();
180         if (br)
181                 ym -= br->size().height();
182         
183         if (r)
184         {
185                 while (y < ym)
186                 {
187                         painter.blit(r, ePoint(pos.right() - r->size().width(), y), eRect(pos.right()-r->size().width(), y, r->size().width(), ym - y));
188                         y += r->size().height();
189                 }
190         }
191 }
192
193 RESULT eWindowStyleSkinned::getFont(int what, ePtr<gFont> &fnt)
194 {
195         fnt = 0;
196         switch (what)
197         {
198         case fontStatic:
199                 fnt = new gFont("Arial", 12);
200                 break;
201         case fontButton:
202                 fnt = new gFont("Arial", 20);
203                 break;
204         case fontTitlebar:
205                 fnt = new gFont("Arial", 25);
206                 break;
207         default:
208                 return -1;
209         }
210         return 0;
211 }
212
213 void eWindowStyleSkinned::setPixmap(int bs, int bp, gPixmap &pixmap)
214 {
215         if ((bs >= bsMax) || (bs < 0))
216                 return;
217         
218         int i = 0;
219         for (int b = 1; b < bpMax; b <<= 1, ++i)
220         {
221                 if (bp & b)
222                         m_border[bs].m_pixmap[i] = &pixmap;
223         }
224         
225                 /* recalc border sizes */
226         m_border[bs].m_border_top = 0;
227         m_border[bs].m_border_left = 0;
228         m_border[bs].m_border_bottom = 0;
229         m_border[bs].m_border_right = 0;
230         
231         for (int i = 0; i < 3; ++i)
232                 if (m_border[bs].m_pixmap[i])
233                         if (m_border[bs].m_border_top < m_border[bs].m_pixmap[i]->size().height())
234                                 m_border[bs].m_border_top = m_border[bs].m_pixmap[i]->size().height();
235         for (int i = 6; i < 9; ++i)
236                 if (m_border[bs].m_pixmap[i])
237                         if (m_border[bs].m_border_bottom < m_border[bs].m_pixmap[i]->size().height())
238                                 m_border[bs].m_border_bottom = m_border[bs].m_pixmap[i]->size().height();
239         for (int i = 0; i < 9; i += 3)
240                 if (m_border[bs].m_pixmap[i])
241                         if (m_border[bs].m_border_left < m_border[bs].m_pixmap[i]->size().width())
242                                 m_border[bs].m_border_left = m_border[bs].m_pixmap[i]->size().width();
243         for (int i = 2; i < 9; i += 3)
244                 if (m_border[bs].m_pixmap[i])
245                         if (m_border[bs].m_border_right < m_border[bs].m_pixmap[i]->size().width())
246                                 m_border[bs].m_border_right = m_border[bs].m_pixmap[i]->size().width();
247         eDebug("recalced border size for %d: %d:%d %d:%d",
248                 bs, 
249                 m_border[bs].m_border_left, m_border[bs].m_border_top, 
250                 m_border[bs].m_border_right, m_border[bs].m_border_bottom); 
251 }
252
253 void eWindowStyleSkinned::setColor(int what, const gRGB &col)
254 {
255         if ((what < colMax) && (what >= 0))
256                 m_color[what] = col;
257 }
258
259 void eWindowStyleSkinned::setTitleOffset(const eSize &offset)
260 {
261         m_title_offset = offset;
262 }
263
264 void eWindowStyleSkinned::setTitleFont(gFont *fnt)
265 {
266         m_fnt = fnt;
267 }
268