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);
120 int get_back_color();
121 void set_back_color(int v);
123 int reposition_window(int x, int y, int w = -1, int rows = -1);
125 int cut(int do_update);
126 int copy(int do_update);
127 int paste(int do_update);
129 static int calculate_h(BC_WindowBase *gui, int font, int has_border, int rows);
130 static int calculate_row_h(int rows, BC_WindowBase *parent_window, int has_border = 1, int font = MEDIUMFONT);
131 static int pixels_to_rows(BC_WindowBase *window, int font, int pixels);
132 static int wcpos(const char *text, const char *ep);
134 void set_precision(int precision);
135 // Whether to draw every time there is a keypress or rely on user to
136 // follow up every keypress with an update().
137 void set_keypress_draw(int value);
138 int get_ibeam_letter();
139 void set_ibeam_letter(int number, int redraw = 1);
140 // Used for custom formatting text boxes
141 int get_last_keypress();
142 // Table of separators to skip. Used by time textboxes
143 // The separator format is "0000:0000". Things not alnum are considered
144 // separators. The alnums are replaced by user text.
145 void set_separators(const char *separators);
147 // 1 - selects text, -1 - deselects, 0 - do nothing
148 // in all cases it returns text_selected after the operation
149 int select_whole_text(int select);
151 // Compute suggestions for a path
152 // If entries is null, just search absolute paths
153 int calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries=0, const char *filter=0);
156 // User computes suggestions after handle_event.
157 // The array is copied to a local variable.
158 // A highlighted extension is added if 1 suggestion or a popup appears
159 // if multiple suggestions.
160 // column - starting column to replace
161 void no_suggestions();
162 void set_suggestions(ArrayList<char*> *suggestions, int column);
164 BC_ScrollTextBoxXScroll *xscroll;
165 BC_ScrollTextBoxYScroll *yscroll;
167 int reset_parameters(int rows, int has_border, int font, int size);
169 void draw(int flush);
172 void copy_selection(int clipboard_num);
173 void paste_selection(int clipboard_num);
174 void delete_selection(int letter1, int letter2, int text_len);
175 void insert_text(const wchar_t *wcp, int len=-1);
176 // Reformat text according to separators.
177 // ibeam_left causes the ibeam to move left.
178 int is_separator(const char *txt, int i);
179 void do_separators(int ibeam_left);
180 int get_x_position(int i, int start=0);
181 void get_ibeam_position(int &x, int &y);
182 void find_ibeam(int dispatch_event);
183 void select_word(int &letter1, int &letter2, int ibeam_letter);
184 void select_line(int &letter1, int &letter2, int ibeam_letter);
185 int get_cursor_letter(int cursor_x, int cursor_y);
186 int get_cursor_letter2(int cursor_x, int cursor_y);
187 int get_row_h(int rows);
188 void default_keypress(int &dispatch_event, int &result);
190 int tstrcmp(const char *cp);
191 char *tstrcpy(const char *cp);
192 char *tstrcat(const char *cp);
193 int text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz);
196 // Top left of text relative to window
198 // Top left of cursor relative to text
199 int ibeam_x, ibeam_y;
201 int ibeam_letter, unicode_active;
202 int highlight_letter1, highlight_letter2;
203 int highlight_letter3, highlight_letter4;
204 int text_x1, text_start, text_end;
205 int text_selected, word_selected, line_selected;
206 int text_ascent, text_descent, text_height;
207 int left_margin, right_margin, top_margin, bottom_margin;
212 int high_color, back_color;
213 int background_color;
214 int size, tsize, dirty;
216 char temp_string[KEYPRESSLEN];
222 // Cause the repeater to skip a cursor refresh if a certain event happened
223 // within a certain time of the last repeat event
225 // Used for custom formatting text boxes
227 const char *separators;
228 ArrayList<BC_ListBoxItem*> *suggestions;
229 BC_TextBoxSuggestions *suggestions_popup;
230 int suggestion_column;
231 int selection_active;
236 class BC_TextBoxSuggestions : public BC_ListBox
239 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
240 virtual ~BC_TextBoxSuggestions();
244 BC_TextBox *text_box;
248 class BC_ScrollTextBox
250 BC_ScrollTextBoxText *text;
251 BC_ScrollTextBoxXScroll *xscroll;
252 BC_ScrollTextBoxYScroll *yscroll;
253 BC_WindowBase *parent_window;
254 const char *default_text;
255 const wchar_t *default_wtext;
259 friend class BC_ScrollTextBoxText;
260 friend class BC_ScrollTextBoxXScroll;
261 friend class BC_ScrollTextBoxYScroll;
263 BC_ScrollTextBox(BC_WindowBase *parent_window,
264 int x, int y, int w, int rows,
265 const char *default_text, int default_size=BCTEXTLEN);
266 BC_ScrollTextBox(BC_WindowBase *parent_window,
267 int x, int y, int w, int rows,
268 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
269 virtual ~BC_ScrollTextBox();
270 void create_objects();
272 virtual int handle_event() { return 1; }
273 virtual int button_press_event();
274 virtual int button_release_event();
276 void set_text(char *text, int isz);
277 int set_text_row(int n);
278 void update(const char *text);
279 void update(const wchar_t *wtext);
280 void reposition_window(int x, int y, int w, int rows);
281 void update_scrollbars();
283 int get_x() { return x; }
284 int get_y() { return y; }
285 int get_w() { return w; }
286 // Visible rows for resizing
287 int get_rows() { return rows; }
289 void set_x_pos(int x);
292 const char *get_text();
293 const wchar_t *get_wtext();
294 int get_buttonpress();
295 void wset_selection(int char1, int char2, int ibeam);
296 void set_selection(int char1, int char2, int ibeam);
297 int get_ibeam_letter();
300 class BC_ScrollTextBoxText : public BC_TextBox
303 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
304 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
305 virtual ~BC_ScrollTextBoxText();
308 BC_ScrollTextBox *gui;
309 int button_press_event() { return gui->button_press_event(); }
310 int button_release_event() { return gui->button_release_event(); }
313 class BC_ScrollTextBoxXScroll : public BC_ScrollBar
316 BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui);
317 virtual ~BC_ScrollTextBoxXScroll();
319 BC_ScrollTextBox *gui;
322 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
325 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
326 virtual ~BC_ScrollTextBoxYScroll();
328 BC_ScrollTextBox *gui;
334 class BC_PopupTextBoxText;
335 class BC_PopupTextBoxList;
337 class BC_PopupTextBox
340 BC_PopupTextBox(BC_WindowBase *parent_window,
341 ArrayList<BC_ListBoxItem*> *list_items,
342 const char *default_text, int x, int y,
343 int text_w, int list_h, int list_format = LISTBOX_TEXT);
344 virtual ~BC_PopupTextBox();
345 int create_objects();
346 virtual int handle_event();
347 const char* get_text();
348 const wchar_t* get_wtext();
354 int get_show_query();
355 void set_show_query(int v);
357 void update(const char *text);
358 void update_list(ArrayList<BC_ListBoxItem*> *data);
359 void reposition_window(int x, int y);
361 friend class BC_PopupTextBoxText;
362 friend class BC_PopupTextBoxList;
365 int x, y, text_w, list_h;
367 const char *default_text;
368 const wchar_t *default_wtext;
369 ArrayList<BC_ListBoxItem*> *list_items;
370 BC_PopupTextBoxText *textbox;
371 BC_PopupTextBoxList *listbox;
372 BC_WindowBase *parent_window;
375 class BC_PopupTextBoxText : public BC_TextBox
378 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
379 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
380 virtual ~BC_PopupTextBoxText();
382 BC_PopupTextBox *popup;
385 class BC_PopupTextBoxList : public BC_ListBox
388 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
390 BC_PopupTextBox *popup;
394 class BC_TumbleTextBoxText;
395 class BC_TumbleTextBoxTumble;
397 class BC_TumbleTextBox
400 BC_TumbleTextBox(BC_WindowBase *parent_window,
401 int64_t default_value,
407 BC_TumbleTextBox(BC_WindowBase *parent_window,
414 BC_TumbleTextBox(BC_WindowBase *parent_window,
421 virtual ~BC_TumbleTextBox();
423 int create_objects();
425 virtual int handle_event();
426 const char* get_text();
427 const wchar_t* get_wtext();
428 BC_TextBox* get_textbox();
429 int update(const char *value);
430 int update(int64_t value);
431 int update(float value);
436 void disable(int hide_text=0);
439 void reposition_window(int x, int y);
440 void set_boundaries(int64_t min, int64_t max);
441 void set_boundaries(float min, float max);
442 void set_precision(int precision);
443 void set_increment(float value);
444 void set_log_floatincrement(int value);
445 void set_tooltip(const char *text);
447 friend class BC_TumbleTextBoxText;
448 friend class BC_TumbleTextBoxTumble;
452 int64_t default_value, min, max;
453 float default_value_f, min_f, max_f;
457 int log_floatincrement;
458 BC_TumbleTextBoxText *textbox;
460 BC_WindowBase *parent_window;
463 class BC_TumbleTextBoxText : public BC_TextBox
466 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
467 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y, int precision);
468 virtual ~BC_TumbleTextBoxText();
470 int button_press_event();
471 BC_TumbleTextBox *popup;
475 class BC_TextMenu : public BC_PopupMenu
478 BC_TextMenu(BC_TextBox *textbox);
481 void create_objects();
486 class BC_TextMenuCut : public BC_MenuItem
489 BC_TextMenuCut(BC_TextMenu *menu);
494 class BC_TextMenuCopy : public BC_MenuItem
497 BC_TextMenuCopy(BC_TextMenu *menu);
502 class BC_TextMenuPaste : public BC_MenuItem
505 BC_TextMenuPaste(BC_TextMenu *menu);