titler rework, some code cleanup and fixes
[goodguy/history.git] / cinelerra-5.1 / guicast / bctextbox.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifndef BCTEXTBOX_H
23 #define BCTEXTBOX_H
24
25 #include "bclistbox.h"
26 #include "bcsubwindow.h"
27 #include "bctumble.h"
28 #include "fonts.h"
29 #include "bctextbox.inc"
30 #include "bctimer.inc"
31
32 #define BCCURSORW 2
33
34
35
36
37 class BC_TextBoxSuggestions;
38 class BC_ScrollTextBoxYScroll;
39
40
41 class BC_TextBox : public BC_SubWindow
42 {
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;
47                 else ch = -1;
48                 return ch;
49         }
50
51         char *text;
52         wchar_t *wtext;
53 public:
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();
69
70
71         friend class BC_TextBoxSuggestions;
72
73
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);
84         void disable();
85         void enable();
86         int get_enabled();
87         int get_rows();
88
89         int initialize();
90
91         int focus_in_event();
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);
99         int keypress_event();
100         int activate();
101         int deactivate();
102         const char* get_text();
103         const wchar_t* get_wtext();
104         void set_text(char *text, int isz);
105         int get_text_rows();
106 // Set top left of text view
107         void set_text_row(int row);
108         int get_text_row();
109
110         int reposition_window(int x, int y, int w = -1, int rows = -1);
111         int uses_text();
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);
116
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);
129
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);
133
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);
137
138
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;
147
148 private:
149         int reset_parameters(int rows, int has_border, int font, int size);
150         void draw(int flush);
151         void draw_border();
152         void draw_cursor();
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);
169         int tstrlen();
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);
174         int wtext_update();
175
176 // Top left of text relative to window
177         int text_x, text_y;
178 // Top left of cursor relative to text
179         int ibeam_x, ibeam_y;
180
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;
188         int has_border;
189         int font;
190         int rows;
191         int highlighted;
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];
197         int is_utf8;
198         int active;
199         int enabled;
200         int precision;
201         int keypress_draw;
202 // Cause the repeater to skip a cursor refresh if a certain event happened
203 // within a certain time of the last repeat event
204         Timer *skip_cursor;
205 // Used for custom formatting text boxes
206         int last_keypress;
207         const char *separators;
208         ArrayList<BC_ListBoxItem*> *suggestions;
209         BC_TextBoxSuggestions *suggestions_popup;
210         int suggestion_column;
211 };
212
213
214
215 class BC_TextBoxSuggestions : public BC_ListBox
216 {
217 public:
218         BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y);
219         virtual ~BC_TextBoxSuggestions();
220
221         int handle_event();
222
223         BC_TextBox *text_box;
224 };
225
226
227
228 class BC_ScrollTextBoxText;
229 class BC_ScrollTextBoxYScroll;
230
231
232 class BC_ScrollTextBox
233 {
234         BC_ScrollTextBoxText *text;
235         BC_ScrollTextBoxYScroll *yscroll;
236         BC_WindowBase *parent_window;
237         const char *default_text;
238         const wchar_t *default_wtext;
239         int default_size;
240         int x, y, w, rows;
241
242         friend class BC_ScrollTextBoxText;
243         friend class BC_ScrollTextBoxYScroll;
244 public:
245         BC_ScrollTextBox(BC_WindowBase *parent_window,
246                 int x, int y, int w, int rows,
247                 const char *default_text, int default_size=BCTEXTLEN);
248         BC_ScrollTextBox(BC_WindowBase *parent_window,
249                 int x, int y, int w, int rows,
250                 const wchar_t *default_wtext, int default_size=BCTEXTLEN);
251         virtual ~BC_ScrollTextBox();
252         void create_objects();
253         virtual int handle_event();
254         virtual int button_press_event();
255         virtual int button_release_event();
256         int get_buttonpress();
257
258         const char* get_text();
259         const wchar_t* get_wtext();
260         void set_text(char *text, int isz);
261         int set_text_row(int n);
262         void update(const char *text);
263         void update(const wchar_t *wtext);
264         void set_selection(int char1, int char2, int ibeam);
265         void wset_selection(int char1, int char2, int ibeam);
266         void reposition_window(int x, int y, int w, int rows);
267
268         int get_x();
269         int get_y();
270         int get_w();
271         int get_h();
272 // Visible rows for resizing
273         int get_rows();
274         int get_ibeam_letter();
275 };
276
277 class BC_ScrollTextBoxText : public BC_TextBox
278 {
279 public:
280         BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text);
281         BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext);
282         virtual ~BC_ScrollTextBoxText();
283         int handle_event();
284         int motion_event();
285         BC_ScrollTextBox *gui;
286         int button_press_event() { return gui->button_press_event(); }
287         int button_release_event() { return gui->button_release_event(); }
288 };
289
290 class BC_ScrollTextBoxYScroll : public BC_ScrollBar
291 {
292 public:
293         BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui);
294         virtual ~BC_ScrollTextBoxYScroll();
295         int handle_event();
296         BC_ScrollTextBox *gui;
297 };
298
299
300
301
302 class BC_PopupTextBoxText;
303 class BC_PopupTextBoxList;
304
305 class BC_PopupTextBox
306 {
307 public:
308         BC_PopupTextBox(BC_WindowBase *parent_window,
309                 ArrayList<BC_ListBoxItem*> *list_items,
310                 const char *default_text, int x, int y,
311                 int text_w, int list_h, int list_format = LISTBOX_TEXT);
312         virtual ~BC_PopupTextBox();
313         int create_objects();
314         virtual int handle_event();
315         const char* get_text();
316         const wchar_t* get_wtext();
317         int get_number();
318         int get_x();
319         int get_y();
320         int get_w();
321         int get_h();
322         void update(const char *text);
323         void update_list(ArrayList<BC_ListBoxItem*> *data);
324         void reposition_window(int x, int y);
325
326         friend class BC_PopupTextBoxText;
327         friend class BC_PopupTextBoxList;
328
329 private:
330         int x, y, text_w, list_h;
331         int list_format;
332         const char *default_text;
333         const wchar_t *default_wtext;
334         ArrayList<BC_ListBoxItem*> *list_items;
335         BC_PopupTextBoxText *textbox;
336         BC_PopupTextBoxList *listbox;
337         BC_WindowBase *parent_window;
338 };
339
340 class BC_PopupTextBoxText : public BC_TextBox
341 {
342 public:
343         BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text);
344         BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext);
345         virtual ~BC_PopupTextBoxText();
346         int handle_event();
347         BC_PopupTextBox *popup;
348 };
349
350 class BC_PopupTextBoxList : public BC_ListBox
351 {
352 public:
353         BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y);
354         int handle_event();
355         BC_PopupTextBox *popup;
356 };
357
358
359 class BC_TumbleTextBoxText;
360 class BC_TumbleTextBoxTumble;
361
362 class BC_TumbleTextBox
363 {
364 public:
365         BC_TumbleTextBox(BC_WindowBase *parent_window,
366                 int64_t default_value,
367                 int64_t min,
368                 int64_t max,
369                 int x,
370                 int y,
371                 int text_w);
372         BC_TumbleTextBox(BC_WindowBase *parent_window,
373                 int default_value,
374                 int min,
375                 int max,
376                 int x,
377                 int y,
378                 int text_w);
379         BC_TumbleTextBox(BC_WindowBase *parent_window,
380                 float default_value,
381                 float min,
382                 float max,
383                 int x,
384                 int y,
385                 int text_w);
386         virtual ~BC_TumbleTextBox();
387
388         int create_objects();
389         void reset();
390         virtual int handle_event();
391         const char* get_text();
392         const wchar_t* get_wtext();
393         BC_TextBox* get_textbox();
394         int update(const char *value);
395         int update(int64_t value);
396         int update(float value);
397         int get_x();
398         int get_y();
399         int get_w();
400         int get_h();
401         void disable(int hide_text=0);
402         void enable();
403         int get_enabled();
404         void reposition_window(int x, int y);
405         void set_boundaries(int64_t min, int64_t max);
406         void set_boundaries(float min, float max);
407         void set_precision(int precision);
408         void set_increment(float value);
409         void set_log_floatincrement(int value);
410
411         friend class BC_TumbleTextBoxText;
412         friend class BC_TumbleTextBoxTumble;
413
414 private:
415         int x, y, text_w;
416         int64_t default_value, min, max;
417         float default_value_f, min_f, max_f;
418         int use_float;
419         int precision;
420         float increment;
421         int log_floatincrement;
422         BC_TumbleTextBoxText *textbox;
423         BC_Tumbler *tumbler;
424         BC_WindowBase *parent_window;
425 };
426
427 class BC_TumbleTextBoxText : public BC_TextBox
428 {
429 public:
430         BC_TumbleTextBoxText(BC_TumbleTextBox *popup, int64_t default_value, int x, int y);
431         BC_TumbleTextBoxText(BC_TumbleTextBox *popup, float default_value, int x, int y);
432         virtual ~BC_TumbleTextBoxText();
433         int handle_event();
434         int button_press_event();
435         BC_TumbleTextBox *popup;
436 };
437
438
439 #endif