Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
util/liblogger/flex_ncurses.h
00001 // copyright 2009 t. schneider tes@mit.edu
00002 //
00003 // this file is part of flex-ostream, a terminal display library
00004 // that provides an ostream with both terminal display and file logging
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This software is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #ifndef FlexNCurses20091110H
00020 #define FlexNCurses20091110H
00021 
00022 #include <vector>
00023 #include <deque>
00024 #include <string>
00025 
00026 #include <boost/thread.hpp>
00027 
00028 #include "term_color.h"
00029 #include "goby/util/time.h"
00030 
00031 class Group;
00032 
00033 namespace goby
00034 {
00035     namespace util
00036     {
00038         class FlexNCurses
00039         {
00040           public:
00041 
00043 
00044             FlexNCurses();
00045             ~FlexNCurses()
00046             {
00047                 alive_ = false;
00048                 cleanup();
00049             }
00051 
00053 
00054             void startup();
00055             void add_win(const Group* title);
00057 
00059 
00060 
00061             void insert(boost::posix_time::ptime t, const std::string& s, Group* g);
00063             
00065 
00066             size_t panel_from_group(Group* g);
00067             void recalculate_win();
00068             void cleanup();
00069             void alive(bool alive);
00071             
00073 
00074 
00075             void run_input();
00077     
00078           private:
00079             class Panel;
00080             void putline(const std::string &s, unsigned scrn, bool refresh = true);
00081 
00082             void putlines(unsigned scrn,
00083                           const std::multimap<boost::posix_time::ptime, std::string>::const_iterator& alpha,
00084                           const std::multimap<boost::posix_time::ptime, std::string>::const_iterator& omega,
00085                           bool refresh = true);
00086     
00087  
00088             void update_size();
00089             long color2attr_t(Colors::Color c);
00090             size_t find_containing_window(int y, int x);
00091     
00092             // void* is WINDOW*
00093             bool in_window(void* p, int y, int x);
00094     
00095             void write_head_title(size_t i);
00096             void deselect_all();    
00097             void select_all();
00098             void select(size_t gt);
00099             void home();
00100             void end();
00101 
00102             size_t down() { return down(find_first_selected()); }
00103             size_t up() { return up(find_first_selected()); }
00104             size_t left() { return left(find_first_selected()); }
00105             size_t right() { return right(find_first_selected()); }        
00106             size_t down(size_t curr);
00107             size_t up(size_t curr);
00108             size_t left(size_t curr);
00109             size_t right(size_t curr);
00110             size_t find_first_selected();
00111             void shift(size_t next);
00112 
00113             bool last_in_col(size_t val);
00114             bool first_in_col(size_t val);
00115     
00116             void move_up(); 
00117             void move_down();
00118             void move_right();
00119             void move_left();
00120 
00121             void combine();
00122             void uncombine_all();
00123             void uncombine(size_t i); 
00124             void uncombine_selected(); 
00125             int lines_from_beg(int l, size_t i);
00126   
00127             void scroll_up(); 
00128             void scroll_down();
00129             void page_up(); 
00130             void page_down();
00131             void scroll_end(); 
00132             void scroll_home();
00133     
00134             void winlock(); 
00135             void winunlock();
00136 
00137             void grow_all();
00138             void shrink_all();
00139             void grow(int i); 
00140             void shrink(int i);
00141 
00142             void restore_order();
00143     
00144     
00145             void toggle_minimized(int i);
00146             void redraw_lines(int j, int offset = -1 );
00147     
00148             std::multimap<boost::posix_time::ptime, std::string> get_history(size_t i, int how_much = -1);
00149             size_t get_history_size(size_t i);
00150     
00151     
00152             template<typename T>
00153                 T min(const T& a, const T& b)
00154             { return (a < b ? a : b); }    
00155 
00156             template<typename T>
00157                 T max(const T& a, const T& b)
00158             { return (a > b ? a : b); }    
00159     
00160           private:
00161             int xmax_;
00162             int ymax_;
00163 
00164             int xwinN_;
00165             int ywinN_;
00166     
00167             int last_select_x_;
00168             int last_select_y_;
00169     
00170             // void* is WINDOW*
00171             std::vector<void* > vert_windows_;
00172             // bottom of the column (indexed by column)
00173             std::vector<void* > col_end_windows_;
00174             void* foot_window_;
00175 
00176             bool is_locked_;
00177             int locked_panel_;
00178     
00179             class Panel
00180             {
00181               public:
00182               Panel(const Group* group = 0)
00183                   : group_(group),
00184                     window_(0),
00185                     head_window_(0),
00186                     minimized_(false),
00187                     selected_(false),
00188                     locked_(false),
00189                     col_(1),
00190                     lines_from_beg_(0),
00191                     original_order_(0),
00192                     max_hist_(20000)
00193                     {}
00194 
00195                 void window(void* v) { window_ = v; }
00196                 void head_window(void* v) { head_window_ = v; }
00197                 void group(const Group* g) { group_ = g; }
00198                 // returns number of lines to grow/shrink
00199                 void set_minimized(bool b) { minimized_ = b; } 
00200                 int minimized(bool b);        
00201                 int toggle_minimized() { return minimized(minimized() ^ true); }
00202                 void selected(bool b) { selected_ = b; }
00203                 void ywidth(int i) { ywidth_ = i; }
00204                 int lines_from_beg(int i);
00205                 void grow() { ++ywidth_; }
00206                 void locked(bool b)  { locked_ = b; }
00207                 bool shrink()
00208                 {
00209                     if(ywidth_ == HEAD_Y)
00210                         return false;
00211                     else
00212                     {
00213                         --ywidth_;
00214                         return true;
00215                     }
00216                 }        
00217                 void col(int i) { col_ = i; }
00218                 void original_order(int i) { original_order_ = i; }
00219                 void add_combined(size_t i) { combined_.insert(i); }
00220                 void clear_combined() { combined_.clear(); }        
00221         
00222                 void* window() const { return window_; }
00223                 void* head_window() const { return head_window_; }
00224                 const Group* group() const { return group_; }
00225                 bool minimized() const { return minimized_; }
00226                 bool selected() const { return selected_; }
00227                 int ywidth() const { return ywidth_; }
00228                 int col() const { return col_; }
00229                 bool locked() const { return locked_; }
00230                 int lines_from_beg() const { return lines_from_beg_; }        
00231                 int original_order() const { return original_order_; }
00232                 std::multimap<boost::posix_time::ptime, std::string>& history() { return history_; }
00233                 unsigned max_hist() const { return max_hist_; }
00234                 const std::set<size_t>& combined() const { return combined_; }
00235         
00236               private:
00237                 const Group* group_;
00238                 void* window_;
00239                 void* head_window_;
00240                 bool minimized_;
00241                 bool selected_;
00242                 bool locked_;
00243                 int ywidth_;
00244                 int unminimized_ywidth_;
00245                 int col_;
00246                 int lines_from_beg_;
00247                 int original_order_;
00248                 unsigned max_hist_;
00249                 std::multimap<boost::posix_time::ptime, std::string> history_;
00250                 std::set<size_t> combined_;
00251             };    
00252 
00253             std::vector<Panel> panels_;
00254             std::set<size_t> unique_panels_;
00255     
00256             // extra lines if everyone is minimized
00257             // indexed by column
00258             std::vector<int> line_buffer_;
00259     
00260             bool alive_;
00261     
00262             // ideal number of characters per line
00263             enum { CHARS_PER_LINE = 85 };
00264 
00265             enum { HEAD_Y = 1 };
00266             enum { FOOTER_Y = 3 }; 
00267     
00268         };
00269     }
00270 }
00271 
00272 
00273 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends