]> git.vomp.tv Git - vompclient.git/blob - vquestion.cc
Rename TCP class to TCPOld
[vompclient.git] / vquestion.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 "vquestion.h"
21
22 #include "input.h"
23 #include "boxstack.h"
24 #include "colour.h"
25 #include "i18n.h"
26 #include "messagequeue.h"
27
28 VQuestion::VQuestion(void* treplyTo)
29 {
30   replyTo = treplyTo;
31   mainText = NULL;
32   selectedOption = NO;
33
34   buttonYes.setPosition(40, 130);
35   buttonNo.setPosition(140, 130);
36
37   buttonYes.setText(tr("Yes"));
38   buttonNo.setText(tr("No"));
39   buttonNo.setActive(1);
40   
41   add(&buttonYes);
42   add(&buttonNo);  
43 }
44
45 VQuestion::~VQuestion()
46 {
47   if (mainText) delete[] mainText;
48 }
49
50 void VQuestion::setMainText(const char* takeText)
51 {
52   int length = strlen(takeText);
53   mainText = new char[length + 1];
54   strcpy(mainText, takeText);
55 }
56
57 void VQuestion::draw()
58 {
59   TBBoxx::draw();
60
61   if (mainText) drawPara(mainText, 10, 45, DrawStyle::LIGHTTEXT);
62 }
63
64 void VQuestion::swap()
65 {
66   if (selectedOption == NO)
67   {
68     selectedOption = YES;
69     buttonYes.setActive(1);
70     buttonNo.setActive(0);
71   }
72   else if (selectedOption == YES)
73   {
74     selectedOption = NO;
75     buttonYes.setActive(0);
76     buttonNo.setActive(1);
77   }
78 }
79
80 int VQuestion::handleCommand(int command)
81 {
82   switch(command)
83   {
84     case Input::LEFT:
85     {
86       swap();
87       draw();
88       BoxStack::getInstance()->update(this);
89       return 2;
90     }
91     case Input::RIGHT:
92     {
93       swap();
94       draw();
95       BoxStack::getInstance()->update(this);
96       return 2;
97     }
98     case Input::BACK:
99     {
100       return 4;
101     }
102     case Input::OK:
103     {
104       if (selectedOption != YES) return 4;
105
106       Message* m = new Message(); // Question/answer mech
107       m->from = this;
108       m->to = replyTo;
109       m->message = Message::QUESTION_YES;
110       MessageQueue::getInstance()->postMessage(m);
111
112       return 4;
113     }
114   }
115   return 1;
116 }
117
118 void VQuestion::setDefault(UCHAR option)
119 {
120   selectedOption = option;
121 }
122
123 void VQuestion::processMessage(Message* m)
124 {
125   if (m->message == Message::MOUSE_MOVE)
126   {
127     if (buttonYes.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
128     {
129       buttonNo.setActive(0);
130       selectedOption = YES;
131       draw();
132       BoxStack::getInstance()->update(this);
133     }
134     else if (buttonNo.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
135     {
136       buttonYes.setActive(0);
137       selectedOption = NO;
138       draw();
139       BoxStack::getInstance()->update(this);
140     }
141   }
142   else if (m->message == Message::MOUSE_LBDOWN)
143   {
144     if (buttonYes.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
145     {
146       BoxStack::getInstance()->handleCommand(Input::OK); //simulate OK press
147     }
148     else if (buttonNo.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
149     {
150       BoxStack::getInstance()->handleCommand(Input::OK); //simulate OK press
151     }
152     else if (coordsOutsideBox(m))
153     {
154       BoxStack::getInstance()->handleCommand(Input::BACK); //simulate cancel press
155     }
156   }
157 }