bindinds: add support for alt/ctrl/shift modifiers and escape key
[ncmpcpp.git] / src / window.h
blob1b8862c15ed739ac5219b8966a4a063d06c87b09
1 /***************************************************************************
2 * Copyright (C) 2008-2014 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 #include "curses.h"
27 #include "gcc.h"
29 #include <boost/optional.hpp>
30 #include <functional>
31 #include <list>
32 #include <stack>
33 #include <vector>
34 #include <string>
35 #include <tuple>
36 #include <queue>
38 #if NCURSES_MOUSE_VERSION == 1
39 # define BUTTON5_PRESSED (1U << 27)
40 #endif // NCURSES_MOUSE_VERSION == 1
42 // undefine macros with colliding names
43 #undef border
44 #undef scroll
46 /// NC namespace provides set of easy-to-use
47 /// wrappers over original curses library.
48 namespace NC {
50 namespace Key {
52 typedef uint32_t Type;
54 const Type None = -1;
56 // modifier masks
57 const Type Special = 1 << 31;
58 const Type Alt = 1 << 30;
59 const Type Ctrl = 1 << 29;
60 const Type Shift = 1 << 28;
62 // useful names
63 const Type Null = 0;
64 const Type Space = 32;
65 const Type Backspace = 127;
67 // ctrl-?
68 const Type Ctrl_A = 1;
69 const Type Ctrl_B = 2;
70 const Type Ctrl_C = 3;
71 const Type Ctrl_D = 4;
72 const Type Ctrl_E = 5;
73 const Type Ctrl_F = 6;
74 const Type Ctrl_G = 7;
75 const Type Ctrl_H = 8;
76 const Type Ctrl_I = 9;
77 const Type Ctrl_J = 10;
78 const Type Ctrl_K = 11;
79 const Type Ctrl_L = 12;
80 const Type Ctrl_M = 13;
81 const Type Ctrl_N = 14;
82 const Type Ctrl_O = 15;
83 const Type Ctrl_P = 16;
84 const Type Ctrl_Q = 17;
85 const Type Ctrl_R = 18;
86 const Type Ctrl_S = 19;
87 const Type Ctrl_T = 20;
88 const Type Ctrl_U = 21;
89 const Type Ctrl_V = 22;
90 const Type Ctrl_W = 23;
91 const Type Ctrl_X = 24;
92 const Type Ctrl_Y = 25;
93 const Type Ctrl_Z = 26;
94 const Type Ctrl_LeftBracket = 27;
95 const Type Ctrl_Backslash = 28;
96 const Type Ctrl_RightBracket = 29;
97 const Type Ctrl_Caret = 30;
98 const Type Ctrl_Underscore = 31;
100 // useful duplicates
101 const Type Tab = 9;
102 const Type Enter = 13;
103 const Type Escape = 27;
105 // special values, beyond one byte
106 const Type Insert = Special | 256;
107 const Type Delete = Special | 257;
108 const Type Home = Special | 258;
109 const Type End = Special | 259;
110 const Type PageUp = Special | 260;
111 const Type PageDown = Special | 261;
112 const Type Up = Special | 262;
113 const Type Down = Special | 263;
114 const Type Left = Special | 264;
115 const Type Right = Special | 265;
116 const Type F1 = Special | 266;
117 const Type F2 = Special | 267;
118 const Type F3 = Special | 268;
119 const Type F4 = Special | 269;
120 const Type F5 = Special | 270;
121 const Type F6 = Special | 271;
122 const Type F7 = Special | 272;
123 const Type F8 = Special | 273;
124 const Type F9 = Special | 274;
125 const Type F10 = Special | 275;
126 const Type F11 = Special | 276;
127 const Type F12 = Special | 277;
128 const Type Mouse = Special | 278;
132 /// Thrown if Ctrl-C or Ctrl-G is pressed during the call to Window::getString()
133 /// @see Window::getString()
134 struct PromptAborted : std::exception
136 template <typename ArgT>
137 PromptAborted(ArgT &&prompt)
138 : m_prompt(std::forward<ArgT>(prompt)) { }
140 virtual const char *what() const noexcept OVERRIDE { return m_prompt.c_str(); }
142 private:
143 std::string m_prompt;
146 struct Color
148 friend struct Window;
150 static const short transparent;
151 static const short previous;
153 Color() : m_impl(0, transparent, true, false) { }
154 Color(short foreground_value, short background_value,
155 bool is_default = false, bool is_end = false)
156 : m_impl(foreground_value, background_value, is_default, is_end)
158 if (isDefault() && isEnd())
159 throw std::logic_error("Color flag can't be marked as both 'default' and 'end'");
162 bool operator==(const Color &rhs) const { return m_impl == rhs.m_impl; }
163 bool operator!=(const Color &rhs) const { return m_impl != rhs.m_impl; }
164 bool operator<(const Color &rhs) const { return m_impl < rhs.m_impl; }
166 bool isDefault() const { return std::get<2>(m_impl); }
167 bool isEnd() const { return std::get<3>(m_impl); }
169 int pairNumber() const;
171 static Color Default;
172 static Color Black;
173 static Color Red;
174 static Color Green;
175 static Color Yellow;
176 static Color Blue;
177 static Color Magenta;
178 static Color Cyan;
179 static Color White;
180 static Color End;
182 private:
183 short foreground() const { return std::get<0>(m_impl); }
184 short background() const { return std::get<1>(m_impl); }
185 bool previousBackground() const { return background() == previous; }
187 std::tuple<short, short, bool, bool> m_impl;
190 std::istream &operator>>(std::istream &is, Color &f);
192 typedef boost::optional<Color> Border;
194 /// Terminal manipulation functions
195 enum class TermManip { ClearToEOL };
197 /// Format flags used by NCurses
198 enum class Format {
199 None,
200 Bold, NoBold,
201 Underline, NoUnderline,
202 Reverse, NoReverse,
203 AltCharset, NoAltCharset
206 /// This indicates how much the window has to be scrolled
207 enum class Scroll { Up, Down, PageUp, PageDown, Home, End };
209 namespace Mouse {
211 void enable();
212 void disable();
216 /// Initializes curses screen and sets some additional attributes
217 /// @param enable_colors enables colors
218 void initScreen(bool enable_colors, bool enable_mouse);
220 /// Destroys the screen
221 void destroyScreen();
223 /// Struct used for going to given coordinates
224 /// @see Window::operator<<()
225 struct XY
227 XY(int xx, int yy) : x(xx), y(yy) { }
228 int x;
229 int y;
232 /// Main class of NCurses namespace, used as base for other specialized windows
233 struct Window
235 /// Helper function that is periodically invoked
236 // inside Window::getString() function
237 /// @see Window::getString()
238 typedef std::function<bool(const char *)> PromptHook;
240 /// Sets helper to a specific value for the current scope
241 struct ScopedPromptHook
243 template <typename HelperT>
244 ScopedPromptHook(Window &w, HelperT &&helper) noexcept
245 : m_w(w), m_hook(std::move(w.m_prompt_hook)) {
246 m_w.m_prompt_hook = std::forward<HelperT>(helper);
248 ~ScopedPromptHook() noexcept {
249 m_w.m_prompt_hook = std::move(m_hook);
252 private:
253 Window &m_w;
254 PromptHook m_hook;
257 Window() : m_window(nullptr) { }
259 /// Constructs an empty window with given parameters
260 /// @param startx X position of left upper corner of constructed window
261 /// @param starty Y position of left upper corner of constructed window
262 /// @param width width of constructed window
263 /// @param height height of constructed window
264 /// @param title title of constructed window
265 /// @param color base color of constructed window
266 /// @param border border of constructed window
267 Window(size_t startx, size_t starty, size_t width, size_t height,
268 std::string title, Color color, Border border);
270 Window(const Window &rhs);
271 Window(Window &&rhs);
272 Window &operator=(Window w);
274 virtual ~Window();
276 /// Allows for direct access to internal WINDOW pointer in case there
277 /// is no wrapper for a function from curses library one may want to use
278 /// @return internal WINDOW pointer
279 WINDOW *raw() const { return m_window; }
281 /// @return window's width
282 size_t getWidth() const;
284 /// @return window's height
285 size_t getHeight() const;
287 /// @return X position of left upper window's corner
288 size_t getStartX() const;
290 /// @return Y position of left upper window's corner
291 size_t getStarty() const;
293 /// @return window's title
294 const std::string &getTitle() const;
296 /// @return current window's color
297 const Color &getColor() const;
299 /// @return current window's border
300 const Border &getBorder() const;
302 /// @return current window's timeout
303 int getTimeout() const;
305 /// @return current mouse event if readKey() returned KEY_MOUSE
306 const MEVENT &getMouseEvent();
308 /// Reads the string from standard input using readline library.
309 /// @param base base string that has to be edited
310 /// @param length max length of the string, unlimited by default
311 /// @param width width of area that entry field can take. if it's reached, string
312 /// will be scrolled. if value is 0, field will be from cursor position to the end
313 /// of current line wide.
314 /// @param encrypted if set to true, '*' characters will be displayed instead of
315 /// actual text.
316 /// @return edited string
318 /// @see setPromptHook()
319 /// @see SetTimeout()
320 std::string prompt(const std::string &base = "", size_t width = -1, bool encrypted = false);
322 /// Moves cursor to given coordinates
323 /// @param x given X position
324 /// @param y given Y position
325 void goToXY(int x, int y);
327 /// @return x window coordinate
328 int getX();
330 /// @return y windows coordinate
331 int getY();
333 /// Used to indicate whether given coordinates of main screen lies within
334 /// window area or not and if they do, transform them into in-window coords.
335 /// Otherwise function doesn't modify its arguments.
336 /// @param x X position of main screen to be checked
337 /// @param y Y position of main screen to be checked
338 /// @return true if it transformed variables, false otherwise
339 bool hasCoords(int &x, int &y);
341 /// Sets hook used in getString()
342 /// @param hook pointer to function that matches getStringHelper prototype
343 /// @see getString()
344 template <typename HookT>
345 void setPromptHook(HookT &&hook) {
346 m_prompt_hook = std::forward<HookT>(hook);
349 /// Run current GetString helper function (if defined).
350 /// @see getString()
351 /// @return true if helper was run, false otherwise
352 bool runPromptHook(const char *arg, bool *done) const;
354 /// Sets window's base color
355 /// @param fg foregound base color
356 /// @param bg background base color
357 void setBaseColor(Color c);
359 /// Sets window's border
360 /// @param border new window's border
361 void setBorder(Border border);
363 /// Sets window's timeout
364 /// @param timeout window's timeout
365 void setTimeout(int timeout);
367 /// Sets window's title
368 /// @param new_title new title for window
369 void setTitle(const std::string &new_title);
371 /// Refreshed whole window and its border
372 /// @see refresh()
373 void display();
375 /// Refreshes whole window, but not the border
376 /// @see display()
377 virtual void refresh();
379 /// Moves the window to new coordinates
380 /// @param new_x new X position of left upper corner of window
381 /// @param new_y new Y position of left upper corner of window
382 virtual void moveTo(size_t new_x, size_t new_y);
384 /// Resizes the window
385 /// @param new_width new window's width
386 /// @param new_height new window's height
387 virtual void resize(size_t new_width, size_t new_height);
389 /// Cleares the window
390 virtual void clear();
392 /// Adds given file descriptor to the list that will be polled in
393 /// readKey() along with stdin and callback that will be invoked
394 /// when there is data waiting for reading in it
395 /// @param fd file descriptor
396 /// @param callback callback
397 void addFDCallback(int fd, void (*callback)());
399 /// Clears list of file descriptors and their callbacks
400 void clearFDCallbacksList();
402 /// Checks if list of file descriptors is empty
403 /// @return true if list is empty, false otherwise
404 bool FDCallbacksListEmpty() const;
406 /// Reads key from standard input (or takes it from input queue)
407 /// and writes it into read_key variable
408 Key::Type readKey();
410 /// Push single character into input queue, so it can get consumed by ReadKey
411 void pushChar(const NC::Key::Type ch);
413 /// @return const reference to internal input queue
414 const std::queue<NC::Key::Type> &inputQueue() { return m_input_queue; }
416 /// Scrolls the window by amount of lines given in its parameter
417 /// @param where indicates how many lines it has to scroll
418 virtual void scroll(Scroll where);
420 Window &operator<<(TermManip tm);
421 Window &operator<<(const Color &color);
422 Window &operator<<(Format format);
423 Window &operator<<(const XY &coords);
424 Window &operator<<(const char *s);
425 Window &operator<<(char c);
426 Window &operator<<(const wchar_t *ws);
427 Window &operator<<(wchar_t wc);
428 Window &operator<<(int i);
429 Window &operator<<(double d);
430 Window &operator<<(size_t s);
431 Window &operator<<(const std::string &s);
432 Window &operator<<(const std::wstring &ws);
433 protected:
434 /// Sets colors of window (interal use only)
435 /// @param fg foregound color
436 /// @param bg background color
438 void setColor(Color c);
440 /// Refreshes window's border
442 void refreshBorder() const;
444 /// Changes dimensions of window, called from resize()
445 /// @param width new window's width
446 /// @param height new window's height
447 /// @see resize()
449 void adjustDimensions(size_t width, size_t height);
451 /// Deletes old window and creates new. It's called by resize(),
452 /// SetBorder() or setTitle() since internally windows are
453 /// handled as curses pads and change in size requires to delete
454 /// them and create again, there is no way to change size of pad.
455 /// @see SetBorder()
456 /// @see setTitle()
457 /// @see resize()
459 virtual void recreate(size_t width, size_t height);
461 /// internal WINDOW pointers
462 WINDOW *m_window;
464 /// start points and dimensions
465 size_t m_start_x;
466 size_t m_start_y;
467 size_t m_width;
468 size_t m_height;
470 /// window timeout
471 int m_window_timeout;
473 /// current colors
474 Color m_color;
476 /// base colors
477 Color m_base_color;
479 /// current border
480 Border m_border;
482 private:
483 Key::Type getInputChar(int key);
485 /// Sets state of bold attribute (internal use only)
486 /// @param bold_state state of bold attribute
488 void bold(bool bold_state) const;
490 /// Sets state of underline attribute (internal use only)
491 /// @param underline_state state of underline attribute
493 void underline(bool underline_state) const;
495 /// Sets state of reverse attribute (internal use only)
496 /// @param reverse_state state of reverse attribute
498 void reverse(bool reverse_state) const;
500 /// Sets state of altcharset attribute (internal use only)
501 /// @param altcharset_state state of altcharset attribute
503 void altCharset(bool altcharset_state) const;
505 /// pointer to helper function used by getString()
506 /// @see getString()
508 PromptHook m_prompt_hook;
510 /// window title
511 std::string m_title;
513 /// stack of colors
514 std::stack<Color> m_color_stack;
516 /// input queue of a window. you can put characters there using
517 /// PushChar and they will be immediately consumed and
518 /// returned by ReadKey
519 std::queue<Key::Type> m_input_queue;
521 /// containter used for additional file descriptors that have
522 /// to be polled in ReadKey() and correspondent callbacks that
523 /// are invoked if there is data available in them
524 typedef std::vector< std::pair<int, void (*)()> > FDCallbacks;
525 FDCallbacks m_fds;
527 MEVENT m_mouse_event;
528 bool m_escape_terminal_sequences;
530 /// counters for format flags
531 int m_bold_counter;
532 int m_underline_counter;
533 int m_reverse_counter;
534 int m_alt_charset_counter;
539 #endif // NCMPCPP_WINDOW_H