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();
106 const char* get_text();
107 const wchar_t* get_wtext();
108 void set_text(char *text, int isz);
110 // Set top left of text view
111 void set_text_row(int row);
115 void set_text_x(int v);
116 void set_text_y(int v);
118 int reposition_window(int x, int y, int w = -1, int rows = -1);
120 static int calculate_h(BC_WindowBase *gui, int font, int has_border, int rows);
121 static int calculate_row_h(int rows, BC_WindowBase *parent_window, int has_border = 1, int font = MEDIUMFONT);
122 static int pixels_to_rows(BC_WindowBase *window, int font, int pixels);
123 static int wcpos(const char *text, const char *ep);
125 void set_precision(int precision);
126 // Whether to draw every time there is a keypress or rely on user to
127 // follow up every keypress with an update().
128 void set_keypress_draw(int value);
129 int get_ibeam_letter();
130 void set_ibeam_letter(int number, int redraw = 1);
131 // Used for custom formatting text boxes
132 int get_last_keypress();
133 // Table of separators to skip. Used by time textboxes
134 // The separator format is "0000:0000". Things not alnum are considered
135 // separators. The alnums are replaced by user text.
136 void set_separators(const char *separators);
138 // 1 - selects text, -1 - deselects, 0 - do nothing
139 // in all cases it returns text_selected after the operation
140 int select_whole_text(int select);
142 // Compute suggestions for a path
143 // If entries is null, just search absolute paths
144 int calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries=0, const char *filter=0);
147 // User computes suggestions after handle_event.
148 // The array is copied to a local variable.
149 // A highlighted extension is added if 1 suggestion or a popup appears
150 // if multiple suggestions.
151 // column - starting column to replace
152 void no_suggestions();
153 void set_suggestions(ArrayList<char*> *suggestions, int column);
155 BC_ScrollTextBoxXScroll *xscroll;
156 BC_ScrollTextBoxYScroll *yscroll;
158 int reset_parameters(int rows, int has_border, int font, int size);
159 void draw(int flush);
162 void copy_selection(int clipboard_num);
163 void paste_selection(int clipboard_num);
164 void delete_selection(int letter1, int letter2, int text_len);
165 void insert_text(const wchar_t *wcp, int len=-1);
166 // Reformat text according to separators.
167 // ibeam_left causes the ibeam to move left.
168 int is_separator(const char *txt, int i);
169 void do_separators(int ibeam_left);
170 int get_x_position(int i, int start=0);
171 void get_ibeam_position(int &x, int &y);
172 void find_ibeam(int dispatch_event);
173 void select_word(int &letter1, int &letter2, int ibeam_letter);
174 void select_line(int &letter1, int &letter2, int ibeam_letter);
175 int get_cursor_letter(int cursor_x, int cursor_y);
176 int get_cursor_letter2(int cursor_x, int cursor_y);
177 int get_row_h(int rows);
178 void default_keypress(int &dispatch_event, int &result);
180 int tstrcmp(const char *cp);
181 char *tstrcpy(const char *cp);
182 char *tstrcat(const char *cp);
183 int text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz);
186 // Top left of text relative to window
188 // Top left of cursor relative to text
189 int ibeam_x, ibeam_y;
191 int ibeam_letter, unicode_active;
192 int highlight_letter1, highlight_letter2;
193 int highlight_letter3, highlight_letter4;
194 int text_x1, text_start, text_end;
195 int text_selected, word_selected, line_selected;
196 int text_ascent, text_descent, text_height;
197 int left_margin, right_margin, top_margin, bottom_margin;
202 int high_color, back_color;
203 int background_color;
204 int size, tsize, dirty;
206 char temp_string[KEYPRESSLEN];
212 // Cause the repeater to skip a cursor refresh if a certain event happened
213 // within a certain time of the last repeat event
215 // Used for custom formatting text boxes
217 const char *separators;
218 ArrayList<BC_ListBoxItem*> *suggestions;
219 BC_TextBoxSuggestions *suggestions_popup;
220 int suggestion_column;
225 class BC_TextBoxSuggestions : public BC_ListBox
228 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
229 virtual ~BC_TextBoxSuggestions();
233 BC_TextBox *text_box;
237 class BC_ScrollTextBox
239 BC_ScrollTextBoxText *text;
240 BC_ScrollTextBoxXScroll *xscroll;
241 BC_ScrollTextBoxYScroll *yscroll;
242 BC_WindowBase *parent_window;
243 const char *default_text;
244 const wchar_t *default_wtext;
248 friend class BC_ScrollTextBoxText;
249 friend class BC_ScrollTextBoxXScroll;
250 friend class BC_ScrollTextBoxYScroll;
252 BC_ScrollTextBox(BC_WindowBase *parent_window,
253 int x, int y, int w, int rows,
254 const char *default_text, int default_size=BCTEXTLEN);
255 BC_ScrollTextBox(BC_WindowBase *parent_window,
256 int x, int y, int w, int rows,
257 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
258 virtual ~BC_ScrollTextBox();
259 void create_objects();
261 virtual int handle_event() { return 1; }
262 virtual int button_press_event();
263 virtual int button_release_event();
265 void set_text(char *text, int isz);
266 int set_text_row(int n);
267 void update(const char *text);
268 void update(const wchar_t *wtext);
269 void reposition_window(int x, int y, int w, int rows);
270 void update_scrollbars();
272 int get_x() { return x; }
273 int get_y() { return y; }
274 int get_w() { return w; }
275 // Visible rows for resizing
276 int get_rows() { return rows; }
278 void set_x_pos(int x);
281 const char *get_text();
282 const wchar_t *get_wtext();
283 int get_buttonpress();
284 void wset_selection(int char1, int char2, int ibeam);
285 void set_selection(int char1, int char2, int ibeam);
286 int get_ibeam_letter();
289 class BC_ScrollTextBoxText : public BC_TextBox
292 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
293 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
294 virtual ~BC_ScrollTextBoxText();
297 BC_ScrollTextBox *gui;
298 int button_press_event() { return gui->button_press_event(); }
299 int button_release_event() { return gui->button_release_event(); }
302 class BC_ScrollTextBoxXScroll : public BC_ScrollBar
305 BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui);
306 virtual ~BC_ScrollTextBoxXScroll();
308 BC_ScrollTextBox *gui;
311 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
314 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
315 virtual ~BC_ScrollTextBoxYScroll();
317 BC_ScrollTextBox *gui;
323 class BC_PopupTextBoxText;
324 class BC_PopupTextBoxList;
326 class BC_PopupTextBox
329 BC_PopupTextBox(BC_WindowBase *parent_window,
330 ArrayList<BC_ListBoxItem*> *list_items,
331 const char *default_text, int x, int y,
332 int text_w, int list_h, int list_format = LISTBOX_TEXT);
333 virtual ~BC_PopupTextBox();
334 int create_objects();
335 virtual int handle_event();
336 const char* get_text();
337 const wchar_t* get_wtext();
343 int get_show_query();
344 void set_show_query(int v);
346 void update(const char *text);
347 void update_list(ArrayList<BC_ListBoxItem*> *data);
348 void reposition_window(int x, int y);
350 friend class BC_PopupTextBoxText;
351 friend class BC_PopupTextBoxList;
354 int x, y, text_w, list_h;
356 const char *default_text;
357 const wchar_t *default_wtext;
358 ArrayList<BC_ListBoxItem*> *list_items;
359 BC_PopupTextBoxText *textbox;
360 BC_PopupTextBoxList *listbox;
361 BC_WindowBase *parent_window;
364 class BC_PopupTextBoxText : public BC_TextBox
367 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
368 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
369 virtual ~BC_PopupTextBoxText();
371 BC_PopupTextBox *popup;
374 class BC_PopupTextBoxList : public BC_ListBox
377 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
379 BC_PopupTextBox *popup;
383 class BC_TumbleTextBoxText;
384 class BC_TumbleTextBoxTumble;
386 class BC_TumbleTextBox
389 BC_TumbleTextBox(BC_WindowBase *parent_window,
390 int64_t default_value,
396 BC_TumbleTextBox(BC_WindowBase *parent_window,
403 BC_TumbleTextBox(BC_WindowBase *parent_window,
410 virtual ~BC_TumbleTextBox();
412 int create_objects();
414 virtual int handle_event();
415 const char* get_text();
416 const wchar_t* get_wtext();
417 BC_TextBox* get_textbox();
418 int update(const char *value);
419 int update(int64_t value);
420 int update(float value);
425 void disable(int hide_text=0);
428 void reposition_window(int x, int y);
429 void set_boundaries(int64_t min, int64_t max);
430 void set_boundaries(float min, float max);
431 void set_precision(int precision);
432 void set_increment(float value);
433 void set_log_floatincrement(int value);
434 void set_tooltip(const char *text);
436 friend class BC_TumbleTextBoxText;
437 friend class BC_TumbleTextBoxTumble;
441 int64_t default_value, min, max;
442 float default_value_f, min_f, max_f;
446 int log_floatincrement;
447 BC_TumbleTextBoxText *textbox;
449 BC_WindowBase *parent_window;
452 class BC_TumbleTextBoxText : public BC_TextBox
455 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
456 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y, int precision);
457 virtual ~BC_TumbleTextBoxText();
459 int button_press_event();
460 BC_TumbleTextBox *popup;