]> git.vomp.tv Git - vompclient.git/blob - vchannelselect.cc
WIP
[vompclient.git] / vchannelselect.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, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "vchannelselect.h"
21
22 #include "remote.h"
23 #include "message.h"
24 #include "boxstack.h"
25 #include "colour.h"
26 #include "log.h"
27 #include "timers.h"
28 #include "vdr.h"
29 #include "messagequeue.h"
30
31 // this class only works as it does because the remote command
32 // values for the numbers are the numbers themselves !
33
34 VChannelSelect::VChannelSelect(Boxx* v)
35 {
36   parent = v;
37   numGot = 0;
38   ignoreTimer = false;
39
40   numWidth = (int)VDR::getInstance()->getChannelNumberWidth();
41   if (numWidth > 10) numWidth = 10;
42   for (int i = 0; i < numWidth; i++) input[i] = -1;
43
44   setSize((numWidth*10) + 22, 30); // 10 px = width of number chars in font
45   createBuffer();
46   setPosition(80, 60);
47   setBackgroundColour(DrawStyle::VIEWBACKGROUND);
48 }
49
50 VChannelSelect::~VChannelSelect()
51 {
52   Timers::getInstance()->cancelTimer(this, 1);
53 }
54
55 void VChannelSelect::timercall(int clientReference)
56 {
57   Log::getInstance()->log("VChannelSelect", Log::DEBUG, "Timer call");
58   if (ignoreTimer) return;
59   changeChannel(false);
60 }
61
62 void VChannelSelect::doInput(int number)
63 {
64   for (int i = numGot - 1; i >= 0; i--)
65   {
66     input[i+1] = input[i];
67   }
68
69   input[0] = number;
70   numGot++;
71 }
72
73 void VChannelSelect::draw()
74 {
75   Boxx::draw();
76
77   // draw numbers
78   char text[2];
79
80   for (int i = 0; i < numWidth; i++)
81   {
82     if (input[i] == -1)
83     {
84       drawText("_", 7 + ((numWidth - 1 - i) * 10), 5, DrawStyle::LIGHTTEXT);
85     }
86     else
87     {
88       sprintf(text, "%i", input[i]);
89       drawText(text, 7 + ((numWidth - 1 - i) * 10), 5, DrawStyle::LIGHTTEXT);
90     }
91   }
92 }
93
94 void VChannelSelect::changeChannel(bool which)
95 {
96   ignoreTimer = true;
97   Timers::getInstance()->cancelTimer(this, 1);
98
99   Message* m;
100
101   // Is there valid data? (is any number not-zero)
102   int i = 0;
103   for(i = 0; i < numWidth; i++) if (input[i]) break;
104
105   if (i < numWidth) // There is data
106   {
107     m = new Message();
108     m->from = this;
109     m->to = parent;
110     m->message = Message::CHANNEL_CHANGE;
111     m->parameter = 0;
112
113     for(i = numGot - 1; i >= 0; i--)
114     {
115       m->parameter += input[i] * (ULONG)pow(10., i);
116     }
117
118     if (which)
119       MessageQueue::getInstance()->postMessageNoLock(m);
120     else
121       MessageQueue::getInstance()->postMessageFromOuterSpace(m);
122   }
123
124   // Close me
125   m = new Message(); // Delete self
126   m->from = this;
127   m->to = BoxStack::getInstance();
128   m->message = Message::CLOSE_ME;
129   if (which)
130     MessageQueue::getInstance()->postMessageNoLock(m);
131   else
132     MessageQueue::getInstance()->postMessageFromOuterSpace(m);
133 }
134
135 int VChannelSelect::handleCommand(int command)
136 {
137   switch(command)
138   {
139     case Remote::ZERO:
140     case Remote::ONE:
141     case Remote::TWO:
142     case Remote::THREE:
143     case Remote::FOUR:
144     case Remote::FIVE:
145     case Remote::SIX:
146     case Remote::SEVEN:
147     case Remote::EIGHT:
148     case Remote::NINE:
149     {
150       doInput(command);
151       draw();
152       BoxStack::getInstance()->update(this);
153       if (numGot == numWidth) changeChannel(true);
154       else Timers::getInstance()->setTimerD(this, 1, 3);
155       return 2;
156     }
157     case Remote::OK:
158     {
159       changeChannel(true);
160       return 2;
161     }
162   }
163
164   // allow command to drop through to other views
165   return 0;
166 }