From 2618a9bfdc6d908554bd4ddfba331529bea32c5a Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 31 Aug 2002 13:42:07 +0000 Subject: use class String for widget labels svn-id: r4878 --- gui/widget.cpp | 53 ++++++++++------------------------------------------- gui/widget.h | 23 ++++++++++++----------- 2 files changed, 22 insertions(+), 54 deletions(-) (limited to 'gui') diff --git a/gui/widget.cpp b/gui/widget.cpp index b407d8200c..90b972663a 100644 --- a/gui/widget.cpp +++ b/gui/widget.cpp @@ -76,70 +76,37 @@ void Widget::draw() #pragma mark - -StaticTextWidget::StaticTextWidget(Dialog *boss, int x, int y, int w, int h, const char *text, int align) - : Widget (boss, x, y, w, h), _label(0), _align(align) +StaticTextWidget::StaticTextWidget(Dialog *boss, int x, int y, int w, int h, const String &text, int align) + : Widget (boss, x, y, w, h), _align(align) { _type = kStaticTextWidget; setLabel(text); } -StaticTextWidget::~StaticTextWidget() -{ - if (_label) { - free(_label); - _label = 0; - } -} - -void StaticTextWidget::setLabel(const char *label) -{ - // Free old label if any - if (_label) - free(_label); - - // Duplicate new label - if (label) - _label = strdup(label); - else - _label = 0; -} - void StaticTextWidget::setValue(int value) { - // Free old label if any - if (_label) - free(_label); - - _label = (char *)malloc(10); - sprintf(_label, "%d", value); + char buf[256]; + sprintf(buf, "%d", value); + _label = buf; } void StaticTextWidget::drawWidget(bool hilite) { NewGui *gui = _boss->getGui(); - gui->drawString(_label, _x, _y, _w, hilite ? gui->_textcolorhi : gui->_textcolor, _align); + gui->drawString(_label.c_str(), _x, _y, _w, hilite ? gui->_textcolorhi : gui->_textcolor, _align); } #pragma mark - -ButtonWidget::ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd, uint8 hotkey) +ButtonWidget::ButtonWidget(Dialog *boss, int x, int y, int w, int h, const String &label, uint32 cmd, uint8 hotkey) : StaticTextWidget(boss, x, y, w, h, label, kTextAlignCenter), CommandSender(boss), _cmd(cmd), _hotkey(hotkey) { - assert(label); _flags = WIDGET_ENABLED | WIDGET_BORDER | WIDGET_CLEARBG ; _type = kButtonWidget; } -ButtonWidget::~ButtonWidget() -{ - if (_label) { - free(_label); - _label = 0; - } -} - void ButtonWidget::handleMouseUp(int x, int y, int button, int clickCount) { if (_flags & WIDGET_ENABLED && x >= 0 && x < _w && y >= 0 && y < _h) @@ -161,7 +128,7 @@ static uint32 checked_img[8] = { 0x00000000, }; -CheckboxWidget::CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd, uint8 hotkey) +CheckboxWidget::CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const String &label, uint32 cmd, uint8 hotkey) : ButtonWidget(boss, x, y, w, h, label, cmd, hotkey), _state(false) { _flags = WIDGET_ENABLED; @@ -191,12 +158,12 @@ void CheckboxWidget::drawWidget(bool hilite) gui->fillRect(_x + 2, _y + 2, 10, 10, gui->_bgcolor); // Finally draw the label - gui->drawString(_label, _x + 20, _y + 3, _w, gui->_textcolor); + gui->drawString(_label.c_str(), _x + 20, _y + 3, _w, gui->_textcolor); } #pragma mark - -SliderWidget::SliderWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd, uint8 hotkey) +SliderWidget::SliderWidget(Dialog *boss, int x, int y, int w, int h, const String &label, uint32 cmd, uint8 hotkey) : ButtonWidget(boss, x, y, w, h, label, cmd, hotkey), _value(0), _oldValue(1), _valueMin(0), _valueMax(100) { diff --git a/gui/widget.h b/gui/widget.h index 7f0901cc3d..67e81044d1 100644 --- a/gui/widget.h +++ b/gui/widget.h @@ -22,6 +22,7 @@ #define WIDGET_H #include "scummsys.h" +#include "common/util.h" class Dialog; @@ -117,16 +118,17 @@ protected: /* StaticTextWidget */ class StaticTextWidget : public Widget { protected: - char *_label; + typedef ScummVM::String String; + + String _label; int _align; public: - StaticTextWidget(Dialog *boss, int x, int y, int w, int h, const char *text, int align); - ~StaticTextWidget(); + StaticTextWidget(Dialog *boss, int x, int y, int w, int h, const String &text, int align); void setValue(int value); - void setLabel(const char *label); - const char *getLabel() const { return _label; } - void setAlign(int align) { _align = align; } - int getAlign() const { return _align; } + void setLabel(const String &label) { _label = label; } + const String &getLabel() const { return _label; } + void setAlign(int align) { _align = align; } + int getAlign() const { return _align; } protected: void drawWidget(bool hilite); @@ -140,8 +142,7 @@ protected: uint32 _cmd; uint8 _hotkey; public: - ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd = 0, uint8 hotkey = 0); - virtual ~ButtonWidget(); + ButtonWidget(Dialog *boss, int x, int y, int w, int h, const String &label, uint32 cmd = 0, uint8 hotkey = 0); void setCmd(uint32 cmd) { _cmd = cmd; } uint32 getCmd() const { return _cmd; } @@ -156,7 +157,7 @@ class CheckboxWidget : public ButtonWidget { protected: bool _state; public: - CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd = 0, uint8 hotkey = 0); + CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const String &label, uint32 cmd = 0, uint8 hotkey = 0); void setState(bool state) { _state = state; } bool getState() const { return _state; } @@ -175,7 +176,7 @@ protected: int _valueMin, _valueMax; bool _isDragging; public: - SliderWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd = 0, uint8 hotkey = 0); + SliderWidget(Dialog *boss, int x, int y, int w, int h, const String &label, uint32 cmd = 0, uint8 hotkey = 0); void setValue(int value) { _value = value; } int getValue() const { return _value; } -- cgit v1.2.3