Utils/Termlib: Implement aux display hide/show support
[senf.git] / Utils / Termlib / Editor.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Editor public header */
25
26 #ifndef HH_SENF_Utils_Termlib_Editor_
27 #define HH_SENF_Utils_Termlib_Editor_ 1
28
29 // Custom includes
30 #include <map>
31 #include <vector>
32 #include <string>
33 #include <senf/Scheduler/ClockService.hh>
34 #include <senf/Scheduler/TimerEvent.hh>
35 #include "AbstractTerminal.hh"
36 #include "Terminfo.hh"
37
38 //#include "Editor.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42 namespace term {
43
44     class BaseEditor
45         : public AbstractTerminal::Callbacks
46     {
47     public:
48         typedef KeyParser::keycode_t keycode_t;
49
50         static unsigned const DEFAULT_KEY_TIMEOUT_MS = 500u;
51
52         explicit BaseEditor(AbstractTerminal & terminal);
53
54         void newline();                 ///< Move to beginning of a new, empty line
55         void toColumn(unsigned c);      ///< Move cursor to column \p c
56         void put(char ch);              ///< Write \p ch at current column
57         void put(std::string const & text); ///< Write \a text starting at current column
58         void clearLine();               ///< Clear current line and move cursor to first column
59         void setBold();                 ///< Set bold char display
60         void setNormal();               ///< Set normal char display
61         void maybeClrScr();             ///< Clear screen if possible
62
63         void toLine(unsigned l);        ///< Move to relative display line \a l
64         void reset();                   ///< Reset display area to single line
65
66         unsigned currentColumn() const; ///< Return number of current column
67         unsigned currentLine() const;   ///< Return number of current relative line
68         unsigned width();               ///< Return current screen width
69         unsigned height();              ///< Return current screen height
70
71     protected:
72         virtual bool cb_init();
73         virtual void cb_windowSizeChanged();
74
75     private:
76         virtual void v_keyReceived(keycode_t key) = 0;
77
78         virtual void cb_charReceived(char c);
79
80         void keySequenceTimeout();
81         void processKeys();
82
83         void write(char ch);
84         void write(std::string const & s);
85         
86         AbstractTerminal * terminal_;
87         Terminfo tifo_;
88         KeyParser keyParser_;
89         std::string inputBuffer_;
90         ClockService::clock_type keyTimeout_;
91         scheduler::TimerEvent timer_;
92         unsigned column_;
93         unsigned displayHeight_;
94         unsigned line_;
95     };
96
97     class LineEditor
98         : public BaseEditor
99     {
100     public:
101         ///////////////////////////////////////////////////////////////////////////
102         // Types
103
104         typedef boost::function<void (LineEditor&)> KeyBinding;
105         typedef boost::function<void (std::string const &)> AcceptCallback;
106
107         static unsigned const MAX_HISTORY_SIZE = 1024u;
108
109         ///////////////////////////////////////////////////////////////////////////
110
111         LineEditor(AbstractTerminal & terminal, AcceptCallback cb);
112         
113         ///////////////////////////////////////////////////////////////////////////
114
115         void prompt(std::string const & text);
116         void set(std::string const & text, unsigned pos = 0u);
117
118         // Overall edit control
119         void show();
120         void hide();
121         void accept();
122         void clear();
123         void redisplay();
124         void forceRedisplay();
125         
126         // Cursor and display movement
127         void gotoChar(unsigned n);
128         void scrollTo(unsigned n);
129
130         // Text manipulation
131         void deleteChar(unsigned n=1);
132         void insert(char ch);
133         void insert(std::string const & text);
134
135         // History
136         void pushHistory(std::string const & text);
137         void prevHistory();
138         void nextHistory();
139
140         // Aux Display
141         void auxDisplay(int line, std::string const & text);
142         unsigned maxAuxDisplayHeight();
143         void clearAuxDisplay();
144
145         // Get information
146         std::string const & text();
147         unsigned point();
148         unsigned displayPos();
149         keycode_t lastKey();
150
151         // Key bindings
152         void defineKey(keycode_t key, KeyBinding binding);
153         void unsetKey(keycode_t key);
154         
155     private:
156         virtual bool cb_init();
157         virtual void cb_windowSizeChanged();
158         virtual void v_keyReceived(keycode_t key);
159
160         typedef std::map<keycode_t, KeyBinding> KeyMap;
161         typedef std::vector<std::string> History;
162         typedef std::vector<std::string> AuxDisplay;
163
164         bool enabled_;
165         bool redisplayNeeded_;
166         std::string prompt_;
167         unsigned promptWidth_;
168         unsigned editWidth_;
169         std::string text_;
170         unsigned point_;
171         unsigned displayPos_;
172         keycode_t lastKey_;
173         AcceptCallback callback_;
174         KeyMap bindings_;
175         History history_;
176         unsigned historyPoint_;
177         AuxDisplay auxDisplay_;
178     };
179
180 namespace bindings {
181
182     void selfInsertCommand   (LineEditor & editor);
183     void forwardChar         (LineEditor & editor);
184     void backwardChar        (LineEditor & editor);
185     void accept              (LineEditor & editor);
186     void backwardDeleteChar  (LineEditor & editor);
187     void deleteChar          (LineEditor & editor);
188     void beginningOfLine     (LineEditor & editor);
189     void endOfLine           (LineEditor & editor);
190     void deleteToEndOfLine   (LineEditor & editor);
191     void restartEdit         (LineEditor & editor);
192     void prevHistory         (LineEditor & editor);
193     void nextHistory         (LineEditor & editor);
194     void clearScreen         (LineEditor & editor);
195
196     typedef boost::function<void (LineEditor &, unsigned b, unsigned e, std::vector<std::string> &)> Completer;
197     void complete            (LineEditor & editor, Completer completer);
198
199 }
200
201 }}
202
203 ///////////////////////////////hh.e////////////////////////////////////////
204 //#include "Editor.cci"
205 //#include "Editor.ct"
206 //#include "Editor.cti"
207 #endif
208
209 \f
210 // Local Variables:
211 // mode: c++
212 // fill-column: 100
213 // comment-column: 40
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // End: