Goby v2
chat_curses.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include <cctype>
23 #include <sstream>
24 #include <string>
25 
26 #include <boost/lexical_cast.hpp>
27 
28 #include "chat_curses.h"
29 
31 {
32  initscr();
33  start_color();
34 
35  refresh();
36  update_size();
37  keypad(stdscr, TRUE);
38  curs_set(false);
39 
40  // tenths of seconds pause waiting for character input (getch)
41  nodelay(stdscr, TRUE);
42  halfdelay(1);
43 
44  noecho();
45 }
46 
47 void ChatCurses::update_size()
48 {
49  getmaxyx(stdscr, ymax_, xmax_);
50 
51  delwin(upper_win_);
52  delwin(lower_win_);
53  delwin(divider_win_);
54 
55  upper_win_ = newwin(ymax_ * UPPER_WIN_FRAC - 1, xmax_, 0, 0);
56  divider_win_ = newwin(1, xmax_, ymax_ * UPPER_WIN_FRAC - 1, 0);
57  lower_win_ = newwin(ymax_ * LOWER_WIN_FRAC, xmax_, ymax_ * UPPER_WIN_FRAC, 0);
58 
59  scrollok(upper_win_, true);
60 
61  mvwhline(divider_win_, 0, 0, 0, xmax_);
62  wrefresh(divider_win_);
63 }
64 
65 void ChatCurses::run_input(std::string& line)
66 {
67  chtype k = getch();
68 
69  if (k == KEY_DC || k == KEY_BACKSPACE)
70  {
71  if (!line_buffer_.empty())
72  line_buffer_.resize(line_buffer_.size() - 1);
73  wclear(lower_win_);
74  waddstr(lower_win_, line_buffer_.c_str());
75  }
76  else if ((k == KEY_ENTER || k == '\n') && !line_buffer_.empty())
77  {
78  line = line_buffer_;
79  wclear(lower_win_);
80  post_message(id_, line_buffer_);
81  line_buffer_.clear();
82  }
83  else if (k == KEY_RESIZE)
84  {
85  update_size();
86  }
87  else if (isprint(k) && line_buffer_.length() < MAX_LINE)
88  {
89  waddch(lower_win_, k);
90  line_buffer_ += k;
91  }
92 
93  wrefresh(lower_win_);
94 }
95 
96 void ChatCurses::cleanup() { endwin(); }
97 
98 void ChatCurses::post_message(unsigned id, const std::string& line)
99 {
100  std::stringstream ss;
101  ss << "[" << id << "]: " << line;
102  post_message(ss.str());
103 }
104 
105 void ChatCurses::post_message(const std::string& line)
106 {
107  std::string line_plus_end = line + "\n";
108  waddstr(upper_win_, line_plus_end.c_str());
109  wrefresh(upper_win_);
110 }
void run_input(std::string &line)
grab a character and if there&#39;s a line to return it will be returned in line
Definition: chat_curses.cpp:65
void startup()
start the display
Definition: chat_curses.cpp:30
void post_message(unsigned id, const std::string &line)
add a message to the upper window (the chat log)
Definition: chat_curses.cpp:98
void cleanup()
end the display
Definition: chat_curses.cpp:96