176868517055307a9d5e7b50ffdfa27d6405db94
[vuplus_dvbapp] / lib / gui / epixmap.cpp
1 #include <lib/gui/epixmap.h>
2 #include <lib/gdi/epng.h>
3 #include <lib/gui/ewidgetdesktop.h>
4
5 ePixmap::ePixmap(eWidget *parent)
6         :eWidget(parent), m_alphatest(false), m_scale(false), m_have_border_color(false), m_border_width(0)
7 {
8 }
9
10 void ePixmap::setAlphatest(int alphatest)
11 {
12         m_alphatest = alphatest;
13         setTransparent(alphatest);
14 }
15
16 void ePixmap::setScale(int scale)
17 {
18         if (m_scale != scale)
19         {
20                 m_scale = scale;
21                 invalidate();
22         }
23 }
24
25 void ePixmap::setPixmap(gPixmap *pixmap)
26 {
27         m_pixmap = pixmap;
28         event(evtChangedPixmap);
29 }
30
31 void ePixmap::setPixmap(ePtr<gPixmap> &pixmap)
32 {
33         m_pixmap = pixmap;
34         event(evtChangedPixmap);
35 }
36
37 void ePixmap::setPixmapFromFile(const char *filename)
38 {
39         loadPNG(m_pixmap, filename);
40         
41         if (!m_pixmap)
42         {
43                 eDebug("ePixmap::setPixmapFromFile: loadPNG failed");
44                 return;
45         }
46         
47                 // TODO: This only works for desktop 0
48         getDesktop(0)->makeCompatiblePixmap(*m_pixmap);
49         event(evtChangedPixmap);
50 }
51
52 void ePixmap::setBorderWidth(int pixel)
53 {
54         m_border_width=pixel;
55         invalidate();
56 }
57
58 void ePixmap::setBorderColor(const gRGB &color)
59 {
60         m_border_color=color;
61         m_have_border_color=true;
62         invalidate();
63 }
64
65 void ePixmap::checkSize()
66 {
67                         /* when we have no pixmap, or a pixmap of different size, we need 
68            to enable transparency in any case. */
69         if (m_pixmap && m_pixmap->size() == size() && !m_alphatest)
70                 setTransparent(0);
71         else
72                 setTransparent(1);
73                 /* fall trough. */
74 }
75
76 int ePixmap::event(int event, void *data, void *data2)
77 {
78         switch (event)
79         {
80         case evtPaint:
81         {
82                 ePtr<eWindowStyle> style;
83
84                 eSize s(size());
85                 getStyle(style);
86
87 //      we don't clear the background before because of performance reasons.
88 //      when the pixmap is too small to fit the whole widget area, the widget is
89 //      transparent anyway, so the background is already painted.
90 //              eWidget::event(event, data, data2); 
91
92                 gPainter &painter = *(gPainter*)data2;
93                 if (m_pixmap)
94                 {
95                         int flags = 0;
96                         if (m_alphatest == 0)
97                                 flags = 0;
98                         else if (m_alphatest == 1)
99                                 flags = gPainter::BT_ALPHATEST;
100                         else if (m_alphatest == 2)
101                                 flags = gPainter::BT_ALPHABLEND;
102                         if (m_scale)
103                                 painter.blitScale(m_pixmap, eRect(ePoint(0, 0), size()), eRect(), flags);
104                         else
105                                 painter.blit(m_pixmap, ePoint(0, 0), eRect(), flags);
106                 }
107
108 // border
109                 if (m_have_border_color)
110                         painter.setForegroundColor(m_border_color);
111                 painter.fill(eRect(0, 0, s.width(), m_border_width));
112                 painter.fill(eRect(0, m_border_width, m_border_width, s.height()-m_border_width));
113                 painter.fill(eRect(m_border_width, s.height()-m_border_width, s.width()-m_border_width, m_border_width));
114                 painter.fill(eRect(s.width()-m_border_width, m_border_width, m_border_width, s.height()-m_border_width));
115
116                 return 0;
117         }
118         case evtChangedPixmap:
119                 checkSize();
120                 invalidate();
121                 return 0;
122         case evtChangedSize:
123                 checkSize();
124                         /* fall trough. */
125         default:
126                 return eWidget::event(event, data, data2);
127         }
128 }