1 /***************************************************************************
2 * Copyright (C) 2008-2014 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 std::ostream
&operator<<(std::ostream
&os
, Color c
);
118 std::istream
&operator>>(std::istream
&is
, Color
&c
);
120 /// Format flags used by NCurses
124 Underline
, NoUnderline
,
126 AltCharset
, NoAltCharset
129 std::ostream
&operator<<(std::ostream
&os
, Format f
);
131 /// Available border colors for window
132 enum class Border
{ None
, Black
, Red
, Green
, Yellow
, Blue
, Magenta
, Cyan
, White
};
134 std::ostream
&operator<<(std::ostream
&os
, Border b
);
135 std::istream
&operator>>(std::istream
&is
, Border
&b
);
137 /// This indicates how much the window has to be scrolled
138 enum class Scroll
{ Up
, Down
, PageUp
, PageDown
, Home
, End
};
140 std::ostream
&operator<<(std::ostream
&os
, Scroll s
);
142 /// Helper function that is invoked each time one will want
143 /// to obtain string from Window::getString() function
144 /// @see Window::getString()
145 typedef std::function
<bool(const char *)> GetStringHelper
;
147 /// Initializes curses screen and sets some additional attributes
148 /// @param window_title title of the window (has an effect only if pdcurses lib is used)
149 /// @param enable_colors enables colors
150 void initScreen(const char *window_title
, bool enable_colors
);
152 /// Destroys the screen
153 void destroyScreen();
155 /// Struct used to set color of both foreground and background of window
156 /// @see Window::operator<<()
159 Colors(Color one
, Color two
= Color::Default
) : fg(one
), bg(two
) { }
164 /// Struct used for going to given coordinates
165 /// @see Window::operator<<()
168 XY(int xx
, int yy
) : x(xx
), y(yy
) { }
173 /// Main class of NCurses namespace, used as base for other specialized windows
176 Window() : m_window(0), m_border_window(0) { }
178 /// Constructs an empty window with given parameters
179 /// @param startx X position of left upper corner of constructed window
180 /// @param starty Y position of left upper corner of constructed window
181 /// @param width width of constructed window
182 /// @param height height of constructed window
183 /// @param title title of constructed window
184 /// @param color base color of constructed window
185 /// @param border border of constructed window
186 Window(size_t startx
, size_t starty
, size_t width
, size_t height
,
187 const std::string
&title
, Color color
, Border border
);
189 Window(const Window
&rhs
);
190 Window(Window
&&rhs
);
191 Window
&operator=(Window w
);
195 /// Allows for direct access to internal WINDOW pointer in case there
196 /// is no wrapper for a function from curses library one may want to use
197 /// @return internal WINDOW pointer
198 WINDOW
*raw() const { return m_window
; }
200 /// @return window's width
201 size_t getWidth() const;
203 /// @return window's height
204 size_t getHeight() const;
206 /// @return X position of left upper window's corner
207 size_t getStartX() const;
209 /// @return Y position of left upper window's corner
210 size_t getStarty() const;
212 /// @return window's title
213 const std::string
&getTitle() const;
215 /// @return current window's color
216 Color
getColor() const;
218 /// @return current window's border
219 Border
getBorder() const;
221 /// @return current window's timeout
222 int getTimeout() const;
224 /// Reads the string from standard input. Note that this is much more complex
225 /// function than getstr() from curses library. It allows for moving through
226 /// letters with arrows, supports scrolling if string's length is bigger than
227 /// given area, supports history of previous strings and each time it receives
228 /// an input from the keyboard or the timeout is reached, it calls helper function
229 /// (if it's set) that takes as an argument currently edited string.
230 /// @param base base string that has to be edited
231 /// @param length max length of string, unlimited by default
232 /// @param width width of area that entry field can take. if it's reached, string
233 /// will be scrolled. if value is 0, field will be from cursor position to the end
234 /// of current line wide.
235 /// @param encrypted if set to true, '*' characters will be displayed instead of
237 /// @return edited string
239 /// @see setGetStringHelper()
240 /// @see SetTimeout()
241 /// @see CreateHistory()
242 std::string
getString(const std::string
&base
, size_t width
= 0, bool encrypted
= 0);
244 /// Wrapper for above function that doesn't take base string (it will be empty).
245 /// Taken parameters are the same as for above.
246 std::string
getString(size_t width
= 0, bool encrypted
= 0)
248 return getString("", width
, encrypted
);
251 /// Moves cursor to given coordinates
252 /// @param x given X position
253 /// @param y given Y position
254 void goToXY(int x
, int y
);
256 /// @return x window coordinate
259 /// @return y windows coordinate
262 /// Used to indicate whether given coordinates of main screen lies within
263 /// window area or not and if they do, transform them into in-window coords.
264 /// Otherwise function doesn't modify its arguments.
265 /// @param x X position of main screen to be checked
266 /// @param y Y position of main screen to be checked
267 /// @return true if it transformed variables, false otherwise
268 bool hasCoords(int &x
, int &y
);
270 /// Sets helper function used in getString()
271 /// @param helper pointer to function that matches getStringHelper prototype
273 void setGetStringHelper(GetStringHelper helper
) { m_get_string_helper
= helper
; }
275 /// Run current GetString helper function (if defined).
277 /// @return true if helper was run, false otherwise
278 bool runGetStringHelper(const char *arg
, bool *done
) const;
280 /// Sets window's base color
281 /// @param fg foregound base color
282 /// @param bg background base color
283 void setBaseColor(Color fg
, Color bg
= Color::Default
);
285 /// Sets window's border
286 /// @param border new window's border
287 void setBorder(Border border
);
289 /// Sets window's timeout
290 /// @param timeout window's timeout
291 void setTimeout(int timeout
);
293 /// Sets window's title
294 /// @param new_title new title for window
295 void setTitle(const std::string
&new_title
);
297 /// Refreshed whole window and its border
301 /// Refreshes whole window, but not the border
303 virtual void refresh();
305 /// Moves the window to new coordinates
306 /// @param new_x new X position of left upper corner of window
307 /// @param new_y new Y position of left upper corner of window
308 virtual void moveTo(size_t new_x
, size_t new_y
);
310 /// Resizes the window
311 /// @param new_width new window's width
312 /// @param new_height new window's height
313 virtual void resize(size_t new_width
, size_t new_height
);
315 /// Cleares the window
316 virtual void clear();
318 /// Adds given file descriptor to the list that will be polled in
319 /// ReadKey() along with stdin and callback that will be invoked
320 /// when there is data waiting for reading in it
321 /// @param fd file descriptor
322 /// @param callback callback
323 void addFDCallback(int fd
, void (*callback
)());
325 /// Clears list of file descriptors and their callbacks
326 void clearFDCallbacksList();
328 /// Checks if list of file descriptors is empty
329 /// @return true if list is empty, false otherwise
330 bool FDCallbacksListEmpty() const;
332 /// Reads key from standard input (or takes it from input queue)
333 /// and writes it into read_key variable
336 /// Push single character into input queue, so it can get consumed by ReadKey
337 void pushChar(int ch
);
339 /// @return const reference to internal input queue
340 const std::queue
<int> &inputQueue() { return m_input_queue
; }
342 /// Scrolls the window by amount of lines given in its parameter
343 /// @param where indicates how many lines it has to scroll
344 virtual void scroll(Scroll where
);
346 /// Applies function of compatible prototype to internal WINDOW pointer
347 /// The mostly used function in this case seem to be wclrtoeol(), which
348 /// clears the window from current cursor position to the end of line.
349 /// Note that delwin() also matches that prototype, but I wouldn't
350 /// recommend anyone passing this pointer here ;)
351 /// @param f pointer to function to call with internal WINDOW pointer
352 /// @return reference to itself
353 Window
&operator<<(int (*f
)(WINDOW
*));
355 /// Applies foreground and background colors to window
356 /// @param colors struct that holds new colors information
357 /// @return reference to itself
358 Window
&operator<<(Colors colors
);
360 /// Applies foregound color to window. Note that colors applied
361 /// that way are stacked, i.e if you applied Color::Red, then Color::Green
362 /// and Color::End, current color would be Color::Red. If you want to discard
363 /// all colors and fall back to base one, pass Color::Default.
364 /// @param color new color value
365 /// @return reference to itself
366 Window
&operator<<(Color color
);
368 /// Applies format flag to window. Note that these attributes are
369 /// also stacked, so if you applied Format::Bold twice, to get rid of
370 /// it you have to pass Format::NoBold also twice.
371 /// @param format format flag
372 /// @return reference to itself
373 Window
&operator<<(Format format
);
375 /// Moves current cursor position to given coordinates.
376 /// @param coords struct that holds information about new coordinations
377 /// @return reference to itself
378 Window
&operator<<(XY coords
);
380 /// Prints string to window
381 /// @param s const pointer to char array to be printed
382 /// @return reference to itself
383 Window
&operator<<(const char *s
);
385 /// Prints single character to window
386 /// @param c character to be printed
387 /// @return reference to itself
388 Window
&operator<<(char c
);
390 /// Prints wide string to window
391 /// @param ws const pointer to wchar_t array to be printed
392 /// @return reference to itself
393 Window
&operator<<(const wchar_t *ws
);
395 /// Prints single wide character to window
396 /// @param wc wide character to be printed
397 /// @return reference to itself
398 Window
&operator<<(wchar_t wc
);
400 /// Prints int to window
401 /// @param i integer value to be printed
402 /// @return reference to itself
403 Window
&operator<<(int i
);
405 /// Prints double to window
406 /// @param d double value to be printed
407 /// @return reference to itself
408 Window
&operator<<(double d
);
410 /// Prints size_t to window
411 /// @param s size value to be printed
412 /// @return reference to itself
413 Window
&operator<<(size_t s
);
415 /// Prints std::string to window
416 /// @param s string to be printed
417 /// @return reference to itself
418 Window
&operator<<(const std::string
&s
);
420 /// Prints std::wstring to window
421 /// @param ws wide string to be printed
422 /// @return reference to itself
423 Window
&operator<<(const std::wstring
&ws
);
425 /// Sets colors of window (interal use only)
426 /// @param fg foregound color
427 /// @param bg background color
429 void setColor(Color fg
, Color bg
= Color::Default
);
431 /// Refreshes window's border
433 void showBorder() const;
435 /// Changes dimensions of window, called from resize()
436 /// @param width new window's width
437 /// @param height new window's height
440 void adjustDimensions(size_t width
, size_t height
);
442 /// Deletes old window and creates new. It's called by resize(),
443 /// SetBorder() or setTitle() since internally windows are
444 /// handled as curses pads and change in size requires to delete
445 /// them and create again, there is no way to change size of pad.
450 virtual void recreate(size_t width
, size_t height
);
452 /// internal WINDOW pointers
454 WINDOW
*m_border_window
;
456 /// start points and dimensions
463 int m_window_timeout
;
471 Color m_base_bg_color
;
477 /// Sets state of bold attribute (internal use only)
478 /// @param bold_state state of bold attribute
480 void bold(bool bold_state
) const;
482 /// Sets state of underline attribute (internal use only)
483 /// @param underline_state state of underline attribute
485 void underline(bool underline_state
) const;
487 /// Sets state of reverse attribute (internal use only)
488 /// @param reverse_state state of reverse attribute
490 void reverse(bool reverse_state
) const;
492 /// Sets state of altcharset attribute (internal use only)
493 /// @param altcharset_state state of altcharset attribute
495 void altCharset(bool altcharset_state
) const;
497 /// pointer to helper function used by getString()
500 GetStringHelper m_get_string_helper
;
506 std::stack
<Colors
> m_color_stack
;
508 /// input queue of a window. you can put characters there using
509 /// PushChar and they will be immediately consumed and
510 /// returned by ReadKey
511 std::queue
<int> m_input_queue
;
513 /// containter used for additional file descriptors that have
514 /// to be polled in ReadKey() and correspondent callbacks that
515 /// are invoked if there is data available in them
516 typedef std::vector
< std::pair
<int, void (*)()> > FDCallbacks
;
519 /// counters for format flags
521 int m_underline_counter
;
522 int m_reverse_counter
;
523 int m_alt_charset_counter
;
528 #endif // NCMPCPP_WINDOW_H