3 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "bcclipboard.h"
22 #include "bclistboxitem.h"
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "bctextbox.h"
30 #include "filesystem.h"
41 #define VERTICAL_MARGIN 2
42 #define VERTICAL_MARGIN_NOBORDER 0
43 #define HORIZONTAL_MARGIN 4
44 #define HORIZONTAL_MARGIN_NOBORDER 2
46 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
47 int size, char *text, int has_border, int font)
48 : BC_SubWindow(x, y, w, 0, -1)
52 reset_parameters(rows, has_border, font, size);
55 else // text referenced directly
59 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
60 int size, wchar_t *wtext, int has_border, int font)
61 : BC_SubWindow(x, y, w, 0, -1)
65 wsize = size > 0 ? size : wcslen(wtext);
66 if( size <= 0 ) size = 2*wsize;
67 reset_parameters(rows, has_border, font, size);
68 this->wtext = new wchar_t[wsize+1];
69 wcsncpy(this->wtext, wtext, wsize);
70 this->wtext[wsize] = 0;
73 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
74 const char *text, int has_border, int font, int is_utf8)
75 : BC_SubWindow(x, y, w, 0, -1)
77 this->is_utf8 = is_utf8;
79 reset_parameters(rows, has_border, font, BCTEXTLEN);
83 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
84 const wchar_t *wtext, int has_border, int font, int is_utf8)
85 : BC_SubWindow(x, y, w, 0, -1)
87 this->is_utf8 = is_utf8;
89 reset_parameters(rows, has_border, font, BCTEXTLEN);
91 wtext = new wchar_t[wsize+1];
92 wcsncpy(this->wtext, wtext, wsize);
93 this->wtext[wsize] = 0;
96 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
97 int64_t text, int has_border, int font)
98 : BC_SubWindow(x, y, w, 0, -1)
102 reset_parameters(rows, has_border, font, BCSTRLEN);
103 snprintf(this->text, this->tsize, "%jd", text);
104 dirty = 1; wtext_update();
107 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
108 float text, int has_border, int font, int precision)
109 : BC_SubWindow(x, y, w, 0, -1)
113 reset_parameters(rows, has_border, font, BCSTRLEN);
114 this->precision = precision;
115 snprintf(this->text, this->tsize, "%0.*f", precision, text);
116 dirty = 1; wtext_update();
119 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
120 int text, int has_border, int font)
121 : BC_SubWindow(x, y, w, 0, -1)
125 reset_parameters(rows, has_border, font, BCSTRLEN);
126 snprintf(this->text, this->tsize, "%d", text);
127 dirty = 1; wtext_update();
130 BC_TextBox::~BC_TextBox()
132 if(skip_cursor) delete skip_cursor;
133 delete suggestions_popup;
134 suggestions->remove_all_objects();
142 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
144 suggestions = new ArrayList<BC_ListBoxItem*>;
145 suggestions_popup = 0;
146 suggestion_column = 0;
149 this->has_border = has_border;
152 this->tsize = size >= 0 ? size : -size;
153 this->text = size > 0 ? new char[size+1] : 0;
154 if( this->text ) this->text[0] = 0;
157 highlight_letter1 = highlight_letter2 = 0;
158 highlight_letter3 = highlight_letter4 = 0;
170 skip_cursor = new Timer;
184 int BC_TextBox::tstrlen()
186 if( !tsize ) return strlen(text);
187 return strnlen(text, tsize);
190 int BC_TextBox::tstrcmp(const char *cp)
192 if( !tsize ) return strcmp(text, cp);
193 return strncmp(text, cp, tsize);
196 char *BC_TextBox::tstrcpy(const char *cp)
201 return strcpy(text, cp);
202 strncpy(text, cp, tsize);
210 char *BC_TextBox::tstrcat(const char *cp)
213 if( !tsize ) return strcat(text, cp);
214 char *result = strncat(text, cp, tsize);
219 int BC_TextBox::wtext_update()
222 const char *src_enc = is_utf8 ? "UTF8" : BC_Resources::encoding;
223 const char *dst_enc = BC_Resources::wide_encoding;
224 int nsize = tsize > 0 ? tsize : strlen(text) + BCTEXTLEN;
225 if( nsize > wsize || !wtext ) {
226 wchar_t *ntext = new wchar_t[nsize+1];
227 memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
228 delete [] wtext; wtext = ntext; wsize = nsize;
229 int *npositions = new int[nsize+1];
231 memcpy(npositions, positions, (plen+1)*sizeof(positions[0]));
234 delete [] positions; positions = npositions; plen = nsize;
236 wlen = BC_Resources::encode(src_enc, dst_enc, text, strlen(text),
237 (char*)wtext, wsize*sizeof(wchar_t)) / sizeof(wchar_t);
244 int BC_TextBox::text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz)
246 const char *src_enc = BC_Resources::wide_encoding;
247 const char *dst_enc = BC_Resources::encoding;
248 if( wsz < 0 ) wsz = wcslen(wcp);
249 int len = BC_Resources::encode(src_enc, dst_enc,
250 (char*)wcp, wsz*sizeof(wchar_t), tcp, tsz);
255 int BC_TextBox::initialize()
258 skip_cursor = new Timer;
259 skip_cursor->update();
261 text_ascent = get_text_ascent(font) + 1;
262 text_descent = get_text_descent(font) + 1;
263 text_height = text_ascent + text_descent;
264 ibeam_letter = wtext_update();
267 left_margin = right_margin = HORIZONTAL_MARGIN;
268 top_margin = bottom_margin = VERTICAL_MARGIN;
272 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
273 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
276 text_x = left_margin;
280 // Create the subwindow
281 BC_SubWindow::initialize();
283 BC_Resources *resources = get_resources();
286 back_color = resources->text_background;
287 high_color = resources->text_background_hi;
291 high_color = resources->text_background_noborder_hi;
292 back_color = bg_color;
296 set_cursor(IBEAM_CURSOR, 0, 0);
301 int BC_TextBox::calculate_h(BC_WindowBase *gui,
306 return rows * (gui->get_text_ascent(font) + 1 +
307 gui->get_text_descent(font) + 1) +
308 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
311 void BC_TextBox::set_precision(int precision)
313 this->precision = precision;
316 // Compute suggestions for a path
317 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries)
319 // Let user delete suggestion
320 if(get_last_keypress() != BACKSPACE)
323 // Compute suggestions
325 ArrayList<char*> suggestions;
326 const char *current_text = get_text();
328 // If directory, tabulate it
329 if(current_text[0] == '/' ||
330 current_text[0] == '~')
332 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
333 char string[BCTEXTLEN];
334 strncpy(string, current_text, sizeof(string));
335 char *ptr = strrchr(string, '/');
336 if(!ptr) ptr = strrchr(string, '~');
338 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
340 int suggestion_column = ptr + 1 - string;
342 fs.set_filter(get_resources()->filebox_filter);
343 // fs.set_sort_order(filebox->sort_order);
344 // fs.set_sort_field(filebox->column_type[filebox->sort_column]);
347 //printf("BC_TextBox::calculate_suggestions %d %c %s\n", __LINE__, *ptr, string);
348 if(current_text[0] == '~' && *ptr != '/')
354 fs.parse_tildas(string);
357 //printf("BC_TextBox::calculate_suggestions %d %d\n", __LINE__, fs.total_files());
360 // Accept only entries with matching trailing characters
361 ptr = strrchr((char*)current_text, '/');
362 if(!ptr) ptr = strrchr((char*)current_text, '~');
364 //printf("BC_TextBox::calculate_suggestions %d %s %p\n", __LINE__, current_text, ptr);
369 for(int i = 0; i < fs.total_files(); i++)
371 char *current_name = fs.get_entry(i)->name;
372 if(!strncmp(ptr, current_name, strlen(ptr)))
374 suggestions.append(current_name);
375 //printf("BC_TextBox::calculate_suggestions %d %s\n", __LINE__, current_name);
380 // Accept all entries
381 for(int i = 0; i < fs.total_files(); i++)
383 //printf("BC_TextBox::calculate_suggestions %d %s\n", __LINE__, fs.get_entry(i)->name);
384 suggestions.append(fs.get_entry(i)->name);
386 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
388 // Add 1 to column to keep /
389 set_suggestions(&suggestions, suggestion_column);
390 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
393 // Get entries from current listbox with matching trailing characters
396 // printf("BC_TextBox::calculate_suggestions %d %d\n",
399 for(int i = 0; i < entries->size(); i++)
401 char *current_name = entries->get(i)->get_text();
403 //printf("BC_TextBox::calculate_suggestions %d %s %s\n", __LINE__, current_text, current_name);
404 if(!strncmp(current_text, current_name, strlen(current_text)))
406 suggestions.append(current_name);
410 set_suggestions(&suggestions, 0);
417 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
420 this->suggestions->remove_all_objects();
421 this->suggestion_column = column;
425 for(int i = 0; i < suggestions->size(); i++)
427 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
430 // Show the popup without taking focus
431 if(suggestions->size() > 1)
433 if(!suggestions_popup)
436 get_parent()->add_subwindow(suggestions_popup =
437 new BC_TextBoxSuggestions(this, x, y));
438 suggestions_popup->set_is_suggestions(1);
439 suggestions_popup->activate(0);
443 suggestions_popup->update(this->suggestions,
447 suggestions_popup->activate(0);
451 // Show the highlighted text
452 if(suggestions->size() == 1)
454 highlight_letter1 = wtext_update();
455 text_update(wtext,wlen, text,tsize);
456 char *current_suggestion = suggestions->get(0);
457 int col = highlight_letter1 - suggestion_column;
458 if( col < 0 ) col = 0;
459 char *cur = current_suggestion + col;
461 highlight_letter2 = wtext_update();
462 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
466 delete suggestions_popup;
467 suggestions_popup = 0;
472 if(!suggestions || !this->suggestions->size())
474 delete suggestions_popup;
475 suggestions_popup = 0;
479 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
481 highlight_letter1 = char1;
482 highlight_letter2 = char2;
483 ibeam_letter = ibeam;
487 // count utf8 chars in text which occur before cp
488 int BC_TextBox::wcpos(const char *text, const char *cp)
491 const unsigned char *bp = (const unsigned char *)text;
492 const unsigned char *ep = (const unsigned char *)cp;
493 while( bp < ep && *bp != 0 ) {
496 if( ch < 0x80 ) continue;
498 int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
499 for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
504 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
506 const char *cp = get_text();
507 wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
510 int BC_TextBox::update(const char *text)
512 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
513 // Don't update if contents are the same
514 if(!tstrcmp(text)) return 0;
516 int wtext_len = wtext_update();
517 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
518 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
519 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
524 int BC_TextBox::update(const wchar_t *wtext)
526 int wtext_len = wcslen(wtext);
527 if( wtext_len >= wsize ) wtext_len = wsize;
528 wcsncpy(this->wtext, wtext, wtext_len);
530 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
531 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
532 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
537 int BC_TextBox::update(int64_t value)
539 char string[BCTEXTLEN];
540 sprintf(string, "%jd", value);
545 int BC_TextBox::update(float value)
547 char string[BCTEXTLEN];
548 sprintf(string, "%0.*f", precision, value);
553 void BC_TextBox::disable()
558 if(active) top_level->deactivate();
564 void BC_TextBox::enable()
574 int BC_TextBox::get_enabled()
579 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
581 return (pixels - 4) /
582 (window->get_text_ascent(font) + 1 +
583 window->get_text_descent(font) + 1);
586 int BC_TextBox::calculate_row_h(int rows,
587 BC_WindowBase *parent_window,
592 (parent_window->get_text_ascent(font) + 1 +
593 parent_window->get_text_descent(font) + 1) +
594 (has_border ? 4 : 0);
597 const char* BC_TextBox::get_text()
599 int wtext_len = wtext_update();
600 text_update(wtext,wtext_len, text,tsize);
604 const wchar_t* BC_TextBox::get_wtext()
610 void BC_TextBox::set_text(char *text, int isz)
612 if( size > 0 || isz < 0 ) return;
621 int BC_TextBox::get_text_rows()
623 int wtext_len = wtext_update();
625 for(int i = 0; i < wtext_len; i++) {
626 if(wtext[i] == '\n') result++;
632 int BC_TextBox::get_row_h(int rows)
634 return rows * text_height + top_margin + bottom_margin;
637 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
640 if(w < 0) w = get_w();
643 new_h = get_row_h(rows);
652 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
653 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
654 BC_WindowBase::reposition_window(x, y, w, new_h);
660 void BC_TextBox::draw_border()
662 BC_Resources *resources = get_resources();
664 set_color(background_color);
665 draw_box(0, 0, left_margin, get_h());
666 draw_box(get_w() - right_margin, 0, right_margin, get_h());
671 draw_3d_border(0, 0, w, h,
672 resources->text_border1,
673 resources->text_border2_hi,
674 resources->text_border3_hi,
675 resources->text_border4);
677 draw_3d_border(0, 0, w, h,
678 resources->text_border1,
679 resources->text_border2,
680 resources->text_border3,
681 resources->text_border4);
685 void BC_TextBox::draw_cursor()
687 // set_color(background_color);
695 draw_box(ibeam_x + text_x,
704 void BC_TextBox::draw(int flush)
707 int row_begin, row_end;
708 int highlight_x1, highlight_x2;
710 BC_Resources *resources = get_resources();
712 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
715 background_color = resources->text_background;
717 background_color = high_color;
719 background_color = back_color;
720 set_color(background_color);
721 draw_box(0, 0, w, h);
723 int wtext_len = wtext_update();
725 // Draw text with selection
728 for(i=0, j=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
731 wchar_t *wtext_row = &wtext[i];
732 for(j=0; j<BCTEXTLEN-1 && i<wtext_len && wtext[i]!='\n'; ++i, ++j);
733 if( (row_end=i) < wtext_len ) ++i;
735 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
736 // Draw highlighted region of row
737 if( highlight_letter2 > highlight_letter1 &&
738 highlight_letter2 > row_begin &&
739 highlight_letter1 <= row_end ) {
740 int color = active && enabled && get_has_focus() ?
741 resources->text_highlight :
742 resources->text_inactive_highlight;
743 if( unicode_active >= 0 )
746 if(highlight_letter1 >= row_begin &&
747 highlight_letter1 <= row_end)
748 highlight_x1 = positions[highlight_letter1];
752 if(highlight_letter2 > row_begin &&
753 highlight_letter2 <= row_end)
754 highlight_x2 = positions[highlight_letter2];
756 highlight_x2 = get_w();
758 draw_box(highlight_x1 + text_x, k,
759 highlight_x2 - highlight_x1, text_height);
762 // Draw text over highlight
763 int len = row_end - row_begin;
765 set_color(enabled ? resources->text_default : DMGREY);
766 draw_wtext(text_x, k + text_ascent, wtext_row, len,
767 0, &positions[wtext_row - wtext]);
770 // Get ibeam location
771 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
773 ibeam_y = k - text_y;
774 ibeam_x = positions[ibeam_letter];
779 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
781 if( wtext_len == 0 ) {
791 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
801 int BC_TextBox::focus_in_event()
807 int BC_TextBox::focus_out_event()
813 int BC_TextBox::cursor_enter_event()
815 if(top_level->event_win == win && enabled)
829 int BC_TextBox::cursor_leave_event()
841 int BC_TextBox::button_press_event()
845 if(!enabled) return 0;
846 if(get_buttonpress() != WHEEL_UP &&
847 get_buttonpress() != WHEEL_DOWN &&
848 get_buttonpress() != LEFT_BUTTON &&
849 get_buttonpress() != MIDDLE_BUTTON) return 0;
853 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
855 int cursor_letter = 0;
856 int wtext_len = wtext_update();
857 int update_scroll = 0;
860 if(top_level->event_win == win)
865 top_level->deactivate();
870 if(get_buttonpress() == WHEEL_UP)
872 text_y += text_height;
873 text_y = MIN(text_y, top_margin);
877 if(get_buttonpress() == WHEEL_DOWN)
879 int min_y = -(get_text_rows() *
883 text_y -= text_height;
884 text_y = MAX(text_y, min_y);
885 text_y = MIN(text_y, top_margin);
891 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
893 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
896 if(get_triple_click())
898 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
900 select_line(highlight_letter1, highlight_letter2, cursor_letter);
901 highlight_letter3 = highlight_letter1;
902 highlight_letter4 = highlight_letter2;
903 ibeam_letter = highlight_letter2;
904 copy_selection(PRIMARY_SELECTION);
907 if(get_double_click())
910 select_word(highlight_letter1, highlight_letter2, cursor_letter);
911 highlight_letter3 = highlight_letter1;
912 highlight_letter4 = highlight_letter2;
913 ibeam_letter = highlight_letter2;
914 copy_selection(PRIMARY_SELECTION);
917 if(get_buttonpress() == MIDDLE_BUTTON)
919 highlight_letter3 = highlight_letter4 =
920 ibeam_letter = highlight_letter1 =
921 highlight_letter2 = cursor_letter;
922 paste_selection(PRIMARY_SELECTION);
927 highlight_letter3 = highlight_letter4 =
928 ibeam_letter = highlight_letter1 =
929 highlight_letter2 = cursor_letter;
933 // Handle scrolling by highlighting text
934 if(text_selected || word_selected || line_selected)
936 set_repeat(top_level->get_resources()->scroll_repeat);
939 if(ibeam_letter < 0) ibeam_letter = 0;
940 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
944 if(update_scroll && yscroll)
946 yscroll->update_length(get_text_rows(),
948 yscroll->get_handlelength(),
955 if(active && (!yscroll || !yscroll->is_event_win()))
957 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
958 // Suggestion popup is not active but must be deactivated.
959 if(suggestions_popup)
962 // printf("BC_TextBox::button_press_event %d\n", __LINE__);
963 // // Pass event to suggestions popup
964 // if(!suggestions_popup->button_press_event())
966 // printf("BC_TextBox::button_press_event %d\n", __LINE__);
967 // top_level->deactivate();
972 top_level->deactivate();
980 int BC_TextBox::button_release_event()
985 if(text_selected || word_selected || line_selected)
991 // Stop scrolling by highlighting text
992 unset_repeat(top_level->get_resources()->scroll_repeat);
998 int BC_TextBox::cursor_motion_event()
1000 int cursor_letter, letter1, letter2;
1003 if(text_selected || word_selected || line_selected)
1005 cursor_letter = get_cursor_letter(top_level->cursor_x,
1006 top_level->cursor_y);
1008 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
1012 select_line(letter1, letter2, cursor_letter);
1017 select_word(letter1, letter2, cursor_letter);
1022 letter1 = letter2 = cursor_letter;
1025 if(letter1 <= highlight_letter3)
1027 highlight_letter1 = letter1;
1028 highlight_letter2 = highlight_letter4;
1029 ibeam_letter = letter1;
1032 if(letter2 >= highlight_letter4)
1034 highlight_letter2 = letter2;
1035 highlight_letter1 = highlight_letter3;
1036 ibeam_letter = letter2;
1039 copy_selection(PRIMARY_SELECTION);
1052 int BC_TextBox::activate()
1054 top_level->active_subwindow = this;
1057 top_level->set_repeat(top_level->get_resources()->blink_rate);
1061 int BC_TextBox::deactivate()
1063 //printf("BC_TextBox::deactivate %d suggestions_popup=%p\n", __LINE__, suggestions_popup);
1065 top_level->unset_repeat(top_level->get_resources()->blink_rate);
1066 if(suggestions_popup)
1068 // Must deactivate instead of delete since this is called from BC_ListBox::button_press_event
1069 // suggestions_popup->deactivate();
1071 delete suggestions_popup;
1072 suggestions_popup = 0;
1079 int BC_TextBox::repeat_event(int64_t duration)
1082 int cursor_y = get_cursor_y();
1083 //int cursor_x = get_cursor_x();
1085 if(duration == top_level->get_resources()->tooltip_delay &&
1086 tooltip_text && tooltip_text[0] != 0 && highlighted)
1093 if(duration == top_level->get_resources()->blink_rate &&
1097 // don't flash if keypress
1098 if(skip_cursor->get_difference() < 500)
1100 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1101 // skip_cursor->get_difference(),
1107 if(!(text_selected || word_selected || line_selected))
1116 if(duration == top_level->get_resources()->scroll_repeat &&
1117 (text_selected || word_selected || line_selected))
1120 if(get_cursor_y() < top_margin)
1122 difference = get_cursor_y() - top_margin ;
1125 if(get_cursor_y() > get_h() - bottom_margin)
1127 difference = get_cursor_y() -
1128 (get_h() - bottom_margin);
1130 if(difference != 0) {
1131 int min_y = -(get_text_rows() * text_height -
1132 get_h() + bottom_margin);
1134 text_y -= difference;
1135 // printf("BC_TextBox::repeat_event %d %d %d\n",
1139 text_y = MAX(min_y, text_y);
1140 text_y = MIN(text_y, top_margin);
1147 if(get_cursor_x() < left_margin)
1149 int difference = left_margin - get_cursor_x();
1151 text_x += difference;
1152 text_x = MIN(text_x, left_margin);
1156 else if(get_cursor_x() > get_w() - right_margin)
1158 int difference = get_cursor_x() - (get_w() - right_margin);
1159 int new_text_x = text_x - difference;
1161 // Get width of current row
1164 int wtext_len = wtext_update();
1167 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1170 while(wtext[i] != '\n' && i < wtext_len) {
1174 if(wtext[i] == '\n') i++;
1176 if(cursor_y >= k && cursor_y < k + text_height) {
1177 row_width = get_text_width(font,
1179 row_end - row_begin);
1185 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1186 new_text_x = MAX(new_text_x, min_x);
1187 new_text_x = MIN(new_text_x, left_margin);
1189 if(new_text_x < text_x) text_x = new_text_x;
1198 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1200 if( (top_level->get_keypress() == RETURN) ||
1201 (top_level->get_keypress() > 30 && top_level->get_keypress() <= 255)) {
1203 wchar_t *temp_string = top_level->get_wkeystring(&len);
1204 if(top_level->get_keypress() == RETURN) {
1205 temp_string[0] = '\n'; temp_string[1] = 0;
1208 insert_text(temp_string, len);
1216 int BC_TextBox::keypress_event()
1218 // Result == 2 contents changed
1219 // Result == 1 trapped keypress
1220 // Result == 0 nothing
1222 int dispatch_event = 0;
1224 if(!active || !enabled) return 0;
1226 int wtext_len = wtext_update();
1227 last_keypress = get_keypress();
1229 if( unicode_active >= 0 ) {
1232 switch( last_keypress ) {
1233 //unicode active acitons
1235 for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1236 int ch = nib(wtext[i]);
1237 if( ch < 0 ) return 1;
1238 wch = (wch<<4) + ch;
1242 unicode_active = -1;
1248 unicode_active = -1;
1253 if(ibeam_letter > 0) {
1254 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1255 highlight_letter2 = --ibeam_letter;
1256 if( highlight_letter1 >= highlight_letter2 )
1257 unicode_active = -1;
1262 case '0': case '1': case '2': case '3': case '4':
1263 case '5': case '6': case '7': case '8': case '9':
1264 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1265 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1266 int n = nib(last_keypress);
1267 wch = n < 10 ? '0'+n : 'A'+n-10;
1281 insert_text(&wch, wlen);
1283 if( unicode_active >= 0 )
1284 highlight_letter2 = ibeam_letter;
1286 highlight_letter1 = highlight_letter2 = 0;
1292 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1293 switch(last_keypress) {
1295 // Deactivate the suggestions
1296 if(suggestions && suggestions_popup) {
1297 delete suggestions_popup;
1298 suggestions_popup = 0;
1302 top_level->deactivate();
1309 top_level->deactivate();
1314 default_keypress(dispatch_event, result);
1319 // Handle like a default keypress
1321 top_level->cycle_textboxes(1);
1326 top_level->cycle_textboxes(-1);
1331 if(ibeam_letter > 0) {
1332 int old_ibeam_letter = ibeam_letter;
1340 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1345 if(top_level->shift_down()) {
1346 // Initialize highlighting
1347 if(highlight_letter1 == highlight_letter2) {
1348 highlight_letter1 = ibeam_letter;
1349 highlight_letter2 = old_ibeam_letter;
1351 else if(highlight_letter1 == old_ibeam_letter) {
1352 // Extend left highlight
1353 highlight_letter1 = ibeam_letter;
1355 else if(highlight_letter2 == old_ibeam_letter) {
1356 // Shrink right highlight
1357 highlight_letter2 = ibeam_letter;
1361 highlight_letter1 = highlight_letter2 = ibeam_letter;
1366 if(keypress_draw) draw(1);
1372 if(ibeam_letter < wtext_len) {
1373 int old_ibeam_letter = ibeam_letter;
1380 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1386 if(top_level->shift_down()) {
1387 // Initialize highlighting
1388 if(highlight_letter1 == highlight_letter2) {
1389 highlight_letter1 = old_ibeam_letter;
1390 highlight_letter2 = ibeam_letter;
1392 else if(highlight_letter1 == old_ibeam_letter) {
1393 // Shrink left highlight
1394 highlight_letter1 = ibeam_letter;
1396 else if(highlight_letter2 == old_ibeam_letter) {
1397 // Expand right highlight
1398 highlight_letter2 = ibeam_letter;
1402 highlight_letter1 = highlight_letter2 = ibeam_letter;
1406 if(keypress_draw) draw(1);
1412 if(suggestions && suggestions_popup) {
1413 // Pass to suggestions popup
1414 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1415 suggestions_popup->activate(1);
1416 suggestions_popup->keypress_event();
1419 else if(ibeam_letter > 0) {
1420 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1421 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1422 ibeam_y + text_y - text_height);
1423 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1426 if(top_level->shift_down()) {
1427 // Initialize highlighting
1428 if(highlight_letter1 == highlight_letter2) {
1429 highlight_letter1 = new_letter;
1430 highlight_letter2 = ibeam_letter;
1432 else if(highlight_letter1 == ibeam_letter) {
1433 // Expand left highlight
1434 highlight_letter1 = new_letter;
1436 else if(highlight_letter2 == ibeam_letter) {
1437 // Shrink right highlight
1438 highlight_letter2 = new_letter;
1442 highlight_letter1 = highlight_letter2 = new_letter;
1444 if(highlight_letter1 > highlight_letter2) {
1445 int temp = highlight_letter1;
1446 highlight_letter1 = highlight_letter2;
1447 highlight_letter2 = temp;
1449 ibeam_letter = new_letter;
1452 if(keypress_draw) draw(1);
1458 if(ibeam_letter > 0) {
1459 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1460 ibeam_y + text_y - get_h());
1463 if(top_level->shift_down()) {
1464 // Initialize highlighting
1465 if(highlight_letter1 == highlight_letter2) {
1466 highlight_letter1 = new_letter;
1467 highlight_letter2 = ibeam_letter;
1469 else if(highlight_letter1 == ibeam_letter) {
1470 // Expand left highlight
1471 highlight_letter1 = new_letter;
1473 else if(highlight_letter2 == ibeam_letter) {
1474 // Shrink right highlight
1475 highlight_letter2 = new_letter;
1479 highlight_letter1 = highlight_letter2 = new_letter;
1481 if(highlight_letter1 > highlight_letter2) {
1482 int temp = highlight_letter1;
1483 highlight_letter1 = highlight_letter2;
1484 highlight_letter2 = temp;
1486 ibeam_letter = new_letter;
1489 if(keypress_draw) draw(1);
1495 // printf("BC_TextBox::keypress_event %d %p %p\n",
1498 // suggestions_popup);
1499 if(suggestions && suggestions_popup) {
1500 // Pass to suggestions popup
1501 suggestions_popup->activate(1);
1502 suggestions_popup->keypress_event();
1506 // if(ibeam_letter > 0)
1509 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1510 ibeam_y + text_y + text_height);
1511 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1513 if(top_level->shift_down()) {
1514 // Initialize highlighting
1515 if(highlight_letter1 == highlight_letter2) {
1516 highlight_letter1 = new_letter;
1517 highlight_letter2 = ibeam_letter;
1519 else if(highlight_letter1 == ibeam_letter) {
1520 // Shrink left highlight
1521 highlight_letter1 = new_letter;
1523 else if(highlight_letter2 == ibeam_letter) {
1524 // Expand right highlight
1525 highlight_letter2 = new_letter;
1529 highlight_letter1 = highlight_letter2 = new_letter;
1531 if(highlight_letter1 > highlight_letter2) {
1532 int temp = highlight_letter1;
1533 highlight_letter1 = highlight_letter2;
1534 highlight_letter2 = temp;
1536 ibeam_letter = new_letter;
1539 if(keypress_draw) draw(1);
1541 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1548 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1549 ibeam_y + text_y + get_h());
1550 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1552 if(top_level->shift_down()) {
1553 // Initialize highlighting
1554 if(highlight_letter1 == highlight_letter2) {
1555 highlight_letter1 = new_letter;
1556 highlight_letter2 = ibeam_letter;
1558 else if(highlight_letter1 == ibeam_letter) {
1559 // Shrink left highlight
1560 highlight_letter1 = new_letter;
1562 else if(highlight_letter2 == ibeam_letter) {
1563 // Expand right highlight
1564 highlight_letter2 = new_letter;
1568 highlight_letter1 = highlight_letter2 = new_letter;
1570 if(highlight_letter1 > highlight_letter2) {
1571 int temp = highlight_letter1;
1572 highlight_letter1 = highlight_letter2;
1573 highlight_letter2 = temp;
1575 ibeam_letter = new_letter;
1578 if(keypress_draw) draw(1);
1580 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1585 delete suggestions_popup;
1586 suggestions_popup = 0;
1588 int old_ibeam_letter = ibeam_letter;
1590 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1593 if(top_level->shift_down()) {
1595 if(highlight_letter1 == highlight_letter2) {
1596 highlight_letter2 = ibeam_letter;
1597 highlight_letter1 = old_ibeam_letter;
1599 else if(highlight_letter1 == old_ibeam_letter) {
1601 highlight_letter1 = highlight_letter2;
1602 highlight_letter2 = ibeam_letter;
1604 else if(highlight_letter2 == old_ibeam_letter) {
1606 highlight_letter2 = ibeam_letter;
1610 highlight_letter1 = highlight_letter2 = ibeam_letter;
1613 if(keypress_draw) draw(1);
1618 delete suggestions_popup;
1619 suggestions_popup = 0;
1621 int old_ibeam_letter = ibeam_letter;
1623 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1626 if(top_level->shift_down())
1629 if(highlight_letter1 == highlight_letter2)
1631 highlight_letter2 = old_ibeam_letter;
1632 highlight_letter1 = ibeam_letter;
1636 if(highlight_letter1 == old_ibeam_letter)
1638 highlight_letter1 = ibeam_letter;
1642 if(highlight_letter2 == old_ibeam_letter)
1644 highlight_letter2 = highlight_letter1;
1645 highlight_letter1 = ibeam_letter;
1649 highlight_letter1 = highlight_letter2 = ibeam_letter;
1652 if(keypress_draw) draw(1);
1657 if(suggestions_popup) {
1658 delete suggestions_popup;
1659 suggestions_popup = 0;
1662 if(highlight_letter1 == highlight_letter2) {
1663 if(ibeam_letter > 0) {
1664 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1669 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1670 highlight_letter2 = ibeam_letter = highlight_letter1;
1674 if(keypress_draw) draw(1);
1680 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1681 if(highlight_letter1 == highlight_letter2) {
1682 if(ibeam_letter < wtext_len) {
1683 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1687 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1688 highlight_letter2 = ibeam_letter = highlight_letter1;
1692 if(keypress_draw) draw(1);
1699 switch( get_keypress() ) {
1700 case 'c': case 'C': {
1701 if(highlight_letter1 != highlight_letter2) {
1702 copy_selection(SECONDARY_SELECTION);
1706 case 'v': case 'V': {
1707 paste_selection(SECONDARY_SELECTION);
1709 if(keypress_draw) draw(1);
1713 case 'x': case 'X': {
1714 if(highlight_letter1 != highlight_letter2) {
1715 copy_selection(SECONDARY_SELECTION);
1716 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1717 highlight_letter2 = ibeam_letter = highlight_letter1;
1721 if(keypress_draw) draw(1);
1725 case 'u': case 'U': {
1726 if( shift_down() ) {
1727 unicode_active = ibeam_letter;
1729 insert_text(&wkey, 1);
1731 highlight_letter1 = unicode_active;
1732 highlight_letter2 = ibeam_letter;
1741 default_keypress(dispatch_event, result);
1746 if(result) skip_cursor->update();
1747 if(dispatch_event && handle_event())
1754 int BC_TextBox::uses_text()
1760 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1763 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1764 wtext[i] = wtext[j];
1772 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1774 if( len < 0 ) len = wcslen(wcp);
1775 int wtext_len = wtext_update();
1776 if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1777 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1778 highlight_letter2 = ibeam_letter = highlight_letter1;
1779 wtext_len = wtext_update();
1783 for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1784 if( j >= wsize ) continue;
1785 wtext[j] = wtext[i];
1788 for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1789 if( i >= wsize ) break;
1792 if( (wlen+=len) > wsize ) wlen = wsize;
1793 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1794 wtext[wlen] = 0; // wtext allocated wsize+1
1798 int BC_TextBox::is_separator(const char *txt, int i)
1800 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1801 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1804 // used for time entry
1805 void BC_TextBox::do_separators(int ibeam_left)
1809 // Remove separators from text
1810 int wtext_len = wtext_update();
1811 for(int i = 0; i < wtext_len; ) {
1812 if( !iswalnum(wtext[i]) ) {
1813 for(int j = i; j < wtext_len - 1; j++)
1814 wtext[j] = wtext[j + 1];
1815 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1821 wtext[wtext_len] = 0;
1827 // Insert separators into text
1828 int separator_len = strlen(separators);
1829 for(int i = 0; i < separator_len; i++) {
1831 // Insert a separator
1832 if( is_separator(separators,i) ) {
1833 for(int j = wtext_len; j >= i; j--) {
1834 wtext[j + 1] = wtext[j];
1836 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1838 wtext[i] = separators[i];
1842 wtext[i] = separators[i];
1847 wtext[separator_len] = 0;
1848 wlen = separator_len;
1852 void BC_TextBox::get_ibeam_position(int &x, int &y)
1854 int i, row_begin, row_end;
1855 int wtext_len = wtext_update();
1859 for( i=0; i<wtext_len; ) {
1861 for(; i<wtext_len && wtext[i]!='\n'; i++);
1864 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1865 x = get_text_width(font, &wtext[row_begin], ibeam_letter - row_begin);
1866 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1870 if( i < wtext_len && wtext[i] == '\n' ) {
1875 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1881 void BC_TextBox::set_text_row(int row)
1883 text_x = left_margin;
1884 text_y = -(row * text_height) + top_margin;
1888 int BC_TextBox::get_text_row()
1890 return -(text_y - top_margin) / text_height;
1893 void BC_TextBox::find_ibeam(int dispatch_event)
1896 int old_x = text_x, old_y = text_y;
1898 get_ibeam_position(x, y);
1900 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1902 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1903 if(text_x > left_margin) text_x = left_margin;
1906 if(left_margin + text_x + x < left_margin)
1908 text_x = -(x - (get_w() / 4)) + left_margin;
1909 if(text_x > left_margin) text_x = left_margin;
1912 int text_row = y / text_height;
1913 if( text_row < rows ) text_y = top_margin;
1915 int pix_rows = get_h() - bottom_margin - (y + text_y);
1916 if( pix_rows < text_height )
1917 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1919 pix_rows = y + text_y - top_margin;
1920 if( pix_rows < 0 ) {
1921 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1922 if( text_y > top_margin ) text_y = top_margin;
1925 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1929 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1931 int i, j, k, row_begin, row_end, result = 0, done = 0;
1932 int column1, column2;
1933 int got_visible_row = 0;
1935 // Select complete row if cursor above the window
1936 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1937 if(cursor_y < text_y - text_height)
1943 int wtext_len = wtext_update();
1945 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1946 // Simulate drawing of 1 row
1948 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1952 int first_visible_row = 0;
1953 int last_visible_row = 0;
1954 if( k+text_height > top_margin && !got_visible_row) {
1955 first_visible_row = 1;
1956 got_visible_row = 1;
1959 if( (k+text_height >= get_h() - bottom_margin ||
1960 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1961 k + text_height > 0)) )
1962 last_visible_row = 1;
1964 // Cursor is inside vertical range of row
1965 if((cursor_y >= top_margin &&
1966 cursor_y < get_h() - bottom_margin &&
1967 cursor_y >= k && cursor_y < k + text_height) ||
1968 // Cursor is above 1st row
1969 (cursor_y < k + text_height && first_visible_row) ||
1970 // Cursor is below last row
1971 (cursor_y >= k && last_visible_row))
1973 column1 = column2 = 0;
1974 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1975 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1976 if((column2 + column1) / 2 >= cursor_x) {
1979 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1980 // __LINE__, result, first_visible_row, last_visible_row);
1991 if(wtext[i] == '\n') i++;
1993 // Select complete row if last visible & cursor is below window
1994 if(last_visible_row && cursor_y > k + text_height * 2)
1997 if(i >= wtext_len && !done) {
2003 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
2004 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
2005 if(result < 0) result = 0;
2006 if(result > wtext_len) {
2007 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
2015 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
2017 int i, j, k, row_begin, row_end, result = 0, done = 0;
2018 int column1, column2;
2019 int wtext_len = wtext_update();
2021 if(cursor_y < text_y) {
2026 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
2028 for(; wtext[i] != '\n' && i < wtext_len; i++);
2031 if(cursor_y >= k && cursor_y < k + text_height) {
2032 column1 = column2 = 0;
2033 for(j = 0; j <= row_end - row_begin && !done; j++) {
2034 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
2035 if((column2 + column1) / 2 >= cursor_x) {
2036 result = row_begin + j - 1;
2047 if(wtext[i] == '\n') i++;
2049 if(i >= wtext_len && !done) {
2053 if(result < 0) result = 0;
2054 if(result > wtext_len) result = wtext_len;
2059 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2061 int wtext_len = wtext_update();
2062 letter1 = letter2 = ibeam_letter;
2063 if( letter1 < 0 ) letter1 = 0;
2064 if( letter2 < 0 ) letter2 = 0;
2065 if( letter1 > wtext_len ) letter1 = wtext_len;
2066 if( letter2 > wtext_len ) letter2 = wtext_len;
2067 if( !wtext_len ) return;
2068 for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
2069 for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
2070 if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
2074 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2076 int wtext_len = wtext_update();
2077 letter1 = letter2 = ibeam_letter;
2078 if( letter1 < 0 ) letter1 = 0;
2079 if( letter2 < 0 ) letter2 = 0;
2080 if( letter1 > wtext_len ) letter1 = wtext_len;
2081 if( letter2 > wtext_len ) letter2 = wtext_len;
2082 if( !wtext_len ) return;
2083 for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2084 for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2087 void BC_TextBox::copy_selection(int clipboard_num)
2089 int wtext_len = wtext_update();
2090 if(!wtext_len) return;
2092 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2093 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2094 highlight_letter2 - highlight_letter1 <= 0) return;
2095 int clip_len = highlight_letter2 - highlight_letter1;
2096 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2097 char ctext[4*clip_len+4];
2098 clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2099 get_clipboard()->to_clipboard(ctext, clip_len, clipboard_num);
2103 void BC_TextBox::paste_selection(int clipboard_num)
2105 int len = get_clipboard()->clipboard_len(clipboard_num);
2108 char cstring[len]; wchar_t wstring[len];
2109 get_clipboard()->from_clipboard(cstring, len, clipboard_num); --len;
2110 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2111 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2112 cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2113 insert_text(wstring, len);
2117 void BC_TextBox::set_keypress_draw(int value)
2119 keypress_draw = value;
2122 int BC_TextBox::get_last_keypress()
2124 return last_keypress;
2127 int BC_TextBox::get_ibeam_letter()
2129 return ibeam_letter;
2132 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2134 this->ibeam_letter = number;
2141 void BC_TextBox::set_separators(const char *separators)
2143 this->separators = (char*)separators;
2146 int BC_TextBox::get_rows()
2157 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box,
2165 text_box->suggestions,
2172 this->text_box = text_box;
2174 set_justify(LISTBOX_LEFT);
2177 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2181 int BC_TextBoxSuggestions::selection_changed()
2184 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2185 BC_ListBoxItem *item = get_selection(0, 0);
2186 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2190 char *current_suggestion = item->get_text();
2191 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2192 // int text_box_len = strlen(text_box->text);
2193 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2194 strcpy(text_box->text + text_box->suggestion_column, current_suggestion);
2195 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2196 *(text_box->text + text_box->suggestion_column + strlen(current_suggestion)) = 0;
2198 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2200 text_box->handle_event();
2210 int BC_TextBoxSuggestions::handle_event()
2212 char *current_suggestion = 0;
2213 BC_ListBoxItem *item = get_selection(0, 0);
2214 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2215 if(item && (current_suggestion=item->get_text()) != 0)
2217 int col = text_box->suggestion_column;
2218 int len = BCTEXTLEN-1 - col;
2219 char *cp = &text_box->text[col];
2220 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2221 strncpy(cp, current_suggestion, len);
2222 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2223 if( (col=strlen(current_suggestion)) >= len )
2225 text_box->dirty = 1;
2229 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2230 text_box->highlight_letter1 =
2231 text_box->highlight_letter2 =
2232 text_box->ibeam_letter = text_box->tstrlen();
2233 text_box->wtext_update();
2235 text_box->handle_event();
2236 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2241 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2242 int x, int y, int w, int rows,
2243 const char *default_text, int default_size)
2245 this->parent_window = parent_window;
2250 this->default_text = default_text;
2251 this->default_wtext = 0;
2252 this->default_size = default_size;
2255 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2256 int x, int y, int w, int rows,
2257 const wchar_t *default_wtext, int default_size)
2259 this->parent_window = parent_window;
2264 this->default_text = 0;
2265 this->default_wtext = default_wtext;
2266 this->default_size = default_size;
2269 BC_ScrollTextBox::~BC_ScrollTextBox()
2278 void BC_ScrollTextBox::create_objects()
2280 // Must be created first
2281 parent_window->add_subwindow(text = default_wtext ?
2282 new BC_ScrollTextBoxText(this, default_wtext) :
2283 new BC_ScrollTextBoxText(this, default_text));
2284 parent_window->add_subwindow(yscroll = new BC_ScrollTextBoxYScroll(this));
2285 text->yscroll = yscroll;
2286 yscroll->bound_to = text;
2290 int BC_ScrollTextBox::handle_event()
2295 int BC_ScrollTextBox::get_x()
2300 int BC_ScrollTextBox::get_y()
2305 int BC_ScrollTextBox::get_w()
2310 int BC_ScrollTextBox::get_h()
2312 return this->text->get_h();
2315 int BC_ScrollTextBox::get_rows()
2321 const char* BC_ScrollTextBox::get_text()
2323 return text->get_text();
2326 const wchar_t* BC_ScrollTextBox::get_wtext()
2328 return text->get_wtext();
2331 void BC_ScrollTextBox::set_text(char *text, int isz)
2333 this->text->set_text(text, isz);
2334 yscroll->update_length(this->text->get_text_rows(),
2335 this->text->get_text_row(),
2336 yscroll->get_handlelength(),
2340 int BC_ScrollTextBox::set_text_row(int n)
2342 text->set_text_row(n);
2343 yscroll->update_value(n);
2347 void BC_ScrollTextBox::update(const char *text)
2349 this->text->update(text);
2350 yscroll->update_length(this->text->get_text_rows(),
2351 this->text->get_text_row(),
2352 yscroll->get_handlelength(),
2356 void BC_ScrollTextBox::update(const wchar_t *wtext)
2358 this->text->update(wtext);
2359 yscroll->update_length(this->text->get_text_rows(),
2360 this->text->get_text_row(),
2361 yscroll->get_handlelength(),
2365 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2372 text->reposition_window(x,
2374 w - yscroll->get_span(),
2376 yscroll->reposition_window(x + w - yscroll->get_span(),
2378 BC_TextBox::calculate_row_h(rows,
2380 yscroll->update_length(text->get_text_rows(),
2381 text->get_text_row(),
2386 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2388 this->text->set_selection(char1, char2, ibeam);
2391 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2393 this->text->wset_selection(char1, char2, ibeam);
2396 int BC_ScrollTextBox::get_ibeam_letter()
2398 return this->text->get_ibeam_letter();
2407 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2408 : BC_TextBox(gui->x, gui->y,
2409 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2410 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2415 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2416 : BC_TextBox(gui->x, gui->y,
2417 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2418 gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2423 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2432 int BC_ScrollTextBoxText::handle_event()
2434 gui->yscroll->update_length(get_text_rows(),
2436 gui->yscroll->get_handlelength(),
2438 return gui->handle_event();
2441 int BC_ScrollTextBoxText::motion_event()
2443 gui->yscroll->update_length(get_text_rows(),
2445 gui->yscroll->get_handlelength(),
2451 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2452 : BC_ScrollBar(gui->x +
2454 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2457 BC_TextBox::calculate_row_h(gui->rows,
2458 gui->parent_window),
2459 gui->text->get_text_rows(),
2466 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2470 int BC_ScrollTextBoxYScroll::handle_event()
2472 gui->text->set_text_row(get_position());
2485 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2486 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2488 this->popup = popup;
2491 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2492 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2494 this->popup = popup;
2497 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2507 int BC_PopupTextBoxText::handle_event()
2509 popup->handle_event();
2513 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2515 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2516 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2518 this->popup = popup;
2520 int BC_PopupTextBoxList::handle_event()
2522 BC_ListBoxItem *item = get_selection(0, 0);
2525 popup->textbox->update(item->get_text());
2526 popup->handle_event();
2534 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2535 ArrayList<BC_ListBoxItem*> *list_items,
2536 const char *default_text, int x, int y,
2537 int text_w, int list_h, int list_format)
2541 this->list_h = list_h;
2542 this->list_format = list_format;
2543 this->default_text = (char*)default_text;
2544 this->default_wtext = 0;
2545 this->text_w = text_w;
2546 this->parent_window = parent_window;
2547 this->list_items = list_items;
2550 BC_PopupTextBox::~BC_PopupTextBox()
2560 int BC_PopupTextBox::create_objects()
2562 int x = this->x, y = this->y;
2563 parent_window->add_subwindow(textbox = default_wtext ?
2564 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2565 new BC_PopupTextBoxText(this, x, y, default_text));
2566 x += textbox->get_w();
2567 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2571 void BC_PopupTextBox::update(const char *text)
2573 textbox->update(text);
2576 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2578 listbox->update(data,
2585 const char* BC_PopupTextBox::get_text()
2587 return textbox->get_text();
2590 const wchar_t* BC_PopupTextBox::get_wtext()
2592 return textbox->get_wtext();
2595 int BC_PopupTextBox::get_number()
2597 return listbox->get_selection_number(0, 0);
2600 int BC_PopupTextBox::get_x()
2605 int BC_PopupTextBox::get_y()
2610 int BC_PopupTextBox::get_w()
2612 return textbox->get_w() + listbox->get_w();
2615 int BC_PopupTextBox::get_h()
2617 return textbox->get_h();
2620 int BC_PopupTextBox::handle_event()
2625 void BC_PopupTextBox::reposition_window(int x, int y)
2630 textbox->reposition_window(x1,
2633 textbox->get_rows());
2634 x1 += textbox->get_w();
2635 listbox->reposition_window(x1, y1, -1, -1, 0);
2636 // if(flush) parent_window->flush();
2652 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2653 int64_t default_value,
2658 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2660 this->popup = popup;
2663 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2664 float default_value,
2669 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2671 this->popup = popup;
2674 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2686 int BC_TumbleTextBoxText::handle_event()
2688 popup->handle_event();
2692 int BC_TumbleTextBoxText::button_press_event()
2696 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
2698 if(get_buttonpress() == 4)
2700 popup->tumbler->handle_up_event();
2703 if(get_buttonpress() == 5)
2705 popup->tumbler->handle_down_event();
2715 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2716 int64_t default_value,
2728 this->default_value = default_value;
2729 this->text_w = text_w;
2730 this->parent_window = parent_window;
2736 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2749 this->default_value = default_value;
2750 this->text_w = text_w;
2751 this->parent_window = parent_window;
2757 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2758 float default_value_f,
2768 this->min_f = min_f;
2769 this->max_f = max_f;
2770 this->default_value_f = default_value_f;
2771 this->text_w = text_w;
2772 this->parent_window = parent_window;
2778 BC_TumbleTextBox::~BC_TumbleTextBox()
2780 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2781 // is deleted anyway by the windowbase so textbox deletes this.
2782 if(tumbler) delete tumbler;
2784 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2793 void BC_TumbleTextBox::reset()
2800 void BC_TumbleTextBox::set_precision(int precision)
2802 this->precision = precision;
2805 void BC_TumbleTextBox::set_increment(float value)
2807 this->increment = value;
2808 if(tumbler) tumbler->set_increment(value);
2811 void BC_TumbleTextBox::set_log_floatincrement(int value)
2813 this->log_floatincrement = value;
2814 if(tumbler) tumbler->set_log_floatincrement(value);
2817 int BC_TumbleTextBox::create_objects()
2819 int x = this->x, y = this->y;
2823 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2829 textbox->set_precision(precision);
2832 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2839 x += textbox->get_w();
2842 parent_window->add_subwindow(tumbler = new BC_FTumbler(textbox,
2848 parent_window->add_subwindow(tumbler = new BC_ITumbler(textbox,
2854 tumbler->set_increment(increment);
2858 const char* BC_TumbleTextBox::get_text()
2860 return textbox->get_text();
2863 const wchar_t* BC_TumbleTextBox::get_wtext()
2865 return textbox->get_wtext();
2868 BC_TextBox* BC_TumbleTextBox::get_textbox()
2873 int BC_TumbleTextBox::update(const char *value)
2875 textbox->update(value);
2879 int BC_TumbleTextBox::update(int64_t value)
2881 textbox->update(value);
2885 int BC_TumbleTextBox::update(float value)
2887 textbox->update(value);
2892 int BC_TumbleTextBox::get_x()
2897 int BC_TumbleTextBox::get_y()
2902 int BC_TumbleTextBox::get_w()
2904 return textbox->get_w() + tumbler->get_w();
2907 int BC_TumbleTextBox::get_h()
2909 return textbox->get_h();
2912 void BC_TumbleTextBox::disable()
2914 if( !get_enabled() ) return;
2915 tumbler->hide_window(0);
2916 return textbox->disable();
2919 void BC_TumbleTextBox::enable()
2921 if( get_enabled() ) return;
2922 tumbler->show_window(0);
2923 return textbox->enable();
2926 int BC_TumbleTextBox::get_enabled()
2928 return textbox->get_enabled();
2931 int BC_TumbleTextBox::handle_event()
2936 void BC_TumbleTextBox::reposition_window(int x, int y)
2941 textbox->reposition_window(x,
2945 tumbler->reposition_window(x + textbox->get_w(),
2947 // if(flush) parent_window->flush();
2951 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2953 tumbler->set_boundaries(min, max);
2956 void BC_TumbleTextBox::set_boundaries(float min, float max)
2958 tumbler->set_boundaries(min, max);