00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef EDITLINE_H
00023 #define EDITLINE_H
00024
00025 #include "widget.h"
00026
00027 #include <vector>
00028
00029 namespace cwidget
00030 {
00031 namespace config
00032 {
00033 class keybindings;
00034 }
00035
00036 namespace widgets
00037 {
00038 class editline : public widget
00039 {
00040 public:
00041 typedef std::vector<std::wstring> history_list;
00042 private:
00043
00044 std::wstring prompt;
00045 std::wstring text;
00046
00047 std::wstring pre_history_text;
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 std::wstring::size_type curloc, startloc;
00059
00060 int desired_size;
00061
00062
00063 history_list *history;
00064
00065 history_list::size_type history_loc;
00066
00067
00068 bool using_history;
00069
00070 bool allow_wrap;
00071
00079 bool clear_on_first_edit;
00080
00081 void normalize_cursor();
00082
00086 int get_line_of_character(size_t n, int width);
00087
00091 int get_character_of_line(size_t n, int width);
00092
00099 wchar_t get_char(size_t n);
00100
00102 size_t get_num_chars() const { return prompt.size() + text.size(); }
00103 protected:
00104 bool handle_key(const config::key &k);
00105
00106 editline(const std::wstring &_prompt,
00107 const std::wstring &_text=L"",
00108 history_list *history=NULL);
00109
00111 editline(const std::string &_prompt,
00112 const std::string &_text="",
00113 history_list *history=NULL);
00114
00115 editline(int maxlength, const std::wstring &_prompt,
00116 const std::wstring &_text, history_list *history);
00117
00119 editline(int maxlength, const std::string &_prompt,
00120 const std::string &_text, history_list *history);
00121
00122 public:
00123 static util::ref_ptr<editline>
00124 create(const std::wstring &prompt, const std::wstring &text = L"",
00125 history_list *history = NULL)
00126 {
00127 util::ref_ptr<editline> rval(new editline(prompt, text, history));
00128 rval->decref();
00129 return rval;
00130 }
00131
00132 static util::ref_ptr<editline>
00133 create(const std::string &prompt, const std::string &text = "",
00134 history_list *history = NULL)
00135 {
00136 util::ref_ptr<editline> rval(new editline(prompt, text, history));
00137 rval->decref();
00138 return rval;
00139 }
00140
00141 static util::ref_ptr<editline>
00142 create(int maxlength, const std::wstring &prompt,
00143 const std::wstring &text = L"", history_list *history = NULL)
00144 {
00145 util::ref_ptr<editline> rval(new editline(maxlength, prompt, text, history));
00146 rval->decref();
00147 return rval;
00148 }
00149
00150 static util::ref_ptr<editline>
00151 create(int maxlength, const std::string &prompt,
00152 const std::string &text = "", history_list *history = NULL)
00153 {
00154 util::ref_ptr<editline> rval(new editline(maxlength, prompt, text, history));
00155 rval->decref();
00156 return rval;
00157 }
00158
00163 bool get_clear_on_first_edit() const { return clear_on_first_edit; }
00169 void set_clear_on_first_edit(bool value)
00170 {
00171 clear_on_first_edit = value;
00172 }
00173
00174 void set_allow_wrap(bool allow) { allow_wrap = allow; }
00175 bool get_allow_wrap() const { return allow_wrap; }
00176
00177 bool focus_me();
00178 void paint(const style &st);
00179 void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
00180
00181 sigc::signal1<void, std::wstring> entered;
00182
00183 sigc::signal1<void, std::wstring> text_changed;
00184
00185
00186 std::wstring get_text() {return text;}
00187 void set_text(std::wstring _text);
00188
00192 void set_text(std::string _text);
00193
00194 bool get_cursorvisible();
00195 point get_cursorloc();
00196
00197 int width_request();
00198 int height_request(int height);
00199
00200 static void add_to_history(std::wstring s,
00201 history_list *history);
00202
00203
00204 void add_to_history(std::wstring s);
00205 void reset_history();
00206
00207 static config::keybindings *bindings;
00208 static void init_bindings();
00209 };
00210
00211 typedef util::ref_ptr<editline> editline_ref;
00212 }
00213 }
00214
00215 #endif