1 #include "bcclipboard.h"
2 #include "bclistboxitem.h"
3 #include "bcresources.h"
16 #define VERTICAL_MARGIN 2
17 #define VERTICAL_MARGIN_NOBORDER 0
18 #define HORIZONTAL_MARGIN 4
19 #define HORIZONTAL_MARGIN_NOBORDER 2
21 BC_TextBox::BC_TextBox(int x,
28 : BC_SubWindow(x, y, w, 0, -1)
31 reset_parameters(rows, has_border, font);
32 strcpy(this->text, text);
35 BC_TextBox::BC_TextBox(int x,
42 : BC_SubWindow(x, y, w, 0, -1)
45 reset_parameters(rows, has_border, font);
46 sprintf(this->text, "%lld", text);
49 BC_TextBox::BC_TextBox(int x,
57 : BC_SubWindow(x, y, w, 0, -1)
60 reset_parameters(rows, has_border, font);
61 this->precision = precision;
62 sprintf(this->text, "%0.*f", precision, text);
65 BC_TextBox::BC_TextBox(int x,
72 : BC_SubWindow(x, y, w, 0, -1)
75 reset_parameters(rows, has_border, font);
76 sprintf(this->text, "%d", text);
79 BC_TextBox::~BC_TextBox()
81 if(skip_cursor) delete skip_cursor;
84 int BC_TextBox::reset_parameters(int rows, int has_border, int font)
87 this->has_border = has_border;
91 highlight_letter1 = highlight_letter2 = 0;
92 highlight_letter3 = highlight_letter4 = 0;
95 text_selected = word_selected = 0;
101 skip_cursor = new Timer;
108 int BC_TextBox::initialize()
111 skip_cursor = new Timer;
112 skip_cursor->update();
114 text_ascent = get_text_ascent(font) + 1;
115 text_descent = get_text_descent(font) + 1;
116 text_height = text_ascent + text_descent;
117 ibeam_letter = strlen(text);
120 left_margin = right_margin = HORIZONTAL_MARGIN;
121 top_margin = bottom_margin = VERTICAL_MARGIN;
125 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
126 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
129 text_x = left_margin;
133 // Create the subwindow
134 BC_SubWindow::initialize();
136 BC_Resources *resources = get_resources();
139 back_color = resources->text_background;
140 high_color = resources->text_background_hi;
144 high_color = resources->text_background_noborder_hi;
145 back_color = bg_color;
149 set_cursor(IBEAM_CURSOR);
153 int BC_TextBox::calculate_h(BC_WindowBase *gui,
158 return rows * (gui->get_text_ascent(font) + 1 +
159 gui->get_text_descent(font) + 1) +
160 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
163 void BC_TextBox::set_precision(int precision)
165 this->precision = precision;
168 int BC_TextBox::update(char *text)
170 //printf("BC_TextBox::update 1 %d %s %s\n", strcmp(text, this->text), text, this->text);
171 int text_len = strlen(text);
172 // Don't update if contents are the same
173 if(!strcmp(text, this->text)) return 0;
176 strcpy(this->text, text);
177 if(highlight_letter1 > text_len) highlight_letter1 = text_len;
178 if(highlight_letter2 > text_len) highlight_letter2 = text_len;
179 if(ibeam_letter > text_len) ibeam_letter = text_len;
184 int BC_TextBox::update(int64_t value)
186 char string[BCTEXTLEN];
187 sprintf(string, "%lld", value);
194 int BC_TextBox::update(float value)
196 char string[BCTEXTLEN];
197 sprintf(string, "%0.*f", precision, value);
203 void BC_TextBox::disable()
210 if(active) top_level->deactivate();
216 void BC_TextBox::enable()
228 int BC_TextBox::get_enabled()
233 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
235 return (pixels - 4) /
236 (window->get_text_ascent(font) + 1 +
237 window->get_text_descent(font) + 1);
240 int BC_TextBox::calculate_row_h(int rows,
241 BC_WindowBase *parent_window,
246 (parent_window->get_text_ascent(font) + 1 +
247 parent_window->get_text_descent(font) + 1) +
248 (has_border ? 4 : 0);
251 char* BC_TextBox::get_text()
256 int BC_TextBox::get_text_rows()
258 int text_len = strlen(text);
260 for(int i = 0; i < text_len; i++)
262 if(text[i] == 0xa) result++;
268 int BC_TextBox::get_row_h(int rows)
270 return rows * text_height + top_margin + bottom_margin;
273 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
276 if(w < 0) w = get_w();
279 new_h = get_row_h(rows);
288 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
289 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
290 BC_WindowBase::reposition_window(x, y, w, new_h);
296 void BC_TextBox::draw_border()
298 BC_Resources *resources = get_resources();
300 set_color(background_color);
301 draw_box(0, 0, left_margin, get_h());
302 draw_box(get_w() - right_margin, 0, right_margin, get_h());
307 draw_3d_border(0, 0, w, h,
308 resources->button_shadow,
309 resources->button_uphighlighted,
310 resources->button_highlighted,
311 resources->button_light);
313 draw_3d_border(0, 0, w, h,
314 resources->text_border1,
315 resources->text_border2,
316 resources->text_border3,
317 resources->text_border4);
321 void BC_TextBox::draw_cursor()
323 // set_color(background_color);
327 draw_box(ibeam_x + text_x,
335 void BC_TextBox::draw()
337 int i, j, k, text_len;
338 int row_begin, row_end;
339 int highlight_x1, highlight_x2;
341 BC_Resources *resources = get_resources();
343 //printf("BC_TextBox::draw 1 %s\n", text);
347 background_color = resources->text_background;
353 background_color = high_color;
357 background_color = back_color;
361 set_color(background_color);
362 draw_box(0, 0, w, h);
364 // Draw text with selection
366 text_len = strlen(text);
367 //printf("BC_TextBox::draw 0 %s %d %d %d %d\n", text, text_y, text_len, get_w(), text_height);
369 for(i = 0, k = text_y; i < text_len && k < get_h(); k += text_height)
372 if(text[i] == '\n') i++;
374 for(j = 0; text[i] != '\n' && i < text_len; j++, i++)
376 text_row[j] = text[i];
381 //printf("BC_TextBox::draw 1 %d %d %c\n", row_begin, row_end, text_row[j - 1]);
383 if(k > -text_height + top_margin && k < get_h() - bottom_margin)
385 // Draw highlighted region of row
386 if(highlight_letter2 > highlight_letter1 &&
387 highlight_letter2 > row_begin && highlight_letter1 < row_end)
389 if(active && enabled && get_has_focus())
390 set_color(resources->text_highlight);
392 set_color(resources->text_inactive_highlight);
394 if(highlight_letter1 >= row_begin && highlight_letter1 < row_end)
395 highlight_x1 = get_text_width(font, text_row, highlight_letter1 - row_begin);
399 if(highlight_letter2 > row_begin && highlight_letter2 <= row_end)
400 highlight_x2 = get_text_width(font, text_row, highlight_letter2 - row_begin);
402 highlight_x2 = get_w();
404 draw_box(highlight_x1 + text_x,
406 highlight_x2 - highlight_x1,
410 // Draw text over highlight
412 set_color(resources->text_default);
416 draw_text(text_x, k + text_ascent, text_row);
418 // Get ibeam location
419 if(ibeam_letter >= row_begin && ibeam_letter <= row_end)
422 ibeam_y = k - text_y;
423 ibeam_x = get_text_width(font, text_row, ibeam_letter - row_begin);
428 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
435 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
446 int BC_TextBox::focus_in_event()
452 int BC_TextBox::focus_out_event()
458 int BC_TextBox::cursor_enter_event()
460 if(top_level->event_win == win && enabled)
475 int BC_TextBox::cursor_leave_event()
488 int BC_TextBox::button_press_event()
490 if(get_buttonpress() > 2) return 0;
492 int cursor_letter = 0;
493 int text_len = strlen(text);
495 if(!enabled) return 0;
497 if(top_level->event_win == win)
502 top_level->deactivate();
506 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
507 if(get_double_click())
510 select_word(highlight_letter1, highlight_letter2, cursor_letter);
511 highlight_letter3 = highlight_letter1;
512 highlight_letter4 = highlight_letter2;
513 ibeam_letter = highlight_letter2;
514 copy_selection(PRIMARY_SELECTION);
517 if(get_buttonpress() == 2)
519 highlight_letter3 = highlight_letter4 =
520 ibeam_letter = highlight_letter1 =
521 highlight_letter2 = cursor_letter;
522 paste_selection(PRIMARY_SELECTION);
527 highlight_letter3 = highlight_letter4 =
528 ibeam_letter = highlight_letter1 =
529 highlight_letter2 = cursor_letter;
532 if(ibeam_letter < 0) ibeam_letter = 0;
533 if(ibeam_letter > text_len) ibeam_letter = text_len;
540 top_level->deactivate();
546 int BC_TextBox::button_release_event()
551 if(text_selected || word_selected)
560 int BC_TextBox::cursor_motion_event()
562 int cursor_letter, text_len = strlen(text), letter1, letter2;
565 if(text_selected || word_selected)
567 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
570 select_word(letter1, letter2, cursor_letter);
575 letter1 = letter2 = cursor_letter;
578 if(letter1 <= highlight_letter3)
580 highlight_letter1 = letter1;
581 highlight_letter2 = highlight_letter4;
582 ibeam_letter = letter1;
585 if(letter2 >= highlight_letter4)
587 highlight_letter2 = letter2;
588 highlight_letter1 = highlight_letter3;
589 ibeam_letter = letter2;
592 copy_selection(PRIMARY_SELECTION);
601 int BC_TextBox::activate()
603 top_level->active_subwindow = this;
606 top_level->set_repeat(top_level->get_resources()->blink_rate);
610 int BC_TextBox::deactivate()
613 top_level->unset_repeat(top_level->get_resources()->blink_rate);
618 int BC_TextBox::repeat_event(int64_t duration)
622 if(duration == top_level->get_resources()->tooltip_delay &&
623 tooltip_text[0] != 0 &&
631 if(duration == top_level->get_resources()->blink_rate &&
635 if(skip_cursor->get_difference() < duration)
637 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
638 // skip_cursor->get_difference(),
650 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
652 if((top_level->get_keypress() == RETURN) ||
653 // (top_level->get_keypress() > 30 && top_level->get_keypress() < 127))
654 (top_level->get_keypress() > 30 && top_level->get_keypress() <= 255))
656 // Substitute UNIX linefeed
657 if(top_level->get_keypress() == RETURN)
658 temp_string[0] = 0xa;
660 temp_string[0] = top_level->get_keypress();
662 insert_text(temp_string);
670 int BC_TextBox::select_whole_text(int select)
674 highlight_letter1 = 0;
675 highlight_letter2 = strlen(text);
676 text_selected = word_selected = 0;
677 ibeam_letter = highlight_letter1;
679 if(keypress_draw) draw();
683 ibeam_letter = strlen(text);
684 highlight_letter1 = ibeam_letter;
685 highlight_letter2 = ibeam_letter;
686 text_selected = word_selected = 0;
688 if(keypress_draw) draw();
690 return highlight_letter2 - highlight_letter1;
693 void BC_TextBox::cycle_textboxes(int amout)
695 top_level->cycle_textboxes(amout);
698 int BC_TextBox::keypress_event()
700 // Result == 2 contents changed
701 // Result == 1 trapped keypress
702 // Result == 0 nothing
705 int dispatch_event = 0;
707 if(!active || !enabled) return 0;
709 text_len = strlen(text);
710 last_keypress = get_keypress();
711 switch(get_keypress())
714 top_level->deactivate();
721 top_level->deactivate();
727 default_keypress(dispatch_event, result);
730 // Handle like a default keypress
745 int old_ibeam_letter = ibeam_letter;
755 while(ibeam_letter > 0 && isalnum(text[ibeam_letter - 1]))
761 if(top_level->shift_down())
763 // Initialize highlighting
764 if(highlight_letter1 == highlight_letter2)
766 highlight_letter1 = ibeam_letter;
767 highlight_letter2 = old_ibeam_letter;
770 // Extend left highlight
771 if(highlight_letter1 == old_ibeam_letter)
773 highlight_letter1 = ibeam_letter;
776 // Shrink right highlight
777 if(highlight_letter2 == old_ibeam_letter)
779 highlight_letter2 = ibeam_letter;
784 highlight_letter1 = highlight_letter2 = ibeam_letter;
789 if(keypress_draw) draw();
795 if(ibeam_letter < text_len)
797 int old_ibeam_letter = ibeam_letter;
806 while(ibeam_letter < text_len && isalnum(text[ibeam_letter++]))
813 if(top_level->shift_down())
815 // Initialize highlighting
816 if(highlight_letter1 == highlight_letter2)
818 highlight_letter1 = old_ibeam_letter;
819 highlight_letter2 = ibeam_letter;
822 // Shrink left highlight
823 if(highlight_letter1 == old_ibeam_letter)
825 highlight_letter1 = ibeam_letter;
828 // Expand right highlight
829 if(highlight_letter2 == old_ibeam_letter)
831 highlight_letter2 = ibeam_letter;
836 highlight_letter1 = highlight_letter2 = ibeam_letter;
840 if(keypress_draw) draw();
848 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
849 int new_letter = get_cursor_letter(ibeam_x + text_x,
850 ibeam_y + text_y - text_height);
851 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
854 if(top_level->shift_down())
856 // Initialize highlighting
857 if(highlight_letter1 == highlight_letter2)
859 highlight_letter1 = new_letter;
860 highlight_letter2 = ibeam_letter;
863 // Expand left highlight
864 if(highlight_letter1 == ibeam_letter)
866 highlight_letter1 = new_letter;
869 // Shrink right highlight
870 if(highlight_letter2 == ibeam_letter)
872 highlight_letter2 = new_letter;
876 highlight_letter1 = highlight_letter2 = new_letter;
878 if(highlight_letter1 > highlight_letter2)
880 int temp = highlight_letter1;
881 highlight_letter1 = highlight_letter2;
882 highlight_letter2 = temp;
884 ibeam_letter = new_letter;
887 if(keypress_draw) draw();
895 int new_letter = get_cursor_letter(ibeam_x + text_x,
896 ibeam_y + text_y - get_h());
899 if(top_level->shift_down())
901 // Initialize highlighting
902 if(highlight_letter1 == highlight_letter2)
904 highlight_letter1 = new_letter;
905 highlight_letter2 = ibeam_letter;
908 // Expand left highlight
909 if(highlight_letter1 == ibeam_letter)
911 highlight_letter1 = new_letter;
914 // Shrink right highlight
915 if(highlight_letter2 == ibeam_letter)
917 highlight_letter2 = new_letter;
921 highlight_letter1 = highlight_letter2 = new_letter;
923 if(highlight_letter1 > highlight_letter2)
925 int temp = highlight_letter1;
926 highlight_letter1 = highlight_letter2;
927 highlight_letter2 = temp;
929 ibeam_letter = new_letter;
932 if(keypress_draw) draw();
938 // if(ibeam_letter > 0)
941 int new_letter = get_cursor_letter(ibeam_x + text_x,
942 ibeam_y + text_y + text_height);
943 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
945 if(top_level->shift_down())
947 // Initialize highlighting
948 if(highlight_letter1 == highlight_letter2)
950 highlight_letter1 = new_letter;
951 highlight_letter2 = ibeam_letter;
954 // Shrink left highlight
955 if(highlight_letter1 == ibeam_letter)
957 highlight_letter1 = new_letter;
960 // Expand right highlight
961 if(highlight_letter2 == ibeam_letter)
963 highlight_letter2 = new_letter;
967 highlight_letter1 = highlight_letter2 = new_letter;
969 if(highlight_letter1 > highlight_letter2)
971 int temp = highlight_letter1;
972 highlight_letter1 = highlight_letter2;
973 highlight_letter2 = temp;
975 ibeam_letter = new_letter;
978 if(keypress_draw) draw();
980 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
988 int new_letter = get_cursor_letter(ibeam_x + text_x,
989 ibeam_y + text_y + get_h());
990 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
992 if(top_level->shift_down())
994 // Initialize highlighting
995 if(highlight_letter1 == highlight_letter2)
997 highlight_letter1 = new_letter;
998 highlight_letter2 = ibeam_letter;
1001 // Shrink left highlight
1002 if(highlight_letter1 == ibeam_letter)
1004 highlight_letter1 = new_letter;
1007 // Expand right highlight
1008 if(highlight_letter2 == ibeam_letter)
1010 highlight_letter2 = new_letter;
1014 highlight_letter1 = highlight_letter2 = new_letter;
1016 if(highlight_letter1 > highlight_letter2)
1018 int temp = highlight_letter1;
1019 highlight_letter1 = highlight_letter2;
1020 highlight_letter2 = temp;
1022 ibeam_letter = new_letter;
1025 if(keypress_draw) draw();
1027 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1034 int old_ibeam_letter = ibeam_letter;
1036 while(ibeam_letter < text_len && text[ibeam_letter] != '\n')
1039 if(top_level->shift_down())
1042 if(highlight_letter1 == highlight_letter2)
1044 highlight_letter2 = ibeam_letter;
1045 highlight_letter1 = old_ibeam_letter;
1049 if(highlight_letter1 == old_ibeam_letter)
1051 highlight_letter1 = highlight_letter2;
1052 highlight_letter2 = ibeam_letter;
1056 if(highlight_letter2 == old_ibeam_letter)
1058 highlight_letter2 = ibeam_letter;
1062 highlight_letter1 = highlight_letter2 = ibeam_letter;
1065 if(keypress_draw) draw();
1072 int old_ibeam_letter = ibeam_letter;
1074 while(ibeam_letter > 0 && text[ibeam_letter - 1] != '\n')
1077 if(top_level->shift_down())
1080 if(highlight_letter1 == highlight_letter2)
1082 highlight_letter2 = old_ibeam_letter;
1083 highlight_letter1 = ibeam_letter;
1087 if(highlight_letter1 == old_ibeam_letter)
1089 highlight_letter1 = ibeam_letter;
1093 if(highlight_letter2 == old_ibeam_letter)
1095 highlight_letter2 = highlight_letter1;
1096 highlight_letter1 = ibeam_letter;
1100 highlight_letter1 = highlight_letter2 = ibeam_letter;
1103 if(keypress_draw) draw();
1109 if(highlight_letter1 == highlight_letter2)
1111 if(ibeam_letter > 0)
1113 delete_selection(ibeam_letter - 1, ibeam_letter, text_len);
1119 delete_selection(highlight_letter1, highlight_letter2, text_len);
1120 highlight_letter2 = ibeam_letter = highlight_letter1;
1124 if(keypress_draw) draw();
1130 if(highlight_letter1 == highlight_letter2)
1132 if(ibeam_letter < text_len)
1134 delete_selection(ibeam_letter, ibeam_letter + 1, text_len);
1139 delete_selection(highlight_letter1, highlight_letter2, text_len);
1140 highlight_letter2 = ibeam_letter = highlight_letter1;
1144 if(keypress_draw) draw();
1154 if(get_keypress() == 'c' || get_keypress() == 'C')
1156 if(highlight_letter1 != highlight_letter2)
1158 copy_selection(SECONDARY_SELECTION);
1163 if(get_keypress() == 'v' || get_keypress() == 'V')
1165 paste_selection(SECONDARY_SELECTION);
1167 if(keypress_draw) draw();
1172 if(get_keypress() == 'x' || get_keypress() == 'X')
1174 if(highlight_letter1 != highlight_letter2)
1176 copy_selection(SECONDARY_SELECTION);
1177 delete_selection(highlight_letter1, highlight_letter2, text_len);
1178 highlight_letter2 = ibeam_letter = highlight_letter1;
1182 if(keypress_draw) draw();
1190 default_keypress(dispatch_event, result);
1194 if(dispatch_event) skip_cursor->update();
1195 if(dispatch_event) handle_event();
1196 //printf("BC_TextBox::keypress_event 100 %d\n", ibeam_letter);
1202 int BC_TextBox::uses_text()
1207 void BC_TextBox::delete_selection(int letter1, int letter2, int text_len)
1211 for(i = letter1, j = letter2; j < text_len; i++, j++)
1220 void BC_TextBox::insert_text(char *string)
1222 int i, j, text_len, string_len;
1224 string_len = strlen(string);
1225 text_len = strlen(text);
1226 if(highlight_letter1 < highlight_letter2)
1228 delete_selection(highlight_letter1, highlight_letter2, text_len);
1229 highlight_letter2 = ibeam_letter = highlight_letter1;
1232 text_len = strlen(text);
1234 for(i = text_len, j = text_len + string_len; i >= ibeam_letter; i--, j--)
1237 for(i = ibeam_letter, j = 0; j < string_len; j++, i++)
1238 text[i] = string[j];
1240 ibeam_letter += string_len;
1245 void BC_TextBox::do_separators(int ibeam_left)
1249 // Remove separators from text
1250 int text_len = strlen(text);
1251 int separator_len = strlen(separators);
1252 for(int i = 0; i < text_len; i++)
1254 if(!isalnum(text[i]))
1256 for(int j = i; j < text_len - 1; j++)
1257 text[j] = text[j + 1];
1258 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1270 // Insert separators into text
1271 for(int i = 0; i < separator_len; i++)
1275 // Insert a separator
1276 if(!isalnum(separators[i]))
1278 for(int j = text_len; j >= i; j--)
1280 text[j + 1] = text[j];
1282 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1284 text[i] = separators[i];
1290 text[i] = separators[i];
1295 text[separator_len] = 0;
1300 void BC_TextBox::get_ibeam_position(int &x, int &y)
1302 int i, j, k, row_begin, row_end, text_len;
1304 text_len = strlen(text);
1307 for(i = 0; i < text_len; )
1310 for(j = 0; text[i] != '\n' && i < text_len; j++, i++)
1312 text_row[j] = text[i];
1318 if(ibeam_letter >= row_begin && ibeam_letter <= row_end)
1320 x = get_text_width(font, text_row, ibeam_letter - row_begin);
1321 //printf("BC_TextBox::get_ibeam_position 9 %d %d\n", x, y);
1331 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1337 void BC_TextBox::set_text_row(int row)
1339 text_y = -(row * text_height) + top_margin;
1343 int BC_TextBox::get_text_row()
1345 return -(text_y - top_margin) / text_height;
1348 void BC_TextBox::find_ibeam(int dispatch_event)
1351 int old_x = text_x, old_y = text_y;
1353 get_ibeam_position(x, y);
1355 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1357 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1358 if(text_x > left_margin) text_x = left_margin;
1361 if(left_margin + text_x + x < left_margin)
1363 text_x = -(x - (get_w() / 4)) + left_margin;
1364 if(text_x > left_margin) text_x = left_margin;
1367 while(y + text_y >= get_h() - text_height - bottom_margin)
1369 text_y -= text_height;
1370 // text_y = -(y - (get_h() / 2)) + top_margin;
1371 // if(text_y > top_margin) text_y = top_margin;
1374 while(y + text_y < top_margin)
1376 text_y += text_height;
1377 if(text_y > top_margin)
1379 text_y = top_margin;
1384 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1387 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1389 int i, j, k, l, row_begin, row_end, text_len, result = 0, done = 0;
1390 text_len = strlen(text);
1392 if(cursor_y < text_y)
1398 for(i = 0, k = text_y; i < text_len && !done; k += text_height)
1401 for(j = 0; text[i] != '\n' && i < text_len; j++, i++)
1403 text_row[j] = text[i];
1408 if(cursor_y >= k && cursor_y < k + text_height)
1410 for(j = 0; j <= row_end - row_begin && !done; j++)
1412 l = get_text_width(font, text_row, j) + text_x;
1415 result = row_begin + j - 1;
1425 if(text[i] == '\n') i++;
1427 if(i >= text_len && !done)
1432 if(result < 0) result = 0;
1433 if(result > text_len) result = text_len;
1437 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
1439 int text_len = strlen(text);
1440 letter1 = letter2 = ibeam_letter;
1443 if(isalnum(text[letter1])) letter1--;
1444 }while(letter1 > 0 && isalnum(text[letter1]));
1445 if(!isalnum(text[letter1])) letter1++;
1449 if(isalnum(text[letter2])) letter2++;
1450 }while(letter2 < text_len && isalnum(text[letter2]));
1451 if(letter2 < text_len && text[letter2] == ' ') letter2++;
1453 if(letter1 < 0) letter1 = 0;
1454 if(letter2 < 0) letter2 = 0;
1455 if(letter1 > text_len) letter1 = text_len;
1456 if(letter2 > text_len) letter2 = text_len;
1459 void BC_TextBox::copy_selection(int clipboard_num)
1461 int text_len = strlen(text);
1463 if(highlight_letter1 >= text_len ||
1464 highlight_letter2 > text_len ||
1465 highlight_letter1 < 0 ||
1466 highlight_letter2 < 0 ||
1467 highlight_letter2 - highlight_letter1 <= 0) return;
1469 get_clipboard()->to_clipboard(&text[highlight_letter1],
1470 highlight_letter2 - highlight_letter1,
1474 void BC_TextBox::paste_selection(int clipboard_num)
1476 int len = get_clipboard()->clipboard_len(clipboard_num);
1479 char *string = new char[len + 1];
1480 get_clipboard()->from_clipboard(string, len, clipboard_num);
1481 insert_text(string);
1485 void BC_TextBox::set_keypress_draw(int value)
1487 keypress_draw = value;
1490 int BC_TextBox::get_last_keypress()
1492 return last_keypress;
1495 int BC_TextBox::get_ibeam_letter()
1497 return ibeam_letter;
1500 void BC_TextBox::set_ibeam_letter(int number, int redraw)
1502 this->ibeam_letter = number;
1509 void BC_TextBox::set_separators(char *separators)
1511 this->separators = separators;
1529 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
1536 this->parent_window = parent_window;
1541 this->default_text = default_text;
1544 BC_ScrollTextBox::~BC_ScrollTextBox()
1554 void BC_ScrollTextBox::create_objects()
1556 // Must be created first
1557 parent_window->add_subwindow(text = new BC_ScrollTextBoxText(this));
1558 parent_window->add_subwindow(yscroll = new BC_ScrollTextBoxYScroll(this));
1561 int BC_ScrollTextBox::handle_event()
1566 int BC_ScrollTextBox::get_x()
1571 int BC_ScrollTextBox::get_y()
1576 int BC_ScrollTextBox::get_w()
1581 int BC_ScrollTextBox::get_rows()
1587 char* BC_ScrollTextBox::get_text()
1589 return text->get_text();
1592 void BC_ScrollTextBox::update(char *text)
1594 this->text->update(text);
1595 yscroll->update_length(this->text->get_text_rows(),
1596 this->text->get_text_row(),
1597 yscroll->get_handlelength());
1600 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
1606 text->reposition_window(x,
1608 w - yscroll->get_span(),
1610 yscroll->reposition_window(x + w - yscroll->get_span(),
1612 BC_TextBox::calculate_row_h(rows,
1614 yscroll->update_length(text->get_text_rows(),
1615 text->get_text_row(),
1627 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui)
1628 : BC_TextBox(gui->x,
1630 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
1637 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
1646 int BC_ScrollTextBoxText::handle_event()
1648 gui->yscroll->update_length(get_text_rows(),
1650 gui->yscroll->get_handlelength());
1651 return gui->handle_event();
1654 int BC_ScrollTextBoxText::motion_event()
1656 gui->yscroll->update_length(get_text_rows(),
1658 gui->yscroll->get_handlelength());
1663 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
1664 : BC_ScrollBar(gui->x +
1666 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
1669 BC_TextBox::calculate_row_h(gui->rows,
1670 gui->parent_window),
1671 gui->text->get_text_rows(),
1678 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
1682 int BC_ScrollTextBoxYScroll::handle_event()
1684 gui->text->set_text_row(get_position());
1697 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y)
1698 : BC_TextBox(x, y, popup->text_w, 1, popup->default_text)
1700 this->popup = popup;
1703 BC_PopupTextBoxText::~BC_PopupTextBoxText()
1714 int BC_PopupTextBoxText::handle_event()
1716 popup->handle_event();
1720 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
1723 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
1733 this->popup = popup;
1735 int BC_PopupTextBoxList::handle_event()
1737 popup->textbox->update(get_selection(0, 0)->get_text());
1738 popup->handle_event();
1745 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
1746 ArrayList<BC_ListBoxItem*> *list_items,
1755 this->list_h = list_h;
1756 this->default_text = default_text;
1757 this->text_w = text_w;
1758 this->parent_window = parent_window;
1759 this->list_items = list_items;
1762 BC_PopupTextBox::~BC_PopupTextBox()
1772 int BC_PopupTextBox::create_objects()
1774 int x = this->x, y = this->y;
1775 parent_window->add_subwindow(textbox = new BC_PopupTextBoxText(this, x, y));
1776 x += textbox->get_w();
1777 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
1781 void BC_PopupTextBox::update(char *text)
1783 textbox->update(text);
1786 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
1788 listbox->update(data,
1795 char* BC_PopupTextBox::get_text()
1797 return textbox->get_text();
1800 int BC_PopupTextBox::get_number()
1802 return listbox->get_selection_number(0, 0);
1805 int BC_PopupTextBox::get_x()
1810 int BC_PopupTextBox::get_y()
1815 int BC_PopupTextBox::get_w()
1817 return textbox->get_w() + listbox->get_w();
1820 int BC_PopupTextBox::get_h()
1822 return textbox->get_h();
1825 int BC_PopupTextBox::handle_event()
1830 void BC_PopupTextBox::reposition_window(int x, int y)
1835 textbox->reposition_window(x1, y1);
1836 x1 += textbox->get_w();
1837 listbox->reposition_window(x1, y1);
1853 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
1854 int64_t default_value,
1865 this->popup = popup;
1868 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
1869 float default_value,
1880 this->popup = popup;
1883 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
1895 int BC_TumbleTextBoxText::handle_event()
1897 popup->handle_event();
1901 int BC_TumbleTextBoxText::button_press_event()
1905 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1906 if(get_buttonpress() == 4)
1908 popup->tumbler->handle_up_event();
1911 if(get_buttonpress() == 5)
1913 popup->tumbler->handle_down_event();
1923 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
1924 int64_t default_value,
1936 this->default_value = default_value;
1937 this->text_w = text_w;
1938 this->parent_window = parent_window;
1944 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
1957 this->default_value = default_value;
1958 this->text_w = text_w;
1959 this->parent_window = parent_window;
1965 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
1966 float default_value_f,
1976 this->min_f = min_f;
1977 this->max_f = max_f;
1978 this->default_value_f = default_value_f;
1979 this->text_w = text_w;
1980 this->parent_window = parent_window;
1986 BC_TumbleTextBox::~BC_TumbleTextBox()
1988 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
1989 // is deleted anyway by the windowbase so textbox deletes this.
1990 if(tumbler) delete tumbler;
1992 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2001 void BC_TumbleTextBox::reset()
2008 void BC_TumbleTextBox::set_precision(int precision)
2010 this->precision = precision;
2013 void BC_TumbleTextBox::set_increment(float value)
2015 this->increment = value;
2016 if(tumbler) tumbler->set_increment(value);
2019 int BC_TumbleTextBox::create_objects()
2021 int x = this->x, y = this->y;
2025 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2031 textbox->set_precision(precision);
2034 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2041 x += textbox->get_w();
2044 parent_window->add_subwindow(tumbler = new BC_FTumbler(textbox,
2050 parent_window->add_subwindow(tumbler = new BC_ITumbler(textbox,
2056 tumbler->set_increment(increment);
2060 char* BC_TumbleTextBox::get_text()
2062 return textbox->get_text();
2065 int BC_TumbleTextBox::update(char *value)
2067 textbox->update(value);
2071 int BC_TumbleTextBox::update(int64_t value)
2073 textbox->update(value);
2077 int BC_TumbleTextBox::update(float value)
2079 textbox->update(value);
2084 int BC_TumbleTextBox::get_x()
2089 int BC_TumbleTextBox::get_y()
2094 int BC_TumbleTextBox::get_w()
2096 return textbox->get_w() + tumbler->get_w();
2099 int BC_TumbleTextBox::get_h()
2101 return textbox->get_h();
2104 int BC_TumbleTextBox::handle_event()
2109 int BC_TumbleTextBox::reposition_widget(int x, int y, int w, int h) {
2110 reposition_window(x, y);
2114 void BC_TumbleTextBox::reposition_window(int x, int y)
2119 textbox->reposition_window(x,
2123 tumbler->reposition_window(x + textbox->get_w(),
2128 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2130 tumbler->set_boundaries(min, max);
2133 void BC_TumbleTextBox::set_boundaries(float min, float max)
2135 tumbler->set_boundaries(min, max);