update copyright info
[ncmpcpp.git] / src / window.h
blob2b99349af246357bd9673ed4a636bba927d4a374
1 /***************************************************************************
2 * Copyright (C) 2008-2013 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_WINDOW_H
22 #define NCMPCPP_WINDOW_H
24 #include "config.h"
26 #ifdef USE_PDCURSES
27 # define NCURSES_MOUSE_VERSION 1
28 #endif
30 #include "curses.h"
31 #include "gcc.h"
33 #include <functional>
34 #include <list>
35 #include <stack>
36 #include <vector>
37 #include <string>
38 #include <queue>
40 // define some Ctrl-? keys
41 #define KEY_CTRL_A 1
42 #define KEY_CTRL_B 2
43 #define KEY_CTRL_C 3
44 #define KEY_CTRL_D 4
45 #define KEY_CTRL_E 5
46 #define KEY_CTRL_F 6
47 #define KEY_CTRL_G 7
48 #define KEY_CTRL_H 8
49 #define KEY_CTRL_I 9
50 #define KEY_CTRL_J 10
51 #define KEY_CTRL_K 11
52 #define KEY_CTRL_L 12
53 #define KEY_CTRL_M 13
54 #define KEY_CTRL_N 14
55 #define KEY_CTRL_O 15
56 #define KEY_CTRL_P 16
57 #define KEY_CTRL_Q 17
58 #define KEY_CTRL_R 18
59 #define KEY_CTRL_S 19
60 #define KEY_CTRL_T 20
61 #define KEY_CTRL_U 21
62 #define KEY_CTRL_V 22
63 #define KEY_CTRL_W 23
64 #define KEY_CTRL_X 24
65 #define KEY_CTRL_Y 25
66 #define KEY_CTRL_Z 26
68 // define F? keys
69 #define KEY_F1 265
70 #define KEY_F2 266
71 #define KEY_F3 267
72 #define KEY_F4 268
73 #define KEY_F5 269
74 #define KEY_F6 270
75 #define KEY_F7 271
76 #define KEY_F8 272
77 #define KEY_F9 273
78 #define KEY_F10 274
79 #define KEY_F11 275
80 #define KEY_F12 276
82 // other handy keys
83 #define KEY_SHIFT_TAB 353
84 #define KEY_SPACE 32
85 #define KEY_TAB 9
87 // define alternative KEY_BACKSPACE (used in some terminal emulators)
88 #define KEY_BACKSPACE_2 127
90 // KEY_ENTER is 343, which doesn't make any sense. This makes it useful.
91 #undef KEY_ENTER
92 #define KEY_ENTER 10
94 // undefine scroll macro as it collides with Window::scroll
95 #undef scroll
97 #ifndef USE_PDCURSES
98 // NOTICE: redefine BUTTON2_PRESSED as it doesn't always work, I noticed
99 // that it sometimes returns 134217728 (2^27) instead of expected mask, so the
100 // modified define does it right but is rather experimental.
101 # undef BUTTON2_PRESSED
102 # define BUTTON2_PRESSED (NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_PRESSED) | (1U << 27))
103 #endif // USE_PDCURSES
105 // workaraund for win32
106 #ifdef WIN32
107 # define wcwidth(x) int(!iscntrl(x))
108 #endif
110 /// NC namespace provides set of easy-to-use
111 /// wrappers over original curses library
112 namespace NC {//
114 /// Colors used by NCurses
115 enum class Color { Default, Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, End };
117 /// Format flags used by NCurses
118 enum class Format {
119 None,
120 Bold, NoBold,
121 Underline, NoUnderline,
122 Reverse, NoReverse,
123 AltCharset, NoAltCharset
126 /// Available border colors for window
127 enum class Border { None, Black, Red, Green, Yellow, Blue, Magenta, Cyan, White };
129 /// This indicates how much the window has to be scrolled
130 enum class Scroll { Up, Down, PageUp, PageDown, Home, End };
132 /// Helper function that is invoked each time one will want
133 /// to obtain string from Window::getString() function
134 /// @see Window::getString()
135 typedef std::function<bool(const std::wstring &)> GetStringHelper;
137 /// Initializes curses screen and sets some additional attributes
138 /// @param window_title title of the window (has an effect only if pdcurses lib is used)
139 /// @param enable_colors enables colors
140 void initScreen(const char *window_title, bool enable_colors);
142 /// Destroys the screen
143 void destroyScreen();
145 /// Struct used to set color of both foreground and background of window
146 /// @see Window::operator<<()
147 struct Colors
149 Colors(Color one, Color two = Color::Default) : fg(one), bg(two) { }
150 Color fg;
151 Color bg;
154 /// Struct used for going to given coordinates
155 /// @see Window::operator<<()
156 struct XY
158 XY(int xx, int yy) : x(xx), y(yy) { }
159 int x;
160 int y;
163 /// Main class of NCurses namespace, used as base for other specialized windows
164 struct Window
166 Window() : m_window(0), m_border_window(0), m_history(0) { }
168 /// Constructs an empty window with given parameters
169 /// @param startx X position of left upper corner of constructed window
170 /// @param starty Y position of left upper corner of constructed window
171 /// @param width width of constructed window
172 /// @param height height of constructed window
173 /// @param title title of constructed window
174 /// @param color base color of constructed window
175 /// @param border border of constructed window
176 Window(size_t startx, size_t starty, size_t width, size_t height,
177 const std::string &title, Color color, Border border);
179 Window(const Window &rhs);
180 Window(Window &&rhs);
181 Window &operator=(Window w);
183 virtual ~Window();
185 /// Allows for direct access to internal WINDOW pointer in case there
186 /// is no wrapper for a function from curses library one may want to use
187 /// @return internal WINDOW pointer
188 WINDOW *raw() const { return m_window; }
190 /// @return window's width
191 size_t getWidth() const;
193 /// @return window's height
194 size_t getHeight() const;
196 /// @return X position of left upper window's corner
197 size_t getStartX() const;
199 /// @return Y position of left upper window's corner
200 size_t getStarty() const;
202 /// @return window's title
203 const std::string &getTitle() const;
205 /// @return current window's color
206 Color getColor() const;
208 /// @return current window's border
209 Border getBorder() const;
211 /// @return current window's timeout
212 int getTimeout() const;
214 /// Reads the string from standard input. Note that this is much more complex
215 /// function than getstr() from curses library. It allows for moving through
216 /// letters with arrows, supports scrolling if string's length is bigger than
217 /// given area, supports history of previous strings and each time it receives
218 /// an input from the keyboard or the timeout is reached, it calls helper function
219 /// (if it's set) that takes as an argument currently edited string.
220 /// @param base base string that has to be edited
221 /// @param length max length of string, unlimited by default
222 /// @param width width of area that entry field can take. if it's reached, string
223 /// will be scrolled. if value is 0, field will be from cursor position to the end
224 /// of current line wide.
225 /// @param encrypted if set to true, '*' characters will be displayed instead of
226 /// actual text.
227 /// @return edited string
229 /// @see setGetStringHelper()
230 /// @see SetTimeout()
231 /// @see CreateHistory()
232 std::string getString(const std::string &base, size_t length_ = -1,
233 size_t width = 0, bool encrypted = 0);
235 /// Wrapper for above function that doesn't take base string (it will be empty).
236 /// Taken parameters are the same as for above.
237 std::string getString(size_t length_ = -1, size_t width = 0, bool encrypted = 0)
239 return getString("", length_, width, encrypted);
242 /// Moves cursor to given coordinates
243 /// @param x given X position
244 /// @param y given Y position
245 void goToXY(int x, int y);
247 /// @return x window coordinate
248 int getX();
250 /// @return y windows coordinate
251 int getY();
253 /// Used to indicate whether given coordinates of main screen lies within
254 /// window area or not and if they do, transform them into in-window coords.
255 /// Otherwise function doesn't modify its arguments.
256 /// @param x X position of main screen to be checked
257 /// @param y Y position of main screen to be checked
258 /// @return true if it transformed variables, false otherwise
259 bool hasCoords(int &x, int &y);
261 /// Sets helper function used in getString()
262 /// @param helper pointer to function that matches getStringHelper prototype
263 /// @see getString()
264 void setGetStringHelper(GetStringHelper helper) { m_get_string_helper = helper; }
266 /// Sets window's base color
267 /// @param fg foregound base color
268 /// @param bg background base color
269 void setBaseColor(Color fg, Color bg = Color::Default);
271 /// Sets window's border
272 /// @param border new window's border
273 void setBorder(Border border);
275 /// Sets window's timeout
276 /// @param timeout window's timeout
277 void setTimeout(int timeout);
279 /// Sets window's title
280 /// @param new_title new title for window
281 void setTitle(const std::string &new_title);
283 /// Creates internal container that stores all previous
284 /// strings that were edited using this window.
285 void createHistory();
287 /// Deletes container with all previous history entries
288 void deleteHistory();
290 /// Refreshed whole window and its border
291 /// @see refresh()
292 void display();
294 /// Refreshes whole window, but not the border
295 /// @see display()
296 virtual void refresh();
298 /// Moves the window to new coordinates
299 /// @param new_x new X position of left upper corner of window
300 /// @param new_y new Y position of left upper corner of window
301 virtual void moveTo(size_t new_x, size_t new_y);
303 /// Resizes the window
304 /// @param new_width new window's width
305 /// @param new_height new window's height
306 virtual void resize(size_t new_width, size_t new_height);
308 /// Cleares the window
309 virtual void clear();
311 /// Adds given file descriptor to the list that will be polled in
312 /// ReadKey() along with stdin and callback that will be invoked
313 /// when there is data waiting for reading in it
314 /// @param fd file descriptor
315 /// @param callback callback
316 void addFDCallback(int fd, void (*callback)());
318 /// Clears list of file descriptors and their callbacks
319 void clearFDCallbacksList();
321 /// Checks if list of file descriptors is empty
322 /// @return true if list is empty, false otherwise
323 bool FDCallbacksListEmpty() const;
325 /// Reads key from standard input (or takes it from input queue)
326 /// and writes it into read_key variable
327 int readKey();
329 /// Push single character into input queue, so it can get consumed by ReadKey
330 void pushChar(int ch);
332 /// @return const reference to internal input queue
333 const std::queue<int> &inputQueue() { return m_input_queue; }
335 /// Scrolls the window by amount of lines given in its parameter
336 /// @param where indicates how many lines it has to scroll
337 virtual void scroll(Scroll where);
339 /// Applies function of compatible prototype to internal WINDOW pointer
340 /// The mostly used function in this case seem to be wclrtoeol(), which
341 /// clears the window from current cursor position to the end of line.
342 /// Note that delwin() also matches that prototype, but I wouldn't
343 /// recommend anyone passing this pointer here ;)
344 /// @param f pointer to function to call with internal WINDOW pointer
345 /// @return reference to itself
346 Window &operator<<(int (*f)(WINDOW *));
348 /// Applies foreground and background colors to window
349 /// @param colors struct that holds new colors information
350 /// @return reference to itself
351 Window &operator<<(Colors colors);
353 /// Applies foregound color to window. Note that colors applied
354 /// that way are stacked, i.e if you applied Color::Red, then Color::Green
355 /// and Color::End, current color would be Color::Red. If you want to discard
356 /// all colors and fall back to base one, pass Color::Default.
357 /// @param color new color value
358 /// @return reference to itself
359 Window &operator<<(Color color);
361 /// Applies format flag to window. Note that these attributes are
362 /// also stacked, so if you applied Format::Bold twice, to get rid of
363 /// it you have to pass Format::NoBold also twice.
364 /// @param format format flag
365 /// @return reference to itself
366 Window &operator<<(Format format);
368 /// Moves current cursor position to given coordinates.
369 /// @param coords struct that holds information about new coordinations
370 /// @return reference to itself
371 Window &operator<<(XY coords);
373 /// Prints string to window
374 /// @param s const pointer to char array to be printed
375 /// @return reference to itself
376 Window &operator<<(const char *s);
378 /// Prints single character to window
379 /// @param c character to be printed
380 /// @return reference to itself
381 Window &operator<<(char c);
383 /// Prints wide string to window
384 /// @param ws const pointer to wchar_t array to be printed
385 /// @return reference to itself
386 Window &operator<<(const wchar_t *ws);
388 /// Prints single wide character to window
389 /// @param wc wide character to be printed
390 /// @return reference to itself
391 Window &operator<<(wchar_t wc);
393 /// Prints int to window
394 /// @param i integer value to be printed
395 /// @return reference to itself
396 Window &operator<<(int i);
398 /// Prints double to window
399 /// @param d double value to be printed
400 /// @return reference to itself
401 Window &operator<<(double d);
403 /// Prints size_t to window
404 /// @param s size value to be printed
405 /// @return reference to itself
406 Window &operator<<(size_t s);
408 /// Prints std::string to window
409 /// @param s string to be printed
410 /// @return reference to itself
411 Window &operator<<(const std::string &s);
413 /// Prints std::wstring to window
414 /// @param ws wide string to be printed
415 /// @return reference to itself
416 Window &operator<<(const std::wstring &ws);
417 protected:
418 /// Sets colors of window (interal use only)
419 /// @param fg foregound color
420 /// @param bg background color
422 void setColor(Color fg, Color bg = Color::Default);
424 /// Refreshes window's border
426 void showBorder() const;
428 /// Changes dimensions of window, called from resize()
429 /// @param width new window's width
430 /// @param height new window's height
431 /// @see resize()
433 void adjustDimensions(size_t width, size_t height);
435 /// Deletes old window and creates new. It's called by resize(),
436 /// SetBorder() or setTitle() since internally windows are
437 /// handled as curses pads and change in size requires to delete
438 /// them and create again, there is no way to change size of pad.
439 /// @see SetBorder()
440 /// @see setTitle()
441 /// @see resize()
443 virtual void recreate(size_t width, size_t height);
445 /// internal WINDOW pointers
446 WINDOW *m_window;
447 WINDOW *m_border_window;
449 /// start points and dimensions
450 size_t m_start_x;
451 size_t m_start_y;
452 size_t m_width;
453 size_t m_height;
455 /// window timeout
456 int m_window_timeout;
458 /// current colors
459 Color m_color;
460 Color m_bg_color;
462 /// base colors
463 Color m_base_color;
464 Color m_base_bg_color;
466 /// current border
467 Border m_border;
469 private:
470 /// Sets state of bold attribute (internal use only)
471 /// @param bold_state state of bold attribute
473 void bold(bool bold_state) const;
475 /// Sets state of underline attribute (internal use only)
476 /// @param underline_state state of underline attribute
478 void underline(bool underline_state) const;
480 /// Sets state of reverse attribute (internal use only)
481 /// @param reverse_state state of reverse attribute
483 void reverse(bool reverse_state) const;
485 /// Sets state of altcharset attribute (internal use only)
486 /// @param altcharset_state state of altcharset attribute
488 void altCharset(bool altcharset_state) const;
490 /// pointer to helper function used by getString()
491 /// @see getString()
493 GetStringHelper m_get_string_helper;
495 /// window title
496 std::string m_title;
498 /// stack of colors
499 std::stack<Colors> m_color_stack;
501 /// input queue of a window. you can put characters there using
502 /// PushChar and they will be immediately consumed and
503 /// returned by ReadKey
504 std::queue<int> m_input_queue;
506 /// containter used for additional file descriptors that have
507 /// to be polled in ReadKey() and correspondent callbacks that
508 /// are invoked if there is data available in them
509 typedef std::vector< std::pair<int, void (*)()> > FDCallbacks;
510 FDCallbacks m_fds;
512 /// pointer to container used as history
513 std::list<std::wstring> *m_history;
515 /// counters for format flags
516 int m_bold_counter;
517 int m_underline_counter;
518 int m_reverse_counter;
519 int m_alt_charset_counter;
524 #endif // NCMPCPP_WINDOW_H