ManaPlus
widget.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 /* _______ __ __ __ ______ __ __ _______ __ __
23  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
24  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
25  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
26  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
27  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
28  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
29  *
30  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
31  *
32  *
33  * Per Larsson a.k.a finalman
34  * Olof Naessén a.k.a jansem/yakslem
35  *
36  * Visit: http://guichan.sourceforge.net
37  *
38  * License: (BSD)
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  * notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  * notice, this list of conditions and the following disclaimer in
46  * the documentation and/or other materials provided with the
47  * distribution.
48  * 3. Neither the name of Guichan nor the names of its contributors may
49  * be used to endorse or promote products derived from this software
50  * without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
58  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
59  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
61  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  */
64 
65 #ifndef GUI_WIDGETS_WIDGET_H
66 #define GUI_WIDGETS_WIDGET_H
67 
69 
70 #include "gui/rect.h"
71 
72 #include "gui/widgets/widget2.h"
73 
74 #include <list>
75 
76 #include "localconsts.h"
77 
78 class ActionListener;
80 class FocusHandler;
81 class FocusListener;
82 class Font;
83 class Graphics;
84 class KeyListener;
85 class MouseListener;
86 class WidgetListener;
87 
98 class Widget notfinal : public Widget2
99 {
100  public:
101  friend class BasicContainer;
102 
108  explicit Widget(const Widget2 *const widget);
109 
111 
112 
115  ~Widget() override;
116 
126  virtual void draw(Graphics *const graphics) A_NONNULL(2) = 0;
127 
128  virtual void safeDraw(Graphics *const graphics) A_NONNULL(2) = 0;
129 
149  virtual void drawFrame(Graphics* graphics A_UNUSED) A_NONNULL(2)
150  { }
151 
152  virtual void safeDrawFrame(Graphics* graphics A_UNUSED) A_NONNULL(2)
153  { }
154 
168  void setFrameSize(const unsigned int frameSize) noexcept2
169  { mFrameSize = frameSize; }
170 
184  unsigned int getFrameSize() const noexcept2 A_WARN_UNUSED
185  { return mFrameSize; }
186 
193  virtual void logic()
194  { }
195 
203  { return mParent; }
204 
212  void setWidth(const int width);
213 
222  { return mDimension.width; }
223 
231  void setHeight(const int height);
232 
241  { return mDimension.height; }
242 
251  void setSize(const int width, const int height);
252 
260  void setX(const int x);
261 
270  { return mDimension.x; }
271 
279  void setY(const int y);
280 
289  { return mDimension.y; }
290 
299  void setPosition(const int x, const int y);
300 
308  void setDimension(const Rect& dimension);
309 
318  { return mDimension; }
319 
327  void setFocusable(const bool focusable);
328 
335  bool isFocusable() const A_WARN_UNUSED;
336 
342  virtual bool isFocused() const A_WARN_UNUSED;
343 
352  void setEnabled(const bool enabled) noexcept2
353  { mEnabled = enabled; }
354 
362  bool isEnabled() const A_WARN_UNUSED;
363 
370  void setVisible(Visible visible);
371 
379  {
380  return mVisible == Visible_true &&
381  ((mParent == nullptr) || mParent->isVisible());
382  }
383 
390  void setBaseColor(const Color& color) noexcept2
391  { mBaseColor = color; }
392 
400  { return mBaseColor; }
401 
408  void setForegroundColor(const Color& color) noexcept2
409  { mForegroundColor = color; }
410 
417  { return mForegroundColor; }
418 
425  void setBackgroundColor(const Color &color) noexcept2
426  { mBackgroundColor = color; }
427 
434  { return mBackgroundColor; }
435 
440  virtual void requestFocus();
441 
445  virtual void requestMoveToTop();
446 
450  virtual void requestMoveToBottom();
451 
462  virtual void setFocusHandler(FocusHandler *const focusHandler);
463 
475  { return mFocusHandler; }
476 
485  void addActionListener(ActionListener *const actionListener);
486 
493  void removeActionListener(ActionListener *const actionListener);
494 
503  void addDeathListener(WidgetDeathListener *const deathListener);
504 
511  void removeDeathListener(WidgetDeathListener *const deathListener);
512 
521  void addMouseListener(MouseListener *const mouseListener);
522 
529  void removeMouseListener(MouseListener *const mouseListener);
530 
539  void addKeyListener(KeyListener *const keyListener);
540 
547  void removeKeyListener(KeyListener *const keyListener);
548 
557  void addFocusListener(FocusListener *const focusListener);
558 
565  void removeFocusListener(FocusListener *const focusListener);
566 
575  void addWidgetListener(WidgetListener *const widgetListener);
576 
583  void removeWidgetListener(WidgetListener *const widgetListener);
584 
596  void setActionEventId(const std::string &actionEventId) noexcept2
597  { mActionEventId = actionEventId; }
598 
605  const std::string &getActionEventId() const noexcept2
606  { return mActionEventId; }
607 
614  virtual void getAbsolutePosition(int& x, int& y) const;
615 
626  virtual void setParent(Widget* parent)
627  { mParent = parent; }
628 
638 
645  static void setGlobalFont(Font *const font);
646 
647  static Font *getGloablFont()
648  { return mGlobalFont; }
649 
650  static void cleanGlobalFont()
651  { mGlobalFont = nullptr; }
652 
660  void setFont(Font *const font);
661 
667  virtual void fontChanged()
668  { }
669 
677  static bool widgetExists(const Widget *const widget) A_WARN_UNUSED;
678 
689  { return mTabIn; }
690 
700  void setTabInEnabled(const bool enabled) noexcept2
701  { mTabIn = enabled; }
702 
713  { return mTabOut; }
714 
724  void setTabOutEnabled(const bool enabled) noexcept2
725  { mTabOut = enabled; }
726 
734  void requestModalFocus();
735 
744  virtual void requestModalMouseInputFocus();
745 
752  virtual void releaseModalFocus();
753 
760  virtual void releaseModalMouseInputFocus();
761 
768  virtual bool isModalFocused() const A_WARN_UNUSED;
769 
777  virtual bool isModalMouseInputFocused() const A_WARN_UNUSED;
778 
792  virtual Widget *getWidgetAt(int x A_UNUSED,
794  { return nullptr; }
795 
801  const std::list<MouseListener*>& getMouseListeners() const
802  A_CONST A_WARN_UNUSED;
803 
809  const std::list<KeyListener*>& getKeyListeners() const
810  A_CONST A_WARN_UNUSED;
811 
817  const std::list<FocusListener*>& getFocusListeners() const
818  A_CONST A_WARN_UNUSED;
819 
840 
849 
858  void setInternalFocusHandler(FocusHandler *const internalFocusHandler);
859 
867  virtual void moveToTop(Widget* widget A_UNUSED)
868  { }
869 
877  virtual void moveToBottom(Widget* widget A_UNUSED)
878  { }
879 
885  virtual void focusNext()
886  { }
887 
893  virtual void focusPrevious()
894  { }
895 
903  virtual void showWidgetPart(Widget *const widget A_UNUSED,
904  const Rect &area A_UNUSED)
905  { }
906 
916  void setId(const std::string& id)
917  { mId = id; }
918 
919  const std::string& getId() const noexcept2 A_WARN_UNUSED
920  { return mId; }
921 
931  virtual void showPart(const Rect &rectangle);
932 
934  { return mAllowLogic; }
935 
936  void setMouseConsume(const bool b) noexcept2
937  { mMouseConsume = b; }
938 
940  { return mMouseConsume; }
941 
942  void setRedraw(const bool b) noexcept2
943  { mRedraw = b; }
944 
945  virtual bool isSelectable() const noexcept2 A_WARN_UNUSED
946  { return mSelectable; }
947 
948  void setSelectable(const bool selectable) noexcept2
949  { mSelectable = selectable; }
950 
951  static void distributeWindowResizeEvent();
952 
953  void windowResized();
954 
955  static Widget *callPostInit(Widget *const widget) RETURNS_NONNULL;
956 
957  virtual void postInit()
958  { }
959 
964 
965  protected:
970  void distributeActionEvent();
971 
975  void distributeResizedEvent();
976 
980  void distributeMovedEvent();
981 
987  void distributeHiddenEvent();
988 
994  void distributeShownEvent();
995 
999  typedef std::list<MouseListener*> MouseListenerList;
1000 
1004  typedef MouseListenerList::iterator MouseListenerIterator;
1005 
1010 
1014  typedef std::list<KeyListener*> KeyListenerList;
1015 
1020 
1024  typedef KeyListenerList::iterator KeyListenerIterator;
1025 
1029  typedef std::list<ActionListener*> ActionListenerList;
1030 
1035 
1039  typedef ActionListenerList::iterator ActionListenerIterator;
1040 
1044  typedef std::list<WidgetDeathListener*> WidgetDeathListenerList;
1045 
1050 
1054  typedef WidgetDeathListenerList::iterator WidgetDeathListenerIterator;
1055 
1059  typedef std::list<FocusListener*> FocusListenerList;
1060 
1065 
1069  typedef FocusListenerList::iterator FocusListenerIterator;
1070 
1071  typedef std::list<WidgetListener*> WidgetListenerList;
1072 
1077 
1081  typedef WidgetListenerList::iterator WidgetListenerIterator;
1082 
1087 
1092 
1097 
1102 
1106  std::string mActionEventId;
1107 
1111  std::string mId;
1112 
1117 
1123 
1129 
1134 
1138  unsigned int mFrameSize;
1139 
1144 
1148  bool mTabIn;
1149 
1153  bool mTabOut;
1154 
1158  bool mEnabled;
1159 
1161 
1163 
1164  bool mRedraw;
1165 
1167 
1172 
1173  private:
1177  static std::list<Widget*> mAllWidgets;
1178 
1179  static std::set<Widget*> mAllWidgetsSet;
1180 };
1181 
1182 #endif // GUI_WIDGETS_WIDGET_H
Definition: color.h:76
Definition: font.h:90
Definition: rect.h:74
int y
Definition: rect.h:214
int width
Definition: rect.h:219
int x
Definition: rect.h:209
int height
Definition: rect.h:224
Definition: widget.h:99
void setVisible(Visible visible)
Definition: widget.cpp:225
bool mFocusable
Definition: widget.h:1143
static std::set< Widget * > mAllWidgetsSet
Definition: widget.h:1179
virtual bool isSelectable() const
Definition: widget.h:945
unsigned int getFrameSize() const
Definition: widget.h:184
Color mForegroundColor
Definition: widget.h:1086
virtual void setFocusHandler(FocusHandler *const focusHandler)
Definition: widget.cpp:238
virtual void safeDrawFrame(Graphics *graphics)
Definition: widget.h:152
virtual Rect getChildrenArea()
Definition: widget.cpp:451
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:168
static std::list< Widget * > mAllWidgets
Definition: widget.h:1177
void setFocusable(const bool focusable)
Definition: widget.cpp:192
void setBackgroundColor(const Color &color)
Definition: widget.h:425
void removeActionListener(ActionListener *const actionListener)
Definition: widget.cpp:257
static void distributeWindowResizeEvent()
Definition: widget.cpp:355
std::list< WidgetDeathListener * > WidgetDeathListenerList
Definition: widget.h:1044
const Color & getBaseColor() const
Definition: widget.h:399
void setWidth(const int width)
Definition: widget.cpp:133
void setSize(const int width, const int height)
Definition: widget.cpp:367
void distributeActionEvent()
Definition: widget.cpp:493
void distributeMovedEvent()
Definition: widget.cpp:475
FocusHandler * getFocusHandler()
Definition: widget.h:474
void setInternalFocusHandler(FocusHandler *const internalFocusHandler)
Definition: widget.cpp:461
std::list< FocusListener * > FocusListenerList
Definition: widget.h:1059
void setMouseConsume(const bool b)
Definition: widget.h:936
virtual void getAbsolutePosition(int &x, int &y) const
Definition: widget.cpp:312
virtual void requestMoveToTop()
Definition: widget.cpp:213
std::list< ActionListener * > ActionListenerList
Definition: widget.h:1029
void setBaseColor(const Color &color)
Definition: widget.h:390
virtual void focusNext()
Definition: widget.h:885
const Color & getForegroundColor() const
Definition: widget.h:416
void removeMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:297
KeyListenerList::iterator KeyListenerIterator
Definition: widget.h:1024
virtual void postInit()
Definition: widget.h:957
virtual void drawFrame(Graphics *graphics)
Definition: widget.h:149
virtual bool isModalMouseInputFocused() const
Definition: widget.cpp:422
virtual FocusHandler * getInternalFocusHandler()
Definition: widget.cpp:456
virtual void logic()
Definition: widget.h:193
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:511
void setForegroundColor(const Color &color)
Definition: widget.h:408
const Rect & getDimension() const
Definition: widget.h:317
Widget * mParent
Definition: widget.h:1128
Widget(const Widget2 *const widget)
Definition: widget.cpp:85
virtual void showWidgetPart(Widget *const widget, const Rect &area)
Definition: widget.h:903
virtual void moveToBottom(Widget *widget)
Definition: widget.h:877
Rect mDimension
Definition: widget.h:1101
WidgetListenerList::iterator WidgetListenerIterator
Definition: widget.h:1081
void setY(const int y)
Definition: widget.cpp:154
unsigned int mFrameSize
Definition: widget.h:1138
Color mBackgroundColor
Definition: widget.h:1091
void removeKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:277
static bool widgetExists(const Widget *const widget)
Definition: widget.cpp:361
static Font * mGlobalFont
Definition: widget.h:1171
bool isMouseConsume() const
Definition: widget.h:939
Font * mCurrentFont
Definition: widget.h:1133
virtual void releaseModalMouseInputFocus()
Definition: widget.cpp:401
bool mAllowLogic
Definition: widget.h:1160
FocusHandler * mFocusHandler
Definition: widget.h:1116
void setEnabled(const bool enabled)
Definition: widget.h:352
bool mTabIn
Definition: widget.h:1148
void removeDeathListener(WidgetDeathListener *const deathListener)
Definition: widget.cpp:267
void setRedraw(const bool b)
Definition: widget.h:942
const std::list< MouseListener * > & getMouseListeners() const A_CONST
Definition: widget.cpp:436
virtual void requestModalMouseInputFocus()
Definition: widget.cpp:387
void windowResized()
Definition: widget.cpp:517
static Widget * callPostInit(Widget *const widget)
Definition: widget.cpp:522
void requestModalFocus()
Definition: widget.cpp:380
void setId(const std::string &id)
Definition: widget.h:916
bool isFocusable() const
Definition: widget.cpp:199
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
ActionListenerList mActionListeners
Definition: widget.h:1034
FocusListenerList::iterator FocusListenerIterator
Definition: widget.h:1069
bool mSelectable
Definition: widget.h:1166
static void setGlobalFont(Font *const font)
Definition: widget.cpp:338
void setFont(Font *const font)
Definition: widget.cpp:349
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
void setHeight(const int height)
Definition: widget.cpp:140
KeyListenerList mKeyListeners
Definition: widget.h:1019
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
MouseListenerList::iterator MouseListenerIterator
Definition: widget.h:1004
void setTabInEnabled(const bool enabled)
Definition: widget.h:700
bool isTabInEnabled() const
Definition: widget.h:688
void distributeResizedEvent()
Definition: widget.cpp:466
virtual void fontChanged()
Definition: widget.h:667
~Widget()
Definition: widget.cpp:118
void setTabOutEnabled(const bool enabled)
Definition: widget.h:724
bool mEnabled
Definition: widget.h:1158
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
std::list< KeyListener * > KeyListenerList
Definition: widget.h:1014
FocusListenerList mFocusListeners
Definition: widget.h:1064
virtual void moveToTop(Widget *widget)
Definition: widget.h:867
virtual void requestFocus()
Definition: widget.cpp:204
int getY() const
Definition: widget.h:288
bool isTabOutEnabled() const
Definition: widget.h:712
const Color & getBackgroundColor() const
Definition: widget.h:433
virtual void focusPrevious()
Definition: widget.h:893
bool isAllowLogic() const
Definition: widget.h:933
std::list< MouseListener * > MouseListenerList
Definition: widget.h:999
void setDimension(const Rect &dimension)
Definition: widget.cpp:169
bool mRedraw
Definition: widget.h:1164
MouseListenerList mMouseListeners
Definition: widget.h:1009
std::string mId
Definition: widget.h:1111
Font * getFont() const
Definition: widget.cpp:331
Color mBaseColor
Definition: widget.h:1096
const std::string & getId() const
Definition: widget.h:919
WidgetDeathListenerList::iterator WidgetDeathListenerIterator
Definition: widget.h:1054
static void cleanGlobalFont()
Definition: widget.h:650
Visible mVisible
Definition: widget.h:963
virtual void safeDraw(Graphics *const graphics)=0
virtual void requestMoveToBottom()
Definition: widget.cpp:219
virtual bool isModalFocused() const
Definition: widget.cpp:408
virtual Widget * getWidgetAt(int x, int y)
Definition: widget.h:792
virtual void setParent(Widget *parent)
Definition: widget.h:626
FocusHandler * mInternalFocusHandler
Definition: widget.h:1122
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:307
void addDeathListener(WidgetDeathListener *const deathListener)
Definition: widget.cpp:262
void distributeHiddenEvent()
Definition: widget.cpp:484
std::string mActionEventId
Definition: widget.h:1106
bool isEnabled() const
Definition: widget.cpp:375
const std::list< KeyListener * > & getKeyListeners() const A_CONST
Definition: widget.cpp:441
void setSelectable(const bool selectable)
Definition: widget.h:948
virtual void draw(Graphics *const graphics)=0
WidgetListenerList mWidgetListeners
Definition: widget.h:1076
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
void setX(const int x)
Definition: widget.cpp:147
void setPosition(const int x, const int y)
Definition: widget.cpp:161
bool mMouseConsume
Definition: widget.h:1162
virtual void releaseModalFocus()
Definition: widget.cpp:394
void addFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:282
bool isVisible() const
Definition: widget.h:378
const std::list< FocusListener * > & getFocusListeners() const A_CONST
Definition: widget.cpp:446
bool mTabOut
Definition: widget.h:1153
void distributeShownEvent()
Definition: widget.cpp:502
void removeFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:287
std::list< WidgetListener * > WidgetListenerList
Definition: widget.h:1071
Widget * getParent() const
Definition: widget.h:202
ActionListenerList::iterator ActionListenerIterator
Definition: widget.h:1039
int getX() const
Definition: widget.h:269
int getHeight() const
Definition: widget.h:240
const std::string & getActionEventId() const
Definition: widget.h:605
int getWidth() const
Definition: widget.h:221
virtual bool isFocused() const
Definition: widget.cpp:184
static Font * getGloablFont()
Definition: widget.h:647
WidgetDeathListenerList mDeathListeners
Definition: widget.h:1049
#define RETURNS_NONNULL
Definition: localconsts.h:167
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define notfinal
Definition: localconsts.h:261
#define A_NONNULL(...)
Definition: localconsts.h:168
#define noexcept2
Definition: localconsts.h:50
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
#define A_UNUSED
Definition: localconsts.h:160
bool Visible
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30