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;
38 class BC_ScrollTextBoxYScroll;
41 class BC_TextBox : public BC_SubWindow
43 static inline int nib(int ch) {
44 if( ch >= '0' && ch <= '9' ) ch -= '0';
45 else if( ch >= 'A' && ch <= 'F' ) ch -= 'A'-10;
46 else if( ch >= 'a' && ch <= 'f' ) ch -= 'a'-10;
54 BC_TextBox(int x, int y, int w, int rows, int size, char *text,
55 int has_border=1, int font=MEDIUMFONT);
56 BC_TextBox(int x, int y, int w, int rows, int size, wchar_t *wtext,
57 int has_border=1, int font=MEDIUMFONT);
58 BC_TextBox(int x, int y, int w, int rows, const char *text,
59 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
60 BC_TextBox(int x, int y, int w, int rows, const wchar_t *wtext,
61 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
62 BC_TextBox(int x, int y, int w, int rows, int64_t text,
63 int has_border=1, int font=MEDIUMFONT);
64 BC_TextBox(int x, int y, int w, int rows, float text,
65 int has_border=1, int font=MEDIUMFONT, int precision=4);
66 BC_TextBox(int x, int y, int w, int rows,
67 int text, int has_border=1, int font=MEDIUMFONT);
68 virtual ~BC_TextBox();
71 friend class BC_TextBoxSuggestions;
74 // Whenever the contents of the text change
75 virtual int handle_event() { return 0; };
76 // Whenever the position of the text changes
77 virtual int motion_event() { return 0; };
78 void set_selection(int char1, int char2, int ibeam);
79 void wset_selection(int char1, int char2, int ibeam);
80 int update(const char *text);
81 int update(const wchar_t *wtext);
82 int update(int64_t value);
83 int update(float value);
92 int focus_out_event();
93 int cursor_enter_event();
94 int cursor_leave_event();
95 int cursor_motion_event();
96 virtual int button_press_event();
97 int button_release_event();
98 int repeat_event(int64_t repeat_id);
102 const char* get_text();
103 const wchar_t* get_wtext();
104 void set_text(char *text, int isz);
106 // Set top left of text view
107 void set_text_row(int row);
110 int reposition_window(int x, int y, int w = -1, int rows = -1);
112 static int calculate_h(BC_WindowBase *gui, int font, int has_border, int rows);
113 static int calculate_row_h(int rows, BC_WindowBase *parent_window, int has_border = 1, int font = MEDIUMFONT);
114 static int pixels_to_rows(BC_WindowBase *window, int font, int pixels);
115 static int wcpos(const char *text, const char *ep);
117 void set_precision(int precision);
118 // Whether to draw every time there is a keypress or rely on user to
119 // follow up every keypress with an update().
120 void set_keypress_draw(int value);
121 int get_ibeam_letter();
122 void set_ibeam_letter(int number, int redraw = 1);
123 // Used for custom formatting text boxes
124 int get_last_keypress();
125 // Table of separators to skip. Used by time textboxes
126 // The separator format is "0000:0000". Things not alnum are considered
127 // separators. The alnums are replaced by user text.
128 void set_separators(const char *separators);
130 // 1 - selects text, -1 - deselects, 0 - do nothing
131 // in all cases it returns text_selected after the operation
132 int select_whole_text(int select);
134 // Compute suggestions for a path
135 // If entries is null, just search absolute paths
136 int calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries=0, const char *filter=0);
139 // User computes suggestions after handle_event.
140 // The array is copied to a local variable.
141 // A highlighted extension is added if 1 suggestion or a popup appears
142 // if multiple suggestions.
143 // column - starting column to replace
144 void no_suggestions();
145 void set_suggestions(ArrayList<char*> *suggestions, int column);
146 BC_ScrollTextBoxYScroll *yscroll;
149 int reset_parameters(int rows, int has_border, int font, int size);
150 void draw(int flush);
153 void copy_selection(int clipboard_num);
154 void paste_selection(int clipboard_num);
155 void delete_selection(int letter1, int letter2, int text_len);
156 void insert_text(const wchar_t *wcp, int len=-1);
157 // Reformat text according to separators.
158 // ibeam_left causes the ibeam to move left.
159 int is_separator(const char *txt, int i);
160 void do_separators(int ibeam_left);
161 int get_x_position(int i, int start=0);
162 void get_ibeam_position(int &x, int &y);
163 void find_ibeam(int dispatch_event);
164 void select_word(int &letter1, int &letter2, int ibeam_letter);
165 void select_line(int &letter1, int &letter2, int ibeam_letter);
166 int get_cursor_letter(int cursor_x, int cursor_y);
167 int get_cursor_letter2(int cursor_x, int cursor_y);
168 int get_row_h(int rows);
169 void default_keypress(int &dispatch_event, int &result);
171 int tstrcmp(const char *cp);
172 char *tstrcpy(const char *cp);
173 char *tstrcat(const char *cp);
174 int text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz);
177 // Top left of text relative to window
179 // Top left of cursor relative to text
180 int ibeam_x, ibeam_y;
182 int ibeam_letter, unicode_active;
183 int highlight_letter1, highlight_letter2;
184 int highlight_letter3, highlight_letter4;
185 int text_x1, text_start, text_end;
186 int text_selected, word_selected, line_selected;
187 int text_ascent, text_descent, text_height;
188 int left_margin, right_margin, top_margin, bottom_margin;
193 int high_color, back_color;
194 int background_color;
195 int size, tsize, dirty;
197 char temp_string[KEYPRESSLEN];
203 // Cause the repeater to skip a cursor refresh if a certain event happened
204 // within a certain time of the last repeat event
206 // Used for custom formatting text boxes
208 const char *separators;
209 ArrayList<BC_ListBoxItem*> *suggestions;
210 BC_TextBoxSuggestions *suggestions_popup;
211 int suggestion_column;
216 class BC_TextBoxSuggestions : public BC_ListBox
219 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
220 virtual ~BC_TextBoxSuggestions();
224 BC_TextBox *text_box;
229 class BC_ScrollTextBoxText;
230 class BC_ScrollTextBoxYScroll;
233 class BC_ScrollTextBox
235 BC_ScrollTextBoxText *text;
236 BC_ScrollTextBoxYScroll *yscroll;
237 BC_WindowBase *parent_window;
238 const char *default_text;
239 const wchar_t *default_wtext;
243 friend class BC_ScrollTextBoxText;
244 friend class BC_ScrollTextBoxYScroll;
246 BC_ScrollTextBox(BC_WindowBase *parent_window,
247 int x, int y, int w, int rows,
248 const char *default_text, int default_size=BCTEXTLEN);
249 BC_ScrollTextBox(BC_WindowBase *parent_window,
250 int x, int y, int w, int rows,
251 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
252 virtual ~BC_ScrollTextBox();
253 void create_objects();
255 virtual int handle_event() { return 1; }
256 virtual int button_press_event();
257 virtual int button_release_event();
259 void set_text(char *text, int isz);
260 int set_text_row(int n);
261 void update(const char *text);
262 void update(const wchar_t *wtext);
263 void reposition_window(int x, int y, int w, int rows);
265 int get_x() { return x; }
266 int get_y() { return y; }
267 int get_w() { return w; }
268 // Visible rows for resizing
269 int get_rows() { return rows; }
273 const char *get_text();
274 const wchar_t *get_wtext();
275 int get_buttonpress();
276 void wset_selection(int char1, int char2, int ibeam);
277 void set_selection(int char1, int char2, int ibeam);
278 int get_ibeam_letter();
281 class BC_ScrollTextBoxText : public BC_TextBox
284 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
285 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
286 virtual ~BC_ScrollTextBoxText();
289 BC_ScrollTextBox *gui;
290 int button_press_event() { return gui->button_press_event(); }
291 int button_release_event() { return gui->button_release_event(); }
294 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
297 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
298 virtual ~BC_ScrollTextBoxYScroll();
300 BC_ScrollTextBox *gui;
306 class BC_PopupTextBoxText;
307 class BC_PopupTextBoxList;
309 class BC_PopupTextBox
312 BC_PopupTextBox(BC_WindowBase *parent_window,
313 ArrayList<BC_ListBoxItem*> *list_items,
314 const char *default_text, int x, int y,
315 int text_w, int list_h, int list_format = LISTBOX_TEXT);
316 virtual ~BC_PopupTextBox();
317 int create_objects();
318 virtual int handle_event();
319 const char* get_text();
320 const wchar_t* get_wtext();
326 int get_show_query();
327 void set_show_query(int v);
329 void update(const char *text);
330 void update_list(ArrayList<BC_ListBoxItem*> *data);
331 void reposition_window(int x, int y);
333 friend class BC_PopupTextBoxText;
334 friend class BC_PopupTextBoxList;
337 int x, y, text_w, list_h;
339 const char *default_text;
340 const wchar_t *default_wtext;
341 ArrayList<BC_ListBoxItem*> *list_items;
342 BC_PopupTextBoxText *textbox;
343 BC_PopupTextBoxList *listbox;
344 BC_WindowBase *parent_window;
347 class BC_PopupTextBoxText : public BC_TextBox
350 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
351 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
352 virtual ~BC_PopupTextBoxText();
354 BC_PopupTextBox *popup;
357 class BC_PopupTextBoxList : public BC_ListBox
360 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
362 BC_PopupTextBox *popup;
366 class BC_TumbleTextBoxText;
367 class BC_TumbleTextBoxTumble;
369 class BC_TumbleTextBox
372 BC_TumbleTextBox(BC_WindowBase *parent_window,
373 int64_t default_value,
379 BC_TumbleTextBox(BC_WindowBase *parent_window,
386 BC_TumbleTextBox(BC_WindowBase *parent_window,
393 virtual ~BC_TumbleTextBox();
395 int create_objects();
397 virtual int handle_event();
398 const char* get_text();
399 const wchar_t* get_wtext();
400 BC_TextBox* get_textbox();
401 int update(const char *value);
402 int update(int64_t value);
403 int update(float value);
408 void disable(int hide_text=0);
411 void reposition_window(int x, int y);
412 void set_boundaries(int64_t min, int64_t max);
413 void set_boundaries(float min, float max);
414 void set_precision(int precision);
415 void set_increment(float value);
416 void set_log_floatincrement(int value);
417 void set_tooltip(const char *text);
419 friend class BC_TumbleTextBoxText;
420 friend class BC_TumbleTextBoxTumble;
424 int64_t default_value, min, max;
425 float default_value_f, min_f, max_f;
429 int log_floatincrement;
430 BC_TumbleTextBoxText *textbox;
432 BC_WindowBase *parent_window;
435 class BC_TumbleTextBoxText : public BC_TextBox
438 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
439 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y);
440 virtual ~BC_TumbleTextBoxText();
442 int button_press_event();
443 BC_TumbleTextBox *popup;