also use refcounting for eTimers
[enigma2.git] / 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)
7 {
8 }
9
10 void ePixmap::setAlphatest(int alphatest)
11 {
12         m_alphatest = alphatest;
13         setTransparent(alphatest);
14 }
15
16 void ePixmap::setPixmap(gPixmap *pixmap)
17 {
18         m_pixmap = pixmap;
19         event(evtChangedPixmap);
20 }
21
22 void ePixmap::setPixmap(ePtr<gPixmap> &pixmap)
23 {
24         m_pixmap = pixmap;
25         event(evtChangedPixmap);
26 }
27
28 void ePixmap::setPixmapFromFile(const char *filename)
29 {
30         loadPNG(m_pixmap, filename);
31         
32         if (!m_pixmap)
33         {
34                 eDebug("ePixmap::setPixmapFromFile: loadPNG failed");
35                 return;
36         }
37         
38                 // TODO: This only works for desktop 0
39         getDesktop(0)->makeCompatiblePixmap(*m_pixmap);
40         event(evtChangedPixmap);
41 }
42
43 void ePixmap::checkSize()
44 {
45                         /* when we have no pixmap, or a pixmap of different size, we need 
46            to enable transparency in any case. */
47         if (m_pixmap && m_pixmap->size() == size() && !m_alphatest)
48                 setTransparent(0);
49         else
50                 setTransparent(1);
51                 /* fall trough. */
52 }
53
54 int ePixmap::event(int event, void *data, void *data2)
55 {
56         switch (event)
57         {
58         case evtPaint:
59         {
60                 ePtr<eWindowStyle> style;
61                 
62                 getStyle(style);
63
64 //      we don't clear the background before because of performance reasons.
65 //      when the pixmap is too small to fit the whole widget area, the widget is
66 //      transparent anyway, so the background is already painted.
67 //              eWidget::event(event, data, data2); 
68
69                 gPainter &painter = *(gPainter*)data2;
70                 if (m_pixmap)
71                 {
72                         int flags = 0;
73                         if (m_alphatest == 0)
74                                 flags = 0;
75                         else if (m_alphatest == 1)
76                                 flags = gPainter::BT_ALPHATEST;
77                         else if (m_alphatest == 2)
78                                 flags = gPainter::BT_ALPHABLEND;
79                         painter.blit(m_pixmap, ePoint(0, 0), eRect(), flags);
80                 }
81
82                 return 0;
83         }
84         case evtChangedPixmap:
85                 checkSize();
86                 invalidate();
87                 return 0;
88         case evtChangedSize:
89                 checkSize();
90                         /* fall trough. */
91         default:
92                 return eWidget::event(event, data, data2);
93         }
94 }