]> git.vomp.tv Git - vompclient.git/blob - vquestion.cc
Rename Remote class to Input, RemoteLinux to InputLinux
[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::DF_LEFT:
85     case Input::LEFT:
86     {
87       swap();
88       draw();
89       BoxStack::getInstance()->update(this);
90       return 2;
91     }
92     case Input::DF_RIGHT:
93     case Input::RIGHT:
94     {
95       swap();
96       draw();
97       BoxStack::getInstance()->update(this);
98       return 2;
99     }
100     case Input::BACK:
101     {
102       return 4;
103     }
104     case Input::OK:
105     {
106       if (selectedOption != YES) return 4;
107
108       Message* m = new Message(); // Question/answer mech
109       m->from = this;
110       m->to = replyTo;
111       m->message = Message::QUESTION_YES;
112       MessageQueue::getInstance()->postMessage(m);
113
114       return 4;
115     }
116   }
117   return 1;
118 }
119
120 void VQuestion::setDefault(UCHAR option)
121 {
122   selectedOption = option;
123 }
124
125 void VQuestion::processMessage(Message* m)
126 {
127   if (m->message == Message::MOUSE_MOVE)
128   {
129     if (buttonYes.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
130     {
131       buttonNo.setActive(0);
132       selectedOption = YES;
133       draw();
134       BoxStack::getInstance()->update(this);
135     }
136     else if (buttonNo.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
137     {
138       buttonYes.setActive(0);
139       selectedOption = NO;
140       draw();
141       BoxStack::getInstance()->update(this);
142     }
143   }
144   else if (m->message == Message::MOUSE_LBDOWN)
145   {
146     if (buttonYes.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
147     {
148       BoxStack::getInstance()->handleCommand(Input::OK); //simulate OK press
149     }
150     else if (buttonNo.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
151     {
152       BoxStack::getInstance()->handleCommand(Input::OK); //simulate OK press
153     }
154     else
155     {
156       //check if press is outside this view! then simulate cancel
157       int x=(m->parameter>>16)-getScreenX();
158       int y=(m->parameter&0xFFFF)-getScreenY();
159       if (x<0 || y <0 || x>(int)getWidth() || y>(int)getHeight())
160       {
161         BoxStack::getInstance()->handleCommand(Input::BACK); //simulate cancel press
162       }
163     }
164   }
165 }