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 bool focus_me() {return true;}
00191       virtual void paint(const style &st);
00192 
00193       int width_request();
00194       int height_request(int w);
00195       bool get_cursorvisible() {return true;}
00196       point get_cursorloc() {return point(0,0);}
00197 
00199       sigc::signal2<void, int, int> line_changed;
00200 
00202       sigc::signal2<void, int, int> column_changed;
00203 
00204       static config::keybindings *bindings;
00205       static void init_bindings();
00206     };
00207 
00209     class file_pager:public pager
00210     {
00211     protected:
00212       file_pager();
00213       file_pager(const std::string &filename, const char *encoding = NULL);
00214       file_pager(const std::wstring &filename, const char *encoding = NULL);
00215 
00216       file_pager(const char *text, int len, const char *encoding = NULL);
00217     public:
00218       static util::ref_ptr<file_pager> create()
00219       {
00220         return new file_pager;
00221       }
00222 
00223       static util::ref_ptr<file_pager> create(const std::string &filename, const char *encoding=NULL)
00224       {
00225         return new file_pager(filename, encoding);
00226       }
00227 
00232       static util::ref_ptr<file_pager>
00233       create(const std::wstring &filename, const char *encoding=NULL)
00234       {
00235         return new file_pager(filename, encoding);
00236       }
00237 
00238       static util::ref_ptr<file_pager>
00239       create(const char *text, int len, const char *encoding=NULL)
00240       {
00241         return new file_pager(text, len, encoding);
00242       }
00243 
00250       void load_file(const std::string &filename, const char *encoding=NULL);
00251 
00259       void load_file(const std::wstring &filename, const char *encoding);
00260 
00268       void load_file(const std::wstring &filename);
00269     };
00270 
00271     typedef util::ref_ptr<pager> pager_ref;
00272     typedef util::ref_ptr<file_pager> file_pager_ref;
00273   }
00274 }
00275 
00276 #endif

Generated on Fri Nov 16 03:33:05 2007 for cwidget by  doxygen 1.5.1