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