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 void get_ibeam_position(int &x, int &y);
162 void find_ibeam(int dispatch_event);
163 void select_word(int &letter1, int &letter2, int ibeam_letter);
164 void select_line(int &letter1, int &letter2, int ibeam_letter);
165 int get_cursor_letter(int cursor_x, int cursor_y);
166 int get_cursor_letter2(int cursor_x, int cursor_y);
167 int get_row_h(int rows);
168 void default_keypress(int &dispatch_event, int &result);
170 int tstrcmp(const char *cp);
171 char *tstrcpy(const char *cp);
172 char *tstrcat(const char *cp);
173 int text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz);
176 // Top left of text relative to window
178 // Top left of cursor relative to text
179 int ibeam_x, ibeam_y;
181 int ibeam_letter, unicode_active;
182 int highlight_letter1, highlight_letter2;
183 int highlight_letter3, highlight_letter4;
184 int text_x1, text_start, text_end;
185 int text_selected, word_selected, line_selected;
186 int text_ascent, text_descent, text_height;
187 int left_margin, right_margin, top_margin, bottom_margin;
192 int high_color, back_color;
193 int background_color;
194 int size, tsize, dirty;
195 int wlen, wsize, *positions, plen;
196 char temp_string[KEYPRESSLEN];
202 // Cause the repeater to skip a cursor refresh if a certain event happened
203 // within a certain time of the last repeat event
205 // Used for custom formatting text boxes
207 const char *separators;
208 ArrayList<BC_ListBoxItem*> *suggestions;
209 BC_TextBoxSuggestions *suggestions_popup;
210 int suggestion_column;
215 class BC_TextBoxSuggestions : public BC_ListBox
218 BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
219 virtual ~BC_TextBoxSuggestions();
223 BC_TextBox *text_box;
228 class BC_ScrollTextBoxText;
229 class BC_ScrollTextBoxYScroll;
232 class BC_ScrollTextBox
235 BC_ScrollTextBox(BC_WindowBase *parent_window,
236 int x, int y, int w, int rows,
237 const char *default_text, int default_size=BCTEXTLEN);
238 BC_ScrollTextBox(BC_WindowBase *parent_window,
239 int x, int y, int w, int rows,
240 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
241 virtual ~BC_ScrollTextBox();
242 void create_objects();
243 virtual int handle_event();
245 const char* get_text();
246 const wchar_t* get_wtext();
247 void set_text(char *text, int isz);
248 int set_text_row(int n);
249 void update(const char *text);
250 void update(const wchar_t *wtext);
251 void set_selection(int char1, int char2, int ibeam);
252 void wset_selection(int char1, int char2, int ibeam);
253 void reposition_window(int x, int y, int w, int rows);
258 // Visible rows for resizing
260 int get_ibeam_letter();
262 friend class BC_ScrollTextBoxText;
263 friend class BC_ScrollTextBoxYScroll;
266 BC_ScrollTextBoxText *text;
267 BC_ScrollTextBoxYScroll *yscroll;
268 BC_WindowBase *parent_window;
269 const char *default_text;
270 const wchar_t *default_wtext;
275 class BC_ScrollTextBoxText : public BC_TextBox
278 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
279 BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
280 virtual ~BC_ScrollTextBoxText();
283 BC_ScrollTextBox *gui;
286 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
289 BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
290 virtual ~BC_ScrollTextBoxYScroll();
292 BC_ScrollTextBox *gui;
298 class BC_PopupTextBoxText;
299 class BC_PopupTextBoxList;
301 class BC_PopupTextBox
304 BC_PopupTextBox(BC_WindowBase *parent_window,
305 ArrayList<BC_ListBoxItem*> *list_items,
306 const char *default_text, int x, int y,
307 int text_w, int list_h, int list_format = LISTBOX_TEXT);
308 virtual ~BC_PopupTextBox();
309 int create_objects();
310 virtual int handle_event();
311 const char* get_text();
312 const wchar_t* get_wtext();
318 void update(const char *text);
319 void update_list(ArrayList<BC_ListBoxItem*> *data);
320 void reposition_window(int x, int y);
322 friend class BC_PopupTextBoxText;
323 friend class BC_PopupTextBoxList;
326 int x, y, text_w, list_h;
328 const char *default_text;
329 const wchar_t *default_wtext;
330 ArrayList<BC_ListBoxItem*> *list_items;
331 BC_PopupTextBoxText *textbox;
332 BC_PopupTextBoxList *listbox;
333 BC_WindowBase *parent_window;
336 class BC_PopupTextBoxText : public BC_TextBox
339 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
340 BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
341 virtual ~BC_PopupTextBoxText();
343 BC_PopupTextBox *popup;
346 class BC_PopupTextBoxList : public BC_ListBox
349 BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
351 BC_PopupTextBox *popup;
355 class BC_TumbleTextBoxText;
356 class BC_TumbleTextBoxTumble;
358 class BC_TumbleTextBox
361 BC_TumbleTextBox(BC_WindowBase *parent_window,
362 int64_t default_value,
368 BC_TumbleTextBox(BC_WindowBase *parent_window,
375 BC_TumbleTextBox(BC_WindowBase *parent_window,
382 virtual ~BC_TumbleTextBox();
384 int create_objects();
386 virtual int handle_event();
387 const char* get_text();
388 const wchar_t* get_wtext();
389 BC_TextBox* get_textbox();
390 int update(const char *value);
391 int update(int64_t value);
392 int update(float value);
397 void disable(int hide_text=0);
400 void reposition_window(int x, int y);
401 void set_boundaries(int64_t min, int64_t max);
402 void set_boundaries(float min, float max);
403 void set_precision(int precision);
404 void set_increment(float value);
405 void set_log_floatincrement(int value);
407 friend class BC_TumbleTextBoxText;
408 friend class BC_TumbleTextBoxTumble;
412 int64_t default_value, min, max;
413 float default_value_f, min_f, max_f;
417 int log_floatincrement;
418 BC_TumbleTextBoxText *textbox;
420 BC_WindowBase *parent_window;
423 class BC_TumbleTextBoxText : public BC_TextBox
426 BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
427 int64_t default_value,
432 BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
438 BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int x, int y);
439 virtual ~BC_TumbleTextBoxText();
441 int button_press_event();
442 BC_TumbleTextBox *popup;