cwidget  0.5.16
pager.h
1 // pager.h -*-c++-*-
2 //
3 // Copyright 2000-2004 Daniel Burrows
4 //
5 // A widget which acts as a text pager.
6 
7 #ifndef PAGER_H
8 #define PAGER_H
9 
10 #include "widget.h"
11 
12 #include <string>
13 #include <vector>
14 
15 class keybindings;
16 
17 namespace cwidget
18 {
19  namespace widgets
20  {
28  class pager : public widget
29  {
30  public:
31  typedef std::vector<std::wstring>::size_type line_count;
32  typedef int col_count;
33  private:
35  std::vector<std::wstring> lines;
36 
38  line_count first_line;
39 
41  col_count first_column;
42 
44  col_count text_width;
45 
47  std::wstring last_search;
48 
50  void layout_me();
51 
53  void search_omnidirectional_for(const std::wstring &s, bool forward);
54 
55  protected:
56  pager(const char *text, int len, const char *encoding = NULL);
57  pager(const std::string &s, const char *encoding = NULL);
58  pager(const std::wstring &s);
59 
60  public:
68  create(const char *text, int len, const char *encoding = NULL)
69  {
70  util::ref_ptr<pager> rval(new pager(text, len, encoding));
71  rval->decref();
72  return rval;
73  }
74 
81  create(const std::string &s, const char *encoding = NULL)
82  {
83  util::ref_ptr<pager> rval(new pager(s, encoding));
84  rval->decref();
85  return rval;
86  }
87 
93  create (const std::wstring &s)
94  {
95  util::ref_ptr<pager> rval(new pager(s));
96 
97  rval->decref();
98 
99  return rval;
100  }
101 
103  virtual ~pager();
104 
111  virtual void set_text(const char *text,
112  std::string::size_type len,
113  const char *encoding=NULL);
114 
120  virtual void set_text(const std::string &s, const char *encoding=NULL);
121 
126  virtual void set_text(const std::wstring &s);
127 
129  void scroll_up(line_count nlines);
130 
132  void scroll_down(line_count nlines);
133 
135  void scroll_right(col_count ncols);
136 
138  void scroll_left(col_count ncols);
139 
141  void scroll_top();
142 
144  void scroll_bottom();
145 
151  void scroll_page(bool dir);
152 
157  void search_for(const std::wstring &s)
158  {
159  search_omnidirectional_for(s, true);
160  }
161 
166  void search_back_for(const std::wstring &s)
167  {
168  search_omnidirectional_for(s, false);
169  }
170 
172  std::wstring get_last_search() {return last_search;}
173 
174  line_count get_first_line() {return first_line;}
175  line_count get_num_lines() {return lines.size();}
176  col_count get_first_column() {return first_column;}
177  col_count get_num_columns() {return text_width;}
178 
182  void do_line_signal();
183 
187  void do_column_signal();
188 
189  virtual bool handle_key(const config::key &k);
190  virtual void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
191  virtual bool focus_me() {return true;}
192  virtual void paint(const style &st);
193 
194  int width_request();
195  int height_request(int w);
196  bool get_cursorvisible() {return true;}
197  point get_cursorloc() {return point(0,0);}
198 
200  sigc::signal2<void, int, int> line_changed;
201 
203  sigc::signal2<void, int, int> column_changed;
204 
205  static config::keybindings *bindings;
206  static void init_bindings();
207  };
208 
210  class file_pager:public pager
211  {
212  protected:
213  file_pager();
214  file_pager(const std::string &filename, const char *encoding = NULL);
215  file_pager(const std::wstring &filename, const char *encoding = NULL);
216 
217  file_pager(const char *text, int len, const char *encoding = NULL);
218  public:
219  static util::ref_ptr<file_pager> create()
220  {
221  return new file_pager;
222  }
223 
224  static util::ref_ptr<file_pager> create(const std::string &filename, const char *encoding=NULL)
225  {
226  return new file_pager(filename, encoding);
227  }
228 
234  create(const std::wstring &filename, const char *encoding=NULL)
235  {
236  return new file_pager(filename, encoding);
237  }
238 
240  create(const char *text, int len, const char *encoding=NULL)
241  {
242  return new file_pager(text, len, encoding);
243  }
244 
251  void load_file(const std::string &filename, const char *encoding=NULL);
252 
260  void load_file(const std::wstring &filename, const char *encoding);
261 
269  void load_file(const std::wstring &filename);
270  };
271 
274  }
275 }
276 
277 #endif