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 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<<()
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) { }
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 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
249 /// @return y windows coordinate
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
263 void setGetStringHelper(GetStringHelper helper
) { m_get_string_helper
= helper
; }
265 /// Run current GetString helper function (if defined).
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
291 /// Refreshes whole window, but not the border
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
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
);
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
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.
440 virtual void recreate(size_t width
, size_t height
);
442 /// internal WINDOW pointers
444 WINDOW
*m_border_window
;
446 /// start points and dimensions
453 int m_window_timeout
;
461 Color m_base_bg_color
;
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()
490 GetStringHelper m_get_string_helper
;
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
;
509 /// counters for format flags
511 int m_underline_counter
;
512 int m_reverse_counter
;
513 int m_alt_charset_counter
;
518 #endif // NCMPCPP_WINDOW_H