summaryrefslogtreecommitdiff
path: root/src/window.cpp
blob: bfa7701e15f29166837e7fcce0423d494238ad78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
                                 window.cpp
                             -------------------
    begin                : Thu Nov 25 2003
    copyright            : (C) 2003 - 2012 by Roland Riegel
    email                : feedback@roland-riegel.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "window.h"

using namespace std;

Window::WindowStreamBuf::WindowStreamBuf(Window& window)
    : m_window(window)
{
}

Window::WindowStreamBuf::~WindowStreamBuf()
{
}

streamsize Window::WindowStreamBuf::xsputn(const char_type* str, streamsize n)
{
    m_window.print(string(str, n));
    return n;
}

Window::WindowStreamBuf::int_type Window::WindowStreamBuf::overflow(int_type c)
{
    if(c == traits_type::eof())
        return c;

    m_window.print((char) c);
    return c;
}

Window::WindowStream::WindowStream(Window& window)
    : basic_ostream<char>(new WindowStreamBuf(window))
{
}

Window::WindowStream::~WindowStream()
{
    delete rdbuf();
}

Window::Window()
 : m_visible(false), m_window(0), m_stream(*this)
{
}

Window::~Window()
{
    hide();
}

// create window and display it
void Window::show(int x, int y, int width, int height)
{
    if(m_window)
        return;
    
    m_window = newwin(height, width, y, x);
    clear();
    refresh();
    
    m_visible = true;
}

// hide window and destroy it
void Window::hide()
{
    if(!m_window)
        return;
    
    clear();
    refresh();
    delwin(m_window);
    
    m_window = 0;
    
    m_visible = false;
}

// is the window currently visible?
bool Window::isVisible()
{
    return m_visible;
}

// refresh window
void Window::refresh()
{
    if(m_window)
        wrefresh(m_window);
}

// clear the content of the window
void Window::clear()
{
    if(m_window)
        wclear(m_window);
}

// move and resize window
void Window::resize(int x, int y, int width, int height)
{
    if(!m_window)
        return;

    wresize(m_window, height, width);
    mvwin(m_window, y, x);
}

// return current window width
int Window::getWidth()
{
    if(!m_window)
        return 0;

    int width, height;
    getmaxyx(m_window, height, width);

    return width;
}

// return current window height
int Window::getHeight()
{
    if(!m_window)
        return 0;

    int width, height;
    getmaxyx(m_window, height, width);

    return height;
}

// return current distance to left screen edge
int Window::getLeft()
{
    if(!m_window)
        return 0;

    int x, y;
    getbegyx(m_window, y, x);

    return x;
}

// return current distance to top screen edge
int Window::getTop()
{
    if(!m_window)
        return 0;

    int x, y;
    getbegyx(m_window, y, x);

    return y;
}

// return current cursor position on the x-axis
int Window::getX()
{
    if(!m_window)
        return 0;

    int x, y;
    getyx(m_window, y, x);

    return x;
}

// return current cursor position on the y-axis
int Window::getY()
{
    if(!m_window)
        return 0;

    int x, y;
    getyx(m_window, y, x);

    return y;
}

// set current cursor x position
void Window::setX(int new_x)
{
    if(!m_window)
        return;

    wmove(m_window, getY(), new_x);
}

// set current cursor y position
void Window::setY(int new_y)
{
    if(!m_window)
        return;

    wmove(m_window, new_y, getX());
}

// set current cursor position
void Window::setXY(int new_x, int new_y)
{
    if(!m_window)
        return;

    wmove(m_window, new_y, new_x);
}

// print some text to the window
void Window::print(const string& text, int new_x, int new_y)
{
    if(!m_window)
        return;
    if(new_x <= -1)
        new_x = getX();
    if(new_y <= -1)
        new_y = getY();
    
    mvwaddstr(m_window, new_y, new_x, text.c_str());
}

// print a char to the window
void Window::print(char text, int new_x, int new_y)
{
    if(!m_window)
        return;
    if(new_x <= -1)
        new_x = getX();
    if(new_y <= -1)
        new_y = getY();
    
    mvwaddch(m_window, new_y, new_x, text);
}

// print via stream to window
Window::WindowStream& Window::print(int x, int y)
{
    if(x <= -1)
        x = getX();
    if(y <= -1)
        y = getY();
    setXY(x, y);

    return m_stream;
}

SubWindow::SubWindow(Window* parent)
 : Window(), m_parent(parent)
{
}

SubWindow::~SubWindow()
{
    hide();
}

// return parent window
Window* SubWindow::getParent()
{
    return m_parent;
}

// create window and display it
void SubWindow::show(int x, int y, int width, int height)
{
    if(m_window)
        return;
    
    m_window = derwin(m_parent->m_window, height, width, y, x);
    clear();
    refresh();
    
    m_visible = true;
}