cursorwindow.h

Go to the documentation of this file.
00001 #ifndef CURSOR_WINDOW_H_INCLUDED
00002 #define CURSOR_WINDOW_H_INCLUDED
00003 
00004 
00005 //
00006 // Copyright 2002, Lowell Boggs Jr.
00007 //
00008 // This file or directory, containing source code for a computer program,
00009 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00010 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00011 // source file without charge or obligation so long as you agree to
00012 // the following:
00013 //
00014 //  1.  You must indemnify Lowell Boggs against any and all financial
00015 //      obligations caused by its use, misuse, function, or malfunction.
00016 //      Further, you acknowledge that there is no warranty of any kind,
00017 //      whatsoever.
00018 //
00019 //  2.  You agree not to attempt to patent any portion of this original
00020 //      work -- though you may attempt to patent your own extensions to
00021 //      it if you so choose.
00022 //
00023 //  3.  You keep this copyright notice with the file and all copies
00024 //      of the file and do not change it anyway except language translation.
00025 //
00026 // You are responsible for enforcing your own compliance with these
00027 // conditions and may not use this source file if you cannot agree to the
00028 // above terms and conditions.
00029 
00041 
00042 #include <string>
00043 #include <vector>
00044 #include <string.h>
00045 #include <list>
00046 #include <map>
00047 #include <cxxtls/streamable.h>
00048 #include <memory>
00049 
00050 namespace cxxtls
00051 {
00052 
00053 class Viewer; // see viewer.h
00054 
00055 class CursorWindow
00056   //
00173   //
00174 : public Ostreamable<CursorWindow>
00175 {
00176     class   Window;
00177     friend class SaveRestoreActiveWindow;
00178 
00179 public:
00180 
00181     static int normal_att;        
00182     static int active_title_att;  
00183     static int inactive_title_att;
00184     static int active_mark_att;   
00185     static int inactive_mark_att; 
00186     static int bottom_att;        
00187     static int highlighted_att;   
00188     static int dialog_normal_att; 
00189     static int dialog_title_att;  
00190 
00191     class    viewport;
00192 
00193     friend class viewport;
00194 
00195     struct row_col
00196       //
00198       //
00199     {
00200       int row_;
00201       int col_;
00202 
00203       row_col(int row, int col) 
00204       : row_(row),
00205         col_(col)
00206       {
00207       }
00208 
00209       row_col()                 
00210       : row_(0),
00211         col_(0)
00212       {
00213       }
00214 
00215       bool operator==(row_col const &r)  
00216       {
00217         return row_ == r.row_ && col_ == r.col_;
00218       }
00219 
00220     };
00221 
00222     char const *error() const;
00223       
00228 
00229     void set_error(char const *);
00230       
00233 
00234     void clear_error() { set_error(0); }
00235       
00237 
00238     CursorWindow();
00239       
00241 
00242    ~CursorWindow();
00243       
00246 
00247     bool open();
00248 
00254 
00255     void close();
00261 
00262     void resize_viewports(row_col const &old_size, row_col const &new_size);
00269 
00270     void repaint_all();
00272 
00273     struct input_event
00274       
00292     {
00293       enum type_constants 
00294       {
00295         NoKey=0,          
00296         DataKey,          
00297         FunctionKey,      
00298         ResizeKey,        
00299 
00300 
00301         ForceExitKey,     
00302         MouseEvent
00303       };
00304 
00305       int type_;   
00306       int value_;  
00307 
00308 
00309 
00310 
00311       int row_;    
00312       int col_;    
00313 
00314       enum buttons
00315       {
00316         left  = 1,
00317         right = 2,
00318         middle= 4 // powers of two...
00319       };
00320 
00321       input_event(int type, int value, int row=0, int col=0)   
00322       : type_(type),
00323         value_(value),
00324         row_(row),
00325         col_(col)
00326       {
00327       }
00328 
00329       input_event()                      
00330       : type_(NoKey),
00331         value_(0),
00332         row_(0),
00333         col_(0)
00334       {
00335       }
00336 
00337     };
00338 
00339     input_event read_input();
00340       
00354 
00355 
00356     row_col curpos() const;
00358 
00359     void set_curpos(int row, int col)
00361     {
00362       set_curpos( row_col(row, col) );
00363     }
00364 
00365     void set_curpos(row_col rc);
00367 
00368     row_col size() const;
00369 
00374 
00375     void beep();
00377 
00378     void write(char const *, size_t length);
00379 
00382 
00383     void write(char const *s)
00387     {
00388        write(s, strlen(s));
00389     }
00390 
00391     void write(std::string const &s, size_t length=1000000)
00392       //
00394     {
00395       size_t actual_length = s.size();
00396 
00397       write(s.data(), actual_length < length ? actual_length : length );
00398     }
00399 
00400     void write(long c, int length=1);
00401 
00405 
00406 
00407     void fill_to_eol(char c=' ');
00410 
00411     void box(int width, 
00412              int height, 
00413              bool filled,
00414              char const *title_string, 
00415              unsigned length
00416             );
00427 
00428     void box(int width, int height, bool filled, char const *title=0)
00439     {
00440       box(width, height, filled, title, title ? strlen(title): 0);
00441     }
00442 
00443     void box(int width, int height, bool filled, std::string title)
00444       //
00454     {
00455       box(width, height, filled, title.data(), title.size());
00456     }
00457 
00458 
00459     void fill_to_eos(char c=' ');
00462 
00463     enum text_attributes
00464       //
00467     {
00468       normal,       
00469       reversed,     
00470       underlined,   
00471       blinking,     
00472       bold,         
00473       reverse_ul,   
00474       reverse_bold, 
00475       rev_ul_bold,  
00476       bold_ul       
00477     };
00478 
00479     int text_attribute() const;
00481 
00482     void set_text_attribute(int);
00484 
00485 
00486     enum key_names 
00487     {
00488        first_function_key=0x100,
00489        //< first the unshifted function keys
00490        key_f1,  key_f2,  key_f3, key_f4, key_f5, key_f6, key_f7, key_f8, key_f9, key_f10,
00491        key_f11, key_f12,
00492        key_up, key_down, key_left, key_right, key_home, key_end, key_prior, key_next,
00493        key_ic,  //< Insert key
00494        key_dc,  //< Delete key
00495        key_bs,  //< Backspace key
00496        key_btab,
00501        key_sf1,  key_sf2,  key_sf3, key_sf4, key_sf5, key_sf6, key_sf7, key_sf8, key_sf9, key_sf10,
00502        key_sf11, key_sf12,
00503        key_sup, key_sdown, key_sleft, key_sright, key_shome, key_send, key_sprior, key_snext,
00504        key_sic, key_sdc,
00505 
00506        last_function_key  
00507 
00508     };
00509 
00510     static std::string key_name(int keyvalue);
00511 
00512     enum edit_function_names
00535     {
00536       func_unknown, 
00537       func_up, func_down, func_left, func_right,
00538       func_ic,      
00539       func_dc,      
00540       func_home,    
00541       func_end,     
00542       func_prior,   
00543       func_next,    
00544       func_dc_prev, 
00545       func_tab,     
00546       func_btab,    
00547       func_top,     
00548       func_bottom,  
00549       func_esc,     
00550       func_enter,   
00551       func_data,    
00552       func_clreol,  
00553       func_nextwd,  
00554       func_prevwd,  
00555       func_upword,  
00556       func_dnword,  
00557       func_undo,    
00558       func_find,    
00559       func_findnxt, 
00560       func_findprv, 
00561       func_repl,    
00562       func_replnext,
00563       func_help,    
00564       func_mark,    
00565       func_dl,      
00566       func_db,      
00567       func_cb,      
00568       func_wb,      
00569       func_paste,   
00570       func_goto,    
00571       func_matching,
00572       func_dw,      
00573       func_join,    
00574       func_insline, 
00575       func_to_edit, 
00576       func_reread,  
00577       func_switch,  
00578       func_insfile, 
00579 
00580       user_function_0=0x100  
00581 
00582 
00583     };
00584 
00585     static std::string        func_name(int funcvalue);
00589 
00590     static std::map<int,int>  func;
00591 
00599 
00600     static std::string word;
00601 
00606 
00607 
00608     struct resize_handler
00627     {
00628       virtual void operator() (CursorWindow*)=0;
00629       virtual ~resize_handler();
00630     };
00631 
00632     void set_resize_handler(resize_handler *);
00636 
00637 
00638     class viewport
00639       //
00767       //
00768       : public Ostreamable<viewport>
00769     {
00770     public:
00771 
00772       struct repaint_handler
00773        //
00788 
00789       {
00790         virtual void operator() ( viewport * vp, int cmd ) = 0;
00791         virtual ~repaint_handler();
00792         
00793         enum cmd_constant
00796         {
00797           activate,     
00798           deactivate,   
00799           resize        
00800         };
00801       };
00802 
00803       enum position
00806       {
00807          tiled,   
00808 
00809          floating 
00810       };
00811 
00812       viewport(CursorWindow*, repaint_handler*, position p=tiled);
00814      ~viewport();                                
00815 
00816 
00817       viewport(viewport const &);                
00818 
00819 
00820 
00821                                                 
00822      private:
00823 
00824       viewport &operator= (viewport const &r);   
00825                                         
00826       public:
00827 
00828       void set_repaint_handler(repaint_handler *r);
00829 
00830       repaint_handler* get_repaint_handler();
00831 
00832 
00833       std::auto_ptr<viewport> vsplit(repaint_handler*);
00834 
00842         
00843       std::auto_ptr<viewport> hsplit(repaint_handler*);
00844 
00852 
00853       row_col curpos() const;
00854 
00856 
00857       void set_curpos(int row, int col)
00858         //
00861       {
00862         set_curpos( row_col(row, col) );
00863       }
00864 
00865       void set_curpos(row_col rc);
00868         
00869       void restore_curpos();
00872 
00873       row_col size() const;
00874 
00881         
00882       row_col origin() const;
00883 
00886 
00887       void beep();
00889         
00890       template<class Iterator>
00891       void write(Iterator first, Iterator last) 
00892       {
00893         while(first != last) write(*first++);
00894       }
00895         
00896       void write(char const *, size_t length);
00897 
00900 
00901       void write(char const *s)
00905       {
00906          write(s, strlen(s));
00907       }
00908 
00909       void write(std::string const &s, size_t length=1000000)
00910         //
00912       {
00913         size_t actual_length = s.size();
00914         
00915         write(s.data(), actual_length < length ? actual_length : length );
00916       }
00917 
00918       void write(long c, int length=1);
00922 
00923 
00924       void fill_to_eol(char c=' ');
00927 
00928       void box(int width, 
00929                int height, 
00930                bool filled,
00931                char const *title_string, 
00932                unsigned length
00933               );
00944       
00945       void box(int width, int height, bool filled, char const *title=0)
00946 
00947         
00948 
00949 
00950 
00951 
00952 
00953 
00954 
00955 
00956       {
00957         box(width, height, filled, title, title ? strlen(title): 0);
00958       }
00959       
00960       void box(int width, int height, bool filled, std::string title)
00961         //
00971       {
00972         box(width, height, filled, title.data(), title.size());
00973       }
00974 
00975       void fill_to_eos(char c=' ');
00976         //
00979 
00980       int text_attribute() const;
00981         //
00983 
00984       void set_text_attribute(int);
00986 
00987 
00988       void move(row_col where, row_col size);
00993         
00994       void activate();
00996 
00997       void refresh();
00998         //
01002 
01003 
01004       struct port;
01005 
01006       port* port_;  
01007 
01008 
01009       bool  owned_; 
01010 
01011 
01012 
01013 
01014       friend class  CursorWindow;
01015 
01016 
01017     public:
01018 
01019       viewport(port*);
01020 
01022 
01023       CursorWindow* window();
01025 
01026       void ostreamable_helper(char c)
01027       {
01028         write(&c, 1);
01029       }
01030 
01031     }; // end of class viewport
01032 
01033     std::auto_ptr<viewport> new_viewport(viewport::repaint_handler*,
01034                                 viewport::position p = viewport::tiled);
01035   
01047 
01048     void needs_resized();
01049     
01051 
01052 
01053     void refresh();
01054     
01058 
01059     class Dialog
01060       //
01079     : public viewport::repaint_handler
01080     {
01081     public:
01082       class Impl;
01083       friend class Impl;
01084 
01085     private:
01086 
01087       Impl *impl_;
01088 
01089     public:
01090 
01091       Dialog(std::string title);
01092        
01094         
01095         
01096      ~Dialog();
01097        
01099 
01100       void operator() ( viewport * vp, int cmd );
01101         
01103 
01104       bool popup(CursorWindow*);
01105 
01108         
01109       void set_completion_directory(std::string const &s);
01110 
01114         
01115       std::string const &completion_directory() const;
01116 
01120 
01121       void set_first_input_field(std::string const &s);
01122 
01125 
01126       class Element
01163       {
01164       protected:
01165 
01166         Dialog*     dialog_; //< when you append a dialog element to a dialog
01167                              //  this field gets set by the Dialog::operator+= method.
01168                         
01169         friend class Dialog;
01170         
01171       public:
01172 
01173         std::string name_;
01174         std::string label_;
01175         std::string value_;
01176         int         input_width_;
01177 
01178         Element(std::string const& name, std::string const &label, int width=0)
01179         : name_(name),
01180           label_(label),
01181           input_width_(width)
01182         {
01183         }
01184         
01185 
01186         virtual ~Element() {}
01187 
01188         virtual void draw_input_area(viewport *v, bool highlighted) = 0;
01189 
01195 
01196         virtual void handle_key(viewport *v, int key) = 0;
01197           
01202 
01203 
01204       };
01205 
01206       typedef std::list<Element*>  element_list_t;
01207       typedef element_list_t::iterator iterator;
01208 
01209     public:
01210 
01211       Dialog& operator += (Element *e);
01212 
01213       iterator begin();
01214       iterator end();
01215 
01216       class Ok
01217         //
01223         //
01224       : public Element
01225       {
01226       public:
01227 
01228         Ok(std::string const &name, std::string const &label)
01229         : Element(name, label, 4)
01230         {
01232         }
01233 
01234         void draw_input_area(viewport *v, bool highlighted);
01236 
01237         void handle_key(viewport*v, int key);
01239       };
01240 
01241       class String
01242         //
01256         //
01257       : public Element
01258       {
01259         int col_;     
01260 
01261 
01262         int offset_;  
01263 
01264 
01265         int width_;   
01266 
01267         int input_;   
01268 
01269 
01270 
01271       protected:
01272 
01273         bool password_;  
01274 
01275       public:
01276 
01277         String(std::string const &name,
01278                std::string const &label,
01279                std::string const &default_value,
01280                int                input_width)
01281         : Element(name, label, input_width),
01282           col_(0),
01283           offset_(0),
01284           width_(input_width),
01285           input_(0),
01286           password_(false)
01287         {
01289         
01290           value_ = default_value;
01291         }
01292 
01293         void draw_input_area(viewport *v, bool highlighted);
01295 
01296         void handle_key(viewport*v, int key);
01298         
01299         bool compute_actual_width(viewport *v);
01304         
01305         void handle_paging(viewport *v);
01309 
01310         void setColumn(int col);
01312 
01313       };
01314 
01315       class Password
01316        //
01319       : public String
01320       {
01321       public:
01322 
01323         Password(std::string const &name,
01324                  std::string const &label,
01325                  std::string const &default_value,
01326                  int                input_width)
01327         : String(name, label, default_value, input_width)
01328         {
01329           password_ = true;
01330         }
01331       };
01332 
01333       std::string element_value(std::string const &name);
01339     };
01340 
01341     class Selection
01342       //
01368     : public viewport::repaint_handler
01369     {
01370     public:
01371 
01372       class   Impl;
01373       typedef std::list<std::string> rep_type;  
01374 
01375     private:
01376 
01377       Impl* impl_;                              
01378 
01379 
01380     public:
01381 
01382       rep_type rep_;                            
01383 
01384 
01385 
01386 
01387 
01388       std::string selection_;                   
01389 
01390 
01391 
01392 
01393 
01394       Selection(std::string title);
01396         
01397         
01398      ~Selection();
01400 
01401       void operator() ( viewport * vp, int cmd );
01402 
01406 
01407       bool popup(CursorWindow*);
01408 
01412         //
01413         
01414       void operator+= (std::string const &s)
01415       {
01416 
01418         //
01419         rep_.push_back(s);
01420       }
01421         
01422     };
01423 
01424 
01425     class Message
01426       //
01455     : public viewport::repaint_handler
01456     {
01457     public:
01458 
01459       class   Impl;
01460 
01461       typedef std::list<std::string> rep_type;  
01462 
01463     private:
01464 
01465       Impl* impl_;                              
01466 
01467 
01468     public:
01469 
01470       rep_type rep_;                            
01471 
01472 
01473 
01474 
01475 
01476       Message(std::string title);
01478         
01479      ~Message();
01481 
01482       void operator() ( viewport * vp, int cmd );
01483   
01487 
01488       void popup(CursorWindow*);
01489   
01494         //
01495         
01496       void operator+= (std::string const &s)
01497       {
01498   
01501   
01502         rep_.push_back(s);
01503       }
01504         
01505     };
01506 
01507 
01508     class FileSelector
01509     : public viewport::repaint_handler
01510     {
01511 
01512     public:
01513 
01514        FileSelector(std::string const &title,
01515                     std::string const &filenamePattern,
01516                     std::string const &startFileName
01517                    );
01531       
01532       void operator() ( viewport * vp, int cmd );
01536 
01537       std::string popup(CursorWindow*window, Viewer *parent);
01552 
01553     private:
01554 
01555         std::string title_;
01556         std::string searchPattern_;
01557         std::string searchDir_;             // populated by readFileInfo
01558         std::string startFile_;
01559 
01560         std::vector<std::string> rowText_;  // names and perhaps other info
01561         size_t maxNameWidth_;               // longest string in rowText_;
01562 
01563         int row_;               // screen relative position of dialog (top row)
01564         int col_;               // left coloumn
01565         int dialogHeight_;      // in rows
01566         int dialogWidth_;       // in cols
01567 
01568 
01569         int displayAreaRow_;    // top of display area
01570         int displayAreaCol_;    // left of display area
01571         int displayAreaHeight_;
01572         int displayAreaWidth_;  
01573 
01574         int inputIndex_;     // into rowText_
01575         int inputTopIndex_;  // index into rowText_ of top item displayed in the display area
01576 
01577         bool noRepaint_;     // true only in minute instants when popup is still running
01578                              // but we want to ignore calls to operator()()
01579 
01580 
01581         void adaptToWindow(viewport *); // recalculate display coordinates and active
01582                                         // window regions based on actual window sizes
01583 
01584         void paintDialogBackground(viewport *); // paint the dialog region in the appropriate colors
01585 
01586         void readFileInfo();
01587 
01588         std::string handleInput(CursorWindow *w, Viewer *parent);
01589                                               
01590         void paintRow(CursorWindow *w, int displayAreaRow);  // repaint current specified row
01591 
01592         void eraseMe(CursorWindow *w);   // remove this dialog from the screen
01593 
01594 
01595     };
01596 
01597     // bogus do not use!
01598     static std::string select_file(std::string const &title,
01599                                    std::string const &dir_file_pattern,
01600                                    CursorWindow *w
01601                                   );
01602 
01603     void ostreamable_helper(char c)
01604       //
01609       //
01610     {
01611       write(&c, 1);
01612     }
01613 
01614 
01615 
01616 
01617     int get_viewports(std::list<viewport> *viewports);
01618 
01624 
01625 private:
01626 
01627                         
01628     Window * window_; 
01629 
01630     void destroy_viewport(viewport*);
01631 
01632 
01633 }; // CursorWindow 
01634 } // namespace cxxtls
01635 
01636 #endif
Generated on Wed Feb 29 22:50:03 2012 for CXXUtilities by  doxygen 1.6.3