ManaPlus
window.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /* _______ __ __ __ ______ __ __ _______ __ __
25  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
26  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
28  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
29  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31  *
32  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33  *
34  *
35  * Per Larsson a.k.a finalman
36  * Olof Naessén a.k.a jansem/yakslem
37  *
38  * Visit: http://guichan.sourceforge.net
39  *
40  * License: (BSD)
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  * notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  * notice, this list of conditions and the following disclaimer in
48  * the documentation and/or other materials provided with the
49  * distribution.
50  * 3. Neither the name of Guichan nor the names of its contributors may
51  * be used to endorse or promote products derived from this software
52  * without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 #ifndef GUI_WIDGETS_WINDOW_H
68 #define GUI_WIDGETS_WINDOW_H
69 
71 #include "enums/simpletypes/move.h"
72 
74 
75 #include "gui/fonts/textchunk.h"
76 
78 
81 
82 #include "render/graphics.h"
83 
84 #include "localconsts.h"
85 
86 class ContainerPlacer;
87 class ImageCollection;
88 class Layout;
89 class LayoutCell;
90 class Skin;
91 class WindowContainer;
92 
100  public MouseListener,
101  public WidgetListener
102 {
103  public:
115  Window(const std::string &caption,
116  const Modal modal,
117  Window *const parent,
118  std::string skin);
119 
121 
122 
125  ~Window() override;
126 
131 
135  void draw(Graphics *const graphics) override A_NONNULL(2);
136 
137  void safeDraw(Graphics *const graphics) override A_NONNULL(2);
138 
142  void setContentSize(int width, int height);
143 
147  void setLocationRelativeTo(const Widget *const widget);
148 
153  *const widget);
154 
158  void setLocationRelativeTo(const ImagePosition::Type &position,
159  int offsetX,
160  int offsetY);
161 
165  void setResizable(const bool resize);
166 
167  void redraw();
168 
172  void widgetResized(const Event &event) override;
173 
174  void widgetMoved(const Event& event) override;
175 
179  void widgetHidden(const Event &event) override;
180 
184  void setCloseButton(const bool flag);
185 
187  { return mCloseWindowButton; }
188 
189  void setAllowClose(const bool b)
190  { mAllowClose = b; }
191 
193  { return mCloseWindowButton || mAllowClose; }
194 
198  bool isResizable() const A_WARN_UNUSED;
199 
203  void setMinWidth(const int width);
204 
206  { return mMinWinWidth; }
207 
211  void setMinHeight(const int height);
212 
214  { return mMinWinHeight; }
215 
219  void setMaxWidth(const int width);
220 
222  { return mMaxWinWidth; }
223 
227  void setMaxHeight(const int height);
228 
230  { return mMaxWinHeight; }
231 
235  void setShowTitle(bool flag)
236  { mShowTitle = flag; }
237 
241  void setStickyButton(const bool flag);
242 
248  void setSticky(const bool sticky);
249 
254  { return mSticky; }
255 
259  void setStickyButtonLock(const bool sticky);
260 
265  { return mStickyButtonLock; }
266 
271  virtual void setVisible(Visible visible);
272 
277  void setVisible(const Visible visible, const bool forceSticky);
278 
283  { return mDefaultVisible; }
284 
288  void setDefaultVisible(const bool save)
289  { mDefaultVisible = save; }
290 
294  bool willSaveVisible() const
295  { return mSaveVisible; }
296 
300  void setSaveVisible(const bool save)
301  { mSaveVisible = save; }
302 
303  void postInit() override;
304 
311  { return mParentWindow; }
312 
317  virtual void scheduleDelete();
318 
322  void mousePressed(MouseEvent &event) override;
323 
328  void mouseDragged(MouseEvent &event) override;
329 
334  void mouseMoved(MouseEvent &event) override;
335 
340  void mouseReleased(MouseEvent &event) override;
341 
346  void mouseExited(MouseEvent &event) override;
347 
348  void mouseEntered(MouseEvent &event) override;
349 
350  void updateResizeHandler(MouseEvent &event);
351 
355  void setWindowName(const std::string &name)
356  { mWindowName = name; }
357 
361  const std::string &getWindowName() const noexcept2 A_WARN_UNUSED
362  { return mWindowName; }
363 
371  void loadWindowState();
372 
377  void saveWindowState();
378 
383  void setDefaultSize(const int defaultX, const int defaultY,
384  int defaultWidth, int defaultHeight);
385 
389  void setDefaultSize();
390 
397  void setDefaultSize(const int defaultWidth,
398  const int defaultHeight,
399  const ImagePosition::Type &position,
400  const int offsetx,
401  const int offsetY);
402 
407  virtual void resetToDefaultSize();
408 
413  void adjustPositionAfterResize(const int oldScreenWidth,
414  const int oldScreenHeight);
415 
420 
425  void clearLayout();
426 
435  void reflowLayout(int w,
436  int h);
437 
441  LayoutCell &place(const int x,
442  const int y,
443  Widget *const wg,
444  const int w,
445  const int h);
446 
450  ContainerPlacer getPlacer(const int x, const int y) A_WARN_UNUSED;
451 
455  void center();
456 
460  void centerHorisontally();
461 
467  virtual void close();
468 
472  void setModal(const Modal modal);
473 
475 
476  bool isResizeAllowed(const MouseEvent &event) const A_WARN_UNUSED;
477 
478  void setCaptionFont(Font *font)
479  { mCaptionFont = font; }
480 
481  void enableVisibleSound(bool b)
482  { mPlayVisibleSound = b; }
483 
485  { return mVisible == Visible_true; }
486 
494  void setPadding(int padding)
495  { mPadding = padding; }
496 
504  int getPadding() const
505  { return mPadding; }
506 
513  void setTitleBarHeight(unsigned int height)
514  { mTitleBarHeight = height; }
515 
522  unsigned int getTitleBarHeight() const
523  { return mTitleBarHeight; }
524 
531  void setCaption(const std::string& caption)
532  { mCaption = caption; mTextChanged = true; }
533 
540  const std::string& getCaption() const
541  { return mCaption; }
542 
550  { mAlignment = alignment; }
551 
559  { return mAlignment; }
560 
567  void setMovable(Move movable)
568  { mMovable = movable; }
569 
576  bool isMovable() const
577  { return mMovable == Move_true; }
578 
579  Rect getChildrenArea() override;
580 
584  virtual void resizeToContent();
585 
586 #ifdef USE_PROFILER
587  virtual void logic();
588 #endif // USE_PROFILER
589 
590  protected:
591  bool canMove() const A_WARN_UNUSED;
592 
593  int getOption(const std::string &name,
594  const int def) const A_WARN_UNUSED;
595 
596  bool getOptionBool(const std::string &name,
597  const bool def) const A_WARN_UNUSED;
598 
599  void setTitlePadding(const int p) noexcept2
600  { mTitlePadding = p; }
601 
603  { return mTitlePadding; }
604 
608  std::string mCaption;
609 
614 
618  int mPadding;
619 
623  unsigned int mTitleBarHeight;
624 
629 
636 
643 
647  bool mMoved;
648 
650  int mDefaultX;
651  int mDefaultY;
656  bool mShowTitle;
658 
659  private:
661  {
662  TOP = 0x01,
663  RIGHT = 0x02,
664  BOTTOM = 0x04,
665  LEFT = 0x08,
666  CLOSE = 0x10
667  };
668 
674  void ensureOnScreen();
675 
676  void adjustSizeToScreen();
677 
685  int getResizeHandles(const MouseEvent &event) A_WARN_UNUSED;
686 
694  std::string mWindowName;
700  static int mouseResize;
701  static int windowInstances;
709  static const unsigned resizeBorderWidth = 10;
725  bool mSticky;
728  bool mInit;
731 };
732 
733 #endif // GUI_WIDGETS_WINDOW_H
Definition: event.h:79
Definition: font.h:90
Definition: layout.h:45
Definition: rect.h:74
Definition: skin.h:37
Definition: widget.h:99
Visible mVisible
Definition: widget.h:963
Definition: window.h:102
int mDragOffsetY
Definition: window.h:642
int mDefaultY
Definition: window.h:651
void center()
Definition: window.cpp:1417
int mTitlePadding
Definition: window.h:712
Graphics::Alignment mAlignment
Definition: window.h:613
int mMinWinWidth
Definition: window.h:695
std::string mWindowName
Definition: window.h:694
void safeDraw(Graphics *const graphics)
Definition: window.cpp:423
int mCaptionOffsetY
Definition: window.h:655
int mDefaultHeight
Definition: window.h:653
void adjustSizeToScreen()
Definition: window.cpp:1285
bool mStickyButtonLock
Definition: window.h:726
bool mLastRedraw
Definition: window.h:657
void mouseReleased(MouseEvent &event)
Definition: window.cpp:907
void setSaveVisible(const bool save)
Definition: window.h:300
~Window()
Definition: window.cpp:260
bool willSaveVisible() const
Definition: window.h:294
void setResizable(const bool resize)
Definition: window.cpp:627
void widgetHidden(const Event &event)
Definition: window.cpp:725
void setMaxWidth(const int width)
Definition: window.cpp:617
void centerHorisontally()
Definition: window.cpp:1422
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1391
virtual void close()
Definition: window.cpp:902
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void setAllowClose(const bool b)
Definition: window.h:189
bool isWindowVisible() const
Definition: window.h:484
void clearLayout()
Definition: window.cpp:1372
bool getOptionBool(const std::string &name, const bool def) const
Definition: window.cpp:1466
void setTitlePadding(const int p)
Definition: window.h:599
Skin * mSkin
Definition: window.h:649
void setContentSize(int width, int height)
Definition: window.cpp:492
int getTitlePadding() const
Definition: window.h:602
void adjustPositionAfterResize(const int oldScreenWidth, const int oldScreenHeight)
Definition: window.cpp:1266
bool mShowTitle
Definition: window.h:656
bool mPlayVisibleSound
Definition: window.h:727
Window * getParentWindow() const
Definition: window.h:310
void reflowLayout(int w, int h)
Definition: window.cpp:1396
ResizeHandles
Definition: window.h:661
@ RIGHT
Definition: window.h:663
@ TOP
Definition: window.h:662
@ BOTTOM
Definition: window.h:664
@ LEFT
Definition: window.h:665
@ CLOSE
Definition: window.h:666
ImageCollection * mVertexes
Definition: window.h:710
Layout & getLayout()
Definition: window.cpp:1365
static int windowInstances
Definition: window.h:701
void setWindowName(const std::string &name)
Definition: window.h:355
bool isResizeAllowed(const MouseEvent &event) const
Definition: window.cpp:1345
int getOption(const std::string &name, const int def) const
Definition: window.cpp:1454
bool mMoved
Definition: window.h:647
TextChunk mTextChunk
Definition: window.h:693
Rect mCloseRect
Definition: window.h:690
unsigned int getTitleBarHeight() const
Definition: window.h:522
void mouseExited(MouseEvent &event)
Definition: window.cpp:924
void mouseDragged(MouseEvent &event)
Definition: window.cpp:982
void ensureOnScreen()
Definition: window.cpp:1427
int mDragOffsetX
Definition: window.h:635
Window(const std::string &caption, const Modal modal, Window *const parent, std::string skin)
Definition: window.cpp:109
bool isSticky() const
Definition: window.h:253
unsigned int mTitleBarHeight
Definition: window.h:623
int mMinWinHeight
Definition: window.h:696
bool canMove() const
Definition: window.cpp:977
Window * mParentWindow
Definition: window.h:688
bool isDefaultVisible() const
Definition: window.h:282
Rect getWindowArea() const
Definition: window.cpp:1446
bool getAlowClose() const
Definition: window.h:192
bool mDefaultVisible
Definition: window.h:722
virtual void resetToDefaultSize()
Definition: window.cpp:1259
void setSticky(const bool sticky)
Definition: window.cpp:766
int getMinWidth() const
Definition: window.h:205
void setModal(const Modal modal)
Definition: window.cpp:1069
bool isMovable() const
Definition: window.h:576
int mStickyPadding
Definition: window.h:718
int mDefaultWidth
Definition: window.h:652
int mOldResizeHandles
Definition: window.h:715
bool mSaveVisible
Definition: window.h:723
Layout * mLayout
Definition: window.h:689
bool mCloseWindowButton
Definition: window.h:721
Rect mGripRect
Definition: window.h:692
Graphics::Alignment mCaptionAlign
Definition: window.h:711
void setCaptionFont(Font *font)
Definition: window.h:478
void mouseEntered(MouseEvent &event)
Definition: window.cpp:919
void setCaption(const std::string &caption)
Definition: window.h:531
Graphics::Alignment getAlignment() const
Definition: window.h:558
int mGripPadding
Definition: window.h:713
void setMinHeight(const int height)
Definition: window.cpp:604
int mMaxWinHeight
Definition: window.h:698
void mouseMoved(MouseEvent &event)
Definition: window.cpp:967
void postInit()
Definition: window.cpp:249
virtual void resizeToContent()
Definition: window.cpp:1481
void setAlignment(Graphics::Alignment alignment)
Definition: window.h:549
bool mSticky
Definition: window.h:725
int getMaxWidth() const
Definition: window.h:221
void setMaxHeight(const int height)
Definition: window.cpp:622
void setMinWidth(const int width)
Definition: window.cpp:591
bool getCloseButton() const
Definition: window.h:186
void mousePressed(MouseEvent &event)
Definition: window.cpp:836
int getPadding() const
Definition: window.h:504
Image * mGrip
Definition: window.h:687
void enableVisibleSound(bool b)
Definition: window.h:481
void widgetResized(const Event &event)
Definition: window.cpp:655
int getResizeHandles(const MouseEvent &event)
Definition: window.cpp:1306
void widgetMoved(const Event &event)
Definition: window.cpp:720
void draw(Graphics *const graphics)
Definition: window.cpp:311
const std::string & getWindowName() const
Definition: window.h:361
int mResizeHandles
Definition: window.h:714
void setShowTitle(bool flag)
Definition: window.h:235
Rect getChildrenArea()
Definition: window.cpp:1473
static void setWindowContainer(WindowContainer *const windowContainer)
Definition: window.cpp:306
void setCloseButton(const bool flag)
Definition: window.cpp:749
Move mMovable
Definition: window.h:628
Rect mStickyRect
Definition: window.h:691
int getMaxHeight() const
Definition: window.h:229
virtual void scheduleDelete()
Definition: window.cpp:831
void setPadding(int padding)
Definition: window.h:494
int mPadding
Definition: window.h:618
const std::string & getCaption() const
Definition: window.h:540
bool mTextChanged
Definition: window.h:729
bool mAllowClose
Definition: window.h:730
bool isStickyButtonLock() const
Definition: window.h:264
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
int mMaxWinWidth
Definition: window.h:697
bool mInit
Definition: window.h:728
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultVisible(const bool save)
Definition: window.h:288
std::string mCaption
Definition: window.h:608
void setDefaultSize()
Definition: window.cpp:1198
int mDefaultX
Definition: window.h:650
int mStickySpacing
Definition: window.h:717
bool mStickyButton
Definition: window.h:724
void setLocationHorisontallyRelativeTo(const Widget *const widget)
Definition: window.cpp:528
bool isResizable() const
Definition: window.cpp:756
Modal mModal
Definition: window.h:720
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:509
void loadWindowState()
Definition: window.cpp:1087
void saveWindowState()
Definition: window.cpp:1149
void setTitleBarHeight(unsigned int height)
Definition: window.h:513
int mClosePadding
Definition: window.h:716
static int mouseResize
Definition: window.h:700
void redraw()
Definition: window.cpp:1406
void setStickyButton(const bool flag)
Definition: window.cpp:761
void setMovable(Move movable)
Definition: window.h:567
Font * mCaptionFont
Definition: window.h:719
int getMinHeight() const
Definition: window.h:213
static const unsigned resizeBorderWidth
Definition: window.h:709
int mCaptionOffsetX
Definition: window.h:654
void updateResizeHandler(MouseEvent &event)
Definition: window.cpp:930
bool Move
Definition: move.h:30
const bool Move_true
Definition: move.h:30
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define notfinal
Definition: localconsts.h:261
#define A_NONNULL(...)
Definition: localconsts.h:168
#define A_NONNULLPOINTER
Definition: localconsts.h:266
#define noexcept2
Definition: localconsts.h:50
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
bool Modal
Definition: modal.h:30
bool Visible
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30
WindowContainer * windowContainer