ManaPlus
widget.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 /* _______ __ __ __ ______ __ __ _______ __ __
22  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
23  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
24  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
25  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
26  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
27  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
28  *
29  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
30  *
31  *
32  * Per Larsson a.k.a finalman
33  * Olof Naessén a.k.a jansem/yakslem
34  *
35  * Visit: http://guichan.sourceforge.net
36  *
37  * License: (BSD)
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  * notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  * notice, this list of conditions and the following disclaimer in
45  * the documentation and/or other materials provided with the
46  * distribution.
47  * 3. Neither the name of Guichan nor the names of its contributors may
48  * be used to endorse or promote products derived from this software
49  * without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
57  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
58  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
59  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #ifndef GUI_WIDGETS_WIDGET_H
65 #define GUI_WIDGETS_WIDGET_H
66 
68 
69 #include "gui/rect.h"
70 
71 #include "gui/widgets/widget2.h"
72 
73 #include <list>
74 
75 #include "localconsts.h"
76 
77 class ActionListener;
79 class FocusHandler;
80 class FocusListener;
81 class Font;
82 class Graphics;
83 class KeyListener;
84 class MouseListener;
85 class WidgetListener;
86 
97 class Widget notfinal : public Widget2
98 {
99  public:
100  friend class BasicContainer;
101 
107  explicit Widget(const Widget2 *const widget);
108 
110 
111 
114  ~Widget() override;
115 
125  virtual void draw(Graphics *const graphics) A_NONNULL(2) = 0;
126 
127  virtual void safeDraw(Graphics *const graphics) A_NONNULL(2) = 0;
128 
148  virtual void drawFrame(Graphics* graphics A_UNUSED) A_NONNULL(2)
149  { }
150 
151  virtual void safeDrawFrame(Graphics* graphics A_UNUSED) A_NONNULL(2)
152  { }
153 
167  void setFrameSize(const unsigned int frameSize) noexcept2
168  { mFrameSize = frameSize; }
169 
183  unsigned int getFrameSize() const noexcept2 A_WARN_UNUSED
184  { return mFrameSize; }
185 
192  virtual void logic()
193  { }
194 
202  { return mParent; }
203 
211  void setWidth(const int width);
212 
221  { return mDimension.width; }
222 
230  void setHeight(const int height);
231 
240  { return mDimension.height; }
241 
250  void setSize(const int width, const int height);
251 
259  void setX(const int x);
260 
269  { return mDimension.x; }
270 
278  void setY(const int y);
279 
288  { return mDimension.y; }
289 
298  void setPosition(const int x, const int y);
299 
307  void setDimension(const Rect& dimension);
308 
317  { return mDimension; }
318 
326  void setFocusable(const bool focusable);
327 
334  bool isFocusable() const A_WARN_UNUSED;
335 
341  virtual bool isFocused() const A_WARN_UNUSED;
342 
351  void setEnabled(const bool enabled) noexcept2
352  { mEnabled = enabled; }
353 
361  bool isEnabled() const A_WARN_UNUSED;
362 
369  void setVisible(Visible visible);
370 
377  bool isVisible() const A_WARN_UNUSED
378  {
379  return mVisible == Visible_true &&
380  ((mParent == nullptr) || mParent->isVisible());
381  }
382 
389  void setBaseColor(const Color& color) noexcept2
390  { mBaseColor = color; }
391 
398  const Color& getBaseColor() const noexcept2 A_WARN_UNUSED
399  { return mBaseColor; }
400 
407  void setForegroundColor(const Color& color) noexcept2
408  { mForegroundColor = color; }
409 
415  const Color& getForegroundColor() const noexcept2 A_WARN_UNUSED
416  { return mForegroundColor; }
417 
424  void setBackgroundColor(const Color &color) noexcept2
425  { mBackgroundColor = color; }
426 
432  const Color &getBackgroundColor() const noexcept2 A_WARN_UNUSED
433  { return mBackgroundColor; }
434 
439  virtual void requestFocus();
440 
444  virtual void requestMoveToTop();
445 
449  virtual void requestMoveToBottom();
450 
461  virtual void setFocusHandler(FocusHandler *const focusHandler);
462 
474  { return mFocusHandler; }
475 
484  void addActionListener(ActionListener *const actionListener);
485 
492  void removeActionListener(ActionListener *const actionListener);
493 
502  void addDeathListener(WidgetDeathListener *const deathListener);
503 
510  void removeDeathListener(WidgetDeathListener *const deathListener);
511 
520  void addMouseListener(MouseListener *const mouseListener);
521 
528  void removeMouseListener(MouseListener *const mouseListener);
529 
538  void addKeyListener(KeyListener *const keyListener);
539 
546  void removeKeyListener(KeyListener *const keyListener);
547 
556  void addFocusListener(FocusListener *const focusListener);
557 
564  void removeFocusListener(FocusListener *const focusListener);
565 
574  void addWidgetListener(WidgetListener *const widgetListener);
575 
582  void removeWidgetListener(WidgetListener *const widgetListener);
583 
595  void setActionEventId(const std::string &actionEventId) noexcept2
596  { mActionEventId = actionEventId; }
597 
604  const std::string &getActionEventId() const noexcept2
605  { return mActionEventId; }
606 
613  virtual void getAbsolutePosition(int& x, int& y) const;
614 
625  virtual void setParent(Widget* parent)
626  { mParent = parent; }
627 
637 
644  static void setGlobalFont(Font *const font);
645 
646  static Font *getGloablFont()
647  { return mGlobalFont; }
648 
649  static void cleanGlobalFont()
650  { mGlobalFont = nullptr; }
651 
659  void setFont(Font *const font);
660 
666  virtual void fontChanged()
667  { }
668 
676  static bool widgetExists(const Widget *const widget) A_WARN_UNUSED;
677 
687  bool isTabInEnabled() const noexcept2 A_WARN_UNUSED
688  { return mTabIn; }
689 
699  void setTabInEnabled(const bool enabled) noexcept2
700  { mTabIn = enabled; }
701 
711  bool isTabOutEnabled() const noexcept2 A_WARN_UNUSED
712  { return mTabOut; }
713 
723  void setTabOutEnabled(const bool enabled) noexcept2
724  { mTabOut = enabled; }
725 
733  void requestModalFocus();
734 
743  virtual void requestModalMouseInputFocus();
744 
751  virtual void releaseModalFocus();
752 
759  virtual void releaseModalMouseInputFocus();
760 
767  virtual bool isModalFocused() const A_WARN_UNUSED;
768 
776  virtual bool isModalMouseInputFocused() const A_WARN_UNUSED;
777 
791  virtual Widget *getWidgetAt(int x A_UNUSED,
792  int y A_UNUSED) A_WARN_UNUSED
793  { return nullptr; }
794 
800  const std::list<MouseListener*>& getMouseListeners() const
801  A_CONST A_WARN_UNUSED;
802 
808  const std::list<KeyListener*>& getKeyListeners() const
809  A_CONST A_WARN_UNUSED;
810 
816  const std::list<FocusListener*>& getFocusListeners() const
817  A_CONST A_WARN_UNUSED;
818 
839 
848 
857  void setInternalFocusHandler(FocusHandler *const internalFocusHandler);
858 
866  virtual void moveToTop(Widget* widget A_UNUSED)
867  { }
868 
876  virtual void moveToBottom(Widget* widget A_UNUSED)
877  { }
878 
884  virtual void focusNext()
885  { }
886 
892  virtual void focusPrevious()
893  { }
894 
902  virtual void showWidgetPart(Widget *const widget A_UNUSED,
903  const Rect &area A_UNUSED)
904  { }
905 
915  void setId(const std::string& id)
916  { mId = id; }
917 
918  const std::string& getId() const noexcept2 A_WARN_UNUSED
919  { return mId; }
920 
930  virtual void showPart(const Rect &rectangle);
931 
932  bool isAllowLogic() const noexcept2 A_WARN_UNUSED
933  { return mAllowLogic; }
934 
935  void setMouseConsume(const bool b) noexcept2
936  { mMouseConsume = b; }
937 
938  bool isMouseConsume() const noexcept2 A_WARN_UNUSED
939  { return mMouseConsume; }
940 
941  void setRedraw(const bool b) noexcept2
942  { mRedraw = b; }
943 
944  virtual bool isSelectable() const noexcept2 A_WARN_UNUSED
945  { return mSelectable; }
946 
947  void setSelectable(const bool selectable) noexcept2
948  { mSelectable = selectable; }
949 
950  static void distributeWindowResizeEvent();
951 
952  void windowResized();
953 
954  static Widget *callPostInit(Widget *const widget) RETURNS_NONNULL;
955 
956  virtual void postInit()
957  { }
958 
963 
964  protected:
969  void distributeActionEvent();
970 
974  void distributeResizedEvent();
975 
979  void distributeMovedEvent();
980 
986  void distributeHiddenEvent();
987 
993  void distributeShownEvent();
994 
998  typedef std::list<MouseListener*> MouseListenerList;
999 
1003  typedef MouseListenerList::iterator MouseListenerIterator;
1004 
1008  MouseListenerList mMouseListeners;
1009 
1013  typedef std::list<KeyListener*> KeyListenerList;
1014 
1018  KeyListenerList mKeyListeners;
1019 
1023  typedef KeyListenerList::iterator KeyListenerIterator;
1024 
1028  typedef std::list<ActionListener*> ActionListenerList;
1029 
1033  ActionListenerList mActionListeners;
1034 
1038  typedef ActionListenerList::iterator ActionListenerIterator;
1039 
1043  typedef std::list<WidgetDeathListener*> WidgetDeathListenerList;
1044 
1048  WidgetDeathListenerList mDeathListeners;
1049 
1053  typedef WidgetDeathListenerList::iterator WidgetDeathListenerIterator;
1054 
1058  typedef std::list<FocusListener*> FocusListenerList;
1059 
1063  FocusListenerList mFocusListeners;
1064 
1068  typedef FocusListenerList::iterator FocusListenerIterator;
1069 
1070  typedef std::list<WidgetListener*> WidgetListenerList;
1071 
1075  WidgetListenerList mWidgetListeners;
1076 
1080  typedef WidgetListenerList::iterator WidgetListenerIterator;
1081 
1086 
1091 
1096 
1101 
1105  std::string mActionEventId;
1106 
1110  std::string mId;
1111 
1116 
1122 
1128 
1133 
1137  unsigned int mFrameSize;
1138 
1143 
1147  bool mTabIn;
1148 
1152  bool mTabOut;
1153 
1157  bool mEnabled;
1158 
1160 
1162 
1163  bool mRedraw;
1164 
1166 
1171 
1172  private:
1176  static std::list<Widget*> mAllWidgets;
1177 
1178  static std::set<Widget*> mAllWidgetsSet;
1179 };
1180 
1181 #endif // GUI_WIDGETS_WIDGET_H
void setSize(const int width, const int height)
Definition: widget.cpp:366
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
Font * getFont() const
Definition: widget.cpp:330
int width
Definition: rect.h:218
virtual void focusNext()
Definition: widget.h:884
FocusListenerList mFocusListeners
Definition: widget.h:1063
virtual void fontChanged()
Definition: widget.h:666
int getWidth() const
Definition: widget.h:220
virtual void safeDrawFrame(Graphics *graphics)
Definition: widget.h:151
std::string mId
Definition: widget.h:1110
static std::list< Widget * > mAllWidgets
Definition: widget.h:1176
KeyListenerList::iterator KeyListenerIterator
Definition: widget.h:1023
void setWidth(const int width)
Definition: widget.cpp:132
static std::set< Widget * > mAllWidgetsSet
Definition: widget.h:1178
Definition: font.h:88
void removeDeathListener(WidgetDeathListener *const deathListener)
Definition: widget.cpp:266
bool mTabIn
Definition: widget.h:1147
void requestModalFocus()
Definition: widget.cpp:379
virtual void releaseModalFocus()
Definition: widget.cpp:393
static void cleanGlobalFont()
Definition: widget.h:649
const bool Visible_true
Definition: visible.h:29
KeyListenerList mKeyListeners
Definition: widget.h:1018
virtual void draw(Graphics *const graphics)=0
static Widget * callPostInit(Widget *const widget)
Definition: widget.cpp:521
void setY(const int y)
Definition: widget.cpp:153
void setRedraw(const bool b)
Definition: widget.h:941
int getY() const
Definition: widget.h:287
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void setVisible(Visible visible)
Definition: widget.cpp:224
Definition: rect.h:72
virtual void requestMoveToTop()
Definition: widget.cpp:212
void removeFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:286
#define A_WARN_UNUSED
Definition: localconsts.h:172
static void setGlobalFont(Font *const font)
Definition: widget.cpp:337
void setTabOutEnabled(const bool enabled)
Definition: widget.h:723
virtual bool isSelectable() const
Definition: widget.h:944
virtual void safeDraw(Graphics *const graphics)=0
void addFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:281
virtual FocusHandler * getInternalFocusHandler()
Definition: widget.cpp:455
void setDimension(const Rect &dimension)
Definition: widget.cpp:168
FocusHandler * getFocusHandler()
Definition: widget.h:473
bool isTabInEnabled() const
Definition: widget.h:687
virtual void releaseModalMouseInputFocus()
Definition: widget.cpp:400
bool isTabOutEnabled() const
Definition: widget.h:711
FocusHandler * mFocusHandler
Definition: widget.h:1115
const std::string & getActionEventId() const
Definition: widget.h:604
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
bool mTabOut
Definition: widget.h:1152
#define A_NONNULL(...)
Definition: localconsts.h:179
Color mForegroundColor
Definition: widget.h:1085
Color mBaseColor
Definition: widget.h:1095
void removeKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:276
virtual bool isFocused() const
Definition: widget.cpp:183
unsigned int getFrameSize() const
Definition: widget.h:183
virtual void requestModalMouseInputFocus()
Definition: widget.cpp:386
std::list< WidgetDeathListener * > WidgetDeathListenerList
Definition: widget.h:1043
MouseListenerList mMouseListeners
Definition: widget.h:1008
Visible mVisible
Definition: widget.h:962
void distributeActionEvent()
Definition: widget.cpp:492
void setSelectable(const bool selectable)
Definition: widget.h:947
std::list< ActionListener * > ActionListenerList
Definition: widget.h:1028
virtual bool isModalFocused() const
Definition: widget.cpp:407
int x
Definition: rect.h:208
virtual void showWidgetPart(Widget *const widget, const Rect &area)
Definition: widget.h:902
const std::string & getId() const
Definition: widget.h:918
Color mBackgroundColor
Definition: widget.h:1090
void removeMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:296
ActionListenerList mActionListeners
Definition: widget.h:1033
virtual void requestFocus()
Definition: widget.cpp:203
void windowResized()
Definition: widget.cpp:516
Font * mCurrentFont
Definition: widget.h:1132
std::list< KeyListener * > KeyListenerList
Definition: widget.h:1013
Widget * mParent
Definition: widget.h:1127
void setX(const int x)
Definition: widget.cpp:146
void setTabInEnabled(const bool enabled)
Definition: widget.h:699
bool mFocusable
Definition: widget.h:1142
int y
Definition: rect.h:213
static void distributeWindowResizeEvent()
Definition: widget.cpp:354
virtual void logic()
Definition: widget.h:192
unsigned int mFrameSize
Definition: widget.h:1137
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:167
bool isMouseConsume() const
Definition: widget.h:938
#define notfinal
Definition: localconsts.h:289
bool mMouseConsume
Definition: widget.h:1161
const Color & getBackgroundColor() const
Definition: widget.h:432
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mAllowLogic
Definition: widget.h:1159
FocusHandler * mInternalFocusHandler
Definition: widget.h:1121
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void setBaseColor(const Color &color)
Definition: widget.h:389
bool isEnabled() const
Definition: widget.cpp:374
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int height
Definition: rect.h:223
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
static Font * getGloablFont()
Definition: widget.h:646
WidgetListenerList mWidgetListeners
Definition: widget.h:1075
virtual void getAbsolutePosition(int &x, int &y) const
Definition: widget.cpp:311
void setForegroundColor(const Color &color)
Definition: widget.h:407
ActionListenerList::iterator ActionListenerIterator
Definition: widget.h:1038
virtual void setParent(Widget *parent)
Definition: widget.h:625
const Rect & getDimension() const
Definition: widget.h:316
virtual bool isModalMouseInputFocused() const
Definition: widget.cpp:421
virtual void moveToBottom(Widget *widget)
Definition: widget.h:876
void setEnabled(const bool enabled)
Definition: widget.h:351
void distributeShownEvent()
Definition: widget.cpp:501
virtual void moveToTop(Widget *widget)
Definition: widget.h:866
MouseListenerList::iterator MouseListenerIterator
Definition: widget.h:1003
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
void setBackgroundColor(const Color &color)
Definition: widget.h:424
int getX() const
Definition: widget.h:268
Widget * getParent() const
Definition: widget.h:201
#define A_UNUSED
Definition: localconsts.h:171
void setFocusable(const bool focusable)
Definition: widget.cpp:191
bool isVisible() const
Definition: widget.h:377
Definition: widget.h:97
void setFont(Font *const font)
Definition: widget.cpp:348
void setId(const std::string &id)
Definition: widget.h:915
const Color & getBaseColor() const
Definition: widget.h:398
virtual void requestMoveToBottom()
Definition: widget.cpp:218
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:306
static bool widgetExists(const Widget *const widget)
Definition: widget.cpp:360
bool mRedraw
Definition: widget.h:1163
bool mEnabled
Definition: widget.h:1157
std::list< WidgetListener * > WidgetListenerList
Definition: widget.h:1070
bool isFocusable() const
Definition: widget.cpp:198
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:510
int getHeight() const
Definition: widget.h:239
WidgetListenerList::iterator WidgetListenerIterator
Definition: widget.h:1080
void setMouseConsume(const bool b)
Definition: widget.h:935
std::list< MouseListener * > MouseListenerList
Definition: widget.h:998
Definition: color.h:74
void distributeResizedEvent()
Definition: widget.cpp:465
std::string mActionEventId
Definition: widget.h:1105
void removeActionListener(ActionListener *const actionListener)
Definition: widget.cpp:256
void addDeathListener(WidgetDeathListener *const deathListener)
Definition: widget.cpp:261
virtual void postInit()
Definition: widget.h:956
void distributeMovedEvent()
Definition: widget.cpp:474
~Widget()
Definition: widget.cpp:117
bool isAllowLogic() const
Definition: widget.h:932
const std::list< KeyListener * > & getKeyListeners() const A_CONST
Definition: widget.cpp:440
virtual void setFocusHandler(FocusHandler *const focusHandler)
Definition: widget.cpp:237
std::list< FocusListener * > FocusListenerList
Definition: widget.h:1058
const Color & getForegroundColor() const
Definition: widget.h:415
WidgetDeathListenerList mDeathListeners
Definition: widget.h:1048
void setInternalFocusHandler(FocusHandler *const internalFocusHandler)
Definition: widget.cpp:460
virtual void drawFrame(Graphics *graphics)
Definition: widget.h:148
FocusListenerList::iterator FocusListenerIterator
Definition: widget.h:1068
virtual Rect getChildrenArea()
Definition: widget.cpp:450
Rect mDimension
Definition: widget.h:1100
#define noexcept2
Definition: localconsts.h:49
virtual void focusPrevious()
Definition: widget.h:892
bool mSelectable
Definition: widget.h:1165
const std::list< FocusListener * > & getFocusListeners() const A_CONST
Definition: widget.cpp:445
virtual Widget * getWidgetAt(int x, int y)
Definition: widget.h:791
WidgetDeathListenerList::iterator WidgetDeathListenerIterator
Definition: widget.h:1053
void distributeHiddenEvent()
Definition: widget.cpp:483
const std::list< MouseListener * > & getMouseListeners() const A_CONST
Definition: widget.cpp:435
#define RETURNS_NONNULL
Definition: localconsts.h:178
static Font * mGlobalFont
Definition: widget.h:1170