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;
59 BC_TextBox(int x, int y, int w, int rows, int size, char *text,
60 int has_border=1, int font=MEDIUMFONT);
61 BC_TextBox(int x, int y, int w, int rows, int size, wchar_t *wtext,
62 int has_border=1, int font=MEDIUMFONT);
63 BC_TextBox(int x, int y, int w, int rows, const char *text,
64 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
65 BC_TextBox(int x, int y, int w, int rows, const wchar_t *wtext,
66 int has_border=1, int font=MEDIUMFONT, int is_utf8=1);
67 BC_TextBox(int x, int y, int w, int rows, int64_t text,
68 int has_border=1, int font=MEDIUMFONT);
69 BC_TextBox(int x, int y, int w, int rows, float text,
70 int has_border=1, int font=MEDIUMFONT, int precision=4);
71 BC_TextBox(int x, int y, int w, int rows,
72 int text, int has_border=1, int font=MEDIUMFONT);
73 virtual ~BC_TextBox();
76 friend class BC_TextBoxSuggestions;
77 friend class BC_ScrollTextBox;
80 // Whenever the contents of the text change
81 virtual int handle_event() { return 0; };
82 // Whenever the position of the text changes
83 virtual int motion_event() { return 0; };
84 void set_selection(int char1, int char2, int ibeam);
85 void wset_selection(int char1, int char2, int ibeam);
86 int update(const char *text);
87 int update(const wchar_t *wtext);
88 int update(int64_t value);
89 int update(float value);
98 int focus_out_event();
99 int cursor_enter_event();
100 int cursor_leave_event();
101 int cursor_motion_event();
102 virtual int button_press_event();
103 int button_release_event();
104 int repeat_event(int64_t repeat_id);
105 int keypress_event();
106 int selection_clear_event();
109 const char* get_text();
110 const wchar_t* get_wtext();
111 void set_text(char *text, int isz);
113 // Set top left of text view
114 void set_text_row(int row);
118 void set_text_x(int v);
119 void set_text_y(int v);
121 int reposition_window(int x, int y, int w = -1, int rows = -1);
123 int cut(int do_update);
124 int copy(int do_update);
125 int paste(int do_update);
127 static int calculate_h(BC_WindowBase *gui, int font, int has_border, int rows);
128 static int calculate_row_h(int rows, BC_WindowBase *parent_window, int has_border = 1, int font = MEDIUMFONT);
129 static int pixels_to_rows(BC_WindowBase *window, int font, int pixels);
130 static int wcpos(const char *text, const char *ep);
132 void set_precision(int precision);
133 // Whether to draw every time there is a keypress or rely on user to
134 // follow up every keypress with an update().
135 void set_keypress_draw(int value);
136 int get_ibeam_letter();
137 void set_ibeam_letter(int number, int redraw = 1);
138 // Used for custom formatting text boxes
139 int get_last_keypress();
140 // Table of separators to skip. Used by time textboxes
141 // The separator format is "0000:0000". Things not alnum are considered
142 // separators. The alnums are replaced by user text.
143 void set_separators(const char *separators);
145 // 1 - selects text, -1 - deselects, 0 - do nothing
146 // in all cases it returns text_selected after the operation
147 int select_whole_text(int select);
149 // Compute suggestions for a path
150 // If entries is null, just search absolute paths
151 int calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries=0, const char *filter=0);
154 // User computes suggestions after handle_event.
155 // The array is copied to a local variable.
156 // A highlighted extension is added if 1 suggestion or a popup appears
157 // if multiple suggestions.
158 // column - starting column to replace
159 void no_suggestions();
160 void set_suggestions(ArrayList<char*> *suggestions, int column);
162 BC_ScrollTextBoxXScroll *xscroll;
163 BC_ScrollTextBoxYScroll *yscroll;
165 int reset_parameters(int rows, int has_border, int font, int size);
167 void draw(int flush);
170 void copy_selection(int clipboard_num);
171 void paste_selection(int clipboard_num);
172 void delete_selection(int letter1, int letter2, int text_len);
173 void insert_text(const wchar_t *wcp, int len=-1);
174 // Reformat text according to separators.
175 // ibeam_left causes the ibeam to move left.
176 int is_separator(const char *txt, int i);
177 void do_separators(int ibeam_left);
178 int get_x_position(int i, int start=0);
179 void get_ibeam_position(int &x, int &y);
180 void find_ibeam(int dispatch_event);
181 void select_word(int &letter1, int &letter2, int ibeam_letter);
182 void select_line(int &letter1, int &letter2, int ibeam_letter);
183 int get_cursor_letter(int cursor_x, int cursor_y);
184 int get_cursor_letter2(int cursor_x, int cursor_y);
185 int get_row_h(int rows);
186 void default_keypress(int &dispatch_event, int &result);
188 int tstrcmp(const char *cp);
189 char *tstrcpy(const char *cp);
190 char *tstrcat(const char *cp);
191 int text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz);
194 // Top left of text relative to window
196 // Top left of cursor relative to text
197 int ibeam_x, ibeam_y;
199 int ibeam_letter, unicode_active;
200 int highlight_letter1, highlight_letter2;
201 int highlight_letter3, highlight_letter4;
202 int text_x1, text_start, text_end;
203 int text_selected, word_selected, line_selected;
204 int text_ascent, text_descent, text_height;
205 int left_margin, right_margin, top_margin, bottom_margin;
210 int high_color, back_color;
211 int background_color;
212 int size, tsize, dirty;
214 char temp_string[KEYPRESSLEN];
220 // Cause the repeater to skip a cursor refresh if a certain event happened
221 // within a certain time of the last repeat event
223 // Used for custom formatting text boxes
225 const char *separators;
226 ArrayList<BC_ListBoxItem*> *suggestions;
227 BC_TextBoxSuggestions *suggestions_popup;
228 int suggestion_column;
229 int selection_active;
234 class BC_TextBoxSuggestions : public BC_ListBox
237 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
238 virtual ~BC_TextBoxSuggestions();
242 BC_TextBox *text_box;
246 class BC_ScrollTextBox
248 BC_ScrollTextBoxText *text;
249 BC_ScrollTextBoxXScroll *xscroll;
250 BC_ScrollTextBoxYScroll *yscroll;
251 BC_WindowBase *parent_window;
252 const char *default_text;
253 const wchar_t *default_wtext;
257 friend class BC_ScrollTextBoxText;
258 friend class BC_ScrollTextBoxXScroll;
259 friend class BC_ScrollTextBoxYScroll;
261 BC_ScrollTextBox(BC_WindowBase *parent_window,
262 int x, int y, int w, int rows,
263 const char *default_text, int default_size=BCTEXTLEN);
264 BC_ScrollTextBox(BC_WindowBase *parent_window,
265 int x, int y, int w, int rows,
266 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
267 virtual ~BC_ScrollTextBox();
268 void create_objects();
270 virtual int handle_event() { return 1; }
271 virtual int button_press_event();
272 virtual int button_release_event();
274 void set_text(char *text, int isz);
275 int set_text_row(int n);
276 void update(const char *text);
277 void update(const wchar_t *wtext);
278 void reposition_window(int x, int y, int w, int rows);
279 void update_scrollbars();
281 int get_x() { return x; }
282 int get_y() { return y; }
283 int get_w() { return w; }
284 // Visible rows for resizing
285 int get_rows() { return rows; }
287 void set_x_pos(int x);
290 const char *get_text();
291 const wchar_t *get_wtext();
292 int get_buttonpress();
293 void wset_selection(int char1, int char2, int ibeam);
294 void set_selection(int char1, int char2, int ibeam);
295 int get_ibeam_letter();
298 class BC_ScrollTextBoxText : public BC_TextBox
301 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
302 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
303 virtual ~BC_ScrollTextBoxText();
306 BC_ScrollTextBox *gui;
307 int button_press_event() { return gui->button_press_event(); }
308 int button_release_event() { return gui->button_release_event(); }
311 class BC_ScrollTextBoxXScroll : public BC_ScrollBar
314 BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui);
315 virtual ~BC_ScrollTextBoxXScroll();
317 BC_ScrollTextBox *gui;
320 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
323 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
324 virtual ~BC_ScrollTextBoxYScroll();
326 BC_ScrollTextBox *gui;
332 class BC_PopupTextBoxText;
333 class BC_PopupTextBoxList;
335 class BC_PopupTextBox
338 BC_PopupTextBox(BC_WindowBase *parent_window,
339 ArrayList<BC_ListBoxItem*> *list_items,
340 const char *default_text, int x, int y,
341 int text_w, int list_h, int list_format = LISTBOX_TEXT);
342 virtual ~BC_PopupTextBox();
343 int create_objects();
344 virtual int handle_event();
345 const char* get_text();
346 const wchar_t* get_wtext();
352 int get_show_query();
353 void set_show_query(int v);
355 void update(const char *text);
356 void update_list(ArrayList<BC_ListBoxItem*> *data);
357 void reposition_window(int x, int y);
359 friend class BC_PopupTextBoxText;
360 friend class BC_PopupTextBoxList;
363 int x, y, text_w, list_h;
365 const char *default_text;
366 const wchar_t *default_wtext;
367 ArrayList<BC_ListBoxItem*> *list_items;
368 BC_PopupTextBoxText *textbox;
369 BC_PopupTextBoxList *listbox;
370 BC_WindowBase *parent_window;
373 class BC_PopupTextBoxText : public BC_TextBox
376 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
377 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
378 virtual ~BC_PopupTextBoxText();
380 BC_PopupTextBox *popup;
383 class BC_PopupTextBoxList : public BC_ListBox
386 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
388 BC_PopupTextBox *popup;
392 class BC_TumbleTextBoxText;
393 class BC_TumbleTextBoxTumble;
395 class BC_TumbleTextBox
398 BC_TumbleTextBox(BC_WindowBase *parent_window,
399 int64_t default_value,
405 BC_TumbleTextBox(BC_WindowBase *parent_window,
412 BC_TumbleTextBox(BC_WindowBase *parent_window,
419 virtual ~BC_TumbleTextBox();
421 int create_objects();
423 virtual int handle_event();
424 const char* get_text();
425 const wchar_t* get_wtext();
426 BC_TextBox* get_textbox();
427 int update(const char *value);
428 int update(int64_t value);
429 int update(float value);
434 void disable(int hide_text=0);
437 void reposition_window(int x, int y);
438 void set_boundaries(int64_t min, int64_t max);
439 void set_boundaries(float min, float max);
440 void set_precision(int precision);
441 void set_increment(float value);
442 void set_log_floatincrement(int value);
443 void set_tooltip(const char *text);
445 friend class BC_TumbleTextBoxText;
446 friend class BC_TumbleTextBoxTumble;
450 int64_t default_value, min, max;
451 float default_value_f, min_f, max_f;
455 int log_floatincrement;
456 BC_TumbleTextBoxText *textbox;
458 BC_WindowBase *parent_window;
461 class BC_TumbleTextBoxText : public BC_TextBox
464 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
465 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y, int precision);
466 virtual ~BC_TumbleTextBoxText();
468 int button_press_event();
469 BC_TumbleTextBox *popup;
473 class BC_TextMenu : public BC_PopupMenu
476 BC_TextMenu(BC_TextBox *textbox);
479 void create_objects();
484 class BC_TextMenuCut : public BC_MenuItem
487 BC_TextMenuCut(BC_TextMenu *menu);
492 class BC_TextMenuCopy : public BC_MenuItem
495 BC_TextMenuCopy(BC_TextMenu *menu);
500 class BC_TextMenuPaste : public BC_MenuItem
503 BC_TextMenuPaste(BC_TextMenu *menu);