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