window: use readline for handling line input
[ncmpcpp.git] / src / window.h
blob2fa62d772adb0fc6515a56e7085b6dfccd2cebf7
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 char *)> 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) { }
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 width = 0, bool encrypted = 0);
234 /// Wrapper for above function that doesn't take base string (it will be empty).
235 /// Taken parameters are the same as for above.
236 std::string getString(size_t width = 0, bool encrypted = 0)
238 return getString("", width, encrypted);
241 /// Moves cursor to given coordinates
242 /// @param x given X position
243 /// @param y given Y position
244 void goToXY(int x, int y);
246 /// @return x window coordinate
247 int getX();
249 /// @return y windows coordinate
250 int getY();
252 /// Used to indicate whether given coordinates of main screen lies within
253 /// window area or not and if they do, transform them into in-window coords.
254 /// Otherwise function doesn't modify its arguments.
255 /// @param x X position of main screen to be checked
256 /// @param y Y position of main screen to be checked
257 /// @return true if it transformed variables, false otherwise
258 bool hasCoords(int &x, int &y);
260 /// Sets helper function used in getString()
261 /// @param helper pointer to function that matches getStringHelper prototype
262 /// @see getString()
263 void setGetStringHelper(GetStringHelper helper) { m_get_string_helper = helper; }
265 /// Run current GetString helper function (if defined).
266 /// @see getString()
267 /// @return true if helper was run, false otherwise
268 bool runGetStringHelper(const char *arg) const;
270 /// Sets window's base color
271 /// @param fg foregound base color
272 /// @param bg background base color
273 void setBaseColor(Color fg, Color bg = Color::Default);
275 /// Sets window's border
276 /// @param border new window's border
277 void setBorder(Border border);
279 /// Sets window's timeout
280 /// @param timeout window's timeout
281 void setTimeout(int timeout);
283 /// Sets window's title
284 /// @param new_title new title for window
285 void setTitle(const std::string &new_title);
287 /// Refreshed whole window and its border
288 /// @see refresh()
289 void display();
291 /// Refreshes whole window, but not the border
292 /// @see display()
293 virtual void refresh();
295 /// Moves the window to new coordinates
296 /// @param new_x new X position of left upper corner of window
297 /// @param new_y new Y position of left upper corner of window
298 virtual void moveTo(size_t new_x, size_t new_y);
300 /// Resizes the window
301 /// @param new_width new window's width
302 /// @param new_height new window's height
303 virtual void resize(size_t new_width, size_t new_height);
305 /// Cleares the window
306 virtual void clear();
308 /// Adds given file descriptor to the list that will be polled in
309 /// ReadKey() along with stdin and callback that will be invoked
310 /// when there is data waiting for reading in it
311 /// @param fd file descriptor
312 /// @param callback callback
313 void addFDCallback(int fd, void (*callback)());
315 /// Clears list of file descriptors and their callbacks
316 void clearFDCallbacksList();
318 /// Checks if list of file descriptors is empty
319 /// @return true if list is empty, false otherwise
320 bool FDCallbacksListEmpty() const;
322 /// Reads key from standard input (or takes it from input queue)
323 /// and writes it into read_key variable
324 int readKey();
326 /// Push single character into input queue, so it can get consumed by ReadKey
327 void pushChar(int ch);
329 /// @return const reference to internal input queue
330 const std::queue<int> &inputQueue() { return m_input_queue; }
332 /// Scrolls the window by amount of lines given in its parameter
333 /// @param where indicates how many lines it has to scroll
334 virtual void scroll(Scroll where);
336 /// Applies function of compatible prototype to internal WINDOW pointer
337 /// The mostly used function in this case seem to be wclrtoeol(), which
338 /// clears the window from current cursor position to the end of line.
339 /// Note that delwin() also matches that prototype, but I wouldn't
340 /// recommend anyone passing this pointer here ;)
341 /// @param f pointer to function to call with internal WINDOW pointer
342 /// @return reference to itself
343 Window &operator<<(int (*f)(WINDOW *));
345 /// Applies foreground and background colors to window
346 /// @param colors struct that holds new colors information
347 /// @return reference to itself
348 Window &operator<<(Colors colors);
350 /// Applies foregound color to window. Note that colors applied
351 /// that way are stacked, i.e if you applied Color::Red, then Color::Green
352 /// and Color::End, current color would be Color::Red. If you want to discard
353 /// all colors and fall back to base one, pass Color::Default.
354 /// @param color new color value
355 /// @return reference to itself
356 Window &operator<<(Color color);
358 /// Applies format flag to window. Note that these attributes are
359 /// also stacked, so if you applied Format::Bold twice, to get rid of
360 /// it you have to pass Format::NoBold also twice.
361 /// @param format format flag
362 /// @return reference to itself
363 Window &operator<<(Format format);
365 /// Moves current cursor position to given coordinates.
366 /// @param coords struct that holds information about new coordinations
367 /// @return reference to itself
368 Window &operator<<(XY coords);
370 /// Prints string to window
371 /// @param s const pointer to char array to be printed
372 /// @return reference to itself
373 Window &operator<<(const char *s);
375 /// Prints single character to window
376 /// @param c character to be printed
377 /// @return reference to itself
378 Window &operator<<(char c);
380 /// Prints wide string to window
381 /// @param ws const pointer to wchar_t array to be printed
382 /// @return reference to itself
383 Window &operator<<(const wchar_t *ws);
385 /// Prints single wide character to window
386 /// @param wc wide character to be printed
387 /// @return reference to itself
388 Window &operator<<(wchar_t wc);
390 /// Prints int to window
391 /// @param i integer value to be printed
392 /// @return reference to itself
393 Window &operator<<(int i);
395 /// Prints double to window
396 /// @param d double value to be printed
397 /// @return reference to itself
398 Window &operator<<(double d);
400 /// Prints size_t to window
401 /// @param s size value to be printed
402 /// @return reference to itself
403 Window &operator<<(size_t s);
405 /// Prints std::string to window
406 /// @param s string to be printed
407 /// @return reference to itself
408 Window &operator<<(const std::string &s);
410 /// Prints std::wstring to window
411 /// @param ws wide string to be printed
412 /// @return reference to itself
413 Window &operator<<(const std::wstring &ws);
414 protected:
415 /// Sets colors of window (interal use only)
416 /// @param fg foregound color
417 /// @param bg background color
419 void setColor(Color fg, Color bg = Color::Default);
421 /// Refreshes window's border
423 void showBorder() const;
425 /// Changes dimensions of window, called from resize()
426 /// @param width new window's width
427 /// @param height new window's height
428 /// @see resize()
430 void adjustDimensions(size_t width, size_t height);
432 /// Deletes old window and creates new. It's called by resize(),
433 /// SetBorder() or setTitle() since internally windows are
434 /// handled as curses pads and change in size requires to delete
435 /// them and create again, there is no way to change size of pad.
436 /// @see SetBorder()
437 /// @see setTitle()
438 /// @see resize()
440 virtual void recreate(size_t width, size_t height);
442 /// internal WINDOW pointers
443 WINDOW *m_window;
444 WINDOW *m_border_window;
446 /// start points and dimensions
447 size_t m_start_x;
448 size_t m_start_y;
449 size_t m_width;
450 size_t m_height;
452 /// window timeout
453 int m_window_timeout;
455 /// current colors
456 Color m_color;
457 Color m_bg_color;
459 /// base colors
460 Color m_base_color;
461 Color m_base_bg_color;
463 /// current border
464 Border m_border;
466 private:
467 /// Sets state of bold attribute (internal use only)
468 /// @param bold_state state of bold attribute
470 void bold(bool bold_state) const;
472 /// Sets state of underline attribute (internal use only)
473 /// @param underline_state state of underline attribute
475 void underline(bool underline_state) const;
477 /// Sets state of reverse attribute (internal use only)
478 /// @param reverse_state state of reverse attribute
480 void reverse(bool reverse_state) const;
482 /// Sets state of altcharset attribute (internal use only)
483 /// @param altcharset_state state of altcharset attribute
485 void altCharset(bool altcharset_state) const;
487 /// pointer to helper function used by getString()
488 /// @see getString()
490 GetStringHelper m_get_string_helper;
492 /// window title
493 std::string m_title;
495 /// stack of colors
496 std::stack<Colors> m_color_stack;
498 /// input queue of a window. you can put characters there using
499 /// PushChar and they will be immediately consumed and
500 /// returned by ReadKey
501 std::queue<int> m_input_queue;
503 /// containter used for additional file descriptors that have
504 /// to be polled in ReadKey() and correspondent callbacks that
505 /// are invoked if there is data available in them
506 typedef std::vector< std::pair<int, void (*)()> > FDCallbacks;
507 FDCallbacks m_fds;
509 /// counters for format flags
510 int m_bold_counter;
511 int m_underline_counter;
512 int m_reverse_counter;
513 int m_alt_charset_counter;
518 #endif // NCMPCPP_WINDOW_H