1 #include "bcclipboard.h"
2 #include "bclistboxitem.h"
3 #include "bcresources.h"
17 #define VERTICAL_MARGIN 2
18 #define VERTICAL_MARGIN_NOBORDER 0
19 #define HORIZONTAL_MARGIN 4
20 #define HORIZONTAL_MARGIN_NOBORDER 2
22 BC_TextBox::BC_TextBox(int x,
29 : BC_SubWindow(x, y, w, 0, -1)
32 reset_parameters(rows, has_border, font);
33 strcpy(this->text, text);
36 BC_TextBox::BC_TextBox(int x,
43 : BC_SubWindow(x, y, w, 0, -1)
46 reset_parameters(rows, has_border, font);
47 sprintf(this->text, "%lld", text);
50 BC_TextBox::BC_TextBox(int x,
58 : BC_SubWindow(x, y, w, 0, -1)
61 this->precision = precision;
62 reset_parameters(rows, has_border, font);
63 sprintf(this->text, "%0.*f", precision, text);
66 BC_TextBox::BC_TextBox(int x,
73 : BC_SubWindow(x, y, w, 0, -1)
76 reset_parameters(rows, has_border, font);
77 sprintf(this->text, "%d", text);
80 BC_TextBox::~BC_TextBox()
82 if(skip_cursor) delete skip_cursor;
85 int BC_TextBox::reset_parameters(int rows, int has_border, int font)
88 this->has_border = has_border;
92 highlight_letter1 = highlight_letter2 = 0;
93 highlight_letter3 = highlight_letter4 = 0;
96 text_selected = word_selected = 0;
102 skip_cursor = new Timer;
109 int BC_TextBox::initialize()
112 skip_cursor = new Timer;
113 skip_cursor->update();
115 text_ascent = get_text_ascent(font) + 1;
116 text_descent = get_text_descent(font) + 1;
117 text_height = text_ascent + text_descent;
118 ibeam_letter = strlen(text);
121 left_margin = right_margin = HORIZONTAL_MARGIN;
122 top_margin = bottom_margin = VERTICAL_MARGIN;
126 left_margin = right_margin = HORIZONTAL_MARGIN_NOBORDER;
127 top_margin = bottom_margin = VERTICAL_MARGIN_NOBORDER;
130 text_x = left_margin;
134 // Create the subwindow
135 BC_SubWindow::initialize();
137 BC_Resources *resources = get_resources();
140 back_color = resources->text_background;
141 high_color = resources->text_background_hi;
145 high_color = resources->text_background_noborder_hi;
146 back_color = bg_color;
150 set_cursor(IBEAM_CURSOR);
154 int BC_TextBox::calculate_h(BC_WindowBase *gui,
159 return rows * (gui->get_text_ascent(font) + 1 +
160 gui->get_text_descent(font) + 1) +
161 2 * (has_border ? VERTICAL_MARGIN : VERTICAL_MARGIN_NOBORDER);
164 void BC_TextBox::set_precision(int precision)
166 this->precision = precision;
169 void BC_TextBox::set_selection(int char1, int char2, int ibeam)
171 highlight_letter1 = char1;
172 highlight_letter2 = char2;
173 ibeam_letter = ibeam;
177 int BC_TextBox::update(char *text)
179 //printf("BC_TextBox::update 1 %d %s %s\n", strcmp(text, this->text), text, this->text);
180 int text_len = strlen(text);
181 // Don't update if contents are the same
182 if(!strcmp(text, this->text)) return 0;
185 strcpy(this->text, text);
186 if(highlight_letter1 > text_len) highlight_letter1 = text_len;
187 if(highlight_letter2 > text_len) highlight_letter2 = text_len;
188 if(ibeam_letter > text_len) ibeam_letter = text_len;
193 int BC_TextBox::update(int64_t value)
195 char string[BCTEXTLEN];
196 sprintf(string, "%lld", value);
203 int BC_TextBox::update(float value)
205 char string[BCTEXTLEN];
206 sprintf(string, "%0.*f", precision, value);
212 void BC_TextBox::disable()
219 if(active) top_level->deactivate();
225 void BC_TextBox::enable()
237 int BC_TextBox::get_enabled()
242 int BC_TextBox::pixels_to_rows(BC_WindowBase *window, int font, int pixels)
244 return (pixels - 4) /
245 (window->get_text_ascent(font) + 1 +
246 window->get_text_descent(font) + 1);
249 int BC_TextBox::calculate_row_h(int rows,
250 BC_WindowBase *parent_window,
255 (parent_window->get_text_ascent(font) + 1 +
256 parent_window->get_text_descent(font) + 1) +
257 (has_border ? 4 : 0);
260 char* BC_TextBox::get_text()
265 int BC_TextBox::get_text_rows()
267 int text_len = strlen(text);
269 for(int i = 0; i < text_len; i++)
271 if(text[i] == 0xa) result++;
277 int BC_TextBox::get_row_h(int rows)
279 return rows * text_height + top_margin + bottom_margin;
282 int BC_TextBox::reposition_window(int x, int y, int w, int rows)
285 if(w < 0) w = get_w();
288 new_h = get_row_h(rows);
297 // printf("BC_TextBox::reposition_window 1 %d %d %d %d %d %d %d %d\n",
298 // x, get_x(), y, get_y(), w, get_w(), new_h, get_h());
299 BC_WindowBase::reposition_window(x, y, w, new_h);
305 void BC_TextBox::draw_border()
307 BC_Resources *resources = get_resources();
309 set_color(background_color);
310 draw_box(0, 0, left_margin, get_h());
311 draw_box(get_w() - right_margin, 0, right_margin, get_h());
316 draw_3d_border(0, 0, w, h,
317 resources->button_shadow,
318 resources->button_uphighlighted,
319 resources->button_highlighted,
320 resources->button_light);
322 draw_3d_border(0, 0, w, h,
323 resources->text_border1,
324 resources->text_border2,
325 resources->text_border3,
326 resources->text_border4);
330 void BC_TextBox::draw_cursor()
332 // set_color(background_color);
336 draw_box(ibeam_x + text_x,
344 void BC_TextBox::draw()
346 int i, j, k, text_len;
347 int row_begin, row_end;
348 int highlight_x1, highlight_x2;
350 BC_Resources *resources = get_resources();
352 //printf("BC_TextBox::draw 1 %s\n", text);
356 background_color = resources->text_background;
362 background_color = high_color;
366 background_color = back_color;
370 set_color(background_color);
371 draw_box(0, 0, w, h);
373 // Draw text with selection
375 text_len = strlen(text);
376 //printf("BC_TextBox::draw 0 %s %d %d %d %d\n", text, text_y, text_len, get_w(), text_height);
378 for(i = 0, k = text_y; i < text_len && k < get_h(); k += text_height)
381 if(text[i] == '\n') i++;
383 for(j = 0; text[i] != '\n' && i < text_len; j++, i++)
385 text_row[j] = text[i];
390 //printf("BC_TextBox::draw 1 %d %d %c\n", row_begin, row_end, text_row[j - 1]);
392 if(k > -text_height + top_margin && k < get_h() - bottom_margin)
394 // Draw highlighted region of row
395 if(highlight_letter2 > highlight_letter1 &&
396 highlight_letter2 > row_begin && highlight_letter1 < row_end)
398 if(active && enabled && get_has_focus())
399 set_color(resources->text_highlight);
401 set_color(resources->text_inactive_highlight);
403 if(highlight_letter1 >= row_begin && highlight_letter1 < row_end)
404 highlight_x1 = get_text_width(font, text_row, highlight_letter1 - row_begin);
408 if(highlight_letter2 > row_begin && highlight_letter2 <= row_end)
409 highlight_x2 = get_text_width(font, text_row, highlight_letter2 - row_begin);
411 highlight_x2 = get_w();
413 draw_box(highlight_x1 + text_x,
415 highlight_x2 - highlight_x1,
419 // Draw text over highlight
421 set_color(resources->text_default);
425 draw_text(text_x, k + text_ascent, text_row);
427 // Get ibeam location
428 if(ibeam_letter >= row_begin && ibeam_letter <= row_end)
431 ibeam_y = k - text_y;
432 ibeam_x = get_text_width(font, text_row, ibeam_letter - row_begin);
437 //printf("BC_TextBox::draw 3 %d\n", ibeam_y);
444 //printf("BC_TextBox::draw 4 %d\n", ibeam_y);
455 int BC_TextBox::focus_in_event()
461 int BC_TextBox::focus_out_event()
467 int BC_TextBox::cursor_enter_event()
469 if(top_level->event_win == win && enabled)
484 int BC_TextBox::cursor_leave_event()
497 int BC_TextBox::button_press_event()
499 if(get_buttonpress() > 2) return 0;
501 int cursor_letter = 0;
502 int text_len = strlen(text);
504 if(!enabled) return 0;
506 if(top_level->event_win == win)
511 top_level->deactivate();
515 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
516 if(get_double_click())
519 select_word(highlight_letter1, highlight_letter2, cursor_letter);
520 highlight_letter3 = highlight_letter1;
521 highlight_letter4 = highlight_letter2;
522 ibeam_letter = highlight_letter2;
523 copy_selection(PRIMARY_SELECTION);
526 if(get_buttonpress() == 2)
528 highlight_letter3 = highlight_letter4 =
529 ibeam_letter = highlight_letter1 =
530 highlight_letter2 = cursor_letter;
531 paste_selection(PRIMARY_SELECTION);
536 highlight_letter3 = highlight_letter4 =
537 ibeam_letter = highlight_letter1 =
538 highlight_letter2 = cursor_letter;
541 if(ibeam_letter < 0) ibeam_letter = 0;
542 if(ibeam_letter > text_len) ibeam_letter = text_len;
549 top_level->deactivate();
555 int BC_TextBox::button_release_event()
560 if(text_selected || word_selected)
569 int BC_TextBox::cursor_motion_event()
571 int cursor_letter, text_len = strlen(text), letter1, letter2;
574 if(text_selected || word_selected)
576 cursor_letter = get_cursor_letter(top_level->cursor_x, top_level->cursor_y);
579 select_word(letter1, letter2, cursor_letter);
584 letter1 = letter2 = cursor_letter;
587 if(letter1 <= highlight_letter3)
589 highlight_letter1 = letter1;
590 highlight_letter2 = highlight_letter4;
591 ibeam_letter = letter1;
594 if(letter2 >= highlight_letter4)
596 highlight_letter2 = letter2;
597 highlight_letter1 = highlight_letter3;
598 ibeam_letter = letter2;
601 copy_selection(PRIMARY_SELECTION);
610 int BC_TextBox::activate()
612 top_level->active_subwindow = this;
615 top_level->set_repeat(top_level->get_resources()->blink_rate);
619 int BC_TextBox::deactivate()
622 top_level->unset_repeat(top_level->get_resources()->blink_rate);
627 int BC_TextBox::repeat_event(int64_t duration)
631 if(duration == top_level->get_resources()->tooltip_delay &&
632 tooltip_text[0] != 0 &&
640 if(duration == top_level->get_resources()->blink_rate &&
644 if(skip_cursor->get_difference() < duration)
646 // printf("BC_TextBox::repeat_event 1 %lld %lld\n",
647 // skip_cursor->get_difference(),
659 void BC_TextBox::default_keypress(int &dispatch_event, int &result)
661 if((top_level->get_keypress() == RETURN) ||
662 // (top_level->get_keypress() > 30 && top_level->get_keypress() < 127))
663 (top_level->get_keypress() > 30 && top_level->get_keypress() <= 255))
665 // Substitute UNIX linefeed
666 if(top_level->get_keypress() == RETURN)
667 temp_string[0] = 0xa;
669 temp_string[0] = top_level->get_keypress();
671 insert_text(temp_string);
679 int BC_TextBox::select_whole_text(int select)
683 highlight_letter1 = 0;
684 highlight_letter2 = strlen(text);
685 text_selected = word_selected = 0;
686 ibeam_letter = highlight_letter1;
688 if(keypress_draw) draw();
692 ibeam_letter = strlen(text);
693 highlight_letter1 = ibeam_letter;
694 highlight_letter2 = ibeam_letter;
695 text_selected = word_selected = 0;
697 if(keypress_draw) draw();
699 return highlight_letter2 - highlight_letter1;
702 void BC_TextBox::cycle_textboxes(int amout)
704 top_level->cycle_textboxes(amout);
707 int BC_TextBox::keypress_event()
709 // Result == 2 contents changed
710 // Result == 1 trapped keypress
711 // Result == 0 nothing
714 int dispatch_event = 0;
716 if(!active || !enabled) return 0;
718 text_len = strlen(text);
719 last_keypress = get_keypress();
720 switch(get_keypress())
723 top_level->deactivate();
730 top_level->deactivate();
736 default_keypress(dispatch_event, result);
739 // Handle like a default keypress
754 int old_ibeam_letter = ibeam_letter;
764 while(ibeam_letter > 0 && isalnum(text[ibeam_letter - 1]))
770 if(top_level->shift_down())
772 // Initialize highlighting
773 if(highlight_letter1 == highlight_letter2)
775 highlight_letter1 = ibeam_letter;
776 highlight_letter2 = old_ibeam_letter;
779 // Extend left highlight
780 if(highlight_letter1 == old_ibeam_letter)
782 highlight_letter1 = ibeam_letter;
785 // Shrink right highlight
786 if(highlight_letter2 == old_ibeam_letter)
788 highlight_letter2 = ibeam_letter;
793 highlight_letter1 = highlight_letter2 = ibeam_letter;
798 if(keypress_draw) draw();
804 if(ibeam_letter < text_len)
806 int old_ibeam_letter = ibeam_letter;
815 while(ibeam_letter < text_len && isalnum(text[ibeam_letter++]))
822 if(top_level->shift_down())
824 // Initialize highlighting
825 if(highlight_letter1 == highlight_letter2)
827 highlight_letter1 = old_ibeam_letter;
828 highlight_letter2 = ibeam_letter;
831 // Shrink left highlight
832 if(highlight_letter1 == old_ibeam_letter)
834 highlight_letter1 = ibeam_letter;
837 // Expand right highlight
838 if(highlight_letter2 == old_ibeam_letter)
840 highlight_letter2 = ibeam_letter;
845 highlight_letter1 = highlight_letter2 = ibeam_letter;
849 if(keypress_draw) draw();
857 //printf("BC_TextBox::keypress_event 1 %d %d %d\n", ibeam_x, ibeam_y, ibeam_letter);
858 int new_letter = get_cursor_letter(ibeam_x + text_x,
859 ibeam_y + text_y - text_height);
860 //printf("BC_TextBox::keypress_event 2 %d %d %d\n", ibeam_x, ibeam_y, new_letter);
863 if(top_level->shift_down())
865 // Initialize highlighting
866 if(highlight_letter1 == highlight_letter2)
868 highlight_letter1 = new_letter;
869 highlight_letter2 = ibeam_letter;
872 // Expand left highlight
873 if(highlight_letter1 == ibeam_letter)
875 highlight_letter1 = new_letter;
878 // Shrink right highlight
879 if(highlight_letter2 == ibeam_letter)
881 highlight_letter2 = new_letter;
885 highlight_letter1 = highlight_letter2 = new_letter;
887 if(highlight_letter1 > highlight_letter2)
889 int temp = highlight_letter1;
890 highlight_letter1 = highlight_letter2;
891 highlight_letter2 = temp;
893 ibeam_letter = new_letter;
896 if(keypress_draw) draw();
904 int new_letter = get_cursor_letter(ibeam_x + text_x,
905 ibeam_y + text_y - get_h());
908 if(top_level->shift_down())
910 // Initialize highlighting
911 if(highlight_letter1 == highlight_letter2)
913 highlight_letter1 = new_letter;
914 highlight_letter2 = ibeam_letter;
917 // Expand left highlight
918 if(highlight_letter1 == ibeam_letter)
920 highlight_letter1 = new_letter;
923 // Shrink right highlight
924 if(highlight_letter2 == ibeam_letter)
926 highlight_letter2 = new_letter;
930 highlight_letter1 = highlight_letter2 = new_letter;
932 if(highlight_letter1 > highlight_letter2)
934 int temp = highlight_letter1;
935 highlight_letter1 = highlight_letter2;
936 highlight_letter2 = temp;
938 ibeam_letter = new_letter;
941 if(keypress_draw) draw();
947 // if(ibeam_letter > 0)
950 int new_letter = get_cursor_letter(ibeam_x + text_x,
951 ibeam_y + text_y + text_height);
952 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
954 if(top_level->shift_down())
956 // Initialize highlighting
957 if(highlight_letter1 == highlight_letter2)
959 highlight_letter1 = new_letter;
960 highlight_letter2 = ibeam_letter;
963 // Shrink left highlight
964 if(highlight_letter1 == ibeam_letter)
966 highlight_letter1 = new_letter;
969 // Expand right highlight
970 if(highlight_letter2 == ibeam_letter)
972 highlight_letter2 = new_letter;
976 highlight_letter1 = highlight_letter2 = new_letter;
978 if(highlight_letter1 > highlight_letter2)
980 int temp = highlight_letter1;
981 highlight_letter1 = highlight_letter2;
982 highlight_letter2 = temp;
984 ibeam_letter = new_letter;
987 if(keypress_draw) draw();
989 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
997 int new_letter = get_cursor_letter(ibeam_x + text_x,
998 ibeam_y + text_y + get_h());
999 //printf("BC_TextBox::keypress_event 10 %d\n", new_letter);
1001 if(top_level->shift_down())
1003 // Initialize highlighting
1004 if(highlight_letter1 == highlight_letter2)
1006 highlight_letter1 = new_letter;
1007 highlight_letter2 = ibeam_letter;
1010 // Shrink left highlight
1011 if(highlight_letter1 == ibeam_letter)
1013 highlight_letter1 = new_letter;
1016 // Expand right highlight
1017 if(highlight_letter2 == ibeam_letter)
1019 highlight_letter2 = new_letter;
1023 highlight_letter1 = highlight_letter2 = new_letter;
1025 if(highlight_letter1 > highlight_letter2)
1027 int temp = highlight_letter1;
1028 highlight_letter1 = highlight_letter2;
1029 highlight_letter2 = temp;
1031 ibeam_letter = new_letter;
1034 if(keypress_draw) draw();
1036 //printf("BC_TextBox::keypress_event 20 %d\n", ibeam_letter);
1043 int old_ibeam_letter = ibeam_letter;
1045 while(ibeam_letter < text_len && text[ibeam_letter] != '\n')
1048 if(top_level->shift_down())
1051 if(highlight_letter1 == highlight_letter2)
1053 highlight_letter2 = ibeam_letter;
1054 highlight_letter1 = old_ibeam_letter;
1058 if(highlight_letter1 == old_ibeam_letter)
1060 highlight_letter1 = highlight_letter2;
1061 highlight_letter2 = ibeam_letter;
1065 if(highlight_letter2 == old_ibeam_letter)
1067 highlight_letter2 = ibeam_letter;
1071 highlight_letter1 = highlight_letter2 = ibeam_letter;
1074 if(keypress_draw) draw();
1081 int old_ibeam_letter = ibeam_letter;
1083 while(ibeam_letter > 0 && text[ibeam_letter - 1] != '\n')
1086 if(top_level->shift_down())
1089 if(highlight_letter1 == highlight_letter2)
1091 highlight_letter2 = old_ibeam_letter;
1092 highlight_letter1 = ibeam_letter;
1096 if(highlight_letter1 == old_ibeam_letter)
1098 highlight_letter1 = ibeam_letter;
1102 if(highlight_letter2 == old_ibeam_letter)
1104 highlight_letter2 = highlight_letter1;
1105 highlight_letter1 = ibeam_letter;
1109 highlight_letter1 = highlight_letter2 = ibeam_letter;
1112 if(keypress_draw) draw();
1118 if(highlight_letter1 == highlight_letter2)
1120 if(ibeam_letter > 0)
1122 delete_selection(ibeam_letter - 1, ibeam_letter, text_len);
1128 delete_selection(highlight_letter1, highlight_letter2, text_len);
1129 highlight_letter2 = ibeam_letter = highlight_letter1;
1133 if(keypress_draw) draw();
1139 if(highlight_letter1 == highlight_letter2)
1141 if(ibeam_letter < text_len)
1143 delete_selection(ibeam_letter, ibeam_letter + 1, text_len);
1148 delete_selection(highlight_letter1, highlight_letter2, text_len);
1149 highlight_letter2 = ibeam_letter = highlight_letter1;
1153 if(keypress_draw) draw();
1163 if(get_keypress() == 'c' || get_keypress() == 'C')
1165 if(highlight_letter1 != highlight_letter2)
1167 copy_selection(SECONDARY_SELECTION);
1172 if(get_keypress() == 'v' || get_keypress() == 'V')
1174 paste_selection(SECONDARY_SELECTION);
1176 if(keypress_draw) draw();
1181 if(get_keypress() == 'x' || get_keypress() == 'X')
1183 if(highlight_letter1 != highlight_letter2)
1185 copy_selection(SECONDARY_SELECTION);
1186 delete_selection(highlight_letter1, highlight_letter2, text_len);
1187 highlight_letter2 = ibeam_letter = highlight_letter1;
1191 if(keypress_draw) draw();
1199 default_keypress(dispatch_event, result);
1203 if(dispatch_event) skip_cursor->update();
1204 if(dispatch_event) handle_event();
1205 //printf("BC_TextBox::keypress_event 100 %d\n", ibeam_letter);
1211 int BC_TextBox::uses_text()
1216 void BC_TextBox::delete_selection(int letter1, int letter2, int text_len)
1220 for(i = letter1, j = letter2; j < text_len; i++, j++)
1229 void BC_TextBox::insert_text(char *string)
1231 int i, j, text_len, string_len;
1233 string_len = strlen(string);
1234 text_len = strlen(text);
1235 if(highlight_letter1 < highlight_letter2)
1237 delete_selection(highlight_letter1, highlight_letter2, text_len);
1238 highlight_letter2 = ibeam_letter = highlight_letter1;
1241 text_len = strlen(text);
1243 for(i = text_len, j = text_len + string_len; i >= ibeam_letter; i--, j--)
1246 for(i = ibeam_letter, j = 0; j < string_len; j++, i++)
1247 text[i] = string[j];
1249 ibeam_letter += string_len;
1254 void BC_TextBox::do_separators(int ibeam_left)
1258 // Remove separators from text
1259 int text_len = strlen(text);
1260 int separator_len = strlen(separators);
1261 for(int i = 0; i < text_len; i++)
1263 if(!isalnum(text[i]))
1265 for(int j = i; j < text_len - 1; j++)
1266 text[j] = text[j + 1];
1267 if(!ibeam_left && i < ibeam_letter) ibeam_letter--;
1279 // Insert separators into text
1280 for(int i = 0; i < separator_len; i++)
1284 // Insert a separator
1285 if(!isalnum(separators[i]))
1287 for(int j = text_len; j >= i; j--)
1289 text[j + 1] = text[j];
1291 if(!ibeam_left && i < ibeam_letter) ibeam_letter++;
1293 text[i] = separators[i];
1299 text[i] = separators[i];
1304 text[separator_len] = 0;
1309 void BC_TextBox::get_ibeam_position(int &x, int &y)
1311 int i, j, k, row_begin, row_end, text_len;
1313 text_len = strlen(text);
1316 for(i = 0; i < text_len; )
1319 for(j = 0; text[i] != '\n' && i < text_len; j++, i++)
1321 text_row[j] = text[i];
1327 if(ibeam_letter >= row_begin && ibeam_letter <= row_end)
1329 x = get_text_width(font, text_row, ibeam_letter - row_begin);
1330 //printf("BC_TextBox::get_ibeam_position 9 %d %d\n", x, y);
1340 //printf("BC_TextBox::get_ibeam_position 10 %d %d\n", x, y);
1346 void BC_TextBox::set_text_row(int row)
1348 text_y = -(row * text_height) + top_margin;
1352 int BC_TextBox::get_text_row()
1354 return -(text_y - top_margin) / text_height;
1357 void BC_TextBox::find_ibeam(int dispatch_event)
1360 int old_x = text_x, old_y = text_y;
1362 get_ibeam_position(x, y);
1364 if(left_margin + text_x + x >= get_w() - right_margin - BCCURSORW)
1366 text_x = -(x - (get_w() - get_w() / 4)) + left_margin;
1367 if(text_x > left_margin) text_x = left_margin;
1370 if(left_margin + text_x + x < left_margin)
1372 text_x = -(x - (get_w() / 4)) + left_margin;
1373 if(text_x > left_margin) text_x = left_margin;
1376 while(y + text_y >= get_h() - text_height - bottom_margin)
1378 text_y -= text_height;
1379 // text_y = -(y - (get_h() / 2)) + top_margin;
1380 // if(text_y > top_margin) text_y = top_margin;
1383 while(y + text_y < top_margin)
1385 text_y += text_height;
1386 if(text_y > top_margin)
1388 text_y = top_margin;
1393 if(dispatch_event && (old_x != text_x || old_y != text_y)) motion_event();
1396 int BC_TextBox::get_cursor_letter(int cursor_x, int cursor_y)
1398 int i, j, k, l, row_begin, row_end, text_len, result = 0, done = 0;
1399 text_len = strlen(text);
1401 if(cursor_y < text_y)
1407 for(i = 0, k = text_y; i < text_len && !done; k += text_height)
1410 for(j = 0; text[i] != '\n' && i < text_len; j++, i++)
1412 text_row[j] = text[i];
1417 if(cursor_y >= k && cursor_y < k + text_height)
1419 for(j = 0; j <= row_end - row_begin && !done; j++)
1421 l = get_text_width(font, text_row, j) + text_x;
1424 result = row_begin + j - 1;
1434 if(text[i] == '\n') i++;
1436 if(i >= text_len && !done)
1441 if(result < 0) result = 0;
1442 if(result > text_len) result = text_len;
1446 void BC_TextBox::select_word(int &letter1, int &letter2, int ibeam_letter)
1448 int text_len = strlen(text);
1449 letter1 = letter2 = ibeam_letter;
1452 if(isalnum(text[letter1])) letter1--;
1453 }while(letter1 > 0 && isalnum(text[letter1]));
1454 if(!isalnum(text[letter1])) letter1++;
1458 if(isalnum(text[letter2])) letter2++;
1459 }while(letter2 < text_len && isalnum(text[letter2]));
1460 if(letter2 < text_len && text[letter2] == ' ') letter2++;
1462 if(letter1 < 0) letter1 = 0;
1463 if(letter2 < 0) letter2 = 0;
1464 if(letter1 > text_len) letter1 = text_len;
1465 if(letter2 > text_len) letter2 = text_len;
1468 void BC_TextBox::copy_selection(int clipboard_num)
1470 int text_len = strlen(text);
1472 if(highlight_letter1 >= text_len ||
1473 highlight_letter2 > text_len ||
1474 highlight_letter1 < 0 ||
1475 highlight_letter2 < 0 ||
1476 highlight_letter2 - highlight_letter1 <= 0) return;
1478 get_clipboard()->to_clipboard(&text[highlight_letter1],
1479 highlight_letter2 - highlight_letter1,
1483 void BC_TextBox::paste_selection(int clipboard_num)
1485 int len = get_clipboard()->clipboard_len(clipboard_num);
1488 char *string = new char[len + 1];
1489 get_clipboard()->from_clipboard(string, len, clipboard_num);
1490 insert_text(string);
1494 void BC_TextBox::set_keypress_draw(int value)
1496 keypress_draw = value;
1499 int BC_TextBox::get_last_keypress()
1501 return last_keypress;
1504 int BC_TextBox::get_ibeam_letter()
1506 return ibeam_letter;
1509 void BC_TextBox::set_ibeam_letter(int number, int redraw)
1511 this->ibeam_letter = number;
1518 void BC_TextBox::set_separators(char *separators)
1520 this->separators = separators;
1538 BC_ScrollTextBox::BC_ScrollTextBox(BC_WindowBase *parent_window,
1545 this->parent_window = parent_window;
1550 this->default_text = default_text;
1553 BC_ScrollTextBox::~BC_ScrollTextBox()
1563 void BC_ScrollTextBox::create_objects()
1565 // Must be created first
1566 parent_window->add_subwindow(text = new BC_ScrollTextBoxText(this));
1567 parent_window->add_subwindow(yscroll = new BC_ScrollTextBoxYScroll(this));
1570 int BC_ScrollTextBox::handle_event()
1575 int BC_ScrollTextBox::get_x()
1580 int BC_ScrollTextBox::get_y()
1585 int BC_ScrollTextBox::get_w()
1590 int BC_ScrollTextBox::get_rows()
1596 char* BC_ScrollTextBox::get_text()
1598 return text->get_text();
1601 void BC_ScrollTextBox::update(char *text)
1603 this->text->update(text);
1604 yscroll->update_length(this->text->get_text_rows(),
1605 this->text->get_text_row(),
1606 yscroll->get_handlelength());
1609 void BC_ScrollTextBox::reposition_window(int x, int y, int w, int rows)
1615 text->reposition_window(x,
1617 w - yscroll->get_span(),
1619 yscroll->reposition_window(x + w - yscroll->get_span(),
1621 BC_TextBox::calculate_row_h(rows,
1623 yscroll->update_length(text->get_text_rows(),
1624 text->get_text_row(),
1636 BC_ScrollTextBoxText::BC_ScrollTextBoxText(BC_ScrollTextBox *gui)
1637 : BC_TextBox(gui->x,
1639 gui->w - get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
1646 BC_ScrollTextBoxText::~BC_ScrollTextBoxText()
1655 int BC_ScrollTextBoxText::handle_event()
1657 gui->yscroll->update_length(get_text_rows(),
1659 gui->yscroll->get_handlelength());
1660 return gui->handle_event();
1663 int BC_ScrollTextBoxText::motion_event()
1665 gui->yscroll->update_length(get_text_rows(),
1667 gui->yscroll->get_handlelength());
1672 BC_ScrollTextBoxYScroll::BC_ScrollTextBoxYScroll(BC_ScrollTextBox *gui)
1673 : BC_ScrollBar(gui->x +
1675 get_resources()->vscroll_data[SCROLL_HANDLE_UP]->get_w(),
1678 BC_TextBox::calculate_row_h(gui->rows,
1679 gui->parent_window),
1680 gui->text->get_text_rows(),
1687 BC_ScrollTextBoxYScroll::~BC_ScrollTextBoxYScroll()
1691 int BC_ScrollTextBoxYScroll::handle_event()
1693 gui->text->set_text_row(get_position());
1706 BC_PopupTextBoxText::BC_PopupTextBoxText(BC_PopupTextBox *popup, int x, int y)
1707 : BC_TextBox(x, y, popup->text_w, 1, popup->default_text)
1709 this->popup = popup;
1712 BC_PopupTextBoxText::~BC_PopupTextBoxText()
1723 int BC_PopupTextBoxText::handle_event()
1725 popup->handle_event();
1729 BC_PopupTextBoxList::BC_PopupTextBoxList(BC_PopupTextBox *popup, int x, int y)
1732 popup->text_w + BC_WindowBase::get_resources()->listbox_button[0]->get_w(),
1742 this->popup = popup;
1744 int BC_PopupTextBoxList::handle_event()
1746 BC_ListBoxItem *item = get_selection(0, 0);
1749 popup->textbox->update(item->get_text());
1750 popup->handle_event();
1758 BC_PopupTextBox::BC_PopupTextBox(BC_WindowBase *parent_window,
1759 ArrayList<BC_ListBoxItem*> *list_items,
1768 this->list_h = list_h;
1769 this->default_text = default_text;
1770 this->text_w = text_w;
1771 this->parent_window = parent_window;
1772 this->list_items = list_items;
1775 BC_PopupTextBox::~BC_PopupTextBox()
1785 int BC_PopupTextBox::create_objects()
1787 int x = this->x, y = this->y;
1788 parent_window->add_subwindow(textbox = new BC_PopupTextBoxText(this, x, y));
1789 x += textbox->get_w();
1790 parent_window->add_subwindow(listbox = new BC_PopupTextBoxList(this, x, y));
1794 void BC_PopupTextBox::update(char *text)
1796 textbox->update(text);
1799 void BC_PopupTextBox::update_list(ArrayList<BC_ListBoxItem*> *data)
1801 listbox->update(data,
1808 char* BC_PopupTextBox::get_text()
1810 return textbox->get_text();
1813 int BC_PopupTextBox::get_number()
1815 return listbox->get_selection_number(0, 0);
1818 int BC_PopupTextBox::get_x()
1823 int BC_PopupTextBox::get_y()
1828 int BC_PopupTextBox::get_w()
1830 return textbox->get_w() + listbox->get_w();
1833 int BC_PopupTextBox::get_h()
1835 return textbox->get_h();
1838 int BC_PopupTextBox::handle_event()
1843 int BC_PopupTextBox::reposition_widget(int x, int y, int w, int h) {
1844 reposition_window(x, y);
1848 void BC_PopupTextBox::reposition_window(int x, int y)
1853 textbox->reposition_window(x1, y1);
1854 x1 += textbox->get_w();
1855 listbox->reposition_window(x1, y1);
1871 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
1872 int64_t default_value,
1883 this->popup = popup;
1886 BC_TumbleTextBoxText::BC_TumbleTextBoxText(BC_TumbleTextBox *popup,
1887 float default_value,
1898 this->popup = popup;
1901 BC_TumbleTextBoxText::~BC_TumbleTextBoxText()
1913 int BC_TumbleTextBoxText::handle_event()
1915 popup->handle_event();
1919 int BC_TumbleTextBoxText::button_press_event()
1923 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1924 if(get_buttonpress() == 4)
1926 popup->tumbler->handle_up_event();
1929 if(get_buttonpress() == 5)
1931 popup->tumbler->handle_down_event();
1941 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
1942 int64_t default_value,
1954 this->default_value = default_value;
1955 this->text_w = text_w;
1956 this->parent_window = parent_window;
1960 log_floatincrement = 0;
1963 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
1976 this->default_value = default_value;
1977 this->text_w = text_w;
1978 this->parent_window = parent_window;
1982 log_floatincrement = 0;
1985 BC_TumbleTextBox::BC_TumbleTextBox(BC_WindowBase *parent_window,
1986 float default_value_f,
1996 this->min_f = min_f;
1997 this->max_f = max_f;
1998 this->default_value_f = default_value_f;
1999 this->text_w = text_w;
2000 this->parent_window = parent_window;
2004 log_floatincrement = 0;
2007 BC_TumbleTextBox::~BC_TumbleTextBox()
2009 // Recursive delete. Normally ~BC_TumbleTextBox is never called but textbox
2010 // is deleted anyway by the windowbase so textbox deletes this.
2011 if(tumbler) delete tumbler;
2013 // Don't delete text here if we were called by ~BC_TumbleTextBoxText
2022 void BC_TumbleTextBox::reset()
2027 log_floatincrement = 0;
2030 void BC_TumbleTextBox::set_precision(int precision)
2032 this->precision = precision;
2035 void BC_TumbleTextBox::set_increment(float value)
2037 this->increment = value;
2038 if(tumbler) tumbler->set_increment(value);
2041 void BC_TumbleTextBox::set_log_floatincrement(int value)
2043 this->log_floatincrement = value;
2044 if(tumbler) tumbler->set_log_floatincrement(value);
2047 int BC_TumbleTextBox::create_objects()
2049 int x = this->x, y = this->y;
2053 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2059 textbox->set_precision(precision);
2062 parent_window->add_subwindow(textbox = new BC_TumbleTextBoxText(this,
2069 x += textbox->get_w();
2072 parent_window->add_subwindow(tumbler = new BC_FTumbler(textbox,
2078 parent_window->add_subwindow(tumbler = new BC_ITumbler(textbox,
2084 tumbler->set_increment(increment);
2085 tumbler->set_log_floatincrement(log_floatincrement);
2089 char* BC_TumbleTextBox::get_text()
2091 return textbox->get_text();
2094 int BC_TumbleTextBox::update(char *value)
2096 textbox->update(value);
2100 int BC_TumbleTextBox::update(int64_t value)
2102 textbox->update(value);
2106 int BC_TumbleTextBox::update(float value)
2108 textbox->update(value);
2113 int BC_TumbleTextBox::get_x()
2118 int BC_TumbleTextBox::get_y()
2123 int BC_TumbleTextBox::get_w()
2125 return textbox->get_w() + tumbler->get_w();
2128 int BC_TumbleTextBox::get_h()
2130 return textbox->get_h();
2133 int BC_TumbleTextBox::handle_event()
2138 int BC_TumbleTextBox::reposition_widget(int x, int y, int w, int h) {
2139 reposition_window(x, y);
2143 void BC_TumbleTextBox::reposition_window(int x, int y)
2148 textbox->reposition_window(x,
2152 tumbler->reposition_window(x + textbox->get_w(),
2157 void BC_TumbleTextBox::set_boundaries(int64_t min, int64_t max)
2159 tumbler->set_boundaries(min, max);
2162 void BC_TumbleTextBox::set_boundaries(float min, float max)
2164 tumbler->set_boundaries(min, max);