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;
169 skip_cursor = new Timer;
183 int BC_TextBox::tstrlen()
185 if( !tsize ) return strlen(text);
186 return strnlen(text, tsize);
189 int BC_TextBox::tstrcmp(const char *cp)
191 if( !tsize ) return strcmp(text, cp);
192 return strncmp(text, cp, tsize);
195 char *BC_TextBox::tstrcpy(const char *cp)
200 return strcpy(text, cp);
201 strncpy(text, cp, tsize);
209 char *BC_TextBox::tstrcat(const char *cp)
212 if( !tsize ) return strcat(text, cp);
213 char *result = strncat(text, cp, tsize);
218 int BC_TextBox::wtext_update()
221 const char *src_enc = is_utf8 ? "UTF8" : BC_Resources::encoding;
222 const char *dst_enc = BC_Resources::wide_encoding;
223 int nsize = tsize > 0 ? tsize : strlen(text) + BCTEXTLEN;
224 if( nsize > wsize || !wtext ) {
225 wchar_t *ntext = new wchar_t[nsize+1];
226 memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
227 delete [] wtext; wtext = ntext; wsize = nsize;
228 int *npositions = new int[nsize+1];
230 memcpy(npositions, positions, (plen+1)*sizeof(positions[0]));
233 delete [] positions; positions = npositions; plen = 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()
257 skip_cursor = new Timer;
258 skip_cursor->update();
260 text_ascent = get_text_ascent(font) + 1;
261 text_descent = get_text_descent(font) + 1;
262 text_height = text_ascent + text_descent;
263 ibeam_letter = wtext_update();
266 left_margin = right_margin = HORIZONTAL_MARGIN;
267 top_margin = bottom_margin = VERTICAL_MARGIN;
271 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
272 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
275 text_x = left_margin;
279 // Create the subwindow
280 BC_SubWindow::initialize();
282 BC_Resources *resources = get_resources();
285 back_color = resources->text_background;
286 high_color = resources->text_background_hi;
290 high_color = resources->text_background_noborder_hi;
291 back_color = bg_color;
295 set_cursor(IBEAM_CURSOR, 0, 0);
300 int BC_TextBox::calculate_h(BC_WindowBase *gui,
305 return rows * (gui->get_text_ascent(font) + 1 +
306 gui->get_text_descent(font) + 1) +
307 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
310 void BC_TextBox::set_precision(int precision)
312 this->precision = precision;
315 // Compute suggestions for a path
316 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries)
318 // Let user delete suggestion
319 if(get_last_keypress() != BACKSPACE)
322 // Compute suggestions
324 ArrayList<char*> suggestions;
325 const char *current_text = get_text();
327 // If directory, tabulate it
328 if(current_text[0] == '/' ||
329 current_text[0] == '~')
331 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
332 char string[BCTEXTLEN];
333 strncpy(string, current_text, sizeof(string));
334 char *ptr = strrchr(string, '/');
335 if(!ptr) ptr = strrchr(string, '~');
337 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
339 int suggestion_column = ptr + 1 - string;
341 fs.set_filter(get_resources()->filebox_filter);
342 // fs.set_sort_order(filebox->sort_order);
343 // fs.set_sort_field(filebox->column_type[filebox->sort_column]);
346 //printf("BC_TextBox::calculate_suggestions %d %c %s\n", __LINE__, *ptr, string);
347 if(current_text[0] == '~' && *ptr != '/')
353 fs.parse_tildas(string);
356 //printf("BC_TextBox::calculate_suggestions %d %d\n", __LINE__, fs.total_files());
359 // Accept only entries with matching trailing characters
360 ptr = strrchr((char*)current_text, '/');
361 if(!ptr) ptr = strrchr((char*)current_text, '~');
363 //printf("BC_TextBox::calculate_suggestions %d %s %p\n", __LINE__, current_text, ptr);
368 for(int i = 0; i < fs.total_files(); i++)
370 char *current_name = fs.get_entry(i)->name;
371 if(!strncmp(ptr, current_name, strlen(ptr)))
373 suggestions.append(current_name);
374 //printf("BC_TextBox::calculate_suggestions %d %s\n", __LINE__, current_name);
379 // Accept all entries
380 for(int i = 0; i < fs.total_files(); i++)
382 //printf("BC_TextBox::calculate_suggestions %d %s\n", __LINE__, fs.get_entry(i)->name);
383 suggestions.append(fs.get_entry(i)->name);
385 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
387 // Add 1 to column to keep /
388 set_suggestions(&suggestions, suggestion_column);
389 //printf("BC_TextBox::calculate_suggestions %d\n", __LINE__);
392 // Get entries from current listbox with matching trailing characters
395 // printf("BC_TextBox::calculate_suggestions %d %d\n",
398 for(int i = 0; i < entries->size(); i++)
400 char *current_name = entries->get(i)->get_text();
402 //printf("BC_TextBox::calculate_suggestions %d %s %s\n", __LINE__, current_text, current_name);
403 if(!strncmp(current_text, current_name, strlen(current_text)))
405 suggestions.append(current_name);
409 set_suggestions(&suggestions, 0);
416 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
419 this->suggestions->remove_all_objects();
420 this->suggestion_column = column;
424 for(int i = 0; i < suggestions->size(); i++)
426 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
429 // Show the popup without taking focus
430 if(suggestions->size() > 1)
432 if(!suggestions_popup)
435 get_parent()->add_subwindow(suggestions_popup =
436 new BC_TextBoxSuggestions(this, x, y));
437 suggestions_popup->set_is_suggestions(1);
438 suggestions_popup->activate(0);
442 suggestions_popup->update(this->suggestions,
446 suggestions_popup->activate(0);
450 // Show the highlighted text
451 if(suggestions->size() == 1)
453 highlight_letter1 = wtext_update();
454 text_update(wtext,wlen, text,tsize);
455 char *current_suggestion = suggestions->get(0);
456 int col = highlight_letter1 - suggestion_column;
457 if( col < 0 ) col = 0;
458 char *cur = current_suggestion + col;
460 highlight_letter2 = wtext_update();
461 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
465 delete suggestions_popup;
466 suggestions_popup = 0;
471 if(!suggestions || !this->suggestions->size())
473 delete suggestions_popup;
474 suggestions_popup = 0;
478 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
480 highlight_letter1 = char1;
481 highlight_letter2 = char2;
482 ibeam_letter = ibeam;
486 int BC_TextBox::update(const char *text)
488 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
489 // Don't update if contents are the same
490 if(!tstrcmp(text)) return 0;
492 int wtext_len = wtext_update();
493 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
494 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
495 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
500 int BC_TextBox::update(const wchar_t *wtext)
502 int wtext_len = wcslen(wtext);
503 if( wtext_len >= wsize ) wtext_len = wsize;
504 wcsncpy(this->wtext, wtext, wtext_len);
506 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
507 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
508 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
513 int BC_TextBox::update(int64_t value)
515 char string[BCTEXTLEN];
516 sprintf(string, "%jd", value);
521 int BC_TextBox::update(float value)
523 char string[BCTEXTLEN];
524 sprintf(string, "%0.*f", precision, value);
529 void BC_TextBox::disable()
534 if(active) top_level->deactivate();
540 void BC_TextBox::enable()
550 int BC_TextBox::get_enabled()
555 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
557 return (pixels - 4) /
558 (window->get_text_ascent(font) + 1 +
559 window->get_text_descent(font) + 1);
562 int BC_TextBox::calculate_row_h(int rows,
563 BC_WindowBase *parent_window,
568 (parent_window->get_text_ascent(font) + 1 +
569 parent_window->get_text_descent(font) + 1) +
570 (has_border ? 4 : 0);
573 const char* BC_TextBox::get_text()
575 int wtext_len = wtext_update();
576 text_update(wtext,wtext_len, text,tsize);
580 const wchar_t* BC_TextBox::get_wtext()
586 void BC_TextBox::set_text(char *text, int isz)
588 if( size > 0 || isz < 0 ) return;
597 int BC_TextBox::get_text_rows()
599 int wtext_len = wtext_update();
601 for(int i = 0; i < wtext_len; i++) {
602 if(wtext[i] == '\n') result++;
608 int BC_TextBox::get_row_h(int rows)
610 return rows * text_height + top_margin + bottom_margin;
613 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
616 if(w < 0) w = get_w();
619 new_h = get_row_h(rows);
628 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
629 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
630 BC_WindowBase::reposition_window(x, y, w, new_h);
636 void BC_TextBox::draw_border()
638 BC_Resources *resources = get_resources();
640 set_color(background_color);
641 draw_box(0, 0, left_margin, get_h());
642 draw_box(get_w() - right_margin, 0, right_margin, get_h());
647 draw_3d_border(0, 0, w, h,
648 resources->text_border1,
649 resources->text_border2_hi,
650 resources->text_border3_hi,
651 resources->text_border4);
653 draw_3d_border(0, 0, w, h,
654 resources->text_border1,
655 resources->text_border2,
656 resources->text_border3,
657 resources->text_border4);
661 void BC_TextBox::draw_cursor()
663 // set_color(background_color);
671 draw_box(ibeam_x + text_x,
680 void BC_TextBox::draw(int flush)
683 int row_begin, row_end;
684 int highlight_x1, highlight_x2;
686 BC_Resources *resources = get_resources();
688 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
691 background_color = resources->text_background;
693 background_color = high_color;
695 background_color = back_color;
696 set_color(background_color);
697 draw_box(0, 0, w, h);
699 int wtext_len = wtext_update();
701 // Draw text with selection
704 for(i=0, j=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
707 wchar_t *wtext_row = &wtext[i];
708 for(j=0; j<BCTEXTLEN-1 && i<wtext_len && wtext[i]!='\n'; ++i, ++j);
709 if( (row_end=i) < wtext_len ) ++i;
711 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
712 // Draw highlighted region of row
713 if(highlight_letter2 > highlight_letter1 &&
714 highlight_letter2 > row_begin &&
715 highlight_letter1 <= row_end) {
716 if(active && enabled && get_has_focus())
717 set_color(resources->text_highlight);
719 set_color(resources->text_inactive_highlight);
721 if(highlight_letter1 >= row_begin &&
722 highlight_letter1 <= row_end)
723 highlight_x1 = positions[highlight_letter1];
727 if(highlight_letter2 > row_begin &&
728 highlight_letter2 <= row_end)
729 highlight_x2 = positions[highlight_letter2];
731 highlight_x2 = get_w();
733 draw_box(highlight_x1 + text_x, k,
734 highlight_x2 - highlight_x1, text_height);
737 // Draw text over highlight
738 int len = row_end - row_begin;
740 set_color(enabled ? resources->text_default : DMGREY);
741 draw_wtext(text_x, k + text_ascent, wtext_row, len,
742 0, &positions[wtext_row - wtext]);
745 // Get ibeam location
746 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
748 ibeam_y = k - text_y;
749 ibeam_x = positions[ibeam_letter];
754 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
756 if( wtext_len == 0 ) {
766 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
776 int BC_TextBox::focus_in_event()
782 int BC_TextBox::focus_out_event()
788 int BC_TextBox::cursor_enter_event()
790 if(top_level->event_win == win && enabled)
804 int BC_TextBox::cursor_leave_event()
816 int BC_TextBox::button_press_event()
820 if(!enabled) return 0;
821 if(get_buttonpress() != WHEEL_UP &&
822 get_buttonpress() != WHEEL_DOWN &&
823 get_buttonpress() != LEFT_BUTTON &&
824 get_buttonpress() != MIDDLE_BUTTON) return 0;
828 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
830 int cursor_letter = 0;
831 int wtext_len = wtext_update();
832 int update_scroll = 0;
835 if(top_level->event_win == win)
840 top_level->deactivate();
845 if(get_buttonpress() == WHEEL_UP)
847 text_y += text_height;
848 text_y = MIN(text_y, top_margin);
852 if(get_buttonpress() == WHEEL_DOWN)
854 int min_y = -(get_text_rows() *
858 text_y -= text_height;
859 text_y = MAX(text_y, min_y);
860 text_y = MIN(text_y, top_margin);
866 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
868 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
871 if(get_triple_click())
873 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
875 select_line(highlight_letter1, highlight_letter2, cursor_letter);
876 highlight_letter3 = highlight_letter1;
877 highlight_letter4 = highlight_letter2;
878 ibeam_letter = highlight_letter2;
879 copy_selection(PRIMARY_SELECTION);
882 if(get_double_click())
885 select_word(highlight_letter1, highlight_letter2, cursor_letter);
886 highlight_letter3 = highlight_letter1;
887 highlight_letter4 = highlight_letter2;
888 ibeam_letter = highlight_letter2;
889 copy_selection(PRIMARY_SELECTION);
892 if(get_buttonpress() == MIDDLE_BUTTON)
894 highlight_letter3 = highlight_letter4 =
895 ibeam_letter = highlight_letter1 =
896 highlight_letter2 = cursor_letter;
897 paste_selection(PRIMARY_SELECTION);
902 highlight_letter3 = highlight_letter4 =
903 ibeam_letter = highlight_letter1 =
904 highlight_letter2 = cursor_letter;
908 // Handle scrolling by highlighting text
909 if(text_selected || word_selected || line_selected)
911 set_repeat(top_level->get_resources()->scroll_repeat);
914 if(ibeam_letter < 0) ibeam_letter = 0;
915 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
919 if(update_scroll && yscroll)
921 yscroll->update_length(get_text_rows(),
923 yscroll->get_handlelength(),
930 if(active && (!yscroll || !yscroll->is_event_win()))
932 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
933 // Suggestion popup is not active but must be deactivated.
934 if(suggestions_popup)
937 // printf("BC_TextBox::button_press_event %d\n", __LINE__);
938 // // Pass event to suggestions popup
939 // if(!suggestions_popup->button_press_event())
941 // printf("BC_TextBox::button_press_event %d\n", __LINE__);
942 // top_level->deactivate();
947 top_level->deactivate();
955 int BC_TextBox::button_release_event()
960 if(text_selected || word_selected || line_selected)
966 // Stop scrolling by highlighting text
967 unset_repeat(top_level->get_resources()->scroll_repeat);
973 int BC_TextBox::cursor_motion_event()
975 int cursor_letter, letter1, letter2;
978 if(text_selected || word_selected || line_selected)
980 cursor_letter = get_cursor_letter(top_level->cursor_x,
981 top_level->cursor_y);
983 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
987 select_line(letter1, letter2, cursor_letter);
992 select_word(letter1, letter2, cursor_letter);
997 letter1 = letter2 = cursor_letter;
1000 if(letter1 <= highlight_letter3)
1002 highlight_letter1 = letter1;
1003 highlight_letter2 = highlight_letter4;
1004 ibeam_letter = letter1;
1007 if(letter2 >= highlight_letter4)
1009 highlight_letter2 = letter2;
1010 highlight_letter1 = highlight_letter3;
1011 ibeam_letter = letter2;
1014 copy_selection(PRIMARY_SELECTION);
1027 int BC_TextBox::activate()
1029 top_level->active_subwindow = this;
1032 top_level->set_repeat(top_level->get_resources()->blink_rate);
1036 int BC_TextBox::deactivate()
1038 //printf("BC_TextBox::deactivate %d suggestions_popup=%p\n", __LINE__, suggestions_popup);
1040 top_level->unset_repeat(top_level->get_resources()->blink_rate);
1041 if(suggestions_popup)
1043 // Must deactivate instead of delete since this is called from BC_ListBox::button_press_event
1044 // suggestions_popup->deactivate();
1046 delete suggestions_popup;
1047 suggestions_popup = 0;
1054 int BC_TextBox::repeat_event(int64_t duration)
1057 int cursor_y = get_cursor_y();
1058 //int cursor_x = get_cursor_x();
1060 if(duration == top_level->get_resources()->tooltip_delay &&
1061 tooltip_text && tooltip_text[0] != 0 && highlighted)
1068 if(duration == top_level->get_resources()->blink_rate &&
1072 // don't flash if keypress
1073 if(skip_cursor->get_difference() < 500)
1075 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1076 // skip_cursor->get_difference(),
1082 if(!(text_selected || word_selected || line_selected))
1091 if(duration == top_level->get_resources()->scroll_repeat &&
1092 (text_selected || word_selected || line_selected))
1095 if(get_cursor_y() < top_margin)
1097 difference = get_cursor_y() - top_margin ;
1100 if(get_cursor_y() > get_h() - bottom_margin)
1102 difference = get_cursor_y() -
1103 (get_h() - bottom_margin);
1105 if(difference != 0) {
1106 int min_y = -(get_text_rows() * text_height -
1107 get_h() + bottom_margin);
1109 text_y -= difference;
1110 // printf("BC_TextBox::repeat_event %d %d %d\n",
1114 text_y = MAX(min_y, text_y);
1115 text_y = MIN(text_y, top_margin);
1122 if(get_cursor_x() < left_margin)
1124 int difference = left_margin - get_cursor_x();
1126 text_x += difference;
1127 text_x = MIN(text_x, left_margin);
1131 else if(get_cursor_x() > get_w() - right_margin)
1133 int difference = get_cursor_x() - (get_w() - right_margin);
1134 int new_text_x = text_x - difference;
1136 // Get width of current row
1139 int wtext_len = wtext_update();
1142 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1145 while(wtext[i] != '\n' && i < wtext_len) {
1149 if(wtext[i] == '\n') i++;
1151 if(cursor_y >= k && cursor_y < k + text_height) {
1152 row_width = get_text_width(font,
1154 row_end - row_begin);
1160 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1161 new_text_x = MAX(new_text_x, min_x);
1162 new_text_x = MIN(new_text_x, left_margin);
1164 if(new_text_x < text_x) text_x = new_text_x;
1173 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1175 if((top_level->get_keypress() == RETURN) ||
1176 (top_level->get_keypress() > 30 && top_level->get_keypress() <= 255))
1179 wchar_t *temp_string = top_level->get_wkeystring(&len);
1180 if(top_level->get_keypress() == RETURN) {
1181 temp_string[0] = '\n'; temp_string[1] = 0;
1184 insert_text(temp_string, len);
1192 int BC_TextBox::keypress_event()
1194 // Result == 2 contents changed
1195 // Result == 1 trapped keypress
1196 // Result == 0 nothing
1198 int dispatch_event = 0;
1200 if(!active || !enabled) return 0;
1202 int wtext_len = wtext_update();
1203 last_keypress = get_keypress();
1204 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1205 switch(last_keypress)
1208 // Deactivate the suggestions
1209 if(suggestions && suggestions_popup)
1211 delete suggestions_popup;
1212 suggestions_popup = 0;
1217 top_level->deactivate();
1229 top_level->deactivate();
1235 default_keypress(dispatch_event, result);
1242 // Handle like a default keypress
1244 top_level->cycle_textboxes(1);
1251 top_level->cycle_textboxes(-1);
1256 if(ibeam_letter > 0)
1258 int old_ibeam_letter = ibeam_letter;
1268 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1274 if(top_level->shift_down())
1276 // Initialize highlighting
1277 if(highlight_letter1 == highlight_letter2)
1279 highlight_letter1 = ibeam_letter;
1280 highlight_letter2 = old_ibeam_letter;
1283 // Extend left highlight
1284 if(highlight_letter1 == old_ibeam_letter)
1286 highlight_letter1 = ibeam_letter;
1289 // Shrink right highlight
1290 if(highlight_letter2 == old_ibeam_letter)
1292 highlight_letter2 = ibeam_letter;
1297 highlight_letter1 = highlight_letter2 = ibeam_letter;
1302 if(keypress_draw) draw(1);
1308 if(ibeam_letter < wtext_len)
1310 int old_ibeam_letter = ibeam_letter;
1319 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]))
1326 if(top_level->shift_down())
1328 // Initialize highlighting
1329 if(highlight_letter1 == highlight_letter2)
1331 highlight_letter1 = old_ibeam_letter;
1332 highlight_letter2 = ibeam_letter;
1335 // Shrink left highlight
1336 if(highlight_letter1 == old_ibeam_letter)
1338 highlight_letter1 = ibeam_letter;
1341 // Expand right highlight
1342 if(highlight_letter2 == old_ibeam_letter)
1344 highlight_letter2 = ibeam_letter;
1349 highlight_letter1 = highlight_letter2 = ibeam_letter;
1353 if(keypress_draw) draw(1);
1359 if(suggestions && suggestions_popup)
1361 // Pass to suggestions popup
1362 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1363 suggestions_popup->activate(1);
1364 suggestions_popup->keypress_event();
1368 if(ibeam_letter > 0)
1370 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1371 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1372 ibeam_y + text_y - text_height);
1373 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1376 if(top_level->shift_down())
1378 // Initialize highlighting
1379 if(highlight_letter1 == highlight_letter2)
1381 highlight_letter1 = new_letter;
1382 highlight_letter2 = ibeam_letter;
1385 // Expand left highlight
1386 if(highlight_letter1 == ibeam_letter)
1388 highlight_letter1 = new_letter;
1391 // Shrink right highlight
1392 if(highlight_letter2 == ibeam_letter)
1394 highlight_letter2 = new_letter;
1398 highlight_letter1 = highlight_letter2 = new_letter;
1400 if(highlight_letter1 > highlight_letter2)
1402 int temp = highlight_letter1;
1403 highlight_letter1 = highlight_letter2;
1404 highlight_letter2 = temp;
1406 ibeam_letter = new_letter;
1409 if(keypress_draw) draw(1);
1415 if(ibeam_letter > 0)
1417 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1418 ibeam_y + text_y - get_h());
1421 if(top_level->shift_down())
1423 // Initialize highlighting
1424 if(highlight_letter1 == highlight_letter2)
1426 highlight_letter1 = new_letter;
1427 highlight_letter2 = ibeam_letter;
1430 // Expand left highlight
1431 if(highlight_letter1 == ibeam_letter)
1433 highlight_letter1 = new_letter;
1436 // Shrink right highlight
1437 if(highlight_letter2 == ibeam_letter)
1439 highlight_letter2 = new_letter;
1443 highlight_letter1 = highlight_letter2 = new_letter;
1445 if(highlight_letter1 > highlight_letter2)
1447 int temp = highlight_letter1;
1448 highlight_letter1 = highlight_letter2;
1449 highlight_letter2 = temp;
1451 ibeam_letter = new_letter;
1454 if(keypress_draw) draw(1);
1460 // printf("BC_TextBox::keypress_event %d %p %p\n",
1463 // suggestions_popup);
1464 if(suggestions && suggestions_popup)
1466 // Pass to suggestions popup
1467 suggestions_popup->activate(1);
1468 suggestions_popup->keypress_event();
1472 // if(ibeam_letter > 0)
1475 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1476 ibeam_y + text_y + text_height);
1477 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1479 if(top_level->shift_down())
1481 // Initialize highlighting
1482 if(highlight_letter1 == highlight_letter2)
1484 highlight_letter1 = new_letter;
1485 highlight_letter2 = ibeam_letter;
1488 // Shrink left highlight
1489 if(highlight_letter1 == ibeam_letter)
1491 highlight_letter1 = new_letter;
1494 // Expand right highlight
1495 if(highlight_letter2 == ibeam_letter)
1497 highlight_letter2 = new_letter;
1501 highlight_letter1 = highlight_letter2 = new_letter;
1503 if(highlight_letter1 > highlight_letter2)
1505 int temp = highlight_letter1;
1506 highlight_letter1 = highlight_letter2;
1507 highlight_letter2 = temp;
1509 ibeam_letter = new_letter;
1512 if(keypress_draw) draw(1);
1514 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1522 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1523 ibeam_y + text_y + get_h());
1524 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1526 if(top_level->shift_down())
1528 // Initialize highlighting
1529 if(highlight_letter1 == highlight_letter2)
1531 highlight_letter1 = new_letter;
1532 highlight_letter2 = ibeam_letter;
1535 // Shrink left highlight
1536 if(highlight_letter1 == ibeam_letter)
1538 highlight_letter1 = new_letter;
1541 // Expand right highlight
1542 if(highlight_letter2 == ibeam_letter)
1544 highlight_letter2 = new_letter;
1548 highlight_letter1 = highlight_letter2 = new_letter;
1550 if(highlight_letter1 > highlight_letter2)
1552 int temp = highlight_letter1;
1553 highlight_letter1 = highlight_letter2;
1554 highlight_letter2 = temp;
1556 ibeam_letter = new_letter;
1559 if(keypress_draw) draw(1);
1561 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1568 delete suggestions_popup;
1569 suggestions_popup = 0;
1571 int old_ibeam_letter = ibeam_letter;
1573 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1576 if(top_level->shift_down())
1579 if(highlight_letter1 == highlight_letter2)
1581 highlight_letter2 = ibeam_letter;
1582 highlight_letter1 = old_ibeam_letter;
1586 if(highlight_letter1 == old_ibeam_letter)
1588 highlight_letter1 = highlight_letter2;
1589 highlight_letter2 = ibeam_letter;
1593 if(highlight_letter2 == old_ibeam_letter)
1595 highlight_letter2 = ibeam_letter;
1599 highlight_letter1 = highlight_letter2 = ibeam_letter;
1602 if(keypress_draw) draw(1);
1609 delete suggestions_popup;
1610 suggestions_popup = 0;
1612 int old_ibeam_letter = ibeam_letter;
1614 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1617 if(top_level->shift_down())
1620 if(highlight_letter1 == highlight_letter2)
1622 highlight_letter2 = old_ibeam_letter;
1623 highlight_letter1 = ibeam_letter;
1627 if(highlight_letter1 == old_ibeam_letter)
1629 highlight_letter1 = ibeam_letter;
1633 if(highlight_letter2 == old_ibeam_letter)
1635 highlight_letter2 = highlight_letter1;
1636 highlight_letter1 = ibeam_letter;
1640 highlight_letter1 = highlight_letter2 = ibeam_letter;
1643 if(keypress_draw) draw(1);
1649 if(suggestions_popup)
1651 delete suggestions_popup;
1652 suggestions_popup = 0;
1655 if(highlight_letter1 == highlight_letter2)
1657 if(ibeam_letter > 0)
1659 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1665 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1666 highlight_letter2 = ibeam_letter = highlight_letter1;
1670 if(keypress_draw) draw(1);
1676 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1677 if(highlight_letter1 == highlight_letter2)
1679 if(ibeam_letter < wtext_len)
1681 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1686 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1687 highlight_letter2 = ibeam_letter = highlight_letter1;
1691 if(keypress_draw) draw(1);
1701 if(get_keypress() == 'c' || get_keypress() == 'C')
1703 if(highlight_letter1 != highlight_letter2)
1705 copy_selection(SECONDARY_SELECTION);
1710 if(get_keypress() == 'v' || get_keypress() == 'V')
1712 paste_selection(SECONDARY_SELECTION);
1714 if(keypress_draw) draw(1);
1719 if(get_keypress() == 'x' || get_keypress() == 'X')
1721 if(highlight_letter1 != highlight_letter2)
1723 copy_selection(SECONDARY_SELECTION);
1724 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1725 highlight_letter2 = ibeam_letter = highlight_letter1;
1729 if(keypress_draw) draw(1);
1737 default_keypress(dispatch_event, result);
1741 if(result) skip_cursor->update();
1742 if(dispatch_event && handle_event())
1749 int BC_TextBox::uses_text()
1755 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1758 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1759 wtext[i] = wtext[j];
1767 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1769 if( len < 0 ) len = wcslen(wcp);
1770 int wtext_len = wtext_update();
1771 if(highlight_letter1 < highlight_letter2)
1773 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1774 highlight_letter2 = ibeam_letter = highlight_letter1;
1775 wtext_len = wtext_update();
1780 for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1781 if( j >= wsize ) continue;
1782 wtext[j] = wtext[i];
1785 for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1786 if( i >= wsize ) break;
1789 if( (wlen+=len) > wsize ) wlen = wsize;
1790 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1791 wtext[wlen] = 0; // wtext allocated wsize+1
1795 int BC_TextBox::is_separator(const char *txt, int i)
1797 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1798 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1801 // used for time entry
1802 void BC_TextBox::do_separators(int ibeam_left)
1806 // Remove separators from text
1807 int wtext_len = wtext_update();
1808 for(int i = 0; i < wtext_len; ) {
1809 if( !iswalnum(wtext[i]) ) {
1810 for(int j = i; j < wtext_len - 1; j++)
1811 wtext[j] = wtext[j + 1];
1812 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1818 wtext[wtext_len] = 0;
1824 // Insert separators into text
1825 int separator_len = strlen(separators);
1826 for(int i = 0; i < separator_len; i++) {
1828 // Insert a separator
1829 if( is_separator(separators,i) ) {
1830 for(int j = wtext_len; j >= i; j--) {
1831 wtext[j + 1] = wtext[j];
1833 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1835 wtext[i] = separators[i];
1839 wtext[i] = separators[i];
1844 wtext[separator_len] = 0;
1845 wlen = separator_len;
1849 void BC_TextBox::get_ibeam_position(int &x, int &y)
1851 int i, row_begin, row_end;
1852 int wtext_len = wtext_update();
1856 for( i=0; i<wtext_len; ) {
1858 for(; i<wtext_len && wtext[i]!='\n'; i++);
1861 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1862 x = get_text_width(font, &wtext[row_begin], ibeam_letter - row_begin);
1863 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1867 if( i < wtext_len && wtext[i] == '\n' ) {
1872 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1878 void BC_TextBox::set_text_row(int row)
1880 text_x = left_margin;
1881 text_y = -(row * text_height) + top_margin;
1885 int BC_TextBox::get_text_row()
1887 return -(text_y - top_margin) / text_height;
1890 void BC_TextBox::find_ibeam(int dispatch_event)
1893 int old_x = text_x, old_y = text_y;
1895 get_ibeam_position(x, y);
1897 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1899 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1900 if(text_x > left_margin) text_x = left_margin;
1903 if(left_margin + text_x + x < left_margin)
1905 text_x = -(x - (get_w() / 4)) + left_margin;
1906 if(text_x > left_margin) text_x = left_margin;
1909 int text_row = y / text_height;
1910 if( text_row < rows ) text_y = top_margin;
1912 int pix_rows = get_h() - bottom_margin - (y + text_y);
1913 if( pix_rows < text_height )
1914 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1916 pix_rows = y + text_y - top_margin;
1917 if( pix_rows < 0 ) {
1918 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1919 if( text_y > top_margin ) text_y = top_margin;
1922 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1926 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1928 int i, j, k, row_begin, row_end, result = 0, done = 0;
1929 int column1, column2;
1930 int got_visible_row = 0;
1932 // Select complete row if cursor above the window
1933 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1934 if(cursor_y < text_y - text_height)
1940 int wtext_len = wtext_update();
1942 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1943 // Simulate drawing of 1 row
1945 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1949 int first_visible_row = 0;
1950 int last_visible_row = 0;
1951 if( k+text_height > top_margin && !got_visible_row) {
1952 first_visible_row = 1;
1953 got_visible_row = 1;
1956 if( (k+text_height >= get_h() - bottom_margin ||
1957 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1958 k + text_height > 0)) )
1959 last_visible_row = 1;
1961 // Cursor is inside vertical range of row
1962 if((cursor_y >= top_margin &&
1963 cursor_y < get_h() - bottom_margin &&
1964 cursor_y >= k && cursor_y < k + text_height) ||
1965 // Cursor is above 1st row
1966 (cursor_y < k + text_height && first_visible_row) ||
1967 // Cursor is below last row
1968 (cursor_y >= k && last_visible_row))
1970 column1 = column2 = 0;
1971 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1972 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1973 if((column2 + column1) / 2 >= cursor_x) {
1976 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1977 // __LINE__, result, first_visible_row, last_visible_row);
1988 if(wtext[i] == '\n') i++;
1990 // Select complete row if last visible & cursor is below window
1991 if(last_visible_row && cursor_y > k + text_height * 2)
1994 if(i >= wtext_len && !done) {
2000 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
2001 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
2002 if(result < 0) result = 0;
2003 if(result > wtext_len) {
2004 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
2012 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
2014 int i, j, k, row_begin, row_end, result = 0, done = 0;
2015 int column1, column2;
2016 int wtext_len = wtext_update();
2018 if(cursor_y < text_y) {
2023 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
2025 for(; wtext[i] != '\n' && i < wtext_len; i++);
2028 if(cursor_y >= k && cursor_y < k + text_height) {
2029 column1 = column2 = 0;
2030 for(j = 0; j <= row_end - row_begin && !done; j++) {
2031 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
2032 if((column2 + column1) / 2 >= cursor_x) {
2033 result = row_begin + j - 1;
2044 if(wtext[i] == '\n') i++;
2046 if(i >= wtext_len && !done) {
2050 if(result < 0) result = 0;
2051 if(result > wtext_len) result = wtext_len;
2056 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2058 int wtext_len = wtext_update();
2059 if(!wtext_len) return;
2061 letter1 = letter2 = ibeam_letter;
2063 if(iswalnum(wtext[letter1])) letter1--;
2064 } while(letter1 > 0 && iswalnum(wtext[letter1]));
2066 if( !iswalnum(wtext[letter1]) ) letter1++;
2069 if( iswalnum(wtext[letter2]) ) letter2++;
2070 } while( letter2 < wtext_len && isalnum(wtext[letter2]) );
2072 if( letter2 < wtext_len && wtext[letter2] == ' ') letter2++;
2074 if(letter1 < 0) letter1 = 0;
2075 if(letter2 < 0) letter2 = 0;
2076 if(letter1 > wtext_len) letter1 = wtext_len;
2077 if(letter2 > wtext_len) letter2 = wtext_len;
2080 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2082 int wtext_len = wtext_update();
2083 if(!wtext_len) return;
2085 letter1 = letter2 = ibeam_letter;
2087 // Rewind to previous linefeed
2089 if( wtext[letter1] != '\n' ) letter1--;
2090 } while( letter1 > 0 && wtext[letter1] != '\n' );
2091 if( wtext[letter1] == '\n' ) letter1++;
2093 // Advance to next linefeed
2095 if( wtext[letter2] != '\n' ) letter2++;
2096 } while( letter2 < wtext_len && wtext[letter2] != '\n' );
2097 if( letter2 < wtext_len && wtext[letter2] == '\n') letter2++;
2099 if(letter1 < 0) letter1 = 0;
2100 if(letter2 < 0) letter2 = 0;
2101 if(letter1 > wtext_len) letter1 = wtext_len;
2102 if(letter2 > wtext_len) letter2 = wtext_len;
2105 void BC_TextBox::copy_selection(int clipboard_num)
2107 int wtext_len = wtext_update();
2108 if(!wtext_len) return;
2110 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2111 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2112 highlight_letter2 - highlight_letter1 <= 0) return;
2113 int clip_len = highlight_letter2 - highlight_letter1;
2114 char ctext[clip_len+1];
2115 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2116 text_update(&wtext[highlight_letter1],clip_len, ctext,clip_len+1);
2117 get_clipboard()->to_clipboard(ctext, clip_len, clipboard_num);
2121 void BC_TextBox::paste_selection(int clipboard_num)
2123 int len = get_clipboard()->clipboard_len(clipboard_num);
2126 char cstring[len]; wchar_t wstring[len]; --len;
2127 get_clipboard()->from_clipboard(cstring, len, clipboard_num);
2128 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2129 BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2130 cstring,(len+1), (char *)wstring,(len+1)*sizeof(wchar_t));
2131 insert_text(wstring, len);
2135 void BC_TextBox::set_keypress_draw(int value)
2137 keypress_draw = value;
2140 int BC_TextBox::get_last_keypress()
2142 return last_keypress;
2145 int BC_TextBox::get_ibeam_letter()
2147 return ibeam_letter;
2150 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2152 this->ibeam_letter = number;
2159 void BC_TextBox::set_separators(const char *separators)
2161 this->separators = (char*)separators;
2164 int BC_TextBox::get_rows()
2175 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box,
2183 text_box->suggestions,
2190 this->text_box = text_box;
2192 set_justify(LISTBOX_LEFT);
2195 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2199 int BC_TextBoxSuggestions::selection_changed()
2202 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2203 BC_ListBoxItem *item = get_selection(0, 0);
2204 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2208 char *current_suggestion = item->get_text();
2209 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2210 // int text_box_len = strlen(text_box->text);
2211 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2212 strcpy(text_box->text + text_box->suggestion_column, current_suggestion);
2213 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2214 *(text_box->text + text_box->suggestion_column + strlen(current_suggestion)) = 0;
2216 //printf("BC_TextBoxSuggestions::selection_changed %d\n", __LINE__);
2218 text_box->handle_event();
2228 int BC_TextBoxSuggestions::handle_event()
2230 char *current_suggestion = 0;
2231 BC_ListBoxItem *item = get_selection(0, 0);
2232 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2233 if(item && (current_suggestion=item->get_text()) != 0)
2235 int col = text_box->suggestion_column;
2236 int len = BCTEXTLEN-1 - col;
2237 char *cp = &text_box->text[col];
2238 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2239 strncpy(cp, current_suggestion, len);
2240 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2241 if( (col=strlen(current_suggestion)) >= len )
2243 text_box->dirty = 1;
2247 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2248 text_box->highlight_letter1 =
2249 text_box->highlight_letter2 =
2250 text_box->ibeam_letter = text_box->tstrlen();
2251 text_box->wtext_update();
2253 text_box->handle_event();
2254 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2265 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2266 int x, int y, int w, int rows,
2267 const char *default_text, int default_size)
2269 this->parent_window = parent_window;
2274 this->default_text = default_text;
2275 this->default_wtext = 0;
2276 this->default_size = default_size;
2279 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2280 int x, int y, int w, int rows,
2281 const wchar_t *default_wtext, int default_size)
2283 this->parent_window = parent_window;
2288 this->default_text = 0;
2289 this->default_wtext = default_wtext;
2290 this->default_size = default_size;
2293 BC_ScrollTextBox::~BC_ScrollTextBox()
2302 void BC_ScrollTextBox::create_objects()
2304 // Must be created first
2305 parent_window->add_subwindow(text = default_wtext ?
2306 new BC_ScrollTextBoxText(this, default_wtext) :
2307 new BC_ScrollTextBoxText(this, default_text));
2308 parent_window->add_subwindow(yscroll = new BC_ScrollTextBoxYScroll(this));
2309 text->yscroll = yscroll;
2310 yscroll->bound_to = text;
2314 int BC_ScrollTextBox::handle_event()
2319 int BC_ScrollTextBox::get_x()
2324 int BC_ScrollTextBox::get_y()
2329 int BC_ScrollTextBox::get_w()
2334 int BC_ScrollTextBox::get_h()
2336 return this->text->get_h();
2339 int BC_ScrollTextBox::get_rows()
2345 const char* BC_ScrollTextBox::get_text()
2347 return text->get_text();
2350 const wchar_t* BC_ScrollTextBox::get_wtext()
2352 return text->get_wtext();
2355 void BC_ScrollTextBox::set_text(char *text, int isz)
2357 this->text->set_text(text, isz);
2358 yscroll->update_length(this->text->get_text_rows(),
2359 this->text->get_text_row(),
2360 yscroll->get_handlelength(),
2364 int BC_ScrollTextBox::set_text_row(int n)
2366 text->set_text_row(n);
2367 yscroll->update_value(n);
2371 void BC_ScrollTextBox::update(const char *text)
2373 this->text->update(text);
2374 yscroll->update_length(this->text->get_text_rows(),
2375 this->text->get_text_row(),
2376 yscroll->get_handlelength(),
2380 void BC_ScrollTextBox::update(const wchar_t *wtext)
2382 this->text->update(wtext);
2383 yscroll->update_length(this->text->get_text_rows(),
2384 this->text->get_text_row(),
2385 yscroll->get_handlelength(),
2389 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2396 text->reposition_window(x,
2398 w - yscroll->get_span(),
2400 yscroll->reposition_window(x + w - yscroll->get_span(),
2402 BC_TextBox::calculate_row_h(rows,
2404 yscroll->update_length(text->get_text_rows(),
2405 text->get_text_row(),
2410 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2412 this->text->set_selection(char1, char2, ibeam);
2421 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2422 : BC_TextBox(gui->x, gui->y,
2423 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2424 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2429 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2430 : BC_TextBox(gui->x, gui->y,
2431 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2432 gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2437 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2446 int BC_ScrollTextBoxText::handle_event()
2448 gui->yscroll->update_length(get_text_rows(),
2450 gui->yscroll->get_handlelength(),
2452 return gui->handle_event();
2455 int BC_ScrollTextBoxText::motion_event()
2457 gui->yscroll->update_length(get_text_rows(),
2459 gui->yscroll->get_handlelength(),
2465 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2466 : BC_ScrollBar(gui->x +
2468 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2471 BC_TextBox::calculate_row_h(gui->rows,
2472 gui->parent_window),
2473 gui->text->get_text_rows(),
2480 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2484 int BC_ScrollTextBoxYScroll::handle_event()
2486 gui->text->set_text_row(get_position());
2499 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2500 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2502 this->popup = popup;
2505 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2506 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2508 this->popup = popup;
2511 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2521 int BC_PopupTextBoxText::handle_event()
2523 popup->handle_event();
2527 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2529 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2530 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2532 this->popup = popup;
2534 int BC_PopupTextBoxList::handle_event()
2536 BC_ListBoxItem *item = get_selection(0, 0);
2539 popup->textbox->update(item->get_text());
2540 popup->handle_event();
2548 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2549 ArrayList<BC_ListBoxItem*> *list_items,
2550 const char *default_text, int x, int y,
2551 int text_w, int list_h, int list_format)
2555 this->list_h = list_h;
2556 this->list_format = list_format;
2557 this->default_text = (char*)default_text;
2558 this->default_wtext = 0;
2559 this->text_w = text_w;
2560 this->parent_window = parent_window;
2561 this->list_items = list_items;
2564 BC_PopupTextBox::~BC_PopupTextBox()
2574 int BC_PopupTextBox::create_objects()
2576 int x = this->x, y = this->y;
2577 parent_window->add_subwindow(textbox = default_wtext ?
2578 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2579 new BC_PopupTextBoxText(this, x, y, default_text));
2580 x += textbox->get_w();
2581 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2585 void BC_PopupTextBox::update(const char *text)
2587 textbox->update(text);
2590 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2592 listbox->update(data,
2599 const char* BC_PopupTextBox::get_text()
2601 return textbox->get_text();
2604 const wchar_t* BC_PopupTextBox::get_wtext()
2606 return textbox->get_wtext();
2609 int BC_PopupTextBox::get_number()
2611 return listbox->get_selection_number(0, 0);
2614 int BC_PopupTextBox::get_x()
2619 int BC_PopupTextBox::get_y()
2624 int BC_PopupTextBox::get_w()
2626 return textbox->get_w() + listbox->get_w();
2629 int BC_PopupTextBox::get_h()
2631 return textbox->get_h();
2634 int BC_PopupTextBox::handle_event()
2639 void BC_PopupTextBox::reposition_window(int x, int y)
2644 textbox->reposition_window(x1,
2647 textbox->get_rows());
2648 x1 += textbox->get_w();
2649 listbox->reposition_window(x1, y1, -1, -1, 0);
2650 // if(flush) parent_window->flush();
2666 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2667 int64_t default_value,
2672 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2674 this->popup = popup;
2677 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2678 float default_value,
2683 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2685 this->popup = popup;
2688 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2700 int BC_TumbleTextBoxText::handle_event()
2702 popup->handle_event();
2706 int BC_TumbleTextBoxText::button_press_event()
2710 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
2712 if(get_buttonpress() == 4)
2714 popup->tumbler->handle_up_event();
2717 if(get_buttonpress() == 5)
2719 popup->tumbler->handle_down_event();
2729 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2730 int64_t default_value,
2742 this->default_value = default_value;
2743 this->text_w = text_w;
2744 this->parent_window = parent_window;
2750 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2763 this->default_value = default_value;
2764 this->text_w = text_w;
2765 this->parent_window = parent_window;
2771 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2772 float default_value_f,
2782 this->min_f = min_f;
2783 this->max_f = max_f;
2784 this->default_value_f = default_value_f;
2785 this->text_w = text_w;
2786 this->parent_window = parent_window;
2792 BC_TumbleTextBox::~BC_TumbleTextBox()
2794 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2795 // is deleted anyway by the windowbase so textbox deletes this.
2796 if(tumbler) delete tumbler;
2798 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2807 void BC_TumbleTextBox::reset()
2814 void BC_TumbleTextBox::set_precision(int precision)
2816 this->precision = precision;
2819 void BC_TumbleTextBox::set_increment(float value)
2821 this->increment = value;
2822 if(tumbler) tumbler->set_increment(value);
2825 void BC_TumbleTextBox::set_log_floatincrement(int value)
2827 this->log_floatincrement = value;
2828 if(tumbler) tumbler->set_log_floatincrement(value);
2831 int BC_TumbleTextBox::create_objects()
2833 int x = this->x, y = this->y;
2837 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2843 textbox->set_precision(precision);
2846 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2853 x += textbox->get_w();
2856 parent_window->add_subwindow(tumbler = new BC_FTumbler(textbox,
2862 parent_window->add_subwindow(tumbler = new BC_ITumbler(textbox,
2868 tumbler->set_increment(increment);
2872 const char* BC_TumbleTextBox::get_text()
2874 return textbox->get_text();
2877 const wchar_t* BC_TumbleTextBox::get_wtext()
2879 return textbox->get_wtext();
2882 BC_TextBox* BC_TumbleTextBox::get_textbox()
2887 int BC_TumbleTextBox::update(const char *value)
2889 textbox->update(value);
2893 int BC_TumbleTextBox::update(int64_t value)
2895 textbox->update(value);
2899 int BC_TumbleTextBox::update(float value)
2901 textbox->update(value);
2906 int BC_TumbleTextBox::get_x()
2911 int BC_TumbleTextBox::get_y()
2916 int BC_TumbleTextBox::get_w()
2918 return textbox->get_w() + tumbler->get_w();
2921 int BC_TumbleTextBox::get_h()
2923 return textbox->get_h();
2926 void BC_TumbleTextBox::disable()
2928 if( !get_enabled() ) return;
2929 tumbler->hide_window(0);
2930 return textbox->disable();
2933 void BC_TumbleTextBox::enable()
2935 if( get_enabled() ) return;
2936 tumbler->show_window(0);
2937 return textbox->enable();
2940 int BC_TumbleTextBox::get_enabled()
2942 return textbox->get_enabled();
2945 int BC_TumbleTextBox::handle_event()
2950 void BC_TumbleTextBox::reposition_window(int x, int y)
2955 textbox->reposition_window(x,
2959 tumbler->reposition_window(x + textbox->get_w(),
2961 // if(flush) parent_window->flush();
2965 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2967 tumbler->set_boundaries(min, max);
2970 void BC_TumbleTextBox::set_boundaries(float min, float max)
2972 tumbler->set_boundaries(min, max);