3 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
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.
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.
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
21 #include "bcclipboard.h"
22 #include "bclistboxitem.h"
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "bctextbox.h"
30 #include "filesystem.h"
41 #define VERTICAL_MARGIN 2
42 #define VERTICAL_MARGIN_NOBORDER 0
43 #define HORIZONTAL_MARGIN 4
44 #define HORIZONTAL_MARGIN_NOBORDER 2
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)
52 reset_parameters(rows, has_border, font, size);
55 else // text referenced directly
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)
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;
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)
77 this->is_utf8 = is_utf8;
79 reset_parameters(rows, has_border, font, BCTEXTLEN);
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)
87 this->is_utf8 = is_utf8;
89 reset_parameters(rows, has_border, font, BCTEXTLEN);
91 wtext = new wchar_t[wsize+1];
92 wcsncpy(this->wtext, wtext, wsize);
93 this->wtext[wsize] = 0;
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)
102 reset_parameters(rows, has_border, font, BCSTRLEN);
103 snprintf(this->text, this->tsize, "%jd", text);
104 dirty = 1; wtext_update();
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)
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();
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)
125 reset_parameters(rows, has_border, font, BCSTRLEN);
126 snprintf(this->text, this->tsize, "%d", text);
127 dirty = 1; wtext_update();
130 BC_TextBox::~BC_TextBox()
132 if(skip_cursor) delete skip_cursor;
133 delete suggestions_popup;
134 suggestions->remove_all_objects();
142 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
144 suggestions = new ArrayList<BC_ListBoxItem*>;
145 suggestions_popup = 0;
146 suggestion_column = 0;
149 this->has_border = has_border;
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;
157 highlight_letter1 = highlight_letter2 = 0;
158 highlight_letter3 = highlight_letter4 = 0;
170 skip_cursor = new Timer;
184 int BC_TextBox::tstrlen()
186 if( !tsize ) return strlen(text);
187 return strnlen(text, tsize);
190 int BC_TextBox::tstrcmp(const char *cp)
192 if( !tsize ) return strcmp(text, cp);
193 return strncmp(text, cp, tsize);
196 char *BC_TextBox::tstrcpy(const char *cp)
201 return strcpy(text, cp);
202 strncpy(text, cp, tsize);
210 char *BC_TextBox::tstrcat(const char *cp)
213 if( !tsize ) return strcat(text, cp);
214 char *result = strncat(text, cp, tsize);
219 int BC_TextBox::wtext_update()
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];
231 memcpy(npositions, positions, (plen+1)*sizeof(positions[0]));
234 delete [] positions; positions = npositions; plen = nsize;
236 wlen = BC_Resources::encode(src_enc, dst_enc, text, strlen(text),
237 (char*)wtext, wsize*sizeof(wchar_t)) / sizeof(wchar_t);
244 int BC_TextBox::text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz)
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);
255 int BC_TextBox::initialize()
258 skip_cursor = new Timer;
259 skip_cursor->update();
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();
267 left_margin = right_margin = HORIZONTAL_MARGIN;
268 top_margin = bottom_margin = VERTICAL_MARGIN;
272 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
273 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
276 text_x = left_margin;
280 // Create the subwindow
281 BC_SubWindow::initialize();
283 BC_Resources *resources = get_resources();
286 back_color = resources->text_background;
287 high_color = resources->text_background_hi;
291 high_color = resources->text_background_noborder_hi;
292 back_color = bg_color;
296 set_cursor(IBEAM_CURSOR, 0, 0);
301 int BC_TextBox::calculate_h(BC_WindowBase *gui,
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);
311 void BC_TextBox::set_precision(int precision)
313 this->precision = precision;
316 // Compute suggestions for a path
317 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries, const char *filter)
319 // Let user delete suggestion
320 if(get_last_keypress() != BACKSPACE) {
321 // Compute suggestions
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));
330 getcwd(dirname, sizeof(dirname));
331 // If directory, tabulate it
333 if( filter ) fs.set_filter(filter);
335 strncpy(dirname, current_text, sizeof(dirname));
336 if( (cp=strrchr(dirname, '/')) ||
337 (cp=strrchr(dirname, '~')) ) *++cp = 0;
338 fs.parse_tildas(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);
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);
361 set_suggestions(&suggestions, suggestion_column);
367 void BC_TextBox::no_suggestions()
369 if( suggestions_popup ) {
370 delete suggestions_popup;
371 suggestions_popup = 0;
376 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
379 this->suggestions->remove_all_objects();
380 this->suggestion_column = column;
383 for(int i = 0; i < suggestions->size(); i++) {
384 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
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);
395 suggestions_popup->update(this->suggestions, 0, 0, 1);
397 suggestions_popup->activate(0);
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;
409 draw(0); // update positions
410 highlight_letter2 = wtext_update();
411 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
419 if( !suggestions || !this->suggestions->size() )
423 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
425 highlight_letter1 = char1;
426 highlight_letter2 = char2;
427 ibeam_letter = ibeam;
431 // count utf8 chars in text which occur before cp
432 int BC_TextBox::wcpos(const char *text, const char *cp)
435 const unsigned char *bp = (const unsigned char *)text;
436 const unsigned char *ep = (const unsigned char *)cp;
437 while( bp < ep && *bp != 0 ) {
440 if( ch < 0x80 ) continue;
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 );
448 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
450 const char *cp = get_text();
451 wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
454 int BC_TextBox::update(const char *text)
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;
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;
468 int BC_TextBox::update(const wchar_t *wtext)
470 int wtext_len = wcslen(wtext);
471 if( wtext_len >= wsize ) wtext_len = wsize;
472 wcsncpy(this->wtext, wtext, 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;
481 int BC_TextBox::update(int64_t value)
483 char string[BCTEXTLEN];
484 sprintf(string, "%jd", value);
489 int BC_TextBox::update(float value)
491 char string[BCTEXTLEN];
492 sprintf(string, "%0.*f", precision, value);
497 void BC_TextBox::disable()
506 void BC_TextBox::enable()
516 int BC_TextBox::get_enabled()
521 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
523 return (pixels - 4) /
524 (window->get_text_ascent(font) + 1 +
525 window->get_text_descent(font) + 1);
528 int BC_TextBox::calculate_row_h(int rows,
529 BC_WindowBase *parent_window,
534 (parent_window->get_text_ascent(font) + 1 +
535 parent_window->get_text_descent(font) + 1) +
536 (has_border ? 4 : 0);
539 const char* BC_TextBox::get_text()
541 int wtext_len = wtext_update();
542 text_update(wtext,wtext_len, text,tsize);
546 const wchar_t* BC_TextBox::get_wtext()
552 void BC_TextBox::set_text(char *text, int isz)
554 if( size > 0 || isz < 0 ) return;
563 int BC_TextBox::get_text_rows()
565 int wtext_len = wtext_update();
567 for(int i = 0; i < wtext_len; i++) {
568 if(wtext[i] == '\n') result++;
574 int BC_TextBox::get_row_h(int rows)
576 return rows * text_height + top_margin + bottom_margin;
579 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
582 if(w < 0) w = get_w();
585 new_h = get_row_h(rows);
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);
602 void BC_TextBox::draw_border()
604 BC_Resources *resources = get_resources();
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());
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);
619 draw_3d_border(0, 0, w, h,
620 resources->text_border1,
621 resources->text_border2,
622 resources->text_border3,
623 resources->text_border4);
627 void BC_TextBox::draw_cursor()
629 // set_color(background_color);
637 draw_box(ibeam_x + text_x,
646 void BC_TextBox::draw(int flush)
649 int row_begin, row_end;
650 int highlight_x1, highlight_x2;
652 BC_Resources *resources = get_resources();
654 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
657 background_color = resources->text_background;
659 background_color = high_color;
661 background_color = back_color;
662 set_color(background_color);
663 draw_box(0, 0, w, h);
665 int wtext_len = wtext_update();
667 // Draw text with selection
670 for(i=0, j=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
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;
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 )
688 if(highlight_letter1 >= row_begin &&
689 highlight_letter1 <= row_end)
690 highlight_x1 = positions[highlight_letter1];
694 if(highlight_letter2 > row_begin &&
695 highlight_letter2 <= row_end)
696 highlight_x2 = positions[highlight_letter2];
698 highlight_x2 = get_w();
700 draw_box(highlight_x1 + text_x, k,
701 highlight_x2 - highlight_x1, text_height);
704 // Draw text over highlight
705 int len = row_end - row_begin;
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]);
712 positions[wtext_row - wtext] = 0;
714 // Get ibeam location
715 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
717 ibeam_y = k - text_y;
718 ibeam_x = positions[ibeam_letter];
723 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
725 // ibeam_x = ibeam_y = !wtext_len ? 0 : -1;
726 ibeam_x = 0; ibeam_y = k - text_y;
729 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
739 int BC_TextBox::focus_in_event()
745 int BC_TextBox::focus_out_event()
751 int BC_TextBox::cursor_enter_event()
753 if(top_level->event_win == win && enabled)
757 top_level->deactivate();
770 int BC_TextBox::cursor_leave_event()
779 if( !suggestions_popup &&
780 top_level->get_resources()->textbox_focus_policy == LEAVE_DEACTIVATE )
785 int BC_TextBox::button_press_event()
789 if(!enabled) return 0;
790 if(get_buttonpress() != WHEEL_UP &&
791 get_buttonpress() != WHEEL_DOWN &&
792 get_buttonpress() != LEFT_BUTTON &&
793 get_buttonpress() != MIDDLE_BUTTON) return 0;
797 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
799 int cursor_letter = 0;
800 int wtext_len = wtext_update();
801 int update_scroll = 0;
804 if(top_level->event_win == win)
809 top_level->deactivate();
814 if(get_buttonpress() == WHEEL_UP)
816 text_y += text_height;
817 text_y = MIN(text_y, top_margin);
821 if(get_buttonpress() == WHEEL_DOWN)
823 int min_y = -(get_text_rows() *
827 text_y -= text_height;
828 text_y = MAX(text_y, min_y);
829 text_y = MIN(text_y, top_margin);
835 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
837 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
840 if(get_triple_click())
842 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
844 select_line(highlight_letter1, highlight_letter2, cursor_letter);
845 highlight_letter3 = highlight_letter1;
846 highlight_letter4 = highlight_letter2;
847 ibeam_letter = highlight_letter2;
848 copy_selection(PRIMARY_SELECTION);
851 if(get_double_click())
854 select_word(highlight_letter1, highlight_letter2, cursor_letter);
855 highlight_letter3 = highlight_letter1;
856 highlight_letter4 = highlight_letter2;
857 ibeam_letter = highlight_letter2;
858 copy_selection(PRIMARY_SELECTION);
861 if(get_buttonpress() == MIDDLE_BUTTON)
863 highlight_letter3 = highlight_letter4 =
864 ibeam_letter = highlight_letter1 =
865 highlight_letter2 = cursor_letter;
866 paste_selection(PRIMARY_SELECTION);
871 highlight_letter3 = highlight_letter4 =
872 ibeam_letter = highlight_letter1 =
873 highlight_letter2 = cursor_letter;
877 // Handle scrolling by highlighting text
878 if(text_selected || word_selected || line_selected)
880 set_repeat(top_level->get_resources()->scroll_repeat);
883 if(ibeam_letter < 0) ibeam_letter = 0;
884 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
888 if(update_scroll && yscroll)
890 yscroll->update_length(get_text_rows(),
892 yscroll->get_handlelength(),
900 if( suggestions_popup && (!yscroll || !yscroll->is_event_win())) {
901 if( suggestions_popup->button_press_event() )
902 return suggestions_popup->handle_event();
904 else if( top_level->get_resources()->textbox_focus_policy == CLICK_DEACTIVATE )
911 int BC_TextBox::button_release_event()
916 if(text_selected || word_selected || line_selected)
922 // Stop scrolling by highlighting text
923 unset_repeat(top_level->get_resources()->scroll_repeat);
929 int BC_TextBox::cursor_motion_event()
931 int cursor_letter, letter1, letter2;
934 if(text_selected || word_selected || line_selected)
936 cursor_letter = get_cursor_letter(top_level->cursor_x,
937 top_level->cursor_y);
939 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
943 select_line(letter1, letter2, cursor_letter);
948 select_word(letter1, letter2, cursor_letter);
953 letter1 = letter2 = cursor_letter;
956 if(letter1 <= highlight_letter3)
958 highlight_letter1 = letter1;
959 highlight_letter2 = highlight_letter4;
960 ibeam_letter = letter1;
963 if(letter2 >= highlight_letter4)
965 highlight_letter2 = letter2;
966 highlight_letter1 = highlight_letter3;
967 ibeam_letter = letter2;
970 copy_selection(PRIMARY_SELECTION);
983 int BC_TextBox::activate()
987 top_level->set_active_subwindow(this);
988 top_level->set_repeat(top_level->get_resources()->blink_rate);
994 int BC_TextBox::deactivate()
998 text_selected = word_selected = line_selected = 0;
999 top_level->set_active_subwindow(0);
1000 top_level->unset_repeat(top_level->get_resources()->blink_rate);
1006 int BC_TextBox::repeat_event(int64_t duration)
1009 int cursor_y = get_cursor_y();
1010 //int cursor_x = get_cursor_x();
1012 if(duration == top_level->get_resources()->tooltip_delay &&
1013 tooltip_text && tooltip_text[0] != 0 && highlighted)
1020 if(duration == top_level->get_resources()->blink_rate &&
1024 // don't flash if keypress
1025 if(skip_cursor->get_difference() < 500)
1027 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1028 // skip_cursor->get_difference(),
1034 if(!(text_selected || word_selected || line_selected))
1043 if(duration == top_level->get_resources()->scroll_repeat &&
1044 (text_selected || word_selected || line_selected))
1047 if(get_cursor_y() < top_margin)
1049 difference = get_cursor_y() - top_margin ;
1052 if(get_cursor_y() > get_h() - bottom_margin)
1054 difference = get_cursor_y() -
1055 (get_h() - bottom_margin);
1057 if(difference != 0) {
1058 int min_y = -(get_text_rows() * text_height -
1059 get_h() + bottom_margin);
1061 text_y -= difference;
1062 // printf("BC_TextBox::repeat_event %d %d %d\n",
1066 text_y = MAX(min_y, text_y);
1067 text_y = MIN(text_y, top_margin);
1074 if(get_cursor_x() < left_margin)
1076 int difference = left_margin - get_cursor_x();
1078 text_x += difference;
1079 text_x = MIN(text_x, left_margin);
1083 else if(get_cursor_x() > get_w() - right_margin)
1085 int difference = get_cursor_x() - (get_w() - right_margin);
1086 int new_text_x = text_x - difference;
1088 // Get width of current row
1091 int wtext_len = wtext_update();
1094 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1097 while(wtext[i] != '\n' && i < wtext_len) {
1101 if(wtext[i] == '\n') i++;
1103 if(cursor_y >= k && cursor_y < k + text_height) {
1104 row_width = get_text_width(font,
1106 row_end - row_begin);
1112 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1113 new_text_x = MAX(new_text_x, min_x);
1114 new_text_x = MIN(new_text_x, left_margin);
1116 if(new_text_x < text_x) text_x = new_text_x;
1125 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1127 int key = top_level->get_keypress(), len;
1128 if( (key == RETURN) || ( key >= 32 && key <= 255 ) ) {
1129 wchar_t *wkeys = top_level->get_wkeystring(&len);
1130 if( key == RETURN ) { wkeys[0] = '\n'; wkeys[1] = 0; len = 1; }
1131 insert_text(wkeys, len);
1139 int BC_TextBox::keypress_event()
1141 // Result == 2 contents changed
1142 // Result == 1 trapped keypress
1143 // Result == 0 nothing
1145 int dispatch_event = 0;
1147 if(!active || !enabled) return 0;
1149 int wtext_len = wtext_update();
1150 last_keypress = get_keypress();
1152 if( unicode_active >= 0 ) {
1155 switch( last_keypress ) {
1156 //unicode active acitons
1158 for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1159 int ch = nib(wtext[i]);
1160 if( ch < 0 ) return 1;
1161 wch = (wch<<4) + ch;
1165 unicode_active = -1;
1171 unicode_active = -1;
1176 if(ibeam_letter > 0) {
1177 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1178 highlight_letter2 = --ibeam_letter;
1179 if( highlight_letter1 >= highlight_letter2 )
1180 unicode_active = -1;
1185 case '0': case '1': case '2': case '3': case '4':
1186 case '5': case '6': case '7': case '8': case '9':
1187 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1188 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1189 int n = nib(last_keypress);
1190 wch = n < 10 ? '0'+n : 'A'+n-10;
1204 insert_text(&wch, wlen);
1206 if( unicode_active >= 0 )
1207 highlight_letter2 = ibeam_letter;
1209 highlight_letter1 = highlight_letter2 = 0;
1215 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1216 switch(last_keypress) {
1218 // Deactivate the suggestions
1219 if( suggestions_popup ) {
1224 top_level->deactivate();
1231 highlight_letter1 = highlight_letter2 = 0;
1232 ibeam_letter = wtext_update();
1233 top_level->deactivate();
1238 default_keypress(dispatch_event, result);
1243 // Handle like a default keypress
1245 top_level->cycle_textboxes(1);
1250 top_level->cycle_textboxes(-1);
1255 if(ibeam_letter > 0) {
1256 int old_ibeam_letter = ibeam_letter;
1264 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1269 if(top_level->shift_down()) {
1270 // Initialize highlighting
1271 if(highlight_letter1 == highlight_letter2) {
1272 highlight_letter1 = ibeam_letter;
1273 highlight_letter2 = old_ibeam_letter;
1275 else if(highlight_letter1 == old_ibeam_letter) {
1276 // Extend left highlight
1277 highlight_letter1 = ibeam_letter;
1279 else if(highlight_letter2 == old_ibeam_letter) {
1280 // Shrink right highlight
1281 highlight_letter2 = ibeam_letter;
1285 highlight_letter1 = highlight_letter2 = ibeam_letter;
1290 if(keypress_draw) draw(1);
1296 if(ibeam_letter < wtext_len) {
1297 int old_ibeam_letter = ibeam_letter;
1304 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1310 if(top_level->shift_down()) {
1311 // Initialize highlighting
1312 if(highlight_letter1 == highlight_letter2) {
1313 highlight_letter1 = old_ibeam_letter;
1314 highlight_letter2 = ibeam_letter;
1316 else if(highlight_letter1 == old_ibeam_letter) {
1317 // Shrink left highlight
1318 highlight_letter1 = ibeam_letter;
1320 else if(highlight_letter2 == old_ibeam_letter) {
1321 // Expand right highlight
1322 highlight_letter2 = ibeam_letter;
1326 highlight_letter1 = highlight_letter2 = ibeam_letter;
1330 if(keypress_draw) draw(1);
1336 if( suggestions && suggestions_popup ) {
1337 // Pass to suggestions popup
1338 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1339 suggestions_popup->activate(1);
1340 suggestions_popup->keypress_event();
1343 else if(ibeam_letter > 0) {
1344 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1345 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1346 ibeam_y + text_y - text_height);
1347 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1350 if(top_level->shift_down()) {
1351 // Initialize highlighting
1352 if(highlight_letter1 == highlight_letter2) {
1353 highlight_letter1 = new_letter;
1354 highlight_letter2 = ibeam_letter;
1356 else if(highlight_letter1 == ibeam_letter) {
1357 // Expand left highlight
1358 highlight_letter1 = new_letter;
1360 else if(highlight_letter2 == ibeam_letter) {
1361 // Shrink right highlight
1362 highlight_letter2 = new_letter;
1366 highlight_letter1 = highlight_letter2 = new_letter;
1368 if(highlight_letter1 > highlight_letter2) {
1369 int temp = highlight_letter1;
1370 highlight_letter1 = highlight_letter2;
1371 highlight_letter2 = temp;
1373 ibeam_letter = new_letter;
1376 if(keypress_draw) draw(1);
1382 if(ibeam_letter > 0) {
1383 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1384 ibeam_y + text_y - get_h());
1387 if(top_level->shift_down()) {
1388 // Initialize highlighting
1389 if(highlight_letter1 == highlight_letter2) {
1390 highlight_letter1 = new_letter;
1391 highlight_letter2 = ibeam_letter;
1393 else if(highlight_letter1 == ibeam_letter) {
1394 // Expand left highlight
1395 highlight_letter1 = new_letter;
1397 else if(highlight_letter2 == ibeam_letter) {
1398 // Shrink right highlight
1399 highlight_letter2 = new_letter;
1403 highlight_letter1 = highlight_letter2 = new_letter;
1405 if(highlight_letter1 > highlight_letter2) {
1406 int temp = highlight_letter1;
1407 highlight_letter1 = highlight_letter2;
1408 highlight_letter2 = temp;
1410 ibeam_letter = new_letter;
1413 if(keypress_draw) draw(1);
1419 // printf("BC_TextBox::keypress_event %d %p %p\n",
1422 // suggestions_popup);
1423 if( suggestions && suggestions_popup ) {
1424 // Pass to suggestions popup
1425 suggestions_popup->activate(1);
1426 suggestions_popup->keypress_event();
1430 // if(ibeam_letter > 0)
1433 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1434 ibeam_y + text_y + text_height);
1435 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1437 if(top_level->shift_down()) {
1438 // Initialize highlighting
1439 if(highlight_letter1 == highlight_letter2) {
1440 highlight_letter1 = new_letter;
1441 highlight_letter2 = ibeam_letter;
1443 else if(highlight_letter1 == ibeam_letter) {
1444 // Shrink left highlight
1445 highlight_letter1 = new_letter;
1447 else if(highlight_letter2 == ibeam_letter) {
1448 // Expand right highlight
1449 highlight_letter2 = new_letter;
1453 highlight_letter1 = highlight_letter2 = new_letter;
1455 if(highlight_letter1 > highlight_letter2) {
1456 int temp = highlight_letter1;
1457 highlight_letter1 = highlight_letter2;
1458 highlight_letter2 = temp;
1460 ibeam_letter = new_letter;
1463 if(keypress_draw) draw(1);
1465 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1472 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1473 ibeam_y + text_y + get_h());
1474 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1476 if(top_level->shift_down()) {
1477 // Initialize highlighting
1478 if(highlight_letter1 == highlight_letter2) {
1479 highlight_letter1 = new_letter;
1480 highlight_letter2 = ibeam_letter;
1482 else if(highlight_letter1 == ibeam_letter) {
1483 // Shrink left highlight
1484 highlight_letter1 = new_letter;
1486 else if(highlight_letter2 == ibeam_letter) {
1487 // Expand right highlight
1488 highlight_letter2 = new_letter;
1492 highlight_letter1 = highlight_letter2 = new_letter;
1494 if(highlight_letter1 > highlight_letter2) {
1495 int temp = highlight_letter1;
1496 highlight_letter1 = highlight_letter2;
1497 highlight_letter2 = temp;
1499 ibeam_letter = new_letter;
1502 if(keypress_draw) draw(1);
1504 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1511 int old_ibeam_letter = ibeam_letter;
1513 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1516 if(top_level->shift_down()) {
1518 if(highlight_letter1 == highlight_letter2) {
1519 highlight_letter2 = ibeam_letter;
1520 highlight_letter1 = old_ibeam_letter;
1522 else if(highlight_letter1 == old_ibeam_letter) {
1524 highlight_letter1 = highlight_letter2;
1525 highlight_letter2 = ibeam_letter;
1527 else if(highlight_letter2 == old_ibeam_letter) {
1529 highlight_letter2 = ibeam_letter;
1533 highlight_letter1 = highlight_letter2 = ibeam_letter;
1536 if(keypress_draw) draw(1);
1543 int old_ibeam_letter = ibeam_letter;
1545 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1548 if(top_level->shift_down())
1551 if(highlight_letter1 == highlight_letter2)
1553 highlight_letter2 = old_ibeam_letter;
1554 highlight_letter1 = ibeam_letter;
1558 if(highlight_letter1 == old_ibeam_letter)
1560 highlight_letter1 = ibeam_letter;
1564 if(highlight_letter2 == old_ibeam_letter)
1566 highlight_letter2 = highlight_letter1;
1567 highlight_letter1 = ibeam_letter;
1571 highlight_letter1 = highlight_letter2 = ibeam_letter;
1574 if(keypress_draw) draw(1);
1581 if(highlight_letter1 == highlight_letter2) {
1582 if(ibeam_letter > 0) {
1583 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1588 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1589 highlight_letter2 = ibeam_letter = highlight_letter1;
1593 if(keypress_draw) draw(1);
1599 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1600 if(highlight_letter1 == highlight_letter2) {
1601 if(ibeam_letter < wtext_len) {
1602 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1606 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1607 highlight_letter2 = ibeam_letter = highlight_letter1;
1611 if(keypress_draw) draw(1);
1618 switch( get_keypress() ) {
1619 case 'c': case 'C': {
1620 if(highlight_letter1 != highlight_letter2) {
1621 copy_selection(SECONDARY_SELECTION);
1625 case 'v': case 'V': {
1626 paste_selection(SECONDARY_SELECTION);
1628 if(keypress_draw) draw(1);
1632 case 'x': case 'X': {
1633 if(highlight_letter1 != highlight_letter2) {
1634 copy_selection(SECONDARY_SELECTION);
1635 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1636 highlight_letter2 = ibeam_letter = highlight_letter1;
1640 if(keypress_draw) draw(1);
1644 case 'u': case 'U': {
1645 if( shift_down() ) {
1646 unicode_active = ibeam_letter;
1648 insert_text(&wkey, 1);
1650 highlight_letter1 = unicode_active;
1651 highlight_letter2 = ibeam_letter;
1660 default_keypress(dispatch_event, result);
1665 if(result) skip_cursor->update();
1666 if(dispatch_event && handle_event())
1673 int BC_TextBox::uses_text()
1679 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1682 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1683 wtext[i] = wtext[j];
1691 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1693 if( len < 0 ) len = wcslen(wcp);
1694 int wtext_len = wtext_update();
1695 if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1696 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1697 highlight_letter2 = ibeam_letter = highlight_letter1;
1698 wtext_len = wtext_update();
1702 for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1703 if( j >= wsize ) continue;
1704 wtext[j] = wtext[i];
1707 for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1708 if( i >= wsize ) break;
1711 if( (wlen+=len) > wsize ) wlen = wsize;
1712 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1713 wtext[wlen] = 0; // wtext allocated wsize+1
1717 int BC_TextBox::is_separator(const char *txt, int i)
1719 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1720 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1723 // used for time entry
1724 void BC_TextBox::do_separators(int ibeam_left)
1728 // Remove separators from text
1729 int wtext_len = wtext_update();
1730 for(int i = 0; i < wtext_len; ) {
1731 if( !iswalnum(wtext[i]) ) {
1732 for(int j = i; j < wtext_len - 1; j++)
1733 wtext[j] = wtext[j + 1];
1734 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1740 wtext[wtext_len] = 0;
1746 // Insert separators into text
1747 int separator_len = strlen(separators);
1748 for(int i = 0; i < separator_len; i++) {
1750 // Insert a separator
1751 if( is_separator(separators,i) ) {
1752 for(int j = wtext_len; j >= i; j--) {
1753 wtext[j + 1] = wtext[j];
1755 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1757 wtext[i] = separators[i];
1761 wtext[i] = separators[i];
1766 wtext[separator_len] = 0;
1767 wlen = separator_len;
1771 void BC_TextBox::get_ibeam_position(int &x, int &y)
1773 int i, row_begin, row_end;
1774 int wtext_len = wtext_update();
1777 for( i=0; i<wtext_len; ) {
1779 for(; i<wtext_len && wtext[i]!='\n'; i++);
1782 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1783 x = get_text_width(font, &wtext[row_begin], ibeam_letter - row_begin);
1784 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1788 if( i < wtext_len && wtext[i] == '\n' ) {
1793 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1799 void BC_TextBox::set_text_row(int row)
1801 text_x = left_margin;
1802 text_y = -(row * text_height) + top_margin;
1806 int BC_TextBox::get_text_row()
1808 return -(text_y - top_margin) / text_height;
1811 void BC_TextBox::find_ibeam(int dispatch_event)
1814 int old_x = text_x, old_y = text_y;
1816 get_ibeam_position(x, y);
1818 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1820 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1821 if(text_x > left_margin) text_x = left_margin;
1824 if(left_margin + text_x + x < left_margin)
1826 text_x = -(x - (get_w() / 4)) + left_margin;
1827 if(text_x > left_margin) text_x = left_margin;
1830 int text_row = y / text_height;
1831 if( text_row < rows ) text_y = top_margin;
1833 int pix_rows = get_h() - bottom_margin - (y + text_y);
1834 if( pix_rows < text_height )
1835 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1837 pix_rows = y + text_y - top_margin;
1838 if( pix_rows < 0 ) {
1839 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1840 if( text_y > top_margin ) text_y = top_margin;
1843 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1847 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1849 int i, j, k, row_begin, row_end, result = 0, done = 0;
1850 int column1, column2;
1851 int got_visible_row = 0;
1853 // Select complete row if cursor above the window
1854 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1855 if(cursor_y < text_y - text_height)
1861 int wtext_len = wtext_update();
1863 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1864 // Simulate drawing of 1 row
1866 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1870 int first_visible_row = 0;
1871 int last_visible_row = 0;
1872 if( k+text_height > top_margin && !got_visible_row) {
1873 first_visible_row = 1;
1874 got_visible_row = 1;
1877 if( (k+text_height >= get_h() - bottom_margin ||
1878 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1879 k + text_height > 0)) )
1880 last_visible_row = 1;
1882 // Cursor is inside vertical range of row
1883 if((cursor_y >= top_margin &&
1884 cursor_y < get_h() - bottom_margin &&
1885 cursor_y >= k && cursor_y < k + text_height) ||
1886 // Cursor is above 1st row
1887 (cursor_y < k + text_height && first_visible_row) ||
1888 // Cursor is below last row
1889 (cursor_y >= k && last_visible_row))
1891 column1 = column2 = 0;
1892 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1893 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1894 if((column2 + column1) / 2 >= cursor_x) {
1897 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1898 // __LINE__, result, first_visible_row, last_visible_row);
1909 if(wtext[i] == '\n') i++;
1911 // Select complete row if last visible & cursor is below window
1912 if(last_visible_row && cursor_y > k + text_height * 2)
1915 if(i >= wtext_len && !done) {
1921 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
1922 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
1923 if(result < 0) result = 0;
1924 if(result > wtext_len) {
1925 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
1933 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
1935 int i, j, k, row_begin, row_end, result = 0, done = 0;
1936 int column1, column2;
1937 int wtext_len = wtext_update();
1939 if(cursor_y < text_y) {
1944 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
1946 for(; wtext[i] != '\n' && i < wtext_len; i++);
1949 if(cursor_y >= k && cursor_y < k + text_height) {
1950 column1 = column2 = 0;
1951 for(j = 0; j <= row_end - row_begin && !done; j++) {
1952 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
1953 if((column2 + column1) / 2 >= cursor_x) {
1954 result = row_begin + j - 1;
1965 if(wtext[i] == '\n') i++;
1967 if(i >= wtext_len && !done) {
1971 if(result < 0) result = 0;
1972 if(result > wtext_len) result = wtext_len;
1977 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
1979 int wtext_len = wtext_update();
1980 letter1 = letter2 = ibeam_letter;
1981 if( letter1 < 0 ) letter1 = 0;
1982 if( letter2 < 0 ) letter2 = 0;
1983 if( letter1 > wtext_len ) letter1 = wtext_len;
1984 if( letter2 > wtext_len ) letter2 = wtext_len;
1985 if( !wtext_len ) return;
1986 for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
1987 for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
1988 if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
1992 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
1994 int wtext_len = wtext_update();
1995 letter1 = letter2 = ibeam_letter;
1996 if( letter1 < 0 ) letter1 = 0;
1997 if( letter2 < 0 ) letter2 = 0;
1998 if( letter1 > wtext_len ) letter1 = wtext_len;
1999 if( letter2 > wtext_len ) letter2 = wtext_len;
2000 if( !wtext_len ) return;
2001 for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2002 for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2005 void BC_TextBox::copy_selection(int clipboard_num)
2007 int wtext_len = wtext_update();
2008 if(!wtext_len) return;
2010 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2011 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2012 highlight_letter2 - highlight_letter1 <= 0) return;
2013 int clip_len = highlight_letter2 - highlight_letter1;
2014 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2015 char ctext[4*clip_len+4];
2016 clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2017 get_clipboard()->to_clipboard(ctext, clip_len, clipboard_num);
2021 void BC_TextBox::paste_selection(int clipboard_num)
2023 int len = get_clipboard()->clipboard_len(clipboard_num);
2026 char cstring[len]; wchar_t wstring[len];
2027 get_clipboard()->from_clipboard(cstring, len, clipboard_num); --len;
2028 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2029 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2030 cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2031 insert_text(wstring, len);
2035 void BC_TextBox::set_keypress_draw(int value)
2037 keypress_draw = value;
2040 int BC_TextBox::get_last_keypress()
2042 return last_keypress;
2045 int BC_TextBox::get_ibeam_letter()
2047 return ibeam_letter;
2050 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2052 this->ibeam_letter = number;
2059 void BC_TextBox::set_separators(const char *separators)
2061 this->separators = (char*)separators;
2064 int BC_TextBox::get_rows()
2075 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y)
2076 : BC_ListBox(x, y, text_box->get_w(), 200, LISTBOX_TEXT,
2077 text_box->suggestions, 0, 0, 1, 0, 1)
2079 this->text_box = text_box;
2081 set_justify(LISTBOX_LEFT);
2084 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2088 int BC_TextBoxSuggestions::handle_event()
2090 char *current_suggestion = 0;
2091 BC_ListBoxItem *item = get_selection(0, 0);
2092 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2093 if(item && (current_suggestion=item->get_text()) != 0)
2095 int col = text_box->suggestion_column;
2096 int len = BCTEXTLEN-1 - col;
2097 char *cp = &text_box->text[col];
2098 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2099 strncpy(cp, current_suggestion, len);
2100 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2101 if( (col=strlen(current_suggestion)) >= len )
2103 text_box->dirty = 1;
2107 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2108 text_box->highlight_letter1 =
2109 text_box->highlight_letter2 =
2110 text_box->ibeam_letter = text_box->tstrlen();
2111 text_box->wtext_update();
2113 text_box->handle_event();
2114 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2119 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2120 int x, int y, int w, int rows,
2121 const char *default_text, int default_size)
2123 this->parent_window = parent_window;
2128 this->default_text = default_text;
2129 this->default_wtext = 0;
2130 this->default_size = default_size;
2133 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2134 int x, int y, int w, int rows,
2135 const wchar_t *default_wtext, int default_size)
2137 this->parent_window = parent_window;
2142 this->default_text = 0;
2143 this->default_wtext = default_wtext;
2144 this->default_size = default_size;
2147 BC_ScrollTextBox::~BC_ScrollTextBox()
2156 void BC_ScrollTextBox::create_objects()
2158 // Must be created first
2159 parent_window->add_subwindow(text = default_wtext ?
2160 new BC_ScrollTextBoxText(this, default_wtext) :
2161 new BC_ScrollTextBoxText(this, default_text));
2162 parent_window->add_subwindow(yscroll = new BC_ScrollTextBoxYScroll(this));
2163 text->yscroll = yscroll;
2164 yscroll->bound_to = text;
2168 int BC_ScrollTextBox::handle_event()
2173 int BC_ScrollTextBox::get_x()
2178 int BC_ScrollTextBox::get_y()
2183 int BC_ScrollTextBox::get_w()
2188 int BC_ScrollTextBox::get_h()
2190 return this->text->get_h();
2193 int BC_ScrollTextBox::get_rows()
2199 const char* BC_ScrollTextBox::get_text()
2201 return text->get_text();
2204 const wchar_t* BC_ScrollTextBox::get_wtext()
2206 return text->get_wtext();
2209 void BC_ScrollTextBox::set_text(char *text, int isz)
2211 this->text->set_text(text, isz);
2212 yscroll->update_length(this->text->get_text_rows(),
2213 this->text->get_text_row(),
2214 yscroll->get_handlelength(),
2218 int BC_ScrollTextBox::set_text_row(int n)
2220 text->set_text_row(n);
2221 yscroll->update_value(n);
2225 void BC_ScrollTextBox::update(const char *text)
2227 this->text->update(text);
2228 yscroll->update_length(this->text->get_text_rows(),
2229 this->text->get_text_row(),
2230 yscroll->get_handlelength(),
2234 void BC_ScrollTextBox::update(const wchar_t *wtext)
2236 this->text->update(wtext);
2237 yscroll->update_length(this->text->get_text_rows(),
2238 this->text->get_text_row(),
2239 yscroll->get_handlelength(),
2243 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2250 text->reposition_window(x,
2252 w - yscroll->get_span(),
2254 yscroll->reposition_window(x + w - yscroll->get_span(),
2256 BC_TextBox::calculate_row_h(rows,
2258 yscroll->update_length(text->get_text_rows(),
2259 text->get_text_row(),
2264 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2266 this->text->set_selection(char1, char2, ibeam);
2269 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2271 this->text->wset_selection(char1, char2, ibeam);
2274 int BC_ScrollTextBox::get_ibeam_letter()
2276 return this->text->get_ibeam_letter();
2285 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2286 : BC_TextBox(gui->x, gui->y,
2287 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2288 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2293 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2294 : BC_TextBox(gui->x, gui->y,
2295 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2296 gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2301 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2310 int BC_ScrollTextBoxText::handle_event()
2312 gui->yscroll->update_length(get_text_rows(),
2314 gui->yscroll->get_handlelength(),
2316 return gui->handle_event();
2319 int BC_ScrollTextBoxText::motion_event()
2321 gui->yscroll->update_length(get_text_rows(),
2323 gui->yscroll->get_handlelength(),
2329 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2330 : BC_ScrollBar(gui->x +
2332 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2335 BC_TextBox::calculate_row_h(gui->rows,
2336 gui->parent_window),
2337 gui->text->get_text_rows(),
2344 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2348 int BC_ScrollTextBoxYScroll::handle_event()
2350 gui->text->set_text_row(get_position());
2363 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2364 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2366 this->popup = popup;
2369 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2370 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2372 this->popup = popup;
2375 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2385 int BC_PopupTextBoxText::handle_event()
2387 popup->handle_event();
2391 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2393 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2394 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2396 this->popup = popup;
2398 int BC_PopupTextBoxList::handle_event()
2400 BC_ListBoxItem *item = get_selection(0, 0);
2403 popup->textbox->update(item->get_text());
2404 popup->handle_event();
2412 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2413 ArrayList<BC_ListBoxItem*> *list_items,
2414 const char *default_text, int x, int y,
2415 int text_w, int list_h, int list_format)
2419 this->list_h = list_h;
2420 this->list_format = list_format;
2421 this->default_text = (char*)default_text;
2422 this->default_wtext = 0;
2423 this->text_w = text_w;
2424 this->parent_window = parent_window;
2425 this->list_items = list_items;
2428 BC_PopupTextBox::~BC_PopupTextBox()
2438 int BC_PopupTextBox::create_objects()
2440 int x = this->x, y = this->y;
2441 parent_window->add_subwindow(textbox = default_wtext ?
2442 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2443 new BC_PopupTextBoxText(this, x, y, default_text));
2444 x += textbox->get_w();
2445 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2449 void BC_PopupTextBox::update(const char *text)
2451 textbox->update(text);
2454 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2456 listbox->update(data,
2463 const char* BC_PopupTextBox::get_text()
2465 return textbox->get_text();
2468 const wchar_t* BC_PopupTextBox::get_wtext()
2470 return textbox->get_wtext();
2473 int BC_PopupTextBox::get_number()
2475 return listbox->get_selection_number(0, 0);
2478 int BC_PopupTextBox::get_x()
2483 int BC_PopupTextBox::get_y()
2488 int BC_PopupTextBox::get_w()
2490 return textbox->get_w() + listbox->get_w();
2493 int BC_PopupTextBox::get_h()
2495 return textbox->get_h();
2498 int BC_PopupTextBox::handle_event()
2503 void BC_PopupTextBox::reposition_window(int x, int y)
2508 textbox->reposition_window(x1,
2511 textbox->get_rows());
2512 x1 += textbox->get_w();
2513 listbox->reposition_window(x1, y1, -1, -1, 0);
2514 // if(flush) parent_window->flush();
2530 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2531 int64_t default_value, int x, int y)
2532 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2534 this->popup = popup;
2537 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2538 float default_value, int x, int y)
2539 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2541 this->popup = popup;
2544 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2556 int BC_TumbleTextBoxText::handle_event()
2558 popup->handle_event();
2562 int BC_TumbleTextBoxText::button_press_event()
2566 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
2568 if(get_buttonpress() == 4)
2570 popup->tumbler->handle_up_event();
2573 if(get_buttonpress() == 5)
2575 popup->tumbler->handle_down_event();
2585 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2586 int64_t default_value,
2598 this->default_value = default_value;
2599 this->text_w = text_w;
2600 this->parent_window = parent_window;
2606 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2619 this->default_value = default_value;
2620 this->text_w = text_w;
2621 this->parent_window = parent_window;
2627 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2628 float default_value_f,
2638 this->min_f = min_f;
2639 this->max_f = max_f;
2640 this->default_value_f = default_value_f;
2641 this->text_w = text_w;
2642 this->parent_window = parent_window;
2648 BC_TumbleTextBox::~BC_TumbleTextBox()
2650 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2651 // is deleted anyway by the windowbase so textbox deletes this.
2652 if(tumbler) delete tumbler;
2654 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2663 void BC_TumbleTextBox::reset()
2670 void BC_TumbleTextBox::set_precision(int precision)
2672 this->precision = precision;
2675 void BC_TumbleTextBox::set_increment(float value)
2677 this->increment = value;
2678 if(tumbler) tumbler->set_increment(value);
2681 void BC_TumbleTextBox::set_log_floatincrement(int value)
2683 this->log_floatincrement = value;
2684 if(tumbler) tumbler->set_log_floatincrement(value);
2687 int BC_TumbleTextBox::create_objects()
2689 int x = this->x, y = this->y;
2693 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2694 default_value_f, x, y));
2695 textbox->set_precision(precision);
2698 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2699 default_value, x, y));
2701 x += textbox->get_w();
2704 parent_window->add_subwindow(tumbler = new BC_FTumbler(textbox,
2710 parent_window->add_subwindow(tumbler = new BC_ITumbler(textbox,
2716 tumbler->set_increment(increment);
2720 const char* BC_TumbleTextBox::get_text()
2722 return textbox->get_text();
2725 const wchar_t* BC_TumbleTextBox::get_wtext()
2727 return textbox->get_wtext();
2730 BC_TextBox* BC_TumbleTextBox::get_textbox()
2735 int BC_TumbleTextBox::update(const char *value)
2737 textbox->update(value);
2741 int BC_TumbleTextBox::update(int64_t value)
2743 textbox->update(value);
2747 int BC_TumbleTextBox::update(float value)
2749 textbox->update(value);
2754 int BC_TumbleTextBox::get_x()
2759 int BC_TumbleTextBox::get_y()
2764 int BC_TumbleTextBox::get_w()
2766 return textbox->get_w() + tumbler->get_w();
2769 int BC_TumbleTextBox::get_h()
2771 return textbox->get_h();
2774 void BC_TumbleTextBox::disable(int hide_text)
2776 if( hide_text && !textbox->is_hidden() )
2777 textbox->hide_window(0);
2778 if( !tumbler->is_hidden() )
2779 tumbler->hide_window(0);
2780 if( !get_enabled() ) return;
2781 return textbox->disable();
2784 void BC_TumbleTextBox::enable()
2786 if( textbox->is_hidden() )
2787 textbox->show_window(0);
2788 if( tumbler->is_hidden() )
2789 tumbler->show_window(0);
2790 if( get_enabled() ) return;
2791 return textbox->enable();
2794 int BC_TumbleTextBox::get_enabled()
2796 return textbox->get_enabled();
2799 int BC_TumbleTextBox::handle_event()
2804 void BC_TumbleTextBox::reposition_window(int x, int y)
2809 textbox->reposition_window(x,
2813 tumbler->reposition_window(x + textbox->get_w(),
2815 // if(flush) parent_window->flush();
2819 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2821 tumbler->set_boundaries(min, max);
2824 void BC_TumbleTextBox::set_boundaries(float min, float max)
2826 tumbler->set_boundaries(min, max);