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 "bcmenuitem.h"
27 #include "bcpopupmenu.h"
28 #include "bcsubwindow.h"
31 #include "bctextbox.inc"
32 #include "bctimer.inc"
39 class BC_TextBoxSuggestions;
41 class BC_ScrollTextBoxXScroll;
42 class BC_ScrollTextBoxYScroll;
43 class BC_ScrollTextBoxText;
46 class BC_TextBox : public BC_SubWindow
48 static inline int nib(int ch) {
49 if( ch >= '0' && ch <= '9' ) ch -= '0';
50 else if( ch >= 'A' && ch <= 'F' ) ch -= 'A'-10;
51 else if( ch >= 'a' && ch <= 'f' ) ch -= 'a'-10;
55 // may be local fixed/dynamic, or shared fixed ref via set_text
57 // always local dynamic
60 // size > 0: fixed buffer, size == 0: dynamic buffer
61 // size < 0: fixed shared buffer via set_text
62 BC_TextBox(int x, int y, int w, int rows, int size, char *text,
63 int has_border=1, int font=MEDIUMFONT);
64 BC_TextBox(int x, int y, int w, int rows, int size, wchr_t *wtext,
65 int has_border=1, int font=MEDIUMFONT);
66 BC_TextBox(int x, int y, int w, int rows, const char *text,
67 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
68 BC_TextBox(int x, int y, int w, int rows, const wchr_t *wtext,
69 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
70 BC_TextBox(int x, int y, int w, int rows, int64_t text,
71 int has_border=1, int font=MEDIUMFONT);
72 BC_TextBox(int x, int y, int w, int rows, float text,
73 int has_border=1, int font=MEDIUMFONT, int precision=4);
74 BC_TextBox(int x, int y, int w, int rows,
75 int text, int has_border=1, int font=MEDIUMFONT);
76 virtual ~BC_TextBox();
79 friend class BC_TextBoxSuggestions;
80 friend class BC_ScrollTextBox;
83 // Whenever the contents of the text change
84 virtual int handle_event() { return 0; };
85 // Whenever the position of the text changes
86 virtual int motion_event() { return 0; };
87 void set_selection(int char1, int char2, int ibeam);
88 void wset_selection(int char1, int char2, int ibeam);
89 int update(const char *text);
90 int update(const wchr_t *wtext);
91 int update(int64_t value);
92 int update(float value);
100 int focus_in_event();
101 int focus_out_event();
102 int cursor_enter_event();
103 int cursor_leave_event();
104 int cursor_motion_event();
105 virtual int button_press_event();
106 int button_release_event();
107 int repeat_event(int64_t repeat_id);
108 int keypress_event();
109 int selection_clear_event();
112 const char* get_text();
113 const wchr_t* get_wtext();
114 void set_text(char *text, int isz);
115 int wdemand(int len);
116 int tdemand(int len);
118 // Set top left of text view
119 void set_text_row(int row);
123 void set_text_x(int v);
124 void set_text_y(int v);
125 int get_back_color();
126 void set_back_color(int v);
128 int reposition_window(int x, int y, int w = -1, int rows = -1);
130 int cut(int do_update);
131 int copy(int do_update);
132 int paste(int do_update);
134 static int calculate_h(BC_WindowBase *gui, int font, int has_border, int rows);
135 static int calculate_row_h(int rows, BC_WindowBase *parent_window, int has_border = 1, int font = MEDIUMFONT);
136 static int pixels_to_rows(BC_WindowBase *window, int font, int pixels);
137 static int wcpos(const char *text, const char *ep);
139 void set_precision(int precision);
140 // Whether to draw every time there is a keypress or rely on user to
141 // follow up every keypress with an update().
142 void set_keypress_draw(int value);
143 int get_ibeam_letter();
144 void set_ibeam_letter(int number, int redraw = 1);
145 // Used for custom formatting text boxes
146 int get_last_keypress();
147 // Table of separators to skip. Used by time textboxes
148 // The separator format is "0000:0000". Things not alnum are considered
149 // separators. The alnums are replaced by user text.
150 void set_separators(const char *separators);
152 // 1 - selects text, -1 - deselects, 0 - do nothing
153 // in all cases it returns text_selected after the operation
154 int select_whole_text(int select);
156 // Compute suggestions for a path
157 // If entries is null, just search absolute paths
158 int calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries=0, const char *filter=0);
161 // User computes suggestions after handle_event.
162 // The array is copied to a local variable.
163 // A highlighted extension is added if 1 suggestion or a popup appears
164 // if multiple suggestions.
165 // column - starting column to replace
166 void no_suggestions();
167 void set_suggestions(ArrayList<char*> *suggestions, int column);
169 BC_ScrollTextBoxXScroll *xscroll;
170 BC_ScrollTextBoxYScroll *yscroll;
172 int reset_parameters(int rows, int has_border, int font, int size);
174 void draw(int flush);
177 void copy_selection(int clipboard_num);
178 void paste_selection(int clipboard_num);
179 void delete_selection(int letter1, int letter2, int text_len);
180 void insert_text(const wchr_t *wcp, int len=-1);
181 // Reformat text according to separators.
182 // ibeam_left causes the ibeam to move left.
183 int is_separator(const char *txt, int i);
184 void do_separators(int ibeam_left);
185 int get_x_position(int i, int start=0);
186 void get_ibeam_position(int &x, int &y);
187 void find_ibeam(int dispatch_event);
188 void select_word(int &letter1, int &letter2, int ibeam_letter);
189 void select_line(int &letter1, int &letter2, int ibeam_letter);
190 int get_cursor_letter(int cursor_x, int cursor_y);
191 int get_cursor_letter2(int cursor_x, int cursor_y);
192 int get_row_h(int rows);
193 void default_keypress(int &dispatch_event, int &result);
195 int tstrcmp(const char *cp);
196 char *tstrcpy(const char *cp);
197 char *tstrcat(const char *cp);
198 int text_update(const wchr_t *wcp, int wsz, char *tcp, int tsz);
201 // Top left of text relative to window
203 // Top left of cursor relative to text
204 int ibeam_x, ibeam_y;
206 int ibeam_letter, unicode_active;
207 int highlight_letter1, highlight_letter2;
208 int highlight_letter3, highlight_letter4;
209 int text_x1, text_start, text_end;
210 int text_selected, word_selected, line_selected;
211 int text_ascent, text_descent, text_height;
212 int left_margin, right_margin, top_margin, bottom_margin;
217 int high_color, back_color;
218 int background_color;
219 int size, tsize, dirty;
221 char temp_string[KEYPRESSLEN];
227 // Cause the repeater to skip a cursor refresh if a certain event happened
228 // within a certain time of the last repeat event
230 // Used for custom formatting text boxes
232 const char *separators;
233 ArrayList<BC_ListBoxItem*> *suggestions;
234 BC_TextBoxSuggestions *suggestions_popup;
235 int suggestion_column;
236 int selection_active;
241 class BC_TextBoxSuggestions : public BC_ListBox
244 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
245 virtual ~BC_TextBoxSuggestions();
249 BC_TextBox *text_box;
253 class BC_ScrollTextBox
255 BC_ScrollTextBoxText *text;
256 BC_ScrollTextBoxXScroll *xscroll;
257 BC_ScrollTextBoxYScroll *yscroll;
258 BC_WindowBase *parent_window;
259 const char *default_text;
260 const wchr_t *default_wtext;
264 friend class BC_ScrollTextBoxText;
265 friend class BC_ScrollTextBoxXScroll;
266 friend class BC_ScrollTextBoxYScroll;
268 BC_ScrollTextBox(BC_WindowBase *parent_window,
269 int x, int y, int w, int rows,
270 const char *default_text, int default_size=BCTEXTLEN);
271 BC_ScrollTextBox(BC_WindowBase *parent_window,
272 int x, int y, int w, int rows,
273 const wchr_t *default_wtext, int default_size=BCTEXTLEN);
274 virtual ~BC_ScrollTextBox();
275 void create_objects();
277 virtual int handle_event() { return 1; }
278 virtual int button_press_event();
279 virtual int button_release_event();
281 void set_text(char *text, int isz);
282 int set_text_row(int n);
283 void update(const char *text);
284 void update(const wchr_t *wtext);
285 void reposition_window(int x, int y, int w, int rows);
286 void update_scrollbars();
288 int get_x() { return x; }
289 int get_y() { return y; }
290 int get_w() { return w; }
291 // Visible rows for resizing
292 int get_rows() { return rows; }
294 void set_x_pos(int x);
297 const char *get_text();
298 const wchr_t *get_wtext();
299 int get_buttonpress();
300 void wset_selection(int char1, int char2, int ibeam);
301 void set_selection(int char1, int char2, int ibeam);
302 int get_ibeam_letter();
305 class BC_ScrollTextBoxText : public BC_TextBox
308 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
309 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchr_t *wtext);
310 virtual ~BC_ScrollTextBoxText();
313 BC_ScrollTextBox *gui;
314 int button_press_event() { return gui->button_press_event(); }
315 int button_release_event() { return gui->button_release_event(); }
318 class BC_ScrollTextBoxXScroll : public BC_ScrollBar
321 BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui);
322 virtual ~BC_ScrollTextBoxXScroll();
324 BC_ScrollTextBox *gui;
327 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
330 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
331 virtual ~BC_ScrollTextBoxYScroll();
333 BC_ScrollTextBox *gui;
339 class BC_PopupTextBoxText;
340 class BC_PopupTextBoxList;
342 class BC_PopupTextBox
345 BC_PopupTextBox(BC_WindowBase *parent_window,
346 ArrayList<BC_ListBoxItem*> *list_items,
347 const char *default_text, int x, int y,
348 int text_w, int list_h, int list_format = LISTBOX_TEXT);
349 virtual ~BC_PopupTextBox();
350 int create_objects();
351 virtual int handle_event();
353 const char *get_text();
354 const wchr_t *get_wtext();
356 void set_number(int v);
361 int get_show_query();
362 void set_show_query(int v);
363 int get_back_color();
364 void set_back_color(int v);
366 void set_tooltip(const char *text);
368 void update(const char *text);
369 void update_list(ArrayList<BC_ListBoxItem*> *data);
370 void reposition_window(int x, int y);
372 friend class BC_PopupTextBoxText;
373 friend class BC_PopupTextBoxList;
376 int x, y, text_w, list_h;
377 int list_format, list_item;
378 const char *default_text;
379 const wchr_t *default_wtext;
380 ArrayList<BC_ListBoxItem*> *list_items;
381 BC_PopupTextBoxText *textbox;
382 BC_PopupTextBoxList *listbox;
383 BC_WindowBase *parent_window;
386 class BC_PopupTextBoxText : public BC_TextBox
389 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
390 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchr_t *wtext);
391 virtual ~BC_PopupTextBoxText();
393 BC_PopupTextBox *popup;
396 class BC_PopupTextBoxList : public BC_ListBox
399 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
401 BC_PopupTextBox *popup;
405 class BC_TumbleTextBox;
406 class BC_TumbleTextBoxText;
407 class BC_FTextTumbler;
408 class BC_ITextTumbler;
411 class BC_TumbleTextBox
414 BC_TumbleTextBox(BC_WindowBase *parent_window,
415 int64_t default_value, int64_t min, int64_t max,
416 int x, int y, int text_w);
417 BC_TumbleTextBox(BC_WindowBase *parent_window,
418 int default_value, int min, int max,
419 int x, int y, int text_w);
420 BC_TumbleTextBox(BC_WindowBase *parent_window,
421 float default_value, float min, float max,
422 int x, int y, int text_w, int precision=4);
423 virtual ~BC_TumbleTextBox();
425 int create_objects();
427 virtual int handle_event();
428 virtual int handle_up_event();
429 virtual int handle_down_event();
430 const char* get_text();
431 const wchr_t* get_wtext();
432 BC_TextBox* get_textbox();
433 int update(const char *value);
434 int update(int64_t value);
435 int update(float value);
440 void disable(int hide_text=0);
443 void reposition_window(int x, int y);
444 void set_boundaries(int64_t min, int64_t max);
445 void set_boundaries(float min, float max);
446 void set_precision(int precision);
447 void set_increment(float value);
448 void set_log_floatincrement(int value);
449 void set_tooltip(const char *text);
451 friend class BC_TumbleTextBoxText;
452 friend class BC_FTextTumbler;
453 friend class BC_ITextTumbler;
457 int64_t default_value, min, max;
458 float default_value_f, min_f, max_f;
462 int log_floatincrement;
463 BC_TumbleTextBoxText *textbox;
465 BC_WindowBase *parent_window;
468 class BC_TumbleTextBoxText : public BC_TextBox
471 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
472 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y, int precision);
473 virtual ~BC_TumbleTextBoxText();
475 int button_press_event();
476 BC_TumbleTextBox *popup;
479 class BC_FTextTumbler : public BC_FTumbler
482 BC_FTextTumbler(BC_TumbleTextBox *tumble_text,
483 float min_f, float max_f, int x, int y)
484 : BC_FTumbler(tumble_text->textbox, min_f, max_f, x, y) {
485 this->tumble_text = tumble_text;
487 ~BC_FTextTumbler() {}
488 int handle_up_event() { return tumble_text->handle_up_event(); }
489 int handle_down_event() { return tumble_text->handle_down_event(); }
491 BC_TumbleTextBox *tumble_text;
494 class BC_ITextTumbler : public BC_ITumbler
497 BC_ITextTumbler(BC_TumbleTextBox *tumble_text,
498 int64_t min, int64_t max, int x, int y)
499 : BC_ITumbler(tumble_text->textbox, min, max, x, y) {
500 this->tumble_text = tumble_text;
502 ~BC_ITextTumbler() {}
503 int handle_up_event() { return tumble_text->handle_up_event(); }
504 int handle_down_event() { return tumble_text->handle_down_event(); }
506 BC_TumbleTextBox *tumble_text;
510 class BC_TextMenu : public BC_PopupMenu
513 BC_TextMenu(BC_TextBox *textbox);
516 void create_objects();
521 class BC_TextMenuCut : public BC_MenuItem
524 BC_TextMenuCut(BC_TextMenu *menu);
529 class BC_TextMenuCopy : public BC_MenuItem
532 BC_TextMenuCopy(BC_TextMenu *menu);
537 class BC_TextMenuPaste : public BC_MenuItem
540 BC_TextMenuPaste(BC_TextMenu *menu);