c404877cced5fbcf885be1096435d7df3a5a21b9
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bctextbox.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "bcclipboard.h"
22 #include "bclistboxitem.h"
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "bctextbox.h"
26 #include "clip.h"
27 #include "bccolors.h"
28 #include <ctype.h>
29 #include "cursors.h"
30 #include "filesystem.h"
31 #include "keys.h"
32 #include "language.h"
33 #include <math.h>
34 #include "bctimer.h"
35 #include "vframe.h"
36
37 #include <string.h>
38 #include <unistd.h>
39 #include <wchar.h>
40 #include <wctype.h>
41
42 #define VERTICAL_MARGIN 2
43 #define VERTICAL_MARGIN_NOBORDER 0
44 #define HORIZONTAL_MARGIN 4
45 #define HORIZONTAL_MARGIN_NOBORDER 2
46
47 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
48         int size, char *text, int has_border, int font)
49  : BC_SubWindow(x, y, w, 0, -1)
50 {
51         is_utf8 = 1;
52         skip_cursor = 0;
53         reset_parameters(rows, has_border, font, size);
54         if( size >= 0 )
55                 tstrcpy(text);
56         else    // reference shared directly
57                 this->text = text;
58 }
59
60 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
61         int size, wchar_t *wtext, int has_border, int font)
62  : BC_SubWindow(x, y, w, 0, -1)
63 {
64         is_utf8 = 1;
65         skip_cursor = 0;
66         reset_parameters(rows, has_border, font, size);
67         wdemand(wcslen(wtext));
68         wcsncpy(this->wtext, wtext, wsize);
69 }
70
71 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
72         const char *text, int has_border, int font, int is_utf8)
73  : BC_SubWindow(x, y, w, 0, -1)
74 {
75         this->is_utf8 = is_utf8;
76         skip_cursor = 0;
77         reset_parameters(rows, has_border, font, BCTEXTLEN);
78         tstrcpy(text);
79 }
80
81 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
82         const wchar_t *wtext, int has_border, int font, int is_utf8)
83  : BC_SubWindow(x, y, w, 0, -1)
84 {
85         this->is_utf8 = is_utf8;
86         skip_cursor = 0;
87         reset_parameters(rows, has_border, font, BCTEXTLEN);
88         wsize = BCTEXTLEN;
89         wtext = new wchar_t[wsize+1];
90         wcsncpy(this->wtext, wtext, wsize);
91         this->wtext[wsize] = 0;
92 }
93
94 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
95         int64_t text, int has_border, int font)
96  : BC_SubWindow(x, y, w, 0, -1)
97 {
98         is_utf8 = 1;
99         skip_cursor = 0;
100         reset_parameters(rows, has_border, font, BCSTRLEN);
101         snprintf(this->text, this->tsize, "%jd", text);
102         dirty = 1;  wtext_update();
103 }
104
105 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
106         float text, int has_border, int font, int precision)
107  : BC_SubWindow(x, y, w, 0, -1)
108 {
109         is_utf8 = 1;
110         skip_cursor = 0;
111         reset_parameters(rows, has_border, font, BCSTRLEN);
112         this->precision = precision;
113         snprintf(this->text, this->tsize, "%0.*f", precision, text);
114         dirty = 1;  wtext_update();
115 }
116
117 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
118         int text, int has_border, int font)
119  : BC_SubWindow(x, y, w, 0, -1)
120 {
121         is_utf8 = 1;
122         skip_cursor = 0;
123         reset_parameters(rows, has_border, font, BCSTRLEN);
124         snprintf(this->text, this->tsize, "%d", text);
125         dirty = 1;  wtext_update();
126 }
127
128 BC_TextBox::~BC_TextBox()
129 {
130         if(skip_cursor) delete skip_cursor;
131         delete suggestions_popup;
132         suggestions->remove_all_objects();
133         delete suggestions;
134         delete menu;
135         delete [] wtext;
136         if( size >= 0 )
137                 delete [] text;
138 }
139
140 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
141 {
142         suggestions = new ArrayList<BC_ListBoxItem*>;
143         suggestions_popup = 0;
144         suggestion_column = 0;
145
146         this->rows = rows;
147         this->has_border = has_border;
148         this->font = font;
149 // size > 0: fixed buffer, size == 0: dynamic buffer
150 // size < 0: fixed shared buffer via set_text
151         this->size = size;
152         this->tsize = size >= 0 ? size : -size;
153         this->text = size > 0 ? new char[this->tsize+1] : 0;
154         if( this->text ) this->text[0] = 0;
155         text_start = 0;
156         text_end = 0;
157         highlight_letter1 = highlight_letter2 = 0;
158         highlight_letter3 = highlight_letter4 = 0;
159         ibeam_letter = 0;
160         active = 0;
161         text_selected = 0;
162         word_selected = 0;
163         line_selected = 0;
164         unicode_active = -1;
165         text_x = 0;
166         enabled = 1;
167         highlighted = 0;
168         back_color = -1;
169         high_color = -1;
170         precision = 4;
171         if (!skip_cursor)
172                 skip_cursor = new Timer;
173         wtext = 0;
174         wsize = 0;
175         wlen = 0;
176         keypress_draw = 1;
177         last_keypress = 0;
178         separators = 0;
179         xscroll = 0;
180         yscroll = 0;
181         menu = 0;
182         dirty = 1;
183         selection_active = 0;
184         return 0;
185 }
186
187 int BC_TextBox::tstrlen()
188 {
189         return !text ? 0 : strnlen(text, tsize);
190 }
191
192 int BC_TextBox::tstrcmp(const char *cp)
193 {
194         const char *tp = get_text();
195         return strncmp(tp, cp, tsize);
196 }
197
198 char *BC_TextBox::tstrcpy(const char *cp)
199 {
200         dirty = 1;
201         if( cp ) {
202                 if( !size ) tdemand(strlen(cp));
203                 strncpy(text, cp, tsize);
204         }
205         else if( text )
206                 text[0] = 0;
207         return text;
208 }
209
210 char *BC_TextBox::tstrcat(const char *cp)
211 {
212         dirty = 1;
213         if( !size ) tdemand(tstrlen() + strlen(cp));
214         char *result = strncat(text, cp, tsize);
215         return result;
216 }
217
218 int BC_TextBox::wtext_update()
219 {
220         if( dirty ) {
221                 const char *src_enc = is_utf8 ? "UTF8" : BC_Resources::encoding;
222                 const char *dst_enc = BC_Resources::wide_encoding;
223                 int tlen = tstrlen();
224                 int nsize = tsize > 0 ? tsize : tlen + BCTEXTLEN;
225                 wdemand(nsize);
226                 wlen = BC_Resources::encode(src_enc, dst_enc, text, tlen,
227                         (char*)wtext, wsize*sizeof(wchar_t)) / sizeof(wchar_t);
228                 dirty = 0;
229         }
230         wtext[wlen] = 0;
231         return wlen;
232 }
233
234 int BC_TextBox::text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz)
235 {
236         const char *src_enc = BC_Resources::wide_encoding;
237         const char *dst_enc = BC_Resources::encoding;
238         if( wsz < 0 ) wsz = wcslen(wcp);
239         int len = BC_Resources::encode(src_enc, dst_enc,
240                 (char*)wcp, wsz*sizeof(wchar_t), tcp, tsz);
241         tcp[len] = 0;
242         return len;
243 }
244
245 int BC_TextBox::initialize()
246 {
247
248         if (!skip_cursor)
249                 skip_cursor = new Timer;
250         skip_cursor->update();
251 // Get dimensions
252         text_ascent = get_text_ascent(font) + 1;
253         text_descent = get_text_descent(font) + 1;
254         text_height = text_ascent + text_descent;
255         ibeam_letter = wtext_update();
256         if(has_border)
257         {
258                 left_margin = right_margin = HORIZONTAL_MARGIN;
259                 top_margin = bottom_margin = VERTICAL_MARGIN;
260         }
261         else
262         {
263                 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
264                 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
265         }
266         h = get_row_h(rows);
267         text_x = left_margin;
268         text_y = top_margin;
269         find_ibeam(0);
270
271 // Create the subwindow
272         BC_SubWindow::initialize();
273
274         BC_Resources *resources = get_resources();
275         if(has_border)
276         {
277                 if( back_color < 0 ) back_color = resources->text_background;
278                 if( high_color < 0 ) high_color = resources->text_background_hi;
279         }
280         else
281         {
282                 if( back_color < 0 ) back_color = bg_color;
283                 if( high_color < 0 ) high_color = resources->text_background_noborder_hi;
284         }
285
286         draw(0);
287         set_cursor(IBEAM_CURSOR, 0, 0);
288         show_window(0);
289
290         add_subwindow(menu = new BC_TextMenu(this));
291         menu->create_objects();
292
293         return 0;
294 }
295
296 int BC_TextBox::calculate_h(BC_WindowBase *gui,
297         int font,
298         int has_border,
299         int rows)
300 {
301         return rows * (gui->get_text_ascent(font) + 1 +
302                 gui->get_text_descent(font) + 1) +
303                 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
304 }
305
306 void BC_TextBox::set_precision(int precision)
307 {
308         this->precision = precision;
309 }
310
311 // Compute suggestions for a path
312 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries, const char *filter)
313 {
314 // Let user delete suggestion
315         if(get_last_keypress() != BACKSPACE) {
316 // Compute suggestions
317                 FileSystem fs;
318                 ArrayList<char*> suggestions;
319                 const char *current_text = get_text();
320                 int suggestion_column = 0;
321                 char dirname[BCTEXTLEN];  dirname[0] = 0;
322                 if( current_text[0] == '/' || current_text[0] == '~' )
323                         strncpy(dirname, current_text, sizeof(dirname));
324                 else if( !entries )
325                         getcwd(dirname, sizeof(dirname));
326 // If directory, tabulate it
327                 if( dirname[0] ) {
328                         if( filter ) fs.set_filter(filter);
329                         char *prefix, *cp;
330                         strncpy(dirname, current_text, sizeof(dirname));
331                         if( (cp=strrchr(dirname, '/')) ||
332                             (cp=strrchr(dirname, '~')) ) *++cp = 0;
333                         fs.parse_tildas(dirname);
334                         fs.update(dirname);
335                         cp = (char *)current_text;
336                         if( (prefix=strrchr(cp, '/')) ||
337                             (prefix=strrchr(cp, '~')) ) ++prefix;
338                         suggestion_column = !prefix ? 0 : prefix - cp;
339                         int prefix_len = prefix ? strlen(prefix) : 0;
340 // only include items where the filename matches the basename prefix
341                         for(int i = 0; i < fs.total_files(); i++) {
342                                 char *current_name = fs.get_entry(i)->name;
343                                 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
344                                 suggestions.append(current_name);
345                         }
346                 }
347                 else if(entries) {
348                         char *prefix = (char *)current_text;
349                         int prefix_len = strlen(prefix);
350                         for(int i = 0; i < entries->size(); i++) {
351                                 char *current_name = entries->get(i)->get_text();
352                                 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
353                                 suggestions.append(current_name);
354                         }
355                 }
356                 set_suggestions(&suggestions, suggestion_column);
357         }
358
359         return 1;
360 }
361
362 void BC_TextBox::no_suggestions()
363 {
364         if( suggestions_popup ) {
365                 delete suggestions_popup;
366                 suggestions_popup = 0;
367                 activate();
368         }
369 }
370
371 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
372 {
373 // Copy the array
374         this->suggestions->remove_all_objects();
375         this->suggestion_column = column;
376
377         if(suggestions) {
378                 for(int i = 0; i < suggestions->size(); i++) {
379                         this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
380                 }
381
382 // Show the popup without taking focus
383                 if( suggestions->size() > 1 ) {
384                         if( !suggestions_popup ) {
385                                 suggestions_popup = new BC_TextBoxSuggestions(this, x, y);
386                                 get_parent()->add_subwindow(suggestions_popup);
387                                 suggestions_popup->set_is_suggestions(1);
388                         }
389                         else {
390                                 suggestions_popup->update(this->suggestions, 0, 0, 1);
391                         }
392                         suggestions_popup->activate(0);
393                 }
394                 else
395 // Show the highlighted text
396                 if( suggestions->size() == 1 ) {
397                         highlight_letter1 = wtext_update();
398                         int len = text_update(wtext,wlen, text,tsize);
399                         char *current_suggestion = suggestions->get(0);
400                         int col = len - suggestion_column;
401                         if( col < 0 ) col = 0;
402                         char *cur = current_suggestion + col;
403                         tstrcat(cur);
404                         highlight_letter2 = wtext_update();
405 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
406
407                         draw(1);
408                         no_suggestions();
409                 }
410         }
411
412 // Clear the popup
413         if( !suggestions || !this->suggestions->size() )
414                 no_suggestions();
415 }
416
417 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
418 {
419         highlight_letter1 = char1;
420         highlight_letter2 = char2;
421         ibeam_letter = ibeam;
422         draw(1);
423 }
424
425 // count utf8 chars in text which occur before cp
426 int BC_TextBox::wcpos(const char *text, const char *cp)
427 {
428         int len = 0;
429         const unsigned char *bp = (const unsigned char *)text;
430         const unsigned char *ep = (const unsigned char *)cp;
431         while( bp < ep && *bp != 0 ) {
432                 ++len;
433                 int ch = *bp++;
434                 if( ch < 0x80 ) continue;
435                 int i = ch - 0x0c0;
436                 int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
437                 for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
438         }
439         return len;
440 }
441
442 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
443 {
444         const char *cp = get_text();
445         wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
446 }
447
448 int BC_TextBox::update(const char *text)
449 {
450 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
451 // Don't update if contents are the same
452         int bg_color = has_border || !highlighted ? back_color : high_color;
453         if( bg_color == background_color && !tstrcmp(text)) return 0;
454         tstrcpy(text);
455         int wtext_len = wtext_update();
456         if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
457         if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
458         if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
459         draw(1);
460         return 0;
461 }
462
463 int BC_TextBox::update(const wchar_t *wtext)
464 {
465         int wtext_len = wcslen(wtext);
466         wdemand(wtext_len);
467         wcsncpy(this->wtext, wtext, wsize);
468         this->wlen = wtext_len;
469         if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
470         if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
471         if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
472         draw(1);
473         return 0;
474 }
475
476 int BC_TextBox::update(int64_t value)
477 {
478         char string[BCTEXTLEN];
479         sprintf(string, "%jd", value);
480         update(string);
481         return 0;
482 }
483
484 int BC_TextBox::update(float value)
485 {
486         char string[BCTEXTLEN];
487         sprintf(string, "%0.*f", precision, value);
488         update(string);
489         return 0;
490 }
491
492 void BC_TextBox::disable()
493 {
494         if(enabled) {
495                 enabled = 0;
496                 deactivate();
497                 draw(1);
498         }
499 }
500
501 void BC_TextBox::enable()
502 {
503         if(!enabled) {
504                 enabled = 1;
505                 if(top_level) {
506                         draw(1);
507                 }
508         }
509 }
510
511 int BC_TextBox::get_enabled() { return enabled; }
512 int BC_TextBox::get_text_x() { return text_x; }
513 int BC_TextBox::get_text_y() { return text_y; }
514 void BC_TextBox::set_text_x(int v) { text_x = v; }
515 void BC_TextBox::set_text_y(int v) { text_y = v; }
516 int BC_TextBox::get_back_color() { return back_color; }
517 void BC_TextBox::set_back_color(int v) { back_color = v; }
518
519 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
520 {
521         return (pixels - 4) /
522                 (window->get_text_ascent(font) + 1 +
523                         window->get_text_descent(font) + 1);
524 }
525
526 int BC_TextBox::calculate_row_h(int rows,
527         BC_WindowBase *parent_window,
528         int has_border,
529         int font)
530 {
531         return rows *
532                 (parent_window->get_text_ascent(font) + 1 +
533                 parent_window->get_text_descent(font) + 1) +
534                 (has_border ? 4 : 0);
535 }
536
537 const char* BC_TextBox::get_text()
538 {
539         int wtext_len = wtext_update();
540         text_update(wtext,wtext_len, text,tsize);
541         return text;
542 }
543
544 const wchar_t* BC_TextBox::get_wtext()
545 {
546         wtext_update();
547         return wtext;
548 }
549
550 void BC_TextBox::set_text(char *text, int isz)
551 {
552         if( size < 0 && isz > 0 ) {
553                 this->text = text;
554                 tsize = isz;
555                 size = -isz;
556                 dirty = 1;
557                 wtext_update();
558                 draw(1);
559         }
560 }
561
562 int BC_TextBox::get_text_rows()
563 {
564         int wtext_len = wtext_update();
565         int result = 1;
566         for(int i = 0; i < wtext_len; i++) {
567                 if(wtext[i] == '\n') result++;
568         }
569         return result;
570 }
571
572
573 int BC_TextBox::get_row_h(int rows)
574 {
575         return rows * text_height + top_margin + bottom_margin;
576 }
577
578 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
579 {
580         int new_h = get_h();
581         if(w < 0) w = get_w();
582         if(rows != -1)
583         {
584                 new_h = get_row_h(rows);
585                 this->rows = rows;
586         }
587
588         if(x != get_x() ||
589                 y != get_y() ||
590                 w != get_w() ||
591                 new_h != get_h())
592         {
593 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
594 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
595                 BC_WindowBase::reposition_window(x, y, w, new_h);
596                 draw(0);
597         }
598         return 0;
599 }
600
601 void BC_TextBox::draw_border()
602 {
603         BC_Resources *resources = get_resources();
604 // Clear margins
605         set_color(background_color);
606         draw_box(0, 0, left_margin, get_h());
607         draw_box(get_w() - right_margin, 0, right_margin, get_h());
608
609         if(has_border)
610         {
611                 if(highlighted)
612                         draw_3d_border(0, 0, w, h,
613                                 resources->text_border1,
614                                 resources->text_border2_hi,
615                                 resources->text_border3_hi,
616                                 resources->text_border4);
617                 else
618                         draw_3d_border(0, 0, w, h,
619                                 resources->text_border1,
620                                 resources->text_border2,
621                                 resources->text_border3,
622                                 resources->text_border4);
623         }
624 }
625
626 void BC_TextBox::draw_cursor()
627 {
628 //      set_color(background_color);
629
630         if(ibeam_x >= 0 &&
631                 ibeam_y >= 0)
632         {
633         set_color(WHITE);
634         set_inverse();
635
636         draw_box(ibeam_x + text_x,
637                 ibeam_y + text_y,
638                 BCCURSORW,
639                 text_height);
640         set_opaque();
641         }
642 }
643
644
645 void BC_TextBox::draw(int flush)
646 {
647         int i, k;
648         int row_begin, row_end;
649         int highlight_x1, highlight_x2;
650         int need_ibeam = 1;
651         BC_Resources *resources = get_resources();
652
653 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
654 // Background
655         background_color = has_border || !highlighted ? back_color : high_color;
656         set_color(background_color);
657         draw_box(0, 0, w, h);
658
659         int wtext_len = wtext_update();
660
661 // Draw text with selection
662         set_font(font);
663
664         for(i=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
665 // Draw row of text
666                 row_begin = i;
667                 wchar_t *wtext_row = &wtext[i];
668                 for( ; i<wtext_len && wtext[i]!='\n'; ++i );
669                 if( (row_end=i) < wtext_len ) ++i;
670
671                 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
672 // Draw highlighted region of row
673                         if( highlight_letter2 > highlight_letter1 &&
674                             highlight_letter2 > row_begin &&
675                             highlight_letter1 <= row_end ) {
676                                 int color = active && enabled && get_has_focus() ?
677                                         resources->text_highlight :
678                                     selection_active ?
679                                         resources->text_selected_highlight :
680                                         resources->text_inactive_highlight;
681                                 if( unicode_active >= 0 )
682                                         color ^= LTBLUE;
683                                 set_color(color);
684                                 if(highlight_letter1 >= row_begin &&
685                                         highlight_letter1 <= row_end)
686                                         highlight_x1 = get_x_position(highlight_letter1, row_begin);
687                                 else
688                                         highlight_x1 = 0;
689
690                                 if(highlight_letter2 > row_begin &&
691                                         highlight_letter2 <= row_end)
692                                         highlight_x2 = get_x_position(highlight_letter2, row_begin);
693                                 else
694                                         highlight_x2 = get_w();
695
696                                 draw_box(highlight_x1 + text_x, k,
697                                         highlight_x2 - highlight_x1, text_height);
698                         }
699
700 // Draw text over highlight
701                         int len = row_end - row_begin;
702                         if( len > 0 ) {
703                                 set_color(enabled ? resources->text_default : DMGREY);
704                                 draw_single_text(1, font, text_x, k + text_ascent, wtext_row, len);
705                         }
706
707 // Get ibeam location
708                         if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
709                                 need_ibeam = 0;
710                                 ibeam_y = k - text_y;
711                                 ibeam_x = get_x_position(ibeam_letter, row_begin);
712                         }
713                 }
714         }
715
716 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
717         if(need_ibeam) {
718 //              ibeam_x = ibeam_y = !wtext_len ? 0 : -1;
719                 ibeam_x = 0;  ibeam_y = k - text_y;
720         }
721
722 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
723 // Draw solid cursor
724         if (active)
725                 draw_cursor();
726
727 // Border
728         draw_border();
729         flash(flush);
730 }
731
732 int BC_TextBox::focus_in_event()
733 {
734         draw(1);
735         return 1;
736 }
737
738 int BC_TextBox::focus_out_event()
739 {
740         draw(1);
741         return 1;
742 }
743
744 int BC_TextBox::cursor_enter_event()
745 {
746         if( top_level->event_win == win && enabled &&
747             !(top_level->get_resources()->textbox_focus_policy & CLICK_ACTIVATE) )
748         {
749                 if( !active ) {
750                         top_level->deactivate();
751                         activate();
752                 }
753                 if(!highlighted)
754                 {
755                         highlighted = 1;
756                         draw_border();
757                         flash(1);
758                 }
759         }
760         return 0;
761 }
762
763 int BC_TextBox::cursor_leave_event()
764 {
765         if(highlighted)
766         {
767                 highlighted = 0;
768                 hide_tooltip();
769                 draw_border();
770                 flash(1);
771         }
772         if( !suggestions_popup && !get_button_down() &&
773             !(top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
774                 deactivate();
775         return 0;
776 }
777
778 int BC_TextBox::button_press_event()
779 {
780         const int debug = 0;
781
782         if(!enabled) return 0;
783 //      if(get_buttonpress() != WHEEL_UP &&
784 //              get_buttonpress() != WHEEL_DOWN &&
785 //              get_buttonpress() != LEFT_BUTTON &&
786 //              get_buttonpress() != MIDDLE_BUTTON) return 0;
787
788         if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
789
790         int cursor_letter = 0;
791         int wtext_len = wtext_update();
792         int update_scroll = 0;
793
794
795         if(top_level->event_win == win)
796         {
797                 if(!active)
798                 {
799                         hide_tooltip();
800                         top_level->deactivate();
801                         activate();
802                 }
803
804
805                 if(get_buttonpress() == WHEEL_UP)
806                 {
807                         text_y += text_height;
808                         text_y = MIN(text_y, top_margin);
809                         update_scroll = 1;
810                 }
811                 else
812                 if(get_buttonpress() == WHEEL_DOWN)
813                 {
814                         int min_y = -(get_text_rows() *
815                                 text_height -
816                                 get_h() +
817                                 bottom_margin);
818                         text_y -= text_height;
819                         text_y = MAX(text_y, min_y);
820                         text_y = MIN(text_y, top_margin);
821                         update_scroll = 1;
822                 }
823                 else
824                 if(get_buttonpress() == RIGHT_BUTTON)
825                 {
826                         menu->activate_menu();
827                 }
828                 else
829                 {
830
831                 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
832
833         //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
834
835
836                 if(get_triple_click())
837                 {
838         //printf("BC_TextBox::button_press_event %d\n", __LINE__);
839                         line_selected = 1;
840                         select_line(highlight_letter1, highlight_letter2, cursor_letter);
841                         highlight_letter3 = highlight_letter1;
842                         highlight_letter4 = highlight_letter2;
843                         ibeam_letter = highlight_letter2;
844                         copy_selection(PRIMARY_SELECTION);
845                 }
846                 else
847                 if(get_double_click())
848                 {
849                         word_selected = 1;
850                         select_word(highlight_letter1, highlight_letter2, cursor_letter);
851                         highlight_letter3 = highlight_letter1;
852                         highlight_letter4 = highlight_letter2;
853                         ibeam_letter = highlight_letter2;
854                         copy_selection(PRIMARY_SELECTION);
855                 }
856                 else
857                 if(get_buttonpress() == MIDDLE_BUTTON)
858                 {
859                         highlight_letter3 = highlight_letter4 =
860                                 ibeam_letter = highlight_letter1 =
861                                 highlight_letter2 = cursor_letter;
862                         paste_selection(PRIMARY_SELECTION);
863                 }
864                 else
865                 {
866                         text_selected = 1;
867                         highlight_letter3 = highlight_letter4 =
868                                 ibeam_letter = highlight_letter1 =
869                                 highlight_letter2 = cursor_letter;
870                 }
871
872
873         // Handle scrolling by highlighting text
874                 if(text_selected || word_selected || line_selected)
875                 {
876                         set_repeat(top_level->get_resources()->scroll_repeat);
877                 }
878
879                 if(ibeam_letter < 0) ibeam_letter = 0;
880                 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
881                 }
882
883                 draw(1);
884                 if(update_scroll && yscroll)
885                 {
886                         yscroll->update_length(get_text_rows(),
887                                 get_text_row(),
888                                 yscroll->get_handlelength(),
889                                 1);
890                 }
891                 handle_event();
892                 return 1;
893         }
894         else
895         if( active ) {
896                 if( suggestions_popup && (!yscroll || !yscroll->is_event_win())) {
897                         if( suggestions_popup->button_press_event() )
898                                 return suggestions_popup->handle_event();
899                 }
900                 else if( (top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
901                         deactivate();
902         }
903
904         return 0;
905 }
906
907 int BC_TextBox::button_release_event()
908 {
909         if(active)
910         {
911                 hide_tooltip();
912                 if(text_selected || word_selected || line_selected)
913                 {
914                         text_selected = 0;
915                         word_selected = 0;
916                         line_selected = 0;
917
918 // Stop scrolling by highlighting text
919                         unset_repeat(top_level->get_resources()->scroll_repeat);
920                 }
921         }
922         return 0;
923 }
924
925 int BC_TextBox::cursor_motion_event()
926 {
927         int cursor_letter, letter1, letter2;
928         if(active)
929         {
930                 if(text_selected || word_selected || line_selected)
931                 {
932                         cursor_letter = get_cursor_letter(top_level->cursor_x,
933                                 top_level->cursor_y);
934
935 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
936
937                         if(line_selected)
938                         {
939                                 select_line(letter1, letter2, cursor_letter);
940                         }
941                         else
942                         if(word_selected)
943                         {
944                                 select_word(letter1, letter2, cursor_letter);
945                         }
946                         else
947                         if(text_selected)
948                         {
949                                 letter1 = letter2 = cursor_letter;
950                         }
951
952                         if(letter1 <= highlight_letter3)
953                         {
954                                 highlight_letter1 = letter1;
955                                 highlight_letter2 = highlight_letter4;
956                                 ibeam_letter = letter1;
957                         }
958                         else
959                         if(letter2 >= highlight_letter4)
960                         {
961                                 highlight_letter2 = letter2;
962                                 highlight_letter1 = highlight_letter3;
963                                 ibeam_letter = letter2;
964                         }
965
966                         copy_selection(PRIMARY_SELECTION);
967
968
969
970
971                         draw(1);
972                         return 1;
973                 }
974         }
975
976         return 0;
977 }
978
979 int BC_TextBox::activate()
980 {
981         if( !active ) {
982                 active = 1;
983                 top_level->set_active_subwindow(this);
984                 top_level->set_repeat(top_level->get_resources()->blink_rate);
985         }
986         draw(1);
987         return 0;
988 }
989
990 int BC_TextBox::deactivate()
991 {
992         if( active ) {
993                 active = 0;
994                 text_selected = word_selected = line_selected = 0;
995                 top_level->set_active_subwindow(0);
996                 top_level->unset_repeat(top_level->get_resources()->blink_rate);
997         }
998         draw(1);
999         return 0;
1000 }
1001
1002 int BC_TextBox::repeat_event(int64_t duration)
1003 {
1004         int result = 0;
1005         int cursor_y = get_cursor_y();
1006         //int cursor_x = get_cursor_x();
1007
1008         if(duration == top_level->get_resources()->tooltip_delay &&
1009                 tooltip_text && tooltip_text[0] != 0 && highlighted)
1010         {
1011                 show_tooltip();
1012                 result = 1;
1013         }
1014
1015         if(duration == top_level->get_resources()->blink_rate &&
1016                 active &&
1017                 get_has_focus())
1018         {
1019 // don't flash if keypress
1020                 if(skip_cursor->get_difference() < 500)
1021                 {
1022 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1023 // skip_cursor->get_difference(),
1024 // duration);
1025                         result = 1;
1026                 }
1027                 else
1028                 {
1029                         if(!(text_selected || word_selected || line_selected))
1030                         {
1031                                 draw_cursor();
1032                                 flash(1);
1033                         }
1034                         result = 1;
1035                 }
1036         }
1037
1038         if(duration == top_level->get_resources()->scroll_repeat &&
1039                 (text_selected || word_selected || line_selected))
1040         {
1041                 int difference = 0;
1042                 if(get_cursor_y() < top_margin)
1043                 {
1044                         difference = get_cursor_y() - top_margin ;
1045                 }
1046                 else
1047                 if(get_cursor_y() > get_h() - bottom_margin)
1048                 {
1049                         difference = get_cursor_y() -
1050                                 (get_h() - bottom_margin);
1051                 }
1052                 if(difference != 0) {
1053                         int min_y = -(get_text_rows() * text_height -
1054                                 get_h() + bottom_margin);
1055
1056                         text_y -= difference;
1057 // printf("BC_TextBox::repeat_event %d %d %d\n",
1058 // __LINE__,
1059 // text_y,
1060 // min_y);
1061                         text_y = MAX(min_y, text_y);
1062                         text_y = MIN(text_y, top_margin);
1063
1064                         draw(1);
1065                         motion_event();
1066                         result = 1;
1067                 }
1068
1069                 if(get_cursor_x() < left_margin)
1070                 {
1071                         int difference = left_margin - get_cursor_x();
1072
1073                         text_x += difference;
1074                         text_x = MIN(text_x, left_margin);
1075                         draw(1);
1076                         result = 1;
1077                 }
1078                 else if(get_cursor_x() > get_w() - right_margin)
1079                 {
1080                         int difference = get_cursor_x() - (get_w() - right_margin);
1081                         int new_text_x = text_x - difference;
1082
1083 // Get width of current row
1084                         int min_x = 0;
1085                         int row_width = 0;
1086                         int wtext_len = wtext_update();
1087                         int row_begin = 0;
1088                         int row_end = 0;
1089                         for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1090                         {
1091                                 row_begin = i;
1092                                 while(wtext[i] != '\n' && i < wtext_len) {
1093                                         i++;
1094                                 }
1095                                 row_end = i;
1096                                 if(wtext[i] == '\n') i++;
1097
1098                                 if(cursor_y >= k && cursor_y < k + text_height) {
1099                                         row_width = get_text_width(font,
1100                                                 wtext + row_begin,
1101                                                 row_end - row_begin);
1102
1103                                         break;
1104                                 }
1105                         }
1106
1107                         min_x = -row_width + get_w() - left_margin - BCCURSORW;
1108                         new_text_x = MAX(new_text_x, min_x);
1109                         new_text_x = MIN(new_text_x, left_margin);
1110
1111                         if(new_text_x < text_x) text_x = new_text_x;
1112                         draw(1);
1113                         result = 1;
1114                 }
1115         }
1116
1117         return result;
1118 }
1119
1120 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1121 {
1122         int key = top_level->get_keypress(), len;
1123         wchar_t *wkeys = top_level->get_wkeystring(&len);
1124         switch( key ) {
1125         case KPENTER:   key = '\n';     goto kpchr;
1126         case KPMINUS:   key = '-';      goto kpchr;
1127         case KPPLUS:    key = '+';      goto kpchr;
1128         case KPDEL:     key = '.';      goto kpchr;
1129         case RETURN:    key = '\n';     goto kpchr;
1130         case KPINS:     key = '0';      goto kpchr;
1131         case KP1: case KP2: case KP3: case KP4: case KP5:
1132         case KP6: case KP7: case KP8: case KP9:
1133                 key = key - KP1 + '1';
1134         kpchr: {
1135                 wkeys[0] = key;  wkeys[1] = 0;  len = 1;
1136                 break; }
1137         default:
1138                 if( key < 32 || key > 255 ) return;
1139         }
1140         insert_text(wkeys, len);
1141         find_ibeam(1);
1142         draw(1);
1143         dispatch_event = 1;
1144         result = 1;
1145 }
1146
1147 int BC_TextBox::keypress_event()
1148 {
1149 // Result == 2 contents changed
1150 // Result == 1 trapped keypress
1151 // Result == 0 nothing
1152         int result = 0;
1153         int dispatch_event = 0;
1154
1155         if(!active || !enabled) return 0;
1156
1157         int wtext_len = wtext_update();
1158         last_keypress = get_keypress();
1159
1160         if( unicode_active >= 0 ) {
1161                 wchar_t wch = 0;
1162                 int wlen =  -1;
1163                 switch( last_keypress ) {
1164 //unicode active acitons
1165                 case RETURN: {
1166                         for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1167                                 int ch = nib(wtext[i]);
1168                                 if( ch < 0 ) return 1;
1169                                 wch = (wch<<4) + ch;
1170                         }
1171                         if( wch ) {
1172                                 dispatch_event = 1;
1173                                 unicode_active = -1;
1174                                 result = 1;
1175                                 wlen = 1;
1176                                 break;
1177                         } } // fall through
1178                 case ESC: {
1179                         unicode_active = -1;
1180                         result = 1;
1181                         wlen = 0;
1182                         break; }
1183                 case BACKSPACE: {
1184                         if(ibeam_letter > 0) {
1185                                 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1186                                 highlight_letter2 = --ibeam_letter;
1187                                 if( highlight_letter1 >= highlight_letter2 )
1188                                         unicode_active = -1;
1189                         }
1190                         result = 1;
1191                         wlen = 0;
1192                         break; }
1193                 case KPINS: last_keypress = KP1-'1'+'0'; // fall thru
1194                 case KP1: case KP2: case KP3: case KP4: case KP5:
1195                 case KP6: case KP7: case KP8: case KP9:
1196                         last_keypress = last_keypress-KP1 + '1';
1197                 case '0': case '1': case '2': case '3': case '4':
1198                 case '5': case '6': case '7': case '8': case '9':
1199                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1200                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1201                         int n = nib(last_keypress);
1202                         wch = n < 10 ? '0'+n : 'A'+n-10;
1203                         result = 1;
1204                         wlen = 1;
1205                         break; }
1206 //normal actions
1207                 case TAB:
1208                 case LEFTTAB:
1209                         break;
1210 //ignored actions
1211                 default:
1212                         result = 1;
1213                         break;
1214                 }
1215                 if( wlen >= 0 ) {
1216                         insert_text(&wch, wlen);
1217                         find_ibeam(1);
1218                         if( unicode_active >= 0 )
1219                                 highlight_letter2 = ibeam_letter;
1220                         else
1221                                 highlight_letter1 = highlight_letter2 = 0;
1222                         draw(1);
1223                 }
1224         }
1225
1226         if( !result ) {
1227 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1228                 switch(last_keypress) {
1229                 case ESC: {
1230 // Deactivate the suggestions
1231                         if( suggestions_popup ) {
1232                                 no_suggestions();
1233                                 result = 1;
1234                         }
1235                         else {
1236                                 top_level->deactivate();
1237                                 result = 0;
1238                         }
1239                         break; }
1240
1241                 case RETURN: {
1242                         if( rows == 1 ) {
1243                                 highlight_letter1 = highlight_letter2 = 0;
1244                                 ibeam_letter = wtext_update();
1245                                 top_level->deactivate();
1246                                 dispatch_event = 1;
1247                                 result = 0;
1248                         }
1249                         else {
1250                                 default_keypress(dispatch_event, result);
1251                         }
1252                         break; }
1253
1254
1255 // Handle like a default keypress
1256                 case TAB: {
1257                         top_level->cycle_textboxes(1);
1258                         result = 1;
1259                         break; }
1260
1261                 case LEFTTAB: {
1262                         top_level->cycle_textboxes(-1);
1263                         result = 1;
1264                         break; }
1265
1266                 case LEFT: {
1267                         if(ibeam_letter > 0) {
1268                                 int old_ibeam_letter = ibeam_letter;
1269 // Single character
1270                                 if(!ctrl_down()) {
1271                                         ibeam_letter--;
1272                                 }
1273                                 else {
1274 // Word
1275                                         ibeam_letter--;
1276                                         while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1277                                                 ibeam_letter--;
1278                                 }
1279
1280 // Extend selection
1281                                 if(top_level->shift_down()) {
1282 // Initialize highlighting
1283                                         if(highlight_letter1 == highlight_letter2) {
1284                                                 highlight_letter1 = ibeam_letter;
1285                                                 highlight_letter2 = old_ibeam_letter;
1286                                         }
1287                                         else if(highlight_letter1 == old_ibeam_letter) {
1288 // Extend left highlight
1289                                                 highlight_letter1 = ibeam_letter;
1290                                         }
1291                                         else if(highlight_letter2 == old_ibeam_letter) {
1292 // Shrink right highlight
1293                                                 highlight_letter2 = ibeam_letter;
1294                                         }
1295                                 }
1296                                 else {
1297                                         highlight_letter1 = highlight_letter2 = ibeam_letter;
1298                                 }
1299
1300
1301                                 find_ibeam(1);
1302                                 if(keypress_draw) draw(1);
1303                         }
1304                         result = 1;
1305                         break; }
1306
1307                 case RIGHT: {
1308                         if(ibeam_letter < wtext_len) {
1309                                 int old_ibeam_letter = ibeam_letter;
1310 // Single character
1311                                 if(!ctrl_down()) {
1312                                         ibeam_letter++;
1313                                 }
1314                                 else {
1315 // Word
1316                                         while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1317                                 }
1318
1319
1320
1321 // Extend selection
1322                                 if(top_level->shift_down()) {
1323 // Initialize highlighting
1324                                         if(highlight_letter1 == highlight_letter2) {
1325                                                 highlight_letter1 = old_ibeam_letter;
1326                                                 highlight_letter2 = ibeam_letter;
1327                                         }
1328                                         else if(highlight_letter1 == old_ibeam_letter) {
1329 // Shrink left highlight
1330                                                 highlight_letter1 = ibeam_letter;
1331                                         }
1332                                         else if(highlight_letter2 == old_ibeam_letter) {
1333 // Expand right highlight
1334                                                 highlight_letter2 = ibeam_letter;
1335                                         }
1336                                 }
1337                                 else {
1338                                         highlight_letter1 = highlight_letter2 = ibeam_letter;
1339                                 }
1340
1341                                 find_ibeam(1);
1342                                 if(keypress_draw) draw(1);
1343                         }
1344                         result = 1;
1345                         break; }
1346
1347                 case UP: {
1348                         if( suggestions && suggestions_popup ) {
1349 // Pass to suggestions popup
1350 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1351                                 suggestions_popup->activate(1);
1352                                 suggestions_popup->keypress_event();
1353                                 result = 1;
1354                         }
1355                         else if(ibeam_letter > 0) {
1356 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1357                                 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1358                                         ibeam_y + text_y - text_height);
1359 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1360
1361 // Extend selection
1362                                 if(top_level->shift_down()) {
1363 // Initialize highlighting
1364                                         if(highlight_letter1 == highlight_letter2) {
1365                                                 highlight_letter1 = new_letter;
1366                                                 highlight_letter2 = ibeam_letter;
1367                                         }
1368                                         else if(highlight_letter1 == ibeam_letter) {
1369 // Expand left highlight
1370                                                 highlight_letter1 = new_letter;
1371                                         }
1372                                         else if(highlight_letter2 == ibeam_letter) {
1373 // Shrink right highlight
1374                                                 highlight_letter2 = new_letter;
1375                                         }
1376                                 }
1377                                 else
1378                                         highlight_letter1 = highlight_letter2 = new_letter;
1379
1380                                 if(highlight_letter1 > highlight_letter2) {
1381                                         int temp = highlight_letter1;
1382                                         highlight_letter1 = highlight_letter2;
1383                                         highlight_letter2 = temp;
1384                                 }
1385                                 ibeam_letter = new_letter;
1386
1387                                 find_ibeam(1);
1388                                 if(keypress_draw) draw(1);
1389                         }
1390                         result = 1;
1391                         break; }
1392
1393                 case PGUP: {
1394                         if(ibeam_letter > 0) {
1395                                 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1396                                         ibeam_y + text_y - get_h());
1397
1398 // Extend selection
1399                                 if(top_level->shift_down()) {
1400 // Initialize highlighting
1401                                         if(highlight_letter1 == highlight_letter2) {
1402                                                 highlight_letter1 = new_letter;
1403                                                 highlight_letter2 = ibeam_letter;
1404                                         }
1405                                         else if(highlight_letter1 == ibeam_letter) {
1406 // Expand left highlight
1407                                                 highlight_letter1 = new_letter;
1408                                         }
1409                                         else if(highlight_letter2 == ibeam_letter) {
1410 // Shrink right highlight
1411                                                 highlight_letter2 = new_letter;
1412                                         }
1413                                 }
1414                                 else
1415                                         highlight_letter1 = highlight_letter2 = new_letter;
1416
1417                                 if(highlight_letter1 > highlight_letter2) {
1418                                         int temp = highlight_letter1;
1419                                         highlight_letter1 = highlight_letter2;
1420                                         highlight_letter2 = temp;
1421                                 }
1422                                 ibeam_letter = new_letter;
1423
1424                                 find_ibeam(1);
1425                                 if(keypress_draw) draw(1);
1426                         }
1427                         result = 1;
1428                         break; }
1429
1430                 case DOWN: {
1431 // printf("BC_TextBox::keypress_event %d %p %p\n",
1432 // __LINE__,
1433 // suggestions,
1434 // suggestions_popup);
1435                         if( suggestions && suggestions_popup ) {
1436 // Pass to suggestions popup
1437                                 suggestions_popup->activate(1);
1438                                 suggestions_popup->keypress_event();
1439                                 result = 1;
1440                         }
1441                         else
1442 //                      if(ibeam_letter > 0)
1443                         {
1444 // Extend selection
1445                                 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1446                                         ibeam_y + text_y + text_height);
1447 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1448
1449                                 if(top_level->shift_down()) {
1450 // Initialize highlighting
1451                                         if(highlight_letter1 == highlight_letter2) {
1452                                                 highlight_letter1 = new_letter;
1453                                                 highlight_letter2 = ibeam_letter;
1454                                         }
1455                                         else if(highlight_letter1 == ibeam_letter) {
1456 // Shrink left highlight
1457                                                 highlight_letter1 = new_letter;
1458                                         }
1459                                         else if(highlight_letter2 == ibeam_letter) {
1460 // Expand right highlight
1461                                                 highlight_letter2 = new_letter;
1462                                         }
1463                                 }
1464                                 else
1465                                         highlight_letter1 = highlight_letter2 = new_letter;
1466
1467                                 if(highlight_letter1 > highlight_letter2) {
1468                                         int temp = highlight_letter1;
1469                                         highlight_letter1 = highlight_letter2;
1470                                         highlight_letter2 = temp;
1471                                 }
1472                                 ibeam_letter = new_letter;
1473
1474                                 find_ibeam(1);
1475                                 if(keypress_draw) draw(1);
1476
1477 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1478                         }
1479                         result = 1;
1480                         break; }
1481
1482                 case PGDN: {
1483 // Extend selection
1484                         int new_letter = get_cursor_letter2(ibeam_x + text_x,
1485                                 ibeam_y + text_y + get_h());
1486 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1487
1488                         if(top_level->shift_down()) {
1489 // Initialize highlighting
1490                                 if(highlight_letter1 == highlight_letter2) {
1491                                         highlight_letter1 = new_letter;
1492                                         highlight_letter2 = ibeam_letter;
1493                                 }
1494                                 else if(highlight_letter1 == ibeam_letter) {
1495 // Shrink left highlight
1496                                         highlight_letter1 = new_letter;
1497                                 }
1498                                 else if(highlight_letter2 == ibeam_letter) {
1499 // Expand right highlight
1500                                         highlight_letter2 = new_letter;
1501                                 }
1502                         }
1503                         else
1504                                 highlight_letter1 = highlight_letter2 = new_letter;
1505
1506                         if(highlight_letter1 > highlight_letter2) {
1507                                 int temp = highlight_letter1;
1508                                 highlight_letter1 = highlight_letter2;
1509                                 highlight_letter2 = temp;
1510                         }
1511                         ibeam_letter = new_letter;
1512
1513                         find_ibeam(1);
1514                         if(keypress_draw) draw(1);
1515
1516 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1517                         result = 1;
1518                         break; }
1519
1520                 case END: {
1521                         no_suggestions();
1522
1523                         int old_ibeam_letter = ibeam_letter;
1524
1525                         while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1526                                 ibeam_letter++;
1527
1528                         if(top_level->shift_down()) {
1529 // Begin selection
1530                                 if(highlight_letter1 == highlight_letter2) {
1531                                         highlight_letter2 = ibeam_letter;
1532                                         highlight_letter1 = old_ibeam_letter;
1533                                 }
1534                                 else if(highlight_letter1 == old_ibeam_letter) {
1535 // Shrink selection
1536                                         highlight_letter1 = highlight_letter2;
1537                                         highlight_letter2 = ibeam_letter;
1538                                 }
1539                                 else if(highlight_letter2 == old_ibeam_letter) {
1540 // Extend selection
1541                                         highlight_letter2 = ibeam_letter;
1542                                 }
1543                         }
1544                         else
1545                                 highlight_letter1 = highlight_letter2 = ibeam_letter;
1546
1547                         find_ibeam(1);
1548                         if(keypress_draw) draw(1);
1549                         result = 1;
1550                         break; }
1551
1552                 case HOME: {
1553                         no_suggestions();
1554
1555                         int old_ibeam_letter = ibeam_letter;
1556
1557                         while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1558                                 ibeam_letter--;
1559
1560                         if(top_level->shift_down())
1561                         {
1562 // Begin selection
1563                                 if(highlight_letter1 == highlight_letter2)
1564                                 {
1565                                         highlight_letter2 = old_ibeam_letter;
1566                                         highlight_letter1 = ibeam_letter;
1567                                 }
1568                                 else
1569 // Extend selection
1570                                 if(highlight_letter1 == old_ibeam_letter)
1571                                 {
1572                                         highlight_letter1 = ibeam_letter;
1573                                 }
1574                                 else
1575 // Shrink selection
1576                                 if(highlight_letter2 == old_ibeam_letter)
1577                                 {
1578                                         highlight_letter2 = highlight_letter1;
1579                                         highlight_letter1 = ibeam_letter;
1580                                 }
1581                         }
1582                         else
1583                                 highlight_letter1 = highlight_letter2 = ibeam_letter;
1584
1585                         find_ibeam(1);
1586                         if(keypress_draw) draw(1);
1587                         result = 1;
1588                         break; }
1589
1590                 case BACKSPACE: {
1591                         no_suggestions();
1592
1593                         if(highlight_letter1 == highlight_letter2) {
1594                                 if(ibeam_letter > 0) {
1595                                         delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1596                                         ibeam_letter--;
1597                                 }
1598                         }
1599                         else {
1600                                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1601                                 highlight_letter2 = ibeam_letter = highlight_letter1;
1602                         }
1603
1604                         find_ibeam(1);
1605                         if(keypress_draw) draw(1);
1606                         dispatch_event = 1;
1607                         result = 1;
1608                         break; }
1609
1610                 case DELETE: {
1611 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1612                         if(highlight_letter1 == highlight_letter2) {
1613                                 if(ibeam_letter < wtext_len) {
1614                                         delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1615                                 }
1616                         }
1617                         else {
1618                                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1619                                 highlight_letter2 = ibeam_letter = highlight_letter1;
1620                         }
1621
1622                         find_ibeam(1);
1623                         if(keypress_draw) draw(1);
1624                         dispatch_event = 1;
1625                         result = 1;
1626                         break; }
1627
1628                 default: {
1629                         if( ctrl_down() ) {
1630                                 switch( last_keypress ) {
1631                                 case 'c': case 'C': {
1632                                         result = copy(0);
1633                                         break; }
1634                                 case 'v': case 'V': {
1635                                         result = paste(0);
1636                                         dispatch_event = 1;
1637                                         break; }
1638                                 case 'x': case 'X': {
1639                                         result = cut(0);
1640                                         dispatch_event = 1;
1641                                         break; }
1642                                 case 'u': case 'U': {
1643                                         if( shift_down() ) {
1644                                                 unicode_active = ibeam_letter;
1645                                                 wchar_t wkey = 'U';
1646                                                 insert_text(&wkey, 1);
1647                                                 find_ibeam(1);
1648                                                 highlight_letter1 = unicode_active;
1649                                                 highlight_letter2 = ibeam_letter;
1650                                                 draw(1);
1651                                                 result = 1;
1652                                         }
1653                                         break; }
1654                                 }
1655                                 break;
1656                         }
1657
1658                         default_keypress(dispatch_event, result);
1659                         break; }
1660                 }
1661         }
1662
1663         if(result) skip_cursor->update();
1664         if(dispatch_event && handle_event())
1665                 result = 1;
1666
1667         return result;
1668 }
1669
1670
1671 int BC_TextBox::cut(int do_update)
1672 {
1673         if( highlight_letter1 != highlight_letter2 ) {
1674                 int wtext_len = wtext_update();
1675                 copy_selection(SECONDARY_SELECTION);
1676                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1677                 highlight_letter2 = ibeam_letter = highlight_letter1;
1678         }
1679
1680         find_ibeam(1);
1681         if( keypress_draw )
1682                 draw(1);
1683         
1684         if( do_update ) {
1685                 skip_cursor->update();
1686                 handle_event();
1687         }
1688         return 1;
1689 }
1690
1691 int BC_TextBox::copy(int do_update)
1692 {
1693         int result = 0;
1694         if( highlight_letter1 != highlight_letter2 ) {
1695                 copy_selection(SECONDARY_SELECTION);
1696                 result = 1;
1697                 if( do_update ) {
1698                         skip_cursor->update();
1699                 }
1700         }
1701         return result;
1702 }
1703
1704 int BC_TextBox::paste(int do_update)
1705 {
1706         paste_selection(SECONDARY_SELECTION);
1707         find_ibeam(1);
1708         if( keypress_draw )
1709                 draw(1);
1710         if( do_update ) {
1711                 skip_cursor->update();
1712                 handle_event();
1713         }
1714         return 1;
1715 }
1716
1717
1718 int BC_TextBox::uses_text()
1719 {
1720         return 1;
1721 }
1722
1723
1724 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1725 {
1726         int i, j;
1727         for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1728                 wtext[i] = wtext[j];
1729         }
1730         wtext[i] = 0;
1731         wlen = i;
1732
1733         do_separators(1);
1734 }
1735
1736 int BC_TextBox::wdemand(int len)
1737 {
1738         if( wtext && wsize >= len ) return 0;
1739         int nsize = len + wlen/2 + BCTEXTLEN;
1740         wchar_t *ntext = new wchar_t[nsize+1];
1741         ntext[nsize] = 0;
1742         memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
1743         delete [] wtext;  wtext = ntext;  wsize = nsize;
1744         return 1;
1745 }
1746
1747 int BC_TextBox::tdemand(int len)
1748 {
1749         if( text && tsize >= len ) return 0;
1750         int tlen = !text ? 0 : strlen(text);
1751         int nsize = len + tlen/2 + BCTEXTLEN;
1752         char *ntext = new char[nsize+1];
1753         ntext[nsize] = 0;
1754         memcpy(ntext, text, tsize*sizeof(text[0]));
1755         delete [] text;  text = ntext;  tsize = nsize;
1756         return 1;
1757 }
1758
1759 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1760 {
1761         if( len < 0 ) len = wcslen(wcp);
1762         int wtext_len = wtext_update();
1763         wdemand(wtext_len + len + 1);
1764         if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1765                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1766                 highlight_letter2 = ibeam_letter = highlight_letter1;
1767                 wtext_len = wtext_update();
1768         }
1769
1770         int i, j;
1771         for( i=wtext_len, j=wtext_len+len; --i>=ibeam_letter; )
1772                 if( --j < wsize ) wtext[j] = wtext[i];
1773         for( i=ibeam_letter,j=0; j<len; ++i, ++j )
1774                 if( i < wsize ) wtext[i] = wcp[j];
1775
1776         if( (wlen+=len) > wsize ) wlen = wsize;
1777         if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1778         wtext[wlen] = 0;  // wtext allocated wsize+1
1779         do_separators(0);
1780 }
1781
1782 int BC_TextBox::is_separator(const char *txt, int i)
1783 {
1784         if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1785         return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1786 }
1787
1788 // used for time entry
1789 void BC_TextBox::do_separators(int ibeam_left)
1790 {
1791         if(separators)
1792         {
1793 // Remove separators from text
1794                 int wtext_len = wtext_update();
1795                 for(int i = 0; i < wtext_len; ) {
1796                         if( !iswalnum(wtext[i]) ) {
1797                                 for(int j = i; j < wtext_len - 1; j++)
1798                                         wtext[j] = wtext[j + 1];
1799                                 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1800                                 wlen = --wtext_len;
1801                                 continue;
1802                         }
1803                         ++i;
1804                 }
1805                 wtext[wtext_len] = 0;
1806
1807
1808
1809
1810
1811 // Insert separators into text
1812                 int separator_len = strlen(separators);
1813                 for(int i = 0; i < separator_len; i++) {
1814                         if(i < wtext_len) {
1815 // Insert a separator
1816                                 if( is_separator(separators,i) ) {
1817                                         for(int j = wtext_len; j >= i; j--) {
1818                                                 wtext[j + 1] = wtext[j];
1819                                         }
1820                                         if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1821                                         ++wtext_len;
1822                                         wtext[i] = separators[i];
1823                                 }
1824                         }
1825                         else {
1826                                 wtext[i] = separators[i];
1827                         }
1828                 }
1829
1830 // Truncate text
1831                 wtext[separator_len] = 0;
1832                 wlen = separator_len;
1833         }
1834 }
1835
1836 int BC_TextBox::get_x_position(int i, int start)
1837 {
1838         return get_text_width(font, &wtext[start], i - start);
1839 }
1840
1841 void BC_TextBox::get_ibeam_position(int &x, int &y)
1842 {
1843         int i, row_begin, row_end;
1844         int wtext_len = wtext_update();
1845         x = y = 0;
1846
1847         for( i=0; i<wtext_len; ) {
1848                 row_begin = i;
1849                 for(; i<wtext_len && wtext[i]!='\n'; i++);
1850                 row_end = i;
1851
1852                 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1853                         x = get_x_position(ibeam_letter, row_begin);
1854 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1855                         return;
1856                 }
1857
1858                 if( i < wtext_len && wtext[i] == '\n' ) {
1859                         i++;
1860                         y += text_height;
1861                 }
1862         }
1863 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1864
1865         x = 0;
1866         return;
1867 }
1868
1869 void BC_TextBox::set_text_row(int row)
1870 {
1871         text_x = left_margin;
1872         text_y = -(row * text_height) + top_margin;
1873         draw(1);
1874 }
1875
1876 int BC_TextBox::get_text_row()
1877 {
1878         return -(text_y - top_margin) / text_height;
1879 }
1880
1881 void BC_TextBox::find_ibeam(int dispatch_event)
1882 {
1883         int x, y;
1884         int old_x = text_x, old_y = text_y;
1885
1886         get_ibeam_position(x, y);
1887
1888         if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1889         {
1890                 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1891                 if(text_x > left_margin) text_x = left_margin;
1892         }
1893         else
1894         if(left_margin + text_x + x < left_margin)
1895         {
1896                 text_x = -(x - (get_w() / 4)) + left_margin;
1897                 if(text_x > left_margin) text_x = left_margin;
1898         }
1899
1900         int text_row = y / text_height;
1901         if( text_row < rows ) text_y = top_margin;
1902
1903         int pix_rows = get_h() - bottom_margin - (y + text_y);
1904         if( pix_rows < text_height )
1905                 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1906
1907         pix_rows = y + text_y - top_margin;
1908         if( pix_rows < 0 ) {
1909                 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1910                 if( text_y > top_margin ) text_y = top_margin;
1911         }
1912
1913         if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1914 }
1915
1916 // New algorithm
1917 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1918 {
1919         int i, j, k, row_begin, row_end, result = 0, done = 0;
1920         int column1, column2;
1921         int got_visible_row = 0;
1922
1923 // Select complete row if cursor above the window
1924 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1925         if(cursor_y < text_y - text_height)
1926         {
1927                 result = 0;
1928                 done = 1;
1929         }
1930
1931         int wtext_len = wtext_update();
1932
1933         for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1934 // Simulate drawing of 1 row
1935                 row_begin = i;
1936                 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1937                 row_end = i;
1938
1939 // check visibility
1940                 int first_visible_row = 0;
1941                 int last_visible_row = 0;
1942                 if( k+text_height > top_margin && !got_visible_row) {
1943                         first_visible_row = 1;
1944                         got_visible_row = 1;
1945                 }
1946
1947                 if( (k+text_height >= get_h() - bottom_margin ||
1948                         (row_end >= wtext_len && k < get_h() - bottom_margin &&
1949                                 k + text_height > 0)) )
1950                         last_visible_row = 1;
1951
1952 // Cursor is inside vertical range of row
1953                 if((cursor_y >= top_margin &&
1954                         cursor_y < get_h() - bottom_margin &&
1955                         cursor_y >= k && cursor_y < k + text_height) ||
1956 // Cursor is above 1st row
1957                         (cursor_y < k + text_height && first_visible_row) ||
1958 // Cursor is below last row
1959                         (cursor_y >= k && last_visible_row))
1960                 {
1961                         column1 = column2 = 0;
1962                         for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1963                                 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1964                                 if((column2 + column1) / 2 >= cursor_x) {
1965                                         result = j - 1;
1966                                         done = 1;
1967 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1968 // __LINE__, result, first_visible_row, last_visible_row);
1969                                 }
1970                                 column1 = column2;
1971                         }
1972
1973                         if(!done) {
1974                                 result = row_end;
1975                                 done = 1;
1976                         }
1977                 }
1978
1979                 if(wtext[i] == '\n') i++;
1980
1981 // Select complete row if last visible & cursor is below window
1982                 if(last_visible_row && cursor_y > k + text_height * 2)
1983                         result = row_end;
1984
1985                 if(i >= wtext_len && !done) {
1986                         result = wtext_len;
1987                 }
1988         }
1989
1990
1991 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
1992 //  __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
1993         if(result < 0) result = 0;
1994         if(result > wtext_len) {
1995 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
1996                 result = wtext_len;
1997         }
1998
1999         return result;
2000 }
2001
2002 // Old algorithm
2003 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
2004 {
2005         int i, j, k, row_begin, row_end, result = 0, done = 0;
2006         int column1, column2;
2007         int wtext_len = wtext_update();
2008
2009         if(cursor_y < text_y) {
2010                 result = 0;
2011                 done = 1;
2012         }
2013
2014         for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
2015                 row_begin = i;
2016                 for(; wtext[i] != '\n' && i < wtext_len; i++);
2017                 row_end = i;
2018
2019                 if(cursor_y >= k && cursor_y < k + text_height) {
2020                         column1 = column2 = 0;
2021                         for(j = 0; j <= row_end - row_begin && !done; j++) {
2022                                 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
2023                                 if((column2 + column1) / 2 >= cursor_x) {
2024                                         result = row_begin + j - 1;
2025                                         done = 1;
2026                                 }
2027                                 column1 = column2;
2028                         }
2029                         if(!done)
2030                         {
2031                                 result = row_end;
2032                                 done = 1;
2033                         }
2034                 }
2035                 if(wtext[i] == '\n') i++;
2036
2037                 if(i >= wtext_len && !done) {
2038                         result = wtext_len;
2039                 }
2040         }
2041         if(result < 0) result = 0;
2042         if(result > wtext_len) result = wtext_len;
2043         return result;
2044 }
2045
2046
2047 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2048 {
2049         int wtext_len = wtext_update();
2050         letter1 = letter2 = ibeam_letter;
2051         if( letter1 < 0 ) letter1 = 0;
2052         if( letter2 < 0 ) letter2 = 0;
2053         if( letter1 > wtext_len ) letter1 = wtext_len;
2054         if( letter2 > wtext_len ) letter2 = wtext_len;
2055         if( !wtext_len ) return;
2056         for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
2057         for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
2058         if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
2059 }
2060
2061
2062 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2063 {
2064         int wtext_len = wtext_update();
2065         letter1 = letter2 = ibeam_letter;
2066         if( letter1 < 0 ) letter1 = 0;
2067         if( letter2 < 0 ) letter2 = 0;
2068         if( letter1 > wtext_len ) letter1 = wtext_len;
2069         if( letter2 > wtext_len ) letter2 = wtext_len;
2070         if( !wtext_len ) return;
2071         for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2072         for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2073 }
2074
2075 void BC_TextBox::copy_selection(int clipboard_num)
2076 {
2077         int wtext_len = wtext_update();
2078         if(!wtext_len) return;
2079
2080         if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2081                 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2082                 highlight_letter2 - highlight_letter1 <= 0) return;
2083         int clip_len = highlight_letter2 - highlight_letter1;
2084 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2085         char ctext[4*clip_len+4];
2086         clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2087         to_clipboard(ctext, clip_len, clipboard_num);
2088         selection_active = 1;
2089 }
2090
2091 int BC_TextBox::selection_clear_event()
2092 {
2093         if( !is_event_win() ) return 0;
2094         selection_active = 0;
2095         draw(1);
2096         return 1;
2097 }
2098
2099 void BC_TextBox::paste_selection(int clipboard_num)
2100 {
2101         int len = clipboard_len(clipboard_num);
2102         if( len > 0 )
2103         {
2104                 char cstring[len];  wchar_t wstring[len];
2105                 from_clipboard(cstring, len, clipboard_num);  --len;
2106 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2107                 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2108                         cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2109                 insert_text(wstring, len);
2110                 last_keypress = 0;
2111         }
2112 }
2113
2114 void BC_TextBox::set_keypress_draw(int value)
2115 {
2116         keypress_draw = value;
2117 }
2118
2119 int BC_TextBox::get_last_keypress()
2120 {
2121         return last_keypress;
2122 }
2123
2124 int BC_TextBox::get_ibeam_letter()
2125 {
2126         return ibeam_letter;
2127 }
2128
2129 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2130 {
2131         this->ibeam_letter = number;
2132         if(redraw)
2133         {
2134                 draw(1);
2135         }
2136 }
2137
2138 void BC_TextBox::set_separators(const char *separators)
2139 {
2140         this->separators = (char*)separators;
2141 }
2142
2143 int BC_TextBox::get_rows()
2144 {
2145         return rows;
2146 }
2147
2148
2149
2150
2151
2152
2153
2154 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y)
2155  : BC_ListBox(x, y, text_box->get_w(), 200, LISTBOX_TEXT,
2156         text_box->suggestions, 0, 0, 1, 0, 1)
2157 {
2158         this->text_box = text_box;
2159         set_use_button(0);
2160         set_justify(LISTBOX_LEFT);
2161 }
2162
2163 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2164 {
2165 }
2166
2167 int BC_TextBoxSuggestions::handle_event()
2168 {
2169         char *current_suggestion = 0;
2170         BC_ListBoxItem *item = get_selection(0, 0);
2171 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2172         if(item && (current_suggestion=item->get_text()) != 0)
2173         {
2174                 int col = text_box->suggestion_column;
2175                 int len = BCTEXTLEN-1 - col;
2176                 char *cp = &text_box->text[col];
2177 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2178                 strncpy(cp, current_suggestion, len);
2179 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2180                 if( (col=strlen(current_suggestion)) >= len )
2181                         cp[len-1] = 0;
2182                 text_box->dirty = 1;
2183         }
2184
2185
2186 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2187         text_box->highlight_letter1 =
2188                 text_box->highlight_letter2 =
2189                 text_box->ibeam_letter = text_box->tstrlen();
2190         text_box->wtext_update();
2191         text_box->draw(1);
2192         text_box->handle_event();
2193 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2194         return 1;
2195 }
2196
2197
2198 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2199         int x, int y, int w, int rows,
2200         const char *default_text, int default_size)
2201 {
2202         this->parent_window = parent_window;
2203         this->x = x;
2204         this->y = y;
2205         this->w = w;
2206         this->rows = rows;
2207         xscroll = 0;  yscroll = 0;
2208         this->default_text = default_text;
2209         this->default_wtext = 0;
2210         this->default_size = default_size;
2211 }
2212
2213 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2214         int x, int y, int w, int rows,
2215         const wchar_t *default_wtext, int default_size)
2216 {
2217         this->parent_window = parent_window;
2218         this->x = x;
2219         this->y = y;
2220         this->w = w;
2221         this->rows = rows;
2222         xscroll = 0;  yscroll = 0;
2223         this->default_text = 0;
2224         this->default_wtext = default_wtext;
2225         this->default_size = default_size;
2226 }
2227
2228 BC_ScrollTextBox::~BC_ScrollTextBox()
2229 {
2230         delete xscroll;
2231         delete yscroll;
2232         if(text) {
2233                 text->gui = 0;
2234                 delete text;
2235         }
2236 }
2237
2238 void BC_ScrollTextBox::create_objects()
2239 {
2240 // Must be created first
2241         parent_window->add_subwindow(text = default_wtext ?
2242                 new BC_ScrollTextBoxText(this, default_wtext) :
2243                 new BC_ScrollTextBoxText(this, default_text));
2244         set_text_row(0);
2245 }
2246
2247 void BC_ScrollTextBox::set_text(char *text, int isz)
2248 {
2249         this->text->set_text(text, isz);
2250         update_scrollbars();
2251 }
2252
2253 int BC_ScrollTextBox::set_text_row(int n)
2254 {
2255         text->set_text_row(n);
2256         update_scrollbars();
2257         return 1;
2258 }
2259
2260 void BC_ScrollTextBox::update(const char *text)
2261 {
2262         this->text->update(text);
2263         update_scrollbars();
2264 }
2265
2266 void BC_ScrollTextBox::update(const wchar_t *wtext)
2267 {
2268         this->text->update(wtext);
2269         update_scrollbars();
2270 }
2271
2272 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2273 {
2274         this->x = x;
2275         this->y = y;
2276         this->w = w;
2277         this->rows = rows;
2278         update_scrollbars();
2279 }
2280
2281 int BC_ScrollTextBox::button_press_event()
2282 {
2283         return text->BC_TextBox::button_press_event();
2284 }
2285 int BC_ScrollTextBox::button_release_event()
2286 {
2287         return text->BC_TextBox::button_release_event();
2288 }
2289
2290 int BC_ScrollTextBox::get_h() { return text->get_h(); }
2291 const char *BC_ScrollTextBox::get_text() { return text->get_text(); }
2292 const wchar_t *BC_ScrollTextBox::get_wtext() { return text->get_wtext(); }
2293
2294 int BC_ScrollTextBox::get_buttonpress()
2295 {
2296         return text->BC_TextBox::get_buttonpress();
2297 }
2298 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2299 {
2300         text->wset_selection(char1, char2, ibeam);
2301 }
2302 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2303 {
2304         text->set_selection(char1, char2, ibeam);
2305 }
2306 int BC_ScrollTextBox::get_ibeam_letter()
2307 {
2308         return text->get_ibeam_letter();
2309 }
2310 int BC_ScrollTextBox::get_x_pos()
2311 {
2312         return text->left_margin - text->get_text_x();
2313 }
2314 void BC_ScrollTextBox::set_x_pos(int x)
2315 {
2316         text->set_text_x(text->left_margin - x);
2317         text->draw(1);
2318 }
2319
2320 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2321  : BC_TextBox(gui->x, gui->y,
2322         gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2323         gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2324 {
2325         this->gui = gui;
2326 }
2327
2328 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2329  : BC_TextBox(gui->x, gui->y,
2330         gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2331         gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2332 {
2333         this->gui = gui;
2334 }
2335
2336 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2337 {
2338         if(gui)
2339         {
2340                 gui->text = 0;
2341                 delete gui;
2342         }
2343 }
2344
2345 void BC_ScrollTextBox::update_scrollbars()
2346 {
2347         int view_w = w, view_rows = rows;
2348         int view_h = BC_TextBox::calculate_row_h(view_rows, parent_window);
2349         int text_rows = text->get_text_rows();
2350         int text_width = parent_window->get_text_width(text->font, text->get_wtext());
2351         BC_Resources *resources = parent_window->get_resources();
2352         int need_xscroll = 0, need_yscroll = 0;
2353
2354 // Create scrollbars as needed
2355         int resize = 1;
2356         while( resize ) {
2357                 resize = 0;
2358                 if( !need_xscroll && (text->get_text_x() != text->left_margin ||
2359                       text_width >= view_w - text->left_margin - text->right_margin) ) {
2360                         resize = need_xscroll = 1;
2361                         view_h -= resources->hscroll_data[SCROLL_HANDLE_UP]->get_h();
2362                         view_rows = BC_TextBox::pixels_to_rows(parent_window, text->font, view_h);
2363                 }
2364                 if( !need_yscroll && (text->get_text_y() != text->top_margin ||
2365                       text_rows > view_rows) ) {
2366                         resize = need_yscroll = 1;
2367                         view_w -= resources->vscroll_data[SCROLL_HANDLE_UP]->get_w();
2368                 }
2369         }
2370
2371         if( !need_xscroll && xscroll ) {
2372                 text->yscroll = 0;
2373                 delete xscroll;  xscroll = 0;
2374         }
2375         if( !need_yscroll && yscroll ) {
2376                 text->yscroll = 0;
2377                 delete yscroll;  yscroll = 0;
2378         }
2379
2380         if( view_rows != text->get_rows() || view_w != text->get_w() ) {
2381                 text->reposition_window(x, y, view_w, view_rows);
2382         }
2383         if( need_xscroll && !xscroll ) {
2384                 xscroll = new BC_ScrollTextBoxXScroll(this);
2385                 parent_window->add_subwindow(xscroll);
2386                 text->xscroll = xscroll;
2387                 xscroll->bound_to = text;
2388                 xscroll->show_window();
2389         }
2390         if( need_yscroll && !yscroll ) {
2391                 yscroll = new BC_ScrollTextBoxYScroll(this);
2392                 parent_window->add_subwindow(yscroll);
2393                 text->yscroll = yscroll;
2394                 yscroll->bound_to = text;
2395                 yscroll->show_window();
2396         }
2397         if( xscroll ) {
2398                 xscroll->reposition_window(x, y + text->get_h(), view_w);
2399                 int xpos = get_x_pos();
2400                 if( xpos != xscroll->get_value() )
2401                         xscroll->update_value(xpos);
2402                 if( text_width != xscroll->get_length() ||
2403                     view_w != xscroll->get_handlelength() )
2404                         xscroll->update_length(text_width, xpos, view_w, 0);
2405         }
2406         if( yscroll ) {
2407                 yscroll->reposition_window(x + w - yscroll->get_span(), y, text->get_h());
2408                 int text_row = text->get_text_row();
2409                 if( text_row != yscroll->get_value() )
2410                         yscroll->update_value(text_row);
2411                 if( text_rows != yscroll->get_length() ||
2412                     view_rows != yscroll->get_handlelength() )
2413                         yscroll->update_length(text_rows, text_row, view_rows, 0);
2414         }
2415 }
2416
2417 int BC_ScrollTextBoxText::handle_event()
2418 {
2419         gui->update_scrollbars();
2420         return gui->handle_event();
2421 }
2422
2423 int BC_ScrollTextBoxText::motion_event()
2424 {
2425         gui->update_scrollbars();
2426         return 1;
2427 }
2428
2429 BC_ScrollTextBoxXScroll::BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui)
2430  : BC_ScrollBar(gui->x, gui->y + gui->text->get_h(), SCROLL_HORIZ + SCROLL_STRETCH,
2431         gui->text->get_w(), gui->text->get_text_width(MEDIUMFONT, gui->get_wtext()),
2432         0, gui->w)
2433 {
2434         this->gui = gui;
2435 }
2436
2437 BC_ScrollTextBoxXScroll::~BC_ScrollTextBoxXScroll()
2438 {
2439 }
2440
2441 int BC_ScrollTextBoxXScroll::handle_event()
2442 {
2443         gui->set_x_pos(get_position());
2444         return 1;
2445 }
2446
2447 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2448  : BC_ScrollBar(gui->x + gui->text->get_w(), gui->y, SCROLL_VERT,
2449         gui->text->get_h(), gui->text->get_text_rows(), 0, gui->rows)
2450 {
2451         this->gui = gui;
2452 }
2453
2454 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2455 {
2456 }
2457
2458 int BC_ScrollTextBoxYScroll::handle_event()
2459 {
2460         gui->text->set_text_row(get_position());
2461         return 1;
2462 }
2463
2464
2465
2466 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2467  : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2468 {
2469         this->popup = popup;
2470 }
2471
2472 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2473  : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2474 {
2475         this->popup = popup;
2476 }
2477
2478 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2479 {
2480         if(popup) {
2481                 popup->textbox = 0;
2482                 delete popup;
2483                 popup = 0;
2484         }
2485 }
2486
2487
2488 int BC_PopupTextBoxText::handle_event()
2489 {
2490         popup->handle_event();
2491         return 1;
2492 }
2493
2494 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2495  : BC_ListBox(x, y,
2496         popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2497         popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2498 {
2499         this->popup = popup;
2500 }
2501 int BC_PopupTextBoxList::handle_event()
2502 {
2503         BC_ListBoxItem *item = get_selection(0, 0);
2504         if(item)
2505         {
2506                 popup->textbox->update(item->get_text());
2507                 popup->textbox->set_text_row(0);
2508                 popup->handle_event();
2509         }
2510         return 1;
2511 }
2512
2513
2514
2515
2516 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2517                 ArrayList<BC_ListBoxItem*> *list_items,
2518                 const char *default_text, int x, int y,
2519                 int text_w, int list_h, int list_format)
2520 {
2521         this->x = x;
2522         this->y = y;
2523         this->list_h = list_h;
2524         this->list_format = list_format;
2525         this->default_text = (char*)default_text;
2526         this->default_wtext = 0;
2527         this->text_w = text_w;
2528         this->parent_window = parent_window;
2529         this->list_items = list_items;
2530 }
2531
2532 BC_PopupTextBox::~BC_PopupTextBox()
2533 {
2534         delete listbox;
2535         if(textbox)
2536         {
2537                 textbox->popup = 0;
2538                 delete textbox;
2539         }
2540 }
2541
2542 int BC_PopupTextBox::create_objects()
2543 {
2544         int x = this->x, y = this->y;
2545         parent_window->add_subwindow(textbox = default_wtext ?
2546                  new BC_PopupTextBoxText(this, x, y, default_wtext) :
2547                  new BC_PopupTextBoxText(this, x, y, default_text));
2548         x += textbox->get_w();
2549         parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2550         return 0;
2551 }
2552
2553 void BC_PopupTextBox::update(const char *text)
2554 {
2555         textbox->update(text);
2556         textbox->set_text_row(0);
2557 }
2558
2559 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2560 {
2561         listbox->update(data, 0, 0, 1);
2562 }
2563
2564
2565 const char* BC_PopupTextBox::get_text()
2566 {
2567         return textbox->get_text();
2568 }
2569
2570 const wchar_t* BC_PopupTextBox::get_wtext()
2571 {
2572         return textbox->get_wtext();
2573 }
2574
2575 int BC_PopupTextBox::get_number()
2576 {
2577         return listbox->get_selection_number(0, 0);
2578 }
2579
2580 int BC_PopupTextBox::get_x()
2581 {
2582         return x;
2583 }
2584
2585 int BC_PopupTextBox::get_y()
2586 {
2587         return y;
2588 }
2589
2590 int BC_PopupTextBox::get_w()
2591 {
2592         return textbox->get_w() + listbox->get_w();
2593 }
2594
2595 int BC_PopupTextBox::get_h()
2596 {
2597         return textbox->get_h();
2598 }
2599
2600 int BC_PopupTextBox::get_show_query()
2601 {
2602         return listbox->get_show_query();
2603 }
2604
2605 void BC_PopupTextBox::set_show_query(int v)
2606 {
2607         listbox->set_show_query(v);
2608 }
2609
2610 int BC_PopupTextBox::handle_event()
2611 {
2612         return 1;
2613 }
2614
2615 void BC_PopupTextBox::reposition_window(int x, int y)
2616 {
2617         this->x = x;
2618         this->y = y;
2619         int x1 = x, y1 = y;
2620         textbox->reposition_window(x1,
2621                 y1,
2622                 textbox->get_w(),
2623                 textbox->get_rows());
2624         x1 += textbox->get_w();
2625         listbox->reposition_window(x1, y1, -1, -1, 0);
2626 //      if(flush) parent_window->flush();
2627 }
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2643         int64_t default_value, int x, int y)
2644  : BC_TextBox(x, y, popup->text_w, 1, default_value)
2645 {
2646         this->popup = popup;
2647 }
2648
2649 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2650         float default_value, int x, int y, int precision)
2651  : BC_TextBox(x, y, popup->text_w, 1, default_value, 1, MEDIUMFONT, precision)
2652 {
2653         this->popup = popup;
2654 }
2655
2656 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2657 {
2658         if(popup)
2659         {
2660                 popup->textbox = 0;
2661                 delete popup;
2662                 popup = 0;
2663         }
2664 }
2665
2666
2667
2668 int BC_TumbleTextBoxText::handle_event()
2669 {
2670         popup->handle_event();
2671         return 1;
2672 }
2673
2674 int BC_TumbleTextBoxText::button_press_event()
2675 {
2676         if( get_enabled() && is_event_win() ) {
2677                 if( get_buttonpress() < 4 ) return BC_TextBox::button_press_event();
2678                 if( get_buttonpress() == 4 )      popup->tumbler->handle_up_event();
2679                 else if( get_buttonpress() == 5 ) popup->tumbler->handle_down_event();
2680                 return 1;
2681         }
2682         return 0;
2683 }
2684
2685
2686
2687
2688 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2689                 int64_t default_value, int64_t min, int64_t max,
2690                 int x, int y, int text_w)
2691 {
2692         reset();
2693         this->x = x;
2694         this->y = y;
2695         this->min = min;
2696         this->max = max;
2697         this->default_value = default_value;
2698         this->text_w = text_w;
2699         this->parent_window = parent_window;
2700         use_float = 0;
2701         precision = 4;
2702         increment = 1;
2703 }
2704
2705 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2706                 int default_value, int min, int max,
2707                 int x, int y, int text_w)
2708 {
2709         reset();
2710         this->x = x;
2711         this->y = y;
2712         this->min = min;
2713         this->max = max;
2714         this->default_value = default_value;
2715         this->text_w = text_w;
2716         this->parent_window = parent_window;
2717         use_float = 0;
2718         precision = 4;
2719         increment = 1;
2720 }
2721
2722 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2723                 float default_value_f, float min_f, float max_f,
2724                 int x, int y, int text_w, int precision)
2725 {
2726         reset();
2727         this->x = x;
2728         this->y = y;
2729         this->min_f = min_f;
2730         this->max_f = max_f;
2731         this->default_value_f = default_value_f;
2732         this->text_w = text_w;
2733         this->precision = precision;
2734         this->parent_window = parent_window;
2735         use_float = 1;
2736         increment = 1;
2737 }
2738
2739 BC_TumbleTextBox::~BC_TumbleTextBox()
2740 {
2741 // Recursive delete.  Normally ~BC_TumbleTextBox is never called but textbox
2742 // is deleted anyway by the windowbase so textbox deletes this.
2743         if(tumbler) delete tumbler;
2744         tumbler = 0;
2745 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2746         if(textbox)
2747         {
2748                 textbox->popup = 0;
2749                 delete textbox;
2750         }
2751         textbox = 0;
2752 }
2753
2754 void BC_TumbleTextBox::reset()
2755 {
2756         textbox = 0;
2757         tumbler = 0;
2758         increment = 1.0;
2759 }
2760
2761 void BC_TumbleTextBox::set_precision(int precision)
2762 {
2763         this->precision = precision;
2764 }
2765
2766 void BC_TumbleTextBox::set_increment(float value)
2767 {
2768         this->increment = value;
2769         if(tumbler) tumbler->set_increment(value);
2770 }
2771
2772 void BC_TumbleTextBox::set_log_floatincrement(int value)
2773 {
2774         this->log_floatincrement = value;
2775         if(tumbler) tumbler->set_log_floatincrement(value);
2776 }
2777
2778 int BC_TumbleTextBox::create_objects()
2779 {
2780         int x = this->x, y = this->y;
2781
2782         textbox = use_float ?
2783                 new BC_TumbleTextBoxText(this, default_value_f, x, y, precision) :
2784                 new BC_TumbleTextBoxText(this, default_value, x, y);
2785
2786         parent_window->add_subwindow(textbox);
2787         x += textbox->get_w();
2788
2789         tumbler = use_float ?
2790                 (BC_Tumbler *)new BC_FTumbler(textbox, min_f, max_f, x, y) :
2791                 (BC_Tumbler *)new BC_ITumbler(textbox, min, max, x, y);
2792         parent_window->add_subwindow(tumbler);
2793         tumbler->set_increment(increment);
2794         return 0;
2795 }
2796
2797 const char* BC_TumbleTextBox::get_text()
2798 {
2799         return textbox->get_text();
2800 }
2801
2802 const wchar_t* BC_TumbleTextBox::get_wtext()
2803 {
2804         return textbox->get_wtext();
2805 }
2806
2807 BC_TextBox* BC_TumbleTextBox::get_textbox()
2808 {
2809         return textbox;
2810 }
2811
2812 int BC_TumbleTextBox::update(const char *value)
2813 {
2814         textbox->update(value);
2815         textbox->set_text_row(0);
2816         return 0;
2817 }
2818
2819 int BC_TumbleTextBox::update(int64_t value)
2820 {
2821         textbox->update(value);
2822         textbox->set_text_row(0);
2823         return 0;
2824 }
2825
2826 int BC_TumbleTextBox::update(float value)
2827 {
2828         textbox->update(value);
2829         textbox->set_text_row(0);
2830         return 0;
2831 }
2832
2833
2834 int BC_TumbleTextBox::get_x()
2835 {
2836         return x;
2837 }
2838
2839 int BC_TumbleTextBox::get_y()
2840 {
2841         return y;
2842 }
2843
2844 int BC_TumbleTextBox::get_w()
2845 {
2846         return textbox->get_w() + tumbler->get_w();
2847 }
2848
2849 int BC_TumbleTextBox::get_h()
2850 {
2851         return textbox->get_h();
2852 }
2853
2854 void BC_TumbleTextBox::disable(int hide_text)
2855 {
2856         if( hide_text && !textbox->is_hidden() )
2857                 textbox->hide_window(0);
2858         if( !tumbler->is_hidden() )
2859                 tumbler->hide_window(0);
2860         if( !get_enabled() ) return;
2861         return textbox->disable();
2862 }
2863
2864 void BC_TumbleTextBox::enable()
2865 {
2866         if( textbox->is_hidden() )
2867                 textbox->show_window(0);
2868         if( tumbler->is_hidden() )
2869                 tumbler->show_window(0);
2870         if( get_enabled() ) return;
2871         return textbox->enable();
2872 }
2873
2874 int BC_TumbleTextBox::get_enabled()
2875 {
2876         return textbox->get_enabled();
2877 }
2878
2879 int BC_TumbleTextBox::handle_event()
2880 {
2881         return 1;
2882 }
2883
2884 void BC_TumbleTextBox::reposition_window(int x, int y)
2885 {
2886         this->x = x;
2887         this->y = y;
2888
2889         textbox->reposition_window(x,
2890                 y,
2891                 text_w,
2892                 1);
2893         tumbler->reposition_window(x + textbox->get_w(),
2894                 y);
2895 //      if(flush) parent_window->flush();
2896 }
2897
2898
2899 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2900 {
2901         tumbler->set_boundaries(min, max);
2902 }
2903
2904 void BC_TumbleTextBox::set_boundaries(float min, float max)
2905 {
2906         tumbler->set_boundaries(min, max);
2907 }
2908
2909
2910
2911 BC_TextMenu::BC_TextMenu(BC_TextBox *textbox)
2912  : BC_PopupMenu(0, 0, 0, "", 0)
2913 {
2914         this->textbox = textbox;
2915 }
2916
2917 BC_TextMenu::~BC_TextMenu()
2918 {
2919 }
2920
2921 void BC_TextMenu::create_objects()
2922 {
2923         add_item(new BC_TextMenuCut(this));
2924         add_item(new BC_TextMenuCopy(this));
2925         add_item(new BC_TextMenuPaste(this));
2926 }
2927
2928
2929 BC_TextMenuCut::BC_TextMenuCut(BC_TextMenu *menu) 
2930  : BC_MenuItem(_("Cut"))
2931 {
2932         this->menu = menu;
2933 }
2934
2935 int BC_TextMenuCut::handle_event()
2936 {
2937         menu->textbox->cut(1);
2938         
2939         return 0;
2940 }
2941
2942
2943 BC_TextMenuCopy::BC_TextMenuCopy(BC_TextMenu *menu) 
2944  : BC_MenuItem(_("Copy"))
2945 {
2946         this->menu = menu;
2947 }
2948
2949 int BC_TextMenuCopy::handle_event()
2950 {
2951         menu->textbox->copy(1);
2952         return 0;
2953 }
2954
2955
2956 BC_TextMenuPaste::BC_TextMenuPaste(BC_TextMenu *menu) 
2957  : BC_MenuItem(_("Paste"))
2958 {
2959         this->menu = menu;
2960 }
2961
2962 int BC_TextMenuPaste::handle_event()
2963 {
2964         menu->textbox->paste(1);
2965         return 0;
2966 }
2967
2968
2969 void BC_TumbleTextBox::set_tooltip(const char *text)
2970 {
2971         textbox->set_tooltip(text);
2972 }
2973