cwidget 0.5.16
pager.h
00001 // pager.h       -*-c++-*-
00002 //
00003 //  Copyright 2000-2004 Daniel Burrows
00004 //
00005 //  A widget which acts as a text pager.
00006 
00007 #ifndef PAGER_H
00008 #define PAGER_H
00009 
00010 #include "widget.h"
00011 
00012 #include <string>
00013 #include <vector>
00014 
00015 class keybindings;
00016 
00017 namespace cwidget
00018 {
00019   namespace widgets
00020   {
00028     class pager : public widget
00029     {
00030     public:
00031       typedef std::vector<std::wstring>::size_type line_count;
00032       typedef int col_count;
00033     private:
00035       std::vector<std::wstring> lines;
00036 
00038       line_count first_line;
00039 
00041       col_count first_column;
00042 
00044       col_count text_width;
00045 
00047       std::wstring last_search;
00048 
00050       void layout_me();
00051 
00053       void search_omnidirectional_for(const std::wstring &s, bool forward);
00054 
00055     protected:
00056       pager(const char *text, int len, const char *encoding = NULL);
00057       pager(const std::string &s, const char *encoding = NULL);
00058       pager(const std::wstring &s);
00059 
00060     public:
00067       static util::ref_ptr<pager>
00068       create(const char *text, int len, const char *encoding = NULL)
00069       {
00070         util::ref_ptr<pager> rval(new pager(text, len, encoding));
00071         rval->decref();
00072         return rval;
00073       }
00074 
00080       static util::ref_ptr<pager>
00081       create(const std::string &s, const char *encoding = NULL)
00082       {
00083         util::ref_ptr<pager> rval(new pager(s, encoding));
00084         rval->decref();
00085         return rval;
00086       }
00087 
00092       static util::ref_ptr<pager>
00093       create (const std::wstring &s)
00094       {
00095         util::ref_ptr<pager> rval(new pager(s));
00096 
00097         rval->decref();
00098 
00099         return rval;
00100       }
00101 
00103       virtual ~pager();
00104 
00111       virtual void set_text(const char *text,
00112                             std::string::size_type len,
00113                             const char *encoding=NULL);
00114 
00120       virtual void set_text(const std::string &s, const char *encoding=NULL);
00121 
00126       virtual void set_text(const std::wstring &s);
00127 
00129       void scroll_up(line_count nlines);
00130 
00132       void scroll_down(line_count nlines);
00133 
00135       void scroll_right(col_count ncols);
00136 
00138       void scroll_left(col_count ncols);
00139 
00141       void scroll_top();
00142 
00144       void scroll_bottom();
00145 
00151       void scroll_page(bool dir);
00152 
00157       void search_for(const std::wstring &s)
00158       {
00159         search_omnidirectional_for(s, true);
00160       }
00161 
00166       void search_back_for(const std::wstring &s)
00167       {
00168         search_omnidirectional_for(s, false);
00169       }
00170 
00172       std::wstring get_last_search() {return last_search;}
00173 
00174       line_count get_first_line() {return first_line;}
00175       line_count get_num_lines() {return lines.size();}
00176       col_count get_first_column() {return first_column;}
00177       col_count get_num_columns() {return text_width;}
00178 
00182       void do_line_signal();
00183 
00187       void do_column_signal();
00188 
00189       virtual bool handle_key(const config::key &k);
00190       virtual void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
00191       virtual bool focus_me() {return true;}
00192       virtual void paint(const style &st);
00193 
00194       int width_request();
00195       int height_request(int w);
00196       bool get_cursorvisible() {return true;}
00197       point get_cursorloc() {return point(0,0);}
00198 
00200       sigc::signal2<void, int, int> line_changed;
00201 
00203       sigc::signal2<void, int, int> column_changed;
00204 
00205       static config::keybindings *bindings;
00206       static void init_bindings();
00207     };
00208 
00210     class file_pager:public pager
00211     {
00212     protected:
00213       file_pager();
00214       file_pager(const std::string &filename, const char *encoding = NULL);
00215       file_pager(const std::wstring &filename, const char *encoding = NULL);
00216 
00217       file_pager(const char *text, int len, const char *encoding = NULL);
00218     public:
00219       static util::ref_ptr<file_pager> create()
00220       {
00221         return new file_pager;
00222       }
00223 
00224       static util::ref_ptr<file_pager> create(const std::string &filename, const char *encoding=NULL)
00225       {
00226         return new file_pager(filename, encoding);
00227       }
00228 
00233       static util::ref_ptr<file_pager>
00234       create(const std::wstring &filename, const char *encoding=NULL)
00235       {
00236         return new file_pager(filename, encoding);
00237       }
00238 
00239       static util::ref_ptr<file_pager>
00240       create(const char *text, int len, const char *encoding=NULL)
00241       {
00242         return new file_pager(text, len, encoding);
00243       }
00244 
00251       void load_file(const std::string &filename, const char *encoding=NULL);
00252 
00260       void load_file(const std::wstring &filename, const char *encoding);
00261 
00269       void load_file(const std::wstring &filename);
00270     };
00271 
00272     typedef util::ref_ptr<pager> pager_ref;
00273     typedef util::ref_ptr<file_pager> file_pager_ref;
00274   }
00275 }
00276 
00277 #endif