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