scrollpad: explicitely include iostream
[ncmpcpp.git] / src / window.cpp
blob55a21cfeec45e8978e07c13a5180ded8698d4301
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 #include <cstring>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <iostream>
25 #include <readline/history.h>
26 #include <readline/readline.h>
28 #ifdef WIN32
29 # include <winsock.h>
30 #else
31 # include <sys/select.h>
32 # include <unistd.h>
33 #endif
35 #include "error.h"
36 #include "utility/string.h"
37 #include "utility/wide_string.h"
38 #include "window.h"
40 namespace {
41 namespace rl {
43 NC::Window *w;
44 size_t start_x;
45 size_t start_y;
46 size_t width;
47 bool encrypted;
48 const char *base;
50 int read_key(FILE *)
52 size_t x;
53 bool done;
54 int result;
57 x = w->getX();
58 if (w->runGetStringHelper(rl_line_buffer, &done))
60 if (done)
62 rl_done = 1;
63 return 0;
65 w->goToXY(x, start_y);
66 w->refresh();
68 result = w->readKey();
69 if (!w->FDCallbacksListEmpty())
71 w->goToXY(x, start_y);
72 w->refresh();
75 while (result == ERR);
76 return result;
79 void display_string()
81 auto print_char = [](wchar_t wc) {
82 if (encrypted)
83 *w << '*';
84 else
85 *w << wc;
87 auto print_string = [](wchar_t *ws, size_t len) {
88 if (encrypted)
89 for (size_t i = 0; i < len; ++i)
90 *w << '*';
91 else
92 *w << ws;
95 char pt = rl_line_buffer[rl_point];
96 rl_line_buffer[rl_point] = 0;
97 wchar_t pre_pos[rl_point+1];
98 pre_pos[mbstowcs(pre_pos, rl_line_buffer, rl_point)] = 0;
99 rl_line_buffer[rl_point] = pt;
101 int pos = wcswidth(pre_pos, rl_point);
102 if (pos < 0)
103 pos = rl_point;
105 mvwhline(w->raw(), start_y, start_x, ' ', width+1);
106 w->goToXY(start_x, start_y);
107 if (size_t(pos) <= width)
109 print_string(pre_pos, pos);
111 wchar_t post_pos[rl_end-rl_point+1];
112 post_pos[mbstowcs(post_pos, rl_line_buffer+rl_point, rl_end-rl_point)] = 0;
114 size_t cpos = pos;
115 for (wchar_t *c = post_pos; *c != 0; ++c)
117 int n = wcwidth(*c);
118 if (n < 0)
120 print_char(L'.');
121 ++cpos;
123 else
125 if (cpos+n > width)
126 break;
127 cpos += n;
128 print_char(*c);
132 else
134 wchar_t *mod_pre_pos = pre_pos;
135 while (*mod_pre_pos != 0)
137 ++mod_pre_pos;
138 int n = wcwidth(*mod_pre_pos);
139 if (n < 0)
140 --pos;
141 else
142 pos -= n;
143 if (size_t(pos) <= width)
144 break;
146 print_string(mod_pre_pos, pos);
148 w->goToXY(start_x+pos, start_y);
151 int add_base()
153 rl_insert_text(base);
154 return 0;
160 namespace NC {//
162 std::ostream &operator<<(std::ostream &os, Color c)
164 switch (c)
166 case Color::Default:
167 os << "default";
168 break;
169 case Color::Black:
170 os << "black";
171 break;
172 case Color::Red:
173 os << "red";
174 break;
175 case Color::Green:
176 os << "green";
177 break;
178 case Color::Yellow:
179 os << "yellow";
180 break;
181 case Color::Blue:
182 os << "blue";
183 break;
184 case Color::Magenta:
185 os << "magenta";
186 break;
187 case Color::Cyan:
188 os << "cyan";
189 break;
190 case Color::White:
191 os << "white";
192 break;
193 case Color::End:
194 os << "color_end";
195 break;
197 return os;
200 std::istream &operator>>(std::istream &is, Color &c)
202 std::string sc;
203 is >> sc;
204 if (sc == "default")
205 c = Color::Default;
206 else if (sc == "black")
207 c = Color::Black;
208 else if (sc == "red")
209 c = Color::Red;
210 else if (sc == "green")
211 c = Color::Green;
212 else if (sc == "yellow")
213 c = Color::Yellow;
214 else if (sc == "blue")
215 c = Color::Blue;
216 else if (sc == "magenta")
217 c = Color::Magenta;
218 else if (sc == "cyan")
219 c = Color::Cyan;
220 else if (sc == "white")
221 c = Color::White;
222 else if (sc == "color_end")
223 c = Color::End;
224 else
225 is.setstate(std::ios::failbit);
226 return is;
229 std::ostream &operator<<(std::ostream &os, Format f)
231 switch (f)
233 case Format::None:
234 os << "none";
235 break;
236 case Format::Bold:
237 os << "bold";
238 break;
239 case Format::NoBold:
240 os << "bold";
241 break;
242 case Format::Underline:
243 os << "underline";
244 break;
245 case Format::NoUnderline:
246 os << "no_underline";
247 break;
248 case Format::Reverse:
249 os << "reverse";
250 break;
251 case Format::NoReverse:
252 os << "no_reverse";
253 break;
254 case Format::AltCharset:
255 os << "alt_charset";
256 break;
257 case Format::NoAltCharset:
258 os << "no_alt_charset";
259 break;
261 return os;
264 std::ostream &operator<<(std::ostream &os, Border b)
266 switch (b)
268 case Border::None:
269 os << "none";
270 break;
271 case Border::Black:
272 os << "black";
273 break;
274 case Border::Red:
275 os << "red";
276 break;
277 case Border::Green:
278 os << "green";
279 break;
280 case Border::Yellow:
281 os << "yellow";
282 break;
283 case Border::Blue:
284 os << "blue";
285 break;
286 case Border::Magenta:
287 os << "magenta";
288 break;
289 case Border::Cyan:
290 os << "cyan";
291 break;
292 case Border::White:
293 os << "white";
294 break;
296 return os;
299 std::istream &operator>>(std::istream &is, Border &b)
301 std::string sb;
302 is >> sb;
303 if (sb == "none")
304 b = Border::None;
305 else if (sb == "black")
306 b = Border::Black;
307 else if (sb == "red")
308 b = Border::Red;
309 else if (sb == "green")
310 b = Border::Green;
311 else if (sb == "yellow")
312 b = Border::Yellow;
313 else if (sb == "blue")
314 b = Border::Blue;
315 else if (sb == "magenta")
316 b = Border::Magenta;
317 else if (sb == "cyan")
318 b = Border::Cyan;
319 else if (sb == "white")
320 b = Border::White;
321 else
322 is.setstate(std::ios::failbit);
323 return is;
326 std::ostream &operator<<(std::ostream &os, Scroll s)
328 switch (s)
330 case Scroll::Up:
331 os << "scroll_up";
332 break;
333 case Scroll::Down:
334 os << "scroll_down";
335 break;
336 case Scroll::PageUp:
337 os << "scroll_page_up";
338 break;
339 case Scroll::PageDown:
340 os << "scroll_page_down";
341 break;
342 case Scroll::Home:
343 os << "scroll_home";
344 break;
345 case Scroll::End:
346 os << "scroll_end";
347 break;
349 return os;
352 void initScreen(GNUC_UNUSED const char *window_title, bool enable_colors)
354 const int ColorsTable[] =
356 COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
357 COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
359 # ifdef XCURSES
360 Xinitscr(1, const_cast<char **>(&window_title));
361 # else
362 initscr();
363 # endif // XCURSES
364 if (has_colors() && enable_colors)
366 start_color();
367 use_default_colors();
368 int num = 1;
369 # ifdef USE_PDCURSES
370 int i = 0;
371 # else
372 int i = -1;
373 # endif // USE_PDCURSES
374 for (; i < 8; ++i)
375 for (int j = 0; j < 8; ++j)
376 init_pair(num++, ColorsTable[j], i < 0 ? i : ColorsTable[i]);
378 raw();
379 nonl();
380 noecho();
381 curs_set(0);
383 rl_initialize();
384 // disable autocompletion
385 rl_bind_key('\t', nullptr);
386 rl_bind_key(KEY_ESCAPE, nullptr);
387 // do not catch signals
388 rl_catch_signals = 0;
389 // overwrite readline callbacks
390 rl_getc_function = rl::read_key;
391 rl_redisplay_function = rl::display_string;
392 rl_startup_hook = rl::add_base;
395 void destroyScreen()
397 curs_set(1);
398 endwin();
401 Window::Window(size_t startx,
402 size_t starty,
403 size_t width,
404 size_t height,
405 const std::string &title,
406 Color color,
407 Border border)
408 : m_window(0),
409 m_border_window(0),
410 m_start_x(startx),
411 m_start_y(starty),
412 m_width(width),
413 m_height(height),
414 m_window_timeout(-1),
415 m_color(color),
416 m_bg_color(Color::Default),
417 m_base_color(color),
418 m_base_bg_color(Color::Default),
419 m_border(border),
420 m_get_string_helper(0),
421 m_title(title),
422 m_bold_counter(0),
423 m_underline_counter(0),
424 m_reverse_counter(0),
425 m_alt_charset_counter(0)
427 if (m_start_x > size_t(COLS)
428 || m_start_y > size_t(LINES)
429 || m_width+m_start_x > size_t(COLS)
430 || m_height+m_start_y > size_t(LINES))
431 FatalError("Constructed window is bigger than terminal size");
433 if (m_border != Border::None)
435 m_border_window = newpad(m_height, m_width);
436 wattron(m_border_window, COLOR_PAIR(int(m_border)));
437 box(m_border_window, 0, 0);
438 m_start_x++;
439 m_start_y++;
440 m_width -= 2;
441 m_height -= 2;
443 if (!m_title.empty())
445 m_start_y += 2;
446 m_height -= 2;
449 m_window = newpad(m_height, m_width);
451 setColor(m_color);
452 keypad(m_window, 1);
455 Window::Window(const Window &rhs)
456 : m_window(dupwin(rhs.m_window))
457 , m_border_window(dupwin(rhs.m_border_window))
458 , m_start_x(rhs.m_start_x)
459 , m_start_y(rhs.m_start_y)
460 , m_width(rhs.m_width)
461 , m_height(rhs.m_height)
462 , m_window_timeout(rhs.m_window_timeout)
463 , m_color(rhs.m_color)
464 , m_bg_color(rhs.m_bg_color)
465 , m_base_color(rhs.m_base_color)
466 , m_base_bg_color(rhs.m_base_bg_color)
467 , m_border(rhs.m_border)
468 , m_get_string_helper(rhs.m_get_string_helper)
469 , m_title(rhs.m_title)
470 , m_color_stack(rhs.m_color_stack)
471 , m_input_queue(rhs.m_input_queue)
472 , m_fds(rhs.m_fds)
473 , m_bold_counter(rhs.m_bold_counter)
474 , m_underline_counter(rhs.m_underline_counter)
475 , m_reverse_counter(rhs.m_reverse_counter)
476 , m_alt_charset_counter(rhs.m_alt_charset_counter)
480 Window::Window(Window &&rhs)
481 : m_window(rhs.m_window)
482 , m_border_window(rhs.m_border_window)
483 , m_start_x(rhs.m_start_x)
484 , m_start_y(rhs.m_start_y)
485 , m_width(rhs.m_width)
486 , m_height(rhs.m_height)
487 , m_window_timeout(rhs.m_window_timeout)
488 , m_color(rhs.m_color)
489 , m_bg_color(rhs.m_bg_color)
490 , m_base_color(rhs.m_base_color)
491 , m_base_bg_color(rhs.m_base_bg_color)
492 , m_border(rhs.m_border)
493 , m_get_string_helper(rhs.m_get_string_helper)
494 , m_title(std::move(rhs.m_title))
495 , m_color_stack(std::move(rhs.m_color_stack))
496 , m_input_queue(std::move(rhs.m_input_queue))
497 , m_fds(std::move(rhs.m_fds))
498 , m_bold_counter(rhs.m_bold_counter)
499 , m_underline_counter(rhs.m_underline_counter)
500 , m_reverse_counter(rhs.m_reverse_counter)
501 , m_alt_charset_counter(rhs.m_alt_charset_counter)
503 rhs.m_window = 0;
504 rhs.m_border_window = 0;
507 Window &Window::operator=(Window rhs)
509 std::swap(m_window, rhs.m_window);
510 std::swap(m_border_window, rhs.m_border_window);
511 std::swap(m_start_x, rhs.m_start_x);
512 std::swap(m_start_y, rhs.m_start_y);
513 std::swap(m_width, rhs.m_width);
514 std::swap(m_height, rhs.m_height);
515 std::swap(m_window_timeout, rhs.m_window_timeout);
516 std::swap(m_color, rhs.m_color);
517 std::swap(m_bg_color, rhs.m_bg_color);
518 std::swap(m_base_color, rhs.m_base_color);
519 std::swap(m_base_bg_color, rhs.m_base_bg_color);
520 std::swap(m_border, rhs.m_border);
521 std::swap(m_get_string_helper, rhs.m_get_string_helper);
522 std::swap(m_title, rhs.m_title);
523 std::swap(m_color_stack, rhs.m_color_stack);
524 std::swap(m_input_queue, rhs.m_input_queue);
525 std::swap(m_fds, rhs.m_fds);
526 std::swap(m_bold_counter, rhs.m_bold_counter);
527 std::swap(m_underline_counter, rhs.m_underline_counter);
528 std::swap(m_reverse_counter, rhs.m_reverse_counter);
529 std::swap(m_alt_charset_counter, rhs.m_alt_charset_counter);
530 return *this;
533 Window::~Window()
535 delwin(m_window);
536 delwin(m_border_window);
539 void Window::setColor(Color fg, Color bg)
541 if (fg == Color::Default)
542 fg = m_base_color;
544 if (fg != Color::Default)
545 wattron(m_window, COLOR_PAIR(int(bg)*8+int(fg)));
546 else
547 wattroff(m_window, COLOR_PAIR(int(m_color)));
548 m_color = fg;
549 m_bg_color = bg;
552 void Window::setBaseColor(Color fg, Color bg)
554 m_base_color = fg;
555 m_base_bg_color = bg;
558 void Window::setBorder(Border border)
560 if (border == Border::None && m_border != Border::None)
562 delwin(m_border_window);
563 m_start_x--;
564 m_start_y--;
565 m_height += 2;
566 m_width += 2;
567 recreate(m_width, m_height);
569 else if (border != Border::None && m_border == Border::None)
571 m_border_window = newpad(m_height, m_width);
572 wattron(m_border_window, COLOR_PAIR(int(border)));
573 box(m_border_window,0,0);
574 m_start_x++;
575 m_start_y++;
576 m_height -= 2;
577 m_width -= 2;
578 recreate(m_width, m_height);
580 else
582 wattron(m_border_window,COLOR_PAIR(int(border)));
583 box(m_border_window, 0, 0);
585 m_border = border;
588 void Window::setTitle(const std::string &new_title)
590 if (m_title == new_title)
592 return;
594 else if (!new_title.empty() && m_title.empty())
596 m_start_y += 2;
597 m_height -= 2;
598 recreate(m_width, m_height);
600 else if (new_title.empty() && !m_title.empty())
602 m_start_y -= 2;
603 m_height += 2;
604 recreate(m_width, m_height);
606 m_title = new_title;
609 void Window::recreate(size_t width, size_t height)
611 delwin(m_window);
612 m_window = newpad(height, width);
613 setTimeout(m_window_timeout);
614 setColor(m_color, m_bg_color);
615 keypad(m_window, 1);
618 void Window::moveTo(size_t new_x, size_t new_y)
620 m_start_x = new_x;
621 m_start_y = new_y;
622 if (m_border != Border::None)
624 m_start_x++;
625 m_start_y++;
627 if (!m_title.empty())
628 m_start_y += 2;
631 void Window::adjustDimensions(size_t width, size_t height)
633 if (m_border != Border::None)
635 delwin(m_border_window);
636 m_border_window = newpad(height, width);
637 wattron(m_border_window, COLOR_PAIR(int(m_border)));
638 box(m_border_window, 0, 0);
639 width -= 2;
640 height -= 2;
642 if (!m_title.empty())
643 height -= 2;
644 m_height = height;
645 m_width = width;
648 void Window::resize(size_t new_width, size_t new_height)
650 adjustDimensions(new_width, new_height);
651 recreate(m_width, m_height);
654 void Window::showBorder() const
656 if (m_border != Border::None)
658 ::refresh();
659 prefresh(m_border_window, 0, 0, getStarty(), getStartX(), m_start_y+m_height, m_start_x+m_width);
661 if (!m_title.empty())
663 if (m_border != Border::None)
664 attron(COLOR_PAIR(int(m_border)));
665 else
666 attron(COLOR_PAIR(int(m_base_color)));
667 mvhline(m_start_y-1, m_start_x, 0, m_width);
668 attron(A_BOLD);
669 mvhline(m_start_y-2, m_start_x, 32, m_width); // clear title line
670 mvaddstr(m_start_y-2, m_start_x, m_title.c_str());
671 attroff(COLOR_PAIR(int(m_border)) | A_BOLD);
673 ::refresh();
676 void Window::display()
678 showBorder();
679 refresh();
682 void Window::refresh()
684 prefresh(m_window, 0, 0, m_start_y, m_start_x, m_start_y+m_height-1, m_start_x+m_width-1);
687 void Window::clear()
689 werase(m_window);
692 void Window::bold(bool bold_state) const
694 (bold_state ? wattron : wattroff)(m_window, A_BOLD);
697 void Window::underline(bool underline_state) const
699 (underline_state ? wattron : wattroff)(m_window, A_UNDERLINE);
702 void Window::reverse(bool reverse_state) const
704 (reverse_state ? wattron : wattroff)(m_window, A_REVERSE);
707 void Window::altCharset(bool altcharset_state) const
709 (altcharset_state ? wattron : wattroff)(m_window, A_ALTCHARSET);
712 void Window::setTimeout(int timeout)
714 if (timeout != m_window_timeout)
716 m_window_timeout = timeout;
717 wtimeout(m_window, timeout);
721 void Window::addFDCallback(int fd, void (*callback)())
723 m_fds.push_back(std::make_pair(fd, callback));
726 void Window::clearFDCallbacksList()
728 m_fds.clear();
731 bool Window::FDCallbacksListEmpty() const
733 return m_fds.empty();
736 int Window::readKey()
738 int result;
739 // if there are characters in input queue, get them and
740 // return immediately.
741 if (!m_input_queue.empty())
743 result = m_input_queue.front();
744 m_input_queue.pop();
745 return result;
747 // in pdcurses polling stdin doesn't work, so we can't poll
748 // both stdin and other file descriptors in one select. the
749 // workaround is to set the timeout of select to 0, poll
750 // other file descriptors and then wait for stdin input with
751 // the given timeout. unfortunately, this results in delays
752 // since ncmpcpp doesn't see that data arrived while waiting
753 // for input from stdin, but it seems there is no better option.
755 fd_set fdset;
756 FD_ZERO(&fdset);
757 # if !defined(USE_PDCURSES)
758 FD_SET(STDIN_FILENO, &fdset);
759 timeval timeout = { m_window_timeout/1000, (m_window_timeout%1000)*1000 };
760 # else
761 timeval timeout = { 0, 0 };
762 # endif
764 int fd_max = STDIN_FILENO;
765 for (FDCallbacks::const_iterator it = m_fds.begin(); it != m_fds.end(); ++it)
767 if (it->first > fd_max)
768 fd_max = it->first;
769 FD_SET(it->first, &fdset);
772 if (select(fd_max+1, &fdset, 0, 0, m_window_timeout < 0 ? 0 : &timeout) > 0)
774 # if !defined(USE_PDCURSES)
775 result = FD_ISSET(STDIN_FILENO, &fdset) ? wgetch(m_window) : ERR;
776 # endif // !USE_PDCURSES
777 for (FDCallbacks::const_iterator it = m_fds.begin(); it != m_fds.end(); ++it)
778 if (FD_ISSET(it->first, &fdset))
779 it->second();
781 # if !defined(USE_PDCURSES)
782 else
783 result = ERR;
784 # else
785 result = wgetch(m_window);
786 # endif
787 return result;
790 void Window::pushChar(int ch)
792 m_input_queue.push(ch);
795 std::string Window::getString(const std::string &base, size_t width, bool encrypted)
797 rl::w = this;
798 getyx(m_window, rl::start_y, rl::start_x);
799 rl::width = width;
800 rl::encrypted = encrypted;
801 rl::base = base.c_str();
803 width--;
804 if (width == size_t(-1))
805 rl::width = m_width-rl::start_x-1;
806 else
807 rl::width = width;
809 mmask_t oldmask;
810 std::string result;
812 curs_set(1);
813 keypad(m_window, 0);
814 mousemask(0, &oldmask);
815 char *input = readline(nullptr);
816 mousemask(oldmask, nullptr);
817 keypad(m_window, 1);
818 curs_set(0);
819 if (input != nullptr)
821 if (input[0] != 0)
822 add_history(input);
823 result = input;
824 free(input);
827 return result;
830 void Window::goToXY(int x, int y)
832 wmove(m_window, y, x);
835 int Window::getX()
837 return getcurx(m_window);
840 int Window::getY()
842 return getcury(m_window);
845 bool Window::hasCoords(int &x, int &y)
847 # ifndef USE_PDCURSES
848 return wmouse_trafo(m_window, &y, &x, 0);
849 # else
850 // wmouse_trafo is broken in pdcurses, use our own implementation
851 size_t u_x = x, u_y = y;
852 if (u_x >= m_start_x && u_x < m_start_x+m_width
853 && u_y >= m_start_y && u_y < m_start_y+m_height)
855 x -= m_start_x;
856 y -= m_start_y;
857 return true;
859 return false;
860 # endif
863 bool Window::runGetStringHelper(const char *arg, bool *done) const
865 if (m_get_string_helper)
867 bool continue_ = m_get_string_helper(arg);
868 if (done != nullptr)
869 *done = !continue_;
870 return true;
872 else
873 return false;
876 size_t Window::getWidth() const
878 if (m_border != Border::None)
879 return m_width+2;
880 else
881 return m_width;
884 size_t Window::getHeight() const
886 size_t height = m_height;
887 if (m_border != Border::None)
888 height += 2;
889 if (!m_title.empty())
890 height += 2;
891 return height;
894 size_t Window::getStartX() const
896 if (m_border != Border::None)
897 return m_start_x-1;
898 else
899 return m_start_x;
902 size_t Window::getStarty() const
904 size_t starty = m_start_y;
905 if (m_border != Border::None)
906 starty--;
907 if (!m_title.empty())
908 starty -= 2;
909 return starty;
912 const std::string &Window::getTitle() const
914 return m_title;
917 Color Window::getColor() const
919 return m_color;
922 Border Window::getBorder() const
924 return m_border;
927 int Window::getTimeout() const
929 return m_window_timeout;
932 void Window::scroll(Scroll where)
934 idlok(m_window, 1);
935 scrollok(m_window, 1);
936 switch (where)
938 case Scroll::Up:
939 wscrl(m_window, 1);
940 break;
941 case Scroll::Down:
942 wscrl(m_window, -1);
943 break;
944 case Scroll::PageUp:
945 wscrl(m_window, m_width);
946 break;
947 case Scroll::PageDown:
948 wscrl(m_window, -m_width);
949 break;
950 default:
951 break;
953 idlok(m_window, 0);
954 scrollok(m_window, 0);
958 Window &Window::operator<<(Colors colors)
960 if (colors.fg == Color::End || colors.bg == Color::End)
962 *this << Color::End;
963 return *this;
965 m_color_stack.push(colors);
966 setColor(colors.fg, colors.bg);
967 return *this;
970 Window &Window::operator<<(Color color)
972 switch (color)
974 case Color::Default:
975 while (!m_color_stack.empty())
976 m_color_stack.pop();
977 setColor(m_base_color, m_base_bg_color);
978 break;
979 case Color::End:
980 if (!m_color_stack.empty())
981 m_color_stack.pop();
982 if (!m_color_stack.empty())
983 setColor(m_color_stack.top().fg, m_color_stack.top().bg);
984 else
985 setColor(m_base_color, m_base_bg_color);
986 break;
987 default:
988 Color bg;
989 if (m_color_stack.empty())
990 bg = m_bg_color;
991 else
992 bg = m_color_stack.top().bg;
993 m_color_stack.push(Colors(color, bg));
994 setColor(m_color_stack.top().fg, m_color_stack.top().bg);
996 return *this;
999 Window &Window::operator<<(Format format)
1001 switch (format)
1003 case Format::None:
1004 bold((m_bold_counter = 0));
1005 reverse((m_reverse_counter = 0));
1006 altCharset((m_alt_charset_counter = 0));
1007 break;
1008 case Format::Bold:
1009 bold(++m_bold_counter);
1010 break;
1011 case Format::NoBold:
1012 if (--m_bold_counter <= 0)
1013 bold((m_bold_counter = 0));
1014 break;
1015 case Format::Underline:
1016 underline(++m_underline_counter);
1017 break;
1018 case Format::NoUnderline:
1019 if (--m_underline_counter <= 0)
1020 underline((m_underline_counter = 0));
1021 break;
1022 case Format::Reverse:
1023 reverse(++m_reverse_counter);
1024 break;
1025 case Format::NoReverse:
1026 if (--m_reverse_counter <= 0)
1027 reverse((m_reverse_counter = 0));
1028 break;
1029 case Format::AltCharset:
1030 altCharset(++m_alt_charset_counter);
1031 break;
1032 case Format::NoAltCharset:
1033 if (--m_alt_charset_counter <= 0)
1034 altCharset((m_alt_charset_counter = 0));
1035 break;
1037 return *this;
1040 Window &Window::operator<<(int (*f)(WINDOW *))
1042 f(m_window);
1043 return *this;
1046 Window &Window::operator<<(XY coords)
1048 goToXY(coords.x, coords.y);
1049 return *this;
1052 Window &Window::operator<<(const char *s)
1054 for (const char *c = s; *c != '\0'; ++c)
1055 wprintw(m_window, "%c", *c);
1056 return *this;
1059 Window &Window::operator<<(char c)
1061 wprintw(m_window, "%c", c);
1062 return *this;
1065 Window &Window::operator<<(const wchar_t *ws)
1067 for (const wchar_t *wc = ws; *wc != L'\0'; ++wc)
1068 wprintw(m_window, "%lc", *wc);
1069 return *this;
1072 Window &Window::operator<<(wchar_t wc)
1074 wprintw(m_window, "%lc", wc);
1075 return *this;
1078 Window &Window::operator<<(int i)
1080 wprintw(m_window, "%d", i);
1081 return *this;
1084 Window &Window::operator<<(double d)
1086 wprintw(m_window, "%f", d);
1087 return *this;
1090 Window &Window::operator<<(const std::string &s)
1092 // for some reason passing whole string at once with "%s" doesn't work
1093 // (string is cut in the middle, probably due to limitation of ncurses'
1094 // internal buffer?), so we need to pass characters in a loop.
1095 for (auto it = s.begin(); it != s.end(); ++it)
1096 wprintw(m_window, "%c", *it);
1097 return *this;
1100 Window &Window::operator<<(const std::wstring &ws)
1102 for (auto it = ws.begin(); it != ws.end(); ++it)
1103 wprintw(m_window, "%lc", *it);
1104 return *this;
1107 Window &Window::operator<<(size_t s)
1109 wprintw(m_window, "%zu", s);
1110 return *this;