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();
141 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
143 suggestions = new ArrayList<BC_ListBoxItem*>;
144 suggestions_popup = 0;
145 suggestion_column = 0;
148 this->has_border = has_border;
151 this->tsize = size >= 0 ? size : -size;
152 this->text = size > 0 ? new char[size+1] : 0;
153 if( this->text ) this->text[0] = 0;
156 highlight_letter1 = highlight_letter2 = 0;
157 highlight_letter3 = highlight_letter4 = 0;
169 skip_cursor = new Timer;
182 int BC_TextBox::tstrlen()
184 if( !tsize ) return strlen(text);
185 return strnlen(text, tsize);
188 int BC_TextBox::tstrcmp(const char *cp)
190 if( !tsize ) return strcmp(text, cp);
191 return strncmp(text, cp, tsize);
194 char *BC_TextBox::tstrcpy(const char *cp)
199 return strcpy(text, cp);
200 strncpy(text, cp, tsize);
208 char *BC_TextBox::tstrcat(const char *cp)
211 if( !tsize ) return strcat(text, cp);
212 char *result = strncat(text, cp, tsize);
217 int BC_TextBox::wtext_update()
220 const char *src_enc = is_utf8 ? "UTF8" : BC_Resources::encoding;
221 const char *dst_enc = BC_Resources::wide_encoding;
222 int nsize = tsize > 0 ? tsize : strlen(text) + BCTEXTLEN;
223 if( nsize > wsize || !wtext ) {
224 wchar_t *ntext = new wchar_t[nsize+1];
225 memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
226 delete [] wtext; wtext = ntext; wsize = nsize;
228 wlen = BC_Resources::encode(src_enc, dst_enc, text, strlen(text),
229 (char*)wtext, wsize*sizeof(wchar_t)) / sizeof(wchar_t);
236 int BC_TextBox::text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz)
238 const char *src_enc = BC_Resources::wide_encoding;
239 const char *dst_enc = BC_Resources::encoding;
240 if( wsz < 0 ) wsz = wcslen(wcp);
241 int len = BC_Resources::encode(src_enc, dst_enc,
242 (char*)wcp, wsz*sizeof(wchar_t), tcp, tsz);
247 int BC_TextBox::initialize()
250 skip_cursor = new Timer;
251 skip_cursor->update();
253 text_ascent = get_text_ascent(font) + 1;
254 text_descent = get_text_descent(font) + 1;
255 text_height = text_ascent + text_descent;
256 ibeam_letter = wtext_update();
259 left_margin = right_margin = HORIZONTAL_MARGIN;
260 top_margin = bottom_margin = VERTICAL_MARGIN;
264 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
265 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
268 text_x = left_margin;
272 // Create the subwindow
273 BC_SubWindow::initialize();
275 BC_Resources *resources = get_resources();
278 back_color = resources->text_background;
279 high_color = resources->text_background_hi;
283 high_color = resources->text_background_noborder_hi;
284 back_color = bg_color;
288 set_cursor(IBEAM_CURSOR, 0, 0);
293 int BC_TextBox::calculate_h(BC_WindowBase *gui,
298 return rows * (gui->get_text_ascent(font) + 1 +
299 gui->get_text_descent(font) + 1) +
300 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
303 void BC_TextBox::set_precision(int precision)
305 this->precision = precision;
308 // Compute suggestions for a path
309 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries, const char *filter)
311 // Let user delete suggestion
312 if(get_last_keypress() != BACKSPACE) {
313 // Compute suggestions
315 ArrayList<char*> suggestions;
316 const char *current_text = get_text();
317 int suggestion_column = 0;
318 char dirname[BCTEXTLEN]; dirname[0] = 0;
319 if( current_text[0] == '/' || current_text[0] == '~' )
320 strncpy(dirname, current_text, sizeof(dirname));
322 getcwd(dirname, sizeof(dirname));
323 // If directory, tabulate it
325 if( filter ) fs.set_filter(filter);
327 strncpy(dirname, current_text, sizeof(dirname));
328 if( (cp=strrchr(dirname, '/')) ||
329 (cp=strrchr(dirname, '~')) ) *++cp = 0;
330 fs.parse_tildas(dirname);
332 cp = (char *)current_text;
333 if( (prefix=strrchr(cp, '/')) ||
334 (prefix=strrchr(cp, '~')) ) ++prefix;
335 suggestion_column = !prefix ? 0 : prefix - cp;
336 int prefix_len = prefix ? strlen(prefix) : 0;
337 // only include items where the filename matches the basename prefix
338 for(int i = 0; i < fs.total_files(); i++) {
339 char *current_name = fs.get_entry(i)->name;
340 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
341 suggestions.append(current_name);
345 char *prefix = (char *)current_text;
346 int prefix_len = strlen(prefix);
347 for(int i = 0; i < entries->size(); i++) {
348 char *current_name = entries->get(i)->get_text();
349 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
350 suggestions.append(current_name);
353 set_suggestions(&suggestions, suggestion_column);
359 void BC_TextBox::no_suggestions()
361 if( suggestions_popup ) {
362 delete suggestions_popup;
363 suggestions_popup = 0;
368 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
371 this->suggestions->remove_all_objects();
372 this->suggestion_column = column;
375 for(int i = 0; i < suggestions->size(); i++) {
376 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
379 // Show the popup without taking focus
380 if( suggestions->size() > 1 ) {
381 if( !suggestions_popup ) {
382 suggestions_popup = new BC_TextBoxSuggestions(this, x, y);
383 get_parent()->add_subwindow(suggestions_popup);
384 suggestions_popup->set_is_suggestions(1);
387 suggestions_popup->update(this->suggestions, 0, 0, 1);
389 suggestions_popup->activate(0);
392 // Show the highlighted text
393 if( suggestions->size() == 1 ) {
394 highlight_letter1 = wtext_update();
395 text_update(wtext,wlen, text,tsize);
396 char *current_suggestion = suggestions->get(0);
397 int col = highlight_letter1 - suggestion_column;
398 if( col < 0 ) col = 0;
399 char *cur = current_suggestion + col;
401 highlight_letter2 = wtext_update();
402 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
410 if( !suggestions || !this->suggestions->size() )
414 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
416 highlight_letter1 = char1;
417 highlight_letter2 = char2;
418 ibeam_letter = ibeam;
422 // count utf8 chars in text which occur before cp
423 int BC_TextBox::wcpos(const char *text, const char *cp)
426 const unsigned char *bp = (const unsigned char *)text;
427 const unsigned char *ep = (const unsigned char *)cp;
428 while( bp < ep && *bp != 0 ) {
431 if( ch < 0x80 ) continue;
433 int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
434 for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
439 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
441 const char *cp = get_text();
442 wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
445 int BC_TextBox::update(const char *text)
447 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
448 // Don't update if contents are the same
449 if(!tstrcmp(text)) return 0;
451 int wtext_len = wtext_update();
452 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
453 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
454 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
459 int BC_TextBox::update(const wchar_t *wtext)
461 int wtext_len = wcslen(wtext);
462 if( wtext_len >= wsize ) wtext_len = wsize;
463 wcsncpy(this->wtext, wtext, wtext_len);
465 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
466 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
467 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
472 int BC_TextBox::update(int64_t value)
474 char string[BCTEXTLEN];
475 sprintf(string, "%jd", value);
480 int BC_TextBox::update(float value)
482 char string[BCTEXTLEN];
483 sprintf(string, "%0.*f", precision, value);
488 void BC_TextBox::disable()
497 void BC_TextBox::enable()
507 int BC_TextBox::get_enabled() { return enabled; }
508 int BC_TextBox::get_text_x() { return text_x; }
509 int BC_TextBox::get_text_y() { return text_y; }
510 void BC_TextBox::set_text_x(int v) { text_x = v; }
511 void BC_TextBox::set_text_y(int v) { text_y = v; }
513 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
515 return (pixels - 4) /
516 (window->get_text_ascent(font) + 1 +
517 window->get_text_descent(font) + 1);
520 int BC_TextBox::calculate_row_h(int rows,
521 BC_WindowBase *parent_window,
526 (parent_window->get_text_ascent(font) + 1 +
527 parent_window->get_text_descent(font) + 1) +
528 (has_border ? 4 : 0);
531 const char* BC_TextBox::get_text()
533 int wtext_len = wtext_update();
534 text_update(wtext,wtext_len, text,tsize);
538 const wchar_t* BC_TextBox::get_wtext()
544 void BC_TextBox::set_text(char *text, int isz)
546 if( size > 0 || isz < 0 ) return;
555 int BC_TextBox::get_text_rows()
557 int wtext_len = wtext_update();
559 for(int i = 0; i < wtext_len; i++) {
560 if(wtext[i] == '\n') result++;
566 int BC_TextBox::get_row_h(int rows)
568 return rows * text_height + top_margin + bottom_margin;
571 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
574 if(w < 0) w = get_w();
577 new_h = get_row_h(rows);
586 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
587 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
588 BC_WindowBase::reposition_window(x, y, w, new_h);
594 void BC_TextBox::draw_border()
596 BC_Resources *resources = get_resources();
598 set_color(background_color);
599 draw_box(0, 0, left_margin, get_h());
600 draw_box(get_w() - right_margin, 0, right_margin, get_h());
605 draw_3d_border(0, 0, w, h,
606 resources->text_border1,
607 resources->text_border2_hi,
608 resources->text_border3_hi,
609 resources->text_border4);
611 draw_3d_border(0, 0, w, h,
612 resources->text_border1,
613 resources->text_border2,
614 resources->text_border3,
615 resources->text_border4);
619 void BC_TextBox::draw_cursor()
621 // set_color(background_color);
629 draw_box(ibeam_x + text_x,
638 void BC_TextBox::draw(int flush)
641 int row_begin, row_end;
642 int highlight_x1, highlight_x2;
644 BC_Resources *resources = get_resources();
646 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
649 background_color = resources->text_background;
651 background_color = high_color;
653 background_color = back_color;
654 set_color(background_color);
655 draw_box(0, 0, w, h);
657 int wtext_len = wtext_update();
659 // Draw text with selection
662 for(i=0, j=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
665 wchar_t *wtext_row = &wtext[i];
666 for(j=0; j<BCTEXTLEN-1 && i<wtext_len && wtext[i]!='\n'; ++i, ++j);
667 if( (row_end=i) < wtext_len ) ++i;
669 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
670 // Draw highlighted region of row
671 if( highlight_letter2 > highlight_letter1 &&
672 highlight_letter2 > row_begin &&
673 highlight_letter1 <= row_end ) {
674 int color = active && enabled && get_has_focus() ?
675 resources->text_highlight :
676 resources->text_inactive_highlight;
677 if( unicode_active >= 0 )
680 if(highlight_letter1 >= row_begin &&
681 highlight_letter1 <= row_end)
682 highlight_x1 = get_x_position(highlight_letter1, row_begin);
686 if(highlight_letter2 > row_begin &&
687 highlight_letter2 <= row_end)
688 highlight_x2 = get_x_position(highlight_letter2, row_begin);
690 highlight_x2 = get_w();
692 draw_box(highlight_x1 + text_x, k,
693 highlight_x2 - highlight_x1, text_height);
696 // Draw text over highlight
697 int len = row_end - row_begin;
699 set_color(enabled ? resources->text_default : DMGREY);
700 draw_wtext(text_x, k + text_ascent, wtext_row, len, 0);
703 // Get ibeam location
704 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
706 ibeam_y = k - text_y;
707 ibeam_x = get_x_position(ibeam_letter, row_begin);
712 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
714 // ibeam_x = ibeam_y = !wtext_len ? 0 : -1;
715 ibeam_x = 0; ibeam_y = k - text_y;
718 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
728 int BC_TextBox::focus_in_event()
734 int BC_TextBox::focus_out_event()
740 int BC_TextBox::cursor_enter_event()
742 if( top_level->event_win == win && enabled &&
743 !(top_level->get_resources()->textbox_focus_policy & CLICK_ACTIVATE) )
746 top_level->deactivate();
759 int BC_TextBox::cursor_leave_event()
768 if( !suggestions_popup && !get_button_down() &&
769 !(top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
774 int BC_TextBox::button_press_event()
778 if(!enabled) return 0;
779 if(get_buttonpress() != WHEEL_UP &&
780 get_buttonpress() != WHEEL_DOWN &&
781 get_buttonpress() != LEFT_BUTTON &&
782 get_buttonpress() != MIDDLE_BUTTON) return 0;
786 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
788 int cursor_letter = 0;
789 int wtext_len = wtext_update();
790 int update_scroll = 0;
793 if(top_level->event_win == win)
798 top_level->deactivate();
803 if(get_buttonpress() == WHEEL_UP)
805 text_y += text_height;
806 text_y = MIN(text_y, top_margin);
810 if(get_buttonpress() == WHEEL_DOWN)
812 int min_y = -(get_text_rows() *
816 text_y -= text_height;
817 text_y = MAX(text_y, min_y);
818 text_y = MIN(text_y, top_margin);
824 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
826 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
829 if(get_triple_click())
831 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
833 select_line(highlight_letter1, highlight_letter2, cursor_letter);
834 highlight_letter3 = highlight_letter1;
835 highlight_letter4 = highlight_letter2;
836 ibeam_letter = highlight_letter2;
837 copy_selection(PRIMARY_SELECTION);
840 if(get_double_click())
843 select_word(highlight_letter1, highlight_letter2, cursor_letter);
844 highlight_letter3 = highlight_letter1;
845 highlight_letter4 = highlight_letter2;
846 ibeam_letter = highlight_letter2;
847 copy_selection(PRIMARY_SELECTION);
850 if(get_buttonpress() == MIDDLE_BUTTON)
852 highlight_letter3 = highlight_letter4 =
853 ibeam_letter = highlight_letter1 =
854 highlight_letter2 = cursor_letter;
855 paste_selection(PRIMARY_SELECTION);
860 highlight_letter3 = highlight_letter4 =
861 ibeam_letter = highlight_letter1 =
862 highlight_letter2 = cursor_letter;
866 // Handle scrolling by highlighting text
867 if(text_selected || word_selected || line_selected)
869 set_repeat(top_level->get_resources()->scroll_repeat);
872 if(ibeam_letter < 0) ibeam_letter = 0;
873 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
877 if(update_scroll && yscroll)
879 yscroll->update_length(get_text_rows(),
881 yscroll->get_handlelength(),
889 if( suggestions_popup && (!yscroll || !yscroll->is_event_win())) {
890 if( suggestions_popup->button_press_event() )
891 return suggestions_popup->handle_event();
893 else if( (top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
900 int BC_TextBox::button_release_event()
905 if(text_selected || word_selected || line_selected)
911 // Stop scrolling by highlighting text
912 unset_repeat(top_level->get_resources()->scroll_repeat);
918 int BC_TextBox::cursor_motion_event()
920 int cursor_letter, letter1, letter2;
923 if(text_selected || word_selected || line_selected)
925 cursor_letter = get_cursor_letter(top_level->cursor_x,
926 top_level->cursor_y);
928 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
932 select_line(letter1, letter2, cursor_letter);
937 select_word(letter1, letter2, cursor_letter);
942 letter1 = letter2 = cursor_letter;
945 if(letter1 <= highlight_letter3)
947 highlight_letter1 = letter1;
948 highlight_letter2 = highlight_letter4;
949 ibeam_letter = letter1;
952 if(letter2 >= highlight_letter4)
954 highlight_letter2 = letter2;
955 highlight_letter1 = highlight_letter3;
956 ibeam_letter = letter2;
959 copy_selection(PRIMARY_SELECTION);
972 int BC_TextBox::activate()
976 top_level->set_active_subwindow(this);
977 top_level->set_repeat(top_level->get_resources()->blink_rate);
983 int BC_TextBox::deactivate()
987 text_selected = word_selected = line_selected = 0;
988 top_level->set_active_subwindow(0);
989 top_level->unset_repeat(top_level->get_resources()->blink_rate);
995 int BC_TextBox::repeat_event(int64_t duration)
998 int cursor_y = get_cursor_y();
999 //int cursor_x = get_cursor_x();
1001 if(duration == top_level->get_resources()->tooltip_delay &&
1002 tooltip_text && tooltip_text[0] != 0 && highlighted)
1008 if(duration == top_level->get_resources()->blink_rate &&
1012 // don't flash if keypress
1013 if(skip_cursor->get_difference() < 500)
1015 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1016 // skip_cursor->get_difference(),
1022 if(!(text_selected || word_selected || line_selected))
1031 if(duration == top_level->get_resources()->scroll_repeat &&
1032 (text_selected || word_selected || line_selected))
1035 if(get_cursor_y() < top_margin)
1037 difference = get_cursor_y() - top_margin ;
1040 if(get_cursor_y() > get_h() - bottom_margin)
1042 difference = get_cursor_y() -
1043 (get_h() - bottom_margin);
1045 if(difference != 0) {
1046 int min_y = -(get_text_rows() * text_height -
1047 get_h() + bottom_margin);
1049 text_y -= difference;
1050 // printf("BC_TextBox::repeat_event %d %d %d\n",
1054 text_y = MAX(min_y, text_y);
1055 text_y = MIN(text_y, top_margin);
1062 if(get_cursor_x() < left_margin)
1064 int difference = left_margin - get_cursor_x();
1066 text_x += difference;
1067 text_x = MIN(text_x, left_margin);
1071 else if(get_cursor_x() > get_w() - right_margin)
1073 int difference = get_cursor_x() - (get_w() - right_margin);
1074 int new_text_x = text_x - difference;
1076 // Get width of current row
1079 int wtext_len = wtext_update();
1082 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1085 while(wtext[i] != '\n' && i < wtext_len) {
1089 if(wtext[i] == '\n') i++;
1091 if(cursor_y >= k && cursor_y < k + text_height) {
1092 row_width = get_text_width(font,
1094 row_end - row_begin);
1100 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1101 new_text_x = MAX(new_text_x, min_x);
1102 new_text_x = MIN(new_text_x, left_margin);
1104 if(new_text_x < text_x) text_x = new_text_x;
1113 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1115 int key = top_level->get_keypress(), len;
1116 if( (key == RETURN) || ( key >= 32 && key <= 255 ) ) {
1117 wchar_t *wkeys = top_level->get_wkeystring(&len);
1118 if( key == RETURN ) { wkeys[0] = '\n'; wkeys[1] = 0; len = 1; }
1119 insert_text(wkeys, len);
1127 int BC_TextBox::keypress_event()
1129 // Result == 2 contents changed
1130 // Result == 1 trapped keypress
1131 // Result == 0 nothing
1133 int dispatch_event = 0;
1135 if(!active || !enabled) return 0;
1137 int wtext_len = wtext_update();
1138 last_keypress = get_keypress();
1140 if( unicode_active >= 0 ) {
1143 switch( last_keypress ) {
1144 //unicode active acitons
1146 for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1147 int ch = nib(wtext[i]);
1148 if( ch < 0 ) return 1;
1149 wch = (wch<<4) + ch;
1153 unicode_active = -1;
1159 unicode_active = -1;
1164 if(ibeam_letter > 0) {
1165 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1166 highlight_letter2 = --ibeam_letter;
1167 if( highlight_letter1 >= highlight_letter2 )
1168 unicode_active = -1;
1173 case '0': case '1': case '2': case '3': case '4':
1174 case '5': case '6': case '7': case '8': case '9':
1175 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1176 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1177 int n = nib(last_keypress);
1178 wch = n < 10 ? '0'+n : 'A'+n-10;
1192 insert_text(&wch, wlen);
1194 if( unicode_active >= 0 )
1195 highlight_letter2 = ibeam_letter;
1197 highlight_letter1 = highlight_letter2 = 0;
1203 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1204 switch(last_keypress) {
1206 // Deactivate the suggestions
1207 if( suggestions_popup ) {
1212 top_level->deactivate();
1219 highlight_letter1 = highlight_letter2 = 0;
1220 ibeam_letter = wtext_update();
1221 top_level->deactivate();
1226 default_keypress(dispatch_event, result);
1231 // Handle like a default keypress
1233 top_level->cycle_textboxes(1);
1238 top_level->cycle_textboxes(-1);
1243 if(ibeam_letter > 0) {
1244 int old_ibeam_letter = ibeam_letter;
1252 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1257 if(top_level->shift_down()) {
1258 // Initialize highlighting
1259 if(highlight_letter1 == highlight_letter2) {
1260 highlight_letter1 = ibeam_letter;
1261 highlight_letter2 = old_ibeam_letter;
1263 else if(highlight_letter1 == old_ibeam_letter) {
1264 // Extend left highlight
1265 highlight_letter1 = ibeam_letter;
1267 else if(highlight_letter2 == old_ibeam_letter) {
1268 // Shrink right highlight
1269 highlight_letter2 = ibeam_letter;
1273 highlight_letter1 = highlight_letter2 = ibeam_letter;
1278 if(keypress_draw) draw(1);
1284 if(ibeam_letter < wtext_len) {
1285 int old_ibeam_letter = ibeam_letter;
1292 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1298 if(top_level->shift_down()) {
1299 // Initialize highlighting
1300 if(highlight_letter1 == highlight_letter2) {
1301 highlight_letter1 = old_ibeam_letter;
1302 highlight_letter2 = ibeam_letter;
1304 else if(highlight_letter1 == old_ibeam_letter) {
1305 // Shrink left highlight
1306 highlight_letter1 = ibeam_letter;
1308 else if(highlight_letter2 == old_ibeam_letter) {
1309 // Expand right highlight
1310 highlight_letter2 = ibeam_letter;
1314 highlight_letter1 = highlight_letter2 = ibeam_letter;
1318 if(keypress_draw) draw(1);
1324 if( suggestions && suggestions_popup ) {
1325 // Pass to suggestions popup
1326 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1327 suggestions_popup->activate(1);
1328 suggestions_popup->keypress_event();
1331 else if(ibeam_letter > 0) {
1332 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1333 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1334 ibeam_y + text_y - text_height);
1335 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1338 if(top_level->shift_down()) {
1339 // Initialize highlighting
1340 if(highlight_letter1 == highlight_letter2) {
1341 highlight_letter1 = new_letter;
1342 highlight_letter2 = ibeam_letter;
1344 else if(highlight_letter1 == ibeam_letter) {
1345 // Expand left highlight
1346 highlight_letter1 = new_letter;
1348 else if(highlight_letter2 == ibeam_letter) {
1349 // Shrink right highlight
1350 highlight_letter2 = new_letter;
1354 highlight_letter1 = highlight_letter2 = new_letter;
1356 if(highlight_letter1 > highlight_letter2) {
1357 int temp = highlight_letter1;
1358 highlight_letter1 = highlight_letter2;
1359 highlight_letter2 = temp;
1361 ibeam_letter = new_letter;
1364 if(keypress_draw) draw(1);
1370 if(ibeam_letter > 0) {
1371 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1372 ibeam_y + text_y - get_h());
1375 if(top_level->shift_down()) {
1376 // Initialize highlighting
1377 if(highlight_letter1 == highlight_letter2) {
1378 highlight_letter1 = new_letter;
1379 highlight_letter2 = ibeam_letter;
1381 else if(highlight_letter1 == ibeam_letter) {
1382 // Expand left highlight
1383 highlight_letter1 = new_letter;
1385 else if(highlight_letter2 == ibeam_letter) {
1386 // Shrink right highlight
1387 highlight_letter2 = new_letter;
1391 highlight_letter1 = highlight_letter2 = new_letter;
1393 if(highlight_letter1 > highlight_letter2) {
1394 int temp = highlight_letter1;
1395 highlight_letter1 = highlight_letter2;
1396 highlight_letter2 = temp;
1398 ibeam_letter = new_letter;
1401 if(keypress_draw) draw(1);
1407 // printf("BC_TextBox::keypress_event %d %p %p\n",
1410 // suggestions_popup);
1411 if( suggestions && suggestions_popup ) {
1412 // Pass to suggestions popup
1413 suggestions_popup->activate(1);
1414 suggestions_popup->keypress_event();
1418 // if(ibeam_letter > 0)
1421 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1422 ibeam_y + text_y + text_height);
1423 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1425 if(top_level->shift_down()) {
1426 // Initialize highlighting
1427 if(highlight_letter1 == highlight_letter2) {
1428 highlight_letter1 = new_letter;
1429 highlight_letter2 = ibeam_letter;
1431 else if(highlight_letter1 == ibeam_letter) {
1432 // Shrink left highlight
1433 highlight_letter1 = new_letter;
1435 else if(highlight_letter2 == ibeam_letter) {
1436 // Expand right highlight
1437 highlight_letter2 = new_letter;
1441 highlight_letter1 = highlight_letter2 = new_letter;
1443 if(highlight_letter1 > highlight_letter2) {
1444 int temp = highlight_letter1;
1445 highlight_letter1 = highlight_letter2;
1446 highlight_letter2 = temp;
1448 ibeam_letter = new_letter;
1451 if(keypress_draw) draw(1);
1453 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1460 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1461 ibeam_y + text_y + get_h());
1462 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1464 if(top_level->shift_down()) {
1465 // Initialize highlighting
1466 if(highlight_letter1 == highlight_letter2) {
1467 highlight_letter1 = new_letter;
1468 highlight_letter2 = ibeam_letter;
1470 else if(highlight_letter1 == ibeam_letter) {
1471 // Shrink left highlight
1472 highlight_letter1 = new_letter;
1474 else if(highlight_letter2 == ibeam_letter) {
1475 // Expand right highlight
1476 highlight_letter2 = new_letter;
1480 highlight_letter1 = highlight_letter2 = new_letter;
1482 if(highlight_letter1 > highlight_letter2) {
1483 int temp = highlight_letter1;
1484 highlight_letter1 = highlight_letter2;
1485 highlight_letter2 = temp;
1487 ibeam_letter = new_letter;
1490 if(keypress_draw) draw(1);
1492 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1499 int old_ibeam_letter = ibeam_letter;
1501 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1504 if(top_level->shift_down()) {
1506 if(highlight_letter1 == highlight_letter2) {
1507 highlight_letter2 = ibeam_letter;
1508 highlight_letter1 = old_ibeam_letter;
1510 else if(highlight_letter1 == old_ibeam_letter) {
1512 highlight_letter1 = highlight_letter2;
1513 highlight_letter2 = ibeam_letter;
1515 else if(highlight_letter2 == old_ibeam_letter) {
1517 highlight_letter2 = ibeam_letter;
1521 highlight_letter1 = highlight_letter2 = ibeam_letter;
1524 if(keypress_draw) draw(1);
1531 int old_ibeam_letter = ibeam_letter;
1533 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1536 if(top_level->shift_down())
1539 if(highlight_letter1 == highlight_letter2)
1541 highlight_letter2 = old_ibeam_letter;
1542 highlight_letter1 = ibeam_letter;
1546 if(highlight_letter1 == old_ibeam_letter)
1548 highlight_letter1 = ibeam_letter;
1552 if(highlight_letter2 == old_ibeam_letter)
1554 highlight_letter2 = highlight_letter1;
1555 highlight_letter1 = ibeam_letter;
1559 highlight_letter1 = highlight_letter2 = ibeam_letter;
1562 if(keypress_draw) draw(1);
1569 if(highlight_letter1 == highlight_letter2) {
1570 if(ibeam_letter > 0) {
1571 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1576 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1577 highlight_letter2 = ibeam_letter = highlight_letter1;
1581 if(keypress_draw) draw(1);
1587 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1588 if(highlight_letter1 == highlight_letter2) {
1589 if(ibeam_letter < wtext_len) {
1590 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1594 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1595 highlight_letter2 = ibeam_letter = highlight_letter1;
1599 if(keypress_draw) draw(1);
1606 switch( get_keypress() ) {
1607 case 'c': case 'C': {
1608 if(highlight_letter1 != highlight_letter2) {
1609 copy_selection(SECONDARY_SELECTION);
1613 case 'v': case 'V': {
1614 paste_selection(SECONDARY_SELECTION);
1616 if(keypress_draw) draw(1);
1620 case 'x': case 'X': {
1621 if(highlight_letter1 != highlight_letter2) {
1622 copy_selection(SECONDARY_SELECTION);
1623 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1624 highlight_letter2 = ibeam_letter = highlight_letter1;
1628 if(keypress_draw) draw(1);
1632 case 'u': case 'U': {
1633 if( shift_down() ) {
1634 unicode_active = ibeam_letter;
1636 insert_text(&wkey, 1);
1638 highlight_letter1 = unicode_active;
1639 highlight_letter2 = ibeam_letter;
1648 default_keypress(dispatch_event, result);
1653 if(result) skip_cursor->update();
1654 if(dispatch_event && handle_event())
1661 int BC_TextBox::uses_text()
1667 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1670 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1671 wtext[i] = wtext[j];
1679 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1681 if( len < 0 ) len = wcslen(wcp);
1682 int wtext_len = wtext_update();
1683 if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1684 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1685 highlight_letter2 = ibeam_letter = highlight_letter1;
1686 wtext_len = wtext_update();
1690 for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1691 if( j >= wsize ) continue;
1692 wtext[j] = wtext[i];
1695 for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1696 if( i >= wsize ) break;
1699 if( (wlen+=len) > wsize ) wlen = wsize;
1700 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1701 wtext[wlen] = 0; // wtext allocated wsize+1
1705 int BC_TextBox::is_separator(const char *txt, int i)
1707 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1708 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1711 // used for time entry
1712 void BC_TextBox::do_separators(int ibeam_left)
1716 // Remove separators from text
1717 int wtext_len = wtext_update();
1718 for(int i = 0; i < wtext_len; ) {
1719 if( !iswalnum(wtext[i]) ) {
1720 for(int j = i; j < wtext_len - 1; j++)
1721 wtext[j] = wtext[j + 1];
1722 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1728 wtext[wtext_len] = 0;
1734 // Insert separators into text
1735 int separator_len = strlen(separators);
1736 for(int i = 0; i < separator_len; i++) {
1738 // Insert a separator
1739 if( is_separator(separators,i) ) {
1740 for(int j = wtext_len; j >= i; j--) {
1741 wtext[j + 1] = wtext[j];
1743 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1745 wtext[i] = separators[i];
1749 wtext[i] = separators[i];
1754 wtext[separator_len] = 0;
1755 wlen = separator_len;
1759 int BC_TextBox::get_x_position(int i, int start)
1761 return get_text_width(font, &wtext[start], i - start);
1764 void BC_TextBox::get_ibeam_position(int &x, int &y)
1766 int i, row_begin, row_end;
1767 int wtext_len = wtext_update();
1770 for( i=0; i<wtext_len; ) {
1772 for(; i<wtext_len && wtext[i]!='\n'; i++);
1775 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1776 x = get_x_position(ibeam_letter, row_begin);
1777 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1781 if( i < wtext_len && wtext[i] == '\n' ) {
1786 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1792 void BC_TextBox::set_text_row(int row)
1794 text_x = left_margin;
1795 text_y = -(row * text_height) + top_margin;
1799 int BC_TextBox::get_text_row()
1801 return -(text_y - top_margin) / text_height;
1804 void BC_TextBox::find_ibeam(int dispatch_event)
1807 int old_x = text_x, old_y = text_y;
1809 get_ibeam_position(x, y);
1811 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1813 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1814 if(text_x > left_margin) text_x = left_margin;
1817 if(left_margin + text_x + x < left_margin)
1819 text_x = -(x - (get_w() / 4)) + left_margin;
1820 if(text_x > left_margin) text_x = left_margin;
1823 int text_row = y / text_height;
1824 if( text_row < rows ) text_y = top_margin;
1826 int pix_rows = get_h() - bottom_margin - (y + text_y);
1827 if( pix_rows < text_height )
1828 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1830 pix_rows = y + text_y - top_margin;
1831 if( pix_rows < 0 ) {
1832 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1833 if( text_y > top_margin ) text_y = top_margin;
1836 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1840 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1842 int i, j, k, row_begin, row_end, result = 0, done = 0;
1843 int column1, column2;
1844 int got_visible_row = 0;
1846 // Select complete row if cursor above the window
1847 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1848 if(cursor_y < text_y - text_height)
1854 int wtext_len = wtext_update();
1856 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1857 // Simulate drawing of 1 row
1859 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1863 int first_visible_row = 0;
1864 int last_visible_row = 0;
1865 if( k+text_height > top_margin && !got_visible_row) {
1866 first_visible_row = 1;
1867 got_visible_row = 1;
1870 if( (k+text_height >= get_h() - bottom_margin ||
1871 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1872 k + text_height > 0)) )
1873 last_visible_row = 1;
1875 // Cursor is inside vertical range of row
1876 if((cursor_y >= top_margin &&
1877 cursor_y < get_h() - bottom_margin &&
1878 cursor_y >= k && cursor_y < k + text_height) ||
1879 // Cursor is above 1st row
1880 (cursor_y < k + text_height && first_visible_row) ||
1881 // Cursor is below last row
1882 (cursor_y >= k && last_visible_row))
1884 column1 = column2 = 0;
1885 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1886 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1887 if((column2 + column1) / 2 >= cursor_x) {
1890 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1891 // __LINE__, result, first_visible_row, last_visible_row);
1902 if(wtext[i] == '\n') i++;
1904 // Select complete row if last visible & cursor is below window
1905 if(last_visible_row && cursor_y > k + text_height * 2)
1908 if(i >= wtext_len && !done) {
1914 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
1915 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
1916 if(result < 0) result = 0;
1917 if(result > wtext_len) {
1918 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
1926 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
1928 int i, j, k, row_begin, row_end, result = 0, done = 0;
1929 int column1, column2;
1930 int wtext_len = wtext_update();
1932 if(cursor_y < text_y) {
1937 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
1939 for(; wtext[i] != '\n' && i < wtext_len; i++);
1942 if(cursor_y >= k && cursor_y < k + text_height) {
1943 column1 = column2 = 0;
1944 for(j = 0; j <= row_end - row_begin && !done; j++) {
1945 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
1946 if((column2 + column1) / 2 >= cursor_x) {
1947 result = row_begin + j - 1;
1958 if(wtext[i] == '\n') i++;
1960 if(i >= wtext_len && !done) {
1964 if(result < 0) result = 0;
1965 if(result > wtext_len) result = wtext_len;
1970 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
1972 int wtext_len = wtext_update();
1973 letter1 = letter2 = ibeam_letter;
1974 if( letter1 < 0 ) letter1 = 0;
1975 if( letter2 < 0 ) letter2 = 0;
1976 if( letter1 > wtext_len ) letter1 = wtext_len;
1977 if( letter2 > wtext_len ) letter2 = wtext_len;
1978 if( !wtext_len ) return;
1979 for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
1980 for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
1981 if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
1985 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
1987 int wtext_len = wtext_update();
1988 letter1 = letter2 = ibeam_letter;
1989 if( letter1 < 0 ) letter1 = 0;
1990 if( letter2 < 0 ) letter2 = 0;
1991 if( letter1 > wtext_len ) letter1 = wtext_len;
1992 if( letter2 > wtext_len ) letter2 = wtext_len;
1993 if( !wtext_len ) return;
1994 for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
1995 for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
1998 void BC_TextBox::copy_selection(int clipboard_num)
2000 int wtext_len = wtext_update();
2001 if(!wtext_len) return;
2003 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2004 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2005 highlight_letter2 - highlight_letter1 <= 0) return;
2006 int clip_len = highlight_letter2 - highlight_letter1;
2007 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2008 char ctext[4*clip_len+4];
2009 clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2010 get_clipboard()->to_clipboard(ctext, clip_len, clipboard_num);
2014 void BC_TextBox::paste_selection(int clipboard_num)
2016 int len = get_clipboard()->clipboard_len(clipboard_num);
2019 char cstring[len]; wchar_t wstring[len];
2020 get_clipboard()->from_clipboard(cstring, len, clipboard_num); --len;
2021 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2022 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2023 cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2024 insert_text(wstring, len);
2028 void BC_TextBox::set_keypress_draw(int value)
2030 keypress_draw = value;
2033 int BC_TextBox::get_last_keypress()
2035 return last_keypress;
2038 int BC_TextBox::get_ibeam_letter()
2040 return ibeam_letter;
2043 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2045 this->ibeam_letter = number;
2052 void BC_TextBox::set_separators(const char *separators)
2054 this->separators = (char*)separators;
2057 int BC_TextBox::get_rows()
2068 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y)
2069 : BC_ListBox(x, y, text_box->get_w(), 200, LISTBOX_TEXT,
2070 text_box->suggestions, 0, 0, 1, 0, 1)
2072 this->text_box = text_box;
2074 set_justify(LISTBOX_LEFT);
2077 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2081 int BC_TextBoxSuggestions::handle_event()
2083 char *current_suggestion = 0;
2084 BC_ListBoxItem *item = get_selection(0, 0);
2085 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2086 if(item && (current_suggestion=item->get_text()) != 0)
2088 int col = text_box->suggestion_column;
2089 int len = BCTEXTLEN-1 - col;
2090 char *cp = &text_box->text[col];
2091 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2092 strncpy(cp, current_suggestion, len);
2093 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2094 if( (col=strlen(current_suggestion)) >= len )
2096 text_box->dirty = 1;
2100 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2101 text_box->highlight_letter1 =
2102 text_box->highlight_letter2 =
2103 text_box->ibeam_letter = text_box->tstrlen();
2104 text_box->wtext_update();
2106 text_box->handle_event();
2107 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2112 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2113 int x, int y, int w, int rows,
2114 const char *default_text, int default_size)
2116 this->parent_window = parent_window;
2121 xscroll = 0; yscroll = 0;
2122 this->default_text = default_text;
2123 this->default_wtext = 0;
2124 this->default_size = default_size;
2127 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2128 int x, int y, int w, int rows,
2129 const wchar_t *default_wtext, int default_size)
2131 this->parent_window = parent_window;
2136 xscroll = 0; yscroll = 0;
2137 this->default_text = 0;
2138 this->default_wtext = default_wtext;
2139 this->default_size = default_size;
2142 BC_ScrollTextBox::~BC_ScrollTextBox()
2152 void BC_ScrollTextBox::create_objects()
2154 // Must be created first
2155 parent_window->add_subwindow(text = default_wtext ?
2156 new BC_ScrollTextBoxText(this, default_wtext) :
2157 new BC_ScrollTextBoxText(this, default_text));
2161 void BC_ScrollTextBox::set_text(char *text, int isz)
2163 this->text->set_text(text, isz);
2164 update_scrollbars();
2167 int BC_ScrollTextBox::set_text_row(int n)
2169 text->set_text_row(n);
2170 update_scrollbars();
2174 void BC_ScrollTextBox::update(const char *text)
2176 this->text->update(text);
2177 update_scrollbars();
2180 void BC_ScrollTextBox::update(const wchar_t *wtext)
2182 this->text->update(wtext);
2183 update_scrollbars();
2186 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2192 update_scrollbars();
2195 int BC_ScrollTextBox::button_press_event()
2197 return text->BC_TextBox::button_press_event();
2199 int BC_ScrollTextBox::button_release_event()
2201 return text->BC_TextBox::button_release_event();
2204 int BC_ScrollTextBox::get_h() { return text->get_h(); }
2205 const char *BC_ScrollTextBox::get_text() { return text->get_text(); }
2206 const wchar_t *BC_ScrollTextBox::get_wtext() { return text->get_wtext(); }
2208 int BC_ScrollTextBox::get_buttonpress()
2210 return text->BC_TextBox::get_buttonpress();
2212 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2214 text->wset_selection(char1, char2, ibeam);
2216 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2218 text->set_selection(char1, char2, ibeam);
2220 int BC_ScrollTextBox::get_ibeam_letter()
2222 return text->get_ibeam_letter();
2224 int BC_ScrollTextBox::get_x_pos()
2226 return text->left_margin - text->get_text_x();
2228 void BC_ScrollTextBox::set_x_pos(int x)
2230 text->set_text_x(text->left_margin - x);
2234 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2235 : BC_TextBox(gui->x, gui->y,
2236 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2237 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2242 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2243 : BC_TextBox(gui->x, gui->y,
2244 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2245 gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2250 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2259 void BC_ScrollTextBox::update_scrollbars()
2261 int view_w = w, view_rows = rows;
2262 int view_h = BC_TextBox::calculate_row_h(view_rows, parent_window);
2263 int text_rows = text->get_text_rows();
2264 int text_width = parent_window->get_text_width(text->font, text->get_wtext());
2265 BC_Resources *resources = parent_window->get_resources();
2266 int need_xscroll = 0, need_yscroll = 0;
2268 // Create scrollbars as needed
2272 if( !need_xscroll && (text->get_text_x() != text->left_margin ||
2273 text_width >= view_w - text->left_margin - text->right_margin) ) {
2274 resize = need_xscroll = 1;
2275 view_h -= resources->hscroll_data[SCROLL_HANDLE_UP]->get_h();
2276 view_rows = BC_TextBox::pixels_to_rows(parent_window, text->font, view_h);
2278 if( !need_yscroll && (text->get_text_y() != text->top_margin ||
2279 text_rows > view_rows) ) {
2280 resize = need_yscroll = 1;
2281 view_w -= resources->vscroll_data[SCROLL_HANDLE_UP]->get_w();
2285 if( !need_xscroll && xscroll ) {
2287 delete xscroll; xscroll = 0;
2289 if( !need_yscroll && yscroll ) {
2291 delete yscroll; yscroll = 0;
2294 if( view_rows != text->get_rows() || view_w != text->get_w() ) {
2295 text->reposition_window(x, y, view_w, view_rows);
2297 if( need_xscroll && !xscroll ) {
2298 xscroll = new BC_ScrollTextBoxXScroll(this);
2299 parent_window->add_subwindow(xscroll);
2300 text->xscroll = xscroll;
2301 xscroll->bound_to = text;
2302 xscroll->show_window();
2304 if( need_yscroll && !yscroll ) {
2305 yscroll = new BC_ScrollTextBoxYScroll(this);
2306 parent_window->add_subwindow(yscroll);
2307 text->yscroll = yscroll;
2308 yscroll->bound_to = text;
2309 yscroll->show_window();
2312 xscroll->reposition_window(x, y + text->get_h(), view_w);
2313 int xpos = get_x_pos();
2314 if( xpos != xscroll->get_value() )
2315 xscroll->update_value(xpos);
2316 int xlength = text_width + view_w/4;
2317 if( xlength != xscroll->get_length() ||
2318 view_w != xscroll->get_handlelength() )
2319 xscroll->update_length(xlength, xpos, view_w, 0);
2322 yscroll->reposition_window(x + w - yscroll->get_span(), y, text->get_h());
2323 int text_row = text->get_text_row();
2324 if( text_row != yscroll->get_value() )
2325 yscroll->update_value(text_row);
2326 if( text_rows != yscroll->get_length() ||
2327 view_rows != yscroll->get_handlelength() )
2328 yscroll->update_length(text_rows, text_row, view_rows, 0);
2332 int BC_ScrollTextBoxText::handle_event()
2334 gui->update_scrollbars();
2335 return gui->handle_event();
2338 int BC_ScrollTextBoxText::motion_event()
2340 gui->update_scrollbars();
2344 BC_ScrollTextBoxXScroll::BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui)
2345 : BC_ScrollBar(gui->x, gui->y + gui->text->get_h(), SCROLL_HORIZ, gui->text->get_w(),
2346 gui->text->get_text_width(MEDIUMFONT, gui->get_wtext()), 0, gui->w)
2351 BC_ScrollTextBoxXScroll::~BC_ScrollTextBoxXScroll()
2355 int BC_ScrollTextBoxXScroll::handle_event()
2357 gui->set_x_pos(get_position());
2361 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2362 : BC_ScrollBar(gui->x + gui->text->get_w(), gui->y, SCROLL_VERT, gui->text->get_h(),
2363 gui->text->get_text_rows(), 0, gui->rows)
2368 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2372 int BC_ScrollTextBoxYScroll::handle_event()
2374 gui->text->set_text_row(get_position());
2380 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2381 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2383 this->popup = popup;
2386 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2387 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2389 this->popup = popup;
2392 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2402 int BC_PopupTextBoxText::handle_event()
2404 popup->handle_event();
2408 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2410 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2411 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2413 this->popup = popup;
2415 int BC_PopupTextBoxList::handle_event()
2417 BC_ListBoxItem *item = get_selection(0, 0);
2420 popup->textbox->update(item->get_text());
2421 popup->textbox->set_text_row(0);
2422 popup->handle_event();
2430 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2431 ArrayList<BC_ListBoxItem*> *list_items,
2432 const char *default_text, int x, int y,
2433 int text_w, int list_h, int list_format)
2437 this->list_h = list_h;
2438 this->list_format = list_format;
2439 this->default_text = (char*)default_text;
2440 this->default_wtext = 0;
2441 this->text_w = text_w;
2442 this->parent_window = parent_window;
2443 this->list_items = list_items;
2446 BC_PopupTextBox::~BC_PopupTextBox()
2456 int BC_PopupTextBox::create_objects()
2458 int x = this->x, y = this->y;
2459 parent_window->add_subwindow(textbox = default_wtext ?
2460 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2461 new BC_PopupTextBoxText(this, x, y, default_text));
2462 x += textbox->get_w();
2463 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2467 void BC_PopupTextBox::update(const char *text)
2469 textbox->update(text);
2470 textbox->set_text_row(0);
2473 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2475 listbox->update(data, 0, 0, 1);
2479 const char* BC_PopupTextBox::get_text()
2481 return textbox->get_text();
2484 const wchar_t* BC_PopupTextBox::get_wtext()
2486 return textbox->get_wtext();
2489 int BC_PopupTextBox::get_number()
2491 return listbox->get_selection_number(0, 0);
2494 int BC_PopupTextBox::get_x()
2499 int BC_PopupTextBox::get_y()
2504 int BC_PopupTextBox::get_w()
2506 return textbox->get_w() + listbox->get_w();
2509 int BC_PopupTextBox::get_h()
2511 return textbox->get_h();
2514 int BC_PopupTextBox::get_show_query()
2516 return listbox->get_show_query();
2519 void BC_PopupTextBox::set_show_query(int v)
2521 listbox->set_show_query(v);
2524 int BC_PopupTextBox::handle_event()
2529 void BC_PopupTextBox::reposition_window(int x, int y)
2534 textbox->reposition_window(x1,
2537 textbox->get_rows());
2538 x1 += textbox->get_w();
2539 listbox->reposition_window(x1, y1, -1, -1, 0);
2540 // if(flush) parent_window->flush();
2556 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2557 int64_t default_value, int x, int y)
2558 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2560 this->popup = popup;
2563 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2564 float default_value, int x, int y, int precision)
2565 : BC_TextBox(x, y, popup->text_w, 1, default_value, 1, MEDIUMFONT, precision)
2567 this->popup = popup;
2570 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2582 int BC_TumbleTextBoxText::handle_event()
2584 popup->handle_event();
2588 int BC_TumbleTextBoxText::button_press_event()
2592 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
2594 if(get_buttonpress() == 4)
2596 popup->tumbler->handle_up_event();
2599 if(get_buttonpress() == 5)
2601 popup->tumbler->handle_down_event();
2611 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2612 int64_t default_value,
2624 this->default_value = default_value;
2625 this->text_w = text_w;
2626 this->parent_window = parent_window;
2632 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2645 this->default_value = default_value;
2646 this->text_w = text_w;
2647 this->parent_window = parent_window;
2653 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2654 float default_value_f,
2664 this->min_f = min_f;
2665 this->max_f = max_f;
2666 this->default_value_f = default_value_f;
2667 this->text_w = text_w;
2668 this->parent_window = parent_window;
2674 BC_TumbleTextBox::~BC_TumbleTextBox()
2676 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2677 // is deleted anyway by the windowbase so textbox deletes this.
2678 if(tumbler) delete tumbler;
2680 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2689 void BC_TumbleTextBox::reset()
2696 void BC_TumbleTextBox::set_precision(int precision)
2698 this->precision = precision;
2701 void BC_TumbleTextBox::set_increment(float value)
2703 this->increment = value;
2704 if(tumbler) tumbler->set_increment(value);
2707 void BC_TumbleTextBox::set_log_floatincrement(int value)
2709 this->log_floatincrement = value;
2710 if(tumbler) tumbler->set_log_floatincrement(value);
2713 int BC_TumbleTextBox::create_objects()
2715 int x = this->x, y = this->y;
2717 textbox = use_float ?
2718 new BC_TumbleTextBoxText(this, default_value_f, x, y, precision) :
2719 new BC_TumbleTextBoxText(this, default_value, x, y);
2721 parent_window->add_subwindow(textbox);
2722 x += textbox->get_w();
2724 tumbler = use_float ?
2725 (BC_Tumbler *)new BC_FTumbler(textbox, min_f, max_f, x, y) :
2726 (BC_Tumbler *)new BC_ITumbler(textbox, min, max, x, y);
2727 parent_window->add_subwindow(tumbler);
2728 tumbler->set_increment(increment);
2732 const char* BC_TumbleTextBox::get_text()
2734 return textbox->get_text();
2737 const wchar_t* BC_TumbleTextBox::get_wtext()
2739 return textbox->get_wtext();
2742 BC_TextBox* BC_TumbleTextBox::get_textbox()
2747 int BC_TumbleTextBox::update(const char *value)
2749 textbox->update(value);
2750 textbox->set_text_row(0);
2754 int BC_TumbleTextBox::update(int64_t value)
2756 textbox->update(value);
2757 textbox->set_text_row(0);
2761 int BC_TumbleTextBox::update(float value)
2763 textbox->update(value);
2764 textbox->set_text_row(0);
2769 int BC_TumbleTextBox::get_x()
2774 int BC_TumbleTextBox::get_y()
2779 int BC_TumbleTextBox::get_w()
2781 return textbox->get_w() + tumbler->get_w();
2784 int BC_TumbleTextBox::get_h()
2786 return textbox->get_h();
2789 void BC_TumbleTextBox::disable(int hide_text)
2791 if( hide_text && !textbox->is_hidden() )
2792 textbox->hide_window(0);
2793 if( !tumbler->is_hidden() )
2794 tumbler->hide_window(0);
2795 if( !get_enabled() ) return;
2796 return textbox->disable();
2799 void BC_TumbleTextBox::enable()
2801 if( textbox->is_hidden() )
2802 textbox->show_window(0);
2803 if( tumbler->is_hidden() )
2804 tumbler->show_window(0);
2805 if( get_enabled() ) return;
2806 return textbox->enable();
2809 int BC_TumbleTextBox::get_enabled()
2811 return textbox->get_enabled();
2814 int BC_TumbleTextBox::handle_event()
2819 void BC_TumbleTextBox::reposition_window(int x, int y)
2824 textbox->reposition_window(x,
2828 tumbler->reposition_window(x + textbox->get_w(),
2830 // if(flush) parent_window->flush();
2834 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2836 tumbler->set_boundaries(min, max);
2839 void BC_TumbleTextBox::set_boundaries(float min, float max)
2841 tumbler->set_boundaries(min, max);
2844 void BC_TumbleTextBox::set_tooltip(const char *text)
2846 textbox->set_tooltip(text);