]> git.vomp.tv Git - vompclient.git/blob - woptionpane.cc
Bitmap and VPictureBanner CWFs
[vompclient.git] / woptionpane.cc
1 /*
2     Copyright 2007-2020 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 "wtextbox.h"
21 #include "i18n.h"
22 #include "woptionbox.h"
23 #include "log.h"
24 #include "vdr.h"
25 #include "input.h"
26 #include "option.h"
27 #include "woptionpane.h"
28
29 WOptionPane::WOptionPane()
30 {
31   numOptions = 0;
32   selectedOption = -1;
33 }
34
35 WOptionPane::~WOptionPane()
36 {
37   for(int i = 0; i < numOptions; i++)
38   {
39     delete textBoxes[i];
40     delete optionBoxes[i];
41   }
42 }
43
44 void WOptionPane::draw()
45 {
46   Boxx::draw();
47   drawText(tr("Press back to exit, <, > or [ok] to change"), 10, area.h - 30, DrawStyle::LIGHTTEXT);
48 }
49
50 void WOptionPane::saveOpts()
51 {
52   // Due to the way the pane is constructed, options[0] corresponds to textBoxes[0] and optionBoxes[0] etc
53   
54   for(int i = 0; i < numOptions; i++)
55   {
56     options[i]->userSetChoice = optionBoxes[i]->getSelectedIndex();
57   }
58 }
59
60 void WOptionPane::addOptionLine(Option* option)
61 {
62   int fontHeight = getFontHeight();
63
64   options.resize(numOptions + 1);
65   options[numOptions] = option;
66
67   WTextbox* tb = new WTextbox();
68   tb->setPosition(4, 4 + (numOptions * 30));
69   tb->setSize(300, fontHeight + 6);
70   tb->setText(tr(option->displayText));
71   tb->setTextPos(0, 0);
72   add(tb);
73
74   textBoxes.resize(numOptions + 1);
75   textBoxes[numOptions] = tb;
76
77   WOptionBox* ob = new WOptionBox();
78   ob->setPosition(310, 4 + (numOptions * 30));
79   ob->setSize(190, fontHeight);  
80   add(ob);
81
82   optionBoxes.resize(numOptions + 1);
83   optionBoxes[numOptions] = ob;
84
85   if (option->optionType == Option::TYPE_TEXT ||
86       option->optionType == Option::TYPE_KEYED_TEXT)
87   {
88     for (UINT j = 0; j < option->numChoices; j++)
89     {
90       Log::getInstance()->log("Options", Log::DEBUG, "Add option: %s", option->options[j]);
91       ob->addOption(tr(option->options[j]));
92     }
93
94     // Set the selected choice
95     ob->setSelected(tr(option->options[option->configChoice]));
96   }
97   else
98   {
99     // int mode
100     ob->setIntMode(option->startInt, option->numChoices);
101     ob->setSelected(option->configChoice);
102   }
103
104   numOptions++;
105 }
106
107 void WOptionPane::deactivateAllControls()
108 {
109   if (selectedOption >= 0)
110   {
111     optionBoxes[selectedOption]->setActive(0);
112     selectedOption = -1;
113   }
114 }
115
116 int WOptionPane::handleCommand(int command)
117 {
118   switch(command)
119   {
120     case Input::UP:
121     {
122       if (selectedOption > 0)
123       {
124         optionBoxes[selectedOption]->setActive(0);
125         --selectedOption;
126         optionBoxes[selectedOption]->setActive(1);
127         return 1;
128       }
129       else if (selectedOption == 0)
130       {
131         optionBoxes[selectedOption--]->setActive(0);
132         return 4; // Signal return control to parent
133       }
134       else if (selectedOption == -1) // Allow UP when inactive, start at the last optionBox
135       {
136         selectedOption = numOptions - 1;
137         optionBoxes[selectedOption]->setActive(1);
138         return 1;
139       }
140       FALLTHROUGH // it doesn't really
141     }
142     case Input::DOWN:
143     {
144       if (selectedOption < (numOptions - 1))
145       {
146         if (selectedOption != -1) optionBoxes[selectedOption]->setActive(0);
147         ++selectedOption;
148         optionBoxes[selectedOption]->setActive(1);
149         return 1;
150       }
151       else if (selectedOption == (numOptions - 1))
152       {
153         optionBoxes[selectedOption]->setActive(0);
154         selectedOption = -1;
155         return 4; // Signal return control to parent
156       }
157       FALLTHROUGH // it doesn't really
158     }
159     case Input::LEFT:
160     {
161       optionBoxes[selectedOption]->left();
162       return 1;
163     }
164     case Input::RIGHT:
165     {
166       optionBoxes[selectedOption]->right();
167       return 1;
168     }
169     case Input::OK:
170     {
171       optionBoxes[selectedOption]->cycle();
172       return 1;
173     }
174   }
175
176   return 0;
177 }
178
179 bool WOptionPane::mouseMove(int x, int y)
180 {
181   for (int i = 0; i < static_cast<int>(optionBoxes.size()); i++)
182   {
183     if (optionBoxes[i]->mouseMove(x, y))
184     {
185       if (selectedOption != i)
186       {
187         if (selectedOption != -1) optionBoxes[selectedOption]->setActive(0);
188         selectedOption = i;
189         return true;
190       }
191       else
192       {
193         return false;
194       }
195     }
196   }
197   return false;
198 }
199
200 bool WOptionPane::mouseLBDOWN(int x, int y)
201 {
202   for (int i = 0; i < static_cast<int>(optionBoxes.size()); i++)
203   {
204     if (optionBoxes[i]->mouseLBDOWN(x, y))
205     {
206       if (selectedOption == i)
207       {
208         optionBoxes[selectedOption]->cycle();
209         return true;
210       }
211       else
212       {
213         return false;
214       }
215     }
216   }
217   return false;
218 }