cwidget 0.5.18
editline.h
1// editline.h -*-c++-*-
2//
3// Copyright (C) 2000, 2007 Daniel Burrows
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License as
7// published by the Free Software Foundation; either version 2 of
8// the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; see the file COPYING. If not, write to
17// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18// Boston, MA 02111-1307, USA.
19
20// A simple line-editor widget.
21
22#ifndef EDITLINE_H
23#define EDITLINE_H
24
25#include "widget.h"
26
27#include <vector>
28
29namespace cwidget
30{
31 namespace config
32 {
33 class keybindings;
34 }
35
36 namespace widgets
37 {
38 class editline : public widget
39 {
40 public:
41 typedef std::vector<std::wstring> history_list;
42 private:
43
44 std::wstring prompt;
45 std::wstring text;
46
47 std::wstring pre_history_text;
48 // Used as a "virtual" history entry.
49
50 // curloc is the offset of the cursor within the input string;
51 // startloc is the offset of the first visible character within the
52 // combined input string and prompt.
53 //
54 // If allow_wrap is true, startloc is the first character of the
55 // first visible line (i.e., it represents vertical scrolling).
56 // Otherwise, it is the first character within the sole line that is
57 // visible (i.e., it represents horizontal scrolling).
58 std::wstring::size_type curloc, startloc;
59
60 int desired_size;
61
62 // The history of the edit-line. (if NULL, there is no history)
63 history_list *history;
64 // The current location in the history
65 history_list::size_type history_loc;
66 // True if we're flipping through the history. (to avoid signedness
67 // problems)
68 bool using_history;
69
70 bool allow_wrap;
71
79 bool clear_on_first_edit;
80
81 void normalize_cursor();
82
86 int get_line_of_character(size_t n, int width);
87
91 int get_character_of_line(size_t n, int width);
92
99 wchar_t get_char(size_t n);
100
102 size_t get_num_chars() const { return prompt.size() + text.size(); }
103 protected:
104 bool handle_key(const config::key &k);
105
106 editline(const std::wstring &_prompt,
107 const std::wstring &_text=L"",
108 history_list *history=NULL);
109
111 editline(const std::string &_prompt,
112 const std::string &_text="",
113 history_list *history=NULL);
114
115 editline(int maxlength, const std::wstring &_prompt,
116 const std::wstring &_text, history_list *history);
117
119 editline(int maxlength, const std::string &_prompt,
120 const std::string &_text, history_list *history);
121
122 public:
124 create(const std::wstring &prompt, const std::wstring &text = L"",
125 history_list *history = NULL)
126 {
127 util::ref_ptr<editline> rval(new editline(prompt, text, history));
128 rval->decref();
129 return rval;
130 }
131
133 create(const std::string &prompt, const std::string &text = "",
134 history_list *history = NULL)
135 {
136 util::ref_ptr<editline> rval(new editline(prompt, text, history));
137 rval->decref();
138 return rval;
139 }
140
142 create(int maxlength, const std::wstring &prompt,
143 const std::wstring &text = L"", history_list *history = NULL)
144 {
145 util::ref_ptr<editline> rval(new editline(maxlength, prompt, text, history));
146 rval->decref();
147 return rval;
148 }
149
150 static util::ref_ptr<editline>
151 create(int maxlength, const std::string &prompt,
152 const std::string &text = "", history_list *history = NULL)
153 {
154 util::ref_ptr<editline> rval(new editline(maxlength, prompt, text, history));
155 rval->decref();
156 return rval;
157 }
158
163 bool get_clear_on_first_edit() const { return clear_on_first_edit; }
169 void set_clear_on_first_edit(bool value)
170 {
171 clear_on_first_edit = value;
172 }
173
174 void set_allow_wrap(bool allow) { allow_wrap = allow; }
175 bool get_allow_wrap() const { return allow_wrap; }
176
177 bool focus_me();
178 void paint(const style &st);
179 void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
180
181 sigc::signal1<void, std::wstring> entered;
182 // Called when the user presses Enter to confirm the text
183 sigc::signal1<void, std::wstring> text_changed;
184 // Called when the text is altered.
185
186 std::wstring get_text() {return text;}
187 void set_text(std::wstring _text);
188
192 void set_text(std::string _text);
193
194 bool get_cursorvisible();
195 point get_cursorloc();
196
197 int width_request();
198 int height_request(int height);
199
200 static void add_to_history(std::wstring s,
201 history_list *history);
202 // Appends the string to the end of the history list (convenience routine)
203
204 void add_to_history(std::wstring s);
205 void reset_history();
206
207 static config::keybindings *bindings;
208 static void init_bindings();
209 };
210
212 }
213}
214
215#endif
Stores the keys bound to various functions.
Definition: keybindings.h:88
Definition: ref_ptr.h:20
Definition: editline.h:39
bool get_clear_on_first_edit() const
Definition: editline.h:163
void set_clear_on_first_edit(bool value)
Change whether the contents of this edit-line widget will be erased if the first character pressed is...
Definition: editline.h:169
bool handle_key(const config::key &k)
Handles a keypress in this widget.
Definition: editline.cc:238
void set_text(std::string _text)
Decodes the given multibyte string, and sets the current text of this edit-line to it.
int height_request(int height)
Calculate the desired height of the widget, given its width.
Definition: editline.cc:638
editline(const std::string &_prompt, const std::string &_text="", history_list *history=NULL)
Transcodes its input strings from the system charset.
int width_request()
Definition: editline.cc:628
void paint(const style &st)
Display this widget.
Definition: editline.cc:490
editline(int maxlength, const std::string &_prompt, const std::string &_text, history_list *history)
Transcodes its input strings from the system charset.
The basic widget interface.
Definition: widget.h:107
The namespace containing everything defined by cwidget.
Definition: columnify.cc:28
Represents a keystroke as seen by curses.
Definition: keybindings.h:43
Definition: widget.h:89