Code cleanup
[crawl.git] / crawl-ref / source / cio.h
blob4e6e4e48d45dd6d16ca1a5c207b083b490cd807c
1 /*
2 * File: cio.h
3 * Summary: System independent console IO functions
4 * Created by: dshaligram on Wed Jun 20 19:00:52 2007 UTC
5 */
7 #ifndef CIO_H
8 #define CIO_H
10 #include "enum.h"
11 #include "externs.h"
12 #include "defines.h"
13 #include "directn.h"
15 #include <cctype>
16 #include <string>
17 #include <vector>
19 class input_history
21 public:
22 input_history(size_t size);
24 void new_input(const std::string &s);
25 void clear();
27 const std::string *prev();
28 const std::string *next();
30 void go_end();
31 private:
32 typedef std::list<std::string> string_list;
34 string_list history;
35 string_list::iterator pos;
36 size_t maxsize;
39 void cursorxy(int x, int y);
40 inline void cursorxy(const coord_def& p) { cursorxy(p.x, p.y); }
42 // Read one key, flag it as a mouse event if appropriate, but apply no
43 // other conversions. Defined in lib$OS.cc, not in cio.cc.
44 int m_getch();
46 // Converts a key to a direction key, converting keypad and other sequences
47 // to vi key sequences (shifted/control key directions are also handled). Non
48 // direction keys (hopefully) pass through unmangled.
49 int unmangle_direction_keys(int keyin, KeymapContext keymap = KMC_DEFAULT,
50 bool fake_ctrl = true, bool fake_shift = true);
52 int nowrapcprintf(int wrapcol, const char *s, ...);
53 int nowrap_eol_cprintf(const char *s, ...);
55 // Returns zero if user entered text and pressed Enter, otherwise returns the
56 // key pressed that caused the exit, usually Escape.
58 // If keyproc is provided, it must return 1 for normal processing, 0 to exit
59 // normally (pretend the user pressed Enter), or -1 to exit as if the user
60 // pressed Escape
61 int cancelable_get_line(char *buf,
62 int len,
63 input_history *mh = NULL,
64 int (*keyproc)(int &c) = NULL);
66 // Do not use this templated function directly. Use the macro below instead.
67 template<int> static int cancelable_get_line_autohist_temp(char *buf, int len)
69 static input_history hist(10);
70 return cancelable_get_line(buf, len, &hist);
73 // This version of cancelable_get_line will automatically retain its own
74 // input history, independent of other calls to cancelable_get_line.
75 #define cancelable_get_line_autohist(buf, len) \
76 cancelable_get_line_autohist_temp<__LINE__>(buf, len)
78 struct c_mouse_event
80 coord_def pos;
81 int bstate;
83 enum button_state_type
85 BUTTON1 = 0x1,
86 BUTTON1_DBL = 0x2,
87 BUTTON2 = 0x4,
88 BUTTON2_DBL = 0x8,
89 BUTTON3 = 0x10,
90 BUTTON3_DBL = 0x20,
91 BUTTON4 = 0x40,
92 BUTTON4_DBL = 0x80,
93 BUTTON_SCRL_UP = 0x100,
94 BUTTON_SCRL_DN = 0x200,
97 c_mouse_event() : pos(-1, -1), bstate(0)
101 c_mouse_event(const coord_def &c, int state = 0) : pos(c), bstate(state)
105 // Returns true for valid events.
106 operator bool () const
108 return (bstate);
111 bool left_clicked() const
113 return (bstate & BUTTON1);
116 bool right_clicked() const
118 return (bstate & BUTTON3);
121 bool scroll_up() const
123 return (bstate & (BUTTON4 | BUTTON4_DBL | BUTTON_SCRL_UP));
126 bool scroll_down() const
128 return (bstate & (BUTTON2 | BUTTON2_DBL | BUTTON_SCRL_DN));
132 coord_def get_mouse_pos();
133 c_mouse_event get_mouse_event();
134 void new_mouse_event(const c_mouse_event &ce);
135 void c_input_reset(bool enable_mouse, bool flush = false);
137 // Keys that getch() must return for keys Crawl is interested in.
138 enum KEYS
140 CK_ENTER = '\r',
141 CK_BKSP = 8,
142 CK_ESCAPE = ESCAPE,
144 // 128 is off-limits because it's the code that's used when running
145 CK_DELETE = 129,
147 // This sequence of enums should not be rearranged.
148 CK_UP,
149 CK_DOWN,
150 CK_LEFT,
151 CK_RIGHT,
153 CK_INSERT,
155 CK_HOME,
156 CK_END,
157 CK_CLEAR,
159 CK_PGUP,
160 CK_PGDN,
162 CK_SHIFT_UP,
163 CK_SHIFT_DOWN,
164 CK_SHIFT_LEFT,
165 CK_SHIFT_RIGHT,
167 CK_SHIFT_INSERT,
169 CK_SHIFT_HOME,
170 CK_SHIFT_END,
171 CK_SHIFT_CLEAR,
173 CK_SHIFT_PGUP,
174 CK_SHIFT_PGDN,
176 CK_CTRL_UP,
177 CK_CTRL_DOWN,
178 CK_CTRL_LEFT,
179 CK_CTRL_RIGHT,
181 CK_CTRL_INSERT,
183 CK_CTRL_HOME,
184 CK_CTRL_END,
185 CK_CTRL_CLEAR,
187 CK_CTRL_PGUP,
188 CK_CTRL_PGDN,
190 // Mouse codes.
191 CK_MOUSE_MOVE = 10001,
192 CK_MOUSE_CMD,
193 CK_MOUSE_B1,
194 CK_MOUSE_B2,
195 CK_MOUSE_B3,
196 CK_MOUSE_B4,
197 CK_MOUSE_B5,
198 CK_MOUSE_CLICK,
201 class cursor_control
203 public:
204 cursor_control(bool cursor_enabled)
205 : cstate(is_cursor_enabled()), smartcstate(is_smart_cursor_enabled())
207 enable_smart_cursor(false);
208 set_cursor_enabled(cursor_enabled);
210 ~cursor_control() {
211 set_cursor_enabled(cstate);
212 enable_smart_cursor(smartcstate);
214 private:
215 bool cstate;
216 bool smartcstate;
219 // Reads lines of text; used internally by cancelable_get_line.
220 class line_reader
222 public:
223 line_reader(char *buffer, size_t bufsz,
224 int wrap_col = get_number_of_cols());
225 virtual ~line_reader();
227 typedef int (*keyproc)(int &key);
229 int read_line(bool clear_previous = true);
231 std::string get_text() const;
233 void set_input_history(input_history *ih);
234 void set_keyproc(keyproc fn);
236 protected:
237 void cursorto(int newcpos);
238 virtual int process_key(int ch);
239 void backspace();
240 void killword();
241 void kill_to_begin();
243 bool is_wordchar(int c);
245 protected:
246 char *buffer;
247 size_t bufsz;
248 input_history *history;
249 GotoRegion region;
250 coord_def start;
251 keyproc keyfn;
252 int wrapcol;
254 // These are subject to change during editing.
255 char *cur;
256 int length;
257 int pos;
260 typedef int keycode_type;
262 #endif