2 Copyright 2007 Chris Tallon
4 This file is part of VOMP.
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.
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.
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.
25 char Boxx::numBoxxes = 0;
29 // I want a parent box or a surface.
38 paraVSpace = 6; // default gap for drawPara
40 backgroundColourSet = false;
44 Log::getInstance()->log("Boxx", Log::DEBUG, "Construct, now %u", numBoxxes);
49 if (surface) delete surface;
51 Log::getInstance()->log("Boxx", Log::DEBUG, "Destruct, now %u", numBoxxes);
56 //Log::getInstance()->log("Boxx", Log::DEBUG, "Draw this %p surface %p", this, surface);
57 if (backgroundColourSet) fillColour(backgroundColour);
60 vector<Boxx*>::iterator j;
61 for (j = children.begin(); j != children.end(); j++)
64 if (currentBoxx->getVisible()) currentBoxx->draw();
68 void Boxx::setSize(UINT w, UINT h)
74 void Boxx::setPosition(UINT x, UINT y)
80 void Boxx::createBuffer()
82 surface = new Surface_TYPE();
83 surface->create(area.w, area.h);
86 void Boxx::add(Boxx* newChild)
88 newChild->setParent(this);
89 children.push_back(newChild);
92 void Boxx::remove(Boxx* oldChild)
94 for(vector<Boxx*>::iterator i = children.begin(); i != children.end(); i++)
102 Log::getInstance()->log("Boxx", Log::ERR, "Remove child box called, child %p not found", oldChild);
105 void Boxx::setParent(Boxx* newParent)
110 void Boxx::setBackgroundColour(const Colour& Tcolour)
112 backgroundColour = Tcolour;
113 backgroundColourSet = true;
116 void Boxx::setVisible(bool isVisible)
121 bool Boxx::getVisible()
126 void Boxx::setGap(UINT gap)
131 void Boxx::blt(Region& r)
133 /* surface update to screen needs:
134 source x distance into this surface
135 source y distance into this surface
138 destination x on screen
139 destination y on screen
142 if (parent) abort(); // if (parent) then this is a child boxx. It can not blt.
144 // this shouldn't be here
148 surface->updateToScreen(r.x, r.y, r.w, r.h, area.x + r.x, area.y + r.y);
152 int Boxx::getScreenX()
154 if (parent) return area.x + parent->getScreenX();
158 int Boxx::getScreenY()
160 if (parent) return area.y + parent->getScreenY();
164 int Boxx::getRootBoxOffsetX() // convert this to be getX and silently do the parent/not thing? same for Y below?
166 if (parent) return area.x + parent->getRootBoxOffsetX();
170 int Boxx::getRootBoxOffsetY()
172 if (parent) return area.y + parent->getRootBoxOffsetY();
188 return area.x + area.w;
193 return area.y + area.h;
196 UINT Boxx::getWidth()
201 UINT Boxx::getHeight()
206 Region* Boxx::getRegion()
211 void Boxx::getRootBoxRegion(Region* r)
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
216 r->x = getRootBoxOffsetX();
217 r->y = getRootBoxOffsetY();
222 // Level 1 drawing functions
224 void Boxx::fillColour(const Colour& colour)
226 rectangle(0, 0, area.w, area.h, colour);
229 void Boxx::drawPara(char* text, int x, int y, const Colour& colour)
232 int lineHeight = surface->getFontHeight() + paraVSpace;
252 if (text[textPos] == '\0') break;
254 if (text[textPos] == '\n')
256 textPos++; // ignore the \n
261 thisCharWidth = surface->getCharWidth(text[textPos]);
262 if ((lineWidth + thisCharWidth) > (int)(area.w - (2 * paraMargin)))
264 // this character would break the right margin
265 if (text[textPos] == ' ')
267 // this char is a space, ignore and break
273 // Need to go back to the last space in the line
274 while ((text[textPos] != ' ') && (linePos >= 0))
279 // Now take the space we just found
284 line[linePos++] = text[textPos];
285 lineWidth += thisCharWidth;
289 // line[linePos++] = '\0';
290 if (linePos>=0) line[linePos++] = '\0'; //Here is the change
292 if (printLine || (linePos > 1)) // if some text was put in line
294 drawText(line, x, ypos, colour);
296 if (ypos > (int)(area.h - lineHeight)) break;
305 void Boxx::rectangle(Region& region, const Colour& colour)
307 rectangle(region.x, region.y, region.w, region.h, colour);
310 // Level 0 drawing functions
312 void Boxx::rectangle(UINT x, UINT y, UINT w, UINT h, const Colour& colour)
314 if (parent) parent->rectangle(area.x + x, area.y + y, w, h, colour);
315 else surface->fillblt(x, y, w, h, colour.rgba());
318 void Boxx::drawText(const char* text, int x, int y, const Colour& colour)
320 if (parent) parent->drawText(text, area.x + x, area.y + y, colour);
321 else surface->drawText(text, x, y, colour.rgba());
324 void Boxx::drawText(const char* text, int x, int y, int width, const Colour& colour)
326 if (parent) parent->drawText(text, area.x + x, area.y + y, width, colour);
327 else surface->drawText(text, x, y, width, colour.rgba());
330 void Boxx::drawTextRJ(const char* text, int x, int y, const Colour& colour)
332 if (parent) parent->drawTextRJ(text, area.x + x, area.y + y, colour);
333 else surface->drawTextRJ(text, x, y, colour.rgba());
336 void Boxx::drawTextCentre(const char* text, int x, int y, const Colour& colour)
338 if (parent) parent->drawTextCentre(text, area.x + x, area.y + y, colour);
339 else surface->drawTextCentre(text, x, y, colour.rgba());
342 void Boxx::drawPixel(UINT x, UINT y, const Colour& colour)
344 if (parent) parent->drawPixel(area.x + x, area.y + y, colour);
347 int c = ( (0xFF000000 )
349 | (colour.green << 8)
352 surface->drawPixel(x, y, c);
356 void Boxx::startFastDraw()
358 if (parent) parent->startFastDraw();
361 surface->startFastDraw();
365 void Boxx::endFastDraw()
367 if (parent) parent->endFastDraw();
370 surface->endFastDraw();
375 int Boxx::charWidth(char c)
377 if (parent) return parent->charWidth(c);
378 else return surface->getCharWidth(c);