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