change window timeout more transparently
[ncmpcpp.git] / src / window.cpp
blob15877c143ae367c0c7ee08319b06e72a7c31dc65
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 noecho();
379 cbreak();
380 curs_set(0);
382 rl_initialize();
383 // disable autocompletion
384 rl_bind_key('\t', nullptr);
385 // overwrite readline callbacks
386 rl_getc_function = rl::read_key;
387 rl_redisplay_function = rl::display_string;
388 rl_startup_hook = rl::add_base;
391 void destroyScreen()
393 curs_set(1);
394 endwin();
397 Window::Window(size_t startx,
398 size_t starty,
399 size_t width,
400 size_t height,
401 const std::string &title,
402 Color color,
403 Border border)
404 : m_window(0),
405 m_border_window(0),
406 m_start_x(startx),
407 m_start_y(starty),
408 m_width(width),
409 m_height(height),
410 m_window_timeout(-1),
411 m_color(color),
412 m_bg_color(Color::Default),
413 m_base_color(color),
414 m_base_bg_color(Color::Default),
415 m_border(border),
416 m_get_string_helper(0),
417 m_title(title),
418 m_bold_counter(0),
419 m_underline_counter(0),
420 m_reverse_counter(0),
421 m_alt_charset_counter(0)
423 if (m_start_x > size_t(COLS)
424 || m_start_y > size_t(LINES)
425 || m_width+m_start_x > size_t(COLS)
426 || m_height+m_start_y > size_t(LINES))
427 FatalError("Constructed window is bigger than terminal size");
429 if (m_border != Border::None)
431 m_border_window = newpad(m_height, m_width);
432 wattron(m_border_window, COLOR_PAIR(int(m_border)));
433 box(m_border_window, 0, 0);
434 m_start_x++;
435 m_start_y++;
436 m_width -= 2;
437 m_height -= 2;
439 if (!m_title.empty())
441 m_start_y += 2;
442 m_height -= 2;
445 m_window = newpad(m_height, m_width);
447 setColor(m_color);
448 keypad(m_window, 1);
451 Window::Window(const Window &rhs)
452 : m_window(dupwin(rhs.m_window))
453 , m_border_window(dupwin(rhs.m_border_window))
454 , m_start_x(rhs.m_start_x)
455 , m_start_y(rhs.m_start_y)
456 , m_width(rhs.m_width)
457 , m_height(rhs.m_height)
458 , m_window_timeout(rhs.m_window_timeout)
459 , m_color(rhs.m_color)
460 , m_bg_color(rhs.m_bg_color)
461 , m_base_color(rhs.m_base_color)
462 , m_base_bg_color(rhs.m_base_bg_color)
463 , m_border(rhs.m_border)
464 , m_get_string_helper(rhs.m_get_string_helper)
465 , m_title(rhs.m_title)
466 , m_color_stack(rhs.m_color_stack)
467 , m_input_queue(rhs.m_input_queue)
468 , m_fds(rhs.m_fds)
469 , m_bold_counter(rhs.m_bold_counter)
470 , m_underline_counter(rhs.m_underline_counter)
471 , m_reverse_counter(rhs.m_reverse_counter)
472 , m_alt_charset_counter(rhs.m_alt_charset_counter)
476 Window::Window(Window &&rhs)
477 : m_window(rhs.m_window)
478 , m_border_window(rhs.m_border_window)
479 , m_start_x(rhs.m_start_x)
480 , m_start_y(rhs.m_start_y)
481 , m_width(rhs.m_width)
482 , m_height(rhs.m_height)
483 , m_window_timeout(rhs.m_window_timeout)
484 , m_color(rhs.m_color)
485 , m_bg_color(rhs.m_bg_color)
486 , m_base_color(rhs.m_base_color)
487 , m_base_bg_color(rhs.m_base_bg_color)
488 , m_border(rhs.m_border)
489 , m_get_string_helper(rhs.m_get_string_helper)
490 , m_title(std::move(rhs.m_title))
491 , m_color_stack(std::move(rhs.m_color_stack))
492 , m_input_queue(std::move(rhs.m_input_queue))
493 , m_fds(std::move(rhs.m_fds))
494 , m_bold_counter(rhs.m_bold_counter)
495 , m_underline_counter(rhs.m_underline_counter)
496 , m_reverse_counter(rhs.m_reverse_counter)
497 , m_alt_charset_counter(rhs.m_alt_charset_counter)
499 rhs.m_window = 0;
500 rhs.m_border_window = 0;
503 Window &Window::operator=(Window rhs)
505 std::swap(m_window, rhs.m_window);
506 std::swap(m_border_window, rhs.m_border_window);
507 std::swap(m_start_x, rhs.m_start_x);
508 std::swap(m_start_y, rhs.m_start_y);
509 std::swap(m_width, rhs.m_width);
510 std::swap(m_height, rhs.m_height);
511 std::swap(m_window_timeout, rhs.m_window_timeout);
512 std::swap(m_color, rhs.m_color);
513 std::swap(m_bg_color, rhs.m_bg_color);
514 std::swap(m_base_color, rhs.m_base_color);
515 std::swap(m_base_bg_color, rhs.m_base_bg_color);
516 std::swap(m_border, rhs.m_border);
517 std::swap(m_get_string_helper, rhs.m_get_string_helper);
518 std::swap(m_title, rhs.m_title);
519 std::swap(m_color_stack, rhs.m_color_stack);
520 std::swap(m_input_queue, rhs.m_input_queue);
521 std::swap(m_fds, rhs.m_fds);
522 std::swap(m_bold_counter, rhs.m_bold_counter);
523 std::swap(m_underline_counter, rhs.m_underline_counter);
524 std::swap(m_reverse_counter, rhs.m_reverse_counter);
525 std::swap(m_alt_charset_counter, rhs.m_alt_charset_counter);
526 return *this;
529 Window::~Window()
531 delwin(m_window);
532 delwin(m_border_window);
535 void Window::setColor(Color fg, Color bg)
537 if (fg == Color::Default)
538 fg = m_base_color;
540 if (fg != Color::Default)
541 wattron(m_window, COLOR_PAIR(int(bg)*8+int(fg)));
542 else
543 wattroff(m_window, COLOR_PAIR(int(m_color)));
544 m_color = fg;
545 m_bg_color = bg;
548 void Window::setBaseColor(Color fg, Color bg)
550 m_base_color = fg;
551 m_base_bg_color = bg;
554 void Window::setBorder(Border border)
556 if (border == Border::None && m_border != Border::None)
558 delwin(m_border_window);
559 m_start_x--;
560 m_start_y--;
561 m_height += 2;
562 m_width += 2;
563 recreate(m_width, m_height);
565 else if (border != Border::None && m_border == Border::None)
567 m_border_window = newpad(m_height, m_width);
568 wattron(m_border_window, COLOR_PAIR(int(border)));
569 box(m_border_window,0,0);
570 m_start_x++;
571 m_start_y++;
572 m_height -= 2;
573 m_width -= 2;
574 recreate(m_width, m_height);
576 else
578 wattron(m_border_window,COLOR_PAIR(int(border)));
579 box(m_border_window, 0, 0);
581 m_border = border;
584 void Window::setTitle(const std::string &new_title)
586 if (m_title == new_title)
588 return;
590 else if (!new_title.empty() && m_title.empty())
592 m_start_y += 2;
593 m_height -= 2;
594 recreate(m_width, m_height);
596 else if (new_title.empty() && !m_title.empty())
598 m_start_y -= 2;
599 m_height += 2;
600 recreate(m_width, m_height);
602 m_title = new_title;
605 void Window::recreate(size_t width, size_t height)
607 delwin(m_window);
608 m_window = newpad(height, width);
609 setTimeout(m_window_timeout);
610 setColor(m_color, m_bg_color);
611 keypad(m_window, 1);
614 void Window::moveTo(size_t new_x, size_t new_y)
616 m_start_x = new_x;
617 m_start_y = new_y;
618 if (m_border != Border::None)
620 m_start_x++;
621 m_start_y++;
623 if (!m_title.empty())
624 m_start_y += 2;
627 void Window::adjustDimensions(size_t width, size_t height)
629 if (m_border != Border::None)
631 delwin(m_border_window);
632 m_border_window = newpad(height, width);
633 wattron(m_border_window, COLOR_PAIR(int(m_border)));
634 box(m_border_window, 0, 0);
635 width -= 2;
636 height -= 2;
638 if (!m_title.empty())
639 height -= 2;
640 m_height = height;
641 m_width = width;
644 void Window::resize(size_t new_width, size_t new_height)
646 adjustDimensions(new_width, new_height);
647 recreate(m_width, m_height);
650 void Window::showBorder() const
652 if (m_border != Border::None)
654 ::refresh();
655 prefresh(m_border_window, 0, 0, getStarty(), getStartX(), m_start_y+m_height, m_start_x+m_width);
657 if (!m_title.empty())
659 if (m_border != Border::None)
660 attron(COLOR_PAIR(int(m_border)));
661 else
662 attron(COLOR_PAIR(int(m_base_color)));
663 mvhline(m_start_y-1, m_start_x, 0, m_width);
664 attron(A_BOLD);
665 mvhline(m_start_y-2, m_start_x, 32, m_width); // clear title line
666 mvaddstr(m_start_y-2, m_start_x, m_title.c_str());
667 attroff(COLOR_PAIR(int(m_border)) | A_BOLD);
669 ::refresh();
672 void Window::display()
674 showBorder();
675 refresh();
678 void Window::refresh()
680 prefresh(m_window, 0, 0, m_start_y, m_start_x, m_start_y+m_height-1, m_start_x+m_width-1);
683 void Window::clear()
685 werase(m_window);
688 void Window::bold(bool bold_state) const
690 (bold_state ? wattron : wattroff)(m_window, A_BOLD);
693 void Window::underline(bool underline_state) const
695 (underline_state ? wattron : wattroff)(m_window, A_UNDERLINE);
698 void Window::reverse(bool reverse_state) const
700 (reverse_state ? wattron : wattroff)(m_window, A_REVERSE);
703 void Window::altCharset(bool altcharset_state) const
705 (altcharset_state ? wattron : wattroff)(m_window, A_ALTCHARSET);
708 void Window::setTimeout(int timeout)
710 if (timeout != m_window_timeout)
712 m_window_timeout = timeout;
713 wtimeout(m_window, timeout);
717 void Window::addFDCallback(int fd, void (*callback)())
719 m_fds.push_back(std::make_pair(fd, callback));
722 void Window::clearFDCallbacksList()
724 m_fds.clear();
727 bool Window::FDCallbacksListEmpty() const
729 return m_fds.empty();
732 int Window::readKey()
734 int result;
735 // if there are characters in input queue, get them and
736 // return immediately.
737 if (!m_input_queue.empty())
739 result = m_input_queue.front();
740 m_input_queue.pop();
741 return result;
743 // in pdcurses polling stdin doesn't work, so we can't poll
744 // both stdin and other file descriptors in one select. the
745 // workaround is to set the timeout of select to 0, poll
746 // other file descriptors and then wait for stdin input with
747 // the given timeout. unfortunately, this results in delays
748 // since ncmpcpp doesn't see that data arrived while waiting
749 // for input from stdin, but it seems there is no better option.
751 fd_set fdset;
752 FD_ZERO(&fdset);
753 # if !defined(USE_PDCURSES)
754 FD_SET(STDIN_FILENO, &fdset);
755 timeval timeout = { m_window_timeout/1000, (m_window_timeout%1000)*1000 };
756 # else
757 timeval timeout = { 0, 0 };
758 # endif
760 int fd_max = STDIN_FILENO;
761 for (FDCallbacks::const_iterator it = m_fds.begin(); it != m_fds.end(); ++it)
763 if (it->first > fd_max)
764 fd_max = it->first;
765 FD_SET(it->first, &fdset);
768 if (select(fd_max+1, &fdset, 0, 0, m_window_timeout < 0 ? 0 : &timeout) > 0)
770 # if !defined(USE_PDCURSES)
771 result = FD_ISSET(STDIN_FILENO, &fdset) ? wgetch(m_window) : ERR;
772 # endif // !USE_PDCURSES
773 for (FDCallbacks::const_iterator it = m_fds.begin(); it != m_fds.end(); ++it)
774 if (FD_ISSET(it->first, &fdset))
775 it->second();
777 # if !defined(USE_PDCURSES)
778 else
779 result = ERR;
780 # else
781 result = wgetch(m_window);
782 # endif
783 return result;
786 void Window::pushChar(int ch)
788 m_input_queue.push(ch);
791 std::string Window::getString(const std::string &base, size_t width, bool encrypted)
793 rl::w = this;
794 getyx(m_window, rl::start_y, rl::start_x);
795 rl::width = width;
796 rl::encrypted = encrypted;
797 rl::base = base.c_str();
799 width--;
800 if (width == size_t(-1))
801 rl::width = m_width-rl::start_x-1;
802 else
803 rl::width = width;
805 mmask_t oldmask;
806 std::string result;
808 curs_set(1);
809 keypad(m_window, 0);
810 mousemask(0, &oldmask);
811 char *input = readline(nullptr);
812 mousemask(oldmask, nullptr);
813 keypad(m_window, 1);
814 curs_set(0);
815 if (input != nullptr)
817 if (input[0] != 0)
818 add_history(input);
819 result = input;
820 free(input);
823 return result;
826 void Window::goToXY(int x, int y)
828 wmove(m_window, y, x);
831 int Window::getX()
833 return getcurx(m_window);
836 int Window::getY()
838 return getcury(m_window);
841 bool Window::hasCoords(int &x, int &y)
843 # ifndef USE_PDCURSES
844 return wmouse_trafo(m_window, &y, &x, 0);
845 # else
846 // wmouse_trafo is broken in pdcurses, use our own implementation
847 size_t u_x = x, u_y = y;
848 if (u_x >= m_start_x && u_x < m_start_x+m_width
849 && u_y >= m_start_y && u_y < m_start_y+m_height)
851 x -= m_start_x;
852 y -= m_start_y;
853 return true;
855 return false;
856 # endif
859 bool Window::runGetStringHelper(const char *arg, bool *done) const
861 if (m_get_string_helper)
863 bool continue_ = m_get_string_helper(arg);
864 if (done != nullptr)
865 *done = !continue_;
866 return true;
868 else
869 return false;
872 size_t Window::getWidth() const
874 if (m_border != Border::None)
875 return m_width+2;
876 else
877 return m_width;
880 size_t Window::getHeight() const
882 size_t height = m_height;
883 if (m_border != Border::None)
884 height += 2;
885 if (!m_title.empty())
886 height += 2;
887 return height;
890 size_t Window::getStartX() const
892 if (m_border != Border::None)
893 return m_start_x-1;
894 else
895 return m_start_x;
898 size_t Window::getStarty() const
900 size_t starty = m_start_y;
901 if (m_border != Border::None)
902 starty--;
903 if (!m_title.empty())
904 starty -= 2;
905 return starty;
908 const std::string &Window::getTitle() const
910 return m_title;
913 Color Window::getColor() const
915 return m_color;
918 Border Window::getBorder() const
920 return m_border;
923 int Window::getTimeout() const
925 return m_window_timeout;
928 void Window::scroll(Scroll where)
930 idlok(m_window, 1);
931 scrollok(m_window, 1);
932 switch (where)
934 case Scroll::Up:
935 wscrl(m_window, 1);
936 break;
937 case Scroll::Down:
938 wscrl(m_window, -1);
939 break;
940 case Scroll::PageUp:
941 wscrl(m_window, m_width);
942 break;
943 case Scroll::PageDown:
944 wscrl(m_window, -m_width);
945 break;
946 default:
947 break;
949 idlok(m_window, 0);
950 scrollok(m_window, 0);
954 Window &Window::operator<<(Colors colors)
956 if (colors.fg == Color::End || colors.bg == Color::End)
958 *this << Color::End;
959 return *this;
961 m_color_stack.push(colors);
962 setColor(colors.fg, colors.bg);
963 return *this;
966 Window &Window::operator<<(Color color)
968 switch (color)
970 case Color::Default:
971 while (!m_color_stack.empty())
972 m_color_stack.pop();
973 setColor(m_base_color, m_base_bg_color);
974 break;
975 case Color::End:
976 if (!m_color_stack.empty())
977 m_color_stack.pop();
978 if (!m_color_stack.empty())
979 setColor(m_color_stack.top().fg, m_color_stack.top().bg);
980 else
981 setColor(m_base_color, m_base_bg_color);
982 break;
983 default:
984 Color bg;
985 if (m_color_stack.empty())
986 bg = m_bg_color;
987 else
988 bg = m_color_stack.top().bg;
989 m_color_stack.push(Colors(color, bg));
990 setColor(m_color_stack.top().fg, m_color_stack.top().bg);
992 return *this;
995 Window &Window::operator<<(Format format)
997 switch (format)
999 case Format::None:
1000 bold((m_bold_counter = 0));
1001 reverse((m_reverse_counter = 0));
1002 altCharset((m_alt_charset_counter = 0));
1003 break;
1004 case Format::Bold:
1005 bold(++m_bold_counter);
1006 break;
1007 case Format::NoBold:
1008 if (--m_bold_counter <= 0)
1009 bold((m_bold_counter = 0));
1010 break;
1011 case Format::Underline:
1012 underline(++m_underline_counter);
1013 break;
1014 case Format::NoUnderline:
1015 if (--m_underline_counter <= 0)
1016 underline((m_underline_counter = 0));
1017 break;
1018 case Format::Reverse:
1019 reverse(++m_reverse_counter);
1020 break;
1021 case Format::NoReverse:
1022 if (--m_reverse_counter <= 0)
1023 reverse((m_reverse_counter = 0));
1024 break;
1025 case Format::AltCharset:
1026 altCharset(++m_alt_charset_counter);
1027 break;
1028 case Format::NoAltCharset:
1029 if (--m_alt_charset_counter <= 0)
1030 altCharset((m_alt_charset_counter = 0));
1031 break;
1033 return *this;
1036 Window &Window::operator<<(int (*f)(WINDOW *))
1038 f(m_window);
1039 return *this;
1042 Window &Window::operator<<(XY coords)
1044 goToXY(coords.x, coords.y);
1045 return *this;
1048 Window &Window::operator<<(const char *s)
1050 for (const char *c = s; *c != '\0'; ++c)
1051 wprintw(m_window, "%c", *c);
1052 return *this;
1055 Window &Window::operator<<(char c)
1057 wprintw(m_window, "%c", c);
1058 return *this;
1061 Window &Window::operator<<(const wchar_t *ws)
1063 for (const wchar_t *wc = ws; *wc != L'\0'; ++wc)
1064 wprintw(m_window, "%lc", *wc);
1065 return *this;
1068 Window &Window::operator<<(wchar_t wc)
1070 wprintw(m_window, "%lc", wc);
1071 return *this;
1074 Window &Window::operator<<(int i)
1076 wprintw(m_window, "%d", i);
1077 return *this;
1080 Window &Window::operator<<(double d)
1082 wprintw(m_window, "%f", d);
1083 return *this;
1086 Window &Window::operator<<(const std::string &s)
1088 // for some reason passing whole string at once with "%s" doesn't work
1089 // (string is cut in the middle, probably due to limitation of ncurses'
1090 // internal buffer?), so we need to pass characters in a loop.
1091 for (auto it = s.begin(); it != s.end(); ++it)
1092 wprintw(m_window, "%c", *it);
1093 return *this;
1096 Window &Window::operator<<(const std::wstring &ws)
1098 for (auto it = ws.begin(); it != ws.end(); ++it)
1099 wprintw(m_window, "%lc", *it);
1100 return *this;
1103 Window &Window::operator<<(size_t s)
1105 wprintw(m_window, "%zu", s);
1106 return *this;