4 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "bclistbox.h"
26 #include "bcsubwindow.h"
29 #include "bctextbox.inc"
30 #include "bctimer.inc"
37 class BC_TextBoxSuggestions;
39 class BC_ScrollTextBoxXScroll;
40 class BC_ScrollTextBoxYScroll;
41 class BC_ScrollTextBoxText;
44 class BC_TextBox : public BC_SubWindow
46 static inline int nib(int ch) {
47 if( ch >= '0' && ch <= '9' ) ch -= '0';
48 else if( ch >= 'A' && ch <= 'F' ) ch -= 'A'-10;
49 else if( ch >= 'a' && ch <= 'f' ) ch -= 'a'-10;
57 BC_TextBox(int x, int y, int w, int rows, int size, char *text,
58 int has_border=1, int font=MEDIUMFONT);
59 BC_TextBox(int x, int y, int w, int rows, int size, wchar_t *wtext,
60 int has_border=1, int font=MEDIUMFONT);
61 BC_TextBox(int x, int y, int w, int rows, const char *text,
62 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
63 BC_TextBox(int x, int y, int w, int rows, const wchar_t *wtext,
64 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
65 BC_TextBox(int x, int y, int w, int rows, int64_t text,
66 int has_border=1, int font=MEDIUMFONT);
67 BC_TextBox(int x, int y, int w, int rows, float text,
68 int has_border=1, int font=MEDIUMFONT, int precision=4);
69 BC_TextBox(int x, int y, int w, int rows,
70 int text, int has_border=1, int font=MEDIUMFONT);
71 virtual ~BC_TextBox();
74 friend class BC_TextBoxSuggestions;
75 friend class BC_ScrollTextBox;
78 // Whenever the contents of the text change
79 virtual int handle_event() { return 0; };
80 // Whenever the position of the text changes
81 virtual int motion_event() { return 0; };
82 void set_selection(int char1, int char2, int ibeam);
83 void wset_selection(int char1, int char2, int ibeam);
84 int update(const char *text);
85 int update(const wchar_t *wtext);
86 int update(int64_t value);
87 int update(float value);
96 int focus_out_event();
97 int cursor_enter_event();
98 int cursor_leave_event();
99 int cursor_motion_event();
100 virtual int button_press_event();
101 int button_release_event();
102 int repeat_event(int64_t repeat_id);
103 int keypress_event();
104 int selection_clear_event();
107 const char* get_text();
108 const wchar_t* get_wtext();
109 void set_text(char *text, int isz);
111 // Set top left of text view
112 void set_text_row(int row);
116 void set_text_x(int v);
117 void set_text_y(int v);
119 int reposition_window(int x, int y, int w = -1, int rows = -1);
121 static int calculate_h(BC_WindowBase *gui, int font, int has_border, int rows);
122 static int calculate_row_h(int rows, BC_WindowBase *parent_window, int has_border = 1, int font = MEDIUMFONT);
123 static int pixels_to_rows(BC_WindowBase *window, int font, int pixels);
124 static int wcpos(const char *text, const char *ep);
126 void set_precision(int precision);
127 // Whether to draw every time there is a keypress or rely on user to
128 // follow up every keypress with an update().
129 void set_keypress_draw(int value);
130 int get_ibeam_letter();
131 void set_ibeam_letter(int number, int redraw = 1);
132 // Used for custom formatting text boxes
133 int get_last_keypress();
134 // Table of separators to skip. Used by time textboxes
135 // The separator format is "0000:0000". Things not alnum are considered
136 // separators. The alnums are replaced by user text.
137 void set_separators(const char *separators);
139 // 1 - selects text, -1 - deselects, 0 - do nothing
140 // in all cases it returns text_selected after the operation
141 int select_whole_text(int select);
143 // Compute suggestions for a path
144 // If entries is null, just search absolute paths
145 int calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries=0, const char *filter=0);
148 // User computes suggestions after handle_event.
149 // The array is copied to a local variable.
150 // A highlighted extension is added if 1 suggestion or a popup appears
151 // if multiple suggestions.
152 // column - starting column to replace
153 void no_suggestions();
154 void set_suggestions(ArrayList<char*> *suggestions, int column);
156 BC_ScrollTextBoxXScroll *xscroll;
157 BC_ScrollTextBoxYScroll *yscroll;
159 int reset_parameters(int rows, int has_border, int font, int size);
160 void draw(int flush);
163 void copy_selection(int clipboard_num);
164 void paste_selection(int clipboard_num);
165 void delete_selection(int letter1, int letter2, int text_len);
166 void insert_text(const wchar_t *wcp, int len=-1);
167 // Reformat text according to separators.
168 // ibeam_left causes the ibeam to move left.
169 int is_separator(const char *txt, int i);
170 void do_separators(int ibeam_left);
171 int get_x_position(int i, int start=0);
172 void get_ibeam_position(int &x, int &y);
173 void find_ibeam(int dispatch_event);
174 void select_word(int &letter1, int &letter2, int ibeam_letter);
175 void select_line(int &letter1, int &letter2, int ibeam_letter);
176 int get_cursor_letter(int cursor_x, int cursor_y);
177 int get_cursor_letter2(int cursor_x, int cursor_y);
178 int get_row_h(int rows);
179 void default_keypress(int &dispatch_event, int &result);
181 int tstrcmp(const char *cp);
182 char *tstrcpy(const char *cp);
183 char *tstrcat(const char *cp);
184 int text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz);
187 // Top left of text relative to window
189 // Top left of cursor relative to text
190 int ibeam_x, ibeam_y;
192 int ibeam_letter, unicode_active;
193 int highlight_letter1, highlight_letter2;
194 int highlight_letter3, highlight_letter4;
195 int text_x1, text_start, text_end;
196 int text_selected, word_selected, line_selected;
197 int text_ascent, text_descent, text_height;
198 int left_margin, right_margin, top_margin, bottom_margin;
203 int high_color, back_color;
204 int background_color;
205 int size, tsize, dirty;
207 char temp_string[KEYPRESSLEN];
213 // Cause the repeater to skip a cursor refresh if a certain event happened
214 // within a certain time of the last repeat event
216 // Used for custom formatting text boxes
218 const char *separators;
219 ArrayList<BC_ListBoxItem*> *suggestions;
220 BC_TextBoxSuggestions *suggestions_popup;
221 int suggestion_column;
222 int selection_active;
227 class BC_TextBoxSuggestions : public BC_ListBox
230 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
231 virtual ~BC_TextBoxSuggestions();
235 BC_TextBox *text_box;
239 class BC_ScrollTextBox
241 BC_ScrollTextBoxText *text;
242 BC_ScrollTextBoxXScroll *xscroll;
243 BC_ScrollTextBoxYScroll *yscroll;
244 BC_WindowBase *parent_window;
245 const char *default_text;
246 const wchar_t *default_wtext;
250 friend class BC_ScrollTextBoxText;
251 friend class BC_ScrollTextBoxXScroll;
252 friend class BC_ScrollTextBoxYScroll;
254 BC_ScrollTextBox(BC_WindowBase *parent_window,
255 int x, int y, int w, int rows,
256 const char *default_text, int default_size=BCTEXTLEN);
257 BC_ScrollTextBox(BC_WindowBase *parent_window,
258 int x, int y, int w, int rows,
259 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
260 virtual ~BC_ScrollTextBox();
261 void create_objects();
263 virtual int handle_event() { return 1; }
264 virtual int button_press_event();
265 virtual int button_release_event();
267 void set_text(char *text, int isz);
268 int set_text_row(int n);
269 void update(const char *text);
270 void update(const wchar_t *wtext);
271 void reposition_window(int x, int y, int w, int rows);
272 void update_scrollbars();
274 int get_x() { return x; }
275 int get_y() { return y; }
276 int get_w() { return w; }
277 // Visible rows for resizing
278 int get_rows() { return rows; }
280 void set_x_pos(int x);
283 const char *get_text();
284 const wchar_t *get_wtext();
285 int get_buttonpress();
286 void wset_selection(int char1, int char2, int ibeam);
287 void set_selection(int char1, int char2, int ibeam);
288 int get_ibeam_letter();
291 class BC_ScrollTextBoxText : public BC_TextBox
294 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
295 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
296 virtual ~BC_ScrollTextBoxText();
299 BC_ScrollTextBox *gui;
300 int button_press_event() { return gui->button_press_event(); }
301 int button_release_event() { return gui->button_release_event(); }
304 class BC_ScrollTextBoxXScroll : public BC_ScrollBar
307 BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui);
308 virtual ~BC_ScrollTextBoxXScroll();
310 BC_ScrollTextBox *gui;
313 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
316 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
317 virtual ~BC_ScrollTextBoxYScroll();
319 BC_ScrollTextBox *gui;
325 class BC_PopupTextBoxText;
326 class BC_PopupTextBoxList;
328 class BC_PopupTextBox
331 BC_PopupTextBox(BC_WindowBase *parent_window,
332 ArrayList<BC_ListBoxItem*> *list_items,
333 const char *default_text, int x, int y,
334 int text_w, int list_h, int list_format = LISTBOX_TEXT);
335 virtual ~BC_PopupTextBox();
336 int create_objects();
337 virtual int handle_event();
338 const char* get_text();
339 const wchar_t* get_wtext();
345 int get_show_query();
346 void set_show_query(int v);
348 void update(const char *text);
349 void update_list(ArrayList<BC_ListBoxItem*> *data);
350 void reposition_window(int x, int y);
352 friend class BC_PopupTextBoxText;
353 friend class BC_PopupTextBoxList;
356 int x, y, text_w, list_h;
358 const char *default_text;
359 const wchar_t *default_wtext;
360 ArrayList<BC_ListBoxItem*> *list_items;
361 BC_PopupTextBoxText *textbox;
362 BC_PopupTextBoxList *listbox;
363 BC_WindowBase *parent_window;
366 class BC_PopupTextBoxText : public BC_TextBox
369 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
370 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
371 virtual ~BC_PopupTextBoxText();
373 BC_PopupTextBox *popup;
376 class BC_PopupTextBoxList : public BC_ListBox
379 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
381 BC_PopupTextBox *popup;
385 class BC_TumbleTextBoxText;
386 class BC_TumbleTextBoxTumble;
388 class BC_TumbleTextBox
391 BC_TumbleTextBox(BC_WindowBase *parent_window,
392 int64_t default_value,
398 BC_TumbleTextBox(BC_WindowBase *parent_window,
405 BC_TumbleTextBox(BC_WindowBase *parent_window,
412 virtual ~BC_TumbleTextBox();
414 int create_objects();
416 virtual int handle_event();
417 const char* get_text();
418 const wchar_t* get_wtext();
419 BC_TextBox* get_textbox();
420 int update(const char *value);
421 int update(int64_t value);
422 int update(float value);
427 void disable(int hide_text=0);
430 void reposition_window(int x, int y);
431 void set_boundaries(int64_t min, int64_t max);
432 void set_boundaries(float min, float max);
433 void set_precision(int precision);
434 void set_increment(float value);
435 void set_log_floatincrement(int value);
436 void set_tooltip(const char *text);
438 friend class BC_TumbleTextBoxText;
439 friend class BC_TumbleTextBoxTumble;
443 int64_t default_value, min, max;
444 float default_value_f, min_f, max_f;
448 int log_floatincrement;
449 BC_TumbleTextBoxText *textbox;
451 BC_WindowBase *parent_window;
454 class BC_TumbleTextBoxText : public BC_TextBox
457 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
458 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y, int precision);
459 virtual ~BC_TumbleTextBoxText();
461 int button_press_event();
462 BC_TumbleTextBox *popup;