]> git.vomp.tv Git - vompclient.git/blob - woptionbox.cc
WOptionBox CWFs
[vompclient.git] / woptionbox.cc
1 /*
2     Copyright 2004-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 "colour.h"
21 #include "log.h"
22 #ifndef WIN32
23 #include "unistd.h"
24 #endif
25
26 #include "woptionbox.h"
27
28 WOptionBox::WOptionBox()
29 {
30   numOptions = 0;
31   options = NULL;
32   active = 0;
33   currentOption = 0;
34   mode = MODE_TEXT;
35   
36   textbox.setPosition(20, 0);
37   textbox.setSize(10, getFontHeight());
38   textbox.setParaMode(false);
39   textbox.setTextPos(0, 0);
40   add(&textbox);
41   
42   leftArrow.setPosition(0, 2);
43   leftArrow.nextSymbol = WSymbol::LEFTARROW;
44   add(&leftArrow);
45   
46   rightArrow.setPosition(0, 2);
47   rightArrow.nextSymbol = WSymbol::RIGHTARROW;
48   add(&rightArrow);
49 }
50
51 WOptionBox::~WOptionBox()
52 {
53   if (!options) return;
54
55   for(UINT i = 0; i < numOptions; i++) delete[] options[i];
56   free(options);
57 }
58
59 void WOptionBox::setSize(UINT w, UINT h)
60 {
61   Boxx::setSize(w, h);
62   textbox.setSize(w - 40, getFontHeight());
63   rightArrow.setPosition(w - 18, 2);
64 }
65
66 void WOptionBox::setActive(UCHAR tactive)
67 {
68   active = tactive;
69   setData();
70 }
71
72 void WOptionBox::setData()
73 {
74   // set colours, set text
75
76   if (active)
77   {
78     textbox.setForegroundColour(DrawStyle::DARKTEXT);
79     textbox.setBackgroundColour(DrawStyle::SELECTHIGHLIGHT);
80     leftArrow.nextColour = DrawStyle::SELECTHIGHLIGHT;
81     rightArrow.nextColour = DrawStyle::SELECTHIGHLIGHT;
82   }
83   else
84   {
85     textbox.setForegroundColour(DrawStyle::LIGHTTEXT);
86     textbox.setBackgroundColour(DrawStyle::BUTTONBACKGROUND);
87     leftArrow.nextColour = DrawStyle::BUTTONBACKGROUND;
88     rightArrow.nextColour = DrawStyle::BUTTONBACKGROUND;
89   }
90   textbox.setText(options[currentOption]);
91
92 }
93
94 void WOptionBox::addOption(const char* takeText)
95 {
96   int length = strlen(takeText);
97   char* newOption = new char[length + 1];
98   strcpy(newOption, takeText);
99   options = static_cast<char**>(realloc(options, (numOptions+1) * sizeof(char*)));
100   options[numOptions] = newOption;
101   numOptions++;
102   setData();
103 }
104
105 void WOptionBox::left()
106 {
107   if (currentOption == 0) return;
108   --currentOption;
109   setData();
110 }
111
112 void WOptionBox::right()
113 {
114   if (currentOption == (numOptions - 1)) return;
115   ++currentOption;
116   setData();
117 }
118
119 void WOptionBox::cycle()
120 {
121   if (currentOption == (numOptions - 1))
122   {
123     currentOption = 0;
124   }
125   else
126   {
127     ++currentOption;
128   }
129   setData();
130 }
131
132 void WOptionBox::setSelected(const char* toSelect)
133 {
134   for(UINT i = 0; i < numOptions; i++)
135   {
136     if (!strcmp(toSelect, options[i]))
137     {
138       currentOption = i;
139       setData();
140       return;
141     }
142   }
143   currentOption = 0;
144   setData();
145 }
146
147 int WOptionBox::getSelectedIndex()
148 {
149   if (mode == MODE_TEXT)
150   {
151     return currentOption;
152   }
153   else
154   {
155     return atoi(options[currentOption]);
156   }
157 }
158
159 void WOptionBox::setIntMode(int startInt, int setNumOptions)
160 {
161   mode = MODE_INT;
162
163   int cInt;
164   char buffer[20];
165
166   for (cInt = startInt; cInt < (startInt + setNumOptions); cInt++)
167   {
168     sprintf(buffer, "%i", cInt);
169     addOption(buffer);
170   }
171 }
172
173 void WOptionBox::setSelected(int toSelect)
174 {
175   for(UINT i = 0; i < numOptions; i++)
176   {
177     if (atoi(options[i]) == toSelect)
178     {
179       currentOption = i;
180       setData();
181       return;
182     }
183   }
184   currentOption = 0;
185   setData();
186 }
187
188 bool WOptionBox::mouseMove(int x, int y)
189 {
190   if (   (x - getRootBoxOffsetX()) >= 0
191       && (y - getRootBoxOffsetY()) >= 0
192       && (x - getRootBoxOffsetX()) <= static_cast<int>(area.w)
193       && (y - getRootBoxOffsetY()) <= static_cast<int>(area.h)
194       && !active
195      )
196   {
197     setActive(1);
198     return true;
199   }
200   
201   return false;
202 }
203
204 bool WOptionBox::mouseLBDOWN(int x, int y)
205 {
206   if (   (x - getRootBoxOffsetX()) >= 0
207       && (y - getRootBoxOffsetY()) >= 0
208       && (x - getRootBoxOffsetX()) <= static_cast<int>(area.w)
209       && (y - getRootBoxOffsetY()) <= static_cast<int>(area.h)
210       && active)
211   {
212     return true;
213   }
214   
215   return false;
216 }