editline.h

00001 // editline.h             -*-c++-*-
00002 //
00003 //   Copyright (C) 2000, 2007 Daniel Burrows
00004 //
00005 //   This program is free software; you can redistribute it and/or
00006 //   modify it under the terms of the GNU General Public License as
00007 //   published by the Free Software Foundation; either version 2 of
00008 //   the License, or (at your option) any later version.
00009 //
00010 //   This program is distributed in the hope that it will be useful,
00011 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 //   General Public License for more details.
00014 //
00015 //   You should have received a copy of the GNU General Public License
00016 //   along with this program; see the file COPYING.  If not, write to
00017 //   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018 //   Boston, MA 02111-1307, USA.
00019 
00020 //  A simple line-editor widget.
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       // Used as a "virtual" history entry.
00049 
00050       // curloc is the offset of the cursor within the input string;
00051       // startloc is the offset of the first visible character within the
00052       // combined input string and prompt.
00053       //
00054       // If allow_wrap is true, startloc is the first character of the
00055       // first visible line (i.e., it represents vertical scrolling).
00056       // Otherwise, it is the first character within the sole line that is
00057       // visible (i.e., it represents horizontal scrolling).
00058       std::wstring::size_type curloc, startloc;
00059 
00060       int desired_size;
00061 
00062       // The history of the edit-line.  (if NULL, there is no history)
00063       history_list *history;
00064       // The current location in the history
00065       history_list::size_type history_loc;
00066       // True if we're flipping through the history.  (to avoid signedness
00067       // problems)
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       // Called when the user presses Enter to confirm the text
00183       sigc::signal1<void, std::wstring> text_changed;
00184       // Called when the text is altered.
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       // Appends the string to the end of the history list (convenience routine)
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

Generated on Sat Jun 12 14:51:02 2010 for cwidget by  doxygen 1.5.6