3 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "bcclipboard.h"
22 #include "bclistboxitem.h"
23 #include "bcresources.h"
24 #include "bcsignals.h"
25 #include "bctextbox.h"
30 #include "filesystem.h"
42 #define VERTICAL_MARGIN 2
43 #define VERTICAL_MARGIN_NOBORDER 0
44 #define HORIZONTAL_MARGIN 4
45 #define HORIZONTAL_MARGIN_NOBORDER 2
47 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
48 int size, char *text, int has_border, int font)
49 : BC_SubWindow(x, y, w, 0, -1)
53 reset_parameters(rows, has_border, font, size);
56 else // text referenced directly
60 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
61 int size, wchar_t *wtext, int has_border, int font)
62 : BC_SubWindow(x, y, w, 0, -1)
66 wsize = size > 0 ? size : wcslen(wtext);
67 if( size <= 0 ) size = 2*wsize;
68 reset_parameters(rows, has_border, font, size);
69 this->wtext = new wchar_t[wsize+1];
70 wcsncpy(this->wtext, wtext, wsize);
71 this->wtext[wsize] = 0;
74 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
75 const char *text, int has_border, int font, int is_utf8)
76 : BC_SubWindow(x, y, w, 0, -1)
78 this->is_utf8 = is_utf8;
80 reset_parameters(rows, has_border, font, BCTEXTLEN);
84 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
85 const wchar_t *wtext, int has_border, int font, int is_utf8)
86 : BC_SubWindow(x, y, w, 0, -1)
88 this->is_utf8 = is_utf8;
90 reset_parameters(rows, has_border, font, BCTEXTLEN);
92 wtext = new wchar_t[wsize+1];
93 wcsncpy(this->wtext, wtext, wsize);
94 this->wtext[wsize] = 0;
97 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
98 int64_t text, int has_border, int font)
99 : BC_SubWindow(x, y, w, 0, -1)
103 reset_parameters(rows, has_border, font, BCSTRLEN);
104 snprintf(this->text, this->tsize, "%jd", text);
105 dirty = 1; wtext_update();
108 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
109 float text, int has_border, int font, int precision)
110 : BC_SubWindow(x, y, w, 0, -1)
114 reset_parameters(rows, has_border, font, BCSTRLEN);
115 this->precision = precision;
116 snprintf(this->text, this->tsize, "%0.*f", precision, text);
117 dirty = 1; wtext_update();
120 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
121 int text, int has_border, int font)
122 : BC_SubWindow(x, y, w, 0, -1)
126 reset_parameters(rows, has_border, font, BCSTRLEN);
127 snprintf(this->text, this->tsize, "%d", text);
128 dirty = 1; wtext_update();
131 BC_TextBox::~BC_TextBox()
133 if(skip_cursor) delete skip_cursor;
134 delete suggestions_popup;
135 suggestions->remove_all_objects();
143 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
145 suggestions = new ArrayList<BC_ListBoxItem*>;
146 suggestions_popup = 0;
147 suggestion_column = 0;
150 this->has_border = has_border;
153 this->tsize = size >= 0 ? size : -size;
154 this->text = size > 0 ? new char[size+1] : 0;
155 if( this->text ) this->text[0] = 0;
158 highlight_letter1 = highlight_letter2 = 0;
159 highlight_letter3 = highlight_letter4 = 0;
171 skip_cursor = new Timer;
182 selection_active = 0;
186 int BC_TextBox::tstrlen()
188 if( !tsize ) return strlen(text);
189 return strnlen(text, tsize);
192 int BC_TextBox::tstrcmp(const char *cp)
194 const char *tp = get_text();
195 if( !tsize ) return strcmp(tp, cp);
196 return strncmp(tp, cp, tsize);
199 char *BC_TextBox::tstrcpy(const char *cp)
204 return strcpy(text, cp);
205 strncpy(text, cp, tsize);
213 char *BC_TextBox::tstrcat(const char *cp)
216 if( !tsize ) return strcat(text, cp);
217 char *result = strncat(text, cp, tsize);
222 int BC_TextBox::wtext_update()
225 const char *src_enc = is_utf8 ? "UTF8" : BC_Resources::encoding;
226 const char *dst_enc = BC_Resources::wide_encoding;
227 int nsize = tsize > 0 ? tsize : strlen(text) + BCTEXTLEN;
228 if( nsize > wsize || !wtext ) {
229 wchar_t *ntext = new wchar_t[nsize+1];
230 memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
231 delete [] wtext; wtext = ntext; wsize = nsize;
233 wlen = BC_Resources::encode(src_enc, dst_enc, text, strlen(text),
234 (char*)wtext, wsize*sizeof(wchar_t)) / sizeof(wchar_t);
241 int BC_TextBox::text_update(const wchar_t *wcp, int wsz, char *tcp, int tsz)
243 const char *src_enc = BC_Resources::wide_encoding;
244 const char *dst_enc = BC_Resources::encoding;
245 if( wsz < 0 ) wsz = wcslen(wcp);
246 int len = BC_Resources::encode(src_enc, dst_enc,
247 (char*)wcp, wsz*sizeof(wchar_t), tcp, tsz);
252 int BC_TextBox::initialize()
256 skip_cursor = new Timer;
257 skip_cursor->update();
259 text_ascent = get_text_ascent(font) + 1;
260 text_descent = get_text_descent(font) + 1;
261 text_height = text_ascent + text_descent;
262 ibeam_letter = wtext_update();
265 left_margin = right_margin = HORIZONTAL_MARGIN;
266 top_margin = bottom_margin = VERTICAL_MARGIN;
270 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
271 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
274 text_x = left_margin;
278 // Create the subwindow
279 BC_SubWindow::initialize();
281 BC_Resources *resources = get_resources();
284 back_color = resources->text_background;
285 high_color = resources->text_background_hi;
289 high_color = resources->text_background_noborder_hi;
290 back_color = bg_color;
294 set_cursor(IBEAM_CURSOR, 0, 0);
297 add_subwindow(menu = new BC_TextMenu(this));
298 menu->create_objects();
303 int BC_TextBox::calculate_h(BC_WindowBase *gui,
308 return rows * (gui->get_text_ascent(font) + 1 +
309 gui->get_text_descent(font) + 1) +
310 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
313 void BC_TextBox::set_precision(int precision)
315 this->precision = precision;
318 // Compute suggestions for a path
319 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries, const char *filter)
321 // Let user delete suggestion
322 if(get_last_keypress() != BACKSPACE) {
323 // Compute suggestions
325 ArrayList<char*> suggestions;
326 const char *current_text = get_text();
327 int suggestion_column = 0;
328 char dirname[BCTEXTLEN]; dirname[0] = 0;
329 if( current_text[0] == '/' || current_text[0] == '~' )
330 strncpy(dirname, current_text, sizeof(dirname));
332 getcwd(dirname, sizeof(dirname));
333 // If directory, tabulate it
335 if( filter ) fs.set_filter(filter);
337 strncpy(dirname, current_text, sizeof(dirname));
338 if( (cp=strrchr(dirname, '/')) ||
339 (cp=strrchr(dirname, '~')) ) *++cp = 0;
340 fs.parse_tildas(dirname);
342 cp = (char *)current_text;
343 if( (prefix=strrchr(cp, '/')) ||
344 (prefix=strrchr(cp, '~')) ) ++prefix;
345 suggestion_column = !prefix ? 0 : prefix - cp;
346 int prefix_len = prefix ? strlen(prefix) : 0;
347 // only include items where the filename matches the basename prefix
348 for(int i = 0; i < fs.total_files(); i++) {
349 char *current_name = fs.get_entry(i)->name;
350 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
351 suggestions.append(current_name);
355 char *prefix = (char *)current_text;
356 int prefix_len = strlen(prefix);
357 for(int i = 0; i < entries->size(); i++) {
358 char *current_name = entries->get(i)->get_text();
359 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
360 suggestions.append(current_name);
363 set_suggestions(&suggestions, suggestion_column);
369 void BC_TextBox::no_suggestions()
371 if( suggestions_popup ) {
372 delete suggestions_popup;
373 suggestions_popup = 0;
378 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
381 this->suggestions->remove_all_objects();
382 this->suggestion_column = column;
385 for(int i = 0; i < suggestions->size(); i++) {
386 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
389 // Show the popup without taking focus
390 if( suggestions->size() > 1 ) {
391 if( !suggestions_popup ) {
392 suggestions_popup = new BC_TextBoxSuggestions(this, x, y);
393 get_parent()->add_subwindow(suggestions_popup);
394 suggestions_popup->set_is_suggestions(1);
397 suggestions_popup->update(this->suggestions, 0, 0, 1);
399 suggestions_popup->activate(0);
402 // Show the highlighted text
403 if( suggestions->size() == 1 ) {
404 highlight_letter1 = wtext_update();
405 int len = text_update(wtext,wlen, text,tsize);
406 char *current_suggestion = suggestions->get(0);
407 int col = len - suggestion_column;
408 if( col < 0 ) col = 0;
409 char *cur = current_suggestion + col;
411 highlight_letter2 = wtext_update();
412 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
420 if( !suggestions || !this->suggestions->size() )
424 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
426 highlight_letter1 = char1;
427 highlight_letter2 = char2;
428 ibeam_letter = ibeam;
432 // count utf8 chars in text which occur before cp
433 int BC_TextBox::wcpos(const char *text, const char *cp)
436 const unsigned char *bp = (const unsigned char *)text;
437 const unsigned char *ep = (const unsigned char *)cp;
438 while( bp < ep && *bp != 0 ) {
441 if( ch < 0x80 ) continue;
443 int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
444 for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
449 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
451 const char *cp = get_text();
452 wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
455 int BC_TextBox::update(const char *text)
457 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
458 // Don't update if contents are the same
459 if(!tstrcmp(text)) return 0;
461 int wtext_len = wtext_update();
462 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
463 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
464 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
469 int BC_TextBox::update(const wchar_t *wtext)
471 int wtext_len = wcslen(wtext);
472 if( wtext_len >= wsize ) wtext_len = wsize;
473 wcsncpy(this->wtext, wtext, wtext_len);
475 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
476 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
477 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
482 int BC_TextBox::update(int64_t value)
484 char string[BCTEXTLEN];
485 sprintf(string, "%jd", value);
490 int BC_TextBox::update(float value)
492 char string[BCTEXTLEN];
493 sprintf(string, "%0.*f", precision, value);
498 void BC_TextBox::disable()
507 void BC_TextBox::enable()
517 int BC_TextBox::get_enabled() { return enabled; }
518 int BC_TextBox::get_text_x() { return text_x; }
519 int BC_TextBox::get_text_y() { return text_y; }
520 void BC_TextBox::set_text_x(int v) { text_x = v; }
521 void BC_TextBox::set_text_y(int v) { text_y = v; }
523 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
525 return (pixels - 4) /
526 (window->get_text_ascent(font) + 1 +
527 window->get_text_descent(font) + 1);
530 int BC_TextBox::calculate_row_h(int rows,
531 BC_WindowBase *parent_window,
536 (parent_window->get_text_ascent(font) + 1 +
537 parent_window->get_text_descent(font) + 1) +
538 (has_border ? 4 : 0);
541 const char* BC_TextBox::get_text()
543 int wtext_len = wtext_update();
544 text_update(wtext,wtext_len, text,tsize);
548 const wchar_t* BC_TextBox::get_wtext()
554 void BC_TextBox::set_text(char *text, int isz)
556 if( size > 0 || isz < 0 ) return;
565 int BC_TextBox::get_text_rows()
567 int wtext_len = wtext_update();
569 for(int i = 0; i < wtext_len; i++) {
570 if(wtext[i] == '\n') result++;
576 int BC_TextBox::get_row_h(int rows)
578 return rows * text_height + top_margin + bottom_margin;
581 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
584 if(w < 0) w = get_w();
587 new_h = get_row_h(rows);
596 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
597 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
598 BC_WindowBase::reposition_window(x, y, w, new_h);
604 void BC_TextBox::draw_border()
606 BC_Resources *resources = get_resources();
608 set_color(background_color);
609 draw_box(0, 0, left_margin, get_h());
610 draw_box(get_w() - right_margin, 0, right_margin, get_h());
615 draw_3d_border(0, 0, w, h,
616 resources->text_border1,
617 resources->text_border2_hi,
618 resources->text_border3_hi,
619 resources->text_border4);
621 draw_3d_border(0, 0, w, h,
622 resources->text_border1,
623 resources->text_border2,
624 resources->text_border3,
625 resources->text_border4);
629 void BC_TextBox::draw_cursor()
631 // set_color(background_color);
639 draw_box(ibeam_x + text_x,
648 void BC_TextBox::draw(int flush)
651 int row_begin, row_end;
652 int highlight_x1, highlight_x2;
654 BC_Resources *resources = get_resources();
656 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
659 background_color = resources->text_background;
661 background_color = high_color;
663 background_color = back_color;
664 set_color(background_color);
665 draw_box(0, 0, w, h);
667 int wtext_len = wtext_update();
669 // Draw text with selection
672 for(i=0, j=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
675 wchar_t *wtext_row = &wtext[i];
676 for(j=0; j<BCTEXTLEN-1 && i<wtext_len && wtext[i]!='\n'; ++i, ++j);
677 if( (row_end=i) < wtext_len ) ++i;
679 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
680 // Draw highlighted region of row
681 if( highlight_letter2 > highlight_letter1 &&
682 highlight_letter2 > row_begin &&
683 highlight_letter1 <= row_end ) {
684 int color = active && enabled && get_has_focus() ?
685 resources->text_highlight :
687 resources->text_selected_highlight :
688 resources->text_inactive_highlight;
689 if( unicode_active >= 0 )
692 if(highlight_letter1 >= row_begin &&
693 highlight_letter1 <= row_end)
694 highlight_x1 = get_x_position(highlight_letter1, row_begin);
698 if(highlight_letter2 > row_begin &&
699 highlight_letter2 <= row_end)
700 highlight_x2 = get_x_position(highlight_letter2, row_begin);
702 highlight_x2 = get_w();
704 draw_box(highlight_x1 + text_x, k,
705 highlight_x2 - highlight_x1, text_height);
708 // Draw text over highlight
709 int len = row_end - row_begin;
711 set_color(enabled ? resources->text_default : DMGREY);
712 draw_single_text(1, font, text_x, k + text_ascent, wtext_row, len);
715 // Get ibeam location
716 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
718 ibeam_y = k - text_y;
719 ibeam_x = get_x_position(ibeam_letter, row_begin);
724 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
726 // ibeam_x = ibeam_y = !wtext_len ? 0 : -1;
727 ibeam_x = 0; ibeam_y = k - text_y;
730 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
740 int BC_TextBox::focus_in_event()
746 int BC_TextBox::focus_out_event()
752 int BC_TextBox::cursor_enter_event()
754 if( top_level->event_win == win && enabled &&
755 !(top_level->get_resources()->textbox_focus_policy & CLICK_ACTIVATE) )
758 top_level->deactivate();
771 int BC_TextBox::cursor_leave_event()
780 if( !suggestions_popup && !get_button_down() &&
781 !(top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
786 int BC_TextBox::button_press_event()
790 if(!enabled) return 0;
791 // if(get_buttonpress() != WHEEL_UP &&
792 // get_buttonpress() != WHEEL_DOWN &&
793 // get_buttonpress() != LEFT_BUTTON &&
794 // get_buttonpress() != MIDDLE_BUTTON) return 0;
796 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
798 int cursor_letter = 0;
799 int wtext_len = wtext_update();
800 int update_scroll = 0;
803 if(top_level->event_win == win)
808 top_level->deactivate();
813 if(get_buttonpress() == WHEEL_UP)
815 text_y += text_height;
816 text_y = MIN(text_y, top_margin);
820 if(get_buttonpress() == WHEEL_DOWN)
822 int min_y = -(get_text_rows() *
826 text_y -= text_height;
827 text_y = MAX(text_y, min_y);
828 text_y = MIN(text_y, top_margin);
832 if(get_buttonpress() == RIGHT_BUTTON)
834 menu->activate_menu();
839 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
841 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
844 if(get_triple_click())
846 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
848 select_line(highlight_letter1, highlight_letter2, cursor_letter);
849 highlight_letter3 = highlight_letter1;
850 highlight_letter4 = highlight_letter2;
851 ibeam_letter = highlight_letter2;
852 copy_selection(PRIMARY_SELECTION);
855 if(get_double_click())
858 select_word(highlight_letter1, highlight_letter2, cursor_letter);
859 highlight_letter3 = highlight_letter1;
860 highlight_letter4 = highlight_letter2;
861 ibeam_letter = highlight_letter2;
862 copy_selection(PRIMARY_SELECTION);
865 if(get_buttonpress() == MIDDLE_BUTTON)
867 highlight_letter3 = highlight_letter4 =
868 ibeam_letter = highlight_letter1 =
869 highlight_letter2 = cursor_letter;
870 paste_selection(PRIMARY_SELECTION);
875 highlight_letter3 = highlight_letter4 =
876 ibeam_letter = highlight_letter1 =
877 highlight_letter2 = cursor_letter;
881 // Handle scrolling by highlighting text
882 if(text_selected || word_selected || line_selected)
884 set_repeat(top_level->get_resources()->scroll_repeat);
887 if(ibeam_letter < 0) ibeam_letter = 0;
888 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
892 if(update_scroll && yscroll)
894 yscroll->update_length(get_text_rows(),
896 yscroll->get_handlelength(),
904 if( suggestions_popup && (!yscroll || !yscroll->is_event_win())) {
905 if( suggestions_popup->button_press_event() )
906 return suggestions_popup->handle_event();
908 else if( (top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
915 int BC_TextBox::button_release_event()
920 if(text_selected || word_selected || line_selected)
926 // Stop scrolling by highlighting text
927 unset_repeat(top_level->get_resources()->scroll_repeat);
933 int BC_TextBox::cursor_motion_event()
935 int cursor_letter, letter1, letter2;
938 if(text_selected || word_selected || line_selected)
940 cursor_letter = get_cursor_letter(top_level->cursor_x,
941 top_level->cursor_y);
943 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
947 select_line(letter1, letter2, cursor_letter);
952 select_word(letter1, letter2, cursor_letter);
957 letter1 = letter2 = cursor_letter;
960 if(letter1 <= highlight_letter3)
962 highlight_letter1 = letter1;
963 highlight_letter2 = highlight_letter4;
964 ibeam_letter = letter1;
967 if(letter2 >= highlight_letter4)
969 highlight_letter2 = letter2;
970 highlight_letter1 = highlight_letter3;
971 ibeam_letter = letter2;
974 copy_selection(PRIMARY_SELECTION);
987 int BC_TextBox::activate()
991 top_level->set_active_subwindow(this);
992 top_level->set_repeat(top_level->get_resources()->blink_rate);
998 int BC_TextBox::deactivate()
1002 text_selected = word_selected = line_selected = 0;
1003 top_level->set_active_subwindow(0);
1004 top_level->unset_repeat(top_level->get_resources()->blink_rate);
1010 int BC_TextBox::repeat_event(int64_t duration)
1013 int cursor_y = get_cursor_y();
1014 //int cursor_x = get_cursor_x();
1016 if(duration == top_level->get_resources()->tooltip_delay &&
1017 tooltip_text && tooltip_text[0] != 0 && highlighted)
1023 if(duration == top_level->get_resources()->blink_rate &&
1027 // don't flash if keypress
1028 if(skip_cursor->get_difference() < 500)
1030 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1031 // skip_cursor->get_difference(),
1037 if(!(text_selected || word_selected || line_selected))
1046 if(duration == top_level->get_resources()->scroll_repeat &&
1047 (text_selected || word_selected || line_selected))
1050 if(get_cursor_y() < top_margin)
1052 difference = get_cursor_y() - top_margin ;
1055 if(get_cursor_y() > get_h() - bottom_margin)
1057 difference = get_cursor_y() -
1058 (get_h() - bottom_margin);
1060 if(difference != 0) {
1061 int min_y = -(get_text_rows() * text_height -
1062 get_h() + bottom_margin);
1064 text_y -= difference;
1065 // printf("BC_TextBox::repeat_event %d %d %d\n",
1069 text_y = MAX(min_y, text_y);
1070 text_y = MIN(text_y, top_margin);
1077 if(get_cursor_x() < left_margin)
1079 int difference = left_margin - get_cursor_x();
1081 text_x += difference;
1082 text_x = MIN(text_x, left_margin);
1086 else if(get_cursor_x() > get_w() - right_margin)
1088 int difference = get_cursor_x() - (get_w() - right_margin);
1089 int new_text_x = text_x - difference;
1091 // Get width of current row
1094 int wtext_len = wtext_update();
1097 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1100 while(wtext[i] != '\n' && i < wtext_len) {
1104 if(wtext[i] == '\n') i++;
1106 if(cursor_y >= k && cursor_y < k + text_height) {
1107 row_width = get_text_width(font,
1109 row_end - row_begin);
1115 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1116 new_text_x = MAX(new_text_x, min_x);
1117 new_text_x = MIN(new_text_x, left_margin);
1119 if(new_text_x < text_x) text_x = new_text_x;
1128 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1130 int key = top_level->get_keypress(), len;
1131 if( (key == RETURN) || ( key >= 32 && key <= 255 ) ) {
1132 wchar_t *wkeys = top_level->get_wkeystring(&len);
1133 if( key == RETURN ) { wkeys[0] = '\n'; wkeys[1] = 0; len = 1; }
1134 insert_text(wkeys, len);
1142 int BC_TextBox::keypress_event()
1144 // Result == 2 contents changed
1145 // Result == 1 trapped keypress
1146 // Result == 0 nothing
1148 int dispatch_event = 0;
1150 if(!active || !enabled) return 0;
1152 int wtext_len = wtext_update();
1153 last_keypress = get_keypress();
1155 if( unicode_active >= 0 ) {
1158 switch( last_keypress ) {
1159 //unicode active acitons
1161 for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1162 int ch = nib(wtext[i]);
1163 if( ch < 0 ) return 1;
1164 wch = (wch<<4) + ch;
1168 unicode_active = -1;
1174 unicode_active = -1;
1179 if(ibeam_letter > 0) {
1180 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1181 highlight_letter2 = --ibeam_letter;
1182 if( highlight_letter1 >= highlight_letter2 )
1183 unicode_active = -1;
1188 case '0': case '1': case '2': case '3': case '4':
1189 case '5': case '6': case '7': case '8': case '9':
1190 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1191 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1192 int n = nib(last_keypress);
1193 wch = n < 10 ? '0'+n : 'A'+n-10;
1207 insert_text(&wch, wlen);
1209 if( unicode_active >= 0 )
1210 highlight_letter2 = ibeam_letter;
1212 highlight_letter1 = highlight_letter2 = 0;
1218 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1219 switch(last_keypress) {
1221 // Deactivate the suggestions
1222 if( suggestions_popup ) {
1227 top_level->deactivate();
1234 highlight_letter1 = highlight_letter2 = 0;
1235 ibeam_letter = wtext_update();
1236 top_level->deactivate();
1241 default_keypress(dispatch_event, result);
1246 // Handle like a default keypress
1248 top_level->cycle_textboxes(1);
1253 top_level->cycle_textboxes(-1);
1258 if(ibeam_letter > 0) {
1259 int old_ibeam_letter = ibeam_letter;
1267 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1272 if(top_level->shift_down()) {
1273 // Initialize highlighting
1274 if(highlight_letter1 == highlight_letter2) {
1275 highlight_letter1 = ibeam_letter;
1276 highlight_letter2 = old_ibeam_letter;
1278 else if(highlight_letter1 == old_ibeam_letter) {
1279 // Extend left highlight
1280 highlight_letter1 = ibeam_letter;
1282 else if(highlight_letter2 == old_ibeam_letter) {
1283 // Shrink right highlight
1284 highlight_letter2 = ibeam_letter;
1288 highlight_letter1 = highlight_letter2 = ibeam_letter;
1293 if(keypress_draw) draw(1);
1299 if(ibeam_letter < wtext_len) {
1300 int old_ibeam_letter = ibeam_letter;
1307 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1313 if(top_level->shift_down()) {
1314 // Initialize highlighting
1315 if(highlight_letter1 == highlight_letter2) {
1316 highlight_letter1 = old_ibeam_letter;
1317 highlight_letter2 = ibeam_letter;
1319 else if(highlight_letter1 == old_ibeam_letter) {
1320 // Shrink left highlight
1321 highlight_letter1 = ibeam_letter;
1323 else if(highlight_letter2 == old_ibeam_letter) {
1324 // Expand right highlight
1325 highlight_letter2 = ibeam_letter;
1329 highlight_letter1 = highlight_letter2 = ibeam_letter;
1333 if(keypress_draw) draw(1);
1339 if( suggestions && suggestions_popup ) {
1340 // Pass to suggestions popup
1341 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1342 suggestions_popup->activate(1);
1343 suggestions_popup->keypress_event();
1346 else if(ibeam_letter > 0) {
1347 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1348 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1349 ibeam_y + text_y - text_height);
1350 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1353 if(top_level->shift_down()) {
1354 // Initialize highlighting
1355 if(highlight_letter1 == highlight_letter2) {
1356 highlight_letter1 = new_letter;
1357 highlight_letter2 = ibeam_letter;
1359 else if(highlight_letter1 == ibeam_letter) {
1360 // Expand left highlight
1361 highlight_letter1 = new_letter;
1363 else if(highlight_letter2 == ibeam_letter) {
1364 // Shrink right highlight
1365 highlight_letter2 = new_letter;
1369 highlight_letter1 = highlight_letter2 = new_letter;
1371 if(highlight_letter1 > highlight_letter2) {
1372 int temp = highlight_letter1;
1373 highlight_letter1 = highlight_letter2;
1374 highlight_letter2 = temp;
1376 ibeam_letter = new_letter;
1379 if(keypress_draw) draw(1);
1385 if(ibeam_letter > 0) {
1386 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1387 ibeam_y + text_y - get_h());
1390 if(top_level->shift_down()) {
1391 // Initialize highlighting
1392 if(highlight_letter1 == highlight_letter2) {
1393 highlight_letter1 = new_letter;
1394 highlight_letter2 = ibeam_letter;
1396 else if(highlight_letter1 == ibeam_letter) {
1397 // Expand left highlight
1398 highlight_letter1 = new_letter;
1400 else if(highlight_letter2 == ibeam_letter) {
1401 // Shrink right highlight
1402 highlight_letter2 = new_letter;
1406 highlight_letter1 = highlight_letter2 = new_letter;
1408 if(highlight_letter1 > highlight_letter2) {
1409 int temp = highlight_letter1;
1410 highlight_letter1 = highlight_letter2;
1411 highlight_letter2 = temp;
1413 ibeam_letter = new_letter;
1416 if(keypress_draw) draw(1);
1422 // printf("BC_TextBox::keypress_event %d %p %p\n",
1425 // suggestions_popup);
1426 if( suggestions && suggestions_popup ) {
1427 // Pass to suggestions popup
1428 suggestions_popup->activate(1);
1429 suggestions_popup->keypress_event();
1433 // if(ibeam_letter > 0)
1436 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1437 ibeam_y + text_y + text_height);
1438 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1440 if(top_level->shift_down()) {
1441 // Initialize highlighting
1442 if(highlight_letter1 == highlight_letter2) {
1443 highlight_letter1 = new_letter;
1444 highlight_letter2 = ibeam_letter;
1446 else if(highlight_letter1 == ibeam_letter) {
1447 // Shrink left highlight
1448 highlight_letter1 = new_letter;
1450 else if(highlight_letter2 == ibeam_letter) {
1451 // Expand right highlight
1452 highlight_letter2 = new_letter;
1456 highlight_letter1 = highlight_letter2 = new_letter;
1458 if(highlight_letter1 > highlight_letter2) {
1459 int temp = highlight_letter1;
1460 highlight_letter1 = highlight_letter2;
1461 highlight_letter2 = temp;
1463 ibeam_letter = new_letter;
1466 if(keypress_draw) draw(1);
1468 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1475 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1476 ibeam_y + text_y + get_h());
1477 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1479 if(top_level->shift_down()) {
1480 // Initialize highlighting
1481 if(highlight_letter1 == highlight_letter2) {
1482 highlight_letter1 = new_letter;
1483 highlight_letter2 = ibeam_letter;
1485 else if(highlight_letter1 == ibeam_letter) {
1486 // Shrink left highlight
1487 highlight_letter1 = new_letter;
1489 else if(highlight_letter2 == ibeam_letter) {
1490 // Expand right highlight
1491 highlight_letter2 = new_letter;
1495 highlight_letter1 = highlight_letter2 = new_letter;
1497 if(highlight_letter1 > highlight_letter2) {
1498 int temp = highlight_letter1;
1499 highlight_letter1 = highlight_letter2;
1500 highlight_letter2 = temp;
1502 ibeam_letter = new_letter;
1505 if(keypress_draw) draw(1);
1507 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1514 int old_ibeam_letter = ibeam_letter;
1516 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1519 if(top_level->shift_down()) {
1521 if(highlight_letter1 == highlight_letter2) {
1522 highlight_letter2 = ibeam_letter;
1523 highlight_letter1 = old_ibeam_letter;
1525 else if(highlight_letter1 == old_ibeam_letter) {
1527 highlight_letter1 = highlight_letter2;
1528 highlight_letter2 = ibeam_letter;
1530 else if(highlight_letter2 == old_ibeam_letter) {
1532 highlight_letter2 = ibeam_letter;
1536 highlight_letter1 = highlight_letter2 = ibeam_letter;
1539 if(keypress_draw) draw(1);
1546 int old_ibeam_letter = ibeam_letter;
1548 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1551 if(top_level->shift_down())
1554 if(highlight_letter1 == highlight_letter2)
1556 highlight_letter2 = old_ibeam_letter;
1557 highlight_letter1 = ibeam_letter;
1561 if(highlight_letter1 == old_ibeam_letter)
1563 highlight_letter1 = ibeam_letter;
1567 if(highlight_letter2 == old_ibeam_letter)
1569 highlight_letter2 = highlight_letter1;
1570 highlight_letter1 = ibeam_letter;
1574 highlight_letter1 = highlight_letter2 = ibeam_letter;
1577 if(keypress_draw) draw(1);
1584 if(highlight_letter1 == highlight_letter2) {
1585 if(ibeam_letter > 0) {
1586 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1591 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1592 highlight_letter2 = ibeam_letter = highlight_letter1;
1596 if(keypress_draw) draw(1);
1602 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1603 if(highlight_letter1 == highlight_letter2) {
1604 if(ibeam_letter < wtext_len) {
1605 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1609 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1610 highlight_letter2 = ibeam_letter = highlight_letter1;
1614 if(keypress_draw) draw(1);
1621 switch( get_keypress() ) {
1622 case 'c': case 'C': {
1625 case 'v': case 'V': {
1629 case 'x': case 'X': {
1633 case 'u': case 'U': {
1634 if( shift_down() ) {
1635 unicode_active = ibeam_letter;
1637 insert_text(&wkey, 1);
1639 highlight_letter1 = unicode_active;
1640 highlight_letter2 = ibeam_letter;
1649 default_keypress(dispatch_event, result);
1654 if(result) skip_cursor->update();
1655 if(dispatch_event && handle_event())
1662 int BC_TextBox::cut(int do_update)
1664 if( highlight_letter1 != highlight_letter2 ) {
1665 int wtext_len = wtext_update();
1666 copy_selection(SECONDARY_SELECTION);
1667 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1668 highlight_letter2 = ibeam_letter = highlight_letter1;
1676 skip_cursor->update();
1682 int BC_TextBox::copy(int do_update)
1685 if( highlight_letter1 != highlight_letter2 ) {
1686 copy_selection(SECONDARY_SELECTION);
1689 skip_cursor->update();
1695 int BC_TextBox::paste(int do_update)
1697 paste_selection(SECONDARY_SELECTION);
1702 skip_cursor->update();
1709 int BC_TextBox::uses_text()
1715 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1718 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1719 wtext[i] = wtext[j];
1727 void BC_TextBox::insert_text(const wchar_t *wcp, int len)
1729 if( len < 0 ) len = wcslen(wcp);
1730 int wtext_len = wtext_update();
1731 if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1732 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1733 highlight_letter2 = ibeam_letter = highlight_letter1;
1734 wtext_len = wtext_update();
1738 for(i=wtext_len-1, j=wtext_len+len-1; i>=ibeam_letter; i--, j--) {
1739 if( j >= wsize ) continue;
1740 wtext[j] = wtext[i];
1743 for(i = ibeam_letter, j = 0; j < len; j++, i++) {
1744 if( i >= wsize ) break;
1747 if( (wlen+=len) > wsize ) wlen = wsize;
1748 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1749 wtext[wlen] = 0; // wtext allocated wsize+1
1753 int BC_TextBox::is_separator(const char *txt, int i)
1755 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1756 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1759 // used for time entry
1760 void BC_TextBox::do_separators(int ibeam_left)
1764 // Remove separators from text
1765 int wtext_len = wtext_update();
1766 for(int i = 0; i < wtext_len; ) {
1767 if( !iswalnum(wtext[i]) ) {
1768 for(int j = i; j < wtext_len - 1; j++)
1769 wtext[j] = wtext[j + 1];
1770 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1776 wtext[wtext_len] = 0;
1782 // Insert separators into text
1783 int separator_len = strlen(separators);
1784 for(int i = 0; i < separator_len; i++) {
1786 // Insert a separator
1787 if( is_separator(separators,i) ) {
1788 for(int j = wtext_len; j >= i; j--) {
1789 wtext[j + 1] = wtext[j];
1791 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1793 wtext[i] = separators[i];
1797 wtext[i] = separators[i];
1802 wtext[separator_len] = 0;
1803 wlen = separator_len;
1807 int BC_TextBox::get_x_position(int i, int start)
1809 return get_text_width(font, &wtext[start], i - start);
1812 void BC_TextBox::get_ibeam_position(int &x, int &y)
1814 int i, row_begin, row_end;
1815 int wtext_len = wtext_update();
1818 for( i=0; i<wtext_len; ) {
1820 for(; i<wtext_len && wtext[i]!='\n'; i++);
1823 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1824 x = get_x_position(ibeam_letter, row_begin);
1825 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1829 if( i < wtext_len && wtext[i] == '\n' ) {
1834 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1840 void BC_TextBox::set_text_row(int row)
1842 text_x = left_margin;
1843 text_y = -(row * text_height) + top_margin;
1847 int BC_TextBox::get_text_row()
1849 return -(text_y - top_margin) / text_height;
1852 void BC_TextBox::find_ibeam(int dispatch_event)
1855 int old_x = text_x, old_y = text_y;
1857 get_ibeam_position(x, y);
1859 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1861 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1862 if(text_x > left_margin) text_x = left_margin;
1865 if(left_margin + text_x + x < left_margin)
1867 text_x = -(x - (get_w() / 4)) + left_margin;
1868 if(text_x > left_margin) text_x = left_margin;
1871 int text_row = y / text_height;
1872 if( text_row < rows ) text_y = top_margin;
1874 int pix_rows = get_h() - bottom_margin - (y + text_y);
1875 if( pix_rows < text_height )
1876 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1878 pix_rows = y + text_y - top_margin;
1879 if( pix_rows < 0 ) {
1880 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1881 if( text_y > top_margin ) text_y = top_margin;
1884 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1888 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1890 int i, j, k, row_begin, row_end, result = 0, done = 0;
1891 int column1, column2;
1892 int got_visible_row = 0;
1894 // Select complete row if cursor above the window
1895 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1896 if(cursor_y < text_y - text_height)
1902 int wtext_len = wtext_update();
1904 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1905 // Simulate drawing of 1 row
1907 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1911 int first_visible_row = 0;
1912 int last_visible_row = 0;
1913 if( k+text_height > top_margin && !got_visible_row) {
1914 first_visible_row = 1;
1915 got_visible_row = 1;
1918 if( (k+text_height >= get_h() - bottom_margin ||
1919 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1920 k + text_height > 0)) )
1921 last_visible_row = 1;
1923 // Cursor is inside vertical range of row
1924 if((cursor_y >= top_margin &&
1925 cursor_y < get_h() - bottom_margin &&
1926 cursor_y >= k && cursor_y < k + text_height) ||
1927 // Cursor is above 1st row
1928 (cursor_y < k + text_height && first_visible_row) ||
1929 // Cursor is below last row
1930 (cursor_y >= k && last_visible_row))
1932 column1 = column2 = 0;
1933 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1934 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1935 if((column2 + column1) / 2 >= cursor_x) {
1938 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1939 // __LINE__, result, first_visible_row, last_visible_row);
1950 if(wtext[i] == '\n') i++;
1952 // Select complete row if last visible & cursor is below window
1953 if(last_visible_row && cursor_y > k + text_height * 2)
1956 if(i >= wtext_len && !done) {
1962 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
1963 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
1964 if(result < 0) result = 0;
1965 if(result > wtext_len) {
1966 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
1974 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
1976 int i, j, k, row_begin, row_end, result = 0, done = 0;
1977 int column1, column2;
1978 int wtext_len = wtext_update();
1980 if(cursor_y < text_y) {
1985 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
1987 for(; wtext[i] != '\n' && i < wtext_len; i++);
1990 if(cursor_y >= k && cursor_y < k + text_height) {
1991 column1 = column2 = 0;
1992 for(j = 0; j <= row_end - row_begin && !done; j++) {
1993 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
1994 if((column2 + column1) / 2 >= cursor_x) {
1995 result = row_begin + j - 1;
2006 if(wtext[i] == '\n') i++;
2008 if(i >= wtext_len && !done) {
2012 if(result < 0) result = 0;
2013 if(result > wtext_len) result = wtext_len;
2018 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2020 int wtext_len = wtext_update();
2021 letter1 = letter2 = ibeam_letter;
2022 if( letter1 < 0 ) letter1 = 0;
2023 if( letter2 < 0 ) letter2 = 0;
2024 if( letter1 > wtext_len ) letter1 = wtext_len;
2025 if( letter2 > wtext_len ) letter2 = wtext_len;
2026 if( !wtext_len ) return;
2027 for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
2028 for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
2029 if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
2033 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2035 int wtext_len = wtext_update();
2036 letter1 = letter2 = ibeam_letter;
2037 if( letter1 < 0 ) letter1 = 0;
2038 if( letter2 < 0 ) letter2 = 0;
2039 if( letter1 > wtext_len ) letter1 = wtext_len;
2040 if( letter2 > wtext_len ) letter2 = wtext_len;
2041 if( !wtext_len ) return;
2042 for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2043 for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2046 void BC_TextBox::copy_selection(int clipboard_num)
2048 int wtext_len = wtext_update();
2049 if(!wtext_len) return;
2051 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2052 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2053 highlight_letter2 - highlight_letter1 <= 0) return;
2054 int clip_len = highlight_letter2 - highlight_letter1;
2055 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2056 char ctext[4*clip_len+4];
2057 clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2058 to_clipboard(ctext, clip_len, clipboard_num);
2059 selection_active = 1;
2062 int BC_TextBox::selection_clear_event()
2064 if( !is_event_win() ) return 0;
2065 selection_active = 0;
2070 void BC_TextBox::paste_selection(int clipboard_num)
2072 int len = clipboard_len(clipboard_num);
2075 char cstring[len]; wchar_t wstring[len];
2076 from_clipboard(cstring, len, clipboard_num); --len;
2077 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2078 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2079 cstring,len, (char *)wstring,(len+1)*sizeof(wchar_t)) / sizeof(wchar_t);
2080 insert_text(wstring, len);
2085 void BC_TextBox::set_keypress_draw(int value)
2087 keypress_draw = value;
2090 int BC_TextBox::get_last_keypress()
2092 return last_keypress;
2095 int BC_TextBox::get_ibeam_letter()
2097 return ibeam_letter;
2100 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2102 this->ibeam_letter = number;
2109 void BC_TextBox::set_separators(const char *separators)
2111 this->separators = (char*)separators;
2114 int BC_TextBox::get_rows()
2125 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y)
2126 : BC_ListBox(x, y, text_box->get_w(), 200, LISTBOX_TEXT,
2127 text_box->suggestions, 0, 0, 1, 0, 1)
2129 this->text_box = text_box;
2131 set_justify(LISTBOX_LEFT);
2134 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2138 int BC_TextBoxSuggestions::handle_event()
2140 char *current_suggestion = 0;
2141 BC_ListBoxItem *item = get_selection(0, 0);
2142 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2143 if(item && (current_suggestion=item->get_text()) != 0)
2145 int col = text_box->suggestion_column;
2146 int len = BCTEXTLEN-1 - col;
2147 char *cp = &text_box->text[col];
2148 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2149 strncpy(cp, current_suggestion, len);
2150 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2151 if( (col=strlen(current_suggestion)) >= len )
2153 text_box->dirty = 1;
2157 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2158 text_box->highlight_letter1 =
2159 text_box->highlight_letter2 =
2160 text_box->ibeam_letter = text_box->tstrlen();
2161 text_box->wtext_update();
2163 text_box->handle_event();
2164 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2169 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2170 int x, int y, int w, int rows,
2171 const char *default_text, int default_size)
2173 this->parent_window = parent_window;
2178 xscroll = 0; yscroll = 0;
2179 this->default_text = default_text;
2180 this->default_wtext = 0;
2181 this->default_size = default_size;
2184 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2185 int x, int y, int w, int rows,
2186 const wchar_t *default_wtext, int default_size)
2188 this->parent_window = parent_window;
2193 xscroll = 0; yscroll = 0;
2194 this->default_text = 0;
2195 this->default_wtext = default_wtext;
2196 this->default_size = default_size;
2199 BC_ScrollTextBox::~BC_ScrollTextBox()
2209 void BC_ScrollTextBox::create_objects()
2211 // Must be created first
2212 parent_window->add_subwindow(text = default_wtext ?
2213 new BC_ScrollTextBoxText(this, default_wtext) :
2214 new BC_ScrollTextBoxText(this, default_text));
2218 void BC_ScrollTextBox::set_text(char *text, int isz)
2220 this->text->set_text(text, isz);
2221 update_scrollbars();
2224 int BC_ScrollTextBox::set_text_row(int n)
2226 text->set_text_row(n);
2227 update_scrollbars();
2231 void BC_ScrollTextBox::update(const char *text)
2233 this->text->update(text);
2234 update_scrollbars();
2237 void BC_ScrollTextBox::update(const wchar_t *wtext)
2239 this->text->update(wtext);
2240 update_scrollbars();
2243 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2249 update_scrollbars();
2252 int BC_ScrollTextBox::button_press_event()
2254 return text->BC_TextBox::button_press_event();
2256 int BC_ScrollTextBox::button_release_event()
2258 return text->BC_TextBox::button_release_event();
2261 int BC_ScrollTextBox::get_h() { return text->get_h(); }
2262 const char *BC_ScrollTextBox::get_text() { return text->get_text(); }
2263 const wchar_t *BC_ScrollTextBox::get_wtext() { return text->get_wtext(); }
2265 int BC_ScrollTextBox::get_buttonpress()
2267 return text->BC_TextBox::get_buttonpress();
2269 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2271 text->wset_selection(char1, char2, ibeam);
2273 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2275 text->set_selection(char1, char2, ibeam);
2277 int BC_ScrollTextBox::get_ibeam_letter()
2279 return text->get_ibeam_letter();
2281 int BC_ScrollTextBox::get_x_pos()
2283 return text->left_margin - text->get_text_x();
2285 void BC_ScrollTextBox::set_x_pos(int x)
2287 text->set_text_x(text->left_margin - x);
2291 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2292 : BC_TextBox(gui->x, gui->y,
2293 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2294 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2299 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchar_t *wtext)
2300 : BC_TextBox(gui->x, gui->y,
2301 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2302 gui->rows, gui->default_size, (wchar_t*)wtext, 1, MEDIUMFONT)
2307 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2316 void BC_ScrollTextBox::update_scrollbars()
2318 int view_w = w, view_rows = rows;
2319 int view_h = BC_TextBox::calculate_row_h(view_rows, parent_window);
2320 int text_rows = text->get_text_rows();
2321 int text_width = parent_window->get_text_width(text->font, text->get_wtext());
2322 BC_Resources *resources = parent_window->get_resources();
2323 int need_xscroll = 0, need_yscroll = 0;
2325 // Create scrollbars as needed
2329 if( !need_xscroll && (text->get_text_x() != text->left_margin ||
2330 text_width >= view_w - text->left_margin - text->right_margin) ) {
2331 resize = need_xscroll = 1;
2332 view_h -= resources->hscroll_data[SCROLL_HANDLE_UP]->get_h();
2333 view_rows = BC_TextBox::pixels_to_rows(parent_window, text->font, view_h);
2335 if( !need_yscroll && (text->get_text_y() != text->top_margin ||
2336 text_rows > view_rows) ) {
2337 resize = need_yscroll = 1;
2338 view_w -= resources->vscroll_data[SCROLL_HANDLE_UP]->get_w();
2342 if( !need_xscroll && xscroll ) {
2344 delete xscroll; xscroll = 0;
2346 if( !need_yscroll && yscroll ) {
2348 delete yscroll; yscroll = 0;
2351 if( view_rows != text->get_rows() || view_w != text->get_w() ) {
2352 text->reposition_window(x, y, view_w, view_rows);
2354 if( need_xscroll && !xscroll ) {
2355 xscroll = new BC_ScrollTextBoxXScroll(this);
2356 parent_window->add_subwindow(xscroll);
2357 text->xscroll = xscroll;
2358 xscroll->bound_to = text;
2359 xscroll->show_window();
2361 if( need_yscroll && !yscroll ) {
2362 yscroll = new BC_ScrollTextBoxYScroll(this);
2363 parent_window->add_subwindow(yscroll);
2364 text->yscroll = yscroll;
2365 yscroll->bound_to = text;
2366 yscroll->show_window();
2369 xscroll->reposition_window(x, y + text->get_h(), view_w);
2370 int xpos = get_x_pos();
2371 if( xpos != xscroll->get_value() )
2372 xscroll->update_value(xpos);
2373 if( text_width != xscroll->get_length() ||
2374 view_w != xscroll->get_handlelength() )
2375 xscroll->update_length(text_width, xpos, view_w, 0);
2378 yscroll->reposition_window(x + w - yscroll->get_span(), y, text->get_h());
2379 int text_row = text->get_text_row();
2380 if( text_row != yscroll->get_value() )
2381 yscroll->update_value(text_row);
2382 if( text_rows != yscroll->get_length() ||
2383 view_rows != yscroll->get_handlelength() )
2384 yscroll->update_length(text_rows, text_row, view_rows, 0);
2388 int BC_ScrollTextBoxText::handle_event()
2390 gui->update_scrollbars();
2391 return gui->handle_event();
2394 int BC_ScrollTextBoxText::motion_event()
2396 gui->update_scrollbars();
2400 BC_ScrollTextBoxXScroll::BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui)
2401 : BC_ScrollBar(gui->x, gui->y + gui->text->get_h(), SCROLL_HORIZ + SCROLL_STRETCH,
2402 gui->text->get_w(), gui->text->get_text_width(MEDIUMFONT, gui->get_wtext()),
2408 BC_ScrollTextBoxXScroll::~BC_ScrollTextBoxXScroll()
2412 int BC_ScrollTextBoxXScroll::handle_event()
2414 gui->set_x_pos(get_position());
2418 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2419 : BC_ScrollBar(gui->x + gui->text->get_w(), gui->y, SCROLL_VERT,
2420 gui->text->get_h(), gui->text->get_text_rows(), 0, gui->rows)
2425 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2429 int BC_ScrollTextBoxYScroll::handle_event()
2431 gui->text->set_text_row(get_position());
2437 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2438 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2440 this->popup = popup;
2443 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchar_t *wtext)
2444 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2446 this->popup = popup;
2449 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2459 int BC_PopupTextBoxText::handle_event()
2461 popup->handle_event();
2465 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2467 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2468 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2470 this->popup = popup;
2472 int BC_PopupTextBoxList::handle_event()
2474 BC_ListBoxItem *item = get_selection(0, 0);
2477 popup->textbox->update(item->get_text());
2478 popup->textbox->set_text_row(0);
2479 popup->handle_event();
2487 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2488 ArrayList<BC_ListBoxItem*> *list_items,
2489 const char *default_text, int x, int y,
2490 int text_w, int list_h, int list_format)
2494 this->list_h = list_h;
2495 this->list_format = list_format;
2496 this->default_text = (char*)default_text;
2497 this->default_wtext = 0;
2498 this->text_w = text_w;
2499 this->parent_window = parent_window;
2500 this->list_items = list_items;
2503 BC_PopupTextBox::~BC_PopupTextBox()
2513 int BC_PopupTextBox::create_objects()
2515 int x = this->x, y = this->y;
2516 parent_window->add_subwindow(textbox = default_wtext ?
2517 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2518 new BC_PopupTextBoxText(this, x, y, default_text));
2519 x += textbox->get_w();
2520 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2524 void BC_PopupTextBox::update(const char *text)
2526 textbox->update(text);
2527 textbox->set_text_row(0);
2530 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2532 listbox->update(data, 0, 0, 1);
2536 const char* BC_PopupTextBox::get_text()
2538 return textbox->get_text();
2541 const wchar_t* BC_PopupTextBox::get_wtext()
2543 return textbox->get_wtext();
2546 int BC_PopupTextBox::get_number()
2548 return listbox->get_selection_number(0, 0);
2551 int BC_PopupTextBox::get_x()
2556 int BC_PopupTextBox::get_y()
2561 int BC_PopupTextBox::get_w()
2563 return textbox->get_w() + listbox->get_w();
2566 int BC_PopupTextBox::get_h()
2568 return textbox->get_h();
2571 int BC_PopupTextBox::get_show_query()
2573 return listbox->get_show_query();
2576 void BC_PopupTextBox::set_show_query(int v)
2578 listbox->set_show_query(v);
2581 int BC_PopupTextBox::handle_event()
2586 void BC_PopupTextBox::reposition_window(int x, int y)
2591 textbox->reposition_window(x1,
2594 textbox->get_rows());
2595 x1 += textbox->get_w();
2596 listbox->reposition_window(x1, y1, -1, -1, 0);
2597 // if(flush) parent_window->flush();
2613 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2614 int64_t default_value, int x, int y)
2615 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2617 this->popup = popup;
2620 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2621 float default_value, int x, int y, int precision)
2622 : BC_TextBox(x, y, popup->text_w, 1, default_value, 1, MEDIUMFONT, precision)
2624 this->popup = popup;
2627 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2639 int BC_TumbleTextBoxText::handle_event()
2641 popup->handle_event();
2645 int BC_TumbleTextBoxText::button_press_event()
2647 if( get_enabled() && is_event_win() ) {
2648 if( get_buttonpress() < 4 ) return BC_TextBox::button_press_event();
2649 if( get_buttonpress() == 4 ) popup->tumbler->handle_up_event();
2650 else if( get_buttonpress() == 5 ) popup->tumbler->handle_down_event();
2659 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2660 int64_t default_value,
2672 this->default_value = default_value;
2673 this->text_w = text_w;
2674 this->parent_window = parent_window;
2680 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2693 this->default_value = default_value;
2694 this->text_w = text_w;
2695 this->parent_window = parent_window;
2701 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2702 float default_value_f,
2712 this->min_f = min_f;
2713 this->max_f = max_f;
2714 this->default_value_f = default_value_f;
2715 this->text_w = text_w;
2716 this->parent_window = parent_window;
2722 BC_TumbleTextBox::~BC_TumbleTextBox()
2724 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2725 // is deleted anyway by the windowbase so textbox deletes this.
2726 if(tumbler) delete tumbler;
2728 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2737 void BC_TumbleTextBox::reset()
2744 void BC_TumbleTextBox::set_precision(int precision)
2746 this->precision = precision;
2749 void BC_TumbleTextBox::set_increment(float value)
2751 this->increment = value;
2752 if(tumbler) tumbler->set_increment(value);
2755 void BC_TumbleTextBox::set_log_floatincrement(int value)
2757 this->log_floatincrement = value;
2758 if(tumbler) tumbler->set_log_floatincrement(value);
2761 int BC_TumbleTextBox::create_objects()
2763 int x = this->x, y = this->y;
2765 textbox = use_float ?
2766 new BC_TumbleTextBoxText(this, default_value_f, x, y, precision) :
2767 new BC_TumbleTextBoxText(this, default_value, x, y);
2769 parent_window->add_subwindow(textbox);
2770 x += textbox->get_w();
2772 tumbler = use_float ?
2773 (BC_Tumbler *)new BC_FTumbler(textbox, min_f, max_f, x, y) :
2774 (BC_Tumbler *)new BC_ITumbler(textbox, min, max, x, y);
2775 parent_window->add_subwindow(tumbler);
2776 tumbler->set_increment(increment);
2780 const char* BC_TumbleTextBox::get_text()
2782 return textbox->get_text();
2785 const wchar_t* BC_TumbleTextBox::get_wtext()
2787 return textbox->get_wtext();
2790 BC_TextBox* BC_TumbleTextBox::get_textbox()
2795 int BC_TumbleTextBox::update(const char *value)
2797 textbox->update(value);
2798 textbox->set_text_row(0);
2802 int BC_TumbleTextBox::update(int64_t value)
2804 textbox->update(value);
2805 textbox->set_text_row(0);
2809 int BC_TumbleTextBox::update(float value)
2811 textbox->update(value);
2812 textbox->set_text_row(0);
2817 int BC_TumbleTextBox::get_x()
2822 int BC_TumbleTextBox::get_y()
2827 int BC_TumbleTextBox::get_w()
2829 return textbox->get_w() + tumbler->get_w();
2832 int BC_TumbleTextBox::get_h()
2834 return textbox->get_h();
2837 void BC_TumbleTextBox::disable(int hide_text)
2839 if( hide_text && !textbox->is_hidden() )
2840 textbox->hide_window(0);
2841 if( !tumbler->is_hidden() )
2842 tumbler->hide_window(0);
2843 if( !get_enabled() ) return;
2844 return textbox->disable();
2847 void BC_TumbleTextBox::enable()
2849 if( textbox->is_hidden() )
2850 textbox->show_window(0);
2851 if( tumbler->is_hidden() )
2852 tumbler->show_window(0);
2853 if( get_enabled() ) return;
2854 return textbox->enable();
2857 int BC_TumbleTextBox::get_enabled()
2859 return textbox->get_enabled();
2862 int BC_TumbleTextBox::handle_event()
2867 void BC_TumbleTextBox::reposition_window(int x, int y)
2872 textbox->reposition_window(x,
2876 tumbler->reposition_window(x + textbox->get_w(),
2878 // if(flush) parent_window->flush();
2882 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2884 tumbler->set_boundaries(min, max);
2887 void BC_TumbleTextBox::set_boundaries(float min, float max)
2889 tumbler->set_boundaries(min, max);
2894 BC_TextMenu::BC_TextMenu(BC_TextBox *textbox)
2895 : BC_PopupMenu(0, 0, 0, "", 0)
2897 this->textbox = textbox;
2900 BC_TextMenu::~BC_TextMenu()
2904 void BC_TextMenu::create_objects()
2906 add_item(new BC_TextMenuCut(this));
2907 add_item(new BC_TextMenuCopy(this));
2908 add_item(new BC_TextMenuPaste(this));
2912 BC_TextMenuCut::BC_TextMenuCut(BC_TextMenu *menu)
2913 : BC_MenuItem(_("Cut"))
2918 int BC_TextMenuCut::handle_event()
2920 menu->textbox->cut(1);
2926 BC_TextMenuCopy::BC_TextMenuCopy(BC_TextMenu *menu)
2927 : BC_MenuItem(_("Copy"))
2932 int BC_TextMenuCopy::handle_event()
2934 menu->textbox->copy(1);
2939 BC_TextMenuPaste::BC_TextMenuPaste(BC_TextMenu *menu)
2940 : BC_MenuItem(_("Paste"))
2945 int BC_TextMenuPaste::handle_event()
2947 menu->textbox->paste(1);
2952 void BC_TumbleTextBox::set_tooltip(const char *text)
2954 textbox->set_tooltip(text);