1 // -*- mode: c++ -*-
2 //
3 // This file is part of libyacurs.
4 // Copyright (C) 2013  Rafael Ostertag
5 //
6 // This program is free software: you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation, either version 3 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see
18 // <http://www.gnu.org/licenses/>.
19 //
20 //
21 // $Id$
22 
23 #ifndef WINDOWBASE_H
24 #define WINDOWBASE_H 1
25 
26 #include "curswin.h"
27 #include "event.h"
28 #include "margin.h"
29 #include "realizeable.h"
30 #include "yacurscurses.h"
31 
32 namespace YACURS {
33 /**
34  * Base class for Windows.
35  *
36  * WindowBase has the following properties:
37  *
38  * - curses window uses entire screen, unless a margin is given
39  *
40  * - positioning is achieved by using margins
41  *
42  * - can have a visible frame
43  *
44  * - connect to EVT_REFRESH, EVT_FORCEREFRESH and EVT_RESIZE
45  *
46  * - allocates only one instance of curses Windows across
47  *   assignments.
48  *
49  * - tracks the number of WindowBase instances created by copying and
50  *   assigning and frees curses window only upon destruction of
51  *   last instance.
52  *
53  * Users of this class are expected to call show()/close() in order to
54  * show and hide the window. Calling realize()/unrealize() is
55  * discouraged.
56  *
57  * Events will be connected upon call to show() and disconnected upon
58  * call to close().
59  *
60  * Derived classes do not need to connect to EVT_REFRESH, or
61  * EVT_RESIZE. The Event handler of WindowBase call refresh() or
62  * resize() which can be implemented by derived classes.
63  */
64 class WindowBase : public Realizeable {
65    private:
66     /**
67      * The dimension of the screen. Used as cache to avoid calls
68      * to Curses::inquiry_screensize().
69      */
70     Area _area;
71 
72     /**
73      * margin of the screen object.
74      */
75     Margin _margin;
76 
77     /**
78      * Curses window
79      */
80     YACURS::INTERNAL::CursWin* _curses_window;
81 
82     /**
83      * Whether or not the window has a border
84      */
85     bool _frame;
86 
87     /**
88      * Flag whether or not Window is shown.
89      */
90     bool _shown;
91 
92     /**
93      * Default color of window.
94      */
95     COLOROBJ _color;
96 
97    protected:
98     YACURS::INTERNAL::CursWin* curses_window() const;
99 
100     const Area& area() const;
101 
102     /**
103      * Gets the area that can be used by widgets
104      *
105      * @return the area in with absolute coordinates that can be
106      * used by widgets.
107      */
108     Area widget_area() const;
109 
110     void unrealize();
111 
112     /**
113      * Called on close.
114      *
115      * Will be called before dialog closes.
116      * To be implemented by user if needed.
117      *
118      * @param st state. See STATE for possible values.
119      *
120      * @return @c false prevents window from being closed.
121      */
122     virtual bool on_close();
123 
124    public:
125     /**
126      * @param m margin to be used.
127      */
128     WindowBase(const Margin& m = Margin());
129     WindowBase(const WindowBase&) = delete;
130     WindowBase& operator=(const WindowBase&) = delete;
131     WindowBase(WindowBase&&) = delete;
132     WindowBase& operator=(WindowBase&&) = delete;
133     virtual ~WindowBase();
134 
135     /**
136      * @todo when setting margin and window is realized, resize
137      * it.
138      */
139     void margin(const Margin& m);
140 
141     const Margin& margin() const;
142 
143     bool frame() const;
144 
145     void color(COLOROBJ c);
146 
147     COLOROBJ color() const;
148 
149     /**
150      * @todo make setting/removing frame take effect immediately,
151      * if window is realized.
152      */
153     void frame(bool b);
154 
155     /**
156      * Show the window.
157      *
158      * This is supposed to be called by the user.
159      */
160     virtual void show();
161 
162     /**
163      * Hide the window.
164      *
165      * This is supposed to be called by the user.
166      */
167     virtual void close();
168 
169     bool shown() const;
170 
171     // Those handlers must be public, so that LockScreen can
172     // suspend them.
173 
174     virtual void redraw_handler(Event& e);
175 
176     virtual void force_refresh_handler(Event& e);
177 
178     virtual void refresh_handler(Event& e);
179 
180     virtual void resize_handler(Event& e);
181 
182     // Those are from Realizable
183     void refresh(bool immediate);
184 
185     void resize(const Area& a);
186 
187     void realize();
188 };
189 }  // namespace YACURS
190 
191 #endif  // WINDOWBASE_H
192