cwidget 0.5.18
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
15class keybindings;
16
17namespace 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
Stores the keys bound to various functions.
Definition keybindings.h:88
Definition ref_ptr.h:20
Load a file from disk; it's assumed to be ASCII for now.
Definition pager.h:211
void load_file(const std::string &filename, const char *encoding=NULL)
Loads the given file into the pager.
void load_file(const std::wstring &filename, const char *encoding)
Attempts to convert the string to a multibyte representation and then load it; a nonconvertible strin...
void load_file(const std::wstring &filename)
Attempts to convert the string to a multibyte representation and then load it; a nonconvertible strin...
static util::ref_ptr< file_pager > create(const std::wstring &filename, const char *encoding=NULL)
Attempts to convert the string to a multibyte representation and then load it; a nonconvertible strin...
Definition pager.h:234
A widget that displays text.
Definition pager.h:29
static util::ref_ptr< pager > create(const char *text, int len, const char *encoding=NULL)
Create a pager from the given memory region.
Definition pager.h:68
virtual void set_text(const char *text, std::string::size_type len, const char *encoding=NULL)
Set the text to the given memory region.
std::wstring get_last_search()
Return the last string which the user searched for.
Definition pager.h:172
virtual void set_text(const std::string &s, const char *encoding=NULL)
Change the displayed text.
void scroll_down(line_count nlines)
Scroll the screen down by the given number of lines.
Definition pager.cc:171
void scroll_right(col_count ncols)
Scroll the screen right by the given number of columns.
Definition pager.cc:194
static util::ref_ptr< pager > create(const std::string &s, const char *encoding=NULL)
Create a pager from a string.
Definition pager.h:81
virtual void set_text(const std::wstring &s)
Change the displayed text.
void scroll_top()
Scroll to the top of the screen.
Definition pager.cc:204
sigc::signal2< void, int, int > line_changed
Announces that the user has scrolled vertically.
Definition pager.h:200
void scroll_bottom()
Scroll to the bottom of the screen.
Definition pager.cc:214
int height_request(int w)
Calculate the desired height of the widget, given its width.
Definition pager.cc:383
static util::ref_ptr< pager > create(const std::wstring &s)
Create a pager from a wide character string.
Definition pager.h:93
sigc::signal2< void, int, int > column_changed
Announces that the user has scrolled horizontally.
Definition pager.h:203
void scroll_up(line_count nlines)
Scroll the screen up by the given number of lines.
Definition pager.cc:158
void do_column_signal()
Emits a signal describing the horizontal location of the display within the text.
Definition pager.cc:150
void scroll_left(col_count ncols)
Scroll the screen left by the given number of columns.
Definition pager.cc:181
virtual ~pager()
Destroy this pager.
Definition pager.cc:71
void scroll_page(bool dir)
Scroll by a page in the given direction.
Definition pager.cc:224
void search_back_for(const std::wstring &s)
Find the previous line containing the given string.
Definition pager.h:166
virtual bool handle_key(const config::key &k)
Handles a keypress in this widget.
Definition pager.cc:291
void do_line_signal()
Emits a signal describing the verical location of the display within the text.
Definition pager.cc:142
int width_request()
Definition pager.cc:378
void search_for(const std::wstring &s)
Find the next line containing the given string.
Definition pager.h:157
virtual void paint(const style &st)
Display this widget.
Definition pager.cc:344
The basic widget interface.
Definition widget.h:107
The namespace containing everything defined by cwidget.
Definition columnify.cc:28