(ralfk) media player actions
[enigma2.git] / lib / gui / esubtitle.cpp
1 #include <lib/gui/esubtitle.h>
2 #include <lib/gdi/grc.h>
3 #include <lib/base/estring.h>
4
5         /*
6                 ok, here's much room for improvements.
7         
8                 first, the placing of the individual elements is sub-optimal.
9                 then maybe a colored background would be an option.
10                 ....
11         */      
12
13 eSubtitleWidget::eSubtitleWidget(eWidget *parent)
14         : eWidget(parent), m_hide_subtitles_timer(eApp)
15 {
16         setBackgroundColor(gRGB(0,0,0,255));
17         m_page_ok = 0;
18         m_dvb_page_ok = 0;
19         CONNECT(m_hide_subtitles_timer.timeout, eSubtitleWidget::clearPage);
20 }
21
22 #define startX 50
23 void eSubtitleWidget::setPage(const eDVBTeletextSubtitlePage &p)
24 {
25         m_page = p;
26         m_page_ok = 1;
27         invalidate(m_visible_region);  // invalidate old visible regions
28         m_visible_region.rects.clear();
29
30         int elements = m_page.m_elements.size();
31         if (elements)
32         {
33                 int startY = elements > 1
34                         ? size().height() / 2
35                         : size().height() / 3 * 2;
36                 int width = size().width() - startX * 2;
37                 int height = size().height() - startY;
38                 int size_per_element = height / (elements ? elements : 1);
39                 for (int i=0; i<elements; ++i)
40                 {
41                         eRect &area = m_page.m_elements[i].m_area;
42                         area.setLeft(startX);
43                         area.setTop(size_per_element * i + startY);
44                         area.setWidth(width);
45                         area.setHeight(size_per_element);
46                         m_visible_region.rects.push_back(area);
47                 }
48         }
49         m_hide_subtitles_timer.start(7500, true);
50         invalidate(m_visible_region);  // invalidate new regions
51 }
52
53 void eSubtitleWidget::setPage(const eDVBSubtitlePage &p)
54 {
55         m_dvb_page = p;
56         invalidate(m_visible_region);  // invalidate old visible regions
57         m_visible_region.rects.clear();
58         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
59                 m_visible_region.rects.push_back(eRect(it->m_position, it->m_pixmap->size()));
60         m_dvb_page_ok = 1;
61         m_hide_subtitles_timer.start(7500, true);
62         invalidate(m_visible_region);  // invalidate new regions
63 }
64
65 void eSubtitleWidget::clearPage()
66 {
67         eDebug("subtitle timeout... hide");
68         m_page_ok = 0;
69         m_dvb_page_ok = 0;
70         invalidate(m_visible_region);
71         m_visible_region.rects.clear();
72 }
73
74 int eSubtitleWidget::event(int event, void *data, void *data2)
75 {
76         switch (event)
77         {
78         case evtPaint:
79         {
80                 ePtr<eWindowStyle> style;
81                 gPainter &painter = *(gPainter*)data2;
82
83                 getStyle(style);
84                 
85                 eWidget::event(event, data, data2);
86                 ePtr<gFont> font = new gFont("Regular", 38);
87                 painter.setFont(font);
88                 
89                 if (m_page_ok)
90                 {
91                         int elements = m_page.m_elements.size();
92                         for (int i=0; i<elements; ++i)
93                         {
94                                 eDVBTeletextSubtitlePageElement &element = m_page.m_elements[i];
95                                 eRect &area = element.m_area;
96                                 eRect shadow = area;
97                                 shadow.moveBy(3,3);
98                                 painter.setForegroundColor(gRGB(0,0,0));
99                                 painter.renderText(shadow, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
100                                 painter.setForegroundColor(element.m_color);
101                                 painter.renderText(area, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
102                         }
103                 }
104                 else if (m_dvb_page_ok)
105                 {
106                         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
107                                 painter.blit(it->m_pixmap, it->m_position);
108                 }
109                 return 0;
110         }
111         default:
112                 return eWidget::event(event, data, data2);
113         }
114 }