34#include <guichan/exception.hpp>
35#include <guichan/focushandler.hpp>
53 , mMaxWinHeight(
graphics->getHeight())
55 Log::debug(
"Window::Window(\"%s\")", caption.c_str());
58 throw GCN_EXCEPTION(
"Window::Window(): no windowContainer set");
63 setFrameSize(skin.frameSize);
64 setPadding(skin.padding);
65 setTitleBarHeight(skin.titleBarHeight);
79 addWidgetListener(
this);
84 Log::debug(
"Window::~Window(\"%s\")", getCaption().c_str());
90 while (!mWidgets.empty())
91 delete mWidgets.front();
93 removeWidgetListener(
this);
105 if (getFrameSize() == 0)
130 widgetState.
width += getFrameSize() * 2;
131 widgetState.
height += getFrameSize() * 2;
134 skin.draw(g, widgetState);
138 if (
auto skinState = skin.getState(widgetState.
flags))
140 auto &textFormat = skinState->textFormat;
141 g->drawText(getCaption(),
142 getFrameSize() + skin.titleOffsetX,
143 getFrameSize() + skin.titleOffsetY,
145 textFormat.bold ?
boldFont : getFont(),
153 width += 2 * getPadding();
154 height += getPadding() + getTitleBarHeight();
165 setSize(width, height);
170 const int padding = getPadding();
171 const int titleBarHeight = getTitleBarHeight();
174 setMinWidth(std::max(skin.getMinWidth(), width + 2 * padding));
175 setMinHeight(std::max(skin.getMinHeight(), height + padding + titleBarHeight));
182 widget->getAbsolutePosition(wx, wy);
186 getAbsolutePosition(x, y);
188 setPosition(getX() + (wx + (widget->getWidth() - getWidth()) / 2 - x),
189 getY() + (wy + (widget->getHeight() - getHeight()) / 2 - y));
214 if ((
bool)
mGrip == r)
220 mGrip->setX(getWidth() -
mGrip->getWidth() - getChildrenArea().x);
221 mGrip->setY(getHeight() -
mGrip->getHeight() - getChildrenArea().y);
234 const gcn::Rectangle area = getChildrenArea();
237 mGrip->setPosition(getWidth() -
mGrip->getWidth() - area.x,
238 getHeight() -
mGrip->getHeight() - area.y);
253 WidgetListIterator it;
255 for (it = mWidgets.begin(); it != mWidgets.end(); it++)
257 if (mFocusHandler->isFocused(*it))
258 mFocusHandler->focusNone();
289 if (visible == isVisible())
296 gcn::Window::setVisible((!forceSticky &&
isSticky()) || visible);
307 gcn::Window::mousePressed(event);
309 if (event.getButton() == gcn::MouseEvent::LEFT)
311 const int x =
event.getX();
312 const int y =
event.getY();
341 gcn::Window::mouseReleased(event);
362 if (event.isConsumed())
400 gcn::Window::mouseDragged(event);
403 if (isMovable() && mMoved)
408 const int dx =
event.getX() - mDragOffsetX;
409 const int dy =
event.getY() - mDragOffsetY;
410 gcn::Rectangle newDim = getDimension();
419 newDim.y -= newDim.height - getHeight();
429 newDim.x -= newDim.width - getWidth();
435 newDim.width += newDim.x;
440 newDim.height += newDim.y;
455 mDragOffsetY += newDim.height - getHeight();
459 mDragOffsetX += newDim.width - getWidth();
463 setDimension(newDim);
520 state.
width = getWidth();
521 state.
height = getHeight();
528 int defaultWidth,
int defaultHeight)
555 int offsetX,
int offsetY)
609 gcn::Rectangle dimension = getDimension();
612 const int rightMargin = oldScreenWidth - (getX() + getWidth());
613 const int bottomMargin = oldScreenHeight - (getY() + getHeight());
614 if (getX() > 0 && getX() > rightMargin)
616 if (getY() > 0 && getY() > bottomMargin)
619 setDimension(dimension);
625 int resizeHandles = 0;
629 const int x =
event.getX();
630 const int y =
event.getY();
631 const int p = getPadding();
633 const bool inPadding = (x < p || x >= getWidth() - p) ||
634 (y < p || y >= getHeight() - p);
636 if (inPadding && event.getSource() ==
this)
643 const int resizeBorderWidth = std::max(
mGrip->getWidth(), 10);
645 resizeHandles |= (x >= getWidth() - resizeBorderWidth) ?
RIGHT :
646 (x < resizeBorderWidth) ?
LEFT : 0;
647 resizeHandles |= (y >= getHeight() - resizeBorderWidth) ?
BOTTOM :
648 (y < resizeBorderWidth) ?
TOP : 0;
651 if (event.getSource() ==
mGrip)
659 return resizeHandles;
671 const int closeHeight = closeSkin.getMinHeight();
674 getWidth() - closeWidth - closeSkin.padding,
692 const int stickyWidth = stickySkin.getMinWidth();
693 const int stickyHeight = stickySkin.getMinHeight();
695 int stickyX = getWidth() - stickyWidth - stickySkin.padding - stickySkin.spacing;
697 stickyX -= closeWidth + closeSkin.padding;
711 return (
int) (alpha * 255.0f);
761 const gcn::Rectangle area = getChildrenArea();
779 if (getWidth() == 0 && getHeight() == 0)
782 gcn::Rectangle dim = getDimension();
789 dim.x = std::max(0, dim.x);
790 dim.y = std::max(0, dim.y);
792 setPosition(dim.x, dim.y);
This class is a helper for adding widgets to nested tables in a window.
A central point of control for graphics.
int getHeight() const
Returns the logical height of the screen.
int getWidth() const
Returns the logical width of the screen.
Theme * getTheme() const
The global GUI theme.
void setCursorType(Cursor cursor)
Sets which cursor should be used.
This class describes the formatting of a widget in the cell of a layout table.
LayoutCell & place(gcn::Widget *wg, int x, int y, int w=1, int h=1)
This class is an helper for setting the position of widgets.
void reflow(int &nW, int &nH)
Sets the positions of all the widgets.
int getMinWidth() const
Returns the minimum width which can be used with this skin.
const Skin & getSkin(SkinType skinType) const
float getMinimumOpacity() const
Get the minimum opacity allowed to skins.
void drawSkin(Graphics *graphics, SkinType type, const WidgetState &state) const
void hideBeingPopup()
Hides the BeingPopup.
void scheduleDelete(gcn::Widget *widget)
Schedule a widget for deletion.
void mouseReleased(gcn::MouseEvent &event) override
When the mouse button has been let go, this ensures that the mouse custom cursor is restored back to ...
int mDefaultY
Default window Y position.
void draw(gcn::Graphics *graphics) override
Draws the window contents.
const Skin & getSkin() const
Returns the Skin used by this window.
void center()
Positions the window in the center of it's parent.
int mMinWinWidth
Minimum window width.
std::string mWindowName
Name of the window.
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
gcn::Rectangle getStickyButtonRect() const
int mDefaultHeight
Default window height.
void adjustPositionAfterResize(int oldScreenWidth, int oldScreenHeight)
Adjusts the window position after the application window has been resized.
void setMinHeight(int height)
Sets the minimum height of the window.
void setStickyButton(bool flag)
Sets whether or not the window has a sticky button.
virtual void close()
Overrideable functionality for when the window is to close.
void clearLayout()
Clears the window's layout (useful for redesigning the window).
void setContentSize(int width, int height)
Sets the size of this window.
bool mShowTitle
Window has a title bar.
Window * getParentWindow() const
Returns the parent window.
Layout & getLayout()
Gets the layout handler for this window.
~Window() override
Destructor.
void ensureOnScreen()
Ensures the window is on the screen, moving it if necessary.
bool isSticky() const
Returns whether the window is sticky.
void mouseDragged(gcn::MouseEvent &event) override
Implements window resizing and makes sure the window is not dragged/resized outside of the screen.
int mMinWinHeight
Minimum window height.
bool mDefaultVisible
Window's default visibility.
virtual void resetToDefaultSize()
Reset the win pos and size to default.
int mDefaultWidth
Default window width.
bool mSaveVisible
Window will save visibility.
void saveWindowState() const
Saves the window state so that when the window is reloaded, it'll maintain its previous state and loc...
Layout * mLayout
Layout handler.
void setMinimumContentSize(int width, int height)
Sets the minimum size of the window content.
void setSticky(bool sticky)
Sets whether the window is sticky.
static void setWindowContainer(WindowContainer *windowContainer)
Sets the window container to be used by new windows.
SkinType mSkinType
The skin type used when drawing the window.
static int getGuiAlpha()
Gets the alpha value used by the window, in a Guichan usable format.
ContainerPlacer getPlacer(int x, int y)
Returns a proxy for adding widgets in an inner table of the layout.
void setResizable(bool resize)
Sets whether or not the window can be resized.
int mMaxWinHeight
Maximum window height.
bool mSticky
Window resists hiding.
void reflowLayout(int w=0, int h=0)
Computes the position of the widgets according to the current layout.
void drawFrame(gcn::Graphics *graphics) override
Draws the window frame.
void mouseMoved(gcn::MouseEvent &event) override
Implements custom cursor image changing context, based on mouse relative position.
void widgetHidden(const gcn::Event &event) override
Called whenever the widget is hidden.
gcn::Rectangle getCloseButtonRect() const
int getResizeHandles(gcn::MouseEvent &event)
Determines if the mouse is in a resize area and returns appropriate resize handles.
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
void widgetResized(const gcn::Event &event) override
Called whenever the widget changes size.
void mouseExited(gcn::MouseEvent &event) override
When the mouse leaves the window this ensures that the custom cursor is restored back to it's standar...
void scheduleDelete()
Schedule this window for deletion.
bool mCloseButton
Window has a close button.
int mMaxWinWidth
Maximum window width.
Window(const std::string &caption="Window", bool modal=false, Window *parent=nullptr)
Constructor.
static int instances
Number of Window instances.
void setCloseButton(bool flag)
Sets whether or not the window has a close button.
void setLocationRelativeTo(gcn::Widget *widget)
Sets the location relative to the given widget.
void setDefaultSize()
Set the default win pos and size to the current ones.
int mDefaultX
Default window X position.
bool mStickyButton
Window has a sticky button.
bool isResizable() const
Returns whether the window can be resized.
ResizeGrip * mGrip
Resize grip.
void loadWindowState()
Reads the position (and the size for resizable windows) in the configuration based on the given strin...
void mousePressed(gcn::MouseEvent &event) override
Starts window resizing when appropriate.
static int mouseResize
Active resize handles.
void setMaxHeight(int height)
Sets the minimum height of the window.
void setMinWidth(int width)
Sets the minimum width of the window.
void setMaxWidth(int width)
Sets the maximum width of the window.
bool mModal
Window is modal.
Config config
Global settings (config.xml)
Viewport * viewport
Viewport on the map.
gcn::Font * boldFont
Bolded text font.
Cursor
Cursors are in graphic order from left to right.
void debug(const char *log_text,...) LOG_PRINTF_ATTR
std::map< std::string, WindowState > windows
std::optional< int > height
std::optional< bool > visible
std::optional< bool > sticky
std::optional< int > width
WindowContainer * windowContainer