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-2017 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /* _______ __ __ __ ______ __ __ _______ __ __
24  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
25  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
26  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
27  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
28  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
29  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
30  *
31  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
32  *
33  *
34  * Per Larsson a.k.a finalman
35  * Olof Naessén a.k.a jansem/yakslem
36  *
37  * Visit: http://guichan.sourceforge.net
38  *
39  * License: (BSD)
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  * notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  * notice, this list of conditions and the following disclaimer in
47  * the documentation and/or other materials provided with the
48  * distribution.
49  * 3. Neither the name of Guichan nor the names of its contributors may
50  * be used to endorse or promote products derived from this software
51  * without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #ifndef GUI_WIDGETS_WINDOW_H
67 #define GUI_WIDGETS_WINDOW_H
68 
70 #include "enums/simpletypes/move.h"
71 
73 
74 #include "gui/fonts/textchunk.h"
75 
77 
80 
81 #include "render/graphics.h"
82 
83 #include "localconsts.h"
84 
85 class ContainerPlacer;
86 class ImageCollection;
87 class Layout;
88 class LayoutCell;
89 class Skin;
90 class WindowContainer;
91 
99  public MouseListener,
100  public WidgetListener
101 {
102  public:
114  explicit Window(const std::string &caption = "Window",
115  const Modal modal = Modal_false,
116  Window *const parent = nullptr,
117  std::string skin = "");
118 
120 
121 
124  virtual ~Window();
125 
130 
134  void draw(Graphics *const graphics) override A_NONNULL(2);
135 
136  void safeDraw(Graphics *const graphics) override A_NONNULL(2);
137 
141  void setContentSize(int width, int height);
142 
146  void setLocationRelativeTo(const Widget *const widget);
147 
152  *const widget);
153 
157  void setLocationRelativeTo(const ImagePosition::Type &position,
158  int offsetX = 0, int offsetY = 0);
159 
163  void setResizable(const bool resize);
164 
165  void redraw();
166 
170  void widgetResized(const Event &event) override;
171 
172  void widgetMoved(const Event& event) override;
173 
177  void widgetHidden(const Event &event) override;
178 
182  void setCloseButton(const bool flag);
183 
185  { return mCloseWindowButton; }
186 
187  void setAllowClose(const bool b)
188  { mAllowClose = b; }
189 
191  { return mCloseWindowButton || mAllowClose; }
192 
196  bool isResizable() const A_WARN_UNUSED;
197 
201  void setMinWidth(const int width);
202 
203  int getMinWidth() const noexcept2 A_WARN_UNUSED
204  { return mMinWinWidth; }
205 
209  void setMinHeight(const int height);
210 
211  int getMinHeight() const noexcept2 A_WARN_UNUSED
212  { return mMinWinHeight; }
213 
217  void setMaxWidth(const int width);
218 
219  int getMaxWidth() const noexcept2 A_WARN_UNUSED
220  { return mMaxWinWidth; }
221 
225  void setMaxHeight(const int height);
226 
227  int getMaxHeight() const noexcept2 A_WARN_UNUSED
228  { return mMaxWinHeight; }
229 
233  void setShowTitle(bool flag)
234  { mShowTitle = flag; }
235 
239  void setStickyButton(const bool flag);
240 
246  void setSticky(const bool sticky);
247 
251  bool isSticky() const noexcept2 A_WARN_UNUSED
252  { return mSticky; }
253 
257  void setStickyButtonLock(const bool sticky);
258 
262  bool isStickyButtonLock() const noexcept2 A_WARN_UNUSED
263  { return mStickyButtonLock; }
264 
269  virtual void setVisible(Visible visible);
270 
275  void setVisible(const Visible visible, const bool forceSticky);
276 
280  bool isDefaultVisible() const noexcept2 A_WARN_UNUSED
281  { return mDefaultVisible; }
282 
286  void setDefaultVisible(const bool save)
287  { mDefaultVisible = save; }
288 
292  bool willSaveVisible() const
293  { return mSaveVisible; }
294 
298  void setSaveVisible(const bool save)
299  { mSaveVisible = save; }
300 
301  void postInit() override;
302 
309  { return mParentWindow; }
310 
315  virtual void scheduleDelete();
316 
320  void mousePressed(MouseEvent &event) override;
321 
326  void mouseDragged(MouseEvent &event) override;
327 
332  void mouseMoved(MouseEvent &event) override;
333 
338  void mouseReleased(MouseEvent &event) override;
339 
344  void mouseExited(MouseEvent &event) override;
345 
346  void mouseEntered(MouseEvent &event) override;
347 
348  void updateResizeHandler(MouseEvent &event);
349 
353  void setWindowName(const std::string &name)
354  { mWindowName = name; }
355 
359  const std::string &getWindowName() const noexcept2 A_WARN_UNUSED
360  { return mWindowName; }
361 
369  void loadWindowState();
370 
375  void saveWindowState();
376 
381  void setDefaultSize(const int defaultX, const int defaultY,
382  int defaultWidth, int defaultHeight);
383 
387  void setDefaultSize();
388 
395  void setDefaultSize(const int defaultWidth, const int defaultHeight,
396  const ImagePosition::Type &position,
397  const int offsetx = 0, const int offsetY = 0);
398 
403  virtual void resetToDefaultSize();
404 
409  void adjustPositionAfterResize(const int oldScreenWidth,
410  const int oldScreenHeight);
411 
416 
421  void clearLayout();
422 
431  void reflowLayout(int w = 0, int h = 0);
432 
436  LayoutCell &place(const int x, const int y, Widget *const wg,
437  const int w = 1, const int h = 1);
438 
442  ContainerPlacer getPlacer(const int x, const int y) A_WARN_UNUSED;
443 
447  void center();
448 
452  void centerHorisontally();
453 
459  virtual void close();
460 
464  void setModal(const Modal modal);
465 
467 
468  bool isResizeAllowed(const MouseEvent &event) const A_WARN_UNUSED;
469 
471  { mCaptionFont = font; }
472 
473  void enableVisibleSound(bool b)
474  { mPlayVisibleSound = b; }
475 
476  bool isWindowVisible() const noexcept2 A_WARN_UNUSED
477  { return mVisible == Visible_true; }
478 
486  void setPadding(int padding)
487  { mPadding = padding; }
488 
496  int getPadding() const
497  { return mPadding; }
498 
505  void setTitleBarHeight(unsigned int height)
506  { mTitleBarHeight = height; }
507 
514  unsigned int getTitleBarHeight() const
515  { return mTitleBarHeight; }
516 
523  void setCaption(const std::string& caption)
524  { mCaption = caption; mTextChanged = true; }
525 
532  const std::string& getCaption() const
533  { return mCaption; }
534 
542  { mAlignment = alignment; }
543 
551  { return mAlignment; }
552 
559  void setMovable(Move movable)
560  { mMovable = movable; }
561 
568  bool isMovable() const
569  { return mMovable == Move_true; }
570 
571  Rect getChildrenArea() override;
572 
576  virtual void resizeToContent();
577 
578 #ifdef USE_PROFILER
579  virtual void logic();
580 #endif // USE_PROFILER
581 
582  protected:
583  bool canMove() const A_WARN_UNUSED;
584 
585  int getOption(const std::string &name,
586  const int def = 0) const A_WARN_UNUSED;
587 
588  bool getOptionBool(const std::string &name,
589  const bool def = false) const A_WARN_UNUSED;
590 
591  void setTitlePadding(const int p) noexcept2
592  { mTitlePadding = p; }
593 
594  int getTitlePadding() const noexcept2 A_WARN_UNUSED
595  { return mTitlePadding; }
596 
600  std::string mCaption;
601 
606 
610  int mPadding;
611 
615  unsigned int mTitleBarHeight;
616 
621 
628 
635 
639  bool mMoved;
640 
642  int mDefaultX;
643  int mDefaultY;
648  bool mShowTitle;
650 
651  private:
653  {
654  TOP = 0x01,
655  RIGHT = 0x02,
656  BOTTOM = 0x04,
657  LEFT = 0x08,
658  CLOSE = 0x10
659  };
660 
666  void ensureOnScreen();
667 
668  void adjustSizeToScreen();
669 
677  int getResizeHandles(const MouseEvent &event) A_WARN_UNUSED;
678 
686  std::string mWindowName;
692  static int mouseResize;
693  static int windowInstances;
701  static const unsigned resizeBorderWidth = 10;
717  bool mSticky;
720  bool mInit;
723 };
724 
725 #endif // GUI_WIDGETS_WINDOW_H
bool mShowTitle
Definition: window.h:648
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
void setDefaultSize()
Definition: window.cpp:1186
Rect mGripRect
Definition: window.h:684
unsigned int getTitleBarHeight() const
Definition: window.h:514
bool isResizable() const
Definition: window.cpp:744
std::string mWindowName
Definition: window.h:686
void updateResizeHandler(MouseEvent &event)
Definition: window.cpp:918
void redraw()
Definition: window.cpp:1394
Font * mCaptionFont
Definition: window.h:711
void setDefaultVisible(const bool save)
Definition: window.h:286
Skin * mSkin
Definition: window.h:641
TextChunk mTextChunk
Definition: window.h:685
void adjustSizeToScreen()
Definition: window.cpp:1273
int mGripPadding
Definition: window.h:705
Definition: skin.h:35
int mDragOffsetY
Definition: window.h:634
int mPadding
Definition: window.h:610
#define A_NONNULLPOINTER
Definition: localconsts.h:294
Definition: font.h:88
void setLocationHorisontallyRelativeTo(const Widget *const widget)
Definition: window.cpp:518
const bool Visible_true
Definition: visible.h:29
void setMaxWidth(const int width)
Definition: window.cpp:605
void loadWindowState()
Definition: window.cpp:1075
WindowContainer * windowContainer
virtual void resetToDefaultSize()
Definition: window.cpp:1247
static int mouseResize
Definition: window.h:692
Rect getChildrenArea()
Definition: window.cpp:1461
void setMaxHeight(const int height)
Definition: window.cpp:610
virtual void resizeToContent()
Definition: window.cpp:1469
int mOldResizeHandles
Definition: window.h:707
bool mTextChanged
Definition: window.h:721
bool mStickyButtonLock
Definition: window.h:718
int mCaptionOffsetY
Definition: window.h:647
bool isDefaultVisible() const
Definition: window.h:280
Definition: rect.h:72
void setMinWidth(const int width)
Definition: window.cpp:579
#define A_WARN_UNUSED
Definition: localconsts.h:172
Definition: window.h:98
int mStickySpacing
Definition: window.h:709
void setShowTitle(bool flag)
Definition: window.h:233
bool mDefaultVisible
Definition: window.h:714
void centerHorisontally()
Definition: window.cpp:1410
ImageCollection * mVertexes
Definition: window.h:702
void mouseMoved(MouseEvent &event)
Definition: window.cpp:955
Rect mCloseRect
Definition: window.h:682
bool mStickyButton
Definition: window.h:716
bool getOptionBool(const std::string &name, const bool def=false) const
Definition: window.cpp:1454
Window * getParentWindow() const
Definition: window.h:308
bool mMoved
Definition: window.h:639
#define A_NONNULL(...)
Definition: localconsts.h:179
int mCaptionOffsetX
Definition: window.h:646
int mDragOffsetX
Definition: window.h:627
bool isMovable() const
Definition: window.h:568
int mMaxWinWidth
Definition: window.h:689
void reflowLayout(int w=0, int h=0)
Definition: window.cpp:1384
int mDefaultY
Definition: window.h:643
Visible mVisible
Definition: widget.h:962
void ensureOnScreen()
Definition: window.cpp:1415
virtual void close()
Definition: window.cpp:890
void widgetHidden(const Event &event)
Definition: window.cpp:713
void setContentSize(int width, int height)
Definition: window.cpp:484
bool mAllowClose
Definition: window.h:722
int getMinHeight() const
Definition: window.h:211
void widgetResized(const Event &event)
Definition: window.cpp:643
int getTitlePadding() const
Definition: window.h:594
void setCaptionFont(Font *font)
Definition: window.h:470
int mTitlePadding
Definition: window.h:704
void setSticky(const bool sticky)
Definition: window.cpp:754
void draw(Graphics *const graphics)
Definition: window.cpp:303
bool canMove() const
Definition: window.cpp:965
int mDefaultWidth
Definition: window.h:644
LayoutCell & place(const int x, const int y, Widget *const wg, const int w=1, const int h=1)
Definition: window.cpp:1372
bool mLastRedraw
Definition: window.h:649
int mStickyPadding
Definition: window.h:710
bool mInit
Definition: window.h:720
Graphics::Alignment mCaptionAlign
Definition: window.h:703
#define notfinal
Definition: localconsts.h:289
static const unsigned resizeBorderWidth
Definition: window.h:701
bool isWindowVisible() const
Definition: window.h:476
int mClosePadding
Definition: window.h:708
virtual ~Window()
Definition: window.cpp:252
bool willSaveVisible() const
Definition: window.h:292
void setCloseButton(const bool flag)
Definition: window.cpp:737
bool mCloseWindowButton
Definition: window.h:713
Rect getWindowArea() const
Definition: window.cpp:1434
int mResizeHandles
Definition: window.h:706
bool mSticky
Definition: window.h:717
void enableVisibleSound(bool b)
Definition: window.h:473
const std::string & getWindowName() const
Definition: window.h:359
const bool Move_true
Definition: move.h:29
void setMinHeight(const int height)
Definition: window.cpp:592
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1379
static int windowInstances
Definition: window.h:693
Rect mStickyRect
Definition: window.h:683
virtual void scheduleDelete()
Definition: window.cpp:819
Modal mModal
Definition: window.h:712
bool mPlayVisibleSound
Definition: window.h:719
Layout & getLayout()
Definition: window.cpp:1353
void widgetMoved(const Event &event)
Definition: window.cpp:708
int getMaxHeight() const
Definition: window.h:227
std::string mCaption
Definition: window.h:600
void setAllowClose(const bool b)
Definition: window.h:187
void setTitlePadding(const int p)
Definition: window.h:591
ResizeHandles
Definition: window.h:652
void mouseReleased(MouseEvent &event)
Definition: window.cpp:895
void setSaveVisible(const bool save)
Definition: window.h:298
bool mSaveVisible
Definition: window.h:715
virtual void setVisible(Visible visible)
Definition: window.cpp:766
void center()
Definition: window.cpp:1405
Move mMovable
Definition: window.h:620
Definition: widget.h:97
int mDefaultHeight
Definition: window.h:645
Definition: event.h:77
int getMaxWidth() const
Definition: window.h:219
int mDefaultX
Definition: window.h:642
void mouseDragged(MouseEvent &event)
Definition: window.cpp:970
bool isStickyButtonLock() const
Definition: window.h:262
void setMovable(Move movable)
Definition: window.h:559
bool isResizeAllowed(const MouseEvent &event) const
Definition: window.cpp:1333
void mouseEntered(MouseEvent &event)
Definition: window.cpp:907
void mousePressed(MouseEvent &event)
Definition: window.cpp:824
Definition: image.h:61
void mouseExited(MouseEvent &event)
Definition: window.cpp:912
Layout * mLayout
Definition: window.h:681
bool getAlowClose() const
Definition: window.h:190
void setResizable(const bool resize)
Definition: window.cpp:615
bool isSticky() const
Definition: window.h:251
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:760
static void setWindowContainer(WindowContainer *const windowContainer)
Definition: window.cpp:298
int getOption(const std::string &name, const int def=0) const
Definition: window.cpp:1442
void setPadding(int padding)
Definition: window.h:486
bool getCloseButton() const
Definition: window.h:184
void setTitleBarHeight(unsigned int height)
Definition: window.h:505
Window(const std::string &caption="Window", const Modal modal=Modal_false, Window *const parent=0, std::string skin="")
Definition: window.cpp:108
Graphics::Alignment getAlignment() const
Definition: window.h:550
void setStickyButton(const bool flag)
Definition: window.cpp:749
Definition: layout.h:43
Window * mParentWindow
Definition: window.h:680
void saveWindowState()
Definition: window.cpp:1137
void clearLayout()
Definition: window.cpp:1360
void setCaption(const std::string &caption)
Definition: window.h:523
unsigned int mTitleBarHeight
Definition: window.h:615
const std::string & getCaption() const
Definition: window.h:532
int mMinWinWidth
Definition: window.h:687
Graphics::Alignment mAlignment
Definition: window.h:605
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:501
int getResizeHandles(const MouseEvent &event)
Definition: window.cpp:1294
#define noexcept2
Definition: localconsts.h:49
int getMinWidth() const
Definition: window.h:203
int getPadding() const
Definition: window.h:496
void setModal(const Modal modal)
Definition: window.cpp:1057
void setAlignment(Graphics::Alignment alignment)
Definition: window.h:541
int mMaxWinHeight
Definition: window.h:690
void postInit()
Definition: window.cpp:241
int mMinWinHeight
Definition: window.h:688
void safeDraw(Graphics *const graphics)
Definition: window.cpp:415
void setWindowName(const std::string &name)
Definition: window.h:353
Image * mGrip
Definition: window.h:679
void adjustPositionAfterResize(const int oldScreenWidth, const int oldScreenHeight)
Definition: window.cpp:1254