1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
22 /// @ingroup cppconsui
27 #include "ConsUICurses.h"
28 #include "CppConsUI.h"
29 #include "InputProcessor.h"
36 return error.getCode(); \
45 typedef std::vector
<Widget
*> Widgets
;
47 class Widget
: public sigc::trackable
, public InputProcessor
{
55 virtual ~Widget() override
;
57 /// Moves and resizes the widget.
58 virtual void moveResize(int newx
, int newy
, int neww
, int newh
);
59 virtual void moveResizeRect(const Rect
&rect
)
61 moveResize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
64 /// The draw() method does the actual drawing on a (virtual) area of the
65 /// screen. The @ref CoreManager singleton calls draw() on all on-screen
66 /// Windows. This causes all draw() implementations needed to draw the screen
68 virtual int draw(Curses::ViewPort area
, Error
&error
) = 0;
70 /// Finds the widget that could be the focus widget from the focus chain
71 /// starting with this widget.
72 virtual Widget
*getFocusWidget();
74 /// Deletes the focus (and input) chain starting from this widget.
75 virtual void cleanFocus();
78 virtual bool restoreFocus();
80 /// Takes focus from the widget. Used when this window is no longer a top
82 virtual void ungrabFocus();
84 /// Makes this widget the widget that has the focus. This operation is
85 /// successful if the widget is visible and all predecessors are visible too.
86 virtual bool grabFocus();
88 virtual bool canFocus() const { return can_focus_
; }
89 virtual bool hasFocus() const { return has_focus_
; }
91 virtual void setVisibility(bool new_visible
);
92 virtual bool isVisible() const { return visible_
; }
93 virtual bool isVisibleRecursive() const;
95 virtual void setParent(Container
&new_parent
);
96 virtual Container
*getParent() const { return parent_
; }
98 virtual int getLeft() const { return xpos_
; }
99 virtual int getTop() const { return ypos_
; }
100 virtual int getWidth() const { return width_
; }
101 virtual int getHeight() const { return height_
; }
103 /// Convenient moveResize() wrapper.
104 virtual void move(int newx
, int newy
);
106 /// Convenient moveResize() wrapper.
107 virtual void resize(int neww
, int newh
);
109 /// Convenient moveResize() wrapper.
110 virtual void setLeft(int newx
);
112 /// Convenient moveResize() wrapper.
113 virtual void setTop(int newy
);
115 /// Convenient moveResize() wrapper.
116 virtual void setWidth(int neww
);
118 /// Convenient moveResize() wrapper.
119 virtual void setHeight(int newh
);
121 /// Returns an absolute position of the widget.
122 virtual Point
getAbsolutePosition() const;
124 /// Returns a relative position of the widget to a given predecessor.
125 virtual Point
getRelativePosition(const Container
&ref
) const;
127 /// Returns an area width that is requested by the widget. This method can be
128 /// used by a parent widget if width is set to AUTOSIZE. The AUTOSIZE returned
129 /// value means "as much as possible".
130 virtual int getWishWidth() const { return wish_width_
; }
132 /// Returns an area height that is requested by the widget. This method can be
133 /// used by a parent widget in if height is set to AUTOSIZE. The AUTOSIZE
134 /// returned value means "as much as possible".
135 virtual int getWishHeight() const { return wish_height_
; }
137 virtual void setRealPosition(int newx
, int newy
);
138 virtual int getRealLeft() const { return real_xpos_
; }
139 virtual int getRealTop() const { return real_ypos_
; }
140 virtual void setRealSize(int neww
, int newh
);
141 virtual int getRealWidth() const { return real_width_
; }
142 virtual int getRealHeight() const { return real_height_
; }
144 virtual void setColorScheme(int new_color_scheme
);
145 virtual int getColorScheme() const;
147 virtual void registerAbsolutePositionListener(Widget
&widget
);
148 virtual void unregisterAbsolutePositionListener(Widget
&widget
);
149 virtual void onAbsolutePositionChange(Widget
&widget
);
151 /// Signal emitted when the widget grabs or looses the focus.
152 sigc::signal
<void, Widget
&, bool> signal_focus
;
154 /// Signal emitted when the visibility property of the widget is changed.
155 sigc::signal
<void, Widget
&, bool> signal_visible
;
158 /// Screen area relative to parent area. Note that this is a requested area if
159 /// parent window is big enough, real width/height can differ.
160 int xpos_
, ypos_
, width_
, height_
;
162 /// Preferable size calculated by the widget.
163 int wish_width_
, wish_height_
;
165 /// Real position and size set by the parent container.
166 int real_xpos_
, real_ypos_
, real_width_
, real_height_
;
168 /// Flag indicating if the widget can grab the focus.
171 // Flag indicating if the widget has the focus. Only one widget can have the
172 // focus in the application.
181 /// Current color scheme ID.
184 /// Vector of widgets that need to be informed when the absolute on-screen
185 /// position of this widget has changed. This is used by widgets that need to
186 /// position itself relative to another widget, for example, MenuWindow
187 /// relative to the originating ComboBox.
188 Widgets absolute_position_listeners_
;
190 virtual void signalMoveResize(const Rect
&oldsize
, const Rect
&newsize
);
191 virtual void signalWishSizeChange(const Size
&oldsize
, const Size
&newsize
);
192 virtual void signalVisible(bool visible
);
193 virtual void signalAbsolutePositionChange();
195 /// Recalculates the widget area. It is called whenever coordinates of the
197 virtual void updateArea();
199 /// Peforms operations needed after setRealSize() is processed.
200 virtual void updateAreaPostRealSizeChange(
201 const Size
&oldsize
, const Size
&newsize
);
203 /// Informs @ref CoreManager that the widget has been updated and the screen
204 /// should be redrawn.
205 virtual void redraw();
207 virtual void setWishSize(int neww
, int newh
);
208 virtual void setWishWidth(int neww
) { setWishSize(neww
, wish_height_
); }
209 virtual void setWishHeight(int newh
) { setWishSize(wish_width_
, newh
); }
211 /// Convenient method that calls getAttributes(property, 0, attrs, error).
212 virtual int getAttributes(int property
, int *attrs
, Error
&error
) const;
213 /// Convenient method that calls COLORSCHEME->getAttributes(getColorScheme(),
214 /// property, subproperty, attrs, error).
215 virtual int getAttributes(
216 int property
, int subproperty
, int *attrs
, Error
&error
) const;
218 /// Returns the top container.
219 virtual Container
*getTopContainer();
222 CONSUI_DISABLE_COPY(Widget
);
225 } // namespace CppConsUI
229 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: