1 /***************************************************************************
2 * Copyright (C) 2008-2013 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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
27 # define NCURSES_MOUSE_VERSION 1
40 // define some Ctrl-? keys
83 #define KEY_SHIFT_TAB 353
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.
94 // undefine scroll macro as it collides with Window::scroll
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
107 # define wcwidth(x) int(!iscntrl(x))
110 /// NC namespace provides set of easy-to-use
111 /// wrappers over original curses library
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
121 Underline
, NoUnderline
,
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<<()
149 Colors(Color one
, Color two
= Color::Default
) : fg(one
), bg(two
) { }
154 /// Struct used for going to given coordinates
155 /// @see Window::operator<<()
158 XY(int xx
, int yy
) : x(xx
), y(yy
) { }
163 /// Main class of NCurses namespace, used as base for other specialized windows
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
);
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
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
250 /// @return y windows coordinate
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
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
294 /// Refreshes whole window, but not the border
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
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
);
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
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.
443 virtual void recreate(size_t width
, size_t height
);
445 /// internal WINDOW pointers
447 WINDOW
*m_border_window
;
449 /// start points and dimensions
456 int m_window_timeout
;
464 Color m_base_bg_color
;
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()
493 GetStringHelper m_get_string_helper
;
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
;
512 /// pointer to container used as history
513 std::list
<std::wstring
> *m_history
;
515 /// counters for format flags
517 int m_underline_counter
;
518 int m_reverse_counter
;
519 int m_alt_charset_counter
;
524 #endif // NCMPCPP_WINDOW_H