]> git.vomp.tv Git - vompclient.git/blob - vtimerlist.cc
Fix for possible crash when removing bar
[vompclient.git] / vtimerlist.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "vtimerlist.h"
22
23 VTimerList::VTimerList()
24 {
25   recTimerList = NULL;
26
27   clockRegion.x = 420;
28   clockRegion.y = 0;
29   clockRegion.w = 150;
30   clockRegion.h = 30;
31
32   indicatorsRegion.x = 6;
33   indicatorsRegion.y = 44;
34   indicatorsRegion.w = 18;
35   indicatorsRegion.h = 15 * (surface->getFontHeight() + 1);
36
37   flipflop = true;
38
39   create(570, 420);
40   if (Video::getInstance()->getFormat() == Video::PAL)
41   {
42     setScreenPos(80, 70);
43   }
44   else
45   {
46     setScreenPos(70, 35);
47   }
48
49
50   setBackgroundColour(Colour::VIEWBACKGROUND);
51   setTitleBarOn(1);
52   setTitleText(tr("Timers"));
53   setTitleBarColour(Colour::TITLEBARBACKGROUND);
54
55   sl.setSurface(surface);
56   sl.setSurfaceOffset(30, 30 + 5);
57   sl.setDimensions(area.w - 40, area.h - 30 - 15 - 30);
58
59   // Draw statics
60
61   draw(); // View::draw
62
63   WSymbol w;
64   w.setSurface(surface);
65
66   w.nextSymbol = WSymbol::UP;
67   w.setSurfaceOffset(20, 385);
68   w.draw();
69
70   w.nextSymbol = WSymbol::DOWN;
71   w.setSurfaceOffset(50, 385);
72   w.draw();
73
74   w.nextSymbol = WSymbol::SKIPBACK;
75   w.setSurfaceOffset(85, 385);
76   w.draw();
77
78   w.nextSymbol = WSymbol::SKIPFORWARD;
79   w.setSurfaceOffset(115, 385);
80   w.draw();
81
82   drawTextRJ("[ok] = edit", 560, 385, Colour::LIGHTTEXT);
83
84   drawClock();
85 }
86
87 VTimerList::~VTimerList()
88 {
89   Timers::getInstance()->cancelTimer(this, 1);
90   if (recTimerList)
91   {
92     for (UINT i = 0; i < recTimerList->size(); i++)
93     {
94       delete (*recTimerList)[i];
95     }
96
97     recTimerList->clear();
98     delete recTimerList;
99   }
100 }
101
102 bool VTimerList::load()
103 {
104   recTimerList = VDR::getInstance()->getRecTimersList();
105
106   if (!recTimerList) return false;
107
108   char strA[300];
109   char strB[300];
110
111   struct tm* btime;
112
113   // FIXME all drawing stuff in this class and sl.clear somewhere?!
114
115   sl.addColumn(0);
116   sl.addColumn(110);
117
118   RecTimer* recTimer;
119   int first = 1;
120
121   for (UINT i = 0; i < recTimerList->size(); i++)
122   {
123     recTimer = (*recTimerList)[i];
124
125     btime = localtime((time_t*)&recTimer->startTime);
126     strftime(strA, 299, "%d/%m %H:%M ", btime);
127     SNPRINTF(strB, 299, "%s\t%s", strA, recTimer->getName());
128     sl.addOption(strB, (ULONG)recTimer, first);
129     first = 0;
130   }
131
132   sl.draw();
133   drawShowing();
134   drawIndicators();
135   ViewMan::getInstance()->updateView(this);
136   return true;
137 }
138
139 void VTimerList::drawClock()
140 {
141   // Blank the area first
142   rectangle(area.w - 150, 0, 150, 30, titleBarColour);
143
144   char timeString[20];
145   time_t t;
146   time(&t);
147   struct tm* tms = localtime(&t);
148   strftime(timeString, 19, "%d/%m %H:%M:%S", tms);
149   drawTextRJ(timeString, 560, 5, Colour::LIGHTTEXT);
150
151   Timers::getInstance()->setTimerT(this, 1, t + 1);
152 }
153
154 void VTimerList::drawShowing()
155 {
156   int topOption = sl.getTopOption() + 1;
157   if (sl.getNumOptions() == 0) topOption = 0;
158
159   rectangle(220, 385, 180, 25, Colour::VIEWBACKGROUND);
160   char showing[200];
161   sprintf(showing, tr("%i to %i of %i"), topOption, sl.getBottomOption(), sl.getNumOptions());
162   drawText(showing, 220, 385, Colour::LIGHTTEXT);
163 }
164
165 void VTimerList::drawIndicators()
166 {
167   int top = sl.getTopOption();
168   int bottom = sl.getBottomOption();
169   int yinc = surface->getFontHeight() + 1;
170   RecTimer* recTimer;
171
172   rectangle(6, 44, 18, 15*yinc, Colour::VIEWBACKGROUND);
173
174   // The indexes recorded from the wselectlist into the index member of the RecTimer
175   // Is the same as the position in the vector of RecTimers
176   // Because they are in order, they don't change order and wselectlist starts from 0 up consecutively
177
178   int ypos = 44;
179   for (int current = top; current < bottom; current++)
180   {
181     recTimer = (*recTimerList)[current];
182
183     if (recTimer->recording) // Flashing red square
184     {
185       if (flipflop)
186       {
187         //rectangle(6, ypos, 18, 16, Colour::RED);
188         rectangle(6, ypos, 18, 16, Colour::RED);
189         drawText("R", 8, ypos-3, Colour::LIGHTTEXT);
190       }
191     }
192     else if (recTimer->pending)
193     {
194       rectangle(6, ypos, 18, 16, Colour::RED);
195       drawText("X", 8, ypos-3, Colour::BLACK);
196     }
197     else if (recTimer->active == 0)
198     {
199       rectangle(6, ypos, 18, 16, Colour::SELECTHIGHLIGHT);
200       drawText("X", 8, ypos-3, Colour::BLACK);
201     }
202     else
203     {
204 //      if (flipflop) rectangle(6, ypos, 18, 16, Colour::GREEN);
205     }
206
207     ypos += yinc;
208   }
209 }
210
211 void VTimerList::timercall(int clientReference)
212 {
213   drawClock();
214
215   Message* m = new Message();
216   m->message = Message::REDRAW;
217   m->to = ViewMan::getInstance();
218   m->from = this;
219   m->parameter = (ULONG)&clockRegion;
220   Command::getInstance()->postMessageFromOuterSpace(m);
221
222   flipflop = !flipflop;
223   drawIndicators();
224
225   m = new Message();
226   m->message = Message::REDRAW;
227   m->to = ViewMan::getInstance();
228   m->from = this;
229   m->parameter = (ULONG)&indicatorsRegion;
230   Command::getInstance()->postMessageFromOuterSpace(m);
231 }
232
233 int VTimerList::handleCommand(int command)
234 {
235   switch(command)
236   {
237     case Remote::DF_UP:
238     case Remote::UP:
239     {
240       sl.up();
241       sl.draw();
242       drawShowing();
243       drawIndicators();
244       ViewMan::getInstance()->updateView(this);
245       return 2;
246     }
247     case Remote::DF_DOWN:
248     case Remote::DOWN:
249     {
250       sl.down();
251       sl.draw();
252       drawShowing();
253       drawIndicators();
254       ViewMan::getInstance()->updateView(this);
255       return 2;
256     }
257     case Remote::SKIPBACK:
258     {
259       sl.pageUp();
260       sl.draw();
261       drawShowing();
262       drawIndicators();
263       ViewMan::getInstance()->updateView(this);
264       return 2;
265     }
266     case Remote::SKIPFORWARD:
267     {
268       sl.pageDown();
269       sl.draw();
270       drawShowing();
271       drawIndicators();
272       ViewMan::getInstance()->updateView(this);
273       return 2;
274     }
275     case Remote::OK:
276     {
277       RecTimer* recTimer = NULL;
278       if (recTimerList) recTimer = (RecTimer*)sl.getCurrentOptionData();
279 //      {
280 //        int currentOption = sl.getCurrentOption();
281 //
282 //        for (UINT i = 0; i < recTimerList->size(); i++)
283 //        {
284 //          recTimer = (*recTimerList)[i];
285 //          if (currentOption == recTimer->index) break;
286 //        }
287 //      }
288
289       if (recTimer == NULL) return 2;
290
291       VTimerEdit* v = new VTimerEdit(recTimer);
292       ViewMan::getInstance()->add(v);
293       ViewMan::getInstance()->updateView(v);
294
295       return 2;
296     }
297     case Remote::BACK:
298     {
299       return 4;
300     }
301   }
302   // stop command getting to any more views
303   return 1;
304 }
305
306 void VTimerList::processMessage(Message* m)
307 {
308   if (m->message == Message::MOUSE_MOVE)
309   {
310     if (sl.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
311     {
312       sl.draw();
313       ViewMan::getInstance()->updateView(this);
314     }
315   }
316   else if (m->message == Message::MOUSE_LBDOWN)
317   {
318     if (sl.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
319     {
320       ViewMan::getInstance()->handleCommand(Remote::OK); //simulate OK press
321     }
322     else
323     {
324       //check if press is outside this view! then simulate cancel
325       int x=(m->parameter>>16)-getScreenX();
326       int y=(m->parameter&0xFFFF)-getScreenY();
327       if (x<0 || y <0 || x>getWidth() || y>getHeight())
328       {
329         ViewMan::getInstance()->handleCommand(Remote::BACK); //simulate cancel press
330       }
331     }
332   }
333 }