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 private:
00231     uint32 _lastTime;
00232 };
00233 
00234 /* PicButtonWidget */
00235 class PicButtonWidget : public ButtonWidget {
00236 public:
00237     PicButtonWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00238     PicButtonWidget(GuiObject *boss, const Common::String &name, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00239     ~PicButtonWidget();
00240 
00241     void setGfx(const Graphics::Surface *gfx, int statenum = kPicButtonStateEnabled);
00242     void setGfx(int w, int h, int r, int g, int b, int statenum = kPicButtonStateEnabled);
00243 
00244     void useAlpha(int alpha) { _alpha = alpha; }
00245     void useThemeTransparency(bool enable) { _transparency = enable; }
00246     void setButtonDisplay(bool enable) {_showButton = enable; }
00247 
00248 protected:
00249     void drawWidget();
00250 
00251     Graphics::Surface _gfx[kPicButtonStateMax + 1];
00252     int _alpha;
00253     bool _transparency;
00254     bool _showButton;
00255 };
00256 
00257 /* CheckboxWidget */
00258 class CheckboxWidget : public ButtonWidget {
00259 protected:
00260     bool    _state;
00261 public:
00262     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);
00263     CheckboxWidget(GuiObject *boss, const Common::String &name, const Common::String &label, const char *tooltip = 0, uint32 cmd = 0, uint8 hotkey = 0);
00264 
00265     void handleMouseUp(int x, int y, int button, int clickCount);
00266     virtual void handleMouseEntered(int button) { setFlags(WIDGET_HILITED); markAsDirty(); }
00267     virtual void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED); markAsDirty(); }
00268 
00269     void setState(bool state);
00270     void toggleState()          { setState(!_state); }
00271     bool getState() const       { return _state; }
00272 
00273 protected:
00274     void drawWidget();
00275 };
00276 
00277 class RadiobuttonWidget;
00278 
00279 class RadiobuttonGroup : public CommandSender {
00280 public:
00281     RadiobuttonGroup(GuiObject *boss, uint32 cmd = 0);
00282     ~RadiobuttonGroup() {}
00283 
00284     void addButton(RadiobuttonWidget *button) { _buttons.push_back(button); }
00285     Common::Array<RadiobuttonWidget *> getButtonList() const { return _buttons; }
00286 
00287     void setValue(int state);
00288     int getValue() const { return _value; }
00289 
00290     void setEnabled(bool ena);
00291 
00292     void setCmd(uint32 cmd)             { _cmd = cmd; }
00293     uint32 getCmd() const               { return _cmd; }
00294 
00295 protected:
00296     Common::Array<RadiobuttonWidget *> _buttons;
00297     int _value;
00298     uint32  _cmd;
00299 };
00300 
00301 /* RadiobuttonWidget */
00302 class RadiobuttonWidget : public ButtonWidget {
00303 protected:
00304     bool    _state;
00305     int _value;
00306 
00307 public:
00308     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);
00309     RadiobuttonWidget(GuiObject *boss, const Common::String &name, RadiobuttonGroup *group, int value, const Common::String &label, const char *tooltip = 0, uint8 hotkey = 0);
00310 
00311     void handleMouseUp(int x, int y, int button, int clickCount);
00312     virtual void handleMouseEntered(int button) { setFlags(WIDGET_HILITED); markAsDirty(); }
00313     virtual void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED); markAsDirty(); }
00314 
00315     void setState(bool state, bool setGroup = true);
00316     void toggleState()          { setState(!_state); }
00317     bool getState() const       { return _state; }
00318     int getValue() const            { return _value; }
00319 
00320 protected:
00321     void drawWidget();
00322 
00323     RadiobuttonGroup *_group;
00324 };
00325 
00326 /* SliderWidget */
00327 class SliderWidget : public Widget, public CommandSender {
00328 protected:
00329     uint32  _cmd;
00330     int     _value, _oldValue;
00331     int     _valueMin, _valueMax;
00332     bool    _isDragging;
00333     uint    _labelWidth;
00334 public:
00335     SliderWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0, uint32 cmd = 0);
00336     SliderWidget(GuiObject *boss, const Common::String &name, const char *tooltip = 0, uint32 cmd = 0);
00337 
00338     void setCmd(uint32 cmd)     { _cmd = cmd; }
00339     uint32 getCmd() const       { return _cmd; }
00340 
00341     void setValue(int value)    { _value = value; }
00342     int getValue() const        { return _value; }
00343 
00344     void setMinValue(int value) { _valueMin = value; }
00345     int getMinValue() const     { return _valueMin; }
00346     void setMaxValue(int value) { _valueMax = value; }
00347     int getMaxValue() const     { return _valueMax; }
00348 
00349     void handleMouseMoved(int x, int y, int button);
00350     void handleMouseDown(int x, int y, int button, int clickCount);
00351     void handleMouseUp(int x, int y, int button, int clickCount);
00352     void handleMouseEntered(int button) { setFlags(WIDGET_HILITED); markAsDirty(); }
00353     void handleMouseLeft(int button)    { clearFlags(WIDGET_HILITED); markAsDirty(); }
00354     void handleMouseWheel(int x, int y, int direction);
00355 
00356 protected:
00357     void drawWidget();
00358 
00359     int valueToPos(int value);
00360     int posToValue(int pos);
00361     int valueToBarWidth(int value);
00362 };
00363 
00364 /* GraphicsWidget */
00365 class GraphicsWidget : public Widget {
00366 public:
00367     GraphicsWidget(GuiObject *boss, int x, int y, int w, int h, const char *tooltip = 0);
00368     GraphicsWidget(GuiObject *boss, const Common::String &name, const char *tooltip = 0);
00369     ~GraphicsWidget();
00370 
00371     void setGfx(const Graphics::Surface *gfx);
00372     void setGfx(int w, int h, int r, int g, int b);
00373 
00374     void useAlpha(int alpha) { _alpha = alpha; }
00375     void useThemeTransparency(bool enable) { _transparency = enable; }
00376 
00377 protected:
00378     void drawWidget();
00379 
00380     Graphics::Surface _gfx;
00381     int _alpha;
00382     bool _transparency;
00383 };
00384 
00385 /* ContainerWidget */
00386 class ContainerWidget : public Widget {
00387 public:
00388     ContainerWidget(GuiObject *boss, int x, int y, int w, int h);
00389     ContainerWidget(GuiObject *boss, const Common::String &name);
00390     ~ContainerWidget();
00391 
00392     virtual bool containsWidget(Widget *) const;
00393     virtual Widget *findWidget(int x, int y);
00394     virtual void removeWidget(Widget *widget);
00395 protected:
00396     void drawWidget();
00397 };
00398 
00399 ButtonWidget *addClearButton(GuiObject *boss, const Common::String &name, uint32 cmd, int x=0, int y=0, int w=0, int h=0);
00400 
00401 } // End of namespace GUI
00402 
00403 #endif


Generated on Sat Feb 16 2019 05:01:11 for ResidualVM by doxygen 1.7.1
curved edge   curved edge