]> git.vomp.tv Git - vompclient.git/blob - boxx.cc
Updates to new streaming protocol and live tv
[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 // Level 1 drawing functions
212
213 void Boxx::fillColour(Colour& colour)
214 {
215   rectangle(0, 0, area.w, area.h, colour);
216 }
217
218 void Boxx::drawPara(char* text, int x, int y, Colour& colour)
219 {
220   char line[256];
221   int lineHeight = surface->getFontHeight() + paraVSpace;
222
223   int lineWidth;
224   int thisCharWidth;
225   int textPos;
226   int linePos;
227   int ypos;
228   int printLine;
229
230   textPos = 0;
231   ypos = y;
232
233   while(1)
234   {
235     linePos = 0;
236     lineWidth = 0;
237     while(1)
238     {
239       printLine = 0;
240
241       if (text[textPos] == '\0') break;
242
243       if (text[textPos] == '\n')
244       {
245         textPos++; // ignore the \n
246         printLine = 1;
247         break;
248       }
249
250       thisCharWidth = surface->getCharWidth(text[textPos]);
251       if ((lineWidth + thisCharWidth) > (int)(area.w - (2 * paraMargin)))
252       {
253         // this character would break the right margin
254         if (text[textPos] == ' ')
255         {
256           // this char is a space, ignore and break
257           textPos++;
258           break;
259         }
260         else
261         {
262           // Need to go back to the last space in the line
263           while ((text[textPos] != ' ') && (linePos >= 0))
264           {
265             textPos--;
266             linePos--;
267           }
268           // Now take the space we just found
269           textPos++;
270           break;
271         }
272       }
273       line[linePos++] = text[textPos];
274       lineWidth += thisCharWidth;
275       textPos++;
276     }
277
278 //    line[linePos++] = '\0';
279     if (linePos>=0) line[linePos++] = '\0'; //Here is the change
280
281     if (printLine || (linePos > 1)) // if some text was put in line
282     {
283       drawText(line, x, ypos, colour);
284       ypos += lineHeight;
285       if (ypos > (int)(area.h - lineHeight)) break;
286     }
287     else
288     {
289       break;
290     }
291   }
292 }
293
294 void Boxx::rectangle(Region& region, Colour& colour)
295 {
296   rectangle(region.x, region.y, region.w, region.h, colour);
297 }
298
299 // Level 0 drawing functions
300
301 void Boxx::rectangle(UINT x, UINT y, UINT w, UINT h, Colour& colour)
302 {
303   if (parent) parent->rectangle(area.x + x, area.y + y, w, h, colour);
304   else surface->fillblt(x, y, w, h, colour.rgba());
305 }
306
307 void Boxx::drawText(const char* text, int x, int y, Colour& colour)
308 {
309   if (parent) parent->drawText(text, area.x + x, area.y + y, colour);
310   else surface->drawText(text, x, y, colour.rgba());
311 }
312
313 void Boxx::drawTextRJ(const char* text, int x, int y, Colour& colour)
314 {
315   if (parent) parent->drawTextRJ(text, area.x + x, area.y + y, colour);
316   else surface->drawTextRJ(text, x, y, colour.rgba());
317 }
318
319 void Boxx::drawTextCentre(const char* text, int x, int y, Colour& colour)
320 {
321   if (parent) parent->drawTextCentre(text, area.x + x, area.y + y, colour);
322   else surface->drawTextCentre(text, x, y, colour.rgba());
323 }
324
325 void Boxx::drawPixel(UINT x, UINT y, Colour& colour)
326 {
327   if (parent) parent->drawPixel(area.x + x, area.y + y, colour);
328   else
329   {
330     int c = (  (0xFF000000         )
331              | (colour.red    << 16)
332              | (colour.green  <<  8)
333              | (colour.blue        ) );
334
335     surface->drawPixel(x, y, c);
336   }
337 }
338
339 void Boxx::startFastDraw()
340 {
341     if (parent) parent->startFastDraw();
342     else
343     {
344         surface->startFastDraw();
345     }
346 }
347
348 void Boxx::endFastDraw()
349 {
350     if (parent) parent->endFastDraw();
351     else
352     {
353         surface->endFastDraw();
354     }
355 }
356  
357
358 int Boxx::charWidth(char c)
359 {
360   if (parent) return parent->charWidth(c);
361   else return surface->getCharWidth(c);
362 }
363