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

widget.h

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 #ifndef GUI_WIDGET_H
00024 #define GUI_WIDGET_H
00025 
00026 #include "common/scummsys.h"
00027 #include "common/array.h"
00028 #include "common/str.h"
00029 #include "common/keyboard.h"
00030 #include "graphics/font.h"
00031 #include "graphics/surface.h"
00032 #include "gui/object.h"
00033 #include "gui/ThemeEngine.h"
00034 
00035 namespace GUI {
00036 
00037 enum {
00038     WIDGET_ENABLED      = 1 <<  0,
00039     WIDGET_INVISIBLE    = 1 <<  1,
00040     WIDGET_HILITED      = 1 <<  2,
00041     WIDGET_BORDER       = 1 <<  3,
00042     WIDGET_PRESSED      = 1 <<  4,
00043     //WIDGET_INV_BORDER = 1 <<  4,
00044     WIDGET_CLEARBG      = 1 <<  5,
00045     WIDGET_WANT_TICKLE  = 1 <<  7,
00046     WIDGET_TRACK_MOUSE  = 1 <<  8,
00047     // Retain focus on mouse up. By default widgets lose focus on mouseup,
00048     // but some widgets might want to retain it - widgets where you enter
00049     // text, for instance
00050     WIDGET_RETAIN_FOCUS = 1 <<  9,
00051     // Usually widgets would lock mouse input when the user pressed the
00052     // left mouse button till the user releases it.
00053     // The PopUpWidget for example does not want this behavior, since the
00054     // mouse down will open up a new dialog which silently eats the mouse
00055     // up event for its own purposes.
00056     WIDGET_IGNORE_DRAG  = 1 << 10
00057 };
00058 
00059 enum {
00060     kStaticTextWidget   = 'TEXT',
00061     kEditTextWidget     = 'EDIT',
00062     kButtonWidget       = 'BTTN',
00063     kCheckboxWidget     = 'CHKB',
00064     kRadiobuttonWidget  = 'RDBT',
00065     kSliderWidget       = 'SLDE',
00066     kListWidget         = 'LIST',
00067     kScrollBarWidget    = 'SCRB',
00068     kPopUpWidget        = 'POPU',
00069     kTabWidget          = 'TABW',
00070     kGraphicsWidget     = 'GFXW',
00071     kContainerWidget    = 'CTNR',
00072     kScrollContainerWidget = 'SCTR'
00073 };
00074 
00075 enum {
00076     kCaretBlinkTime = 300
00077 };
00078 
00079 enum {
00080     kPressedButtonTime = 200
00081 };
00082 
00083 enum {
00084     kPicButtonStateEnabled = 0,
00085     kPicButtonHighlight = 1,
00086     kPicButtonStateDisabled = 2,
00087     kPicButtonStatePressed = 3,
00088 
00089     kPicButtonStateMax = 3
00090 };
00091 
00092 /* Widget */
00093 class Widget : public GuiObject {
00094     friend class Dialog;
00095 protected:
00096     uint32      _type;
00097     GuiObject   *_boss;
00098     Widget      *_next;
00099     uint16      _id;
00100     bool        _hasFocus;
00101     ThemeEngine::WidgetStateInfo _state;
00102     Common::String _tooltip;
00103 
00104 private:
00105     uint16      _flags;
00106     bool        _needsRedraw;
00107 
00108 public:
00109     static Widget *findWidgetInChain(Widget *start, int x, int y);
00110     static Widget *findWidgetInChain(Widget *start, const char *name);
00111     static bool containsWidgetInChain(Widget *start, Widget *search);
00112 
00113 public:
00114     Widget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0);
00115     Widget(GuiObject *boss, const Common::String &name, const char *tooltip = 0);
00116     virtual ~Widget();
00117 
00118     void init();
00119     void resize(int x, int y, int w, int h);
00120 
00121     void setNext(Widget *w) { _next = w; }
00122     Widget *next() { return _next; }
00123 
00124     virtual int16   getAbsX() const { return _x + _boss->getChildX(); }
00125     virtual int16   getAbsY() const { return _y + _boss->getChildY(); }
00126 
00127     virtual void setPos(int x, int y) { _x = x; _y = y; }
00128     virtual void setSize(int w, int h) { _w = w; _h = h; }
00129 
00130     virtual void handleMouseDown(int x, int y, int button, int clickCount) {}
00131     virtual void handleMouseUp(int x, int y, int button, int clickCount) {}
00132     virtual void handleMouseEntered(int button) {}
00133     virtual void handleMouseLeft(int button) {}
00134     virtual void handleMouseMoved(int x, int y, int button) {}
00135     virtual void handleMouseWheel(int x, int y, int direction) {}
00136     virtual bool handleKeyDown(Common::KeyState state) { return false; }    // Return true if the event was handled
00137     virtual bool handleKeyUp(Common::KeyState state) { return false; }  // Return true if the event was handled
00138     virtual void handleTickle() {}
00139 
00141     virtual void markAsDirty();
00142 
00144     virtual void draw();
00145 
00146     void receivedFocus() { _hasFocus = true; receivedFocusWidget(); }
00147     void lostFocus() { _hasFocus = false; lostFocusWidget(); }
00148     virtual bool wantsFocus() { return false; }
00149 
00150     void setFlags(int flags);
00151     void clearFlags(int flags);
00152     int getFlags() const        { return _flags; }
00153 
00154     void setEnabled(bool e);
00155     bool isEnabled() const;
00156 
00157     void setVisible(bool e);
00158     bool isVisible() const;
00159 
00160     uint8 parseHotkey(const Common::String &label);
00161     Common::String cleanupHotkey(const Common::String &label);
00162 
00163     bool hasTooltip() const { return !_tooltip.empty(); }
00164     const Common::String &getTooltip() const { return _tooltip; }
00165     void setTooltip(const Common::String &tooltip) { _tooltip = tooltip; }
00166 
00167     virtual bool containsWidget(Widget *) const { return false; }
00168 
00169 protected:
00170     void updateState(int oldFlags, int newFlags);
00171 
00172     virtual void drawWidget() = 0;
00173 
00174     virtual void receivedFocusWidget() {}
00175     virtual void lostFocusWidget() {}
00176 
00177     virtual Widget *findWidget(int x, int y) { return this; }
00178 
00179     void releaseFocus() { assert(_boss); _boss->releaseFocus(); }
00180 
00181     // By default, delegate unhandled commands to the boss
00182     void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) { assert(_boss); _boss->handleCommand(sender, cmd, data); }
00183 };
00184 
00185 /* StaticTextWidget */
00186 class StaticTextWidget : public Widget {
00187 protected:
00188     Common::String          _label;
00189     Graphics::TextAlign     _align;
00190     ThemeEngine::FontStyle  _font;
00191 public:
00192     StaticTextWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &text, Graphics::TextAlign align, const char *tooltip = 0, ThemeEngine::FontStyle font = ThemeEngine::kFontStyleBold);
00193     StaticTextWidget(GuiObject *boss, const Common::String &name, const Common::String &text, const char *tooltip = 0, ThemeEngine::FontStyle font = ThemeEngine::kFontStyleBold);
00194     void setValue(int value);
00195     void setLabel(const Common::String &label);
00196     const Common::String &getLabel() const      { return _label; }
00197     void setAlign(Graphics::TextAlign align);
00198     Graphics::TextAlign getAlign() const        { return _align; }
00199 
00200 protected:
00201     void drawWidget();
00202 };
00203 
00204 /* ButtonWidget */
00205 class ButtonWidget : public StaticTextWidget, public CommandSender {
00206     friend class Dialog;    // Needed for the hotkey handling
00207 protected:
00208     uint32  _cmd;
00209     uint8   _hotkey;
00210 public:
00211     ButtonWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &label, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00212     ButtonWidget(GuiObject *boss, const Common::String &name, const Common::String &label, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00213 
00214     void setCmd(uint32 cmd)             { _cmd = cmd; }
00215     uint32 getCmd() const               { return _cmd; }
00216 
00217     void setLabel(const Common::String &label);
00218 
00219     void handleMouseUp(int x, int y, int button, int clickCount);
00220     void handleMouseDown(int x, int y, int button, int clickCount);
00221     void handleMouseEntered(int button) { if (_duringPress) { setFlags(WIDGET_PRESSED); } else { setFlags(WIDGET_HILITED); } markAsDirty(); }
00222     void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED | WIDGET_PRESSED); markAsDirty(); }
00223 
00224     void setHighLighted(bool enable);
00225     void setPressedState();
00226     void setUnpressedState();
00227 protected:
00228     void drawWidget();
00229     bool _duringPress;
00230 };
00231 
00232 /* DropdownButtonWidget */
00233 class DropdownButtonWidget : public ButtonWidget {
00234 public:
00235     DropdownButtonWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &label, const char *tooltip = nullptr, uint32 cmd = 0, uint8 hotkey = 0);
00236     DropdownButtonWidget(GuiObject *boss, const Common::String &name, const Common::String &label, const char *tooltip = nullptr, uint32 cmd = 0, uint8 hotkey = 0);
00237 
00238     void handleMouseMoved(int x, int y, int button) override;
00239     void handleMouseUp(int x, int y, int button, int clickCount) override;
00240     void reflowLayout() override;
00241 
00242     void appendEntry(const Common::String &label, uint32 cmd);
00243     void clearEntries();
00244 
00245 protected:
00246     struct Entry {
00247         Common::String label;
00248         uint32 cmd;
00249     };
00250     typedef Common::Array<Entry> EntryList;
00251 
00252     // Widget API
00253     void drawWidget() override;
00254 
00255     void reset();
00256     bool isInDropDown(int x, int y) const;
00257 
00258     EntryList _entries;
00259 
00260     uint32 _dropdownWidth;
00261     bool _inDropdown;
00262     bool _inButton;
00263 };
00264 
00265 /* PicButtonWidget */
00266 class PicButtonWidget : public ButtonWidget {
00267 public:
00268     PicButtonWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00269     PicButtonWidget(GuiObject *boss, const Common::String &name, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00270     ~PicButtonWidget();
00271 
00272     void setGfx(const Graphics::Surface *gfx, int statenum = kPicButtonStateEnabled);
00273     void setGfx(int w, int h, int r, int g, int b, int statenum = kPicButtonStateEnabled);
00274 
00275     void useAlpha(int alpha) { _alpha = alpha; }
00276     void useThemeTransparency(bool enable) { _transparency = enable; }
00277     void setButtonDisplay(bool enable) {_showButton = enable; }
00278 
00279 protected:
00280     void drawWidget();
00281 
00282     Graphics::Surface _gfx[kPicButtonStateMax + 1];
00283     int _alpha;
00284     bool _transparency;
00285     bool _showButton;
00286 };
00287 
00288 /* CheckboxWidget */
00289 class CheckboxWidget : public ButtonWidget {
00290 protected:
00291     bool    _state;
00292 public:
00293     CheckboxWidget(GuiObject *boss, int x, int y, int w, int h, const Common::String &label, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00294     CheckboxWidget(GuiObject *boss, const Common::String &name, const Common::String &label, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00295 
00296     void handleMouseUp(int x, int y, int button, int clickCount);
00297     virtual void handleMouseEntered(int button) { setFlags(WIDGET_HILITED); markAsDirty(); }
00298     virtual void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED); markAsDirty(); }
00299 
00300     void setState(bool state);
00301     void toggleState()          { setState(!_state); }
00302     bool getState() const       { return _state; }
00303 
00304 protected:
00305     void drawWidget();
00306 };
00307 
00308 class RadiobuttonWidget;
00309 
00310 class RadiobuttonGroup : public CommandSender {
00311 public:
00312     RadiobuttonGroup(GuiObject *boss, uint32 cmd = 0);
00313     ~RadiobuttonGroup() {}
00314 
00315     void addButton(RadiobuttonWidget *button) { _buttons.push_back(button); }
00316     Common::Array<RadiobuttonWidget *> getButtonList() const { return _buttons; }
00317 
00318     void setValue(int state);
00319     int getValue() const { return _value; }
00320 
00321     void setEnabled(bool ena);
00322 
00323     void setCmd(uint32 cmd)             { _cmd = cmd; }
00324     uint32 getCmd() const               { return _cmd; }
00325 
00326 protected:
00327     Common::Array<RadiobuttonWidget *> _buttons;
00328     int _value;
00329     uint32  _cmd;
00330 };
00331 
00332 /* RadiobuttonWidget */
00333 class RadiobuttonWidget : public ButtonWidget {
00334 protected:
00335     bool    _state;
00336     int _value;
00337 
00338 public:
00339     RadiobuttonWidget(GuiObject *boss, int x, int y, int w, int h, RadiobuttonGroup *group, int value, const Common::String &label, const char *tooltip = 0, uint8 hotkey = 0);
00340     RadiobuttonWidget(GuiObject *boss, const Common::String &name, RadiobuttonGroup *group, int value, const Common::String &label, const char *tooltip = 0, uint8 hotkey = 0);
00341 
00342     void handleMouseUp(int x, int y, int button, int clickCount);
00343     virtual void handleMouseEntered(int button) { setFlags(WIDGET_HILITED); markAsDirty(); }
00344     virtual void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED); markAsDirty(); }
00345 
00346     void setState(bool state, bool setGroup = true);
00347     void toggleState()          { setState(!_state); }
00348     bool getState() const       { return _state; }
00349     int getValue() const            { return _value; }
00350 
00351 protected:
00352     void drawWidget();
00353 
00354     RadiobuttonGroup *_group;
00355 };
00356 
00357 /* SliderWidget */
00358 class SliderWidget : public Widget, public CommandSender {
00359 protected:
00360     uint32  _cmd;
00361     int     _value, _oldValue;
00362     int     _valueMin, _valueMax;
00363     bool    _isDragging;
00364     uint    _labelWidth;
00365 public:
00366     SliderWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0, uint32 cmd = 0);
00367     SliderWidget(GuiObject *boss, const Common::String &name, const char *tooltip = 0, uint32 cmd = 0);
00368 
00369     void setCmd(uint32 cmd)     { _cmd = cmd; }
00370     uint32 getCmd() const       { return _cmd; }
00371 
00372     void setValue(int value)    { _value = value; }
00373     int getValue() const        { return _value; }
00374 
00375     void setMinValue(int value) { _valueMin = value; }
00376     int getMinValue() const     { return _valueMin; }
00377     void setMaxValue(int value) { _valueMax = value; }
00378     int getMaxValue() const     { return _valueMax; }
00379 
00380     void handleMouseMoved(int x, int y, int button);
00381     void handleMouseDown(int x, int y, int button, int clickCount);
00382     void handleMouseUp(int x, int y, int button, int clickCount);
00383     void handleMouseEntered(int button) { setFlags(WIDGET_HILITED); markAsDirty(); }
00384     void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED); markAsDirty(); }
00385     void handleMouseWheel(int x, int y, int direction);
00386 
00387 protected:
00388     void drawWidget();
00389 
00390     int valueToPos(int value);
00391     int posToValue(int pos);
00392     int valueToBarWidth(int value);
00393 };
00394 
00395 /* GraphicsWidget */
00396 class GraphicsWidget : public Widget {
00397 public:
00398     GraphicsWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0);
00399     GraphicsWidget(GuiObject *boss, const Common::String &name, const char *tooltip = 0);
00400     ~GraphicsWidget();
00401 
00402     void setGfx(const Graphics::Surface *gfx);
00403     void setGfx(int w, int h, int r, int g, int b);
00404 
00405     void useAlpha(int alpha) { _alpha = alpha; }
00406     void useThemeTransparency(bool enable) { _transparency = enable; }
00407 
00408 protected:
00409     void drawWidget();
00410 
00411     Graphics::Surface _gfx;
00412     int _alpha;
00413     bool _transparency;
00414 };
00415 
00416 /* ContainerWidget */
00417 class ContainerWidget : public Widget {
00418 public:
00419     ContainerWidget(GuiObject *boss, int x, int y, int w, int h);
00420     ContainerWidget(GuiObject *boss, const Common::String &name);
00421     ~ContainerWidget();
00422 
00423     virtual bool containsWidget(Widget *) const;
00424     virtual Widget *findWidget(int x, int y);
00425     virtual void removeWidget(Widget *widget);
00426 protected:
00427     void drawWidget();
00428 };
00429 
00430 ButtonWidget *addClearButton(GuiObject *boss, const Common::String &name, uint32 cmd, int x=0, int y=0, int w=0, int h=0);
00431 
00432 } // End of namespace GUI
00433 
00434 #endif


Generated on Sat Dec 14 2019 05:00:51 for ResidualVM by doxygen 1.7.1
curved edge   curved edge