add alpha to bluebananna, add unicode Ctl/Shft/U textbox input, bluray presets
[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
770 // Get ibeam location
771                         if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
772                                 need_ibeam = 0;
773                                 ibeam_y = k - text_y;
774                                 ibeam_x = positions[ibeam_letter];
775                         }
776                 }
777         }
778
779 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
780         if(need_ibeam) {
781                 if( wtext_len == 0 ) {
782                         ibeam_x = 0;
783                         ibeam_y = 0;
784                 }
785                 else {
786                         ibeam_x = -1;
787                         ibeam_y = -1;
788                 }
789         }
790
791 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
792 // Draw solid cursor
793         if (active)
794                 draw_cursor();
795
796 // Border
797         draw_border();
798         flash(flush);
799 }
800
801 int BC_TextBox::focus_in_event()
802 {
803         draw(1);
804         return 1;
805 }
806
807 int BC_TextBox::focus_out_event()
808 {
809         draw(1);
810         return 1;
811 }
812
813 int BC_TextBox::cursor_enter_event()
814 {
815         if(top_level->event_win == win && enabled)
816         {
817                 tooltip_done = 0;
818
819                 if(!highlighted)
820                 {
821                         highlighted = 1;
822                         draw_border();
823                         flash(1);
824                 }
825         }
826         return 0;
827 }
828
829 int BC_TextBox::cursor_leave_event()
830 {
831         if(highlighted)
832         {
833                 highlighted = 0;
834                 draw_border();
835                 hide_tooltip();
836                 flash(1);
837         }
838         return 0;
839 }
840
841 int BC_TextBox::button_press_event()
842 {
843         const int debug = 0;
844
845         if(!enabled) return 0;
846         if(get_buttonpress() != WHEEL_UP &&
847                 get_buttonpress() != WHEEL_DOWN &&
848                 get_buttonpress() != LEFT_BUTTON &&
849                 get_buttonpress() != MIDDLE_BUTTON) return 0;
850
851
852
853         if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
854
855         int cursor_letter = 0;
856         int wtext_len = wtext_update();
857         int update_scroll = 0;
858
859
860         if(top_level->event_win == win)
861         {
862                 if(!active)
863                 {
864                         hide_tooltip();
865                         top_level->deactivate();
866                         activate();
867                 }
868
869
870                 if(get_buttonpress() == WHEEL_UP)
871                 {
872                         text_y += text_height;
873                         text_y = MIN(text_y, top_margin);
874                         update_scroll = 1;
875                 }
876                 else
877                 if(get_buttonpress() == WHEEL_DOWN)
878                 {
879                         int min_y = -(get_text_rows() *
880                                 text_height -
881                                 get_h() +
882                                 bottom_margin);
883                         text_y -= text_height;
884                         text_y = MAX(text_y, min_y);
885                         text_y = MIN(text_y, top_margin);
886                         update_scroll = 1;
887                 }
888                 else
889                 {
890
891                 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
892
893         //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
894
895
896                 if(get_triple_click())
897                 {
898         //printf("BC_TextBox::button_press_event %d\n", __LINE__);
899                         line_selected = 1;
900                         select_line(highlight_letter1, highlight_letter2, cursor_letter);
901                         highlight_letter3 = highlight_letter1;
902                         highlight_letter4 = highlight_letter2;
903                         ibeam_letter = highlight_letter2;
904                         copy_selection(PRIMARY_SELECTION);
905                 }
906                 else
907                 if(get_double_click())
908                 {
909                         word_selected = 1;
910                         select_word(highlight_letter1, highlight_letter2, cursor_letter);
911                         highlight_letter3 = highlight_letter1;
912                         highlight_letter4 = highlight_letter2;
913                         ibeam_letter = highlight_letter2;
914                         copy_selection(PRIMARY_SELECTION);
915                 }
916                 else
917                 if(get_buttonpress() == MIDDLE_BUTTON)
918                 {
919                         highlight_letter3 = highlight_letter4 =
920                                 ibeam_letter = highlight_letter1 =
921                                 highlight_letter2 = cursor_letter;
922                         paste_selection(PRIMARY_SELECTION);
923                 }
924                 else
925                 {
926                         text_selected = 1;
927                         highlight_letter3 = highlight_letter4 =
928                                 ibeam_letter = highlight_letter1 =
929                                 highlight_letter2 = cursor_letter;
930                 }
931
932
933         // Handle scrolling by highlighting text
934                 if(text_selected || word_selected || line_selected)
935                 {
936                         set_repeat(top_level->get_resources()->scroll_repeat);
937                 }
938
939                 if(ibeam_letter < 0) ibeam_letter = 0;
940                 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
941                 }
942
943                 draw(1);
944                 if(update_scroll && yscroll)
945                 {
946                         yscroll->update_length(get_text_rows(),
947                                 get_text_row(),
948                                 yscroll->get_handlelength(),
949                                 1);
950                 }
951                 handle_event();
952                 return 1;
953         }
954         else
955         if(active && (!yscroll || !yscroll->is_event_win()))
956         {
957 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
958 // Suggestion popup is not active but must be deactivated.
959                 if(suggestions_popup)
960                 {
961                         return 0;
962 // printf("BC_TextBox::button_press_event %d\n", __LINE__);
963 // // Pass event to suggestions popup
964 //                      if(!suggestions_popup->button_press_event())
965 //                      {
966 // printf("BC_TextBox::button_press_event %d\n", __LINE__);
967 //                              top_level->deactivate();
968 //                      }
969                 }
970                 else
971                 {
972                         top_level->deactivate();
973                 }
974         }
975
976
977         return 0;
978 }
979
980 int BC_TextBox::button_release_event()
981 {
982         if(active)
983         {
984                 hide_tooltip();
985                 if(text_selected || word_selected || line_selected)
986                 {
987                         text_selected = 0;
988                         word_selected = 0;
989                         line_selected = 0;
990
991 // Stop scrolling by highlighting text
992                         unset_repeat(top_level->get_resources()->scroll_repeat);
993                 }
994         }
995         return 0;
996 }
997
998 int BC_TextBox::cursor_motion_event()
999 {
1000         int cursor_letter, letter1, letter2;
1001         if(active)
1002         {
1003                 if(text_selected || word_selected || line_selected)
1004                 {
1005                         cursor_letter = get_cursor_letter(top_level->cursor_x,
1006                                 top_level->cursor_y);
1007
1008 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
1009
1010                         if(line_selected)
1011                         {
1012                                 select_line(letter1, letter2, cursor_letter);
1013                         }
1014                         else
1015                         if(word_selected)
1016                         {
1017                                 select_word(letter1, letter2, cursor_letter);
1018                         }
1019                         else
1020                         if(text_selected)
1021                         {
1022                                 letter1 = letter2 = cursor_letter;
1023                         }
1024
1025                         if(letter1 <= highlight_letter3)
1026                         {
1027                                 highlight_letter1 = letter1;
1028                                 highlight_letter2 = highlight_letter4;
1029                                 ibeam_letter = letter1;
1030                         }
1031                         else
1032                         if(letter2 >= highlight_letter4)
1033                         {
1034                                 highlight_letter2 = letter2;
1035                                 highlight_letter1 = highlight_letter3;
1036                                 ibeam_letter = letter2;
1037                         }
1038
1039                         copy_selection(PRIMARY_SELECTION);
1040
1041
1042
1043
1044                         draw(1);
1045                         return 1;
1046                 }
1047         }
1048
1049         return 0;
1050 }
1051
1052 int BC_TextBox::activate()
1053 {
1054         top_level->active_subwindow = this;
1055         active = 1;
1056         draw(1);
1057         top_level->set_repeat(top_level->get_resources()->blink_rate);
1058         return 0;
1059 }
1060
1061 int BC_TextBox::deactivate()
1062 {
1063 //printf("BC_TextBox::deactivate %d suggestions_popup=%p\n", __LINE__, suggestions_popup);
1064         active = 0;
1065         top_level->unset_repeat(top_level->get_resources()->blink_rate);
1066         if(suggestions_popup)
1067         {
1068 // Must deactivate instead of delete since this is called from BC_ListBox::button_press_event
1069 //              suggestions_popup->deactivate();
1070
1071                 delete suggestions_popup;
1072                 suggestions_popup = 0;
1073         }
1074
1075         draw(1);
1076         return 0;
1077 }
1078
1079 int BC_TextBox::repeat_event(int64_t duration)
1080 {
1081         int result = 0;
1082         int cursor_y = get_cursor_y();
1083         //int cursor_x = get_cursor_x();
1084
1085         if(duration == top_level->get_resources()->tooltip_delay &&
1086                 tooltip_text && tooltip_text[0] != 0 && highlighted)
1087         {
1088                 show_tooltip();
1089                 tooltip_done = 1;
1090                 result = 1;
1091         }
1092
1093         if(duration == top_level->get_resources()->blink_rate &&
1094                 active &&
1095                 get_has_focus())
1096         {
1097 // don't flash if keypress
1098                 if(skip_cursor->get_difference() < 500)
1099                 {
1100 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1101 // skip_cursor->get_difference(),
1102 // duration);
1103                         result = 1;
1104                 }
1105                 else
1106                 {
1107                         if(!(text_selected || word_selected || line_selected))
1108                         {
1109                                 draw_cursor();
1110                                 flash(1);
1111                         }
1112                         result = 1;
1113                 }
1114         }
1115
1116         if(duration == top_level->get_resources()->scroll_repeat &&
1117                 (text_selected || word_selected || line_selected))
1118         {
1119                 int difference = 0;
1120                 if(get_cursor_y() < top_margin)
1121                 {
1122                         difference = get_cursor_y() - top_margin ;
1123                 }
1124                 else
1125                 if(get_cursor_y() > get_h() - bottom_margin)
1126                 {
1127                         difference = get_cursor_y() -
1128                                 (get_h() - bottom_margin);
1129                 }
1130                 if(difference != 0) {
1131                         int min_y = -(get_text_rows() * text_height -
1132                                 get_h() + bottom_margin);
1133
1134                         text_y -= difference;
1135 // printf("BC_TextBox::repeat_event %d %d %d\n",
1136 // __LINE__,
1137 // text_y,
1138 // min_y);
1139                         text_y = MAX(min_y, text_y);
1140                         text_y = MIN(text_y, top_margin);
1141
1142                         draw(1);
1143                         motion_event();
1144                         result = 1;
1145                 }
1146
1147                 if(get_cursor_x() < left_margin)
1148                 {
1149                         int difference = left_margin - get_cursor_x();
1150
1151                         text_x += difference;
1152                         text_x = MIN(text_x, left_margin);
1153                         draw(1);
1154                         result = 1;
1155                 }
1156                 else if(get_cursor_x() > get_w() - right_margin)
1157                 {
1158                         int difference = get_cursor_x() - (get_w() - right_margin);
1159                         int new_text_x = text_x - difference;
1160
1161 // Get width of current row
1162                         int min_x = 0;
1163                         int row_width = 0;
1164                         int wtext_len = wtext_update();
1165                         int row_begin = 0;
1166                         int row_end = 0;
1167                         for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1168                         {
1169                                 row_begin = i;
1170                                 while(wtext[i] != '\n' && i < wtext_len) {
1171                                         i++;
1172                                 }
1173                                 row_end = i;
1174                                 if(wtext[i] == '\n') i++;
1175
1176                                 if(cursor_y >= k && cursor_y < k + text_height) {
1177                                         row_width = get_text_width(font,
1178                                                 wtext + row_begin,
1179                                                 row_end - row_begin);
1180
1181                                         break;
1182                                 }
1183                         }
1184
1185                         min_x = -row_width + get_w() - left_margin - BCCURSORW;
1186                         new_text_x = MAX(new_text_x, min_x);
1187                         new_text_x = MIN(new_text_x, left_margin);
1188
1189                         if(new_text_x < text_x) text_x = new_text_x;
1190                         draw(1);
1191                         result = 1;
1192                 }
1193         }
1194
1195         return result;
1196 }
1197
1198 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1199 {
1200         if( (top_level->get_keypress() == RETURN) ||
1201             (top_level->get_keypress() > 30 && top_level->get_keypress() <= 255)) {
1202                 int len;
1203                 wchar_t *temp_string = top_level->get_wkeystring(&len);
1204                 if(top_level->get_keypress() == RETURN) {
1205                         temp_string[0] = '\n';  temp_string[1] = 0;
1206                         len = 1;
1207                 }
1208                 insert_text(temp_string, len);
1209                 find_ibeam(1);
1210                 draw(1);
1211                 dispatch_event = 1;
1212                 result = 1;
1213         }
1214 }
1215
1216 int BC_TextBox::keypress_event()
1217 {
1218 // Result == 2 contents changed
1219 // Result == 1 trapped keypress
1220 // Result == 0 nothing
1221         int result = 0;
1222         int dispatch_event = 0;
1223
1224         if(!active || !enabled) return 0;
1225
1226         int wtext_len = wtext_update();
1227         last_keypress = get_keypress();
1228
1229         if( unicode_active >= 0 ) {
1230                 wchar_t wch = 0;
1231                 int wlen =  -1;
1232                 switch( last_keypress ) {
1233 //unicode active acitons
1234                 case ESC: {
1235                         unicode_active = -1;
1236                         result = 1;
1237                         wlen = 0;
1238                         break; }
1239                 case RETURN: {
1240                         for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1241                                 int ch = nib(wtext[i]);
1242                                 if( ch < 0 ) return 1;
1243                                 wch = (wch<<4) + ch;
1244                         }
1245                         unicode_active = -1;
1246                         dispatch_event = 1;
1247                         result = 1;
1248                         wlen = 1;
1249                         break; }
1250                 case BACKSPACE: {
1251                         if(ibeam_letter > 0) {
1252                                 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1253                                 highlight_letter2 = --ibeam_letter;
1254                                 if( highlight_letter1 >= highlight_letter2 )
1255                                         unicode_active = -1;
1256                         }
1257                         result = 1;
1258                         wlen = 0;
1259                         break; }
1260                 case '0': case '1': case '2': case '3': case '4':
1261                 case '5': case '6': case '7': case '8': case '9':
1262                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1263                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1264                         int n = nib(last_keypress);
1265                         wch = n < 10 ? '0'+n : 'A'+n-10;
1266                         result = 1;
1267                         wlen = 1;
1268                         break; }
1269 //normal actions
1270                 case TAB:
1271                 case LEFTTAB:
1272                         break;
1273 //ignored actions
1274                 default:
1275                         result = 1;
1276                         break;
1277                 }
1278                 if( wlen >= 0 ) {
1279                         insert_text(&wch, wlen);
1280                         find_ibeam(1);
1281                         if( unicode_active >= 0 )
1282                                 highlight_letter2 = ibeam_letter;
1283                         else
1284                                 highlight_letter1 = highlight_letter2 = 0;
1285                         draw(1);
1286                 }
1287         }
1288
1289         if( !result ) {
1290 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1291                 switch(last_keypress) {
1292                 case ESC: {
1293 // Deactivate the suggestions
1294                         if(suggestions && suggestions_popup) {
1295                                 delete suggestions_popup;
1296                                 suggestions_popup = 0;
1297                                 result = 1;
1298                         }
1299                         else {
1300                                 top_level->deactivate();
1301                                 result = 0;
1302                         }
1303                         break; }
1304
1305                 case RETURN: {
1306                         if( rows == 1 ) {
1307                                 top_level->deactivate();
1308                                 dispatch_event = 1;
1309                                 result = 0;
1310                         }
1311                         else {
1312                                 default_keypress(dispatch_event, result);
1313                         }
1314                         break; }
1315
1316
1317 // Handle like a default keypress
1318                 case TAB: {
1319                         top_level->cycle_textboxes(1);
1320                         result = 1;
1321                         break; }
1322
1323                 case LEFTTAB: {
1324                         top_level->cycle_textboxes(-1);
1325                         result = 1;
1326                         break; }
1327
1328                 case LEFT: {
1329                         if(ibeam_letter > 0) {
1330                                 int old_ibeam_letter = ibeam_letter;
1331 // Single character
1332                                 if(!ctrl_down()) {
1333                                         ibeam_letter--;
1334                                 }
1335                                 else {
1336 // Word
1337                                         ibeam_letter--;
1338                                         while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1339                                                 ibeam_letter--;
1340                                 }
1341
1342 // Extend selection
1343                                 if(top_level->shift_down()) {
1344 // Initialize highlighting
1345                                         if(highlight_letter1 == highlight_letter2) {
1346                                                 highlight_letter1 = ibeam_letter;
1347                                                 highlight_letter2 = old_ibeam_letter;
1348                                         }
1349                                         else if(highlight_letter1 == old_ibeam_letter) {
1350 // Extend left highlight
1351                                                 highlight_letter1 = ibeam_letter;
1352                                         }
1353                                         else if(highlight_letter2 == old_ibeam_letter) {
1354 // Shrink right highlight
1355                                                 highlight_letter2 = ibeam_letter;
1356                                         }
1357                                 }
1358                                 else {
1359                                         highlight_letter1 = highlight_letter2 = ibeam_letter;
1360                                 }
1361
1362
1363                                 find_ibeam(1);
1364                                 if(keypress_draw) draw(1);
1365                         }
1366                         result = 1;
1367                         break; }
1368
1369                 case RIGHT: {
1370                         if(ibeam_letter < wtext_len) {
1371                                 int old_ibeam_letter = ibeam_letter;
1372 // Single character
1373                                 if(!ctrl_down()) {
1374                                         ibeam_letter++;
1375                                 }
1376                                 else {
1377 // Word
1378                                         while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1379                                 }
1380
1381
1382
1383 // Extend selection
1384                                 if(top_level->shift_down()) {
1385 // Initialize highlighting
1386                                         if(highlight_letter1 == highlight_letter2) {
1387                                                 highlight_letter1 = old_ibeam_letter;
1388                                                 highlight_letter2 = ibeam_letter;
1389                                         }
1390                                         else if(highlight_letter1 == old_ibeam_letter) {
1391 // Shrink left highlight
1392                                                 highlight_letter1 = ibeam_letter;
1393                                         }
1394                                         else if(highlight_letter2 == old_ibeam_letter) {
1395 // Expand right highlight
1396                                                 highlight_letter2 = ibeam_letter;
1397                                         }
1398                                 }
1399                                 else {
1400                                         highlight_letter1 = highlight_letter2 = ibeam_letter;
1401                                 }
1402
1403                                 find_ibeam(1);
1404                                 if(keypress_draw) draw(1);
1405                         }
1406                         result = 1;
1407                         break; }
1408
1409                 case UP: {
1410                         if(suggestions && suggestions_popup) {
1411 // Pass to suggestions popup
1412 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1413                                 suggestions_popup->activate(1);
1414                                 suggestions_popup->keypress_event();
1415                                 result = 1;
1416                         }
1417                         else if(ibeam_letter > 0) {
1418 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1419                                 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1420                                         ibeam_y + text_y - text_height);
1421 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1422
1423 // Extend selection
1424                                 if(top_level->shift_down()) {
1425 // Initialize highlighting
1426                                         if(highlight_letter1 == highlight_letter2) {
1427                                                 highlight_letter1 = new_letter;
1428                                                 highlight_letter2 = ibeam_letter;
1429                                         }
1430                                         else if(highlight_letter1 == ibeam_letter) {
1431 // Expand left highlight
1432                                                 highlight_letter1 = new_letter;
1433                                         }
1434                                         else if(highlight_letter2 == ibeam_letter) {
1435 // Shrink right highlight
1436                                                 highlight_letter2 = new_letter;
1437                                         }
1438                                 }
1439                                 else
1440                                         highlight_letter1 = highlight_letter2 = new_letter;
1441
1442                                 if(highlight_letter1 > highlight_letter2) {
1443                                         int temp = highlight_letter1;
1444                                         highlight_letter1 = highlight_letter2;
1445                                         highlight_letter2 = temp;
1446                                 }
1447                                 ibeam_letter = new_letter;
1448
1449                                 find_ibeam(1);
1450                                 if(keypress_draw) draw(1);
1451                         }
1452                         result = 1;
1453                         break; }
1454
1455                 case PGUP: {
1456                         if(ibeam_letter > 0) {
1457                                 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1458                                         ibeam_y + text_y - get_h());
1459
1460 // Extend selection
1461                                 if(top_level->shift_down()) {
1462 // Initialize highlighting
1463                                         if(highlight_letter1 == highlight_letter2) {
1464                                                 highlight_letter1 = new_letter;
1465                                                 highlight_letter2 = ibeam_letter;
1466                                         }
1467                                         else if(highlight_letter1 == ibeam_letter) {
1468 // Expand left highlight
1469                                                 highlight_letter1 = new_letter;
1470                                         }
1471                                         else if(highlight_letter2 == ibeam_letter) {
1472 // Shrink right highlight
1473                                                 highlight_letter2 = new_letter;
1474                                         }
1475                                 }
1476                                 else
1477                                         highlight_letter1 = highlight_letter2 = new_letter;
1478
1479                                 if(highlight_letter1 > highlight_letter2) {
1480                                         int temp = highlight_letter1;
1481                                         highlight_letter1 = highlight_letter2;
1482                                         highlight_letter2 = temp;
1483                                 }
1484                                 ibeam_letter = new_letter;
1485
1486                                 find_ibeam(1);
1487                                 if(keypress_draw) draw(1);
1488                         }
1489                         result = 1;
1490                         break; }
1491
1492                 case DOWN: {
1493 // printf("BC_TextBox::keypress_event %d %p %p\n",
1494 // __LINE__,
1495 // suggestions,
1496 // suggestions_popup);
1497                         if(suggestions && suggestions_popup) {
1498 // Pass to suggestions popup
1499                                 suggestions_popup->activate(1);
1500                                 suggestions_popup->keypress_event();
1501                                 result = 1;
1502                         }
1503                         else
1504 //                      if(ibeam_letter > 0)
1505                         {
1506 // Extend selection
1507                                 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1508                                         ibeam_y + text_y + text_height);
1509 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1510
1511                                 if(top_level->shift_down()) {
1512 // Initialize highlighting
1513                                         if(highlight_letter1 == highlight_letter2) {
1514                                                 highlight_letter1 = new_letter;
1515                                                 highlight_letter2 = ibeam_letter;
1516                                         }
1517                                         else if(highlight_letter1 == ibeam_letter) {
1518 // Shrink left highlight
1519                                                 highlight_letter1 = new_letter;
1520                                         }
1521                                         else if(highlight_letter2 == ibeam_letter) {
1522 // Expand right highlight
1523                                                 highlight_letter2 = new_letter;
1524                                         }
1525                                 }
1526                                 else
1527                                         highlight_letter1 = highlight_letter2 = new_letter;
1528
1529                                 if(highlight_letter1 > highlight_letter2) {
1530                                         int temp = highlight_letter1;
1531                                         highlight_letter1 = highlight_letter2;
1532                                         highlight_letter2 = temp;
1533                                 }
1534                                 ibeam_letter = new_letter;
1535
1536                                 find_ibeam(1);
1537                                 if(keypress_draw) draw(1);
1538
1539 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1540                         }
1541                         result = 1;
1542                         break; }
1543
1544                 case PGDN: {
1545 // Extend selection
1546                         int new_letter = get_cursor_letter2(ibeam_x + text_x,
1547                                 ibeam_y + text_y + get_h());
1548 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1549
1550                         if(top_level->shift_down()) {
1551 // Initialize highlighting
1552                                 if(highlight_letter1 == highlight_letter2) {
1553                                         highlight_letter1 = new_letter;
1554                                         highlight_letter2 = ibeam_letter;
1555                                 }
1556                                 else if(highlight_letter1 == ibeam_letter) {
1557 // Shrink left highlight
1558                                         highlight_letter1 = new_letter;
1559                                 }
1560                                 else if(highlight_letter2 == ibeam_letter) {
1561 // Expand right highlight
1562                                         highlight_letter2 = new_letter;
1563                                 }
1564                         }
1565                         else
1566                                 highlight_letter1 = highlight_letter2 = new_letter;
1567
1568                         if(highlight_letter1 > highlight_letter2) {
1569                                 int temp = highlight_letter1;
1570                                 highlight_letter1 = highlight_letter2;
1571                                 highlight_letter2 = temp;
1572                         }
1573                         ibeam_letter = new_letter;
1574
1575                         find_ibeam(1);
1576                         if(keypress_draw) draw(1);
1577
1578 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1579                         result = 1;
1580                         break; }
1581
1582                 case END: {
1583                         delete suggestions_popup;
1584                         suggestions_popup = 0;
1585
1586                         int old_ibeam_letter = ibeam_letter;
1587
1588                         while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1589                                 ibeam_letter++;
1590
1591                         if(top_level->shift_down()) {
1592 // Begin selection
1593                                 if(highlight_letter1 == highlight_letter2) {
1594                                         highlight_letter2 = ibeam_letter;
1595                                         highlight_letter1 = old_ibeam_letter;
1596                                 }
1597                                 else if(highlight_letter1 == old_ibeam_letter) {
1598 // Shrink selection
1599                                         highlight_letter1 = highlight_letter2;
1600                                         highlight_letter2 = ibeam_letter;
1601                                 }
1602                                 else if(highlight_letter2 == old_ibeam_letter) {
1603 // Extend selection
1604                                         highlight_letter2 = ibeam_letter;
1605                                 }
1606                         }
1607                         else
1608                                 highlight_letter1 = highlight_letter2 = ibeam_letter;
1609
1610                         find_ibeam(1);
1611                         if(keypress_draw) draw(1);
1612                         result = 1;
1613                         break; }
1614
1615                 case HOME: {
1616                         delete suggestions_popup;
1617                         suggestions_popup = 0;
1618
1619                         int old_ibeam_letter = ibeam_letter;
1620
1621                         while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1622                                 ibeam_letter--;
1623
1624                         if(top_level->shift_down())
1625                         {
1626 // Begin selection
1627                                 if(highlight_letter1 == highlight_letter2)
1628                                 {
1629                                         highlight_letter2 = old_ibeam_letter;
1630                                         highlight_letter1 = ibeam_letter;
1631                                 }
1632                                 else
1633 // Extend selection
1634                                 if(highlight_letter1 == old_ibeam_letter)
1635                                 {
1636                                         highlight_letter1 = ibeam_letter;
1637                                 }
1638                                 else
1639 // Shrink selection
1640                                 if(highlight_letter2 == old_ibeam_letter)
1641                                 {
1642                                         highlight_letter2 = highlight_letter1;
1643                                         highlight_letter1 = ibeam_letter;
1644                                 }
1645                         }
1646                         else
1647                                 highlight_letter1 = highlight_letter2 = ibeam_letter;
1648
1649                         find_ibeam(1);
1650                         if(keypress_draw) draw(1);
1651                         result = 1;
1652                         break; }
1653
1654                 case BACKSPACE: {
1655                         if(suggestions_popup) {
1656                                 delete suggestions_popup;
1657                                 suggestions_popup = 0;
1658                         }
1659
1660                         if(highlight_letter1 == highlight_letter2) {
1661                                 if(ibeam_letter > 0) {
1662                                         delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1663                                         ibeam_letter--;
1664                                 }
1665                         }
1666                         else {
1667                                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1668                                 highlight_letter2 = ibeam_letter = highlight_letter1;
1669                         }
1670
1671                         find_ibeam(1);
1672                         if(keypress_draw) draw(1);
1673                         dispatch_event = 1;
1674                         result = 1;
1675                         break; }
1676
1677                 case DELETE: {
1678 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1679                         if(highlight_letter1 == highlight_letter2) {
1680                                 if(ibeam_letter < wtext_len) {
1681                                         delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1682                                 }
1683                         }
1684                         else {
1685                                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1686                                 highlight_letter2 = ibeam_letter = highlight_letter1;
1687                         }
1688
1689                         find_ibeam(1);
1690                         if(keypress_draw) draw(1);
1691                         dispatch_event = 1;
1692                         result = 1;
1693                         break; }
1694
1695                 default: {
1696                         if( ctrl_down() ) {
1697                                 switch( get_keypress() ) {
1698                                 case 'c': case 'C': {
1699                                         if(highlight_letter1 != highlight_letter2) {
1700                                                 copy_selection(SECONDARY_SELECTION);
1701                                                 result = 1;
1702                                         }
1703                                         break; }
1704                                 case 'v': case 'V': {
1705                                         paste_selection(SECONDARY_SELECTION);
1706                                         find_ibeam(1);
1707                                         if(keypress_draw) draw(1);
1708                                         dispatch_event = 1;
1709                                         result = 1;
1710                                         break; }
1711                                 case 'x': case 'X': {
1712                                         if(highlight_letter1 != highlight_letter2) {
1713                                                 copy_selection(SECONDARY_SELECTION);
1714                                                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1715                                                 highlight_letter2 = ibeam_letter = highlight_letter1;
1716                                         }
1717
1718                                         find_ibeam(1);
1719                                         if(keypress_draw) draw(1);
1720                                         dispatch_event = 1;
1721                                         result = 1;
1722                                         break; }
1723                                 case 'u': case 'U': {
1724                                         if( shift_down() ) {
1725                                                 unicode_active = ibeam_letter;
1726                                                 wchar_t wkey = 'U';
1727                                                 insert_text(&wkey, 1);
1728                                                 find_ibeam(1);
1729                                                 highlight_letter1 = unicode_active;
1730                                                 highlight_letter2 = ibeam_letter;
1731                                                 draw(1);
1732                                                 result = 1;
1733                                         }
1734                                         break; }
1735                                 }
1736                                 break;
1737                         }
1738
1739                         default_keypress(dispatch_event, result);
1740                         break; }
1741                 }
1742         }
1743
1744         if(result) skip_cursor->update();
1745         if(dispatch_event && handle_event())
1746                 result = 1;
1747
1748         return result;
1749 }
1750
1751
1752 int BC_TextBox::uses_text()
1753 {
1754         return 1;
1755 }
1756
1757
1758 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1759 {
1760         int i, j;
1761         for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1762                 wtext[i] = wtext[j];
1763         }
1764         wtext[i] = 0;
1765         wlen = i;
1766
1767         do_separators(1);
1768 }
1769
1770 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1771 {
1772         if( len < 0 ) len = wcslen(wcp);
1773         int wtext_len = wtext_update();
1774         if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1775                 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1776                 highlight_letter2 = ibeam_letter = highlight_letter1;
1777                 wtext_len = wtext_update();
1778         }
1779
1780         int i, j;
1781         for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1782                 if( j >= wsize ) continue;
1783                 wtext[j] = wtext[i];
1784         }
1785
1786         for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1787                 if( i >= wsize ) break;
1788                 wtext[i] = wcp[j];
1789         }
1790         if( (wlen+=len) > wsize ) wlen = wsize;
1791         if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1792         wtext[wlen] = 0;  // wtext allocated wsize+1
1793         do_separators(0);
1794 }
1795
1796 int BC_TextBox::is_separator(const char *txt, int i)
1797 {
1798         if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1799         return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1800 }
1801
1802 // used for time entry
1803 void BC_TextBox::do_separators(int ibeam_left)
1804 {
1805         if(separators)
1806         {
1807 // Remove separators from text
1808                 int wtext_len = wtext_update();
1809                 for(int i = 0; i < wtext_len; ) {
1810                         if( !iswalnum(wtext[i]) ) {
1811                                 for(int j = i; j < wtext_len - 1; j++)
1812                                         wtext[j] = wtext[j + 1];
1813                                 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1814                                 wlen = --wtext_len;
1815                                 continue;
1816                         }
1817                         ++i;
1818                 }
1819                 wtext[wtext_len] = 0;
1820
1821
1822
1823
1824
1825 // Insert separators into text
1826                 int separator_len = strlen(separators);
1827                 for(int i = 0; i < separator_len; i++) {
1828                         if(i < wtext_len) {
1829 // Insert a separator
1830                                 if( is_separator(separators,i) ) {
1831                                         for(int j = wtext_len; j >= i; j--) {
1832                                                 wtext[j + 1] = wtext[j];
1833                                         }
1834                                         if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1835                                         ++wtext_len;
1836                                         wtext[i] = separators[i];
1837                                 }
1838                         }
1839                         else {
1840                                 wtext[i] = separators[i];
1841                         }
1842                 }
1843
1844 // Truncate text
1845                 wtext[separator_len] = 0;
1846                 wlen = separator_len;
1847         }
1848 }
1849
1850 void BC_TextBox::get_ibeam_position(int &x, int &y)
1851 {
1852         int i, row_begin, row_end;
1853         int wtext_len = wtext_update();
1854
1855         y = 0;
1856         x = 0;
1857         for( i=0; i<wtext_len; ) {
1858                 row_begin = i;
1859                 for(; i<wtext_len && wtext[i]!='\n'; i++);
1860                 row_end = i;
1861
1862                 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1863                         x = get_text_width(font,  &wtext[row_begin], ibeam_letter - row_begin);
1864 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1865                         return;
1866                 }
1867
1868                 if( i < wtext_len && wtext[i] == '\n' ) {
1869                         i++;
1870                         y += text_height;
1871                 }
1872         }
1873 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1874
1875         x = 0;
1876         return;
1877 }
1878
1879 void BC_TextBox::set_text_row(int row)
1880 {
1881         text_x = left_margin;
1882         text_y = -(row * text_height) + top_margin;
1883         draw(1);
1884 }
1885
1886 int BC_TextBox::get_text_row()
1887 {
1888         return -(text_y - top_margin) / text_height;
1889 }
1890
1891 void BC_TextBox::find_ibeam(int dispatch_event)
1892 {
1893         int x, y;
1894         int old_x = text_x, old_y = text_y;
1895
1896         get_ibeam_position(x, y);
1897
1898         if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1899         {
1900                 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1901                 if(text_x > left_margin) text_x = left_margin;
1902         }
1903         else
1904         if(left_margin + text_x + x < left_margin)
1905         {
1906                 text_x = -(x - (get_w() / 4)) + left_margin;
1907                 if(text_x > left_margin) text_x = left_margin;
1908         }
1909
1910         int text_row = y / text_height;
1911         if( text_row < rows ) text_y = top_margin;
1912
1913         int pix_rows = get_h() - bottom_margin - (y + text_y);
1914         if( pix_rows < text_height )
1915                 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1916
1917         pix_rows = y + text_y - top_margin;
1918         if( pix_rows < 0 ) {
1919                 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1920                 if( text_y > top_margin ) text_y = top_margin;
1921         }
1922
1923         if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1924 }
1925
1926 // New algorithm
1927 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1928 {
1929         int i, j, k, row_begin, row_end, result = 0, done = 0;
1930         int column1, column2;
1931         int got_visible_row = 0;
1932
1933 // Select complete row if cursor above the window
1934 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1935         if(cursor_y < text_y - text_height)
1936         {
1937                 result = 0;
1938                 done = 1;
1939         }
1940
1941         int wtext_len = wtext_update();
1942
1943         for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1944 // Simulate drawing of 1 row
1945                 row_begin = i;
1946                 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1947                 row_end = i;
1948
1949 // check visibility
1950                 int first_visible_row = 0;
1951                 int last_visible_row = 0;
1952                 if( k+text_height > top_margin && !got_visible_row) {
1953                         first_visible_row = 1;
1954                         got_visible_row = 1;
1955                 }
1956
1957                 if( (k+text_height >= get_h() - bottom_margin ||
1958                         (row_end >= wtext_len && k < get_h() - bottom_margin &&
1959                                 k + text_height > 0)) )
1960                         last_visible_row = 1;
1961
1962 // Cursor is inside vertical range of row
1963                 if((cursor_y >= top_margin &&
1964                         cursor_y < get_h() - bottom_margin &&
1965                         cursor_y >= k && cursor_y < k + text_height) ||
1966 // Cursor is above 1st row
1967                         (cursor_y < k + text_height && first_visible_row) ||
1968 // Cursor is below last row
1969                         (cursor_y >= k && last_visible_row))
1970                 {
1971                         column1 = column2 = 0;
1972                         for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1973                                 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1974                                 if((column2 + column1) / 2 >= cursor_x) {
1975                                         result = j - 1;
1976                                         done = 1;
1977 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1978 // __LINE__, result, first_visible_row, last_visible_row);
1979                                 }
1980                                 column1 = column2;
1981                         }
1982
1983                         if(!done) {
1984                                 result = row_end;
1985                                 done = 1;
1986                         }
1987                 }
1988
1989                 if(wtext[i] == '\n') i++;
1990
1991 // Select complete row if last visible & cursor is below window
1992                 if(last_visible_row && cursor_y > k + text_height * 2)
1993                         result = row_end;
1994
1995                 if(i >= wtext_len && !done) {
1996                         result = wtext_len;
1997                 }
1998         }
1999
2000
2001 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
2002 //  __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
2003         if(result < 0) result = 0;
2004         if(result > wtext_len) {
2005 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
2006                 result = wtext_len;
2007         }
2008
2009         return result;
2010 }
2011
2012 // Old algorithm
2013 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
2014 {
2015         int i, j, k, row_begin, row_end, result = 0, done = 0;
2016         int column1, column2;
2017         int wtext_len = wtext_update();
2018
2019         if(cursor_y < text_y) {
2020                 result = 0;
2021                 done = 1;
2022         }
2023
2024         for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
2025                 row_begin = i;
2026                 for(; wtext[i] != '\n' && i < wtext_len; i++);
2027                 row_end = i;
2028
2029                 if(cursor_y >= k && cursor_y < k + text_height) {
2030                         column1 = column2 = 0;
2031                         for(j = 0; j <= row_end - row_begin && !done; j++) {
2032                                 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
2033                                 if((column2 + column1) / 2 >= cursor_x) {
2034                                         result = row_begin + j - 1;
2035                                         done = 1;
2036                                 }
2037                                 column1 = column2;
2038                         }
2039                         if(!done)
2040                         {
2041                                 result = row_end;
2042                                 done = 1;
2043                         }
2044                 }
2045                 if(wtext[i] == '\n') i++;
2046
2047                 if(i >= wtext_len && !done) {
2048                         result = wtext_len;
2049                 }
2050         }
2051         if(result < 0) result = 0;
2052         if(result > wtext_len) result = wtext_len;
2053         return result;
2054 }
2055
2056
2057 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2058 {
2059         int wtext_len = wtext_update();
2060         letter1 = letter2 = ibeam_letter;
2061         if( letter1 < 0 ) letter1 = 0;
2062         if( letter2 < 0 ) letter2 = 0;
2063         if( letter1 > wtext_len ) letter1 = wtext_len;
2064         if( letter2 > wtext_len ) letter2 = wtext_len;
2065         if( !wtext_len ) return;
2066         for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
2067         for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
2068         if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
2069 }
2070
2071
2072 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2073 {
2074         int wtext_len = wtext_update();
2075         letter1 = letter2 = ibeam_letter;
2076         if( letter1 < 0 ) letter1 = 0;
2077         if( letter2 < 0 ) letter2 = 0;
2078         if( letter1 > wtext_len ) letter1 = wtext_len;
2079         if( letter2 > wtext_len ) letter2 = wtext_len;
2080         if( !wtext_len ) return;
2081         for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2082         for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2083 }
2084
2085 void BC_TextBox::copy_selection(int clipboard_num)
2086 {
2087         int wtext_len = wtext_update();
2088         if(!wtext_len) return;
2089
2090         if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2091                 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2092                 highlight_letter2 - highlight_letter1 <= 0) return;
2093         int clip_len = highlight_letter2 - highlight_letter1;
2094 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2095         char ctext[4*clip_len+4];
2096         clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2097         get_clipboard()->to_clipboard(ctext, clip_len, clipboard_num);
2098 }
2099
2100
2101 void BC_TextBox::paste_selection(int clipboard_num)
2102 {
2103         int len = get_clipboard()->clipboard_len(clipboard_num);
2104         if( len > 0 )
2105         {
2106                 char cstring[len];  wchar_t wstring[len];
2107                 get_clipboard()->from_clipboard(cstring, len, clipboard_num);  --len;
2108 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2109                 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2110                         cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2111                 insert_text(wstring, len);
2112         }
2113 }
2114
2115 void BC_TextBox::set_keypress_draw(int value)
2116 {
2117         keypress_draw = value;
2118 }
2119
2120 int BC_TextBox::get_last_keypress()
2121 {
2122         return last_keypress;
2123 }
2124
2125 int BC_TextBox::get_ibeam_letter()
2126 {
2127         return ibeam_letter;
2128 }
2129
2130 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2131 {
2132         this->ibeam_letter = number;
2133         if(redraw)
2134         {
2135                 draw(1);
2136         }
2137 }
2138
2139 void BC_TextBox::set_separators(const char *separators)
2140 {
2141         this->separators = (char*)separators;
2142 }
2143
2144 int BC_TextBox::get_rows()
2145 {
2146         return rows;
2147 }
2148
2149
2150
2151
2152
2153
2154
2155 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box,
2156         int x,
2157         int y)
2158  : BC_ListBox(x,
2159         y,
2160         text_box->get_w(),
2161         200,
2162         LISTBOX_TEXT,
2163         text_box->suggestions,
2164         0,
2165         0,
2166         1,
2167         0,
2168         1)
2169 {
2170         this->text_box = text_box;
2171         set_use_button(0);
2172         set_justify(LISTBOX_LEFT);
2173 }
2174
2175 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2176 {
2177 }
2178
2179 int BC_TextBoxSuggestions::selection_changed()
2180 {
2181 #if 0
2182 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2183         BC_ListBoxItem *item = get_selection(0, 0);
2184 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2185
2186         if(item)
2187         {
2188                 char *current_suggestion = item->get_text();
2189 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2190 //              int text_box_len = strlen(text_box->text);
2191 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2192                 strcpy(text_box->text + text_box->suggestion_column, current_suggestion);
2193 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2194                 *(text_box->text + text_box->suggestion_column + strlen(current_suggestion)) = 0;
2195
2196 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2197                 text_box->draw(1);
2198                 text_box->handle_event();
2199         }
2200
2201         return 1;
2202 #else
2203         return 0;
2204 #endif
2205 }
2206
2207
2208 int BC_TextBoxSuggestions::handle_event()
2209 {
2210         char *current_suggestion = 0;
2211         BC_ListBoxItem *item = get_selection(0, 0);
2212 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2213         if(item && (current_suggestion=item->get_text()) != 0)
2214         {
2215                 int col = text_box->suggestion_column;
2216                 int len = BCTEXTLEN-1 - col;
2217                 char *cp = &text_box->text[col];
2218 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2219                 strncpy(cp, current_suggestion, len);
2220 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2221                 if( (col=strlen(current_suggestion)) >= len )
2222                         cp[len-1] = 0;
2223                 text_box->dirty = 1;
2224         }
2225
2226
2227 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2228         text_box->highlight_letter1 =
2229                 text_box->highlight_letter2 =
2230                 text_box->ibeam_letter = text_box->tstrlen();
2231         text_box->wtext_update();
2232         text_box->draw(1);
2233         text_box->handle_event();
2234 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2235         return 1;
2236 }
2237
2238
2239 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2240         int x, int y, int w, int rows,
2241         const char *default_text, int default_size)
2242 {
2243         this->parent_window = parent_window;
2244         this->x = x;
2245         this->y = y;
2246         this->w = w;
2247         this->rows = rows;
2248         this->default_text = default_text;
2249         this->default_wtext = 0;
2250         this->default_size = default_size;
2251 }
2252
2253 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2254         int x, int y, int w, int rows,
2255         const wchar_t *default_wtext, int default_size)
2256 {
2257         this->parent_window = parent_window;
2258         this->x = x;
2259         this->y = y;
2260         this->w = w;
2261         this->rows = rows;
2262         this->default_text = 0;
2263         this->default_wtext = default_wtext;
2264         this->default_size = default_size;
2265 }
2266
2267 BC_ScrollTextBox::~BC_ScrollTextBox()
2268 {
2269         delete yscroll;
2270         if(text) {
2271                 text->gui = 0;
2272                 delete text;
2273         }
2274 }
2275
2276 void BC_ScrollTextBox::create_objects()
2277 {
2278 // Must be created first
2279         parent_window->add_subwindow(text = default_wtext ?
2280                 new BC_ScrollTextBoxText(this, default_wtext) :
2281                 new BC_ScrollTextBoxText(this, default_text));
2282         parent_window->add_subwindow(yscroll = new BC_ScrollTextBoxYScroll(this));
2283         text->yscroll = yscroll;
2284         yscroll->bound_to = text;
2285
2286 }
2287
2288 int BC_ScrollTextBox::handle_event()
2289 {
2290         return 1;
2291 }
2292
2293 int BC_ScrollTextBox::get_x()
2294 {
2295         return x;
2296 }
2297
2298 int BC_ScrollTextBox::get_y()
2299 {
2300         return y;
2301 }
2302
2303 int BC_ScrollTextBox::get_w()
2304 {
2305         return w;
2306 }
2307
2308 int BC_ScrollTextBox::get_h()
2309 {
2310         return this->text->get_h();
2311 }
2312
2313 int BC_ScrollTextBox::get_rows()
2314 {
2315         return rows;
2316 }
2317
2318
2319 const char* BC_ScrollTextBox::get_text()
2320 {
2321         return text->get_text();
2322 }
2323
2324 const wchar_t* BC_ScrollTextBox::get_wtext()
2325 {
2326         return text->get_wtext();
2327 }
2328
2329 void BC_ScrollTextBox::set_text(char *text, int isz)
2330 {
2331         this->text->set_text(text, isz);
2332         yscroll->update_length(this->text->get_text_rows(),
2333                 this->text->get_text_row(),
2334                 yscroll->get_handlelength(),
2335                 1);
2336 }
2337
2338 int BC_ScrollTextBox::set_text_row(int n)
2339 {
2340         text->set_text_row(n);
2341         yscroll->update_value(n);
2342         return 1;
2343 }
2344
2345 void BC_ScrollTextBox::update(const char *text)
2346 {
2347         this->text->update(text);
2348         yscroll->update_length(this->text->get_text_rows(),
2349                 this->text->get_text_row(),
2350                 yscroll->get_handlelength(),
2351                 1);
2352 }
2353
2354 void BC_ScrollTextBox::update(const wchar_t *wtext)
2355 {
2356         this->text->update(wtext);
2357         yscroll->update_length(this->text->get_text_rows(),
2358                 this->text->get_text_row(),
2359                 yscroll->get_handlelength(),
2360                 1);
2361 }
2362
2363 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2364 {
2365         this->x = x;
2366         this->y = y;
2367         this->w = w;
2368         this->rows = rows;
2369
2370         text->reposition_window(x,
2371                 y,
2372                 w - yscroll->get_span(),
2373                 rows);
2374         yscroll->reposition_window(x + w - yscroll->get_span(),
2375                 y,
2376                 BC_TextBox::calculate_row_h(rows,
2377                         parent_window));
2378         yscroll->update_length(text->get_text_rows(),
2379                 text->get_text_row(),
2380                 rows,
2381                 0);
2382 }
2383
2384 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2385 {
2386         this->text->set_selection(char1, char2, ibeam);
2387 }
2388
2389 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2390 {
2391         this->text->wset_selection(char1, char2, ibeam);
2392 }
2393
2394 int BC_ScrollTextBox::get_ibeam_letter()
2395 {
2396         return this->text->get_ibeam_letter();
2397 }
2398
2399
2400
2401
2402
2403
2404
2405 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2406  : BC_TextBox(gui->x, gui->y,
2407         gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2408         gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2409 {
2410         this->gui = gui;
2411 }
2412
2413 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2414  : BC_TextBox(gui->x, gui->y,
2415         gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2416         gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2417 {
2418         this->gui = gui;
2419 }
2420
2421 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2422 {
2423         if(gui)
2424         {
2425                 gui->text = 0;
2426                 delete gui;
2427         }
2428 }
2429
2430 int BC_ScrollTextBoxText::handle_event()
2431 {
2432         gui->yscroll->update_length(get_text_rows(),
2433                 get_text_row(),
2434                 gui->yscroll->get_handlelength(),
2435                 1);
2436         return gui->handle_event();
2437 }
2438
2439 int BC_ScrollTextBoxText::motion_event()
2440 {
2441         gui->yscroll->update_length(get_text_rows(),
2442                 get_text_row(),
2443                 gui->yscroll->get_handlelength(),
2444                 1);
2445         return 1;
2446 }
2447
2448
2449 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2450  : BC_ScrollBar(gui->x +
2451                         gui->w -
2452                         get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2453                 gui->y,
2454                 SCROLL_VERT,
2455                 BC_TextBox::calculate_row_h(gui->rows,
2456                         gui->parent_window),
2457                 gui->text->get_text_rows(),
2458                 0,
2459                 gui->rows)
2460 {
2461         this->gui = gui;
2462 }
2463
2464 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2465 {
2466 }
2467
2468 int BC_ScrollTextBoxYScroll::handle_event()
2469 {
2470         gui->text->set_text_row(get_position());
2471         return 1;
2472 }
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2484  : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2485 {
2486         this->popup = popup;
2487 }
2488
2489 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2490  : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2491 {
2492         this->popup = popup;
2493 }
2494
2495 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2496 {
2497         if(popup) {
2498                 popup->textbox = 0;
2499                 delete popup;
2500                 popup = 0;
2501         }
2502 }
2503
2504
2505 int BC_PopupTextBoxText::handle_event()
2506 {
2507         popup->handle_event();
2508         return 1;
2509 }
2510
2511 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2512  : BC_ListBox(x, y,
2513         popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2514         popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2515 {
2516         this->popup = popup;
2517 }
2518 int BC_PopupTextBoxList::handle_event()
2519 {
2520         BC_ListBoxItem *item = get_selection(0, 0);
2521         if(item)
2522         {
2523                 popup->textbox->update(item->get_text());
2524                 popup->handle_event();
2525         }
2526         return 1;
2527 }
2528
2529
2530
2531
2532 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2533                 ArrayList<BC_ListBoxItem*> *list_items,
2534                 const char *default_text, int x, int y,
2535                 int text_w, int list_h, int list_format)
2536 {
2537         this->x = x;
2538         this->y = y;
2539         this->list_h = list_h;
2540         this->list_format = list_format;
2541         this->default_text = (char*)default_text;
2542         this->default_wtext = 0;
2543         this->text_w = text_w;
2544         this->parent_window = parent_window;
2545         this->list_items = list_items;
2546 }
2547
2548 BC_PopupTextBox::~BC_PopupTextBox()
2549 {
2550         delete listbox;
2551         if(textbox)
2552         {
2553                 textbox->popup = 0;
2554                 delete textbox;
2555         }
2556 }
2557
2558 int BC_PopupTextBox::create_objects()
2559 {
2560         int x = this->x, y = this->y;
2561         parent_window->add_subwindow(textbox = default_wtext ?
2562                  new BC_PopupTextBoxText(this, x, y, default_wtext) :
2563                  new BC_PopupTextBoxText(this, x, y, default_text));
2564         x += textbox->get_w();
2565         parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2566         return 0;
2567 }
2568
2569 void BC_PopupTextBox::update(const char *text)
2570 {
2571         textbox->update(text);
2572 }
2573
2574 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2575 {
2576         listbox->update(data,
2577                 0,
2578                 0,
2579                 1);
2580 }
2581
2582
2583 const char* BC_PopupTextBox::get_text()
2584 {
2585         return textbox->get_text();
2586 }
2587
2588 const wchar_t* BC_PopupTextBox::get_wtext()
2589 {
2590         return textbox->get_wtext();
2591 }
2592
2593 int BC_PopupTextBox::get_number()
2594 {
2595         return listbox->get_selection_number(0, 0);
2596 }
2597
2598 int BC_PopupTextBox::get_x()
2599 {
2600         return x;
2601 }
2602
2603 int BC_PopupTextBox::get_y()
2604 {
2605         return y;
2606 }
2607
2608 int BC_PopupTextBox::get_w()
2609 {
2610         return textbox->get_w() + listbox->get_w();
2611 }
2612
2613 int BC_PopupTextBox::get_h()
2614 {
2615         return textbox->get_h();
2616 }
2617
2618 int BC_PopupTextBox::handle_event()
2619 {
2620         return 1;
2621 }
2622
2623 void BC_PopupTextBox::reposition_window(int x, int y)
2624 {
2625         this->x = x;
2626         this->y = y;
2627         int x1 = x, y1 = y;
2628         textbox->reposition_window(x1,
2629                 y1,
2630                 textbox->get_w(),
2631                 textbox->get_rows());
2632         x1 += textbox->get_w();
2633         listbox->reposition_window(x1, y1, -1, -1, 0);
2634 //      if(flush) parent_window->flush();
2635 }
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2651         int64_t default_value,
2652         int64_t min,
2653         int64_t max,
2654         int x,
2655         int y)
2656  : BC_TextBox(x, y, popup->text_w, 1, default_value)
2657 {
2658         this->popup = popup;
2659 }
2660
2661 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2662         float default_value,
2663         float min,
2664         float max,
2665         int x,
2666         int y)
2667  : BC_TextBox(x, y, popup->text_w, 1, default_value)
2668 {
2669         this->popup = popup;
2670 }
2671
2672 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2673 {
2674         if(popup)
2675         {
2676                 popup->textbox = 0;
2677                 delete popup;
2678                 popup = 0;
2679         }
2680 }
2681
2682
2683
2684 int BC_TumbleTextBoxText::handle_event()
2685 {
2686         popup->handle_event();
2687         return 1;
2688 }
2689
2690 int BC_TumbleTextBoxText::button_press_event()
2691 {
2692         if(is_event_win())
2693         {
2694                 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
2695
2696                 if(get_buttonpress() == 4)
2697                 {
2698                         popup->tumbler->handle_up_event();
2699                 }
2700                 else
2701                 if(get_buttonpress() == 5)
2702                 {
2703                         popup->tumbler->handle_down_event();
2704                 }
2705                 return 1;
2706         }
2707         return 0;
2708 }
2709
2710
2711
2712
2713 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2714                 int64_t default_value,
2715                 int64_t min,
2716                 int64_t max,
2717                 int x,
2718                 int y,
2719                 int text_w)
2720 {
2721         reset();
2722         this->x = x;
2723         this->y = y;
2724         this->min = min;
2725         this->max = max;
2726         this->default_value = default_value;
2727         this->text_w = text_w;
2728         this->parent_window = parent_window;
2729         use_float = 0;
2730         precision = 4;
2731         increment = 1;
2732 }
2733
2734 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2735                 int default_value,
2736                 int min,
2737                 int max,
2738                 int x,
2739                 int y,
2740                 int text_w)
2741 {
2742         reset();
2743         this->x = x;
2744         this->y = y;
2745         this->min = min;
2746         this->max = max;
2747         this->default_value = default_value;
2748         this->text_w = text_w;
2749         this->parent_window = parent_window;
2750         use_float = 0;
2751         precision = 4;
2752         increment = 1;
2753 }
2754
2755 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2756                 float default_value_f,
2757                 float min_f,
2758                 float max_f,
2759                 int x,
2760                 int y,
2761                 int text_w)
2762 {
2763         reset();
2764         this->x = x;
2765         this->y = y;
2766         this->min_f = min_f;
2767         this->max_f = max_f;
2768         this->default_value_f = default_value_f;
2769         this->text_w = text_w;
2770         this->parent_window = parent_window;
2771         use_float = 1;
2772         precision = 4;
2773         increment = 1;
2774 }
2775
2776 BC_TumbleTextBox::~BC_TumbleTextBox()
2777 {
2778 // Recursive delete.  Normally ~BC_TumbleTextBox is never called but textbox
2779 // is deleted anyway by the windowbase so textbox deletes this.
2780         if(tumbler) delete tumbler;
2781         tumbler = 0;
2782 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2783         if(textbox)
2784         {
2785                 textbox->popup = 0;
2786                 delete textbox;
2787         }
2788         textbox = 0;
2789 }
2790
2791 void BC_TumbleTextBox::reset()
2792 {
2793         textbox = 0;
2794         tumbler = 0;
2795         increment = 1.0;
2796 }
2797
2798 void BC_TumbleTextBox::set_precision(int precision)
2799 {
2800         this->precision = precision;
2801 }
2802
2803 void BC_TumbleTextBox::set_increment(float value)
2804 {
2805         this->increment = value;
2806         if(tumbler) tumbler->set_increment(value);
2807 }
2808
2809 void BC_TumbleTextBox::set_log_floatincrement(int value)
2810 {
2811         this->log_floatincrement = value;
2812         if(tumbler) tumbler->set_log_floatincrement(value);
2813 }
2814
2815 int BC_TumbleTextBox::create_objects()
2816 {
2817         int x = this->x, y = this->y;
2818
2819         if(use_float)
2820         {
2821                 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2822                         default_value_f,
2823                         min_f,
2824                         max_f,
2825                         x,
2826                         y));
2827                 textbox->set_precision(precision);
2828         }
2829         else
2830                 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2831                         default_value,
2832                         min,
2833                         max,
2834                         x,
2835                         y));
2836
2837         x += textbox->get_w();
2838
2839         if(use_float)
2840                 parent_window->add_subwindow(tumbler = new BC_FTumbler(textbox,
2841                         min_f,
2842                         max_f,
2843                         x,
2844                         y));
2845         else
2846                 parent_window->add_subwindow(tumbler = new BC_ITumbler(textbox,
2847                         min,
2848                         max,
2849                         x,
2850                         y));
2851
2852         tumbler->set_increment(increment);
2853         return 0;
2854 }
2855
2856 const char* BC_TumbleTextBox::get_text()
2857 {
2858         return textbox->get_text();
2859 }
2860
2861 const wchar_t* BC_TumbleTextBox::get_wtext()
2862 {
2863         return textbox->get_wtext();
2864 }
2865
2866 BC_TextBox* BC_TumbleTextBox::get_textbox()
2867 {
2868         return textbox;
2869 }
2870
2871 int BC_TumbleTextBox::update(const char *value)
2872 {
2873         textbox->update(value);
2874         return 0;
2875 }
2876
2877 int BC_TumbleTextBox::update(int64_t value)
2878 {
2879         textbox->update(value);
2880         return 0;
2881 }
2882
2883 int BC_TumbleTextBox::update(float value)
2884 {
2885         textbox->update(value);
2886         return 0;
2887 }
2888
2889
2890 int BC_TumbleTextBox::get_x()
2891 {
2892         return x;
2893 }
2894
2895 int BC_TumbleTextBox::get_y()
2896 {
2897         return y;
2898 }
2899
2900 int BC_TumbleTextBox::get_w()
2901 {
2902         return textbox->get_w() + tumbler->get_w();
2903 }
2904
2905 int BC_TumbleTextBox::get_h()
2906 {
2907         return textbox->get_h();
2908 }
2909
2910 void BC_TumbleTextBox::disable()
2911 {
2912         if( !get_enabled() ) return;
2913         tumbler->hide_window(0);
2914         return textbox->disable();
2915 }
2916
2917 void BC_TumbleTextBox::enable()
2918 {
2919         if( get_enabled() ) return;
2920         tumbler->show_window(0);
2921         return textbox->enable();
2922 }
2923
2924 int BC_TumbleTextBox::get_enabled()
2925 {
2926         return textbox->get_enabled();
2927 }
2928
2929 int BC_TumbleTextBox::handle_event()
2930 {
2931         return 1;
2932 }
2933
2934 void BC_TumbleTextBox::reposition_window(int x, int y)
2935 {
2936         this->x = x;
2937         this->y = y;
2938
2939         textbox->reposition_window(x,
2940                 y,
2941                 text_w,
2942                 1);
2943         tumbler->reposition_window(x + textbox->get_w(),
2944                 y);
2945 //      if(flush) parent_window->flush();
2946 }
2947
2948
2949 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2950 {
2951         tumbler->set_boundaries(min, max);
2952 }
2953
2954 void BC_TumbleTextBox::set_boundaries(float min, float max)
2955 {
2956         tumbler->set_boundaries(min, max);
2957 }