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"
42 #define VERTICAL_MARGIN 2
43 #define VERTICAL_MARGIN_NOBORDER 0
44 #define HORIZONTAL_MARGIN 4
45 #define HORIZONTAL_MARGIN_NOBORDER 2
47 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
48 int size, char *text, int has_border, int font)
49 : BC_SubWindow(x, y, w, 0, -1)
53 reset_parameters(rows, has_border, font, size);
56 else // text referenced directly
60 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
61 int size, wchar_t *wtext, int has_border, int font)
62 : BC_SubWindow(x, y, w, 0, -1)
66 wsize = size > 0 ? size : wcslen(wtext);
67 if( size <= 0 ) size = 2*wsize;
68 reset_parameters(rows, has_border, font, size);
69 this->wtext = new wchar_t[wsize+1];
70 wcsncpy(this->wtext, wtext, wsize);
71 this->wtext[wsize] = 0;
74 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
75 const char *text, int has_border, int font, int is_utf8)
76 : BC_SubWindow(x, y, w, 0, -1)
78 this->is_utf8 = is_utf8;
80 reset_parameters(rows, has_border, font, BCTEXTLEN);
84 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
85 const wchar_t *wtext, int has_border, int font, int is_utf8)
86 : BC_SubWindow(x, y, w, 0, -1)
88 this->is_utf8 = is_utf8;
90 reset_parameters(rows, has_border, font, BCTEXTLEN);
92 wtext = new wchar_t[wsize+1];
93 wcsncpy(this->wtext, wtext, wsize);
94 this->wtext[wsize] = 0;
97 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
98 int64_t text, int has_border, int font)
99 : BC_SubWindow(x, y, w, 0, -1)
103 reset_parameters(rows, has_border, font, BCSTRLEN);
104 snprintf(this->text, this->tsize, "%jd", text);
105 dirty = 1; wtext_update();
108 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
109 float text, int has_border, int font, int precision)
110 : BC_SubWindow(x, y, w, 0, -1)
114 reset_parameters(rows, has_border, font, BCSTRLEN);
115 this->precision = precision;
116 snprintf(this->text, this->tsize, "%0.*f", precision, text);
117 dirty = 1; wtext_update();
120 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
121 int text, int has_border, int font)
122 : BC_SubWindow(x, y, w, 0, -1)
126 reset_parameters(rows, has_border, font, BCSTRLEN);
127 snprintf(this->text, this->tsize, "%d", text);
128 dirty = 1; wtext_update();
131 BC_TextBox::~BC_TextBox()
133 if(skip_cursor) delete skip_cursor;
134 delete suggestions_popup;
135 suggestions->remove_all_objects();
143 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
145 suggestions = new ArrayList<BC_ListBoxItem*>;
146 suggestions_popup = 0;
147 suggestion_column = 0;
150 this->has_border = has_border;
153 this->tsize = size >= 0 ? size : -size;
154 this->text = size > 0 ? new char[size+1] : 0;
155 if( this->text ) this->text[0] = 0;
158 highlight_letter1 = highlight_letter2 = 0;
159 highlight_letter3 = highlight_letter4 = 0;
173 skip_cursor = new Timer;
184 selection_active = 0;
188 int BC_TextBox::tstrlen()
190 if( !tsize ) return strlen(text);
191 return strnlen(text, tsize);
194 int BC_TextBox::tstrcmp(const char *cp)
196 const char *tp = get_text();
197 if( !tsize ) return strcmp(tp, cp);
198 return strncmp(tp, cp, tsize);
201 char *BC_TextBox::tstrcpy(const char *cp)
206 return strcpy(text, cp);
207 strncpy(text, cp, tsize);
215 char *BC_TextBox::tstrcat(const char *cp)
218 if( !tsize ) return strcat(text, cp);
219 char *result = strncat(text, cp, tsize);
224 int BC_TextBox::wtext_update()
227 const char *src_enc = is_utf8 ? "UTF8" : BC_Resources::encoding;
228 const char *dst_enc = BC_Resources::wide_encoding;
229 int nsize = tsize > 0 ? tsize : strlen(text) + BCTEXTLEN;
230 if( nsize > wsize || !wtext ) {
231 wchar_t *ntext = new wchar_t[nsize+1];
232 memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
233 delete [] wtext; wtext = ntext; wsize = nsize;
235 wlen = BC_Resources::encode(src_enc, dst_enc, text, strlen(text),
236 (char*)wtext, wsize*sizeof(wchar_t)) / sizeof(wchar_t);
243 int BC_TextBox::text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz)
245 const char *src_enc = BC_Resources::wide_encoding;
246 const char *dst_enc = BC_Resources::encoding;
247 if( wsz < 0 ) wsz = wcslen(wcp);
248 int len = BC_Resources::encode(src_enc, dst_enc,
249 (char*)wcp, wsz*sizeof(wchar_t), tcp, tsz);
254 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 if( back_color < 0 ) back_color = resources->text_background;
287 if( high_color < 0 ) high_color = resources->text_background_hi;
291 if( back_color < 0 ) back_color = bg_color;
292 if( high_color < 0 ) high_color = resources->text_background_noborder_hi;
296 set_cursor(IBEAM_CURSOR, 0, 0);
299 add_subwindow(menu = new BC_TextMenu(this));
300 menu->create_objects();
305 int BC_TextBox::calculate_h(BC_WindowBase *gui,
310 return rows * (gui->get_text_ascent(font) + 1 +
311 gui->get_text_descent(font) + 1) +
312 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
315 void BC_TextBox::set_precision(int precision)
317 this->precision = precision;
320 // Compute suggestions for a path
321 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries, const char *filter)
323 // Let user delete suggestion
324 if(get_last_keypress() != BACKSPACE) {
325 // Compute suggestions
327 ArrayList<char*> suggestions;
328 const char *current_text = get_text();
329 int suggestion_column = 0;
330 char dirname[BCTEXTLEN]; dirname[0] = 0;
331 if( current_text[0] == '/' || current_text[0] == '~' )
332 strncpy(dirname, current_text, sizeof(dirname));
334 getcwd(dirname, sizeof(dirname));
335 // If directory, tabulate it
337 if( filter ) fs.set_filter(filter);
339 strncpy(dirname, current_text, sizeof(dirname));
340 if( (cp=strrchr(dirname, '/')) ||
341 (cp=strrchr(dirname, '~')) ) *++cp = 0;
342 fs.parse_tildas(dirname);
344 cp = (char *)current_text;
345 if( (prefix=strrchr(cp, '/')) ||
346 (prefix=strrchr(cp, '~')) ) ++prefix;
347 suggestion_column = !prefix ? 0 : prefix - cp;
348 int prefix_len = prefix ? strlen(prefix) : 0;
349 // only include items where the filename matches the basename prefix
350 for(int i = 0; i < fs.total_files(); i++) {
351 char *current_name = fs.get_entry(i)->name;
352 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
353 suggestions.append(current_name);
357 char *prefix = (char *)current_text;
358 int prefix_len = strlen(prefix);
359 for(int i = 0; i < entries->size(); i++) {
360 char *current_name = entries->get(i)->get_text();
361 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
362 suggestions.append(current_name);
365 set_suggestions(&suggestions, suggestion_column);
371 void BC_TextBox::no_suggestions()
373 if( suggestions_popup ) {
374 delete suggestions_popup;
375 suggestions_popup = 0;
380 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
383 this->suggestions->remove_all_objects();
384 this->suggestion_column = column;
387 for(int i = 0; i < suggestions->size(); i++) {
388 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
391 // Show the popup without taking focus
392 if( suggestions->size() > 1 ) {
393 if( !suggestions_popup ) {
394 suggestions_popup = new BC_TextBoxSuggestions(this, x, y);
395 get_parent()->add_subwindow(suggestions_popup);
396 suggestions_popup->set_is_suggestions(1);
399 suggestions_popup->update(this->suggestions, 0, 0, 1);
401 suggestions_popup->activate(0);
404 // Show the highlighted text
405 if( suggestions->size() == 1 ) {
406 highlight_letter1 = wtext_update();
407 int len = text_update(wtext,wlen, text,tsize);
408 char *current_suggestion = suggestions->get(0);
409 int col = len - suggestion_column;
410 if( col < 0 ) col = 0;
411 char *cur = current_suggestion + col;
413 highlight_letter2 = wtext_update();
414 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
422 if( !suggestions || !this->suggestions->size() )
426 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
428 highlight_letter1 = char1;
429 highlight_letter2 = char2;
430 ibeam_letter = ibeam;
434 // count utf8 chars in text which occur before cp
435 int BC_TextBox::wcpos(const char *text, const char *cp)
438 const unsigned char *bp = (const unsigned char *)text;
439 const unsigned char *ep = (const unsigned char *)cp;
440 while( bp < ep && *bp != 0 ) {
443 if( ch < 0x80 ) continue;
445 int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
446 for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
451 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
453 const char *cp = get_text();
454 wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
457 int BC_TextBox::update(const char *text)
459 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
460 // Don't update if contents are the same
461 int bg_color = has_border || !highlighted ? back_color : high_color;
462 if( bg_color == background_color && !tstrcmp(text)) return 0;
464 int wtext_len = wtext_update();
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(const wchar_t *wtext)
474 int wtext_len = wcslen(wtext);
475 if( wtext_len >= wsize ) wtext_len = wsize;
476 wcsncpy(this->wtext, wtext, wtext_len);
478 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
479 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
480 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
485 int BC_TextBox::update(int64_t value)
487 char string[BCTEXTLEN];
488 sprintf(string, "%jd", value);
493 int BC_TextBox::update(float value)
495 char string[BCTEXTLEN];
496 sprintf(string, "%0.*f", precision, value);
501 void BC_TextBox::disable()
510 void BC_TextBox::enable()
520 int BC_TextBox::get_enabled() { return enabled; }
521 int BC_TextBox::get_text_x() { return text_x; }
522 int BC_TextBox::get_text_y() { return text_y; }
523 void BC_TextBox::set_text_x(int v) { text_x = v; }
524 void BC_TextBox::set_text_y(int v) { text_y = v; }
525 int BC_TextBox::get_back_color() { return back_color; }
526 void BC_TextBox::set_back_color(int v) { back_color = v; }
528 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
530 return (pixels - 4) /
531 (window->get_text_ascent(font) + 1 +
532 window->get_text_descent(font) + 1);
535 int BC_TextBox::calculate_row_h(int rows,
536 BC_WindowBase *parent_window,
541 (parent_window->get_text_ascent(font) + 1 +
542 parent_window->get_text_descent(font) + 1) +
543 (has_border ? 4 : 0);
546 const char* BC_TextBox::get_text()
548 int wtext_len = wtext_update();
549 text_update(wtext,wtext_len, text,tsize);
553 const wchar_t* BC_TextBox::get_wtext()
559 void BC_TextBox::set_text(char *text, int isz)
561 if( size > 0 || isz < 0 ) return;
570 int BC_TextBox::get_text_rows()
572 int wtext_len = wtext_update();
574 for(int i = 0; i < wtext_len; i++) {
575 if(wtext[i] == '\n') result++;
581 int BC_TextBox::get_row_h(int rows)
583 return rows * text_height + top_margin + bottom_margin;
586 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
589 if(w < 0) w = get_w();
592 new_h = get_row_h(rows);
601 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
602 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
603 BC_WindowBase::reposition_window(x, y, w, new_h);
609 void BC_TextBox::draw_border()
611 BC_Resources *resources = get_resources();
613 set_color(background_color);
614 draw_box(0, 0, left_margin, get_h());
615 draw_box(get_w() - right_margin, 0, right_margin, get_h());
620 draw_3d_border(0, 0, w, h,
621 resources->text_border1,
622 resources->text_border2_hi,
623 resources->text_border3_hi,
624 resources->text_border4);
626 draw_3d_border(0, 0, w, h,
627 resources->text_border1,
628 resources->text_border2,
629 resources->text_border3,
630 resources->text_border4);
634 void BC_TextBox::draw_cursor()
636 // set_color(background_color);
644 draw_box(ibeam_x + text_x,
653 void BC_TextBox::draw(int flush)
656 int row_begin, row_end;
657 int highlight_x1, highlight_x2;
659 BC_Resources *resources = get_resources();
661 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
663 background_color = has_border || !highlighted ? back_color : high_color;
664 set_color(background_color);
665 draw_box(0, 0, w, h);
667 int wtext_len = wtext_update();
669 // Draw text with selection
672 for(i=0, j=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
675 wchar_t *wtext_row = &wtext[i];
676 for(j=0; j<BCTEXTLEN-1 && i<wtext_len && wtext[i]!='\n'; ++i, ++j);
677 if( (row_end=i) < wtext_len ) ++i;
679 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
680 // Draw highlighted region of row
681 if( highlight_letter2 > highlight_letter1 &&
682 highlight_letter2 > row_begin &&
683 highlight_letter1 <= row_end ) {
684 int color = active && enabled && get_has_focus() ?
685 resources->text_highlight :
687 resources->text_selected_highlight :
688 resources->text_inactive_highlight;
689 if( unicode_active >= 0 )
692 if(highlight_letter1 >= row_begin &&
693 highlight_letter1 <= row_end)
694 highlight_x1 = get_x_position(highlight_letter1, row_begin);
698 if(highlight_letter2 > row_begin &&
699 highlight_letter2 <= row_end)
700 highlight_x2 = get_x_position(highlight_letter2, row_begin);
702 highlight_x2 = get_w();
704 draw_box(highlight_x1 + text_x, k,
705 highlight_x2 - highlight_x1, text_height);
708 // Draw text over highlight
709 int len = row_end - row_begin;
711 set_color(enabled ? resources->text_default : DMGREY);
712 draw_single_text(1, font, text_x, k + text_ascent, wtext_row, len);
715 // Get ibeam location
716 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
718 ibeam_y = k - text_y;
719 ibeam_x = get_x_position(ibeam_letter, row_begin);
724 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
726 // ibeam_x = ibeam_y = !wtext_len ? 0 : -1;
727 ibeam_x = 0; ibeam_y = k - text_y;
730 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
740 int BC_TextBox::focus_in_event()
746 int BC_TextBox::focus_out_event()
752 int BC_TextBox::cursor_enter_event()
754 if( top_level->event_win == win && enabled &&
755 !(top_level->get_resources()->textbox_focus_policy & CLICK_ACTIVATE) )
758 top_level->deactivate();
771 int BC_TextBox::cursor_leave_event()
780 if( !suggestions_popup && !get_button_down() &&
781 !(top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
786 int BC_TextBox::button_press_event()
790 if(!enabled) return 0;
791 // if(get_buttonpress() != WHEEL_UP &&
792 // get_buttonpress() != WHEEL_DOWN &&
793 // get_buttonpress() != LEFT_BUTTON &&
794 // get_buttonpress() != MIDDLE_BUTTON) return 0;
796 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
798 int cursor_letter = 0;
799 int wtext_len = wtext_update();
800 int update_scroll = 0;
803 if(top_level->event_win == win)
808 top_level->deactivate();
813 if(get_buttonpress() == WHEEL_UP)
815 text_y += text_height;
816 text_y = MIN(text_y, top_margin);
820 if(get_buttonpress() == WHEEL_DOWN)
822 int min_y = -(get_text_rows() *
826 text_y -= text_height;
827 text_y = MAX(text_y, min_y);
828 text_y = MIN(text_y, top_margin);
832 if(get_buttonpress() == RIGHT_BUTTON)
834 menu->activate_menu();
839 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
841 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
844 if(get_triple_click())
846 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
848 select_line(highlight_letter1, highlight_letter2, cursor_letter);
849 highlight_letter3 = highlight_letter1;
850 highlight_letter4 = highlight_letter2;
851 ibeam_letter = highlight_letter2;
852 copy_selection(PRIMARY_SELECTION);
855 if(get_double_click())
858 select_word(highlight_letter1, highlight_letter2, cursor_letter);
859 highlight_letter3 = highlight_letter1;
860 highlight_letter4 = highlight_letter2;
861 ibeam_letter = highlight_letter2;
862 copy_selection(PRIMARY_SELECTION);
865 if(get_buttonpress() == MIDDLE_BUTTON)
867 highlight_letter3 = highlight_letter4 =
868 ibeam_letter = highlight_letter1 =
869 highlight_letter2 = cursor_letter;
870 paste_selection(PRIMARY_SELECTION);
875 highlight_letter3 = highlight_letter4 =
876 ibeam_letter = highlight_letter1 =
877 highlight_letter2 = cursor_letter;
881 // Handle scrolling by highlighting text
882 if(text_selected || word_selected || line_selected)
884 set_repeat(top_level->get_resources()->scroll_repeat);
887 if(ibeam_letter < 0) ibeam_letter = 0;
888 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
892 if(update_scroll && yscroll)
894 yscroll->update_length(get_text_rows(),
896 yscroll->get_handlelength(),
904 if( suggestions_popup && (!yscroll || !yscroll->is_event_win())) {
905 if( suggestions_popup->button_press_event() )
906 return suggestions_popup->handle_event();
908 else if( (top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
915 int BC_TextBox::button_release_event()
920 if(text_selected || word_selected || line_selected)
926 // Stop scrolling by highlighting text
927 unset_repeat(top_level->get_resources()->scroll_repeat);
933 int BC_TextBox::cursor_motion_event()
935 int cursor_letter, letter1, letter2;
938 if(text_selected || word_selected || line_selected)
940 cursor_letter = get_cursor_letter(top_level->cursor_x,
941 top_level->cursor_y);
943 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
947 select_line(letter1, letter2, cursor_letter);
952 select_word(letter1, letter2, cursor_letter);
957 letter1 = letter2 = cursor_letter;
960 if(letter1 <= highlight_letter3)
962 highlight_letter1 = letter1;
963 highlight_letter2 = highlight_letter4;
964 ibeam_letter = letter1;
967 if(letter2 >= highlight_letter4)
969 highlight_letter2 = letter2;
970 highlight_letter1 = highlight_letter3;
971 ibeam_letter = letter2;
974 copy_selection(PRIMARY_SELECTION);
987 int BC_TextBox::activate()
991 top_level->set_active_subwindow(this);
992 top_level->set_repeat(top_level->get_resources()->blink_rate);
998 int BC_TextBox::deactivate()
1002 text_selected = word_selected = line_selected = 0;
1003 top_level->set_active_subwindow(0);
1004 top_level->unset_repeat(top_level->get_resources()->blink_rate);
1010 int BC_TextBox::repeat_event(int64_t duration)
1013 int cursor_y = get_cursor_y();
1014 //int cursor_x = get_cursor_x();
1016 if(duration == top_level->get_resources()->tooltip_delay &&
1017 tooltip_text && tooltip_text[0] != 0 && highlighted)
1023 if(duration == top_level->get_resources()->blink_rate &&
1027 // don't flash if keypress
1028 if(skip_cursor->get_difference() < 500)
1030 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1031 // skip_cursor->get_difference(),
1037 if(!(text_selected || word_selected || line_selected))
1046 if(duration == top_level->get_resources()->scroll_repeat &&
1047 (text_selected || word_selected || line_selected))
1050 if(get_cursor_y() < top_margin)
1052 difference = get_cursor_y() - top_margin ;
1055 if(get_cursor_y() > get_h() - bottom_margin)
1057 difference = get_cursor_y() -
1058 (get_h() - bottom_margin);
1060 if(difference != 0) {
1061 int min_y = -(get_text_rows() * text_height -
1062 get_h() + bottom_margin);
1064 text_y -= difference;
1065 // printf("BC_TextBox::repeat_event %d %d %d\n",
1069 text_y = MAX(min_y, text_y);
1070 text_y = MIN(text_y, top_margin);
1077 if(get_cursor_x() < left_margin)
1079 int difference = left_margin - get_cursor_x();
1081 text_x += difference;
1082 text_x = MIN(text_x, left_margin);
1086 else if(get_cursor_x() > get_w() - right_margin)
1088 int difference = get_cursor_x() - (get_w() - right_margin);
1089 int new_text_x = text_x - difference;
1091 // Get width of current row
1094 int wtext_len = wtext_update();
1097 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1100 while(wtext[i] != '\n' && i < wtext_len) {
1104 if(wtext[i] == '\n') i++;
1106 if(cursor_y >= k && cursor_y < k + text_height) {
1107 row_width = get_text_width(font,
1109 row_end - row_begin);
1115 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1116 new_text_x = MAX(new_text_x, min_x);
1117 new_text_x = MIN(new_text_x, left_margin);
1119 if(new_text_x < text_x) text_x = new_text_x;
1128 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1130 int key = top_level->get_keypress(), len;
1131 if( (key == RETURN) || ( key >= 32 && key <= 255 ) ||
1132 (key >= KP1 && key <= KP9) || key == KPINS ) {
1133 wchar_t *wkeys = top_level->get_wkeystring(&len);
1135 case RETURN: key = '\n'; goto kpchr;
1136 case KPINS: key = '0'; goto kpchr;
1137 case KP1: case KP2: case KP3: case KP4: case KP5:
1138 case KP6: case KP7: case KP8: case KP9:
1139 key = key - KP1 + '1';
1141 wkeys[0] = key; wkeys[1] = 0; len = 1;
1144 insert_text(wkeys, len);
1152 int BC_TextBox::keypress_event()
1154 // Result == 2 contents changed
1155 // Result == 1 trapped keypress
1156 // Result == 0 nothing
1158 int dispatch_event = 0;
1160 if(!active || !enabled) return 0;
1162 int wtext_len = wtext_update();
1163 last_keypress = get_keypress();
1165 if( unicode_active >= 0 ) {
1168 switch( last_keypress ) {
1169 //unicode active acitons
1171 for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1172 int ch = nib(wtext[i]);
1173 if( ch < 0 ) return 1;
1174 wch = (wch<<4) + ch;
1178 unicode_active = -1;
1184 unicode_active = -1;
1189 if(ibeam_letter > 0) {
1190 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1191 highlight_letter2 = --ibeam_letter;
1192 if( highlight_letter1 >= highlight_letter2 )
1193 unicode_active = -1;
1198 case KPINS: last_keypress = KP1-'1'+'0'; // fall thru
1199 case KP1: case KP2: case KP3: case KP4: case KP5:
1200 case KP6: case KP7: case KP8: case KP9:
1201 last_keypress = last_keypress-KP1 + '1';
1202 case '0': case '1': case '2': case '3': case '4':
1203 case '5': case '6': case '7': case '8': case '9':
1204 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1205 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1206 int n = nib(last_keypress);
1207 wch = n < 10 ? '0'+n : 'A'+n-10;
1221 insert_text(&wch, wlen);
1223 if( unicode_active >= 0 )
1224 highlight_letter2 = ibeam_letter;
1226 highlight_letter1 = highlight_letter2 = 0;
1232 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1233 switch(last_keypress) {
1235 // Deactivate the suggestions
1236 if( suggestions_popup ) {
1241 top_level->deactivate();
1248 highlight_letter1 = highlight_letter2 = 0;
1249 ibeam_letter = wtext_update();
1250 top_level->deactivate();
1255 default_keypress(dispatch_event, result);
1260 // Handle like a default keypress
1262 top_level->cycle_textboxes(1);
1267 top_level->cycle_textboxes(-1);
1272 if(ibeam_letter > 0) {
1273 int old_ibeam_letter = ibeam_letter;
1281 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1286 if(top_level->shift_down()) {
1287 // Initialize highlighting
1288 if(highlight_letter1 == highlight_letter2) {
1289 highlight_letter1 = ibeam_letter;
1290 highlight_letter2 = old_ibeam_letter;
1292 else if(highlight_letter1 == old_ibeam_letter) {
1293 // Extend left highlight
1294 highlight_letter1 = ibeam_letter;
1296 else if(highlight_letter2 == old_ibeam_letter) {
1297 // Shrink right highlight
1298 highlight_letter2 = ibeam_letter;
1302 highlight_letter1 = highlight_letter2 = ibeam_letter;
1307 if(keypress_draw) draw(1);
1313 if(ibeam_letter < wtext_len) {
1314 int old_ibeam_letter = ibeam_letter;
1321 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1327 if(top_level->shift_down()) {
1328 // Initialize highlighting
1329 if(highlight_letter1 == highlight_letter2) {
1330 highlight_letter1 = old_ibeam_letter;
1331 highlight_letter2 = ibeam_letter;
1333 else if(highlight_letter1 == old_ibeam_letter) {
1334 // Shrink left highlight
1335 highlight_letter1 = ibeam_letter;
1337 else if(highlight_letter2 == old_ibeam_letter) {
1338 // Expand right highlight
1339 highlight_letter2 = ibeam_letter;
1343 highlight_letter1 = highlight_letter2 = ibeam_letter;
1347 if(keypress_draw) draw(1);
1353 if( suggestions && suggestions_popup ) {
1354 // Pass to suggestions popup
1355 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1356 suggestions_popup->activate(1);
1357 suggestions_popup->keypress_event();
1360 else if(ibeam_letter > 0) {
1361 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1362 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1363 ibeam_y + text_y - text_height);
1364 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1367 if(top_level->shift_down()) {
1368 // Initialize highlighting
1369 if(highlight_letter1 == highlight_letter2) {
1370 highlight_letter1 = new_letter;
1371 highlight_letter2 = ibeam_letter;
1373 else if(highlight_letter1 == ibeam_letter) {
1374 // Expand left highlight
1375 highlight_letter1 = new_letter;
1377 else if(highlight_letter2 == ibeam_letter) {
1378 // Shrink right highlight
1379 highlight_letter2 = new_letter;
1383 highlight_letter1 = highlight_letter2 = new_letter;
1385 if(highlight_letter1 > highlight_letter2) {
1386 int temp = highlight_letter1;
1387 highlight_letter1 = highlight_letter2;
1388 highlight_letter2 = temp;
1390 ibeam_letter = new_letter;
1393 if(keypress_draw) draw(1);
1399 if(ibeam_letter > 0) {
1400 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1401 ibeam_y + text_y - get_h());
1404 if(top_level->shift_down()) {
1405 // Initialize highlighting
1406 if(highlight_letter1 == highlight_letter2) {
1407 highlight_letter1 = new_letter;
1408 highlight_letter2 = ibeam_letter;
1410 else if(highlight_letter1 == ibeam_letter) {
1411 // Expand left highlight
1412 highlight_letter1 = new_letter;
1414 else if(highlight_letter2 == ibeam_letter) {
1415 // Shrink right highlight
1416 highlight_letter2 = new_letter;
1420 highlight_letter1 = highlight_letter2 = new_letter;
1422 if(highlight_letter1 > highlight_letter2) {
1423 int temp = highlight_letter1;
1424 highlight_letter1 = highlight_letter2;
1425 highlight_letter2 = temp;
1427 ibeam_letter = new_letter;
1430 if(keypress_draw) draw(1);
1436 // printf("BC_TextBox::keypress_event %d %p %p\n",
1439 // suggestions_popup);
1440 if( suggestions && suggestions_popup ) {
1441 // Pass to suggestions popup
1442 suggestions_popup->activate(1);
1443 suggestions_popup->keypress_event();
1447 // if(ibeam_letter > 0)
1450 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1451 ibeam_y + text_y + text_height);
1452 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1454 if(top_level->shift_down()) {
1455 // Initialize highlighting
1456 if(highlight_letter1 == highlight_letter2) {
1457 highlight_letter1 = new_letter;
1458 highlight_letter2 = ibeam_letter;
1460 else if(highlight_letter1 == ibeam_letter) {
1461 // Shrink left highlight
1462 highlight_letter1 = new_letter;
1464 else if(highlight_letter2 == ibeam_letter) {
1465 // Expand right highlight
1466 highlight_letter2 = new_letter;
1470 highlight_letter1 = highlight_letter2 = new_letter;
1472 if(highlight_letter1 > highlight_letter2) {
1473 int temp = highlight_letter1;
1474 highlight_letter1 = highlight_letter2;
1475 highlight_letter2 = temp;
1477 ibeam_letter = new_letter;
1480 if(keypress_draw) draw(1);
1482 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1489 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1490 ibeam_y + text_y + get_h());
1491 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1493 if(top_level->shift_down()) {
1494 // Initialize highlighting
1495 if(highlight_letter1 == highlight_letter2) {
1496 highlight_letter1 = new_letter;
1497 highlight_letter2 = ibeam_letter;
1499 else if(highlight_letter1 == ibeam_letter) {
1500 // Shrink left highlight
1501 highlight_letter1 = new_letter;
1503 else if(highlight_letter2 == ibeam_letter) {
1504 // Expand right highlight
1505 highlight_letter2 = new_letter;
1509 highlight_letter1 = highlight_letter2 = new_letter;
1511 if(highlight_letter1 > highlight_letter2) {
1512 int temp = highlight_letter1;
1513 highlight_letter1 = highlight_letter2;
1514 highlight_letter2 = temp;
1516 ibeam_letter = new_letter;
1519 if(keypress_draw) draw(1);
1521 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1528 int old_ibeam_letter = ibeam_letter;
1530 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1533 if(top_level->shift_down()) {
1535 if(highlight_letter1 == highlight_letter2) {
1536 highlight_letter2 = ibeam_letter;
1537 highlight_letter1 = old_ibeam_letter;
1539 else if(highlight_letter1 == old_ibeam_letter) {
1541 highlight_letter1 = highlight_letter2;
1542 highlight_letter2 = ibeam_letter;
1544 else if(highlight_letter2 == old_ibeam_letter) {
1546 highlight_letter2 = ibeam_letter;
1550 highlight_letter1 = highlight_letter2 = ibeam_letter;
1553 if(keypress_draw) draw(1);
1560 int old_ibeam_letter = ibeam_letter;
1562 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1565 if(top_level->shift_down())
1568 if(highlight_letter1 == highlight_letter2)
1570 highlight_letter2 = old_ibeam_letter;
1571 highlight_letter1 = ibeam_letter;
1575 if(highlight_letter1 == old_ibeam_letter)
1577 highlight_letter1 = ibeam_letter;
1581 if(highlight_letter2 == old_ibeam_letter)
1583 highlight_letter2 = highlight_letter1;
1584 highlight_letter1 = ibeam_letter;
1588 highlight_letter1 = highlight_letter2 = ibeam_letter;
1591 if(keypress_draw) draw(1);
1598 if(highlight_letter1 == highlight_letter2) {
1599 if(ibeam_letter > 0) {
1600 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1605 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1606 highlight_letter2 = ibeam_letter = highlight_letter1;
1610 if(keypress_draw) draw(1);
1616 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1617 if(highlight_letter1 == highlight_letter2) {
1618 if(ibeam_letter < wtext_len) {
1619 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1623 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1624 highlight_letter2 = ibeam_letter = highlight_letter1;
1628 if(keypress_draw) draw(1);
1635 switch( last_keypress ) {
1636 case 'c': case 'C': {
1639 case 'v': case 'V': {
1643 case 'x': case 'X': {
1647 case 'u': case 'U': {
1648 if( shift_down() ) {
1649 unicode_active = ibeam_letter;
1651 insert_text(&wkey, 1);
1653 highlight_letter1 = unicode_active;
1654 highlight_letter2 = ibeam_letter;
1663 default_keypress(dispatch_event, result);
1668 if(result) skip_cursor->update();
1669 if(dispatch_event && handle_event())
1676 int BC_TextBox::cut(int do_update)
1678 if( highlight_letter1 != highlight_letter2 ) {
1679 int wtext_len = wtext_update();
1680 copy_selection(SECONDARY_SELECTION);
1681 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1682 highlight_letter2 = ibeam_letter = highlight_letter1;
1690 skip_cursor->update();
1696 int BC_TextBox::copy(int do_update)
1699 if( highlight_letter1 != highlight_letter2 ) {
1700 copy_selection(SECONDARY_SELECTION);
1703 skip_cursor->update();
1709 int BC_TextBox::paste(int do_update)
1711 paste_selection(SECONDARY_SELECTION);
1716 skip_cursor->update();
1723 int BC_TextBox::uses_text()
1729 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1732 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1733 wtext[i] = wtext[j];
1741 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1743 if( len < 0 ) len = wcslen(wcp);
1744 int wtext_len = wtext_update();
1745 if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1746 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1747 highlight_letter2 = ibeam_letter = highlight_letter1;
1748 wtext_len = wtext_update();
1752 for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1753 if( j >= wsize ) continue;
1754 wtext[j] = wtext[i];
1757 for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1758 if( i >= wsize ) break;
1761 if( (wlen+=len) > wsize ) wlen = wsize;
1762 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1763 wtext[wlen] = 0; // wtext allocated wsize+1
1767 int BC_TextBox::is_separator(const char *txt, int i)
1769 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1770 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1773 // used for time entry
1774 void BC_TextBox::do_separators(int ibeam_left)
1778 // Remove separators from text
1779 int wtext_len = wtext_update();
1780 for(int i = 0; i < wtext_len; ) {
1781 if( !iswalnum(wtext[i]) ) {
1782 for(int j = i; j < wtext_len - 1; j++)
1783 wtext[j] = wtext[j + 1];
1784 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1790 wtext[wtext_len] = 0;
1796 // Insert separators into text
1797 int separator_len = strlen(separators);
1798 for(int i = 0; i < separator_len; i++) {
1800 // Insert a separator
1801 if( is_separator(separators,i) ) {
1802 for(int j = wtext_len; j >= i; j--) {
1803 wtext[j + 1] = wtext[j];
1805 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1807 wtext[i] = separators[i];
1811 wtext[i] = separators[i];
1816 wtext[separator_len] = 0;
1817 wlen = separator_len;
1821 int BC_TextBox::get_x_position(int i, int start)
1823 return get_text_width(font, &wtext[start], i - start);
1826 void BC_TextBox::get_ibeam_position(int &x, int &y)
1828 int i, row_begin, row_end;
1829 int wtext_len = wtext_update();
1832 for( i=0; i<wtext_len; ) {
1834 for(; i<wtext_len && wtext[i]!='\n'; i++);
1837 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1838 x = get_x_position(ibeam_letter, row_begin);
1839 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1843 if( i < wtext_len && wtext[i] == '\n' ) {
1848 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1854 void BC_TextBox::set_text_row(int row)
1856 text_x = left_margin;
1857 text_y = -(row * text_height) + top_margin;
1861 int BC_TextBox::get_text_row()
1863 return -(text_y - top_margin) / text_height;
1866 void BC_TextBox::find_ibeam(int dispatch_event)
1869 int old_x = text_x, old_y = text_y;
1871 get_ibeam_position(x, y);
1873 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1875 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1876 if(text_x > left_margin) text_x = left_margin;
1879 if(left_margin + text_x + x < left_margin)
1881 text_x = -(x - (get_w() / 4)) + left_margin;
1882 if(text_x > left_margin) text_x = left_margin;
1885 int text_row = y / text_height;
1886 if( text_row < rows ) text_y = top_margin;
1888 int pix_rows = get_h() - bottom_margin - (y + text_y);
1889 if( pix_rows < text_height )
1890 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1892 pix_rows = y + text_y - top_margin;
1893 if( pix_rows < 0 ) {
1894 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1895 if( text_y > top_margin ) text_y = top_margin;
1898 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1902 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1904 int i, j, k, row_begin, row_end, result = 0, done = 0;
1905 int column1, column2;
1906 int got_visible_row = 0;
1908 // Select complete row if cursor above the window
1909 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1910 if(cursor_y < text_y - text_height)
1916 int wtext_len = wtext_update();
1918 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1919 // Simulate drawing of 1 row
1921 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1925 int first_visible_row = 0;
1926 int last_visible_row = 0;
1927 if( k+text_height > top_margin && !got_visible_row) {
1928 first_visible_row = 1;
1929 got_visible_row = 1;
1932 if( (k+text_height >= get_h() - bottom_margin ||
1933 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1934 k + text_height > 0)) )
1935 last_visible_row = 1;
1937 // Cursor is inside vertical range of row
1938 if((cursor_y >= top_margin &&
1939 cursor_y < get_h() - bottom_margin &&
1940 cursor_y >= k && cursor_y < k + text_height) ||
1941 // Cursor is above 1st row
1942 (cursor_y < k + text_height && first_visible_row) ||
1943 // Cursor is below last row
1944 (cursor_y >= k && last_visible_row))
1946 column1 = column2 = 0;
1947 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1948 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1949 if((column2 + column1) / 2 >= cursor_x) {
1952 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1953 // __LINE__, result, first_visible_row, last_visible_row);
1964 if(wtext[i] == '\n') i++;
1966 // Select complete row if last visible & cursor is below window
1967 if(last_visible_row && cursor_y > k + text_height * 2)
1970 if(i >= wtext_len && !done) {
1976 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
1977 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
1978 if(result < 0) result = 0;
1979 if(result > wtext_len) {
1980 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
1988 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
1990 int i, j, k, row_begin, row_end, result = 0, done = 0;
1991 int column1, column2;
1992 int wtext_len = wtext_update();
1994 if(cursor_y < text_y) {
1999 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
2001 for(; wtext[i] != '\n' && i < wtext_len; i++);
2004 if(cursor_y >= k && cursor_y < k + text_height) {
2005 column1 = column2 = 0;
2006 for(j = 0; j <= row_end - row_begin && !done; j++) {
2007 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
2008 if((column2 + column1) / 2 >= cursor_x) {
2009 result = row_begin + j - 1;
2020 if(wtext[i] == '\n') i++;
2022 if(i >= wtext_len && !done) {
2026 if(result < 0) result = 0;
2027 if(result > wtext_len) result = wtext_len;
2032 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2034 int wtext_len = wtext_update();
2035 letter1 = letter2 = ibeam_letter;
2036 if( letter1 < 0 ) letter1 = 0;
2037 if( letter2 < 0 ) letter2 = 0;
2038 if( letter1 > wtext_len ) letter1 = wtext_len;
2039 if( letter2 > wtext_len ) letter2 = wtext_len;
2040 if( !wtext_len ) return;
2041 for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
2042 for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
2043 if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
2047 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2049 int wtext_len = wtext_update();
2050 letter1 = letter2 = ibeam_letter;
2051 if( letter1 < 0 ) letter1 = 0;
2052 if( letter2 < 0 ) letter2 = 0;
2053 if( letter1 > wtext_len ) letter1 = wtext_len;
2054 if( letter2 > wtext_len ) letter2 = wtext_len;
2055 if( !wtext_len ) return;
2056 for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2057 for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2060 void BC_TextBox::copy_selection(int clipboard_num)
2062 int wtext_len = wtext_update();
2063 if(!wtext_len) return;
2065 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2066 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2067 highlight_letter2 - highlight_letter1 <= 0) return;
2068 int clip_len = highlight_letter2 - highlight_letter1;
2069 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2070 char ctext[4*clip_len+4];
2071 clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2072 to_clipboard(ctext, clip_len, clipboard_num);
2073 selection_active = 1;
2076 int BC_TextBox::selection_clear_event()
2078 if( !is_event_win() ) return 0;
2079 selection_active = 0;
2084 void BC_TextBox::paste_selection(int clipboard_num)
2086 int len = clipboard_len(clipboard_num);
2089 char cstring[len]; wchar_t wstring[len];
2090 from_clipboard(cstring, len, clipboard_num); --len;
2091 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2092 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2093 cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2094 insert_text(wstring, len);
2099 void BC_TextBox::set_keypress_draw(int value)
2101 keypress_draw = value;
2104 int BC_TextBox::get_last_keypress()
2106 return last_keypress;
2109 int BC_TextBox::get_ibeam_letter()
2111 return ibeam_letter;
2114 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2116 this->ibeam_letter = number;
2123 void BC_TextBox::set_separators(const char *separators)
2125 this->separators = (char*)separators;
2128 int BC_TextBox::get_rows()
2139 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y)
2140 : BC_ListBox(x, y, text_box->get_w(), 200, LISTBOX_TEXT,
2141 text_box->suggestions, 0, 0, 1, 0, 1)
2143 this->text_box = text_box;
2145 set_justify(LISTBOX_LEFT);
2148 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2152 int BC_TextBoxSuggestions::handle_event()
2154 char *current_suggestion = 0;
2155 BC_ListBoxItem *item = get_selection(0, 0);
2156 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2157 if(item && (current_suggestion=item->get_text()) != 0)
2159 int col = text_box->suggestion_column;
2160 int len = BCTEXTLEN-1 - col;
2161 char *cp = &text_box->text[col];
2162 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2163 strncpy(cp, current_suggestion, len);
2164 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2165 if( (col=strlen(current_suggestion)) >= len )
2167 text_box->dirty = 1;
2171 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2172 text_box->highlight_letter1 =
2173 text_box->highlight_letter2 =
2174 text_box->ibeam_letter = text_box->tstrlen();
2175 text_box->wtext_update();
2177 text_box->handle_event();
2178 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2183 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2184 int x, int y, int w, int rows,
2185 const char *default_text, int default_size)
2187 this->parent_window = parent_window;
2192 xscroll = 0; yscroll = 0;
2193 this->default_text = default_text;
2194 this->default_wtext = 0;
2195 this->default_size = default_size;
2198 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2199 int x, int y, int w, int rows,
2200 const wchar_t *default_wtext, int default_size)
2202 this->parent_window = parent_window;
2207 xscroll = 0; yscroll = 0;
2208 this->default_text = 0;
2209 this->default_wtext = default_wtext;
2210 this->default_size = default_size;
2213 BC_ScrollTextBox::~BC_ScrollTextBox()
2223 void BC_ScrollTextBox::create_objects()
2225 // Must be created first
2226 parent_window->add_subwindow(text = default_wtext ?
2227 new BC_ScrollTextBoxText(this, default_wtext) :
2228 new BC_ScrollTextBoxText(this, default_text));
2232 void BC_ScrollTextBox::set_text(char *text, int isz)
2234 this->text->set_text(text, isz);
2235 update_scrollbars();
2238 int BC_ScrollTextBox::set_text_row(int n)
2240 text->set_text_row(n);
2241 update_scrollbars();
2245 void BC_ScrollTextBox::update(const char *text)
2247 this->text->update(text);
2248 update_scrollbars();
2251 void BC_ScrollTextBox::update(const wchar_t *wtext)
2253 this->text->update(wtext);
2254 update_scrollbars();
2257 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2263 update_scrollbars();
2266 int BC_ScrollTextBox::button_press_event()
2268 return text->BC_TextBox::button_press_event();
2270 int BC_ScrollTextBox::button_release_event()
2272 return text->BC_TextBox::button_release_event();
2275 int BC_ScrollTextBox::get_h() { return text->get_h(); }
2276 const char *BC_ScrollTextBox::get_text() { return text->get_text(); }
2277 const wchar_t *BC_ScrollTextBox::get_wtext() { return text->get_wtext(); }
2279 int BC_ScrollTextBox::get_buttonpress()
2281 return text->BC_TextBox::get_buttonpress();
2283 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2285 text->wset_selection(char1, char2, ibeam);
2287 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2289 text->set_selection(char1, char2, ibeam);
2291 int BC_ScrollTextBox::get_ibeam_letter()
2293 return text->get_ibeam_letter();
2295 int BC_ScrollTextBox::get_x_pos()
2297 return text->left_margin - text->get_text_x();
2299 void BC_ScrollTextBox::set_x_pos(int x)
2301 text->set_text_x(text->left_margin - x);
2305 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2306 : BC_TextBox(gui->x, gui->y,
2307 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2308 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2313 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2314 : BC_TextBox(gui->x, gui->y,
2315 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2316 gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2321 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2330 void BC_ScrollTextBox::update_scrollbars()
2332 int view_w = w, view_rows = rows;
2333 int view_h = BC_TextBox::calculate_row_h(view_rows, parent_window);
2334 int text_rows = text->get_text_rows();
2335 int text_width = parent_window->get_text_width(text->font, text->get_wtext());
2336 BC_Resources *resources = parent_window->get_resources();
2337 int need_xscroll = 0, need_yscroll = 0;
2339 // Create scrollbars as needed
2343 if( !need_xscroll && (text->get_text_x() != text->left_margin ||
2344 text_width >= view_w - text->left_margin - text->right_margin) ) {
2345 resize = need_xscroll = 1;
2346 view_h -= resources->hscroll_data[SCROLL_HANDLE_UP]->get_h();
2347 view_rows = BC_TextBox::pixels_to_rows(parent_window, text->font, view_h);
2349 if( !need_yscroll && (text->get_text_y() != text->top_margin ||
2350 text_rows > view_rows) ) {
2351 resize = need_yscroll = 1;
2352 view_w -= resources->vscroll_data[SCROLL_HANDLE_UP]->get_w();
2356 if( !need_xscroll && xscroll ) {
2358 delete xscroll; xscroll = 0;
2360 if( !need_yscroll && yscroll ) {
2362 delete yscroll; yscroll = 0;
2365 if( view_rows != text->get_rows() || view_w != text->get_w() ) {
2366 text->reposition_window(x, y, view_w, view_rows);
2368 if( need_xscroll && !xscroll ) {
2369 xscroll = new BC_ScrollTextBoxXScroll(this);
2370 parent_window->add_subwindow(xscroll);
2371 text->xscroll = xscroll;
2372 xscroll->bound_to = text;
2373 xscroll->show_window();
2375 if( need_yscroll && !yscroll ) {
2376 yscroll = new BC_ScrollTextBoxYScroll(this);
2377 parent_window->add_subwindow(yscroll);
2378 text->yscroll = yscroll;
2379 yscroll->bound_to = text;
2380 yscroll->show_window();
2383 xscroll->reposition_window(x, y + text->get_h(), view_w);
2384 int xpos = get_x_pos();
2385 if( xpos != xscroll->get_value() )
2386 xscroll->update_value(xpos);
2387 if( text_width != xscroll->get_length() ||
2388 view_w != xscroll->get_handlelength() )
2389 xscroll->update_length(text_width, xpos, view_w, 0);
2392 yscroll->reposition_window(x + w - yscroll->get_span(), y, text->get_h());
2393 int text_row = text->get_text_row();
2394 if( text_row != yscroll->get_value() )
2395 yscroll->update_value(text_row);
2396 if( text_rows != yscroll->get_length() ||
2397 view_rows != yscroll->get_handlelength() )
2398 yscroll->update_length(text_rows, text_row, view_rows, 0);
2402 int BC_ScrollTextBoxText::handle_event()
2404 gui->update_scrollbars();
2405 return gui->handle_event();
2408 int BC_ScrollTextBoxText::motion_event()
2410 gui->update_scrollbars();
2414 BC_ScrollTextBoxXScroll::BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui)
2415 : BC_ScrollBar(gui->x, gui->y + gui->text->get_h(), SCROLL_HORIZ + SCROLL_STRETCH,
2416 gui->text->get_w(), gui->text->get_text_width(MEDIUMFONT, gui->get_wtext()),
2422 BC_ScrollTextBoxXScroll::~BC_ScrollTextBoxXScroll()
2426 int BC_ScrollTextBoxXScroll::handle_event()
2428 gui->set_x_pos(get_position());
2432 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2433 : BC_ScrollBar(gui->x + gui->text->get_w(), gui->y, SCROLL_VERT,
2434 gui->text->get_h(), gui->text->get_text_rows(), 0, gui->rows)
2439 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2443 int BC_ScrollTextBoxYScroll::handle_event()
2445 gui->text->set_text_row(get_position());
2451 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2452 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2454 this->popup = popup;
2457 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2458 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2460 this->popup = popup;
2463 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2473 int BC_PopupTextBoxText::handle_event()
2475 popup->handle_event();
2479 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2481 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2482 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2484 this->popup = popup;
2486 int BC_PopupTextBoxList::handle_event()
2488 BC_ListBoxItem *item = get_selection(0, 0);
2491 popup->textbox->update(item->get_text());
2492 popup->textbox->set_text_row(0);
2493 popup->handle_event();
2501 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2502 ArrayList<BC_ListBoxItem*> *list_items,
2503 const char *default_text, int x, int y,
2504 int text_w, int list_h, int list_format)
2508 this->list_h = list_h;
2509 this->list_format = list_format;
2510 this->default_text = (char*)default_text;
2511 this->default_wtext = 0;
2512 this->text_w = text_w;
2513 this->parent_window = parent_window;
2514 this->list_items = list_items;
2517 BC_PopupTextBox::~BC_PopupTextBox()
2527 int BC_PopupTextBox::create_objects()
2529 int x = this->x, y = this->y;
2530 parent_window->add_subwindow(textbox = default_wtext ?
2531 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2532 new BC_PopupTextBoxText(this, x, y, default_text));
2533 x += textbox->get_w();
2534 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2538 void BC_PopupTextBox::update(const char *text)
2540 textbox->update(text);
2541 textbox->set_text_row(0);
2544 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2546 listbox->update(data, 0, 0, 1);
2550 const char* BC_PopupTextBox::get_text()
2552 return textbox->get_text();
2555 const wchar_t* BC_PopupTextBox::get_wtext()
2557 return textbox->get_wtext();
2560 int BC_PopupTextBox::get_number()
2562 return listbox->get_selection_number(0, 0);
2565 int BC_PopupTextBox::get_x()
2570 int BC_PopupTextBox::get_y()
2575 int BC_PopupTextBox::get_w()
2577 return textbox->get_w() + listbox->get_w();
2580 int BC_PopupTextBox::get_h()
2582 return textbox->get_h();
2585 int BC_PopupTextBox::get_show_query()
2587 return listbox->get_show_query();
2590 void BC_PopupTextBox::set_show_query(int v)
2592 listbox->set_show_query(v);
2595 int BC_PopupTextBox::handle_event()
2600 void BC_PopupTextBox::reposition_window(int x, int y)
2605 textbox->reposition_window(x1,
2608 textbox->get_rows());
2609 x1 += textbox->get_w();
2610 listbox->reposition_window(x1, y1, -1, -1, 0);
2611 // if(flush) parent_window->flush();
2627 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2628 int64_t default_value, int x, int y)
2629 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2631 this->popup = popup;
2634 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2635 float default_value, int x, int y, int precision)
2636 : BC_TextBox(x, y, popup->text_w, 1, default_value, 1, MEDIUMFONT, precision)
2638 this->popup = popup;
2641 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2653 int BC_TumbleTextBoxText::handle_event()
2655 popup->handle_event();
2659 int BC_TumbleTextBoxText::button_press_event()
2661 if( get_enabled() && is_event_win() ) {
2662 if( get_buttonpress() < 4 ) return BC_TextBox::button_press_event();
2663 if( get_buttonpress() == 4 ) popup->tumbler->handle_up_event();
2664 else if( get_buttonpress() == 5 ) popup->tumbler->handle_down_event();
2673 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2674 int64_t default_value,
2686 this->default_value = default_value;
2687 this->text_w = text_w;
2688 this->parent_window = parent_window;
2694 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2707 this->default_value = default_value;
2708 this->text_w = text_w;
2709 this->parent_window = parent_window;
2715 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2716 float default_value_f,
2726 this->min_f = min_f;
2727 this->max_f = max_f;
2728 this->default_value_f = default_value_f;
2729 this->text_w = text_w;
2730 this->parent_window = parent_window;
2736 BC_TumbleTextBox::~BC_TumbleTextBox()
2738 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2739 // is deleted anyway by the windowbase so textbox deletes this.
2740 if(tumbler) delete tumbler;
2742 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2751 void BC_TumbleTextBox::reset()
2758 void BC_TumbleTextBox::set_precision(int precision)
2760 this->precision = precision;
2763 void BC_TumbleTextBox::set_increment(float value)
2765 this->increment = value;
2766 if(tumbler) tumbler->set_increment(value);
2769 void BC_TumbleTextBox::set_log_floatincrement(int value)
2771 this->log_floatincrement = value;
2772 if(tumbler) tumbler->set_log_floatincrement(value);
2775 int BC_TumbleTextBox::create_objects()
2777 int x = this->x, y = this->y;
2779 textbox = use_float ?
2780 new BC_TumbleTextBoxText(this, default_value_f, x, y, precision) :
2781 new BC_TumbleTextBoxText(this, default_value, x, y);
2783 parent_window->add_subwindow(textbox);
2784 x += textbox->get_w();
2786 tumbler = use_float ?
2787 (BC_Tumbler *)new BC_FTumbler(textbox, min_f, max_f, x, y) :
2788 (BC_Tumbler *)new BC_ITumbler(textbox, min, max, x, y);
2789 parent_window->add_subwindow(tumbler);
2790 tumbler->set_increment(increment);
2794 const char* BC_TumbleTextBox::get_text()
2796 return textbox->get_text();
2799 const wchar_t* BC_TumbleTextBox::get_wtext()
2801 return textbox->get_wtext();
2804 BC_TextBox* BC_TumbleTextBox::get_textbox()
2809 int BC_TumbleTextBox::update(const char *value)
2811 textbox->update(value);
2812 textbox->set_text_row(0);
2816 int BC_TumbleTextBox::update(int64_t value)
2818 textbox->update(value);
2819 textbox->set_text_row(0);
2823 int BC_TumbleTextBox::update(float value)
2825 textbox->update(value);
2826 textbox->set_text_row(0);
2831 int BC_TumbleTextBox::get_x()
2836 int BC_TumbleTextBox::get_y()
2841 int BC_TumbleTextBox::get_w()
2843 return textbox->get_w() + tumbler->get_w();
2846 int BC_TumbleTextBox::get_h()
2848 return textbox->get_h();
2851 void BC_TumbleTextBox::disable(int hide_text)
2853 if( hide_text && !textbox->is_hidden() )
2854 textbox->hide_window(0);
2855 if( !tumbler->is_hidden() )
2856 tumbler->hide_window(0);
2857 if( !get_enabled() ) return;
2858 return textbox->disable();
2861 void BC_TumbleTextBox::enable()
2863 if( textbox->is_hidden() )
2864 textbox->show_window(0);
2865 if( tumbler->is_hidden() )
2866 tumbler->show_window(0);
2867 if( get_enabled() ) return;
2868 return textbox->enable();
2871 int BC_TumbleTextBox::get_enabled()
2873 return textbox->get_enabled();
2876 int BC_TumbleTextBox::handle_event()
2881 void BC_TumbleTextBox::reposition_window(int x, int y)
2886 textbox->reposition_window(x,
2890 tumbler->reposition_window(x + textbox->get_w(),
2892 // if(flush) parent_window->flush();
2896 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2898 tumbler->set_boundaries(min, max);
2901 void BC_TumbleTextBox::set_boundaries(float min, float max)
2903 tumbler->set_boundaries(min, max);
2908 BC_TextMenu::BC_TextMenu(BC_TextBox *textbox)
2909 : BC_PopupMenu(0, 0, 0, "", 0)
2911 this->textbox = textbox;
2914 BC_TextMenu::~BC_TextMenu()
2918 void BC_TextMenu::create_objects()
2920 add_item(new BC_TextMenuCut(this));
2921 add_item(new BC_TextMenuCopy(this));
2922 add_item(new BC_TextMenuPaste(this));
2926 BC_TextMenuCut::BC_TextMenuCut(BC_TextMenu *menu)
2927 : BC_MenuItem(_("Cut"))
2932 int BC_TextMenuCut::handle_event()
2934 menu->textbox->cut(1);
2940 BC_TextMenuCopy::BC_TextMenuCopy(BC_TextMenu *menu)
2941 : BC_MenuItem(_("Copy"))
2946 int BC_TextMenuCopy::handle_event()
2948 menu->textbox->copy(1);
2953 BC_TextMenuPaste::BC_TextMenuPaste(BC_TextMenu *menu)
2954 : BC_MenuItem(_("Paste"))
2959 int BC_TextMenuPaste::handle_event()
2961 menu->textbox->paste(1);
2966 void BC_TumbleTextBox::set_tooltip(const char *text)
2968 textbox->set_tooltip(text);