ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

widget.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/scummsys.h"
00024 #include "common/system.h"
00025 #include "common/rect.h"
00026 #include "common/textconsole.h"
00027 #include "common/translation.h"
00028 #include "graphics/pixelformat.h"
00029 #include "gui/widget.h"
00030 #include "gui/gui-manager.h"
00031 
00032 #include "gui/ThemeEval.h"
00033 
00034 #include "gui/dialog.h"
00035 
00036 namespace GUI {
00037 
00038 Widget::Widget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip)
00039     : GuiObject(x, y, w, h), _type(0), _boss(boss), _tooltip(tooltip),
00040       _id(0), _flags(0), _hasFocus(false), _state(ThemeEngine::kStateEnabled) {
00041     init();
00042 }
00043 
00044 Widget::Widget(GuiObject *boss, const Common::String &name, const char *tooltip)
00045     : GuiObject(name), _type(0), _boss(boss), _tooltip(tooltip),
00046       _id(0), _flags(0), _hasFocus(false), _state(ThemeEngine::kStateDisabled) {
00047     init();
00048 }
00049 
00050 void Widget::init() {
00051     // Insert into the widget list of the boss
00052     _next = _boss->_firstWidget;
00053     _boss->_firstWidget = this;
00054     _needsRedraw = true;
00055 }
00056 
00057 Widget::~Widget() {
00058     delete _next;
00059     _next = 0;
00060 }
00061 
00062 void Widget::resize(int x, int y, int w, int h) {
00063     _x = x;
00064     _y = y;
00065     _w = w;
00066     _h = h;
00067 }
00068 
00069 void Widget::setFlags(int flags) {
00070     updateState(_flags, _flags | flags);
00071     _flags |= flags;
00072 }
00073 
00074 void Widget::clearFlags(int flags) {
00075     updateState(_flags, _flags & ~flags);
00076     _flags &= ~flags;
00077 }
00078 
00079 void Widget::updateState(int oldFlags, int newFlags) {
00080     if (newFlags & WIDGET_ENABLED) {
00081         _state = ThemeEngine::kStateEnabled;
00082         if (newFlags & WIDGET_HILITED)
00083             _state = ThemeEngine::kStateHighlight;
00084         if (newFlags & WIDGET_PRESSED)
00085             _state = ThemeEngine::kStatePressed;
00086     } else {
00087         _state = ThemeEngine::kStateDisabled;
00088     }
00089 }
00090 
00091 void Widget::markAsDirty() {
00092     _needsRedraw = true;
00093 
00094     Widget *w = _firstWidget;
00095     while (w) {
00096         w->markAsDirty();
00097         w = w->next();
00098     }
00099 }
00100 
00101 void Widget::draw() {
00102     if (!isVisible() || !_boss->isVisible())
00103         return;
00104 
00105     if (_needsRedraw) {
00106         int oldX = _x, oldY = _y;
00107 
00108         // Account for our relative position in the dialog
00109         _x = getAbsX();
00110         _y = getAbsY();
00111 
00112         Common::Rect oldClip = g_gui.theme()->swapClipRect(_boss->getClipRect());
00113 
00114         // Draw border
00115         if (_flags & WIDGET_BORDER) {
00116             g_gui.theme()->drawWidgetBackground(Common::Rect(_x, _y, _x + _w, _y + _h), 0,
00117                                                 ThemeEngine::kWidgetBackgroundBorder);
00118             _x += 4;
00119             _y += 4;
00120             _w -= 8;
00121             _h -= 8;
00122         }
00123 
00124         // Now perform the actual widget draw
00125         drawWidget();
00126 
00127         g_gui.theme()->swapClipRect(oldClip);
00128 
00129         // Restore x/y
00130         if (_flags & WIDGET_BORDER) {
00131             _x -= 4;
00132             _y -= 4;
00133             _w += 8;
00134             _h += 8;
00135         }
00136 
00137         _x = oldX;
00138         _y = oldY;
00139 
00140         _needsRedraw = false;
00141     }
00142 
00143     // Draw all children
00144     Widget *w = _firstWidget;
00145     while (w) {
00146         w->draw();
00147         w = w->_next;
00148     }
00149 }
00150 
00151 Widget *Widget::findWidgetInChain(Widget *w, int x, int y) {
00152     while (w) {
00153         // Stop as soon as we find a widget that contains the point (x,y)
00154         if (x >= w->_x && x < w->_x + w->_w && y >= w->_y && y < w->_y + w->getHeight())
00155             break;
00156         w = w->_next;
00157     }
00158     if (w)
00159         w = w->findWidget(x - w->_x, y - w->_y);
00160     return w;
00161 }
00162 
00163 Widget *Widget::findWidgetInChain(Widget *w, const char *name) {
00164     while (w) {
00165         if (w->_name == name) {
00166             return w;
00167         }
00168         w = w->_next;
00169     }
00170     return 0;
00171 }
00172 
00173 bool Widget::containsWidgetInChain(Widget *w, Widget *search) {
00174     while (w) {
00175         if (w == search || w->containsWidget(search))
00176             return true;
00177         w = w->_next;
00178     }
00179     return false;
00180 }
00181 
00182 void Widget::setEnabled(bool e) {
00183     if ((_flags & WIDGET_ENABLED) != e) {
00184         if (e)
00185             setFlags(WIDGET_ENABLED);
00186         else
00187             clearFlags(WIDGET_ENABLED);
00188 
00189         g_gui.scheduleTopDialogRedraw();
00190     }
00191 }
00192 
00193 bool Widget::isEnabled() const {
00194     if (g_gui.xmlEval()->getVar("Dialog." + _name + ".Enabled", 1) == 0) {
00195         return false;
00196     }
00197     return ((_flags & WIDGET_ENABLED) != 0);
00198 }
00199 
00200 void Widget::setVisible(bool e) {
00201     if (e)
00202         clearFlags(WIDGET_INVISIBLE);
00203     else
00204         setFlags(WIDGET_INVISIBLE);
00205 }
00206 
00207 bool Widget::isVisible() const {
00208     if (g_gui.xmlEval()->getVar("Dialog." + _name + ".Visible", 1) == 0)
00209         return false;
00210 
00211     return !(_flags & WIDGET_INVISIBLE);
00212 }
00213 
00214 uint8 Widget::parseHotkey(const Common::String &label) {
00215     if (!label.contains('~'))
00216         return 0;
00217 
00218     int state = 0;
00219     uint8 hotkey = 0;
00220 
00221     for (uint i = 0; i < label.size() && state != 3; i++) {
00222         switch (state) {
00223         case 0:
00224             if (label[i] == '~')
00225                 state = 1;
00226             break;
00227         case 1:
00228             if (label[i] != '~') {
00229                 state = 2;
00230                 hotkey = label[i];
00231             } else
00232                 state = 0;
00233             break;
00234         case 2:
00235             if (label[i] == '~')
00236                 state = 3;
00237             else
00238                 state = 0;
00239             break;
00240         }
00241     }
00242 
00243     if (state == 3)
00244         return hotkey;
00245 
00246     return 0;
00247 }
00248 
00249 Common::String Widget::cleanupHotkey(const Common::String &label) {
00250     Common::String res;
00251 
00252     for (uint i = 0; i < label.size(); i++)
00253         if (label[i] != '~')
00254             res = res + label[i];
00255 
00256     return res;
00257 }
00258 
00259 #pragma mark -
00260 
00261 StaticTextWidget::StaticTextWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &text, Graphics::TextAlign align, const char *tooltip, ThemeEngine::FontStyle font)
00262     : Widget(boss, x, y, w, h, tooltip), _align(align) {
00263     setFlags(WIDGET_ENABLED);
00264     _type = kStaticTextWidget;
00265     _label = text;
00266     _font = font;
00267 }
00268 
00269 StaticTextWidget::StaticTextWidget(GuiObject *boss, const Common::String &name, const Common::String &text, const char *tooltip, ThemeEngine::FontStyle font)
00270     : Widget(boss, name, tooltip) {
00271     setFlags(WIDGET_ENABLED | WIDGET_CLEARBG);
00272     _type = kStaticTextWidget;
00273     _label = text;
00274 
00275     _align = g_gui.xmlEval()->getWidgetTextHAlign(name);
00276     _font = font;
00277 }
00278 
00279 void StaticTextWidget::setValue(int value) {
00280     _label = Common::String::format("%d", value);
00281 }
00282 
00283 void StaticTextWidget::setLabel(const Common::String &label) {
00284     if (_label != label) {
00285         _label = label;
00286 
00287         markAsDirty();
00288     }
00289 }
00290 
00291 void StaticTextWidget::setAlign(Graphics::TextAlign align) {
00292     if (_align != align){
00293         _align = align;
00294 
00295         markAsDirty();
00296     }
00297 }
00298 
00299 
00300 void StaticTextWidget::drawWidget() {
00301     g_gui.theme()->drawText(
00302             Common::Rect(_x, _y, _x + _w, _y + _h),
00303             _label, _state, _align, ThemeEngine::kTextInversionNone, 0, true, _font
00304     );
00305 }
00306 
00307 #pragma mark -
00308 
00309 ButtonWidget::ButtonWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &label, const char *tooltip, uint32 cmd, uint8 hotkey)
00310     : StaticTextWidget(boss, x, y, w, h, cleanupHotkey(label), Graphics::kTextAlignCenter, tooltip), CommandSender(boss),
00311       _cmd(cmd), _hotkey(hotkey), _lastTime(0), _duringPress(false) {
00312 
00313     if (hotkey == 0)
00314         _hotkey = parseHotkey(label);
00315 
00316     setFlags(WIDGET_ENABLED/* | WIDGET_BORDER*/ | WIDGET_CLEARBG);
00317     _type = kButtonWidget;
00318 }
00319 
00320 ButtonWidget::ButtonWidget(GuiObject *boss, const Common::String &name, const Common::String &label, const char *tooltip, uint32 cmd, uint8 hotkey)
00321     : StaticTextWidget(boss, name, cleanupHotkey(label), tooltip), CommandSender(boss),
00322       _cmd(cmd), _hotkey(hotkey), _lastTime(0), _duringPress(false) {
00323     if (hotkey == 0)
00324         _hotkey = parseHotkey(label);
00325     setFlags(WIDGET_ENABLED/* | WIDGET_BORDER*/ | WIDGET_CLEARBG);
00326     _type = kButtonWidget;
00327 }
00328 
00329 void ButtonWidget::handleMouseUp(int x, int y, int button, int clickCount) {
00330     if (isEnabled() && _duringPress && x >= 0 && x < _w && y >= 0 && y < _h) {
00331         setUnpressedState();
00332         sendCommand(_cmd, 0);
00333     }
00334     _duringPress = false;
00335 }
00336 
00337 void ButtonWidget::handleMouseDown(int x, int y, int button, int clickCount) {
00338     _duringPress = true;
00339     setPressedState();
00340 }
00341 
00342 void ButtonWidget::drawWidget() {
00343     g_gui.theme()->drawButton(Common::Rect(_x, _y, _x + _w, _y + _h), _label, _state, getFlags());
00344 }
00345 
00346 void ButtonWidget::setLabel(const Common::String &label) {
00347     StaticTextWidget::setLabel(cleanupHotkey(label));
00348 }
00349 
00350 ButtonWidget *addClearButton(GuiObject *boss, const Common::String &name, uint32 cmd, int x, int y, int w, int h) {
00351     ButtonWidget *button;
00352 
00353 #ifndef DISABLE_FANCY_THEMES
00354     if (g_gui.xmlEval()->getVar("Globals.ShowSearchPic") == 1 && g_gui.theme()->supportsImages()) {
00355         if (!name.empty())
00356             button = new PicButtonWidget(boss, name, _("Clear value"), cmd);
00357         else
00358             button = new PicButtonWidget(boss, x, y, w, h, _("Clear value"), cmd);
00359         ((PicButtonWidget *)button)->useThemeTransparency(true);
00360         ((PicButtonWidget *)button)->setGfx(g_gui.theme()->getImageSurface(ThemeEngine::kImageEraser));
00361     } else
00362 #endif
00363         if (!name.empty())
00364             button = new ButtonWidget(boss, name, "C", _("Clear value"), cmd);
00365         else
00366             button = new ButtonWidget(boss, x, y, w, h, "C", _("Clear value"), cmd);
00367 
00368     return button;
00369 }
00370 
00371 void ButtonWidget::setHighLighted(bool enable) {
00372     (enable) ? setFlags(WIDGET_HILITED) : clearFlags(WIDGET_HILITED);
00373     markAsDirty();
00374 }
00375 
00376 void ButtonWidget::setPressedState() {
00377     setFlags(WIDGET_PRESSED);
00378     clearFlags(WIDGET_HILITED);
00379     markAsDirty();
00380 }
00381 
00382 void ButtonWidget::setUnpressedState() {
00383     clearFlags(WIDGET_PRESSED);
00384     markAsDirty();
00385 }
00386 
00387 #pragma mark -
00388 
00389 PicButtonWidget::PicButtonWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip, uint32 cmd, uint8 hotkey)
00390     : ButtonWidget(boss, x, y, w, h, "", tooltip, cmd, hotkey),
00391       _alpha(255), _transparency(false), _showButton(true) {
00392 
00393     setFlags(WIDGET_ENABLED/* | WIDGET_BORDER*/ | WIDGET_CLEARBG);
00394     _type = kButtonWidget;
00395 }
00396 
00397 PicButtonWidget::PicButtonWidget(GuiObject *boss, const Common::String &name, const char *tooltip, uint32 cmd, uint8 hotkey)
00398     : ButtonWidget(boss, name, "", tooltip, cmd, hotkey),
00399       _alpha(255), _transparency(false), _showButton(true) {
00400     setFlags(WIDGET_ENABLED/* | WIDGET_BORDER*/ | WIDGET_CLEARBG);
00401     _type = kButtonWidget;
00402 }
00403 
00404 PicButtonWidget::~PicButtonWidget() {
00405     for (int i = 0; i < kPicButtonStateMax + 1; i++)
00406         _gfx[i].free();
00407 }
00408 
00409 void PicButtonWidget::setGfx(const Graphics::Surface *gfx, int statenum) {
00410     _gfx[statenum].free();
00411 
00412     if (!gfx || !gfx->getPixels())
00413         return;
00414 
00415     if (gfx->format.bytesPerPixel == 1) {
00416         warning("PicButtonWidget::setGfx got paletted surface passed");
00417         return;
00418     }
00419 
00420 
00421     if (gfx->w > _w || gfx->h > _h) {
00422         warning("PicButtonWidget has size %dx%d, but a surface with %dx%d is to be set", _w, _h, gfx->w, gfx->h);
00423         return;
00424     }
00425 
00426     _gfx[statenum].copyFrom(*gfx);
00427 }
00428 
00429 void PicButtonWidget::setGfx(int w, int h, int r, int g, int b, int statenum) {
00430     if (w == -1)
00431         w = _w;
00432     if (h == -1)
00433         h = _h;
00434 
00435     const Graphics::PixelFormat &requiredFormat = g_gui.theme()->getPixelFormat();
00436 
00437     _gfx[statenum].free();
00438     _gfx[statenum].create(w, h, requiredFormat);
00439     _gfx[statenum].fillRect(Common::Rect(0, 0, w, h), _gfx[statenum].format.RGBToColor(r, g, b));
00440 }
00441 
00442 void PicButtonWidget::drawWidget() {
00443     if (_showButton)
00444         g_gui.theme()->drawButton(Common::Rect(_x, _y, _x + _w, _y + _h), "", _state, getFlags());
00445 
00446     Graphics::Surface *gfx;
00447 
00448     if (_state == ThemeEngine::kStateHighlight)
00449         gfx = &_gfx[kPicButtonHighlight];
00450     else if (_state == ThemeEngine::kStateDisabled)
00451         gfx = &_gfx[kPicButtonStateDisabled];
00452     else if (_state == ThemeEngine::kStatePressed)
00453         gfx = &_gfx[kPicButtonStatePressed];
00454     else
00455         gfx = &_gfx[kPicButtonStateEnabled];
00456 
00457     if (!gfx->getPixels())
00458         gfx = &_gfx[kPicButtonStateEnabled];
00459 
00460     if (gfx->getPixels()) {
00461         // Check whether the set up surface needs to be converted to the GUI
00462         // color format.
00463         const Graphics::PixelFormat &requiredFormat = g_gui.theme()->getPixelFormat();
00464         if (gfx->format != requiredFormat) {
00465             gfx->convertToInPlace(requiredFormat);
00466         }
00467 
00468         const int x = _x + (_w - gfx->w) / 2;
00469         const int y = _y + (_h - gfx->h) / 2;
00470 
00471         g_gui.theme()->drawSurface(Common::Rect(x, y, x + gfx->w, y + gfx->h), *gfx, _transparency);
00472     }
00473 }
00474 
00475 #pragma mark -
00476 
00477 CheckboxWidget::CheckboxWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &label, const char *tooltip, uint32 cmd, uint8 hotkey)
00478     : ButtonWidget(boss, x, y, w, h, label, tooltip, cmd, hotkey), _state(false) {
00479     setFlags(WIDGET_ENABLED);
00480     _type = kCheckboxWidget;
00481 }
00482 
00483 CheckboxWidget::CheckboxWidget(GuiObject *boss, const Common::String &name, const Common::String &label, const char *tooltip, uint32 cmd, uint8 hotkey)
00484     : ButtonWidget(boss, name, label, tooltip, cmd, hotkey), _state(false) {
00485     setFlags(WIDGET_ENABLED);
00486     _type = kCheckboxWidget;
00487 }
00488 
00489 void CheckboxWidget::handleMouseUp(int x, int y, int button, int clickCount) {
00490     if (isEnabled() && _duringPress && x >= 0 && x < _w && y >= 0 && y < _h) {
00491         toggleState();
00492     }
00493     _duringPress = false;
00494 }
00495 
00496 void CheckboxWidget::setState(bool state) {
00497     if (_state != state) {
00498         _state = state;
00499         //_flags ^= WIDGET_INV_BORDER;
00500         markAsDirty();
00501     }
00502     sendCommand(_cmd, _state);
00503 }
00504 
00505 void CheckboxWidget::drawWidget() {
00506     g_gui.theme()->drawCheckbox(Common::Rect(_x, _y, _x + _w, _y + _h), _label, _state, Widget::_state);
00507 }
00508 
00509 #pragma mark -
00510 RadiobuttonGroup::RadiobuttonGroup(GuiObject *boss, uint32 cmd) : CommandSender(boss) {
00511     _value = -1;
00512     _cmd = cmd;
00513 }
00514 
00515 void RadiobuttonGroup::setValue(int value) {
00516     Common::Array<RadiobuttonWidget *>::iterator button = _buttons.begin();
00517     while (button != _buttons.end()) {
00518         (*button)->setState((*button)->getValue() == value, false);
00519 
00520         button++;
00521     }
00522 
00523     _value = value;
00524 
00525     sendCommand(_cmd, _value);
00526 }
00527 
00528 void RadiobuttonGroup::setEnabled(bool ena) {
00529     Common::Array<RadiobuttonWidget *>::iterator button = _buttons.begin();
00530     while (button != _buttons.end()) {
00531         (*button)->setEnabled(ena);
00532 
00533         button++;
00534     }
00535 }
00536 
00537 #pragma mark -
00538 
00539 RadiobuttonWidget::RadiobuttonWidget(GuiObject *boss, int x, int y, int w, int h, RadiobuttonGroup *group, int value, const Common::String &label, const char *tooltip, uint8 hotkey)
00540     : ButtonWidget(boss, x, y, w, h, label, tooltip, 0, hotkey), _state(false), _value(value), _group(group) {
00541     setFlags(WIDGET_ENABLED);
00542     _type = kRadiobuttonWidget;
00543     _group->addButton(this);
00544 }
00545 
00546 RadiobuttonWidget::RadiobuttonWidget(GuiObject *boss, const Common::String &name, RadiobuttonGroup *group, int value, const Common::String &label, const char *tooltip, uint8 hotkey)
00547     : ButtonWidget(boss, name, label, tooltip, 0, hotkey), _state(false), _value(value), _group(group) {
00548     setFlags(WIDGET_ENABLED);
00549     _type = kRadiobuttonWidget;
00550     _group->addButton(this);
00551 }
00552 
00553 void RadiobuttonWidget::handleMouseUp(int x, int y, int button, int clickCount) {
00554     if (isEnabled() && _duringPress && x >= 0 && x < _w && y >= 0 && y < _h) {
00555         toggleState();
00556     }
00557     _duringPress = false;
00558 }
00559 
00560 void RadiobuttonWidget::setState(bool state, bool setGroup) {
00561     if (setGroup) {
00562         _group->setValue(_value);
00563         return;
00564     }
00565 
00566     if (_state != state) {
00567         _state = state;
00568         //_flags ^= WIDGET_INV_BORDER;
00569         markAsDirty();
00570     }
00571     sendCommand(_cmd, _state);
00572 }
00573 
00574 void RadiobuttonWidget::drawWidget() {
00575     g_gui.theme()->drawRadiobutton(Common::Rect(_x, _y, _x + _w, _y + _h), _label, _state, Widget::_state);
00576 }
00577 
00578 #pragma mark -
00579 
00580 SliderWidget::SliderWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip, uint32 cmd)
00581     : Widget(boss, x, y, w, h, tooltip), CommandSender(boss),
00582       _cmd(cmd), _value(0), _oldValue(0), _valueMin(0), _valueMax(100), _isDragging(false), _labelWidth(0) {
00583     setFlags(WIDGET_ENABLED | WIDGET_TRACK_MOUSE | WIDGET_CLEARBG);
00584     _type = kSliderWidget;
00585 }
00586 
00587 SliderWidget::SliderWidget(GuiObject *boss, const Common::String &name, const char *tooltip, uint32 cmd)
00588     : Widget(boss, name, tooltip), CommandSender(boss),
00589       _cmd(cmd), _value(0), _oldValue(0), _valueMin(0), _valueMax(100), _isDragging(false), _labelWidth(0) {
00590     setFlags(WIDGET_ENABLED | WIDGET_TRACK_MOUSE | WIDGET_CLEARBG);
00591     _type = kSliderWidget;
00592 }
00593 
00594 void SliderWidget::handleMouseMoved(int x, int y, int button) {
00595     if (isEnabled() && _isDragging) {
00596         int newValue = posToValue(x);
00597         if (newValue < _valueMin)
00598             newValue = _valueMin;
00599         else if (newValue > _valueMax)
00600             newValue = _valueMax;
00601 
00602         if (newValue != _value) {
00603             _value = newValue;
00604             markAsDirty();
00605             sendCommand(_cmd, _value);  // FIXME - hack to allow for "live update" in sound dialog
00606         }
00607     }
00608 }
00609 
00610 void SliderWidget::handleMouseDown(int x, int y, int button, int clickCount) {
00611     if (isEnabled()) {
00612         _isDragging = true;
00613         handleMouseMoved(x, y, button);
00614     }
00615 }
00616 
00617 void SliderWidget::handleMouseUp(int x, int y, int button, int clickCount) {
00618     if (isEnabled() && _isDragging) {
00619         sendCommand(_cmd, _value);
00620     }
00621     _isDragging = false;
00622 }
00623 
00624 void SliderWidget::handleMouseWheel(int x, int y, int direction) {
00625     if (isEnabled() && !_isDragging) {
00626         // Increment or decrement by one
00627         int newValue = _value - direction;
00628 
00629         if (newValue < _valueMin)
00630             newValue = _valueMin;
00631         else if (newValue > _valueMax)
00632             newValue = _valueMax;
00633 
00634         if (newValue != _value) {
00635             _value = newValue;
00636             markAsDirty();
00637             sendCommand(_cmd, _value);  // FIXME - hack to allow for "live update" in sound dialog
00638         }
00639     }
00640 }
00641 
00642 void SliderWidget::drawWidget() {
00643     g_gui.theme()->drawSlider(Common::Rect(_x, _y, _x + _w, _y + _h), valueToBarWidth(_value), _state);
00644 }
00645 
00646 int SliderWidget::valueToBarWidth(int value) {
00647     value = CLIP(value, _valueMin, _valueMax);
00648     return (_w * (value - _valueMin) / (_valueMax - _valueMin));
00649 }
00650 
00651 int SliderWidget::valueToPos(int value) {
00652     value = CLIP(value, _valueMin, _valueMax);
00653     return ((_w - 1) * (value - _valueMin + 1) / (_valueMax - _valueMin));
00654 }
00655 
00656 int SliderWidget::posToValue(int pos) {
00657     return (pos) * (_valueMax - _valueMin) / (_w - 1) + _valueMin;
00658 }
00659 
00660 #pragma mark -
00661 
00662 GraphicsWidget::GraphicsWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip)
00663     : Widget(boss, x, y, w, h, tooltip), _gfx(), _alpha(255), _transparency(false) {
00664     setFlags(WIDGET_ENABLED | WIDGET_CLEARBG);
00665     _type = kGraphicsWidget;
00666 }
00667 
00668 GraphicsWidget::GraphicsWidget(GuiObject *boss, const Common::String &name, const char *tooltip)
00669     : Widget(boss, name, tooltip), _gfx(), _alpha(255), _transparency(false) {
00670     setFlags(WIDGET_ENABLED | WIDGET_CLEARBG);
00671     _type = kGraphicsWidget;
00672 }
00673 
00674 GraphicsWidget::~GraphicsWidget() {
00675     _gfx.free();
00676 }
00677 
00678 void GraphicsWidget::setGfx(const Graphics::Surface *gfx) {
00679     _gfx.free();
00680 
00681     if (!gfx || !gfx->getPixels())
00682         return;
00683 
00684     if (gfx->format.bytesPerPixel == 1) {
00685         warning("GraphicsWidget::setGfx got paletted surface passed");
00686         return;
00687     }
00688 
00689     if (gfx->w > _w || gfx->h > _h) {
00690         warning("GraphicsWidget has size %dx%d, but a surface with %dx%d is to be set", _w, _h, gfx->w, gfx->h);
00691         return;
00692     }
00693 
00694     _gfx.copyFrom(*gfx);
00695 }
00696 
00697 void GraphicsWidget::setGfx(int w, int h, int r, int g, int b) {
00698     if (w == -1)
00699         w = _w;
00700     if (h == -1)
00701         h = _h;
00702 
00703     const Graphics::PixelFormat &requiredFormat = g_gui.theme()->getPixelFormat();
00704 
00705     _gfx.free();
00706     _gfx.create(w, h, requiredFormat);
00707     _gfx.fillRect(Common::Rect(0, 0, w, h), _gfx.format.RGBToColor(r, g, b));
00708 }
00709 
00710 void GraphicsWidget::drawWidget() {
00711     if (_gfx.getPixels()) {
00712         // Check whether the set up surface needs to be converted to the GUI
00713         // color format.
00714         const Graphics::PixelFormat &requiredFormat = g_gui.theme()->getPixelFormat();
00715         if (_gfx.format != requiredFormat) {
00716             _gfx.convertToInPlace(requiredFormat);
00717         }
00718 
00719         const int x = _x + (_w - _gfx.w) / 2;
00720         const int y = _y + (_h - _gfx.h) / 2;
00721 
00722         g_gui.theme()->drawSurface(Common::Rect(x, y, x + _gfx.w, y + _gfx.h), _gfx, _transparency);
00723     }
00724 }
00725 
00726 #pragma mark -
00727 
00728 ContainerWidget::ContainerWidget(GuiObject *boss, int x, int y, int w, int h) : Widget(boss, x, y, w, h) {
00729     setFlags(WIDGET_ENABLED | WIDGET_CLEARBG);
00730     _type = kContainerWidget;
00731 }
00732 
00733 ContainerWidget::ContainerWidget(GuiObject *boss, const Common::String &name) : Widget(boss, name) {
00734     setFlags(WIDGET_ENABLED | WIDGET_CLEARBG);
00735     _type = kContainerWidget;
00736 }
00737 
00738 ContainerWidget::~ContainerWidget() {
00739     // We also remove the widget from the boss to avoid segfaults, when the
00740     // deleted widget is an active widget in the boss.
00741     for (Widget *w = _firstWidget; w; w = w->next()) {
00742         _boss->removeWidget(w);
00743     }
00744 }
00745 
00746 bool ContainerWidget::containsWidget(Widget *w) const {
00747     return containsWidgetInChain(_firstWidget, w);
00748 }
00749 
00750 Widget *ContainerWidget::findWidget(int x, int y) {
00751     Widget *w = findWidgetInChain(_firstWidget, x, y);
00752     if (w)
00753         return w;
00754     return this;
00755 }
00756 
00757 void ContainerWidget::removeWidget(Widget *widget) {
00758     // We also remove the widget from the boss to avoid a reference to a
00759     // widget not in the widget chain anymore.
00760     _boss->removeWidget(widget);
00761 
00762     Widget::removeWidget(widget);
00763 }
00764 
00765 void ContainerWidget::drawWidget() {
00766     g_gui.theme()->drawWidgetBackground(Common::Rect(_x, _y, _x + _w, _y + _h), 0,
00767                                         ThemeEngine::kWidgetBackgroundBorder);
00768 }
00769 
00770 } // End of namespace GUI


Generated on Sat Nov 16 2019 05:00:48 for ResidualVM by doxygen 1.7.1
curved edge   curved edge