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