aef09a2e54e849e5c4de886a5fb881022f911b48
[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         eDebug("setPage");
56         m_dvb_page = p;
57         invalidate(m_visible_region);  // invalidate old visible regions
58         m_visible_region.rects.clear();
59         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
60         {
61                 eDebug("add %d %d %d %d", it->m_position.x(), it->m_position.y(), it->m_pixmap->size().width(), it->m_pixmap->size().height());
62                 m_visible_region.rects.push_back(eRect(it->m_position, it->m_pixmap->size()));
63         }
64         m_dvb_page_ok = 1;
65         m_hide_subtitles_timer.start(7500, true);
66         invalidate(m_visible_region);  // invalidate new regions
67 }
68
69 void eSubtitleWidget::clearPage()
70 {
71         eDebug("subtitle timeout... hide");
72         m_page_ok = 0;
73         m_dvb_page_ok = 0;
74         invalidate(m_visible_region);
75         m_visible_region.rects.clear();
76 }
77
78 int eSubtitleWidget::event(int event, void *data, void *data2)
79 {
80         switch (event)
81         {
82         case evtPaint:
83         {
84                 ePtr<eWindowStyle> style;
85                 gPainter &painter = *(gPainter*)data2;
86
87                 getStyle(style);
88                 
89                 eWidget::event(event, data, data2);
90                 ePtr<gFont> font = new gFont("Regular", 38);
91                 painter.setFont(font);
92                 
93                 if (m_page_ok)
94                 {
95                         int elements = m_page.m_elements.size();
96                         for (int i=0; i<elements; ++i)
97                         {
98                                 eDVBTeletextSubtitlePageElement &element = m_page.m_elements[i];
99                                 eRect &area = element.m_area;
100                                 eRect shadow = area;
101                                 shadow.moveBy(3,3);
102                                 painter.setForegroundColor(gRGB(0,0,0));
103                                 painter.renderText(shadow, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
104                                 painter.setForegroundColor(element.m_color);
105                                 painter.renderText(area, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
106                         }
107                 }
108                 else if (m_dvb_page_ok)
109                 {
110                         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
111                                 painter.blit(it->m_pixmap, it->m_position);
112                 }
113                 return 0;
114         }
115         default:
116                 return eWidget::event(event, data, data2);
117         }
118 }