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 // reference shared directly
60 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
61 int size, wchr_t *wtext, int has_border, int font)
62 : BC_SubWindow(x, y, w, 0, -1)
66 reset_parameters(rows, has_border, font, size);
67 wdemand(wstrlen(wtext));
68 wstrncpy(this->wtext, wtext, wsize);
71 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
72 const char *text, int has_border, int font, int is_utf8)
73 : BC_SubWindow(x, y, w, 0, -1)
75 this->is_utf8 = is_utf8;
77 reset_parameters(rows, has_border, font, BCTEXTLEN);
81 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
82 const wchr_t *wtext, int has_border, int font, int is_utf8)
83 : BC_SubWindow(x, y, w, 0, -1)
85 this->is_utf8 = is_utf8;
87 reset_parameters(rows, has_border, font, BCTEXTLEN);
89 wtext = new wchr_t[wsize+1];
90 wstrncpy(this->wtext, wtext, wsize);
91 this->wtext[wsize] = 0;
94 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
95 int64_t text, int has_border, int font)
96 : BC_SubWindow(x, y, w, 0, -1)
100 reset_parameters(rows, has_border, font, BCSTRLEN);
101 snprintf(this->text, this->tsize, "%jd", text);
102 dirty = 1; wtext_update();
105 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
106 float text, int has_border, int font, int precision)
107 : BC_SubWindow(x, y, w, 0, -1)
111 reset_parameters(rows, has_border, font, BCSTRLEN);
112 this->precision = precision;
113 snprintf(this->text, this->tsize, "%0.*f", precision, text);
114 dirty = 1; wtext_update();
117 BC_TextBox::BC_TextBox(int x, int y, int w, int rows,
118 int text, int has_border, int font)
119 : BC_SubWindow(x, y, w, 0, -1)
123 reset_parameters(rows, has_border, font, BCSTRLEN);
124 snprintf(this->text, this->tsize, "%d", text);
125 dirty = 1; wtext_update();
128 BC_TextBox::~BC_TextBox()
130 if(skip_cursor) delete skip_cursor;
131 delete suggestions_popup;
132 suggestions->remove_all_objects();
140 int BC_TextBox::reset_parameters(int rows, int has_border, int font, int size)
142 suggestions = new ArrayList<BC_ListBoxItem*>;
143 suggestions_popup = 0;
144 suggestion_column = 0;
147 this->has_border = has_border;
149 // size > 0: fixed buffer, size == 0: dynamic buffer
150 // size < 0: fixed shared buffer via set_text
152 this->tsize = size >= 0 ? size : -size;
153 this->text = size > 0 ? new char[this->tsize+1] : 0;
154 if( this->text ) this->text[0] = 0;
157 highlight_letter1 = highlight_letter2 = 0;
158 highlight_letter3 = highlight_letter4 = 0;
172 skip_cursor = new Timer;
183 selection_active = 0;
187 int BC_TextBox::tstrlen()
189 return !text ? 0 : strnlen(text, tsize);
192 int BC_TextBox::tstrcmp(const char *cp)
194 const char *tp = get_text();
195 return strncmp(tp, cp, tsize);
198 char *BC_TextBox::tstrcpy(const char *cp)
202 if( !size ) tdemand(strlen(cp));
203 strncpy(text, cp, tsize);
210 char *BC_TextBox::tstrcat(const char *cp)
213 if( !size ) tdemand(tstrlen() + strlen(cp));
214 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 tlen = tstrlen();
224 int nsize = tsize > 0 ? tsize : tlen + BCTEXTLEN;
226 wlen = BC_Resources::encode(src_enc, dst_enc, text, tlen,
227 (char*)wtext, wsize*sizeof(wchr_t)) / sizeof(wchr_t);
234 int BC_TextBox::text_update(const wchr_t *wcp, int wsz, char *tcp, int tsz)
236 const char *src_enc = BC_Resources::wide_encoding;
237 const char *dst_enc = BC_Resources::encoding;
238 if( wsz < 0 ) wsz = wstrlen(wcp);
239 int len = BC_Resources::encode(src_enc, dst_enc,
240 (char*)wcp, wsz*sizeof(wchr_t), tcp, tsz);
245 int BC_TextBox::initialize()
249 skip_cursor = new Timer;
250 skip_cursor->update();
252 text_ascent = get_text_ascent(font) + 1;
253 text_descent = get_text_descent(font) + 1;
254 text_height = text_ascent + text_descent;
255 ibeam_letter = wtext_update();
258 left_margin = right_margin = HORIZONTAL_MARGIN;
259 top_margin = bottom_margin = VERTICAL_MARGIN;
263 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
264 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
267 text_x = left_margin;
271 // Create the subwindow
272 BC_SubWindow::initialize();
274 BC_Resources *resources = get_resources();
277 if( back_color < 0 ) back_color = resources->text_background;
278 if( high_color < 0 ) high_color = resources->text_background_hi;
282 if( back_color < 0 ) back_color = bg_color;
283 if( high_color < 0 ) high_color = resources->text_background_noborder_hi;
287 set_cursor(IBEAM_CURSOR, 0, 0);
290 add_subwindow(menu = new BC_TextMenu(this));
291 menu->create_objects();
296 int BC_TextBox::calculate_h(BC_WindowBase *gui,
301 return rows * (gui->get_text_ascent(font) + 1 +
302 gui->get_text_descent(font) + 1) +
303 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
306 void BC_TextBox::set_precision(int precision)
308 this->precision = precision;
311 // Compute suggestions for a path
312 int BC_TextBox::calculate_suggestions(ArrayList<BC_ListBoxItem*> *entries, const char *filter)
314 // Let user delete suggestion
315 if(get_last_keypress() != BACKSPACE) {
316 // Compute suggestions
318 ArrayList<char*> suggestions;
319 const char *current_text = get_text();
320 int suggestion_column = 0;
321 char dirname[BCTEXTLEN]; dirname[0] = 0;
322 if( current_text[0] == '/' || current_text[0] == '~' )
323 strncpy(dirname, current_text, sizeof(dirname));
325 getcwd(dirname, sizeof(dirname));
326 // If directory, tabulate it
328 if( filter ) fs.set_filter(filter);
330 strncpy(dirname, current_text, sizeof(dirname));
331 if( (cp=strrchr(dirname, '/')) ||
332 (cp=strrchr(dirname, '~')) ) *++cp = 0;
333 fs.parse_tildas(dirname);
335 cp = (char *)current_text;
336 if( (prefix=strrchr(cp, '/')) ||
337 (prefix=strrchr(cp, '~')) ) ++prefix;
338 suggestion_column = !prefix ? 0 : prefix - cp;
339 int prefix_len = prefix ? strlen(prefix) : 0;
340 // only include items where the filename matches the basename prefix
341 for(int i = 0; i < fs.total_files(); i++) {
342 char *current_name = fs.get_entry(i)->name;
343 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
344 suggestions.append(current_name);
348 char *prefix = (char *)current_text;
349 int prefix_len = strlen(prefix);
350 for(int i = 0; i < entries->size(); i++) {
351 char *current_name = entries->get(i)->get_text();
352 if( prefix_len>0 && strncmp(prefix, current_name, prefix_len) ) continue;
353 suggestions.append(current_name);
356 set_suggestions(&suggestions, suggestion_column);
362 void BC_TextBox::no_suggestions()
364 if( suggestions_popup ) {
365 delete suggestions_popup;
366 suggestions_popup = 0;
371 void BC_TextBox::set_suggestions(ArrayList<char*> *suggestions, int column)
374 this->suggestions->remove_all_objects();
375 this->suggestion_column = column;
378 for(int i = 0; i < suggestions->size(); i++) {
379 this->suggestions->append(new BC_ListBoxItem(suggestions->get(i)));
382 // Show the popup without taking focus
383 if( suggestions->size() > 1 ) {
384 if( !suggestions_popup ) {
385 suggestions_popup = new BC_TextBoxSuggestions(this, x, y);
386 get_parent()->add_subwindow(suggestions_popup);
387 suggestions_popup->set_is_suggestions(1);
390 suggestions_popup->update(this->suggestions, 0, 0, 1);
392 suggestions_popup->activate(0);
395 // Show the highlighted text
396 if( suggestions->size() == 1 ) {
397 highlight_letter1 = wtext_update();
398 int len = text_update(wtext,wlen, text,tsize);
399 char *current_suggestion = suggestions->get(0);
400 int col = len - suggestion_column;
401 if( col < 0 ) col = 0;
402 char *cur = current_suggestion + col;
404 highlight_letter2 = wtext_update();
405 //printf("BC_TextBox::set_suggestions %d %d\n", __LINE__, suggestion_column);
413 if( !suggestions || !this->suggestions->size() )
417 void BC_TextBox::wset_selection(int char1, int char2, int ibeam)
419 highlight_letter1 = char1;
420 highlight_letter2 = char2;
421 ibeam_letter = ibeam;
425 // count utf8 chars in text which occur before cp
426 int BC_TextBox::wcpos(const char *text, const char *cp)
429 const unsigned char *bp = (const unsigned char *)text;
430 const unsigned char *ep = (const unsigned char *)cp;
431 while( bp < ep && *bp != 0 ) {
434 if( ch < 0x80 ) continue;
436 int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
437 for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
442 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
444 const char *cp = get_text();
445 wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
448 int BC_TextBox::update(const char *text)
450 //printf("BC_TextBox::update 1 %d %s %s\n", tstrcmp(text), text, this->text);
451 // Don't update if contents are the same
452 int bg_color = has_border || !highlighted ? back_color : high_color;
453 if( bg_color == background_color && !tstrcmp(text)) return 0;
455 int wtext_len = wtext_update();
456 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
457 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
458 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
463 int BC_TextBox::update(const wchr_t *wtext)
465 int wtext_len = wstrlen(wtext);
467 wstrncpy(this->wtext, wtext, wsize);
468 this->wlen = wtext_len;
469 if(highlight_letter1 > wtext_len) highlight_letter1 = wtext_len;
470 if(highlight_letter2 > wtext_len) highlight_letter2 = wtext_len;
471 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
476 int BC_TextBox::update(int64_t value)
478 char string[BCTEXTLEN];
479 sprintf(string, "%jd", value);
484 int BC_TextBox::update(float value)
486 char string[BCTEXTLEN];
487 sprintf(string, "%0.*f", precision, value);
492 void BC_TextBox::disable()
501 void BC_TextBox::enable()
511 int BC_TextBox::get_enabled() { return enabled; }
512 int BC_TextBox::get_text_x() { return text_x; }
513 int BC_TextBox::get_text_y() { return text_y; }
514 void BC_TextBox::set_text_x(int v) { text_x = v; }
515 void BC_TextBox::set_text_y(int v) { text_y = v; }
516 int BC_TextBox::get_back_color() { return back_color; }
517 void BC_TextBox::set_back_color(int v) { back_color = v; }
519 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
521 return (pixels - 4) /
522 (window->get_text_ascent(font) + 1 +
523 window->get_text_descent(font) + 1);
526 int BC_TextBox::calculate_row_h(int rows,
527 BC_WindowBase *parent_window,
532 (parent_window->get_text_ascent(font) + 1 +
533 parent_window->get_text_descent(font) + 1) +
534 (has_border ? 4 : 0);
537 const char* BC_TextBox::get_text()
539 int wtext_len = wtext_update();
540 text_update(wtext,wtext_len, text,tsize);
544 const wchr_t* BC_TextBox::get_wtext()
550 void BC_TextBox::set_text(char *text, int isz)
552 if( size < 0 && isz > 0 ) {
562 int BC_TextBox::get_text_rows()
564 int wtext_len = wtext_update();
566 for(int i = 0; i < wtext_len; i++) {
567 if(wtext[i] == '\n') result++;
573 int BC_TextBox::get_row_h(int rows)
575 return rows * text_height + top_margin + bottom_margin;
578 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
581 if(w < 0) w = get_w();
584 new_h = get_row_h(rows);
593 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
594 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
595 BC_WindowBase::reposition_window(x, y, w, new_h);
601 void BC_TextBox::draw_border()
603 BC_Resources *resources = get_resources();
605 set_color(background_color);
606 draw_box(0, 0, left_margin, get_h());
607 draw_box(get_w() - right_margin, 0, right_margin, get_h());
612 draw_3d_border(0, 0, w, h,
613 resources->text_border1,
614 resources->text_border2_hi,
615 resources->text_border3_hi,
616 resources->text_border4);
618 draw_3d_border(0, 0, w, h,
619 resources->text_border1,
620 resources->text_border2,
621 resources->text_border3,
622 resources->text_border4);
626 void BC_TextBox::draw_cursor()
628 // set_color(background_color);
636 draw_box(ibeam_x + text_x,
645 void BC_TextBox::draw(int flush)
648 int row_begin, row_end;
649 int highlight_x1, highlight_x2;
651 BC_Resources *resources = get_resources();
653 //printf("BC_TextBox::draw %d %s\n", __LINE__, text);
655 background_color = has_border || !highlighted ? back_color : high_color;
656 set_color(background_color);
657 draw_box(0, 0, w, h);
659 int wtext_len = wtext_update();
661 // Draw text with selection
664 for(i=0, k=text_y; i < wtext_len && k < get_h(); k += text_height) {
667 wchr_t *wtext_row = &wtext[i];
668 for( ; i<wtext_len && wtext[i]!='\n'; ++i );
669 if( (row_end=i) < wtext_len ) ++i;
671 if(k > top_margin-text_height && k < get_h()-bottom_margin) {
672 // Draw highlighted region of row
673 if( highlight_letter2 > highlight_letter1 &&
674 highlight_letter2 > row_begin &&
675 highlight_letter1 <= row_end ) {
676 int color = active && enabled && get_has_focus() ?
677 resources->text_highlight :
679 resources->text_selected_highlight :
680 resources->text_inactive_highlight;
681 if( unicode_active >= 0 )
684 if(highlight_letter1 >= row_begin &&
685 highlight_letter1 <= row_end)
686 highlight_x1 = get_x_position(highlight_letter1, row_begin);
690 if(highlight_letter2 > row_begin &&
691 highlight_letter2 <= row_end)
692 highlight_x2 = get_x_position(highlight_letter2, row_begin);
694 highlight_x2 = get_w();
696 draw_box(highlight_x1 + text_x, k,
697 highlight_x2 - highlight_x1, text_height);
700 // Draw text over highlight
701 int len = row_end - row_begin;
703 set_color(enabled ? resources->text_default : DMGREY);
704 draw_single_text(1, font, text_x, k + text_ascent, wtext_row, len);
707 // Get ibeam location
708 if(ibeam_letter >= row_begin && ibeam_letter <= row_end) {
710 ibeam_y = k - text_y;
711 ibeam_x = get_x_position(ibeam_letter, row_begin);
716 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
718 // ibeam_x = ibeam_y = !wtext_len ? 0 : -1;
719 ibeam_x = 0; ibeam_y = k - text_y;
722 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
732 int BC_TextBox::focus_in_event()
738 int BC_TextBox::focus_out_event()
744 int BC_TextBox::cursor_enter_event()
746 if( top_level->event_win == win && enabled &&
747 !(top_level->get_resources()->textbox_focus_policy & CLICK_ACTIVATE) )
750 top_level->deactivate();
763 int BC_TextBox::cursor_leave_event()
772 if( !suggestions_popup && !get_button_down() &&
773 !(top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
778 int BC_TextBox::button_press_event()
782 if(!enabled) return 0;
783 // if(get_buttonpress() != WHEEL_UP &&
784 // get_buttonpress() != WHEEL_DOWN &&
785 // get_buttonpress() != LEFT_BUTTON &&
786 // get_buttonpress() != MIDDLE_BUTTON) return 0;
788 if(debug) printf("BC_TextBox::button_press_event %d\n", __LINE__);
790 int cursor_letter = 0;
791 int wtext_len = wtext_update();
792 int update_scroll = 0;
795 if(top_level->event_win == win)
800 top_level->deactivate();
805 if(get_buttonpress() == WHEEL_UP)
807 text_y += text_height;
808 text_y = MIN(text_y, top_margin);
812 if(get_buttonpress() == WHEEL_DOWN)
814 int min_y = -(get_text_rows() *
818 text_y -= text_height;
819 text_y = MAX(text_y, min_y);
820 text_y = MIN(text_y, top_margin);
824 if(get_buttonpress() == RIGHT_BUTTON)
826 menu->activate_menu();
831 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
833 //printf("BC_TextBox::button_press_event %d %d\n", __LINE__, cursor_letter);
836 if(get_triple_click())
838 //printf("BC_TextBox::button_press_event %d\n", __LINE__);
840 select_line(highlight_letter1, highlight_letter2, cursor_letter);
841 highlight_letter3 = highlight_letter1;
842 highlight_letter4 = highlight_letter2;
843 ibeam_letter = highlight_letter2;
844 copy_selection(PRIMARY_SELECTION);
847 if(get_double_click())
850 select_word(highlight_letter1, highlight_letter2, cursor_letter);
851 highlight_letter3 = highlight_letter1;
852 highlight_letter4 = highlight_letter2;
853 ibeam_letter = highlight_letter2;
854 copy_selection(PRIMARY_SELECTION);
857 if(get_buttonpress() == MIDDLE_BUTTON)
859 highlight_letter3 = highlight_letter4 =
860 ibeam_letter = highlight_letter1 =
861 highlight_letter2 = cursor_letter;
862 paste_selection(PRIMARY_SELECTION);
867 highlight_letter3 = highlight_letter4 =
868 ibeam_letter = highlight_letter1 =
869 highlight_letter2 = cursor_letter;
873 // Handle scrolling by highlighting text
874 if(text_selected || word_selected || line_selected)
876 set_repeat(top_level->get_resources()->scroll_repeat);
879 if(ibeam_letter < 0) ibeam_letter = 0;
880 if(ibeam_letter > wtext_len) ibeam_letter = wtext_len;
884 if(update_scroll && yscroll)
886 yscroll->update_length(get_text_rows(),
888 yscroll->get_handlelength(),
896 if( suggestions_popup && (!yscroll || !yscroll->is_event_win())) {
897 if( suggestions_popup->button_press_event() )
898 return suggestions_popup->handle_event();
900 else if( (top_level->get_resources()->textbox_focus_policy & CLICK_DEACTIVATE) )
907 int BC_TextBox::button_release_event()
912 if(text_selected || word_selected || line_selected)
918 // Stop scrolling by highlighting text
919 unset_repeat(top_level->get_resources()->scroll_repeat);
925 int BC_TextBox::cursor_motion_event()
927 int cursor_letter, letter1, letter2;
930 if(text_selected || word_selected || line_selected)
932 cursor_letter = get_cursor_letter(top_level->cursor_x,
933 top_level->cursor_y);
935 //printf("BC_TextBox::cursor_motion_event %d cursor_letter=%d\n", __LINE__, cursor_letter);
939 select_line(letter1, letter2, cursor_letter);
944 select_word(letter1, letter2, cursor_letter);
949 letter1 = letter2 = cursor_letter;
952 if(letter1 <= highlight_letter3)
954 highlight_letter1 = letter1;
955 highlight_letter2 = highlight_letter4;
956 ibeam_letter = letter1;
959 if(letter2 >= highlight_letter4)
961 highlight_letter2 = letter2;
962 highlight_letter1 = highlight_letter3;
963 ibeam_letter = letter2;
966 copy_selection(PRIMARY_SELECTION);
979 int BC_TextBox::activate()
983 top_level->set_active_subwindow(this);
984 top_level->set_repeat(top_level->get_resources()->blink_rate);
990 int BC_TextBox::deactivate()
994 text_selected = word_selected = line_selected = 0;
995 top_level->set_active_subwindow(0);
996 top_level->unset_repeat(top_level->get_resources()->blink_rate);
1002 int BC_TextBox::repeat_event(int64_t duration)
1005 int cursor_y = get_cursor_y();
1006 //int cursor_x = get_cursor_x();
1008 if(duration == top_level->get_resources()->tooltip_delay &&
1009 tooltip_text && tooltip_text[0] != 0 && highlighted)
1015 if(duration == top_level->get_resources()->blink_rate &&
1019 // don't flash if keypress
1020 if(skip_cursor->get_difference() < 500)
1022 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
1023 // skip_cursor->get_difference(),
1029 if(!(text_selected || word_selected || line_selected))
1038 if(duration == top_level->get_resources()->scroll_repeat &&
1039 (text_selected || word_selected || line_selected))
1042 if(get_cursor_y() < top_margin)
1044 difference = get_cursor_y() - top_margin ;
1047 if(get_cursor_y() > get_h() - bottom_margin)
1049 difference = get_cursor_y() -
1050 (get_h() - bottom_margin);
1052 if(difference != 0) {
1053 int min_y = -(get_text_rows() * text_height -
1054 get_h() + bottom_margin);
1056 text_y -= difference;
1057 // printf("BC_TextBox::repeat_event %d %d %d\n",
1061 text_y = MAX(min_y, text_y);
1062 text_y = MIN(text_y, top_margin);
1069 if(get_cursor_x() < left_margin)
1071 int difference = left_margin - get_cursor_x();
1073 text_x += difference;
1074 text_x = MIN(text_x, left_margin);
1078 else if(get_cursor_x() > get_w() - right_margin)
1080 int difference = get_cursor_x() - (get_w() - right_margin);
1081 int new_text_x = text_x - difference;
1083 // Get width of current row
1086 int wtext_len = wtext_update();
1089 for(int i = 0, k = text_y; i < wtext_len; k += text_height)
1092 while(wtext[i] != '\n' && i < wtext_len) {
1096 if(wtext[i] == '\n') i++;
1098 if(cursor_y >= k && cursor_y < k + text_height) {
1099 row_width = get_text_width(font,
1101 row_end - row_begin);
1107 min_x = -row_width + get_w() - left_margin - BCCURSORW;
1108 new_text_x = MAX(new_text_x, min_x);
1109 new_text_x = MIN(new_text_x, left_margin);
1111 if(new_text_x < text_x) text_x = new_text_x;
1120 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
1122 int key = top_level->get_keypress(), len;
1123 wchr_t *wkeys = top_level->get_wkeystring(&len);
1125 case KPENTER: key = '\n'; goto kpchr;
1126 case KPMINUS: key = '-'; goto kpchr;
1127 case KPPLUS: key = '+'; goto kpchr;
1128 case KPDEL: key = '.'; goto kpchr;
1129 case RETURN: key = '\n'; goto kpchr;
1130 case KPINS: key = '0'; goto kpchr;
1131 case KP1: case KP2: case KP3: case KP4: case KP5:
1132 case KP6: case KP7: case KP8: case KP9:
1133 key = key - KP1 + '1';
1135 wkeys[0] = key; wkeys[1] = 0; len = 1;
1138 if( key < 32 || key > 255 ) return;
1140 insert_text(wkeys, len);
1147 int BC_TextBox::keypress_event()
1149 // Result == 2 contents changed
1150 // Result == 1 trapped keypress
1151 // Result == 0 nothing
1153 int dispatch_event = 0;
1155 if(!active || !enabled) return 0;
1157 int wtext_len = wtext_update();
1158 last_keypress = get_keypress();
1160 if( unicode_active >= 0 ) {
1163 switch( last_keypress ) {
1164 //unicode active acitons
1166 for( int i=highlight_letter1+1; i<highlight_letter2; ++i ) {
1167 int ch = nib(wtext[i]);
1168 if( ch < 0 ) return 1;
1169 wch = (wch<<4) + ch;
1173 unicode_active = -1;
1179 unicode_active = -1;
1184 if(ibeam_letter > 0) {
1185 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1186 highlight_letter2 = --ibeam_letter;
1187 if( highlight_letter1 >= highlight_letter2 )
1188 unicode_active = -1;
1193 case KPINS: last_keypress = KP1-'1'+'0'; // fall thru
1194 case KP1: case KP2: case KP3: case KP4: case KP5:
1195 case KP6: case KP7: case KP8: case KP9:
1196 last_keypress = last_keypress-KP1 + '1';
1197 case '0': case '1': case '2': case '3': case '4':
1198 case '5': case '6': case '7': case '8': case '9':
1199 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1200 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': {
1201 int n = nib(last_keypress);
1202 wch = n < 10 ? '0'+n : 'A'+n-10;
1216 insert_text(&wch, wlen);
1218 if( unicode_active >= 0 )
1219 highlight_letter2 = ibeam_letter;
1221 highlight_letter1 = highlight_letter2 = 0;
1227 //printf("BC_TextBox::keypress_event %d %x\n", __LINE__, last_keypress)
1228 switch(last_keypress) {
1230 // Deactivate the suggestions
1231 if( suggestions_popup ) {
1236 top_level->deactivate();
1243 highlight_letter1 = highlight_letter2 = 0;
1244 ibeam_letter = wtext_update();
1245 top_level->deactivate();
1250 default_keypress(dispatch_event, result);
1255 // Handle like a default keypress
1257 top_level->cycle_textboxes(1);
1262 top_level->cycle_textboxes(-1);
1267 if(ibeam_letter > 0) {
1268 int old_ibeam_letter = ibeam_letter;
1276 while(ibeam_letter > 0 && isalnum(wtext[ibeam_letter - 1]))
1281 if(top_level->shift_down()) {
1282 // Initialize highlighting
1283 if(highlight_letter1 == highlight_letter2) {
1284 highlight_letter1 = ibeam_letter;
1285 highlight_letter2 = old_ibeam_letter;
1287 else if(highlight_letter1 == old_ibeam_letter) {
1288 // Extend left highlight
1289 highlight_letter1 = ibeam_letter;
1291 else if(highlight_letter2 == old_ibeam_letter) {
1292 // Shrink right highlight
1293 highlight_letter2 = ibeam_letter;
1297 highlight_letter1 = highlight_letter2 = ibeam_letter;
1302 if(keypress_draw) draw(1);
1308 if(ibeam_letter < wtext_len) {
1309 int old_ibeam_letter = ibeam_letter;
1316 while(ibeam_letter < wtext_len && isalnum(wtext[ibeam_letter++]));
1322 if(top_level->shift_down()) {
1323 // Initialize highlighting
1324 if(highlight_letter1 == highlight_letter2) {
1325 highlight_letter1 = old_ibeam_letter;
1326 highlight_letter2 = ibeam_letter;
1328 else if(highlight_letter1 == old_ibeam_letter) {
1329 // Shrink left highlight
1330 highlight_letter1 = ibeam_letter;
1332 else if(highlight_letter2 == old_ibeam_letter) {
1333 // Expand right highlight
1334 highlight_letter2 = ibeam_letter;
1338 highlight_letter1 = highlight_letter2 = ibeam_letter;
1342 if(keypress_draw) draw(1);
1348 if( suggestions && suggestions_popup ) {
1349 // Pass to suggestions popup
1350 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1351 suggestions_popup->activate(1);
1352 suggestions_popup->keypress_event();
1355 else if(ibeam_letter > 0) {
1356 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
1357 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1358 ibeam_y + text_y - text_height);
1359 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
1362 if(top_level->shift_down()) {
1363 // Initialize highlighting
1364 if(highlight_letter1 == highlight_letter2) {
1365 highlight_letter1 = new_letter;
1366 highlight_letter2 = ibeam_letter;
1368 else if(highlight_letter1 == ibeam_letter) {
1369 // Expand left highlight
1370 highlight_letter1 = new_letter;
1372 else if(highlight_letter2 == ibeam_letter) {
1373 // Shrink right highlight
1374 highlight_letter2 = new_letter;
1378 highlight_letter1 = highlight_letter2 = new_letter;
1380 if(highlight_letter1 > highlight_letter2) {
1381 int temp = highlight_letter1;
1382 highlight_letter1 = highlight_letter2;
1383 highlight_letter2 = temp;
1385 ibeam_letter = new_letter;
1388 if(keypress_draw) draw(1);
1394 if(ibeam_letter > 0) {
1395 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1396 ibeam_y + text_y - get_h());
1399 if(top_level->shift_down()) {
1400 // Initialize highlighting
1401 if(highlight_letter1 == highlight_letter2) {
1402 highlight_letter1 = new_letter;
1403 highlight_letter2 = ibeam_letter;
1405 else if(highlight_letter1 == ibeam_letter) {
1406 // Expand left highlight
1407 highlight_letter1 = new_letter;
1409 else if(highlight_letter2 == ibeam_letter) {
1410 // Shrink right highlight
1411 highlight_letter2 = new_letter;
1415 highlight_letter1 = highlight_letter2 = new_letter;
1417 if(highlight_letter1 > highlight_letter2) {
1418 int temp = highlight_letter1;
1419 highlight_letter1 = highlight_letter2;
1420 highlight_letter2 = temp;
1422 ibeam_letter = new_letter;
1425 if(keypress_draw) draw(1);
1431 // printf("BC_TextBox::keypress_event %d %p %p\n",
1434 // suggestions_popup);
1435 if( suggestions && suggestions_popup ) {
1436 // Pass to suggestions popup
1437 suggestions_popup->activate(1);
1438 suggestions_popup->keypress_event();
1442 // if(ibeam_letter > 0)
1445 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1446 ibeam_y + text_y + text_height);
1447 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1449 if(top_level->shift_down()) {
1450 // Initialize highlighting
1451 if(highlight_letter1 == highlight_letter2) {
1452 highlight_letter1 = new_letter;
1453 highlight_letter2 = ibeam_letter;
1455 else if(highlight_letter1 == ibeam_letter) {
1456 // Shrink left highlight
1457 highlight_letter1 = new_letter;
1459 else if(highlight_letter2 == ibeam_letter) {
1460 // Expand right highlight
1461 highlight_letter2 = new_letter;
1465 highlight_letter1 = highlight_letter2 = new_letter;
1467 if(highlight_letter1 > highlight_letter2) {
1468 int temp = highlight_letter1;
1469 highlight_letter1 = highlight_letter2;
1470 highlight_letter2 = temp;
1472 ibeam_letter = new_letter;
1475 if(keypress_draw) draw(1);
1477 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1484 int new_letter = get_cursor_letter2(ibeam_x + text_x,
1485 ibeam_y + text_y + get_h());
1486 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1488 if(top_level->shift_down()) {
1489 // Initialize highlighting
1490 if(highlight_letter1 == highlight_letter2) {
1491 highlight_letter1 = new_letter;
1492 highlight_letter2 = ibeam_letter;
1494 else if(highlight_letter1 == ibeam_letter) {
1495 // Shrink left highlight
1496 highlight_letter1 = new_letter;
1498 else if(highlight_letter2 == ibeam_letter) {
1499 // Expand right highlight
1500 highlight_letter2 = new_letter;
1504 highlight_letter1 = highlight_letter2 = new_letter;
1506 if(highlight_letter1 > highlight_letter2) {
1507 int temp = highlight_letter1;
1508 highlight_letter1 = highlight_letter2;
1509 highlight_letter2 = temp;
1511 ibeam_letter = new_letter;
1514 if(keypress_draw) draw(1);
1516 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1523 int old_ibeam_letter = ibeam_letter;
1525 while(ibeam_letter < wtext_len && wtext[ibeam_letter] != '\n')
1528 if(top_level->shift_down()) {
1530 if(highlight_letter1 == highlight_letter2) {
1531 highlight_letter2 = ibeam_letter;
1532 highlight_letter1 = old_ibeam_letter;
1534 else if(highlight_letter1 == old_ibeam_letter) {
1536 highlight_letter1 = highlight_letter2;
1537 highlight_letter2 = ibeam_letter;
1539 else if(highlight_letter2 == old_ibeam_letter) {
1541 highlight_letter2 = ibeam_letter;
1545 highlight_letter1 = highlight_letter2 = ibeam_letter;
1548 if(keypress_draw) draw(1);
1555 int old_ibeam_letter = ibeam_letter;
1557 while(ibeam_letter > 0 && wtext[ibeam_letter - 1] != '\n')
1560 if(top_level->shift_down())
1563 if(highlight_letter1 == highlight_letter2)
1565 highlight_letter2 = old_ibeam_letter;
1566 highlight_letter1 = ibeam_letter;
1570 if(highlight_letter1 == old_ibeam_letter)
1572 highlight_letter1 = ibeam_letter;
1576 if(highlight_letter2 == old_ibeam_letter)
1578 highlight_letter2 = highlight_letter1;
1579 highlight_letter1 = ibeam_letter;
1583 highlight_letter1 = highlight_letter2 = ibeam_letter;
1586 if(keypress_draw) draw(1);
1593 if(highlight_letter1 == highlight_letter2) {
1594 if(ibeam_letter > 0) {
1595 delete_selection(ibeam_letter - 1, ibeam_letter, wtext_len);
1600 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1601 highlight_letter2 = ibeam_letter = highlight_letter1;
1605 if(keypress_draw) draw(1);
1611 //printf("BC_TextBox::keypress_event %d\n", __LINE__);
1612 if(highlight_letter1 == highlight_letter2) {
1613 if(ibeam_letter < wtext_len) {
1614 delete_selection(ibeam_letter, ibeam_letter + 1, wtext_len);
1618 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1619 highlight_letter2 = ibeam_letter = highlight_letter1;
1623 if(keypress_draw) draw(1);
1630 switch( last_keypress ) {
1631 case 'c': case 'C': {
1634 case 'v': case 'V': {
1638 case 'x': case 'X': {
1642 case 'u': case 'U': {
1643 if( shift_down() ) {
1644 unicode_active = ibeam_letter;
1646 insert_text(&wkey, 1);
1648 highlight_letter1 = unicode_active;
1649 highlight_letter2 = ibeam_letter;
1658 default_keypress(dispatch_event, result);
1663 if(result) skip_cursor->update();
1664 if(dispatch_event && handle_event())
1671 int BC_TextBox::cut(int do_update)
1673 if( highlight_letter1 != highlight_letter2 ) {
1674 int wtext_len = wtext_update();
1675 copy_selection(SECONDARY_SELECTION);
1676 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1677 highlight_letter2 = ibeam_letter = highlight_letter1;
1685 skip_cursor->update();
1691 int BC_TextBox::copy(int do_update)
1694 if( highlight_letter1 != highlight_letter2 ) {
1695 copy_selection(SECONDARY_SELECTION);
1698 skip_cursor->update();
1704 int BC_TextBox::paste(int do_update)
1706 paste_selection(SECONDARY_SELECTION);
1711 skip_cursor->update();
1718 int BC_TextBox::uses_text()
1724 void BC_TextBox::delete_selection(int letter1, int letter2, int wtext_len)
1727 for(i=letter1, j=letter2; j<wtext_len; i++, j++) {
1728 wtext[i] = wtext[j];
1736 int BC_TextBox::wdemand(int len)
1738 if( wtext && wsize >= len ) return 0;
1739 int nsize = len + wlen/2 + BCTEXTLEN;
1740 wchr_t *ntext = new wchr_t[nsize+1];
1742 memcpy(ntext, wtext, wsize*sizeof(wtext[0]));
1743 delete [] wtext; wtext = ntext; wsize = nsize;
1747 int BC_TextBox::tdemand(int len)
1749 if( text && tsize >= len ) return 0;
1750 int tlen = !text ? 0 : strlen(text);
1751 int nsize = len + tlen/2 + BCTEXTLEN;
1752 char *ntext = new char[nsize+1];
1754 memcpy(ntext, text, tsize*sizeof(text[0]));
1755 delete [] text; text = ntext; tsize = nsize;
1759 void BC_TextBox::insert_text(const wchr_t *wcp, int len)
1761 if( len < 0 ) len = wstrlen(wcp);
1762 int wtext_len = wtext_update();
1763 wdemand(wtext_len + len + 1);
1764 if( unicode_active < 0 && highlight_letter1 < highlight_letter2 ) {
1765 delete_selection(highlight_letter1, highlight_letter2, wtext_len);
1766 highlight_letter2 = ibeam_letter = highlight_letter1;
1767 wtext_len = wtext_update();
1771 for( i=wtext_len, j=wtext_len+len; --i>=ibeam_letter; )
1772 if( --j < wsize ) wtext[j] = wtext[i];
1773 for( i=ibeam_letter,j=0; j<len; ++i, ++j )
1774 if( i < wsize ) wtext[i] = wcp[j];
1776 if( (wlen+=len) > wsize ) wlen = wsize;
1777 if( (ibeam_letter+=len) > wsize ) ibeam_letter = wsize;
1778 wtext[wlen] = 0; // wtext allocated wsize+1
1782 int BC_TextBox::is_separator(const char *txt, int i)
1784 if( i != 0 || separators[0] != '+' ) return !isalnum(txt[i]);
1785 return txt[0] != '+' && txt[0] != '-' && !isalnum(txt[0]);
1788 // used for time entry
1789 void BC_TextBox::do_separators(int ibeam_left)
1793 // Remove separators from text
1794 int wtext_len = wtext_update();
1795 for(int i = 0; i < wtext_len; ) {
1796 if( !iswalnum(wtext[i]) ) {
1797 for(int j = i; j < wtext_len - 1; j++)
1798 wtext[j] = wtext[j + 1];
1799 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1805 wtext[wtext_len] = 0;
1811 // Insert separators into text
1812 int separator_len = strlen(separators);
1813 for(int i = 0; i < separator_len; i++) {
1815 // Insert a separator
1816 if( is_separator(separators,i) ) {
1817 for(int j = wtext_len; j >= i; j--) {
1818 wtext[j + 1] = wtext[j];
1820 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1822 wtext[i] = separators[i];
1826 wtext[i] = separators[i];
1831 wtext[separator_len] = 0;
1832 wlen = separator_len;
1836 int BC_TextBox::get_x_position(int i, int start)
1838 return get_text_width(font, &wtext[start], i - start);
1841 void BC_TextBox::get_ibeam_position(int &x, int &y)
1843 int i, row_begin, row_end;
1844 int wtext_len = wtext_update();
1847 for( i=0; i<wtext_len; ) {
1849 for(; i<wtext_len && wtext[i]!='\n'; i++);
1852 if( ibeam_letter >= row_begin && ibeam_letter <= row_end ) {
1853 x = get_x_position(ibeam_letter, row_begin);
1854 //printf("BC_TextBox::get_ibeam_position %d %d %d %d %d\n", ibeam_letter, row_begin, row_end, x, y);
1858 if( i < wtext_len && wtext[i] == '\n' ) {
1863 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1869 void BC_TextBox::set_text_row(int row)
1871 text_x = left_margin;
1872 text_y = -(row * text_height) + top_margin;
1876 int BC_TextBox::get_text_row()
1878 return -(text_y - top_margin) / text_height;
1881 void BC_TextBox::find_ibeam(int dispatch_event)
1884 int old_x = text_x, old_y = text_y;
1886 get_ibeam_position(x, y);
1888 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1890 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1891 if(text_x > left_margin) text_x = left_margin;
1894 if(left_margin + text_x + x < left_margin)
1896 text_x = -(x - (get_w() / 4)) + left_margin;
1897 if(text_x > left_margin) text_x = left_margin;
1900 int text_row = y / text_height;
1901 if( text_row < rows ) text_y = top_margin;
1903 int pix_rows = get_h() - bottom_margin - (y + text_y);
1904 if( pix_rows < text_height )
1905 text_y -= text_height * ((2*text_height-1-pix_rows) / text_height);
1907 pix_rows = y + text_y - top_margin;
1908 if( pix_rows < 0 ) {
1909 text_y += text_height * ((text_height-1-pix_rows) / text_height);
1910 if( text_y > top_margin ) text_y = top_margin;
1913 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1917 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1919 int i, j, k, row_begin, row_end, result = 0, done = 0;
1920 int column1, column2;
1921 int got_visible_row = 0;
1923 // Select complete row if cursor above the window
1924 //printf("BC_TextBox::get_cursor_letter %d %d\n", __LINE__, text_y);
1925 if(cursor_y < text_y - text_height)
1931 int wtext_len = wtext_update();
1933 for(i=0, k=text_y; i<wtext_len && k<get_h() && !done; k+=text_height) {
1934 // Simulate drawing of 1 row
1936 for(j = 0; wtext[i]!='\n' && i<wtext_len; i++);
1940 int first_visible_row = 0;
1941 int last_visible_row = 0;
1942 if( k+text_height > top_margin && !got_visible_row) {
1943 first_visible_row = 1;
1944 got_visible_row = 1;
1947 if( (k+text_height >= get_h() - bottom_margin ||
1948 (row_end >= wtext_len && k < get_h() - bottom_margin &&
1949 k + text_height > 0)) )
1950 last_visible_row = 1;
1952 // Cursor is inside vertical range of row
1953 if((cursor_y >= top_margin &&
1954 cursor_y < get_h() - bottom_margin &&
1955 cursor_y >= k && cursor_y < k + text_height) ||
1956 // Cursor is above 1st row
1957 (cursor_y < k + text_height && first_visible_row) ||
1958 // Cursor is below last row
1959 (cursor_y >= k && last_visible_row))
1961 column1 = column2 = 0;
1962 for(j = row_begin; j<wsize && j<=row_end && !done; j++) {
1963 column2 = get_text_width(font, &wtext[row_begin], j-row_begin) + text_x;
1964 if((column2 + column1) / 2 >= cursor_x) {
1967 // printf("BC_TextBox::get_cursor_letter %d %d %d %d\n",
1968 // __LINE__, result, first_visible_row, last_visible_row);
1979 if(wtext[i] == '\n') i++;
1981 // Select complete row if last visible & cursor is below window
1982 if(last_visible_row && cursor_y > k + text_height * 2)
1985 if(i >= wtext_len && !done) {
1991 // printf("BC_TextBox::get_cursor_letter %d cursor_y=%d k=%d h=%d %d %d\n",
1992 // __LINE__, cursor_y, k, get_h(), first_visible_row, last_visible_row);
1993 if(result < 0) result = 0;
1994 if(result > wtext_len) {
1995 //printf("BC_TextBox::get_cursor_letter %d\n", __LINE__);
2003 int BC_TextBox::get_cursor_letter2(int cursor_x, int cursor_y)
2005 int i, j, k, row_begin, row_end, result = 0, done = 0;
2006 int column1, column2;
2007 int wtext_len = wtext_update();
2009 if(cursor_y < text_y) {
2014 for(i = 0, k = text_y; i < wtext_len && !done; k += text_height) {
2016 for(; wtext[i] != '\n' && i < wtext_len; i++);
2019 if(cursor_y >= k && cursor_y < k + text_height) {
2020 column1 = column2 = 0;
2021 for(j = 0; j <= row_end - row_begin && !done; j++) {
2022 column2 = get_text_width(font, &wtext[row_begin], j) + text_x;
2023 if((column2 + column1) / 2 >= cursor_x) {
2024 result = row_begin + j - 1;
2035 if(wtext[i] == '\n') i++;
2037 if(i >= wtext_len && !done) {
2041 if(result < 0) result = 0;
2042 if(result > wtext_len) result = wtext_len;
2047 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
2049 int wtext_len = wtext_update();
2050 letter1 = letter2 = ibeam_letter;
2051 if( letter1 < 0 ) letter1 = 0;
2052 if( letter2 < 0 ) letter2 = 0;
2053 if( letter1 > wtext_len ) letter1 = wtext_len;
2054 if( letter2 > wtext_len ) letter2 = wtext_len;
2055 if( !wtext_len ) return;
2056 for( int i=letter1; i>=0 && iswalnum(wtext[i]); --i ) letter1 = i;
2057 for( int i=letter2; i<wtext_len && iswalnum(wtext[i]); ) letter2 = ++i;
2058 if( letter2 < wtext_len && wtext[letter2] == ' ' ) ++letter2;
2062 void BC_TextBox::select_line(int &letter1, int &letter2, int ibeam_letter)
2064 int wtext_len = wtext_update();
2065 letter1 = letter2 = ibeam_letter;
2066 if( letter1 < 0 ) letter1 = 0;
2067 if( letter2 < 0 ) letter2 = 0;
2068 if( letter1 > wtext_len ) letter1 = wtext_len;
2069 if( letter2 > wtext_len ) letter2 = wtext_len;
2070 if( !wtext_len ) return;
2071 for( int i=letter1; i>=0 && wtext[i]!='\n'; --i ) letter1 = i;
2072 for( int i=letter2; i<wtext_len && wtext[i]!='\n'; ) letter2 = ++i;
2075 void BC_TextBox::copy_selection(int clipboard_num)
2077 int wtext_len = wtext_update();
2078 if(!wtext_len) return;
2080 if(highlight_letter1 >= wtext_len || highlight_letter2 > wtext_len ||
2081 highlight_letter1 < 0 || highlight_letter2 < 0 ||
2082 highlight_letter2 - highlight_letter1 <= 0) return;
2083 int clip_len = highlight_letter2 - highlight_letter1;
2084 //printf(" BC_TextBox::copy_selection %d %d %d\n",highlight_letter1, highlight_letter2, clip_len);
2085 char ctext[4*clip_len+4];
2086 clip_len = text_update(&wtext[highlight_letter1],clip_len, ctext,4*clip_len+4);
2087 to_clipboard(ctext, clip_len, clipboard_num);
2088 selection_active = 1;
2091 int BC_TextBox::selection_clear_event()
2093 if( !is_event_win() ) return 0;
2094 selection_active = 0;
2099 void BC_TextBox::paste_selection(int clipboard_num)
2101 int len = clipboard_len(clipboard_num);
2104 char cstring[len]; wchr_t wstring[len];
2105 from_clipboard(cstring, len, clipboard_num); --len;
2106 //printf("BC_TextBox::paste_selection %d '%*.*s'\n",len,len,len,cstring);
2107 len = BC_Resources::encode(BC_Resources::encoding, BC_Resources::wide_encoding,
2108 cstring,len, (char *)wstring,(len+1)*sizeof(wchr_t)) / sizeof(wchr_t);
2109 insert_text(wstring, len);
2114 void BC_TextBox::set_keypress_draw(int value)
2116 keypress_draw = value;
2119 int BC_TextBox::get_last_keypress()
2121 return last_keypress;
2124 int BC_TextBox::get_ibeam_letter()
2126 return ibeam_letter;
2129 void BC_TextBox::set_ibeam_letter(int number, int redraw)
2131 this->ibeam_letter = number;
2138 void BC_TextBox::set_separators(const char *separators)
2140 this->separators = (char*)separators;
2143 int BC_TextBox::get_rows()
2154 BC_TextBoxSuggestions::BC_TextBoxSuggestions(BC_TextBox *text_box, int x, int y)
2155 : BC_ListBox(x, y, text_box->get_w(), 200, LISTBOX_TEXT,
2156 text_box->suggestions, 0, 0, 1, 0, 1)
2158 this->text_box = text_box;
2160 set_justify(LISTBOX_LEFT);
2163 BC_TextBoxSuggestions::~BC_TextBoxSuggestions()
2167 int BC_TextBoxSuggestions::handle_event()
2169 char *current_suggestion = 0;
2170 BC_ListBoxItem *item = get_selection(0, 0);
2171 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2172 if(item && (current_suggestion=item->get_text()) != 0)
2174 int col = text_box->suggestion_column;
2175 int len = BCTEXTLEN-1 - col;
2176 char *cp = &text_box->text[col];
2177 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2178 strncpy(cp, current_suggestion, len);
2179 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2180 if( (col=strlen(current_suggestion)) >= len )
2182 text_box->dirty = 1;
2186 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2187 text_box->highlight_letter1 =
2188 text_box->highlight_letter2 =
2189 text_box->ibeam_letter = text_box->tstrlen();
2190 text_box->wtext_update();
2192 text_box->handle_event();
2193 //printf("BC_TextBoxSuggestions::handle_event %d\n", __LINE__);
2198 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2199 int x, int y, int w, int rows,
2200 const char *default_text, int default_size)
2202 this->parent_window = parent_window;
2207 xscroll = 0; yscroll = 0;
2208 this->default_text = default_text;
2209 this->default_wtext = 0;
2210 this->default_size = default_size;
2213 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
2214 int x, int y, int w, int rows,
2215 const wchr_t *default_wtext, int default_size)
2217 this->parent_window = parent_window;
2222 xscroll = 0; yscroll = 0;
2223 this->default_text = 0;
2224 this->default_wtext = default_wtext;
2225 this->default_size = default_size;
2228 BC_ScrollTextBox::~BC_ScrollTextBox()
2238 void BC_ScrollTextBox::create_objects()
2240 // Must be created first
2241 parent_window->add_subwindow(text = default_wtext ?
2242 new BC_ScrollTextBoxText(this, default_wtext) :
2243 new BC_ScrollTextBoxText(this, default_text));
2247 void BC_ScrollTextBox::set_text(char *text, int isz)
2249 this->text->set_text(text, isz);
2250 update_scrollbars();
2253 int BC_ScrollTextBox::set_text_row(int n)
2255 text->set_text_row(n);
2256 update_scrollbars();
2260 void BC_ScrollTextBox::update(const char *text)
2262 this->text->update(text);
2263 update_scrollbars();
2266 void BC_ScrollTextBox::update(const wchr_t *wtext)
2268 this->text->update(wtext);
2269 update_scrollbars();
2272 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
2278 update_scrollbars();
2281 int BC_ScrollTextBox::button_press_event()
2283 return text->BC_TextBox::button_press_event();
2285 int BC_ScrollTextBox::button_release_event()
2287 return text->BC_TextBox::button_release_event();
2290 int BC_ScrollTextBox::get_h() { return text->get_h(); }
2291 const char *BC_ScrollTextBox::get_text() { return text->get_text(); }
2292 const wchr_t *BC_ScrollTextBox::get_wtext() { return text->get_wtext(); }
2294 int BC_ScrollTextBox::get_buttonpress()
2296 return text->BC_TextBox::get_buttonpress();
2298 void BC_ScrollTextBox::wset_selection(int char1, int char2, int ibeam)
2300 text->wset_selection(char1, char2, ibeam);
2302 void BC_ScrollTextBox::set_selection(int char1, int char2, int ibeam)
2304 text->set_selection(char1, char2, ibeam);
2306 int BC_ScrollTextBox::get_ibeam_letter()
2308 return text->get_ibeam_letter();
2310 int BC_ScrollTextBox::get_x_pos()
2312 return text->left_margin - text->get_text_x();
2314 void BC_ScrollTextBox::set_x_pos(int x)
2316 text->set_text_x(text->left_margin - x);
2320 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const char *text)
2321 : BC_TextBox(gui->x, gui->y,
2322 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2323 gui->rows, gui->default_size, (char*)text, 1, MEDIUMFONT)
2328 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui, const wchr_t *wtext)
2329 : BC_TextBox(gui->x, gui->y,
2330 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
2331 gui->rows, gui->default_size, (wchr_t*)wtext, 1, MEDIUMFONT)
2336 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
2345 void BC_ScrollTextBox::update_scrollbars()
2347 int view_w = w, view_rows = rows;
2348 int view_h = BC_TextBox::calculate_row_h(view_rows, parent_window);
2349 int text_rows = text->get_text_rows();
2350 int text_width = parent_window->get_text_width(text->font, text->get_wtext());
2351 BC_Resources *resources = parent_window->get_resources();
2352 int need_xscroll = 0, need_yscroll = 0;
2354 // Create scrollbars as needed
2358 if( !need_xscroll && (text->get_text_x() != text->left_margin ||
2359 text_width >= view_w - text->left_margin - text->right_margin) ) {
2360 resize = need_xscroll = 1;
2361 view_h -= resources->hscroll_data[SCROLL_HANDLE_UP]->get_h();
2362 view_rows = BC_TextBox::pixels_to_rows(parent_window, text->font, view_h);
2364 if( !need_yscroll && (text->get_text_y() != text->top_margin ||
2365 text_rows > view_rows) ) {
2366 resize = need_yscroll = 1;
2367 view_w -= resources->vscroll_data[SCROLL_HANDLE_UP]->get_w();
2371 if( !need_xscroll && xscroll ) {
2373 delete xscroll; xscroll = 0;
2375 if( !need_yscroll && yscroll ) {
2377 delete yscroll; yscroll = 0;
2380 if( view_rows != text->get_rows() || view_w != text->get_w() ||
2381 x != text->get_x() || y != text->get_y() ) {
2382 text->reposition_window(x, y, view_w, view_rows);
2384 if( need_xscroll && !xscroll ) {
2385 xscroll = new BC_ScrollTextBoxXScroll(this);
2386 parent_window->add_subwindow(xscroll);
2387 text->xscroll = xscroll;
2388 xscroll->bound_to = text;
2389 xscroll->show_window();
2391 if( need_yscroll && !yscroll ) {
2392 yscroll = new BC_ScrollTextBoxYScroll(this);
2393 parent_window->add_subwindow(yscroll);
2394 text->yscroll = yscroll;
2395 yscroll->bound_to = text;
2396 yscroll->show_window();
2399 xscroll->reposition_window(x, y + text->get_h(), view_w);
2400 int xpos = get_x_pos();
2401 if( xpos != xscroll->get_value() )
2402 xscroll->update_value(xpos);
2403 if( text_width != xscroll->get_length() ||
2404 view_w != xscroll->get_handlelength() )
2405 xscroll->update_length(text_width, xpos, view_w, 0);
2408 yscroll->reposition_window(x + w - yscroll->get_span(), y, text->get_h());
2409 int text_row = text->get_text_row();
2410 if( text_row != yscroll->get_value() )
2411 yscroll->update_value(text_row);
2412 if( text_rows != yscroll->get_length() ||
2413 view_rows != yscroll->get_handlelength() )
2414 yscroll->update_length(text_rows, text_row, view_rows, 0);
2418 int BC_ScrollTextBoxText::handle_event()
2420 gui->update_scrollbars();
2421 return gui->handle_event();
2424 int BC_ScrollTextBoxText::motion_event()
2426 gui->update_scrollbars();
2430 BC_ScrollTextBoxXScroll::BC_ScrollTextBoxXScroll(BC_ScrollTextBox *gui)
2431 : BC_ScrollBar(gui->x, gui->y + gui->text->get_h(), SCROLL_HORIZ + SCROLL_STRETCH,
2432 gui->text->get_w(), gui->text->get_text_width(MEDIUMFONT, gui->get_wtext()),
2438 BC_ScrollTextBoxXScroll::~BC_ScrollTextBoxXScroll()
2442 int BC_ScrollTextBoxXScroll::handle_event()
2444 gui->set_x_pos(get_position());
2448 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
2449 : BC_ScrollBar(gui->x + gui->text->get_w(), gui->y, SCROLL_VERT,
2450 gui->text->get_h(), gui->text->get_text_rows(), 0, gui->rows)
2455 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
2459 int BC_ScrollTextBoxYScroll::handle_event()
2461 gui->text->set_text_row(get_position());
2467 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const char *text)
2468 : BC_TextBox(x, y, popup->text_w, 1, text, BCTEXTLEN)
2470 this->popup = popup;
2473 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y, const wchr_t *wtext)
2474 : BC_TextBox(x, y, popup->text_w, 1, wtext, BCTEXTLEN)
2476 this->popup = popup;
2479 BC_PopupTextBoxText::~BC_PopupTextBoxText()
2489 int BC_PopupTextBoxText::handle_event()
2491 popup->list_item = -1;
2492 popup->handle_event();
2496 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
2498 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
2499 popup->list_h, popup->list_format, popup->list_items, 0, 0, 1, 0, 1)
2501 this->popup = popup;
2503 int BC_PopupTextBoxList::handle_event()
2505 int k = get_selection_number(0, 0);
2506 popup->list_item = k;
2507 if( k >= 0 && k < popup->list_items->size() ) {
2508 popup->textbox->update(popup->list_items->get(k)->get_text());
2509 popup->textbox->set_text_row(0);
2510 popup->handle_event();
2518 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
2519 ArrayList<BC_ListBoxItem*> *list_items,
2520 const char *default_text, int x, int y,
2521 int text_w, int list_h, int list_format)
2525 this->list_h = list_h;
2526 this->list_format = list_format;
2527 this->default_text = (char*)default_text;
2528 this->default_wtext = 0;
2529 this->text_w = text_w;
2530 this->parent_window = parent_window;
2531 this->list_items = list_items;
2534 BC_PopupTextBox::~BC_PopupTextBox()
2544 int BC_PopupTextBox::create_objects()
2546 int x = this->x, y = this->y;
2547 parent_window->add_subwindow(textbox = default_wtext ?
2548 new BC_PopupTextBoxText(this, x, y, default_wtext) :
2549 new BC_PopupTextBoxText(this, x, y, default_text));
2550 x += textbox->get_w();
2551 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
2555 void BC_PopupTextBox::update(const char *text)
2557 textbox->update(text);
2558 textbox->set_text_row(0);
2561 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
2564 listbox->update(data, 0, 0, 1);
2567 int BC_PopupTextBox::handle_event()
2572 const char *BC_PopupTextBox::get_text() { return textbox->get_text(); }
2573 const wchr_t *BC_PopupTextBox::get_wtext() { return textbox->get_wtext(); }
2574 int BC_PopupTextBox::get_number() { return list_item; }
2575 void BC_PopupTextBox::set_number(int v) { list_item = v; }
2576 int BC_PopupTextBox::get_x() { return x; }
2577 int BC_PopupTextBox::get_y() { return y; }
2578 int BC_PopupTextBox::get_w() { return textbox->get_w() + listbox->get_w(); }
2579 int BC_PopupTextBox::get_h() { return textbox->get_h(); }
2580 int BC_PopupTextBox::get_show_query() { return listbox->get_show_query(); }
2581 void BC_PopupTextBox::set_show_query(int v) { listbox->set_show_query(v); }
2582 int BC_PopupTextBox::get_back_color() { return textbox->get_back_color(); }
2583 void BC_PopupTextBox::set_back_color(int v) { textbox->set_back_color(v); }
2585 void BC_PopupTextBox::set_tooltip(const char *text)
2587 listbox->set_tooltip(text);
2590 void BC_PopupTextBox::reposition_window(int x, int y)
2595 textbox->reposition_window(x1,
2598 textbox->get_rows());
2599 x1 += textbox->get_w();
2600 listbox->reposition_window(x1, y1, -1, -1, 0);
2601 // if(flush) parent_window->flush();
2605 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2606 int64_t default_value, int x, int y)
2607 : BC_TextBox(x, y, popup->text_w, 1, default_value)
2609 this->popup = popup;
2612 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
2613 float default_value, int x, int y, int precision)
2614 : BC_TextBox(x, y, popup->text_w, 1, default_value, 1, MEDIUMFONT, precision)
2616 this->popup = popup;
2619 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
2631 int BC_TumbleTextBoxText::handle_event()
2633 popup->handle_event();
2637 int BC_TumbleTextBoxText::button_press_event()
2639 if( get_enabled() && is_event_win() ) {
2640 if( get_buttonpress() < 4 ) return BC_TextBox::button_press_event();
2641 if( get_buttonpress() == 4 ) popup->tumbler->handle_up_event();
2642 else if( get_buttonpress() == 5 ) popup->tumbler->handle_down_event();
2650 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2651 int64_t default_value, int64_t min, int64_t max,
2652 int x, int y, int text_w)
2659 this->default_value = default_value;
2660 this->text_w = text_w;
2661 this->parent_window = parent_window;
2667 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2668 int default_value, int min, int max,
2669 int x, int y, int text_w)
2676 this->default_value = default_value;
2677 this->text_w = text_w;
2678 this->parent_window = parent_window;
2684 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
2685 float default_value_f, float min_f, float max_f,
2686 int x, int y, int text_w, int precision)
2691 this->min_f = min_f;
2692 this->max_f = max_f;
2693 this->default_value_f = default_value_f;
2694 this->text_w = text_w;
2695 this->precision = precision;
2696 this->parent_window = parent_window;
2701 BC_TumbleTextBox::~BC_TumbleTextBox()
2703 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2704 // is deleted anyway by the windowbase so textbox deletes this.
2705 if(tumbler) delete tumbler;
2707 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2716 void BC_TumbleTextBox::reset()
2723 void BC_TumbleTextBox::set_precision(int precision)
2725 this->precision = precision;
2728 void BC_TumbleTextBox::set_increment(float value)
2730 this->increment = value;
2731 if(tumbler) tumbler->set_increment(value);
2734 void BC_TumbleTextBox::set_log_floatincrement(int value)
2736 this->log_floatincrement = value;
2737 if(tumbler) tumbler->set_log_floatincrement(value);
2740 int BC_TumbleTextBox::create_objects()
2742 int x = this->x, y = this->y;
2744 textbox = use_float ?
2745 new BC_TumbleTextBoxText(this, default_value_f, x, y, precision) :
2746 new BC_TumbleTextBoxText(this, default_value, x, y);
2748 parent_window->add_subwindow(textbox);
2749 x += textbox->get_w();
2751 tumbler = use_float ?
2752 (BC_Tumbler *)new BC_FTextTumbler(this, min_f, max_f, x, y) :
2753 (BC_Tumbler *)new BC_ITextTumbler(this, min, max, x, y);
2754 parent_window->add_subwindow(tumbler);
2755 tumbler->set_increment(increment);
2759 int BC_TumbleTextBox::handle_up_event()
2762 ((BC_FTumbler *)tumbler)->BC_FTumbler::handle_up_event() :
2763 ((BC_ITumbler *)tumbler)->BC_ITumbler::handle_up_event() ;
2766 int BC_TumbleTextBox::handle_down_event()
2769 ((BC_FTumbler *)tumbler)->BC_FTumbler::handle_down_event() :
2770 ((BC_ITumbler *)tumbler)->BC_ITumbler::handle_down_event() ;
2773 const char* BC_TumbleTextBox::get_text()
2775 return textbox->get_text();
2778 const wchr_t* BC_TumbleTextBox::get_wtext()
2780 return textbox->get_wtext();
2783 BC_TextBox* BC_TumbleTextBox::get_textbox()
2788 int BC_TumbleTextBox::update(const char *value)
2790 textbox->update(value);
2791 textbox->set_text_row(0);
2795 int BC_TumbleTextBox::update(int64_t value)
2797 textbox->update(value);
2798 textbox->set_text_row(0);
2802 int BC_TumbleTextBox::update(float value)
2804 textbox->update(value);
2805 textbox->set_text_row(0);
2810 int BC_TumbleTextBox::get_x()
2815 int BC_TumbleTextBox::get_y()
2820 int BC_TumbleTextBox::get_w()
2822 return textbox->get_w() + tumbler->get_w();
2825 int BC_TumbleTextBox::get_h()
2827 return textbox->get_h();
2830 void BC_TumbleTextBox::disable(int hide_text)
2832 if( hide_text && !textbox->is_hidden() )
2833 textbox->hide_window(0);
2834 if( !tumbler->is_hidden() )
2835 tumbler->hide_window(0);
2836 if( !get_enabled() ) return;
2837 return textbox->disable();
2840 void BC_TumbleTextBox::enable()
2842 if( textbox->is_hidden() )
2843 textbox->show_window(0);
2844 if( tumbler->is_hidden() )
2845 tumbler->show_window(0);
2846 if( get_enabled() ) return;
2847 return textbox->enable();
2850 int BC_TumbleTextBox::get_enabled()
2852 return textbox->get_enabled();
2855 int BC_TumbleTextBox::handle_event()
2860 void BC_TumbleTextBox::reposition_window(int x, int y)
2865 textbox->reposition_window(x, y, text_w, 1);
2866 tumbler->reposition_window(x + textbox->get_w(), y);
2867 // if(flush) parent_window->flush();
2871 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2873 tumbler->set_boundaries(min, max);
2876 void BC_TumbleTextBox::set_boundaries(float min, float max)
2878 tumbler->set_boundaries(min, max);
2883 BC_TextMenu::BC_TextMenu(BC_TextBox *textbox)
2884 : BC_PopupMenu(0, 0, 0, "", 0)
2886 this->textbox = textbox;
2889 BC_TextMenu::~BC_TextMenu()
2893 void BC_TextMenu::create_objects()
2895 add_item(new BC_TextMenuCut(this));
2896 add_item(new BC_TextMenuCopy(this));
2897 add_item(new BC_TextMenuPaste(this));
2901 BC_TextMenuCut::BC_TextMenuCut(BC_TextMenu *menu)
2902 : BC_MenuItem(_("Cut"))
2907 int BC_TextMenuCut::handle_event()
2909 menu->textbox->cut(1);
2915 BC_TextMenuCopy::BC_TextMenuCopy(BC_TextMenu *menu)
2916 : BC_MenuItem(_("Copy"))
2921 int BC_TextMenuCopy::handle_event()
2923 menu->textbox->copy(1);
2928 BC_TextMenuPaste::BC_TextMenuPaste(BC_TextMenu *menu)
2929 : BC_MenuItem(_("Paste"))
2934 int BC_TextMenuPaste::handle_event()
2936 menu->textbox->paste(1);
2941 void BC_TumbleTextBox::set_tooltip(const char *text)
2943 textbox->set_tooltip(text);