]> git.vomp.tv Git - vompclient.git/blob - boxx.cc
*** empty log message ***
[vompclient.git] / boxx.cc
1 /*
2     Copyright 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "boxx.h"
22
23 #include "log.h"
24
25 char Boxx::numBoxxes = 0;
26
27 Boxx::Boxx()
28 {
29   // I want a parent box or a surface.
30   parent = NULL;
31   surface = NULL;
32
33   area.x = 0;
34   area.y = 0;
35   area.w = 0;
36   area.h = 0;
37
38   paraVSpace = 6; // default gap for drawPara
39
40   backgroundColourSet = false;
41   visible = true;
42
43   numBoxxes++;
44   Log::getInstance()->log("Boxx", Log::DEBUG, "Construct, now %u", numBoxxes);
45 }
46
47 Boxx::~Boxx()
48 {
49   if (surface) delete surface;
50   numBoxxes--;
51   Log::getInstance()->log("Boxx", Log::DEBUG, "Destruct, now %u", numBoxxes);
52 }
53
54 void Boxx::draw()
55 {
56   //Log::getInstance()->log("Boxx", Log::DEBUG, "Draw this %p surface %p", this, surface);
57   if (backgroundColourSet) fillColour(backgroundColour);
58
59   Boxx* currentBoxx;
60   vector<Boxx*>::iterator j;
61   for (j = children.begin(); j != children.end(); j++)
62   {
63     currentBoxx = *j;
64     if (currentBoxx->getVisible()) currentBoxx->draw();
65   }  
66 }
67
68 void Boxx::setSize(UINT w, UINT h)
69 {
70   area.w = w;
71   area.h = h;
72 }
73
74 void Boxx::setPosition(UINT x, UINT y)
75 {
76   area.x = x;
77   area.y = y;
78 }
79
80 void Boxx::createBuffer()
81 {
82   surface = new Surface_TYPE();
83   surface->create(area.w, area.h);
84 }
85
86 void Boxx::add(Boxx* newChild)
87 {
88   newChild->setParent(this);
89   children.push_back(newChild);
90 }
91
92 void Boxx::remove(Boxx* oldChild)
93 {
94   for(vector<Boxx*>::iterator i = children.begin(); i != children.end(); i++)
95   {
96     if (*i == oldChild)
97     {
98       children.erase(i);
99       return;
100     }
101   }
102   Log::getInstance()->log("Boxx", Log::ERR, "Remove child box called, child %p not found", oldChild);
103 }
104
105 void Boxx::setParent(Boxx* newParent)
106 {
107   parent = newParent;
108 }
109
110 void Boxx::setBackgroundColour(Colour& Tcolour)
111 {
112   backgroundColour = Tcolour;
113   backgroundColourSet = true;
114 }
115
116 void Boxx::setVisible(bool isVisible)
117 {
118   visible = isVisible;
119 }
120
121 bool Boxx::getVisible()
122 {
123   return visible;
124 }
125
126 void Boxx::setGap(UINT gap)
127 {
128   paraVSpace = gap;
129 }
130
131 void Boxx::blt(Region& r)
132 {
133   /* surface update to screen needs:
134   source x distance into this surface
135   source y distance into this surface
136   width of update
137   height of update
138   destination x on screen
139   destination y on screen
140   */
141
142   if (parent) abort(); // if (parent) then this is a child boxx. It can not blt.
143
144   // this shouldn't be here
145   r.x -= area.x;
146   r.y -= area.y;
147
148   surface->updateToScreen(r.x, r.y, r.w, r.h, area.x + r.x, area.y + r.y);
149   
150 }
151
152 int Boxx::getScreenX()
153 {
154   if (parent) return area.x + parent->getScreenX();
155   return area.x;
156 }
157
158 int Boxx::getScreenY()
159 {
160   if (parent) return area.y + parent->getScreenY();
161   return area.y;
162 }
163
164 int Boxx::getRootBoxOffsetX()  // convert this to be getX and silently do the parent/not thing? same for Y below?
165 {
166   if (parent) return area.x + parent->getRootBoxOffsetX();
167   return 0;
168 }
169
170 int Boxx::getRootBoxOffsetY()
171 {
172   if (parent) return area.y + parent->getRootBoxOffsetY();
173   return 0;
174 }
175
176 int Boxx::getX()
177 {
178   return area.x;
179 }
180
181 int Boxx::getY()
182 {
183   return area.y;
184 }
185
186 int Boxx::getX2()
187 {
188   return area.x + area.w;
189 }
190
191 int Boxx::getY2()
192 {
193   return area.y + area.h;
194 }
195
196 UINT Boxx::getWidth()
197 {
198   return area.w;
199 }
200
201 UINT Boxx::getHeight()
202 {
203   return area.h;
204 }
205
206 Region* Boxx::getRegion()
207 {
208   return &area;
209 }
210
211 void Boxx::getRootBoxRegion(Region* r)
212 {
213   // Returns a region that describes the position of this box on the box with the surface
214   // To be used for boxstack->update calls
215
216   r->x = getRootBoxOffsetX();
217   r->y = getRootBoxOffsetY();
218   r->w = area.w;
219   r->h = area.h;  
220 }
221
222 // Level 1 drawing functions
223
224 void Boxx::fillColour(Colour& colour)
225 {
226   rectangle(0, 0, area.w, area.h, colour);
227 }
228
229 void Boxx::drawPara(char* text, int x, int y, Colour& colour)
230 {
231   char line[256];
232   int lineHeight = surface->getFontHeight() + paraVSpace;
233
234   int lineWidth;
235   int thisCharWidth;
236   int textPos;
237   int linePos;
238   int ypos;
239   int printLine;
240
241   textPos = 0;
242   ypos = y;
243
244   while(1)
245   {
246     linePos = 0;
247     lineWidth = 0;
248     while(1)
249     {
250       printLine = 0;
251
252       if (text[textPos] == '\0') break;
253
254       if (text[textPos] == '\n')
255       {
256         textPos++; // ignore the \n
257         printLine = 1;
258         break;
259       }
260
261       thisCharWidth = surface->getCharWidth(text[textPos]);
262       if ((lineWidth + thisCharWidth) > (int)(area.w - (2 * paraMargin)))
263       {
264         // this character would break the right margin
265         if (text[textPos] == ' ')
266         {
267           // this char is a space, ignore and break
268           textPos++;
269           break;
270         }
271         else
272         {
273           // Need to go back to the last space in the line
274           while ((text[textPos] != ' ') && (linePos >= 0))
275           {
276             textPos--;
277             linePos--;
278           }
279           // Now take the space we just found
280           textPos++;
281           break;
282         }
283       }
284       line[linePos++] = text[textPos];
285       lineWidth += thisCharWidth;
286       textPos++;
287     }
288
289 //    line[linePos++] = '\0';
290     if (linePos>=0) line[linePos++] = '\0'; //Here is the change
291
292     if (printLine || (linePos > 1)) // if some text was put in line
293     {
294       drawText(line, x, ypos, colour);
295       ypos += lineHeight;
296       if (ypos > (int)(area.h - lineHeight)) break;
297     }
298     else
299     {
300       break;
301     }
302   }
303 }
304
305 void Boxx::rectangle(Region& region, Colour& colour)
306 {
307   rectangle(region.x, region.y, region.w, region.h, colour);
308 }
309
310 // Level 0 drawing functions
311
312 void Boxx::rectangle(UINT x, UINT y, UINT w, UINT h, Colour& colour)
313 {
314   if (parent) parent->rectangle(area.x + x, area.y + y, w, h, colour);
315   else surface->fillblt(x, y, w, h, colour.rgba());
316 }
317
318 void Boxx::drawText(const char* text, int x, int y, Colour& colour)
319 {
320   if (parent) parent->drawText(text, area.x + x, area.y + y, colour);
321   else surface->drawText(text, x, y, colour.rgba());
322 }
323
324 void Boxx::drawText(const char* text, int x, int y, int width, Colour& colour)
325 {
326   if (parent) parent->drawText(text, area.x + x, area.y + y, width, colour);
327   else surface->drawText(text, x, y, width, colour.rgba());
328 }
329
330 void Boxx::drawTextRJ(const char* text, int x, int y, Colour& colour)
331 {
332   if (parent) parent->drawTextRJ(text, area.x + x, area.y + y, colour);
333   else surface->drawTextRJ(text, x, y, colour.rgba());
334 }
335
336 void Boxx::drawTextCentre(const char* text, int x, int y, Colour& colour)
337 {
338   if (parent) parent->drawTextCentre(text, area.x + x, area.y + y, colour);
339   else surface->drawTextCentre(text, x, y, colour.rgba());
340 }
341
342 void Boxx::drawPixel(UINT x, UINT y, Colour& colour)
343 {
344   if (parent) parent->drawPixel(area.x + x, area.y + y, colour);
345   else
346   {
347     int c = (  (0xFF000000         )
348              | (colour.red    << 16)
349              | (colour.green  <<  8)
350              | (colour.blue        ) );
351
352     surface->drawPixel(x, y, c);
353   }
354 }
355
356 void Boxx::startFastDraw()
357 {
358     if (parent) parent->startFastDraw();
359     else
360     {
361         surface->startFastDraw();
362     }
363 }
364
365 void Boxx::endFastDraw()
366 {
367     if (parent) parent->endFastDraw();
368     else
369     {
370         surface->endFastDraw();
371     }
372 }
373  
374
375 int Boxx::charWidth(char c)
376 {
377   if (parent) return parent->charWidth(c);
378   else return surface->getCharWidth(c);
379 }
380